OSDN Git Service

Uniform buffer related fixes
authorAlexis Hetu <sugoi@google.com>
Wed, 22 Jul 2015 20:37:02 +0000 (16:37 -0400)
committerAlexis Hétu <sugoi@google.com>
Thu, 23 Jul 2015 16:57:13 +0000 (16:57 +0000)
- Added an actual offset, in registers, to the Uniform structure to
  take into account that types can have different register sizes.
- Fixed the array check in OutputASM::declareUniform() so that it
  doesn't make an array of blocks when declaring a member as an
  array in the default uniform block.
- Fixed arrayStride and matrixStride in the BlockInfo constructor.
- Fixed memberUniformIndexes to use uniform index instead of
  register index.

Change-Id: Id8ba23b5fef71c772bb45a45bb897ca5e2fae385
Reviewed-on: https://swiftshader-review.googlesource.com/3750
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
src/OpenGL/compiler/OutputASM.cpp
src/OpenGL/compiler/OutputASM.h
src/OpenGL/libGLESv2/Program.cpp

index da90024..e722baa 100644 (file)
@@ -75,8 +75,8 @@ namespace glsl
                ConstantUnion constants[4];\r
        };\r
 \r
-       Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int blockId) :\r
-               type(type), precision(precision), name(name), arraySize(arraySize), registerIndex(registerIndex), blockId(blockId)\r
+       Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int offset, int blockId) :\r
+               type(type), precision(precision), name(name), arraySize(arraySize), registerIndex(registerIndex), offset(offset), blockId(blockId)\r
        {\r
        }\r
 \r
@@ -2402,7 +2402,7 @@ namespace glsl
                }\r
        }\r
 \r
-       void OutputASM::declareUniform(const TType &type, const TString &name, int offset, int blockId)\r
+       void OutputASM::declareUniform(const TType &type, const TString &name, int registerIndex, int offset, int blockId)\r
        {\r
                const TStructure *structure = type.getStruct();\r
                const TInterfaceBlock *block = (type.isInterfaceBlock() || (blockId == -1)) ? type.getInterfaceBlock() : nullptr;\r
@@ -2414,7 +2414,7 @@ namespace glsl
                        blockId = activeUniformBlocks.size();\r
                        unsigned int dataSize = block->objectSize() * 4; // FIXME: assuming 4 bytes per element\r
                        activeUniformBlocks.push_back(UniformBlock(block->name().c_str(), block->hasInstanceName() ? block->instanceName().c_str() : std::string(), dataSize,\r
-                                                                  block->arraySize(), block->blockStorage(), block->matrixPacking() == EmpRowMajor, offset, blockId));\r
+                                                                  block->arraySize(), block->blockStorage(), block->matrixPacking() == EmpRowMajor, registerIndex, blockId));\r
                }\r
 \r
                if(!structure && !block)\r
@@ -2423,13 +2423,13 @@ namespace glsl
                        {\r
                                shaderObject->activeUniformBlocks[blockId].fields.push_back(activeUniforms.size());\r
                        }\r
-                       activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), offset, blockId));\r
+                       activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), registerIndex, offset, blockId));\r
 \r
                        if(isSamplerRegister(type))\r
                        {\r
                                for(int i = 0; i < type.totalRegisterCount(); i++)\r
                                {\r
-                                       shader->declareSampler(offset + i);\r
+                                       shader->declareSampler(registerIndex + i);\r
                                }\r
                        }\r
                }\r
@@ -2438,28 +2438,30 @@ namespace glsl
                        const TFieldList& fields = structure ? structure->fields() : block->fields();\r
                        const bool containerHasName = structure || block->hasInstanceName();\r
                        const TString &containerName = structure ? name : (containerHasName ? block->instanceName() : TString());\r
