1 // 2 // Copyright 2002 The ANGLE Project Authors. All rights reserved. 3 // 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 COMPILER_TRANSLATOR_COMPILER_H_ 8 #define COMPILER_TRANSLATOR_COMPILER_H_ 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 17 #include <GLSLANG/ShaderVars.h> 18 19 #include "common/PackedEnums.h" 20 #include "compiler/translator/BuiltInFunctionEmulator.h" 21 #include "compiler/translator/CallDAG.h" 22 #include "compiler/translator/Diagnostics.h" 23 #include "compiler/translator/ExtensionBehavior.h" 24 #include "compiler/translator/HashNames.h" 25 #include "compiler/translator/InfoSink.h" 26 #include "compiler/translator/Pragma.h" 27 #include "compiler/translator/SymbolTable.h" 28 #include "compiler/translator/ValidateAST.h" 29 30 namespace sh 31 { 32 33 class TCompiler; 34 class TParseContext; 35 #ifdef ANGLE_ENABLE_HLSL 36 class TranslatorHLSL; 37 #endif // ANGLE_ENABLE_HLSL 38 #ifdef ANGLE_ENABLE_METAL 39 class TranslatorMetalDirect; 40 #endif // ANGLE_ENABLE_METAL 41 42 using SpecConstUsageBits = angle::PackedEnumBitSet<vk::SpecConstUsage, uint32_t>; 43 44 // 45 // Helper function to check if the shader type is GLSL. 46 // 47 bool IsGLSL130OrNewer(ShShaderOutput output); 48 bool IsGLSL420OrNewer(ShShaderOutput output); 49 bool IsGLSL410OrOlder(ShShaderOutput output); 50 51 // 52 // Helper function to check if the invariant qualifier can be removed. 53 // 54 bool RemoveInvariant(sh::GLenum shaderType, 55 int shaderVersion, 56 ShShaderOutput outputType, 57 ShCompileOptions compileOptions); 58 59 // 60 // The base class used to back handles returned to the driver. 61 // 62 class TShHandleBase 63 { 64 public: 65 TShHandleBase(); 66 virtual ~TShHandleBase(); getAsCompiler()67 virtual TCompiler *getAsCompiler() { return 0; } 68 #ifdef ANGLE_ENABLE_HLSL getAsTranslatorHLSL()69 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; } 70 #endif // ANGLE_ENABLE_HLSL 71 #ifdef ANGLE_ENABLE_METAL getAsTranslatorMetalDirect()72 virtual TranslatorMetalDirect *getAsTranslatorMetalDirect() { return nullptr; } 73 #endif // ANGLE_ENABLE_METAL 74 75 protected: 76 // Memory allocator. Allocates and tracks memory required by the compiler. 77 // Deallocates all memory when compiler is destructed. 78 angle::PoolAllocator allocator; 79 }; 80 81 struct TFunctionMetadata 82 { 83 bool used = false; 84 }; 85 86 // 87 // The base class for the machine dependent compiler to derive from 88 // for managing object code from the compile. 89 // 90 class TCompiler : public TShHandleBase 91 { 92 public: 93 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); 94 ~TCompiler() override; getAsCompiler()95 TCompiler *getAsCompiler() override { return this; } 96 97 bool Init(const ShBuiltInResources &resources); 98 99 // compileTreeForTesting should be used only when tests require access to 100 // the AST. Users of this function need to manually manage the global pool 101 // allocator. Returns nullptr whenever there are compilation errors. 102 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[], 103 size_t numStrings, 104 ShCompileOptions compileOptions); 105 106 bool compile(const char *const shaderStrings[], 107 size_t numStrings, 108 ShCompileOptions compileOptions); 109 110 // Get results of the last compilation. getShaderVersion()111 int getShaderVersion() const { return mShaderVersion; } getInfoSink()112 TInfoSink &getInfoSink() { return mInfoSink; } 113 isEarlyFragmentTestsSpecified()114 bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; } isEarlyFragmentTestsOptimized()115 bool isEarlyFragmentTestsOptimized() const { return mEarlyFragmentTestsOptimized; } getSpecConstUsageBits()116 SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; } 117 isComputeShaderLocalSizeDeclared()118 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; } getComputeShaderLocalSize()119 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; } getNumViews()120 int getNumViews() const { return mNumViews; } 121 122 // Clears the results from the previous compilation. 123 void clearResults(); 124 getAttributes()125 const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; } getOutputVariables()126 const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; } getUniforms()127 const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; } getInputVaryings()128 const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; } getOutputVaryings()129 const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; } getInterfaceBlocks()130 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; } getUniformBlocks()131 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } getShaderStorageBlocks()132 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const 133 { 134 return mShaderStorageBlocks; 135 } 136 getHashFunction()137 ShHashFunction64 getHashFunction() const { return mResources.HashFunction; } getNameMap()138 NameMap &getNameMap() { return mNameMap; } getSymbolTable()139 TSymbolTable &getSymbolTable() { return mSymbolTable; } getShaderSpec()140 ShShaderSpec getShaderSpec() const { return mShaderSpec; } getOutputType()141 ShShaderOutput getOutputType() const { return mOutputType; } getBuiltInResourcesString()142 const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; } 143 144 bool isHighPrecisionSupported() const; 145 146 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const; 147 bool shouldLimitTypeSizes() const; 148 149 // Get the resources set by InitBuiltInSymbolTable 150 const ShBuiltInResources &getResources() const; 151 getPragma()152 const TPragma &getPragma() const { return mPragma; } 153 getGeometryShaderMaxVertices()154 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; } getGeometryShaderInvocations()155 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; } getGeometryShaderInputPrimitiveType()156 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const 157 { 158 return mGeometryShaderInputPrimitiveType; 159 } getGeometryShaderOutputPrimitiveType()160 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const 161 { 162 return mGeometryShaderOutputPrimitiveType; 163 } 164 165 unsigned int getStructSize(const ShaderVariable &var) const; 166 getTessControlShaderOutputVertices()167 int getTessControlShaderOutputVertices() const { return mTessControlShaderOutputVertices; } getTessEvaluationShaderInputPrimitiveType()168 TLayoutTessEvaluationType getTessEvaluationShaderInputPrimitiveType() const 169 { 170 return mTessEvaluationShaderInputPrimitiveType; 171 } getTessEvaluationShaderInputVertexSpacingType()172 TLayoutTessEvaluationType getTessEvaluationShaderInputVertexSpacingType() const 173 { 174 return mTessEvaluationShaderInputVertexSpacingType; 175 } getTessEvaluationShaderInputOrderingType()176 TLayoutTessEvaluationType getTessEvaluationShaderInputOrderingType() const 177 { 178 return mTessEvaluationShaderInputOrderingType; 179 } getTessEvaluationShaderInputPointType()180 TLayoutTessEvaluationType getTessEvaluationShaderInputPointType() const 181 { 182 return mTessEvaluationShaderInputPointType; 183 } 184 hasAnyPreciseType()185 bool hasAnyPreciseType() const { return mHasAnyPreciseType; } 186 getAdvancedBlendEquations()187 AdvancedBlendEquations getAdvancedBlendEquations() const { return mAdvancedBlendEquations; } 188 189 unsigned int getSharedMemorySize() const; 190 getShaderType()191 sh::GLenum getShaderType() const { return mShaderType; } 192 193 // Validate the AST and produce errors if it is inconsistent. 194 bool validateAST(TIntermNode *root); 195 // Some transformations may need to temporarily disable validation until they are complete. A 196 // set of disable/enable helpers are used for this purpose. 197 bool disableValidateFunctionCall(); 198 void restoreValidateFunctionCall(bool enable); 199 bool disableValidateVariableReferences(); 200 void restoreValidateVariableReferences(bool enable); 201 // When the AST is post-processed (such as to determine precise-ness of intermediate nodes), 202 // it's expected to no longer transform. 203 void enableValidateNoMoreTransformations(); 204 205 protected: 206 // Add emulated functions to the built-in function emulator. initBuiltInFunctionEmulator(BuiltInFunctionEmulator * emu,ShCompileOptions compileOptions)207 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, 208 ShCompileOptions compileOptions) 209 {} 210 // Translate to object code. May generate performance warnings through the diagnostics. 211 ANGLE_NO_DISCARD virtual bool translate(TIntermBlock *root, 212 ShCompileOptions compileOptions, 213 PerformanceDiagnostics *perfDiagnostics) = 0; 214 // Get built-in extensions with default behavior. 215 const TExtensionBehavior &getExtensionBehavior() const; 216 const char *getSourcePath() const; 217 // Relies on collectVariables having been called. 218 bool isVaryingDefined(const char *varyingName); 219 220 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const; 221 222 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0; 223 virtual bool shouldCollectVariables(ShCompileOptions compileOptions); 224 225 bool wereVariablesCollected() const; 226 std::vector<sh::ShaderVariable> mAttributes; 227 std::vector<sh::ShaderVariable> mOutputVariables; 228 std::vector<sh::ShaderVariable> mUniforms; 229 std::vector<sh::ShaderVariable> mInputVaryings; 230 std::vector<sh::ShaderVariable> mOutputVaryings; 231 std::vector<sh::ShaderVariable> mSharedVariables; 232 std::vector<sh::InterfaceBlock> mInterfaceBlocks; 233 std::vector<sh::InterfaceBlock> mUniformBlocks; 234 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 235 236 // Track what should be validated given passes currently applied. 237 ValidateASTOptions mValidateASTOptions; 238 239 // Specialization constant usage bits 240 SpecConstUsageBits mSpecConstUsageBits; 241 242 private: 243 // Initialize symbol-table with built-in symbols. 244 bool initBuiltInSymbolTable(const ShBuiltInResources &resources); 245 // Compute the string representation of the built-in resources 246 void setResourceString(); 247 // Return false if the call depth is exceeded. 248 bool checkCallDepth(); 249 // Insert statements to reference all members in unused uniform blocks with standard and shared 250 // layout. This is to work around a Mac driver that treats unused standard/shared 251 // uniform blocks as inactive. 252 ANGLE_NO_DISCARD bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root); 253 // Insert statements to initialize output variables in the beginning of main(). 254 // This is to avoid undefined behaviors. 255 ANGLE_NO_DISCARD bool initializeOutputVariables(TIntermBlock *root); 256 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main(). 257 // It is to work around a Linux driver bug where missing this causes compile failure 258 // while spec says it is allowed. 259 // This function should only be applied to vertex shaders. 260 ANGLE_NO_DISCARD bool initializeGLPosition(TIntermBlock *root); 261 // Return true if the maximum expression complexity is below the limit. 262 bool limitExpressionComplexity(TIntermBlock *root); 263 // Creates the function call DAG for further analysis, returning false if there is a recursion 264 bool initCallDag(TIntermNode *root); 265 // Return false if "main" doesn't exist 266 bool tagUsedFunctions(); 267 void internalTagUsedFunction(size_t index); 268 269 void collectInterfaceBlocks(); 270 271 bool mVariablesCollected; 272 273 bool mGLPositionInitialized; 274 275 // Removes unused function declarations and prototypes from the AST 276 bool pruneUnusedFunctions(TIntermBlock *root); 277 278 TIntermBlock *compileTreeImpl(const char *const shaderStrings[], 279 size_t numStrings, 280 const ShCompileOptions compileOptions); 281 282 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader 283 // version. 284 void setASTMetadata(const TParseContext &parseContext); 285 286 // Check if shader version meets the requirement. 287 bool checkShaderVersion(TParseContext *parseContext); 288 289 // Does checks that need to be run after parsing is complete and returns true if they pass. 290 bool checkAndSimplifyAST(TIntermBlock *root, 291 const TParseContext &parseContext, 292 ShCompileOptions compileOptions); 293 294 bool postParseChecks(const TParseContext &parseContext); 295 296 sh::GLenum mShaderType; 297 ShShaderSpec mShaderSpec; 298 ShShaderOutput mOutputType; 299 300 CallDAG mCallDag; 301 std::vector<TFunctionMetadata> mFunctionMetadata; 302 303 ShBuiltInResources mResources; 304 std::string mBuiltInResourcesString; 305 306 // Built-in symbol table for the given language, spec, and resources. 307 // It is preserved from compile-to-compile. 308 TSymbolTable mSymbolTable; 309 // Built-in extensions with default behavior. 310 TExtensionBehavior mExtensionBehavior; 311 312 BuiltInFunctionEmulator mBuiltInFunctionEmulator; 313 314 // Results of compilation. 315 int mShaderVersion; 316 TInfoSink mInfoSink; // Output sink. 317 TDiagnostics mDiagnostics; 318 const char *mSourcePath; // Path of source file or NULL 319 320 // fragment shader early fragment tests 321 bool mEarlyFragmentTestsSpecified; 322 bool mEarlyFragmentTestsOptimized; 323 324 // compute shader local group size 325 bool mComputeShaderLocalSizeDeclared; 326 sh::WorkGroupSize mComputeShaderLocalSize; 327 328 // GL_OVR_multiview num_views. 329 int mNumViews; 330 331 // geometry shader parameters. 332 int mGeometryShaderMaxVertices; 333 int mGeometryShaderInvocations; 334 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType; 335 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType; 336 337 // tesssellation shader parameters 338 int mTessControlShaderOutputVertices; 339 TLayoutTessEvaluationType mTessEvaluationShaderInputPrimitiveType; 340 TLayoutTessEvaluationType mTessEvaluationShaderInputVertexSpacingType; 341 TLayoutTessEvaluationType mTessEvaluationShaderInputOrderingType; 342 TLayoutTessEvaluationType mTessEvaluationShaderInputPointType; 343 344 bool mHasAnyPreciseType; 345 346 // advanced blend equation parameters 347 AdvancedBlendEquations mAdvancedBlendEquations; 348 349 // name hashing. 350 NameMap mNameMap; 351 352 TPragma mPragma; 353 354 ShCompileOptions mCompileOptions; 355 }; 356 357 // 358 // This is the interface between the machine independent code 359 // and the machine dependent code. 360 // 361 // The machine dependent code should derive from the classes 362 // above. Then Construct*() and Delete*() will create and 363 // destroy the machine dependent objects, which contain the 364 // above machine independent information. 365 // 366 TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); 367 void DeleteCompiler(TCompiler *); 368 369 } // namespace sh 370 371 #endif // COMPILER_TRANSLATOR_COMPILER_H_ 372