OpenGL and Threads

BlitzMax Forums/BlitzMax Programming/OpenGL and Threads

Stu_ovine(Posted 2009) [#1]
I take it you cant use threads to update graphics using OpenGL ?


DX version shows output

Strict
Import pub.threads
Graphics 800,600	
Local loader:Int = CreateThread(Progress, Null)
Delay(3000)
DetachThread(loader) 
Delay 10
Function Progress:Object(data:Object)
		Cls
		SetColor 255,0,0
		DrawRect 0,0,30,30
		DrawText "TEST", 50, 50
		Flip 
End Function



OpenGl does not

Strict
Import pub.threads
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600	
Local loader:Int = CreateThread(Progress, Null)
Delay(3000)
DetachThread(loader) 
Delay 10
Function Progress:Object(data:Object)
		Cls
		SetColor 255,0,0
		DrawRect 0,0,30,30
		DrawText "TEST", 50, 50
		Flip 
End Function



Gabriel(Posted 2009) [#2]
AFAIK you shouldn't do it with either. DirectX, at least up to and including version 9 is not threadsafe. There was talk of this changing, but I can't recall if it was DX10 or DX11. I think 11.


Brucey(Posted 2009) [#3]
Interestingly, someone posted a tweak to the Mac GL code a while back which enabled GL rendering to run via threads - not *your* threads, mind you, but an improvement nonetheless (although it does depend on how you use the render pipeline).

But Gabriel is right. You can only use it from the main thread.


plash(Posted 2009) [#4]
So does this make it impossible to load images in separate threads? (Fairly certain it does for OpenGL TImages - don't they get turned into a gltexture name?)


Brucey(Posted 2009) [#5]
You can load pixmaps in separate threads... and then batch convert them to TImage when they are loaded?


Stu_ovine(Posted 2009) [#6]
Ive change the test code to only load the images in the Thread now but it randomly crashes etc. I was only going for an animated display whilst level media was loading, I'll do it the old fashioned way instead.


Gabriel(Posted 2009) [#7]
Flip it around. Animate the display in your main thread and load media in the new thread. I guess you'll have to do it in such a way that you're doing an intermediate load, so as not to interfere with OpenGL. That's to say, you would load from disk to memory, but not actually create any OpenGL Objects. But then if everything is in memory, it should only take a second or two to create the OpenGL objects from memory, so I think it ought to work acceptably. I was hoping to have tested this theory out by now but despite assurances that BlitzMax updates would be bundled up officially a bit more often, they haven't been.


JoshK(Posted 2009) [#8]
There is only one pipeline from the CPU to the GPU anyways, so in DX11 the "multithreaded" rendering is really just adding the commands to a queue to send to the GPU as they would normally be sent.