From 9fb179e1393b107096476d65c81cb350d6355c66 Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Wed, 8 Nov 2023 17:39:13 -0800 Subject: [PATCH] iglu | Faster ShaderUniforms::setFloat3Array() Summary: Faster `ShaderUniforms::setFloat3Array()` without heap allocations and multiple memory overwrites. Reviewed By: dmannemela, AmesingFlank Differential Revision: D51089951 fbshipit-source-id: 2b3c4e58f33b692c50512e68332091416d932268 --- IGLU/simple_renderer/ShaderUniforms.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/IGLU/simple_renderer/ShaderUniforms.cpp b/IGLU/simple_renderer/ShaderUniforms.cpp index 2a0bc932d3..d48fd2b2c5 100644 --- a/IGLU/simple_renderer/ShaderUniforms.cpp +++ b/IGLU/simple_renderer/ShaderUniforms.cpp @@ -423,18 +423,20 @@ void ShaderUniforms::setFloat3Array(const igl::NameHandle& uniformName, setUniformBytes(uniformName, value, sizeof(iglu::simdtypes::float3), count, arrayIndex); } else { // simdtypes::float3 is padded to have an extra float. - // Remove it so we can send the packed version to OpenGL/Vulkan - auto* packedArray = new float[3 * count]; - float* packedArrayPtr = packedArray; - auto paddedArrayPtr = reinterpret_cast(value); + // This code path should not be used for Vulkan. (it should only be used for OpenGL when uniform + // blocks are not used). + const size_t size = sizeof(float) * 3u * count; + IGL_ASSERT(size <= 65536); + float* IGL_RESTRICT packedArray = reinterpret_cast(alloca(size)); + float* IGL_RESTRICT packedArrayPtr = packedArray; + const float* paddedArrayPtr = reinterpret_cast(value); for (size_t i = 0; i < count; i++) { for (int j = 0; j < 3; j++) { *packedArrayPtr++ = *paddedArrayPtr++; } paddedArrayPtr++; // padded float } - setUniformBytes(uniformName, packedArray, sizeof(float) * 3 * count, 1, arrayIndex); - delete[] packedArray; + setUniformBytes(uniformName, packedArray, size, 1, arrayIndex); } }