开发者

ARB Fragment If/Else

I have a problem and I can't seem to wrap my head around it, so I was hoping someon here might be able to help me out.

I'm writing a compiler for miniGLSL, and so far so good. I'm at the point where I need to output to an ARB fragment program, but the problem is, the ARB I have to target doesn't support branching. (A full list of supported instructions can be found here http://petewarden.com/notes/archives/2005/05/fragment_progra_2.html ).

In order to simulate if/else, I've been making use of the CMP program as follows (assuming 0 or greater = true, otherwise, false. // represents comments as # causes bad formatting on here):

if (a < b)
  a = 1 + 1;
  if (f < g)
    c = 2 + 3;
else
  if (h < i)
    b = 1 + 2;
  else
    d = 2 + 3;

into ARB fragment:

TEMP cond1, cond2, cond3, tempvar1, tempvar2, tempvar3, tempvar4, a, b, c, d, e, f, g;
//TOP IF
//condition a < b
SLT a, b, cond1;
SUB cond1, 1.0, cond1;

//Assign if true
ADD 1.0, 1.0, tempvar1;
CMP cond1, a, tempvar1, a;

//Condition f < g
SLT f, g, cond2;
SUB cond2, 1.0, cond2;
//if top level if was false, assign false, otherwise assign it to itself
CMP cond1, -1.0, cond2, cond2;
//Assignment
ADD 2.0, 3.0, tempvar2;
CMP cond2, c, tempvar2, c;

//TOP ELSE
//if h < i
SLT h, i, cond2;
SUB cond2, 1.0, cond2;
//If top level if was true, make false
CMP cond1, cond2, -1.0, cond2;
CMP cond2, tempvar3, b, b;
//Else
//if top level if was true, and previous if was false, make true

This is about where I get before i realize my code is going to start getting really ugly. Each level of if/else is going to introduce cont开发者_Python百科inually stacking compares, and additionally, the last else requires me to re-evaluate cond2, or use another register. I know I'm probably doing something wrong here, but I'm not sure what. I've tried using counters, tried adding the result of previous stages of if/else block, anding, oring, etc. but I can't find a good solution to how to convert if/else blocks into ARB fragment assembly that doesn't really on increasingly large stacks of CMP statements. Does any one have an idea how to make this simpler so my compiler can output this programmatically? I'm not worried to much about optimization at this point, I just want to get it to work.

Thanks


are taking csc467 at uoft cause if you are im in your class lol.

So this is how I think this should be implemented , I just thought about it so not sure if its corrext.

example: if (a < b) a = 1 + 1; if (f < g) c = 2 + 3; else if (h < i) b = 1 + 2; else d = 2 + 3;

and from what i read here http://www.cs.uaf.edu/~olawlor/ref/gl/glfp/ you can flip the sign of an input but if its not the case then my idea is garbage

firstIf:

//calculate condition SLT a, b, condition1;

// calculate expression 1+1 it doesnt change any of the registers ADD 1 ,1 , temp; cmp -condition , temp , a ,a // if condition was true -(condition) = -1 <0 //so you store 1+1 in a else you store a in a

secondIf:

//calculate condition SLT f , g , condition2;

//now because you had previus condition1 you need to add them together if both are true //only then execute cod

TEMP combinedCon1 ; TEMP temp2 = {2.0}; ADD condition1, condition2, combinedCon1;
SGE combinedCon1 , temp2, combinedCon1 // if the two subexpresiions added together == 2 //then 1 else 0

//calculate 2+3 ADD 2, 3, temp;

//perform assignment if combineCon1 ==1 CMP -combineCon1 , temp , c ,c ;

//now you can do else with CMP instruction so follow same steps only interchange a few things,

so for example if you have ("else a = 2" ); its CMP -condition1 , a , temp ,a ; instead of CMP -condition1 , temp, a ,a ;

// so hopefully this works so every time you have another nested condition you have to && them and use the result in a CMP instruction..

I think this should work , not sure

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