Page 1 of 1

Engine 3D Multi-threading

Posted: Mon May 27, 2013 12:09 pm
by Olby
Perhaps someone in the know can tell me if PB-OGRE implementation is thread safe?

I tried this code (with "create threadsafe executable") and it occasionally crashes on RenderWorld().
When OpenGL subsystem is used it crashes straight away (application stopped responding).
So my assumption is that OGRE is not threadsafe or at least it's PB implementation.

I would appreciate if anyone can clarify this. Thanks.

Code: Select all

#FlashSpeed=2000

Procedure th1(*ptr)
	Repeat
		Debug 2
		;CreateCube(1,10)
		LoadMesh(1,#PB_Compiler_Home+"Examples\3D\Data\Models\robot.mesh")
		CreateEntity(1,MeshID(1),0,0,0,0)
		Delay(#FlashSpeed)
		If IsEntity(1):FreeEntity(1):EndIf
		If IsMesh(1):FreeMesh(1):EndIf
		Delay(#FlashSpeed)
	ForEver
EndProcedure

Procedure th2(*ptr)
	Repeat
		Debug 3
		If IsEntity(2):FreeEntity(2):EndIf
		If IsMesh(2):FreeMesh(2):EndIf
		Delay(#FlashSpeed)
		CreateSphere(2,5)
		CreateEntity(2,MeshID(2),0,0,0,0)
		Delay(#FlashSpeed)
	ForEver
EndProcedure

#CameraSpeed = 1

If InitEngine3D(#PB_Engine3D_DebugOutput)
	InitSprite()
	InitKeyboard()
	InitMouse()
	
	If OpenWindow(1,-1,-1,640,480,"Multi-threading using PB-OGRE",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
		AntialiasingMode(#PB_AntialiasingMode_x2)
		If OpenWindowedScreen(WindowID(1),0,0,640,480)
			
			Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\",#PB_3DArchive_FileSystem)
			
			CreateCamera(0,0,0,640,480)
			CameraRenderMode(0,#PB_Camera_Wireframe)
			MoveCamera(0,0,0,40,#PB_Absolute)
			
			CreateThread(@th1(),10)
			CreateThread(@th2(),10)
			
			Repeat
				
	      If ExamineMouse()
	        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
	        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
	      EndIf
	      
	      If ExamineKeyboard()
	        
	        If KeyboardPushed(#PB_Key_Left)
	          KeyX = -#CameraSpeed 
	        ElseIf KeyboardPushed(#PB_Key_Right)
	          KeyX = #CameraSpeed 
	        Else
	          KeyX = 0
	        EndIf
	                  
	        If KeyboardPushed(#PB_Key_Up)
	          KeyY = -#CameraSpeed 
	        ElseIf KeyboardPushed(#PB_Key_Down)
	          KeyY = #CameraSpeed 
	        Else
	          KeyY = 0
	        EndIf
	
	      EndIf
	      
	      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
	      MoveCamera  (0, KeyX, 0, KeyY)
				
				RenderWorld()
				FlipBuffers()
			Until KeyboardPushed(#PB_Key_Escape) ;WindowEvent()=#PB_Event_CloseWindow
			
		EndIf
	EndIf
EndIf

Re: Engine 3D Multi-threading

Posted: Mon May 27, 2013 8:08 pm
by PMV
Jep, PB-OGRE is not threadsafe, everything related to PB-OGRE needs to be in the same thread.
But this could change in future when DX10/11 is supported. :D

MFG PMV

Re: Engine 3D Multi-threading

Posted: Mon May 27, 2013 10:01 pm
by Olby
Bad news I guess. I was expecting to load my media (textures/meshes) in a separate thread.

Re: Engine 3D Multi-threading

Posted: Tue Jan 14, 2014 11:50 am
by PMV
there haven't changed anything and it doesn't seem to change in future
This quote is a little bit old, but i can't find anything against that:

http://www.ogre3d.org/forums/viewtopic. ... 47#p466555
CABAListic on Aug 16, 2012 wrote:[...] Making Ogre fully thread-safe is problematic and will likely interfere with other planned performance-related core changes. So even though DirectX and OpenGL may be able to run in a thread-safe manner, Ogre will not. [...]
MFG PMV

Re: Engine 3D Multi-threading

Posted: Tue Jan 14, 2014 11:57 am
by Bananenfreak
There are several things you can load in a thread. I created a program which loaded terrain in multiple threads. But it needed same time to create the whole terrain... so it was useless.