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/SkDeque.h" 12 #include "src/gpu/GrGpu.h" 13 #include "src/gpu/GrRenderTarget.h" 14 #include "src/gpu/GrSemaphore.h" 15 #include "src/gpu/GrStagingBufferManager.h" 16 #include "src/gpu/d3d/GrD3DCaps.h" 17 #include "src/gpu/d3d/GrD3DCommandList.h" 18 #include "src/gpu/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_endCapture() 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) const 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 SkBudgeted, 139 GrProtected, 140 int mipLevelCount, 141 uint32_t levelClearMask) override; 142 143 sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions, 144 const GrBackendFormat&, 145 SkBudgeted, 146 GrMipmapped, 147 GrProtected, 148 const void* data, size_t dataSize) override; 149 150 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, 151 GrWrapOwnership, 152 GrWrapCacheable, 153 GrIOType) override; 154 sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&, 155 GrWrapOwnership, 156 GrWrapCacheable) override; 157 158 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, 159 int sampleCnt, 160 GrWrapOwnership, 161 GrWrapCacheable) override; 162 163 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; 164 165 sk_sp<GrGpuBuffer> onCreateBuffer(size_t sizeInBytes, GrGpuBufferType, GrAccessPattern, 166 const void*) override; 167 168 bool onReadPixels(GrSurface*, 169 SkIRect, 170 GrColorType surfaceColorType, 171 GrColorType dstColorType, 172 void*, 173 size_t rowBytes) override; 174 175 bool onWritePixels(GrSurface*, 176 SkIRect, 177 GrColorType surfaceColorType, 178 GrColorType srcColorType, 179 const GrMipLevel[], 180 int mipLevelCount, 181 bool prepForTexSampling) override; 182 183 bool onTransferPixelsTo(GrTexture*, 184 SkIRect, 185 GrColorType surfaceColorType, 186 GrColorType bufferColorType, 187 sk_sp<GrGpuBuffer>, 188 size_t offset, 189 size_t rowBytes) override; 190 191 bool onTransferPixelsFrom(GrSurface*, 192 SkIRect, 193 GrColorType surfaceColorType, 194 GrColorType bufferColorType, 195 sk_sp<GrGpuBuffer>, 196 size_t offset) override; 197 198 bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, 199 const SkIPoint& dstPoint) override; 200 201 bool onRegenerateMipMapLevels(GrTexture*) override; 202 203 void onResolveRenderTarget(GrRenderTarget* target, const SkIRect&) override; 204 205 void addFinishedProc(GrGpuFinishedProc finishedProc, 206 GrGpuFinishedContext finishedContext) override; 207 void addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback); 208 209 GrOpsRenderPass* onGetOpsRenderPass(GrRenderTarget*, 210 bool useMSAASurface, 211 GrAttachment*, 212 GrSurfaceOrigin, 213 const SkIRect&, 214 const GrOpsRenderPass::LoadAndStoreInfo&, 215 const GrOpsRenderPass::StencilLoadAndStoreInfo&, 216 const SkTArray<GrSurfaceProxy*, true>& sampledProxies, 217 GrXferBarrierFlags renderPassXferBarriers) override; 218 219 void prepareSurfacesForBackendAccessAndStateUpdates( 220 SkSpan<GrSurfaceProxy*> proxies, 221 SkSurface::BackendSurfaceAccess access, 222 const GrBackendSurfaceMutableState* newState) override; 223 224 bool onSubmitToGpu(bool syncCpu) override; 225 226 GrBackendTexture onCreateBackendTexture(SkISize dimensions, 227 const GrBackendFormat&, 228 GrRenderable, 229 GrMipmapped, 230 GrProtected) override; 231 232 bool onClearBackendTexture(const GrBackendTexture&, 233 sk_sp<GrRefCntedCallback> finishedCallback, 234 std::array<float, 4> color) override; 235 236 GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions, 237 const GrBackendFormat&, 238 GrMipmapped, 239 GrProtected) override; 240 241 bool onUpdateCompressedBackendTexture(const GrBackendTexture&, 242 sk_sp<GrRefCntedCallback> finishedCallback, 243 const void* data, 244 size_t size) override; 245 246 bool submitDirectCommandList(SyncQueue sync); 247 248 void checkForFinishedCommandLists(); 249 void waitForQueueCompletion(); 250 251 void copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src, GrD3DTextureResource* dstResource, 252 GrD3DTextureResource* srcResource, const SkIRect& srcRect, 253 const SkIPoint& dstPoint); 254 255 void copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, 256 const SkIPoint& dstPoint); 257 void resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY, 258 GrD3DRenderTarget* src, const SkIRect& srcRect); 259 260 sk_sp<GrD3DTexture> createD3DTexture(SkISize, 261 DXGI_FORMAT, 262 GrRenderable, 263 int renderTargetSampleCnt, 264 SkBudgeted, 265 GrProtected, 266 int mipLevelCount, 267 GrMipmapStatus); 268 269 bool uploadToTexture(GrD3DTexture* tex, 270 SkIRect rect, 271 GrColorType colorType, 272 const GrMipLevel texels[], 273 int mipLevelCount); 274 275 void readOrTransferPixels(GrD3DTextureResource* texResource, 276 SkIRect rect, 277 sk_sp<GrGpuBuffer> transferBuffer, 278 const D3D12_PLACED_SUBRESOURCE_FOOTPRINT& placedFootprint); 279 280 bool createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat, 281 SkISize dimensions, 282 GrTexturable texturable, 283 GrRenderable renderable, 284 GrMipmapped mipMapped, 285 int sampleCnt, 286 GrD3DTextureResourceInfo* info, 287 GrProtected isProtected); 288 289 gr_cp<ID3D12Device> fDevice; 290 gr_cp<ID3D12CommandQueue> fQueue; 291 292 sk_sp<GrD3DMemoryAllocator> fMemoryAllocator; 293 294 GrD3DResourceProvider fResourceProvider; 295 GrStagingBufferManager fStagingBufferManager; 296 GrRingBuffer fConstantsRingBuffer; 297 298 gr_cp<ID3D12Fence> fFence; 299 uint64_t fCurrentFenceValue = 0; 300 301 std::unique_ptr<GrD3DDirectCommandList> fCurrentDirectCommandList; 302 // One-off special-case descriptors created directly for the mipmap compute shader 303 // and hence aren't tracked by the normal path. 304 SkSTArray<32, GrD3DDescriptorHeap::CPUHandle> fMipmapCPUDescriptors; 305 306 struct OutstandingCommandList { OutstandingCommandListOutstandingCommandList307 OutstandingCommandList(std::unique_ptr<GrD3DDirectCommandList> commandList, 308 uint64_t fenceValue) 309 : fCommandList(std::move(commandList)), fFenceValue(fenceValue) { 310 } 311 std::unique_ptr<GrD3DDirectCommandList> fCommandList; 312 uint64_t fFenceValue; 313 }; 314 315 SkDeque fOutstandingCommandLists; 316 317 std::unique_ptr<GrD3DOpsRenderPass> fCachedOpsRenderPass; 318 319 #if GR_TEST_UTILS 320 IDXGraphicsAnalysis* fGraphicsAnalysis; 321 #endif 322 323 using INHERITED = GrGpu; 324 }; 325 326 #endif 327