Commit 536d7265 by Geoff Lang

Replaced the char pointers with std::strings in the Shader classes.

TRAC #23775 Author: Geoff Lang Signed-off-by: Jamie Madill Signed-off-by: Shannon Woods
parent 42359ca9
...@@ -25,10 +25,6 @@ void *Shader::mVertexCompiler = NULL; ...@@ -25,10 +25,6 @@ void *Shader::mVertexCompiler = NULL;
Shader::Shader(ResourceManager *manager, const rx::Renderer *renderer, GLuint handle) Shader::Shader(ResourceManager *manager, const rx::Renderer *renderer, GLuint handle)
: mHandle(handle), mRenderer(renderer), mResourceManager(manager) : mHandle(handle), mRenderer(renderer), mResourceManager(manager)
{ {
mSource = NULL;
mHlsl = NULL;
mInfoLog = NULL;
uncompile(); uncompile();
initializeCompiler(); initializeCompiler();
...@@ -39,9 +35,6 @@ Shader::Shader(ResourceManager *manager, const rx::Renderer *renderer, GLuint ha ...@@ -39,9 +35,6 @@ Shader::Shader(ResourceManager *manager, const rx::Renderer *renderer, GLuint ha
Shader::~Shader() Shader::~Shader()
{ {
delete[] mSource;
delete[] mHlsl;
delete[] mInfoLog;
} }
GLuint Shader::getHandle() const GLuint Shader::getHandle() const
...@@ -51,67 +44,29 @@ GLuint Shader::getHandle() const ...@@ -51,67 +44,29 @@ GLuint Shader::getHandle() const
void Shader::setSource(GLsizei count, const char *const *string, const GLint *length) void Shader::setSource(GLsizei count, const char *const *string, const GLint *length)
{ {
delete[] mSource; std::ostringstream stream;
int totalLength = 0;
for (int i = 0; i < count; i++)
{
if (length && length[i] >= 0)
{
totalLength += length[i];
}
else
{
totalLength += (int)strlen(string[i]);
}
}
mSource = new char[totalLength + 1];
char *code = mSource;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
int stringLength; stream << string[i];
if (length && length[i] >= 0)
{
stringLength = length[i];
}
else
{
stringLength = (int)strlen(string[i]);
}
strncpy(code, string[i], stringLength);
code += stringLength;
} }
mSource[totalLength] = '\0'; mSource = stream.str();
} }
int Shader::getInfoLogLength() const int Shader::getInfoLogLength() const
{ {
if (!mInfoLog) return mInfoLog.empty() ? 0 : (mInfoLog.length() + 1);
{
return 0;
}
else
{
return strlen(mInfoLog) + 1;
}
} }
void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
{ {
int index = 0; int index = 0;
if (bufSize > 0) if (bufSize > 0)
{ {
if (mInfoLog) index = std::min(bufSize - 1, static_cast<GLsizei>(mInfoLog.length()));
{ memcpy(infoLog, mInfoLog.c_str(), index);
index = std::min(bufSize - 1, (int)strlen(mInfoLog));
memcpy(infoLog, mInfoLog, index);
}
infoLog[index] = '\0'; infoLog[index] = '\0';
} }
...@@ -124,39 +79,22 @@ void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) ...@@ -124,39 +79,22 @@ void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
int Shader::getSourceLength() const int Shader::getSourceLength() const
{ {
if (!mSource) return mSource.empty() ? 0 : (mSource.length() + 1);
{
return 0;
}
else
{
return strlen(mSource) + 1;
}
} }
int Shader::getTranslatedSourceLength() const int Shader::getTranslatedSourceLength() const
{ {
if (!mHlsl) return mHlsl.empty() ? 0 : (mHlsl.length() + 1);
{
return 0;
}
else
{
return strlen(mHlsl) + 1;
}
} }
void Shader::getSourceImpl(char *source, GLsizei bufSize, GLsizei *length, char *buffer) void Shader::getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer) const
{ {
int index = 0; int index = 0;
if (bufSize > 0) if (bufSize > 0)
{ {
if (source) index = std::min(bufSize - 1, static_cast<GLsizei>(source.length()));
{ memcpy(buffer, source.c_str(), index);
index = std::min(bufSize - 1, (int)strlen(source));
memcpy(buffer, source, index);
}
buffer[index] = '\0'; buffer[index] = '\0';
} }
...@@ -167,32 +105,32 @@ void Shader::getSourceImpl(char *source, GLsizei bufSize, GLsizei *length, char ...@@ -167,32 +105,32 @@ void Shader::getSourceImpl(char *source, GLsizei bufSize, GLsizei *length, char
} }
} }
void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const
{ {
getSourceImpl(mSource, bufSize, length, buffer); getSourceImpl(mSource, bufSize, length, buffer);
} }
void Shader::getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) void Shader::getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const
{ {
getSourceImpl(mHlsl, bufSize, length, buffer); getSourceImpl(mHlsl, bufSize, length, buffer);
} }
const sh::ActiveUniforms &Shader::getUniforms() const sh::ActiveUniforms &Shader::getUniforms() const
{ {
return mActiveUniforms; return mActiveUniforms;
} }
const sh::ActiveInterfaceBlocks &Shader::getInterfaceBlocks() const sh::ActiveInterfaceBlocks &Shader::getInterfaceBlocks() const
{ {
return mActiveInterfaceBlocks; return mActiveInterfaceBlocks;
} }
bool Shader::isCompiled() bool Shader::isCompiled() const
{ {
return mHlsl != NULL; return !mHlsl.empty();
} }
const char *Shader::getHLSL() const std::string &Shader::getHLSL() const
{ {
return mHlsl; return mHlsl;
} }
...@@ -279,17 +217,22 @@ void Shader::releaseCompiler() ...@@ -279,17 +217,22 @@ void Shader::releaseCompiler()
void Shader::parseVaryings() void Shader::parseVaryings()
{ {
if (mHlsl) if (!mHlsl.empty())
{ {
const char *input = strstr(mHlsl, "// Varyings") + 12; const std::string varyingsTitle("// Varyings");
size_t input = mHlsl.find(varyingsTitle);
if (input != std::string::npos)
{
input += varyingsTitle.length() + 1;
}
while(true) while(input != std::string::npos)
{ {
char string1[256]; char string1[256];
char string2[256]; char string2[256];
char string3[256]; char string3[256];
int matches = sscanf(input, "static %255s %255s %255s", string1, string2, string3); int matches = sscanf(mHlsl.c_str() + input, "static %255s %255s %255s", string1, string2, string3);
char *interpolation = "linear"; // Default char *interpolation = "linear"; // Default
char *type = string1; char *type = string1;
...@@ -320,18 +263,23 @@ void Shader::parseVaryings() ...@@ -320,18 +263,23 @@ void Shader::parseVaryings()
mVaryings.push_back(Varying(parseInterpolation(interpolation), parseType(type), name, size, array != NULL)); mVaryings.push_back(Varying(parseInterpolation(interpolation), parseType(type), name, size, array != NULL));
input = strstr(input, ";") + 2; const std::string semiColon(";");
input = mHlsl.find(semiColon, input);
if (input != std::string::npos)
{
input += semiColon.length() + 1;
}
} }
mUsesMultipleRenderTargets = strstr(mHlsl, "GL_USES_MRT") != NULL; mUsesMultipleRenderTargets = mHlsl.find("GL_USES_MRT") != std::string::npos;
mUsesFragColor = strstr(mHlsl, "GL_USES_FRAG_COLOR") != NULL; mUsesFragColor = mHlsl.find("GL_USES_FRAG_COLOR") != std::string::npos;
mUsesFragData = strstr(mHlsl, "GL_USES_FRAG_DATA") != NULL; mUsesFragData = mHlsl.find("GL_USES_FRAG_DATA") != std::string::npos;
mUsesFragCoord = strstr(mHlsl, "GL_USES_FRAG_COORD") != NULL; mUsesFragCoord = mHlsl.find("GL_USES_FRAG_COORD") != std::string::npos;
mUsesFrontFacing = strstr(mHlsl, "GL_USES_FRONT_FACING") != NULL; mUsesFrontFacing = mHlsl.find("GL_USES_FRONT_FACING") != std::string::npos;
mUsesPointSize = strstr(mHlsl, "GL_USES_POINT_SIZE") != NULL; mUsesPointSize = mHlsl.find("GL_USES_POINT_SIZE") != std::string::npos;
mUsesPointCoord = strstr(mHlsl, "GL_USES_POINT_COORD") != NULL; mUsesPointCoord = mHlsl.find("GL_USES_POINT_COORD") != std::string::npos;
mUsesDepthRange = strstr(mHlsl, "GL_USES_DEPTH_RANGE") != NULL; mUsesDepthRange = mHlsl.find("GL_USES_DEPTH_RANGE") != std::string::npos;
mUsesFragDepth = strstr(mHlsl, "GL_USES_FRAG_DEPTH") != NULL; mUsesFragDepth = mHlsl.find("GL_USES_FRAG_DEPTH") != std::string::npos;
} }
} }
...@@ -348,10 +296,8 @@ void Shader::resetVaryingsRegisterAssignment() ...@@ -348,10 +296,8 @@ void Shader::resetVaryingsRegisterAssignment()
void Shader::uncompile() void Shader::uncompile()
{ {
// set by compileToHLSL // set by compileToHLSL
delete[] mHlsl; mHlsl.clear();
mHlsl = NULL; mInfoLog.clear();
delete[] mInfoLog;
mInfoLog = NULL;
// set by parseVaryings // set by parseVaryings
mVaryings.clear(); mVaryings.clear();
...@@ -373,13 +319,6 @@ void Shader::uncompile() ...@@ -373,13 +319,6 @@ void Shader::uncompile()
void Shader::compileToHLSL(void *compiler) void Shader::compileToHLSL(void *compiler)
{ {
// ensure we don't pass a NULL source to the compiler
const char *source = "\0";
if (mSource)
{
source = mSource;
}
// ensure the compiler is loaded // ensure the compiler is loaded
initializeCompiler(); initializeCompiler();
...@@ -388,24 +327,29 @@ void Shader::compileToHLSL(void *compiler) ...@@ -388,24 +327,29 @@ void Shader::compileToHLSL(void *compiler)
if (perfActive()) if (perfActive())
{ {
sourcePath = getTempPath(); sourcePath = getTempPath();
writeFile(sourcePath.c_str(), source, strlen(source)); writeFile(sourcePath.c_str(), mSource.c_str(), mSource.length());
compileOptions |= SH_LINE_DIRECTIVES; compileOptions |= SH_LINE_DIRECTIVES;
} }
int result; int result;
if (sourcePath.empty()) if (sourcePath.empty())
{ {
result = ShCompile(compiler, &source, 1, compileOptions); const char* sourceStrings[] =
{
mSource.c_str(),
};
result = ShCompile(compiler, sourceStrings, ArraySize(sourceStrings), compileOptions);
} }
else else
{ {
const char* sourceStrings[2] = const char* sourceStrings[] =
{ {
sourcePath.c_str(), sourcePath.c_str(),
source mSource.c_str(),
}; };
result = ShCompile(compiler, sourceStrings, 2, compileOptions | SH_SOURCE_PATH); result = ShCompile(compiler, sourceStrings, ArraySize(sourceStrings), compileOptions | SH_SOURCE_PATH);
} }
size_t shaderVersion = 100; size_t shaderVersion = 100;
...@@ -415,18 +359,18 @@ void Shader::compileToHLSL(void *compiler) ...@@ -415,18 +359,18 @@ void Shader::compileToHLSL(void *compiler)
if (shaderVersion == 300 && mRenderer->getCurrentClientVersion() < 3) if (shaderVersion == 300 && mRenderer->getCurrentClientVersion() < 3)
{ {
const char versionError[] = "GLSL ES 3.00 is not supported by OpenGL ES 2.0 contexts"; mInfoLog = "GLSL ES 3.00 is not supported by OpenGL ES 2.0 contexts";
mInfoLog = new char[sizeof(versionError) + 1]; TRACE("\n%s", mInfoLog.c_str());
strcpy(mInfoLog, versionError);
TRACE("\n%s", mInfoLog);
} }
else if (result) else if (result)
{ {
size_t objCodeLen = 0; size_t objCodeLen = 0;
ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &objCodeLen); ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &objCodeLen);
mHlsl = new char[objCodeLen];
ShGetObjectCode(compiler, mHlsl); char* outputHLSL = new char[objCodeLen];
ShGetObjectCode(compiler, outputHLSL);
mHlsl = outputHLSL;
delete[] outputHLSL;
void *activeUniforms; void *activeUniforms;
ShGetInfoPointer(compiler, SH_ACTIVE_UNIFORMS_ARRAY, &activeUniforms); ShGetInfoPointer(compiler, SH_ACTIVE_UNIFORMS_ARRAY, &activeUniforms);
...@@ -440,10 +384,12 @@ void Shader::compileToHLSL(void *compiler) ...@@ -440,10 +384,12 @@ void Shader::compileToHLSL(void *compiler)
{ {
size_t infoLogLen = 0; size_t infoLogLen = 0;
ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &infoLogLen); ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &infoLogLen);
mInfoLog = new char[infoLogLen];
ShGetInfoLog(compiler, mInfoLog);
TRACE("\n%s", mInfoLog); char* infoLog = new char[infoLogLen];
ShGetInfoLog(compiler, infoLog);
mInfoLog = infoLog;
TRACE("\n%s", mInfoLog.c_str());
} }
} }
...@@ -666,8 +612,8 @@ int VertexShader::getSemanticIndex(const std::string &attributeName) ...@@ -666,8 +612,8 @@ int VertexShader::getSemanticIndex(const std::string &attributeName)
void VertexShader::parseAttributes() void VertexShader::parseAttributes()
{ {
const char *hlsl = getHLSL(); const std::string &hlsl = getHLSL();
if (hlsl) if (!hlsl.empty())
{ {
void *activeAttributes; void *activeAttributes;
ShGetInfoPointer(mVertexCompiler, SH_ACTIVE_ATTRIBUTES_ARRAY, &activeAttributes); ShGetInfoPointer(mVertexCompiler, SH_ACTIVE_ATTRIBUTES_ARRAY, &activeAttributes);
...@@ -697,8 +643,8 @@ void FragmentShader::compile() ...@@ -697,8 +643,8 @@ void FragmentShader::compile()
parseVaryings(); parseVaryings();
mVaryings.sort(compareVarying); mVaryings.sort(compareVarying);
const char *hlsl = getHLSL(); const std::string &hlsl = getHLSL();
if (hlsl) if (!hlsl.empty())
{ {
void *activeOutputVariables; void *activeOutputVariables;
ShGetInfoPointer(mFragmentCompiler, SH_ACTIVE_OUTPUT_VARIABLES_ARRAY, &activeOutputVariables); ShGetInfoPointer(mFragmentCompiler, SH_ACTIVE_OUTPUT_VARIABLES_ARRAY, &activeOutputVariables);
......
...@@ -72,18 +72,18 @@ class Shader ...@@ -72,18 +72,18 @@ class Shader
void deleteSource(); void deleteSource();
void setSource(GLsizei count, const char *const *string, const GLint *length); void setSource(GLsizei count, const char *const *string, const GLint *length);
int getInfoLogLength() const; int getInfoLogLength() const;
void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const;
int getSourceLength() const; int getSourceLength() const;
void getSource(GLsizei bufSize, GLsizei *length, char *buffer); void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
int getTranslatedSourceLength() const; int getTranslatedSourceLength() const;
void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer); void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
const sh::ActiveUniforms &getUniforms(); const sh::ActiveUniforms &getUniforms() const;
const sh::ActiveInterfaceBlocks &getInterfaceBlocks(); const sh::ActiveInterfaceBlocks &getInterfaceBlocks() const;
virtual void compile() = 0; virtual void compile() = 0;
virtual void uncompile(); virtual void uncompile();
bool isCompiled(); bool isCompiled() const;
const char *getHLSL(); const std::string &getHLSL() const;
void addRef(); void addRef();
void release(); void release();
...@@ -100,7 +100,7 @@ class Shader ...@@ -100,7 +100,7 @@ class Shader
void compileToHLSL(void *compiler); void compileToHLSL(void *compiler);
void getSourceImpl(char *source, GLsizei bufSize, GLsizei *length, char *buffer); void getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer) const;
static Interpolation parseInterpolation(const std::string &type); static Interpolation parseInterpolation(const std::string &type);
static GLenum parseType(const std::string &type); static GLenum parseType(const std::string &type);
...@@ -133,9 +133,9 @@ class Shader ...@@ -133,9 +133,9 @@ class Shader
unsigned int mRefCount; // Number of program objects this shader is attached to unsigned int mRefCount; // Number of program objects this shader is attached to
bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use
char *mSource; std::string mSource;
char *mHlsl; std::string mHlsl;
char *mInfoLog; std::string mInfoLog;
sh::ActiveUniforms mActiveUniforms; sh::ActiveUniforms mActiveUniforms;
sh::ActiveInterfaceBlocks mActiveInterfaceBlocks; sh::ActiveInterfaceBlocks mActiveInterfaceBlocks;
......
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