Some questions about multithreading

Started by
2 comments, last by Nickie 11 years, 6 months ago
So I'll jump from first time into the world of multiprogramming!
I'm not here to ask the general questions about the thread safety.. after all the internet is full of information.
I want to ask how shoud I make the renderer... I currently have a scene graph which is only accessed by the renderer by default. (I'm jsut passing the scene graph to the renderer and it will do it's job. Or I can also just iterate through it and call renderer->DrawMesh(this/iterator);
So my question is, shoud I make a queue and jsut queue what is for drawing and whats not? I don't like this options cuz its actaully useless. After all thats what is done in the video card...
OR I shoud make an active thread, which is non-stop iteration by iteself in the scene graph and rendering it?. ANd where shoud I put the window creation. In the rendering thread? OR I shoud create the window in main thread and pass the handle so I can create the d39 device?
Any other advices I missed to ask for and you think they will be useful for me?
Thanks!
Advertisement
Rendering in the main thread is usually the best option (since you have your window there and stuff). That way, you don't have to share the graphics API resources. I would recommend using separate threads only for computation-heavy and independent tasks, e. g. physics. Otherwise, multithreading just brings problems and doesn't speed up the tasks much.
I've read you post couple of times and it's a bit incoherent. Could you elaborate a lot, like what you've done, or planning to do?

IMO abstracted scene graphs are not practical. It is much more easy to have multiple typed object spatial trees.

Lets talk about 4 core CPU on D3D11. Multithreaded 3d graphics takes a lot of discipline. First, you only make so many threads the current CPU has cores, the worker threads. You dont want context switches. Some physics and audio libraries spawn own threads, so you end up with more threads than cores in the process. Then you build the engine on tasks. Tasks will have dependecies to other tasks. Update task must become before frustum culling task for lights, before shadow rendering task, which must become before lighting pass where the shadow is used. But you can render G-buffer simultaneusly, just after the update task. There will be a task scheduler, which feeds the taks to queues. Main thread will create the window, schedule tasks and add couple synchronization points.

It's efficient to have 4 task queues, one per thread, consumers (worker threads) does not need locking for fetching tasks. If the dependencies are correct, there is no need for synchronization (mutexes or lockfree) for any data access. When the queues are populated with tasks for the frame, main thread can join work (you only need 3 threads plus main). Most important thing is to make your codebase more functional, and all the shared data must be made immutable. I assure you, you can make 32 core multithreaded d3d11 graphics engine with 0 mutexes, no lock-free code (lock-free will trash all cache), which keeps all cores fully utilized.

Ofcourse I'm only talking about using deferred ID3D11DeviceContext. It just emulates the multithreading for commands you feed it, you could very well create own mechanism for it. The graphics driver will play back all the commands sequantially anyway. (though the driver has like 3 frames worth of commands to keep GPU busy, say thousands of draw calls queued up)

But one doesnt always need to multithread. If you spam the D3D API with single thread, some graphics drivers are multithreaded, and they will spread the load (assuming one can push the bottleneck/load to the driver).
Hello,
Sorry for reading my post. I actually had many things to do at this time and didn't pay much attention to my question.
Your post answered most of my questions. Thank you.
Lets talk about 4 core CPU on D3D11.[/quote]
First of all, I'm using direct3d9. My video card does not support DX11 ;).
IMO abstracted scene graphs are not practical. It is much more easy to have multiple typed object spatial trees.[/quote]
My plan was actually to start with a scene graph and turn it in BSP tree, or something like this.

So what I understood is:
1. I don't need rendering thread. Main thread would be able to do the job.
2. I need just worker threads. Each with each own task queues.

  • File loading thread. Loads and decopress files and put the in the memory.
  • Asset Manager. Use the already loaded files and create engine objects which can be used in real code directly. (teapot.obj -> Mesh). A question: how can i create a buffer in this thread? Do i need second d3d9 device or the function is multithreaded.

These are the threads I'm thinking about right now. I actually have no idea how can I split the physics in another thread. Won't I spam the queue with too many messages?
So... I'll put everything in the main thread except the file related tasks.

This topic is closed to new replies.

Advertisement