1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _COMPILER_INCLUDED_
16 #define _COMPILER_INCLUDED_
17 
18 #include "ExtensionBehavior.h"
19 #include "InfoSink.h"
20 #include "SymbolTable.h"
21 
22 enum ShCompileOptions
23 {
24   SH_VALIDATE                = 0,
25   SH_VALIDATE_LOOP_INDEXING  = 0x0001,
26   SH_INTERMEDIATE_TREE       = 0x0002,
27   SH_OBJECT_CODE             = 0x0004,
28   SH_ATTRIBUTES_UNIFORMS     = 0x0008,
29   SH_LINE_DIRECTIVES         = 0x0010,
30   SH_SOURCE_PATH             = 0x0020
31 };
32 
33 //
34 // Implementation dependent built-in resources (constants and extensions).
35 // The names for these resources has been obtained by stripping gl_/GL_.
36 //
37 struct ShBuiltInResources
38 {
39 	ShBuiltInResources();
40 
41 	// Constants.
42 	int MaxVertexAttribs;
43 	int MaxVertexUniformVectors;
44 	int MaxVaryingVectors;
45 	int MaxVertexTextureImageUnits;
46 	int MaxCombinedTextureImageUnits;
47 	int MaxTextureImageUnits;
48 	int MaxFragmentUniformVectors;
49 	int MaxDrawBuffers;
50 	int MaxVertexOutputVectors;
51 	int MaxFragmentInputVectors;
52 	int MinProgramTexelOffset;
53 	int MaxProgramTexelOffset;
54 
55 	// Extensions.
56 	// Set to 1 to enable the extension, else 0.
57 	int OES_standard_derivatives;
58 	int OES_fragment_precision_high;
59 	int OES_EGL_image_external;
60 	int EXT_draw_buffers;
61 	int ARB_texture_rectangle;
62 
63 	unsigned int MaxCallStackDepth;
64 };
65 
66 typedef unsigned int GLenum;
67 #define GL_FRAGMENT_SHADER                0x8B30
68 #define GL_VERTEX_SHADER                  0x8B31
69 
70 // Note: GL_ARB_texture_rectangle is part of gl2extchromium.h in the Chromium repo
71 // GL_ARB_texture_rectangle
72 #ifndef GL_ARB_texture_rectangle
73 #define GL_ARB_texture_rectangle 1
74 
75 #ifndef GL_SAMPLER_2D_RECT_ARB
76 #define GL_SAMPLER_2D_RECT_ARB 0x8B63
77 #endif
78 
79 #ifndef GL_TEXTURE_BINDING_RECTANGLE_ARB
80 #define GL_TEXTURE_BINDING_RECTANGLE_ARB 0x84F6
81 #endif
82 
83 #ifndef GL_TEXTURE_RECTANGLE_ARB
84 #define GL_TEXTURE_RECTANGLE_ARB 0x84F5
85 #endif
86 
87 #ifndef GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB
88 #define GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB 0x84F8
89 #endif
90 
91 #endif  // GL_ARB_texture_rectangle
92 
93 //
94 // The base class for the machine dependent compiler to derive from
95 // for managing object code from the compile.
96 //
97 class TCompiler
98 {
99 public:
100 	TCompiler(GLenum shaderType);
101 	virtual ~TCompiler();
getAsCompiler()102 	virtual TCompiler* getAsCompiler() { return this; }
103 
104 	bool Init(const ShBuiltInResources& resources);
105 	bool compile(const char* const shaderStrings[],
106 	             const int numStrings,
107 	             int compileOptions);
108 
109 	// Get results of the last compilation.
getShaderVersion()110 	int getShaderVersion() const { return shaderVersion; }
getInfoSink()111 	TInfoSink& getInfoSink() { return infoSink; }
112 
113 protected:
getShaderType()114 	GLenum getShaderType() const { return shaderType; }
115 	// Initialize symbol-table with built-in symbols.
116 	bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
117 	// Clears the results from the previous compilation.
118 	void clearResults();
119 	// Return true if function recursion is detected or call depth exceeded.
120 	bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink);
121 	// Returns true if the given shader does not exceed the minimum
122 	// functionality mandated in GLSL 1.0 spec Appendix A.
123 	bool validateLimitations(TIntermNode *root);
124 	// Translate to object code.
125 	virtual bool translate(TIntermNode *root) = 0;
126 	// Get built-in extensions with default behavior.
127 	const TExtensionBehavior& getExtensionBehavior() const;
128 
129 private:
130 	GLenum shaderType;
131 
132 	unsigned int maxCallStackDepth;
133 
134 	// Built-in symbol table for the given language, spec, and resources.
135 	// It is preserved from compile-to-compile.
136 	TSymbolTable symbolTable;
137 	// Built-in extensions with default behavior.
138 	TExtensionBehavior extensionBehavior;
139 
140 	// Results of compilation.
141 	int shaderVersion;
142 	TInfoSink infoSink;  // Output sink.
143 
144 	// Memory allocator. Allocates and tracks memory required by the compiler.
145 	// Deallocates all memory when compiler is destructed.
146 	TPoolAllocator allocator;
147 };
148 
149 bool InitCompilerGlobals();
150 void FreeCompilerGlobals();
151 
152 #endif // _COMPILER_INCLUDED_
153