John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame^] | 7 | #ifndef _COMPILER_INCLUDED_ |
| 8 | #define _COMPILER_INCLUDED_ |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 9 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 10 | #include "ExtensionBehavior.h" |
| 11 | #include "InfoSink.h" |
| 12 | #include "SymbolTable.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 13 | |
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 | // The names of the following enums have been derived by replacing GL prefix |
| 16 | // with SH. For example, SH_INFO_LOG_LENGTH is equivalent to GL_INFO_LOG_LENGTH. |
| 17 | // The enum values are also equal to the values of their GL counterpart. This |
| 18 | // is done to make it easier for applications to use the shader library. |
| 19 | // |
| 20 | enum ShShaderType |
| 21 | { |
| 22 | SH_FRAGMENT_SHADER = 0x8B30, |
| 23 | SH_VERTEX_SHADER = 0x8B31 |
| 24 | }; |
| 25 | |
| 26 | enum ShShaderSpec |
| 27 | { |
| 28 | SH_GLES2_SPEC = 0x8B40, |
| 29 | SH_WEBGL_SPEC = 0x8B41 |
| 30 | }; |
| 31 | |
| 32 | enum ShShaderInfo |
| 33 | { |
| 34 | SH_INFO_LOG_LENGTH = 0x8B84, |
| 35 | SH_OBJECT_CODE_LENGTH = 0x8B88, // GL_SHADER_SOURCE_LENGTH |
| 36 | SH_ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87, |
| 37 | SH_ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A |
| 38 | }; |
| 39 | |
| 40 | enum ShCompileOptions |
| 41 | { |
| 42 | SH_VALIDATE = 0, |
| 43 | SH_VALIDATE_LOOP_INDEXING = 0x0001, |
| 44 | SH_INTERMEDIATE_TREE = 0x0002, |
| 45 | SH_OBJECT_CODE = 0x0004, |
| 46 | SH_ATTRIBUTES_UNIFORMS = 0x0008, |
| 47 | SH_LINE_DIRECTIVES = 0x0010, |
| 48 | SH_SOURCE_PATH = 0x0020 |
| 49 | }; |
| 50 | |
| 51 | // |
| 52 | // Implementation dependent built-in resources (constants and extensions). |
| 53 | // The names for these resources has been obtained by stripping gl_/GL_. |
| 54 | // |
| 55 | struct ShBuiltInResources |
| 56 | { |
| 57 | ShBuiltInResources(); |
| 58 | |
| 59 | // Constants. |
| 60 | int MaxVertexAttribs; |
| 61 | int MaxVertexUniformVectors; |
| 62 | int MaxVaryingVectors; |
| 63 | int MaxVertexTextureImageUnits; |
| 64 | int MaxCombinedTextureImageUnits; |
| 65 | int MaxTextureImageUnits; |
| 66 | int MaxFragmentUniformVectors; |
| 67 | int MaxDrawBuffers; |
| 68 | |
| 69 | // Extensions. |
| 70 | // Set to 1 to enable the extension, else 0. |
| 71 | int OES_standard_derivatives; |
| 72 | int OES_fragment_precision_high; |
| 73 | int OES_EGL_image_external; |
| 74 | |
| 75 | unsigned int MaxCallStackDepth; |
| 76 | }; |
| 77 | |
| 78 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 79 | // The base class for the machine dependent compiler to derive from |
| 80 | // for managing object code from the compile. |
| 81 | // |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 82 | class TCompiler |
| 83 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 84 | public: |
| 85 | TCompiler(ShShaderType type, ShShaderSpec spec); |
| 86 | virtual ~TCompiler(); |
| 87 | virtual TCompiler* getAsCompiler() { return this; } |
| 88 | |
| 89 | bool Init(const ShBuiltInResources& resources); |
| 90 | bool compile(const char* const shaderStrings[], |
| 91 | const int numStrings, |
| 92 | int compileOptions); |
| 93 | |
| 94 | // Get results of the last compilation. |
| 95 | TInfoSink& getInfoSink() { return infoSink; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 96 | |
| 97 | protected: |
| 98 | ShShaderType getShaderType() const { return shaderType; } |
| 99 | ShShaderSpec getShaderSpec() const { return shaderSpec; } |
| 100 | // Initialize symbol-table with built-in symbols. |
| 101 | bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); |
| 102 | // Clears the results from the previous compilation. |
| 103 | void clearResults(); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 104 | // Return true if function recursion is detected or call depth exceeded. |
| 105 | bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 106 | // Returns true if the given shader does not exceed the minimum |
| 107 | // functionality mandated in GLSL 1.0 spec Appendix A. |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 108 | bool validateLimitations(TIntermNode *root); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 109 | // Translate to object code. |
Nicolas Capens | 014b9a6 | 2014-10-15 10:28:29 -0400 | [diff] [blame] | 110 | virtual bool translate(TIntermNode *root) = 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 111 | // Get built-in extensions with default behavior. |
| 112 | const TExtensionBehavior& getExtensionBehavior() const; |
| 113 | |
| 114 | private: |
| 115 | ShShaderType shaderType; |
| 116 | ShShaderSpec shaderSpec; |
| 117 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 118 | unsigned int maxCallStackDepth; |
| 119 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 120 | // Built-in symbol table for the given language, spec, and resources. |
| 121 | // It is preserved from compile-to-compile. |
| 122 | TSymbolTable symbolTable; |
| 123 | // Built-in extensions with default behavior. |
| 124 | TExtensionBehavior extensionBehavior; |
| 125 | |
| 126 | // Results of compilation. |
| 127 | TInfoSink infoSink; // Output sink. |
Nicolas Capens | 7a8ccc4 | 2015-02-10 15:42:31 -0500 | [diff] [blame] | 128 | |
| 129 | // Memory allocator. Allocates and tracks memory required by the compiler. |
| 130 | // Deallocates all memory when compiler is destructed. |
| 131 | TPoolAllocator allocator; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame^] | 134 | bool InitCompilerGlobals(); |
| 135 | void FreeCompilerGlobals(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 136 | |
Nicolas Capens | 6407fe8 | 2015-02-10 16:27:49 -0500 | [diff] [blame^] | 137 | #endif // _COMPILER_INCLUDED_ |