Small performance optimization for mesh drawing.

This commit is contained in:
Sasha Szpakowski
2024-08-23 21:32:03 -03:00
parent 746aefef15
commit 2d606b1f0c
4 changed files with 24 additions and 11 deletions
+14 -6
View File
@@ -141,7 +141,12 @@ void Mesh::setupAttachedAttributes()
if (getAttachedAttributeIndex(name) != -1) if (getAttachedAttributeIndex(name) != -1)
throw love::Exception("Duplicate vertex attribute name: %s", name.c_str()); throw love::Exception("Duplicate vertex attribute name: %s", name.c_str());
attachedAttributes.push_back({name, vertexBuffer, nullptr, name, (int) i, 0, STEP_PER_VERTEX, true}); BuiltinVertexAttribute builtinattrib;
int builtinAttribIndex = -1;
if (getConstant(name.c_str(), builtinattrib))
builtinAttribIndex = (int)builtinattrib;
attachedAttributes.push_back({name, vertexBuffer, nullptr, name, (int) i, 0, STEP_PER_VERTEX, builtinAttribIndex, true});
} }
} }
@@ -168,6 +173,12 @@ void Mesh::finalizeAttribute(BufferAttribute &attrib) const
if (indexInBuffer < 0) if (indexInBuffer < 0)
throw love::Exception("Buffer does not have a vertex attribute with name '%s'.", attrib.nameInBuffer.c_str()); throw love::Exception("Buffer does not have a vertex attribute with name '%s'.", attrib.nameInBuffer.c_str());
BuiltinVertexAttribute builtinattrib;
if (getConstant(attrib.name.c_str(), builtinattrib))
attrib.builtinAttributeIndex = (int)builtinattrib;
else
attrib.builtinAttributeIndex = -1;
attrib.indexInBuffer = indexInBuffer; attrib.indexInBuffer = indexInBuffer;
} }
@@ -593,14 +604,11 @@ void Mesh::drawInternal(Graphics *gfx, const Matrix4 &m, int instancecount, Buff
continue; continue;
Buffer *buffer = attrib.buffer.get(); Buffer *buffer = attrib.buffer.get();
int attributeindex = -1; int attributeindex = attrib.builtinAttributeIndex;
// If the attribute is one of the LOVE-defined ones, use the constant // If the attribute is one of the LOVE-defined ones, use the constant
// attribute index for it, otherwise query the index from the shader. // attribute index for it, otherwise query the index from the shader.
BuiltinVertexAttribute builtinattrib; if (attributeindex < 0 && Shader::current)
if (getConstant(attrib.name.c_str(), builtinattrib))
attributeindex = (int) builtinattrib;
else if (Shader::current)
attributeindex = Shader::current->getVertexAttributeIndex(attrib.name); attributeindex = Shader::current->getVertexAttributeIndex(attrib.name);
if (attributeindex >= 0) if (attributeindex >= 0)
+1
View File
@@ -60,6 +60,7 @@ public:
int indexInBuffer; int indexInBuffer;
int startArrayIndex; int startArrayIndex;
AttributeStep step; AttributeStep step;
int builtinAttributeIndex;
bool enabled; bool enabled;
}; };
+8 -5
View File
@@ -285,6 +285,12 @@ void SpriteBatch::attachAttribute(const std::string &name, Buffer *buffer, Mesh
newattrib.buffer = buffer; newattrib.buffer = buffer;
newattrib.mesh = mesh; newattrib.mesh = mesh;
BuiltinVertexAttribute builtinattrib;
if (getConstant(name.c_str(), builtinattrib))
newattrib.builtinAttributeIndex = (int)builtinattrib;
else
newattrib.builtinAttributeIndex = -1;
attached_attributes[name] = newattrib; attached_attributes[name] = newattrib;
} }
@@ -355,14 +361,11 @@ void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m)
if (buffer->getArrayLength() < (size_t) next * 4) if (buffer->getArrayLength() < (size_t) next * 4)
throw love::Exception("Buffer with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str()); throw love::Exception("Buffer with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
int attributeindex = -1; int attributeindex = it.second.builtinAttributeIndex;
// If the attribute is one of the LOVE-defined ones, use the constant // If the attribute is one of the LOVE-defined ones, use the constant
// attribute index for it, otherwise query the index from the shader. // attribute index for it, otherwise query the index from the shader.
BuiltinVertexAttribute builtinattrib; if (attributeindex < 0 && Shader::current)
if (getConstant(it.first.c_str(), builtinattrib))
attributeindex = (int) builtinattrib;
else if (Shader::current)
attributeindex = Shader::current->getVertexAttributeIndex(it.first); attributeindex = Shader::current->getVertexAttributeIndex(it.first);
if (attributeindex >= 0) if (attributeindex >= 0)
+1
View File
@@ -113,6 +113,7 @@ private:
StrongRef<Buffer> buffer; StrongRef<Buffer> buffer;
StrongRef<Mesh> mesh; StrongRef<Mesh> mesh;
int index; int index;
int builtinAttributeIndex;
}; };
/** /**