diff --git a/src/bgfx.cpp b/src/bgfx.cpp index a57a57dba..c1ecf925a 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1441,6 +1441,8 @@ namespace bgfx frameNoRenderWait(); + uint16_t idx = m_encoderHandle.alloc(); + BX_CHECK(0 == idx, "Internal encoder handle is not 0 (idx %d).", idx); BX_UNUSED(idx); m_encoder[0].begin(m_submit, 0); m_encoder0 = reinterpret_cast(&m_encoder[0]); @@ -1529,7 +1531,7 @@ namespace bgfx getCommandBuffer(CommandBuffer::RendererShutdownEnd); frame(); - m_encoder[0].end(); + m_encoder[0].end(true); m_dynVertexBufferAllocator.compact(); m_dynIndexBufferAllocator.compact(); @@ -1690,14 +1692,14 @@ namespace bgfx { bx::MutexScope scopeLock(m_encoderApiLock); - if (BGFX_CONFIG_MAX_ENCODERS == m_numEncoders ) + uint16_t idx = m_encoderHandle.alloc(); + if (kInvalidHandle == idx) { return NULL; } - uint8_t idx = uint8_t(m_numEncoders++); encoder = &m_encoder[idx]; - encoder->begin(m_submit, idx); + encoder->begin(m_submit, uint8_t(idx) ); } #endif // BGFX_CONFIG_MULTITHREADED @@ -1710,9 +1712,9 @@ namespace bgfx if (BGFX_API_THREAD_MAGIC != s_threadIndex) { EncoderImpl* encoder = reinterpret_cast(_encoder); - encoder->end(); + encoder->end(true); - m_encoderApiSem.post(); + m_encoderEndSem.post(); } #else BX_UNUSED(_encoder); @@ -1721,7 +1723,7 @@ namespace bgfx uint32_t Context::frame(bool _capture) { - m_encoder[0].end(); + m_encoder[0].end(true); #if BGFX_CONFIG_MULTITHREADED bx::MutexScope resourceApiScope(m_resourceApiLock); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 78f7ef707..ba3264c14 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1999,12 +1999,15 @@ namespace bgfx m_numDropped = 0; } - void end() + void end(bool _finalize) { - UniformBuffer* uniformBuffer = m_frame->m_uniformBuffer[m_uniformIdx]; - uniformBuffer->finish(); + if (_finalize) + { + UniformBuffer* uniformBuffer = m_frame->m_uniformBuffer[m_uniformIdx]; + uniformBuffer->finish(); - m_cpuTimeEnd = bx::getHPCounter(); + m_cpuTimeEnd = bx::getHPCounter(); + } if (BX_ENABLED(BGFX_CONFIG_DEBUG_OCCLUSION) ) { @@ -2581,8 +2584,7 @@ namespace bgfx struct Context { Context() - : m_numEncoders(1) - , m_render(&m_frame[0]) + : m_render(&m_frame[0]) , m_submit(&m_frame[BGFX_CONFIG_MULTITHREADED ? 1 : 0]) , m_numFreeDynamicIndexBufferHandles(0) , m_numFreeDynamicVertexBufferHandles(0) @@ -4398,27 +4400,33 @@ namespace bgfx void encoderApiWait() { - for (uint32_t ii = 1, num = m_numEncoders; ii < num; ++ii) + uint16_t numEncoders = m_encoderHandle.getNumHandles(); + + for (uint16_t ii = 1; ii < numEncoders; ++ii) { - m_encoderApiSem.wait(); + m_encoderEndSem.wait(); } - for (uint32_t ii = 0, num = m_numEncoders; ii < num; ++ii) + for (uint16_t ii = 0; ii < numEncoders; ++ii) { - m_encoderStats[ii].cpuTimeBegin = m_encoder[ii].m_cpuTimeBegin; - m_encoderStats[ii].cpuTimeEnd = m_encoder[ii].m_cpuTimeEnd; + uint16_t idx = m_encoderHandle.getHandleAt(ii); + m_encoderStats[ii].cpuTimeBegin = m_encoder[idx].m_cpuTimeBegin; + m_encoderStats[ii].cpuTimeEnd = m_encoder[idx].m_cpuTimeEnd; } - m_submit->m_perfStats.numEncoders = uint8_t(m_numEncoders); - m_numEncoders = 1; + m_submit->m_perfStats.numEncoders = uint8_t(numEncoders); + + m_encoderHandle.reset(); + uint16_t idx = m_encoderHandle.alloc(); + BX_CHECK(0 == idx, "Internal encoder handle is not 0 (idx %d).", idx); BX_UNUSED(idx); } bx::Semaphore m_renderSem; bx::Semaphore m_apiSem; - bx::Semaphore m_encoderApiSem; + bx::Semaphore m_encoderEndSem; bx::Mutex m_encoderApiLock; bx::Mutex m_resourceApiLock; - bx::Thread m_thread; + bx::Thread m_thread; #else void apiSemPost() { @@ -4450,6 +4458,7 @@ namespace bgfx Encoder* m_encoder0; EncoderImpl m_encoder[BGFX_CONFIG_MAX_ENCODERS]; uint32_t m_numEncoders; + bx::HandleAllocT m_encoderHandle; Frame m_frame[1+(BGFX_CONFIG_MULTITHREADED ? 1 : 0)]; Frame* m_render;