为什么OpenCL着色器在写入OpenGL纹理时会忽略绿色,蓝色和Alpha通道?

时间:2017-09-26 04:01:34

标签: opengl textures opencl shared

我有一个opencl着色器,试图将颜色写入纹理,但由于某种原因,只有红色通道应用于纹理,其余部分将被忽略。不仅如此,这个红色通道值被复制到所有其他通道,给我白色透明度。有人知道为什么会这样吗?我在Mac上使用Xcode编程,(OpenCL 1.2)。纹理首先使用OpenGL创建并与OpenCL共享。

这就是我使用OpenGL声明我写的纹理的方法:

GLint error;
accume_buffer = clCreateFromGLTexture(context,CL_MEM_READ_ONLY, GL_TEXTURE_3D, 0, _lpvTextures.accumeBuffer, &error);
assert(error == CL_SUCCESS);
accume_buffer2 = clCreateFromGLTexture(context,CL_MEM_WRITE_ONLY, GL_TEXTURE_3D, 0, _lpvTextures.accumeBuffer2, &error);
assert(error == CL_SUCCESS);

这是使用OpenCL创建共享资源的代码:

void LPV::runComputeShader(){

glFlush();
glFinish();
GLint error = 0;
cl_event opengl_get_completion;
error =clEnqueueAcquireGLObjects(command_queue, 1, &accume_buffer, 0,0,&opengl_get_completion);
clWaitForEvents(1, &opengl_get_completion);
clReleaseEvent(opengl_get_completion);
assert(error == CL_SUCCESS);
error =clEnqueueAcquireGLObjects(command_queue, 1, &accume_buffer2, 0,0,0);
assert(error == CL_SUCCESS);

error |= clSetKernelArg(computeKernel, 0, sizeof(cl_image), &accume_buffer);
assert( error == CL_SUCCESS);
error |= clSetKernelArg(computeKernel, 1, sizeof(cl_image), (void*)&accume_buffer2);
assert( error == CL_SUCCESS);


GLuint dimensions = 3;
size_t globalWorkSize[] = {LPV::LPV_WIDTH, LPV_HEIGHT, LPV_DEPTH };
size_t localWorkSize[] = { 1, 1, 1};

cl_event kernel_completion;
error = clEnqueueNDRangeKernel(command_queue, computeKernel, dimensions, NULL, globalWorkSize, nullptr, 0, NULL, &kernel_completion);
assert(error == CL_SUCCESS);
error = clWaitForEvents(1, &kernel_completion);
error |= clReleaseEvent(kernel_completion);
assert(error == CL_SUCCESS);


error = clEnqueueReleaseGLObjects(command_queue, 1, &accume_buffer, 0, 0, 0);
assert(error == CL_SUCCESS);
error = clEnqueueReleaseGLObjects(command_queue, 1, &accume_buffer2, 0, 0, 0);
assert(error == CL_SUCCESS);

clFlush(command_queue);
clFinish(command_queue);


}

这是运行opencl着色器的代码:

#pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable

const sampler_t lpvSampler = CLK_NORMALIZED_COORDS_FALSE | 
CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;


kernel void propagate_lpv
                    (
                      read_only image3d_t src_buffer,
                      write_only image3d_t dest_buffer
                    )
{
    int4 coord;
    coord.x = (int)get_global_id(0);
    coord.y = (int)get_global_id(1);
    coord.z = (int)get_global_id(2);
    coord.w = 1;

float4 color = float4(1.0f, 0.0f, 0.0f, 1.0f);
write_imagef(dest_buffer, coord, color);


}

最后,这是opencl着色器本身:

var string = "<h2><span style=\\\"color: #ff6600;\\\">sgsag<\/span><\/h2>";
var content = string.replace(/\\/g, '');
$('textarea').val(content);

有没有人知道为什么会这样?感谢

1 个答案:

答案 0 :(得分:2)

问题是我使用了错误的语法。可悲的是,OpenCL和GLSL似乎没有遵循我的假设相同的语法。根据OpenCL编程指南的正确语法是:

(float4)( float, float, float, float )
(float4)( float2, float, float )
(float4)( float, float2, float )
(float4)( float, float, float2 )
(float4)( float2, float2 )
(float4)( float3, float )
(float4)( float, float3 )
(float4)( float )

所以改变我的路线:

float4 color = float4(1.0f, 0.0f, 0.0f, 1.0f);

为:

float4 color = (float4)(1.0f, 0.0f, 0.0f, 1.0f);

给我一​​个红色的纹理,这是我所期待的。