Jump to content
  • Advertisement
Sign in to follow this  
david_watt78

hlsl question(solved)

This topic is 3259 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;
		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]

Share this post


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

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;
color += InputTexture1.Sample(ColorSampler, UV ,int2(i*-1,0)) * PixelWeight;
}
return color;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!