0

シェーダー コード (HLSL) に問題があります。「DirectX for Managed Code」と Shader Model 3.0 を使用しています。ピクセル シェーダーの出力構造体で DEPTH セマンティックを使用して、カスタムの深度値を深度バッファーに書き込もうとしています。

struct PSOutput
{
    float4 col : COLOR0;
    float dept : DEPTH;
};

そして、この構造体をピクセル シェーダーの戻り値として使用します。

PSOutput PSFunction(VertexShaderOutput input)
{
    PSOutput output;
    ...
    output.col = float4(...);
    output.dept = ...;

    return output;
}

このシェーダーをコンパイルしようとすると、DirectX は例外をスローしますが、詳細な理由を提供しません。しかし、出力構造体から深さ変数を削除すると機能します! DEPTH0 をセマンティックとして記述しようとしましたが、成功しませんでした。誰かがそれを手伝ってくれることを願っています。

編集:

次のように書くと失敗します。

PSOutput PSFunction(VertexShaderOutput input)
{
    PSOutput output;

    float resDepth = input.Position[2] / input.Position[3];

    if(...)
    {
       resDepth = ...; 
    }
    output.col = float4(...);
    output.dept = resDepth;

    return output;
}

しかし、このコードを書くと、コンパイルされます:

PSOutput PSFunction(VertexShaderOutput input)
{
    PSOutput output;

    float resDepth = input.Position[2] / input.Position[3];

    if(...)
    {
       resDepth = ...; 
    }
    output.col = float4(...);
    output.dept = 0.5;

    return output;
}

何か案は?

完全なコードは次のとおりです。

float4x4 World;
float4x4 View;
float4x4 Projection;

float4 CamPos;
float4 LightDir;
float4 ObjColor;

static const float PI = 3.14159265f;

static const int MAX_FU = 32;

float fuPercent[MAX_FU];
float4 fuColor[MAX_FU];

int buildDir;
static int fuCount = 2;

float4 boxMin;
float4 boxMax;

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL;
    float3 ExactPos : TEXCOORD1;
};

struct PSOutput
{
    float4 col : COLOR0;
    //float dept : DEPTH;
};

VertexShaderOutput VSFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    output.Normal = mul(input.Normal, World);
    output.ExactPos = input.Position;

    return output;
}

PSOutput PSFunction(VertexShaderOutput input)
{
    PSOutput output;

    float4 resColor = ObjColor;
    float resDepth = input.Position[2] / input.Position[3];

    float prpos = 0;

    if (buildDir == 0)
    {
        prpos = (input.ExactPos[1] - boxMin[1]) / (boxMax[1] - boxMin[1]);
    }
    else if (buildDir == 1)
    {
        prpos = 1.0 - ((input.ExactPos[1] - boxMin[1]) / (boxMax[1] - boxMin[1]));
    }
    else if (buildDir == 2)
    {
        prpos = (input.ExactPos[2] - boxMin[2]) / (boxMax[2] - boxMin[2]);
    }
    else if (buildDir == 3)
    {
        prpos = 1.0 - ((input.ExactPos[2] - boxMin[2]) / (boxMax[1] - boxMin[2]));
    }
    else if (buildDir == 4)
    {
        prpos = (input.ExactPos[0] - boxMin[0]) / (boxMax[0] - boxMin[0]);
    }
    else if (buildDir == 5)
    {
        prpos = 1.0 - ((input.ExactPos[0] - boxMin[0]) / (boxMax[0] - boxMin[0]));
    }

    float currPerc = 1.1;

    for (int i = 0; i < fuCount; i++)
    {
        if (prpos - 0.0001 <= fuPercent[i])
        {
            if (fuPercent[i] < currPerc)
            {
                currPerc = fuPercent[i];
                resColor = fuColor[i];
            }
        }
        else
        {
            resDepth = 1.0;
            resColor[3] = 0.0;
        }
    }

    float3 nor = input.Normal;
    float3 pos = input.ExactPos;
    float glo = 0.5;

    float id = (acos(dot(LightDir,nor) / pow(dot(LightDir,LightDir) * dot(nor, nor), 0.5))  / PI );
    id = pow(id,2);

    float3 look = reflect(normalize(pos - CamPos), nor);
    float gl = (acos(dot(LightDir,look) / pow(dot(LightDir,LightDir) * dot(look, look), 0.5))  / PI );
    gl = max(gl * 10.0 - 9.0, 0.0);
    gl = pow(gl,2) * glo;

    output.col = float4(resColor[0] * id + gl, resColor[1] * id + gl, resColor[2] * id + gl, resColor[3]);
    //output.dept = resDepth;

    return output;
}

technique MyTechnique
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VSFunction();
        PixelShader = compile ps_3_0 PSFunction();
    }
}
4

1 に答える 1

0

FXC がコンパイル エラーではなく、コンパイル中に例外をスローしている場合は、おそらく何も間違っていません。

DirectX SDK を使用している場合は、最新バージョン (2010 年 6 月) を使用していることを確認してください。Windows Kit 8.0 SDK を使用している場合は、コンパイラのバグが見つかった可能性があります。SDK / fxc のどのバージョンを使用していますか?

実際にコンパイルされるシェーダーを投稿できますか (VertexShaderOutput 構造体がなく、実際のコードの代わりに ... がないもの)? 不足しているコードを埋めたので、Windows Kit 8.0 の fxc を使用して問題なくコンパイルできます。

編集:

いいえ、コンパイルしない原因となったコードをコメントアウトしていたことに気づきませんでした。

案の定、コンパイルされませんが、それは有効なコードではないためです (コンパイル エラーで報告されます)。POSITION セマンティックをピクセル シェーダーへの入力として使用していますが、これは無効です。頂点シェーダーから出力された位置をピクセル シェーダーへの入力として使用する場合は、それを 2 番目の属性にコピーして代わりに使用します。次のコードをシェーダーに代入すると、コンパイルされます。

struct VertexShaderOutput
{
    float4 ClipPosition : POSITION; // Renamed this to ClipPosition.
    float4 Position : TEXCOORD0;    // This is valid to use as an input to the pixel shader.
    float3 Normal : NORMAL;
    float3 ExactPos : TEXCOORD1;
};

struct PSOutput
{
    float4 col : COLOR0;
    float dept : DEPTH;
};

VertexShaderOutput VSFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.ClipPosition = mul(viewPosition, Projection); 
    output.Position = output.ClipPosition; // Copy output position to our other attribute.
    output.Normal = mul(input.Normal, World);
    output.ExactPos = input.Position;

    return output;
}
于 2013-06-21T10:40:59.070 に答える