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 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 10 | #include "compiler/translator/blocklayout.h" |
| 11 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 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() |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 19 | : mCurrentOffset(0) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 23 | BlockMemberInfo BlockLayoutEncoder::encodeType(GLenum type, unsigned int arraySize, bool isRowMajorMatrix) |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 24 | { |
| 25 | int arrayStride; |
| 26 | int matrixStride; |
| 27 | |
| 28 | getBlockLayoutInfo(type, arraySize, isRowMajorMatrix, &arrayStride, &matrixStride); |
| 29 | |
| 30 | const BlockMemberInfo memberInfo(mCurrentOffset * BytesPerComponent, arrayStride * BytesPerComponent, matrixStride * BytesPerComponent, isRowMajorMatrix); |
| 31 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 32 | advanceOffset(type, arraySize, isRowMajorMatrix, arrayStride, matrixStride); |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 33 | |
| 34 | return memberInfo; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Jamie Madill | 2857f48 | 2015-02-09 15:35:29 -0500 | [diff] [blame] | 37 | // static |
| 38 | size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info) |
| 39 | { |
| 40 | return (info.offset / BytesPerComponent) / ComponentsPerRegister; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info) |
| 45 | { |
| 46 | return (info.offset / BytesPerComponent) % ComponentsPerRegister; |
| 47 | } |
| 48 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 49 | void BlockLayoutEncoder::nextRegister() |
| 50 | { |
| 51 | mCurrentOffset = rx::roundUp<size_t>(mCurrentOffset, ComponentsPerRegister); |
| 52 | } |
| 53 | |
Jamie Madill | e04a5b7 | 2014-07-18 10:33:12 -0400 | [diff] [blame] | 54 | Std140BlockEncoder::Std140BlockEncoder() |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
| 58 | void Std140BlockEncoder::enterAggregateType() |
| 59 | { |
| 60 | nextRegister(); |
| 61 | } |
| 62 | |
| 63 | void Std140BlockEncoder::exitAggregateType() |
| 64 | { |
| 65 | nextRegister(); |
| 66 | } |
| 67 | |
| 68 | void Std140BlockEncoder::getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut) |
| 69 | { |
| 70 | // 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] | 71 | ASSERT(gl::VariableComponentSize(gl::VariableComponentType(type)) == BytesPerComponent); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 72 | |
| 73 | size_t baseAlignment = 0; |
| 74 | int matrixStride = 0; |
| 75 | int arrayStride = 0; |
| 76 | |
| 77 | if (gl::IsMatrixType(type)) |
| 78 | { |
| 79 | baseAlignment = ComponentsPerRegister; |
| 80 | matrixStride = ComponentsPerRegister; |
| 81 | |
| 82 | if (arraySize > 0) |
| 83 | { |
| 84 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 85 | arrayStride = ComponentsPerRegister * numRegisters; |
| 86 | } |
| 87 | } |
| 88 | else if (arraySize > 0) |
| 89 | { |
| 90 | baseAlignment = ComponentsPerRegister; |
| 91 | arrayStride = ComponentsPerRegister; |
| 92 | } |
| 93 | else |
| 94 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 95 | const int numComponents = gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 96 | baseAlignment = (numComponents == 3 ? 4u : static_cast<size_t>(numComponents)); |
| 97 | } |
| 98 | |
| 99 | mCurrentOffset = rx::roundUp(mCurrentOffset, baseAlignment); |
| 100 | |
| 101 | *matrixStrideOut = matrixStride; |
| 102 | *arrayStrideOut = arrayStride; |
| 103 | } |
| 104 | |
| 105 | void Std140BlockEncoder::advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride) |
| 106 | { |
| 107 | if (arraySize > 0) |
| 108 | { |
| 109 | mCurrentOffset += arrayStride * arraySize; |
| 110 | } |
| 111 | else if (gl::IsMatrixType(type)) |
| 112 | { |
| 113 | ASSERT(matrixStride == ComponentsPerRegister); |
| 114 | const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix); |
| 115 | mCurrentOffset += ComponentsPerRegister * numRegisters; |
| 116 | } |
| 117 | else |
| 118 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 119 | mCurrentOffset += gl::VariableComponentCount(type); |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 123 | } |