Commit 4a667fe9 by Zhenyao Mo

Add an option to initialize varyings without static use in vertex shaders

ANGLEBUG=554 TEST=webgl conformance test on mac: shaders-with-varyings.html r=kbr@chromium.org,nicolascapens@chromium.org cc=alokp@chromium.org,shannonwoods@chromium.org Change-Id: I2e692d43fb15f1cf3ade3e398020d1fedb2b32f0 Reviewed-on: https://chromium-review.googlesource.com/185922Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org> Tested-by: 's avatarZhenyao Mo <zmo@chromium.org> Conflicts: src/common/version.h src/compiler/translator/Compiler.cpp Change-Id: If7db13ef345bd6199d4ea0d7786f0de20885f2f3 Reviewed-on: https://chromium-review.googlesource.com/186144Reviewed-by: 's avatarNicolas Capens <nicolascapens@chromium.org> Reviewed-by: 's avatarKenneth Russell <kbr@chromium.org> Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org> Tested-by: 's avatarZhenyao Mo <zmo@chromium.org>
parent 32d508e2
......@@ -212,8 +212,8 @@ typedef enum {
// This flag limits the depth of the call stack.
SH_LIMIT_CALL_STACK_DEPTH = 0x4000,
// This flag initializes gl_Position to vec4(0.0, 0.0, 0.0, 1.0) at
// the beginning of the vertex shader, and has no effect in the
// This flag initializes gl_Position to vec4(0,0,0,0) at the
// beginning of the vertex shader's main(), and has no effect in the
// fragment shader. It is intended as a workaround for drivers which
// incorrectly fail to link programs if gl_Position is not written.
SH_INIT_GL_POSITION = 0x8000,
......@@ -224,6 +224,12 @@ typedef enum {
// This is to work around a MacOSX driver bug that |b| is executed
// independent of |a|'s value.
SH_UNFOLD_SHORT_CIRCUIT = 0x10000,
// This flag initializes varyings without static use in vertex shader
// at the beginning of main(), and has no effects in the fragment shader.
// It is intended as a workaround for drivers which incorrectly optimize
// out such varyings and cause a link failure.
SH_INIT_VARYINGS_WITHOUT_STATIC_USE = 0x20000,
} ShCompileOptions;
// Defines alternate strategies for implementing array index clamping.
......
......@@ -8,8 +8,8 @@
#include "compiler/translator/DetectCallDepth.h"
#include "compiler/translator/ForLoopUnroll.h"
#include "compiler/translator/Initialize.h"
#include "compiler/translator/InitializeGLPosition.h"
#include "compiler/translator/InitializeParseContext.h"
#include "compiler/translator/InitializeVariables.h"
#include "compiler/translator/MapLongVariableNames.h"
#include "compiler/translator/ParseContext.h"
#include "compiler/translator/RenameFunction.h"
......@@ -30,43 +30,51 @@ bool isWebGLBasedSpec(ShShaderSpec spec)
}
namespace {
class TScopedPoolAllocator {
public:
TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator) {
class TScopedPoolAllocator
{
public:
TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
{
mAllocator->push();
SetGlobalPoolAllocator(mAllocator);
}
~TScopedPoolAllocator() {
~TScopedPoolAllocator()
{
SetGlobalPoolAllocator(NULL);
mAllocator->pop();
}
private:
private:
TPoolAllocator* mAllocator;
};
class TScopedSymbolTableLevel {
public:
TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table) {
class TScopedSymbolTableLevel
{
public:
TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
{
ASSERT(mTable->atBuiltInLevel());
mTable->push();
}
~TScopedSymbolTableLevel() {
~TScopedSymbolTableLevel()
{
while (!mTable->atBuiltInLevel())
mTable->pop();
}
private:
private:
TSymbolTable* mTable;
};
} // namespace
TShHandleBase::TShHandleBase() {
TShHandleBase::TShHandleBase()
{
allocator.push();
SetGlobalPoolAllocator(&allocator);
}
TShHandleBase::~TShHandleBase() {
TShHandleBase::~TShHandleBase()
{
SetGlobalPoolAllocator(NULL);
allocator.popAll();
}
......@@ -156,7 +164,8 @@ bool TCompiler::compile(const char* const shaderStrings[],
shaderVersion = parseContext.getShaderVersion();
if (success) {
if (success)
{
TIntermNode* root = parseContext.treeRoot;
success = intermediate.postProcess(root);
......@@ -198,26 +207,31 @@ bool TCompiler::compile(const char* const shaderStrings[],
if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) && hashFunction == NULL)
mapLongVariableNames(root);
if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION)) {
InitializeGLPosition initGLPosition;
root->traverse(&initGLPosition);
}
if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
initializeGLPosition(root);
if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)) {
if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
{
UnfoldShortCircuitAST unfoldShortCircuit;
root->traverse(&unfoldShortCircuit);
unfoldShortCircuit.updateTree();
}
if (success && (compileOptions & SH_VARIABLES)) {
if (success && (compileOptions & SH_VARIABLES))
{
collectVariables(root);
if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) {
if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
{
success = enforcePackingRestrictions();
if (!success) {
if (!success)
{
infoSink.info.prefix(EPrefixError);
infoSink.info << "too many uniforms";
}
}
if (success && shaderType == SH_VERTEX_SHADER &&
(compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
initializeVaryingsWithoutStaticUse(root);
}
if (success && (compileOptions & SH_INTERMEDIATE_TREE))
......@@ -268,12 +282,14 @@ bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
symbolTable.setDefaultPrecision(integer, EbpHigh);
symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
break;
default: assert(false && "Language not supported");
default:
assert(false && "Language not supported");
}
// We set defaults for all the sampler types, even those that are
// only available if an extension exists.
for (int samplerType = EbtGuardSamplerBegin + 1;
samplerType < EbtGuardSamplerEnd; ++samplerType) {
samplerType < EbtGuardSamplerEnd; ++samplerType)
{
sampler.type = static_cast<TBasicType>(samplerType);
symbolTable.setDefaultPrecision(sampler, EbpLow);
}
......@@ -305,24 +321,25 @@ bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool lim
{
DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
root->traverse(&detect);
switch (detect.detectCallDepth()) {
case DetectCallDepth::kErrorNone:
return true;
case DetectCallDepth::kErrorMissingMain:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Missing main()";
return false;
case DetectCallDepth::kErrorRecursion:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Function recursion detected";
return false;
case DetectCallDepth::kErrorMaxDepthExceeded:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Function call stack too deep";
return false;
default:
UNREACHABLE();
return false;
switch (detect.detectCallDepth())
{
case DetectCallDepth::kErrorNone:
return true;
case DetectCallDepth::kErrorMissingMain:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Missing main()";
return false;
case DetectCallDepth::kErrorRecursion:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Function recursion detected";
return false;
case DetectCallDepth::kErrorMaxDepthExceeded:
infoSink.info.prefix(EPrefixError);
infoSink.info << "Function call stack too deep";
return false;
default:
UNREACHABLE();
return false;
}
}
......@@ -339,7 +356,8 @@ void TCompiler::rewriteCSSShader(TIntermNode* root)
root->traverse(&renamer);
}
bool TCompiler::validateLimitations(TIntermNode* root) {
bool TCompiler::validateLimitations(TIntermNode* root)
{
ValidateLimitations validate(shaderType, infoSink.info);
root->traverse(&validate);
return validate.numErrors() == 0;
......@@ -347,26 +365,30 @@ bool TCompiler::validateLimitations(TIntermNode* root) {
bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
{
if (shaderSpec != SH_WEBGL_SPEC) {
if (shaderSpec != SH_WEBGL_SPEC)
{
infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
return false;
}
if (shaderType == SH_FRAGMENT_SHADER) {
if (shaderType == SH_FRAGMENT_SHADER)
{
TDependencyGraph graph(root);
// Output any errors first.
bool success = enforceFragmentShaderTimingRestrictions(graph);
// Then, output the dependency graph.
if (outputGraph) {
if (outputGraph)
{
TDependencyGraphOutput output(infoSink.info);
output.outputAllSpanningTrees(graph);
}
return success;
}
else {
else
{
return enforceVertexShaderTimingRestrictions(root);
}
}
......@@ -386,7 +408,8 @@ bool TCompiler::limitExpressionComplexity(TIntermNode* root)
samplerSymbol->traverse(&graphTraverser);
}
if (traverser.getMaxDepth() > maxExpressionComplexity) {
if (traverser.getMaxDepth() > maxExpressionComplexity)
{
infoSink.info << "Expression too complex.";
return false;
}
......@@ -419,6 +442,70 @@ bool TCompiler::enforcePackingRestrictions()
return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
}
void TCompiler::initializeGLPosition(TIntermNode* root)
{
InitializeVariables::InitVariableInfoList variables;
InitializeVariables::InitVariableInfo var(
"gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
variables.push_back(var);
InitializeVariables initializer(variables);
root->traverse(&initializer);
}
void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
{
InitializeVariables::InitVariableInfoList variables;
for (size_t ii = 0; ii < varyings.size(); ++ii)
{
const TVariableInfo& varying = varyings[ii];
if (varying.staticUse)
continue;
unsigned char size = 0;
bool matrix = false;
switch (varying.type)
{
case SH_FLOAT:
size = 1;
break;
case SH_FLOAT_VEC2:
size = 2;
break;
case SH_FLOAT_VEC3:
size = 3;
break;
case SH_FLOAT_VEC4:
size = 4;
break;
case SH_FLOAT_MAT2:
size = 2;
matrix = true;
break;
case SH_FLOAT_MAT3:
size = 3;
matrix = true;
break;
case SH_FLOAT_MAT4:
size = 4;
matrix = true;
break;
default:
ASSERT(false);
}
TType type(EbtFloat, EbpUndefined, EvqVaryingOut, size, matrix, varying.isArray);
TString name = varying.name.c_str();
if (varying.isArray)
{
type.setArraySize(varying.size);
name = name.substr(0, name.find_first_of('['));
}
InitializeVariables::InitVariableInfo var(name, type);
variables.push_back(var);
}
InitializeVariables initializer(variables);
root->traverse(&initializer);
}
void TCompiler::mapLongVariableNames(TIntermNode* root)
{
ASSERT(longNameMap);
......
//
// Copyright (c) 2002-2013 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/translator/InitializeGLPosition.h"
#include "compiler/translator/compilerdebug.h"
bool InitializeGLPosition::visitAggregate(Visit visit, TIntermAggregate* node)
{
bool visitChildren = !mCodeInserted;
switch (node->getOp())
{
case EOpSequence: break;
case EOpFunction:
{
// Function definition.
ASSERT(visit == PreVisit);
if (node->getName() == "main(")
{
TIntermSequence &sequence = node->getSequence();
ASSERT((sequence.size() == 1) || (sequence.size() == 2));
TIntermAggregate *body = NULL;
if (sequence.size() == 1)
{
body = new TIntermAggregate(EOpSequence);
sequence.push_back(body);
}
else
{
body = sequence[1]->getAsAggregate();
}
ASSERT(body);
insertCode(body->getSequence());
mCodeInserted = true;
}
break;
}
default: visitChildren = false; break;
}
return visitChildren;
}
void InitializeGLPosition::insertCode(TIntermSequence& sequence)
{
TIntermBinary *binary = new TIntermBinary(EOpAssign);
sequence.insert(sequence.begin(), binary);
TIntermSymbol *left = new TIntermSymbol(
0, "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
binary->setLeft(left);
ConstantUnion *u = new ConstantUnion[4];
for (int ii = 0; ii < 3; ++ii)
u[ii].setFConst(0.0f);
u[3].setFConst(1.0f);
TIntermConstantUnion *right = new TIntermConstantUnion(
u, TType(EbtFloat, EbpUndefined, EvqConst, 4));
binary->setRight(right);
}
//
// Copyright (c) 2002-2013 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/translator/InitializeVariables.h"
#include "compiler/translator/compilerdebug.h"
namespace
{
TIntermConstantUnion* constructFloatConstUnionNode(const TType& type)
{
TType myType = type;
unsigned char size = myType.getNominalSize();
if (myType.isMatrix())
size *= size;
ConstantUnion *u = new ConstantUnion[size];
for (int ii = 0; ii < size; ++ii)
u[ii].setFConst(0.0f);
myType.clearArrayness();
myType.setQualifier(EvqConst);
TIntermConstantUnion *node = new TIntermConstantUnion(u, myType);
return node;
}
TIntermConstantUnion* constructIndexNode(int index)
{
ConstantUnion *u = new ConstantUnion[1];
u[0].setIConst(index);
TType type(EbtInt, EbpUndefined, EvqConst, 1);
TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
return node;
}
} // namespace anonymous
bool InitializeVariables::visitAggregate(Visit visit, TIntermAggregate* node)
{
bool visitChildren = !mCodeInserted;
switch (node->getOp())
{
case EOpSequence:
break;
case EOpFunction:
{
// Function definition.
ASSERT(visit == PreVisit);
if (node->getName() == "main(")
{
TIntermSequence &sequence = node->getSequence();
ASSERT((sequence.size() == 1) || (sequence.size() == 2));
TIntermAggregate *body = NULL;
if (sequence.size() == 1)
{
body = new TIntermAggregate(EOpSequence);
sequence.push_back(body);
}
else
{
body = sequence[1]->getAsAggregate();
}
ASSERT(body);
insertInitCode(body->getSequence());
mCodeInserted = true;
}
break;
}
default:
visitChildren = false;
break;
}
return visitChildren;
}
void InitializeVariables::insertInitCode(TIntermSequence& sequence)
{
for (size_t ii = 0; ii < mVariables.size(); ++ii)
{
const InitVariableInfo& varInfo = mVariables[ii];
if (varInfo.type.isArray())
{
for (int index = varInfo.type.getArraySize() - 1; index >= 0; --index)
{
TIntermBinary *assign = new TIntermBinary(EOpAssign);
sequence.insert(sequence.begin(), assign);
TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect);
TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
indexDirect->setLeft(symbol);
TIntermConstantUnion *indexNode = constructIndexNode(index);
indexDirect->setRight(indexNode);
assign->setLeft(indexDirect);
TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
assign->setRight(zeroConst);
}
}
else
{
TIntermBinary *assign = new TIntermBinary(EOpAssign);
sequence.insert(sequence.begin(), assign);
TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
assign->setLeft(symbol);
TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
assign->setRight(zeroConst);
}
}
}
......@@ -4,17 +4,34 @@
// found in the LICENSE file.
//
#ifndef COMPILER_INITIALIZE_GL_POSITION_H_
#define COMPILER_INITIALIZE_GL_POSITION_H_
#ifndef COMPILER_INITIALIZE_VARIABLES_H_
#define COMPILER_INITIALIZE_VARIABLES_H_
#include "compiler/translator/intermediate.h"
class InitializeGLPosition : public TIntermTraverser
class InitializeVariables : public TIntermTraverser
{
public:
InitializeGLPosition() : mCodeInserted(false) { }
protected:
public:
struct InitVariableInfo
{
TString name;
TType type;
InitVariableInfo(const TString& _name, const TType& _type)
: name(_name),
type(_type)
{
}
};
typedef TVector<InitVariableInfo> InitVariableInfoList;
InitializeVariables(const InitVariableInfoList& vars)
: mCodeInserted(false),
mVariables(vars)
{
}
protected:
virtual bool visitBinary(Visit visit, TIntermBinary* node) { return false; }
virtual bool visitUnary(Visit visit, TIntermUnary* node) { return false; }
virtual bool visitSelection(Visit visit, TIntermSelection* node) { return false; }
......@@ -23,11 +40,11 @@ protected:
virtual bool visitAggregate(Visit visit, TIntermAggregate* node);
private:
// Insert AST node in the beginning of main() for "gl_Position = vec4(0.0, 0.0, 0.0, 1.0);".
void insertCode(TIntermSequence& sequence);
private:
void insertInitCode(TIntermSequence& sequence);
InitVariableInfoList mVariables;
bool mCodeInserted;
};
#endif // COMPILER_INITIALIZE_GL_POSITION_H_
#endif // COMPILER_INITIALIZE_VARIABLES_H_
......@@ -103,6 +103,16 @@ protected:
// 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();
// Insert statements to initialize varyings without static use in the beginning
// of main(). It is to work around a Mac driver where such varyings in a vertex
// shader may be optimized out incorrectly at compile time, causing a link failure.
// This function should only be applied to vertex shaders.
void initializeVaryingsWithoutStaticUse(TIntermNode* root);
// Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
// It is to work around a Linux driver bug where missing this causes compile failure
// while spec says it is allowed.
// This function should only be applied to vertex shaders.
void initializeGLPosition(TIntermNode* root);
// Returns true if the shader passes the restrictions that aim to prevent timing attacks.
bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
// Returns true if the shader does not use samplers.
......
......@@ -163,10 +163,12 @@ void getBuiltInVariableInfo(const TType& type,
varInfo.name = (name + "[0]").c_str();
varInfo.mappedName = (mappedName + "[0]").c_str();
varInfo.size = type.getArraySize();
varInfo.isArray = true;
} else {
varInfo.name = name.c_str();
varInfo.mappedName = mappedName.c_str();
varInfo.size = 1;
varInfo.isArray = false;
}
varInfo.precision = type.getPrecision();
varInfo.type = getVariableDataType(type);
......@@ -214,6 +216,7 @@ TVariableInfo* findVariable(const TType& type,
TVariableInfo::TVariableInfo()
: type(SH_NONE),
size(0),
isArray(false),
precision(EbpUndefined),
staticUse(false)
{
......@@ -222,6 +225,7 @@ TVariableInfo::TVariableInfo()
TVariableInfo::TVariableInfo(ShDataType type, int size)
: type(type),
size(size),
isArray(false),
precision(EbpUndefined),
staticUse(false)
{
......
......@@ -20,6 +20,7 @@ struct TVariableInfo {
TPersistString mappedName;
ShDataType type;
int size;
bool isArray;
TPrecision precision;
bool staticUse;
};
......
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