Vulkan: Push constants #104880

Merged
Jeroen Bakker merged 73 commits from Jeroen-Bakker/blender:vulkan-push-constants into main 2023-03-06 12:29:06 +01:00
7 changed files with 262 additions and 176 deletions
Showing only changes of commit 739d088c55 - Show all commits

View File

@ -202,6 +202,7 @@ set(VULKAN_SRC
vulkan/vk_framebuffer.cc
vulkan/vk_index_buffer.cc
vulkan/vk_pipeline.cc
vulkan/vk_memory_layout.cc
vulkan/vk_memory.cc
vulkan/vk_pixel_buffer.cc
vulkan/vk_push_constants.cc
@ -228,6 +229,7 @@ set(VULKAN_SRC
vulkan/vk_framebuffer.hh
vulkan/vk_index_buffer.hh
vulkan/vk_pipeline.hh
vulkan/vk_memory_layout.hh
vulkan/vk_memory.hh
vulkan/vk_pixel_buffer.hh
vulkan/vk_push_constants.hh

View File

@ -44,126 +44,4 @@ void vk_memory_free(void * /*user_data*/, void *memory)
#endif
uint32_t Std430::component_mem_size(const shader::Type /*type*/)
{
return 4;
}
uint32_t Std430::element_alignment(const shader::Type type)
{
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:
return 8;
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 as push constant");
}
return 0;
}
uint32_t Std430::element_components_len(const shader::Type 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 as push constant");
}
return 0;
}
uint32_t Std140::component_mem_size(const shader::Type /*type*/)
{
return 4;
}
uint32_t Std140::element_alignment(const shader::Type type)
{
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:
return 8;
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 as push constant");
}
return 0;
}
uint32_t Std140::element_components_len(const shader::Type 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 as push constant");
}
return 0;
}
} // namespace blender::gpu

View File

@ -9,8 +9,6 @@
#include "vk_common.hh"
#include "gpu_shader_create_info.hh"
namespace blender::gpu {
/**
@ -60,28 +58,4 @@ constexpr VkAllocationCallbacks vk_allocation_callbacks_init(const char *name)
static constexpr const VkAllocationCallbacks *vk_allocation_callbacks = nullptr;
#endif
/**
* Information about alignment/components and memory size for types when using std430 layout.
*/
struct Std430 {
/** Get the memory size in bytes of a single component using by the given type.*/
static uint32_t component_mem_size(const shader::Type type);
/** Get to alignment of the given type in bytes.*/
static uint32_t element_alignment(const shader::Type type);
/** Get the number of components that should be allocated for the given type.*/
static uint32_t element_components_len(const shader::Type type);
};
/**
* Information about alignment/components and memory size for types when using std140 layout.
*/
struct Std140 {
/** Get the memory size in bytes of a single component using by the given type.*/
static uint32_t component_mem_size(const shader::Type type);
/** Get to alignment of the given type in bytes.*/
static uint32_t element_alignment(const shader::Type type);
/** Get the number of components that should be allocated for the given type.*/
static uint32_t element_components_len(const shader::Type type);
};
} // namespace blender::gpu

View File

@ -0,0 +1,166 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2023 Blender Foundation. All rights reserved. */
/** \file
* \ingroup gpu
*/
#include "vk_memory_layout.hh"
namespace blender::gpu {
uint32_t Std430::component_mem_size(const shader::Type /*type*/)
{
return 4;
}
uint32_t Std430::element_alignment(const shader::Type type)
{
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:
return 8;
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 as push constant");
}
return 0;
}
uint32_t Std430::element_components_len(const shader::Type 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 as push constant");
}
return 0;
}
uint32_t Std430::array_components_len(const shader::Type type)
{
return Std430::element_components_len(type);
}
uint32_t Std140::component_mem_size(const shader::Type /*type*/)
{
return 4;
}
uint32_t Std140::element_alignment(const shader::Type type)
{
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:
return 8;
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 as push constant");
}
return 0;
}
uint32_t Std140::element_components_len(const shader::Type 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 as push constant");
}
return 0;
}
uint32_t Std140::array_components_len(const shader::Type type)
{
switch (type) {
case shader::Type::FLOAT:
case shader::Type::UINT:
case shader::Type::INT:
case shader::Type::BOOL:
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:
return 4;
case shader::Type::MAT3:
return 12;
case shader::Type::MAT4:
return 16;
default:
BLI_assert_msg(false, "Type not supported as push constant");
}
return 0;
}
} // namespace blender::gpu

View File

