From 4dbb9b34c6b4d38779e55d36a79d252cc4e2fc2a Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Thu, 30 Jan 2025 15:51:09 +0100 Subject: [PATCH] Fix: Vulkan: Compositor cryptomatte When using cryptomatte the last identifier was never used due to a memory alignment issue. Scalar types should not be aligned, but they were. Pull Request: https://projects.blender.org/blender/blender/pulls/133815 --- .../gpu/vulkan/tests/vk_memory_layout_test.cc | 12 ++++++++++ source/blender/gpu/vulkan/vk_memory_layout.cc | 22 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/source/blender/gpu/vulkan/tests/vk_memory_layout_test.cc b/source/blender/gpu/vulkan/tests/vk_memory_layout_test.cc index dda8787818a..ec9efef5957 100644 --- a/source/blender/gpu/vulkan/tests/vk_memory_layout_test.cc +++ b/source/blender/gpu/vulkan/tests/vk_memory_layout_test.cc @@ -134,4 +134,16 @@ TEST(std430, simple_lighting) EXPECT_EQ(offset, 112); } +TEST(std430, compositor_cryptomatte_matte_compute) +{ + uint32_t offset = 0; + + def_attr(shader::Type::VEC2, 0, 0, 8, &offset); + def_attr(shader::Type::FLOAT, 0, 8, 12, &offset); + def_attr(shader::Type::FLOAT, 32, 12, 140, &offset); + + align_end_of_struct(&offset); + EXPECT_EQ(offset, 144); +} + } // namespace blender::gpu diff --git a/source/blender/gpu/vulkan/vk_memory_layout.cc b/source/blender/gpu/vulkan/vk_memory_layout.cc index 8d526c16eb9..6f596a59656 100644 --- a/source/blender/gpu/vulkan/vk_memory_layout.cc +++ b/source/blender/gpu/vulkan/vk_memory_layout.cc @@ -22,7 +22,27 @@ uint32_t Std430::component_mem_size(const shader::Type /*type*/) uint32_t Std430::element_alignment(const shader::Type type, const bool is_array) { if (is_array) { - return 16; + switch (type) { + case shader::Type::FLOAT: + case shader::Type::UINT: + case shader::Type::INT: + case shader::Type::BOOL: + return 4; + case shader::Type::VEC2: + case shader::Type::UVEC2: + case shader::Type::IVEC2: + case shader::Type::VEC3: + case shader::Type::UVEC3: + case shader::Type::IVEC3: + case shader::Type::VEC4: + case shader::Type::UVEC4: + case shader::Type::IVEC4: + case shader::Type::MAT3: + case shader::Type::MAT4: + return 16; + default: + BLI_assert_msg(false, "Type not supported in dynamic structs."); + } } switch (type) { case shader::Type::FLOAT: