Skip to content
Snippets Groups Projects
Verified Commit 4498dd7d authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Fix variable descriptors and cleanup shader program reflection

parent 0ec24e9c
No related branches found
No related tags found
No related merge requests found
...@@ -90,6 +90,14 @@ int main(int argc, const char** argv) { ...@@ -90,6 +90,14 @@ int main(int argc, const char** argv) {
} }
); );
features.requireExtensionFeature<vk::PhysicalDeviceDescriptorIndexingFeatures>(
VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME,
[](vk::PhysicalDeviceDescriptorIndexingFeatures& features) {
features.setDescriptorBindingPartiallyBound(true);
features.setDescriptorBindingVariableDescriptorCount(true);
}
);
vkcv::Core core = vkcv::Core::create( vkcv::Core core = vkcv::Core::create(
applicationName, applicationName,
VK_MAKE_VERSION(0, 0, 1), VK_MAKE_VERSION(0, 0, 1),
......
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
#include "vkcv/File.hpp" #include "vkcv/File.hpp"
#include "vkcv/Logger.hpp" #include "vkcv/Logger.hpp"
#include <cstddef>
#include <cstdint>
#include <limits>
namespace vkcv { namespace vkcv {
...@@ -79,73 +82,48 @@ namespace vkcv { ...@@ -79,73 +82,48 @@ namespace vkcv {
return true; return true;
} }
void ShaderProgram::reflectShader(ShaderStage shaderStage) { static void reflectShaderDescriptorSets(Dictionary<uint32_t, DescriptorBindings> &descriptorSets,
auto shaderCode = m_Shaders.at(shaderStage); ShaderStage shaderStage,
DescriptorType descriptorType,
spirv_cross::Compiler comp(shaderCode); const spirv_cross::Compiler &comp,
spirv_cross::ShaderResources resources = comp.get_shader_resources(); const spirv_cross::ShaderResources &resources) {
const spirv_cross::SmallVector<spirv_cross::Resource> *res = nullptr;
// reflect vertex input
if (shaderStage == ShaderStage::VERTEX) { switch (descriptorType) {
// spirv-cross API (hopefully) returns the stage_inputs in order case DescriptorType::UNIFORM_BUFFER:
for (uint32_t i = 0; i < resources.stage_inputs.size(); i++) { res = &(resources.uniform_buffers);
// spirv-cross specific objects break;
auto &stage_input = resources.stage_inputs [i]; case DescriptorType::STORAGE_BUFFER:
const spirv_cross::SPIRType &base_type = comp.get_type(stage_input.base_type_id); res = &(resources.storage_buffers);
break;
// vertex input location case DescriptorType::SAMPLER:
const uint32_t attachment_loc = res = &(resources.separate_samplers);
comp.get_decoration(stage_input.id, spv::DecorationLocation); break;
// vertex input name case DescriptorType::IMAGE_SAMPLED:
const std::string attachment_name = stage_input.name; res = &(resources.separate_images);
// vertex input format (implies its size) break;
const VertexAttachmentFormat attachment_format = case DescriptorType::IMAGE_STORAGE:
convertFormat(base_type.basetype, base_type.vecsize); res = &(resources.storage_images);
break;
m_VertexAttachments.push_back( case DescriptorType::UNIFORM_BUFFER_DYNAMIC:
{ attachment_loc, attachment_name, attachment_format, 0 }); res = &(resources.uniform_buffers);
} break;
case DescriptorType::STORAGE_BUFFER_DYNAMIC:
res = &(resources.storage_buffers);
break;
case DescriptorType::ACCELERATION_STRUCTURE_KHR:
res = &(resources.acceleration_structures);
break;
default:
break;
} }
// reflect descriptor sets (uniform buffer, storage buffer, sampler, sampled image, storage if (nullptr == res) {
// image) return;
Vector<std::pair<uint32_t, DescriptorBinding>> bindings;
for (uint32_t i = 0; i < resources.uniform_buffers.size(); i++) {
auto &u = resources.uniform_buffers [i];
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
const spirv_cross::SPIRType &type = comp.get_type(u.type_id);
uint32_t setID = comp.get_decoration(u.id, spv::DecorationDescriptorSet);
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding);
uint32_t descriptorCount = base_type.vecsize;
bool variableCount = false;
// query whether reflected resources are qualified as one-dimensional array
if (type.array_size_literal [0]) {
if (type.array [0] == 0)
variableCount = true;
}
DescriptorBinding binding {
bindingID, DescriptorType::UNIFORM_BUFFER, descriptorCount, shaderStage,
variableCount,
variableCount // partialBinding == variableCount
};
auto insertionResult =
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding));
if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage;
vkcv_log(LogLevel::WARNING,
"Attempting to overwrite already existing binding %u at set ID %u.",
bindingID, setID);
}
} }
for (uint32_t i = 0; i < resources.storage_buffers.size(); i++) { for (uint32_t i = 0; i < res->size(); i++) {
auto &u = resources.storage_buffers [i]; const spirv_cross::Resource &u = (*res)[i];
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id); const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
const spirv_cross::SPIRType &type = comp.get_type(u.type_id); const spirv_cross::SPIRType &type = comp.get_type(u.type_id);
...@@ -153,86 +131,23 @@ namespace vkcv { ...@@ -153,86 +131,23 @@ namespace vkcv {
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding); uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding);
uint32_t descriptorCount = base_type.vecsize; uint32_t descriptorCount = base_type.vecsize;
bool variableCount = false;
// query whether reflected resources are qualified as one-dimensional array
if (type.array_size_literal [0]) {
if (type.array [0] == 0)
variableCount = true;
}
DescriptorBinding binding {
bindingID, DescriptorType::STORAGE_BUFFER, descriptorCount, shaderStage,
variableCount,
variableCount // partialBinding == variableCount
};
auto insertionResult =
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding));
if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage;
vkcv_log(LogLevel::WARNING,
"Attempting to overwrite already existing binding %u at set ID %u.",
bindingID, setID);
}
}
for (uint32_t i = 0; i < resources.separate_samplers.size(); i++) {
auto &u = resources.separate_samplers [i];
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
const spirv_cross::SPIRType &type = comp.get_type(u.type_id);
uint32_t setID = comp.get_decoration(u.id, spv::DecorationDescriptorSet);
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding);
uint32_t descriptorCount = base_type.vecsize;
bool variableCount = false; bool variableCount = false;
// query whether reflected resources are qualified as one-dimensional array // query whether reflected resources are qualified as one-dimensional array
if (type.array_size_literal [0]) { if (descriptorCount == 0) {
if (type.array [0] == 0) variableCount = true;
variableCount = true;
} }
DescriptorBinding binding { DescriptorBinding binding {
bindingID, DescriptorType::SAMPLER, descriptorCount, shaderStage, variableCount, bindingID,
variableCount // partialBinding == variableCount descriptorType,
}; descriptorCount,
shaderStage,
auto insertionResult =
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding));
if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage;
vkcv_log(LogLevel::WARNING,
"Attempting to overwrite already existing binding %u at set ID %u.",
bindingID, setID);
}
}
for (uint32_t i = 0; i < resources.separate_images.size(); i++) {
auto &u = resources.separate_images [i];
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
const spirv_cross::SPIRType &type = comp.get_type(u.type_id);
uint32_t setID = comp.get_decoration(u.id, spv::DecorationDescriptorSet);
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding);
uint32_t descriptorCount = base_type.vecsize;
bool variableCount = false;
// query whether reflected resources are qualified as one-dimensional array
if (type.array_size_literal [0]) {
if (type.array [0] == 0)
variableCount = true;
}
DescriptorBinding binding {
bindingID, DescriptorType::IMAGE_SAMPLED, descriptorCount, shaderStage,
variableCount, variableCount,
variableCount // partialBinding == variableCount variableCount // partialBinding == variableCount
}; };
auto insertionResult = auto insertionResult = descriptorSets[setID].insert(std::make_pair(bindingID, binding));
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding));
if (!insertionResult.second) { if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage; insertionResult.first->second.shaderStages |= shaderStage;
...@@ -241,62 +156,96 @@ namespace vkcv { ...@@ -241,62 +156,96 @@ namespace vkcv {
bindingID, setID); bindingID, setID);
} }
} }
}
for (uint32_t i = 0; i < resources.storage_images.size(); i++) { void ShaderProgram::reflectShader(ShaderStage shaderStage) {
auto &u = resources.storage_images [i]; auto shaderCode = m_Shaders.at(shaderStage);
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id);
const spirv_cross::SPIRType &type = comp.get_type(u.type_id);
uint32_t setID = comp.get_decoration(u.id, spv::DecorationDescriptorSet); spirv_cross::Compiler comp(shaderCode);
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding); spirv_cross::ShaderResources resources = comp.get_shader_resources();
uint32_t descriptorCount = base_type.vecsize; // reflect vertex input
bool variableCount = false; if (shaderStage == ShaderStage::VERTEX) {
// query whether reflected resources are qualified as one-dimensional array // spirv-cross API (hopefully) returns the stage_inputs in order
if (type.array_size_literal [0]) { for (uint32_t i = 0; i < resources.stage_inputs.size(); i++) {
if (type.array [0] == 0) // spirv-cross specific objects
variableCount = true; auto &stage_input = resources.stage_inputs [i];
} const spirv_cross::SPIRType &base_type = comp.get_type(stage_input.base_type_id);
DescriptorBinding binding { // vertex input location
bindingID, DescriptorType::IMAGE_STORAGE, descriptorCount, shaderStage, const uint32_t attachment_loc =
variableCount, comp.get_decoration(stage_input.id, spv::DecorationLocation);
variableCount // partialBinding == variableCount // vertex input name
}; const std::string attachment_name = stage_input.name;
// vertex input format (implies its size)
const VertexAttachmentFormat attachment_format =
convertFormat(base_type.basetype, base_type.vecsize);
auto insertionResult = m_VertexAttachments.push_back(
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding)); { attachment_loc, attachment_name, attachment_format, 0 });
if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage;
vkcv_log(LogLevel::WARNING,
"Attempting to overwrite already existing binding %u at set ID %u.",
bindingID, setID);
} }
} }
// Used to reflect acceleration structure bindings for RTX. reflectShaderDescriptorSets(
for (uint32_t i = 0; i < resources.acceleration_structures.size(); i++) { m_DescriptorSets,
auto &u = resources.acceleration_structures [i]; shaderStage,
const spirv_cross::SPIRType &base_type = comp.get_type(u.base_type_id); DescriptorType::UNIFORM_BUFFER,
comp,
resources
);
reflectShaderDescriptorSets(
m_DescriptorSets,
shaderStage,
DescriptorType::STORAGE_BUFFER,
comp,
resources
);
reflectShaderDescriptorSets(
m_DescriptorSets,
shaderStage,
DescriptorType::SAMPLER,
comp,
resources
);
reflectShaderDescriptorSets(
m_DescriptorSets,
shaderStage,
DescriptorType::IMAGE_SAMPLED,
comp,
resources
);
reflectShaderDescriptorSets(
m_DescriptorSets,
shaderStage,
DescriptorType::IMAGE_STORAGE,
comp,
resources
);
reflectShaderDescriptorSets(
m_DescriptorSets,
shaderStage,
DescriptorType::ACCELERATION_STRUCTURE_KHR,
comp,
resources
);
for (auto &descriptorSet : m_DescriptorSets) {
uint32_t maxVariableBindingID = 0;
for (const auto &binding : descriptorSet.second) {
maxVariableBindingID = std::max(maxVariableBindingID, binding.first);
}
uint32_t setID = comp.get_decoration(u.id, spv::DecorationDescriptorSet); for (auto &binding : descriptorSet.second) {
uint32_t bindingID = comp.get_decoration(u.id, spv::DecorationBinding); if (binding.first < maxVariableBindingID) {
auto binding = DescriptorBinding { bindingID, binding.second.variableCount &= false;
DescriptorType::ACCELERATION_STRUCTURE_KHR, binding.second.partialBinding &= false;
base_type.vecsize, }
shaderStage,
false,
false };
auto insertionResult =
m_DescriptorSets [setID].insert(std::make_pair(bindingID, binding));
if (!insertionResult.second) {
insertionResult.first->second.shaderStages |= shaderStage;
vkcv_log(LogLevel::WARNING,
"Attempting to overwrite already existing binding %u at set ID %u.",
bindingID, setID);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment