• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 GrD3DGpu_DEFINED
9 #define GrD3DGpu_DEFINED
10 
11 #include "include/private/base/SkDeque.h"
12 #include "src/gpu/ganesh/GrGpu.h"
13 #include "src/gpu/ganesh/GrRenderTarget.h"
14 #include "src/gpu/ganesh/GrSemaphore.h"
15 #include "src/gpu/ganesh/GrStagingBufferManager.h"
16 #include "src/gpu/ganesh/d3d/GrD3DCaps.h"
17 #include "src/gpu/ganesh/d3d/GrD3DCommandList.h"
18 #include "src/gpu/ganesh/d3d/GrD3DResourceProvider.h"
19 
20 struct GrD3DBackendContext;
21 class GrD3DOpsRenderPass;
22 struct GrD3DOptions;
23 class GrPipeline;
24 #if GR_TEST_UTILS
25 struct IDXGraphicsAnalysis;
26 #endif
27 
28 class GrD3DGpu : public GrGpu {
29 public:
30     static sk_sp<GrGpu> Make(const GrD3DBackendContext& backendContext, const GrContextOptions&,
31                              GrDirectContext*);
32 
33     ~GrD3DGpu() override;
34 
d3dCaps()35     const GrD3DCaps& d3dCaps() const { return static_cast<const GrD3DCaps&>(*this->caps()); }
36 
resourceProvider()37     GrD3DResourceProvider& resourceProvider() { return fResourceProvider; }
38 
39     GrThreadSafePipelineBuilder* pipelineBuilder() override;
40     sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() override;
41 
device()42     ID3D12Device* device() const { return fDevice.get(); }
queue()43     ID3D12CommandQueue* queue() const { return fQueue.get(); }
44 
memoryAllocator()45     GrD3DMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); }
46 
currentCommandList()47     GrD3DDirectCommandList* currentCommandList() const { return fCurrentDirectCommandList.get(); }
48 
stagingBufferManager()49     GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; }
50     void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override;
51 
uniformsRingBuffer()52     GrRingBuffer* uniformsRingBuffer() override { return &fConstantsRingBuffer; }
53 
protectedContext()54     bool protectedContext() const { return false; }
55 
xferBarrier(GrRenderTarget *,GrXferBarrierType)56     void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
57 
58     void deleteBackendTexture(const GrBackendTexture&) override;
59 
60     bool compile(const GrProgramDesc&, const GrProgramInfo&) override;
61 
62 #if GR_TEST_UTILS
63     bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
64 
65     GrBackendRenderTarget createTestingOnlyBackendRenderTarget(SkISize dimensions,
66                                                                GrColorType,
67                                                                int sampleCnt,
68                                                                GrProtected) override;
69     void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
70 
71     void testingOnly_startCapture() override;
72     void testingOnly_stopCapture() override;
73 
resetShaderCacheForTesting()74     void resetShaderCacheForTesting() const override {
75         fResourceProvider.resetShaderCacheForTesting();
76     }
77 #endif
78 
79     sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& /*colorFormat*/,
80                                               SkISize dimensions, int numStencilSamples) override;
81 
getPreferredStencilFormat(const GrBackendFormat &)82     GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) override {
83         return GrBackendFormat::MakeDxgi(this->d3dCaps().preferredStencilFormat());
84     }
85 
makeMSAAAttachment(SkISize dimensions,const GrBackendFormat & format,int numSamples,GrProtected isProtected,GrMemoryless isMemoryless)86     sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions,
87                                            const GrBackendFormat& format,
88                                            int numSamples,
89                                            GrProtected isProtected,
90                                            GrMemoryless isMemoryless) override {
91         return nullptr;
92     }
93 
94     void addResourceBarriers(sk_sp<GrManagedResource> resource,
95                              int numBarriers,
96                              D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const;
97 
98     void addBufferResourceBarriers(GrD3DBuffer* buffer,
99                                    int numBarriers,
100                                    D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const;
101 
102     GrFence SK_WARN_UNUSED_RESULT insertFence() override;
103     bool waitFence(GrFence) override;
deleteFence(GrFence)104     void deleteFence(GrFence) override {}
105 
106     std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
107     std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
108                                                       GrSemaphoreWrapType,
109                                                       GrWrapOwnership) override;
110     void insertSemaphore(GrSemaphore* semaphore) override;
111     void waitSemaphore(GrSemaphore* semaphore) override;
prepareTextureForCrossContextUsage(GrTexture *)112     std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override {
113         return nullptr;
114     }
115 
116     void submit(GrOpsRenderPass* renderPass) override;
117     void endRenderPass(GrRenderTarget* target, GrSurfaceOrigin origin,
118                        const SkIRect& bounds);
119 
checkFinishProcs()120     void checkFinishProcs() override { this->checkForFinishedCommandLists(); }
121     void finishOutstandingGpuWork() override;
122 
123 private:
124     enum class SyncQueue {
125         kForce,
126         kSkip
127     };
128 
129     GrD3DGpu(GrDirectContext*, const GrContextOptions&, const GrD3DBackendContext&,
130              sk_sp<GrD3DMemoryAllocator>);
131 
132     void destroyResources();
133 
134     sk_sp<GrTexture> onCreateTexture(SkISize,
135                                      const GrBackendFormat&,
136                                      GrRenderable,
137                                      int renderTargetSampleCnt,
138                                      skgpu::Budgeted,
139                                      GrProtected,
140                                      int mipLevelCount,
141                                      uint32_t levelClearMask,
142                                      std::string_view label) override;
143 
144     sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions,
145                                                const GrBackendFormat&,
146                                                skgpu::Budgeted,
147                                                GrMipmapped,
148                                                GrProtected,
149                                                const void* data,
150                                                size_t dataSize) override;
151 
152     sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
153                                           GrWrapOwnership,
154                                           GrWrapCacheable,
155                                           GrIOType) override;
156     sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&,
157                                                     GrWrapOwnership,
158                                                     GrWrapCacheable) override;
159 
160     sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
161                                                     int sampleCnt,
162                                                     GrWrapOwnership,
163                                                     GrWrapCacheable) override;
164 
165     sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
166 
167     sk_sp<GrGpuBuffer> onCreateBuffer(size_t sizeInBytes,
168                                       GrGpuBufferType,
169                                       GrAccessPattern) override;
170 
171     bool onReadPixels(GrSurface*,
172                       SkIRect,
173                       GrColorType surfaceColorType,
174                       GrColorType dstColorType,
175                       void*,
176                       size_t rowBytes) override;
177 
178     bool onWritePixels(GrSurface*,
179                        SkIRect,
180                        GrColorType surfaceColorType,
181                        GrColorType srcColorType,
182                        const GrMipLevel[],
183                        int mipLevelCount,
184                        bool prepForTexSampling) override;
185 
186     bool onTransferFromBufferToBuffer(sk_sp<GrGpuBuffer> src,
187                                       size_t srcOffset,
188                                       sk_sp<GrGpuBuffer> dst,
189                                       size_t dstOffset,
190                                       size_t size) override;
191 
192     bool onTransferPixelsTo(GrTexture*,
193                             SkIRect,
194                             GrColorType surfaceColorType,
195                             GrColorType bufferColorType,
196                             sk_sp<GrGpuBuffer>,
197                             size_t offset,
198                             size_t rowBytes) override;
199 
200     bool onTransferPixelsFrom(GrSurface*,
201                               SkIRect,
202                               GrColorType surfaceColorType,
203                               GrColorType bufferColorType,
204                               sk_sp<GrGpuBuffer>,
205                               size_t offset) override;
206 
207     bool onCopySurface(GrSurface* dst, const SkIRect& dstRect,
208                        GrSurface* src, const SkIRect& srcRect,
209                        GrSamplerState::Filter) override;
210 
211     bool onRegenerateMipMapLevels(GrTexture*) override;
212 
213     void onResolveRenderTarget(GrRenderTarget* target, const SkIRect&) override;
214 
215     void addFinishedProc(GrGpuFinishedProc finishedProc,
216                          GrGpuFinishedContext finishedContext) override;
217     void addFinishedCallback(sk_sp<skgpu::RefCntedCallback> finishedCallback);
218 
219     GrOpsRenderPass* onGetOpsRenderPass(GrRenderTarget*,
220                                         bool useMSAASurface,
221                                         GrAttachment*,
222                                         GrSurfaceOrigin,
223                                         const SkIRect&,
224                                         const GrOpsRenderPass::LoadAndStoreInfo&,
225                                         const GrOpsRenderPass::StencilLoadAndStoreInfo&,
226                                         const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
227                                         GrXferBarrierFlags renderPassXferBarriers) override;
228 
229     void prepareSurfacesForBackendAccessAndStateUpdates(
230             SkSpan<GrSurfaceProxy*> proxies,
231             SkSurface::BackendSurfaceAccess access,
232             const skgpu::MutableTextureState* newState) override;
233 
234     bool onSubmitToGpu(bool syncCpu) override;
235 
236     GrBackendTexture onCreateBackendTexture(SkISize dimensions,
237                                             const GrBackendFormat&,
238                                             GrRenderable,
239                                             GrMipmapped,
240                                             GrProtected,
241                                             std::string_view label) override;
242 
243     bool onClearBackendTexture(const GrBackendTexture&,
244                                sk_sp<skgpu::RefCntedCallback> finishedCallback,
245                                std::array<float, 4> color) override;
246 
247     GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions,
248                                                       const GrBackendFormat&,
249                                                       GrMipmapped,
250                                                       GrProtected) override;
251 
252     bool onUpdateCompressedBackendTexture(const GrBackendTexture&,
253                                           sk_sp<skgpu::RefCntedCallback> finishedCallback,
254                                           const void* data,
255                                           size_t size) override;
256 
257     bool submitDirectCommandList(SyncQueue sync);
258 
259     void checkForFinishedCommandLists();
260     void waitForQueueCompletion();
261 
262     void copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src, GrD3DTextureResource* dstResource,
263                                   GrD3DTextureResource* srcResource, const SkIRect& srcRect,
264                                   const SkIPoint& dstPoint);
265 
266     void copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
267                               const SkIPoint& dstPoint);
268     void resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
269                         GrD3DRenderTarget* src, const SkIRect& srcRect);
270 
271     sk_sp<GrD3DTexture> createD3DTexture(SkISize,
272                                          DXGI_FORMAT,
273                                          GrRenderable,
274                                          int renderTargetSampleCnt,
275                                          skgpu::Budgeted,
276                                          GrProtected,
277                                          int mipLevelCount,
278                                          GrMipmapStatus,
279                                          std::string_view label);
280 
281     bool uploadToTexture(GrD3DTexture* tex,
282                          SkIRect rect,
283                          GrColorType colorType,
284                          const GrMipLevel texels[],
285                          int mipLevelCount);
286 
287     void readOrTransferPixels(GrD3DTextureResource* texResource,
288                               SkIRect rect,
289                               sk_sp<GrGpuBuffer> transferBuffer,
290                               const D3D12_PLACED_SUBRESOURCE_FOOTPRINT& placedFootprint);
291 
292     bool createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
293                                                 SkISize dimensions,
294                                                 GrTexturable texturable,
295                                                 GrRenderable renderable,
296                                                 GrMipmapped mipmapped,
297                                                 int sampleCnt,
298                                                 GrD3DTextureResourceInfo* info,
299                                                 GrProtected isProtected);
300 
301     gr_cp<ID3D12Device> fDevice;
302     gr_cp<ID3D12CommandQueue> fQueue;
303 
304     sk_sp<GrD3DMemoryAllocator> fMemoryAllocator;
305 
306     GrD3DResourceProvider fResourceProvider;
307     GrStagingBufferManager fStagingBufferManager;
308     GrRingBuffer fConstantsRingBuffer;
309 
310     gr_cp<ID3D12Fence> fFence;
311     uint64_t fCurrentFenceValue = 0;
312 
313     std::unique_ptr<GrD3DDirectCommandList> fCurrentDirectCommandList;
314     // One-off special-case descriptors created directly for the mipmap compute shader
315     // and hence aren't tracked by the normal path.
316     SkSTArray<32, GrD3DDescriptorHeap::CPUHandle> fMipmapCPUDescriptors;
317 
318     struct OutstandingCommandList {
OutstandingCommandListOutstandingCommandList319         OutstandingCommandList(std::unique_ptr<GrD3DDirectCommandList> commandList,
320                                uint64_t fenceValue)
321             : fCommandList(std::move(commandList)), fFenceValue(fenceValue) {
322         }
323         std::unique_ptr<GrD3DDirectCommandList> fCommandList;
324         uint64_t fFenceValue;
325     };
326 
327     SkDeque fOutstandingCommandLists;
328 
329     std::unique_ptr<GrD3DOpsRenderPass> fCachedOpsRenderPass;
330 
331 #if GR_TEST_UTILS
332     IDXGraphicsAnalysis* fGraphicsAnalysis;
333 #endif
334 
335     using INHERITED = GrGpu;
336 };
337 
338 #endif
339