Sign in to follow this  
Migi0027

DX11 DX11 - Tessellation - Something is wrong

Recommended Posts

Migi0027    4630

Hi guys!

 

I recently just started implementing tessellation for my terrain system, but it isn't quite working as I expected, nothing shows up. Further down I am going to give you some code which includes the tessellation, nothing fancy, as I said, I just got started. I don't expect you to read it all, just that if you spot something you find weird/wrong, please say so as it may help me.

 

Without implementing the tessellation, the terrain renders perfectly, but when trying to implement the tessellation, nothing is shown. PS. One interesting thing is that when rendering without tessellation, I use: D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, but when tessellating, I understood that I had to use D3D11_PRIMITIVE_TOPOLOGY_3_CONTROL_POINT_PATCHLIST, well according to my shader file.

 

Shader (Simple):

cbuffer ConstantObjectBuffer : register (b0)
{
	matrix worldMatrix;
};

cbuffer ConstantFrameBuffer : register (b1)
{
	matrix viewMatrix;
	matrix projectionMatrix;

	float3 eyepos;
	float cppad;

	float4 lightvec;
	float4 lightcol;

	float FogStart;
	float FogEnd;
	float2 __space;

	float3 FogColor;
	float shadows;

	float SpecularIntensity;
	float3 pad3;
	float4 SpecularColor;
}


//***************************************************//
//                 VERTEX SHADER                     //
//***************************************************//

struct VOut
{
    float4 position : SV_POSITION;
	float2 texcoord : TEXCOORD;
	float access : ACCESS;
	
	float3 NormalW : NORMWORLD;
	float4 depthPosition : TEXTURE0;
};

struct GlobalIn
{
	float4 position : POSITION;
	float4 normal : NORMAL;
	float2 texcoord : TEXCOORD;
};

Texture2D t_map : register(t0);
SamplerState ss;

VOut VShader(GlobalIn input)
{
    VOut output;

    input.position.w = 1.0f;
	output.texcoord = input.texcoord;

	// Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    output.NormalW = mul(float4(input.normal.xyz,0), mul(worldMatrix, viewMatrix));

	// Store the position value in a second input value for depth value calculations.
	output.depthPosition.xyz = mul(float4(input.position.xyz,1), mul(worldMatrix, viewMatrix)).xyz;

	// Per Vertex lighting
	float4 norm = normalize(input.normal);
	output.access = saturate(dot(norm, lightvec));
	
    return output;
}

//***************************************************//
//                 HULL SHADER                       //
//***************************************************//

struct HOutput
{
    float edges[3] : SV_TessFactor;
    float inside : SV_InsideTessFactor;
};

#define tessellationAmount 12.0f

HOutput ColorPatchConstantFunction(InputPatch<VOut, 3> inputPatch, uint patchId : SV_PrimitiveID)
{    
    HOutput output;

    // Set the tessellation factors for the three edges of the triangle.
    output.edges[0] = 12.0f;
    output.edges[1] = 12.0f;
    output.edges[2] = 12.0f;

    // Set the tessellation factor for tessallating inside the triangle.
    output.inside = 12.0f;

    return output;
}

[domain("tri")]
[partitioning("integer")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("ColorPatchConstantFunction")]

VOut HShader(InputPatch<VOut, 3> patch, uint pointId : SV_OutputControlPointID, uint patchId : SV_PrimitiveID)
{
    VOut output;

    // Set the x for this control point as the output x.
    output.position = patch[pointId].position;

    output.texcoord = patch[pointId].texcoord;
    output.access = patch[pointId].access;
    output.NormalW = patch[pointId].NormalW;
    output.depthPosition = patch[pointId].depthPosition;

    return output;
}

//***************************************************//
//                 DOMAIN SHADER                     //
//***************************************************//

[domain("tri")]

VOut DShader(HOutput input, float3 uvwCoord : SV_DomainLocation, const OutputPatch<VOut, 3> patch)
{
    float3 vertexPosition;
    VOut output;
 
    // Determine the position of the new vertex.
    vertexPosition = uvwCoord.x * patch[0].position + uvwCoord.y * patch[1].position + uvwCoord.z * patch[2].position;
    
    // Calculate the position of the new vertex against the world, view, and projection matrices.
    output.position = mul(float4(vertexPosition, 1.0f), worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    // Send the input color into the pixel shader.
    output.texcoord = patch[0].texcoord;
    output.access = patch[0].access;
    output.NormalW = patch[0].NormalW;
    output.depthPosition = patch[0].depthPosition;

    return output;
}

//***************************************************//
//                 PIXEL SHADER                      //
//***************************************************//

struct POut
{
	float4 Diffuse  : SV_Target0;
	float4 Depth    : SV_Target1;
	float4 Normals  : SV_Target2;
	float4 Lighting : SV_Target3;
};

POut PShader(VOut input)
{
	POut output;

	// Depth
	output.Depth = float4(input.depthPosition.xyz, 1.0f);

	// Normals
	output.Normals = float4(normalize(input.NormalW), 1);

	output.Diffuse = float4(1, 1, 1, 1);
	output.Lighting = float4(lightcol.rgb * input.access, 1.0f);

	output.Lighting = float4(1, 1, 1, 1);

	return output;
}

C++:

 

Before Rendering:

devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_3_CONTROL_POINT_PATCHLIST);

