1 /* 2 * Copyright 2019 Google Inc. 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 GrDawnGpu_DEFINED 9 #define GrDawnGpu_DEFINED 10 11 #include "src/gpu/GrGpu.h" 12 13 #include "dawn/webgpu_cpp.h" 14 #include "src/core/SkLRUCache.h" 15 #include "src/gpu/GrFinishCallbacks.h" 16 #include "src/gpu/GrProgramDesc.h" 17 #include "src/gpu/GrStagingBufferManager.h" 18 #include "src/gpu/dawn/GrDawnRingBuffer.h" 19 #include "src/sksl/ir/SkSLProgram.h" 20 21 #include <unordered_map> 22 23 class GrDawnOpsRenderPass; 24 class GrDawnStagingBuffer; 25 class GrDirectContext; 26 class GrPipeline; 27 struct GrDawnProgram; 28 29 class GrDawnGpu : public GrGpu { 30 public: 31 static sk_sp<GrGpu> Make(const wgpu::Device&, const GrContextOptions&, GrDirectContext*); 32 33 ~GrDawnGpu() override; 34 35 void disconnect(DisconnectType) override; 36 37 GrThreadSafePipelineBuilder* pipelineBuilder() override; 38 sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() override; 39 stagingBufferManager()40 GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; } 41 void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override; 42 device()43 const wgpu::Device& device() const { return fDevice; } queue()44 const wgpu::Queue& queue() const { return fQueue; } 45 xferBarrier(GrRenderTarget *,GrXferBarrierType)46 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} 47 48 void deleteBackendTexture(const GrBackendTexture&) override; 49 50 bool compile(const GrProgramDesc&, const GrProgramInfo&) override; 51 52 #if GR_TEST_UTILS 53 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override; 54 55 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(SkISize dimensions, 56 GrColorType, 57 int sampleCnt, 58 GrProtected) override; 59 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override; 60 #endif 61 62 sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& /*colorFormat*/, 63 SkISize dimensions, int numStencilSamples) override; 64 getPreferredStencilFormat(const GrBackendFormat &)65 GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) override { 66 return GrBackendFormat::MakeDawn(wgpu::TextureFormat::Depth24PlusStencil8); 67 } 68 makeMSAAAttachment(SkISize dimensions,const GrBackendFormat & format,int numSamples,GrProtected isProtected)69 sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions, 70 const GrBackendFormat& format, 71 int numSamples, 72 GrProtected isProtected) override { 73 return nullptr; 74 } 75 76 void submit(GrOpsRenderPass*) override; 77 78 GrFence SK_WARN_UNUSED_RESULT insertFence() override; 79 bool waitFence(GrFence) override; 80 void deleteFence(GrFence) const override; 81 82 std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true) override; 83 std::unique_ptr<GrSemaphore> wrapBackendSemaphore( 84 const GrBackendSemaphore& semaphore, 85 GrResourceProvider::SemaphoreWrapType wrapType, 86 GrWrapOwnership ownership) override; 87 void insertSemaphore(GrSemaphore* semaphore) override; 88 void waitSemaphore(GrSemaphore* semaphore) override; 89 void checkFinishProcs() override; 90 void finishOutstandingGpuWork() override; 91 92 std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override; 93 94 sk_sp<GrDawnProgram> getOrCreateRenderPipeline(GrRenderTarget*, const GrProgramInfo&); 95 96 wgpu::Sampler getOrCreateSampler(GrSamplerState samplerState); 97 98 GrDawnRingBuffer::Slice allocateUniformRingBufferSlice(int size); 99 wgpu::CommandEncoder getCopyEncoder(); 100 void flushCopyEncoder(); 101 void appendCommandBuffer(wgpu::CommandBuffer commandBuffer); 102 103 void waitOnAllBusyStagingBuffers(); 104 SkSL::String SkSLToSPIRV(const char* shaderString, SkSL::ProgramKind, bool flipY, 105 uint32_t rtHeightOffset, SkSL::Program::Inputs*); 106 wgpu::ShaderModule createShaderModule(const SkSL::String& spirvSource); 107 108 private: 109 GrDawnGpu(GrDirectContext*, const GrContextOptions&, const wgpu::Device&); 110 111 sk_sp<GrTexture> onCreateTexture(SkISize, 112 const GrBackendFormat&, 113 GrRenderable, 114 int renderTargetSampleCnt, 115 SkBudgeted, 116 GrProtected, 117 int mipLevelCount, 118 uint32_t levelClearMask) override; 119 120 sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions, 121 const GrBackendFormat&, 122 SkBudgeted, 123 GrMipmapped, 124 GrProtected, 125 const void* data, size_t dataSize) override; 126 127 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, 128 GrWrapOwnership, 129 GrWrapCacheable, 130 GrIOType) override; 131 sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&, 132 GrWrapOwnership, 133 GrWrapCacheable) override; 134 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, 135 int sampleCnt, 136 GrWrapOwnership, 137 GrWrapCacheable) override; 138 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; 139 140 GrBackendTexture onCreateBackendTexture(SkISize dimensions, 141 const GrBackendFormat&, 142 GrRenderable, 143 GrMipmapped, 144 GrProtected) override; 145 146 bool onClearBackendTexture(const GrBackendTexture&, 147 sk_sp<GrRefCntedCallback> finishedCallback, 148 std::array<float, 4> color) override; 149 150 GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions, 151 const GrBackendFormat&, 152 GrMipmapped, 153 GrProtected) override; 154 155 bool onUpdateCompressedBackendTexture(const GrBackendTexture&, 156 sk_sp<GrRefCntedCallback> finishedCallback, 157 const void* data, 158 size_t size) override; 159 160 sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType type, GrAccessPattern, 161 const void* data) override; 162 163 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, 164 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer, 165 size_t rowBytes) override; 166 167 bool onWritePixels(GrSurface* surface, int left, int top, int width, int height, 168 GrColorType surfaceColorType, GrColorType srcColorType, 169 const GrMipLevel texels[], int mipLevelCount, 170 bool prepForTexSampling) override; 171 172 bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height, 173 GrColorType textureColorType, GrColorType bufferColorType, 174 sk_sp<GrGpuBuffer> transferBuffer, size_t offset, 175 size_t rowBytes) override; 176 177 bool onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, 178 GrColorType surfaceColorType, GrColorType bufferColorType, 179 sk_sp<GrGpuBuffer> transferBuffer, size_t offset) override; 180 onResolveRenderTarget(GrRenderTarget *,const SkIRect &)181 void onResolveRenderTarget(GrRenderTarget*, const SkIRect&) override {} 182 183 bool onRegenerateMipMapLevels(GrTexture*) override; 184 185 bool onCopySurface(GrSurface* dst, GrSurface* src, 186 const SkIRect& srcRect, const SkIPoint& dstPoint) override; 187 188 void addFinishedProc(GrGpuFinishedProc finishedProc, 189 GrGpuFinishedContext finishedContext) override; 190 191 GrOpsRenderPass* onGetOpsRenderPass(GrRenderTarget*, 192 bool useMSAASurface, 193 GrAttachment*, 194 GrSurfaceOrigin, 195 const SkIRect&, 196 const GrOpsRenderPass::LoadAndStoreInfo&, 197 const GrOpsRenderPass::StencilLoadAndStoreInfo&, 198 const SkTArray<GrSurfaceProxy*, true>& sampledProxies, 199 GrXferBarrierFlags renderPassXferBarriers) override; 200 201 bool onSubmitToGpu(bool syncCpu) override; 202 203 void uploadTextureData(GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount, 204 const SkIRect& rect, wgpu::Texture texture); 205 206 void moveStagingBuffersToBusyAndMapAsync(); 207 void checkForCompletedStagingBuffers(); 208 209 wgpu::Device fDevice; 210 wgpu::Queue fQueue; 211 std::unique_ptr<GrDawnOpsRenderPass> fOpsRenderPass; 212 GrDawnRingBuffer fUniformRingBuffer; 213 wgpu::CommandEncoder fCopyEncoder; 214 std::vector<wgpu::CommandBuffer> fCommandBuffers; 215 GrStagingBufferManager fStagingBufferManager; 216 std::list<sk_sp<GrGpuBuffer>> fBusyStagingBuffers; 217 // Temporary array of staging buffers to hold refs on the staging buffers between detaching 218 // from the GrStagingManager and moving them to the busy list which must happen after 219 // submission. 220 std::vector<sk_sp<GrGpuBuffer>> fSubmittedStagingBuffers; 221 222 struct ProgramDescHash { operatorProgramDescHash223 uint32_t operator()(const GrProgramDesc& desc) const { 224 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); 225 } 226 }; 227 228 struct SamplerHash { operatorSamplerHash229 size_t operator()(GrSamplerState samplerState) const { 230 return SkOpts::hash_fn(&samplerState, sizeof(samplerState), 0); 231 } 232 }; 233 234 SkLRUCache<GrProgramDesc, sk_sp<GrDawnProgram>, ProgramDescHash> fRenderPipelineCache; 235 std::unordered_map<GrSamplerState, wgpu::Sampler, SamplerHash> fSamplers; 236 237 GrFinishCallbacks fFinishCallbacks; 238 239 using INHERITED = GrGpu; 240 }; 241 242 #endif 243