diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index c20dcafd00..8a71043844 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -69,6 +69,7 @@ DrawEngineCommon::DrawEngineCommon() : decoderMap_(32) { } if (useDepthRaster_) { depthDraws_.reserve(256); + depthThread_ = std::thread([this]() { DepthThreadFunc(); }); } } @@ -87,6 +88,11 @@ DrawEngineCommon::~DrawEngineCommon() { delete decoder; }); ClearSplineBezierWeights(); + if (depthThread_.joinable()) { + exitDepthThread_ = true; + depthEnqueueCond_.notify_one(); + depthThread_.join(); + } } void DrawEngineCommon::Init() { @@ -1071,6 +1077,18 @@ void DrawEngineCommon::FlushQueuedDepth() { inDepthDrawPass_ = false; } +void DrawEngineCommon::DepthThreadFunc() { + while (true) { + { + std::unique_lock lock(depthEnqueueMutex_); + depthEnqueueCond_.wait(lock); + if (exitDepthThread_) { + break; + } + } + } +} + void DrawEngineCommon::DepthRasterTransform(GEPrimitiveType prim, VertexDecoder *dec, uint32_t vertTypeID, int vertexCount) { if (!gstate.isModeClear() && (!gstate.isDepthTestEnabled() || !gstate.isDepthWriteEnabled())) { return; diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index ab1e29d7b3..efd464cae1 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -18,6 +18,8 @@ #pragma once #include +#include +#include #include "Common/CommonTypes.h" #include "Common/Data/Collections/Hashmaps.h" @@ -365,6 +367,7 @@ protected: private: // Internal implementation details. + void DepthThreadFunc(); inline void EnqueueDepthDraw(const DepthDraw &draw); inline void ProcessDepthDraw(const DepthDraw &draw); @@ -381,5 +384,14 @@ private: double depthRasterPassStart_ = 0.0; + std::thread depthThread_; + bool exitDepthThread_ = false; // notify depthEnqueueCond_ to check this. + bool inDepthDrawPass_ = false; + std::mutex depthEnqueueMutex_; + std::condition_variable depthEnqueueCond_; + + bool processingDepthDraw_ = false; + std::mutex depthFinishMutex_; + std::condition_variable depthFinishCond_; };