Commit 8efc5ad5 by Olli Etuaho

Initialize BuiltInFunctionEmulator outside Compiler

This moves GLSL output specific code from the Compiler class to the GLSL/ESSL translators. BUG=angleproject:865 Change-Id: I2d552e9cdb41f7d8ddfee7b0249a99d629a6d7d7 Reviewed-on: https://chromium-review.googlesource.com/255471Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarOlli Etuaho <oetuaho@nvidia.com>
parent b27f79a7
...@@ -100,7 +100,7 @@ class BuiltInFunctionEmulator::BuiltInFunctionEmulationMarker : public TIntermTr ...@@ -100,7 +100,7 @@ class BuiltInFunctionEmulator::BuiltInFunctionEmulationMarker : public TIntermTr
BuiltInFunctionEmulator::BuiltInFunctionEmulator() BuiltInFunctionEmulator::BuiltInFunctionEmulator()
{} {}
void BuiltInFunctionEmulator::AddEmulatedFunction( void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param, TOperator op, const TType& param,
const char* emulatedFunctionDefinition) const char* emulatedFunctionDefinition)
{ {
...@@ -108,7 +108,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction( ...@@ -108,7 +108,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction(
std::string(emulatedFunctionDefinition); std::string(emulatedFunctionDefinition);
} }
void BuiltInFunctionEmulator::AddEmulatedFunction( void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param1, const TType& param2, TOperator op, const TType& param1, const TType& param2,
const char* emulatedFunctionDefinition) const char* emulatedFunctionDefinition)
{ {
...@@ -116,7 +116,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction( ...@@ -116,7 +116,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction(
std::string(emulatedFunctionDefinition); std::string(emulatedFunctionDefinition);
} }
void BuiltInFunctionEmulator::AddEmulatedFunction( void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param1, const TType& param2, const TType& param3, TOperator op, const TType& param1, const TType& param2, const TType& param3,
const char* emulatedFunctionDefinition) const char* emulatedFunctionDefinition)
{ {
...@@ -174,6 +174,9 @@ void BuiltInFunctionEmulator::MarkBuiltInFunctionsForEmulation( ...@@ -174,6 +174,9 @@ void BuiltInFunctionEmulator::MarkBuiltInFunctionsForEmulation(
{ {
ASSERT(root); ASSERT(root);
if (mEmulatedFunctions.empty())
return;
BuiltInFunctionEmulationMarker marker(*this); BuiltInFunctionEmulationMarker marker(*this);
root->traverse(&marker); root->traverse(&marker);
} }
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
class BuiltInFunctionEmulator class BuiltInFunctionEmulator
{ {
public: public:
BuiltInFunctionEmulator();
void MarkBuiltInFunctionsForEmulation(TIntermNode* root); void MarkBuiltInFunctionsForEmulation(TIntermNode* root);
void Cleanup(); void Cleanup();
...@@ -32,13 +34,10 @@ class BuiltInFunctionEmulator ...@@ -32,13 +34,10 @@ class BuiltInFunctionEmulator
// shader source. // shader source.
void OutputEmulatedFunctions(TInfoSinkBase& out) const; void OutputEmulatedFunctions(TInfoSinkBase& out) const;
protected:
BuiltInFunctionEmulator();
// Add functions that need to be emulated. // Add functions that need to be emulated.
void AddEmulatedFunction(TOperator op, const TType& param, const char* emulatedFunctionDefinition); void addEmulatedFunction(TOperator op, const TType& param, const char* emulatedFunctionDefinition);
void AddEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const char* emulatedFunctionDefinition); void addEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const char* emulatedFunctionDefinition);
void AddEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const TType& param3, const char* emulatedFunctionDefinition); void addEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const TType& param3, const char* emulatedFunctionDefinition);
private: private:
class BuiltInFunctionEmulationMarker; class BuiltInFunctionEmulationMarker;
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
// //
#include "angle_gl.h" #include "angle_gl.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"
BuiltInFunctionEmulatorGLSL::BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType) void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType)
: BuiltInFunctionEmulator()
{ {
#if defined(__APPLE__) #if defined(__APPLE__)
// 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
...@@ -25,15 +25,15 @@ BuiltInFunctionEmulatorGLSL::BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType) ...@@ -25,15 +25,15 @@ BuiltInFunctionEmulatorGLSL::BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType)
if (shaderType == GL_FRAGMENT_SHADER) if (shaderType == GL_FRAGMENT_SHADER)
{ {
AddEmulatedFunction(EOpCos, float1, "webgl_emu_precision float webgl_cos_emu(webgl_emu_precision float a) { return cos(a); }"); emu->addEmulatedFunction(EOpCos, float1, "webgl_emu_precision float webgl_cos_emu(webgl_emu_precision float a) { return cos(a); }");
AddEmulatedFunction(EOpCos, float2, "webgl_emu_precision vec2 webgl_cos_emu(webgl_emu_precision vec2 a) { return cos(a); }"); emu->addEmulatedFunction(EOpCos, float2, "webgl_emu_precision vec2 webgl_cos_emu(webgl_emu_precision vec2 a) { return cos(a); }");
AddEmulatedFunction(EOpCos, float3, "webgl_emu_precision vec3 webgl_cos_emu(webgl_emu_precision vec3 a) { return cos(a); }"); emu->addEmulatedFunction(EOpCos, float3, "webgl_emu_precision vec3 webgl_cos_emu(webgl_emu_precision vec3 a) { return cos(a); }");
AddEmulatedFunction(EOpCos, float4, "webgl_emu_precision vec4 webgl_cos_emu(webgl_emu_precision vec4 a) { return cos(a); }"); emu->addEmulatedFunction(EOpCos, float4, "webgl_emu_precision vec4 webgl_cos_emu(webgl_emu_precision vec4 a) { return cos(a); }");
} }
AddEmulatedFunction(EOpDistance, float1, float1, "#define webgl_distance_emu(x, y) ((x) >= (y) ? (x) - (y) : (y) - (x))"); emu->addEmulatedFunction(EOpDistance, float1, float1, "#define webgl_distance_emu(x, y) ((x) >= (y) ? (x) - (y) : (y) - (x))");
AddEmulatedFunction(EOpDot, float1, float1, "#define webgl_dot_emu(x, y) ((x) * (y))"); emu->addEmulatedFunction(EOpDot, float1, float1, "#define webgl_dot_emu(x, y) ((x) * (y))");
AddEmulatedFunction(EOpLength, float1, "#define webgl_length_emu(x) ((x) >= 0.0 ? (x) : -(x))"); emu->addEmulatedFunction(EOpLength, float1, "#define webgl_length_emu(x) ((x) >= 0.0 ? (x) : -(x))");
AddEmulatedFunction(EOpNormalize, float1, "#define webgl_normalize_emu(x) ((x) == 0.0 ? 0.0 : ((x) > 0.0 ? 1.0 : -1.0))"); emu->addEmulatedFunction(EOpNormalize, float1, "#define webgl_normalize_emu(x) ((x) == 0.0 ? 0.0 : ((x) > 0.0 ? 1.0 : -1.0))");
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))");
#endif #endif
} }
...@@ -7,15 +7,13 @@ ...@@ -7,15 +7,13 @@
#ifndef COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_ #ifndef COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_
#define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_ #define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_
#include "compiler/translator/BuiltInFunctionEmulator.h" #include "GLSLANG/ShaderLang.h"
class BuiltInFunctionEmulator;
// //
// This class 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.
// //
class BuiltInFunctionEmulatorGLSL : public BuiltInFunctionEmulator void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType);
{
public:
BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType);
};
#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_ #endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_
...@@ -7,15 +7,10 @@ ...@@ -7,15 +7,10 @@
#ifndef COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_ #ifndef COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
#define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_ #define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
#include "compiler/translator/BuiltInFunctionEmulator.h" #include "GLSLANG/ShaderLang.h"
// class BuiltInFunctionEmulator;
// This class is needed to emulate GLSL functions that don't exist in HLSL.
// void InitBuiltInFunctionEmulatorForHLSL(BuiltInFunctionEmulator *emu);
class BuiltInFunctionEmulatorHLSL : public BuiltInFunctionEmulator
{
public:
BuiltInFunctionEmulatorHLSL();
};
#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_ #endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
...@@ -125,7 +125,7 @@ TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output) ...@@ -125,7 +125,7 @@ TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
maxCallStackDepth(0), maxCallStackDepth(0),
fragmentPrecisionHigh(false), fragmentPrecisionHigh(false),
clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC), clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
builtInFunctionEmulator(type), builtInFunctionEmulator(),
mSourcePath(NULL) mSourcePath(NULL)
{ {
} }
...@@ -265,8 +265,11 @@ TIntermNode *TCompiler::compileTreeImpl(const char* const shaderStrings[], ...@@ -265,8 +265,11 @@ TIntermNode *TCompiler::compileTreeImpl(const char* const shaderStrings[],
} }
// Built-in function emulation needs to happen after validateLimitations pass. // Built-in function emulation needs to happen after validateLimitations pass.
if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)) if (success)
{
initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root); builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
}
// Clamping uniform array bounds needs to happen after validateLimitations pass. // Clamping uniform array bounds needs to happen after validateLimitations pass.
if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)) if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
...@@ -649,7 +652,7 @@ ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const ...@@ -649,7 +652,7 @@ ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
return clampingStrategy; return clampingStrategy;
} }
const BuiltInFunctionEmulatorGLSL& TCompiler::getBuiltInFunctionEmulator() const const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
{ {
return builtInFunctionEmulator; return builtInFunctionEmulator;
} }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
// This should not be included by driver code. // This should not be included by driver code.
// //
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h" #include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/ExtensionBehavior.h" #include "compiler/translator/ExtensionBehavior.h"
#include "compiler/translator/HashNames.h" #include "compiler/translator/HashNames.h"
#include "compiler/translator/InfoSink.h" #include "compiler/translator/InfoSink.h"
...@@ -114,6 +114,8 @@ class TCompiler : public TShHandleBase ...@@ -114,6 +114,8 @@ class TCompiler : public TShHandleBase
bool validateLimitations(TIntermNode* root); bool validateLimitations(TIntermNode* root);
// Collect info for all attribs, uniforms, varyings. // Collect info for all attribs, uniforms, varyings.
void collectVariables(TIntermNode* root); void collectVariables(TIntermNode* root);
// Add emulated functions to the built-in function emulator.
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) {};
// Translate to object code. // Translate to object code.
virtual void translate(TIntermNode *root, int compileOptions) = 0; virtual void translate(TIntermNode *root, int compileOptions) = 0;
// Returns true if, after applying the packing rules in the GLSL 1.017 spec // Returns true if, after applying the packing rules in the GLSL 1.017 spec
...@@ -146,7 +148,7 @@ class TCompiler : public TShHandleBase ...@@ -146,7 +148,7 @@ class TCompiler : public TShHandleBase
const ArrayBoundsClamper& getArrayBoundsClamper() const; const ArrayBoundsClamper& getArrayBoundsClamper() const;
ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
const BuiltInFunctionEmulatorGLSL& getBuiltInFunctionEmulator() const; const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
std::vector<sh::Attribute> attributes; std::vector<sh::Attribute> attributes;
std::vector<sh::Attribute> outputVariables; std::vector<sh::Attribute> outputVariables;
...@@ -179,7 +181,7 @@ class TCompiler : public TShHandleBase ...@@ -179,7 +181,7 @@ class TCompiler : public TShHandleBase
ArrayBoundsClamper arrayBoundsClamper; ArrayBoundsClamper arrayBoundsClamper;
ShArrayIndexClampingStrategy clampingStrategy; ShArrayIndexClampingStrategy clampingStrategy;
BuiltInFunctionEmulatorGLSL builtInFunctionEmulator; BuiltInFunctionEmulator builtInFunctionEmulator;
// Results of compilation. // Results of compilation.
int shaderVersion; int shaderVersion;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "common/angleutils.h" #include "common/angleutils.h"
#include "common/utilities.h" #include "common/utilities.h"
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h" #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
#include "compiler/translator/DetectDiscontinuity.h" #include "compiler/translator/DetectDiscontinuity.h"
#include "compiler/translator/FlagStd140Structs.h" #include "compiler/translator/FlagStd140Structs.h"
...@@ -178,7 +179,8 @@ void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink) ...@@ -178,7 +179,8 @@ void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
RewriteElseBlocks(treeRoot); RewriteElseBlocks(treeRoot);
} }
BuiltInFunctionEmulatorHLSL builtInFunctionEmulator; BuiltInFunctionEmulator builtInFunctionEmulator;
InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot); builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
// Output the body and footer first to determine what has to go in the header // Output the body and footer first to determine what has to go in the header
...@@ -289,7 +291,7 @@ TString OutputHLSL::structInitializerString(int indent, const TStructure &struct ...@@ -289,7 +291,7 @@ TString OutputHLSL::structInitializerString(int indent, const TStructure &struct
return init; return init;
} }
void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator) void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
{ {
TInfoSinkBase &out = getInfoSink(); TInfoSinkBase &out = getInfoSink();
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "compiler/translator/IntermNode.h" #include "compiler/translator/IntermNode.h"
#include "compiler/translator/ParseContext.h" #include "compiler/translator/ParseContext.h"
class BuiltInFunctionEmulatorHLSL; class BuiltInFunctionEmulator;
namespace sh namespace sh
{ {
...@@ -47,7 +47,7 @@ class OutputHLSL : public TIntermTraverser ...@@ -47,7 +47,7 @@ class OutputHLSL : public TIntermTraverser
TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); } TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); }
protected: protected:
void header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator); void header(const BuiltInFunctionEmulator *builtInFunctionEmulator);
// Visit AST nodes and output their code to the body stream // Visit AST nodes and output their code to the body stream
void visitSymbol(TIntermSymbol*); void visitSymbol(TIntermSymbol*);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "compiler/translator/TranslatorESSL.h" #include "compiler/translator/TranslatorESSL.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/EmulatePrecision.h" #include "compiler/translator/EmulatePrecision.h"
#include "compiler/translator/OutputESSL.h" #include "compiler/translator/OutputESSL.h"
#include "angle_gl.h" #include "angle_gl.h"
...@@ -15,6 +16,12 @@ TranslatorESSL::TranslatorESSL(sh::GLenum type, ShShaderSpec spec) ...@@ -15,6 +16,12 @@ TranslatorESSL::TranslatorESSL(sh::GLenum type, ShShaderSpec spec)
{ {
} }
void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions)
{
if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)
InitBuiltInFunctionEmulatorForGLSL(emu, getShaderType());
}
void TranslatorESSL::translate(TIntermNode *root, int) { void TranslatorESSL::translate(TIntermNode *root, int) {
TInfoSinkBase& sink = getInfoSink().obj; TInfoSinkBase& sink = getInfoSink().obj;
......
...@@ -15,6 +15,8 @@ class TranslatorESSL : public TCompiler ...@@ -15,6 +15,8 @@ class TranslatorESSL : public TCompiler
TranslatorESSL(sh::GLenum type, ShShaderSpec spec); TranslatorESSL(sh::GLenum type, ShShaderSpec spec);
protected: protected:
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) override;
virtual void translate(TIntermNode *root, int compileOptions); virtual void translate(TIntermNode *root, int compileOptions);
private: private:
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "compiler/translator/TranslatorGLSL.h" #include "compiler/translator/TranslatorGLSL.h"
#include "angle_gl.h" #include "angle_gl.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/EmulatePrecision.h" #include "compiler/translator/EmulatePrecision.h"
#include "compiler/translator/OutputGLSL.h" #include "compiler/translator/OutputGLSL.h"
#include "compiler/translator/VersionGLSL.h" #include "compiler/translator/VersionGLSL.h"
...@@ -61,6 +62,12 @@ TranslatorGLSL::TranslatorGLSL(sh::GLenum type, ...@@ -61,6 +62,12 @@ TranslatorGLSL::TranslatorGLSL(sh::GLenum type,
: TCompiler(type, spec, output) { : TCompiler(type, spec, output) {
} }
void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions)
{
if (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)
InitBuiltInFunctionEmulatorForGLSL(emu, getShaderType());
}
void TranslatorGLSL::translate(TIntermNode *root, int) { void TranslatorGLSL::translate(TIntermNode *root, int) {
TInfoSinkBase& sink = getInfoSink().obj; TInfoSinkBase& sink = getInfoSink().obj;
......
...@@ -15,6 +15,8 @@ class TranslatorGLSL : public TCompiler ...@@ -15,6 +15,8 @@ class TranslatorGLSL : public TCompiler
TranslatorGLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); TranslatorGLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
protected: protected:
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) override;
virtual void translate(TIntermNode *root, int compileOptions); virtual void translate(TIntermNode *root, int compileOptions);
private: private:
......
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