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.cpp: |
| 7 | // Implementation for block layout classes and methods. |
| 8 | // |
| 9 | |
| 10 | #include "common/blocklayout.h" |
| 11 | #include "common/shadervars.h" |
| 12 | #include "common/mathutil.h" |
| 13 | #include "common/utilities.h" |
| 14 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 15 | namespace sh |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 16 | { |
| 17 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 18 | BlockLayoutEncoder::BlockLayoutEncoder() |
| 19 | : mCurrentOffset(0) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
| 23 | void BlockLayoutEncoder::encodeInterfaceBlockFields(const std::vector<InterfaceBlockField> &fields) |
| 24 | { |
| 25 | for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
| 26 | { |
| 27 | const InterfaceBlockField &variable = fields[fieldIndex]; |
| 28 | |
| 29 | if (variable.fields.size() > 0) |
| 30 | { |
| 31 | const unsigned int elementCount = std::max(1u, variable.arraySize); |
| 32 | |
| 33 | for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++) |
| 34 | { |
| 35 | enterAggregateType(); |
| 36 | encodeInterfaceBlockFields(variable.fields); |
| 37 | exitAggregateType(); |
| 38 | } |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | encodeInterfaceBlockField(variable); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 47 | BlockMemberInfo BlockLayoutEncoder::encodeInterfaceBlockField(const InterfaceBlockField &field) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 48 | { |
| 49 | int arrayStride; |
| 50 | int matrixStride; |
| 51 | |
| 52 | ASSERT(field.fields.empty()); |
| 53 | getBlockLayoutInfo(field.type, field.arraySize, field.isRowMajorMatrix, &arrayStride, &matrixStride); |
| 54 | |
| 55 | const BlockMemberInfo memberInfo(mCurrentOffset * BytesPerComponent, arrayStride * BytesPerComponent, matrixStride * BytesPerComponent, field.isRowMajorMatrix); |
| 56 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 57 | advanceOffset(field.type, field.arraySize, field.isRowMajorMatrix, arrayStride, matrixStride); |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 58 | |
| 59 | return memberInfo; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void BlockLayoutEncoder::encodeType(GLenum type, unsigned int arraySize, bool isRowMajorMatrix) |
| 63 | { |
| 64 | int arrayStride; |
| 65 | int matrixStride; |
| 66 | |
| 67 | getBlockLayoutInfo(type, arraySize, isRowMajorMatrix, &arrayStride, &matrixStride); |
| 68 | |
| 69 | const BlockMemberInfo memberInfo(mCurrentOffset * BytesPerComponent, arrayStride * BytesPerComponent, matrixStride * BytesPerComponent, isRowMajorMatrix); |
| 70 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 71 | advanceOffset(type, arraySize, isRowMajorMatrix, arrayStride, matrixStride); |
| 72 | } |
| 73 | |
| 74 | void BlockLayoutEncoder::nextRegister() |
| 75 | { |
| 76 | mCurrentOffset = rx::roundUp<size_t>(mCurrentOffset, ComponentsPerRegister); |
| 77 | } |
| 78 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 79 | Std140BlockEncoder::Std140BlockEncoder() |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
| 83 | void Std140BlockEncoder::enterAggregateType() |
| 84 | { |
| 85 | nextRegister(); |
| 86 | } |
| 87 | |
| 88 | void Std140BlockEncoder::exitAggregateType() |
| 89 | { |
| 90 | nextRegister(); |
| 91 | } |
| 92 | |
| 93 | void Std140BlockEncoder::getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut) |
| 94 | { |
| 95 | // We assume we are only dealing with 4 byte components (no doubles or half-words currently) |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 96 | ASSERT(gl::VariableComponentSize(gl::VariableComponentType(type)) == BytesPerComponent); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 97 | |
| 98 | size_t baseAlignment = 0; |
| 99 | int matrixStride = 0; |
| 100 | int arrayStride = 0; |
| 101 | |
| 102 | if (gl::IsMatrixType(type)) |
| 103 | { |
| 104 | baseAlignment = ComponentsPerRegister; |
| 105 | matrixStride = ComponentsPerRegister; |
| 106 | |
| 107 | if (arraySize > 0) |
| 108 | { |
| 109 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 110 | arrayStride = ComponentsPerRegister * numRegisters; |
| 111 | } |
| 112 | } |
| 113 | else if (arraySize > 0) |
| 114 | { |
| 115 | baseAlignment = ComponentsPerRegister; |
| 116 | arrayStride = ComponentsPerRegister; |
| 117 | } |
| 118 | else |
| 119 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 120 | const int numComponents = gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 121 | baseAlignment = (numComponents == 3 ? 4u : static_cast<size_t>(numComponents)); |
| 122 | } |
| 123 | |
| 124 | mCurrentOffset = rx::roundUp(mCurrentOffset, baseAlignment); |
| 125 | |
| 126 | *matrixStrideOut = matrixStride; |
| 127 | *arrayStrideOut = arrayStride; |
| 128 | } |
| 129 | |
| 130 | void Std140BlockEncoder::advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride) |
| 131 | { |
| 132 | if (arraySize > 0) |
| 133 | { |
| 134 | mCurrentOffset += arrayStride * arraySize; |
| 135 | } |
| 136 | else if (gl::IsMatrixType(type)) |
| 137 | { |
| 138 | ASSERT(matrixStride == ComponentsPerRegister); |
| 139 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 140 | mCurrentOffset += ComponentsPerRegister * numRegisters; |
| 141 | } |
| 142 | else |
| 143 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 144 | mCurrentOffset += gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 148 | HLSLBlockEncoder::HLSLBlockEncoder(HLSLBlockEncoderStrategy strategy) |
| 149 | : mEncoderStrategy(strategy) |
Jamie Madill | bf9cce2 | 2014-07-18 10:33:09 -0400 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 153 | void HLSLBlockEncoder::enterAggregateType() |
| 154 | { |
| 155 | nextRegister(); |
| 156 | } |
| 157 | |
| 158 | void HLSLBlockEncoder::exitAggregateType() |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | void HLSLBlockEncoder::getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut) |
| 163 | { |
| 164 | // We assume we are only dealing with 4 byte components (no doubles or half-words currently) |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 165 | ASSERT(gl::VariableComponentSize(gl::VariableComponentType(type)) == BytesPerComponent); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 166 | |
| 167 | int matrixStride = 0; |
| 168 | int arrayStride = 0; |
| 169 | |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 170 | // if variables are not to be packed, or we're about to |
| 171 | // pack a matrix or array, skip to the start of the next |
| 172 | // register |
| 173 | if (!isPacked() || |
| 174 | gl::IsMatrixType(type) || |
| 175 | arraySize > 0) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 176 | { |
| 177 | nextRegister(); |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (gl::IsMatrixType(type)) |
| 181 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 182 | matrixStride = ComponentsPerRegister; |
| 183 | |
| 184 | if (arraySize > 0) |
| 185 | { |
| 186 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 187 | arrayStride = ComponentsPerRegister * numRegisters; |
| 188 | } |
| 189 | } |
| 190 | else if (arraySize > 0) |
| 191 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 192 | arrayStride = ComponentsPerRegister; |
| 193 | } |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 194 | else if (isPacked()) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 195 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 196 | int numComponents = gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 197 | if ((numComponents + (mCurrentOffset % ComponentsPerRegister)) > ComponentsPerRegister) |
| 198 | { |
| 199 | nextRegister(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | *matrixStrideOut = matrixStride; |
| 204 | *arrayStrideOut = arrayStride; |
| 205 | } |
| 206 | |
| 207 | void HLSLBlockEncoder::advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride) |
| 208 | { |
| 209 | if (arraySize > 0) |
| 210 | { |
| 211 | mCurrentOffset += arrayStride * (arraySize - 1); |
| 212 | } |
| 213 | |
| 214 | if (gl::IsMatrixType(type)) |
| 215 | { |
| 216 | ASSERT(matrixStride == ComponentsPerRegister); |
| 217 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 218 | const int numComponents = gl::MatrixComponentCount(type, isRowMajorMatrix); |
| 219 | mCurrentOffset += ComponentsPerRegister * (numRegisters - 1); |
| 220 | mCurrentOffset += numComponents; |
| 221 | } |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 222 | else if (isPacked()) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 223 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 224 | mCurrentOffset += gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 225 | } |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 226 | else |
| 227 | { |
| 228 | mCurrentOffset += ComponentsPerRegister; |
| 229 | } |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Jamie Madill | c600c8c | 2014-05-16 11:22:21 -0400 | [diff] [blame] | 232 | void HLSLBlockEncoder::skipRegisters(unsigned int numRegisters) |
| 233 | { |
| 234 | mCurrentOffset += (numRegisters * ComponentsPerRegister); |
| 235 | } |
| 236 | |
Jamie Madill | bf9cce2 | 2014-07-18 10:33:09 -0400 | [diff] [blame] | 237 | HLSLBlockEncoder::HLSLBlockEncoderStrategy HLSLBlockEncoder::GetStrategyFor(ShShaderOutput outputType) |
| 238 | { |
| 239 | switch (outputType) |
| 240 | { |
| 241 | case SH_HLSL9_OUTPUT: return ENCODE_LOOSE; |
| 242 | case SH_HLSL11_OUTPUT: return ENCODE_PACKED; |
| 243 | default: UNREACHABLE(); return ENCODE_PACKED; |
| 244 | } |
| 245 | } |
| 246 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 247 | template <class ShaderVarType> |
| 248 | void HLSLVariableRegisterCount(const ShaderVarType &variable, HLSLBlockEncoder *encoder) |
| 249 | { |
| 250 | if (variable.isStruct()) |
| 251 | { |
| 252 | for (size_t arrayElement = 0; arrayElement < variable.elementCount(); arrayElement++) |
| 253 | { |
| 254 | encoder->enterAggregateType(); |
| 255 | |
| 256 | for (size_t fieldIndex = 0; fieldIndex < variable.fields.size(); fieldIndex++) |
| 257 | { |
| 258 | HLSLVariableRegisterCount(variable.fields[fieldIndex], encoder); |
| 259 | } |
| 260 | |
| 261 | encoder->exitAggregateType(); |
| 262 | } |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | // We operate only on varyings and uniforms, which do not have matrix layout qualifiers |
| 267 | encoder->encodeType(variable.type, variable.arraySize, false); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | unsigned int HLSLVariableRegisterCount(const Varying &variable) |
| 272 | { |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 273 | HLSLBlockEncoder encoder(HLSLBlockEncoder::ENCODE_PACKED); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 274 | HLSLVariableRegisterCount(variable, &encoder); |
| 275 | |
| 276 | const size_t registerBytes = (encoder.BytesPerComponent * encoder.ComponentsPerRegister); |
| 277 | return static_cast<unsigned int>(rx::roundUp<size_t>(encoder.getBlockSize(), registerBytes) / registerBytes); |
| 278 | } |
| 279 | |
Vladimir Vukicevic | 24d8d67 | 2014-05-27 12:07:51 -0400 | [diff] [blame] | 280 | unsigned int HLSLVariableRegisterCount(const Uniform &variable, ShShaderOutput outputType) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 281 | { |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame^] | 282 | HLSLBlockEncoder encoder(HLSLBlockEncoder::GetStrategyFor(outputType)); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 283 | HLSLVariableRegisterCount(variable, &encoder); |
| 284 | |
| 285 | const size_t registerBytes = (encoder.BytesPerComponent * encoder.ComponentsPerRegister); |
| 286 | return static_cast<unsigned int>(rx::roundUp<size_t>(encoder.getBlockSize(), registerBytes) / registerBytes); |
| 287 | } |
| 288 | |
| 289 | } |