GameTechDev/MaskedOcclusionCulling

thread safe?

PooyaEimandar opened this issue · 2 comments

Hi,
Is CullingThreadpool thread-safe and can be called from multiple threads to speed up processing? I tested with intel tbb parallell_for and I'v got following error "Access violation reading location"

Hi Pooya,

The CullingThredpool is not thread safe, with the exception of the TestRect() method. It's asynchronous and designed to work similar to GL/DX, so calling RenderTriangles() only queues up render work and is actually very inexpensive to call. The intended use is with one master thread, interfacing with CullingThredpool, which internally has worker threads that does the actual occluder rendering. I'm not sure you'll see any benefit from interfacing with the threadpool from multiple threads (you'll compete for the same resources as the worker threads), but if you really need to do so you'll have to put a mutex / critical section around the calling code and serialize the calls.

There are two additional caveats to calling the threadpool from multiple threads. First, the CullingThredpool class is state based, so for example the transform matrix and vertex layout are set as part of the state. The RenderTriangles() will use whatever state is active when called, so you must ensure that no other thread can come in and alter the state before the RenderTriangles() method has finished (i.e. your critical section begins before setting the first state, and ends after RenderTriangles() has finished). Second, the CullingThredpool class will perform work in the order of submission. Submitting work from multiple threads may cause non-deterministic ordering (timing dependent). Usually this is not a problem, but it may cause fluctuations in performance. In general, best practice/perf is to submit work in near-to-far order if possible.

Regards,
Jon

Hi Jon
Thank you for the help, really appreciated.