Sign in to follow this  

hlsl question(solved)

This topic is 3042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i];
		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[i];
	}
	return color;
}

[Edited by - david_watt78 on August 18, 2009 11:23:08 AM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i];
color += InputTexture1.Sample(ColorSampler, UV ,int2(i*-1,0)) * PixelWeight[i];
}
return color;
}

Share this post


Link to post
Share on other sites

This topic is 3042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this