blob: f2c796e89f29b59a991b40bf8677f3499ef0f28e [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"
Mike Reed13711eb2020-07-14 17:16:32 -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"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040016#include "src/gpu/GrTexture.h"
Jim Van Verth1b89eb72020-09-23 16:29:51 -040017#include "src/gpu/d3d/GrD3DAMDMemoryAllocator.h"
Jim Van Verthd6ad4802020-04-03 14:59:20 -040018#include "src/gpu/d3d/GrD3DBuffer.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050019#include "src/gpu/d3d/GrD3DCaps.h"
20#include "src/gpu/d3d/GrD3DOpsRenderPass.h"
Jim Van Verthc1a67b52020-06-25 13:10:29 -040021#include "src/gpu/d3d/GrD3DSemaphore.h"
Jim Van Verth4f51f472020-04-13 11:02:21 -040022#include "src/gpu/d3d/GrD3DStencilAttachment.h"
Jim Van Verthaa90dad2020-03-30 15:00:39 -040023#include "src/gpu/d3d/GrD3DTexture.h"
24#include "src/gpu/d3d/GrD3DTextureRenderTarget.h"
25#include "src/gpu/d3d/GrD3DUtil.h"
Greg Daniel5fc5c812020-04-23 10:30:23 -040026#include "src/sksl/SkSLCompiler.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050027
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040028#if GR_TEST_UTILS
29#include <DXProgrammableCapture.h>
30#endif
31
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050032sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext,
Adlai Holler3d0359a2020-07-09 15:35:55 -040033 const GrContextOptions& contextOptions, GrDirectContext* direct) {
Jim Van Verth1b89eb72020-09-23 16:29:51 -040034 sk_sp<GrD3DMemoryAllocator> memoryAllocator = backendContext.fMemoryAllocator;
35 if (!memoryAllocator) {
36 // We were not given a memory allocator at creation
37 memoryAllocator = GrD3DAMDMemoryAllocator::Make(
38 backendContext.fAdapter.get(), backendContext.fDevice.get());
39 }
40 if (!memoryAllocator) {
41 SkDEBUGFAIL("No supplied Direct3D memory allocator and unable to create one internally.");
42 return nullptr;
43 }
44
45 return sk_sp<GrGpu>(new GrD3DGpu(direct, contextOptions, backendContext, memoryAllocator));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050046}
47
Greg Daniel83ed2132020-03-24 13:15:33 -040048// This constant determines how many OutstandingCommandLists are allocated together as a block in
49// the deque. As such it needs to balance allocating too much memory vs. incurring
50// allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding
51// command lists we expect to see.
52static const int kDefaultOutstandingAllocCnt = 8;
53
Jim Van Verth711b0392020-07-23 16:45:00 -040054// constants have to be aligned to 256
55constexpr int kConstantAlignment = 256;
56
Adlai Holler3d0359a2020-07-09 15:35:55 -040057GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions,
Jim Van Verth1b89eb72020-09-23 16:29:51 -040058 const GrD3DBackendContext& backendContext,
59 sk_sp<GrD3DMemoryAllocator> allocator)
Adlai Holler3d0359a2020-07-09 15:35:55 -040060 : INHERITED(direct)
Jim Van Verth03b8ab22020-02-24 11:36:15 -050061 , fDevice(backendContext.fDevice)
Greg Daniel02c45902020-03-09 10:58:09 -040062 , fQueue(backendContext.fQueue)
Jim Van Verth1b89eb72020-09-23 16:29:51 -040063 , fMemoryAllocator(std::move(allocator))
Greg Daniel83ed2132020-03-24 13:15:33 -040064 , fResourceProvider(this)
Greg Danielcffb0622020-07-16 13:19:17 -040065 , fStagingBufferManager(this)
Jim Van Verth711b0392020-07-23 16:45:00 -040066 , fConstantsRingBuffer(this, 128 * 1024, kConstantAlignment, GrGpuBufferType::kVertex)
Greg Daniel5fc5c812020-04-23 10:30:23 -040067 , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt)
68 , fCompiler(new SkSL::Compiler()) {
Jim Van Verth8ec13302020-02-26 12:59:56 -050069 fCaps.reset(new GrD3DCaps(contextOptions,
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040070 backendContext.fAdapter.get(),
71 backendContext.fDevice.get()));
Greg Daniel85da3362020-03-09 15:18:35 -040072
73 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
Greg Daniele52c9782020-03-23 14:18:37 -040074 SkASSERT(fCurrentDirectCommandList);
Greg Daniel83ed2132020-03-24 13:15:33 -040075
76 SkASSERT(fCurrentFenceValue == 0);
Jim Van Verthe3810362020-07-01 10:12:11 -040077 GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE,
78 IID_PPV_ARGS(&fFence)));
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040079
80#if GR_TEST_UTILS
81 HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis));
82 if (FAILED(getAnalysis)) {
83 fGraphicsAnalysis = nullptr;
84 }
85#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050086}
87
Greg Daniel83ed2132020-03-24 13:15:33 -040088GrD3DGpu::~GrD3DGpu() {
89 this->destroyResources();
90}
91
92void GrD3DGpu::destroyResources() {
93 if (fCurrentDirectCommandList) {
94 fCurrentDirectCommandList->close();
Jim Van Verthba7f2292020-04-21 08:56:47 -040095 fCurrentDirectCommandList->reset();
Greg Daniel83ed2132020-03-24 13:15:33 -040096 }
97
98 // We need to make sure everything has finished on the queue.
Jim Van Verth3b0d7d12020-07-06 11:52:42 -040099 this->waitForQueueCompletion();
Greg Daniel83ed2132020-03-24 13:15:33 -0400100
101 SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();)
102
103 // We used a placement new for each object in fOutstandingCommandLists, so we're responsible
104 // for calling the destructor on each of them as well.
105 while (!fOutstandingCommandLists.empty()) {
Jim Van Verthf43da142020-06-09 16:34:43 -0400106 OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400107 SkASSERT(list->fFenceValue <= fenceValue);
108 // No reason to recycle the command lists since we are destroying all resources anyways.
109 list->~OutstandingCommandList();
Jim Van Verthf43da142020-06-09 16:34:43 -0400110 fOutstandingCommandLists.pop_front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400111 }
Jim Van Verthf2788862020-06-03 17:33:12 -0400112
Greg Danielcffb0622020-07-16 13:19:17 -0400113 fStagingBufferManager.reset();
114
Jim Van Verthf2788862020-06-03 17:33:12 -0400115 fResourceProvider.destroyResources();
Greg Daniel83ed2132020-03-24 13:15:33 -0400116}
Greg Daniel31a7b072020-02-26 15:31:49 -0500117
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500118GrOpsRenderPass* GrD3DGpu::getOpsRenderPass(
Greg Daniel9a18b082020-08-14 14:03:50 -0400119 GrRenderTarget* rt, GrStencilAttachment*,
120 GrSurfaceOrigin origin, const SkIRect& bounds,
121 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
122 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
123 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400124 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500125 if (!fCachedOpsRenderPass) {
126 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
127 }
128
129 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
130 return nullptr;
131 }
132 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500133}
134
Jim Van Verthc632aa62020-04-17 16:58:20 -0400135bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400136 SkASSERT(fCurrentDirectCommandList);
137
Jim Van Verth3eadce22020-06-01 11:34:49 -0400138 fResourceProvider.prepForSubmit();
139
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400140 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
Jim Van Verthc632aa62020-04-17 16:58:20 -0400141 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
142 return false;
143 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
144 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400145 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400146 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400147 }
148 return true;
149 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400150
Greg Daniela581a8b2020-06-19 15:22:18 -0400151 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
152 // uniform data as dirty.
153 fResourceProvider.markPipelineStateUniformsDirty();
154
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400155 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400156 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400157 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400158
Jim Van Verthc632aa62020-04-17 16:58:20 -0400159 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400160 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400161 }
162
Greg Daniel83ed2132020-03-24 13:15:33 -0400163 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
164
165 // This should be done after we have a new command list in case the freeing of any resources
166 // held by a finished command list causes us send a new command to the gpu (like changing the
167 // resource state.
168 this->checkForFinishedCommandLists();
169
170 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400171 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400172}
173
174void GrD3DGpu::checkForFinishedCommandLists() {
175 uint64_t currentFenceValue = fFence->GetCompletedValue();
176
177 // Iterate over all the outstanding command lists to see if any have finished. The commands
178 // lists are in order from oldest to newest, so we start at the front to check if their fence
179 // value is less than the last signaled value. If so we pop it off and move onto the next.
180 // Repeat till we find a command list that has not finished yet (and all others afterwards are
181 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400182 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
183 while (front && front->fFenceValue <= currentFenceValue) {
184 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400185 // Since we used placement new we are responsible for calling the destructor manually.
186 front->~OutstandingCommandList();
187 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400188 fResourceProvider.recycleDirectCommandList(std::move(currList));
189 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400190 }
191}
192
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400193void GrD3DGpu::waitForQueueCompletion() {
194 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
195 HANDLE fenceEvent;
196 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
197 SkASSERT(fenceEvent);
198 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
199 WaitForSingleObject(fenceEvent, INFINITE);
200 CloseHandle(fenceEvent);
201 }
202}
203
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500204void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400205 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
206
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500207 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400208 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500209}
210
Greg Danield4928d02020-06-19 11:13:26 -0400211void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
212 GrGpuFinishedContext finishedContext) {
213 SkASSERT(finishedProc);
214 sk_sp<GrRefCntedCallback> finishedCallback(
215 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400216 this->addFinishedCallback(std::move(finishedCallback));
217}
218
219void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
220 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400221 // Besides the current command list, we also add the finishedCallback to the newest outstanding
222 // command list. Our contract for calling the proc is that all previous submitted command lists
223 // have finished when we call it. However, if our current command list has no work when it is
224 // flushed it will drop its ref to the callback immediately. But the previous work may not have
225 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
226 // must finish after all previously submitted command lists.
227 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
228 if (back) {
229 back->fCommandList->addFinishedCallback(finishedCallback);
230 }
231 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
232}
233
Jim Van Verth765c5922020-08-10 17:23:50 -0400234void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
235 SkTArray<SkPoint>* sampleLocations) {
236 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
237 // These are transformed from D3D's integer coordinate system with origin at the center,
238 // to our normalized coordinate system with origin at the upper left.
239 // This ends up corresponding with Vulkan's sample locations.
240 SkASSERT(this->caps()->sampleLocationsSupport());
241 static constexpr SkPoint kStandardSampleLocations_1[1] = {
242 {0.5f, 0.5f} };
243 static constexpr SkPoint kStandardSampleLocations_2[2] = {
244 {0.75f, 0.75f}, {0.25f, 0.25f} };
245 static constexpr SkPoint kStandardSampleLocations_4[4] = {
246 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
247 static constexpr SkPoint kStandardSampleLocations_8[8] = {
248 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
249 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
250 static constexpr SkPoint kStandardSampleLocations_16[16] = {
251 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
252 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
253 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
254 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
255
256 int numSamples = renderTarget->numSamples();
257 // TODO: support mixed samples?
258 SkASSERT(numSamples > 1);
259 SkASSERT(!renderTarget->getStencilAttachment() ||
260 numSamples == renderTarget->getStencilAttachment()->numSamples());
261
262 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
263 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
264 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
265 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
266 return;
267 }
268 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
269
270 switch (numSamples) {
271 case 2:
272 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
273 break;
274 case 4:
275 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
276 break;
277 case 8:
278 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
279 break;
280 case 16:
281 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
282 break;
283 default:
284 SK_ABORT("Invalid sample count.");
285 break;
286 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500287}
288
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400289sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
290 DXGI_FORMAT dxgiFormat,
291 GrRenderable renderable,
292 int renderTargetSampleCnt,
293 SkBudgeted budgeted,
294 GrProtected isProtected,
295 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400296 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400297 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400298 if (renderable == GrRenderable::kYes) {
299 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
300 }
301
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400302 // This desc refers to a texture that will be read by the client. Thus even if msaa is
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400303 // requested, this describes the resolved texture. Therefore we always have samples set
304 // to 1.
305 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400306 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400307 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
308 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
309 // might want to manually set alignment to 4KB for smaller textures
310 resourceDesc.Alignment = 0;
311 resourceDesc.Width = dimensions.fWidth;
312 resourceDesc.Height = dimensions.fHeight;
313 resourceDesc.DepthOrArraySize = 1;
314 resourceDesc.MipLevels = mipLevelCount;
315 resourceDesc.Format = dxgiFormat;
316 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400317 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400318 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400319 resourceDesc.Flags = usageFlags;
320
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400321 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400322 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400323 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400324 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400325 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400326 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400327 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400328 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400329}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400330
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
332 const GrBackendFormat& format,
333 GrRenderable renderable,
334 int renderTargetSampleCnt,
335 SkBudgeted budgeted,
336 GrProtected isProtected,
337 int mipLevelCount,
338 uint32_t levelClearMask) {
339 DXGI_FORMAT dxgiFormat;
340 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
341 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
342
Brian Salomona6db5102020-07-21 09:56:23 -0400343 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
344 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400345
346 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
347 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400348 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400349 if (!tex) {
350 return nullptr;
351 }
352
353 if (levelClearMask) {
354 // TODO
355 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400356
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400357 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500358}
359
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400360static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
361 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
362 UINT* numRows, UINT64* rowSizeInBytes,
363 const void* compressedData, int numMipLevels) {
364 SkASSERT(compressedData && numMipLevels);
365 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
366 SkASSERT(mapPtr);
367
368 const char* src = static_cast<const char*>(compressedData);
369 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
370 // copy data into the buffer, skipping any trailing bytes
371 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
372 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
373 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
374 numRows[currentMipLevel]);
375 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
376 }
377}
378
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500379sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
380 const GrBackendFormat& format,
381 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400382 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500383 GrProtected isProtected,
384 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400385 DXGI_FORMAT dxgiFormat;
386 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
387 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
388
389 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
390 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400391 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400392
393 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400394 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400395 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400396 }
Brian Salomona6db5102020-07-21 09:56:23 -0400397 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
398 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400399
400 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
401 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400402 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400403 if (!d3dTex) {
404 return nullptr;
405 }
406
407 ID3D12Resource* d3dResource = d3dTex->d3dResource();
408 SkASSERT(d3dResource);
409 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
410 // Either upload only the first miplevel or all miplevels
411 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
412
413 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
414 SkAutoTMalloc<UINT> numRows(mipLevelCount);
415 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
416 UINT64 combinedBufferSize;
417 // We reset the width and height in the description to match our subrectangle size
418 // so we don't end up allocating more space than we need.
419 desc.Width = dimensions.width();
420 desc.Height = dimensions.height();
421 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
422 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
423 SkASSERT(combinedBufferSize);
424
Greg Danielcffb0622020-07-16 13:19:17 -0400425 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
426 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
427 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400428 return false;
429 }
Greg Danielcffb0622020-07-16 13:19:17 -0400430
431 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400432
433 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
434 rowSizeInBytes.get(), data, mipLevelCount);
435
Greg Danielcffb0622020-07-16 13:19:17 -0400436 // Update the offsets in the footprints to be relative to the slice's offset
437 for (int i = 0; i < mipLevelCount; ++i) {
438 placedFootprints[i].Offset += slice.fOffset;
439 }
440
Greg Daniel69267912020-07-24 10:42:53 -0400441 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400442 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
443 placedFootprints.get(), 0, 0);
444
445 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500446}
447
Jim Van Verth9145f782020-04-28 12:01:12 -0400448static int get_surface_sample_cnt(GrSurface* surf) {
449 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
450 return rt->numSamples();
451 }
452 return 0;
453}
454
455bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
456 const SkIPoint& dstPoint) {
457
458 if (src->isProtected() && !dst->isProtected()) {
459 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400460 return false;
461 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400462
463 int dstSampleCnt = get_surface_sample_cnt(dst);
464 int srcSampleCnt = get_surface_sample_cnt(src);
465
466 GrD3DTextureResource* dstTexResource;
467 GrD3DTextureResource* srcTexResource;
468 GrRenderTarget* dstRT = dst->asRenderTarget();
469 if (dstRT) {
470 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
471 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
472 } else {
473 SkASSERT(dst->asTexture());
474 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
475 }
476 GrRenderTarget* srcRT = src->asRenderTarget();
477 if (srcRT) {
478 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
479 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
480 } else {
481 SkASSERT(src->asTexture());
482 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
483 }
484
485 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
486 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
487
488 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
489 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
490 return true;
491 }
492
493 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
494 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
495 return true;
496 }
497
498 return false;
499}
500
501void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
502 GrD3DTextureResource* dstResource,
503 GrD3DTextureResource* srcResource,
504 const SkIRect& srcRect, const SkIPoint& dstPoint) {
505#ifdef SK_DEBUG
506 int dstSampleCnt = get_surface_sample_cnt(dst);
507 int srcSampleCnt = get_surface_sample_cnt(src);
508 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
509 DXGI_FORMAT srcFormat;
510 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
511 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
512#endif
513 if (src->isProtected() && !dst->isProtected()) {
514 SkDebugf("Can't copy from protected memory to non-protected");
515 return;
516 }
517
518 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
519 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
520
521 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
522 dstLocation.pResource = dstResource->d3dResource();
523 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
524 dstLocation.SubresourceIndex = 0;
525
526 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
527 srcLocation.pResource = srcResource->d3dResource();
528 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
529 srcLocation.SubresourceIndex = 0;
530
531 D3D12_BOX srcBox = {};
532 srcBox.left = srcRect.fLeft;
533 srcBox.top = srcRect.fTop;
534 srcBox.right = srcRect.fRight;
535 srcBox.bottom = srcRect.fBottom;
536 srcBox.front = 0;
537 srcBox.back = 1;
538 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400539 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
540 &dstLocation,
541 dstPoint.fX, dstPoint.fY,
542 srcResource->resource(),
543 &srcLocation,
544 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400545
546 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
547 srcRect.width(), srcRect.height());
548 // The rect is already in device space so we pass in kTopLeft so no flip is done.
549 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
550}
551
552void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
553 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400554 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
555 SkASSERT(srcRT);
556
557 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
558}
559
560void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
561 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
562 SkASSERT(dst);
563 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
564
565 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
566
567 GrD3DTextureResource* dstTextureResource;
568 GrRenderTarget* dstRT = dst->asRenderTarget();
569 if (dstRT) {
570 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
571 } else {
572 SkASSERT(dst->asTexture());
573 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
574 }
575
576 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
577 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
578
579 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
580 src->msaaTextureResource(), &srcRect);
581}
582
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400583void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400584 SkASSERT(target->numSamples() > 1);
585 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
586 SkASSERT(rt->msaaTextureResource());
587
588 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400589}
590
Jim Van Verthba7f2292020-04-21 08:56:47 -0400591bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
592 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
593 size_t rowBytes) {
594 SkASSERT(surface);
595
596 if (surfaceColorType != dstColorType) {
597 return false;
598 }
599
600 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400601 GrD3DTextureResource* texResource = nullptr;
602 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
603 if (rt) {
604 texResource = rt;
605 } else {
606 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
607 }
608
609 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400610 return false;
611 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400612
Jim Van Verthba7f2292020-04-21 08:56:47 -0400613 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400614 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400615 SkASSERT(srcLocation.pResource);
616 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
617 srcLocation.SubresourceIndex = 0;
618
619 D3D12_BOX srcBox = {};
620 srcBox.left = left;
621 srcBox.top = top;
622 srcBox.right = left + width;
623 srcBox.bottom = top + height;
624 srcBox.front = 0;
625 srcBox.back = 1;
626
627 // Set up dst location and create transfer buffer
628 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
629 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400630 UINT64 transferTotalBytes;
631 const UINT64 baseOffset = 0;
632 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
633 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400634 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400635 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400636 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400637 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400638 return false;
639 }
640 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400641
642 // TODO: implement some way of reusing buffers instead of making a new one every time.
643 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
644 GrGpuBufferType::kXferGpuToCpu,
645 kDynamic_GrAccessPattern);
646 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
647 dstLocation.pResource = d3dBuf->d3dResource();
648
649 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400650 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400651
Greg Daniel69267912020-07-24 10:42:53 -0400652 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
653 texResource->resource(), &srcLocation,
654 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400655 this->submitDirectCommandList(SyncQueue::kForce);
656
657 const void* mappedMemory = transferBuffer->map();
658
659 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400660 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400661
662 transferBuffer->unmap();
663
664 return true;
665}
666
667bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
668 GrColorType surfaceColorType, GrColorType srcColorType,
669 const GrMipLevel texels[], int mipLevelCount,
670 bool prepForTexSampling) {
671 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
672 if (!d3dTex) {
673 return false;
674 }
675
676 // Make sure we have at least the base level
677 if (!mipLevelCount || !texels[0].fPixels) {
678 return false;
679 }
680
681 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
682 bool success = false;
683
684 // Need to change the resource state to COPY_DEST in order to upload to it
685 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
686
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400687 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400688 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
689 mipLevelCount);
690
691 if (prepForTexSampling) {
692 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
693 }
694
695 return success;
696}
697
698bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
699 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
700 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
701 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
702 SkASSERT(1 == mipLevelCount ||
703 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
704
705 // We assume that if the texture has mip levels, we either upload to all the levels or just the
706 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400707 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400708
709 if (width == 0 || height == 0) {
710 return false;
711 }
712
713 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
714 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
715
716 ID3D12Resource* d3dResource = tex->d3dResource();
717 SkASSERT(d3dResource);
718 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
719 // Either upload only the first miplevel or all miplevels
720 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
721
722 if (1 == mipLevelCount && !texels[0].fPixels) {
723 return true; // no data to upload
724 }
725
726 for (int i = 0; i < mipLevelCount; ++i) {
727 // We do not allow any gaps in the mip data
728 if (!texels[i].fPixels) {
729 return false;
730 }
731 }
732
733 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400734 UINT64 combinedBufferSize;
735 // We reset the width and height in the description to match our subrectangle size
736 // so we don't end up allocating more space than we need.
737 desc.Width = width;
738 desc.Height = height;
739 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400740 nullptr, nullptr, &combinedBufferSize);
741 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400742 SkASSERT(combinedBufferSize);
743
Greg Danielcffb0622020-07-16 13:19:17 -0400744 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
745 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
746 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400747 return false;
748 }
Greg Danielcffb0622020-07-16 13:19:17 -0400749
750 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400751
752 int currentWidth = width;
753 int currentHeight = height;
754 int layerHeight = tex->height();
755
756 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
757 if (texels[currentMipLevel].fPixels) {
758 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
759
Jim Van Verthfdd36852020-04-21 16:29:44 -0400760 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400761 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
762
763 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
764
765 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400766 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400767 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
768 src, srcRowBytes, trimRowBytes, currentHeight);
769 }
770 currentWidth = std::max(1, currentWidth / 2);
771 currentHeight = std::max(1, currentHeight / 2);
772 layerHeight = currentHeight;
773 }
774
Greg Danielcffb0622020-07-16 13:19:17 -0400775 // Update the offsets in the footprints to be relative to the slice's offset
776 for (int i = 0; i < mipLevelCount; ++i) {
777 placedFootprints[i].Offset += slice.fOffset;
778 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400779
Greg Daniel69267912020-07-24 10:42:53 -0400780 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400781 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
782 placedFootprints.get(), left, top);
783
784 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400785 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400786 }
787
788 return true;
789}
790
Jim Van Verth9145f782020-04-28 12:01:12 -0400791static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400792 if (!info.fResource.get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400793 return false;
794 }
795 return true;
796}
797
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400798static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
799 if (!caps.isFormatTexturable(info.fFormat)) {
800 return false;
801 }
802 return true;
803}
804
805static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
806 int sampleCnt) {
807 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
808 return false;
809 }
810 return true;
811}
812
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400813sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
814 GrWrapOwnership,
815 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400816 GrIOType ioType) {
817 GrD3DTextureResourceInfo textureInfo;
818 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
819 return nullptr;
820 }
821
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400822 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400823 return nullptr;
824 }
825
826 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
827 return nullptr;
828 }
829
830 // TODO: support protected context
831 if (tex.isProtected()) {
832 return nullptr;
833 }
834
835 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
836 SkASSERT(state);
837 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
838 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500839}
840
841sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400842 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500843 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400844 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500845}
846
847sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
848 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500849 GrWrapOwnership ownership,
850 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400851 GrD3DTextureResourceInfo textureInfo;
852 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
853 return nullptr;
854 }
855
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400856 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400857 return nullptr;
858 }
859
860 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
861 return nullptr;
862 }
863 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
864 return nullptr;
865 }
866
867 // TODO: support protected context
868 if (tex.isProtected()) {
869 return nullptr;
870 }
871
872 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
873
874 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
875 SkASSERT(state);
876
877 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
878 sampleCnt, cacheable,
879 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500880}
881
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400882sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400883 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
884 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
885 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
886 // creating and owning the MSAA images.
887 if (rt.sampleCnt() > 1) {
888 return nullptr;
889 }
890
891 GrD3DTextureResourceInfo info;
892 if (!rt.getD3DTextureResourceInfo(&info)) {
893 return nullptr;
894 }
895
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400896 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400897 return nullptr;
898 }
899
900 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
901 return nullptr;
902 }
903
904 // TODO: support protected context
905 if (rt.isProtected()) {
906 return nullptr;
907 }
908
909 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
910
911 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
912 this, rt.dimensions(), 1, info, std::move(state));
913
914 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
915 SkASSERT(!rt.stencilBits());
916 if (tgt) {
917 SkASSERT(tgt->canAttemptStencilAttachment());
918 }
919
920 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500921}
922
923sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400924 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400925
926 GrD3DTextureResourceInfo textureInfo;
927 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
928 return nullptr;
929 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400930 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400931 return nullptr;
932 }
933
934 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
935 return nullptr;
936 }
937
938 // TODO: support protected context
939 if (tex.isProtected()) {
940 return nullptr;
941 }
942
943 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
944 if (!sampleCnt) {
945 return nullptr;
946 }
947
948 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
949 SkASSERT(state);
950
951 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
952 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500953}
954
955sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400956 GrAccessPattern accessPattern, const void* data) {
957 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
958 if (data && buffer) {
959 buffer->updateData(data, sizeInBytes);
960 }
961
962 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500963}
964
965GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
Greg Daniele77162e2020-09-21 15:32:11 -0400966 const GrRenderTarget* rt, SkISize dimensions, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400967 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400968 SkASSERT(dimensions.width() >= rt->width());
969 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400970
971 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
972
973 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
Greg Daniele77162e2020-09-21 15:32:11 -0400974 dimensions,
Jim Van Verth4f51f472020-04-13 11:02:21 -0400975 numStencilSamples,
976 sFmt));
977 fStats.incStencilAttachmentCreates();
978 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500979}
980
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400981bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
982 SkISize dimensions,
983 GrTexturable texturable,
984 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400985 GrMipmapped mipMapped,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400986 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400987 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400988 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400989
990 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
991 return false;
992 }
993
994 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
995 return false;
996 }
997
998 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
999 return false;
1000 }
1001
1002 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001003 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001004 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001005 }
1006
1007 // create the texture
1008 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
1009 if (renderable == GrRenderable::kYes) {
1010 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1011 }
1012
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001013 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001014 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -04001015 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001016 resourceDesc.Width = dimensions.fWidth;
1017 resourceDesc.Height = dimensions.fHeight;
1018 resourceDesc.DepthOrArraySize = 1;
1019 resourceDesc.MipLevels = numMipLevels;
1020 resourceDesc.Format = dxgiFormat;
1021 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -04001022 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -04001023 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001024 resourceDesc.Flags = usageFlags;
1025
Jim Van Verth280d4f72020-05-04 10:04:24 -04001026 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
1027 D3D12_CLEAR_VALUE clearValue = {};
1028 if (renderable == GrRenderable::kYes) {
1029 clearValue.Format = dxgiFormat;
1030 // Assume transparent black
1031 clearValue.Color[0] = 0;
1032 clearValue.Color[1] = 0;
1033 clearValue.Color[2] = 0;
1034 clearValue.Color[3] = 0;
1035 clearValuePtr = &clearValue;
1036 }
1037
Greg Daniel16032b32020-05-06 15:31:10 -04001038 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1039 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1040 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001041 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001042 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001043 SkDebugf("Failed to init texture resource info\n");
1044 return false;
1045 }
1046
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001047 return true;
1048}
1049
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001050GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001051 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001052 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001053 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001054 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001055 this->handleDirtyContext();
1056
1057 const GrD3DCaps& caps = this->d3dCaps();
1058
1059 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1060 return {};
1061 }
1062
1063 DXGI_FORMAT dxgiFormat;
1064 if (!format.asDxgiFormat(&dxgiFormat)) {
1065 return {};
1066 }
1067
1068 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1069 if (!caps.isFormatTexturable(dxgiFormat)) {
1070 return {};
1071 }
1072
1073 GrD3DTextureResourceInfo info;
1074 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1075 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001076 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001077 return {};
1078 }
1079
1080 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001081}
1082
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001083static void copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
1084 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1085 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001086 SkASSERT(srcData && numMipLevels);
1087 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1088 SkASSERT(mapPtr);
1089
1090 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
1091
1092 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1093 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1094
1095 // copy data into the buffer, skipping any trailing bytes
1096 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1097 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1098 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1099 trimRowBytes, srcData[currentMipLevel].height());
1100 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001101}
1102
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001103static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1104 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1105 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1106 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001107 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001108 if (colorType == GrColorType::kUnknown) {
1109 return false;
1110 }
1111 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1112 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1113 return false;
1114 }
1115
1116 return true;
1117}
1118
Greg Daniel16032b32020-05-06 15:31:10 -04001119bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1120 sk_sp<GrRefCntedCallback> finishedCallback,
1121 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001122 GrD3DTextureResourceInfo info;
1123 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1124
1125 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1126 SkASSERT(state);
1127 sk_sp<GrD3DTexture> texture =
1128 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1129 GrWrapCacheable::kNo,
1130 kRW_GrIOType, info, std::move(state));
1131 if (!texture) {
1132 return false;
1133 }
1134
1135 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1136 if (!cmdList) {
1137 return false;
1138 }
1139
1140 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1141
1142 ID3D12Resource* d3dResource = texture->d3dResource();
1143 SkASSERT(d3dResource);
1144 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1145 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001146 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001147 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001148 backendTexture.dimensions().height()) + 1;
1149 }
1150 SkASSERT(mipLevelCount == info.fLevelCount);
1151 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1152 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001153 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1154 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001155 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001156 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001157 SkASSERT(combinedBufferSize);
1158 if (data->type() == BackendTextureData::Type::kColor &&
1159 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1160 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1161 combinedBufferSize =
1162 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1163 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1164 placedFootprints[i].Offset = 0;
1165 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1166 }
1167 }
1168
Greg Danielcffb0622020-07-16 13:19:17 -04001169 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1170 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1171 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001172 return false;
1173 }
Greg Danielcffb0622020-07-16 13:19:17 -04001174
1175 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001176 SkASSERT(bufferData);
1177
Jim Van Verth43a6e172020-06-23 11:59:08 -04001178 if (data->type() == BackendTextureData::Type::kPixmaps) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001179 copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
1180 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001181 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001182 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1183 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001184 } else {
1185 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1186 SkImage::CompressionType compression =
1187 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1188 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001189 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1190 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001191 return false;
1192 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001193 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001194 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1195 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001196 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001197 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001198 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001199 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001200 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1201 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001202 }
1203 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001204
Greg Danielcffb0622020-07-16 13:19:17 -04001205 // Update the offsets in the footprints to be relative to the slice's offset
1206 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1207 placedFootprints[i].Offset += slice.fOffset;
1208 }
1209
Greg Daniel69267912020-07-24 10:42:53 -04001210 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001211 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1212 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001213
1214 if (finishedCallback) {
1215 this->addFinishedCallback(std::move(finishedCallback));
1216 }
1217
Greg Daniel16032b32020-05-06 15:31:10 -04001218 return true;
1219}
1220
Greg Danielc1ad77c2020-05-06 11:40:03 -04001221GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001222 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001223 GrProtected isProtected) {
1224 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1225 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001226}
1227
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001228bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001229 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001230 const BackendTextureData* data) {
1231 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001232}
1233
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001234void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001235 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1236 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001237}
1238
Robert Phillips979b2232020-02-20 10:47:29 -05001239bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1240 return false;
1241}
1242
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001243#if GR_TEST_UTILS
1244bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001245 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1246
1247 GrD3DTextureResourceInfo info;
1248 if (!tex.getD3DTextureResourceInfo(&info)) {
1249 return false;
1250 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001251 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001252 if (!textureResource) {
1253 return false;
1254 }
1255 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001256}
1257
1258GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001259 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001260 this->handleDirtyContext();
1261
1262 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001263 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001264 }
1265
1266 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1267
1268 GrD3DTextureResourceInfo info;
1269 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001270 GrRenderable::kYes, GrMipmapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001271 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001272 return {};
1273 }
1274
1275 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001276}
1277
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001278void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1279 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1280
1281 GrD3DTextureResourceInfo info;
1282 if (rt.getD3DTextureResourceInfo(&info)) {
1283 this->testingOnly_flushGpuAndSync();
1284 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1285 // is deleted.
1286 }
1287}
1288
1289void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001290 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001291}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001292
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001293void GrD3DGpu::testingOnly_startCapture() {
1294 if (fGraphicsAnalysis) {
1295 fGraphicsAnalysis->BeginCapture();
1296 }
1297}
1298
1299void GrD3DGpu::testingOnly_endCapture() {
1300 if (fGraphicsAnalysis) {
1301 fGraphicsAnalysis->EndCapture();
1302 }
1303}
1304#endif
1305
Jim Van Verthc632aa62020-04-17 16:58:20 -04001306///////////////////////////////////////////////////////////////////////////////
1307
Greg Daniela5a6b322020-04-23 12:52:27 -04001308void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001309 int numBarriers,
1310 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1311 SkASSERT(fCurrentDirectCommandList);
1312 SkASSERT(resource);
1313
Greg Daniela5a6b322020-04-23 12:52:27 -04001314 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001315}
1316
Greg Daniel69267912020-07-24 10:42:53 -04001317void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1318 int numBarriers,
1319 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1320 SkASSERT(fCurrentDirectCommandList);
1321 SkASSERT(buffer);
1322
1323 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1324 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1325}
1326
1327
Greg Daniel9efe3862020-06-11 11:51:06 -04001328void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1329 GrSurfaceProxy* proxies[],
1330 int numProxies,
1331 SkSurface::BackendSurfaceAccess access,
1332 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001333 SkASSERT(numProxies >= 0);
1334 SkASSERT(!numProxies || proxies);
1335
1336 // prepare proxies by transitioning to PRESENT renderState
1337 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1338 GrD3DTextureResource* resource;
1339 for (int i = 0; i < numProxies; ++i) {
1340 SkASSERT(proxies[i]->isInstantiated());
1341 if (GrTexture* tex = proxies[i]->peekTexture()) {
1342 resource = static_cast<GrD3DTexture*>(tex);
1343 } else {
1344 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1345 SkASSERT(rt);
1346 resource = static_cast<GrD3DRenderTarget*>(rt);
1347 }
1348 resource->prepareForPresent(this);
1349 }
1350 }
1351}
1352
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001353void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001354 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001355}
1356
Jim Van Verthc632aa62020-04-17 16:58:20 -04001357bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1358 if (syncCpu) {
1359 return this->submitDirectCommandList(SyncQueue::kForce);
1360 } else {
1361 return this->submitDirectCommandList(SyncQueue::kSkip);
1362 }
1363}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001364
1365std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1366 return GrD3DSemaphore::Make(this);
1367}
1368std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1369 const GrBackendSemaphore& semaphore,
1370 GrResourceProvider::SemaphoreWrapType,
1371 GrWrapOwnership) {
1372 SkASSERT(this->caps()->semaphoreSupport());
1373 GrD3DFenceInfo fenceInfo;
1374 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1375 return nullptr;
1376 }
1377 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1378}
1379
1380void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001381 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001382 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1383 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1384 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1385}
1386
1387void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001388 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001389 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1390 // TODO: Do we need to track the lifetime of this?
1391 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1392}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001393
1394GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001395 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001396 return fCurrentFenceValue;
1397}
1398
1399bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001400 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001401}