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 TranslatorMSL; 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 const 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 nullptr; } 68 #ifdef ANGLE_ENABLE_HLSL getAsTranslatorHLSL()69 virtual TranslatorHLSL *getAsTranslatorHLSL() { return nullptr; } 70 #endif // ANGLE_ENABLE_HLSL 71 #ifdef ANGLE_ENABLE_METAL getAsTranslatorMSL()72 virtual TranslatorMSL *getAsTranslatorMSL() { 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 const ShCompileOptions &compileOptions); 105 106 bool compile(const char *const shaderStrings[], 107 size_t numStrings, 108 const ShCompileOptions &compileOptions); 109 110 // Get results of the last compilation. getShaderVersion()111 int getShaderVersion() const { return mShaderVersion; } getInfoSink()112 TInfoSink &getInfoSink() { return mInfoSink; } 113 specifyEarlyFragmentTests()114 bool specifyEarlyFragmentTests() { return mEarlyFragmentTestsSpecified = true; } isEarlyFragmentTestsSpecified()115 bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; } hasDiscard()116 bool hasDiscard() const { return mHasDiscard; } enablesPerSampleShading()117 bool enablesPerSampleShading() const { return mEnablesPerSampleShading; } getSpecConstUsageBits()118 SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; } 119 isComputeShaderLocalSizeDeclared()120 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; } getComputeShaderLocalSize()121 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; } getNumViews()122 int getNumViews() const { return mNumViews; } 123 124 // Clears the results from the previous compilation. 125 void clearResults(); 126 getAttributes()127 const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; } getOutputVariables()128 const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; } getUniforms()129 const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; } getInputVaryings()130 const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; } getOutputVaryings()131 const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; } getInterfaceBlocks()132 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; } getUniformBlocks()133 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } getShaderStorageBlocks()134 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const 135 { 136 return mShaderStorageBlocks; 137 } 138 getHashFunction()139 ShHashFunction64 getHashFunction() const { return mResources.HashFunction; } getNameMap()140 NameMap &getNameMap() { return mNameMap; } getSymbolTable()141 TSymbolTable &getSymbolTable() { return mSymbolTable; } getShaderSpec()142 ShShaderSpec getShaderSpec() const { return mShaderSpec; } getOutputType()143 ShShaderOutput getOutputType() const { return mOutputType; } getBuiltInResources()144 const ShBuiltInResources &getBuiltInResources() const { return mResources; } getBuiltInResourcesString()145 const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; } 146 147 bool isHighPrecisionSupported() const; 148 149 bool shouldRunLoopAndIndexingValidation(const ShCompileOptions &compileOptions) const; 150 bool shouldLimitTypeSizes() const; 151 152 // Get the resources set by InitBuiltInSymbolTable 153 const ShBuiltInResources &getResources() const; 154 getPragma()155 const TPragma &getPragma() const { return mPragma; } 156 getGeometryShaderMaxVertices()157 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; } getGeometryShaderInvocations()158 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; } getGeometryShaderInputPrimitiveType()159 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const 160 { 161 return mGeometryShaderInputPrimitiveType; 162 } getGeometryShaderOutputPrimitiveType()163 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const 164 { 165 return mGeometryShaderOutputPrimitiveType; 166 } 167 168 unsigned int getStructSize(const ShaderVariable &var) const; 169 getTessControlShaderOutputVertices()170 int getTessControlShaderOutputVertices() const { return mTessControlShaderOutputVertices; } getTessEvaluationShaderInputPrimitiveType()171 TLayoutTessEvaluationType getTessEvaluationShaderInputPrimitiveType() const 172 { 173 return mTessEvaluationShaderInputPrimitiveType; 174 } getTessEvaluationShaderInputVertexSpacingType()175 TLayoutTessEvaluationType getTessEvaluationShaderInputVertexSpacingType() const 176 { 177 return mTessEvaluationShaderInputVertexSpacingType; 178 } getTessEvaluationShaderInputOrderingType()179 TLayoutTessEvaluationType getTessEvaluationShaderInputOrderingType() const 180 { 181 return mTessEvaluationShaderInputOrderingType; 182 } getTessEvaluationShaderInputPointType()183 TLayoutTessEvaluationType getTessEvaluationShaderInputPointType() const 184 { 185 return mTessEvaluationShaderInputPointType; 186 } 187 hasAnyPreciseType()188 bool hasAnyPreciseType() const { return mHasAnyPreciseType; } 189 getAdvancedBlendEquations()190 AdvancedBlendEquations getAdvancedBlendEquations() const { return mAdvancedBlendEquations; } 191 hasPixelLocalStorageUniforms()192 bool hasPixelLocalStorageUniforms() const { return mHasPixelLocalStorageUniforms; } 193 194 unsigned int getSharedMemorySize() const; 195 getShaderType()196 sh::GLenum getShaderType() const { return mShaderType; } 197 198 // Generate a self-contained binary representation of the shader. 199 bool getShaderBinary(const ShHandle compilerHandle, 200 const char *const shaderStrings[], 201 size_t numStrings, 202 const ShCompileOptions &compileOptions, 203 ShaderBinaryBlob *const binaryOut); 204 205 // Validate the AST and produce errors if it is inconsistent. 206 bool validateAST(TIntermNode *root); 207 // Some transformations may need to temporarily disable validation until they are complete. A 208 // set of disable/enable helpers are used for this purpose. 209 bool disableValidateFunctionCall(); 210 void restoreValidateFunctionCall(bool enable); 211 bool disableValidateVariableReferences(); 212 void restoreValidateVariableReferences(bool enable); 213 // When the AST is post-processed (such as to determine precise-ness of intermediate nodes), 214 // it's expected to no longer transform. 215 void enableValidateNoMoreTransformations(); 216 areClipDistanceOrCullDistanceRedeclared()217 bool areClipDistanceOrCullDistanceRedeclared() const 218 { 219 return mClipDistanceRedeclared || mCullDistanceRedeclared; 220 } 221 getClipDistanceArraySize()222 uint8_t getClipDistanceArraySize() const { return mClipDistanceSize; } 223 getCullDistanceArraySize()224 uint8_t getCullDistanceArraySize() const { return mCullDistanceSize; } 225 isClipDistanceRedeclared()226 bool isClipDistanceRedeclared() const { return mClipDistanceRedeclared; } 227 hasClipDistance()228 bool hasClipDistance() const { return mClipDistanceUsed; } 229 230 protected: 231 // Add emulated functions to the built-in function emulator. initBuiltInFunctionEmulator(BuiltInFunctionEmulator * emu,const ShCompileOptions & compileOptions)232 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, 233 const ShCompileOptions &compileOptions) 234 {} 235 // Translate to object code. May generate performance warnings through the diagnostics. 236 [[nodiscard]] virtual bool translate(TIntermBlock *root, 237 const ShCompileOptions &compileOptions, 238 PerformanceDiagnostics *perfDiagnostics) = 0; 239 // Get built-in extensions with default behavior. 240 const TExtensionBehavior &getExtensionBehavior() const; 241 const char *getSourcePath() const; 242 // Relies on collectVariables having been called. 243 bool isVaryingDefined(const char *varyingName); 244 245 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const; 246 247 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0; 248 249 std::vector<sh::ShaderVariable> mAttributes; 250 std::vector<sh::ShaderVariable> mOutputVariables; 251 std::vector<sh::ShaderVariable> mUniforms; 252 std::vector<sh::ShaderVariable> mInputVaryings; 253 std::vector<sh::ShaderVariable> mOutputVaryings; 254 std::vector<sh::ShaderVariable> mSharedVariables; 255 std::vector<sh::InterfaceBlock> mInterfaceBlocks; 256 std::vector<sh::InterfaceBlock> mUniformBlocks; 257 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 258 259 // Track what should be validated given passes currently applied. 260 ValidateASTOptions mValidateASTOptions; 261 262 // Specialization constant usage bits 263 SpecConstUsageBits mSpecConstUsageBits; 264 265 private: 266 // Initialize symbol-table with built-in symbols. 267 bool initBuiltInSymbolTable(const ShBuiltInResources &resources); 268 // Compute the string representation of the built-in resources 269 void setResourceString(); 270 // Return false if the call depth is exceeded. 271 bool checkCallDepth(); 272 // Insert statements to reference all members in unused uniform blocks with standard and shared 273 // layout. This is to work around a Mac driver that treats unused standard/shared 274 // uniform blocks as inactive. 275 [[nodiscard]] bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root); 276 // Insert statements to initialize output variables in the beginning of main(). 277 // This is to avoid undefined behaviors. 278 [[nodiscard]] bool initializeOutputVariables(TIntermBlock *root); 279 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main(). 280 // It is to work around a Linux driver bug where missing this causes compile failure 281 // while spec says it is allowed. 282 // This function should only be applied to vertex shaders. 283 [[nodiscard]] bool initializeGLPosition(TIntermBlock *root); 284 // Return true if the maximum expression complexity is below the limit. 285 bool limitExpressionComplexity(TIntermBlock *root); 286 // Creates the function call DAG for further analysis, returning false if there is a recursion 287 bool initCallDag(TIntermNode *root); 288 // Return false if "main" doesn't exist 289 bool tagUsedFunctions(); 290 void internalTagUsedFunction(size_t index); 291 292 void collectInterfaceBlocks(); 293 294 bool mVariablesCollected; 295 296 bool mGLPositionInitialized; 297 298 // Removes unused function declarations and prototypes from the AST 299 bool pruneUnusedFunctions(TIntermBlock *root); 300 301 TIntermBlock *compileTreeImpl(const char *const shaderStrings[], 302 size_t numStrings, 303 const ShCompileOptions &compileOptions); 304 305 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader 306 // version. 307 void setASTMetadata(const TParseContext &parseContext); 308 309 // Check if shader version meets the requirement. 310 bool checkShaderVersion(TParseContext *parseContext); 311 312 // Does checks that need to be run after parsing is complete and returns true if they pass. 313 bool checkAndSimplifyAST(TIntermBlock *root, 314 const TParseContext &parseContext, 315 const ShCompileOptions &compileOptions); 316 317 bool resizeClipAndCullDistanceBuiltins(TIntermBlock *root); 318 319 bool postParseChecks(const TParseContext &parseContext); 320 321 sh::GLenum mShaderType; 322 ShShaderSpec mShaderSpec; 323 ShShaderOutput mOutputType; 324 325 CallDAG mCallDag; 326 std::vector<TFunctionMetadata> mFunctionMetadata; 327 328 ShBuiltInResources mResources; 329 std::string mBuiltInResourcesString; 330 331 // Built-in symbol table for the given language, spec, and resources. 332 // It is preserved from compile-to-compile. 333 TSymbolTable mSymbolTable; 334 // Built-in extensions with default behavior. 335 TExtensionBehavior mExtensionBehavior; 336 337 BuiltInFunctionEmulator mBuiltInFunctionEmulator; 338 339 // Results of compilation. 340 int mShaderVersion; 341 TInfoSink mInfoSink; // Output sink. 342 TDiagnostics mDiagnostics; 343 const char *mSourcePath; // Path of source file or NULL 344 345 // Fragment shader early fragment tests 346 bool mEarlyFragmentTestsSpecified; 347 348 // Fragment shader has the discard instruction 349 bool mHasDiscard; 350 351 // Whether per-sample shading is enabled by the shader. In OpenGL, this keyword should 352 // implicitly trigger per-sample shading without the API enabling it. 353 bool mEnablesPerSampleShading; 354 355 // compute shader local group size 356 bool mComputeShaderLocalSizeDeclared; 357 sh::WorkGroupSize mComputeShaderLocalSize; 358 359 // GL_OVR_multiview num_views. 360 int mNumViews; 361 362 // Track gl_ClipDistance / gl_CullDistance usage. 363 uint8_t mClipDistanceSize; 364 uint8_t mCullDistanceSize; 365 bool mClipDistanceRedeclared; 366 bool mCullDistanceRedeclared; 367 bool mClipDistanceUsed; 368 369 // geometry shader parameters. 370 int mGeometryShaderMaxVertices; 371 int mGeometryShaderInvocations; 372 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType; 373 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType; 374 375 // tesssellation shader parameters 376 int mTessControlShaderOutputVertices; 377 TLayoutTessEvaluationType mTessEvaluationShaderInputPrimitiveType; 378 TLayoutTessEvaluationType mTessEvaluationShaderInputVertexSpacingType; 379 TLayoutTessEvaluationType mTessEvaluationShaderInputOrderingType; 380 TLayoutTessEvaluationType mTessEvaluationShaderInputPointType; 381 382 bool mHasAnyPreciseType; 383 384 // advanced blend equation parameters 385 AdvancedBlendEquations mAdvancedBlendEquations; 386 387 // ANGLE_shader_pixel_local_storage. 388 bool mHasPixelLocalStorageUniforms; 389 390 // name hashing. 391 NameMap mNameMap; 392 393 TPragma mPragma; 394 395 ShCompileOptions mCompileOptions; 396 }; 397 398 // 399 // This is the interface between the machine independent code 400 // and the machine dependent code. 401 // 402 // The machine dependent code should derive from the classes 403 // above. Then Construct*() and Delete*() will create and 404 // destroy the machine dependent objects, which contain the 405 // above machine independent information. 406 // 407 TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); 408 void DeleteCompiler(TCompiler *); 409 410 struct ShaderDumpHeader 411 { 412 uint32_t type; 413 uint32_t spec; 414 uint32_t output; 415 uint8_t basicCompileOptions[32]; 416 uint8_t metalCompileOptions[32]; 417 uint8_t plsCompileOptions[32]; 418 uint8_t padding[20]; 419 }; 420 static_assert(sizeof(ShaderDumpHeader) == 128); 421 422 } // namespace sh 423 424 #endif // COMPILER_TRANSLATOR_COMPILER_H_ 425