blob: 0faaeb15e904d66f014dc8d38ab2bb11d329e3ce [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
zmo@google.comb9f64aa2012-01-20 00:35:15 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef _SHHANDLE_INCLUDED_
8#define _SHHANDLE_INCLUDED_
9
10//
11// Machine independent part of the compiler private objects
12// sent as ShHandle to the driver.
13//
14// This should not be included by driver code.
15//
16
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000017#include "GLSLANG/ShaderLang.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
zmo@google.com32e97312011-08-24 01:03:11 +000019#include "compiler/BuiltInFunctionEmulator.h"
alokp@chromium.orgad771eb2010-09-07 17:36:23 +000020#include "compiler/ExtensionBehavior.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "compiler/InfoSink.h"
alokp@chromium.orge4249f02010-07-26 18:13:52 +000022#include "compiler/SymbolTable.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000023#include "compiler/VariableInfo.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024
zmo@google.comb9f64aa2012-01-20 00:35:15 +000025class LongNameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000027class TDependencyGraph;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028
29//
30// The base class used to back handles returned to the driver.
31//
32class TShHandleBase {
33public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000034 TShHandleBase();
35 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036 virtual TCompiler* getAsCompiler() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000037
38protected:
39 // Memory allocator. Allocates and tracks memory required by the compiler.
40 // Deallocates all memory when compiler is destructed.
41 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042};
43
44//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045// The base class for the machine dependent compiler to derive from
46// for managing object code from the compile.
47//
48class TCompiler : public TShHandleBase {
49public:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000050 TCompiler(ShShaderType type, ShShaderSpec spec);
51 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000053
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000054 bool Init(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000055 bool compile(const char* const shaderStrings[],
56 const int numStrings,
57 int compileOptions);
58
59 // Get results of the last compilation.
60 TInfoSink& getInfoSink() { return infoSink; }
61 const TVariableInfoList& getAttribs() const { return attribs; }
62 const TVariableInfoList& getUniforms() const { return uniforms; }
zmo@google.comfd747b82011-04-23 01:30:07 +000063 int getMappedNameMaxLength() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +000064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065protected:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000066 ShShaderType getShaderType() const { return shaderType; }
67 ShShaderSpec getShaderSpec() const { return shaderSpec; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000068 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000069 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000070 // Clears the results from the previous compilation.
71 void clearResults();
zmo@google.comb1762df2011-07-30 02:04:23 +000072 // Return true if function recursion is detected.
73 bool detectRecursion(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000074 // Returns true if the given shader does not exceed the minimum
75 // functionality mandated in GLSL 1.0 spec Appendix A.
76 bool validateLimitations(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000077 // Collect info for all attribs and uniforms.
78 void collectAttribsUniforms(TIntermNode* root);
zmo@google.comfd747b82011-04-23 01:30:07 +000079 // Map long variable names into shorter ones.
80 void mapLongVariableNames(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000081 // Translate to object code.
82 virtual void translate(TIntermNode* root) = 0;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000083 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
84 bool enforceTimingRestrictions(TIntermNode* root,
85 const TString& restrictedSymbol,
86 bool outputGraph);
87 // Returns true if the shader does not define the restricted symbol.
88 bool enforceVertexShaderTimingRestrictions(TIntermNode* root,
89 const TString& restrictedSymbol);
90 // Returns true if the shader does not use the restricted symbol to affect control flow or in
91 // operations whose time can depend on the input values.
92 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph,
93 const TString& restrictedSymbol);
zmo@google.com5601ea02011-06-10 18:23:25 +000094 // Get built-in extensions with default behavior.
95 const TExtensionBehavior& getExtensionBehavior() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +000096
zmo@google.com32e97312011-08-24 01:03:11 +000097 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
98
alokp@chromium.org07620a52010-09-23 17:53:56 +000099private:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000100 ShShaderType shaderType;
101 ShShaderSpec shaderSpec;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000102
103 // Built-in symbol table for the given language, spec, and resources.
104 // It is preserved from compile-to-compile.
105 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000106 // Built-in extensions with default behavior.
107 TExtensionBehavior extensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000108
zmo@google.com32e97312011-08-24 01:03:11 +0000109 BuiltInFunctionEmulator builtInFunctionEmulator;
110
alokp@chromium.org07620a52010-09-23 17:53:56 +0000111 // Results of compilation.
112 TInfoSink infoSink; // Output sink.
113 TVariableInfoList attribs; // Active attributes in the compiled shader.
114 TVariableInfoList uniforms; // Active uniforms in the compiled shader.
zmo@google.com24c08c42011-05-27 17:40:48 +0000115
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000116 // Cached copy of the ref-counted singleton.
117 LongNameMap* longNameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118};
119
120//
121// This is the interface between the machine independent code
122// and the machine dependent code.
123//
124// The machine dependent code should derive from the classes
125// above. Then Construct*() and Delete*() will create and
126// destroy the machine dependent objects, which contain the
127// above machine independent information.
128//
zmo@google.com5601ea02011-06-10 18:23:25 +0000129TCompiler* ConstructCompiler(
130 ShShaderType type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131void DeleteCompiler(TCompiler*);
132
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000133#endif // _SHHANDLE_INCLUDED_