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/DawnTypes.h" 12 #include "src/core/SkLRUCache.h" 13 #include "src/core/SkTHash.h" 14 #include "src/gpu/graphite/ResourceProvider.h" 15 16 namespace skgpu::graphite { 17 18 class DawnGraphicsPipeline; 19 class DawnSampler; 20 class DawnSharedContext; 21 class DawnTexture; 22 class DawnBuffer; 23 24 class DawnResourceProvider final : public ResourceProvider { 25 public: 26 DawnResourceProvider(SharedContext* sharedContext, 27 SingleOwner*, 28 uint32_t recorderID, 29 size_t resourceBudget); 30 ~DawnResourceProvider() override; 31 32 sk_sp<DawnTexture> findOrCreateDiscardableMSAALoadTexture(SkISize dimensions, 33 const TextureInfo& msaaInfo); 34 35 wgpu::RenderPipeline findOrCreateBlitWithDrawPipeline(const RenderPassDesc& renderPassDesc); 36 37 sk_sp<DawnBuffer> findOrCreateDawnBuffer(size_t size, 38 BufferType type, 39 AccessPattern, 40 std::string_view label); 41 42 const wgpu::BindGroupLayout& getOrCreateUniformBuffersBindGroupLayout(); 43 const wgpu::BindGroupLayout& getOrCreateSingleTextureSamplerBindGroupLayout(); 44 45 // Find the cached bind group or create a new one based on the bound buffers and their 46 // binding sizes (boundBuffersAndSizes) for these uniforms (in order): 47 // - Intrinsic constants. 48 // - Render step uniforms. 49 // - Paint uniforms. 50 const wgpu::BindGroup& findOrCreateUniformBuffersBindGroup( 51 const std::array<std::pair<const DawnBuffer*, uint32_t>, 4>& boundBuffersAndSizes); 52 53 // Find or create a bind group containing the given sampler & texture. 54 const wgpu::BindGroup& findOrCreateSingleTextureSamplerBindGroup(const DawnSampler* sampler, 55 const DawnTexture* texture); 56 57 const sk_sp<DawnBuffer>& getOrCreateIntrinsicConstantBuffer(); 58 59 private: 60 sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*, 61 const GraphicsPipelineDesc&, 62 const RenderPassDesc&) override; 63 sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override; 64 65 sk_sp<Texture> createTexture(SkISize, const TextureInfo&, skgpu::Budgeted) override; 66 sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) override; 67 68 sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) override; 69 70 sk_sp<Sampler> createSampler(const SamplerDesc&) override; 71 72 BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override; 73 void onDeleteBackendTexture(const BackendTexture&) override; 74 75 const wgpu::Buffer& getOrCreateNullBuffer(); 76 77 DawnSharedContext* dawnSharedContext() const; 78 79 skia_private::THashMap<uint64_t, wgpu::RenderPipeline> fBlitWithDrawPipelines; 80 81 wgpu::BindGroupLayout fUniformBuffersBindGroupLayout; 82 wgpu::BindGroupLayout fSingleTextureSamplerBindGroupLayout; 83 84 wgpu::Buffer fNullBuffer; 85 86 sk_sp<DawnBuffer> fIntrinsicConstantBuffer; 87 88 struct UniqueKeyHash { operatorUniqueKeyHash89 uint32_t operator()(const skgpu::UniqueKey& key) const { return key.hash(); } 90 }; 91 92 using BindGroupCache = SkLRUCache<UniqueKey, wgpu::BindGroup, UniqueKeyHash>; 93 94 BindGroupCache fUniformBufferBindGroupCache; 95 BindGroupCache fSingleTextureSamplerBindGroups; 96 }; 97 98 } // namespace skgpu::graphite 99 100 #endif // skgpu_graphite_DawnResourceProvider_DEFINED 101