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.
......
//
// 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