From ff0d2ba6c7fb23c9dca60801d3f12a79d360fef3 Mon Sep 17 00:00:00 2001 From: Jamie Madill Date: Wed, 14 May 2014 13:49:10 -0400 Subject: [PATCH] Split register allocation of varyings from the translator. The translator gl::Varying struct does not need to know about register allocation. We can put that entirely in the API library. This makes exposing Varying variables with the new translator types cleaner. BUG=angle:466 Change-Id: Ib85ac27de003913a01f76d5f4dc52454530859e6 Reviewed-on: https://chromium-review.googlesource.com/199736 Reviewed-by: Nicolas Capens Reviewed-by: Shannon Woods Tested-by: Jamie Madill --- src/common/shadervars.h | 12 +---- src/libGLESv2/DynamicHLSL.cpp | 91 ++++++++++++++++----------------- src/libGLESv2/DynamicHLSL.h | 13 +++-- src/libGLESv2/ProgramBinary.cpp | 12 ++--- src/libGLESv2/Shader.cpp | 10 ++-- src/libGLESv2/Shader.h | 23 +++++++-- 6 files changed, 89 insertions(+), 72 deletions(-) diff --git a/src/common/shadervars.h b/src/common/shadervars.h index f4617d76b..576060c62 100644 --- a/src/common/shadervars.h +++ b/src/common/shadervars.h @@ -60,7 +60,7 @@ struct ShaderVariable struct Uniform : public ShaderVariable { unsigned int registerIndex; - unsigned int elementIndex; // For struct varyings + unsigned int elementIndex; // Offset within a register, for struct members std::vector fields; Uniform(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, @@ -105,22 +105,14 @@ struct Varying : public ShaderVariable { InterpolationType interpolation; std::vector fields; - unsigned int registerIndex; // Assigned during link std::string structName; Varying(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, InterpolationType interpolationIn) : ShaderVariable(typeIn, precisionIn, nameIn, arraySizeIn), - interpolation(interpolationIn), - registerIndex(GL_INVALID_INDEX) + interpolation(interpolationIn) {} bool isStruct() const { return !fields.empty(); } - bool registerAssigned() const { return registerIndex != GL_INVALID_INDEX; } - - void resetRegisterAssignment() - { - registerIndex = GL_INVALID_INDEX; - } }; struct BlockMemberInfo diff --git a/src/libGLESv2/DynamicHLSL.cpp b/src/libGLESv2/DynamicHLSL.cpp index 8ec313880..20e4a2054 100644 --- a/src/libGLESv2/DynamicHLSL.cpp +++ b/src/libGLESv2/DynamicHLSL.cpp @@ -89,18 +89,17 @@ DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer) { } -static bool packVarying(Varying *varying, const int maxVaryingVectors, const ShaderVariable *packing[][4]) +static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing) { GLenum transposedType = TransposeMatrixType(varying->type); // matrices within varying structs are not transposed int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount(); int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType)); - bool success = false; - if (elements == 2 || elements == 3 || elements == 4) + if (elements >= 2 && elements <= 4) { - for (int r = 0; r <= maxVaryingVectors - registers && !success; r++) + for (int r = 0; r <= maxVaryingVectors - registers; r++) { bool available = true; @@ -127,13 +126,13 @@ static bool packVarying(Varying *varying, const int maxVaryingVectors, const Sha } } - success = true; + return true; } } - if (!success && elements == 2) + if (elements == 2) { - for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--) + for (int r = maxVaryingVectors - registers; r >= 0; r--) { bool available = true; @@ -160,7 +159,7 @@ static bool packVarying(Varying *varying, const int maxVaryingVectors, const Sha } } - success = true; + return true; } } } @@ -204,17 +203,17 @@ static bool packVarying(Varying *varying, const int maxVaryingVectors, const Sha } } - success = true; + return true; } } else UNREACHABLE(); - return success; + return false; } // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111 // Returns the number of used varying registers, or -1 if unsuccesful -int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, +int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader, VertexShader *vertexShader, const std::vector& transformFeedbackVaryings) { const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); @@ -226,7 +225,7 @@ int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][ for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++) { - Varying *varying = &fragmentShader->mVaryings[varyingIndex]; + PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex]; if (packVarying(varying, maxVaryingVectors, packing)) { packedVaryings.insert(varying->name); @@ -246,7 +245,7 @@ int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][ bool found = false; for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++) { - Varying *varying = &vertexShader->mVaryings[varyingIndex]; + PackedVarying *varying = &vertexShader->mVaryings[varyingIndex]; if (transformFeedbackVarying == varying->name) { if (!packVarying(varying, maxVaryingVectors, packing)) @@ -289,17 +288,17 @@ std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::st for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++) { - Varying *varying = &shader->mVaryings[varyingIndex]; - if (varying->registerAssigned()) + const PackedVarying &varying = shader->mVaryings[varyingIndex]; + if (varying.registerAssigned()) { - GLenum transposedType = TransposeMatrixType(varying->type); - int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType)); + GLenum transposedType = TransposeMatrixType(varying.type); + int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); - for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) + for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) { for (int row = 0; row < variableRows; row++) { - switch (varying->interpolation) + switch (varying.interpolation) { case INTERPOLATION_SMOOTH: varyingHLSL += " "; break; case INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break; @@ -307,16 +306,16 @@ std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::st default: UNREACHABLE(); } - unsigned int semanticIndex = elementIndex * variableRows + varying->registerIndex + row; + unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row; std::string n = Str(semanticIndex); std::string typeString; - if (varying->isStruct()) + if (varying.isStruct()) { // matrices within structs are not transposed, so // do not use the special struct prefix "rm" - typeString = decorateVariable(varying->structName); + typeString = decorateVariable(varying.structName); } else { @@ -330,9 +329,9 @@ std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::st if (linkedVaryings) { - linkedVaryings->push_back(LinkedVarying(varying->name, varying->type, varying->elementCount(), - varyingSemantic, varying->registerIndex, - variableRows * varying->elementCount())); + linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(), + varyingSemantic, varying.registerIndex, + variableRows * varying.elementCount())); } } } @@ -404,7 +403,7 @@ std::string DynamicHLSL::generateInputLayoutHLSL(const VertexFormat inputLayout[ "}\n"; } -bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4], +bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader, const std::vector& transformFeedbackVaryings, @@ -563,16 +562,16 @@ bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++) { - Varying *varying = &vertexShader->mVaryings[vertVaryingIndex]; - if (varying->registerAssigned()) + const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex]; + if (varying.registerAssigned()) { - for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) + for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) { - int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type))); + int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type))); for (int row = 0; row < variableRows; row++) { - int r = varying->registerIndex + elementIndex * variableRows + row; + int r = varying.registerIndex + elementIndex * variableRows + row; vertexHLSL += " output.v" + Str(r); bool sharedRegister = false; // Register used by multiple varyings @@ -592,7 +591,7 @@ bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const for (int x = 0; x < 4; x++) { - if (packing[r][x] == &*varying) + if (packing[r][x] == &varying) { switch(x) { @@ -605,9 +604,9 @@ bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const } } - vertexHLSL += " = _" + varying->name; + vertexHLSL += " = _" + varying.name; - if (varying->isArray()) + if (varying.isArray()) { vertexHLSL += ArrayString(elementIndex); } @@ -762,19 +761,19 @@ bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++) { - Varying *varying = &fragmentShader->mVaryings[varyingIndex]; - if (varying->registerAssigned()) + const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex]; + if (varying.registerAssigned()) { - for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) + for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) { - GLenum transposedType = TransposeMatrixType(varying->type); - int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType)); + GLenum transposedType = TransposeMatrixType(varying.type); + int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); for (int row = 0; row < variableRows; row++) { - std::string n = Str(varying->registerIndex + elementIndex * variableRows + row); - pixelHLSL += " _" + varying->name; + std::string n = Str(varying.registerIndex + elementIndex * variableRows + row); + pixelHLSL += " _" + varying.name; - if (varying->isArray()) + if (varying.isArray()) { pixelHLSL += ArrayString(elementIndex); } @@ -784,7 +783,7 @@ bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const pixelHLSL += ArrayString(row); } - if (varying->isStruct()) + if (varying.isStruct()) { pixelHLSL += " = input.v" + n + ";\n"; break; } @@ -870,14 +869,14 @@ void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map } } -std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const +std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const { // for now we only handle point sprite emulation ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4); - return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader); + return generatePointSpriteHLSL(registers, fragmentShader, vertexShader); } -std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const +std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const { ASSERT(registers >= 0); ASSERT(vertexShader->mUsesPointSize); diff --git a/src/libGLESv2/DynamicHLSL.h b/src/libGLESv2/DynamicHLSL.h index 352a5de07..07c2a2f2b 100644 --- a/src/libGLESv2/DynamicHLSL.h +++ b/src/libGLESv2/DynamicHLSL.h @@ -10,6 +10,7 @@ #define LIBGLESV2_DYNAMIC_HLSL_H_ #include "common/angleutils.h" +#include "libGLESv2/constants.h" namespace rx { @@ -27,24 +28,28 @@ struct LinkedVarying; class VertexAttribute; struct VertexFormat; struct ShaderVariable; +struct Varying; struct Attribute; +struct PackedVarying; + +typedef const PackedVarying *VaryingPacking[IMPLEMENTATION_MAX_VARYING_VECTORS][4]; class DynamicHLSL { public: explicit DynamicHLSL(rx::Renderer *const renderer); - int packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, + int packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader, VertexShader *vertexShader, const std::vector& transformFeedbackVaryings); std::string generateInputLayoutHLSL(const VertexFormat inputLayout[], const Attribute shaderAttributes[]) const; - bool generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4], + bool generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader, const std::vector& transformFeedbackVaryings, std::vector *linkedVaryings, std::map *programOutputVars) const; - std::string generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; + std::string generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const; void getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const; static const std::string VERTEX_ATTRIBUTE_STUB_STRING; @@ -57,7 +62,7 @@ class DynamicHLSL std::string generateVaryingHLSL(VertexShader *shader, const std::string &varyingSemantic, std::vector *linkedVaryings) const; void defineOutputVariables(FragmentShader *fragmentShader, std::map *programOutputVars) const; - std::string generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; + std::string generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const; // Prepend an underscore static std::string decorateVariable(const std::string &name); diff --git a/src/libGLESv2/ProgramBinary.cpp b/src/libGLESv2/ProgramBinary.cpp index 05c289556..a0742ab25 100644 --- a/src/libGLESv2/ProgramBinary.cpp +++ b/src/libGLESv2/ProgramBinary.cpp @@ -1013,17 +1013,17 @@ bool ProgramBinary::applyUniformBuffers(const std::vector boundBuff bool ProgramBinary::linkVaryings(InfoLog &infoLog, FragmentShader *fragmentShader, VertexShader *vertexShader) { - std::vector &fragmentVaryings = fragmentShader->getVaryings(); - std::vector &vertexVaryings = vertexShader->getVaryings(); + std::vector &fragmentVaryings = fragmentShader->getVaryings(); + std::vector &vertexVaryings = vertexShader->getVaryings(); for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++) { - gl::Varying *input = &fragmentVaryings[fragVaryingIndex]; + PackedVarying *input = &fragmentVaryings[fragVaryingIndex]; bool matched = false; for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++) { - gl::Varying *output = &vertexVaryings[vertVaryingIndex]; + PackedVarying *output = &vertexVaryings[vertVaryingIndex]; if (output->name == input->name) { if (!linkValidateVariables(infoLog, output->name, *input, *output)) @@ -1536,7 +1536,7 @@ bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBin mVertexWorkarounds = vertexShader->getD3DWorkarounds(); // Map the varyings to the register file - const gl::ShaderVariable *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL}; + VaryingPacking packing = { NULL }; int registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShader, vertexShader, transformFeedbackVaryings); if (registers < 0) @@ -1602,7 +1602,7 @@ bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBin if (usesGeometryShader()) { - std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader); + std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShader, vertexShader); mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings, (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), diff --git a/src/libGLESv2/Shader.cpp b/src/libGLESv2/Shader.cpp index baced1574..038ca626f 100644 --- a/src/libGLESv2/Shader.cpp +++ b/src/libGLESv2/Shader.cpp @@ -125,7 +125,7 @@ const std::vector &Shader::getInterfaceBlocks() const return mActiveInterfaceBlocks; } -std::vector &Shader::getVaryings() +std::vector &Shader::getVaryings() { return mVaryings; } @@ -227,7 +227,11 @@ void Shader::parseVaryings(void *compiler) { std::vector *activeVaryings; ShGetInfoPointer(compiler, SH_ACTIVE_VARYINGS_ARRAY, reinterpret_cast(&activeVaryings)); - mVaryings = *activeVaryings; + + for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++) + { + mVaryings.push_back(PackedVarying((*activeVaryings)[varyingIndex])); + } mUsesMultipleRenderTargets = mHlsl.find("GL_USES_MRT") != std::string::npos; mUsesFragColor = mHlsl.find("GL_USES_FRAG_COLOR") != std::string::npos; @@ -440,7 +444,7 @@ static const GLenum varyingPriorityList[] = }; // true if varying x has a higher priority in packing than y -bool Shader::compareVarying(const ShaderVariable &x, const ShaderVariable &y) +bool Shader::compareVarying(const PackedVarying &x, const PackedVarying &y) { if (x.type == y.type) { diff --git a/src/libGLESv2/Shader.h b/src/libGLESv2/Shader.h index e7df4e9f1..6b2f9dc3f 100644 --- a/src/libGLESv2/Shader.h +++ b/src/libGLESv2/Shader.h @@ -32,6 +32,23 @@ namespace gl { class ResourceManager; +struct PackedVarying : public Varying +{ + unsigned int registerIndex; // Assigned during link + + PackedVarying(const Varying &varying) + : Varying(varying), + registerIndex(GL_INVALID_INDEX) + {} + + bool registerAssigned() const { return registerIndex != GL_INVALID_INDEX; } + + void resetRegisterAssignment() + { + registerIndex = GL_INVALID_INDEX; + } +}; + class Shader { friend class DynamicHLSL; @@ -54,7 +71,7 @@ class Shader void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const; const std::vector &getUniforms() const; const std::vector &getInterfaceBlocks() const; - std::vector &getVaryings(); + std::vector &getVaryings(); virtual void compile() = 0; virtual void uncompile(); @@ -82,11 +99,11 @@ class Shader void getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer) const; - static bool compareVarying(const ShaderVariable &x, const ShaderVariable &y); + static bool compareVarying(const PackedVarying &x, const PackedVarying &y); const rx::Renderer *const mRenderer; - std::vector mVaryings; + std::vector mVaryings; bool mUsesMultipleRenderTargets; bool mUsesFragColor;