# Issues with 2D raycasting lighting under limitations of HLSL 3.0 pixel shader

I've been writing my own HLSL pixel shader for dynamic lighting using raycasting. Unfortunately, since I'm using this out of XNA, I can only use up to ps_3_0. As you can see, the limitations difference between 3 and 4 are drastic, especially in instruction slots and temp registers:

Specifically, I'm running out of instruction slots. This limitation is preventing me from having accurate rays. (As in the amount of pixels the ray's position increases by from the light source has to be a lot less than I want)

Here's what it looks like. And here's the relevant part of the code:

//Cast rays (Point Light)
float4 CastRays(float2 texCoord: TEXCOORD0) : COLOR0
{
float dir;
float2 move;
float2 rayPos;

float2 pixelPos = float2(texCoord.x * width, texCoord.y * height);

float dist = distance(pixelPos, lightPos);

if (sqrt(dist) <= lightSize)
{
rayPos = lightPos;
dir = atan2(lightPos.y - pixelPos.y, lightPos.x - pixelPos.x);
move = float2(cos(dir), sin(dir)) * rayLength;
for (int ii = 0; ii < clamp(dist / rayLength, 0, 7); ii++)
{
if (tex2D(s0, float2(rayPos.x / width, rayPos.y / height)).a > 0)
{
return black;
}
rayPos -= move;
}
}
else
return black;

float light = 1 - clamp((float)abs(sqrt(dist)) / lightSize, 0.0, 1.0);
return lightColor * light;
}


The limitation variable I've put in place is rayLength. The larger this number, the less accurate the rays are. I can give more specific examples of what this looks like if anybody wants.

I'm very new to the concept of raycasting, and fairly new to HLSL. Is there any way I can make this work under the limitations and/or increase the limits?

Thank you.

• Could u blur ray out put texture in another pass? When I do my god rays in 3d I have to blur it for it to look nice. From the vid u posted it looks good, just a bit jaggy, um could u maybe use some form of shadow filtering? – Justin William Stanley Bryant Oct 23 '16 at 1:54