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
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,
// managed, and de-allocated by the compiler. It's contents
// are defined by and used by the compiler.
......@@ -99,6 +84,20 @@ typedef void* ShHandle;
ShHandle ShConstructCompiler(EShLanguage, EShSpec, const TBuiltInResource*);
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
// success or failure.
......@@ -110,16 +109,127 @@ int ShCompile(
const ShHandle,
const char* const shaderStrings[],
const int numStrings,
const EShOptimizationLevel,
int debugOptions
int compileOptions
);
// 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
// available in the object passed down, or the object is bad.
//
const char* ShGetInfoLog(const ShHandle);
const char* ShGetObjectCode(const ShHandle);
// The only exception to this rule is SH_OBJECT_CODE_LENGTH, which does not
// have a GL equivalent. It uses the value of GL_SHADER_SOURCE_LENGTH instead.
typedef enum {
SH_INFO_LOG_LENGTH = 0x8B84,
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
}
......
......@@ -54,12 +54,12 @@ int main(int argc, char* argv[])
{
TFailCode failCode = ESuccess;
int debugOptions = 0;
bool writeObjectCode = false;
int compileOptions = 0;
int numCompiles = 0;
ShHandle vertexCompiler = 0;
ShHandle fragmentCompiler = 0;
char* buffer = 0;
int bufferLen = 0;
ShInitialize();
......@@ -71,8 +71,8 @@ int main(int argc, char* argv[])
for (; (argc >= 1) && (failCode == ESuccess); argc--, argv++) {
if (argv[0][0] == '-' || argv[0][0] == '/') {
switch (argv[0][1]) {
case 'i': debugOptions |= EDebugOpIntermediate; break;
case 'o': writeObjectCode = true; break;
case 'i': compileOptions |= EShOptIntermediateTree; break;
case 'o': compileOptions |= EShOptObjectCode; break;
default: failCode = EFailUsage;
}
} else {
......@@ -91,15 +91,21 @@ int main(int argc, char* argv[])
default: break;
}
if (compiler) {
bool compiled = CompileFile(argv[0], compiler, debugOptions);
bool compiled = CompileFile(argv[0], compiler, compileOptions);
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");
if (compiled && writeObjectCode) {
if (compiled && (compileOptions & EShOptObjectCode)) {
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");
}
if (!compiled)
......@@ -120,6 +126,8 @@ int main(int argc, char* argv[])
ShDestruct(vertexCompiler);
if (fragmentCompiler)
ShDestruct(fragmentCompiler);
if (buffer)
free(buffer);
ShFinalize();
return failCode;
......@@ -153,7 +161,7 @@ static EShLanguage FindLanguage(char *name)
//
// 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;
char **data = ReadFileData(fileName);
......@@ -161,7 +169,7 @@ bool CompileFile(char *fileName, ShHandle compiler, int debugOptions)
if (!data)
return false;
ret = ShCompile(compiler, data, OutputMultipleStrings, EShOptNone, debugOptions);
ret = ShCompile(compiler, data, OutputMultipleStrings, compileOptions);
FreeFileData(data);
......
......@@ -91,6 +91,7 @@ public:
}
void erase() { sink.clear(); }
int size() { return static_cast<int>(sink.size()); }
const TPersistString& str() const { return sink; }
const char* c_str() const { return sink.c_str(); }
......
......@@ -180,9 +180,7 @@ int ShCompile(
const ShHandle handle,
const char* const shaderStrings[],
const int numStrings,
const EShOptimizationLevel optLevel,
int debugOptions
)
int compileOptions)
{
if (!InitThread())
return 0;
......@@ -227,40 +225,34 @@ int ShCompile(
if (!symbolTable.atGlobalLevel())
parseContext.infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
int ret = PaParseStrings(const_cast<char**>(shaderStrings), 0, numStrings, parseContext);
if (ret)
if (PaParseStrings(const_cast<char**>(shaderStrings), 0, numStrings, parseContext))
success = false;
if (success && parseContext.treeRoot) {
if (optLevel == EShOptNoGeneration)
parseContext.infoSink.info.message(EPrefixNone, "No errors. No code generation was requested.");
else {
success = intermediate.postProcess(parseContext.treeRoot, parseContext.language);
if (success) {
if (debugOptions & EDebugOpIntermediate)
intermediate.outputTree(parseContext.treeRoot);
//
// Call the machine dependent compiler
//
if (!compiler->compile(parseContext.treeRoot))
success = false;
}
success = intermediate.postProcess(parseContext.treeRoot, parseContext.language);
if (success) {
if (compileOptions & EShOptIntermediateTree)
intermediate.outputTree(parseContext.treeRoot);
//
// Call the machine dependent compiler
//
if (compileOptions & EShOptObjectCode)
success = compiler->compile(parseContext.treeRoot);
// TODO(alokp): Extract attributes and uniforms.
//if (compileOptions & EShOptAttribsUniforms)
}
} else if (!success) {
parseContext.infoSink.info.prefix(EPrefixError);
parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n";
success = false;
if (debugOptions & EDebugOpIntermediate)
if (compileOptions & EShOptIntermediateTree)
intermediate.outputTree(parseContext.treeRoot);
} else if (!parseContext.treeRoot) {
parseContext.error(1, "Unexpected end of file.", "", "");
parseContext.infoSink.info << parseContext.numErrors << " compilation errors. No code generated.\n\n";
success = false;
if (debugOptions & EDebugOpIntermediate)
intermediate.outputTree(parseContext.treeRoot);
}
intermediate.remove(parseContext.treeRoot);
......@@ -281,43 +273,88 @@ int ShCompile(
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.
//
const char* ShGetInfoLog(const ShHandle handle)
void ShGetInfoLog(const ShHandle handle, char* infoLog)
{
if (!InitThread())
return 0;
if (handle == 0)
return 0;
if (!handle || !infoLog)
return;
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TInfoSink* infoSink = 0;
if (base->getAsCompiler())
infoSink = &(base->getAsCompiler()->getInfoSink());
TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
infoSink->info << infoSink->debug.c_str();
return infoSink->info.c_str();
TInfoSink& infoSink = compiler->getInfoSink();
strcpy(infoLog, infoSink.info.c_str());
}
//
// Return any object code.
//
const char* ShGetObjectCode(const ShHandle handle)
void ShGetObjectCode(const ShHandle handle, char* objCode)
{
if (!InitThread())
return 0;
if (handle == 0)
return 0;
if (!handle || !objCode)
return;
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TInfoSink* infoSink;
TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
if (base->getAsCompiler())
infoSink = &(base->getAsCompiler()->getInfoSink());
TInfoSink& infoSink = compiler->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)
delete[] mInfoLog;
mInfoLog = NULL;
int result = ShCompile(compiler, &mSource, 1, EShOptNone, EDebugOpNone);
const char *obj = ShGetObjectCode(compiler);
const char *info = ShGetInfoLog(compiler);
int result = ShCompile(compiler, &mSource, 1, EShOptObjectCode);
if (result)
{
mHlsl = new char[strlen(obj) + 1];
strcpy(mHlsl, obj);
int objCodeLen = 0;
ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &objCodeLen);
mHlsl = new char[objCodeLen];
ShGetObjectCode(compiler, mHlsl);
TRACE("\n%s", mHlsl);
}
else
{
mInfoLog = new char[strlen(info) + 1];
strcpy(mInfoLog, info);
int infoLogLen = 0;
ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &infoLogLen);
mInfoLog = new char[infoLogLen];
ShGetInfoLog(compiler, 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