Shader Creation:

D3DX11CompileFromFile(finals.c_str(), 0, 0, "VShader", "vs_5_0", CE_DBG, 0, 0, &VS, &vserrors, &HR);
D3DX11CompileFromFile(finals.c_str(), 0, 0, "PShader", "ps_5_0", CE_DBG, 0, 0, &PS, &pserrors, &HR);
D3DX11CompileFromFile(finals.c_str(), 0, 0, "HShader", "hs_5_0", CE_DBG, 0, 0, &HS, &hserrors, &HR);
D3DX11CompileFromFile(finals.c_str(), 0, 0, "DShader", "ds_5_0", CE_DBG, 0, 0, &DS, &dserrors, &HR);

// Error Checking

dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
dev->CreateHullShader(HS->GetBufferPointer(), HS->GetBufferSize(), NULL, &pHS);
dev->CreateDomainShader(DS->GetBufferPointer(), DS->GetBufferSize(), NULL, &pDS);
 
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
 
if (dev->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout) != S_OK)
CE_WARNING("Input Layout Creation", "Input Layout creation in terrain shader has failed!");

Applying Shader:

devcon->VSSetShader(pVS, 0, 0);
devcon->HSSetShader(pHS, 0, 0);
devcon->DSSetShader(pDS, 0, 0);
devcon->PSSetShader(pPS, 0, 0);
devcon->IASetInputLayout(pLayout);

Rendering: 

// Set constant buffers

devcon->DrawIndexed(bf.IndexCount, 0, 0);

PIX:

2cqldzc.png

 

 

I hope that I have supplied you with enough information, as I haven't found the solution yet, but is still trying. happy.png

 

Thank you, as always GameDev.

Edited by Migi0027

Share this post


Link to post
Share on other sites
MJP    19786

I know this problem because it happened to me when I first implemented tessellation! You can't use SV_Position to pass values between VS/HS and HS/DS. You can only use it for outputting from your domain shader. For other shader stages, you should use a non-SV semantic for passing position.

Share this post


Link to post
Share on other sites
Migi0027    4630

So is this the correct way?:

cbuffer ConstantObjectBuffer : register (b0)
{
	matrix worldMatrix;
};

cbuffer ConstantFrameBuffer : register (b1)
{
	matrix viewMatrix;
	matrix projectionMatrix;

	float3 eyepos;
	float cppad;

	float4 lightvec;
	float4 lightcol;

	float FogStart;
	float FogEnd;
	float2 __space;

	float3 FogColor;
	float shadows;

	float SpecularIntensity;
	float3 pad3;
	float4 SpecularColor;
}


//***************************************************//
//                 VERTEX SHADER                     //
//***************************************************//

struct VOut
{
    float4 position : POSITION;
	float2 texcoord : TEXCOORD;
	float access : ACCESS;
	
	float3 NormalW : NORMWORLD;
	float4 depthPosition : TEXTURE0;
};

struct GlobalIn
{
	float4 position : POSITION;
	float4 normal : NORMAL;
	float2 texcoord : TEXCOORD;
};

Texture2D t_map : register(t0);
SamplerState ss;

VOut VShader(GlobalIn input)
{
    VOut output;

    input.position.w = 1.0f;
	output.texcoord = input.texcoord;

	// Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    output.NormalW = mul(float4(input.normal.xyz,0), mul(worldMatrix, viewMatrix));

	// Store the position value in a second input value for depth value calculations.
	output.depthPosition.xyz = mul(float4(input.position.xyz,1), mul(worldMatrix, viewMatrix)).xyz;

	// Per Vertex lighting
	float4 norm = normalize(input.normal);
	output.access = saturate(dot(norm, lightvec));
	
    return output;
}

//***************************************************//
//                 HULL SHADER                       //
//***************************************************//