-                       if(type.isArray())\r
+                       if(type.isArray() && (structure || type.isInterfaceBlock()))\r
                        {\r
-                               int elementOffset = offset;\r
+                               int fieldRegisterIndex = (blockId == -1) ? registerIndex : 0;\r
+                               int fieldOffset = 0;\r
 \r
                                for(int i = 0; i < type.getArraySize(); i++)\r
                                {\r
-                                       int fieldOffset = (blockId == -1) ? elementOffset : 0;\r
                                        for(size_t j = 0; j < fields.size(); j++)\r
                                        {\r
                                                const TType &fieldType = *(fields[j]->type());\r
                                                const TString &fieldName = fields[j]->name();\r
 \r
                                                const TString uniformName = containerHasName ? containerName + "[" + str(i) + "]." + fieldName : fieldName;\r
-                                               declareUniform(fieldType, uniformName, fieldOffset, blockId);\r
-                                               fieldOffset += fieldType.totalRegisterCount();\r
+                                               declareUniform(fieldType, uniformName, fieldRegisterIndex, fieldOffset, blockId);\r
+                                               int registerCount = fieldType.totalRegisterCount();\r
+                                               fieldRegisterIndex += registerCount;\r
+                                               fieldOffset += registerCount * fieldType.registerSize();\r
                                        }\r
-                                       elementOffset = fieldOffset;\r
                                }\r
                        }\r
                        else\r
                        {\r
-                               int fieldOffset = (blockId == -1) ? offset : 0;\r
+                               int fieldRegisterIndex = (blockId == -1) ? registerIndex : 0;\r
+                               int fieldOffset = 0;\r
 \r
                                for(size_t i = 0; i < fields.size(); i++)\r
                                {\r
@@ -2467,8 +2469,10 @@ namespace glsl
                                        const TString &fieldName = fields[i]->name();\r
 \r
                                        const TString uniformName = containerHasName ? containerName + "." + fieldName : fieldName;\r
-                                       declareUniform(fieldType, uniformName, fieldOffset, blockId);\r
-                                       fieldOffset += fieldType.totalRegisterCount();\r
+                                       declareUniform(fieldType, uniformName, fieldRegisterIndex, fieldOffset, blockId);\r
+                                       int registerCount = fieldType.totalRegisterCount();\r
+                                       fieldRegisterIndex += registerCount;\r
+                                       fieldOffset += registerCount * fieldType.registerSize();\r
                                }\r
                        }\r
                }\r
index 156fda5..ccee01a 100644 (file)
@@ -32,7 +32,7 @@ namespace glsl
 {\r
        struct Uniform\r
        {\r
-               Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int blockId);\r
+               Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int offset, int blockId);\r
 \r
                GLenum type;\r
                GLenum precision;\r
@@ -40,6 +40,7 @@ namespace glsl
                int arraySize;\r
        \r
                int registerIndex;\r
+               int offset;\r
 \r
                int blockId;\r
        };\r
@@ -205,7 +206,7 @@ namespace glsl
                int allocate(VariableArray &list, TIntermTyped *variable);\r
                void free(VariableArray &list, TIntermTyped *variable);\r
 \r
-               void declareUniform(const TType &type, const TString &name, int offset, int blockId = -1);\r
+               void declareUniform(const TType &type, const TString &name, int registerIndex, int offset = 0, int blockId = -1);\r
                GLenum glVariableType(const TType &type);\r
                GLenum glVariablePrecision(const TType &type);\r
 \r
index 4f17718..72d4683 100644 (file)
@@ -15,6 +15,7 @@
 #include "Program.h"\r
 \r
 #include "main.h"\r
+#include "Buffer.h"\r
 #include "Shader.h"\r
 #include "utilities.h"\r
 #include "common/debug.h"\r
@@ -37,14 +38,28 @@ namespace es2
 \r
        Uniform::BlockInfo::BlockInfo(const glsl::Uniform& uniform, int blockIndex, bool rowMajorLayout)\r
        {\r
+               static unsigned int registerSizeStd140 = 4; // std140 packing requires dword alignment\r
+\r
                if(blockIndex >= 0)\r
                {\r
                        index = blockIndex;\r
-                       offset = uniform.registerIndex;\r
-                       arrayStride = UniformTypeSize(uniform.type) * uniform.arraySize;\r
+                       offset = uniform.offset * registerSizeStd140;\r
                        isRowMajorMatrix = rowMajorLayout;\r
+                       int componentSize = UniformTypeSize(UniformComponentType(uniform.type));\r
                        int rowCount = VariableRowCount(uniform.type);\r
-                       matrixStride = (rowCount > 1) ? (isRowMajorMatrix ? rowCount : VariableColumnCount(uniform.type)) * UniformTypeSize(UniformComponentType(uniform.type)) : 0;\r
+                       if(rowCount > 1)\r
+                       {\r
+                               int colCount = VariableColumnCount(uniform.type);\r
+                               int matrixComponentCount = (isRowMajorMatrix ? colCount : rowCount);\r
+                               matrixStride = (rowCount > 1) ? matrixComponentCount * componentSize : 0;\r
+                               arrayStride = (uniform.arraySize > 0) ? matrixStride * (isRowMajorMatrix ? rowCount : colCount) : 0;\r
+                       }\r
+                       else\r
+                       {\r
+                               matrixStride = 0;\r
+                               int componentCount = UniformComponentCount(uniform.type);\r
+                               arrayStride = (uniform.arraySize > 0) ? componentSize * componentCount : 0;\r
+                       }\r
                }\r
                else\r
                {\r
@@ -1691,7 +1706,7 @@ namespace es2
                        std::vector<unsigned int> memberUniformIndexes;\r
                        for(size_t i = 0; i < fields.size(); ++i)\r
                        {\r
-                               memberUniformIndexes.push_back(activeUniforms[fields[i]].registerIndex);\r
+                               memberUniformIndexes.push_back(fields[i]);\r
                        }\r
 \r
                        if(block.arraySize > 0)\r