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 // 82 // The base class for the machine dependent compiler to derive from 83 // for managing object code from the compile. 84 // 85 class TCompiler : public TShHandleBase 86 { 87 public: 88 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); 89 ~TCompiler() override; getAsCompiler()90 TCompiler *getAsCompiler() override { return this; } 91 92 bool Init(const ShBuiltInResources &resources); 93 94 // compileTreeForTesting should be used only when tests require access to 95 // the AST. Users of this function need to manually manage the global pool 96 // allocator. Returns nullptr whenever there are compilation errors. 97 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[], 98 size_t numStrings, 99 ShCompileOptions compileOptions); 100 101 bool compile(const char *const shaderStrings[], 102 size_t numStrings, 103 ShCompileOptions compileOptions); 104 105 // Get results of the last compilation. getShaderVersion()106 int getShaderVersion() const { return mShaderVersion; } getInfoSink()107 TInfoSink &getInfoSink() { return mInfoSink; } 108 isEarlyFragmentTestsSpecified()109 bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; } isEarlyFragmentTestsOptimized()110 bool isEarlyFragmentTestsOptimized() const { return mEarlyFragmentTestsOptimized; } getSpecConstUsageBits()111 SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; } 112 isComputeShaderLocalSizeDeclared()113 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; } getComputeShaderLocalSize()114 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; } getNumViews()115 int getNumViews() const { return mNumViews; } 116 117 // Clears the results from the previous compilation. 118 void clearResults(); 119 getAttributes()120 const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; } getOutputVariables()121 const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; } getUniforms()122 const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; } getInputVaryings()123 const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; } getOutputVaryings()124 const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; } getInterfaceBlocks()125 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; } getUniformBlocks()126 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } getShaderStorageBlocks()127 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const 128 { 129 return mShaderStorageBlocks; 130 } 131 getHashFunction()132 ShHashFunction64 getHashFunction() const { return mResources.HashFunction; } getNameMap()133 NameMap &getNameMap() { return mNameMap; } getSymbolTable()134 TSymbolTable &getSymbolTable() { return mSymbolTable; } getShaderSpec()135 ShShaderSpec getShaderSpec() const { return mShaderSpec; } getOutputType()136 ShShaderOutput getOutputType() const { return mOutputType; } getBuiltInResourcesString()137 const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; } 138 139 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const; 140 bool shouldLimitTypeSizes() const; 141 142 // Get the resources set by InitBuiltInSymbolTable 143 const ShBuiltInResources &getResources() const; 144 getPragma()145 const TPragma &getPragma() const { return mPragma; } 146 getGeometryShaderMaxVertices()147 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; } getGeometryShaderInvocations()148 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; } getGeometryShaderInputPrimitiveType()149 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const 150 { 151 return mGeometryShaderInputPrimitiveType; 152 } getGeometryShaderOutputPrimitiveType()153 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const 154 { 155 return mGeometryShaderOutputPrimitiveType; 156 } 157 158 unsigned int getStructSize(const ShaderVariable &var) const; 159 getTessControlShaderOutputVertices()160 int getTessControlShaderOutputVertices() const { return mTessControlShaderOutputVertices; } getTessEvaluationShaderInputPrimitiveType()161 TLayoutTessEvaluationType getTessEvaluationShaderInputPrimitiveType() const 162 { 163 return mTessEvaluationShaderInputPrimitiveType; 164 } getTessEvaluationShaderInputVertexSpacingType()165 TLayoutTessEvaluationType getTessEvaluationShaderInputVertexSpacingType() const 166 { 167 return mTessEvaluationShaderInputVertexSpacingType; 168 } getTessEvaluationShaderInputOrderingType()169 TLayoutTessEvaluationType getTessEvaluationShaderInputOrderingType() const 170 { 171 return mTessEvaluationShaderInputOrderingType; 172 } getTessEvaluationShaderInputPointType()173 TLayoutTessEvaluationType getTessEvaluationShaderInputPointType() const 174 { 175 return mTessEvaluationShaderInputPointType; 176 } 177 178 unsigned int getSharedMemorySize() const; 179 getShaderType()180 sh::GLenum getShaderType() const { return mShaderType; } 181 182 bool validateAST(TIntermNode *root); 183 184 protected: 185 // Add emulated functions to the built-in function emulator. initBuiltInFunctionEmulator(BuiltInFunctionEmulator * emu,ShCompileOptions compileOptions)186 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, 187 ShCompileOptions compileOptions) 188 {} 189 // Translate to object code. May generate performance warnings through the diagnostics. 190 ANGLE_NO_DISCARD virtual bool translate(TIntermBlock *root, 191 ShCompileOptions compileOptions, 192 PerformanceDiagnostics *perfDiagnostics) = 0; 193 // Get built-in extensions with default behavior. 194 const TExtensionBehavior &getExtensionBehavior() const; 195 const char *getSourcePath() const; 196 // Relies on collectVariables having been called. 197 bool isVaryingDefined(const char *varyingName); 198 199 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const; 200 201 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0; 202 virtual bool shouldCollectVariables(ShCompileOptions compileOptions); 203 // If precision emulation needed, set isNeeded to true and emulate precision for given 204 // outputLanguage, returning false if that fails, else returning true. 205 bool emulatePrecisionIfNeeded(TIntermBlock *root, 206 TInfoSinkBase &sink, 207 bool *isNeeded, 208 const ShShaderOutput outputLanguage); 209 210 bool wereVariablesCollected() const; 211 std::vector<sh::ShaderVariable> mAttributes; 212 std::vector<sh::ShaderVariable> mOutputVariables; 213 std::vector<sh::ShaderVariable> mUniforms; 214 std::vector<sh::ShaderVariable> mInputVaryings; 215 std::vector<sh::ShaderVariable> mOutputVaryings; 216 std::vector<sh::ShaderVariable> mSharedVariables; 217 std::vector<sh::InterfaceBlock> mInterfaceBlocks; 218 std::vector<sh::InterfaceBlock> mUniformBlocks; 219 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 220 221 // Track what should be validated given passes currently applied. 222 ValidateASTOptions mValidateASTOptions; 223 224 // Specialization constant usage bits 225 SpecConstUsageBits mSpecConstUsageBits; 226 227 private: 228 // Initialize symbol-table with built-in symbols. 229 bool initBuiltInSymbolTable(const ShBuiltInResources &resources); 230 // Compute the string representation of the built-in resources 231 void setResourceString(); 232 // Return false if the call depth is exceeded. 233 bool checkCallDepth(); 234 // Insert statements to reference all members in unused uniform blocks with standard and shared 235 // layout. This is to work around a Mac driver that treats unused standard/shared 236 // uniform blocks as inactive. 237 ANGLE_NO_DISCARD bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root); 238 // Insert statements to initialize output variables in the beginning of main(). 239 // This is to avoid undefined behaviors. 240 ANGLE_NO_DISCARD bool initializeOutputVariables(TIntermBlock *root); 241 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main(). 242 // It is to work around a Linux driver bug where missing this causes compile failure 243 // while spec says it is allowed. 244 // This function should only be applied to vertex shaders. 245 ANGLE_NO_DISCARD bool initializeGLPosition(TIntermBlock *root); 246 // Return true if the maximum expression complexity is below the limit. 247 bool limitExpressionComplexity(TIntermBlock *root); 248 // Creates the function call DAG for further analysis, returning false if there is a recursion 249 bool initCallDag(TIntermNode *root); 250 // Return false if "main" doesn't exist 251 bool tagUsedFunctions(); 252 void internalTagUsedFunction(size_t index); 253 254 void collectInterfaceBlocks(); 255 256 bool mVariablesCollected; 257 258 bool mGLPositionInitialized; 259 260 // Removes unused function declarations and prototypes from the AST 261 class UnusedPredicate; 262 void pruneUnusedFunctions(TIntermBlock *root); 263 264 TIntermBlock *compileTreeImpl(const char *const shaderStrings[], 265 size_t numStrings, 266 const ShCompileOptions compileOptions); 267 268 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader 269 // version. 270 void setASTMetadata(const TParseContext &parseContext); 271 272 // Check if shader version meets the requirement. 273 bool checkShaderVersion(TParseContext *parseContext); 274 275 // Does checks that need to be run after parsing is complete and returns true if they pass. 276 bool checkAndSimplifyAST(TIntermBlock *root, 277 const TParseContext &parseContext, 278 ShCompileOptions compileOptions); 279 280 sh::GLenum mShaderType; 281 ShShaderSpec mShaderSpec; 282 ShShaderOutput mOutputType; 283 284 struct FunctionMetadata 285 { FunctionMetadataFunctionMetadata286 FunctionMetadata() : used(false) {} 287 bool used; 288 }; 289 290 CallDAG mCallDag; 291 std::vector<FunctionMetadata> mFunctionMetadata; 292 293 ShBuiltInResources mResources; 294 std::string mBuiltInResourcesString; 295 296 // Built-in symbol table for the given language, spec, and resources. 297 // It is preserved from compile-to-compile. 298 TSymbolTable mSymbolTable; 299 // Built-in extensions with default behavior. 300 TExtensionBehavior mExtensionBehavior; 301 302 BuiltInFunctionEmulator mBuiltInFunctionEmulator; 303 304 // Results of compilation. 305 int mShaderVersion; 306 TInfoSink mInfoSink; // Output sink. 307 TDiagnostics mDiagnostics; 308 const char *mSourcePath; // Path of source file or NULL 309 310 // fragment shader early fragment tests 311 bool mEarlyFragmentTestsSpecified; 312 bool mEarlyFragmentTestsOptimized; 313 314 // compute shader local group size 315 bool mComputeShaderLocalSizeDeclared; 316 sh::WorkGroupSize mComputeShaderLocalSize; 317 318 // GL_OVR_multiview num_views. 319 int mNumViews; 320 321 // geometry shader parameters. 322 int mGeometryShaderMaxVertices; 323 int mGeometryShaderInvocations; 324 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType; 325 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType; 326 327 // tesssellation shader parameters 328 int mTessControlShaderOutputVertices; 329 TLayoutTessEvaluationType mTessEvaluationShaderInputPrimitiveType; 330 TLayoutTessEvaluationType mTessEvaluationShaderInputVertexSpacingType; 331 TLayoutTessEvaluationType mTessEvaluationShaderInputOrderingType; 332 TLayoutTessEvaluationType mTessEvaluationShaderInputPointType; 333 334 // name hashing. 335 NameMap mNameMap; 336 337 TPragma mPragma; 338 339 ShCompileOptions mCompileOptions; 340 }; 341 342 // 343 // This is the interface between the machine independent code 344 // and the machine dependent code. 345 // 346 // The machine dependent code should derive from the classes 347 // above. Then Construct*() and Delete*() will create and 348 // destroy the machine dependent objects, which contain the 349 // above machine independent information. 350 // 351 TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); 352 void DeleteCompiler(TCompiler *); 353 354 } // namespace sh 355 356 #endif // COMPILER_TRANSLATOR_COMPILER_H_ 357