blob: 87b6974422b2ccb65c4917afbdd926f4b12ac6bb [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);
Brian Salomon72c7b982020-10-06 10:07:38 -0400586 SkASSERT(rt->msaaTextureResource() && rt != rt->msaaTextureResource());
Jim Van Verth275f4192020-07-07 15:36:41 -0400587
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);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400637 if (GrDxgiFormatBytesPerBlock(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 }
Brian Salomon718ae762020-09-29 16:52:49 -0400802 // We don't support sampling from multisampled textures.
803 if (info.fSampleCount != 1) {
804 return false;
805 }
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400806 return true;
807}
808
809static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
810 int sampleCnt) {
811 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
812 return false;
813 }
814 return true;
815}
816
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400817sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
818 GrWrapOwnership,
819 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400820 GrIOType ioType) {
821 GrD3DTextureResourceInfo textureInfo;
822 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
823 return nullptr;
824 }
825
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400826 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400827 return nullptr;
828 }
829
830 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
831 return nullptr;
832 }
833
834 // TODO: support protected context
835 if (tex.isProtected()) {
836 return nullptr;
837 }
838
839 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
840 SkASSERT(state);
841 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
842 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500843}
844
845sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400846 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500847 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400848 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500849}
850
851sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
852 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500853 GrWrapOwnership ownership,
854 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400855 GrD3DTextureResourceInfo textureInfo;
856 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
857 return nullptr;
858 }
859
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400860 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400861 return nullptr;
862 }
863
864 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
865 return nullptr;
866 }
867 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
868 return nullptr;
869 }
870
871 // TODO: support protected context
872 if (tex.isProtected()) {
873 return nullptr;
874 }
875
876 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
877
878 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
879 SkASSERT(state);
880
881 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
882 sampleCnt, cacheable,
883 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500884}
885
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400886sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400887 GrD3DTextureResourceInfo info;
888 if (!rt.getD3DTextureResourceInfo(&info)) {
889 return nullptr;
890 }
891
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400892 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400893 return nullptr;
894 }
895
896 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
897 return nullptr;
898 }
899
900 // TODO: support protected context
901 if (rt.isProtected()) {
902 return nullptr;
903 }
904
905 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
906
907 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
Brian Salomon72c7b982020-10-06 10:07:38 -0400908 this, rt.dimensions(), rt.sampleCnt(), info, std::move(state));
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400909
910 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
911 SkASSERT(!rt.stencilBits());
912 if (tgt) {
913 SkASSERT(tgt->canAttemptStencilAttachment());
914 }
915
916 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500917}
918
919sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400920 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400921
922 GrD3DTextureResourceInfo textureInfo;
923 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
924 return nullptr;
925 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400926 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400927 return nullptr;
928 }
929
Brian Salomon718ae762020-09-29 16:52:49 -0400930 // If sampleCnt is > 1 we will create an intermediate MSAA VkImage and then resolve into
931 // the wrapped VkImage. We don't yet support rendering directly to client-provided MSAA texture.
932 if (textureInfo.fSampleCount != 1) {
933 return nullptr;
934 }
935
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400936 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
937 return nullptr;
938 }
939
940 // TODO: support protected context
941 if (tex.isProtected()) {
942 return nullptr;
943 }
944
945 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
946 if (!sampleCnt) {
947 return nullptr;
948 }
949
950 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
951 SkASSERT(state);
952
953 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
954 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500955}
956
957sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400958 GrAccessPattern accessPattern, const void* data) {
959 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
960 if (data && buffer) {
961 buffer->updateData(data, sizeInBytes);
962 }
963
964 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500965}
966
967GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
Greg Daniele77162e2020-09-21 15:32:11 -0400968 const GrRenderTarget* rt, SkISize dimensions, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400969 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400970 SkASSERT(dimensions.width() >= rt->width());
971 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400972
Greg Daniel8ade5e82020-10-07 13:09:48 -0400973 DXGI_FORMAT sFmt = this->d3dCaps().preferredStencilFormat();
Jim Van Verth4f51f472020-04-13 11:02:21 -0400974
975 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
Greg Daniele77162e2020-09-21 15:32:11 -0400976 dimensions,
Jim Van Verth4f51f472020-04-13 11:02:21 -0400977 numStencilSamples,
978 sFmt));
979 fStats.incStencilAttachmentCreates();
980 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500981}
982
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400983bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
984 SkISize dimensions,
985 GrTexturable texturable,
986 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400987 GrMipmapped mipMapped,
Brian Salomon72c7b982020-10-06 10:07:38 -0400988 int sampleCnt,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400989 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400990 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400991 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400992
993 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
994 return false;
995 }
996
997 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
998 return false;
999 }
1000
1001 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
1002 return false;
1003 }
1004
1005 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001006 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001007 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001008 }
1009
1010 // create the texture
1011 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
1012 if (renderable == GrRenderable::kYes) {
1013 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1014 }
1015
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001016 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001017 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -04001018 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001019 resourceDesc.Width = dimensions.fWidth;
1020 resourceDesc.Height = dimensions.fHeight;
1021 resourceDesc.DepthOrArraySize = 1;
1022 resourceDesc.MipLevels = numMipLevels;
1023 resourceDesc.Format = dxgiFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -04001024 resourceDesc.SampleDesc.Count = sampleCnt;
Jim Van Verth765c5922020-08-10 17:23:50 -04001025 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -04001026 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001027 resourceDesc.Flags = usageFlags;
1028
Jim Van Verth280d4f72020-05-04 10:04:24 -04001029 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
1030 D3D12_CLEAR_VALUE clearValue = {};
1031 if (renderable == GrRenderable::kYes) {
1032 clearValue.Format = dxgiFormat;
1033 // Assume transparent black
1034 clearValue.Color[0] = 0;
1035 clearValue.Color[1] = 0;
1036 clearValue.Color[2] = 0;
1037 clearValue.Color[3] = 0;
1038 clearValuePtr = &clearValue;
1039 }
1040
Greg Daniel16032b32020-05-06 15:31:10 -04001041 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1042 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1043 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001044 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001045 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001046 SkDebugf("Failed to init texture resource info\n");
1047 return false;
1048 }
1049
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001050 return true;
1051}
1052
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001053GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001054 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001055 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001056 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001057 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001058 this->handleDirtyContext();
1059
1060 const GrD3DCaps& caps = this->d3dCaps();
1061
1062 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1063 return {};
1064 }
1065
1066 DXGI_FORMAT dxgiFormat;
1067 if (!format.asDxgiFormat(&dxgiFormat)) {
1068 return {};
1069 }
1070
1071 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1072 if (!caps.isFormatTexturable(dxgiFormat)) {
1073 return {};
1074 }
1075
1076 GrD3DTextureResourceInfo info;
1077 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
Brian Salomon72c7b982020-10-06 10:07:38 -04001078 renderable, mipMapped, 1, &info,
1079 isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001080 return {};
1081 }
1082
1083 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001084}
1085
Greg Daniel0eca74c2020-10-01 13:46:00 -04001086static void copy_src_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001087 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1088 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001089 SkASSERT(srcData && numMipLevels);
1090 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1091 SkASSERT(mapPtr);
1092
Greg Daniel0eca74c2020-10-01 13:46:00 -04001093 size_t bytesPerPixel = GrDxgiFormatBytesPerBlock(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001094
1095 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1096 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1097
1098 // copy data into the buffer, skipping any trailing bytes
1099 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1100 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1101 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1102 trimRowBytes, srcData[currentMipLevel].height());
1103 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001104}
1105
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001106static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1107 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1108 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1109 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001110 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001111 if (colorType == GrColorType::kUnknown) {
1112 return false;
1113 }
1114 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1115 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1116 return false;
1117 }
1118
1119 return true;
1120}
1121
Greg Daniel16032b32020-05-06 15:31:10 -04001122bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1123 sk_sp<GrRefCntedCallback> finishedCallback,
1124 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001125 GrD3DTextureResourceInfo info;
1126 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1127
1128 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1129 SkASSERT(state);
1130 sk_sp<GrD3DTexture> texture =
1131 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1132 GrWrapCacheable::kNo,
1133 kRW_GrIOType, info, std::move(state));
1134 if (!texture) {
1135 return false;
1136 }
1137
1138 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1139 if (!cmdList) {
1140 return false;
1141 }
1142
1143 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1144
1145 ID3D12Resource* d3dResource = texture->d3dResource();
1146 SkASSERT(d3dResource);
1147 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1148 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001149 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001150 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001151 backendTexture.dimensions().height()) + 1;
1152 }
1153 SkASSERT(mipLevelCount == info.fLevelCount);
1154 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1155 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001156 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1157 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001158 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001159 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001160 SkASSERT(combinedBufferSize);
1161 if (data->type() == BackendTextureData::Type::kColor &&
1162 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1163 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1164 combinedBufferSize =
1165 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1166 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1167 placedFootprints[i].Offset = 0;
1168 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1169 }
1170 }
1171
Greg Danielcffb0622020-07-16 13:19:17 -04001172 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1173 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1174 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001175 return false;
1176 }
Greg Danielcffb0622020-07-16 13:19:17 -04001177
1178 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001179 SkASSERT(bufferData);
1180
Jim Van Verth43a6e172020-06-23 11:59:08 -04001181 if (data->type() == BackendTextureData::Type::kPixmaps) {
Greg Daniel0eca74c2020-10-01 13:46:00 -04001182 copy_src_data(bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001183 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001184 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001185 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1186 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001187 } else {
1188 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1189 SkImage::CompressionType compression =
1190 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1191 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001192 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1193 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001194 return false;
1195 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001196 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001197 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1198 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001199 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001200 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001201 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001202 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001203 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1204 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001205 }
1206 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001207
Greg Danielcffb0622020-07-16 13:19:17 -04001208 // Update the offsets in the footprints to be relative to the slice's offset
1209 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1210 placedFootprints[i].Offset += slice.fOffset;
1211 }
1212
Greg Daniel69267912020-07-24 10:42:53 -04001213 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001214 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1215 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001216
1217 if (finishedCallback) {
1218 this->addFinishedCallback(std::move(finishedCallback));
1219 }
1220
Greg Daniel16032b32020-05-06 15:31:10 -04001221 return true;
1222}
1223
Greg Danielc1ad77c2020-05-06 11:40:03 -04001224GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001225 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001226 GrProtected isProtected) {
1227 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1228 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001229}
1230
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001231bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001232 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001233 const BackendTextureData* data) {
1234 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001235}
1236
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001237void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001238 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1239 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001240}
1241
Robert Phillips979b2232020-02-20 10:47:29 -05001242bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1243 return false;
1244}
1245
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001246#if GR_TEST_UTILS
1247bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001248 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1249
1250 GrD3DTextureResourceInfo info;
1251 if (!tex.getD3DTextureResourceInfo(&info)) {
1252 return false;
1253 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001254 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001255 if (!textureResource) {
1256 return false;
1257 }
1258 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001259}
1260
Brian Salomon72c7b982020-10-06 10:07:38 -04001261GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
1262 GrColorType colorType,
1263 int sampleCnt) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001264 this->handleDirtyContext();
1265
Brian Salomon72c7b982020-10-06 10:07:38 -04001266 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
1267 dimensions.height() > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001268 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001269 }
1270
1271 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1272
1273 GrD3DTextureResourceInfo info;
Brian Salomon72c7b982020-10-06 10:07:38 -04001274 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001275 GrRenderable::kYes, GrMipmapped::kNo,
Brian Salomon72c7b982020-10-06 10:07:38 -04001276 sampleCnt, &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001277 return {};
1278 }
1279
Brian Salomon72c7b982020-10-06 10:07:38 -04001280 return GrBackendRenderTarget(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001281}
1282
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001283void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1284 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1285
1286 GrD3DTextureResourceInfo info;
1287 if (rt.getD3DTextureResourceInfo(&info)) {
1288 this->testingOnly_flushGpuAndSync();
1289 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1290 // is deleted.
1291 }
1292}
1293
1294void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001295 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001296}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001297
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001298void GrD3DGpu::testingOnly_startCapture() {
1299 if (fGraphicsAnalysis) {
1300 fGraphicsAnalysis->BeginCapture();
1301 }
1302}
1303
1304void GrD3DGpu::testingOnly_endCapture() {
1305 if (fGraphicsAnalysis) {
1306 fGraphicsAnalysis->EndCapture();
1307 }
1308}
1309#endif
1310
Jim Van Verthc632aa62020-04-17 16:58:20 -04001311///////////////////////////////////////////////////////////////////////////////
1312
Greg Daniela5a6b322020-04-23 12:52:27 -04001313void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001314 int numBarriers,
1315 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1316 SkASSERT(fCurrentDirectCommandList);
1317 SkASSERT(resource);
1318
Greg Daniela5a6b322020-04-23 12:52:27 -04001319 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001320}
1321
Greg Daniel69267912020-07-24 10:42:53 -04001322void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1323 int numBarriers,
1324 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1325 SkASSERT(fCurrentDirectCommandList);
1326 SkASSERT(buffer);
1327
1328 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1329 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1330}
1331
1332
Greg Daniel9efe3862020-06-11 11:51:06 -04001333void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1334 GrSurfaceProxy* proxies[],
1335 int numProxies,
1336 SkSurface::BackendSurfaceAccess access,
1337 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001338 SkASSERT(numProxies >= 0);
1339 SkASSERT(!numProxies || proxies);
1340
1341 // prepare proxies by transitioning to PRESENT renderState
1342 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1343 GrD3DTextureResource* resource;
1344 for (int i = 0; i < numProxies; ++i) {
1345 SkASSERT(proxies[i]->isInstantiated());
1346 if (GrTexture* tex = proxies[i]->peekTexture()) {
1347 resource = static_cast<GrD3DTexture*>(tex);
1348 } else {
1349 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1350 SkASSERT(rt);
1351 resource = static_cast<GrD3DRenderTarget*>(rt);
1352 }
1353 resource->prepareForPresent(this);
1354 }
1355 }
1356}
1357
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001358void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001359 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001360}
1361
Jim Van Verthc632aa62020-04-17 16:58:20 -04001362bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1363 if (syncCpu) {
1364 return this->submitDirectCommandList(SyncQueue::kForce);
1365 } else {
1366 return this->submitDirectCommandList(SyncQueue::kSkip);
1367 }
1368}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001369
1370std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1371 return GrD3DSemaphore::Make(this);
1372}
1373std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1374 const GrBackendSemaphore& semaphore,
1375 GrResourceProvider::SemaphoreWrapType,
1376 GrWrapOwnership) {
1377 SkASSERT(this->caps()->semaphoreSupport());
1378 GrD3DFenceInfo fenceInfo;
1379 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1380 return nullptr;
1381 }
1382 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1383}
1384
1385void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001386 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001387 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1388 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1389 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1390}
1391
1392void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001393 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001394 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1395 // TODO: Do we need to track the lifetime of this?
1396 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1397}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001398
1399GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001400 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001401 return fCurrentFenceValue;
1402}
1403
1404bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001405 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001406}