テクスチャから値を読み取り、それらをグローバル メモリに書き戻そうとしています。カーネルに定数値を入れることができ、出力でそれらを確認できるため、書き込み部分が機能すると確信しています。
__global__ void
bartureKernel( float* g_odata, int width, int height)
{
unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
if(x < width && y < height) {
unsigned int idx = (y*width + x);
g_odata[idx] = tex2D(texGrad, (float)x, (float)y).x;
}
}
使用したいテクスチャは 2 つのチャネルを持つ 2D float テクスチャなので、次のように定義しました。
texture<float2, 2, cudaReadModeElementType> texGrad;
そして、カーネルを呼び出すコードは、ゼロ以外の定数値でテクスチャを初期化します。
float* d_data_grad = NULL;
cudaMalloc((void**) &d_data_grad, gradientSize * sizeof(float));
CHECK_CUDA_ERROR;
texGrad.addressMode[0] = cudaAddressModeClamp;
texGrad.addressMode[1] = cudaAddressModeClamp;
texGrad.filterMode = cudaFilterModeLinear;
texGrad.normalized = false;
cudaMemset(d_data_grad, 50, gradientSize * sizeof(float));
CHECK_CUDA_ERROR;
cudaBindTexture(NULL, texGrad, d_data_grad, cudaCreateChannelDesc<float2>(), gradientSize * sizeof(float));
float* d_data_barture = NULL;
cudaMalloc((void**) &d_data_barture, outputSize * sizeof(float));
CHECK_CUDA_ERROR;
dim3 dimBlock(8, 8, 1);
dim3 dimGrid( ((width-1) / dimBlock.x)+1, ((height-1) / dimBlock.y)+1, 1);
bartureKernel<<< dimGrid, dimBlock, 0 >>>( d_data_barture, width, height);
テクスチャ バイトをすべて「50」に設定しても、フロートのコンテキストではあまり意味がありませんが、少なくともゼロ以外の値を読み取る必要があります。
ゼロしか読めないけど…