我有一个问题,我似乎无法解决它,所以我希望这里的某个人能够帮助我。
我正在为 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 都将引入不断堆叠的比较,此外,最后一个 else 要求我重新评估 cond2,或使用另一个寄存器。我知道我可能在这里做错了什么,但我不确定是什么。我尝试过使用计数器,尝试添加 if/else 块、anding、oring 等先前阶段的结果,但我找不到如何将 if/else 块转换为 ARB 片段程序集的好的解决方案。 t 真的在越来越多的 CMP 声明堆栈上。有谁知道如何使这更简单,以便我的编译器可以以编程方式输出?在这一点上,我不太担心优化,我只是想让它发挥作用。
谢谢