blob: 842bc7655f2032f585e20073a5441cbf21980359 [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
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_COMPILER_H_
8#define COMPILER_TRANSLATOR_COMPILER_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
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
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020017#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/ExtensionBehavior.h"
19#include "compiler/translator/HashNames.h"
20#include "compiler/translator/InfoSink.h"
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070021#include "compiler/translator/Pragma.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/SymbolTable.h"
23#include "compiler/translator/VariableInfo.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000024#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025
26class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000027class TDependencyGraph;
daniel@transgaming.com043da132012-12-20 21:12:22 +000028class TranslatorHLSL;
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//
Jamie Madill5508f392014-02-20 13:31:36 -050034bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000035
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; }
daniel@transgaming.com043da132012-12-20 21:12:22 +000044 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000045
46protected:
47 // Memory allocator. Allocates and tracks memory required by the compiler.
48 // Deallocates all memory when compiler is destructed.
49 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050};
51
52//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053// The base class for the machine dependent compiler to derive from
54// for managing object code from the compile.
55//
Jamie Madilla718c1e2014-07-02 15:31:22 -040056class TCompiler : public TShHandleBase
57{
58 public:
Jamie Madill183bde52014-07-02 15:31:19 -040059 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000060 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000062
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000063 bool Init(const ShBuiltInResources& resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020064
65 // compileTreeForTesting should be used only when tests require access to
66 // the AST. Users of this function need to manually manage the global pool
67 // allocator. Returns NULL whenever there are compilation errors.
68 TIntermNode *compileTreeForTesting(const char* const shaderStrings[],
69 size_t numStrings, int compileOptions);
70
alokp@chromium.org07620a52010-09-23 17:53:56 +000071 bool compile(const char* const shaderStrings[],
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020072 size_t numStrings, int compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000073
74 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000075 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000076 TInfoSink& getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040077
78 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
79 const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040080 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
81 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madilled27c722014-07-02 15:31:23 -040082 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000083
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000084 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000085 NameMap& getNameMap() { return nameMap; }
86 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070087 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -040088 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -070089 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000090
Jamie Madill54ad4f82014-09-03 09:40:46 -040091 // Get the resources set by InitBuiltInSymbolTable
92 const ShBuiltInResources& getResources() const;
93
Jamie Madilla718c1e2014-07-02 15:31:22 -040094 protected:
Jamie Madill183bde52014-07-02 15:31:19 -040095 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000096 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000097 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -040098 // Compute the string representation of the built-in resources
99 void setResourceString();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000100 // Clears the results from the previous compilation.
101 void clearResults();
Jamie Madilleb1a0102013-07-08 13:31:38 -0400102 // Return true if function recursion is detected or call depth exceeded.
103 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400104 // Returns true if a program has no conflicting or missing fragment outputs
105 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000106 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
107 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000108 // Returns true if the given shader does not exceed the minimum
109 // functionality mandated in GLSL 1.0 spec Appendix A.
110 bool validateLimitations(TIntermNode* root);
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400111 // Collect info for all attribs, uniforms, varyings.
112 void collectVariables(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000113 // Translate to object code.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200114 virtual void translate(TIntermNode *root, int compileOptions) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000115 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
116 // Appendix A, section 7, the shader does not use too many uniforms.
117 bool enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800118 // Insert statements to initialize varyings without static use in the beginning
119 // of main(). It is to work around a Mac driver where such varyings in a vertex
120 // shader may be optimized out incorrectly at compile time, causing a link failure.
121 // This function should only be applied to vertex shaders.
122 void initializeVaryingsWithoutStaticUse(TIntermNode* root);
123 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
124 // It is to work around a Linux driver bug where missing this causes compile failure
125 // while spec says it is allowed.
126 // This function should only be applied to vertex shaders.
127 void initializeGLPosition(TIntermNode* root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000128 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000129 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
130 // Returns true if the shader does not use samplers.
131 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
Jamie Madilld4a3a312014-06-25 16:04:56 -0400132 // Returns true if the shader does not use sampler dependent values to affect control
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000133 // flow or in operations whose time can depend on the input values.
134 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400135 // Return true if the maximum expression complexity is below the limit.
136 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000137 // Get built-in extensions with default behavior.
138 const TExtensionBehavior& getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200139 const char *getSourcePath() const;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700140 const TPragma& getPragma() const { return mPragma; }
141 void writePragma();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000142
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000143 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000144 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200145 const BuiltInFunctionEmulatorGLSL& getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000146
Jamie Madilled27c722014-07-02 15:31:23 -0400147 std::vector<sh::Attribute> attributes;
148 std::vector<sh::Attribute> outputVariables;
149 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400150 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400151 std::vector<sh::Varying> varyings;
152 std::vector<sh::InterfaceBlock> interfaceBlocks;
153
Jamie Madilla718c1e2014-07-02 15:31:22 -0400154 private:
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200155 TIntermNode *compileTreeImpl(const char* const shaderStrings[],
156 size_t numStrings, int compileOptions);
157
Jamie Madill183bde52014-07-02 15:31:19 -0400158 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000159 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400160 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000161
gman@chromium.org8d804792012-10-17 21:33:48 +0000162 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400163 int maxExpressionComplexity;
164 int maxCallStackDepth;
gman@chromium.org8d804792012-10-17 21:33:48 +0000165
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000166 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400167 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000168
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000169 // Built-in symbol table for the given language, spec, and resources.
170 // It is preserved from compile-to-compile.
171 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000172 // Built-in extensions with default behavior.
173 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000174 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000175
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000176 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000177 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200178 BuiltInFunctionEmulatorGLSL builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000179
alokp@chromium.org07620a52010-09-23 17:53:56 +0000180 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000181 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182 TInfoSink infoSink; // Output sink.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200183 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000184
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000185 // name hashing.
186 ShHashFunction64 hashFunction;
187 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700188
189 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190};
191
192//
193// This is the interface between the machine independent code
194// and the machine dependent code.
195//
196// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400197// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198// destroy the machine dependent objects, which contain the
199// above machine independent information.
200//
zmo@google.com5601ea02011-06-10 18:23:25 +0000201TCompiler* ConstructCompiler(
Jamie Madill183bde52014-07-02 15:31:19 -0400202 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203void DeleteCompiler(TCompiler*);
204
Geoff Lang0a73dd82014-11-19 16:18:08 -0500205#endif // COMPILER_TRANSLATOR_COMPILER_H_