Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 14 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 15 | #ifndef _COMPILER_INCLUDED_ |
| 16 | #define _COMPILER_INCLUDED_ |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 17 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 18 | #include "ExtensionBehavior.h" |
| 19 | #include "InfoSink.h" |
| 20 | #include "SymbolTable.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 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 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 54 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 55 | // Extensions. |
| 56 | // Set to 1 to enable the extension, else 0. |
| 57 | int OES_standard_derivatives; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 58 | int OES_fragment_precision_high; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 59 | int OES_EGL_image_external; |
Nicolas Capens | c66f0e3 | 2016-04-18 14:34:24 -0400 | [diff] [blame] | 60 | int EXT_draw_buffers; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 61 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 62 | unsigned int MaxCallStackDepth; |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 63 | }; |
| 64 | |
Nicolas Capens | 08ca3c6 | 2015-02-13 16:06:45 -0500 | [diff] [blame] | 65 | typedef unsigned int GLenum; |
| 66 | #define GL_FRAGMENT_SHADER 0x8B30 |
| 67 | #define GL_VERTEX_SHADER 0x8B31 |
| 68 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 69 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 70 | // The base class for the machine dependent compiler to derive from |
| 71 | // for managing object code from the compile. |
| 72 | // |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 73 | class TCompiler |
| 74 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 75 | public: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 76 | TCompiler(GLenum shaderType); |
| 77 | virtual ~TCompiler(); |
| 78 | virtual TCompiler* getAsCompiler() { return this; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 79 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 80 | bool Init(const ShBuiltInResources& resources); |
| 81 | bool compile(const char* const shaderStrings[], |
| 82 | const int numStrings, |
| 83 | int compileOptions); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 84 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 85 | // Get results of the last compilation. |
| 86 | int getShaderVersion() const { return shaderVersion; } |
| 87 | TInfoSink& getInfoSink() { return infoSink; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 88 | |
| 89 | protected: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 90 | GLenum getShaderType() const { return shaderType; } |
| 91 | // Initialize symbol-table with built-in symbols. |
| 92 | bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); |
| 93 | // Clears the results from the previous compilation. |
| 94 | void clearResults(); |
| 95 | // Return true if function recursion is detected or call depth exceeded. |
| 96 | bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink); |
| 97 | // Returns true if the given shader does not exceed the minimum |
| 98 | // functionality mandated in GLSL 1.0 spec Appendix A. |
| 99 | bool validateLimitations(TIntermNode *root); |
| 100 | // Translate to object code. |
| 101 | virtual bool translate(TIntermNode *root) = 0; |
| 102 | // Get built-in extensions with default behavior. |
| 103 | const TExtensionBehavior& getExtensionBehavior() const; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 104 | |
| 105 | private: |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 106 | GLenum shaderType; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 107 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 108 | unsigned int maxCallStackDepth; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 109 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 110 | // Built-in symbol table for the given language, spec, and resources. |
| 111 | // It is preserved from compile-to-compile. |
| 112 | TSymbolTable symbolTable; |
| 113 | // Built-in extensions with default behavior. |
| 114 | TExtensionBehavior extensionBehavior; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 115 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 116 | // Results of compilation. |
| 117 | int shaderVersion; |
| 118 | TInfoSink infoSink; // Output sink. |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 119 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 120 | // Memory allocator. Allocates and tracks memory required by the compiler. |
| 121 | // Deallocates all memory when compiler is destructed. |
| 122 | TPoolAllocator allocator; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 125 | bool InitCompilerGlobals(); |
| 126 | void FreeCompilerGlobals(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 127 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame] | 128 | #endif // _COMPILER_INCLUDED_ |