michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: #include "compiler/VersionGLSL.h" michael@0: michael@0: static const int GLSL_VERSION_110 = 110; michael@0: static const int GLSL_VERSION_120 = 120; michael@0: michael@0: // We need to scan for the following: michael@0: // 1. "invariant" keyword: This can occur in both - vertex and fragment shaders michael@0: // but only at the global scope. michael@0: // 2. "gl_PointCoord" built-in variable: This can only occur in fragment shader michael@0: // but inside any scope. michael@0: // 3. Call to a matrix constructor with another matrix as argument. michael@0: // (These constructors were reserved in GLSL version 1.10.) michael@0: // 4. Arrays as "out" function parameters. michael@0: // GLSL spec section 6.1.1: "When calling a function, expressions that do michael@0: // not evaluate to l-values cannot be passed to parameters declared as michael@0: // out or inout." michael@0: // GLSL 1.1 section 5.8: "Other binary or unary expressions, michael@0: // non-dereferenced arrays, function names, swizzles with repeated fields, michael@0: // and constants cannot be l-values." michael@0: // GLSL 1.2 relaxed the restriction on arrays, section 5.8: "Variables that michael@0: // are built-in types, entire structures or arrays... are all l-values." michael@0: // michael@0: // TODO(alokp): The following two cases of invariant decalaration get lost michael@0: // during parsing - they do not get carried over to the intermediate tree. michael@0: // Handle these cases: michael@0: // 1. When a pragma is used to force all output variables to be invariant: michael@0: // - #pragma STDGL invariant(all) michael@0: // 2. When a previously decalared or built-in variable is marked invariant: michael@0: // - invariant gl_Position; michael@0: // - varying vec3 color; invariant color; michael@0: // michael@0: TVersionGLSL::TVersionGLSL(ShShaderType type) michael@0: : mShaderType(type), michael@0: mVersion(GLSL_VERSION_110) michael@0: { michael@0: } michael@0: michael@0: void TVersionGLSL::visitSymbol(TIntermSymbol* node) michael@0: { michael@0: if (node->getSymbol() == "gl_PointCoord") michael@0: updateVersion(GLSL_VERSION_120); michael@0: } michael@0: michael@0: void TVersionGLSL::visitConstantUnion(TIntermConstantUnion*) michael@0: { michael@0: } michael@0: michael@0: bool TVersionGLSL::visitBinary(Visit, TIntermBinary*) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: bool TVersionGLSL::visitUnary(Visit, TIntermUnary*) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: bool TVersionGLSL::visitSelection(Visit, TIntermSelection*) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate* node) michael@0: { michael@0: bool visitChildren = true; michael@0: michael@0: switch (node->getOp()) { michael@0: case EOpSequence: michael@0: // We need to visit sequence children to get to global or inner scope. michael@0: visitChildren = true; michael@0: break; michael@0: case EOpDeclaration: { michael@0: const TIntermSequence& sequence = node->getSequence(); michael@0: TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier(); michael@0: if ((qualifier == EvqInvariantVaryingIn) || michael@0: (qualifier == EvqInvariantVaryingOut)) { michael@0: updateVersion(GLSL_VERSION_120); michael@0: } michael@0: break; michael@0: } michael@0: case EOpParameters: { michael@0: const TIntermSequence& params = node->getSequence(); michael@0: for (TIntermSequence::const_iterator iter = params.begin(); michael@0: iter != params.end(); ++iter) michael@0: { michael@0: const TIntermTyped* param = (*iter)->getAsTyped(); michael@0: if (param->isArray()) michael@0: { michael@0: TQualifier qualifier = param->getQualifier(); michael@0: if ((qualifier == EvqOut) || (qualifier == EvqInOut)) michael@0: { michael@0: updateVersion(GLSL_VERSION_120); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: // Fully processed. No need to visit children. michael@0: visitChildren = false; michael@0: break; michael@0: } michael@0: case EOpConstructMat2: michael@0: case EOpConstructMat3: michael@0: case EOpConstructMat4: { michael@0: const TIntermSequence& sequence = node->getSequence(); michael@0: if (sequence.size() == 1) { michael@0: TIntermTyped* typed = sequence.front()->getAsTyped(); michael@0: if (typed && typed->isMatrix()) { michael@0: updateVersion(GLSL_VERSION_120); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: break; michael@0: } michael@0: michael@0: return visitChildren; michael@0: } michael@0: michael@0: bool TVersionGLSL::visitLoop(Visit, TIntermLoop*) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: bool TVersionGLSL::visitBranch(Visit, TIntermBranch*) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: void TVersionGLSL::updateVersion(int version) michael@0: { michael@0: mVersion = std::max(version, mVersion); michael@0: } michael@0: