Added instancing support to Meshes via Mesh:setInstanceCount. Added a new built-in variable to vertex shaders: int love_InstanceID.

love.graphics.draw(mesh) will draw the mesh instancecount times, using hardware instancing when available. The only way to draw individual instances differently from each other is to use the love_InstanceID variable in a vertex shader.

Added love.graphics.isSupported(“instancing”). Hardware instancing is supported if true, otherwise a (slower) pseudo-instancing fallback is used internally when drawing instanced meshes.
This commit is contained in:
Alex Szpakowski
2014-01-21 06:01:50 -04:00
parent 81d9810ed8
commit 73f1ce0d40
13 changed files with 217 additions and 13 deletions
+30
View File
@@ -70,6 +70,7 @@ Shader::Shader(const ShaderSources &sources)
: shaderSources(sources)
, program(0)
, builtinUniforms()
, vertexAttributes()
, lastCanvas((Canvas *) -1)
{
if (shaderSources.empty())
@@ -181,6 +182,14 @@ void Shader::createProgram(const std::vector<GLuint> &shaderids)
for (it = shaderids.begin(); it != shaderids.end(); ++it)
glAttachShader(program, *it);
// Bind generic vertex attribute indices to names in the shader.
for (int i = 0; i < int(OpenGL::ATTRIB_MAX_ENUM); i++)
{
const char *name = nullptr;
if (attribNames.find((OpenGL::VertexAttrib) i, name))
glBindAttribLocation(program, i, (const GLchar *) name);
}
glLinkProgram(program);
// flag shaders for auto-deletion when the program object is deleted.
@@ -273,6 +282,15 @@ bool Shader::loadVolatile()
// Retreive all active uniform variables in this shader from OpenGL.
mapActiveUniforms();
for (int i = 0; i < int(OpenGL::ATTRIB_MAX_ENUM); i++)
{
const char *name = nullptr;
if (attribNames.find(OpenGL::VertexAttrib(i), name))
vertexAttributes[i] = glGetAttribLocation(program, name);
else
vertexAttributes[i] = -1;
}
if (current == this)
{
// make sure glUseProgram gets called.
@@ -633,6 +651,11 @@ int Shader::getTextureUnit(const std::string &name)
return texunit;
}
bool Shader::hasVertexAttrib(OpenGL::VertexAttrib attrib) const
{
return vertexAttributes[int(attrib)] != -1;
}
bool Shader::hasBuiltinExtern(BuiltinExtern builtin) const
{
return builtinUniforms[int(builtin)] != -1;
@@ -730,6 +753,13 @@ StringMap<Shader::ShaderType, Shader::TYPE_MAX_ENUM>::Entry Shader::typeNameEntr
StringMap<Shader::ShaderType, Shader::TYPE_MAX_ENUM> Shader::typeNames(Shader::typeNameEntries, sizeof(Shader::typeNameEntries));
StringMap<OpenGL::VertexAttrib, OpenGL::ATTRIB_MAX_ENUM>::Entry Shader::attribNameEntries[] =
{
{"love_PseudoInstanceID", OpenGL::ATTRIB_PSEUDO_INSTANCE_ID},
};
StringMap<OpenGL::VertexAttrib, OpenGL::ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));
StringMap<Shader::BuiltinExtern, Shader::BUILTIN_MAX_ENUM>::Entry Shader::builtinNameEntries[] =
{
{"love_ScreenParams", Shader::BUILTIN_SCREEN_PARAMS},