hlsl question(solved)

Started by
2 comments, last by david_watt78 14 years, 8 months ago
Everytime I try to use if conditions in a shader where I calculate the UV per pixel in the pixel shader, I get an compile error saying something about branching is not allowed where calculating derivatives across pixels. Is there a clever way to get around this issue such as using a different means to do the texture sampling. I am using dx10 and hlsl 4.0 . I am trying to fix an issue with HDRI where a glowing object passes in front of another. Here is the Pixel shader I am using for the Horizontal blur phase. I want to not blur if the original pixel is greater than 1.0 .

float4 HorzPS(PixelInput Input) : SV_Target
{
	float4 Original = InputTexture2.Sample(ColorSampler,Input.UV);

	Original = SuppressLDR( Original );
	if(any(Original))
	{
		return Original;
	}
	// Sample pixels on either side
	float2 UV = Input.UV;
	float fOffset = 0;
	float4 color = InputTexture1.Sample(ColorSampler,Input.UV);
	color *= 0.53;
	bool b = all(Original);
	int n = int(b) * 10;
    for( int i = 1; i < 10; i++ )
	{
		fOffset = i;
		fOffset /= g_fWidth;
		UV.x = Input.UV.x + fOffset;
		if(UV.x > 1)
		{
			UV.x = 1;
		}
		if(UV.x < 0)
		{
			UV.x = 0;
		}
		color += InputTexture1.Sample(ColorSampler, UV) * PixelWeight;
		UV.x = Input.UV.x - fOffset;
		if(UV.x < 0)
		{
			UV.x = 0;
		}
		if(UV.x > 1)
		{
			UV.x = 1;
		}
		color += InputTexture1.Sample(ColorSampler, UV ) * PixelWeight;
	}
	return color;
}

[Edited by - david_watt78 on August 18, 2009 11:23:08 AM]
Advertisement
The easiest way to get around it is to explicitly calculate your x and y partial derivatives outside of the branch using ddx() and ddy(), and then pass those in to Texture.SampleGrad

Also just an FYI...if you want to post source code here you can use the "source" or "code" tags to format it properly. More info in the FAQ.
ok is there an example someplace of using SampleGradient like that? I am unsure as to what you mean by the ddx() and ddy() part or what values I should pass into them. I assume there values would be the ddx and ddy parameters to sample gradient.
Found the answer myself. I could have used the Load function since it doesn't bother the compiler, but then there is no sampling, so i used offsets instead and it works fine and solved my star passing in front of star issue. So you can compute offsets and the compiler doesn't complain.
[Source]float4 HorzPS(PixelInput Input) : SV_Target{	float4 Original = InputTexture2.Sample(ColorSampler,Input.UV);	Original = SuppressLDR( Original );	if(any(Original))	{		return Original;	}	float4 color = InputTexture1.Sample(ColorSampler,Input.UV);	color *= 0.53;	for( int i = 1; i < 7; i++ )	{		color += InputTexture1.Sample(ColorSampler, UV,int2(i,0)) * PixelWeight;		color += InputTexture1.Sample(ColorSampler, UV ,int2(i*-1,0)) * PixelWeight;	}	return color;}

This topic is closed to new replies.

Advertisement