From 11729403f47affecc0e4ad89882787daa8061f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 20 Dec 2018 20:18:30 -0800 Subject: [PATCH] Metal: Invalidate PSO when program is destroyed. Issue #1585. --- src/renderer_mtl.h | 14 +++++++------- src/renderer_mtl.mm | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/renderer_mtl.h b/src/renderer_mtl.h index 0084fb859..ddc688328 100644 --- a/src/renderer_mtl.h +++ b/src/renderer_mtl.h @@ -778,6 +778,13 @@ namespace bgfx { namespace mtl uint32_t m_hash; }; + struct SamplerInfo + { + uint32_t m_index; + UniformHandle m_uniform; + bool m_fragment; + }; + struct ProgramMtl { ProgramMtl() @@ -812,13 +819,6 @@ namespace bgfx { namespace mtl uint32_t m_fshConstantBufferSize; uint32_t m_fshConstantBufferAlignmentMask; - struct SamplerInfo - { - uint32_t m_index; - bgfx::UniformHandle m_uniform; - bool m_fragment; - }; - SamplerInfo m_samplers[BGFX_CONFIG_MAX_TEXTURE_SAMPLERS]; uint32_t m_samplerCount; diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index af3cb1c65..b3d65619a 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -634,6 +634,7 @@ namespace bgfx { namespace mtl m_gpuTimer.shutdown(); m_pipelineStateCache.invalidate(); + m_pipelineProgram.clear(); for (uint32_t ii = 0; ii < BX_COUNTOF(m_shaders); ++ii) { @@ -763,6 +764,19 @@ namespace bgfx { namespace mtl void destroyProgram(ProgramHandle _handle) override { + for (PipelineProgramArray::iterator it = m_pipelineProgram.begin(); it != m_pipelineProgram.end();) + { + if (it->program.idx == _handle.idx) + { + m_pipelineStateCache.invalidate(it->key); + it = m_pipelineProgram.erase(it); + } + else + { + ++it; + } + } + m_program[_handle.idx].destroy(); } @@ -2041,6 +2055,7 @@ namespace bgfx { namespace mtl } m_pipelineStateCache.add(hash, pso); + m_pipelineProgram.push_back({hash, _program}); } return pso; @@ -2161,6 +2176,15 @@ namespace bgfx { namespace mtl UniformRegistry m_uniformReg; void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS]; + struct PipelineProgram + { + uint64_t key; + ProgramHandle program; + }; + + typedef stl::vector PipelineProgramArray; + + PipelineProgramArray m_pipelineProgram; StateCacheT m_pipelineStateCache; StateCacheT m_depthStencilStateCache; StateCacheT m_samplerStateCache; @@ -3991,7 +4015,7 @@ namespace bgfx { namespace mtl for (uint32_t sampler = 0; sampler < program.m_samplerCount; ++sampler) { - ProgramMtl::SamplerInfo& samplerInfo = program.m_samplers[sampler]; + SamplerInfo& samplerInfo = program.m_samplers[sampler]; UniformHandle handle = samplerInfo.m_uniform; int stage = *((int*)m_uniforms[handle.idx]);