GPGPUで整数計算(4)

全面的に書き直し。

整数の精度

整数のデータを入力したかったが、よくわからなかった。
検索しても出てこないし。
仕方がないので、floatの配列をGPUに送って、キャストすることにした。


const int nWidth = 4;
const int nHeight = 2;
const int nData = nWidth * nHeight * 4;
float *data = new float[nData];
const int nRep = 12;

配列に1,2,3,…と代入して、


for(int i = 0; i < nData; i++) {
data[i*4] = (float)(i + 1);
}

シェーダープログラムは、
5倍していって、
4つのfloatのうち、10万までを1つ目、その上を2つ目に入れる。


#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

uniform sampler2DRect texUnit0; // data

uniform int nRep;

void main(void) {
int nPrimes = 0;
vec4 color = texRECT(texUnit0, gl_FragCoord.xy);
int n = int(color.x);

for(int i = 0; i < nRep; i++) {
n *= 5;
}

gl_FragColor = vec4(n % 100000, n / 100000, 0, 0);
}

出力はこうする。


for(int i = 0; i < nData / 4; i++) {
printf("%d%05d\n", (int)data[i*4+1], (int)data[i*4]);
}

出力は、


244140625
488281250
732421875
976562500
1220703125
1464843750
1708984375
1953125000

問題ない。
オーバーフローすると、負に戻る。