Commit 8d804799 by gman@chromium.org

Add Variable Packing checks to ANGLE

BUG=373 TEST=unit tests git-svn-id: https://angleproject.googlecode.com/svn/trunk@1317 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 0d3b36de
...@@ -142,7 +142,10 @@ typedef enum { ...@@ -142,7 +142,10 @@ typedef enum {
// - The shader spec is SH_WEBGL_SPEC. // - The shader spec is SH_WEBGL_SPEC.
// - The compile options contain the SH_TIMING_RESTRICTIONS flag. // - The compile options contain the SH_TIMING_RESTRICTIONS flag.
// - The shader type is SH_FRAGMENT_SHADER. // - The shader type is SH_FRAGMENT_SHADER.
SH_DEPENDENCY_GRAPH = 0x0400 SH_DEPENDENCY_GRAPH = 0x0400,
// Enforce the GLSL 1.017 Appendix A section 7 packing restrictions.
SH_ENFORCE_PACKING_RESTRICTIONS = 0x0800,
} ShCompileOptions; } ShCompileOptions;
// //
......
...@@ -118,6 +118,8 @@ ...@@ -118,6 +118,8 @@
'compiler/ValidateLimitations.h', 'compiler/ValidateLimitations.h',
'compiler/VariableInfo.cpp', 'compiler/VariableInfo.cpp',
'compiler/VariableInfo.h', 'compiler/VariableInfo.h',
'compiler/VariablePacker.cpp',
'compiler/VariablePacker.h',
# Old preprocessor # Old preprocessor
'compiler/preprocessor/atom.c', 'compiler/preprocessor/atom.c',
'compiler/preprocessor/atom.h', 'compiler/preprocessor/atom.h',
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "compiler/RenameFunction.h" #include "compiler/RenameFunction.h"
#include "compiler/ShHandle.h" #include "compiler/ShHandle.h"
#include "compiler/ValidateLimitations.h" #include "compiler/ValidateLimitations.h"
#include "compiler/VariablePacker.h"
#include "compiler/depgraph/DependencyGraph.h" #include "compiler/depgraph/DependencyGraph.h"
#include "compiler/depgraph/DependencyGraphOutput.h" #include "compiler/depgraph/DependencyGraphOutput.h"
#include "compiler/timing/RestrictFragmentShaderTiming.h" #include "compiler/timing/RestrictFragmentShaderTiming.h"
...@@ -113,6 +114,9 @@ TCompiler::~TCompiler() ...@@ -113,6 +114,9 @@ TCompiler::~TCompiler()
bool TCompiler::Init(const ShBuiltInResources& resources) bool TCompiler::Init(const ShBuiltInResources& resources)
{ {
maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ?
resources.MaxVertexUniformVectors :
resources.MaxFragmentUniformVectors;
TScopedPoolAllocator scopedAlloc(&allocator, false); TScopedPoolAllocator scopedAlloc(&allocator, false);
// Generate built-in symbol table. // Generate built-in symbol table.
...@@ -192,8 +196,15 @@ bool TCompiler::compile(const char* const shaderStrings[], ...@@ -192,8 +196,15 @@ bool TCompiler::compile(const char* const shaderStrings[],
if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES)) if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES))
mapLongVariableNames(root); mapLongVariableNames(root);
if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) {
collectAttribsUniforms(root); collectAttribsUniforms(root);
if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) {
success = enforcePackingRestrictions();
if (!success) {
infoSink.info.message(EPrefixError, "too many uniforms");
}
}
}
if (success && (compileOptions & SH_INTERMEDIATE_TREE)) if (success && (compileOptions & SH_INTERMEDIATE_TREE))
intermediate.outputTree(root); intermediate.outputTree(root);
...@@ -310,6 +321,12 @@ void TCompiler::collectAttribsUniforms(TIntermNode* root) ...@@ -310,6 +321,12 @@ void TCompiler::collectAttribsUniforms(TIntermNode* root)
root->traverse(&collect); root->traverse(&collect);
} }
bool TCompiler::enforcePackingRestrictions()
{
VariablePacker packer;
return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
}
void TCompiler::mapLongVariableNames(TIntermNode* root) void TCompiler::mapLongVariableNames(TIntermNode* root)
{ {
ASSERT(longNameMap); ASSERT(longNameMap);
......
...@@ -88,6 +88,9 @@ protected: ...@@ -88,6 +88,9 @@ protected:
void mapLongVariableNames(TIntermNode* root); void mapLongVariableNames(TIntermNode* root);
// Translate to object code. // Translate to object code.
virtual void translate(TIntermNode* root) = 0; virtual void translate(TIntermNode* root) = 0;
// Returns true if, after applying the packing rules in the GLSL 1.017 spec
// Appendix A, section 7, the shader does not use too many uniforms.
bool enforcePackingRestrictions();
// Returns true if the shader passes the restrictions that aim to prevent timing attacks. // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph); bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
// Returns true if the shader does not use samplers. // Returns true if the shader does not use samplers.
...@@ -104,6 +107,8 @@ private: ...@@ -104,6 +107,8 @@ private:
ShShaderType shaderType; ShShaderType shaderType;
ShShaderSpec shaderSpec; ShShaderSpec shaderSpec;
int maxUniformVectors;
// Built-in symbol table for the given language, spec, and resources. // Built-in symbol table for the given language, spec, and resources.
// It is preserved from compile-to-compile. // It is preserved from compile-to-compile.
TSymbolTable symbolTable; TSymbolTable symbolTable;
......
...@@ -138,6 +138,16 @@ void getUserDefinedVariableInfo(const TType& type, ...@@ -138,6 +138,16 @@ void getUserDefinedVariableInfo(const TType& type,
} }
} }
TVariableInfo::TVariableInfo()
{
}
TVariableInfo::TVariableInfo(ShDataType type, int size)
: type(type),
size(size)
{
}
CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs, CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
TVariableInfoList& uniforms) TVariableInfoList& uniforms)
: mAttribs(attribs), : mAttribs(attribs),
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
// Provides information about a variable. // Provides information about a variable.
// It is currently being used to store info about active attribs and uniforms. // It is currently being used to store info about active attribs and uniforms.
struct TVariableInfo { struct TVariableInfo {
TVariableInfo(ShDataType type, int size);
TVariableInfo();
TPersistString name; TPersistString name;
TPersistString mappedName; TPersistString mappedName;
ShDataType type; ShDataType type;
......
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "compiler/VariablePacker.h"
#include <algorithm>
#include "compiler/ShHandle.h"
namespace {
int GetSortOrder(ShDataType type)
{
switch (type) {
case SH_FLOAT_MAT4:
return 0;
case SH_FLOAT_MAT2:
return 1;
case SH_FLOAT_VEC4:
case SH_INT_VEC4:
case SH_BOOL_VEC4:
return 2;
case SH_FLOAT_MAT3:
return 3;
case SH_FLOAT_VEC3:
case SH_INT_VEC3:
case SH_BOOL_VEC3:
return 4;
case SH_FLOAT_VEC2:
case SH_INT_VEC2:
case SH_BOOL_VEC2:
return 5;
case SH_FLOAT:
case SH_INT:
case SH_BOOL:
case SH_SAMPLER_2D:
case SH_SAMPLER_CUBE:
case SH_SAMPLER_EXTERNAL_OES:
case SH_SAMPLER_2D_RECT_ARB:
return 6;
default:
ASSERT(false);
return 7;
}
}
} // namespace
int VariablePacker::GetNumComponentsPerRow(ShDataType type)
{
switch (type) {
case SH_FLOAT_MAT4:
case SH_FLOAT_MAT2:
case SH_FLOAT_VEC4:
case SH_INT_VEC4:
case SH_BOOL_VEC4:
return 4;
case SH_FLOAT_MAT3:
case SH_FLOAT_VEC3:
case SH_INT_VEC3:
case SH_BOOL_VEC3:
return 3;
case SH_FLOAT_VEC2:
case SH_INT_VEC2:
case SH_BOOL_VEC2:
return 2;
case SH_FLOAT:
case SH_INT:
case SH_BOOL:
case SH_SAMPLER_2D:
case SH_SAMPLER_CUBE:
case SH_SAMPLER_EXTERNAL_OES:
case SH_SAMPLER_2D_RECT_ARB:
return 1;
default:
ASSERT(false);
return 5;
}
}
int VariablePacker::GetNumRows(ShDataType type)
{
switch (type) {
case SH_FLOAT_MAT4:
return 4;
case SH_FLOAT_MAT3:
return 3;
case SH_FLOAT_MAT2:
return 1;
case SH_FLOAT_VEC4:
case SH_INT_VEC4:
case SH_BOOL_VEC4:
case SH_FLOAT_VEC3:
case SH_INT_VEC3:
case SH_BOOL_VEC3:
case SH_FLOAT_VEC2:
case SH_INT_VEC2:
case SH_BOOL_VEC2:
case SH_FLOAT:
case SH_INT:
case SH_BOOL:
case SH_SAMPLER_2D:
case SH_SAMPLER_CUBE:
case SH_SAMPLER_EXTERNAL_OES:
case SH_SAMPLER_2D_RECT_ARB:
return 1;
default:
ASSERT(false);
return 100000;
}
}
struct TVariableInfoComparer {
bool operator()(const TVariableInfo& lhs, const TVariableInfo& rhs) const
{
int lhsSortOrder = GetSortOrder(lhs.type);
int rhsSortOrder = GetSortOrder(rhs.type);
if (lhsSortOrder != rhsSortOrder) {
return lhsSortOrder < rhsSortOrder;
}
// Sort by largest first.
return lhs.size > rhs.size;
}
};
unsigned VariablePacker::makeColumnFlags(int column, int numComponentsPerRow)
{
return ((kColumnMask << (kNumColumns - numComponentsPerRow)) &
kColumnMask) >> column;
}
void VariablePacker::fillColumns(int topRow, int numRows, int column, int numComponentsPerRow)
{
unsigned columnFlags = makeColumnFlags(column, numComponentsPerRow);
for (int r = 0; r < numRows; ++r) {
int row = topRow + r;
ASSERT((rows_[row] & columnFlags) == 0);
rows_[row] |= columnFlags;
}
}
bool VariablePacker::searchColumn(int column, int numRows, int* destRow, int* destSize)
{
ASSERT(destRow);
for (; topNonFullRow_ < maxRows_ && rows_[topNonFullRow_] == kColumnMask;
++topNonFullRow_) {
}
for (; bottomNonFullRow_ >= 0 && rows_[bottomNonFullRow_] == kColumnMask;
--bottomNonFullRow_) {
}
if (bottomNonFullRow_ - topNonFullRow_ + 1 < numRows) {
return false;
}
unsigned columnFlags = makeColumnFlags(column, 1);
int topGoodRow = 0;
int smallestGoodTop = -1;
int smallestGoodSize = maxRows_ + 1;
int bottomRow = bottomNonFullRow_ + 1;
bool found = false;
for (int row = topNonFullRow_; row <= bottomRow; ++row) {
bool rowEmpty = row < bottomRow ? ((rows_[row] & columnFlags) == 0) : false;
if (rowEmpty) {
if (!found) {
topGoodRow = row;
found = true;
}
} else {
if (found) {
int size = row - topGoodRow;
if (size >= numRows && size < smallestGoodSize) {
smallestGoodSize = size;
smallestGoodTop = topGoodRow;
}
}
found = false;
}
}
if (smallestGoodTop < 0) {
return false;
}
*destRow = smallestGoodTop;
if (destSize) {
*destSize = smallestGoodSize;
}
return true;
}
bool VariablePacker::CheckVariablesWithinPackingLimits(int maxVectors, const TVariableInfoList& in_variables)
{
ASSERT(maxVectors > 0);
maxRows_ = maxVectors;
topNonFullRow_ = 0;
bottomNonFullRow_ = maxRows_ - 1;
TVariableInfoList variables(in_variables);
// 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 TVariableInfo& variable = variables[ii];
if (GetNumComponentsPerRow(variable.type) != 4) {
break;
}
topNonFullRow_ += GetNumRows(variable.type) * variable.size;
}
if (topNonFullRow_ > maxRows_) {
return false;
}
// Packs the 3 column variables.
int num3ColumnRows = 0;
for (; ii < variables.size(); ++ii) {
const TVariableInfo& variable = variables[ii];
if (GetNumComponentsPerRow(variable.type) != 3) {
break;
}
num3ColumnRows += GetNumRows(variable.type) * variable.size;
}
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 TVariableInfo& variable = variables[ii];
if (GetNumComponentsPerRow(variable.type) != 2) {
break;
}
int numRows = GetNumRows(variable.type) * variable.size;
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 TVariableInfo& variable = variables[ii];
ASSERT(1 == GetNumComponentsPerRow(variable.type));
int numRows = GetNumRows(variable.type) * variable.size;
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;
}
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#ifndef _VARIABLEPACKER_INCLUDED_
#define _VARIABLEPACKER_INCLUDED_
#include <vector>
#include "compiler/ShHandle.h"
class VariablePacker {
public:
// Returns true if the passed in variables pack in maxVectors following
// the packing rules from the GLSL 1.017 spec, Appendix A, section 7.
bool CheckVariablesWithinPackingLimits(
int maxVectors,
const TVariableInfoList& in_variables);
// Gets how many components in a row a data type takes.
static int GetNumComponentsPerRow(ShDataType type);
// Gets how many rows a data type takes.
static int GetNumRows(ShDataType type);
private:
static const int kNumColumns = 4;
static const unsigned kColumnMask = (1 << kNumColumns) - 1;
unsigned makeColumnFlags(int column, int numComponentsPerRow);
void fillColumns(int topRow, int numRows, int column, int numComponentsPerRow);
bool searchColumn(int column, int numRows, int* destRow, int* destSize);
int topNonFullRow_;
int bottomNonFullRow_;
int maxRows_;
std::vector<unsigned> rows_;
};
#endif // _VARIABLEPACKER_INCLUDED_
...@@ -164,6 +164,7 @@ ...@@ -164,6 +164,7 @@
<ClCompile Include="util.cpp" /> <ClCompile Include="util.cpp" />
<ClCompile Include="ValidateLimitations.cpp" /> <ClCompile Include="ValidateLimitations.cpp" />
<ClCompile Include="VariableInfo.cpp" /> <ClCompile Include="VariableInfo.cpp" />
<ClCompile Include="VariablePacker.cpp" />
<ClCompile Include="preprocessor\atom.c" /> <ClCompile Include="preprocessor\atom.c" />
<ClCompile Include="preprocessor\cpp.c" /> <ClCompile Include="preprocessor\cpp.c" />
<ClCompile Include="preprocessor\cppstruct.c" /> <ClCompile Include="preprocessor\cppstruct.c" />
...@@ -263,6 +264,7 @@ ...@@ -263,6 +264,7 @@
<ClInclude Include="util.h" /> <ClInclude Include="util.h" />
<ClInclude Include="ValidateLimitations.h" /> <ClInclude Include="ValidateLimitations.h" />
<ClInclude Include="VariableInfo.h" /> <ClInclude Include="VariableInfo.h" />
<ClInclude Include="VariablePacker.h" />
<ClInclude Include="preprocessor\atom.h" /> <ClInclude Include="preprocessor\atom.h" />
<ClInclude Include="preprocessor\compile.h" /> <ClInclude Include="preprocessor\compile.h" />
<ClInclude Include="preprocessor\cpp.h" /> <ClInclude Include="preprocessor\cpp.h" />
......
...@@ -113,6 +113,9 @@ ...@@ -113,6 +113,9 @@
<ClCompile Include="VariableInfo.cpp"> <ClCompile Include="VariableInfo.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="VariablePacker.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="preprocessor\atom.c"> <ClCompile Include="preprocessor\atom.c">
<Filter>Source Files\preprocessor</Filter> <Filter>Source Files\preprocessor</Filter>
</ClCompile> </ClCompile>
...@@ -256,6 +259,9 @@ ...@@ -256,6 +259,9 @@
<ClInclude Include="VariableInfo.h"> <ClInclude Include="VariableInfo.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="VariablePacker.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="preprocessor\atom.h"> <ClInclude Include="preprocessor\atom.h">
<Filter>Header Files\preprocessor</Filter> <Filter>Header Files\preprocessor</Filter>
</ClInclude> </ClInclude>
......
...@@ -63,6 +63,25 @@ ...@@ -63,6 +63,25 @@
'preprocessor_tests/version_test.cpp', 'preprocessor_tests/version_test.cpp',
], ],
}, },
{
'target_name': 'compiler_tests',
'type': 'executable',
'dependencies': [
'../src/build_angle.gyp:translator_common',
'gtest',
'gmock',
],
'include_dirs': [
'../include',
'../src',
'../third_party/googletest/include',
'../third_party/googlemock/include',
],
'sources': [
'../third_party/googlemock/src/gmock_main.cc',
'compiler_tests/VariablePacker_test.cpp',
],
},
], ],
} }
......
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "compiler/VariablePacker.h"
#include "gtest/gtest.h"
TEST(VariablePacking, Pack) {
VariablePacker packer;
TVariableInfoList vars;
const int kMaxRows = 16;
// test no vars.
EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
ShDataType types[] = {
SH_FLOAT_MAT4, // 0
SH_FLOAT_MAT2, // 1
SH_FLOAT_VEC4, // 2
SH_INT_VEC4, // 3
SH_BOOL_VEC4, // 4
SH_FLOAT_MAT3, // 5
SH_FLOAT_VEC3, // 6
SH_INT_VEC3, // 7
SH_BOOL_VEC3, // 8
SH_FLOAT_VEC2, // 9
SH_INT_VEC2, // 10
SH_BOOL_VEC2, // 11
SH_FLOAT, // 12
SH_INT, // 13
SH_BOOL, // 14
SH_SAMPLER_2D, // 15
SH_SAMPLER_CUBE, // 16
SH_SAMPLER_EXTERNAL_OES, // 17
SH_SAMPLER_2D_RECT_ARB, // 18
};
for (size_t tt = 0; tt < sizeof(types) / sizeof(types[0]); ++tt) {
ShDataType type = types[tt];
int num_rows = VariablePacker::GetNumRows(type);
int num_components_per_row = VariablePacker::GetNumComponentsPerRow(type);
// Check 1 of the type.
vars.clear();
vars.push_back(TVariableInfo(type, 1));
EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
// Check exactly the right amount of 1 type as an array.
int num_vars = kMaxRows / num_rows;
vars.clear();
vars.push_back(TVariableInfo(type, num_vars));
EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
// test too many
vars.clear();
vars.push_back(TVariableInfo(type, num_vars + 1));
EXPECT_FALSE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
// Check exactly the right amount of 1 type as individual vars.
num_vars = kMaxRows / num_rows *
((num_components_per_row > 2) ? 1 : (4 / num_components_per_row));
vars.clear();
for (int ii = 0; ii < num_vars; ++ii) {
vars.push_back(TVariableInfo(type, 1));
}
EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
// Check 1 too many.
vars.push_back(TVariableInfo( type, 1));
EXPECT_FALSE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
}
// Test example from GLSL ES 3.0 spec chapter 11.
vars.clear();
vars.push_back(TVariableInfo(SH_FLOAT_VEC4, 1));
vars.push_back(TVariableInfo(SH_FLOAT_MAT3, 1));
vars.push_back(TVariableInfo(SH_FLOAT_MAT3, 1));
vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 6));
vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 4));
vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 1));
vars.push_back(TVariableInfo(SH_FLOAT, 3));
vars.push_back(TVariableInfo(SH_FLOAT, 2));
vars.push_back(TVariableInfo(SH_FLOAT, 1));
EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment