0

I am using the latest version of XNA. I am attempting to write a lighting pixel shader for my 2D game. (Thinks Starbound's lighting system)

To do this, I need to implement a recursive function to spread light onto the texture.

Unfortunately, when I try to compile the following HLSL code it throws an exception...

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

Exception:

Error 1
Errors compiling 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': recursive functions not yet implemented C:\Users\Benjamin\Desktop\CURRENT PROJECTS\gmjosack-xna-2d-shader-examples-07cfe1a5aafb\ShaderTests\ShaderTestsContent\effects\Test.fx(67,23): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression ID3DXEffectCompiler: Compilation failed

Do I need to be using a newer version of HLSL? If so, how? If not, how can I get around this?

ben
  • 133
  • 13
  • Why don't you just try it? `PixelShader = compile ps_3_0 ...`. – Nico Schertler Jun 01 '14 at 10:17
  • @NicoSchertler That still returns the same error. Is recursion allowed in any HLSL version? – ben Jun 01 '14 at 13:29
  • Just tried it. None of the available profiles support recursive functions. It seems you have to re-write the algorithm so it doesn't use recursions. If you need help with that, just say so. A new question might be appropriate for this. – Nico Schertler Jun 01 '14 at 15:09

1 Answers1

2

Currently, none of the available pixel shader or vertex shader profiles (up to shader model 5) support recursive function calls. You will have to re-design the algorithm.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70