From 6f29f580bd5ccb398f1ebc67fb75edd35d0bff74 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Thu, 25 Jul 2024 22:07:43 +0200 Subject: [PATCH] Memcpy related fixes. --- samples/00_util/gpu_resource_manager.cpp | 8 ++++---- samples/03_model_render/light_manager.cpp | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/samples/00_util/gpu_resource_manager.cpp b/samples/00_util/gpu_resource_manager.cpp index 85495b1..1683a48 100644 --- a/samples/00_util/gpu_resource_manager.cpp +++ b/samples/00_util/gpu_resource_manager.cpp @@ -36,7 +36,7 @@ TextureManager::Commit(Texture *texture) // Ensure it is copyable. static_assert(std::is_trivially_copyable_v); - memcpy(allocatedTexture, texture, sizeof *texture); + *allocatedTexture = *texture; // Take ownership of the buffer. texture->m_MipLevels_ &= ~Texture::OWNED_BIT; @@ -51,7 +51,7 @@ TextureManager::Commit(Texture *texture) // Ensure it is copyable. static_assert(std::is_trivially_copyable_v); - memcpy(allocatedTexture, texture, sizeof *texture); + *allocatedTexture = *texture; texture->m_MipLevels_ &= ~Texture::OWNED_BIT; @@ -115,7 +115,7 @@ BufferManager::Commit(StorageBuffer *buffer) // Ensure it is copyable. static_assert(std::is_trivially_copyable_v); - memcpy(allocatedBuffer, buffer, sizeof *buffer); + *allocatedBuffer = *buffer; // Take ownership of the buffer. buffer->m_Size_ &= ~StorageBuffer::OWNED_BIT; @@ -130,7 +130,7 @@ BufferManager::Commit(StorageBuffer *buffer) // Ensure it is copyable. static_assert(std::is_trivially_copyable_v); - memcpy(allocatedBuffer, buffer, sizeof *buffer); + *allocatedBuffer = *buffer; buffer->m_Size_ &= ~StorageBuffer::OWNED_BIT; diff --git a/samples/03_model_render/light_manager.cpp b/samples/03_model_render/light_manager.cpp index 3dbd7c6..195d57b 100644 --- a/samples/03_model_render/light_manager.cpp +++ b/samples/03_model_render/light_manager.cpp @@ -154,10 +154,14 @@ LightManager::AddDirectional(const vec3 &direction, const vec3 &color, f32 inten if (m_MetaInfo.m_PointLightMaxCount > 0) // Edge Case: nullptr at size 0 { Light *oldPointStart = m_Lights.data() + oldPointLightOffset; - Light *newPointStart = oldPointStart + 2; + Light *oldPointEnd = oldPointStart + pointLightMaxCount; + Light *newPointEnd = oldPointEnd + 2; static_assert(std::is_trivially_copyable_v); - memcpy(newPointStart, oldPointStart, pointLightMaxCount * sizeof *newPointStart); + + // Overlaps since 0 -> 2, 1 -> 3, 2 -> 4 (old 2 is lost) + // Backward copy fixes this. + std::copy_backward(oldPointStart, oldPointEnd, newPointEnd); } m_MetaInfo.m_PointLightOffset += 2;