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