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 MINOR_VERSION 0
#define BUILD_VERSION 0
#define BUILD_REVISION 754
#define BUILD_REVISION 759
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
......
......@@ -33,7 +33,8 @@ std::string str(int i)
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;
data = new unsigned char[bytes];
......@@ -47,8 +48,13 @@ Uniform::~Uniform()
delete[] data;
}
UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
: name(name), element(element), index(index)
bool Uniform::isArray()
{
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
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;
// Strip any trailing array operator and retrieve the subscript
size_t open = _name.find_last_of('[');
size_t close = _name.find_last_of(']');
if (open != std::string::npos && close == _name.length() - 1)
size_t open = name.find_last_of('[');
size_t close = name.find_last_of(']');
if (open != std::string::npos && close == name.length() - 1)
{
subscript = atoi(_name.substr(open + 1).c_str());
_name.erase(open);
subscript = atoi(name.substr(open + 1).c_str());
name.erase(open);
}
unsigned int numUniforms = mUniformIndex.size();
for (unsigned int location = 0; location < numUniforms; location++)
{
if (mUniformIndex[location].name == _name &&
if (mUniformIndex[location].name == name &&
mUniformIndex[location].element == subscript)
{
return location;
......@@ -1588,12 +1593,12 @@ void Program::link()
// these uniforms are searched as already-decorated because gl_ and dx_
// are reserved prefixes, and do not receive additional decoration
mDxDepthRangeLocation = getUniformLocation("dx_DepthRange", true);
mDxDepthLocation = getUniformLocation("dx_Depth", true);
mDxViewportLocation = getUniformLocation("dx_Viewport", true);
mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize", true);
mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW", true);
mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines", true);
mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
mDxDepthLocation = getUniformLocation("dx_Depth");
mDxViewportLocation = getUniformLocation("dx_Viewport");
mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
mLinked = true; // Success
}
......@@ -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)
{
......@@ -1794,7 +1799,7 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s
}
// Check if already defined
GLint location = getUniformLocation(name.c_str(), true);
GLint location = getUniformLocation(uniform->name);
GLenum type = uniform->type;
if (location >= 0)
......@@ -1816,13 +1821,13 @@ bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::s
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;
}
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
{
......@@ -1831,44 +1836,44 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
case D3DXPT_SAMPLER2D:
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();
}
break;
case D3DXPT_SAMPLERCUBE:
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();
}
break;
case D3DXPT_BOOL:
switch (constantDescription.Columns)
{
case 1: return new Uniform(GL_BOOL, 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 4: return new Uniform(GL_BOOL_VEC4, 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 3: return new Uniform(GL_BOOL_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_BOOL_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE();
}
break;
case D3DXPT_INT:
switch (constantDescription.Columns)
{
case 1: return new Uniform(GL_INT, 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 4: return new Uniform(GL_INT_VEC4, 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 3: return new Uniform(GL_INT_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_INT_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE();
}
break;
case D3DXPT_FLOAT:
switch (constantDescription.Columns)
{
case 1: return new Uniform(GL_FLOAT, 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 4: return new Uniform(GL_FLOAT_VEC4, 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 3: return new Uniform(GL_FLOAT_VEC3, _name, constantDescription.Elements);
case 4: return new Uniform(GL_FLOAT_VEC4, _name, constantDescription.Elements);
default: UNREACHABLE();
}
break;
......@@ -1883,9 +1888,9 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
case D3DXPT_FLOAT:
switch (constantDescription.Rows)
{
case 2: return new Uniform(GL_FLOAT_MAT2, 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 2: return new Uniform(GL_FLOAT_MAT2, _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);
default: UNREACHABLE();
}
break;
......@@ -1898,28 +1903,24 @@ Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, st
}
// 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_")
{
return "_" + string;
}
else
if (name.substr(0, 3) != "gl_" && name.substr(0, 3) != "dx_")
{
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);
}
else
{
return string;
return _name.substr(1);
}
return _name;
}
bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
......@@ -2790,9 +2791,9 @@ void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, G
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]";
}
......@@ -2836,8 +2837,8 @@ GLint Program::getActiveUniformMaxLength()
{
if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.substr(0, 3) != "dx_")
{
int length = (int)(undecorate(mUniforms[uniformIndex]->name).length() + 1);
if (mUniforms[uniformIndex]->arraySize != 1)
int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
if (mUniforms[uniformIndex]->isArray())
{
length += 3; // Counting in "[0]".
}
......@@ -2972,8 +2973,8 @@ void Program::getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS,
{
if (!targetUniform->handlesSet)
{
targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->name.c_str());
targetUniform->vsHandle = mConstantTableVS->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->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
// found in the LICENSE file.
//
......@@ -28,12 +28,15 @@ class VertexShader;
// Helper struct representing a single shader uniform
struct Uniform
{
Uniform(GLenum type, const std::string &name, unsigned int arraySize);
Uniform(GLenum type, const std::string &_name, unsigned int arraySize);
~Uniform();
bool isArray();
const GLenum type;
const std::string name;
const std::string _name; // Decorated name
const std::string name; // Undecorated name
const unsigned int arraySize;
unsigned char *data;
......@@ -47,7 +50,7 @@ struct Uniform
// Struct used for correlating uniforms/elements of uniform arrays to handles
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;
unsigned int element;
......@@ -75,7 +78,7 @@ class Program
GLint getSamplerMapping(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 setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
......@@ -165,8 +168,8 @@ class Program
void appendToInfoLog(const char *info, ...);
void resetInfoLog();
static std::string decorate(const std::string &string); // Prepend an underscore
static std::string undecorate(const std::string &string); // Remove leading underscore
static std::string decorate(const std::string &name); // Prepend an underscore
static std::string undecorate(const std::string &_name); // Remove leading underscore
static unsigned int issueSerial();
......
......@@ -3446,7 +3446,7 @@ int __stdcall glGetUniformLocation(GLuint program, const GLchar* name)
return error(GL_INVALID_OPERATION, -1);
}
return programObject->getUniformLocation(name, false);
return programObject->getUniformLocation(name);
}
}
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