blob: 7e9a7c35770264a6ff491839f5c5fa79bbe899d5 [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.
Nicolas Capensb28964b2015-02-10 15:23:06 -050095 int getShaderVersion() const { return shaderVersion; }
John Bauman66b8ab22014-05-06 15:57:45 -040096 TInfoSink& getInfoSink() { return infoSink; }
John Bauman66b8ab22014-05-06 15:57:45 -040097
98protected:
99 ShShaderType getShaderType() const { return shaderType; }
100 ShShaderSpec getShaderSpec() const { return shaderSpec; }
101 // Initialize symbol-table with built-in symbols.
102 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
103 // Clears the results from the previous compilation.
104 void clearResults();
John Baumand4ae8632014-05-06 16:18:33 -0400105 // Return true if function recursion is detected or call depth exceeded.
106 bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400107 // Returns true if the given shader does not exceed the minimum
108 // functionality mandated in GLSL 1.0 spec Appendix A.
John Baumand4ae8632014-05-06 16:18:33 -0400109 bool validateLimitations(TIntermNode *root);
John Bauman66b8ab22014-05-06 15:57:45 -0400110 // Translate to object code.
Nicolas Capens014b9a62014-10-15 10:28:29 -0400111 virtual bool translate(TIntermNode *root) = 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400112 // Get built-in extensions with default behavior.
113 const TExtensionBehavior& getExtensionBehavior() const;
114
115private:
116 ShShaderType shaderType;
117 ShShaderSpec shaderSpec;
118
John Baumand4ae8632014-05-06 16:18:33 -0400119 unsigned int maxCallStackDepth;
120
John Bauman66b8ab22014-05-06 15:57:45 -0400121 // Built-in symbol table for the given language, spec, and resources.
122 // It is preserved from compile-to-compile.
123 TSymbolTable symbolTable;
124 // Built-in extensions with default behavior.
125 TExtensionBehavior extensionBehavior;
126
127 // Results of compilation.
Nicolas Capensb28964b2015-02-10 15:23:06 -0500128 int shaderVersion;
John Bauman66b8ab22014-05-06 15:57:45 -0400129 TInfoSink infoSink; // Output sink.
Nicolas Capens7a8ccc42015-02-10 15:42:31 -0500130
131 // Memory allocator. Allocates and tracks memory required by the compiler.
132 // Deallocates all memory when compiler is destructed.
133 TPoolAllocator allocator;
John Bauman66b8ab22014-05-06 15:57:45 -0400134};
135
Nicolas Capens6407fe82015-02-10 16:27:49 -0500136bool InitCompilerGlobals();
137void FreeCompilerGlobals();
John Bauman66b8ab22014-05-06 15:57:45 -0400138
Nicolas Capens6407fe82015-02-10 16:27:49 -0500139#endif // _COMPILER_INCLUDED_