blob: b523a77fa99850c3612574c8100b9810a0c7d9a6 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +00002// Copyright (c) 2002-2013 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[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000066 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +000067 int compileOptions);
68
69 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000070 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000071 TInfoSink& getInfoSink() { return infoSink; }
72 const TVariableInfoList& getAttribs() const { return attribs; }
73 const TVariableInfoList& getUniforms() const { return uniforms; }
zmo@google.comfd747b82011-04-23 01:30:07 +000074 int getMappedNameMaxLength() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +000075
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000076 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000077 NameMap& getNameMap() { return nameMap; }
78 TSymbolTable& getSymbolTable() { return symbolTable; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080protected:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000081 ShShaderType getShaderType() const { return shaderType; }
82 ShShaderSpec getShaderSpec() const { return shaderSpec; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000083 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000084 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000085 // Clears the results from the previous compilation.
86 void clearResults();
Jamie Madilleb1a0102013-07-08 13:31:38 -040087 // Return true if function recursion is detected or call depth exceeded.
88 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
Jamie Madill05a80ce2013-06-20 11:55:49 -040089 // Returns true if a program has no conflicting or missing fragment outputs
90 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000091 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
92 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000093 // Returns true if the given shader does not exceed the minimum
94 // functionality mandated in GLSL 1.0 spec Appendix A.
95 bool validateLimitations(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000096 // Collect info for all attribs and uniforms.
97 void collectAttribsUniforms(TIntermNode* root);
zmo@google.comfd747b82011-04-23 01:30:07 +000098 // Map long variable names into shorter ones.
99 void mapLongVariableNames(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000100 // Translate to object code.
101 virtual void translate(TIntermNode* root) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000102 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
103 // Appendix A, section 7, the shader does not use too many uniforms.
104 bool enforcePackingRestrictions();
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000105 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000106 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
107 // Returns true if the shader does not use samplers.
108 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
109 // Returns true if the shader does not use sampler dependent values to affect control
110 // flow or in operations whose time can depend on the input values.
111 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400112 // Return true if the maximum expression complexity is below the limit.
113 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000114 // Get built-in extensions with default behavior.
115 const TExtensionBehavior& getExtensionBehavior() const;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000116 // Get the resources set by InitBuiltInSymbolTable
117 const ShBuiltInResources& getResources() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000118
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000119 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000120 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000121 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
122
alokp@chromium.org07620a52010-09-23 17:53:56 +0000123private:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000124 ShShaderType shaderType;
125 ShShaderSpec shaderSpec;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000126
gman@chromium.org8d804792012-10-17 21:33:48 +0000127 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400128 int maxExpressionComplexity;
129 int maxCallStackDepth;
gman@chromium.org8d804792012-10-17 21:33:48 +0000130
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000131 ShBuiltInResources compileResources;
132
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000133 // Built-in symbol table for the given language, spec, and resources.
134 // It is preserved from compile-to-compile.
135 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000136 // Built-in extensions with default behavior.
137 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000138 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000139
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000140 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000141 ShArrayIndexClampingStrategy clampingStrategy;
zmo@google.com32e97312011-08-24 01:03:11 +0000142 BuiltInFunctionEmulator builtInFunctionEmulator;
143
alokp@chromium.org07620a52010-09-23 17:53:56 +0000144 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000145 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146 TInfoSink infoSink; // Output sink.
147 TVariableInfoList attribs; // Active attributes in the compiled shader.
148 TVariableInfoList uniforms; // Active uniforms in the compiled shader.
zmo@google.com24c08c42011-05-27 17:40:48 +0000149
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000150 // Cached copy of the ref-counted singleton.
151 LongNameMap* longNameMap;
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000152
153 // name hashing.
154 ShHashFunction64 hashFunction;
155 NameMap nameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156};
157
158//
159// This is the interface between the machine independent code
160// and the machine dependent code.
161//
162// The machine dependent code should derive from the classes
163// above. Then Construct*() and Delete*() will create and
164// destroy the machine dependent objects, which contain the
165// above machine independent information.
166//
zmo@google.com5601ea02011-06-10 18:23:25 +0000167TCompiler* ConstructCompiler(
168 ShShaderType type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169void DeleteCompiler(TCompiler*);
170
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000171#endif // _SHHANDLE_INCLUDED_