blob: 0c77bb092481f34a2fe56494c7eb1b5962fcdf8b [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.comc23f4612012-11-28 19:42:57 +000021#include "compiler/HashNames.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000022#include "compiler/InfoSink.h"
alokp@chromium.orge4249f02010-07-26 18:13:52 +000023#include "compiler/SymbolTable.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000024#include "compiler/VariableInfo.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025
zmo@google.comb9f64aa2012-01-20 00:35:15 +000026class LongNameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000028class TDependencyGraph;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029
30//
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000031// Helper function to identify specs that are based on the WebGL spec,
32// like the CSS Shaders spec.
33//
34bool isWebGLBasedSpec(ShShaderSpec spec);
35
36//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037// The base class used to back handles returned to the driver.
38//
39class TShHandleBase {
40public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000041 TShHandleBase();
42 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043 virtual TCompiler* getAsCompiler() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000044
45protected:
46 // Memory allocator. Allocates and tracks memory required by the compiler.
47 // Deallocates all memory when compiler is destructed.
48 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049};
50
51//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052// The base class for the machine dependent compiler to derive from
53// for managing object code from the compile.
54//
55class TCompiler : public TShHandleBase {
56public:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000057 TCompiler(ShShaderType type, ShShaderSpec spec);
58 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000060
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000061 bool Init(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000062 bool compile(const char* const shaderStrings[],
63 const int numStrings,
64 int compileOptions);
65
66 // Get results of the last compilation.
67 TInfoSink& getInfoSink() { return infoSink; }
68 const TVariableInfoList& getAttribs() const { return attribs; }
69 const TVariableInfoList& getUniforms() const { return uniforms; }
zmo@google.comfd747b82011-04-23 01:30:07 +000070 int getMappedNameMaxLength() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +000071
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000072 ShHashFunction64 getHashFunction() const { return hashFunction; }
73 const NameMap& getNameMap() const { return nameMap; }
74
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075protected:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000076 ShShaderType getShaderType() const { return shaderType; }
77 ShShaderSpec getShaderSpec() const { return shaderSpec; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000078 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000079 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000080 // Clears the results from the previous compilation.
81 void clearResults();
zmo@google.comb1762df2011-07-30 02:04:23 +000082 // Return true if function recursion is detected.
83 bool detectRecursion(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000084 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
85 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000086 // Returns true if the given shader does not exceed the minimum
87 // functionality mandated in GLSL 1.0 spec Appendix A.
88 bool validateLimitations(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000089 // Collect info for all attribs and uniforms.
90 void collectAttribsUniforms(TIntermNode* root);
zmo@google.comfd747b82011-04-23 01:30:07 +000091 // Map long variable names into shorter ones.
92 void mapLongVariableNames(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000093 // Translate to object code.
94 virtual void translate(TIntermNode* root) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +000095 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
96 // Appendix A, section 7, the shader does not use too many uniforms.
97 bool enforcePackingRestrictions();
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000098 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +000099 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
100 // Returns true if the shader does not use samplers.
101 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
102 // Returns true if the shader does not use sampler dependent values to affect control
103 // flow or in operations whose time can depend on the input values.
104 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
zmo@google.com5601ea02011-06-10 18:23:25 +0000105 // Get built-in extensions with default behavior.
106 const TExtensionBehavior& getExtensionBehavior() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000107
zmo@google.com32e97312011-08-24 01:03:11 +0000108 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
109
alokp@chromium.org07620a52010-09-23 17:53:56 +0000110private:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000111 ShShaderType shaderType;
112 ShShaderSpec shaderSpec;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000113
gman@chromium.org8d804792012-10-17 21:33:48 +0000114 int maxUniformVectors;
115
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000116 // Built-in symbol table for the given language, spec, and resources.
117 // It is preserved from compile-to-compile.
118 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000119 // Built-in extensions with default behavior.
120 TExtensionBehavior extensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000121
zmo@google.com32e97312011-08-24 01:03:11 +0000122 BuiltInFunctionEmulator builtInFunctionEmulator;
123
alokp@chromium.org07620a52010-09-23 17:53:56 +0000124 // Results of compilation.
125 TInfoSink infoSink; // Output sink.
126 TVariableInfoList attribs; // Active attributes in the compiled shader.
127 TVariableInfoList uniforms; // Active uniforms in the compiled shader.
zmo@google.com24c08c42011-05-27 17:40:48 +0000128
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000129 // Cached copy of the ref-counted singleton.
130 LongNameMap* longNameMap;
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000131
132 // name hashing.
133 ShHashFunction64 hashFunction;
134 NameMap nameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135};
136
137//
138// This is the interface between the machine independent code
139// and the machine dependent code.
140//
141// The machine dependent code should derive from the classes
142// above. Then Construct*() and Delete*() will create and
143// destroy the machine dependent objects, which contain the
144// above machine independent information.
145//
zmo@google.com5601ea02011-06-10 18:23:25 +0000146TCompiler* ConstructCompiler(
147 ShShaderType type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148void DeleteCompiler(TCompiler*);
149
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000150#endif // _SHHANDLE_INCLUDED_