blob: bb81741582c24c2e88b6b8e2d9fcefab230fabbc [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 Bratell73941de2015-02-25 14:34:49 +010028#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000029class TranslatorHLSL;
Daniel Bratell73941de2015-02-25 14:34:49 +010030#endif // ANGLE_ENABLE_HLSL
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//
Jamie Madill5508f392014-02-20 13:31:36 -050036bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000037
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 Bratell73941de2015-02-25 14:34:49 +010046#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000047 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010048#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000049
50protected:
51 // Memory allocator. Allocates and tracks memory required by the compiler.
52 // Deallocates all memory when compiler is destructed.
53 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054};
55
56//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057// The base class for the machine dependent compiler to derive from
58// for managing object code from the compile.
59//
Jamie Madilla718c1e2014-07-02 15:31:22 -040060class TCompiler : public TShHandleBase
61{
62 public:
Jamie Madill183bde52014-07-02 15:31:19 -040063 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000064 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000066
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000067 bool Init(const ShBuiltInResources& resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020068
69 // compileTreeForTesting should be used only when tests require access to
70 // the AST. Users of this function need to manually manage the global pool
71 // allocator. Returns NULL whenever there are compilation errors.
72 TIntermNode *compileTreeForTesting(const char* const shaderStrings[],
73 size_t numStrings, int compileOptions);
74
alokp@chromium.org07620a52010-09-23 17:53:56 +000075 bool compile(const char* const shaderStrings[],
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020076 size_t numStrings, int compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000077
78 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000079 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000080 TInfoSink& getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040081
82 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
83 const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040084 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
85 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madilled27c722014-07-02 15:31:23 -040086 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000087
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000088 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000089 NameMap& getNameMap() { return nameMap; }
90 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070091 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -040092 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -070093 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000094
Jamie Madill54ad4f82014-09-03 09:40:46 -040095 // Get the resources set by InitBuiltInSymbolTable
96 const ShBuiltInResources& getResources() const;
97
Jamie Madilla718c1e2014-07-02 15:31:22 -040098 protected:
Jamie Madill183bde52014-07-02 15:31:19 -040099 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000100 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000101 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400102 // Compute the string representation of the built-in resources
103 void setResourceString();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000104 // Clears the results from the previous compilation.
105 void clearResults();
Jamie Madilleb1a0102013-07-08 13:31:38 -0400106 // Return true if function recursion is detected or call depth exceeded.
107 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400108 // Returns true if a program has no conflicting or missing fragment outputs
109 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000110 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
111 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000112 // Returns true if the given shader does not exceed the minimum
113 // functionality mandated in GLSL 1.0 spec Appendix A.
114 bool validateLimitations(TIntermNode* root);
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400115 // Collect info for all attribs, uniforms, varyings.
116 void collectVariables(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000117 // Translate to object code.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200118 virtual void translate(TIntermNode *root, int compileOptions) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000119 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
120 // Appendix A, section 7, the shader does not use too many uniforms.
121 bool enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800122 // Insert statements to initialize varyings without static use in the beginning
123 // of main(). It is to work around a Mac driver where such varyings in a vertex
124 // shader may be optimized out incorrectly at compile time, causing a link failure.
125 // This function should only be applied to vertex shaders.
126 void initializeVaryingsWithoutStaticUse(TIntermNode* root);
127 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
128 // It is to work around a Linux driver bug where missing this causes compile failure
129 // while spec says it is allowed.
130 // This function should only be applied to vertex shaders.
131 void initializeGLPosition(TIntermNode* root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000132 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000133 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
134 // Returns true if the shader does not use samplers.
135 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
Jamie Madilld4a3a312014-06-25 16:04:56 -0400136 // Returns true if the shader does not use sampler dependent values to affect control
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000137 // flow or in operations whose time can depend on the input values.
138 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400139 // Return true if the maximum expression complexity is below the limit.
140 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000141 // Get built-in extensions with default behavior.
142 const TExtensionBehavior& getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200143 const char *getSourcePath() const;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700144 const TPragma& getPragma() const { return mPragma; }
145 void writePragma();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000147 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000148 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200149 const BuiltInFunctionEmulatorGLSL& getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000150
Jamie Madilled27c722014-07-02 15:31:23 -0400151 std::vector<sh::Attribute> attributes;
152 std::vector<sh::Attribute> outputVariables;
153 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400154 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400155 std::vector<sh::Varying> varyings;
156 std::vector<sh::InterfaceBlock> interfaceBlocks;
157
Jamie Madilla718c1e2014-07-02 15:31:22 -0400158 private:
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200159 TIntermNode *compileTreeImpl(const char* const shaderStrings[],
160 size_t numStrings, int compileOptions);
161
Jamie Madill183bde52014-07-02 15:31:19 -0400162 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000163 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400164 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000165
gman@chromium.org8d804792012-10-17 21:33:48 +0000166 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400167 int maxExpressionComplexity;
168 int maxCallStackDepth;
gman@chromium.org8d804792012-10-17 21:33:48 +0000169
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000170 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400171 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000172
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000173 // Built-in symbol table for the given language, spec, and resources.
174 // It is preserved from compile-to-compile.
175 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000176 // Built-in extensions with default behavior.
177 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000178 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000180 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000181 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200182 BuiltInFunctionEmulatorGLSL builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000183
alokp@chromium.org07620a52010-09-23 17:53:56 +0000184 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000185 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186 TInfoSink infoSink; // Output sink.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200187 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000188
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000189 // name hashing.
190 ShHashFunction64 hashFunction;
191 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700192
193 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194};
195
196//
197// This is the interface between the machine independent code
198// and the machine dependent code.
199//
200// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400201// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202// destroy the machine dependent objects, which contain the
203// above machine independent information.
204//
zmo@google.com5601ea02011-06-10 18:23:25 +0000205TCompiler* ConstructCompiler(
Jamie Madill183bde52014-07-02 15:31:19 -0400206 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207void DeleteCompiler(TCompiler*);
208
Geoff Lang0a73dd82014-11-19 16:18:08 -0500209#endif // COMPILER_TRANSLATOR_COMPILER_H_