2 // Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 // Methods and classes related to uniform layout and packing in GLSL and HLSL.
10 #ifndef COMMON_BLOCKLAYOUT_H_
11 #define COMMON_BLOCKLAYOUT_H_
17 #include <GLSLANG/ShaderLang.h>
21 struct ShaderVariable;
22 struct InterfaceBlockField;
25 struct InterfaceBlock;
27 struct BlockMemberInfo
29 BlockMemberInfo(int offset, int arrayStride, int matrixStride, bool isRowMajorMatrix)
31 arrayStride(arrayStride),
32 matrixStride(matrixStride),
33 isRowMajorMatrix(isRowMajorMatrix)
36 static BlockMemberInfo getDefaultBlockInfo()
38 return BlockMemberInfo(-1, -1, -1, false);
44 bool isRowMajorMatrix;
47 class BlockLayoutEncoder
52 void encodeInterfaceBlockFields(const std::vector<InterfaceBlockField> &fields);
53 BlockMemberInfo encodeInterfaceBlockField(const InterfaceBlockField &field);
54 void encodeType(GLenum type, unsigned int arraySize, bool isRowMajorMatrix);
56 size_t getBlockSize() const { return mCurrentOffset * BytesPerComponent; }
57 size_t getCurrentRegister() const { return mCurrentOffset / ComponentsPerRegister; }
58 size_t getCurrentElement() const { return mCurrentOffset % ComponentsPerRegister; }
60 virtual void enterAggregateType() = 0;
61 virtual void exitAggregateType() = 0;
63 static const size_t BytesPerComponent = 4u;
64 static const unsigned int ComponentsPerRegister = 4u;
67 size_t mCurrentOffset;
71 virtual void getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut) = 0;
72 virtual void advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride) = 0;
75 // Block layout according to the std140 block layout
76 // See "Standard Uniform Block Layout" in Section 2.11.6 of the OpenGL ES 3.0 specification
78 class Std140BlockEncoder : public BlockLayoutEncoder
83 virtual void enterAggregateType();
84 virtual void exitAggregateType();
87 virtual void getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut);
88 virtual void advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride);
91 // Block layout packed according to the D3D9 or default D3D10+ register packing rules
92 // See http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx
93 // The strategy should be ENCODE_LOOSE for D3D9 constant blocks, and ENCODE_PACKED
94 // for everything else (D3D10+ constant blocks and all attributes/varyings).
96 class HLSLBlockEncoder : public BlockLayoutEncoder
99 enum HLSLBlockEncoderStrategy
105 HLSLBlockEncoder(HLSLBlockEncoderStrategy strategy);
107 virtual void enterAggregateType();
108 virtual void exitAggregateType();
109 void skipRegisters(unsigned int numRegisters);
111 bool isPacked() const { return mEncoderStrategy == ENCODE_PACKED; }
113 static HLSLBlockEncoderStrategy GetStrategyFor(ShShaderOutput outputType);
116 virtual void getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut);
117 virtual void advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride);
119 HLSLBlockEncoderStrategy mEncoderStrategy;
122 // This method returns the number of used registers for a ShaderVariable. It is dependent on the HLSLBlockEncoder
123 // class to count the number of used registers in a struct (which are individually packed according to the same rules).
124 unsigned int HLSLVariableRegisterCount(const Varying &variable);
125 unsigned int HLSLVariableRegisterCount(const Uniform &variable, ShShaderOutput outputType);
129 #endif // COMMON_BLOCKLAYOUT_H_