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