From 6c5784934d161f21f9820cd2320e085d23df2a9b Mon Sep 17 00:00:00 2001 From: Hyblocker Date: Sat, 2 Nov 2024 16:45:02 +0100 Subject: [PATCH] (fix): Remove m_geometry_cache As per suggestion 1 from wh1t3lord --- Backends/RmlUi_Renderer_DX11.cpp | 161 ++++++++++++------------------- Backends/RmlUi_Renderer_DX11.h | 2 - 2 files changed, 63 insertions(+), 100 deletions(-) diff --git a/Backends/RmlUi_Renderer_DX11.cpp b/Backends/RmlUi_Renderer_DX11.cpp index 35f6fa9cc..e13fcb399 100644 --- a/Backends/RmlUi_Renderer_DX11.cpp +++ b/Backends/RmlUi_Renderer_DX11.cpp @@ -831,18 +831,6 @@ static void DestroyShaders(const ProgramData& data) } // namespace Gfx -static uintptr_t HashPointer(uintptr_t inPtr) -{ - uintptr_t Value = (uintptr_t)inPtr; - Value = ~Value + (Value << 15); - Value = Value ^ (Value >> 12); - Value = Value + (Value << 2); - Value = Value ^ (Value >> 4); - Value = Value * 2057; - Value = Value ^ (Value >> 16); - return Value; -} - RenderInterface_DX11::RenderInterface_DX11() {} RenderInterface_DX11::~RenderInterface_DX11() {} @@ -1115,17 +1103,16 @@ void RenderInterface_DX11::Init(ID3D11Device* p_d3d_device) void RenderInterface_DX11::Cleanup() { - if (program_data) + if (m_fullscreen_quad_geometry) { - Gfx::DestroyShaders(*program_data); - program_data.reset(); + RenderInterface_DX11::ReleaseGeometry(m_fullscreen_quad_geometry); + m_fullscreen_quad_geometry = {}; } - // Loop through geometry cache and free all resources - for (auto& it : m_geometry_cache) + if (program_data) { - DX_CLEANUP_RESOURCE_IF_CREATED(it.second.vertex_buffer); - DX_CLEANUP_RESOURCE_IF_CREATED(it.second.index_buffer); + Gfx::DestroyShaders(*program_data); + program_data.reset(); } // Cleans up all general resources @@ -1211,7 +1198,6 @@ void RenderInterface_DX11::BeginFrame(ID3D11RenderTargetView* p_render_target_vi m_d3d_context->ClearRenderTargetView(m_render_layers.GetTopLayer().render_target_view, clearColor); UseProgram(ProgramId::None); - // program_transform_dirty.set(); m_scissor_state = Rml::Rectanglei::MakeInvalid(); } @@ -1232,7 +1218,6 @@ void RenderInterface_DX11::EndFrame() // Instead, if we had a transparent destination that didn't use premultiplied alpha, we would need to perform a manual un-premultiplication step. Gfx::BindTexture(m_d3d_context, rt_postprocess); - // @TODO: Find a pattern for flipped textures UseProgram(ProgramId::Passthrough); DrawFullscreenQuad(); @@ -1381,93 +1366,77 @@ Rml::CompiledGeometryHandle RenderInterface_DX11::CompileGeometry(Rml::Spanvertex_buffer = vertex_buffer; + geometryData->index_buffer = index_buffer; + geometryData->index_count = indices.size(); - m_geometry_cache.emplace(handleId, geometryData); - return Rml::CompiledGeometryHandle(handleId); + return (Rml::CompiledGeometryHandle)geometryData; } void RenderInterface_DX11::ReleaseGeometry(Rml::CompiledGeometryHandle handle) { - if (m_geometry_cache.find(handle) != m_geometry_cache.end()) - { - DX11_GeometryData geometryData = m_geometry_cache[handle]; + DX11_GeometryData* geometry = (DX11_GeometryData*)handle; - DX_CLEANUP_RESOURCE_IF_CREATED(geometryData.vertex_buffer); - DX_CLEANUP_RESOURCE_IF_CREATED(geometryData.index_buffer); + DX_CLEANUP_RESOURCE_IF_CREATED(geometry->vertex_buffer); + DX_CLEANUP_RESOURCE_IF_CREATED(geometry->index_buffer); - m_geometry_cache.erase(handle); - } - else - { - Rml::Log::Message(Rml::Log::LT_WARNING, "Geometry Handle %d does not exist!", handle); - } + delete geometry; } void RenderInterface_DX11::RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture) { - if (m_geometry_cache.find(handle) != m_geometry_cache.end()) - { - DX11_GeometryData geometryData = m_geometry_cache[handle]; + DX11_GeometryData* geometry = (DX11_GeometryData*)handle; - if (m_translation != translation) - { - m_translation = translation; - m_cbuffer_dirty = true; - } - - if (texture == TexturePostprocess || texture == TexturePostprocessNoBinding) - { - // Do nothing. - } - else if (texture) - { - // Texture available - UseProgram(ProgramId::Texture); - if (texture != TextureEnableWithoutBinding) - { - ID3D11ShaderResourceView* texture_view = (ID3D11ShaderResourceView*)texture; - m_d3d_context->PSSetShaderResources(0, 1, &texture_view); - } - m_d3d_context->PSSetSamplers(0, 1, &m_sampler_state); - } - else - { - // No texture, use color - UseProgram(ProgramId::Color); - } + if (m_translation != translation) + { + m_translation = translation; + m_cbuffer_dirty = true; + } - if (texture != TexturePostprocessNoBinding) + if (texture == TexturePostprocess || texture == TexturePostprocessNoBinding) + { + // Do nothing. + } + else if (texture) + { + // Texture available + UseProgram(ProgramId::Texture); + if (texture != TextureEnableWithoutBinding) { - UpdateConstantBuffer(); - - m_d3d_context->VSSetConstantBuffers(0, 1, &m_shader_buffer); - m_d3d_context->PSSetConstantBuffers(0, 1, &m_shader_buffer); + ID3D11ShaderResourceView* texture_view = (ID3D11ShaderResourceView*)texture; + m_d3d_context->PSSetShaderResources(0, 1, &texture_view); } - - // Bind vertex and index buffers, issue draw call - uint32_t stride = sizeof(Rml::Vertex); - uint32_t offset = 0; - m_d3d_context->IASetInputLayout(m_vertex_layout); - m_d3d_context->IASetVertexBuffers(0, 1, &geometryData.vertex_buffer, &stride, &offset); - m_d3d_context->IASetIndexBuffer(geometryData.index_buffer, DXGI_FORMAT_R32_UINT, 0); - m_d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - m_d3d_context->DrawIndexed(static_cast(geometryData.index_count), 0, 0); - - // Unbind resources - ID3D11ShaderResourceView* const nullSRV = nullptr; - m_d3d_context->PSSetShaderResources(0, 1, &nullSRV); + m_d3d_context->PSSetSamplers(0, 1, &m_sampler_state); } else { - Rml::Log::Message(Rml::Log::LT_WARNING, "Geometry Handle %d does not exist!", handle); + // No texture, use color + UseProgram(ProgramId::Color); + } + + if (texture != TexturePostprocessNoBinding) + { + UpdateConstantBuffer(); + + m_d3d_context->VSSetConstantBuffers(0, 1, &m_shader_buffer); + m_d3d_context->PSSetConstantBuffers(0, 1, &m_shader_buffer); } + + // Bind vertex and index buffers, issue draw call + uint32_t stride = sizeof(Rml::Vertex); + uint32_t offset = 0; + m_d3d_context->IASetInputLayout(m_vertex_layout); + m_d3d_context->IASetVertexBuffers(0, 1, &geometry->vertex_buffer, &stride, &offset); + m_d3d_context->IASetIndexBuffer(geometry->index_buffer, DXGI_FORMAT_R32_UINT, 0); + m_d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + m_d3d_context->DrawIndexed(static_cast(geometry->index_count), 0, 0); + + // Unbind resources + ID3D11ShaderResourceView* const nullSRV = nullptr; + m_d3d_context->PSSetShaderResources(0, 1, &nullSRV); } // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file @@ -2273,11 +2242,7 @@ void RenderInterface_DX11::RenderShader(Rml::CompiledShaderHandle shader_handle, const CompiledShader& shader = *reinterpret_cast(shader_handle); const CompiledShaderType type = shader.type; - DX11_GeometryData geometry{}; - if (m_geometry_cache.find(geometry_handle) != m_geometry_cache.end()) - { - geometry = m_geometry_cache[geometry_handle]; - } + DX11_GeometryData* geometry = (DX11_GeometryData*)geometry_handle; switch (type) { @@ -2322,10 +2287,10 @@ void RenderInterface_DX11::RenderShader(Rml::CompiledShaderHandle shader_handle, m_d3d_context->PSSetConstantBuffers(0, 1, &m_shader_buffer); uint32_t stride = sizeof(Rml::Vertex); uint32_t offset = 0; - m_d3d_context->IASetVertexBuffers(0, 1, &geometry.vertex_buffer, &stride, &offset); - m_d3d_context->IASetIndexBuffer(geometry.index_buffer, DXGI_FORMAT_R32_UINT, 0); + m_d3d_context->IASetVertexBuffers(0, 1, &geometry->vertex_buffer, &stride, &offset); + m_d3d_context->IASetIndexBuffer(geometry->index_buffer, DXGI_FORMAT_R32_UINT, 0); m_d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - m_d3d_context->DrawIndexed(static_cast(geometry.index_count), 0, 0); + m_d3d_context->DrawIndexed(static_cast(geometry->index_count), 0, 0); } break; case CompiledShaderType::Creation: @@ -2360,10 +2325,10 @@ void RenderInterface_DX11::RenderShader(Rml::CompiledShaderHandle shader_handle, m_d3d_context->PSSetConstantBuffers(0, 1, &m_shader_buffer); uint32_t stride = sizeof(Rml::Vertex); uint32_t offset = 0; - m_d3d_context->IASetVertexBuffers(0, 1, &geometry.vertex_buffer, &stride, &offset); - m_d3d_context->IASetIndexBuffer(geometry.index_buffer, DXGI_FORMAT_R32_UINT, 0); + m_d3d_context->IASetVertexBuffers(0, 1, &geometry->vertex_buffer, &stride, &offset); + m_d3d_context->IASetIndexBuffer(geometry->index_buffer, DXGI_FORMAT_R32_UINT, 0); m_d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - m_d3d_context->DrawIndexed(static_cast(geometry.index_count), 0, 0); + m_d3d_context->DrawIndexed(static_cast(geometry->index_count), 0, 0); } break; case CompiledShaderType::Invalid: diff --git a/Backends/RmlUi_Renderer_DX11.h b/Backends/RmlUi_Renderer_DX11.h index fbce19b38..e70471435 100644 --- a/Backends/RmlUi_Renderer_DX11.h +++ b/Backends/RmlUi_Renderer_DX11.h @@ -214,8 +214,6 @@ class RenderInterface_DX11 : public Rml::RenderInterface { size_t index_count = 0; }; - std::unordered_map m_geometry_cache; - // D3D11 state for RmlUi rendering bool m_current_blend_state_enabled = false; ID3D11BlendState* m_current_blend_state = nullptr;