Fix: Vulkan Grid Overlay

In Vulkan the grid overlay wasn't rendering correctly. The reason was
a mis alignment in the std430 layout. This PR fixes the std430 layout
so the grid overlay (and perhaps also other sections) would work.

Root cause is that the reserve size if an nVec3 is different when used
as an array versus a single attribute. This was not taken into account
where the overlay flag was read from uninitialized memory.

Pull Request: https://projects.blender.org/blender/blender/pulls/112564
This commit is contained in:
Jeroen Bakker 2023-09-19 11:52:59 +02:00
parent a288ab45b7
commit 2d70f46265
3 changed files with 38 additions and 2 deletions

View File

@ -112,4 +112,15 @@ TEST(std140, gpu_shader_2D_widget_base)
EXPECT_EQ(offset, 272);
}
TEST(std430, overlay_grid)
{
uint32_t offset = 0;
def_attr<Std430>(shader::Type::VEC3, 0, 0, 12, &offset);
def_attr<Std430>(shader::Type::INT, 0, 12, 16, &offset);
align_end_of_struct<Std430>(&offset);
EXPECT_EQ(offset, 16);
}
} // namespace blender::gpu

View File

@ -60,6 +60,7 @@ uint32_t Std430::element_components_len(const shader::Type type)
case shader::Type::VEC3:
case shader::Type::UVEC3:
case shader::Type::IVEC3:
return 3;
case shader::Type::VEC4:
case shader::Type::UVEC4:
case shader::Type::IVEC4:
@ -76,7 +77,31 @@ uint32_t Std430::element_components_len(const shader::Type type)
uint32_t Std430::array_components_len(const shader::Type type)
{
return Std430::element_components_len(type);
switch (type) {
case shader::Type::FLOAT:
case shader::Type::UINT:
case shader::Type::INT:
case shader::Type::BOOL:
return 1;
case shader::Type::VEC2:
case shader::Type::UVEC2:
case shader::Type::IVEC2:
return 2;
case shader::Type::VEC3:
case shader::Type::UVEC3:
case shader::Type::IVEC3:
case shader::Type::VEC4:
case shader::Type::UVEC4:
case shader::Type::IVEC4:
return 4;
case shader::Type::MAT3:
return 12;
case shader::Type::MAT4:
return 16;
default:
BLI_assert_msg(false, "Type not supported in dynamic structs.");
}
return 0;
}
uint32_t Std140::component_mem_size(const shader::Type /*type*/)

View File

@ -1006,7 +1006,7 @@ std::string VKShader::resources_declare(const shader::ShaderCreateInfo &info) co
if (push_constants_storage != VKPushConstants::StorageType::NONE) {
ss << "\n/* Push Constants. */\n";
if (push_constants_storage == VKPushConstants::StorageType::PUSH_CONSTANTS) {
ss << "layout(push_constant) uniform constants\n";
ss << "layout(push_constant, std430) uniform constants\n";
}
else if (push_constants_storage == VKPushConstants::StorageType::UNIFORM_BUFFER) {
ss << "layout(binding = " << push_constants_layout.descriptor_set_location_get()