DirectX 11 Tesellation 着色器不工作

DirectX 11 Tesellation Shader Not Working

我的曲面细分着色器有问题。它在我不使用 Hull ShaderDomain Shader 时呈现。只是普通的顶点和像素着色器工作正常。这是我的 VS 和 PS 着色器:

VOut VShader(float4 position : POSITION, float4 color : COLOR) 
{
VOut output;
output.position = mul(world, position);
output.color = color;
return output;  
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}

我正在使用正交投影将像素映射到它们的原始位置。这是有问题的着色器。其中输入 float3 个顶点和 float4 个颜色。

cbuffer cbPerFrame : register(b0) {
    matrix world; };

struct VS_CONTROL_POINT_INPUT {
    float3 vPosition        : POSITION; };

struct VS_CONTROL_POINT_OUTPUT {
    float3 vPosition        : POSITION; };

struct HS_CONSTANT_DATA_OUTPUT {
    float Edges[3]          : SV_TessFactor;
    float Inside            : SV_InsideTessFactor; };

struct HS_OUTPUT {
    float3 vPosition           : POSITION; };

HS_CONSTANT_DATA_OUTPUT ConstantHS(InputPatch<VS_CONTROL_POINT_OUTPUT, 3> ip,uint PatchID : SV_PrimitiveID) {
    HS_CONSTANT_DATA_OUTPUT Output;
    Output.Edges[0] = Output.Edges[1] = Output.Edges[2] = 4;
    Output.Inside = 4;
    return Output; }

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

HS_OUTPUT HShader(InputPatch<VS_CONTROL_POINT_OUTPUT, 3> p, uint i : SV_OutputControlPointID, uint PatchID : SV_PrimitiveID)

 {
        HS_OUTPUT Output;
        Output.vPosition = p[i].vPosition;
        return Output;
 }

struct DS_OUTPUT {
    float4 vPosition        : SV_POSITION; };

[domain("tri")]
 DS_OUTPUT DShader(HS_CONSTANT_DATA_OUTPUT input, float3 UVW : SV_DomainLocation, const OutputPatch<HS_OUTPUT, 3> quad) {
        DS_OUTPUT Output;
        float3 finalPos = UVW.x * quad[0].vPosition + UVW.y * quad[1].vPosition + UVW.z * quad[2].vPosition;
        Output.vPosition = mul(world,float4(finalPos, 1));
        return Output; }

VS_CONTROL_POINT_OUTPUT VShader(VS_CONTROL_POINT_INPUT Input) {
    VS_CONTROL_POINT_OUTPUT Output;
    Output.vPosition = Input.vPosition;
    return Output; }

float4 PShader(DS_OUTPUT Input) : SV_TARGET {
    return float4(1, 0, 0, 1); }

我的着色器初始化。代码:

D3DCompileFromFile(L"shader.hlsl", NULL, NULL, "VShader", "vs_5_0", 0, 0, &VS, &ERR);
D3DCompileFromFile(L"shader.hlsl", NULL, NULL, "PShader", "ps_5_0", 0, 0, &PS, &ERR); 
D3DCompileFromFile(L"shader.hlsl", NULL, NULL, "HShader", "hs_5_0", 0, 0, &HS, &ERR);
D3DCompileFromFile(L"shader.hlsl", NULL, NULL, "DShader", "ds_5_0", 0, 0, &DS, &ERR);

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);

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

输入描述符:

D3D11_INPUT_ELEMENT_DESC ied[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devcon->IASetInputLayout(pLayout);

光栅化器:

D3D11_RASTERIZER_DESC RasterDesc = {};
RasterDesc.FillMode = D3D11_FILL_SOLID;
RasterDesc.CullMode = D3D11_CULL_NONE;
RasterDesc.DepthClipEnable = TRUE;
ID3D11RasterizerState* WireFrame=NULL;

dev->CreateRasterizerState(&RasterDesc, &WireFrame);
devcon->RSSetState(WireFrame);

输入顶点:

OurVertices = (VERTEX*)malloc(PointCount * sizeof(VERTEX));
for (int i = 0; i < PointCount; i++)
{
    OurVertices[i] = { RandOm() * i,RandOm() * i ,RandOm() ,{abs(RandOm()),abs(RandOm()),abs(RandOm()),1.0f} };
}

C缓冲区:

ID3D11Buffer* g_pConstantBuffer11 = NULL;
cbuff.world = XMMatrixOrthographicOffCenterLH(SceneY - (ViewPortWidth / 2) * SceneZoom, SceneY + (ViewPortWidth / 2) * SceneZoom,
    SceneX - (ViewPortHeight / 2) * SceneZoom, SceneX + (ViewPortHeight / 2) * SceneZoom,-10000.0f, 10000.0f);

D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = sizeof(CBUFFER);
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;
   
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &cbuff;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

dev->CreateBuffer(&cbDesc, &InitData,&g_pConstantBuffer11);

devcon->VSSetConstantBuffers(0, 1, &g_pConstantBuffer11);

渲染时:

devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_3_CONTROL_POINT_PATCHLIST); 

所有可能的异常都在这段代码中处理。只是为了干净的代码,它们被删除了InitRender 阶段没有警告或异常。 我什至无法调试它,因为没有任何内容被绘制到输出中。另外,有什么方法可以查看着色器的输出值吗?

cbuffer cbPerFrame : register(b0) 
{
    matrix world; 
};

不再被顶点着色器使用,但被域着色器使用,但是,你似乎没有附加它,所以你应该:

devcon->VSSetConstantBuffers(0, 1, &g_pConstantBuffer11); //Vertex
devcon->DSSetConstantBuffers(0, 1, &g_pConstantBuffer11); //Domain

(请注意,如果您仅使用曲面细分,通常不再需要绑定到顶点)。

否则管道将读取零矩阵。