Sign in to follow this  
bwhiting

shader assembly if statements

Recommended Posts

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

Share this post


Link to post
Share on other sites
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? [img]http://public.gamedev.net/public/style_emoticons/default/huh.gif[/img]

Share this post


Link to post
Share on other sites
[quote name='bwhiting' timestamp='1313699740' post='4850921']
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? [img]http://public.gamedev.net/public/style_emoticons/default/huh.gif[/img]
[/quote]

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.

Share this post


Link to post
Share on other sites

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