blob: 28edf5e00f8d65986e28be28df13240cf591747e [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 Verth1e6460d2020-06-30 15:47:52 -040081 this->waitFence(fCurrentFenceValue);
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 Verth1e6460d2020-06-30 15:47:52 -0400124 this->waitFence(fCurrentFenceValue);
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 Verth1e6460d2020-06-30 15:47:52 -0400139 this->waitFence(fence);
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 Verthd2d4c5e2020-02-19 14:57:58 -0500172void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400173 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
174
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500175 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400176 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500177}
178
Greg Danield4928d02020-06-19 11:13:26 -0400179void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
180 GrGpuFinishedContext finishedContext) {
181 SkASSERT(finishedProc);
182 sk_sp<GrRefCntedCallback> finishedCallback(
183 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400184 this->addFinishedCallback(std::move(finishedCallback));
185}
186
187void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
188 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400189 // Besides the current command list, we also add the finishedCallback to the newest outstanding
190 // command list. Our contract for calling the proc is that all previous submitted command lists
191 // have finished when we call it. However, if our current command list has no work when it is
192 // flushed it will drop its ref to the callback immediately. But the previous work may not have
193 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
194 // must finish after all previously submitted command lists.
195 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
196 if (back) {
197 back->fCommandList->addFinishedCallback(finishedCallback);
198 }
199 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
200}
201
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500202void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) {
203 // TODO
204}
205
206sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
207 const GrBackendFormat& format,
208 GrRenderable renderable,
209 int renderTargetSampleCnt,
210 SkBudgeted budgeted,
211 GrProtected isProtected,
212 int mipLevelCount,
213 uint32_t levelClearMask) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400214 DXGI_FORMAT dxgiFormat;
215 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
216 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
217
218 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
219
220 if (renderable == GrRenderable::kYes) {
221 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
222 }
223
224 // This desc refers to the texture that will be read by the client. Thus even if msaa is
225 // requested, this describes the resolved texture. Therefore we always have samples set
226 // to 1.
227 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400228 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400229 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
230 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
231 // might want to manually set alignment to 4KB for smaller textures
232 resourceDesc.Alignment = 0;
233 resourceDesc.Width = dimensions.fWidth;
234 resourceDesc.Height = dimensions.fHeight;
235 resourceDesc.DepthOrArraySize = 1;
236 resourceDesc.MipLevels = mipLevelCount;
237 resourceDesc.Format = dxgiFormat;
238 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400239 // quality levels are only supported for tiled resources so ignore for now
240 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400241 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400242 resourceDesc.Flags = usageFlags;
243
244 GrMipMapsStatus mipMapsStatus =
245 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
246
247 sk_sp<GrD3DTexture> tex;
248 if (renderable == GrRenderable::kYes) {
249 tex = GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
250 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
251 mipMapsStatus);
252 } else {
253 tex = GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
254 mipMapsStatus);
255 }
256
257 if (!tex) {
258 return nullptr;
259 }
260
261 if (levelClearMask) {
262 // TODO
263 }
264 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500265}
266
267sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
268 const GrBackendFormat& format,
269 SkBudgeted budgeted,
270 GrMipMapped mipMapped,
271 GrProtected isProtected,
272 const void* data, size_t dataSize) {
273 // TODO
274 return nullptr;
275}
276
Jim Van Verth9145f782020-04-28 12:01:12 -0400277static int get_surface_sample_cnt(GrSurface* surf) {
278 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
279 return rt->numSamples();
280 }
281 return 0;
282}
283
284bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
285 const SkIPoint& dstPoint) {
286
287 if (src->isProtected() && !dst->isProtected()) {
288 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400289 return false;
290 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400291
292 int dstSampleCnt = get_surface_sample_cnt(dst);
293 int srcSampleCnt = get_surface_sample_cnt(src);
294
295 GrD3DTextureResource* dstTexResource;
296 GrD3DTextureResource* srcTexResource;
297 GrRenderTarget* dstRT = dst->asRenderTarget();
298 if (dstRT) {
299 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
300 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
301 } else {
302 SkASSERT(dst->asTexture());
303 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
304 }
305 GrRenderTarget* srcRT = src->asRenderTarget();
306 if (srcRT) {
307 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
308 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
309 } else {
310 SkASSERT(src->asTexture());
311 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
312 }
313
314 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
315 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
316
317 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
318 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
319 return true;
320 }
321
322 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
323 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
324 return true;
325 }
326
327 return false;
328}
329
330void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
331 GrD3DTextureResource* dstResource,
332 GrD3DTextureResource* srcResource,
333 const SkIRect& srcRect, const SkIPoint& dstPoint) {
334#ifdef SK_DEBUG
335 int dstSampleCnt = get_surface_sample_cnt(dst);
336 int srcSampleCnt = get_surface_sample_cnt(src);
337 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
338 DXGI_FORMAT srcFormat;
339 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
340 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
341#endif
342 if (src->isProtected() && !dst->isProtected()) {
343 SkDebugf("Can't copy from protected memory to non-protected");
344 return;
345 }
346
347 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
348 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
349
350 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
351 dstLocation.pResource = dstResource->d3dResource();
352 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
353 dstLocation.SubresourceIndex = 0;
354
355 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
356 srcLocation.pResource = srcResource->d3dResource();
357 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
358 srcLocation.SubresourceIndex = 0;
359
360 D3D12_BOX srcBox = {};
361 srcBox.left = srcRect.fLeft;
362 srcBox.top = srcRect.fTop;
363 srcBox.right = srcRect.fRight;
364 srcBox.bottom = srcRect.fBottom;
365 srcBox.front = 0;
366 srcBox.back = 1;
367 // TODO: use copyResource if copying full resource and sizes match
368 fCurrentDirectCommandList->copyTextureRegion(dstResource->resource(),
369 &dstLocation,
370 dstPoint.fX, dstPoint.fY,
371 srcResource->resource(),
372 &srcLocation,
373 &srcBox);
374
375 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
376 srcRect.width(), srcRect.height());
377 // The rect is already in device space so we pass in kTopLeft so no flip is done.
378 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
379}
380
381void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
382 const SkIPoint& dstPoint) {
383 // TODO
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400384}
385
Jim Van Verthba7f2292020-04-21 08:56:47 -0400386bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
387 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
388 size_t rowBytes) {
389 SkASSERT(surface);
390
391 if (surfaceColorType != dstColorType) {
392 return false;
393 }
394
395 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400396 GrD3DTextureResource* texResource = nullptr;
397 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
398 if (rt) {
399 texResource = rt;
400 } else {
401 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
402 }
403
404 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400405 return false;
406 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400407
Jim Van Verthba7f2292020-04-21 08:56:47 -0400408 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400409 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400410 SkASSERT(srcLocation.pResource);
411 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
412 srcLocation.SubresourceIndex = 0;
413
414 D3D12_BOX srcBox = {};
415 srcBox.left = left;
416 srcBox.top = top;
417 srcBox.right = left + width;
418 srcBox.bottom = top + height;
419 srcBox.front = 0;
420 srcBox.back = 1;
421
422 // Set up dst location and create transfer buffer
423 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
424 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400425 UINT64 transferTotalBytes;
426 const UINT64 baseOffset = 0;
427 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
428 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400429 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400430 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400431 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400432 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400433 return false;
434 }
435 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400436
437 // TODO: implement some way of reusing buffers instead of making a new one every time.
438 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
439 GrGpuBufferType::kXferGpuToCpu,
440 kDynamic_GrAccessPattern);
441 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
442 dstLocation.pResource = d3dBuf->d3dResource();
443
444 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400445 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400446
447 fCurrentDirectCommandList->copyTextureRegion(d3dBuf->resource(), &dstLocation, 0, 0,
Jim Van Verthc12aad92020-04-24 16:19:01 -0400448 texResource->resource(), &srcLocation, &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400449 this->submitDirectCommandList(SyncQueue::kForce);
450
451 const void* mappedMemory = transferBuffer->map();
452
453 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400454 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400455
456 transferBuffer->unmap();
457
458 return true;
459}
460
461bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
462 GrColorType surfaceColorType, GrColorType srcColorType,
463 const GrMipLevel texels[], int mipLevelCount,
464 bool prepForTexSampling) {
465 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
466 if (!d3dTex) {
467 return false;
468 }
469
470 // Make sure we have at least the base level
471 if (!mipLevelCount || !texels[0].fPixels) {
472 return false;
473 }
474
475 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
476 bool success = false;
477
478 // Need to change the resource state to COPY_DEST in order to upload to it
479 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
480
481 SkASSERT(mipLevelCount <= d3dTex->texturePriv().maxMipMapLevel() + 1);
482 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
483 mipLevelCount);
484
485 if (prepForTexSampling) {
486 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
487 }
488
489 return success;
490}
491
492bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
493 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
494 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
495 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
496 SkASSERT(1 == mipLevelCount ||
497 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
498
499 // We assume that if the texture has mip levels, we either upload to all the levels or just the
500 // first.
501 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
502
503 if (width == 0 || height == 0) {
504 return false;
505 }
506
507 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
508 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
509
510 ID3D12Resource* d3dResource = tex->d3dResource();
511 SkASSERT(d3dResource);
512 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
513 // Either upload only the first miplevel or all miplevels
514 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
515
516 if (1 == mipLevelCount && !texels[0].fPixels) {
517 return true; // no data to upload
518 }
519
520 for (int i = 0; i < mipLevelCount; ++i) {
521 // We do not allow any gaps in the mip data
522 if (!texels[i].fPixels) {
523 return false;
524 }
525 }
526
527 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400528 UINT64 combinedBufferSize;
529 // We reset the width and height in the description to match our subrectangle size
530 // so we don't end up allocating more space than we need.
531 desc.Width = width;
532 desc.Height = height;
533 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400534 nullptr, nullptr, &combinedBufferSize);
535 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400536 SkASSERT(combinedBufferSize);
537
538 // TODO: do this until we have slices of buttery buffers
539 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
540 GrGpuBufferType::kXferCpuToGpu,
541 kDynamic_GrAccessPattern);
542 if (!transferBuffer) {
543 return false;
544 }
545 char* bufferData = (char*)transferBuffer->map();
546
547 int currentWidth = width;
548 int currentHeight = height;
549 int layerHeight = tex->height();
550
551 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
552 if (texels[currentMipLevel].fPixels) {
553 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
554
Jim Van Verthfdd36852020-04-21 16:29:44 -0400555 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400556 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
557
558 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
559
560 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400561 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400562 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
563 src, srcRowBytes, trimRowBytes, currentHeight);
564 }
565 currentWidth = std::max(1, currentWidth / 2);
566 currentHeight = std::max(1, currentHeight / 2);
567 layerHeight = currentHeight;
568 }
569
570 transferBuffer->unmap();
571
572 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
573 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
574 placedFootprints.get(), left, top);
575
576 if (mipLevelCount < (int)desc.MipLevels) {
577 tex->texturePriv().markMipMapsDirty();
578 }
579
580 return true;
581}
582
Jim Van Verth9145f782020-04-28 12:01:12 -0400583static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
584 if (!info.fResource.get()) {
585 return false;
586 }
587 return true;
588}
589
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400590static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
591 if (!caps.isFormatTexturable(info.fFormat)) {
592 return false;
593 }
594 return true;
595}
596
597static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
598 int sampleCnt) {
599 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
600 return false;
601 }
602 return true;
603}
604
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400605sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
606 GrWrapOwnership,
607 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400608 GrIOType ioType) {
609 GrD3DTextureResourceInfo textureInfo;
610 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
611 return nullptr;
612 }
613
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400614 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400615 return nullptr;
616 }
617
618 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
619 return nullptr;
620 }
621
622 // TODO: support protected context
623 if (tex.isProtected()) {
624 return nullptr;
625 }
626
627 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
628 SkASSERT(state);
629 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
630 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500631}
632
633sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400634 GrWrapOwnership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500635 GrWrapCacheable wrapType) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400636 GrD3DTextureResourceInfo textureInfo;
637 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
638 return nullptr;
639 }
640
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400641 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400642 return nullptr;
643 }
644
645 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
646 return nullptr;
647 }
648
649 // TODO: support protected context
650 if (tex.isProtected()) {
651 return nullptr;
652 }
653
654 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
655 SkASSERT(state);
656 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, kRead_GrIOType,
657 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500658}
659
660sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
661 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500662 GrWrapOwnership ownership,
663 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400664 GrD3DTextureResourceInfo textureInfo;
665 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
666 return nullptr;
667 }
668
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400669 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400670 return nullptr;
671 }
672
673 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
674 return nullptr;
675 }
676 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
677 return nullptr;
678 }
679
680 // TODO: support protected context
681 if (tex.isProtected()) {
682 return nullptr;
683 }
684
685 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
686
687 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
688 SkASSERT(state);
689
690 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
691 sampleCnt, cacheable,
692 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500693}
694
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400695sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400696 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
697 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
698 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
699 // creating and owning the MSAA images.
700 if (rt.sampleCnt() > 1) {
701 return nullptr;
702 }
703
704 GrD3DTextureResourceInfo info;
705 if (!rt.getD3DTextureResourceInfo(&info)) {
706 return nullptr;
707 }
708
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400709 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400710 return nullptr;
711 }
712
713 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
714 return nullptr;
715 }
716
717 // TODO: support protected context
718 if (rt.isProtected()) {
719 return nullptr;
720 }
721
722 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
723
724 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
725 this, rt.dimensions(), 1, info, std::move(state));
726
727 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
728 SkASSERT(!rt.stencilBits());
729 if (tgt) {
730 SkASSERT(tgt->canAttemptStencilAttachment());
731 }
732
733 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500734}
735
736sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400737 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400738
739 GrD3DTextureResourceInfo textureInfo;
740 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
741 return nullptr;
742 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400743 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400744 return nullptr;
745 }
746
747 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
748 return nullptr;
749 }
750
751 // TODO: support protected context
752 if (tex.isProtected()) {
753 return nullptr;
754 }
755
756 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
757 if (!sampleCnt) {
758 return nullptr;
759 }
760
761 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
762 SkASSERT(state);
763
764 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
765 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500766}
767
768sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400769 GrAccessPattern accessPattern, const void* data) {
770 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
771 if (data && buffer) {
772 buffer->updateData(data, sizeInBytes);
773 }
774
775 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500776}
777
778GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
779 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400780 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
781 SkASSERT(width >= rt->width());
782 SkASSERT(height >= rt->height());
783
784 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
785
786 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
787 width,
788 height,
789 numStencilSamples,
790 sFmt));
791 fStats.incStencilAttachmentCreates();
792 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500793}
794
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400795bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
796 SkISize dimensions,
797 GrTexturable texturable,
798 GrRenderable renderable,
799 GrMipMapped mipMapped,
800 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400801 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400802 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400803
804 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
805 return false;
806 }
807
808 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
809 return false;
810 }
811
812 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
813 return false;
814 }
815
816 int numMipLevels = 1;
817 if (mipMapped == GrMipMapped::kYes) {
818 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
819 }
820
821 // create the texture
822 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
823 if (renderable == GrRenderable::kYes) {
824 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
825 }
826
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400827 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400828 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400829 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400830 resourceDesc.Width = dimensions.fWidth;
831 resourceDesc.Height = dimensions.fHeight;
832 resourceDesc.DepthOrArraySize = 1;
833 resourceDesc.MipLevels = numMipLevels;
834 resourceDesc.Format = dxgiFormat;
835 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400836 // quality levels are only supported for tiled resources so ignore for now
837 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400838 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400839 resourceDesc.Flags = usageFlags;
840
Jim Van Verth280d4f72020-05-04 10:04:24 -0400841 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
842 D3D12_CLEAR_VALUE clearValue = {};
843 if (renderable == GrRenderable::kYes) {
844 clearValue.Format = dxgiFormat;
845 // Assume transparent black
846 clearValue.Color[0] = 0;
847 clearValue.Color[1] = 0;
848 clearValue.Color[2] = 0;
849 clearValue.Color[3] = 0;
850 clearValuePtr = &clearValue;
851 }
852
Greg Daniel16032b32020-05-06 15:31:10 -0400853 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
854 ? D3D12_RESOURCE_STATE_RENDER_TARGET
855 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400856 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400857 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400858 SkDebugf("Failed to init texture resource info\n");
859 return false;
860 }
861
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400862 return true;
863}
864
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500865GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400866 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400867 GrRenderable renderable,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400868 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400869 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400870 this->handleDirtyContext();
871
872 const GrD3DCaps& caps = this->d3dCaps();
873
874 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
875 return {};
876 }
877
878 DXGI_FORMAT dxgiFormat;
879 if (!format.asDxgiFormat(&dxgiFormat)) {
880 return {};
881 }
882
883 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
884 if (!caps.isFormatTexturable(dxgiFormat)) {
885 return {};
886 }
887
888 GrD3DTextureResourceInfo info;
889 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
890 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400891 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400892 return {};
893 }
894
895 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500896}
897
Jim Van Verth43a6e172020-06-23 11:59:08 -0400898bool copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
899 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
900 const SkPixmap srcData[], int numMipLevels) {
901 SkASSERT(srcData && numMipLevels);
902 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
903 SkASSERT(mapPtr);
904
905 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
906
907 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
908 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
909
910 // copy data into the buffer, skipping any trailing bytes
911 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
912 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
913 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
914 trimRowBytes, srcData[currentMipLevel].height());
915 }
916
917 return true;
918}
919
Greg Daniel746460e2020-06-30 13:13:53 -0400920bool copy_color_data(const GrD3DCaps& caps, char* mapPtr, DXGI_FORMAT dxgiFormat,
921 SkISize dimensions, D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
922 SkColor4f color) {
923 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -0400924 if (colorType == GrColorType::kUnknown) {
925 return false;
926 }
927 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
928 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
929 return false;
930 }
931
932 return true;
933}
934
Greg Daniel16032b32020-05-06 15:31:10 -0400935bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
936 sk_sp<GrRefCntedCallback> finishedCallback,
937 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -0400938 GrD3DTextureResourceInfo info;
939 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
940
941 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
942 SkASSERT(state);
943 sk_sp<GrD3DTexture> texture =
944 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
945 GrWrapCacheable::kNo,
946 kRW_GrIOType, info, std::move(state));
947 if (!texture) {
948 return false;
949 }
950
951 GrD3DDirectCommandList* cmdList = this->currentCommandList();
952 if (!cmdList) {
953 return false;
954 }
955
956 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
957
958 ID3D12Resource* d3dResource = texture->d3dResource();
959 SkASSERT(d3dResource);
960 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
961 unsigned int mipLevelCount = 1;
962 if (backendTexture.fMipMapped == GrMipMapped::kYes) {
963 mipLevelCount = SkMipMap::ComputeLevelCount(backendTexture.dimensions().width(),
964 backendTexture.dimensions().height()) + 1;
965 }
966 SkASSERT(mipLevelCount == info.fLevelCount);
967 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
968 UINT64 combinedBufferSize;
969 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
970 nullptr, nullptr, &combinedBufferSize);
971 SkASSERT(combinedBufferSize);
972 if (data->type() == BackendTextureData::Type::kColor &&
973 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
974 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
975 combinedBufferSize =
976 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
977 for (unsigned int i = 1; i < mipLevelCount; ++i) {
978 placedFootprints[i].Offset = 0;
979 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
980 }
981 }
982
983 // TODO: do this until we have slices of buttery buffers
984 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize,
985 GrGpuBufferType::kXferCpuToGpu,
986 kDynamic_GrAccessPattern);
987 if (!transferBuffer) {
988 return false;
989 }
990 char* bufferData = (char*)transferBuffer->map();
991 SkASSERT(bufferData);
992
993 bool result;
994 if (data->type() == BackendTextureData::Type::kPixmaps) {
995 result = copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(),
996 data->pixmaps(), info.fLevelCount);
997 } else if (data->type() == BackendTextureData::Type::kCompressed) {
998 memcpy(bufferData, data->compressedData(), data->compressedSize());
999 result = true;
1000 } else {
1001 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1002 SkImage::CompressionType compression =
1003 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1004 if (SkImage::CompressionType::kNone == compression) {
Greg Daniel746460e2020-06-30 13:13:53 -04001005 result = copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1006 backendTexture.dimensions(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001007 placedFootprints, data->color());
1008 } else {
1009 GrFillInCompressedData(compression, backendTexture.dimensions(),
1010 backendTexture.fMipMapped, bufferData, data->color());
1011 result = true;
1012 }
1013 }
1014 transferBuffer->unmap();
1015
1016 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get());
1017 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(),
1018 0, 0);
1019
1020 if (finishedCallback) {
1021 this->addFinishedCallback(std::move(finishedCallback));
1022 }
1023
Greg Daniel16032b32020-05-06 15:31:10 -04001024 return true;
1025}
1026
Greg Danielc1ad77c2020-05-06 11:40:03 -04001027GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
1028 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
1029 GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback,
1030 const BackendTextureData* data) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001031 this->handleDirtyContext();
1032
1033 const GrD3DCaps& caps = this->d3dCaps();
1034
1035 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1036 return {};
1037 }
1038
1039 DXGI_FORMAT dxgiFormat;
1040 if (!format.asDxgiFormat(&dxgiFormat)) {
1041 return {};
1042 }
1043
1044 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1045 if (!caps.isFormatTexturable(dxgiFormat)) {
1046 return {};
1047 }
1048
1049 GrD3DTextureResourceInfo info;
1050 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1051 GrRenderable::kNo, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001052 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001053 return {};
1054 }
1055
1056 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001057}
1058
1059void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001060 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1061 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001062}
1063
Robert Phillips979b2232020-02-20 10:47:29 -05001064bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1065 return false;
1066}
1067
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001068#if GR_TEST_UTILS
1069bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001070 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1071
1072 GrD3DTextureResourceInfo info;
1073 if (!tex.getD3DTextureResourceInfo(&info)) {
1074 return false;
1075 }
1076 ID3D12Resource* textureResource = info.fResource.get();
1077 if (!textureResource) {
1078 return false;
1079 }
1080 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001081}
1082
1083GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001084 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001085 this->handleDirtyContext();
1086
1087 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001088 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001089 }
1090
1091 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1092
1093 GrD3DTextureResourceInfo info;
1094 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
1095 GrRenderable::kYes, GrMipMapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001096 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001097 return {};
1098 }
1099
1100 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001101}
1102
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001103void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1104 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1105
1106 GrD3DTextureResourceInfo info;
1107 if (rt.getD3DTextureResourceInfo(&info)) {
1108 this->testingOnly_flushGpuAndSync();
1109 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1110 // is deleted.
1111 }
1112}
1113
1114void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001115 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001116}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001117
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001118void GrD3DGpu::testingOnly_startCapture() {
1119 if (fGraphicsAnalysis) {
1120 fGraphicsAnalysis->BeginCapture();
1121 }
1122}
1123
1124void GrD3DGpu::testingOnly_endCapture() {
1125 if (fGraphicsAnalysis) {
1126 fGraphicsAnalysis->EndCapture();
1127 }
1128}
1129#endif
1130
Jim Van Verthc632aa62020-04-17 16:58:20 -04001131///////////////////////////////////////////////////////////////////////////////
1132
Greg Daniela5a6b322020-04-23 12:52:27 -04001133void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001134 int numBarriers,
1135 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1136 SkASSERT(fCurrentDirectCommandList);
1137 SkASSERT(resource);
1138
Greg Daniela5a6b322020-04-23 12:52:27 -04001139 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001140}
1141
Greg Daniel9efe3862020-06-11 11:51:06 -04001142void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1143 GrSurfaceProxy* proxies[],
1144 int numProxies,
1145 SkSurface::BackendSurfaceAccess access,
1146 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001147 SkASSERT(numProxies >= 0);
1148 SkASSERT(!numProxies || proxies);
1149
1150 // prepare proxies by transitioning to PRESENT renderState
1151 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1152 GrD3DTextureResource* resource;
1153 for (int i = 0; i < numProxies; ++i) {
1154 SkASSERT(proxies[i]->isInstantiated());
1155 if (GrTexture* tex = proxies[i]->peekTexture()) {
1156 resource = static_cast<GrD3DTexture*>(tex);
1157 } else {
1158 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1159 SkASSERT(rt);
1160 resource = static_cast<GrD3DRenderTarget*>(rt);
1161 }
1162 resource->prepareForPresent(this);
1163 }
1164 }
1165}
1166
Jim Van Verthc632aa62020-04-17 16:58:20 -04001167bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1168 if (syncCpu) {
1169 return this->submitDirectCommandList(SyncQueue::kForce);
1170 } else {
1171 return this->submitDirectCommandList(SyncQueue::kSkip);
1172 }
1173}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001174
1175std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1176 return GrD3DSemaphore::Make(this);
1177}
1178std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1179 const GrBackendSemaphore& semaphore,
1180 GrResourceProvider::SemaphoreWrapType,
1181 GrWrapOwnership) {
1182 SkASSERT(this->caps()->semaphoreSupport());
1183 GrD3DFenceInfo fenceInfo;
1184 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1185 return nullptr;
1186 }
1187 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1188}
1189
1190void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001191 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001192 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1193 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1194 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1195}
1196
1197void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001198 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001199 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1200 // TODO: Do we need to track the lifetime of this?
1201 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1202}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001203
1204GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001205 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001206 return fCurrentFenceValue;
1207}
1208
1209bool GrD3DGpu::waitFence(GrFence fence) {
1210 if (fFence->GetCompletedValue() < fence) {
1211 HANDLE fenceEvent;
1212 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
1213 SkASSERT(fenceEvent);
Jim Van Verthe3810362020-07-01 10:12:11 -04001214 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fence, fenceEvent));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001215 WaitForSingleObject(fenceEvent, INFINITE);
1216 CloseHandle(fenceEvent);
1217 }
1218
1219 return true;
1220}