diff --git a/impls/Windows_Libs/Render/src/RendererState.cpp b/impls/Windows_Libs/Render/src/RendererState.cpp index 6c48ebd..34bc918 100644 --- a/impls/Windows_Libs/Render/src/RendererState.cpp +++ b/impls/Windows_Libs/Render/src/RendererState.cpp @@ -677,13 +677,19 @@ void Renderer::UpdateLightingState() void Renderer::UpdateTexGenState() { - PROFILER_SCOPE("Renderer::UpdateTexGenState", "UpdateTexGenState", MP_ORCHID1); - Context &c = getContext(); + PROFILER_SCOPE("Renderer::UpdateTexGenState", "UpdateTexGenState", MP_ORCHID1); + Context &c = getContext(); - D3D11_MAPPED_SUBRESOURCE mapped = {}; - c.m_pDeviceContext->Map(c.m_texGenMatricesBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); - c.m_pDeviceContext->Unmap(c.m_texGenMatricesBuffer, 0); - std::memcpy(mapped.pData, c.texGenMatrices, sizeof(c.texGenMatrices)); + D3D11_MAPPED_SUBRESOURCE mapped = {}; + HRESULT hr = c.m_pDeviceContext->Map(c.m_texGenMatricesBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); + // @CDevJoud + // the old code has a major problem where it tried to memcpy data after unmaping which + // in some cases results in unexpected crashes as mapped.pData may point to invalid memory + // after unmaping + if (SUCCEEDED(hr)) { + std::memcpy(mapped.pData, c.texGenMatrices, sizeof(c.texGenMatrices)); + c.m_pDeviceContext->Unmap(c.m_texGenMatricesBuffer, 0); + } }