Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 1 | // |
| 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. |
| 5 | // |
| 6 | // blocklayout.h: |
| 7 | // Methods and classes related to uniform layout and packing in GLSL and HLSL. |
| 8 | // |
| 9 | |
| 10 | #ifndef COMMON_BLOCKLAYOUT_H_ |
| 11 | #define COMMON_BLOCKLAYOUT_H_ |
| 12 | |
Jamie Madill | 13cfd27 | 2014-07-17 14:16:28 -0400 | [diff] [blame^] | 13 | #include <cstddef> |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 14 | #include <vector> |
Jamie Madill | 13cfd27 | 2014-07-17 14:16:28 -0400 | [diff] [blame^] | 15 | |
Jamie Madill | f51639a | 2014-06-25 16:04:57 -0400 | [diff] [blame] | 16 | #include "angle_gl.h" |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 17 | #include <GLSLANG/ShaderLang.h> |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 18 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 19 | namespace sh |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 20 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 21 | struct ShaderVariable; |
| 22 | struct InterfaceBlockField; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 23 | struct Uniform; |
| 24 | struct Varying; |
Jamie Madill | fc43d27 | 2014-07-11 17:02:02 -0400 | [diff] [blame] | 25 | struct InterfaceBlock; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 26 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 27 | struct BlockMemberInfo |
| 28 | { |
| 29 | BlockMemberInfo(int offset, int arrayStride, int matrixStride, bool isRowMajorMatrix) |
| 30 | : offset(offset), |
| 31 | arrayStride(arrayStride), |
| 32 | matrixStride(matrixStride), |
| 33 | isRowMajorMatrix(isRowMajorMatrix) |
| 34 | {} |
| 35 | |
| 36 | static BlockMemberInfo getDefaultBlockInfo() |
| 37 | { |
| 38 | return BlockMemberInfo(-1, -1, -1, false); |
| 39 | } |
| 40 | |
| 41 | int offset; |
| 42 | int arrayStride; |
| 43 | int matrixStride; |
| 44 | bool isRowMajorMatrix; |
| 45 | }; |
| 46 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 47 | class BlockLayoutEncoder |
| 48 | { |
| 49 | public: |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 50 | BlockLayoutEncoder(); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 51 | |
| 52 | void encodeInterfaceBlockFields(const std::vector<InterfaceBlockField> &fields); |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 53 | BlockMemberInfo encodeInterfaceBlockField(const InterfaceBlockField &field); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 54 | void encodeType(GLenum type, unsigned int arraySize, bool isRowMajorMatrix); |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 55 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 56 | size_t getBlockSize() const { return mCurrentOffset * BytesPerComponent; } |
Jamie Madill | bf9cce2 | 2014-07-18 10:33:09 -0400 | [diff] [blame] | 57 | size_t getCurrentRegister() const { return mCurrentOffset / ComponentsPerRegister; } |
| 58 | size_t getCurrentElement() const { return mCurrentOffset % ComponentsPerRegister; } |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 59 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 60 | virtual void enterAggregateType() = 0; |
| 61 | virtual void exitAggregateType() = 0; |
| 62 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 63 | static const size_t BytesPerComponent = 4u; |
| 64 | static const unsigned int ComponentsPerRegister = 4u; |
| 65 | |
| 66 | protected: |
| 67 | size_t mCurrentOffset; |
| 68 | |
| 69 | void nextRegister(); |
| 70 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 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; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 73 | }; |
| 74 | |
| 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 |
| 77 | |
| 78 | class Std140BlockEncoder : public BlockLayoutEncoder |
| 79 | { |
| 80 | public: |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 81 | Std140BlockEncoder(); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 82 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 83 | virtual void enterAggregateType(); |
| 84 | virtual void exitAggregateType(); |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 85 | |
| 86 | protected: |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 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); |
| 89 | }; |
| 90 | |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 91 | // Block layout packed according to the D3D9 or default D3D10+ register packing rules |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 92 | // See http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 93 | // The strategy should be ENCODE_LOOSE for D3D9 constant blocks, and ENCODE_PACKED |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 94 | // for everything else (D3D10+ constant blocks and all attributes/varyings). |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 95 | |
| 96 | class HLSLBlockEncoder : public BlockLayoutEncoder |
| 97 | { |
| 98 | public: |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 99 | enum HLSLBlockEncoderStrategy |
| 100 | { |
| 101 | ENCODE_PACKED, |
| 102 | ENCODE_LOOSE |
| 103 | }; |
| 104 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 105 | HLSLBlockEncoder(HLSLBlockEncoderStrategy strategy); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 106 | |
| 107 | virtual void enterAggregateType(); |
| 108 | virtual void exitAggregateType(); |
Jamie Madill | c600c8c | 2014-05-16 11:22:21 -0400 | [diff] [blame] | 109 | void skipRegisters(unsigned int numRegisters); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 110 | |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 111 | bool isPacked() const { return mEncoderStrategy == ENCODE_PACKED; } |
| 112 | |
Jamie Madill | bf9cce2 | 2014-07-18 10:33:09 -0400 | [diff] [blame] | 113 | static HLSLBlockEncoderStrategy GetStrategyFor(ShShaderOutput outputType); |
| 114 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 115 | protected: |
| 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); |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 118 | |
| 119 | HLSLBlockEncoderStrategy mEncoderStrategy; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 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); |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 125 | unsigned int HLSLVariableRegisterCount(const Uniform &variable, ShShaderOutput outputType); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 126 | |
| 127 | } |
| 128 | |
| 129 | #endif // COMMON_BLOCKLAYOUT_H_ |