void ExpandUniforms(const std::vector<Uniform> &compact, std::vector<ShaderVariable> *expanded) { for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++) { const ShaderVariable &variable = compact[variableIndex]; ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded); } }
char* word_to_string(word_t* wd, BOOL ghil) { char* first_word = strdup(wd->string); char* ret; DIE(!first_word, "No memory in word_to_string()"); if (wd->expand) { free(first_word); first_word = ExpandVariable(wd->string); } if (ghil) { char* tmp; ret = calloc(strlen(first_word) + 5, 1); DIE(!ret, "No memory in word_to_string()"); tmp = first_word; first_word = escape_quotes(first_word); free(tmp); _snprintf_s(ret, strlen(first_word) + 5, strlen(first_word) + 5, "\"%s\"", first_word); free(first_word); } else ret = first_word; if (wd->next_word) { char* next_word = word_to_string(wd->next_word, ghil); DIE(!next_word, "No memory in word_to_string()"); append(&ret, next_word); free(next_word); } if (wd->next_part) { char* next_part = word_to_string(wd->next_part, FALSE); char* tmp = malloc(strlen(ret) + strlen(next_part) + 1); if (!strcmp(next_part, "=")) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); memcpy(tmp, ret, strlen(ret)); memcpy(tmp + strlen(ret), next_part, strlen(next_part) + 1); free(next_part); ret = tmp; } return ret; }
bool VariablePacker::CheckVariablesWithinPackingLimits( unsigned int maxVectors, const std::vector<sh::ShaderVariable> &in_variables) { ASSERT(maxVectors > 0); maxRows_ = maxVectors; topNonFullRow_ = 0; bottomNonFullRow_ = maxRows_ - 1; std::vector<sh::ShaderVariable> variables; for (const auto &variable : in_variables) { ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, &variables); } // Check whether each variable fits in the available vectors. for (size_t i = 0; i < variables.size(); i++) { const sh::ShaderVariable &variable = variables[i]; if (variable.elementCount() > maxVectors / GetNumRows(variable.type)) { return false; } } // As per GLSL 1.017 Appendix A, Section 7 variables are packed in specific // order by type, then by size of array, largest first. std::sort(variables.begin(), variables.end(), TVariableInfoComparer()); rows_.clear(); rows_.resize(maxVectors, 0); // Packs the 4 column variables. size_t ii = 0; for (; ii < variables.size(); ++ii) { const sh::ShaderVariable &variable = variables[ii]; if (GetNumComponentsPerRow(variable.type) != 4) { break; } topNonFullRow_ += GetNumRows(variable.type) * variable.elementCount(); } if (topNonFullRow_ > maxRows_) { return false; } // Packs the 3 column variables. int num3ColumnRows = 0; for (; ii < variables.size(); ++ii) { const sh::ShaderVariable &variable = variables[ii]; if (GetNumComponentsPerRow(variable.type) != 3) { break; } num3ColumnRows += GetNumRows(variable.type) * variable.elementCount(); } if (topNonFullRow_ + num3ColumnRows > maxRows_) { return false; } fillColumns(topNonFullRow_, num3ColumnRows, 0, 3); // Packs the 2 column variables. int top2ColumnRow = topNonFullRow_ + num3ColumnRows; int twoColumnRowsAvailable = maxRows_ - top2ColumnRow; int rowsAvailableInColumns01 = twoColumnRowsAvailable; int rowsAvailableInColumns23 = twoColumnRowsAvailable; for (; ii < variables.size(); ++ii) { const sh::ShaderVariable &variable = variables[ii]; if (GetNumComponentsPerRow(variable.type) != 2) { break; } int numRows = GetNumRows(variable.type) * variable.elementCount(); if (numRows <= rowsAvailableInColumns01) { rowsAvailableInColumns01 -= numRows; } else if (numRows <= rowsAvailableInColumns23) { rowsAvailableInColumns23 -= numRows; } else { return false; } } int numRowsUsedInColumns01 = twoColumnRowsAvailable - rowsAvailableInColumns01; int numRowsUsedInColumns23 = twoColumnRowsAvailable - rowsAvailableInColumns23; fillColumns(top2ColumnRow, numRowsUsedInColumns01, 0, 2); fillColumns(maxRows_ - numRowsUsedInColumns23, numRowsUsedInColumns23, 2, 2); // Packs the 1 column variables. for (; ii < variables.size(); ++ii) { const sh::ShaderVariable &variable = variables[ii]; ASSERT(1 == GetNumComponentsPerRow(variable.type)); int numRows = GetNumRows(variable.type) * variable.elementCount(); int smallestColumn = -1; int smallestSize = maxRows_ + 1; int topRow = -1; for (int column = 0; column < kNumColumns; ++column) { int row = 0; int size = 0; if (searchColumn(column, numRows, &row, &size)) { if (size < smallestSize) { smallestSize = size; smallestColumn = column; topRow = row; } } } if (smallestColumn < 0) { return false; } fillColumns(topRow, numRows, smallestColumn, 1); } ASSERT(variables.size() == ii); return true; }