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