HLSL 2D像素着色器递归帮助? (XNA)

时间:2014-05-31 19:11:15

标签: c# recursion xna hlsl pixel-shader

我使用的是最新版本的XNA。我正在尝试为我的2D游戏编写一个照明像素着色器。 (想想Starbound的照明系统)

要做到这一点,我需要实现一个递归函数来将光线传播到纹理上。

不幸的是,当我尝试编译以下HLSL代码时,它会引发异常......

sampler s0;
texture lightMask;
sampler _lightMask = sampler_state{Texture = lightMask;};
texture blockMask;
sampler _blockMask = sampler_state{Texture = blockMask;};

int x1;
int y1;

//Info
float4 black = float4(0,0,0,1);
float4 white = float4(1,1,1,1);
float width, height; 
float ux, uy;

//Recursive Lighting
float4 ApplyLight(float4 lastLight, float2 pos, bool first)
{
    float4 newLight = lastLight;


if (!first)
{
newLight.rgb = lastLight.rgb - 0.1;
if ((newLight.r + newLight.g + newLight.b) / 3 <= 0)
return lastLight;
}
else
{
newLight = lastLight;
}

ApplyLight(newLight, pos + float2(0, uy), false);
ApplyLight(newLight, pos + float2(0, -uy), false);
ApplyLight(newLight, pos + float2(ux, 0), false);
ApplyLight(newLight, pos + float2(-ux, 0), false);

float4 color = tex2D(_lightMask, pos);
color = newLight;
return newLight;
}


//Shader Function
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
float4 lightHere = tex2D(_lightMask, coords);


if (lightHere.r > 0
||  lightHere.g > 0
||  lightHere.b > 0)
{
    ApplyLight(lightHere, coords, true);
}


    return lightHere;
}


//Technique
technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

例外:

错误1
错误编译C:\ Users \ Benjamin \ Desktop \ CURRENT PROJECTS \ gmjosack-xna-2d-shader-examples-07cfe1a5aafb \ ShaderTests \ ShaderTestsContent \ effects \ Test.fx: C:\ Users \ Benjamin \ Desktop \ CURRENT PROJECTS \ gmjosack-xna-2d-shader-examples-07cfe1a5aafb \ ShaderTests \ ShaderTestsContent \ effects \ Test.fx(17,8):error X3500:'ApplyLight':递归函数还没有实施 C:\ Users \ Benjamin \ Desktop \ CURRENT PROJECTS \ gmjosack-xna-2d-shader-examples-07cfe1a5aafb \ ShaderTests \ ShaderTestsContent \ effects \ Test.fx(67,23):ID3DXEffectCompiler :: CompileEffect:编译表达式时出错 ID3DXEffectCompiler:编译失败

我是否需要使用更新版本的HLSL?如果是这样,怎么样?如果没有,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

目前,没有可用的像素着色器或顶点着色器配置文件(直到着色器模型5)支持递归函数调用。您将不得不重新设计算法。