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 | // |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 6 | // Check whether variables fit within packing limits according to the packing rules from the GLSL ES |
| 7 | // 1.00.17 spec, Appendix A, section 7. |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 10 | |
Jamie Madill | e294bb8 | 2014-07-17 14:16:26 -0400 | [diff] [blame] | 11 | #include "angle_gl.h" |
| 12 | |
| 13 | #include "compiler/translator/VariablePacker.h" |
| 14 | #include "common/utilities.h" |
| 15 | |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 16 | namespace sh |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 17 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 18 | |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | void ExpandVariable(const ShaderVariable &variable, |
| 23 | const std::string &name, |
| 24 | const std::string &mappedName, |
| 25 | bool markStaticUse, |
| 26 | std::vector<ShaderVariable> *expanded); |
| 27 | |
| 28 | void ExpandUserDefinedVariable(const ShaderVariable &variable, |
| 29 | const std::string &name, |
| 30 | const std::string &mappedName, |
| 31 | bool markStaticUse, |
| 32 | std::vector<ShaderVariable> *expanded) |
| 33 | { |
| 34 | ASSERT(variable.isStruct()); |
| 35 | |
| 36 | const std::vector<ShaderVariable> &fields = variable.fields; |
| 37 | |
| 38 | for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 39 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 40 | const ShaderVariable &field = fields[fieldIndex]; |
| 41 | ExpandVariable(field, name + "." + field.name, mappedName + "." + field.mappedName, |
| 42 | markStaticUse, expanded); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 46 | void ExpandVariable(const ShaderVariable &variable, |
| 47 | const std::string &name, |
| 48 | const std::string &mappedName, |
| 49 | bool markStaticUse, |
| 50 | std::vector<ShaderVariable> *expanded) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 51 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 52 | if (variable.isStruct()) |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 53 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 54 | if (variable.isArray()) |
| 55 | { |
| 56 | for (unsigned int elementIndex = 0; elementIndex < variable.elementCount(); |
| 57 | elementIndex++) |
| 58 | { |
| 59 | std::string lname = name + ::ArrayString(elementIndex); |
| 60 | std::string lmappedName = mappedName + ::ArrayString(elementIndex); |
| 61 | ExpandUserDefinedVariable(variable, lname, lmappedName, markStaticUse, expanded); |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | ExpandUserDefinedVariable(variable, name, mappedName, markStaticUse, expanded); |
| 67 | } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | ShaderVariable expandedVar = variable; |
| 72 | |
| 73 | expandedVar.name = name; |
| 74 | expandedVar.mappedName = mappedName; |
| 75 | |
| 76 | // Mark all expanded fields as used if the parent is used |
| 77 | if (markStaticUse) |
| 78 | { |
| 79 | expandedVar.staticUse = true; |
| 80 | } |
| 81 | |
| 82 | if (expandedVar.isArray()) |
| 83 | { |
| 84 | expandedVar.name += "[0]"; |
| 85 | expandedVar.mappedName += "[0]"; |
| 86 | } |
| 87 | |
| 88 | expanded->push_back(expandedVar); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 92 | class VariablePacker |
| 93 | { |
| 94 | public: |
| 95 | bool checkExpandedVariablesWithinPackingLimits(unsigned int maxVectors, |
| 96 | std::vector<sh::ShaderVariable> *variables); |
| 97 | |
| 98 | private: |
| 99 | static const int kNumColumns = 4; |
| 100 | static const unsigned kColumnMask = (1 << kNumColumns) - 1; |
| 101 | |
| 102 | unsigned makeColumnFlags(int column, int numComponentsPerRow); |
| 103 | void fillColumns(int topRow, int numRows, int column, int numComponentsPerRow); |
| 104 | bool searchColumn(int column, int numRows, int *destRow, int *destSize); |
| 105 | |
| 106 | int topNonFullRow_; |
| 107 | int bottomNonFullRow_; |
| 108 | int maxRows_; |
| 109 | std::vector<unsigned> rows_; |
| 110 | }; |
| 111 | |
Jamie Madill | 865d145 | 2014-07-02 15:31:20 -0400 | [diff] [blame] | 112 | struct TVariableInfoComparer |
| 113 | { |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 114 | bool operator()(const sh::ShaderVariable &lhs, const sh::ShaderVariable &rhs) const |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 115 | { |
Jamie Madill | 865d145 | 2014-07-02 15:31:20 -0400 | [diff] [blame] | 116 | int lhsSortOrder = gl::VariableSortOrder(lhs.type); |
| 117 | int rhsSortOrder = gl::VariableSortOrder(rhs.type); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 118 | if (lhsSortOrder != rhsSortOrder) |
| 119 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 120 | return lhsSortOrder < rhsSortOrder; |
| 121 | } |
| 122 | // Sort by largest first. |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 123 | return lhs.arraySize > rhs.arraySize; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 124 | } |
| 125 | }; |
| 126 | |
| 127 | unsigned VariablePacker::makeColumnFlags(int column, int numComponentsPerRow) |
| 128 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 129 | return ((kColumnMask << (kNumColumns - numComponentsPerRow)) & kColumnMask) >> column; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void VariablePacker::fillColumns(int topRow, int numRows, int column, int numComponentsPerRow) |
| 133 | { |
| 134 | unsigned columnFlags = makeColumnFlags(column, numComponentsPerRow); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 135 | for (int r = 0; r < numRows; ++r) |
| 136 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 137 | int row = topRow + r; |
| 138 | ASSERT((rows_[row] & columnFlags) == 0); |
| 139 | rows_[row] |= columnFlags; |
| 140 | } |
| 141 | } |
| 142 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 143 | bool VariablePacker::searchColumn(int column, int numRows, int *destRow, int *destSize) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 144 | { |
| 145 | ASSERT(destRow); |
| 146 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 147 | for (; topNonFullRow_ < maxRows_ && rows_[topNonFullRow_] == kColumnMask; ++topNonFullRow_) |
| 148 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 151 | for (; bottomNonFullRow_ >= 0 && rows_[bottomNonFullRow_] == kColumnMask; --bottomNonFullRow_) |
| 152 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 155 | if (bottomNonFullRow_ - topNonFullRow_ + 1 < numRows) |
| 156 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | unsigned columnFlags = makeColumnFlags(column, 1); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 161 | int topGoodRow = 0; |
| 162 | int smallestGoodTop = -1; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 163 | int smallestGoodSize = maxRows_ + 1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 164 | int bottomRow = bottomNonFullRow_ + 1; |
| 165 | bool found = false; |
| 166 | for (int row = topNonFullRow_; row <= bottomRow; ++row) |
| 167 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 168 | bool rowEmpty = row < bottomRow ? ((rows_[row] & columnFlags) == 0) : false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 169 | if (rowEmpty) |
| 170 | { |
| 171 | if (!found) |
| 172 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 173 | topGoodRow = row; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 174 | found = true; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 175 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
| 179 | if (found) |
| 180 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 181 | int size = row - topGoodRow; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 182 | if (size >= numRows && size < smallestGoodSize) |
| 183 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 184 | smallestGoodSize = size; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 185 | smallestGoodTop = topGoodRow; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | found = false; |
| 189 | } |
| 190 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 191 | if (smallestGoodTop < 0) |
| 192 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
| 196 | *destRow = smallestGoodTop; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 197 | if (destSize) |
| 198 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 199 | *destSize = smallestGoodSize; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 204 | bool VariablePacker::checkExpandedVariablesWithinPackingLimits( |
Corentin Wallez | e58e141 | 2016-07-18 16:40:46 -0400 | [diff] [blame] | 205 | unsigned int maxVectors, |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 206 | std::vector<sh::ShaderVariable> *variables) |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 207 | { |
| 208 | ASSERT(maxVectors > 0); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 209 | maxRows_ = maxVectors; |
| 210 | topNonFullRow_ = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 211 | bottomNonFullRow_ = maxRows_ - 1; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 212 | |
Nicolas Capens | 1d1c115 | 2014-02-26 11:00:28 -0500 | [diff] [blame] | 213 | // Check whether each variable fits in the available vectors. |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 214 | for (const sh::ShaderVariable &variable : *variables) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 215 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 216 | // Structs should have been expanded before reaching here. |
| 217 | ASSERT(!variable.isStruct()); |
| 218 | if (variable.elementCount() > maxVectors / GetVariablePackingRows(variable.type)) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 219 | { |
Nicolas Capens | 1d1c115 | 2014-02-26 11:00:28 -0500 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 224 | // As per GLSL 1.017 Appendix A, Section 7 variables are packed in specific |
| 225 | // order by type, then by size of array, largest first. |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 226 | std::sort(variables->begin(), variables->end(), TVariableInfoComparer()); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 227 | rows_.clear(); |
| 228 | rows_.resize(maxVectors, 0); |
| 229 | |
| 230 | // Packs the 4 column variables. |
| 231 | size_t ii = 0; |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 232 | for (; ii < variables->size(); ++ii) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 233 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 234 | const sh::ShaderVariable &variable = (*variables)[ii]; |
| 235 | if (GetVariablePackingComponentsPerRow(variable.type) != 4) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 236 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 239 | topNonFullRow_ += GetVariablePackingRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 242 | if (topNonFullRow_ > maxRows_) |
| 243 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
| 247 | // Packs the 3 column variables. |
| 248 | int num3ColumnRows = 0; |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 249 | for (; ii < variables->size(); ++ii) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 250 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 251 | const sh::ShaderVariable &variable = (*variables)[ii]; |
| 252 | if (GetVariablePackingComponentsPerRow(variable.type) != 3) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 253 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 256 | num3ColumnRows += GetVariablePackingRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 259 | if (topNonFullRow_ + num3ColumnRows > maxRows_) |
| 260 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | fillColumns(topNonFullRow_, num3ColumnRows, 0, 3); |
| 265 | |
| 266 | // Packs the 2 column variables. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 267 | int top2ColumnRow = topNonFullRow_ + num3ColumnRows; |
| 268 | int twoColumnRowsAvailable = maxRows_ - top2ColumnRow; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 269 | int rowsAvailableInColumns01 = twoColumnRowsAvailable; |
| 270 | int rowsAvailableInColumns23 = twoColumnRowsAvailable; |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 271 | for (; ii < variables->size(); ++ii) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 272 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 273 | const sh::ShaderVariable &variable = (*variables)[ii]; |
| 274 | if (GetVariablePackingComponentsPerRow(variable.type) != 2) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 275 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 276 | break; |
| 277 | } |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 278 | int numRows = GetVariablePackingRows(variable.type) * variable.elementCount(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 279 | if (numRows <= rowsAvailableInColumns01) |
| 280 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 281 | rowsAvailableInColumns01 -= numRows; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 282 | } |
| 283 | else if (numRows <= rowsAvailableInColumns23) |
| 284 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 285 | rowsAvailableInColumns23 -= numRows; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 293 | int numRowsUsedInColumns01 = twoColumnRowsAvailable - rowsAvailableInColumns01; |
| 294 | int numRowsUsedInColumns23 = twoColumnRowsAvailable - rowsAvailableInColumns23; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 295 | fillColumns(top2ColumnRow, numRowsUsedInColumns01, 0, 2); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 296 | fillColumns(maxRows_ - numRowsUsedInColumns23, numRowsUsedInColumns23, 2, 2); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 297 | |
| 298 | // Packs the 1 column variables. |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 299 | for (; ii < variables->size(); ++ii) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 300 | { |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 301 | const sh::ShaderVariable &variable = (*variables)[ii]; |
| 302 | ASSERT(1 == GetVariablePackingComponentsPerRow(variable.type)); |
| 303 | int numRows = GetVariablePackingRows(variable.type) * variable.elementCount(); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 304 | int smallestColumn = -1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 305 | int smallestSize = maxRows_ + 1; |
| 306 | int topRow = -1; |
| 307 | for (int column = 0; column < kNumColumns; ++column) |
| 308 | { |
| 309 | int row = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 310 | int size = 0; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 311 | if (searchColumn(column, numRows, &row, &size)) |
| 312 | { |
| 313 | if (size < smallestSize) |
| 314 | { |
| 315 | smallestSize = size; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 316 | smallestColumn = column; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 317 | topRow = row; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 322 | if (smallestColumn < 0) |
| 323 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
| 327 | fillColumns(topRow, numRows, smallestColumn, 1); |
| 328 | } |
| 329 | |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 330 | ASSERT(variables->size() == ii); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 331 | |
| 332 | return true; |
| 333 | } |
Olli Etuaho | d7487b1 | 2017-08-09 15:45:13 +0300 | [diff] [blame^] | 334 | |
| 335 | } // anonymous namespace |
| 336 | |
| 337 | int GetVariablePackingComponentsPerRow(sh::GLenum type) |
| 338 | { |
| 339 | switch (type) |
| 340 | { |
| 341 | case GL_FLOAT_MAT4: |
| 342 | case GL_FLOAT_MAT2: |
| 343 | case GL_FLOAT_MAT2x4: |
| 344 | case GL_FLOAT_MAT3x4: |
| 345 | case GL_FLOAT_MAT4x2: |
| 346 | case GL_FLOAT_MAT4x3: |
| 347 | case GL_FLOAT_VEC4: |
| 348 | case GL_INT_VEC4: |
| 349 | case GL_BOOL_VEC4: |
| 350 | case GL_UNSIGNED_INT_VEC4: |
| 351 | return 4; |
| 352 | case GL_FLOAT_MAT3: |
| 353 | case GL_FLOAT_MAT2x3: |
| 354 | case GL_FLOAT_MAT3x2: |
| 355 | case GL_FLOAT_VEC3: |
| 356 | case GL_INT_VEC3: |
| 357 | case GL_BOOL_VEC3: |
| 358 | case GL_UNSIGNED_INT_VEC3: |
| 359 | return 3; |
| 360 | case GL_FLOAT_VEC2: |
| 361 | case GL_INT_VEC2: |
| 362 | case GL_BOOL_VEC2: |
| 363 | case GL_UNSIGNED_INT_VEC2: |
| 364 | return 2; |
| 365 | default: |
| 366 | ASSERT(gl::VariableComponentCount(type) == 1); |
| 367 | return 1; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | int GetVariablePackingRows(sh::GLenum type) |
| 372 | { |
| 373 | switch (type) |
| 374 | { |
| 375 | case GL_FLOAT_MAT4: |
| 376 | case GL_FLOAT_MAT2x4: |
| 377 | case GL_FLOAT_MAT3x4: |
| 378 | case GL_FLOAT_MAT4x3: |
| 379 | case GL_FLOAT_MAT4x2: |
| 380 | return 4; |
| 381 | case GL_FLOAT_MAT3: |
| 382 | case GL_FLOAT_MAT2x3: |
| 383 | case GL_FLOAT_MAT3x2: |
| 384 | return 3; |
| 385 | case GL_FLOAT_MAT2: |
| 386 | return 2; |
| 387 | default: |
| 388 | ASSERT(gl::VariableRowCount(type) == 1); |
| 389 | return 1; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | template <typename T> |
| 394 | bool CheckVariablesInPackingLimits(unsigned int maxVectors, const std::vector<T> &variables) |
| 395 | { |
| 396 | VariablePacker packer; |
| 397 | std::vector<sh::ShaderVariable> expandedVariables; |
| 398 | for (const ShaderVariable &variable : variables) |
| 399 | { |
| 400 | ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, |
| 401 | &expandedVariables); |
| 402 | } |
| 403 | return packer.checkExpandedVariablesWithinPackingLimits(maxVectors, &expandedVariables); |
| 404 | } |
| 405 | |
| 406 | template bool CheckVariablesInPackingLimits<ShaderVariable>( |
| 407 | unsigned int maxVectors, |
| 408 | const std::vector<ShaderVariable> &variables); |
| 409 | template bool CheckVariablesInPackingLimits<Uniform>(unsigned int maxVectors, |
| 410 | const std::vector<Uniform> &variables); |
| 411 | |
| 412 | } // namespace sh |