1*8975f5c5SAndroid Build Coastguard Worker // 2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2023 The ANGLE Project Authors. All rights reserved. 3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file. 5*8975f5c5SAndroid Build Coastguard Worker // 6*8975f5c5SAndroid Build Coastguard Worker // ShareGroupVk.h: 7*8975f5c5SAndroid Build Coastguard Worker // Defines the class interface for ShareGroupVk, implementing ShareGroupImpl. 8*8975f5c5SAndroid Build Coastguard Worker // 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_RENDERER_VULKAN_SHAREGROUPVK_H_ 11*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_RENDERER_VULKAN_SHAREGROUPVK_H_ 12*8975f5c5SAndroid Build Coastguard Worker 13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/ShareGroupImpl.h" 14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_cache_utils.h" 15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_helpers.h" 16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_resource.h" 17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_utils.h" 18*8975f5c5SAndroid Build Coastguard Worker 19*8975f5c5SAndroid Build Coastguard Worker namespace rx 20*8975f5c5SAndroid Build Coastguard Worker { 21*8975f5c5SAndroid Build Coastguard Worker constexpr VkDeviceSize kMaxTotalEmptyBufferBytes = 16 * 1024 * 1024; 22*8975f5c5SAndroid Build Coastguard Worker 23*8975f5c5SAndroid Build Coastguard Worker class TextureUpload 24*8975f5c5SAndroid Build Coastguard Worker { 25*8975f5c5SAndroid Build Coastguard Worker public: TextureUpload()26*8975f5c5SAndroid Build Coastguard Worker TextureUpload() { mPrevUploadedMutableTexture = nullptr; } ~TextureUpload()27*8975f5c5SAndroid Build Coastguard Worker ~TextureUpload() { resetPrevTexture(); } 28*8975f5c5SAndroid Build Coastguard Worker angle::Result onMutableTextureUpload(ContextVk *contextVk, TextureVk *newTexture); 29*8975f5c5SAndroid Build Coastguard Worker void onTextureRelease(TextureVk *textureVk); resetPrevTexture()30*8975f5c5SAndroid Build Coastguard Worker void resetPrevTexture() { mPrevUploadedMutableTexture = nullptr; } 31*8975f5c5SAndroid Build Coastguard Worker 32*8975f5c5SAndroid Build Coastguard Worker private: 33*8975f5c5SAndroid Build Coastguard Worker // Keep track of the previously stored texture. Used to flush mutable textures. 34*8975f5c5SAndroid Build Coastguard Worker TextureVk *mPrevUploadedMutableTexture; 35*8975f5c5SAndroid Build Coastguard Worker }; 36*8975f5c5SAndroid Build Coastguard Worker 37*8975f5c5SAndroid Build Coastguard Worker class ShareGroupVk : public ShareGroupImpl 38*8975f5c5SAndroid Build Coastguard Worker { 39*8975f5c5SAndroid Build Coastguard Worker public: 40*8975f5c5SAndroid Build Coastguard Worker ShareGroupVk(const egl::ShareGroupState &state, vk::Renderer *renderer); 41*8975f5c5SAndroid Build Coastguard Worker void onDestroy(const egl::Display *display) override; 42*8975f5c5SAndroid Build Coastguard Worker 43*8975f5c5SAndroid Build Coastguard Worker void onContextAdd() override; 44*8975f5c5SAndroid Build Coastguard Worker getFramebufferCache()45*8975f5c5SAndroid Build Coastguard Worker FramebufferCache &getFramebufferCache() { return mFramebufferCache; } 46*8975f5c5SAndroid Build Coastguard Worker hasAnyContextWithRobustness()47*8975f5c5SAndroid Build Coastguard Worker bool hasAnyContextWithRobustness() const { return mState.hasAnyContextWithRobustness(); } 48*8975f5c5SAndroid Build Coastguard Worker 49*8975f5c5SAndroid Build Coastguard Worker // PipelineLayoutCache and DescriptorSetLayoutCache can be shared between multiple threads 50*8975f5c5SAndroid Build Coastguard Worker // accessing them via shared contexts. The ShareGroup locks around gl entrypoints ensuring 51*8975f5c5SAndroid Build Coastguard Worker // synchronous update to the caches. getPipelineLayoutCache()52*8975f5c5SAndroid Build Coastguard Worker PipelineLayoutCache &getPipelineLayoutCache() { return mPipelineLayoutCache; } getDescriptorSetLayoutCache()53*8975f5c5SAndroid Build Coastguard Worker DescriptorSetLayoutCache &getDescriptorSetLayoutCache() { return mDescriptorSetLayoutCache; } getContexts()54*8975f5c5SAndroid Build Coastguard Worker const egl::ContextMap &getContexts() const { return mState.getContexts(); } getMetaDescriptorPools()55*8975f5c5SAndroid Build Coastguard Worker vk::DescriptorSetArray<vk::MetaDescriptorPool> &getMetaDescriptorPools() 56*8975f5c5SAndroid Build Coastguard Worker { 57*8975f5c5SAndroid Build Coastguard Worker return mMetaDescriptorPools; 58*8975f5c5SAndroid Build Coastguard Worker } 59*8975f5c5SAndroid Build Coastguard Worker 60*8975f5c5SAndroid Build Coastguard Worker // Used to flush the mutable textures more often. 61*8975f5c5SAndroid Build Coastguard Worker angle::Result onMutableTextureUpload(ContextVk *contextVk, TextureVk *newTexture); 62*8975f5c5SAndroid Build Coastguard Worker 63*8975f5c5SAndroid Build Coastguard Worker vk::BufferPool *getDefaultBufferPool(VkDeviceSize size, 64*8975f5c5SAndroid Build Coastguard Worker uint32_t memoryTypeIndex, 65*8975f5c5SAndroid Build Coastguard Worker BufferUsageType usageType); 66*8975f5c5SAndroid Build Coastguard Worker 67*8975f5c5SAndroid Build Coastguard Worker void pruneDefaultBufferPools(); 68*8975f5c5SAndroid Build Coastguard Worker 69*8975f5c5SAndroid Build Coastguard Worker void calculateTotalBufferCount(size_t *bufferCount, VkDeviceSize *totalSize) const; 70*8975f5c5SAndroid Build Coastguard Worker void logBufferPools() const; 71*8975f5c5SAndroid Build Coastguard Worker 72*8975f5c5SAndroid Build Coastguard Worker // Temporary workaround until VkSemaphore(s) will be used between different priorities. 73*8975f5c5SAndroid Build Coastguard Worker angle::Result unifyContextsPriority(ContextVk *newContextVk); 74*8975f5c5SAndroid Build Coastguard Worker // Temporary workaround until VkSemaphore(s) will be used between different priorities. 75*8975f5c5SAndroid Build Coastguard Worker angle::Result lockDefaultContextsPriority(ContextVk *contextVk); 76*8975f5c5SAndroid Build Coastguard Worker getUpdateDescriptorSetsBuilder()77*8975f5c5SAndroid Build Coastguard Worker UpdateDescriptorSetsBuilder *getUpdateDescriptorSetsBuilder() 78*8975f5c5SAndroid Build Coastguard Worker { 79*8975f5c5SAndroid Build Coastguard Worker return &mUpdateDescriptorSetsBuilder; 80*8975f5c5SAndroid Build Coastguard Worker } 81*8975f5c5SAndroid Build Coastguard Worker 82*8975f5c5SAndroid Build Coastguard Worker void onTextureRelease(TextureVk *textureVk); 83*8975f5c5SAndroid Build Coastguard Worker getVertexInputGraphicsPipelineCache()84*8975f5c5SAndroid Build Coastguard Worker VertexInputGraphicsPipelineCache *getVertexInputGraphicsPipelineCache() 85*8975f5c5SAndroid Build Coastguard Worker { 86*8975f5c5SAndroid Build Coastguard Worker return &mVertexInputGraphicsPipelineCache; 87*8975f5c5SAndroid Build Coastguard Worker } getFragmentOutputGraphicsPipelineCache()88*8975f5c5SAndroid Build Coastguard Worker FragmentOutputGraphicsPipelineCache *getFragmentOutputGraphicsPipelineCache() 89*8975f5c5SAndroid Build Coastguard Worker { 90*8975f5c5SAndroid Build Coastguard Worker return &mFragmentOutputGraphicsPipelineCache; 91*8975f5c5SAndroid Build Coastguard Worker } 92*8975f5c5SAndroid Build Coastguard Worker 93*8975f5c5SAndroid Build Coastguard Worker angle::Result scheduleMonolithicPipelineCreationTask( 94*8975f5c5SAndroid Build Coastguard Worker ContextVk *contextVk, 95*8975f5c5SAndroid Build Coastguard Worker vk::WaitableMonolithicPipelineCreationTask *taskOut); 96*8975f5c5SAndroid Build Coastguard Worker void waitForCurrentMonolithicPipelineCreationTask(); 97*8975f5c5SAndroid Build Coastguard Worker getRefCountedEventsGarbageRecycler()98*8975f5c5SAndroid Build Coastguard Worker vk::RefCountedEventsGarbageRecycler *getRefCountedEventsGarbageRecycler() 99*8975f5c5SAndroid Build Coastguard Worker { 100*8975f5c5SAndroid Build Coastguard Worker return &mRefCountedEventsGarbageRecycler; 101*8975f5c5SAndroid Build Coastguard Worker } cleanupRefCountedEventGarbage()102*8975f5c5SAndroid Build Coastguard Worker void cleanupRefCountedEventGarbage() { mRefCountedEventsGarbageRecycler.cleanup(mRenderer); } cleanupExcessiveRefCountedEventGarbage()103*8975f5c5SAndroid Build Coastguard Worker void cleanupExcessiveRefCountedEventGarbage() 104*8975f5c5SAndroid Build Coastguard Worker { 105*8975f5c5SAndroid Build Coastguard Worker // TODO: b/336844257 needs tune. 106*8975f5c5SAndroid Build Coastguard Worker constexpr size_t kExcessiveGarbageCountThreshold = 256; 107*8975f5c5SAndroid Build Coastguard Worker if (mRefCountedEventsGarbageRecycler.getGarbageCount() > kExcessiveGarbageCountThreshold) 108*8975f5c5SAndroid Build Coastguard Worker { 109*8975f5c5SAndroid Build Coastguard Worker mRefCountedEventsGarbageRecycler.cleanup(mRenderer); 110*8975f5c5SAndroid Build Coastguard Worker } 111*8975f5c5SAndroid Build Coastguard Worker } 112*8975f5c5SAndroid Build Coastguard Worker 113*8975f5c5SAndroid Build Coastguard Worker void onFramebufferBoundary(); getCurrentFrameCount()114*8975f5c5SAndroid Build Coastguard Worker uint32_t getCurrentFrameCount() const { return mCurrentFrameCount; } 115*8975f5c5SAndroid Build Coastguard Worker 116*8975f5c5SAndroid Build Coastguard Worker private: 117*8975f5c5SAndroid Build Coastguard Worker angle::Result updateContextsPriority(ContextVk *contextVk, egl::ContextPriority newPriority); 118*8975f5c5SAndroid Build Coastguard Worker 119*8975f5c5SAndroid Build Coastguard Worker bool isDueForBufferPoolPrune(); 120*8975f5c5SAndroid Build Coastguard Worker 121*8975f5c5SAndroid Build Coastguard Worker vk::Renderer *mRenderer; 122*8975f5c5SAndroid Build Coastguard Worker 123*8975f5c5SAndroid Build Coastguard Worker // Tracks the total number of frames rendered. 124*8975f5c5SAndroid Build Coastguard Worker uint32_t mCurrentFrameCount; 125*8975f5c5SAndroid Build Coastguard Worker 126*8975f5c5SAndroid Build Coastguard Worker // VkFramebuffer caches 127*8975f5c5SAndroid Build Coastguard Worker FramebufferCache mFramebufferCache; 128*8975f5c5SAndroid Build Coastguard Worker resetPrevTexture()129*8975f5c5SAndroid Build Coastguard Worker void resetPrevTexture() { mTextureUpload.resetPrevTexture(); } 130*8975f5c5SAndroid Build Coastguard Worker 131*8975f5c5SAndroid Build Coastguard Worker // ANGLE uses a PipelineLayout cache to store compatible pipeline layouts. 132*8975f5c5SAndroid Build Coastguard Worker PipelineLayoutCache mPipelineLayoutCache; 133*8975f5c5SAndroid Build Coastguard Worker 134*8975f5c5SAndroid Build Coastguard Worker // DescriptorSetLayouts are also managed in a cache. 135*8975f5c5SAndroid Build Coastguard Worker DescriptorSetLayoutCache mDescriptorSetLayoutCache; 136*8975f5c5SAndroid Build Coastguard Worker 137*8975f5c5SAndroid Build Coastguard Worker // Descriptor set caches 138*8975f5c5SAndroid Build Coastguard Worker vk::DescriptorSetArray<vk::MetaDescriptorPool> mMetaDescriptorPools; 139*8975f5c5SAndroid Build Coastguard Worker 140*8975f5c5SAndroid Build Coastguard Worker // Priority of all Contexts in the context set 141*8975f5c5SAndroid Build Coastguard Worker egl::ContextPriority mContextsPriority; 142*8975f5c5SAndroid Build Coastguard Worker bool mIsContextsPriorityLocked; 143*8975f5c5SAndroid Build Coastguard Worker 144*8975f5c5SAndroid Build Coastguard Worker // Storage for vkUpdateDescriptorSets 145*8975f5c5SAndroid Build Coastguard Worker UpdateDescriptorSetsBuilder mUpdateDescriptorSetsBuilder; 146*8975f5c5SAndroid Build Coastguard Worker 147*8975f5c5SAndroid Build Coastguard Worker // The per shared group buffer pools that all buffers should sub-allocate from. 148*8975f5c5SAndroid Build Coastguard Worker vk::BufferPoolPointerArray mDefaultBufferPools; 149*8975f5c5SAndroid Build Coastguard Worker 150*8975f5c5SAndroid Build Coastguard Worker // The system time when last pruneEmptyBuffer gets called. 151*8975f5c5SAndroid Build Coastguard Worker double mLastPruneTime; 152*8975f5c5SAndroid Build Coastguard Worker 153*8975f5c5SAndroid Build Coastguard Worker // Used when VK_EXT_graphics_pipeline_library is available, the vertex input and fragment output 154*8975f5c5SAndroid Build Coastguard Worker // partial pipelines are created in the following caches. These caches are in the share group 155*8975f5c5SAndroid Build Coastguard Worker // because linked pipelines using these pipeline libraries are referenced from 156*8975f5c5SAndroid Build Coastguard Worker // ProgramExecutableVk, and as such must stay alive as long as the program may be alive. 157*8975f5c5SAndroid Build Coastguard Worker VertexInputGraphicsPipelineCache mVertexInputGraphicsPipelineCache; 158*8975f5c5SAndroid Build Coastguard Worker FragmentOutputGraphicsPipelineCache mFragmentOutputGraphicsPipelineCache; 159*8975f5c5SAndroid Build Coastguard Worker 160*8975f5c5SAndroid Build Coastguard Worker // The system time when the last monolithic pipeline creation job was launched. This is 161*8975f5c5SAndroid Build Coastguard Worker // rate-limited to avoid hogging all cores and interfering with the application threads. A 162*8975f5c5SAndroid Build Coastguard Worker // single pipeline creation job is currently supported. 163*8975f5c5SAndroid Build Coastguard Worker double mLastMonolithicPipelineJobTime; 164*8975f5c5SAndroid Build Coastguard Worker std::shared_ptr<angle::WaitableEvent> mMonolithicPipelineCreationEvent; 165*8975f5c5SAndroid Build Coastguard Worker 166*8975f5c5SAndroid Build Coastguard Worker // Texture update manager used to flush uploaded mutable textures. 167*8975f5c5SAndroid Build Coastguard Worker TextureUpload mTextureUpload; 168*8975f5c5SAndroid Build Coastguard Worker 169*8975f5c5SAndroid Build Coastguard Worker // Holds RefCountedEvent that are free and ready to reuse 170*8975f5c5SAndroid Build Coastguard Worker vk::RefCountedEventsGarbageRecycler mRefCountedEventsGarbageRecycler; 171*8975f5c5SAndroid Build Coastguard Worker }; 172*8975f5c5SAndroid Build Coastguard Worker } // namespace rx 173*8975f5c5SAndroid Build Coastguard Worker 174*8975f5c5SAndroid Build Coastguard Worker #endif // LIBANGLE_RENDERER_VULKAN_SHAREGROUPVK_H_ 175