Commit 7b9b284b by Geoff Lang

Refactor GLSL version calculations for emulated functions.

Pass the target GLSL version to InitBuiltInFunctionEmulatorForGLSL so that it can be updated to generate emulated functions for multiple versions. BUG=angleproject:1044 Change-Id: I34c408df52fd0f7d6c2f66d0579ac854afd93b87 Reviewed-on: https://chromium-review.googlesource.com/277700Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 12425d25
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
#include "compiler/translator/BuiltInFunctionEmulator.h" #include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h" #include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
#include "compiler/translator/VersionGLSL.h"
void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType) void InitBuiltInFunctionEmulatorForGLSLWorkarounds(BuiltInFunctionEmulator *emu, sh::GLenum shaderType)
{ {
// we use macros here instead of function definitions to work around more GLSL // we use macros here instead of function definitions to work around more GLSL
// compiler bugs, in particular on NVIDIA hardware on Mac OSX. Macros are // compiler bugs, in particular on NVIDIA hardware on Mac OSX. Macros are
...@@ -36,9 +37,12 @@ void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum ...@@ -36,9 +37,12 @@ void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum
emu->addEmulatedFunction(EOpReflect, float1, float1, "#define webgl_reflect_emu(I, N) ((I) - 2.0 * (N) * (I) * (N))"); emu->addEmulatedFunction(EOpReflect, float1, float1, "#define webgl_reflect_emu(I, N) ((I) - 2.0 * (N) * (I) * (N))");
} }
// emulate built-in functions missing from OpenGL 4.1 // Emulate built-in functions missing from GLSL 1.30 and higher
void InitBuiltInFunctionEmulatorForGLSL4_1(BuiltInFunctionEmulator *emu, sh::GLenum shaderType) void InitBuiltInFunctionEmulatorForGLSLMissingFunctions(BuiltInFunctionEmulator *emu, sh::GLenum shaderType,
int targetGLSLVersion)
{ {
if (targetGLSLVersion == GLSL_VERSION_410)
{
TType *float2 = new TType(EbtFloat, 2); TType *float2 = new TType(EbtFloat, 2);
TType *uint1 = new TType(EbtUInt); TType *uint1 = new TType(EbtUInt);
...@@ -104,4 +108,5 @@ void InitBuiltInFunctionEmulatorForGLSL4_1(BuiltInFunctionEmulator *emu, sh::GLe ...@@ -104,4 +108,5 @@ void InitBuiltInFunctionEmulatorForGLSL4_1(BuiltInFunctionEmulator *emu, sh::GLe
" uint x = u & 0xffffu;\n" " uint x = u & 0xffffu;\n"
" return vec2(webgl_f16tof32(x), webgl_f16tof32(y));\n" " return vec2(webgl_f16tof32(x), webgl_f16tof32(y));\n"
"}\n"); "}\n");
}
} }
...@@ -14,11 +14,12 @@ class BuiltInFunctionEmulator; ...@@ -14,11 +14,12 @@ class BuiltInFunctionEmulator;
// //
// This is only a workaround for OpenGL driver bugs, and isn't needed in general. // This is only a workaround for OpenGL driver bugs, and isn't needed in general.
// //
void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType); void InitBuiltInFunctionEmulatorForGLSLWorkarounds(BuiltInFunctionEmulator *emu, sh::GLenum shaderType);
// //
// This function is emulating built-in functions missing from OpenGL 4.1. // This function is emulating built-in functions missing from GLSL 1.30 and higher.
// //
void InitBuiltInFunctionEmulatorForGLSL4_1(BuiltInFunctionEmulator *emu, sh::GLenum shaderType); void InitBuiltInFunctionEmulatorForGLSLMissingFunctions(BuiltInFunctionEmulator *emu, sh::GLenum shaderType,
int targetGLSLVersion);
#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_ #endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_
...@@ -20,7 +20,9 @@ TranslatorESSL::TranslatorESSL(sh::GLenum type, ShShaderSpec spec) ...@@ -20,7 +20,9 @@ TranslatorESSL::TranslatorESSL(sh::GLenum type, ShShaderSpec spec)
void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions)
{ {
if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS) if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)
InitBuiltInFunctionEmulatorForGLSL(emu, getShaderType()); {
InitBuiltInFunctionEmulatorForGLSLWorkarounds(emu, getShaderType());
}
} }
void TranslatorESSL::translate(TIntermNode *root, int) { void TranslatorESSL::translate(TIntermNode *root, int) {
......
...@@ -66,9 +66,12 @@ TranslatorGLSL::TranslatorGLSL(sh::GLenum type, ...@@ -66,9 +66,12 @@ TranslatorGLSL::TranslatorGLSL(sh::GLenum type,
void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions)
{ {
if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS) if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)
InitBuiltInFunctionEmulatorForGLSL(emu, getShaderType()); {
if (getOutputType() == SH_GLSL_410_CORE_OUTPUT) InitBuiltInFunctionEmulatorForGLSLWorkarounds(emu, getShaderType());
InitBuiltInFunctionEmulatorForGLSL4_1(emu, getShaderType()); }
int targetGLSLVersion = ShaderOutputTypeToGLSLVersion(getOutputType());
InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion);
} }
void TranslatorGLSL::translate(TIntermNode *root, int) { void TranslatorGLSL::translate(TIntermNode *root, int) {
......
...@@ -6,11 +6,17 @@ ...@@ -6,11 +6,17 @@
#include "compiler/translator/VersionGLSL.h" #include "compiler/translator/VersionGLSL.h"
static const int GLSL_VERSION_110 = 110; int ShaderOutputTypeToGLSLVersion(ShShaderOutput output)
static const int GLSL_VERSION_120 = 120; {
static const int GLSL_VERSION_130 = 130; switch (output)
static const int GLSL_VERSION_410 = 410; {
static const int GLSL_VERSION_420 = 420; case SH_GLSL_130_OUTPUT: return GLSL_VERSION_130;
case SH_GLSL_410_CORE_OUTPUT: return GLSL_VERSION_410;
case SH_GLSL_420_CORE_OUTPUT: return GLSL_VERSION_420;
case SH_GLSL_COMPATIBILITY_OUTPUT: return GLSL_VERSION_110;
default: UNREACHABLE(); return 0;
}
}
// We need to scan for the following: // We need to scan for the following:
// 1. "invariant" keyword: This can occur in both - vertex and fragment shaders // 1. "invariant" keyword: This can occur in both - vertex and fragment shaders
...@@ -34,32 +40,19 @@ TVersionGLSL::TVersionGLSL(sh::GLenum type, ...@@ -34,32 +40,19 @@ TVersionGLSL::TVersionGLSL(sh::GLenum type,
ShShaderOutput output) ShShaderOutput output)
: TIntermTraverser(true, false, false) : TIntermTraverser(true, false, false)
{ {
if (output == SH_GLSL_130_OUTPUT) mVersion = ShaderOutputTypeToGLSLVersion(output);
{
mVersion = GLSL_VERSION_130;
}
else if (output == SH_GLSL_410_CORE_OUTPUT)
{
mVersion = GLSL_VERSION_410;
}
else if (output == SH_GLSL_420_CORE_OUTPUT)
{
mVersion = GLSL_VERSION_420;
}
else
{
ASSERT(output == SH_GLSL_COMPATIBILITY_OUTPUT);
if (pragma.stdgl.invariantAll) if (pragma.stdgl.invariantAll)
mVersion = GLSL_VERSION_120; {
else ensureVersionIsAtLeast(GLSL_VERSION_120);
mVersion = GLSL_VERSION_110;
} }
} }
void TVersionGLSL::visitSymbol(TIntermSymbol *node) void TVersionGLSL::visitSymbol(TIntermSymbol *node)
{ {
if (node->getSymbol() == "gl_PointCoord") if (node->getSymbol() == "gl_PointCoord")
updateVersion(GLSL_VERSION_120); {
ensureVersionIsAtLeast(GLSL_VERSION_120);
}
} }
bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
...@@ -77,12 +70,12 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) ...@@ -77,12 +70,12 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
const TIntermSequence &sequence = *(node->getSequence()); const TIntermSequence &sequence = *(node->getSequence());
if (sequence.front()->getAsTyped()->getType().isInvariant()) if (sequence.front()->getAsTyped()->getType().isInvariant())
{ {
updateVersion(GLSL_VERSION_120); ensureVersionIsAtLeast(GLSL_VERSION_120);
} }
break; break;
} }
case EOpInvariantDeclaration: case EOpInvariantDeclaration:
updateVersion(GLSL_VERSION_120); ensureVersionIsAtLeast(GLSL_VERSION_120);
break; break;
case EOpParameters: case EOpParameters:
{ {
...@@ -96,7 +89,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) ...@@ -96,7 +89,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
TQualifier qualifier = param->getQualifier(); TQualifier qualifier = param->getQualifier();
if ((qualifier == EvqOut) || (qualifier == EvqInOut)) if ((qualifier == EvqOut) || (qualifier == EvqInOut))
{ {
updateVersion(GLSL_VERSION_120); ensureVersionIsAtLeast(GLSL_VERSION_120);
break; break;
} }
} }
...@@ -115,7 +108,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) ...@@ -115,7 +108,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
TIntermTyped *typed = sequence.front()->getAsTyped(); TIntermTyped *typed = sequence.front()->getAsTyped();
if (typed && typed->isMatrix()) if (typed && typed->isMatrix())
{ {
updateVersion(GLSL_VERSION_120); ensureVersionIsAtLeast(GLSL_VERSION_120);
} }
} }
break; break;
...@@ -127,7 +120,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node) ...@@ -127,7 +120,7 @@ bool TVersionGLSL::visitAggregate(Visit, TIntermAggregate *node)
return visitChildren; return visitChildren;
} }
void TVersionGLSL::updateVersion(int version) void TVersionGLSL::ensureVersionIsAtLeast(int version)
{ {
mVersion = std::max(version, mVersion); mVersion = std::max(version, mVersion);
} }
......
...@@ -11,6 +11,14 @@ ...@@ -11,6 +11,14 @@
#include "compiler/translator/Pragma.h" #include "compiler/translator/Pragma.h"
static const int GLSL_VERSION_110 = 110;
static const int GLSL_VERSION_120 = 120;
static const int GLSL_VERSION_130 = 130;
static const int GLSL_VERSION_410 = 410;
static const int GLSL_VERSION_420 = 420;
int ShaderOutputTypeToGLSLVersion(ShShaderOutput output);
// Traverses the intermediate tree to return the minimum GLSL version // Traverses the intermediate tree to return the minimum GLSL version
// required to legally access all built-in features used in the shader. // required to legally access all built-in features used in the shader.
// GLSL 1.1 which is mandated by OpenGL 2.0 provides: // GLSL 1.1 which is mandated by OpenGL 2.0 provides:
...@@ -39,15 +47,14 @@ class TVersionGLSL : public TIntermTraverser ...@@ -39,15 +47,14 @@ class TVersionGLSL : public TIntermTraverser
// - matrix/matrix constructors // - matrix/matrix constructors
// - array "out" parameters // - array "out" parameters
// Else 110 is returned. // Else 110 is returned.
int getVersion() { return mVersion; } int getVersion() const { return mVersion; }
virtual void visitSymbol(TIntermSymbol *); virtual void visitSymbol(TIntermSymbol *);
virtual bool visitAggregate(Visit, TIntermAggregate *); virtual bool visitAggregate(Visit, TIntermAggregate *);
protected:
void updateVersion(int version);
private: private:
void ensureVersionIsAtLeast(int version);
int mVersion; int mVersion;
}; };
......
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