• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1polyfrag  Members

Posted 22 March 2014 - 06:57 PM

I've separated the drawing and the sim logic of an RTS into two threads.

The problem is updating unit drawing position. The drawing framerate might be lower than the sim framerate, but the sim must be kept at 30 FPS. If I use a mutex with infinite wait time, that essentially slows down the sim to the same framerate as the drawing thread?

Is there a simple solution?

### #2IceCave  Members

Posted 22 March 2014 - 10:08 PM

I am trying to understand your problem first, which is a little bit difficult because I don't speak english very good (ironically for a software engineer).

> When I got it correctly you have multiple threads which are not independent from one another which is propably an artifact from your design decision to program it at the beginning as a single core game.

So to sinc them you could let your drawing thread wait in an endless loop to be invoked by the sim thread using a simple boolean or vice versa. So they would run both at the same time but one thread would wait for the other to finish.

> Your exact problem is that you have problems updating your unit drawing position. This is strange because shouldn't the position of your object be computed in your sim-thead?

I personally can't really locate the problem.

What happens when the drawing framerate is lower (or higher(?)) then the other thread?

### #3Hodgman  Moderators

Posted 22 March 2014 - 10:33 PM

POPULAR

If these are two decoupled threads, updating at different frequencies, then you need double-buffering. i.e. You need to be able to produce/store more than one set of drawing positions.
Each update, the update thread can lock one "buffer" and fill it with data. Each draw, you lock the most recently produced buffer available and draw from it.
You may find that you need 3 of these buffers. One that's full of valid data, ready for the next draw, one being used for drawing now, and one being used for updating now.

### #4polyfrag  Members

Posted 22 March 2014 - 10:39 PM

The problem is if I'm updating the unit position in the sim thread at the same time as I'm reading that position in the draw thread, it crashes.

What I'm thinking of doing is adding a render-state class to each object that gets update only if the sim thread can get a mutex lock. Otherwise it doesn't wait for it and the draw thread draws at a stale position or a unit at a different frame or something.

### #5Juliean  GDNet+

Posted 23 March 2014 - 06:03 AM

The problem is if I'm updating the unit position in the sim thread at the same time as I'm reading that position in the draw thread, it crashes.

Thats why Hodgman suggested multiple buffers, you update one of them while drawing from the other. After both threads have finished, you swap the buffers and repeat.

### #6polyfrag  Members

Posted 23 March 2014 - 06:18 PM

I see. That's what I'm doing. It's making my code ugly though, and I don't know if I will benefit.

There's problems with multithreading for me

- must set all the render-state buffer variables for each object each sim/update frame, wasting cpu cycles, insteading of just setting what changes

For example, for a building here are all the variables that are relevant to rendering:

	class RenderState
{
public:
Vec3f drawpos;
int type;
int stateowner;
int corpowner;
int unitowner;
bool finished;
int pownetw;
int crpipenetw;
};

Is it worth it?

 It's probably too much copying because entire vertex arrays might change during a frame.

Edited by polyfrag, 23 March 2014 - 07:00 PM.

### #7polyfrag  Members

Posted 23 March 2014 - 07:47 PM

In Doom 3 BFG, does the renderer frontend that sends draw commands to the renderer back end (that sends them to the GPU) also send a copy of the vertex arrays to draw? http://fabiensanglard.net/doom3_bfg/threading.php

### #8Scourage  Members

Posted 23 March 2014 - 08:44 PM

Another option is to change your threading model. You could run your update phase in a multithreaded fashion, using worker threads to update multiple groups of entities at the same time. Then if it's time to render the scene, create all your draw calls (also could be done in a bunch of worker treads) and send them off to the gpu.

Cheers,

Bob

Halfway down the trail to Hell...

### #9polyfrag  Members

Posted 23 March 2014 - 09:57 PM

run your update phase in a multithreaded fashion, using worker threads to update multiple groups of entities at the same time

The result of one unit's update might depend on another, so this isn't a solution for lockstep multiplayer games.

create all your draw calls (also could be done in a bunch of worker treads) and send them off to the gpu

Sending them off to the GPU in each worker thread? They share the same GL context and would have to constantly switch with wglMakeCurrent. Plus what if they require different shaders?

### #10polyfrag  Members

Posted 23 March 2014 - 10:01 PM

I've decided that there's too many problems with multithreading, so I'm going back to single-threading. Mutex locks everywhere, not knowing if I might be missing one somewhere...

### #11frob  Moderators

Posted 24 March 2014 - 02:24 AM

I've decided that there's too many problems with multithreading, so I'm going back to single-threading. Mutex locks everywhere, not knowing if I might be missing one somewhere...

Probably a good decision based on what you described.

Developing a multi-threaded game is an engineering exercise. It requires planning, and the execution requires discipline. It uses a lot of the "science" aspect of "computer science".

You must carefully partition tasks, coordinate communications between tasks, map tasks to processes, and ensure the protocols for communication are followed. There is an area of computer science fully dedicated to parallel processing and concurrent computing. People get doctorates in that sub-field.

A mutex is one potential part of the process of coordinating communications between tasks. If your code style just says "I think I need a mutex here" rather than specific rules about when and why you need or do not need them, then you probably have insufficient architecture for a multiprocessing engine.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #12Scourage  Members

Posted 24 March 2014 - 10:47 AM

Multithreading is hard, but it's not impossible.  You do need to think of your game slightly differently in terms of data and processes.  I like to do things with events, commands, and worker threads.  I don't have any mutexes or locks in my system, but I do design my data structures so that they are contention free for the duration of the worker thread's processing. I use lockless queues to pass events/messages between threads (typically updates from my entity system to the main thread for dispatching later in the frame/next frame).

The result of one unit's update might depend on another, so this isn't a solution for lockstep multiplayer games.

I've run into that before, what I've done is maintain several buckets of entities to update based on a parent.  I've never really got further than 4 or 5 deep.  I then update all the entities in bucket 1 (since they don't depend on each other or a parent), then 2 (their parents are all up to date), etc, etc.  This requires a little extra book keeping, but definitely possible.

Sending them off to the GPU in each worker thread? They share the same GL context and would have to constantly switch with wglMakeCurrent. Plus what if they require different shaders?

You don't have to actually call any GL commands from the worker threads, just get everything ready to draw (cull and uniform setup).  If you're doing some cool multidraw-indirect stuff, you can have each thread touch the command buffer, but that requires some rather specialized data structures and beyond what I know about.  In my rendering engine, I've encapsulated rendering of objects into a command class. Worker threads generate all the command classes in parallel (each renderable object generally caches and reuses it's render commands).  The command classes get their uniform data updated (not calling glUniform, just holding the vector/matrix ready to call glUniform).  When the worker threads are all done, I then combine each worker threads command list, sort by render state and then process the entire list calling exec on each command.  This is where the GL calls are actually made.  You could even push the entire sorted command list into a separate thread just for rendering.

Cheers,

Bob

Halfway down the trail to Hell...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.