1 // 2 // Copyright 2020 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 // ProgramExecutable.h: Collects the information and interfaces common to both Programs and 7 // ProgramPipelines in order to execute/draw with either. 8 9 #ifndef LIBANGLE_PROGRAMEXECUTABLE_H_ 10 #define LIBANGLE_PROGRAMEXECUTABLE_H_ 11 12 #include "BinaryStream.h" 13 #include "libANGLE/Caps.h" 14 #include "libANGLE/InfoLog.h" 15 #include "libANGLE/ProgramLinkedResources.h" 16 #include "libANGLE/Shader.h" 17 #include "libANGLE/Uniform.h" 18 #include "libANGLE/VaryingPacking.h" 19 #include "libANGLE/angletypes.h" 20 21 namespace gl 22 { 23 24 // This small structure encapsulates binding sampler uniforms to active GL textures. 25 struct SamplerBinding 26 { 27 SamplerBinding(TextureType textureTypeIn, 28 GLenum samplerTypeIn, 29 SamplerFormat formatIn, 30 size_t elementCount); 31 SamplerBinding(const SamplerBinding &other); 32 ~SamplerBinding(); 33 34 // Necessary for retrieving active textures from the GL state. 35 TextureType textureType; 36 37 GLenum samplerType; 38 39 SamplerFormat format; 40 41 // List of all textures bound to this sampler, of type textureType. 42 // Cropped by the amount of unused elements reported by the driver. 43 std::vector<GLuint> boundTextureUnits; 44 }; 45 46 struct ImageBinding 47 { 48 ImageBinding(size_t count, TextureType textureTypeIn); 49 ImageBinding(GLuint imageUnit, size_t count, TextureType textureTypeIn); 50 ImageBinding(const ImageBinding &other); 51 ~ImageBinding(); 52 53 // Necessary for distinguishing between textures with images and texture buffers. 54 TextureType textureType; 55 56 // List of all textures bound. 57 // Cropped by the amount of unused elements reported by the driver. 58 std::vector<GLuint> boundImageUnits; 59 }; 60 61 // A varying with transform feedback enabled. If it's an array, either the whole array or one of its 62 // elements specified by 'arrayIndex' can set to be enabled. 63 struct TransformFeedbackVarying : public sh::ShaderVariable 64 { TransformFeedbackVaryingTransformFeedbackVarying65 TransformFeedbackVarying(const sh::ShaderVariable &varyingIn, GLuint arrayIndexIn) 66 : sh::ShaderVariable(varyingIn), arrayIndex(arrayIndexIn) 67 { 68 ASSERT(!isArrayOfArrays()); 69 } 70 TransformFeedbackVaryingTransformFeedbackVarying71 TransformFeedbackVarying(const sh::ShaderVariable &field, const sh::ShaderVariable &parent) 72 : arrayIndex(GL_INVALID_INDEX) 73 { 74 sh::ShaderVariable *thisVar = this; 75 *thisVar = field; 76 interpolation = parent.interpolation; 77 isInvariant = parent.isInvariant; 78 ASSERT(parent.isShaderIOBlock || !parent.name.empty()); 79 if (!parent.name.empty()) 80 { 81 name = parent.name + "." + name; 82 mappedName = parent.mappedName + "." + mappedName; 83 } 84 structOrBlockName = parent.structOrBlockName; 85 mappedStructOrBlockName = parent.mappedStructOrBlockName; 86 } 87 nameWithArrayIndexTransformFeedbackVarying88 std::string nameWithArrayIndex() const 89 { 90 std::stringstream fullNameStr; 91 fullNameStr << name; 92 if (arrayIndex != GL_INVALID_INDEX) 93 { 94 fullNameStr << "[" << arrayIndex << "]"; 95 } 96 return fullNameStr.str(); 97 } sizeTransformFeedbackVarying98 GLsizei size() const 99 { 100 return (isArray() && arrayIndex == GL_INVALID_INDEX ? getOutermostArraySize() : 1); 101 } 102 103 GLuint arrayIndex; 104 }; 105 106 class ProgramState; 107 class ProgramPipelineState; 108 109 class ProgramExecutable final : public angle::Subject 110 { 111 public: 112 ProgramExecutable(); 113 ProgramExecutable(const ProgramExecutable &other); 114 ~ProgramExecutable() override; 115 116 void reset(); 117 118 void save(bool isSeparable, gl::BinaryOutputStream *stream) const; 119 void load(bool isSeparable, gl::BinaryInputStream *stream); 120 121 int getInfoLogLength() const; getInfoLog()122 InfoLog &getInfoLog() { return mInfoLog; } 123 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const; 124 std::string getInfoLogString() const; resetInfoLog()125 void resetInfoLog() { mInfoLog.reset(); } 126 resetLinkedShaderStages()127 void resetLinkedShaderStages() { mLinkedShaderStages.reset(); } getLinkedShaderStages()128 const ShaderBitSet &getLinkedShaderStages() const { return mLinkedShaderStages; } setLinkedShaderStages(ShaderType shaderType)129 void setLinkedShaderStages(ShaderType shaderType) 130 { 131 mLinkedShaderStages.set(shaderType); 132 updateCanDrawWith(); 133 } hasLinkedShaderStage(ShaderType shaderType)134 bool hasLinkedShaderStage(ShaderType shaderType) const 135 { 136 ASSERT(shaderType != ShaderType::InvalidEnum); 137 return mLinkedShaderStages[shaderType]; 138 } getLinkedShaderStageCount()139 size_t getLinkedShaderStageCount() const { return mLinkedShaderStages.count(); } hasLinkedGraphicsShader()140 bool hasLinkedGraphicsShader() const 141 { 142 return mLinkedShaderStages.any() && 143 mLinkedShaderStages != gl::ShaderBitSet{gl::ShaderType::Compute}; 144 } hasLinkedTessellationShader()145 bool hasLinkedTessellationShader() const 146 { 147 return mLinkedShaderStages[ShaderType::TessEvaluation]; 148 } 149 150 ShaderType getLinkedTransformFeedbackStage() const; 151 getActiveAttribLocationsMask()152 const AttributesMask &getActiveAttribLocationsMask() const 153 { 154 return mActiveAttribLocationsMask; 155 } 156 bool isAttribLocationActive(size_t attribLocation) const; getNonBuiltinAttribLocationsMask()157 const AttributesMask &getNonBuiltinAttribLocationsMask() const { return mAttributesMask; } getMaxActiveAttribLocation()158 unsigned int getMaxActiveAttribLocation() const { return mMaxActiveAttribLocation; } getAttributesTypeMask()159 const ComponentTypeMask &getAttributesTypeMask() const { return mAttributesTypeMask; } 160 AttributesMask getAttributesMask() const; 161 getActiveSamplersMask()162 const ActiveTextureMask &getActiveSamplersMask() const { return mActiveSamplersMask; } setActiveTextureMask(ActiveTextureMask mask)163 void setActiveTextureMask(ActiveTextureMask mask) { mActiveSamplersMask = mask; } getSamplerFormatForTextureUnitIndex(size_t textureUnitIndex)164 SamplerFormat getSamplerFormatForTextureUnitIndex(size_t textureUnitIndex) const 165 { 166 return mActiveSamplerFormats[textureUnitIndex]; 167 } getSamplerShaderBitsForTextureUnitIndex(size_t textureUnitIndex)168 const ShaderBitSet getSamplerShaderBitsForTextureUnitIndex(size_t textureUnitIndex) const 169 { 170 return mActiveSamplerShaderBits[textureUnitIndex]; 171 } getActiveImagesMask()172 const ActiveTextureMask &getActiveImagesMask() const { return mActiveImagesMask; } setActiveImagesMask(ActiveTextureMask mask)173 void setActiveImagesMask(ActiveTextureMask mask) { mActiveImagesMask = mask; } getActiveImageShaderBits()174 const ActiveTextureArray<ShaderBitSet> &getActiveImageShaderBits() const 175 { 176 return mActiveImageShaderBits; 177 } 178 getActiveYUVSamplers()179 const ActiveTextureMask &getActiveYUVSamplers() const { return mActiveSamplerYUV; } 180 getActiveSamplerTypes()181 const ActiveTextureArray<TextureType> &getActiveSamplerTypes() const 182 { 183 return mActiveSamplerTypes; 184 } 185 186 void updateActiveSamplers(const ProgramState &programState); 187 188 bool hasDefaultUniforms() const; 189 bool hasTextures() const; 190 bool hasUniformBuffers() const; 191 bool hasStorageBuffers() const; 192 bool hasAtomicCounterBuffers() const; 193 bool hasImages() const; hasTransformFeedbackOutput()194 bool hasTransformFeedbackOutput() const 195 { 196 return !getLinkedTransformFeedbackVaryings().empty(); 197 } 198 bool usesFramebufferFetch() const; 199 200 // Count the number of uniform and storage buffer declarations, counting arrays as one. getTransformFeedbackBufferCount()201 size_t getTransformFeedbackBufferCount() const { return mTransformFeedbackStrides.size(); } 202 203 void updateCanDrawWith(); hasVertexShader()204 bool hasVertexShader() const { return mCanDrawWith; } 205 getProgramInputs()206 const std::vector<sh::ShaderVariable> &getProgramInputs() const { return mProgramInputs; } getOutputVariables()207 const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; } getOutputLocations()208 const std::vector<VariableLocation> &getOutputLocations() const { return mOutputLocations; } getSecondaryOutputLocations()209 const std::vector<VariableLocation> &getSecondaryOutputLocations() const 210 { 211 return mSecondaryOutputLocations; 212 } getUniforms()213 const std::vector<LinkedUniform> &getUniforms() const { return mUniforms; } getUniformBlocks()214 const std::vector<InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } getActiveUniformBlockBindings()215 const UniformBlockBindingMask &getActiveUniformBlockBindings() const 216 { 217 return mActiveUniformBlockBindings; 218 } getSamplerBindings()219 const std::vector<SamplerBinding> &getSamplerBindings() const { return mSamplerBindings; } getImageBindings()220 const std::vector<ImageBinding> &getImageBindings() const { return mImageBindings; } getImageBindings()221 std::vector<ImageBinding> *getImageBindings() { return &mImageBindings; } getDefaultUniformRange()222 const RangeUI &getDefaultUniformRange() const { return mDefaultUniformRange; } getSamplerUniformRange()223 const RangeUI &getSamplerUniformRange() const { return mSamplerUniformRange; } getImageUniformRange()224 const RangeUI &getImageUniformRange() const { return mImageUniformRange; } getAtomicCounterUniformRange()225 const RangeUI &getAtomicCounterUniformRange() const { return mAtomicCounterUniformRange; } getFragmentInoutRange()226 const RangeUI &getFragmentInoutRange() const { return mFragmentInoutRange; } usesEarlyFragmentTestsOptimization()227 bool usesEarlyFragmentTestsOptimization() const { return mUsesEarlyFragmentTestsOptimization; } getAdvancedBlendEquations()228 BlendEquationBitSet getAdvancedBlendEquations() const { return mAdvancedBlendEquations; } getLinkedTransformFeedbackVaryings()229 const std::vector<TransformFeedbackVarying> &getLinkedTransformFeedbackVaryings() const 230 { 231 return mLinkedTransformFeedbackVaryings; 232 } getTransformFeedbackBufferMode()233 GLint getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; } getUniformBlockBinding(GLuint uniformBlockIndex)234 GLuint getUniformBlockBinding(GLuint uniformBlockIndex) const 235 { 236 ASSERT(uniformBlockIndex < mUniformBlocks.size()); 237 return mUniformBlocks[uniformBlockIndex].binding; 238 } getShaderStorageBlockBinding(GLuint blockIndex)239 GLuint getShaderStorageBlockBinding(GLuint blockIndex) const 240 { 241 ASSERT(blockIndex < mShaderStorageBlocks.size()); 242 return mShaderStorageBlocks[blockIndex].binding; 243 } getTransformFeedbackStrides()244 const std::vector<GLsizei> &getTransformFeedbackStrides() const 245 { 246 return mTransformFeedbackStrides; 247 } getAtomicCounterBuffers()248 const std::vector<AtomicCounterBuffer> &getAtomicCounterBuffers() const 249 { 250 return mAtomicCounterBuffers; 251 } getShaderStorageBlocks()252 const std::vector<InterfaceBlock> &getShaderStorageBlocks() const 253 { 254 return mShaderStorageBlocks; 255 } getUniformByIndex(GLuint index)256 const LinkedUniform &getUniformByIndex(GLuint index) const 257 { 258 ASSERT(index < static_cast<size_t>(mUniforms.size())); 259 return mUniforms[index]; 260 } 261 getActiveUniformBlockCount()262 ANGLE_INLINE GLuint getActiveUniformBlockCount() const 263 { 264 return static_cast<GLuint>(mUniformBlocks.size()); 265 } 266 getActiveAtomicCounterBufferCount()267 ANGLE_INLINE GLuint getActiveAtomicCounterBufferCount() const 268 { 269 return static_cast<GLuint>(mAtomicCounterBuffers.size()); 270 } 271 getActiveShaderStorageBlockCount()272 ANGLE_INLINE GLuint getActiveShaderStorageBlockCount() const 273 { 274 size_t shaderStorageBlocksSize = mShaderStorageBlocks.size(); 275 return static_cast<GLuint>(shaderStorageBlocksSize); 276 } 277 278 GLuint getUniformIndexFromImageIndex(GLuint imageIndex) const; 279 280 GLuint getUniformIndexFromSamplerIndex(GLuint samplerIndex) const; 281 282 void saveLinkedStateInfo(const ProgramState &state); getLinkedOutputVaryings(ShaderType shaderType)283 const std::vector<sh::ShaderVariable> &getLinkedOutputVaryings(ShaderType shaderType) const 284 { 285 return mLinkedOutputVaryings[shaderType]; 286 } getLinkedInputVaryings(ShaderType shaderType)287 const std::vector<sh::ShaderVariable> &getLinkedInputVaryings(ShaderType shaderType) const 288 { 289 return mLinkedInputVaryings[shaderType]; 290 } 291 getLinkedUniforms(ShaderType shaderType)292 const std::vector<sh::ShaderVariable> &getLinkedUniforms(ShaderType shaderType) const 293 { 294 return mLinkedUniforms[shaderType]; 295 } 296 getLinkedUniformBlocks(ShaderType shaderType)297 const std::vector<sh::InterfaceBlock> &getLinkedUniformBlocks(ShaderType shaderType) const 298 { 299 return mLinkedUniformBlocks[shaderType]; 300 } 301 getLinkedShaderVersion(ShaderType shaderType)302 int getLinkedShaderVersion(ShaderType shaderType) const 303 { 304 return mLinkedShaderVersions[shaderType]; 305 } 306 307 bool isYUVOutput() const; 308 getGeometryShaderInputPrimitiveType()309 PrimitiveMode getGeometryShaderInputPrimitiveType() const 310 { 311 return mGeometryShaderInputPrimitiveType; 312 } 313 getGeometryShaderOutputPrimitiveType()314 PrimitiveMode getGeometryShaderOutputPrimitiveType() const 315 { 316 return mGeometryShaderOutputPrimitiveType; 317 } 318 getGeometryShaderInvocations()319 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; } 320 getGeometryShaderMaxVertices()321 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; } 322 getTessGenMode()323 GLenum getTessGenMode() const { return mTessGenMode; } 324 resetCachedValidateSamplersResult()325 void resetCachedValidateSamplersResult() { mCachedValidateSamplersResult.reset(); } validateSamplers(InfoLog * infoLog,const Caps & caps)326 bool validateSamplers(InfoLog *infoLog, const Caps &caps) const 327 { 328 // Use the cache if: 329 // - we aren't using an info log (which gives the full error). 330 // - The sample mapping hasn't changed and we've already validated. 331 if (infoLog == nullptr && mCachedValidateSamplersResult.valid()) 332 { 333 return mCachedValidateSamplersResult.value(); 334 } 335 336 return validateSamplersImpl(infoLog, caps); 337 } 338 getFragmentOutputsTypeMask()339 ComponentTypeMask getFragmentOutputsTypeMask() const { return mDrawBufferTypeMask; } getActiveOutputVariablesMask()340 DrawBufferMask getActiveOutputVariablesMask() const { return mActiveOutputVariablesMask; } 341 342 bool linkUniforms(const Context *context, 343 const ShaderMap<std::vector<sh::ShaderVariable>> &shaderUniforms, 344 InfoLog &infoLog, 345 const ProgramAliasedBindings &uniformLocationBindings, 346 GLuint *combinedImageUniformsCount, 347 std::vector<UnusedUniform> *unusedUniforms, 348 std::vector<VariableLocation> *uniformLocationsOutOrNull); 349 350 void copyInputsFromProgram(const ProgramState &programState); 351 void copyShaderBuffersFromProgram(const ProgramState &programState, ShaderType shaderType); 352 void clearSamplerBindings(); 353 void copySamplerBindingsFromProgram(const ProgramState &programState); 354 void copyImageBindingsFromProgram(const ProgramState &programState); 355 void copyOutputsFromProgram(const ProgramState &programState); 356 void copyUniformsFromProgramMap(const ShaderMap<Program *> &programs); 357 358 private: 359 // TODO(timvp): http://anglebug.com/3570: Investigate removing these friend 360 // class declarations and accessing the necessary members with getters/setters. 361 friend class Program; 362 friend class ProgramPipeline; 363 friend class ProgramState; 364 365 void updateActiveImages(const ProgramExecutable &executable); 366 367 // Scans the sampler bindings for type conflicts with sampler 'textureUnitIndex'. 368 void setSamplerUniformTextureTypeAndFormat(size_t textureUnitIndex, 369 std::vector<SamplerBinding> &samplerBindings); 370 371 bool linkMergedVaryings(const Context *context, 372 const ProgramMergedVaryings &mergedVaryings, 373 const std::vector<std::string> &transformFeedbackVaryingNames, 374 const LinkingVariables &linkingVariables, 375 bool isSeparable, 376 ProgramVaryingPacking *varyingPacking); 377 378 bool linkValidateTransformFeedback( 379 const Context *context, 380 const ProgramMergedVaryings &varyings, 381 ShaderType stage, 382 const std::vector<std::string> &transformFeedbackVaryingNames); 383 384 void gatherTransformFeedbackVaryings( 385 const ProgramMergedVaryings &varyings, 386 ShaderType stage, 387 const std::vector<std::string> &transformFeedbackVaryingNames); 388 389 void updateTransformFeedbackStrides(); 390 391 bool validateSamplersImpl(InfoLog *infoLog, const Caps &caps) const; 392 393 bool linkValidateOutputVariables(const Caps &caps, 394 const Extensions &extensions, 395 const Version &version, 396 GLuint combinedImageUniformsCount, 397 GLuint combinedShaderStorageBlocksCount, 398 const std::vector<sh::ShaderVariable> &outputVariables, 399 int fragmentShaderVersion, 400 const ProgramAliasedBindings &fragmentOutputLocations, 401 const ProgramAliasedBindings &fragmentOutputIndices); 402 403 void linkSamplerAndImageBindings(GLuint *combinedImageUniformsCount); 404 bool linkAtomicCounterBuffers(const Context *context, InfoLog &infoLog); 405 406 InfoLog mInfoLog; 407 408 ShaderBitSet mLinkedShaderStages; 409 410 angle::BitSet<MAX_VERTEX_ATTRIBS> mActiveAttribLocationsMask; 411 unsigned int mMaxActiveAttribLocation; 412 ComponentTypeMask mAttributesTypeMask; 413 // mAttributesMask is identical to mActiveAttribLocationsMask with built-in attributes removed. 414 AttributesMask mAttributesMask; 415 416 // Cached mask of active samplers and sampler types. 417 ActiveTextureMask mActiveSamplersMask; 418 ActiveTextureArray<uint32_t> mActiveSamplerRefCounts; 419 ActiveTextureArray<TextureType> mActiveSamplerTypes; 420 ActiveTextureMask mActiveSamplerYUV; 421 ActiveTextureArray<SamplerFormat> mActiveSamplerFormats; 422 ActiveTextureArray<ShaderBitSet> mActiveSamplerShaderBits; 423 424 // Cached mask of active images. 425 ActiveTextureMask mActiveImagesMask; 426 ActiveTextureArray<ShaderBitSet> mActiveImageShaderBits; 427 428 bool mCanDrawWith; 429 430 // Names and mapped names of output variables that are arrays include [0] in the end, similarly 431 // to uniforms. 432 std::vector<sh::ShaderVariable> mOutputVariables; 433 std::vector<VariableLocation> mOutputLocations; 434 DrawBufferMask mActiveOutputVariablesMask; 435 // EXT_blend_func_extended secondary outputs (ones with index 1) 436 std::vector<VariableLocation> mSecondaryOutputLocations; 437 bool mYUVOutput; 438 // Vertex attributes, Fragment input varyings, etc. 439 std::vector<sh::ShaderVariable> mProgramInputs; 440 std::vector<TransformFeedbackVarying> mLinkedTransformFeedbackVaryings; 441 // The size of the data written to each transform feedback buffer per vertex. 442 std::vector<GLsizei> mTransformFeedbackStrides; 443 GLenum mTransformFeedbackBufferMode; 444 // Uniforms are sorted in order: 445 // 1. Non-opaque uniforms 446 // 2. Sampler uniforms 447 // 3. Image uniforms 448 // 4. Atomic counter uniforms 449 // 5. Subpass Input uniforms (Only for Vulkan) 450 // 6. Uniform block uniforms 451 // This makes opaque uniform validation easier, since we don't need a separate list. 452 // For generating the entries and naming them we follow the spec: GLES 3.1 November 2016 section 453 // 7.3.1.1 Naming Active Resources. There's a separate entry for each struct member and each 454 // inner array of an array of arrays. Names and mapped names of uniforms that are arrays include 455 // [0] in the end. This makes implementation of queries simpler. 456 std::vector<LinkedUniform> mUniforms; 457 RangeUI mDefaultUniformRange; 458 RangeUI mSamplerUniformRange; 459 RangeUI mImageUniformRange; 460 RangeUI mAtomicCounterUniformRange; 461 std::vector<InterfaceBlock> mUniformBlocks; 462 463 // For faster iteration on the blocks currently being bound. 464 UniformBlockBindingMask mActiveUniformBlockBindings; 465 466 std::vector<AtomicCounterBuffer> mAtomicCounterBuffers; 467 std::vector<InterfaceBlock> mShaderStorageBlocks; 468 469 RangeUI mFragmentInoutRange; 470 bool mUsesEarlyFragmentTestsOptimization; 471 472 // KHR_blend_equation_advanced supported equation list 473 BlendEquationBitSet mAdvancedBlendEquations; 474 475 // An array of the samplers that are used by the program 476 std::vector<SamplerBinding> mSamplerBindings; 477 478 // An array of the images that are used by the program 479 std::vector<ImageBinding> mImageBindings; 480 481 ShaderMap<std::vector<sh::ShaderVariable>> mLinkedOutputVaryings; 482 ShaderMap<std::vector<sh::ShaderVariable>> mLinkedInputVaryings; 483 ShaderMap<std::vector<sh::ShaderVariable>> mLinkedUniforms; 484 ShaderMap<std::vector<sh::InterfaceBlock>> mLinkedUniformBlocks; 485 486 ShaderMap<int> mLinkedShaderVersions; 487 488 // GL_EXT_geometry_shader. 489 PrimitiveMode mGeometryShaderInputPrimitiveType; 490 PrimitiveMode mGeometryShaderOutputPrimitiveType; 491 int mGeometryShaderInvocations; 492 int mGeometryShaderMaxVertices; 493 494 // GL_EXT_tessellation_shader 495 int mTessControlShaderVertices; 496 GLenum mTessGenMode; 497 GLenum mTessGenSpacing; 498 GLenum mTessGenVertexOrder; 499 GLenum mTessGenPointMode; 500 501 // Fragment output variable base types: FLOAT, INT, or UINT. Ordered by location. 502 std::vector<GLenum> mOutputVariableTypes; 503 ComponentTypeMask mDrawBufferTypeMask; 504 505 // Cache for sampler validation 506 mutable Optional<bool> mCachedValidateSamplersResult; 507 }; 508 } // namespace gl 509 510 #endif // LIBANGLE_PROGRAMEXECUTABLE_H_ 511