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
BuiltInFunctionEmulator::BuiltInFunctionEmulator()
{}
void BuiltInFunctionEmulator::AddEmulatedFunction(
void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param,
const char* emulatedFunctionDefinition)
{
......@@ -108,7 +108,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction(
std::string(emulatedFunctionDefinition);
}
void BuiltInFunctionEmulator::AddEmulatedFunction(
void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param1, const TType& param2,
const char* emulatedFunctionDefinition)
{
......@@ -116,7 +116,7 @@ void BuiltInFunctionEmulator::AddEmulatedFunction(
std::string(emulatedFunctionDefinition);
}
void BuiltInFunctionEmulator::AddEmulatedFunction(
void BuiltInFunctionEmulator::addEmulatedFunction(
TOperator op, const TType& param1, const TType& param2, const TType& param3,
const char* emulatedFunctionDefinition)
{
......@@ -174,6 +174,9 @@ void BuiltInFunctionEmulator::MarkBuiltInFunctionsForEmulation(
{
ASSERT(root);
if (mEmulatedFunctions.empty())
return;
BuiltInFunctionEmulationMarker marker(*this);
root->traverse(&marker);
}
......
......@@ -19,6 +19,8 @@
class BuiltInFunctionEmulator
{
public:
BuiltInFunctionEmulator();
void MarkBuiltInFunctionsForEmulation(TIntermNode* root);
void Cleanup();
......@@ -32,13 +34,10 @@ class BuiltInFunctionEmulator
// shader source.
void OutputEmulatedFunctions(TInfoSinkBase& out) const;
protected:
BuiltInFunctionEmulator();
// Add functions that need to be emulated.
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 TType& param3, 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 TType& param3, const char* emulatedFunctionDefinition);
private:
class BuiltInFunctionEmulationMarker;
......
......@@ -5,11 +5,11 @@
//
#include "angle_gl.h"
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/SymbolTable.h"
BuiltInFunctionEmulatorGLSL::BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType)
: BuiltInFunctionEmulator()
void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType)
{
#if defined(__APPLE__)
// we use macros here instead of function definitions to work around more GLSL
......@@ -25,15 +25,15 @@ BuiltInFunctionEmulatorGLSL::BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType)
if (shaderType == GL_FRAGMENT_SHADER)
{
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); }");
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, float1, "webgl_emu_precision float webgl_cos_emu(webgl_emu_precision float a) { return cos(a); }");
emu->addEmulatedFunction(EOpCos, float2, "webgl_emu_precision vec2 webgl_cos_emu(webgl_emu_precision vec2 a) { return cos(a); }");
emu->addEmulatedFunction(EOpCos, float3, "webgl_emu_precision vec3 webgl_cos_emu(webgl_emu_precision vec3 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))");
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))");
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(EOpDistance, float1, float1, "#define webgl_distance_emu(x, y) ((x) >= (y) ? (x) - (y) : (y) - (x))");
emu->addEmulatedFunction(EOpDot, float1, float1, "#define webgl_dot_emu(x, y) ((x) * (y))");
emu->addEmulatedFunction(EOpLength, float1, "#define webgl_length_emu(x) ((x) >= 0.0 ? (x) : -(x))");
emu->addEmulatedFunction(EOpNormalize, float1, "#define webgl_normalize_emu(x) ((x) == 0.0 ? 0.0 : ((x) > 0.0 ? 1.0 : -1.0))");
emu->addEmulatedFunction(EOpReflect, float1, float1, "#define webgl_reflect_emu(I, N) ((I) - 2.0 * (N) * (I) * (N))");
#endif
}
......@@ -7,15 +7,13 @@
#ifndef 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
{
public:
BuiltInFunctionEmulatorGLSL(sh::GLenum shaderType);
};
void InitBuiltInFunctionEmulatorForGLSL(BuiltInFunctionEmulator *emu, sh::GLenum shaderType);
#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORGLSL_H_
......@@ -7,15 +7,10 @@
#ifndef COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
#define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "GLSLANG/ShaderLang.h"
//
// This class is needed to emulate GLSL functions that don't exist in HLSL.
//
class BuiltInFunctionEmulatorHLSL : public BuiltInFunctionEmulator
{
public:
BuiltInFunctionEmulatorHLSL();
};
class BuiltInFunctionEmulator;
void InitBuiltInFunctionEmulatorForHLSL(BuiltInFunctionEmulator *emu);
#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATORHLSL_H_
......@@ -125,7 +125,7 @@ TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
maxCallStackDepth(0),
fragmentPrecisionHigh(false),
clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
builtInFunctionEmulator(type),
builtInFunctionEmulator(),
mSourcePath(NULL)
{
}
......@@ -265,8 +265,11 @@ TIntermNode *TCompiler::compileTreeImpl(const char* const shaderStrings[],
}
// 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);
}
// Clamping uniform array bounds needs to happen after validateLimitations pass.
if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
......@@ -649,7 +652,7 @@ ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
return clampingStrategy;
}
const BuiltInFunctionEmulatorGLSL& TCompiler::getBuiltInFunctionEmulator() const
const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
{
return builtInFunctionEmulator;
}
......
......@@ -14,7 +14,7 @@
// 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/HashNames.h"
#include "compiler/translator/InfoSink.h"
......@@ -114,6 +114,8 @@ class TCompiler : public TShHandleBase
bool validateLimitations(TIntermNode* root);
// Collect info for all attribs, uniforms, varyings.
void collectVariables(TIntermNode* root);
// Add emulated functions to the built-in function emulator.
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) {};
// Translate to object code.
virtual void translate(TIntermNode *root, int compileOptions) = 0;
// Returns true if, after applying the packing rules in the GLSL 1.017 spec
......@@ -146,7 +148,7 @@ class TCompiler : public TShHandleBase
const ArrayBoundsClamper& getArrayBoundsClamper() const;
ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
const BuiltInFunctionEmulatorGLSL& getBuiltInFunctionEmulator() const;
const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
std::vector<sh::Attribute> attributes;
std::vector<sh::Attribute> outputVariables;
......@@ -179,7 +181,7 @@ class TCompiler : public TShHandleBase
ArrayBoundsClamper arrayBoundsClamper;
ShArrayIndexClampingStrategy clampingStrategy;
BuiltInFunctionEmulatorGLSL builtInFunctionEmulator;
BuiltInFunctionEmulator builtInFunctionEmulator;
// Results of compilation.
int shaderVersion;
......
......@@ -12,6 +12,7 @@
#include "common/angleutils.h"
#include "common/utilities.h"
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
#include "compiler/translator/DetectDiscontinuity.h"
#include "compiler/translator/FlagStd140Structs.h"
......@@ -178,7 +179,8 @@ void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
RewriteElseBlocks(treeRoot);
}
BuiltInFunctionEmulatorHLSL builtInFunctionEmulator;
BuiltInFunctionEmulator builtInFunctionEmulator;
InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
// 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
return init;
}
void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator)
void OutputHLSL::header(const BuiltInFunctionEmulator *builtInFunctionEmulator)
{
TInfoSinkBase &out = getInfoSink();
......
......@@ -16,7 +16,7 @@
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/ParseContext.h"
class BuiltInFunctionEmulatorHLSL;
class BuiltInFunctionEmulator;
namespace sh
{
......@@ -47,7 +47,7 @@ class OutputHLSL : public TIntermTraverser
TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); }
protected:
void header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator);
void header(const BuiltInFunctionEmulator *builtInFunctionEmulator);
// Visit AST nodes and output their code to the body stream
void visitSymbol(TIntermSymbol*);
......
......@@ -6,6 +6,7 @@
#include "compiler/translator/TranslatorESSL.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/EmulatePrecision.h"
#include "compiler/translator/OutputESSL.h"
#include "angle_gl.h"
......@@ -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) {
TInfoSinkBase& sink = getInfoSink().obj;
......
......@@ -15,6 +15,8 @@ class TranslatorESSL : public TCompiler
TranslatorESSL(sh::GLenum type, ShShaderSpec spec);
protected:
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) override;
virtual void translate(TIntermNode *root, int compileOptions);
private:
......
......@@ -7,6 +7,7 @@
#include "compiler/translator/TranslatorGLSL.h"
#include "angle_gl.h"
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "compiler/translator/EmulatePrecision.h"
#include "compiler/translator/OutputGLSL.h"
#include "compiler/translator/VersionGLSL.h"
......@@ -61,6 +62,12 @@ TranslatorGLSL::TranslatorGLSL(sh::GLenum type,
: 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) {
TInfoSinkBase& sink = getInfoSink().obj;
......
......@@ -15,6 +15,8 @@ class TranslatorGLSL : public TCompiler
TranslatorGLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
protected:
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) override;
virtual void translate(TIntermNode *root, int compileOptions);
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