struct HOutput
{
    float edges[3] : SV_TessFactor;
    float inside : SV_InsideTessFactor;
};

#define tessellationAmount 12.0f

HOutput ColorPatchConstantFunction(InputPatch<VOut, 3> inputPatch, uint patchId : SV_PrimitiveID)
{    
    HOutput output;

    // Set the tessellation factors for the three edges of the triangle.
    output.edges[0] = 12.0f;
    output.edges[1] = 12.0f;
    output.edges[2] = 12.0f;

    // Set the tessellation factor for tessallating inside the triangle.
    output.inside = 12.0f;

    return output;
}

[domain("tri")]
[partitioning("integer")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("ColorPatchConstantFunction")]

VOut HShader(InputPatch<VOut, 3> patch, uint pointId : SV_OutputControlPointID, uint patchId : SV_PrimitiveID)
{
    VOut output;

    // Set the x for this control point as the output x.
    output.position = patch[pointId].position;

    output.texcoord = patch[pointId].texcoord;
    output.access = patch[pointId].access;
    output.NormalW = patch[pointId].NormalW;
    output.depthPosition = patch[pointId].depthPosition;

    return output;
}

//***************************************************//
//                 DOMAIN SHADER                     //
//***************************************************//

struct DOut
{
    float4 position : SV_Position;
	float2 texcoord : TEXCOORD;
	float access : ACCESS;
	
	float3 NormalW : NORMWORLD;
	float4 depthPosition : TEXTURE0;
};

[domain("tri")]

