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 @@
#define COMPILER_EXPORT
#endif
#include "KHR/khrplatform.h"
#include <stddef.h>
#include "KHR/khrplatform.h"
//
// This is the platform independent interface between an OGL driver
// and the shading language compiler.
......@@ -37,13 +38,16 @@ namespace sh
typedef unsigned int GLenum;
}
// Must be included after GLenum proxy typedef
#include "ShaderVars.h"
#ifdef __cplusplus
extern "C" {
#endif
// Version number for shader translation API.
// It is incremented every time the API changes.
#define ANGLE_SH_VERSION 128
#define ANGLE_SH_VERSION 129
typedef enum {
SH_GLES2_SPEC = 0x8B40,
......@@ -100,14 +104,9 @@ typedef enum {
SH_NAME_MAX_LENGTH = 0x6001,
SH_HASHED_NAME_MAX_LENGTH = 0x6002,
SH_HASHED_NAMES_COUNT = 0x6003,
SH_ACTIVE_UNIFORMS_ARRAY = 0x6004,
SH_SHADER_VERSION = 0x6005,
SH_ACTIVE_INTERFACE_BLOCKS_ARRAY = 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
SH_SHADER_VERSION = 0x6004,
SH_RESOURCES_STRING_LENGTH = 0x6005,
SH_OUTPUT_TYPE = 0x6006
} ShShaderInfo;
// Compile options.
......@@ -453,17 +452,17 @@ COMPILER_EXPORT void ShGetNameHashingEntry(const ShHandle handle,
char* name,
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:
// handle: Specifies the compiler
// pname: Specifies the parameter to query.
// The following parameters are defined:
// SH_ACTIVE_UNIFORMS_ARRAY: an STL vector of active uniforms. Valid only for
// HLSL output.
// params: Requested parameter
COMPILER_EXPORT void ShGetInfoPointer(const ShHandle handle,
ShShaderInfo pname,
void** params);
COMPILER_EXPORT const std::vector<sh::Uniform> *ShGetUniforms(const ShHandle handle);
COMPILER_EXPORT const std::vector<sh::Varying> *ShGetVaryings(const ShHandle handle);
COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetAttributes(const ShHandle handle);
COMPILER_EXPORT const std::vector<sh::Attribute> *ShGetOutputVariables(const ShHandle handle);
COMPILER_EXPORT const std::vector<sh::InterfaceBlock> *ShGetInterfaceBlocks(const ShHandle handle);
typedef struct
{
......
......@@ -14,7 +14,7 @@
#include <vector>
#include <algorithm>
#include "ShaderLang.h"
// Assume ShaderLang.h is included before ShaderVars.h, for sh::GLenum
namespace sh
{
......
......@@ -7,8 +7,6 @@
// Implementation for block layout classes and methods.
//
#include <GLSLANG/ShaderVars.h>
#include "common/blocklayout.h"
#include "common/mathutil.h"
#include "common/utilities.h"
......
......@@ -7,7 +7,7 @@
// Methods for GL variable types (varyings, uniforms, etc)
//
#include <GLSLANG/ShaderVars.h>
#include <GLSLANG/ShaderLang.h>
namespace sh
{
......
......@@ -18,14 +18,26 @@
#include "compiler/translator/VariablePacker.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
// and the shading language compiler.
//
static bool checkVariableMaxLengths(const ShHandle handle,
static bool CheckVariableMaxLengths(const ShHandle handle,
size_t expectedValue)
{
size_t activeUniformLimit = 0;
......@@ -39,7 +51,7 @@ static bool checkVariableMaxLengths(const ShHandle handle,
expectedValue == varyingLimit);
}
static bool checkMappedNameMaxLength(const ShHandle handle, size_t expectedValue)
bool CheckMappedNameMaxLength(const ShHandle handle, size_t expectedValue)
{
size_t mappedNameMaxLength = 0;
ShGetInfo(handle, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameMaxLength);
......@@ -47,7 +59,7 @@ static bool checkMappedNameMaxLength(const ShHandle handle, size_t expectedValue
}
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())
{
......@@ -57,7 +69,7 @@ static const sh::ShaderVariable *ReturnVariable(const std::vector<VarT> &infoLis
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)
{
......@@ -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)
{
......@@ -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.
// Subsequent calls to this function are no-op.
......@@ -372,7 +433,7 @@ void ShGetVariableInfo(const ShHandle handle,
// SH_ACTIVE_UNIFORM_MAX_LENGTH, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, SH_VARYING_MAX_LENGTH
// in ShGetInfo, below.
size_t variableLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
ASSERT(checkVariableMaxLengths(handle, variableLength));
ASSERT(CheckVariableMaxLengths(handle, variableLength));
strncpy(name, varInfo->name.c_str(), variableLength);
name[variableLength - 1] = 0;
if (mappedName)
......@@ -380,7 +441,7 @@ void ShGetVariableInfo(const ShHandle handle,
// This size must match that queried by
// SH_MAPPED_NAME_MAX_LENGTH in ShGetInfo, below.
size_t maxMappedNameLength = 1 + GetGlobalMaxTokenSize(compiler->getShaderSpec());
ASSERT(checkMappedNameMaxLength(handle, maxMappedNameLength));
ASSERT(CheckMappedNameMaxLength(handle, maxMappedNameLength));
strncpy(mappedName, varInfo->mappedName.c_str(), maxMappedNameLength);
mappedName[maxMappedNameLength - 1] = 0;
}
......@@ -429,34 +490,29 @@ void ShGetNameHashingEntry(const ShHandle handle,
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;
return GetShaderVariables<sh::Uniform>(handle, SHADERVAR_UNIFORM);
}
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TranslatorHLSL* translator = base->getAsTranslatorHLSL();
if (!translator) return;
const std::vector<sh::Varying> *ShGetVaryings(const ShHandle handle)
{
return GetShaderVariables<sh::Varying>(handle, SHADERVAR_VARYING);
}
switch(pname)
{
case SH_ACTIVE_UNIFORMS_ARRAY:
*params = (void*)&translator->getUniforms();
break;
case SH_ACTIVE_INTERFACE_BLOCKS_ARRAY:
*params = (void*)&translator->getInterfaceBlocks();
break;
case SH_ACTIVE_OUTPUT_VARIABLES_ARRAY:
*params = (void*)&translator->getOutputVariables();
break;
case SH_ACTIVE_ATTRIBUTES_ARRAY:
*params = (void*)&translator->getAttributes();
break;
case SH_ACTIVE_VARYINGS_ARRAY:
*params = (void*)&translator->getVaryings();
break;
default: UNREACHABLE();
}
const std::vector<sh::Attribute> *ShGetAttributes(const ShHandle handle)
{
return GetShaderVariables<sh::Attribute>(handle, SHADERVAR_ATTRIBUTE);
}
const std::vector<sh::Attribute> *ShGetOutputVariables(const ShHandle handle)
{
return GetShaderVariables<sh::Attribute>(handle, SHADERVAR_OUTPUTVARIABLE);
}
const std::vector<sh::InterfaceBlock> *ShGetInterfaceBlocks(const ShHandle handle)
{
return GetShaderVariables<sh::InterfaceBlock>(handle, SHADERVAR_INTERFACEBLOCK);
}
int ShCheckVariablesWithinPackingLimits(
......
......@@ -7,7 +7,7 @@
#ifndef COMPILER_VARIABLE_INFO_H_
#define COMPILER_VARIABLE_INFO_H_
#include <GLSLANG/ShaderVars.h>
#include <GLSLANG/ShaderLang.h>
#include "compiler/translator/intermediate.h"
......
......@@ -3,12 +3,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "compiler/translator/VariablePacker.h"
#include "angle_gl.h"
#include "common/utilities.h"
#include <algorithm>
#include "angle_gl.h"
#include "compiler/translator/VariablePacker.h"
#include "common/utilities.h"
int VariablePacker::GetNumComponentsPerRow(sh::GLenum type)
{
switch (type)
......
......@@ -10,7 +10,7 @@
#include <stack>
#include "angle_gl.h"
#include <GLSLANG/ShaderVars.h>
#include <GLSLANG/ShaderLang.h>
#include "compiler/translator/Types.h"
......
......@@ -22,6 +22,14 @@ namespace gl
void *Shader::mFragmentCompiler = 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)
: mHandle(handle), mRenderer(renderer), mResourceManager(manager)
{
......@@ -241,8 +249,8 @@ void Shader::parseVaryings(void *compiler)
{
if (!mHlsl.empty())
{
std::vector<sh::Varying> *activeVaryings;
ShGetInfoPointer(compiler, SH_ACTIVE_VARYINGS_ARRAY, reinterpret_cast<void**>(&activeVaryings));
const std::vector<sh::Varying> *activeVaryings = ShGetVaryings(compiler);
ASSERT(activeVaryings);
for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++)
{
......@@ -373,11 +381,9 @@ void Shader::compileToHLSL(void *compiler)
mHlsl = outputHLSL;
#endif
delete[] outputHLSL;
SafeDeleteArray(outputHLSL);
void *activeUniforms;
ShGetInfoPointer(compiler, SH_ACTIVE_UNIFORMS_ARRAY, &activeUniforms);
mActiveUniforms = *(std::vector<sh::Uniform>*)activeUniforms;
mActiveUniforms = *GetShaderVariables(ShGetUniforms(compiler));
for (size_t uniformIndex = 0; uniformIndex < mActiveUniforms.size(); uniformIndex++)
{
......@@ -391,9 +397,7 @@ void Shader::compileToHLSL(void *compiler)
mUniformRegisterMap[uniform.name] = index;
}
void *activeInterfaceBlocks;
ShGetInfoPointer(compiler, SH_ACTIVE_INTERFACE_BLOCKS_ARRAY, &activeInterfaceBlocks);
mActiveInterfaceBlocks = *(std::vector<sh::InterfaceBlock>*)activeInterfaceBlocks;
mActiveInterfaceBlocks = *GetShaderVariables(ShGetInterfaceBlocks(compiler));
for (size_t blockIndex = 0; blockIndex < mActiveInterfaceBlocks.size(); blockIndex++)
{
......@@ -524,9 +528,7 @@ void VertexShader::parseAttributes()
const std::string &hlsl = getHLSL();
if (!hlsl.empty())
{
void *activeAttributes;
ShGetInfoPointer(mVertexCompiler, SH_ACTIVE_ATTRIBUTES_ARRAY, &activeAttributes);
mActiveAttributes = *(std::vector<sh::Attribute>*)activeAttributes;
mActiveAttributes = *GetShaderVariables(ShGetAttributes(mVertexCompiler));
}
}
......@@ -555,9 +557,7 @@ void FragmentShader::compile()
const std::string &hlsl = getHLSL();
if (!hlsl.empty())
{
void *activeOutputVariables;
ShGetInfoPointer(mFragmentCompiler, SH_ACTIVE_OUTPUT_VARIABLES_ARRAY, &activeOutputVariables);
mActiveOutputVariables = *(std::vector<sh::Attribute>*)activeOutputVariables;
mActiveOutputVariables = *GetShaderVariables(ShGetOutputVariables(mFragmentCompiler));
}
}
......
......@@ -18,7 +18,7 @@
#include <vector>
#include "angle_gl.h"
#include <GLSLANG/ShaderVars.h>
#include <GLSLANG/ShaderLang.h>
#include "common/angleutils.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