gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2012 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 | // |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 8 | |
Jamie Madill | e294bb8 | 2014-07-17 14:16:26 -0400 | [diff] [blame] | 9 | #include "angle_gl.h" |
| 10 | |
| 11 | #include "compiler/translator/VariablePacker.h" |
| 12 | #include "common/utilities.h" |
| 13 | |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 14 | int VariablePacker::GetNumComponentsPerRow(sh::GLenum type) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 15 | { |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 16 | switch (type) |
| 17 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 18 | case GL_FLOAT_MAT4: |
| 19 | case GL_FLOAT_MAT2: |
| 20 | case GL_FLOAT_MAT2x4: |
| 21 | case GL_FLOAT_MAT3x4: |
| 22 | case GL_FLOAT_MAT4x2: |
| 23 | case GL_FLOAT_MAT4x3: |
| 24 | case GL_FLOAT_VEC4: |
| 25 | case GL_INT_VEC4: |
| 26 | case GL_BOOL_VEC4: |
| 27 | case GL_UNSIGNED_INT_VEC4: |
| 28 | return 4; |
| 29 | case GL_FLOAT_MAT3: |
| 30 | case GL_FLOAT_MAT2x3: |
| 31 | case GL_FLOAT_MAT3x2: |
| 32 | case GL_FLOAT_VEC3: |
| 33 | case GL_INT_VEC3: |
| 34 | case GL_BOOL_VEC3: |
| 35 | case GL_UNSIGNED_INT_VEC3: |
| 36 | return 3; |
| 37 | case GL_FLOAT_VEC2: |
| 38 | case GL_INT_VEC2: |
| 39 | case GL_BOOL_VEC2: |
| 40 | case GL_UNSIGNED_INT_VEC2: |
| 41 | return 2; |
| 42 | default: |
| 43 | ASSERT(gl::VariableComponentCount(type) == 1); |
| 44 | return 1; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 48 | int VariablePacker::GetNumRows(sh::GLenum type) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 49 | { |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 50 | switch (type) |
| 51 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 52 | case GL_FLOAT_MAT4: |
| 53 | case GL_FLOAT_MAT2x4: |
| 54 | case GL_FLOAT_MAT3x4: |
| 55 | case GL_FLOAT_MAT4x3: |
| 56 | case GL_FLOAT_MAT4x2: |
| 57 | return 4; |
| 58 | case GL_FLOAT_MAT3: |
| 59 | case GL_FLOAT_MAT2x3: |
| 60 | case GL_FLOAT_MAT3x2: |
| 61 | return 3; |
| 62 | case GL_FLOAT_MAT2: |
| 63 | return 2; |
| 64 | default: |
| 65 | ASSERT(gl::VariableRowCount(type) == 1); |
| 66 | return 1; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Jamie Madill | 865d145 | 2014-07-02 15:31:20 -0400 | [diff] [blame] | 70 | struct TVariableInfoComparer |
| 71 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 72 | bool operator()(const sh::ShaderVariable &lhs, const sh::ShaderVariable &rhs) const |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 73 | { |
Jamie Madill | 865d145 | 2014-07-02 15:31:20 -0400 | [diff] [blame] | 74 | int lhsSortOrder = gl::VariableSortOrder(lhs.type); |
| 75 | int rhsSortOrder = gl::VariableSortOrder(rhs.type); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 76 | if (lhsSortOrder != rhsSortOrder) |
| 77 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 78 | return lhsSortOrder < rhsSortOrder; |
| 79 | } |
| 80 | // Sort by largest first. |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 81 | return lhs.arraySize > rhs.arraySize; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | unsigned VariablePacker::makeColumnFlags(int column, int numComponentsPerRow) |
| 86 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 87 | return ((kColumnMask << (kNumColumns - numComponentsPerRow)) & kColumnMask) >> column; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void VariablePacker::fillColumns(int topRow, int numRows, int column, int numComponentsPerRow) |
| 91 | { |
| 92 | unsigned columnFlags = makeColumnFlags(column, numComponentsPerRow); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 93 | for (int r = 0; r < numRows; ++r) |
| 94 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 95 | int row = topRow + r; |
| 96 | ASSERT((rows_[row] & columnFlags) == 0); |
| 97 | rows_[row] |= columnFlags; |
| 98 | } |
| 99 | } |
| 100 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 101 | bool VariablePacker::searchColumn(int column, int numRows, int *destRow, int *destSize) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 102 | { |
| 103 | ASSERT(destRow); |
| 104 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 105 | for (; topNonFullRow_ < maxRows_ && rows_[topNonFullRow_] == kColumnMask; ++topNonFullRow_) |
| 106 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 109 | for (; bottomNonFullRow_ >= 0 && rows_[bottomNonFullRow_] == kColumnMask; --bottomNonFullRow_) |
| 110 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 113 | if (bottomNonFullRow_ - topNonFullRow_ + 1 < numRows) |
| 114 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
| 118 | unsigned columnFlags = makeColumnFlags(column, 1); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 119 | int topGoodRow = 0; |
| 120 | int smallestGoodTop = -1; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 121 | int smallestGoodSize = maxRows_ + 1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 122 | int bottomRow = bottomNonFullRow_ + 1; |
| 123 | bool found = false; |
| 124 | for (int row = topNonFullRow_; row <= bottomRow; ++row) |
| 125 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 126 | bool rowEmpty = row < bottomRow ? ((rows_[row] & columnFlags) == 0) : false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 127 | if (rowEmpty) |
| 128 | { |
| 129 | if (!found) |
| 130 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 131 | topGoodRow = row; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 132 | found = true; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 133 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 134 | } |
| 135 | else |
| 136 | { |
| 137 | if (found) |
| 138 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 139 | int size = row - topGoodRow; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 140 | if (size >= numRows && size < smallestGoodSize) |
| 141 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 142 | smallestGoodSize = size; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 143 | smallestGoodTop = topGoodRow; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | found = false; |
| 147 | } |
| 148 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 149 | if (smallestGoodTop < 0) |
| 150 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | |
| 154 | *destRow = smallestGoodTop; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 155 | if (destSize) |
| 156 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 157 | *destSize = smallestGoodSize; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
Corentin Wallez | e58e141 | 2016-07-18 16:40:46 -0400 | [diff] [blame] | 162 | bool VariablePacker::CheckVariablesWithinPackingLimits( |
| 163 | unsigned int maxVectors, |
| 164 | const std::vector<sh::ShaderVariable> &in_variables) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 165 | { |
| 166 | ASSERT(maxVectors > 0); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 167 | maxRows_ = maxVectors; |
| 168 | topNonFullRow_ = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 169 | bottomNonFullRow_ = maxRows_ - 1; |
Corentin Wallez | e58e141 | 2016-07-18 16:40:46 -0400 | [diff] [blame] | 170 | std::vector<sh::ShaderVariable> variables; |
| 171 | |
| 172 | for (const auto &variable : in_variables) |
| 173 | { |
| 174 | ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, |
| 175 | &variables); |
| 176 | } |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 177 | |
Nicolas Capens | 1d1c115 | 2014-02-26 11:00:28 -0500 | [diff] [blame] | 178 | // Check whether each variable fits in the available vectors. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 179 | for (size_t i = 0; i < variables.size(); i++) |
| 180 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 181 | const sh::ShaderVariable &variable = variables[i]; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 182 | if (variable.elementCount() > maxVectors / GetNumRows(variable.type)) |
| 183 | { |
Nicolas Capens | 1d1c115 | 2014-02-26 11:00:28 -0500 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 188 | // As per GLSL 1.017 Appendix A, Section 7 variables are packed in specific |
| 189 | // order by type, then by size of array, largest first. |
| 190 | std::sort(variables.begin(), variables.end(), TVariableInfoComparer()); |
| 191 | rows_.clear(); |
| 192 | rows_.resize(maxVectors, 0); |
| 193 | |
| 194 | // Packs the 4 column variables. |
| 195 | size_t ii = 0; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 196 | for (; ii < variables.size(); ++ii) |
| 197 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 198 | const sh::ShaderVariable &variable = variables[ii]; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 199 | if (GetNumComponentsPerRow(variable.type) != 4) |
| 200 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 203 | topNonFullRow_ += GetNumRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 206 | if (topNonFullRow_ > maxRows_) |
| 207 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // Packs the 3 column variables. |
| 212 | int num3ColumnRows = 0; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 213 | for (; ii < variables.size(); ++ii) |
| 214 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 215 | const sh::ShaderVariable &variable = variables[ii]; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 216 | if (GetNumComponentsPerRow(variable.type) != 3) |
| 217 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 218 | break; |
| 219 | } |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 220 | num3ColumnRows += GetNumRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 223 | if (topNonFullRow_ + num3ColumnRows > maxRows_) |
| 224 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
| 228 | fillColumns(topNonFullRow_, num3ColumnRows, 0, 3); |
| 229 | |
| 230 | // Packs the 2 column variables. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 231 | int top2ColumnRow = topNonFullRow_ + num3ColumnRows; |
| 232 | int twoColumnRowsAvailable = maxRows_ - top2ColumnRow; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 233 | int rowsAvailableInColumns01 = twoColumnRowsAvailable; |
| 234 | int rowsAvailableInColumns23 = twoColumnRowsAvailable; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 235 | for (; ii < variables.size(); ++ii) |
| 236 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 237 | const sh::ShaderVariable &variable = variables[ii]; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 238 | if (GetNumComponentsPerRow(variable.type) != 2) |
| 239 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 240 | break; |
| 241 | } |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 242 | int numRows = GetNumRows(variable.type) * variable.elementCount(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 243 | if (numRows <= rowsAvailableInColumns01) |
| 244 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 245 | rowsAvailableInColumns01 -= numRows; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 246 | } |
| 247 | else if (numRows <= rowsAvailableInColumns23) |
| 248 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 249 | rowsAvailableInColumns23 -= numRows; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 250 | } |
| 251 | else |
| 252 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 257 | int numRowsUsedInColumns01 = twoColumnRowsAvailable - rowsAvailableInColumns01; |
| 258 | int numRowsUsedInColumns23 = twoColumnRowsAvailable - rowsAvailableInColumns23; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 259 | fillColumns(top2ColumnRow, numRowsUsedInColumns01, 0, 2); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 260 | fillColumns(maxRows_ - numRowsUsedInColumns23, numRowsUsedInColumns23, 2, 2); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 261 | |
| 262 | // Packs the 1 column variables. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 263 | for (; ii < variables.size(); ++ii) |
| 264 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 265 | const sh::ShaderVariable &variable = variables[ii]; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 266 | ASSERT(1 == GetNumComponentsPerRow(variable.type)); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 267 | int numRows = GetNumRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 268 | int smallestColumn = -1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 269 | int smallestSize = maxRows_ + 1; |
| 270 | int topRow = -1; |
| 271 | for (int column = 0; column < kNumColumns; ++column) |
| 272 | { |
| 273 | int row = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 274 | int size = 0; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 275 | if (searchColumn(column, numRows, &row, &size)) |
| 276 | { |
| 277 | if (size < smallestSize) |
| 278 | { |
| 279 | smallestSize = size; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 280 | smallestColumn = column; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 281 | topRow = row; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame^] | 286 | if (smallestColumn < 0) |
| 287 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | fillColumns(topRow, numRows, smallestColumn, 1); |
| 292 | } |
| 293 | |
| 294 | ASSERT(variables.size() == ii); |
| 295 | |
| 296 | return true; |
| 297 | } |