2

我正在尝试使用 OpenCL 编写光线追踪器。然而,我遇到了一些麻烦。

我想在 OpenGL 和 OpenCL 之间共享纹理内存,以避免不必要的内存来回复制。我的程序运行良好,每次调用 GL 和 CL 后我都会检查,我没有收到任何错误。

如标题所述,使用 write_imagef 写入内核中的纹理会在每个通道中生成 1.0。

我怀疑纹理的格式有问题,但我一直在互联网上寻找有效的纹理格式,但我看不出有什么问题。我尝试了 write_imageui 和 write_imagef 以及纹理格式的不同组合,但没有成功。

内核程序:

__kernel void Draw( __global __write_only image2d_t image, const int width, const int height )
{
    int x = get_global_id(0);
    int y = get_global_id(1);
    // Write some red color
    write_imagef(image, (int2)(x,y), (float4)(1.0f,0.0f,0.0f,1.0f));
}

我已经通过修改我的代码来确认我的内核正在运行

if(x < width/2)
    write_imagef(image, (int2)(x,y), (float4)(1.0f,0.0f,0.0f,1.0f));

并将我的纹理初始化为一种颜色(绿色)。结果:一半白色和一半绿色的纹理,这意味着内核正在写入应该写入的图像(但不是正确的值)。

创建我的纹理和创建 cl_mem 对象:

// Create OpenGL Texture
GLuint textureID;
glGenTextures( 1, &textureID );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_FLOAT, nullptr );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

// Create OpenCL memobject from gl texture
cl_mem textureMem = clCreateFromGLTexture2D( context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, textureID, &ret );

执行内核并渲染纹理:

// Main loop
...

// Set kernel args
ret = clSetKernelArg( kernel, 0, sizeof(cl_mem), (void *)&textureMem );
ret = clSetKernelArg( kernel, 1, sizeof(int), (void *)&imageWidth );
ret = clSetKernelArg( kernel, 2, sizeof(int), (void *)&imageHeight );

cl_event event[3];
// Aquire texture
ret = clEnqueueAcquireGLObjects( commandQueue, 1, &textureMem, 0, NULL, &event[0] );

// Execute kernel
size_t globalWorkSize[] = {imageWidth, imageHeight};
size_t localWorkSize[] = {32,32};
ret = clEnqueueNDRangeKernel( commandQueue, kernel, 2, NULL, globalWorkSize, localWorkSize, 1, &event[0], &event[1] );

// Release texture
ret = clEnqueueReleaseGLObjects( commandQueue, 1, &textureMem, 1, &event[1], &event[2] );

clWaitForEvents( 1, &event[2] );

// Render textured quad with OpenGL
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( quadProgramID );
glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer );
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, textureID );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 4, GL_FLOAT, GL_FALSE, 0, (void*)0 );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
glDisableVertexAttribArray( 0 );
glUseProgram(0);
glfwSwapBuffers( window );
glFinish();

我还可以补充一点,我已经成功地能够使用相同的主机程序在内核中发送和处理常规浮点数组。

我希望有人有类似的问题,因为我现在完全被困住了,我不妨把这个问题扔出去。

如果上面的代码不够相关,这里是完整的源http://pastebin.com/LhBhQDSR

我很感激任何帮助。

4

1 回答 1

1

这是频道格式的问题。在创建 glTexture 时,我将内部格式从 GL_RGBA 更改为 GL_RGBA32F,现在它就像一个魅力。

于 2013-11-01T23:31:20.137 回答