1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef skgpu_graphite_DawnResourceProvider_DEFINED 9 #define skgpu_graphite_DawnResourceProvider_DEFINED 10 11 #include "include/gpu/graphite/dawn/DawnGraphiteTypes.h" 12 #include "src/core/SkLRUCache.h" 13 #include "src/core/SkTHash.h" 14 #include "src/gpu/graphite/PipelineData.h" 15 #include "src/gpu/graphite/ResourceProvider.h" 16 17 namespace skgpu::graphite { 18 19 class DawnGraphicsPipeline; 20 class DawnSampler; 21 class DawnSharedContext; 22 class DawnTexture; 23 class DawnBuffer; 24 class DawnCommandBuffer; 25 26 class DawnResourceProvider final : public ResourceProvider { 27 public: 28 template <size_t NumEntries> 29 using BindGroupKey = FixedSizeKey<2 * NumEntries>; 30 31 static constexpr size_t kNumUniformEntries = 4; 32 33 DawnResourceProvider(SharedContext* sharedContext, 34 SingleOwner*, 35 uint32_t recorderID, 36 size_t resourceBudget); 37 ~DawnResourceProvider() override; 38 39 sk_sp<DawnTexture> findOrCreateDiscardableMSAALoadTexture(SkISize dimensions, 40 const TextureInfo& msaaInfo); 41 42 wgpu::RenderPipeline findOrCreateBlitWithDrawPipeline(const RenderPassDesc& renderPassDesc, 43 bool srcIsMSAA); 44 45 sk_sp<DawnBuffer> findOrCreateDawnBuffer(size_t size, 46 BufferType type, 47 AccessPattern, 48 std::string_view label); 49 50 const wgpu::BindGroupLayout& getOrCreateUniformBuffersBindGroupLayout(); 51 const wgpu::BindGroupLayout& getOrCreateSingleTextureSamplerBindGroupLayout(); 52 53 // Find the cached bind group or create a new one based on the bound buffers and their 54 // binding sizes (boundBuffersAndSizes) for these uniforms (in order): 55 // - Intrinsic constants. 56 // - Render step uniforms. 57 // - Paint uniforms. 58 const wgpu::BindGroup& findOrCreateUniformBuffersBindGroup( 59 const std::array<std::pair<const DawnBuffer*, uint32_t>, kNumUniformEntries>& 60 boundBuffersAndSizes); 61 62 // Find or create a bind group containing the given sampler & texture. 63 const wgpu::BindGroup& findOrCreateSingleTextureSamplerBindGroup(const DawnSampler* sampler, 64 const DawnTexture* texture); 65 66 // Find the cached bind buffer info, or create a new one for the given intrinsic values. 67 BindBufferInfo findOrCreateIntrinsicBindBufferInfo(DawnCommandBuffer* cb, 68 UniformDataBlock intrinsicValues); 69 70 private: 71 sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*, 72 const UniqueKey&, 73 const GraphicsPipelineDesc&, 74 const RenderPassDesc&, 75 SkEnumBitMask<PipelineCreationFlags>, 76 uint32_t compilationID) override; 77 sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override; 78 79 sk_sp<Texture> createTexture(SkISize, const TextureInfo&) override; 80 sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) override; 81 82 sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) override; 83 84 sk_sp<Sampler> createSampler(const SamplerDesc&) override; 85 86 BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override; 87 void onDeleteBackendTexture(const BackendTexture&) override; 88 89 const wgpu::Buffer& getOrCreateNullBuffer(); 90 91 DawnSharedContext* dawnSharedContext() const; 92 93 void onFreeGpuResources() override; 94 void onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) override; 95 96 skia_private::THashMap<uint32_t, wgpu::RenderPipeline> fBlitWithDrawPipelines; 97 98 wgpu::BindGroupLayout fUniformBuffersBindGroupLayout; 99 wgpu::BindGroupLayout fSingleTextureSamplerBindGroupLayout; 100 101 wgpu::Buffer fNullBuffer; 102 103 template <size_t NumEntries> 104 using BindGroupCache = SkLRUCache<BindGroupKey<NumEntries>, 105 wgpu::BindGroup, 106 typename BindGroupKey<NumEntries>::Hash>; 107 108 BindGroupCache<kNumUniformEntries> fUniformBufferBindGroupCache; 109 BindGroupCache<1> fSingleTextureSamplerBindGroups; 110 111 class IntrinsicBuffer; 112 class IntrinsicConstantsManager; 113 std::unique_ptr<IntrinsicConstantsManager> fIntrinsicConstantsManager; 114 115 SingleOwner* fSingleOwner = nullptr; 116 }; 117 118 } // namespace skgpu::graphite 119 120 #endif // skgpu_graphite_DawnResourceProvider_DEFINED 121