为什么我的 Hull 和 Domain 着色器没有编译? (HLSL)

问题描述 投票:0回答:0

这是我的外壳着色器,我在主函数上方的每个属性上都收到“警告 X3554:未知属性 ,或该语句的属性无效”,以及“错误 X3658:'main':InputPatch 输入只能用于域着色器和外壳着色器的补丁常量函数”在主函数中的 InputPatch 参数上:

struct HullShaderInput
{
    float4 wPos : WPOSITION;
    float4 position : SV_POSITION;
    float3 colour : COLOUR;
    float3 normal : NORMAL;
    float2 uv : UV;
};

struct HullShaderConstDataOutput
{
    float EdgeTessFactor[3] : SV_TessFactor;
    float InsideTessFactor : SV_InsideTessFactor;
};

struct HullShaderOutput
{
    float4 wPos : WPOSITION;
    float4 position : SV_POSITION;
    float3 colour : COLOUR;
    float3 normal : NORMAL;
    float2 uv : UV;
};

cbuffer cBuff : register(b0)
{
    float4x4 world;
    float4x4 view;
    float4x4 projection;
    float4 camPos;
}

//cbuffer iBuff : register(b1)
//{
//  float4 objPos;
//}

#define NUM_CONTROL_POINTS 3

float calcTessFactor(float3 vertP1, float3 vertP2)
{
    float edgeLen = distance(vertP1, vertP2);
    float edgeCamDist = distance((vertP1+vertP2)/2, camPos.xyz);
    
    // Not based on objects size on the screen
    // To give more accurate approximation, multiply with the screen height in pixels and an FOV factor (1 at 90 deg, increasing at lower FOV),
    //and divide by the allowed edge length in pixels.
    return clamp((edgeLen)/(edgeCamDist), 1.0f, 63.0f); // Using max=63 because of fractional_odd partitioning
}

HullShaderConstDataOutput CalcHSPatchConstants
(   InputPatch<HullShaderInput, NUM_CONTROL_POINTS> ip,
    uint i : SV_PrimitiveID )
{
    HullShaderConstDataOutput output;
    // This function should calculate the factors based on distance to camera, so the constant value is just temporary... I think
    //output.EdgeTessFactor[0] = output.EdgeTessFactor[1] = output.EdgeTessFactor[2] = output.InsideTessFactor = 32;
    // Anyways, this should have a decent approximation for realistic tessellation factors based on distance to camera
    // That is, if every "ip" represents a triangle, and "i" represents a point on said triangle
    output.EdgeTessFactor[i] = calcTessFactor(ip[i].wPos.xyz, ip[(i+1)%3].wPos.xyz);
    output.InsideTessFactor = (output.EdgeTessFactor[0]+output.EdgeTessFactor[1]+output.EdgeTessFactor[2])/3;

    return output;
}

[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("HullShaderConstDataOutput")]
HullShaderOutput main
(   InputPatch<HullShaderInput, NUM_CONTROL_POINTS> ip,
    uint i : SV_OutputControlPointID    )
{
    HullShaderOutput output;
    
    output.wPos = ip[i].wPos;
    output.position = ip[i].position;
    output.normal = ip[i].normal;
    output.uv = ip[i].uv;
    
    return output;
}

这是我的域着色器,我在 [domain("tri")] 上收到“warning X3554” 和主函数的 OutputPatch 参数上的“error X3658”:

struct DomainShaderConstDataInput
{
    float EdgeTessFactor[3] : SV_TessFactor;
    float InsideTessFactor : SV_InsideTessFactor;
};

struct DomainShaderInput
{
    float4 wPos : WPOSITION;
    float4 position : SV_POSITION;
    float3 colour : COLOUR;
    float3 normal : NORMAL;
    float2 uv : UV;
};

struct DomainShaderOutput
{
    float4 wPos : WPOSITION;
    float4 position : SV_POSITION;
    float3 colour : COLOUR;
    float3 normal : NORMAL;
    float2 uv : UV;
};

cbuffer cBuff : register(b0)
{
    float4x4 world;
    float4x4 view;
    float4x4 projection;
    float4 camPos;
};

#define NUM_CONTROL_POINTS 3

[domain("tri")]
DomainShaderOutput main
(   DomainShaderConstDataInput input, float3 barycentric : SV_DomainLocation,
    const OutputPatch<DomainShaderInput, NUM_CONTROL_POINTS> patch  )
{
    DomainShaderOutput output;
    output.wPos = patch[0].wPos*barycentric.x + patch[1].wPos*barycentric.y + patch[2].wPos*barycentric.z;
    output.normal = normalize(patch[0].normal*barycentric.x + patch[1].normal*barycentric.y + patch[2].normal*barycentric.z);
    output.uv = patch[0].uv*barycentric.x + patch[1].uv*barycentric.y + patch[2].uv*barycentric.z;
    output.position = mul(mul(output.wPos, view), projection);
    
    return output;
}

我在这里做错了什么?我怎样才能真正让这些着色器编译? 请告诉我这里是否需要顶点和像素着色器,以便您了解问题所在!

3d shader hlsl direct3d direct3d11
© www.soinside.com 2019 - 2024. All rights reserved.