Commit 7beea408 by alokp@chromium.org

Added API to query for active attribs and uniforms. These functions are modeled…

Added API to query for active attribs and uniforms. These functions are modeled after glGetShaderiv, glGetProgramiv, glGetActiveAttrib, and glGetActiveUniform. The main difference between this and OpenGL API is that we do not have programs - just shaders. BUG=26 Review URL: http://codereview.appspot.com/2183041 git-svn-id: https://angleproject.googlecode.com/svn/trunk@425 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 8d012dfc
...@@ -70,21 +70,6 @@ typedef struct ...@@ -70,21 +70,6 @@ typedef struct
void ShInitBuiltInResource(TBuiltInResource* resources); void ShInitBuiltInResource(TBuiltInResource* resources);
// //
// Optimization level for the compiler.
//
typedef enum {
EShOptNoGeneration,
EShOptNone,
EShOptSimple, // Optimizations that can be done quickly
EShOptFull, // Optimizations that will take more time
} EShOptimizationLevel;
enum TDebugOptions {
EDebugOpNone = 0x000,
EDebugOpIntermediate = 0x001, // Writes intermediate tree into info-log.
};
//
// ShHandle held by but opaque to the driver. It is allocated, // ShHandle held by but opaque to the driver. It is allocated,
// managed, and de-allocated by the compiler. It's contents // managed, and de-allocated by the compiler. It's contents
// are defined by and used by the compiler. // are defined by and used by the compiler.
...@@ -99,6 +84,20 @@ typedef void* ShHandle; ...@@ -99,6 +84,20 @@ typedef void* ShHandle;
ShHandle ShConstructCompiler(EShLanguage, EShSpec, const TBuiltInResource*); ShHandle ShConstructCompiler(EShLanguage, EShSpec, const TBuiltInResource*);
void ShDestruct(ShHandle); void ShDestruct(ShHandle);
typedef enum {
// Performs validations only.
EShOptNone = 0x000,
// Writes intermediate tree to info log.
// Can be queried by calling ShGetInfoLog().
EShOptIntermediateTree = 0x001,
// Translates intermediate tree to glsl or hlsl shader.
// Can be queried by calling ShGetObjectCode().
EShOptObjectCode = 0x002,
// Extracts attributes and uniforms.
// Can be queried by calling ShGetActiveAttrib() and ShGetActiveUniform().
EShOptAttribsUniforms = 0x004,
} EShCompileOptions;
// //
// The return value of ShCompile is boolean, indicating // The return value of ShCompile is boolean, indicating
// success or failure. // success or failure.
...@@ -110,16 +109,127 @@ int ShCompile( ...@@ -110,16 +109,127 @@ int ShCompile(
const ShHandle, const ShHandle,
const char* const shaderStrings[], const char* const shaderStrings[],
const int numStrings, const int numStrings,
const EShOptimizationLevel, int compileOptions
int debugOptions
); );
// The names of the following enums have been derived by replacing GL prefix
// with SH. For example, SH_INFO_LOG_LENGTH is equivalent to GL_INFO_LOG_LENGTH.
// The enum values are also equal to the values of their GL counterpart. This
// is done to make it easier for applications to use the shader library.
// //
// All the following return 0 if the information is not // The only exception to this rule is SH_OBJECT_CODE_LENGTH, which does not
// available in the object passed down, or the object is bad. // have a GL equivalent. It uses the value of GL_SHADER_SOURCE_LENGTH instead.
// typedef enum {
const char* ShGetInfoLog(const ShHandle); SH_INFO_LOG_LENGTH = 0x8B84,
const char* ShGetObjectCode(const ShHandle); SH_OBJECT_CODE_LENGTH = 0x8B88, // equal to GL_SHADER_SOURCE_LENGTH.
SH_ACTIVE_UNIFORMS = 0x8B86,
SH_ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
SH_ACTIVE_ATTRIBUTES = 0x8B89,
SH_ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
} EShInfo;
typedef enum {
SH_FLOAT = 0x1406,
SH_FLOAT_VEC2 = 0x8B50,
SH_FLOAT_VEC3 = 0x8B51,
SH_FLOAT_VEC4 = 0x8B52,
SH_FLOAT_MAT2 = 0x8B5A,
SH_FLOAT_MAT3 = 0x8B5B,
SH_FLOAT_MAT4 = 0x8B5C,
SH_INT = 0x1404,
SH_INT_VEC2 = 0x8B53,
SH_INT_VEC3 = 0x8B54,
SH_INT_VEC4 = 0x8B55,
SH_BOOL = 0x8B56,
SH_BOOL_VEC2 = 0x8B57,
SH_BOOL_VEC3 = 0x8B58,
SH_BOOL_VEC4 = 0x8B59,
SH_SAMPLER_2D = 0x8B5E,
SH_SAMPLER_CUBE = 0x8B60,
} EShDataType;
// Returns a parameter from a compiled shader.
// Parameters:
// handle: Specifies the compiler
// pname: Specifies the parameter to query.
// The following parameters are defined:
// SH_INFO_LOG_LENGTH: the number of characters in the information log
// including the null termination character.
// SH_OBJECT_CODE_LENGTH: the number of characters in the object code
// including the null termination character.
// SH_ACTIVE_ATTRIBUTES: the number of active attribute variables.
// SH_ACTIVE_ATTRIBUTE_MAX_LENGTH: the length of the longest active attribute
// variable name including the null
// termination character.
// SH_ACTIVE_UNIFORMS: the number of active uniform variables.
// SH_ACTIVE_UNIFORM_MAX_LENGTH: the length of the longest active uniform
// variable name including the null
// termination character.
//
// params: Requested parameter
void ShGetInfo(const ShHandle handle, EShInfo pname, int* params);
// Returns nul-terminated information log for a compiled shader.
// Parameters:
// handle: Specifies the compiler
// infoLog: Specifies an array of characters that is used to return
// the information log. It is assumed that infoLog has enough memory
// to accomodate the information log. The size of the buffer required
// to store the returned information log can be obtained by calling
// ShGetInfo with SH_INFO_LOG_LENGTH.
void ShGetInfoLog(const ShHandle handle, char* infoLog);
// Returns null-terminated object code for a compiled shader.
// Parameters:
// handle: Specifies the compiler
// infoLog: Specifies an array of characters that is used to return
// the object code. It is assumed that infoLog has enough memory to
// accomodate the object code. The size of the buffer required to
// store the returned object code can be obtained by calling
// ShGetInfo with SH_OBJECT_CODE_LENGTH.
void ShGetObjectCode(const ShHandle handle, char* objCode);
// Returns information about an active attribute variable.
// Parameters:
// handle: Specifies the compiler
// index: Specifies the index of the attribute variable to be queried.
// length: Returns the number of characters actually written in the string
// indicated by name (excluding the null terminator) if a value other
// than NULL is passed.
// size: Returns the size of the attribute variable.
// type: Returns the data type of the attribute variable.
// name: Returns a null terminated string containing the name of the
// attribute variable. It is assumed that name has enough memory to
// accomodate the attribute variable name. The size of the buffer
// required to store the attribute variable name can be obtained by
// calling ShGetInfo with SH_ACTIVE_ATTRIBUTE_MAX_LENGTH.
void ShGetActiveAttrib(const ShHandle handle,
int index,
int* length,
int* size,
EShDataType* type,
char* name);
// Returns information about an active uniform variable.
// Parameters:
// handle: Specifies the compiler
// index: Specifies the index of the uniform variable to be queried.
// length: Returns the number of characters actually written in the string
// indicated by name (excluding the null terminator) if a value
// other than NULL is passed.
// size: Returns the size of the uniform variable.
// type: Returns the data type of the uniform variable.
// name: Returns a null terminated string containing the name of the
// uniform variable. It is assumed that name has enough memory to
// accomodate the uniform variable name. The size of the buffer required
// to store the uniform variable name can be obtained by calling
// ShGetInfo with SH_ACTIVE_UNIFORMS_MAX_LENGTH.
void ShGetActiveUniform(const ShHandle handle,
int index,
int* length,
int* size,
EShDataType* type,
char* name);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -54,12 +54,12 @@ int main(int argc, char* argv[]) ...@@ -54,12 +54,12 @@ int main(int argc, char* argv[])
{ {
TFailCode failCode = ESuccess; TFailCode failCode = ESuccess;
int debugOptions = 0; int compileOptions = 0;
bool writeObjectCode = false;
int numCompiles = 0; int numCompiles = 0;
ShHandle vertexCompiler = 0; ShHandle vertexCompiler = 0;
ShHandle fragmentCompiler = 0; ShHandle fragmentCompiler = 0;
char* buffer = 0;
int bufferLen = 0;
ShInitialize(); ShInitialize();
...@@ -71,8 +71,8 @@ int main(int argc, char* argv[]) ...@@ -71,8 +71,8 @@ int main(int argc, char* argv[])
for (; (argc >= 1) && (failCode == ESuccess); argc--, argv++) { for (; (argc >= 1) && (failCode == ESuccess); argc--, argv++) {
if (argv[0][0] == '-' || argv[0][0] == '/') { if (argv[0][0] == '-' || argv[0][0] == '/') {
switch (argv[0][1]) { switch (argv[0][1]) {
case 'i': debugOptions |= EDebugOpIntermediate; break; case 'i': compileOptions |= EShOptIntermediateTree; break;
case 'o': writeObjectCode = true; break; case 'o': compileOptions |= EShOptObjectCode; break;
default: failCode = EFailUsage; default: failCode = EFailUsage;
} }
} else { } else {
...@@ -91,15 +91,21 @@ int main(int argc, char* argv[]) ...@@ -91,15 +91,21 @@ int main(int argc, char* argv[])
default: break; default: break;
} }
if (compiler) { if (compiler) {
bool compiled = CompileFile(argv[0], compiler, debugOptions); bool compiled = CompileFile(argv[0], compiler, compileOptions);
LogMsg("BEGIN", "COMPILER", numCompiles, "INFO LOG"); LogMsg("BEGIN", "COMPILER", numCompiles, "INFO LOG");
puts(ShGetInfoLog(compiler)); ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &bufferLen);
buffer = (char*) realloc(buffer, bufferLen * sizeof(char));
ShGetInfoLog(compiler, buffer);
puts(buffer);
LogMsg("END", "COMPILER", numCompiles, "INFO LOG"); LogMsg("END", "COMPILER", numCompiles, "INFO LOG");
if (compiled && writeObjectCode) { if (compiled && (compileOptions & EShOptObjectCode)) {
LogMsg("BEGIN", "COMPILER", numCompiles, "OBJ CODE"); LogMsg("BEGIN", "COMPILER", numCompiles, "OBJ CODE");
puts(ShGetObjectCode(compiler)); ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &bufferLen);
buffer = (char*) realloc(buffer, bufferLen * sizeof(char));
ShGetObjectCode(compiler, buffer);
puts(buffer);
LogMsg("END", "COMPILER", numCompiles, "OBJ CODE"); LogMsg("END", "COMPILER", numCompiles, "OBJ CODE");
} }
if (!compiled) if (!compiled)
...@@ -120,6 +126,8 @@ int main(int argc, char* argv[]) ...@@ -120,6 +126,8 @@ int main(int argc, char* argv[])
ShDestruct(vertexCompiler); ShDestruct(vertexCompiler);
if (fragmentCompiler) if (fragmentCompiler)
ShDestruct(fragmentCompiler); ShDestruct(fragmentCompiler);
if (buffer)
free(buffer);
ShFinalize(); ShFinalize();
return failCode; return failCode;
...@@ -153,7 +161,7 @@ static EShLanguage FindLanguage(char *name) ...@@ -153,7 +161,7 @@ static EShLanguage FindLanguage(char *name)
// //
// Read a file's data into a string, and compile it using ShCompile // Read a file's data into a string, and compile it using ShCompile
// //
bool CompileFile(char *fileName, ShHandle compiler, int debugOptions) bool CompileFile(char *fileName, ShHandle compiler, int compileOptions)
{ {
int ret; int ret;
char **data = ReadFileData(fileName); char **data = ReadFileData(fileName);
...@@ -161,7 +169,7 @@ bool CompileFile(char *fileName, ShHandle compiler, int debugOptions) ...@@ -161,7 +169,7 @@ bool CompileFile(char *fileName, ShHandle compiler, int debugOptions)
if (!data) if (!data)
return false; return false;
ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, debugOptions); ret = ShCompile(compiler, data, OutputMultipleStrings, compileOptions);
FreeFileData(data); FreeFileData(data);
......
...@@ -91,6 +91,7 @@ public: ...@@ -91,6 +91,7 @@ public:
} }
void erase() { sink.clear(); } void erase() { sink.clear(); }
int size() { return static_cast<int>(sink.size()); }
const TPersistString& str() const { return sink; } const TPersistString& str() const { return sink; }
const char* c_str() const { return sink.c_str(); } const char* c_str() const { return sink.c_str(); }
......
...@@ -180,9 +180,7 @@ int ShCompile( ...@@ -180,9 +180,7 @@ int ShCompile(
const ShHandle handle, const ShHandle handle,
const char* const shaderStrings[], const char* const shaderStrings[],
const int numStrings, const int numStrings,
const EShOptimizationLevel optLevel, int compileOptions)
int debugOptions
)
{ {
if (!InitThread()) if (!InitThread())
return 0; return 0;
...@@ -227,40 +225,34 @@ int ShCompile( ...@@ -227,40 +225,34 @@ int ShCompile(
if (!symbolTable.atGlobalLevel()) if (!symbolTable.atGlobalLevel())
parseContext.infoSink.info.message(EPrefixInternalError, "Wrong symbol table level"); parseContext.infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
int ret = PaParseStrings(const_cast<char**>(shaderStrings), 0, numStrings, parseContext); if (PaParseStrings(const_cast<char**>(shaderStrings), 0, numStrings, parseContext))
if (ret)
success = false; success = false;
if (success && parseContext.treeRoot) { if (success && parseContext.treeRoot) {
if (optLevel == EShOptNoGeneration) success = intermediate.postProcess(parseContext.treeRoot, parseContext.language);
parseContext.infoSink.info.message(EPrefixNone, "No errors. No code generation was requested."); if (success) {
else { if (compileOptions & EShOptIntermediateTree)
success = intermediate.postProcess(parseContext.treeRoot, parseContext.language); intermediate.outputTree(parseContext.treeRoot);
if (success) { //
// Call the machine dependent compiler
if (debugOptions & EDebugOpIntermediate) //
intermediate.outputTree(parseContext.treeRoot); if (compileOptions & EShOptObjectCode)
success = compiler->compile(parseContext.treeRoot);
//
// Call the machine dependent compiler // TODO(alokp): Extract attributes and uniforms.
// //if (compileOptions & EShOptAttribsUniforms)
if (!compiler->compile(parseContext.treeRoot))
success = false;
}
} }
} else if (!success) { } else if (!success) {
parseContext.infoSink.info.prefix(EPrefixError); parseContext.infoSink.info.prefix(EPrefixError);
parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n"; parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n";
success = false; success = false;
if (debugOptions & EDebugOpIntermediate) if (compileOptions & EShOptIntermediateTree)
intermediate.outputTree(parseContext.treeRoot); intermediate.outputTree(parseContext.treeRoot);
} else if (!parseContext.treeRoot) { } else if (!parseContext.treeRoot) {
parseContext.error(1, "Unexpected end of file.", "", ""); parseContext.error(1, "Unexpected end of file.", "", "");
parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n"; parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n";
success = false; success = false;
if (debugOptions & EDebugOpIntermediate)
intermediate.outputTree(parseContext.treeRoot);
} }
intermediate.remove(parseContext.treeRoot); intermediate.remove(parseContext.treeRoot);
...@@ -281,43 +273,88 @@ int ShCompile( ...@@ -281,43 +273,88 @@ int ShCompile(
return success ? 1 : 0; return success ? 1 : 0;
} }
void ShGetInfo(const ShHandle handle, EShInfo pname, int* params)
{
if (!handle || !params)
return;
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
switch(pname)
{
case SH_INFO_LOG_LENGTH:
*params = compiler->getInfoSink().info.size() + 1;
break;
case SH_OBJECT_CODE_LENGTH:
*params = compiler->getInfoSink().obj.size() + 1;
break;
case SH_ACTIVE_UNIFORMS:
UNIMPLEMENTED();
break;
case SH_ACTIVE_UNIFORM_MAX_LENGTH:
UNIMPLEMENTED();
break;
case SH_ACTIVE_ATTRIBUTES:
UNIMPLEMENTED();
break;
case SH_ACTIVE_ATTRIBUTE_MAX_LENGTH:
UNIMPLEMENTED();
break;
default: UNREACHABLE();
}
}
// //
// Return any compiler log of messages for the application. // Return any compiler log of messages for the application.
// //
const char* ShGetInfoLog(const ShHandle handle) void ShGetInfoLog(const ShHandle handle, char* infoLog)
{ {
if (!InitThread()) if (!handle || !infoLog)
return 0; return;
if (handle == 0)
return 0;
TShHandleBase* base = static_cast<TShHandleBase*>(handle); TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TInfoSink* infoSink = 0; TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
if (base->getAsCompiler())
infoSink = &(base->getAsCompiler()->getInfoSink());
infoSink->info << infoSink->debug.c_str(); TInfoSink& infoSink = compiler->getInfoSink();
return infoSink->info.c_str(); strcpy(infoLog, infoSink.info.c_str());
} }
// //
// Return any object code. // Return any object code.
// //
const char* ShGetObjectCode(const ShHandle handle) void ShGetObjectCode(const ShHandle handle, char* objCode)
{ {
if (!InitThread()) if (!handle || !objCode)
return 0; return;
if (handle == 0)
return 0;
TShHandleBase* base = static_cast<TShHandleBase*>(handle); TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TInfoSink* infoSink; TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
if (base->getAsCompiler()) TInfoSink& infoSink = compiler->getInfoSink();
infoSink = &(base->getAsCompiler()->getInfoSink()); strcpy(objCode, infoSink.obj.c_str());
}
return infoSink->obj.c_str(); void ShGetActiveAttrib(const ShHandle handle,
int index,
int* length,
int* size,
EShDataType* type,
char* name)
{
UNIMPLEMENTED();
}
void ShGetActiveUniform(const ShHandle handle,
int index,
int* length,
int* size,
EShDataType* type,
char* name)
{
UNIMPLEMENTED();
} }
...@@ -281,21 +281,23 @@ void Shader::compileToHLSL(void *compiler) ...@@ -281,21 +281,23 @@ void Shader::compileToHLSL(void *compiler)
delete[] mInfoLog; delete[] mInfoLog;
mInfoLog = NULL; mInfoLog = NULL;
int result = ShCompile(compiler, &mSource, 1, EShOptNone, EDebugOpNone); int result = ShCompile(compiler, &mSource, 1, EShOptObjectCode);
const char *obj = ShGetObjectCode(compiler);
const char *info = ShGetInfoLog(compiler);
if (result) if (result)
{ {
mHlsl = new char[strlen(obj) + 1]; int objCodeLen = 0;
strcpy(mHlsl, obj); ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &objCodeLen);
mHlsl = new char[objCodeLen];
ShGetObjectCode(compiler, mHlsl);
TRACE("\n%s", mHlsl); TRACE("\n%s", mHlsl);
} }
else else
{ {
mInfoLog = new char[strlen(info) + 1]; int infoLogLen = 0;
strcpy(mInfoLog, info); ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &infoLogLen);
mInfoLog = new char[infoLogLen];
ShGetInfoLog(compiler, mInfoLog);
TRACE("\n%s", mInfoLog); TRACE("\n%s", mInfoLog);
} }
......
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