blob: 274f7929cfe0cf73def22922499efe84ff62a0f9 [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"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000025#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026
zmo@google.comb9f64aa2012-01-20 00:35:15 +000027class LongNameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000029class TDependencyGraph;
daniel@transgaming.com043da132012-12-20 21:12:22 +000030class TranslatorHLSL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
32//
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000033// Helper function to identify specs that are based on the WebGL spec,
34// like the CSS Shaders spec.
35//
36bool isWebGLBasedSpec(ShShaderSpec spec);
37
38//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039// The base class used to back handles returned to the driver.
40//
41class TShHandleBase {
42public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000043 TShHandleBase();
44 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 virtual TCompiler* getAsCompiler() { return 0; }
daniel@transgaming.com043da132012-12-20 21:12:22 +000046 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000047
48protected:
49 // Memory allocator. Allocates and tracks memory required by the compiler.
50 // Deallocates all memory when compiler is destructed.
51 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052};
53
54//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055// The base class for the machine dependent compiler to derive from
56// for managing object code from the compile.
57//
58class TCompiler : public TShHandleBase {
59public:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000060 TCompiler(ShShaderType type, ShShaderSpec spec);
61 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000063
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000064 bool Init(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000065 bool compile(const char* const shaderStrings[],
66 const int numStrings,
67 int compileOptions);
68
69 // Get results of the last compilation.
70 TInfoSink& getInfoSink() { return infoSink; }
71 const TVariableInfoList& getAttribs() const { return attribs; }
72 const TVariableInfoList& getUniforms() const { return uniforms; }
zmo@google.comfd747b82011-04-23 01:30:07 +000073 int getMappedNameMaxLength() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +000074
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000075 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000076 NameMap& getNameMap() { return nameMap; }
77 TSymbolTable& getSymbolTable() { return symbolTable; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000078
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079protected:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000080 ShShaderType getShaderType() const { return shaderType; }
81 ShShaderSpec getShaderSpec() const { return shaderSpec; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000082 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000083 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000084 // Clears the results from the previous compilation.
85 void clearResults();
zmo@google.comb1762df2011-07-30 02:04:23 +000086 // Return true if function recursion is detected.
87 bool detectRecursion(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000088 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
89 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000090 // Returns true if the given shader does not exceed the minimum
91 // functionality mandated in GLSL 1.0 spec Appendix A.
92 bool validateLimitations(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000093 // Collect info for all attribs and uniforms.
94 void collectAttribsUniforms(TIntermNode* root);
zmo@google.comfd747b82011-04-23 01:30:07 +000095 // Map long variable names into shorter ones.
96 void mapLongVariableNames(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000097 // Translate to object code.
98 virtual void translate(TIntermNode* root) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +000099 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
100 // Appendix A, section 7, the shader does not use too many uniforms.
101 bool enforcePackingRestrictions();
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000102 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000103 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
104 // Returns true if the shader does not use samplers.
105 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
106 // Returns true if the shader does not use sampler dependent values to affect control
107 // flow or in operations whose time can depend on the input values.
108 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
zmo@google.com5601ea02011-06-10 18:23:25 +0000109 // Get built-in extensions with default behavior.
110 const TExtensionBehavior& getExtensionBehavior() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000111
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000112 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000113 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000114 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
115
alokp@chromium.org07620a52010-09-23 17:53:56 +0000116private:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000117 ShShaderType shaderType;
118 ShShaderSpec shaderSpec;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000119
gman@chromium.org8d804792012-10-17 21:33:48 +0000120 int maxUniformVectors;
121
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000122 // Built-in symbol table for the given language, spec, and resources.
123 // It is preserved from compile-to-compile.
124 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000125 // Built-in extensions with default behavior.
126 TExtensionBehavior extensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000127
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000128 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000129 ShArrayIndexClampingStrategy clampingStrategy;
zmo@google.com32e97312011-08-24 01:03:11 +0000130 BuiltInFunctionEmulator builtInFunctionEmulator;
131
alokp@chromium.org07620a52010-09-23 17:53:56 +0000132 // Results of compilation.
133 TInfoSink infoSink; // Output sink.
134 TVariableInfoList attribs; // Active attributes in the compiled shader.
135 TVariableInfoList uniforms; // Active uniforms in the compiled shader.
zmo@google.com24c08c42011-05-27 17:40:48 +0000136
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000137 // Cached copy of the ref-counted singleton.
138 LongNameMap* longNameMap;
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000139
140 // name hashing.
141 ShHashFunction64 hashFunction;
142 NameMap nameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143};
144
145//
146// This is the interface between the machine independent code
147// and the machine dependent code.
148//
149// The machine dependent code should derive from the classes
150// above. Then Construct*() and Delete*() will create and
151// destroy the machine dependent objects, which contain the
152// above machine independent information.
153//
zmo@google.com5601ea02011-06-10 18:23:25 +0000154TCompiler* ConstructCompiler(
155 ShShaderType type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156void DeleteCompiler(TCompiler*);
157
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000158#endif // _SHHANDLE_INCLUDED_