Skip to content

Commit

Permalink
vulkan: fix shader cross-stage io (varying) location mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Apr 8, 2024
1 parent 994079e commit d40b78b
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/modules/graphics/vulkan/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ static const uint32_t DESCRIPTOR_POOL_SIZE = 1000;
class BindingMapper
{
public:

BindingMapper(spv::Decoration decoration)
: decoration(decoration)
{}

uint32_t operator()(spirv_cross::CompilerGLSL &comp, std::vector<uint32_t> &spirv, const std::string &name, int count, const spirv_cross::ID &id)
{
auto it = bindingMappings.find(name);
Expand All @@ -58,7 +63,7 @@ class BindingMapper
uint32_t freeBinding = getFreeBinding(count);

uint32_t binaryBindingOffset;
if (!comp.get_binary_offset_for_decoration(id, spv::DecorationBinding, binaryBindingOffset))
if (!comp.get_binary_offset_for_decoration(id, decoration, binaryBindingOffset))
throw love::Exception("could not get binary offset for uniform %s binding", name.c_str());

spirv[binaryBindingOffset] = freeBinding;
Expand All @@ -73,7 +78,7 @@ class BindingMapper
auto binding = (uint32_t)it->second.getOffset();

uint32_t binaryBindingOffset;
if (!comp.get_binary_offset_for_decoration(id, spv::DecorationBinding, binaryBindingOffset))
if (!comp.get_binary_offset_for_decoration(id, decoration, binaryBindingOffset))
throw love::Exception("could not get binary offset for uniform %s binding", name.c_str());

spirv[binaryBindingOffset] = binding;
Expand Down Expand Up @@ -104,6 +109,7 @@ class BindingMapper
return true;
}

spv::Decoration decoration;
std::map<std::string, Range> bindingMappings;

};
Expand Down Expand Up @@ -570,7 +576,8 @@ void Shader::compileShaders()
if (!program->mapIO())
throw love::Exception("mapIO failed");

BindingMapper bindingMapper;
BindingMapper bindingMapper(spv::DecorationBinding);
BindingMapper ioLocationMapper(spv::DecorationLocation);

for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
{
Expand Down Expand Up @@ -711,6 +718,24 @@ void Shader::compileShaders()

attributes[r.name] = index;
}

for (const auto &r : shaderResources.stage_outputs)
{
const auto &type = comp.get_type(r.type_id);
int count = type.array.empty() ? 1 : type.array[0];

ioLocationMapper(comp, spirv, r.name, count, r.id);
}
}
else if (shaderStage == SHADERSTAGE_PIXEL)
{
for (const auto &r : shaderResources.stage_inputs)
{
const auto &type = comp.get_type(r.type_id);
int count = type.array.empty() ? 1 : type.array[0];

ioLocationMapper(comp, spirv, r.name, count, r.id);
}
}

VkShaderModuleCreateInfo createInfo{};
Expand Down

0 comments on commit d40b78b

Please sign in to comment.