【问题标题】:How do you implement an efficient parallel SIMD compare and select in Cg?如何在 Cg 中实现高效的并行 SIMD 比较和选择?
【发布时间】:2011-09-12 16:18:47
【问题描述】:

如何有效地进行并行选择?

例如,给定这个标量代码,有没有办法编写它,以便 Cg 编译器使代码并行/SIMD 执行(也可能使用无分支选择)。

            Out.x = ( A.x <= threshold) ? B.x : C.x ;
            Out.y = ( A.y <= threshold) ? B.y : C.y ;
            Out.z = ( A.z <= threshold) ? B.z : C.z ;
            Out.w = ( A.w <= threshold) ? B.w : C.w ;

【问题讨论】:

    标签: selection parallel-processing shader simd cg


    【解决方案1】:

    显然,我错过了 Cg 手册中的这些行:

    The ?:, ||, &&, &, and comparison operators can
    be used with bool vectors to perform multiple
    conditional operations simultaneously.
    

    所以我尝试了这个,它似乎工作:

    Out.xyzw = ( A.xyzw <= threshold) ? B.xyzw : C.xyzw ;
    

    我想我没想到最简单的解决方案能奏效!

    我的图形程序员同事还建议,在某些平台上,Cg 编译器可能足够智能,可以为我优化原始源代码,但不能保证,如果可能,最好明确指定并行 SIMD 操作.

    【讨论】:

      最近更新 更多