1 // 2 // Copyright (c) 2011 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 #ifndef COMPILIER_BUILT_IN_FUNCTION_EMULATOR_H_ 8 #define COMPILIER_BUILT_IN_FUNCTION_EMULATOR_H_ 9 10 #include "GLSLANG/ShaderLang.h" 11 12 #include "compiler/InfoSink.h" 13 #include "compiler/intermediate.h" 14 15 // 16 // This class decides which built-in functions need to be replaced with the 17 // emulated ones. 18 // It's only a workaround for OpenGL driver bugs, and isn't needed in general. 19 // 20 class BuiltInFunctionEmulator { 21 public: 22 BuiltInFunctionEmulator(ShShaderType shaderType); 23 // Records that a function is called by the shader and might needs to be 24 // emulated. If the function's group is not in mFunctionGroupFilter, this 25 // becomes an no-op. 26 // Returns true if the function call needs to be replaced with an emulated 27 // one. 28 bool SetFunctionCalled(TOperator op, const TType& param); 29 bool SetFunctionCalled( 30 TOperator op, const TType& param1, const TType& param2); 31 32 // Output function emulation definition. This should be before any other 33 // shader source. 34 void OutputEmulatedFunctionDefinition(TInfoSinkBase& out, bool withPrecision) const; 35 36 void MarkBuiltInFunctionsForEmulation(TIntermNode* root); 37 38 void Cleanup(); 39 40 // "name(" becomes "webgl_name_emu(". 41 static TString GetEmulatedFunctionName(const TString& name); 42 43 private: 44 // 45 // Built-in functions. 46 // 47 enum TBuiltInFunction { 48 TFunctionCos1 = 0, // float cos(float); 49 TFunctionCos2, // vec2 cos(vec2); 50 TFunctionCos3, // vec3 cos(vec3); 51 TFunctionCos4, // vec4 cos(vec4); 52 53 TFunctionDistance1_1, // float distance(float, float); 54 TFunctionDistance2_2, // vec2 distance(vec2, vec2); 55 TFunctionDistance3_3, // vec3 distance(vec3, vec3); 56 TFunctionDistance4_4, // vec4 distance(vec4, vec4); 57 58 TFunctionDot1_1, // float dot(float, float); 59 TFunctionDot2_2, // vec2 dot(vec2, vec2); 60 TFunctionDot3_3, // vec3 dot(vec3, vec3); 61 TFunctionDot4_4, // vec4 dot(vec4, vec4); 62 63 TFunctionLength1, // float length(float); 64 TFunctionLength2, // float length(vec2); 65 TFunctionLength3, // float length(vec3); 66 TFunctionLength4, // float length(vec4); 67 68 TFunctionNormalize1, // float normalize(float); 69 TFunctionNormalize2, // vec2 normalize(vec2); 70 TFunctionNormalize3, // vec3 normalize(vec3); 71 TFunctionNormalize4, // vec4 normalize(vec4); 72 73 TFunctionReflect1_1, // float reflect(float, float); 74 TFunctionReflect2_2, // vec2 reflect(vec2, vec2); 75 TFunctionReflect3_3, // vec3 reflect(vec3, vec3); 76 TFunctionReflect4_4, // vec4 reflect(vec4, vec4); 77 78 TFunctionUnknown 79 }; 80 81 TBuiltInFunction IdentifyFunction(TOperator op, const TType& param); 82 TBuiltInFunction IdentifyFunction( 83 TOperator op, const TType& param1, const TType& param2); 84 85 bool SetFunctionCalled(TBuiltInFunction function); 86 87 std::vector<TBuiltInFunction> mFunctions; 88 89 const bool* mFunctionMask; // a boolean flag for each function. 90 const char** mFunctionSource; 91 }; 92 93 #endif // COMPILIER_BUILT_IN_FUNCTION_EMULATOR_H_ 94