blob: e555c00fbfc6311b600157d35baee23340317552 [file] [log] [blame]
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001/*
Jim Van Verth03b8ab22020-02-24 11:36:15 -05002 * Copyright 2020 Google LLC
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05008#include "src/gpu/d3d/GrD3DGpu.h"
9
Jim Van Verth96bfeff2020-04-09 14:36:12 -040010#include "include/gpu/GrBackendSurface.h"
Jim Van Verth9aa9a682020-04-01 10:13:48 -040011#include "include/gpu/d3d/GrD3DBackendContext.h"
Jim Van Verthc632aa62020-04-17 16:58:20 -040012#include "src/core/SkConvertPixels.h"
Jim Van Verth96bfeff2020-04-09 14:36:12 -040013#include "src/core/SkMipMap.h"
Jim Van Verth43a6e172020-06-23 11:59:08 -040014#include "src/gpu/GrBackendUtils.h"
Jim Van Verthc632aa62020-04-17 16:58:20 -040015#include "src/gpu/GrDataUtils.h"
16#include "src/gpu/GrTexturePriv.h"
Jim Van Verthd6ad4802020-04-03 14:59:20 -040017#include "src/gpu/d3d/GrD3DBuffer.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050018#include "src/gpu/d3d/GrD3DCaps.h"
19#include "src/gpu/d3d/GrD3DOpsRenderPass.h"
Jim Van Verthc1a67b52020-06-25 13:10:29 -040020#include "src/gpu/d3d/GrD3DSemaphore.h"
Jim Van Verth4f51f472020-04-13 11:02:21 -040021#include "src/gpu/d3d/GrD3DStencilAttachment.h"
Jim Van Verthaa90dad2020-03-30 15:00:39 -040022#include "src/gpu/d3d/GrD3DTexture.h"
23#include "src/gpu/d3d/GrD3DTextureRenderTarget.h"
24#include "src/gpu/d3d/GrD3DUtil.h"
Greg Daniel5fc5c812020-04-23 10:30:23 -040025#include "src/sksl/SkSLCompiler.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050026
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040027#if GR_TEST_UTILS
28#include <DXProgrammableCapture.h>
29#endif
30
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050031sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext,
32 const GrContextOptions& contextOptions, GrContext* context) {
33 return sk_sp<GrGpu>(new GrD3DGpu(context, contextOptions, backendContext));
34}
35
Greg Daniel83ed2132020-03-24 13:15:33 -040036// This constant determines how many OutstandingCommandLists are allocated together as a block in
37// the deque. As such it needs to balance allocating too much memory vs. incurring
38// allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding
39// command lists we expect to see.
40static const int kDefaultOutstandingAllocCnt = 8;
41
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050042GrD3DGpu::GrD3DGpu(GrContext* context, const GrContextOptions& contextOptions,
43 const GrD3DBackendContext& backendContext)
Jim Van Verth03b8ab22020-02-24 11:36:15 -050044 : INHERITED(context)
45 , fDevice(backendContext.fDevice)
Greg Daniel02c45902020-03-09 10:58:09 -040046
47 , fQueue(backendContext.fQueue)
Greg Daniel83ed2132020-03-24 13:15:33 -040048 , fResourceProvider(this)
Greg Daniel5fc5c812020-04-23 10:30:23 -040049 , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt)
50 , fCompiler(new SkSL::Compiler()) {
Jim Van Verth8ec13302020-02-26 12:59:56 -050051 fCaps.reset(new GrD3DCaps(contextOptions,
Jim Van Verth9aa9a682020-04-01 10:13:48 -040052 backendContext.fAdapter.get(),
53 backendContext.fDevice.get()));
Greg Daniel85da3362020-03-09 15:18:35 -040054
55 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
Greg Daniele52c9782020-03-23 14:18:37 -040056 SkASSERT(fCurrentDirectCommandList);
Greg Daniel83ed2132020-03-24 13:15:33 -040057
58 SkASSERT(fCurrentFenceValue == 0);
Jim Van Verthe3810362020-07-01 10:12:11 -040059 GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE,
60 IID_PPV_ARGS(&fFence)));
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040061
62#if GR_TEST_UTILS
63 HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis));
64 if (FAILED(getAnalysis)) {
65 fGraphicsAnalysis = nullptr;
66 }
67#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050068}
69
Greg Daniel83ed2132020-03-24 13:15:33 -040070GrD3DGpu::~GrD3DGpu() {
71 this->destroyResources();
72}
73
74void GrD3DGpu::destroyResources() {
75 if (fCurrentDirectCommandList) {
76 fCurrentDirectCommandList->close();
Jim Van Verthba7f2292020-04-21 08:56:47 -040077 fCurrentDirectCommandList->reset();
Greg Daniel83ed2132020-03-24 13:15:33 -040078 }
79
80 // We need to make sure everything has finished on the queue.
Jim Van Verth3b0d7d12020-07-06 11:52:42 -040081 this->waitForQueueCompletion();
Greg Daniel83ed2132020-03-24 13:15:33 -040082
83 SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();)
84
85 // We used a placement new for each object in fOutstandingCommandLists, so we're responsible
86 // for calling the destructor on each of them as well.
87 while (!fOutstandingCommandLists.empty()) {
Jim Van Verthf43da142020-06-09 16:34:43 -040088 OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -040089 SkASSERT(list->fFenceValue <= fenceValue);
90 // No reason to recycle the command lists since we are destroying all resources anyways.
91 list->~OutstandingCommandList();
Jim Van Verthf43da142020-06-09 16:34:43 -040092 fOutstandingCommandLists.pop_front();
Greg Daniel83ed2132020-03-24 13:15:33 -040093 }
Jim Van Verthf2788862020-06-03 17:33:12 -040094
95 fResourceProvider.destroyResources();
Greg Daniel83ed2132020-03-24 13:15:33 -040096}
Greg Daniel31a7b072020-02-26 15:31:49 -050097
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050098GrOpsRenderPass* GrD3DGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -040099 GrRenderTarget* rt, GrStencilAttachment*,
100 GrSurfaceOrigin origin, const SkIRect& bounds,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500101 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Daniel31a7b072020-02-26 15:31:49 -0500102 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500103 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500104 if (!fCachedOpsRenderPass) {
105 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
106 }
107
108 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
109 return nullptr;
110 }
111 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500112}
113
Jim Van Verthc632aa62020-04-17 16:58:20 -0400114bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400115 SkASSERT(fCurrentDirectCommandList);
116
Jim Van Verth3eadce22020-06-01 11:34:49 -0400117 fResourceProvider.prepForSubmit();
118
Jim Van Verthc632aa62020-04-17 16:58:20 -0400119 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
120 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
121 return false;
122 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
123 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400124 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400125 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400126 }
127 return true;
128 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400129
Greg Daniela581a8b2020-06-19 15:22:18 -0400130 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
131 // uniform data as dirty.
132 fResourceProvider.markPipelineStateUniformsDirty();
133
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400134 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400135 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400136 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400137
Jim Van Verthc632aa62020-04-17 16:58:20 -0400138 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400139 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400140 }
141
Greg Daniel83ed2132020-03-24 13:15:33 -0400142 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
143
144 // This should be done after we have a new command list in case the freeing of any resources
145 // held by a finished command list causes us send a new command to the gpu (like changing the
146 // resource state.
147 this->checkForFinishedCommandLists();
148
149 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400150 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400151}
152
153void GrD3DGpu::checkForFinishedCommandLists() {
154 uint64_t currentFenceValue = fFence->GetCompletedValue();
155
156 // Iterate over all the outstanding command lists to see if any have finished. The commands
157 // lists are in order from oldest to newest, so we start at the front to check if their fence
158 // value is less than the last signaled value. If so we pop it off and move onto the next.
159 // Repeat till we find a command list that has not finished yet (and all others afterwards are
160 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400161 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
162 while (front && front->fFenceValue <= currentFenceValue) {
163 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400164 // Since we used placement new we are responsible for calling the destructor manually.
165 front->~OutstandingCommandList();
166 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400167 fResourceProvider.recycleDirectCommandList(std::move(currList));
168 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400169 }
170}
171
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400172void GrD3DGpu::waitForQueueCompletion() {
173 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
174 HANDLE fenceEvent;
175 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
176 SkASSERT(fenceEvent);
177 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
178 WaitForSingleObject(fenceEvent, INFINITE);
179 CloseHandle(fenceEvent);
180 }
181}
182
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500183void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400184 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
185
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500186 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400187 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500188}
189
Greg Danield4928d02020-06-19 11:13:26 -0400190void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
191 GrGpuFinishedContext finishedContext) {
192 SkASSERT(finishedProc);
193 sk_sp<GrRefCntedCallback> finishedCallback(
194 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400195 this->addFinishedCallback(std::move(finishedCallback));
196}
197
198void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
199 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400200 // Besides the current command list, we also add the finishedCallback to the newest outstanding
201 // command list. Our contract for calling the proc is that all previous submitted command lists
202 // have finished when we call it. However, if our current command list has no work when it is
203 // flushed it will drop its ref to the callback immediately. But the previous work may not have
204 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
205 // must finish after all previously submitted command lists.
206 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
207 if (back) {
208 back->fCommandList->addFinishedCallback(finishedCallback);
209 }
210 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
211}
212
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500213void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) {
214 // TODO
215}
216
217sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
218 const GrBackendFormat& format,
219 GrRenderable renderable,
220 int renderTargetSampleCnt,
221 SkBudgeted budgeted,
222 GrProtected isProtected,
223 int mipLevelCount,
224 uint32_t levelClearMask) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400225 DXGI_FORMAT dxgiFormat;
226 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
227 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
228
229 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
230
231 if (renderable == GrRenderable::kYes) {
232 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
233 }
234
235 // This desc refers to the texture that will be read by the client. Thus even if msaa is
236 // requested, this describes the resolved texture. Therefore we always have samples set
237 // to 1.
238 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400239 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400240 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
241 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
242 // might want to manually set alignment to 4KB for smaller textures
243 resourceDesc.Alignment = 0;
244 resourceDesc.Width = dimensions.fWidth;
245 resourceDesc.Height = dimensions.fHeight;
246 resourceDesc.DepthOrArraySize = 1;
247 resourceDesc.MipLevels = mipLevelCount;
248 resourceDesc.Format = dxgiFormat;
249 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400250 // quality levels are only supported for tiled resources so ignore for now
251 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400252 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400253 resourceDesc.Flags = usageFlags;
254
255 GrMipMapsStatus mipMapsStatus =
256 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
257
258 sk_sp<GrD3DTexture> tex;
259 if (renderable == GrRenderable::kYes) {
260 tex = GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
261 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
262 mipMapsStatus);
263 } else {
264 tex = GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
265 mipMapsStatus);
266 }
267
268 if (!tex) {
269 return nullptr;
270 }
271
272 if (levelClearMask) {
273 // TODO
274 }
275 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500276}
277
278sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
279 const GrBackendFormat& format,
280 SkBudgeted budgeted,
281 GrMipMapped mipMapped,
282 GrProtected isProtected,
283 const void* data, size_t dataSize) {
284 // TODO
285 return nullptr;
286}
287
Jim Van Verth9145f782020-04-28 12:01:12 -0400288static int get_surface_sample_cnt(GrSurface* surf) {
289 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
290 return rt->numSamples();
291 }
292 return 0;
293}
294
295bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
296 const SkIPoint& dstPoint) {
297
298 if (src->isProtected() && !dst->isProtected()) {
299 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400300 return false;
301 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400302
303 int dstSampleCnt = get_surface_sample_cnt(dst);
304 int srcSampleCnt = get_surface_sample_cnt(src);
305
306 GrD3DTextureResource* dstTexResource;
307 GrD3DTextureResource* srcTexResource;
308 GrRenderTarget* dstRT = dst->asRenderTarget();
309 if (dstRT) {
310 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
311 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
312 } else {
313 SkASSERT(dst->asTexture());
314 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
315 }
316 GrRenderTarget* srcRT = src->asRenderTarget();
317 if (srcRT) {
318 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
319 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
320 } else {
321 SkASSERT(src->asTexture());
322 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
323 }
324
325 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
326 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
327
328 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
329 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
330 return true;
331 }
332
333 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
334 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
335 return true;
336 }
337
338 return false;
339}
340
341void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
342 GrD3DTextureResource* dstResource,
343 GrD3DTextureResource* srcResource,
344 const SkIRect& srcRect, const SkIPoint& dstPoint) {
345#ifdef SK_DEBUG
346 int dstSampleCnt = get_surface_sample_cnt(dst);
347 int srcSampleCnt = get_surface_sample_cnt(src);
348 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
349 DXGI_FORMAT srcFormat;
350 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
351 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
352#endif
353 if (src->isProtected() && !dst->isProtected()) {
354 SkDebugf("Can't copy from protected memory to non-protected");
355 return;
356 }
357
358 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
359 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
360
361 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
362 dstLocation.pResource = dstResource->d3dResource();
363 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
364 dstLocation.SubresourceIndex = 0;
365
366 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
367 srcLocation.pResource = srcResource->d3dResource();
368 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
369 srcLocation.SubresourceIndex = 0;
370
371 D3D12_BOX srcBox = {};
372 srcBox.left = srcRect.fLeft;
373 srcBox.top = srcRect.fTop;
374 srcBox.right = srcRect.fRight;
375 srcBox.bottom = srcRect.fBottom;
376 srcBox.front = 0;
377 srcBox.back = 1;
378 // TODO: use copyResource if copying full resource and sizes match
379 fCurrentDirectCommandList->copyTextureRegion(dstResource->resource(),
380 &dstLocation,
381 dstPoint.fX, dstPoint.fY,
382 srcResource->resource(),
383 &srcLocation,
384 &srcBox);
385
386 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
387 srcRect.width(), srcRect.height());
388 // The rect is already in device space so we pass in kTopLeft so no flip is done.
389 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
390}
391
392void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
393 const SkIPoint& dstPoint) {
394 // TODO
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400395}
396
Jim Van Verthba7f2292020-04-21 08:56:47 -0400397bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
398 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
399 size_t rowBytes) {
400 SkASSERT(surface);
401
402 if (surfaceColorType != dstColorType) {
403 return false;
404 }
405
406 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400407 GrD3DTextureResource* texResource = nullptr;
408 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
409 if (rt) {
410 texResource = rt;
411 } else {
412 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
413 }
414
415 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400416 return false;
417 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400418
Jim Van Verthba7f2292020-04-21 08:56:47 -0400419 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400420 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400421 SkASSERT(srcLocation.pResource);
422 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
423 srcLocation.SubresourceIndex = 0;
424
425 D3D12_BOX srcBox = {};
426 srcBox.left = left;
427 srcBox.top = top;
428 srcBox.right = left + width;
429 srcBox.bottom = top + height;
430 srcBox.front = 0;
431 srcBox.back = 1;
432
433 // Set up dst location and create transfer buffer
434 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
435 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400436 UINT64 transferTotalBytes;
437 const UINT64 baseOffset = 0;
438 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
439 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400440 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400441 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400442 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400443 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400444 return false;
445 }
446 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400447
448 // TODO: implement some way of reusing buffers instead of making a new one every time.
449 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
450 GrGpuBufferType::kXferGpuToCpu,
451 kDynamic_GrAccessPattern);
452 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
453 dstLocation.pResource = d3dBuf->d3dResource();
454
455 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400456 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400457
458 fCurrentDirectCommandList->copyTextureRegion(d3dBuf->resource(), &dstLocation, 0, 0,
Jim Van Verthc12aad92020-04-24 16:19:01 -0400459 texResource->resource(), &srcLocation, &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400460 this->submitDirectCommandList(SyncQueue::kForce);
461
462 const void* mappedMemory = transferBuffer->map();
463
464 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400465 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400466
467 transferBuffer->unmap();
468
469 return true;
470}
471
472bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
473 GrColorType surfaceColorType, GrColorType srcColorType,
474 const GrMipLevel texels[], int mipLevelCount,
475 bool prepForTexSampling) {
476 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
477 if (!d3dTex) {
478 return false;
479 }
480
481 // Make sure we have at least the base level
482 if (!mipLevelCount || !texels[0].fPixels) {
483 return false;
484 }
485
486 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
487 bool success = false;
488
489 // Need to change the resource state to COPY_DEST in order to upload to it
490 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
491
492 SkASSERT(mipLevelCount <= d3dTex->texturePriv().maxMipMapLevel() + 1);
493 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
494 mipLevelCount);
495
496 if (prepForTexSampling) {
497 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
498 }
499
500 return success;
501}
502
503bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
504 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
505 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
506 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
507 SkASSERT(1 == mipLevelCount ||
508 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
509
510 // We assume that if the texture has mip levels, we either upload to all the levels or just the
511 // first.
512 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
513
514 if (width == 0 || height == 0) {
515 return false;
516 }
517
518 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
519 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
520
521 ID3D12Resource* d3dResource = tex->d3dResource();
522 SkASSERT(d3dResource);
523 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
524 // Either upload only the first miplevel or all miplevels
525 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
526
527 if (1 == mipLevelCount && !texels[0].fPixels) {
528 return true; // no data to upload
529 }
530
531 for (int i = 0; i < mipLevelCount; ++i) {
532 // We do not allow any gaps in the mip data
533 if (!texels[i].fPixels) {
534 return false;
535 }
536 }
537
538 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400539 UINT64 combinedBufferSize;
540 // We reset the width and height in the description to match our subrectangle size
541 // so we don't end up allocating more space than we need.
542 desc.Width = width;
543 desc.Height = height;
544 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400545 nullptr, nullptr, &combinedBufferSize);
546 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400547 SkASSERT(combinedBufferSize);
548
549 // TODO: do this until we have slices of buttery buffers
550 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
551 GrGpuBufferType::kXferCpuToGpu,
552 kDynamic_GrAccessPattern);
553 if (!transferBuffer) {
554 return false;
555 }
556 char* bufferData = (char*)transferBuffer->map();
557
558 int currentWidth = width;
559 int currentHeight = height;
560 int layerHeight = tex->height();
561
562 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
563 if (texels[currentMipLevel].fPixels) {
564 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
565
Jim Van Verthfdd36852020-04-21 16:29:44 -0400566 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400567 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
568
569 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
570
571 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400572 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400573 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
574 src, srcRowBytes, trimRowBytes, currentHeight);
575 }
576 currentWidth = std::max(1, currentWidth / 2);
577 currentHeight = std::max(1, currentHeight / 2);
578 layerHeight = currentHeight;
579 }
580
581 transferBuffer->unmap();
582
583 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
584 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
585 placedFootprints.get(), left, top);
586
587 if (mipLevelCount < (int)desc.MipLevels) {
588 tex->texturePriv().markMipMapsDirty();
589 }
590
591 return true;
592}
593
Jim Van Verth9145f782020-04-28 12:01:12 -0400594static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
595 if (!info.fResource.get()) {
596 return false;
597 }
598 return true;
599}
600
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400601static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
602 if (!caps.isFormatTexturable(info.fFormat)) {
603 return false;
604 }
605 return true;
606}
607
608static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
609 int sampleCnt) {
610 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
611 return false;
612 }
613 return true;
614}
615
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400616sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
617 GrWrapOwnership,
618 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400619 GrIOType ioType) {
620 GrD3DTextureResourceInfo textureInfo;
621 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
622 return nullptr;
623 }
624
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400625 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400626 return nullptr;
627 }
628
629 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
630 return nullptr;
631 }
632
633 // TODO: support protected context
634 if (tex.isProtected()) {
635 return nullptr;
636 }
637
638 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
639 SkASSERT(state);
640 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
641 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500642}
643
644sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400645 GrWrapOwnership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500646 GrWrapCacheable wrapType) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400647 GrD3DTextureResourceInfo textureInfo;
648 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
649 return nullptr;
650 }
651
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400652 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400653 return nullptr;
654 }
655
656 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
657 return nullptr;
658 }
659
660 // TODO: support protected context
661 if (tex.isProtected()) {
662 return nullptr;
663 }
664
665 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
666 SkASSERT(state);
667 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, kRead_GrIOType,
668 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500669}
670
671sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
672 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500673 GrWrapOwnership ownership,
674 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400675 GrD3DTextureResourceInfo textureInfo;
676 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
677 return nullptr;
678 }
679
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400680 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400681 return nullptr;
682 }
683
684 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
685 return nullptr;
686 }
687 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
688 return nullptr;
689 }
690
691 // TODO: support protected context
692 if (tex.isProtected()) {
693 return nullptr;
694 }
695
696 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
697
698 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
699 SkASSERT(state);
700
701 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
702 sampleCnt, cacheable,
703 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500704}
705
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400706sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400707 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
708 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
709 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
710 // creating and owning the MSAA images.
711 if (rt.sampleCnt() > 1) {
712 return nullptr;
713 }
714
715 GrD3DTextureResourceInfo info;
716 if (!rt.getD3DTextureResourceInfo(&info)) {
717 return nullptr;
718 }
719
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400720 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400721 return nullptr;
722 }
723
724 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
725 return nullptr;
726 }
727
728 // TODO: support protected context
729 if (rt.isProtected()) {
730 return nullptr;
731 }
732
733 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
734
735 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
736 this, rt.dimensions(), 1, info, std::move(state));
737
738 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
739 SkASSERT(!rt.stencilBits());
740 if (tgt) {
741 SkASSERT(tgt->canAttemptStencilAttachment());
742 }
743
744 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500745}
746
747sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400748 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400749
750 GrD3DTextureResourceInfo textureInfo;
751 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
752 return nullptr;
753 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400754 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400755 return nullptr;
756 }
757
758 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
759 return nullptr;
760 }
761
762 // TODO: support protected context
763 if (tex.isProtected()) {
764 return nullptr;
765 }
766
767 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
768 if (!sampleCnt) {
769 return nullptr;
770 }
771
772 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
773 SkASSERT(state);
774
775 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
776 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500777}
778
779sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400780 GrAccessPattern accessPattern, const void* data) {
781 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
782 if (data && buffer) {
783 buffer->updateData(data, sizeInBytes);
784 }
785
786 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500787}
788
789GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
790 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400791 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
792 SkASSERT(width >= rt->width());
793 SkASSERT(height >= rt->height());
794
795 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
796
797 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
798 width,
799 height,
800 numStencilSamples,
801 sFmt));
802 fStats.incStencilAttachmentCreates();
803 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500804}
805
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400806bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
807 SkISize dimensions,
808 GrTexturable texturable,
809 GrRenderable renderable,
810 GrMipMapped mipMapped,
811 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400812 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400813 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400814
815 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
816 return false;
817 }
818
819 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
820 return false;
821 }
822
823 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
824 return false;
825 }
826
827 int numMipLevels = 1;
828 if (mipMapped == GrMipMapped::kYes) {
829 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
830 }
831
832 // create the texture
833 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
834 if (renderable == GrRenderable::kYes) {
835 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
836 }
837
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400838 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400839 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400840 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400841 resourceDesc.Width = dimensions.fWidth;
842 resourceDesc.Height = dimensions.fHeight;
843 resourceDesc.DepthOrArraySize = 1;
844 resourceDesc.MipLevels = numMipLevels;
845 resourceDesc.Format = dxgiFormat;
846 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400847 // quality levels are only supported for tiled resources so ignore for now
848 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400849 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400850 resourceDesc.Flags = usageFlags;
851
Jim Van Verth280d4f72020-05-04 10:04:24 -0400852 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
853 D3D12_CLEAR_VALUE clearValue = {};
854 if (renderable == GrRenderable::kYes) {
855 clearValue.Format = dxgiFormat;
856 // Assume transparent black
857 clearValue.Color[0] = 0;
858 clearValue.Color[1] = 0;
859 clearValue.Color[2] = 0;
860 clearValue.Color[3] = 0;
861 clearValuePtr = &clearValue;
862 }
863
Greg Daniel16032b32020-05-06 15:31:10 -0400864 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
865 ? D3D12_RESOURCE_STATE_RENDER_TARGET
866 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400867 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400868 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400869 SkDebugf("Failed to init texture resource info\n");
870 return false;
871 }
872
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400873 return true;
874}
875
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500876GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400877 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400878 GrRenderable renderable,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400879 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400880 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400881 this->handleDirtyContext();
882
883 const GrD3DCaps& caps = this->d3dCaps();
884
885 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
886 return {};
887 }
888
889 DXGI_FORMAT dxgiFormat;
890 if (!format.asDxgiFormat(&dxgiFormat)) {
891 return {};
892 }
893
894 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
895 if (!caps.isFormatTexturable(dxgiFormat)) {
896 return {};
897 }
898
899 GrD3DTextureResourceInfo info;
900 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
901 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400902 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400903 return {};
904 }
905
906 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500907}
908
Jim Van Verth43a6e172020-06-23 11:59:08 -0400909bool copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
910 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
911 const SkPixmap srcData[], int numMipLevels) {
912 SkASSERT(srcData && numMipLevels);
913 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
914 SkASSERT(mapPtr);
915
916 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
917
918 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
919 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
920
921 // copy data into the buffer, skipping any trailing bytes
922 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
923 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
924 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
925 trimRowBytes, srcData[currentMipLevel].height());
926 }
927
928 return true;
929}
930
Greg Daniel746460e2020-06-30 13:13:53 -0400931bool copy_color_data(const GrD3DCaps& caps, char* mapPtr, DXGI_FORMAT dxgiFormat,
932 SkISize dimensions, D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
933 SkColor4f color) {
934 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -0400935 if (colorType == GrColorType::kUnknown) {
936 return false;
937 }
938 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
939 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
940 return false;
941 }
942
943 return true;
944}
945
Greg Daniel16032b32020-05-06 15:31:10 -0400946bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
947 sk_sp<GrRefCntedCallback> finishedCallback,
948 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -0400949 GrD3DTextureResourceInfo info;
950 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
951
952 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
953 SkASSERT(state);
954 sk_sp<GrD3DTexture> texture =
955 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
956 GrWrapCacheable::kNo,
957 kRW_GrIOType, info, std::move(state));
958 if (!texture) {
959 return false;
960 }
961
962 GrD3DDirectCommandList* cmdList = this->currentCommandList();
963 if (!cmdList) {
964 return false;
965 }
966
967 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
968
969 ID3D12Resource* d3dResource = texture->d3dResource();
970 SkASSERT(d3dResource);
971 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
972 unsigned int mipLevelCount = 1;
973 if (backendTexture.fMipMapped == GrMipMapped::kYes) {
974 mipLevelCount = SkMipMap::ComputeLevelCount(backendTexture.dimensions().width(),
975 backendTexture.dimensions().height()) + 1;
976 }
977 SkASSERT(mipLevelCount == info.fLevelCount);
978 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
979 UINT64 combinedBufferSize;
980 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
981 nullptr, nullptr, &combinedBufferSize);
982 SkASSERT(combinedBufferSize);
983 if (data->type() == BackendTextureData::Type::kColor &&
984 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
985 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
986 combinedBufferSize =
987 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
988 for (unsigned int i = 1; i < mipLevelCount; ++i) {
989 placedFootprints[i].Offset = 0;
990 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
991 }
992 }
993
994 // TODO: do this until we have slices of buttery buffers
995 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
996 GrGpuBufferType::kXferCpuToGpu,
997 kDynamic_GrAccessPattern);
998 if (!transferBuffer) {
999 return false;
1000 }
1001 char* bufferData = (char*)transferBuffer->map();
1002 SkASSERT(bufferData);
1003
1004 bool result;
1005 if (data->type() == BackendTextureData::Type::kPixmaps) {
1006 result = copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(),
1007 data->pixmaps(), info.fLevelCount);
1008 } else if (data->type() == BackendTextureData::Type::kCompressed) {
1009 memcpy(bufferData, data->compressedData(), data->compressedSize());
1010 result = true;
1011 } else {
1012 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1013 SkImage::CompressionType compression =
1014 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1015 if (SkImage::CompressionType::kNone == compression) {
Greg Daniel746460e2020-06-30 13:13:53 -04001016 result = copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1017 backendTexture.dimensions(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001018 placedFootprints, data->color());
1019 } else {
1020 GrFillInCompressedData(compression, backendTexture.dimensions(),
1021 backendTexture.fMipMapped, bufferData, data->color());
1022 result = true;
1023 }
1024 }
1025 transferBuffer->unmap();
1026
1027 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
1028 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(),
1029 0, 0);
1030
1031 if (finishedCallback) {
1032 this->addFinishedCallback(std::move(finishedCallback));
1033 }
1034
Greg Daniel16032b32020-05-06 15:31:10 -04001035 return true;
1036}
1037
Greg Danielc1ad77c2020-05-06 11:40:03 -04001038GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
1039 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
1040 GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback,
1041 const BackendTextureData* data) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001042 this->handleDirtyContext();
1043
1044 const GrD3DCaps& caps = this->d3dCaps();
1045
1046 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1047 return {};
1048 }
1049
1050 DXGI_FORMAT dxgiFormat;
1051 if (!format.asDxgiFormat(&dxgiFormat)) {
1052 return {};
1053 }
1054
1055 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1056 if (!caps.isFormatTexturable(dxgiFormat)) {
1057 return {};
1058 }
1059
1060 GrD3DTextureResourceInfo info;
1061 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1062 GrRenderable::kNo, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001063 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001064 return {};
1065 }
1066
1067 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001068}
1069
1070void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001071 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1072 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001073}
1074
Robert Phillips979b2232020-02-20 10:47:29 -05001075bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1076 return false;
1077}
1078
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001079#if GR_TEST_UTILS
1080bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001081 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1082
1083 GrD3DTextureResourceInfo info;
1084 if (!tex.getD3DTextureResourceInfo(&info)) {
1085 return false;
1086 }
1087 ID3D12Resource* textureResource = info.fResource.get();
1088 if (!textureResource) {
1089 return false;
1090 }
1091 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001092}
1093
1094GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001095 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001096 this->handleDirtyContext();
1097
1098 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001099 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001100 }
1101
1102 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1103
1104 GrD3DTextureResourceInfo info;
1105 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
1106 GrRenderable::kYes, GrMipMapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001107 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001108 return {};
1109 }
1110
1111 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001112}
1113
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001114void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1115 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1116
1117 GrD3DTextureResourceInfo info;
1118 if (rt.getD3DTextureResourceInfo(&info)) {
1119 this->testingOnly_flushGpuAndSync();
1120 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1121 // is deleted.
1122 }
1123}
1124
1125void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001126 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001127}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001128
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001129void GrD3DGpu::testingOnly_startCapture() {
1130 if (fGraphicsAnalysis) {
1131 fGraphicsAnalysis->BeginCapture();
1132 }
1133}
1134
1135void GrD3DGpu::testingOnly_endCapture() {
1136 if (fGraphicsAnalysis) {
1137 fGraphicsAnalysis->EndCapture();
1138 }
1139}
1140#endif
1141
Jim Van Verthc632aa62020-04-17 16:58:20 -04001142///////////////////////////////////////////////////////////////////////////////
1143
Greg Daniela5a6b322020-04-23 12:52:27 -04001144void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001145 int numBarriers,
1146 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1147 SkASSERT(fCurrentDirectCommandList);
1148 SkASSERT(resource);
1149
Greg Daniela5a6b322020-04-23 12:52:27 -04001150 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001151}
1152
Greg Daniel9efe3862020-06-11 11:51:06 -04001153void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1154 GrSurfaceProxy* proxies[],
1155 int numProxies,
1156 SkSurface::BackendSurfaceAccess access,
1157 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001158 SkASSERT(numProxies >= 0);
1159 SkASSERT(!numProxies || proxies);
1160
1161 // prepare proxies by transitioning to PRESENT renderState
1162 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1163 GrD3DTextureResource* resource;
1164 for (int i = 0; i < numProxies; ++i) {
1165 SkASSERT(proxies[i]->isInstantiated());
1166 if (GrTexture* tex = proxies[i]->peekTexture()) {
1167 resource = static_cast<GrD3DTexture*>(tex);
1168 } else {
1169 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1170 SkASSERT(rt);
1171 resource = static_cast<GrD3DRenderTarget*>(rt);
1172 }
1173 resource->prepareForPresent(this);
1174 }
1175 }
1176}
1177
Jim Van Verthc632aa62020-04-17 16:58:20 -04001178bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1179 if (syncCpu) {
1180 return this->submitDirectCommandList(SyncQueue::kForce);
1181 } else {
1182 return this->submitDirectCommandList(SyncQueue::kSkip);
1183 }
1184}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001185
1186std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1187 return GrD3DSemaphore::Make(this);
1188}
1189std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1190 const GrBackendSemaphore& semaphore,
1191 GrResourceProvider::SemaphoreWrapType,
1192 GrWrapOwnership) {
1193 SkASSERT(this->caps()->semaphoreSupport());
1194 GrD3DFenceInfo fenceInfo;
1195 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1196 return nullptr;
1197 }
1198 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1199}
1200
1201void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001202 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001203 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1204 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1205 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1206}
1207
1208void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001209 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001210 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1211 // TODO: Do we need to track the lifetime of this?
1212 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1213}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001214
1215GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001216 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001217 return fCurrentFenceValue;
1218}
1219
1220bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001221 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001222}