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 COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATOR_H_
8#define COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATOR_H_
9
10#include "compiler/translator/InfoSink.h"
11#include "compiler/translator/IntermNode.h"
12
13//
14// This class decides which built-in functions need to be replaced with the
15// emulated ones.
16// It can be used to work around driver bugs or implement functions that are
17// not natively implemented on a specific platform.
18//
19class BuiltInFunctionEmulator
20{
21 public:
22 BuiltInFunctionEmulator();
23
24 void MarkBuiltInFunctionsForEmulation(TIntermNode* root);
25
26 void Cleanup();
27
28 // "name(" becomes "webgl_name_emu(".
29 static TString GetEmulatedFunctionName(const TString& name);
30
31 bool IsOutputEmpty() const;
32
33 // Output function emulation definition. This should be before any other
34 // shader source.
35 void OutputEmulatedFunctions(TInfoSinkBase& out) const;
36
37 // Add functions that need to be emulated.
38 void addEmulatedFunction(TOperator op, const TType& param, const char* emulatedFunctionDefinition);
39 void addEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const char* emulatedFunctionDefinition);
40 void addEmulatedFunction(TOperator op, const TType& param1, const TType& param2, const TType& param3, const char* emulatedFunctionDefinition);
41
42 private:
43 class BuiltInFunctionEmulationMarker;
44
45 // Records that a function is called by the shader and might need to be
46 // emulated. If the function is not in mEmulatedFunctions, this becomes a
47 // no-op. Returns true if the function call needs to be replaced with an
48 // emulated one.
49 bool SetFunctionCalled(TOperator op, const TType& param);
50 bool SetFunctionCalled(
51 TOperator op, const TType& param1, const TType& param2);
52 bool SetFunctionCalled(
53 TOperator op, const TType& param1, const TType& param2, const TType& param3);
54
55 class FunctionId {
56 public:
57 FunctionId(TOperator op, const TType& param);
58 FunctionId(TOperator op, const TType& param1, const TType& param2);
59 FunctionId(TOperator op, const TType& param1, const TType& param2, const TType& param3);
60
61 bool operator==(const FunctionId& other) const;
62 bool operator<(const FunctionId& other) const;
63 private:
64 TOperator mOp;
65 TType mParam1;
66 TType mParam2;
67 TType mParam3;
68 };
69
70 bool SetFunctionCalled(const FunctionId& functionId);
71
72 // Map from function id to emulated function definition
73 std::map<FunctionId, std::string> mEmulatedFunctions;
74
75 // Called function ids
76 std::vector<FunctionId> mFunctions;
77};
78
79#endif // COMPILER_TRANSLATOR_BUILTINFUNCTIONEMULATOR_H_
80