gfx/angle/src/compiler/VariablePacker.cpp

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 //
michael@0 2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
michael@0 3 // Use of this source code is governed by a BSD-style license that can be
michael@0 4 // found in the LICENSE file.
michael@0 5 //
michael@0 6 #include "compiler/VariablePacker.h"
michael@0 7
michael@0 8 #include <algorithm>
michael@0 9 #include "compiler/ShHandle.h"
michael@0 10
michael@0 11 namespace {
michael@0 12 int GetSortOrder(ShDataType type)
michael@0 13 {
michael@0 14 switch (type) {
michael@0 15 case SH_FLOAT_MAT4:
michael@0 16 return 0;
michael@0 17 case SH_FLOAT_MAT2:
michael@0 18 return 1;
michael@0 19 case SH_FLOAT_VEC4:
michael@0 20 case SH_INT_VEC4:
michael@0 21 case SH_BOOL_VEC4:
michael@0 22 return 2;
michael@0 23 case SH_FLOAT_MAT3:
michael@0 24 return 3;
michael@0 25 case SH_FLOAT_VEC3:
michael@0 26 case SH_INT_VEC3:
michael@0 27 case SH_BOOL_VEC3:
michael@0 28 return 4;
michael@0 29 case SH_FLOAT_VEC2:
michael@0 30 case SH_INT_VEC2:
michael@0 31 case SH_BOOL_VEC2:
michael@0 32 return 5;
michael@0 33 case SH_FLOAT:
michael@0 34 case SH_INT:
michael@0 35 case SH_BOOL:
michael@0 36 case SH_SAMPLER_2D:
michael@0 37 case SH_SAMPLER_CUBE:
michael@0 38 case SH_SAMPLER_EXTERNAL_OES:
michael@0 39 case SH_SAMPLER_2D_RECT_ARB:
michael@0 40 return 6;
michael@0 41 default:
michael@0 42 ASSERT(false);
michael@0 43 return 7;
michael@0 44 }
michael@0 45 }
michael@0 46 } // namespace
michael@0 47
michael@0 48 int VariablePacker::GetNumComponentsPerRow(ShDataType type)
michael@0 49 {
michael@0 50 switch (type) {
michael@0 51 case SH_FLOAT_MAT4:
michael@0 52 case SH_FLOAT_MAT2:
michael@0 53 case SH_FLOAT_VEC4:
michael@0 54 case SH_INT_VEC4:
michael@0 55 case SH_BOOL_VEC4:
michael@0 56 return 4;
michael@0 57 case SH_FLOAT_MAT3:
michael@0 58 case SH_FLOAT_VEC3:
michael@0 59 case SH_INT_VEC3:
michael@0 60 case SH_BOOL_VEC3:
michael@0 61 return 3;
michael@0 62 case SH_FLOAT_VEC2:
michael@0 63 case SH_INT_VEC2:
michael@0 64 case SH_BOOL_VEC2:
michael@0 65 return 2;
michael@0 66 case SH_FLOAT:
michael@0 67 case SH_INT:
michael@0 68 case SH_BOOL:
michael@0 69 case SH_SAMPLER_2D:
michael@0 70 case SH_SAMPLER_CUBE:
michael@0 71 case SH_SAMPLER_EXTERNAL_OES:
michael@0 72 case SH_SAMPLER_2D_RECT_ARB:
michael@0 73 return 1;
michael@0 74 default:
michael@0 75 ASSERT(false);
michael@0 76 return 5;
michael@0 77 }
michael@0 78 }
michael@0 79
michael@0 80 int VariablePacker::GetNumRows(ShDataType type)
michael@0 81 {
michael@0 82 switch (type) {
michael@0 83 case SH_FLOAT_MAT4:
michael@0 84 return 4;
michael@0 85 case SH_FLOAT_MAT3:
michael@0 86 return 3;
michael@0 87 case SH_FLOAT_MAT2:
michael@0 88 return 2;
michael@0 89 case SH_FLOAT_VEC4:
michael@0 90 case SH_INT_VEC4:
michael@0 91 case SH_BOOL_VEC4:
michael@0 92 case SH_FLOAT_VEC3:
michael@0 93 case SH_INT_VEC3:
michael@0 94 case SH_BOOL_VEC3:
michael@0 95 case SH_FLOAT_VEC2:
michael@0 96 case SH_INT_VEC2:
michael@0 97 case SH_BOOL_VEC2:
michael@0 98 case SH_FLOAT:
michael@0 99 case SH_INT:
michael@0 100 case SH_BOOL:
michael@0 101 case SH_SAMPLER_2D:
michael@0 102 case SH_SAMPLER_CUBE:
michael@0 103 case SH_SAMPLER_EXTERNAL_OES:
michael@0 104 case SH_SAMPLER_2D_RECT_ARB:
michael@0 105 return 1;
michael@0 106 default:
michael@0 107 ASSERT(false);
michael@0 108 return 100000;
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 struct TVariableInfoComparer {
michael@0 113 bool operator()(const TVariableInfo& lhs, const TVariableInfo& rhs) const
michael@0 114 {
michael@0 115 int lhsSortOrder = GetSortOrder(lhs.type);
michael@0 116 int rhsSortOrder = GetSortOrder(rhs.type);
michael@0 117 if (lhsSortOrder != rhsSortOrder) {
michael@0 118 return lhsSortOrder < rhsSortOrder;
michael@0 119 }
michael@0 120 // Sort by largest first.
michael@0 121 return lhs.size > rhs.size;
michael@0 122 }
michael@0 123 };
michael@0 124
michael@0 125 unsigned VariablePacker::makeColumnFlags(int column, int numComponentsPerRow)
michael@0 126 {
michael@0 127 return ((kColumnMask << (kNumColumns - numComponentsPerRow)) &
michael@0 128 kColumnMask) >> column;
michael@0 129 }
michael@0 130
michael@0 131 void VariablePacker::fillColumns(int topRow, int numRows, int column, int numComponentsPerRow)
michael@0 132 {
michael@0 133 unsigned columnFlags = makeColumnFlags(column, numComponentsPerRow);
michael@0 134 for (int r = 0; r < numRows; ++r) {
michael@0 135 int row = topRow + r;
michael@0 136 ASSERT((rows_[row] & columnFlags) == 0);
michael@0 137 rows_[row] |= columnFlags;
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 bool VariablePacker::searchColumn(int column, int numRows, int* destRow, int* destSize)
michael@0 142 {
michael@0 143 ASSERT(destRow);
michael@0 144
michael@0 145 for (; topNonFullRow_ < maxRows_ && rows_[topNonFullRow_] == kColumnMask;
michael@0 146 ++topNonFullRow_) {
michael@0 147 }
michael@0 148
michael@0 149 for (; bottomNonFullRow_ >= 0 && rows_[bottomNonFullRow_] == kColumnMask;
michael@0 150 --bottomNonFullRow_) {
michael@0 151 }
michael@0 152
michael@0 153 if (bottomNonFullRow_ - topNonFullRow_ + 1 < numRows) {
michael@0 154 return false;
michael@0 155 }
michael@0 156
michael@0 157 unsigned columnFlags = makeColumnFlags(column, 1);
michael@0 158 int topGoodRow = 0;
michael@0 159 int smallestGoodTop = -1;
michael@0 160 int smallestGoodSize = maxRows_ + 1;
michael@0 161 int bottomRow = bottomNonFullRow_ + 1;
michael@0 162 bool found = false;
michael@0 163 for (int row = topNonFullRow_; row <= bottomRow; ++row) {
michael@0 164 bool rowEmpty = row < bottomRow ? ((rows_[row] & columnFlags) == 0) : false;
michael@0 165 if (rowEmpty) {
michael@0 166 if (!found) {
michael@0 167 topGoodRow = row;
michael@0 168 found = true;
michael@0 169 }
michael@0 170 } else {
michael@0 171 if (found) {
michael@0 172 int size = row - topGoodRow;
michael@0 173 if (size >= numRows && size < smallestGoodSize) {
michael@0 174 smallestGoodSize = size;
michael@0 175 smallestGoodTop = topGoodRow;
michael@0 176 }
michael@0 177 }
michael@0 178 found = false;
michael@0 179 }
michael@0 180 }
michael@0 181 if (smallestGoodTop < 0) {
michael@0 182 return false;
michael@0 183 }
michael@0 184
michael@0 185 *destRow = smallestGoodTop;
michael@0 186 if (destSize) {
michael@0 187 *destSize = smallestGoodSize;
michael@0 188 }
michael@0 189 return true;
michael@0 190 }
michael@0 191
michael@0 192 bool VariablePacker::CheckVariablesWithinPackingLimits(int maxVectors, const TVariableInfoList& in_variables)
michael@0 193 {
michael@0 194 ASSERT(maxVectors > 0);
michael@0 195 maxRows_ = maxVectors;
michael@0 196 topNonFullRow_ = 0;
michael@0 197 bottomNonFullRow_ = maxRows_ - 1;
michael@0 198 TVariableInfoList variables(in_variables);
michael@0 199
michael@0 200 // As per GLSL 1.017 Appendix A, Section 7 variables are packed in specific
michael@0 201 // order by type, then by size of array, largest first.
michael@0 202 std::sort(variables.begin(), variables.end(), TVariableInfoComparer());
michael@0 203 rows_.clear();
michael@0 204 rows_.resize(maxVectors, 0);
michael@0 205
michael@0 206 // Packs the 4 column variables.
michael@0 207 size_t ii = 0;
michael@0 208 for (; ii < variables.size(); ++ii) {
michael@0 209 const TVariableInfo& variable = variables[ii];
michael@0 210 if (GetNumComponentsPerRow(variable.type) != 4) {
michael@0 211 break;
michael@0 212 }
michael@0 213 topNonFullRow_ += GetNumRows(variable.type) * variable.size;
michael@0 214 }
michael@0 215
michael@0 216 if (topNonFullRow_ > maxRows_) {
michael@0 217 return false;
michael@0 218 }
michael@0 219
michael@0 220 // Packs the 3 column variables.
michael@0 221 int num3ColumnRows = 0;
michael@0 222 for (; ii < variables.size(); ++ii) {
michael@0 223 const TVariableInfo& variable = variables[ii];
michael@0 224 if (GetNumComponentsPerRow(variable.type) != 3) {
michael@0 225 break;
michael@0 226 }
michael@0 227 num3ColumnRows += GetNumRows(variable.type) * variable.size;
michael@0 228 }
michael@0 229
michael@0 230 if (topNonFullRow_ + num3ColumnRows > maxRows_) {
michael@0 231 return false;
michael@0 232 }
michael@0 233
michael@0 234 fillColumns(topNonFullRow_, num3ColumnRows, 0, 3);
michael@0 235
michael@0 236 // Packs the 2 column variables.
michael@0 237 int top2ColumnRow = topNonFullRow_ + num3ColumnRows;
michael@0 238 int twoColumnRowsAvailable = maxRows_ - top2ColumnRow;
michael@0 239 int rowsAvailableInColumns01 = twoColumnRowsAvailable;
michael@0 240 int rowsAvailableInColumns23 = twoColumnRowsAvailable;
michael@0 241 for (; ii < variables.size(); ++ii) {
michael@0 242 const TVariableInfo& variable = variables[ii];
michael@0 243 if (GetNumComponentsPerRow(variable.type) != 2) {
michael@0 244 break;
michael@0 245 }
michael@0 246 int numRows = GetNumRows(variable.type) * variable.size;
michael@0 247 if (numRows <= rowsAvailableInColumns01) {
michael@0 248 rowsAvailableInColumns01 -= numRows;
michael@0 249 } else if (numRows <= rowsAvailableInColumns23) {
michael@0 250 rowsAvailableInColumns23 -= numRows;
michael@0 251 } else {
michael@0 252 return false;
michael@0 253 }
michael@0 254 }
michael@0 255
michael@0 256 int numRowsUsedInColumns01 =
michael@0 257 twoColumnRowsAvailable - rowsAvailableInColumns01;
michael@0 258 int numRowsUsedInColumns23 =
michael@0 259 twoColumnRowsAvailable - rowsAvailableInColumns23;
michael@0 260 fillColumns(top2ColumnRow, numRowsUsedInColumns01, 0, 2);
michael@0 261 fillColumns(maxRows_ - numRowsUsedInColumns23, numRowsUsedInColumns23,
michael@0 262 2, 2);
michael@0 263
michael@0 264 // Packs the 1 column variables.
michael@0 265 for (; ii < variables.size(); ++ii) {
michael@0 266 const TVariableInfo& variable = variables[ii];
michael@0 267 ASSERT(1 == GetNumComponentsPerRow(variable.type));
michael@0 268 int numRows = GetNumRows(variable.type) * variable.size;
michael@0 269 int smallestColumn = -1;
michael@0 270 int smallestSize = maxRows_ + 1;
michael@0 271 int topRow = -1;
michael@0 272 for (int column = 0; column < kNumColumns; ++column) {
michael@0 273 int row = 0;
michael@0 274 int size = 0;
michael@0 275 if (searchColumn(column, numRows, &row, &size)) {
michael@0 276 if (size < smallestSize) {
michael@0 277 smallestSize = size;
michael@0 278 smallestColumn = column;
michael@0 279 topRow = row;
michael@0 280 }
michael@0 281 }
michael@0 282 }
michael@0 283
michael@0 284 if (smallestColumn < 0) {
michael@0 285 return false;
michael@0 286 }
michael@0 287
michael@0 288 fillColumns(topRow, numRows, smallestColumn, 1);
michael@0 289 }
michael@0 290
michael@0 291 ASSERT(variables.size() == ii);
michael@0 292
michael@0 293 return true;
michael@0 294 }
michael@0 295
michael@0 296
michael@0 297

mercurial