blob: 809ff1169d8269963f106b65476636d4a62ac553 [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,
Adlai Holler3d0359a2020-07-09 15:35:55 -040032 const GrContextOptions& contextOptions, GrDirectContext* direct) {
33 return sk_sp<GrGpu>(new GrD3DGpu(direct, contextOptions, backendContext));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050034}
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
Adlai Holler3d0359a2020-07-09 15:35:55 -040042GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050043 const GrD3DBackendContext& backendContext)
Adlai Holler3d0359a2020-07-09 15:35:55 -040044 : INHERITED(direct)
Jim Van Verth03b8ab22020-02-24 11:36:15 -050045 , 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) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400394 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
395 SkASSERT(srcRT);
396
397 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
398}
399
400void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
401 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
402 SkASSERT(dst);
403 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
404
405 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
406
407 GrD3DTextureResource* dstTextureResource;
408 GrRenderTarget* dstRT = dst->asRenderTarget();
409 if (dstRT) {
410 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
411 } else {
412 SkASSERT(dst->asTexture());
413 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
414 }
415
416 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
417 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
418
419 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
420 src->msaaTextureResource(), &srcRect);
421}
422
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400423void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400424 SkASSERT(target->numSamples() > 1);
425 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
426 SkASSERT(rt->msaaTextureResource());
427
428 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400429}
430
Jim Van Verthba7f2292020-04-21 08:56:47 -0400431bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
432 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
433 size_t rowBytes) {
434 SkASSERT(surface);
435
436 if (surfaceColorType != dstColorType) {
437 return false;
438 }
439
440 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400441 GrD3DTextureResource* texResource = nullptr;
442 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
443 if (rt) {
444 texResource = rt;
445 } else {
446 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
447 }
448
449 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400450 return false;
451 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400452
Jim Van Verthba7f2292020-04-21 08:56:47 -0400453 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400454 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400455 SkASSERT(srcLocation.pResource);
456 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
457 srcLocation.SubresourceIndex = 0;
458
459 D3D12_BOX srcBox = {};
460 srcBox.left = left;
461 srcBox.top = top;
462 srcBox.right = left + width;
463 srcBox.bottom = top + height;
464 srcBox.front = 0;
465 srcBox.back = 1;
466
467 // Set up dst location and create transfer buffer
468 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
469 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400470 UINT64 transferTotalBytes;
471 const UINT64 baseOffset = 0;
472 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
473 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400474 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400475 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400476 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400477 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400478 return false;
479 }
480 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400481
482 // TODO: implement some way of reusing buffers instead of making a new one every time.
483 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
484 GrGpuBufferType::kXferGpuToCpu,
485 kDynamic_GrAccessPattern);
486 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
487 dstLocation.pResource = d3dBuf->d3dResource();
488
489 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400490 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400491
492 fCurrentDirectCommandList->copyTextureRegion(d3dBuf->resource(), &dstLocation, 0, 0,
Jim Van Verthc12aad92020-04-24 16:19:01 -0400493 texResource->resource(), &srcLocation, &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400494 this->submitDirectCommandList(SyncQueue::kForce);
495
496 const void* mappedMemory = transferBuffer->map();
497
498 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400499 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400500
501 transferBuffer->unmap();
502
503 return true;
504}
505
506bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
507 GrColorType surfaceColorType, GrColorType srcColorType,
508 const GrMipLevel texels[], int mipLevelCount,
509 bool prepForTexSampling) {
510 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
511 if (!d3dTex) {
512 return false;
513 }
514
515 // Make sure we have at least the base level
516 if (!mipLevelCount || !texels[0].fPixels) {
517 return false;
518 }
519
520 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
521 bool success = false;
522
523 // Need to change the resource state to COPY_DEST in order to upload to it
524 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
525
526 SkASSERT(mipLevelCount <= d3dTex->texturePriv().maxMipMapLevel() + 1);
527 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
528 mipLevelCount);
529
530 if (prepForTexSampling) {
531 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
532 }
533
534 return success;
535}
536
537bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
538 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
539 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
540 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
541 SkASSERT(1 == mipLevelCount ||
542 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
543
544 // We assume that if the texture has mip levels, we either upload to all the levels or just the
545 // first.
546 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
547
548 if (width == 0 || height == 0) {
549 return false;
550 }
551
552 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
553 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
554
555 ID3D12Resource* d3dResource = tex->d3dResource();
556 SkASSERT(d3dResource);
557 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
558 // Either upload only the first miplevel or all miplevels
559 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
560
561 if (1 == mipLevelCount && !texels[0].fPixels) {
562 return true; // no data to upload
563 }
564
565 for (int i = 0; i < mipLevelCount; ++i) {
566 // We do not allow any gaps in the mip data
567 if (!texels[i].fPixels) {
568 return false;
569 }
570 }
571
572 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400573 UINT64 combinedBufferSize;
574 // We reset the width and height in the description to match our subrectangle size
575 // so we don't end up allocating more space than we need.
576 desc.Width = width;
577 desc.Height = height;
578 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400579 nullptr, nullptr, &combinedBufferSize);
580 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400581 SkASSERT(combinedBufferSize);
582
583 // TODO: do this until we have slices of buttery buffers
584 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
585 GrGpuBufferType::kXferCpuToGpu,
586 kDynamic_GrAccessPattern);
587 if (!transferBuffer) {
588 return false;
589 }
590 char* bufferData = (char*)transferBuffer->map();
591
592 int currentWidth = width;
593 int currentHeight = height;
594 int layerHeight = tex->height();
595
596 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
597 if (texels[currentMipLevel].fPixels) {
598 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
599
Jim Van Verthfdd36852020-04-21 16:29:44 -0400600 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400601 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
602
603 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
604
605 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400606 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400607 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
608 src, srcRowBytes, trimRowBytes, currentHeight);
609 }
610 currentWidth = std::max(1, currentWidth / 2);
611 currentHeight = std::max(1, currentHeight / 2);
612 layerHeight = currentHeight;
613 }
614
615 transferBuffer->unmap();
616
617 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
618 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
619 placedFootprints.get(), left, top);
620
621 if (mipLevelCount < (int)desc.MipLevels) {
622 tex->texturePriv().markMipMapsDirty();
623 }
624
625 return true;
626}
627
Jim Van Verth9145f782020-04-28 12:01:12 -0400628static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
629 if (!info.fResource.get()) {
630 return false;
631 }
632 return true;
633}
634
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400635static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
636 if (!caps.isFormatTexturable(info.fFormat)) {
637 return false;
638 }
639 return true;
640}
641
642static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
643 int sampleCnt) {
644 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
645 return false;
646 }
647 return true;
648}
649
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400650sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
651 GrWrapOwnership,
652 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400653 GrIOType ioType) {
654 GrD3DTextureResourceInfo textureInfo;
655 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
656 return nullptr;
657 }
658
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400659 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400660 return nullptr;
661 }
662
663 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
664 return nullptr;
665 }
666
667 // TODO: support protected context
668 if (tex.isProtected()) {
669 return nullptr;
670 }
671
672 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
673 SkASSERT(state);
674 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
675 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500676}
677
678sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400679 GrWrapOwnership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500680 GrWrapCacheable wrapType) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400681 GrD3DTextureResourceInfo textureInfo;
682 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
683 return nullptr;
684 }
685
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400686 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400687 return nullptr;
688 }
689
690 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
691 return nullptr;
692 }
693
694 // TODO: support protected context
695 if (tex.isProtected()) {
696 return nullptr;
697 }
698
699 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
700 SkASSERT(state);
701 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, kRead_GrIOType,
702 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500703}
704
705sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
706 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500707 GrWrapOwnership ownership,
708 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400709 GrD3DTextureResourceInfo textureInfo;
710 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
711 return nullptr;
712 }
713
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400714 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400715 return nullptr;
716 }
717
718 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
719 return nullptr;
720 }
721 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
722 return nullptr;
723 }
724
725 // TODO: support protected context
726 if (tex.isProtected()) {
727 return nullptr;
728 }
729
730 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
731
732 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
733 SkASSERT(state);
734
735 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
736 sampleCnt, cacheable,
737 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500738}
739
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400740sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400741 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
742 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
743 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
744 // creating and owning the MSAA images.
745 if (rt.sampleCnt() > 1) {
746 return nullptr;
747 }
748
749 GrD3DTextureResourceInfo info;
750 if (!rt.getD3DTextureResourceInfo(&info)) {
751 return nullptr;
752 }
753
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400754 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400755 return nullptr;
756 }
757
758 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
759 return nullptr;
760 }
761
762 // TODO: support protected context
763 if (rt.isProtected()) {
764 return nullptr;
765 }
766
767 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
768
769 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
770 this, rt.dimensions(), 1, info, std::move(state));
771
772 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
773 SkASSERT(!rt.stencilBits());
774 if (tgt) {
775 SkASSERT(tgt->canAttemptStencilAttachment());
776 }
777
778 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500779}
780
781sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400782 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400783
784 GrD3DTextureResourceInfo textureInfo;
785 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
786 return nullptr;
787 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400788 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400789 return nullptr;
790 }
791
792 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
793 return nullptr;
794 }
795
796 // TODO: support protected context
797 if (tex.isProtected()) {
798 return nullptr;
799 }
800
801 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
802 if (!sampleCnt) {
803 return nullptr;
804 }
805
806 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
807 SkASSERT(state);
808
809 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
810 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500811}
812
813sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400814 GrAccessPattern accessPattern, const void* data) {
815 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
816 if (data && buffer) {
817 buffer->updateData(data, sizeInBytes);
818 }
819
820 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500821}
822
823GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
824 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400825 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
826 SkASSERT(width >= rt->width());
827 SkASSERT(height >= rt->height());
828
829 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
830
831 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
832 width,
833 height,
834 numStencilSamples,
835 sFmt));
836 fStats.incStencilAttachmentCreates();
837 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500838}
839
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400840bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
841 SkISize dimensions,
842 GrTexturable texturable,
843 GrRenderable renderable,
844 GrMipMapped mipMapped,
845 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400846 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400847 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400848
849 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
850 return false;
851 }
852
853 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
854 return false;
855 }
856
857 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
858 return false;
859 }
860
861 int numMipLevels = 1;
862 if (mipMapped == GrMipMapped::kYes) {
863 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
864 }
865
866 // create the texture
867 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
868 if (renderable == GrRenderable::kYes) {
869 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
870 }
871
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400872 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400873 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400874 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400875 resourceDesc.Width = dimensions.fWidth;
876 resourceDesc.Height = dimensions.fHeight;
877 resourceDesc.DepthOrArraySize = 1;
878 resourceDesc.MipLevels = numMipLevels;
879 resourceDesc.Format = dxgiFormat;
880 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400881 // quality levels are only supported for tiled resources so ignore for now
882 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400883 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400884 resourceDesc.Flags = usageFlags;
885
Jim Van Verth280d4f72020-05-04 10:04:24 -0400886 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
887 D3D12_CLEAR_VALUE clearValue = {};
888 if (renderable == GrRenderable::kYes) {
889 clearValue.Format = dxgiFormat;
890 // Assume transparent black
891 clearValue.Color[0] = 0;
892 clearValue.Color[1] = 0;
893 clearValue.Color[2] = 0;
894 clearValue.Color[3] = 0;
895 clearValuePtr = &clearValue;
896 }
897
Greg Daniel16032b32020-05-06 15:31:10 -0400898 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
899 ? D3D12_RESOURCE_STATE_RENDER_TARGET
900 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400901 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400902 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400903 SkDebugf("Failed to init texture resource info\n");
904 return false;
905 }
906
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400907 return true;
908}
909
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500910GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400911 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400912 GrRenderable renderable,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400913 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400914 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400915 this->handleDirtyContext();
916
917 const GrD3DCaps& caps = this->d3dCaps();
918
919 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
920 return {};
921 }
922
923 DXGI_FORMAT dxgiFormat;
924 if (!format.asDxgiFormat(&dxgiFormat)) {
925 return {};
926 }
927
928 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
929 if (!caps.isFormatTexturable(dxgiFormat)) {
930 return {};
931 }
932
933 GrD3DTextureResourceInfo info;
934 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
935 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400936 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400937 return {};
938 }
939
940 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500941}
942
Jim Van Verth43a6e172020-06-23 11:59:08 -0400943bool copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
944 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
945 const SkPixmap srcData[], int numMipLevels) {
946 SkASSERT(srcData && numMipLevels);
947 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
948 SkASSERT(mapPtr);
949
950 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
951
952 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
953 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
954
955 // copy data into the buffer, skipping any trailing bytes
956 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
957 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
958 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
959 trimRowBytes, srcData[currentMipLevel].height());
960 }
961
962 return true;
963}
964
Greg Daniel746460e2020-06-30 13:13:53 -0400965bool copy_color_data(const GrD3DCaps& caps, char* mapPtr, DXGI_FORMAT dxgiFormat,
966 SkISize dimensions, D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
967 SkColor4f color) {
968 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -0400969 if (colorType == GrColorType::kUnknown) {
970 return false;
971 }
972 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
973 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
974 return false;
975 }
976
977 return true;
978}
979
Greg Daniel16032b32020-05-06 15:31:10 -0400980bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
981 sk_sp<GrRefCntedCallback> finishedCallback,
982 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -0400983 GrD3DTextureResourceInfo info;
984 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
985
986 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
987 SkASSERT(state);
988 sk_sp<GrD3DTexture> texture =
989 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
990 GrWrapCacheable::kNo,
991 kRW_GrIOType, info, std::move(state));
992 if (!texture) {
993 return false;
994 }
995
996 GrD3DDirectCommandList* cmdList = this->currentCommandList();
997 if (!cmdList) {
998 return false;
999 }
1000
1001 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1002
1003 ID3D12Resource* d3dResource = texture->d3dResource();
1004 SkASSERT(d3dResource);
1005 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1006 unsigned int mipLevelCount = 1;
1007 if (backendTexture.fMipMapped == GrMipMapped::kYes) {
1008 mipLevelCount = SkMipMap::ComputeLevelCount(backendTexture.dimensions().width(),
1009 backendTexture.dimensions().height()) + 1;
1010 }
1011 SkASSERT(mipLevelCount == info.fLevelCount);
1012 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1013 UINT64 combinedBufferSize;
1014 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
1015 nullptr, nullptr, &combinedBufferSize);
1016 SkASSERT(combinedBufferSize);
1017 if (data->type() == BackendTextureData::Type::kColor &&
1018 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1019 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1020 combinedBufferSize =
1021 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1022 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1023 placedFootprints[i].Offset = 0;
1024 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1025 }
1026 }
1027
1028 // TODO: do this until we have slices of buttery buffers
1029 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
1030 GrGpuBufferType::kXferCpuToGpu,
1031 kDynamic_GrAccessPattern);
1032 if (!transferBuffer) {
1033 return false;
1034 }
1035 char* bufferData = (char*)transferBuffer->map();
1036 SkASSERT(bufferData);
1037
1038 bool result;
1039 if (data->type() == BackendTextureData::Type::kPixmaps) {
1040 result = copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(),
1041 data->pixmaps(), info.fLevelCount);
1042 } else if (data->type() == BackendTextureData::Type::kCompressed) {
1043 memcpy(bufferData, data->compressedData(), data->compressedSize());
1044 result = true;
1045 } else {
1046 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1047 SkImage::CompressionType compression =
1048 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1049 if (SkImage::CompressionType::kNone == compression) {
Greg Daniel746460e2020-06-30 13:13:53 -04001050 result = copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1051 backendTexture.dimensions(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001052 placedFootprints, data->color());
1053 } else {
1054 GrFillInCompressedData(compression, backendTexture.dimensions(),
1055 backendTexture.fMipMapped, bufferData, data->color());
1056 result = true;
1057 }
1058 }
1059 transferBuffer->unmap();
1060
1061 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
1062 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(),
1063 0, 0);
1064
1065 if (finishedCallback) {
1066 this->addFinishedCallback(std::move(finishedCallback));
1067 }
1068
Greg Daniel16032b32020-05-06 15:31:10 -04001069 return true;
1070}
1071
Greg Danielc1ad77c2020-05-06 11:40:03 -04001072GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
1073 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
1074 GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback,
1075 const BackendTextureData* data) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001076 this->handleDirtyContext();
1077
1078 const GrD3DCaps& caps = this->d3dCaps();
1079
1080 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1081 return {};
1082 }
1083
1084 DXGI_FORMAT dxgiFormat;
1085 if (!format.asDxgiFormat(&dxgiFormat)) {
1086 return {};
1087 }
1088
1089 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1090 if (!caps.isFormatTexturable(dxgiFormat)) {
1091 return {};
1092 }
1093
1094 GrD3DTextureResourceInfo info;
1095 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1096 GrRenderable::kNo, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001097 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001098 return {};
1099 }
1100
1101 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001102}
1103
1104void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001105 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1106 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001107}
1108
Robert Phillips979b2232020-02-20 10:47:29 -05001109bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1110 return false;
1111}
1112
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001113#if GR_TEST_UTILS
1114bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001115 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1116
1117 GrD3DTextureResourceInfo info;
1118 if (!tex.getD3DTextureResourceInfo(&info)) {
1119 return false;
1120 }
1121 ID3D12Resource* textureResource = info.fResource.get();
1122 if (!textureResource) {
1123 return false;
1124 }
1125 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001126}
1127
1128GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001129 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001130 this->handleDirtyContext();
1131
1132 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001133 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001134 }
1135
1136 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1137
1138 GrD3DTextureResourceInfo info;
1139 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
1140 GrRenderable::kYes, GrMipMapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001141 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001142 return {};
1143 }
1144
1145 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001146}
1147
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001148void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1149 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1150
1151 GrD3DTextureResourceInfo info;
1152 if (rt.getD3DTextureResourceInfo(&info)) {
1153 this->testingOnly_flushGpuAndSync();
1154 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1155 // is deleted.
1156 }
1157}
1158
1159void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001160 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001161}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001162
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001163void GrD3DGpu::testingOnly_startCapture() {
1164 if (fGraphicsAnalysis) {
1165 fGraphicsAnalysis->BeginCapture();
1166 }
1167}
1168
1169void GrD3DGpu::testingOnly_endCapture() {
1170 if (fGraphicsAnalysis) {
1171 fGraphicsAnalysis->EndCapture();
1172 }
1173}
1174#endif
1175
Jim Van Verthc632aa62020-04-17 16:58:20 -04001176///////////////////////////////////////////////////////////////////////////////
1177
Greg Daniela5a6b322020-04-23 12:52:27 -04001178void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001179 int numBarriers,
1180 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1181 SkASSERT(fCurrentDirectCommandList);
1182 SkASSERT(resource);
1183
Greg Daniela5a6b322020-04-23 12:52:27 -04001184 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001185}
1186
Greg Daniel9efe3862020-06-11 11:51:06 -04001187void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1188 GrSurfaceProxy* proxies[],
1189 int numProxies,
1190 SkSurface::BackendSurfaceAccess access,
1191 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001192 SkASSERT(numProxies >= 0);
1193 SkASSERT(!numProxies || proxies);
1194
1195 // prepare proxies by transitioning to PRESENT renderState
1196 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1197 GrD3DTextureResource* resource;
1198 for (int i = 0; i < numProxies; ++i) {
1199 SkASSERT(proxies[i]->isInstantiated());
1200 if (GrTexture* tex = proxies[i]->peekTexture()) {
1201 resource = static_cast<GrD3DTexture*>(tex);
1202 } else {
1203 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1204 SkASSERT(rt);
1205 resource = static_cast<GrD3DRenderTarget*>(rt);
1206 }
1207 resource->prepareForPresent(this);
1208 }
1209 }
1210}
1211
Jim Van Verthc632aa62020-04-17 16:58:20 -04001212bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1213 if (syncCpu) {
1214 return this->submitDirectCommandList(SyncQueue::kForce);
1215 } else {
1216 return this->submitDirectCommandList(SyncQueue::kSkip);
1217 }
1218}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001219
1220std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1221 return GrD3DSemaphore::Make(this);
1222}
1223std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1224 const GrBackendSemaphore& semaphore,
1225 GrResourceProvider::SemaphoreWrapType,
1226 GrWrapOwnership) {
1227 SkASSERT(this->caps()->semaphoreSupport());
1228 GrD3DFenceInfo fenceInfo;
1229 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1230 return nullptr;
1231 }
1232 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1233}
1234
1235void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001236 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001237 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1238 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1239 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1240}
1241
1242void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001243 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001244 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1245 // TODO: Do we need to track the lifetime of this?
1246 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1247}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001248
1249GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001250 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001251 return fCurrentFenceValue;
1252}
1253
1254bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001255 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001256}