blob: ae0407139ba03f6739a25d9cd4ea6e96604a138f [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// 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
John Bauman66b8ab22014-05-06 15:57:45 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// 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.
John Bauman66b8ab22014-05-06 15:57:45 -040014
Nicolas Capens6407fe82015-02-10 16:27:49 -050015#ifndef _COMPILER_INCLUDED_
16#define _COMPILER_INCLUDED_
John Bauman66b8ab22014-05-06 15:57:45 -040017
Nicolas Capenscc863da2015-01-21 15:50:55 -050018#include "ExtensionBehavior.h"
19#include "InfoSink.h"
20#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040021
Nicolas Capens6407fe82015-02-10 16:27:49 -050022enum 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//
37struct ShBuiltInResources
38{
39 ShBuiltInResources();
40
Nicolas Capens0bac2852016-05-07 06:09:58 -040041 // 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;
Nicolas Capens6407fe82015-02-10 16:27:49 -050054
Nicolas Capens0bac2852016-05-07 06:09:58 -040055 // Extensions.
56 // Set to 1 to enable the extension, else 0.
57 int OES_standard_derivatives;
Nicolas Capens6407fe82015-02-10 16:27:49 -050058 int OES_fragment_precision_high;
Nicolas Capens0bac2852016-05-07 06:09:58 -040059 int OES_EGL_image_external;
Nicolas Capensc66f0e32016-04-18 14:34:24 -040060 int EXT_draw_buffers;
Alexis Hetu46768622018-01-16 22:09:28 -050061 int ARB_texture_rectangle;
Nicolas Capens6407fe82015-02-10 16:27:49 -050062
Nicolas Capens0bac2852016-05-07 06:09:58 -040063 unsigned int MaxCallStackDepth;
Nicolas Capens6407fe82015-02-10 16:27:49 -050064};
65
Nicolas Capens08ca3c62015-02-13 16:06:45 -050066typedef unsigned int GLenum;
67#define GL_FRAGMENT_SHADER 0x8B30
68#define GL_VERTEX_SHADER 0x8B31
69
Alexis Hetu46768622018-01-16 22:09:28 -050070// 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#endif // GL_ARB_texture_rectangle
87
Nicolas Capens6407fe82015-02-10 16:27:49 -050088//
John Bauman66b8ab22014-05-06 15:57:45 -040089// The base class for the machine dependent compiler to derive from
90// for managing object code from the compile.
91//
Nicolas Capens7a8ccc42015-02-10 15:42:31 -050092class TCompiler
93{
John Bauman66b8ab22014-05-06 15:57:45 -040094public:
Nicolas Capens0bac2852016-05-07 06:09:58 -040095 TCompiler(GLenum shaderType);
96 virtual ~TCompiler();
97 virtual TCompiler* getAsCompiler() { return this; }
John Bauman66b8ab22014-05-06 15:57:45 -040098
Nicolas Capens0bac2852016-05-07 06:09:58 -040099 bool Init(const ShBuiltInResources& resources);
100 bool compile(const char* const shaderStrings[],
101 const int numStrings,
102 int compileOptions);
John Bauman66b8ab22014-05-06 15:57:45 -0400103
Nicolas Capens0bac2852016-05-07 06:09:58 -0400104 // Get results of the last compilation.
105 int getShaderVersion() const { return shaderVersion; }
106 TInfoSink& getInfoSink() { return infoSink; }
John Bauman66b8ab22014-05-06 15:57:45 -0400107
108protected:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400109 GLenum getShaderType() const { return shaderType; }
110 // Initialize symbol-table with built-in symbols.
111 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
112 // Clears the results from the previous compilation.
113 void clearResults();
114 // Return true if function recursion is detected or call depth exceeded.
115 bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink);
116 // Returns true if the given shader does not exceed the minimum
117 // functionality mandated in GLSL 1.0 spec Appendix A.
118 bool validateLimitations(TIntermNode *root);
119 // Translate to object code.
120 virtual bool translate(TIntermNode *root) = 0;
121 // Get built-in extensions with default behavior.
122 const TExtensionBehavior& getExtensionBehavior() const;
John Bauman66b8ab22014-05-06 15:57:45 -0400123
124private:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400125 GLenum shaderType;
John Bauman66b8ab22014-05-06 15:57:45 -0400126
Nicolas Capens0bac2852016-05-07 06:09:58 -0400127 unsigned int maxCallStackDepth;
John Baumand4ae8632014-05-06 16:18:33 -0400128
Nicolas Capens0bac2852016-05-07 06:09:58 -0400129 // Built-in symbol table for the given language, spec, and resources.
130 // It is preserved from compile-to-compile.
131 TSymbolTable symbolTable;
132 // Built-in extensions with default behavior.
133 TExtensionBehavior extensionBehavior;
John Bauman66b8ab22014-05-06 15:57:45 -0400134
Nicolas Capens0bac2852016-05-07 06:09:58 -0400135 // Results of compilation.
136 int shaderVersion;
137 TInfoSink infoSink; // Output sink.
Nicolas Capens7a8ccc42015-02-10 15:42:31 -0500138
Nicolas Capens0bac2852016-05-07 06:09:58 -0400139 // Memory allocator. Allocates and tracks memory required by the compiler.
140 // Deallocates all memory when compiler is destructed.
141 TPoolAllocator allocator;
John Bauman66b8ab22014-05-06 15:57:45 -0400142};
143
Nicolas Capens6407fe82015-02-10 16:27:49 -0500144bool InitCompilerGlobals();
145void FreeCompilerGlobals();
John Bauman66b8ab22014-05-06 15:57:45 -0400146
Nicolas Capens6407fe82015-02-10 16:27:49 -0500147#endif // _COMPILER_INCLUDED_