Is there a way to safely bind texture using OpenGL in a worker thread of Qt GUI application?

A OpenGL context may be active in only one thread at a time. In general multithreaded OpenGL operation usually becomes a major nightmare, to get right. In your case what you intend to do is delegating resource loading.In the old days, before there were buffer object you'd have done this by creating a helper context and share its "lists" with the main context.

I will also perform binding using OpenGL glBindtexture() immediately after loading each image or texture so that I only need to save texture IDs in each SceneObject instance. When I tried to perform the load in a worker thread, the whole program would just crash. A OpenGL context may be active in only one thread at a time.In general multithreaded OpenGL operation usually becomes a major nightmare, to get right.

In your case what you intend to do is delegating resource loading. In the old days, before there were buffer object you'd have done this by creating a helper context and share its "lists" with the main context. Today we have something better: Buffer objects.

A buffer object allows you to send data to OpenGL in a asynchronous way. It goes along the likes of glGenBuffers(...); glBindBuffer(...); glBufferData(..., size, usage); void *p = glMapBuffer(...); memcpy(p, data, size); glUnmapBuffer(...); glTexImage / glDrawPixels / etc.The important part to understand is, that the address space allocated by glMapBuffer is shared across threads. So you can tell the OpenGL context in the main thread to map a buffer object, sending a signal to your worker thread, with the allocation.

The worker thread then fills in the data and upon finishing sends a signal to the OpenGL context thread to unmap. EDIT for multithreading So to do this you'd implement some signal handlers on both sides (pseudocode) signal OpenGLThread::mapPixelBufferObjectForWrite(ImageLoader il): glGenBuffers(1, &self. BufferId) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self.

BufferId) glBufferData(GL_PIXEL_UNPACK_BUFFER, il. UnpackedImageSize, NULL, GL_STATIC_DRAW) BufferObjectMapping map(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)) send_signal(target_thread = workerthread, target_signal_handler = workerthread::loadImageToBuffer(map, il), signal_on_finish = self. UnmapPixelBufferObjectWriteFinishedGenTexture(map)) signal IOThread::loadImageToBuffer(BufferObjectMapping map, ImageLoader il): /* ... */ signal OpenGLThread::unmapPixelBufferObjectWriteFinishedGenTexture(BufferObjectMapping map, ImageLoader il): if(map.

Mapping_target == GL_PIXEL_UNPACK_BUFFER) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self. BufferId) glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER) glGenTextures(1, &il. TextureId) glBindTexture(il.

Target, il. TextureId) for mipmaplevel in il. Levels glTexImage2D(il.

Target, mipmaplevel, il. Internalformat, il. Width, il.

Height, il. Border, il. Format, il.

Type, mipmaplevel. Offset).

Don't forget that buffers only work for pixel uploads if you bind them to the GL_UNPACK_BUFFER target. – Nicol Bolas Sep 13 at 10:19 @datenwolf: Sorry I do not quite understand the pseudocode. Are all these signal handlers called on the worker thread or main thread?

– ksming Sep 14 at 1:52 Does the use of Buffer Objects require me to add OpenGL extensions via GLEW for example to my Qt project? I'm trying to avoid adding external libraries and make use of Qt OpenGL module instead. – ksming Sep 14 at 6:02 @ksming: I addeed namespace designators to the pseudocode, to highlight it, but actually if looking closely to the signal sending you could tell from those, already.

+++ Yes you'll need to use the OpenGL extension mechanism on Windows to make use of Buffer Objects, like you need to use extensions for any kind of modern OpenGL. You can compile and link GLEW statically into your program. – datenwolf Sep 14 at 7:33 Thanks for the clarification.

I will try to implement the method you have suggested in Qt and see how it goes. – ksming Sep 14 at 7:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions