Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenGL/OpenCL interop texture render issues, and shared interop architecture abstraction could be improved. #1016

Closed
goofyseeker311 opened this issue Nov 4, 2024 · 5 comments

Comments

@goofyseeker311
Copy link

goofyseeker311 commented Nov 4, 2024

Question

What is missing in the code? the normal code works with int[] pixel buffed being loaded to the opengl texture (is base buffer rendering and display code is working), but when reimplemented to using the interop sharing mode between opencl and opengl, the final image is not showing up. I'm not implementing any other synchronization than CL10GL.clEnqueueAcquireGLObjects() and CL10GL.clEnqueueReleaseGLObjects() before and after the buffer write operations. CL10GL.clCreateFromGLTexture2D() function does not crash and returns a non-zero buffer long pointer.

Parts of the relevant java/opengl code:

createQuadProgram();
createFullScreenQuad();
int tex = createTexture(this.graphicswidth,this.graphicsheight);

long glbuffer = CL12GL.clCreateFromGLTexture2D(context, CL12.CL_MEM_READ_WRITE, GL31.GL_TEXTURE_2D, 0, texture, errcode_ret);
CL12GL.clEnqueueAcquireGLObjects(queue, vmem, null, null);
...
//clEnqueueNDRangeKernel() and buffer edits on the OpenCL side. waits for queue to finish.
...
CL12GL.clEnqueueReleaseGLObjects(queue, vmem, null, null);

while(!GLFW.glfwWindowShouldClose(window)) {
	GL31.glClear(GL31.GL_COLOR_BUFFER_BIT | GL31.GL_DEPTH_BUFFER_BIT);
	GL31.glViewport(0, 0, graphicswidth, graphicsheight);
	GL31.glUseProgram(quadProgram);
	GL31.glBindVertexArray(vao);
	GL31.glDrawArrays(GL31.GL_TRIANGLES, 0, 6);
	GL31.glBindVertexArray(0);
	GL31.glUseProgram(0);
	GLFW.glfwSwapBuffers(window);
	GLFW.glfwPollEvents();
}

opencl init code:

PointerBuffer clCtxProps = clStack.mallocPointer(7);
//clCtxProps.put(0, CL12.CL_CONTEXT_PLATFORM).put(2, 0);
switch (Platform.get()) {
case WINDOWS:
	clCtxProps
	.put(KHRGLSharing.CL_GL_CONTEXT_KHR)
	.put(GLFWNativeWGL.glfwGetWGLContext(window))
	.put(KHRGLSharing.CL_WGL_HDC_KHR)
	.put(WGL.wglGetCurrentDC());
	break;
case FREEBSD:
case LINUX:
	clCtxProps
	.put(KHRGLSharing.CL_GL_CONTEXT_KHR)
	.put(GLFWNativeGLX.glfwGetGLXContext(window))
	.put(KHRGLSharing.CL_GLX_DISPLAY_KHR)
	.put(GLFWNativeX11.glfwGetX11Display());
	break;
case MACOSX:
	clCtxProps
	.put(APPLEGLSharing.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE)
	.put(CGL.CGLGetShareGroup(CGL.CGLGetCurrentContext()));
}
clCtxProps.put(CL12.CL_CONTEXT_PLATFORM).put(platform).put(NULL).flip();
...
long context = CL12.clCreateContext(clCtxProps, device, (CLContextCallback)null, NULL, errcode_ret);

NOTE: instead of having to put the platform specific init code from the programmer side, it should be abstracted by the lwjgl side automatically. so that you dont have to deal with any platform specifics.

@goofyseeker311
Copy link
Author

goofyseeker311 commented Nov 4, 2024

I just spotted that there were some old clCtxProps.put() statements that would make the clCtxProps incorrect.

edit: removing the old clCtxProps code did not make any difference. is the opengl side synchronization missing a key? there are no frames, being show, not even some. just a black screen.

edit: I added synchronization waiting to the opencl queue. no effect. the opencl program kernel seems not to be running. at all.

edit: changing the argument image buffer to the opengl texture buffer results in clEnqueueNDRangeKernel error -52, ie invalid arguments.

@goofyseeker311
Copy link
Author

goofyseeker311 commented Nov 4, 2024

(why the memory buffer sharing interop is not using the normal buffer address kernel argument system?)
the other part of the arguments are defined in separate place from the function call in the lwjgl opengl interop demo.

edit: the opengl texture buffer is of the correct format, so whats up? and the opencl image long address is created successfully.
edit: adding glFlush() or glFinish() to the end of the render loop and then waiting for the opencl program to run does not change anything.

edit: even the acquire and release globjects() function succeeds. only the kernel parameter setting and running fails. also the image buffer is the same pointer type that the previous int[] array was. always using the setkernelarg1p() function.

edit: even specifically unbinding the created texture after creation and after a render pass, nothing happens.

note: the lwjgl opengl interop opengl opencl demo works tho. so whats the issue.

@goofyseeker311
Copy link
Author

goofyseeker311 commented Nov 5, 2024

I'm not calling the acquire or release functions from the opengl thread. Would that break everything. Opencl is not thread locked.

I moved all opengl and opencl buffer stuff to the main render loop/thread. now it crash-dumps on enqueuefillbuffer. just after enqueueacquireglobjects. no it actually crashes on waitforevents() function not on the fillbuffer() function. someone said the event stuff is different in gl and in cl. opencl kernel still not running. error is still -52 in the enqueuendrangekernel call.

@goofyseeker311
Copy link
Author

goofyseeker311 commented Nov 5, 2024

Well, opengl buffer sharing works, but the exact same code minus the buffer copy to texture, does not work. so whatever is going on with textures interop sharing from opengl is not working.

@goofyseeker311
Copy link
Author

goofyseeker311 commented Nov 7, 2024

as a side note, amd 5600g igpu failed to create shared device context, even if it advertised khr_sharing as capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant