Jump to content

View more

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Shader Flow control problem

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 Naruto-kun   Members   

442
Like
0Likes
Like

Posted 19 July 2013 - 08:55 AM

Hi guys

 

I am having some trouble with if statements in my pixel shader.

Texture2D shaderTextures[3];
SamplerState SampleType;

struct VS_Output
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
};

cbuffer Lights
{
    float g;
    float c;
    float t;
}

float4 Blender(VS_Output input):SV_TARGET
{
	float4 color1;
	float4 color2;
	float4 color3;
	float4 color4;
	
	if( g != 0 && c == 0 && t == 0)
	{
		color2 = shaderTextures[0].Sample(SampleType, input.tex);
		color1 = color2 * g;
	}
	
	if( g == 0 && c != 0 && t == 0)
	{
		color3 = shaderTextures[1].Sample(SampleType, input.tex);
		color1 = color3 * c;
	}

	if( g == 0 && c == 0 && t != 0)
	{
		color4 = shaderTexture[2].Sample(SampleType, input.tex);
		color1 = color4 * t;
	}
	
	return color1;
}

If i remove the if blocks leaving any one of the statements inside,

D3DX11CompileFromFile is successful. But the moment i include any if control, it fails. Where am i going wrong?

 

Thanks

JB



#2 Styves   Members   

1749
Like
3Likes
Like

Posted 19 July 2013 - 09:49 AM

Use SampleLevel instead of Sample in your branches.Otherwise the compiler will move them out of the loop before processing - if you have warnings on or anything of that sort, the compile will fail.



#3 Naruto-kun   Members   

442
Like
0Likes
Like

Posted 19 July 2013 - 10:55 AM

Thanks a bunch. All working now.



#4 NateMc4   Members   

167
Like
0Likes
Like

Posted 20 July 2013 - 05:50 PM

I think color1 is considered uninitialized because all the the assignments of color1 are inside ifs that could be false in some case.

 

float4 color1 = float4(0,0,0,0);

 

Use SampleLevel if you want to set your mip level manually or sample in a shader that's not the pixel shader.



#5 imoogiBG   Members   

3164
Like
0Likes
Like

Posted 22 July 2013 - 09:41 AM

use that code if it fits your needs (I'm not pretty shure what you're trying to achieve)

 

index = g*1 + c*2 + t*3 -1;
color = shaderTextures[index].Sample(SampleType, input.tex);

color *= g + c + t;

 

always try to avoid branching


Edited by imoogiBG, 22 July 2013 - 09:45 AM.


#6 Naruto-kun   Members   

442
Like
0Likes
Like

Posted 28 July 2013 - 07:16 AM

Got sorted. I was making a shader that blended textures by RGB values (the lower the RGB the more transparent it is) and at least 3+ textures were involved. Was trying to keep certain blends from occuring if the g/c/t values in the constant buffer were zero to improve efficiency.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.