@ -0,0 +1,83 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2023 Blender Foundation. All rights reserved. */
/** \file
* \ingroup gpu
*/
#pragma once
#include "gpu_shader_create_info.hh"
namespace blender::gpu {
/**
* Information about alignment/components and memory size for types when using std430 layout.
*/
struct Std430 {
/** Get the memory size in bytes of a single component using by the given type.*/
static uint32_t component_mem_size(const shader::Type type);
/** Get to alignment of the given type in bytes.*/
static uint32_t element_alignment(const shader::Type type);
/** Get the number of components that should be allocated for the given type.*/
static uint32_t element_components_len(const shader::Type type);
/** Get the number of components of the given type when used in an array.*/
static uint32_t array_components_len(const shader::Type type);
};
/**
* Information about alignment/components and memory size for types when using std140 layout.
*/
struct Std140 {
/** Get the memory size in bytes of a single component using by the given type.*/
static uint32_t component_mem_size(const shader::Type type);
/** Get to alignment of the given type in bytes.*/
static uint32_t element_alignment(const shader::Type type);
/** Get the number of components that should be allocated for the given type.*/
static uint32_t element_components_len(const shader::Type type);
/** Get the number of components of the given type when used in an array.*/
static uint32_t array_components_len(const shader::Type type);
};
template<typename Layout> static void align(const shader::Type &type, uint32_t *r_offset)
Jeroen-Bakker marked this conversation as resolved

I think Layout should be LayoutT to avoid thinking it is the layout itself (as in the content of a UBO/SSBO).

I think `Layout` should be `LayoutT` to avoid thinking it is the layout itself (as in the content of a UBO/SSBO).
{
uint32_t alignment = Layout::element_alignment(type);
uint32_t alignment_mask = alignment - 1;
uint32_t offset = *r_offset;
if ((offset & alignment_mask) != 0) {
offset &= ~alignment_mask;
offset += alignment;
*r_offset = offset;
}
}
template<typename Layout> static uint32_t element_stride(const shader::Type type)
{
return Layout::element_components_len(type) * Layout::component_mem_size(type);
}
template<typename Layout> static uint32_t array_stride(const shader::Type type)
{
return Layout::array_components_len(type) * Layout::component_mem_size(type);
}
template<typename Layout>
static void reserve(const shader::Type type, int32_t array_size, uint32_t *r_offset)
{
uint32_t size = array_size == 0 ? element_stride<Layout>(type) :
array_stride<Layout>(type) * array_size;
*r_offset += size;
}
template<typename Layout>
static void reserve(const shader::ShaderCreateInfo::PushConst &push_constant, uint32_t *r_offset)
{
reserve<Layout>(push_constant.type, push_constant.array_size, r_offset);
}
template<typename Layout> static void align_end_of_struct(uint32_t *r_offset)
{
align<Layout>(shader::Type::VEC4, r_offset);
}
} // namespace blender::gpu

View File

@ -7,36 +7,13 @@
#include "vk_push_constants.hh"
#include "vk_backend.hh"
#include "vk_memory.hh"
#include "vk_memory_layout.hh"
#include "vk_shader_interface.hh"
#include "vk_storage_buffer.hh"
#include "vk_uniform_buffer.hh"
namespace blender::gpu {
template<typename Layout> static void align(const shader::Type &type, uint32_t *r_offset)
{
uint32_t alignment = Layout::element_alignment(type);
uint32_t alignment_mask = alignment - 1;
uint32_t offset = *r_offset;
if ((offset & alignment_mask) != 0) {
offset &= ~alignment_mask;
offset += alignment;
*r_offset = offset;
}
}
template<typename Layout>
static void reserve(const shader::ShaderCreateInfo::PushConst &push_constant, uint32_t *r_offset)
{
uint32_t size = Layout::element_components_len(push_constant.type) *
Layout::component_mem_size(push_constant.type);
if (push_constant.array_size != 0) {
size *= push_constant.array_size;
}
*r_offset += size;
}
template<typename Layout>
static VKPushConstantsLayout::PushConstantLayout init_constant(
const shader::ShaderCreateInfo::PushConst &push_constant,
@ -64,9 +41,7 @@ uint32_t struct_size(Span<shader::ShaderCreateInfo::PushConst> push_constants)
reserve<Layout>(push_constant, &offset);
}
/* Make sure result is aligned to 64 bytes.*/
align<Layout>(shader::Type::VEC4, &offset);
align_end_of_struct<Layout>(&offset);
return offset;
}
@ -103,6 +78,14 @@ void VKPushConstantsLayout::init(const shader::ShaderCreateInfo &info,
push_constants.append(init_constant<Std430>(push_constant, *shader_input, &offset));
}
}
/* Align end of struct. */
if (storage_type == StorageType::UNIFORM_BUFFER) {
align<Std140>(shader::Type::VEC4, &offset);
}
else {
align_end_of_struct<Std430>(&offset);
}
size_in_bytes_ = offset;
}

View File

@ -55,7 +55,7 @@ struct VKPushConstantsLayout {
*/
UNIFORM_BUFFER,
};
static constexpr StorageType STORAGE_TYPE_DEFAULT = StorageType::PUSH_CONSTANTS;
static constexpr StorageType STORAGE_TYPE_DEFAULT = StorageType::UNIFORM_BUFFER;
static constexpr StorageType STORAGE_TYPE_FALLBACK = StorageType::UNIFORM_BUFFER;
struct PushConstantLayout {