Skip to content

Commit

Permalink
vulkan: fix shaders with int vertex attributes when nothing using tha…
Browse files Browse the repository at this point in the history
…t attribute is drawn.
  • Loading branch information
slime73 committed Apr 12, 2024
1 parent 565d635 commit 111bbde
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
28 changes: 20 additions & 8 deletions src/modules/graphics/vulkan/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ void Graphics::createVulkanVertexFormat(

for (const auto &pair : shader->getVertexAttributeIndices())
{
int i = pair.second;
int i = pair.second.index;
uint32 bit = 1u << i;

VkVertexInputAttributeDescription attribdesc{};
Expand Down Expand Up @@ -2295,13 +2295,25 @@ void Graphics::createVulkanVertexFormat(
attribdesc.binding = DEFAULT_VERTEX_BUFFER_BINDING;

// Indices should match the creation parameters for defaultVertexBuffer.
// TODO: handle int/uint attributes?
if (i == ATTRIB_COLOR)
attribdesc.offset = defaultVertexBuffer->getDataMember(2).offset;
else
attribdesc.offset = defaultVertexBuffer->getDataMember(0).offset;

attribdesc.format = Vulkan::getVulkanVertexFormat(DATAFORMAT_FLOAT_VEC4);
switch (pair.second.baseType)
{
case DATA_BASETYPE_INT:
attribdesc.offset = defaultVertexBuffer->getDataMember(1).offset;
attribdesc.format = Vulkan::getVulkanVertexFormat(DATAFORMAT_INT32_VEC4);
break;
case DATA_BASETYPE_UINT:
attribdesc.offset = defaultVertexBuffer->getDataMember(1).offset;
attribdesc.format = Vulkan::getVulkanVertexFormat(DATAFORMAT_UINT32_VEC4);
break;
case DATA_BASETYPE_FLOAT:
default:
if (i == ATTRIB_COLOR)
attribdesc.offset = defaultVertexBuffer->getDataMember(2).offset;
else
attribdesc.offset = defaultVertexBuffer->getDataMember(0).offset;
attribdesc.format = Vulkan::getVulkanVertexFormat(DATAFORMAT_FLOAT_VEC4);
break;
}

if (usedBuffers.find(DEFAULT_VERTEX_BUFFER_BINDING) == usedBuffers.end())
{
Expand Down
18 changes: 16 additions & 2 deletions src/modules/graphics/vulkan/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ void Shader::attach()
int Shader::getVertexAttributeIndex(const std::string &name)
{
auto it = attributes.find(name);
return it == attributes.end() ? -1 : it->second;
return it == attributes.end() ? -1 : it->second.index;
}

const Shader::UniformInfo *Shader::getUniformInfo(BuiltinUniform builtin) const
Expand Down Expand Up @@ -716,7 +716,21 @@ void Shader::compileShaders()

spirv[locationOffset] = (uint32_t)index;

attributes[r.name] = index;
DataBaseType basetype = DATA_BASETYPE_FLOAT;

switch (comp.get_type(r.base_type_id).basetype)
{
case spirv_cross::SPIRType::Int:
basetype = DATA_BASETYPE_INT;
break;
case spirv_cross::SPIRType::UInt:
basetype = DATA_BASETYPE_UINT;
break;
default:
break;
}

attributes[r.name] = { index, basetype };
}

for (const auto &r : shaderResources.stage_outputs)
Expand Down
11 changes: 9 additions & 2 deletions src/modules/graphics/vulkan/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Shader final
, public Volatile
{
public:

struct AttributeInfo
{
int index;
DataBaseType baseType;
};

Shader(StrongRef<love::graphics::ShaderStage> stages[], const CompileOptions &options);
virtual ~Shader();

Expand All @@ -75,7 +82,7 @@ class Shader final
std::string getWarnings() const override { return ""; }

int getVertexAttributeIndex(const std::string &name) override;
const std::unordered_map<std::string, int> getVertexAttributeIndices() const { return attributes; }
const std::unordered_map<std::string, AttributeInfo> getVertexAttributeIndices() const { return attributes; }

const UniformInfo *getUniformInfo(BuiltinUniform builtin) const override;

Expand Down Expand Up @@ -126,7 +133,7 @@ class Shader final
uint32_t localUniformLocation;
OptionalInt builtinUniformDataOffset;

std::unordered_map<std::string, int> attributes;
std::unordered_map<std::string, AttributeInfo> attributes;

uint32_t currentFrame;
uint32_t currentDescriptorPool;
Expand Down

0 comments on commit 111bbde

Please sign in to comment.