hello @everyone, i’m trying to write a program where i calculate a 2D texture each frame via OpenCLand display it with OpenGL. I already did something similar with vertex buffers, where I manipulated the vertices each frame with OpenCL and rendered them with OpenGL, and it worked fine - however, I’m having serious issues getting the interop textures to work.
I create an OpenGL 2D texture:
glGenTextures(1, &texID[0]);
glBindTexture(GL_TEXTURE_2D, texID[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_FLOAT, NULL);
glGenerateMipmap(GL_TEXTURE_2D);
and afterwards, an OpenCL 2D image from the texture:
cl::Image2DGL resultTex = cl::Image2DGL(context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, texID[0], &result);
if (result != CL_SUCCESS) {...}
each frame, I acquire the texture, execute my kernel & release the texture again:
cl_int result = CL_SUCCESS;
cl::Event ev;
cl::NDRange globalRange = cl::NDRange(256, 256);
cl::NDRange localRange = cl::NDRange(16, 16);
std::vector<cl::Memory> memObjs;
memObjs.clear();
memObjs.push_back(resultTex);
result = queue.enqueueAcquireGLObjects(&memObjs, NULL, &ev);
ev.wait();
if (result != CL_SUCCESS) {...}
result = queue.enqueueNDRangeKernel(kernelWriteTest, cl::NullRange, globalRange, localRange, NULL, &ev);
ev.wait();
if (result != CL_SUCCESS) {...}
result = queue.enqueueReleaseGLObjects(&memObjs, NULL, &ev);
ev.wait();
if (result != CL_SUCCESS) {...}
glGenerateMipmap(GL_TEXTURE_2D);
the simplified kernel looks like this:
__kernel void testKernelWrite(__write_only image2d_t resultTexture)
{
int2 imgCoords = (int2)(get_global_id(0), get_global_id(1));
float4 imgVal = (float4)(1.0f, 1.0f, 0.0f, 1.0f);
write_imagef(resultTexture, imgCoords, imgVal);
}
basically, the program compiles & runs without problems - however, the color of the texture when rendered with OpenGL never changes, no matter how many times I execute the kernel. i.e. if I pass NULL during texture creation, the texture stays black; if i initialize it with some image, rendering the texture will show the initial image and nothing else.
I also wrote a 2nd kernel, which I too executed each frame after releasing & acquiring the shared texture again:
#pragma OPENCL EXTENSION cl_amd_printf : enable
__kernel void testKernelRead(__read_only image2d_t inputTexture)
{
int2 imgCoords = (int2)(get_global_id(0), get_global_id(1));
printf("%d %d ", imgCoords.x, imgCoords.y);
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
float4 imgVal = read_imagef(inputTexture, smp, imgCoords);
printf("%f %f %f %f
", imgVal.x, imgVal.y, imgVal.z, imgVal.w);
}
weirdly, when executing this kernel, it prints the correct values…
I really hope someone can help me with this problem (: