Commit e294bb87 by Jamie Madill

Add new shader inspection APIs.

Each new entry point corresponds to one of the variable types: varyings, attributes, uniforms, output variables, and interface blocks. They return a pointer to the vector with all of the parsed variables, which then the app can copy to its own memory. Currently we do not support the staticUse field in the HLSL translator. BUG=angle:466 Change-Id: I7dc09e761ab070feef5360ad27740110c44853b3 Reviewed-on: https://chromium-review.googlesource.com/208750Tested-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org> Reviewed-by: 's avatarNicolas Capens <capn@chromium.org>
parent 13cfd276
...@@ -23,9 +23,10 @@ ...@@ -23,9 +23,10 @@
#define COMPILER_EXPORT #define COMPILER_EXPORT
#endif #endif
#include "KHR/khrplatform.h"
#include <stddef.h> #include <stddef.h>
#include "KHR/khrplatform.h"
// //
// This is the platform independent interface between an OGL driver // This is the platform independent interface between an OGL driver
// and the shading language compiler. // and the shading language compiler.
...@@ -37,13 +38,16 @@ namespace sh ...@@ -37,13 +38,16 @@ namespace sh
typedef unsigned int GLenum; typedef unsigned int GLenum;
} }
// Must be included after GLenum proxy typedef
#include "ShaderVars.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// Version number for shader translation API. // Version number for shader translation API.
// It is incremented every time the API changes. // It is incremented every time the API changes.
#define ANGLE_SH_VERSION 128 #define ANGLE_SH_VERSION 129
typedef enum { typedef enum {
SH_GLES2_SPEC = 0x8B40, SH_GLES2_SPEC = 0x8B40,
...@@ -100,14 +104,9 @@ typedef enum { ...@@ -100,14 +104,9 @@ typedef enum {
SH_NAME_MAX_LENGTH = 0x6001, SH_NAME_MAX_LENGTH = 0x6001,
SH_HASHED_NAME_MAX_LENGTH = 0x6002, SH_HASHED_NAME_MAX_LENGTH = 0x6002,
SH_HASHED_NAMES_COUNT = 0x6003, SH_HASHED_NAMES_COUNT = 0x6003,
SH_ACTIVE_UNIFORMS_ARRAY = 0x6004, SH_SHADER_VERSION = 0x6004,
SH_SHADER_VERSION = 0x6005, SH_RESOURCES_STRING_LENGTH = 0x6005,
SH_ACTIVE_INTERFACE_BLOCKS_ARRAY = 0x6006, SH_OUTPUT_TYPE = 0x6006
SH_ACTIVE_OUTPUT_VARIABLES_ARRAY = 0x6007,
SH_ACTIVE_ATTRIBUTES_ARRAY = 0x6008,
SH_ACTIVE_VARYINGS_ARRAY = 0x6009,
SH_RESOURCES_STRING_LENGTH = 0x600A,
SH_OUTPUT_TYPE = 0x600B
} ShShaderInfo; } ShShaderInfo;
// Compile options. // Compile options.
...@@ -453,17 +452,17 @@ COMPILER_EXPORT void ShGetNameHashingEntry(const ShHandle handle, ...@@ -453,17 +452,17 @@ COMPILER_EXPORT void ShGetNameHashingEntry(const ShHandle handle,
char* name, char* name,
char* hashedName); char* hashedName);
// Returns a parameter from a compiled shader. // Shader variable inspection.
// Returns a pointer to a list of variables of the designated type.
// (See ShaderVars.h for type definitions, included above)
// Returns NULL on failure.
// Parameters: // Parameters:
// handle: Specifies the compiler // handle: Specifies the compiler
// pname: Specifies the parameter to query. COMPILER_EXPORT const std::vector<sh::Uniform> *ShGetUniforms(const ShHandle handle);
// The following parameters are defined: COMPILER_EXPORT const std::vector<sh::Varying> *ShGetVaryings(const ShHandle handle);
// SH_ACTIVE_UNIFORMS_ARRAY: an STL vector of active uniforms. Valid only for COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetAttributes(const ShHandle handle);
// HLSL output. COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetOutputVariables(const ShHandle handle);
// params: Requested parameter COMPILER_EXPORT const std::vector<sh::InterfaceBlock> *ShGetInterfaceBlocks(const ShHandle handle);
COMPILER_EXPORT void ShGetInfoPointer(const ShHandle handle,
ShShaderInfo pname,
void** params);
typedef struct typedef struct
{ {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include "ShaderLang.h" // Assume ShaderLang.h is included before ShaderVars.h, for sh::GLenum
namespace sh namespace sh
{ {
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
// Implementation for block layout classes and methods. // Implementation for block layout classes and methods.
// //
#include <GLSLANG/ShaderVars.h>
#include "common/blocklayout.h" #include "common/blocklayout.h"
#include "common/mathutil.h" #include "common/mathutil.h"
#include "common/utilities.h" #include "common/utilities.h"
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
// Methods for GL variable types (varyings, uniforms, etc) // Methods for GL variable types (varyings, uniforms, etc)
// //
#include <GLSLANG/ShaderVars.h> #include <GLSLANG/ShaderLang.h>
namespace sh namespace sh
{ {
......
...@@ -18,14 +18,26 @@ ...@@ -18,14 +18,26 @@
#include "compiler/translator/VariablePacker.h" #include "compiler/translator/VariablePacker.h"
#include "angle_gl.h" #include "angle_gl.h"
static bool isInitialized = false; namespace
{
enum ShaderVariableType
{
SHADERVAR_UNIFORM,
SHADERVAR_VARYING,
SHADERVAR_ATTRIBUTE,
SHADERVAR_OUTPUTVARIABLE,
SHADERVAR_INTERFACEBLOCK
};
bool isInitialized = false;
// //
// This is the platform independent interface between an OGL driver // This is the platform independent interface between an OGL driver
// and the shading language compiler. // and the shading language compiler.
// //
static bool checkVariableMaxLengths(const ShHandle handle, static bool CheckVariableMaxLengths(const ShHandle handle,
size_t expectedValue) size_t expectedValue)
{ {
size_t activeUniformLimit = 0; size_t activeUniformLimit = 0;
...@@ -39,7 +51,7 @@ static bool checkVariableMaxLengths(const ShHandle handle, ...@@ -39,7 +51,7 @@ static bool checkVariableMaxLengths(const ShHandle handle,
expectedValue == varyingLimit); expectedValue == varyingLimit);
} }
static bool checkMappedNameMaxLength(const ShHandle handle, size_t expectedValue) bool CheckMappedNameMaxLength(const ShHandle handle, size_t expectedValue)
{ {
size_t mappedNameMaxLength = 0; size_t mappedNameMaxLength = 0;
ShGetInfo(handle, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameMaxLength); ShGetInfo(handle, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameMaxLength);
...@@ -47,7 +59,7 @@ static bool checkMappedNameMaxLength(const ShHandle handle, size_t expectedValue ...@@ -47,7 +59,7 @@ static bool checkMappedNameMaxLength(const ShHandle handle, size_t expectedValue
} }
template <typename VarT> template <typename VarT>
static const sh::ShaderVariable *ReturnVariable(const std::vector<VarT> &infoList, int index) const sh::ShaderVariable *ReturnVariable(const std::vector<VarT> &infoList, int index)
{ {
if (index < 0 || static_cast<size_t>(index) >= infoList.size()) if (index < 0 || static_cast<size_t>(index) >= infoList.size())
{ {
...@@ -57,7 +69,7 @@ static const sh::ShaderVariable *ReturnVariable(const std::vector<VarT> &infoLis ...@@ -57,7 +69,7 @@ static const sh::ShaderVariable *ReturnVariable(const std::vector<VarT> &infoLis
return &infoList[index]; return &infoList[index];
} }
static const sh::ShaderVariable *GetVariable(const TCompiler *compiler, ShShaderInfo varType, int index) const sh::ShaderVariable *GetVariable(const TCompiler *compiler, ShShaderInfo varType, int index)
{ {
switch (varType) switch (varType)
{ {
...@@ -73,7 +85,7 @@ static const sh::ShaderVariable *GetVariable(const TCompiler *compiler, ShShader ...@@ -73,7 +85,7 @@ static const sh::ShaderVariable *GetVariable(const TCompiler *compiler, ShShader
} }
} }
static ShPrecisionType ConvertPrecision(sh::GLenum precision) ShPrecisionType ConvertPrecision(sh::GLenum precision)
{ {
switch (precision) switch (precision)
{ {
...@@ -91,6 +103,55 @@ static ShPrecisionType ConvertPrecision(sh::GLenum precision) ...@@ -91,6 +103,55 @@ static ShPrecisionType ConvertPrecision(sh::GLenum precision)
} }
} }
template <typename VarT>
const std::vector<VarT> *GetVariableList(const TCompiler *compiler, ShaderVariableType variableType);
template <>
const std::vector<sh::Uniform> *GetVariableList(const TCompiler *compiler, ShaderVariableType)
{
return &compiler->getUniforms();
}
template <>
const std::vector<sh::Varying> *GetVariableList(const TCompiler *compiler, ShaderVariableType)
{
return &compiler->getVaryings();
}
template <>
const std::vector<sh::Attribute> *GetVariableList(const TCompiler *compiler, ShaderVariableType variableType)
{
return (variableType == SHADERVAR_ATTRIBUTE ?
&compiler->getAttributes() :
&compiler->getOutputVariables());
}
template <>
const std::vector<sh::InterfaceBlock> *GetVariableList(const TCompiler *compiler, ShaderVariableType)
{
return &compiler->getInterfaceBlocks();
}
template <typename VarT>
const std::vector<VarT> *GetShaderVariables(const ShHandle handle, ShaderVariableType variableType)
{
if (!handle)
{
return NULL;
}
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TCompiler* compiler = base->getAsCompiler();
if (!compiler)
{
return NULL;
}
return GetVariableList<VarT>(compiler, variableType);
}
}
// //
// Driver must call this first, once, before doing any other compiler operations. // Driver must call this first, once, before doing any other compiler operations.
// Subsequent calls to this function are no-op. // Subsequent calls to this function are no-op.
...@@ -372,7 +433,7 @@ void ShGetVariableInfo(const ShHandle handle, ...@@ -372,7 +433,7 @@ void ShGetVariableInfo(const ShHandle handle,
// SH_ACTIVE_UNIFORM_MAX_LENGTH, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, SH_VARYING_MAX_LENGTH // SH_ACTIVE_UNIFORM_MAX_LENGTH, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, SH_VARYING_MAX_LENGTH
// in ShGetInfo, below. // in ShGetInfo, below.
size_t variableLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec()); size_t variableLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
ASSERT(checkVariableMaxLengths(handle, variableLength)); ASSERT(CheckVariableMaxLengths(handle, variableLength));
strncpy(name, varInfo->name.c_str(), variableLength); strncpy(name, varInfo->name.c_str(), variableLength);
name[variableLength - 1] = 0; name[variableLength - 1] = 0;
if (mappedName) if (mappedName)
...@@ -380,7 +441,7 @@ void ShGetVariableInfo(const ShHandle handle, ...@@ -380,7 +441,7 @@ void ShGetVariableInfo(const ShHandle handle,
// This size must match that queried by // This size must match that queried by
// SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below. // SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below.
size_t maxMappedNameLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec()); size_t maxMappedNameLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
ASSERT(checkMappedNameMaxLength(handle, maxMappedNameLength)); ASSERT(CheckMappedNameMaxLength(handle, maxMappedNameLength));
strncpy(mappedName, varInfo->mappedName.c_str(), maxMappedNameLength); strncpy(mappedName, varInfo->mappedName.c_str(), maxMappedNameLength);
mappedName[maxMappedNameLength - 1] = 0; mappedName[maxMappedNameLength - 1] = 0;
} }
...@@ -429,34 +490,29 @@ void ShGetNameHashingEntry(const ShHandle handle, ...@@ -429,34 +490,29 @@ void ShGetNameHashingEntry(const ShHandle handle,
hashedName[len - 1] = '\0'; hashedName[len - 1] = '\0';
} }
void ShGetInfoPointer(const ShHandle handle, ShShaderInfo pname, void** params) const std::vector<sh::Uniform> *ShGetUniforms(const ShHandle handle)
{ {
if (!handle || !params) return GetShaderVariables<sh::Uniform>(handle, SHADERVAR_UNIFORM);
return; }
TShHandleBase* base = static_cast<TShHandleBase*>(handle); const std::vector<sh::Varying> *ShGetVaryings(const ShHandle handle)
TranslatorHLSL* translator = base->getAsTranslatorHLSL(); {
if (!translator) return; return GetShaderVariables<sh::Varying>(handle, SHADERVAR_VARYING);
}
switch(pname) const std::vector<sh::Attribute> *ShGetAttributes(const ShHandle handle)
{ {
case SH_ACTIVE_UNIFORMS_ARRAY: return GetShaderVariables<sh::Attribute>(handle, SHADERVAR_ATTRIBUTE);
*params = (void*)&translator->getUniforms(); }
break;
case SH_ACTIVE_INTERFACE_BLOCKS_ARRAY: const std::vector<sh::Attribute> *ShGetOutputVariables(const ShHandle handle)
*params = (void*)&translator->getInterfaceBlocks(); {
break; return GetShaderVariables<sh::Attribute>(handle, SHADERVAR_OUTPUTVARIABLE);
case SH_ACTIVE_OUTPUT_VARIABLES_ARRAY: }
*params = (void*)&translator->getOutputVariables();
break; const std::vector<sh::InterfaceBlock> *ShGetInterfaceBlocks(const ShHandle handle)
case SH_ACTIVE_ATTRIBUTES_ARRAY: {
*params = (void*)&translator->getAttributes(); return GetShaderVariables<sh::InterfaceBlock>(handle, SHADERVAR_INTERFACEBLOCK);
break;
case SH_ACTIVE_VARYINGS_ARRAY:
*params = (void*)&translator->getVaryings();
break;
default: UNREACHABLE();
}
} }
int ShCheckVariablesWithinPackingLimits( int ShCheckVariablesWithinPackingLimits(
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#ifndef COMPILER_VARIABLE_INFO_H_ #ifndef COMPILER_VARIABLE_INFO_H_
#define COMPILER_VARIABLE_INFO_H_ #define COMPILER_VARIABLE_INFO_H_
#include <GLSLANG/ShaderVars.h> #include <GLSLANG/ShaderLang.h>
#include "compiler/translator/intermediate.h" #include "compiler/translator/intermediate.h"
......
...@@ -3,12 +3,14 @@ ...@@ -3,12 +3,14 @@
// 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.
// //
#include "compiler/translator/VariablePacker.h"
#include "angle_gl.h"
#include "common/utilities.h"
#include <algorithm> #include <algorithm>
#include "angle_gl.h"
#include "compiler/translator/VariablePacker.h"
#include "common/utilities.h"
int VariablePacker::GetNumComponentsPerRow(sh::GLenum type) int VariablePacker::GetNumComponentsPerRow(sh::GLenum type)
{ {
switch (type) switch (type)
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include <stack> #include <stack>
#include "angle_gl.h" #include "angle_gl.h"
#include <GLSLANG/ShaderVars.h> #include <GLSLANG/ShaderLang.h>
#include "compiler/translator/Types.h" #include "compiler/translator/Types.h"
......
...@@ -22,6 +22,14 @@ namespace gl ...@@ -22,6 +22,14 @@ namespace gl
void *Shader::mFragmentCompiler = NULL; void *Shader::mFragmentCompiler = NULL;
void *Shader::mVertexCompiler = NULL; void *Shader::mVertexCompiler = NULL;
template <typename VarT>
const std::vector<VarT> *GetShaderVariables(const std::vector<VarT> *variableList)
{
// TODO: handle staticUse. for now, assume all returned variables are active.
ASSERT(variableList);
return variableList;
}
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)
{ {
...@@ -241,8 +249,8 @@ void Shader::parseVaryings(void *compiler) ...@@ -241,8 +249,8 @@ void Shader::parseVaryings(void *compiler)
{ {
if (!mHlsl.empty()) if (!mHlsl.empty())
{ {
std::vector<sh::Varying> *activeVaryings; const std::vector<sh::Varying> *activeVaryings = ShGetVaryings(compiler);
ShGetInfoPointer(compiler, SH_ACTIVE_VARYINGS_ARRAY, reinterpret_cast<void**>(&activeVaryings)); ASSERT(activeVaryings);
for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++) for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++)
{ {
...@@ -373,11 +381,9 @@ void Shader::compileToHLSL(void *compiler) ...@@ -373,11 +381,9 @@ void Shader::compileToHLSL(void *compiler)
mHlsl = outputHLSL; mHlsl = outputHLSL;
#endif #endif
delete[] outputHLSL; SafeDeleteArray(outputHLSL);
void *activeUniforms; mActiveUniforms = *GetShaderVariables(ShGetUniforms(compiler));
ShGetInfoPointer(compiler, SH_ACTIVE_UNIFORMS_ARRAY, &activeUniforms);
mActiveUniforms = *(std::vector<sh::Uniform>*)activeUniforms;
for (size_t uniformIndex = 0; uniformIndex < mActiveUniforms.size(); uniformIndex++) for (size_t uniformIndex = 0; uniformIndex < mActiveUniforms.size(); uniformIndex++)
{ {
...@@ -391,9 +397,7 @@ void Shader::compileToHLSL(void *compiler) ...@@ -391,9 +397,7 @@ void Shader::compileToHLSL(void *compiler)
mUniformRegisterMap[uniform.name] = index; mUniformRegisterMap[uniform.name] = index;
} }
void *activeInterfaceBlocks; mActiveInterfaceBlocks = *GetShaderVariables(ShGetInterfaceBlocks(compiler));
ShGetInfoPointer(compiler, SH_ACTIVE_INTERFACE_BLOCKS_ARRAY, &activeInterfaceBlocks);
mActiveInterfaceBlocks = *(std::vector<sh::InterfaceBlock>*)activeInterfaceBlocks;
for (size_t blockIndex = 0; blockIndex < mActiveInterfaceBlocks.size(); blockIndex++) for (size_t blockIndex = 0; blockIndex < mActiveInterfaceBlocks.size(); blockIndex++)
{ {
...@@ -524,9 +528,7 @@ void VertexShader::parseAttributes() ...@@ -524,9 +528,7 @@ void VertexShader::parseAttributes()
const std::string &hlsl = getHLSL(); const std::string &hlsl = getHLSL();
if (!hlsl.empty()) if (!hlsl.empty())
{ {
void *activeAttributes; mActiveAttributes = *GetShaderVariables(ShGetAttributes(mVertexCompiler));
ShGetInfoPointer(mVertexCompiler, SH_ACTIVE_ATTRIBUTES_ARRAY, &activeAttributes);
mActiveAttributes = *(std::vector<sh::Attribute>*)activeAttributes;
} }
} }
...@@ -555,9 +557,7 @@ void FragmentShader::compile() ...@@ -555,9 +557,7 @@ void FragmentShader::compile()
const std::string &hlsl = getHLSL(); const std::string &hlsl = getHLSL();
if (!hlsl.empty()) if (!hlsl.empty())
{ {
void *activeOutputVariables; mActiveOutputVariables = *GetShaderVariables(ShGetOutputVariables(mFragmentCompiler));
ShGetInfoPointer(mFragmentCompiler, SH_ACTIVE_OUTPUT_VARIABLES_ARRAY, &activeOutputVariables);
mActiveOutputVariables = *(std::vector<sh::Attribute>*)activeOutputVariables;
} }
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include <vector> #include <vector>
#include "angle_gl.h" #include "angle_gl.h"
#include <GLSLANG/ShaderVars.h> #include <GLSLANG/ShaderLang.h>
#include "common/angleutils.h" #include "common/angleutils.h"
#include "libGLESv2/angletypes.h" #include "libGLESv2/angletypes.h"
......
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