Optimize uniform handling by storing both decorated and undecorated names. Use a…

Optimize uniform handling by storing both decorated and undecorated names. Use a consistent naming scheme to clarify decorated/undecorated name usage. TRAC #16567 Bug=136 Signed-off-by: Daniel Koch Author: Nicolas Capens git-svn-id: https://angleproject.googlecode.com/svn/trunk@759 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent a3b4ab4c
#define MAJOR_VERSION 0 #define MAJOR_VERSION 0
#define MINOR_VERSION 0 #define MINOR_VERSION 0
#define BUILD_VERSION 0 #define BUILD_VERSION 0
#define BUILD_REVISION 754 #define BUILD_REVISION 759
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x) #define MACRO_STRINGIFY(x) STRINGIFY(x)
......
...@@ -33,7 +33,8 @@ std::string str(int i) ...@@ -33,7 +33,8 @@ std::string str(int i)
return buffer; return buffer;
} }
Uniform::Uniform(GLenum type, const std::string &name, unsigned int arraySize) : type(type), name(name), arraySize(arraySize) Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
: type(type), _name(_name), name(Program::undecorate(_name)), arraySize(arraySize)
{ {
int bytes = UniformTypeSize(type) * arraySize; int bytes = UniformTypeSize(type) * arraySize;
data = new unsigned char[bytes]; data = new unsigned char[bytes];
...@@ -47,8 +48,13 @@ Uniform::~Uniform() ...@@ -47,8 +48,13 @@ Uniform::~Uniform()
delete[] data; delete[] data;
} }
UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index) bool Uniform::isArray()
: name(name), element(element), index(index) {
return arraySize != 1; // FIXME: Arrays can be of size 1
}
UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
: name(Program::undecorate(_name)), element(element), index(index)
{ {
} }
...@@ -249,24 +255,23 @@ TextureType Program::getSamplerTextureType(SamplerType type, unsigned int sample ...@@ -249,24 +255,23 @@ TextureType Program::getSamplerTextureType(SamplerType type, unsigned int sample
return TEXTURE_2D; return TEXTURE_2D;
} }
GLint Program::getUniformLocation(const char *name, bool decorated) GLint Program::getUniformLocation(std::string name)
{ {
std::string _name = decorated ? name : decorate(name);
int subscript = 0; int subscript = 0;
// Strip any trailing array operator and retrieve the subscript // Strip any trailing array operator and retrieve the subscript
size_t open = _name.find_last_of('['); size_t open = name.find_last_of('[');
size_t close = _name.find_last_of(']'); size_t close = name.find_last_of(']');
if (open != std::string::npos && close == _name.length() - 1) if (open != std::string::npos && close == name.length() - 1)
{ {
subscript = atoi(_name.substr(open + 1).c_str()); subscript = atoi(name.substr(open + 1).c_str());
_name.erase(open); name.erase(open);
} }
unsigned int numUniforms = mUniformIndex.size(); unsigned int numUniforms = mUniformIndex.size();
for (unsigned int location = 0; location < numUniforms; location++) for (unsigned int location = 0; location < numUniforms; location++)
{ {
if (mUniformIndex[location].name == _name && if (mUniformIndex[location].name == name &&
mUniformIndex[location].element == subscript) mUniformIndex[location].element == subscript)
{ {
return location; return location;
...@@ -1588,12 +1593,12 @@ void Program::link() ...@@ -1588,12 +1593,12 @@ void Program::link()
// these uniforms are searched as already-decorated because gl_ and dx_ // these uniforms are searched as already-decorated because gl_ and dx_
// are reserved prefixes, and do not receive additional decoration // are reserved prefixes, and do not receive additional decoration
mDxDepthRangeLocation = getUniformLocation("dx_DepthRange", true); mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
mDxDepthLocation = getUniformLocation("dx_Depth", true); mDxDepthLocation = getUniformLocation("dx_Depth");
mDxViewportLocation = getUniformLocation("dx_Viewport", true); mDxViewportLocation = getUniformLocation("dx_Viewport");
mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize", true); mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW", true); mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines", true); mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
mLinked = true; // Success mLinked = true; // Success
} }
...@@ -1784,9 +1789,9 @@ bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT ...@@ -1784,9 +1789,9 @@ bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT
} }
} }
bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &_name)
{ {
Uniform *uniform = createUniform(constantDescription, name); Uniform *uniform = createUniform(constantDescription, _name);
if(!uniform) if(!uniform)
{ {
...@@ -1794,7 +1799,7 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s ...@@ -1794,7 +1799,7 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s
} }
// Check if already defined // Check if already defined
GLint location = getUniformLocation(name.c_str(), true); GLint location = getUniformLocation(uniform->name);
GLenum type = uniform->type; GLenum type = uniform->type;
if (location >= 0) if (location >= 0)
...@@ -1816,13 +1821,13 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s ...@@ -1816,13 +1821,13 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s
for (unsigned int i = 0; i < uniform->arraySize; ++i) for (unsigned int i = 0; i < uniform->arraySize; ++i)
{ {
mUniformIndex.push_back(UniformLocation(name, i, uniformIndex)); mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
} }
return true; return true;
} }
Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name) Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &_name)
{ {
if (constantDescription.Rows == 1) // Vectors and scalars if (constantDescription.Rows == 1) // Vectors and scalars
{ {
...@@ -1831,44 +1836,44 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st ...@@ -1831,44 +1836,44 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
case D3DXPT_SAMPLER2D: case D3DXPT_SAMPLER2D:
switch (constantDescription.Columns) switch (constantDescription.Columns)
{ {
case 1: return new Uniform(GL_SAMPLER_2D, name, constantDescription.Elements); case 1: return new Uniform(GL_SAMPLER_2D, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
case D3DXPT_SAMPLERCUBE: case D3DXPT_SAMPLERCUBE:
switch (constantDescription.Columns) switch (constantDescription.Columns)
{ {
case 1: return new Uniform(GL_SAMPLER_CUBE, name, constantDescription.Elements); case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
case D3DXPT_BOOL: case D3DXPT_BOOL:
switch (constantDescription.Columns) switch (constantDescription.Columns)
{ {
case 1: return new Uniform(GL_BOOL, name, constantDescription.Elements); case 1: return new Uniform(GL_BOOL, _name, constantDescription.Elements);
case 2: return new Uniform(GL_BOOL_VEC2, name, constantDescription.Elements); case 2: return new Uniform(GL_BOOL_VEC2, _name, constantDescription.Elements);
case 3: return new Uniform(GL_BOOL_VEC3, name, constantDescription.Elements); case 3: return new Uniform(GL_BOOL_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_BOOL_VEC4, name, constantDescription.Elements); case 4: return new Uniform(GL_BOOL_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
case D3DXPT_INT: case D3DXPT_INT:
switch (constantDescription.Columns) switch (constantDescription.Columns)
{ {
case 1: return new Uniform(GL_INT, name, constantDescription.Elements); case 1: return new Uniform(GL_INT, _name, constantDescription.Elements);
case 2: return new Uniform(GL_INT_VEC2, name, constantDescription.Elements); case 2: return new Uniform(GL_INT_VEC2, _name, constantDescription.Elements);
case 3: return new Uniform(GL_INT_VEC3, name, constantDescription.Elements); case 3: return new Uniform(GL_INT_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_INT_VEC4, name, constantDescription.Elements); case 4: return new Uniform(GL_INT_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
case D3DXPT_FLOAT: case D3DXPT_FLOAT:
switch (constantDescription.Columns) switch (constantDescription.Columns)
{ {
case 1: return new Uniform(GL_FLOAT, name, constantDescription.Elements); case 1: return new Uniform(GL_FLOAT, _name, constantDescription.Elements);
case 2: return new Uniform(GL_FLOAT_VEC2, name, constantDescription.Elements); case 2: return new Uniform(GL_FLOAT_VEC2, _name, constantDescription.Elements);
case 3: return new Uniform(GL_FLOAT_VEC3, name, constantDescription.Elements); case 3: return new Uniform(GL_FLOAT_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_FLOAT_VEC4, name, constantDescription.Elements); case 4: return new Uniform(GL_FLOAT_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
...@@ -1883,9 +1888,9 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st ...@@ -1883,9 +1888,9 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
case D3DXPT_FLOAT: case D3DXPT_FLOAT:
switch (constantDescription.Rows) switch (constantDescription.Rows)
{ {
case 2: return new Uniform(GL_FLOAT_MAT2, name, constantDescription.Elements); case 2: return new Uniform(GL_FLOAT_MAT2, _name, constantDescription.Elements);
case 3: return new Uniform(GL_FLOAT_MAT3, name, constantDescription.Elements); case 3: return new Uniform(GL_FLOAT_MAT3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_FLOAT_MAT4, name, constantDescription.Elements); case 4: return new Uniform(GL_FLOAT_MAT4, _name, constantDescription.Elements);
default: UNREACHABLE(); default: UNREACHABLE();
} }
break; break;
...@@ -1898,28 +1903,24 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st ...@@ -1898,28 +1903,24 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
} }
// This method needs to match OutputHLSL::decorate // This method needs to match OutputHLSL::decorate
std::string Program::decorate(const std::string &string) std::string Program::decorate(const std::string &name)
{ {
if (string.substr(0, 3) != "gl_" && string.substr(0, 3) != "dx_") if (name.substr(0, 3) != "gl_" && name.substr(0, 3) != "dx_")
{
return "_" + string;
}
else
{ {
return string; return "_" + name;
} }
return name;
} }
std::string Program::undecorate(const std::string &string) std::string Program::undecorate(const std::string &_name)
{ {
if (string.substr(0, 1) == "_") if (_name.substr(0, 1) == "_")
{ {
return string.substr(1); return _name.substr(1);
}
else
{
return string;
} }
return _name;
} }
bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v) bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
...@@ -2790,9 +2791,9 @@ void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, G ...@@ -2790,9 +2791,9 @@ void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, G
if (bufsize > 0) if (bufsize > 0)
{ {
std::string string = undecorate(mUniforms[uniform]->name); std::string string = mUniforms[uniform]->name;
if (mUniforms[uniform]->arraySize != 1) if (mUniforms[uniform]->isArray())
{ {
string += "[0]"; string += "[0]";
} }
...@@ -2836,8 +2837,8 @@ GLint Program::getActiveUniformMaxLength() ...@@ -2836,8 +2837,8 @@ GLint Program::getActiveUniformMaxLength()
{ {
if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.substr(0, 3) != "dx_") if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.substr(0, 3) != "dx_")
{ {
int length = (int)(undecorate(mUniforms[uniformIndex]->name).length() + 1); int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
if (mUniforms[uniformIndex]->arraySize != 1) if (mUniforms[uniformIndex]->isArray())
{ {
length += 3; // Counting in "[0]". length += 3; // Counting in "[0]".
} }
...@@ -2972,8 +2973,8 @@ void Program::getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS, ...@@ -2972,8 +2973,8 @@ void Program::getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS,
{ {
if (!targetUniform->handlesSet) if (!targetUniform->handlesSet)
{ {
targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->name.c_str()); targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->_name.c_str());
targetUniform->vsHandle = mConstantTableVS->GetConstantByName(0, targetUniform->name.c_str()); targetUniform->vsHandle = mConstantTableVS->GetConstantByName(0, targetUniform->_name.c_str());
targetUniform->handlesSet = true; targetUniform->handlesSet = true;
} }
......
// //
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
...@@ -28,12 +28,15 @@ class VertexShader; ...@@ -28,12 +28,15 @@ class VertexShader;
// Helper struct representing a single shader uniform // Helper struct representing a single shader uniform
struct Uniform struct Uniform
{ {
Uniform(GLenum type, const std::string &name, unsigned int arraySize); Uniform(GLenum type, const std::string &_name, unsigned int arraySize);
~Uniform(); ~Uniform();
bool isArray();
const GLenum type; const GLenum type;
const std::string name; const std::string _name; // Decorated name
const std::string name; // Undecorated name
const unsigned int arraySize; const unsigned int arraySize;
unsigned char *data; unsigned char *data;
...@@ -47,7 +50,7 @@ struct Uniform ...@@ -47,7 +50,7 @@ struct Uniform
// Struct used for correlating uniforms/elements of uniform arrays to handles // Struct used for correlating uniforms/elements of uniform arrays to handles
struct UniformLocation struct UniformLocation
{ {
UniformLocation(const std::string &name, unsigned int element, unsigned int index); UniformLocation(const std::string &_name, unsigned int element, unsigned int index);
std::string name; std::string name;
unsigned int element; unsigned int element;
...@@ -75,7 +78,7 @@ class Program ...@@ -75,7 +78,7 @@ class Program
GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex); GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex);
TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex); TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex);
GLint getUniformLocation(const char *name, bool decorated); GLint getUniformLocation(std::string name);
bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
...@@ -165,8 +168,8 @@ class Program ...@@ -165,8 +168,8 @@ class Program
void appendToInfoLog(const char *info, ...); void appendToInfoLog(const char *info, ...);
void resetInfoLog(); void resetInfoLog();
static std::string decorate(const std::string &string); // Prepend an underscore static std::string decorate(const std::string &name); // Prepend an underscore
static std::string undecorate(const std::string &string); // Remove leading underscore static std::string undecorate(const std::string &_name); // Remove leading underscore
static unsigned int issueSerial(); static unsigned int issueSerial();
......
...@@ -3446,7 +3446,7 @@ int __stdcall glGetUniformLocation(GLuint program, const GLchar* name) ...@@ -3446,7 +3446,7 @@ int __stdcall glGetUniformLocation(GLuint program, const GLchar* name)
return error(GL_INVALID_OPERATION, -1); return error(GL_INVALID_OPERATION, -1);
} }
return programObject->getUniformLocation(name, false); return programObject->getUniformLocation(name);
} }
} }
catch(std::bad_alloc&) catch(std::bad_alloc&)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment