blob: c66297dcec0b10eae1b06b3180c5f7646dc038b0 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capens6407fe82015-02-10 16:27:49 -05007#ifndef _COMPILER_INCLUDED_
8#define _COMPILER_INCLUDED_
John Bauman66b8ab22014-05-06 15:57:45 -04009
Nicolas Capenscc863da2015-01-21 15:50:55 -050010#include "ExtensionBehavior.h"
11#include "InfoSink.h"
12#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040013
John Bauman66b8ab22014-05-06 15:57:45 -040014//
Nicolas Capens6407fe82015-02-10 16:27:49 -050015// 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//
20enum ShShaderType
21{
22 SH_FRAGMENT_SHADER = 0x8B30,
23 SH_VERTEX_SHADER = 0x8B31
24};
25
26enum 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
40enum 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//
55struct 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 Bauman66b8ab22014-05-06 15:57:45 -040079// The base class for the machine dependent compiler to derive from
80// for managing object code from the compile.
81//
Nicolas Capens7a8ccc42015-02-10 15:42:31 -050082class TCompiler
83{
John Bauman66b8ab22014-05-06 15:57:45 -040084public:
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 Bauman66b8ab22014-05-06 15:57:45 -040096
97protected:
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 Baumand4ae8632014-05-06 16:18:33 -0400104 // Return true if function recursion is detected or call depth exceeded.
105 bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400106 // Returns true if the given shader does not exceed the minimum
107 // functionality mandated in GLSL 1.0 spec Appendix A.
John Baumand4ae8632014-05-06 16:18:33 -0400108 bool validateLimitations(TIntermNode *root);
John Bauman66b8ab22014-05-06 15:57:45 -0400109 // Translate to object code.
Nicolas Capens014b9a62014-10-15 10:28:29 -0400110 virtual bool translate(TIntermNode *root) = 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400111 // Get built-in extensions with default behavior.
112 const TExtensionBehavior& getExtensionBehavior() const;
113
114private:
115 ShShaderType shaderType;
116 ShShaderSpec shaderSpec;
117
John Baumand4ae8632014-05-06 16:18:33 -0400118 unsigned int maxCallStackDepth;
119
John Bauman66b8ab22014-05-06 15:57:45 -0400120 // 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 Capens7a8ccc42015-02-10 15:42:31 -0500128
129 // Memory allocator. Allocates and tracks memory required by the compiler.
130 // Deallocates all memory when compiler is destructed.
131 TPoolAllocator allocator;
John Bauman66b8ab22014-05-06 15:57:45 -0400132};
133
Nicolas Capens6407fe82015-02-10 16:27:49 -0500134bool InitCompilerGlobals();
135void FreeCompilerGlobals();
John Bauman66b8ab22014-05-06 15:57:45 -0400136
Nicolas Capens6407fe82015-02-10 16:27:49 -0500137#endif // _COMPILER_INCLUDED_