我有一個問題,我似乎無法把頭圍住它,所以我希望有人能在這裏幫助我。ARB Fragment If/Else
我正在爲miniGLSL編寫一個編譯器,到目前爲止這麼好。我處於需要輸出到ARB片段程序的地步,但問題是,我必須針對的ARB不支持分支。 (支持的說明的完整列表可以在這裏找到http://petewarden.com/notes/archives/2005/05/fragment_progra_2.html)。
爲了模擬if/else,我一直在使用CMP程序,如下所示(假設爲0或更大= true,否則爲false; //代表註釋爲#會導致格式不對):
if (a < b)
a = 1 + 1;
if (f < g)
c = 2 + 3;
else
if (h < i)
b = 1 + 2;
else
d = 2 + 3;
到ARB片段:
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
這大約是從哪裏獲得前我意識到我的代碼將開始變得非常難看。 if/else的每個級別都會引入連續的堆棧比較,此外,最後一個要求我重新評估cond2,或者使用另一個註冊表。我知道我可能在這裏做錯了什麼,但我不知道是什麼。我嘗試過使用計數器,嘗試添加if/else塊的先前階段的結果,以及oring等,但我無法找到一個很好的解決方案,以便如何將if/else塊轉換爲ARB碎片組合體,實際上越來越多的CMP語句堆積如山。有沒有人有一個想法如何使這個更簡單,所以我的編譯器可以輸出這個編程?在這一點上,我並不擔心優化問題,我只是想讓它工作。
感謝