blob: b0fa5481ff42e2f8c9c2ec80879e3d228fc631b4 [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
Nicolas Capens6407fe82015-02-10 16:27:49 -050014enum ShCompileOptions
15{
16 SH_VALIDATE = 0,
17 SH_VALIDATE_LOOP_INDEXING = 0x0001,
18 SH_INTERMEDIATE_TREE = 0x0002,
19 SH_OBJECT_CODE = 0x0004,
20 SH_ATTRIBUTES_UNIFORMS = 0x0008,
21 SH_LINE_DIRECTIVES = 0x0010,
22 SH_SOURCE_PATH = 0x0020
23};
24
25//
26// Implementation dependent built-in resources (constants and extensions).
27// The names for these resources has been obtained by stripping gl_/GL_.
28//
29struct ShBuiltInResources
30{
31 ShBuiltInResources();
32
33 // Constants.
34 int MaxVertexAttribs;
35 int MaxVertexUniformVectors;
36 int MaxVaryingVectors;
37 int MaxVertexTextureImageUnits;
38 int MaxCombinedTextureImageUnits;
39 int MaxTextureImageUnits;
40 int MaxFragmentUniformVectors;
41 int MaxDrawBuffers;
42
43 // Extensions.
44 // Set to 1 to enable the extension, else 0.
45 int OES_standard_derivatives;
46 int OES_fragment_precision_high;
47 int OES_EGL_image_external;
48
49 unsigned int MaxCallStackDepth;
50};
51
Nicolas Capens08ca3c62015-02-13 16:06:45 -050052typedef unsigned int GLenum;
53#define GL_FRAGMENT_SHADER 0x8B30
54#define GL_VERTEX_SHADER 0x8B31
55
Nicolas Capens6407fe82015-02-10 16:27:49 -050056//
John Bauman66b8ab22014-05-06 15:57:45 -040057// The base class for the machine dependent compiler to derive from
58// for managing object code from the compile.
59//
Nicolas Capens7a8ccc42015-02-10 15:42:31 -050060class TCompiler
61{
John Bauman66b8ab22014-05-06 15:57:45 -040062public:
Nicolas Capens08ca3c62015-02-13 16:06:45 -050063 TCompiler(GLenum shaderType);
John Bauman66b8ab22014-05-06 15:57:45 -040064 virtual ~TCompiler();
65 virtual TCompiler* getAsCompiler() { return this; }
66
67 bool Init(const ShBuiltInResources& resources);
68 bool compile(const char* const shaderStrings[],
69 const int numStrings,
70 int compileOptions);
71
72 // Get results of the last compilation.
Nicolas Capensb28964b2015-02-10 15:23:06 -050073 int getShaderVersion() const { return shaderVersion; }
John Bauman66b8ab22014-05-06 15:57:45 -040074 TInfoSink& getInfoSink() { return infoSink; }
John Bauman66b8ab22014-05-06 15:57:45 -040075
76protected:
Nicolas Capens08ca3c62015-02-13 16:06:45 -050077 GLenum getShaderType() const { return shaderType; }
John Bauman66b8ab22014-05-06 15:57:45 -040078 // Initialize symbol-table with built-in symbols.
79 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
80 // Clears the results from the previous compilation.
81 void clearResults();
John Baumand4ae8632014-05-06 16:18:33 -040082 // Return true if function recursion is detected or call depth exceeded.
83 bool validateCallDepth(TIntermNode *root, TInfoSink &infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -040084 // Returns true if the given shader does not exceed the minimum
85 // functionality mandated in GLSL 1.0 spec Appendix A.
John Baumand4ae8632014-05-06 16:18:33 -040086 bool validateLimitations(TIntermNode *root);
John Bauman66b8ab22014-05-06 15:57:45 -040087 // Translate to object code.
Nicolas Capens014b9a62014-10-15 10:28:29 -040088 virtual bool translate(TIntermNode *root) = 0;
John Bauman66b8ab22014-05-06 15:57:45 -040089 // Get built-in extensions with default behavior.
90 const TExtensionBehavior& getExtensionBehavior() const;
91
92private:
Nicolas Capens08ca3c62015-02-13 16:06:45 -050093 GLenum shaderType;
John Bauman66b8ab22014-05-06 15:57:45 -040094
John Baumand4ae8632014-05-06 16:18:33 -040095 unsigned int maxCallStackDepth;
96
John Bauman66b8ab22014-05-06 15:57:45 -040097 // Built-in symbol table for the given language, spec, and resources.
98 // It is preserved from compile-to-compile.
99 TSymbolTable symbolTable;
100 // Built-in extensions with default behavior.
101 TExtensionBehavior extensionBehavior;
102
103 // Results of compilation.
Nicolas Capensb28964b2015-02-10 15:23:06 -0500104 int shaderVersion;
John Bauman66b8ab22014-05-06 15:57:45 -0400105 TInfoSink infoSink; // Output sink.
Nicolas Capens7a8ccc42015-02-10 15:42:31 -0500106
107 // Memory allocator. Allocates and tracks memory required by the compiler.
108 // Deallocates all memory when compiler is destructed.
109 TPoolAllocator allocator;
John Bauman66b8ab22014-05-06 15:57:45 -0400110};
111
Nicolas Capens6407fe82015-02-10 16:27:49 -0500112bool InitCompilerGlobals();
113void FreeCompilerGlobals();
John Bauman66b8ab22014-05-06 15:57:45 -0400114
Nicolas Capens6407fe82015-02-10 16:27:49 -0500115#endif // _COMPILER_INCLUDED_