DOut DShader(HOutput input, float3 uvwCoord : SV_DomainLocation, const OutputPatch<VOut, 3> patch)
{
    float3 vertexPosition;
    DOut output;
 
    // Determine the position of the new vertex.
    vertexPosition = uvwCoord.x * patch[0].position + uvwCoord.y * patch[1].position + uvwCoord.z * patch[2].position;
    
    // Calculate the position of the new vertex against the world, view, and projection matrices.
    output.position = mul(float4(vertexPosition, 1.0f), worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    // Send the input color into the pixel shader.
    output.texcoord = patch[0].texcoord;
    output.access = patch[0].access;
    output.NormalW = patch[0].NormalW;
    output.depthPosition = patch[0].depthPosition;

    return output;
}

//***************************************************//
//                 PIXEL SHADER                      //
//***************************************************//

struct POut
{
	float4 Diffuse  : SV_Target0;
	float4 Depth    : SV_Target1;
	float4 Normals  : SV_Target2;
	float4 Lighting : SV_Target3;
};

POut PShader(VOut input)
{
	POut output;

	// Depth
	output.Depth = float4(input.depthPosition.xyz, 1.0f);

	// Normals
	output.Normals = float4(normalize(input.NormalW), 1);

	output.Diffuse = float4(1, 1, 1, 1);
	output.Lighting = float4(lightcol.rgb * input.access, 1.0f);

	output.Lighting = float4(1, 1, 1, 1);

	return output;
}

Well, that fixed one issue, now the other, still can't see anything. biggrin.png

 

But thanks MJP.

Share this post


Link to post
Share on other sites
unbird    8338
You're transforming your vertices twice, once in the vertex, once in the domain shader. You rather e.g. use a pass-through vertex shader - or transform to view or world space only. Depends on what you're doing. I recommend using view space if you later going to determine the tesselation factors according to (view) distance.

Projection transform should only happen at the last stage (before the pixel shader). In particular, sub-tesselation in clip space is probably very wrong (domain shader).

You also should interpolate the other attributes (normal, texcoord, etc.) otherwise you get wrong results.

Since PIX is quite reluctant concerning the tesselation stage you can use a pass-through geometry shader to inspect the attributes more easily.

Share this post


Link to post
Share on other sites
Migi0027    4630

Thank you both MJP and unbird for correcting me.

 

It Works!

 

And it looks like I forgot to send the matrices to the domain shader as well.

 

Best Regards

Migi0027

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
    • By fighting_falcon93
      Imagine that we have a vertex structure that looks like this:
      struct Vertex { XMFLOAT3 position; XMFLOAT4 color; }; The vertex shader looks like this:
      cbuffer MatrixBuffer { matrix world; matrix view; matrix projection; }; struct VertexInput { float4 position : POSITION; float4 color : COLOR; }; struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInput main(VertexInput input) { PixelInput output; input.position.w = 1.0f; output.position = mul(input.position, world); output.position = mul(output.position, view); output.position = mul(output.position, projection); output.color = input.color; return output; } And the pixel shader looks like this:
      struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; float4 main(PixelInput input) : SV_TARGET { return input.color; } Now let's create a quad consisting of 2 triangles and the vertices A, B, C and D:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex B. vertices[1].position = XMFLOAT3( 1.0f, 1.0f, 0.0f); vertices[1].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex D. vertices[3].position = XMFLOAT3( 1.0f, -1.0f, 0.0f); vertices[3].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // 1st triangle. indices[0] = 0; // Vertex A. indices[1] = 3; // Vertex D. indices[2] = 2; // Vertex C. // 2nd triangle. indices[3] = 0; // Vertex A. indices[4] = 1; // Vertex B. indices[5] = 3; // Vertex D. This will result in a grey quad as shown in the image below. I've outlined the edges in red color to better illustrate the triangles:

      Now imagine that we’d want our quad to have a different color in vertex A:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      That works as expected since there’s now an interpolation between the black color in vertex A and the grey color in vertices B, C and D. Let’s revert the previus changes and instead change the color of vertex C:
      // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      As you can see, the interpolation is only done half of the way across the first triangle and not across the entire quad. This is because there's no edge between vertex C and vertex B.
      Which brings us to my question:
      I want the interpolation to go across the entire quad and not only across the triangle. So regardless of which vertex we decide to change the color of, the color interpolation should always go across the entire quad. Is there any efficient way of achieving this without adding more vertices and triangles?
      An illustration of what I'm trying to achieve is shown in the image below:

       
      Background
      This is just a very brief explanation of the problems background in case that would make it easier for you to understand the problems roots and maybe help you with finding a better solution to the problem.
      I'm trying to texture a terrain mesh in DirectX11. It's working, but I'm a bit unsatisfied with the result. When changing the terrain texture of a single vertex, the interpolation with the other vertices results in a hexagon shape instead of a squared shape:

      As the red arrows illustrate, I'd like the texture to be interpolated all the way into the corners of the quads.
    • By -Tau-
      Hello, I'm close to releasing my first game to Steam however, my game keeps failing the review process because it keeps crashing. The problem is that the game doesn't crash on my computer, on my laptop, on our family computer, on fathers laptop and i also gave 3 beta keys to people i know and they said the game hasn't crashed.
      Steam reports that the game doesn't crash on startup but few frames after a level has been started.
      What could cause something like this? I have no way of debugging this as the game works fine on every computer i have.
       
      Game is written in C++, using DirectX 11 and DXUT framework.
    • By haiiry
      I'm trying to get, basically, screenshot (each 1 second, without saving) of Direct3D11 application. Code works fine on my PC(Intel CPU, Radeon GPU) but crashes after few iterations on 2 others (Intel CPU + Intel integrated GPU, Intel CPU + Nvidia GPU).
      void extractBitmap(void* texture) { if (texture) { ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture; ID3D11Texture2D* pNewTexture = NULL; D3D11_TEXTURE2D_DESC desc; d3dtex->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; desc.Usage = D3D11_USAGE_STAGING; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; HRESULT hRes = D3D11Device->CreateTexture2D(&desc, NULL, &pNewTexture); if (FAILED(hRes)) { printCon(std::string("CreateTexture2D FAILED:" + format_error(hRes)).c_str()); if (hRes == DXGI_ERROR_DEVICE_REMOVED) printCon(std::string("DXGI_ERROR_DEVICE_REMOVED -- " + format_error(D3D11Device->GetDeviceRemovedReason())).c_str()); } else { if (pNewTexture) { D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); // Wokring with texture pNewTexture->Release(); } } } return; } D3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pBackBuffer)); extractBitmap(pBackBuffer); pBackBuffer->Release(); Crash log:
      CreateTexture2D FAILED:887a0005 DXGI_ERROR_DEVICE_REMOVED -- 887a0020 Once I comment out 
      D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); 
      code works fine on all 3 PC's.
    • By Fluffy10
      Hi i'm new to this forum and was wondering if there are any good places to start learning directX 11. I bought Frank D Luna's book but it's really outdated and the projects won't even compile. I was excited to start learning from this book because it gives detailed explanations on the functions being used as well as the mathematics. Are there any tutorials / courses /books that are up to date which goes over the 3D math and functions in a detailed manner? Or where does anyone here learn directX 11? I've followed some tutorials from this website http://www.directxtutorial.com/LessonList.aspx?listid=11 which did a nice job but it doesn't explain what's happening with the math so I feel like I'm not actually learning, and it only goes up until color blending. Rasteriks tutorials doesn't go over the functions much at all or the math involved either. I'd really appreciate it if anyone can point me in the right direction, I feel really lost. Thank you
  • Popular Now