shader assembly if statements

Started by
2 comments, last by Syranide 12 years, 8 months ago
I currently write my shaders in an assembly language (Adobe's AGAL)

and so far so good, its pretty simple and quite powerful because it makes it really easy to chain shaders together etc...

but I would like to implement some if statement type logic, this is new to me and I ain't too sure how it works!

so far stuff looks like

add("ft3", "ft11, "ft2" )
mov(...
m44(..
...

that kinda stuff all pretty much linear.

anyone know how if statements should look?

i think these are the available conditionals:

"ifz";
"inz";
"ife";
"ine";
"ifg";
"ifl";
"ieg";
"iel";
"els";
"eif";
"rep";


anyone know how these should/could be used to do if statements or looping?!?!?

to be totally honest I am not sure if they are all even implemented yet but if someone can give me a heads up on how they might be used that would be great!!

thanks :D
Advertisement
managed to find out a little about shader assembly and it seems that most older gpus still execute both sides of a conditional anyway.. they just reject the result somehow

hmmm

suppose you wanted to do this..

//pseudo code
if(uv.x > 0.5)
{
colour.rgb = 1;
}else{
colour.rgb = 0;
}

not really sure how you would convert that but i guess you could convert the stuff in the conditional into temps and then set either one temp or the other one to zero based on the conditional, and then add it to the colour?

would that be how its done? huh.gif
Probably you can just use fxc.exe to compile the shader and then look at the generated assembly?

Edit: Nevermind. Somehow thought you were talking about HLSL.

managed to find out a little about shader assembly and it seems that most older gpus still execute both sides of a conditional anyway.. they just reject the result somehow

hmmm

suppose you wanted to do this..

//pseudo code
if(uv.x > 0.5)
{
colour.rgb = 1;
}else{
colour.rgb = 0;
}

not really sure how you would convert that but i guess you could convert the stuff in the conditional into temps and then set either one temp or the other one to zero based on the conditional, and then add it to the colour?

would that be how its done? huh.gif


From what I understand, the way it is done is that you convert it all into a mathematical expression where all but one branch is always cancelled out.

Assuming a stupid compiler, your example would become:
colour.rgb = 1 * (uv.x > 0.5) + 0 * (uv.x <= 0.5)

There are obviously lots of optimizations that can be done on this, often in general too I believe.

However, you must take care when working with shaders if they do not support actual branches, because as you might figure from the expression above... INFINITY and special values don't play nice! Meaning, you might not be able to check a value against infinity and then use it in a branch if it isn't infinity. As it will still be part of the final expression and possibly also wrongfully messing up the result. This may not be an issue in your language, but I've had this happen to a friend of mine.


This topic is closed to new replies.

Advertisement