GLSL奇数甚至合并排序

时间:2014-09-29 06:22:12

标签: sorting glsl gpu mergesort fragment-shader

我试图在gpugems网站上了解the odd-ever merge sort example,但我很难搞清楚他们传递给制服的一些内容。这里是整个着色器。

uniform vec3 Param1;
uniform vec3 Param2;
uniform sampler2D Data;

#define OwnPos gl_TexCoord[0]

// contents of the uniform data fields
#define TwoStage Param1.x
#define Pass_mod_Stage Param1.y
#define TwoStage_PmS_1 Param1.z
#define Width Param2.x
#define Height Param2.y
#define Pass Param2.z

void main(void){
// get self
vec4 self = texture2D(Data, OwnPos.xy);

float i = floor(OwnPos.x * Width) + floor(OwnPos.y * Height) * Width;

// my position within the range to merge
float j = floor(mod(i, TwoStage));

float compare;

if ( (j < Pass_mod_Stage) || (j > TwoStage_PmS_1) )
  // must copy -> compare with self
  compare = 0.0;
else
  // must sort
  if ( mod((j + Pass_mod_Stage) / Pass, 2.0) < 1.0)
    // we are on the left side -> compare with partner on the right
    compare = 1.0;
  else
    // we are on the right side -> compare with partner on the left
    compare = -1.0;

// get the partner
float adr = i + compare * Pass;

vec4 partner = texture2D(Data, vec2(floor(mod(adr, Width)) / Width, floor(adr / Width) / Height));

// on the left it's a < operation; on the right it's a >= operation
gl_FragColor = (self.x * compare < partner.x * compare) ? self : partner;
}

绊倒我的那部分正在弄清楚他们分配给Param1和Param2.z。

Param2.x和Param2.y只是图像的宽度和高度。每次通过循环时,pass变量只是一个递增的数字吗?

Param1.x,Param1.y和Param1.z让我完全难过。是否应该在这个程序的CPU端发生一些他们不包括在这里的事情?

任何帮助或清晰度将不胜感激!谢谢

1 个答案:

答案 0 :(得分:0)

此示例的代码可用heremain.cpp文件的第298行是您需要的文件:

glUniform3fARB(oddevenMergeSort.getUniformLocation("Param1"),
    float(pstage+pstage),
    float(ppass%pstage),
    float((pstage+pstage)-(ppass%pstage)-1)
);
glUniform3fARB(oddevenMergeSort.getUniformLocation("Param2"),
    float(width),
    float(height),
    float(ppass)
); 

使用int ppass = 1<<pass;int pstage = 1<<stage;