blob: 7d76ccf3486bbc9e2a409be0ec50f7eedd47b87f [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"
Greg Danielc0d69152020-10-08 14:59:00 -040018#include "src/gpu/d3d/GrD3DAttachment.h"
Jim Van Verthd6ad4802020-04-03 14:59:20 -040019#include "src/gpu/d3d/GrD3DBuffer.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050020#include "src/gpu/d3d/GrD3DCaps.h"
21#include "src/gpu/d3d/GrD3DOpsRenderPass.h"
Jim Van Verthc1a67b52020-06-25 13:10:29 -040022#include "src/gpu/d3d/GrD3DSemaphore.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)
Brian Osmand7e76592020-11-02 12:26:22 -050067 , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt) {
Brian Osmancbdc2612020-11-23 15:30:48 -050068 this->initCapsAndCompiler(sk_make_sp<GrD3DCaps>(contextOptions,
69 backendContext.fAdapter.get(),
70 backendContext.fDevice.get()));
Greg Daniel85da3362020-03-09 15:18:35 -040071
72 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
Greg Daniele52c9782020-03-23 14:18:37 -040073 SkASSERT(fCurrentDirectCommandList);
Greg Daniel83ed2132020-03-24 13:15:33 -040074
75 SkASSERT(fCurrentFenceValue == 0);
Jim Van Verthe3810362020-07-01 10:12:11 -040076 GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE,
77 IID_PPV_ARGS(&fFence)));
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040078
79#if GR_TEST_UTILS
80 HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis));
81 if (FAILED(getAnalysis)) {
82 fGraphicsAnalysis = nullptr;
83 }
84#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050085}
86
Greg Daniel83ed2132020-03-24 13:15:33 -040087GrD3DGpu::~GrD3DGpu() {
88 this->destroyResources();
89}
90
91void GrD3DGpu::destroyResources() {
92 if (fCurrentDirectCommandList) {
93 fCurrentDirectCommandList->close();
Jim Van Verthba7f2292020-04-21 08:56:47 -040094 fCurrentDirectCommandList->reset();
Greg Daniel83ed2132020-03-24 13:15:33 -040095 }
96
97 // We need to make sure everything has finished on the queue.
Jim Van Verth3b0d7d12020-07-06 11:52:42 -040098 this->waitForQueueCompletion();
Greg Daniel83ed2132020-03-24 13:15:33 -040099
100 SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();)
101
102 // We used a placement new for each object in fOutstandingCommandLists, so we're responsible
103 // for calling the destructor on each of them as well.
104 while (!fOutstandingCommandLists.empty()) {
Jim Van Verthf43da142020-06-09 16:34:43 -0400105 OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400106 SkASSERT(list->fFenceValue <= fenceValue);
107 // No reason to recycle the command lists since we are destroying all resources anyways.
108 list->~OutstandingCommandList();
Jim Van Verthf43da142020-06-09 16:34:43 -0400109 fOutstandingCommandLists.pop_front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400110 }
Jim Van Verthf2788862020-06-03 17:33:12 -0400111
Greg Danielcffb0622020-07-16 13:19:17 -0400112 fStagingBufferManager.reset();
113
Jim Van Verthf2788862020-06-03 17:33:12 -0400114 fResourceProvider.destroyResources();
Greg Daniel83ed2132020-03-24 13:15:33 -0400115}
Greg Daniel31a7b072020-02-26 15:31:49 -0500116
Greg Daniel65476e02020-10-27 09:20:20 -0400117GrOpsRenderPass* GrD3DGpu::onGetOpsRenderPass(
Greg Danielc0d69152020-10-08 14:59:00 -0400118 GrRenderTarget* rt,
119 GrAttachment*,
120 GrSurfaceOrigin origin,
121 const SkIRect& bounds,
Greg Daniel9a18b082020-08-14 14:03:50 -0400122 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
123 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
124 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400125 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500126 if (!fCachedOpsRenderPass) {
127 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
128 }
129
130 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
131 return nullptr;
132 }
133 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500134}
135
Jim Van Verthc632aa62020-04-17 16:58:20 -0400136bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400137 SkASSERT(fCurrentDirectCommandList);
138
Jim Van Verth3eadce22020-06-01 11:34:49 -0400139 fResourceProvider.prepForSubmit();
140
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400141 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
Jim Van Verthc632aa62020-04-17 16:58:20 -0400142 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
143 return false;
144 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
145 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400146 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400147 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400148 }
149 return true;
150 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400151
Greg Daniela581a8b2020-06-19 15:22:18 -0400152 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
153 // uniform data as dirty.
154 fResourceProvider.markPipelineStateUniformsDirty();
155
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400156 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400157 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400158 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400159
Jim Van Verthc632aa62020-04-17 16:58:20 -0400160 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400161 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400162 }
163
Greg Daniel83ed2132020-03-24 13:15:33 -0400164 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
165
166 // This should be done after we have a new command list in case the freeing of any resources
167 // held by a finished command list causes us send a new command to the gpu (like changing the
168 // resource state.
169 this->checkForFinishedCommandLists();
170
171 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400172 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400173}
174
175void GrD3DGpu::checkForFinishedCommandLists() {
176 uint64_t currentFenceValue = fFence->GetCompletedValue();
177
178 // Iterate over all the outstanding command lists to see if any have finished. The commands
179 // lists are in order from oldest to newest, so we start at the front to check if their fence
180 // value is less than the last signaled value. If so we pop it off and move onto the next.
181 // Repeat till we find a command list that has not finished yet (and all others afterwards are
182 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400183 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
184 while (front && front->fFenceValue <= currentFenceValue) {
185 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400186 // Since we used placement new we are responsible for calling the destructor manually.
187 front->~OutstandingCommandList();
188 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400189 fResourceProvider.recycleDirectCommandList(std::move(currList));
190 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400191 }
192}
193
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400194void GrD3DGpu::waitForQueueCompletion() {
195 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
196 HANDLE fenceEvent;
197 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
198 SkASSERT(fenceEvent);
199 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
200 WaitForSingleObject(fenceEvent, INFINITE);
201 CloseHandle(fenceEvent);
202 }
203}
204
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500205void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400206 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
207
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500208 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400209 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500210}
211
Greg Danield4928d02020-06-19 11:13:26 -0400212void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
213 GrGpuFinishedContext finishedContext) {
214 SkASSERT(finishedProc);
Brian Salomon694ff172020-11-04 16:54:28 -0500215 this->addFinishedCallback(GrRefCntedCallback::Make(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400216}
217
218void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
219 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400220 // Besides the current command list, we also add the finishedCallback to the newest outstanding
221 // command list. Our contract for calling the proc is that all previous submitted command lists
222 // have finished when we call it. However, if our current command list has no work when it is
223 // flushed it will drop its ref to the callback immediately. But the previous work may not have
224 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
225 // must finish after all previously submitted command lists.
226 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
227 if (back) {
228 back->fCommandList->addFinishedCallback(finishedCallback);
229 }
230 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
231}
232
Jim Van Verth765c5922020-08-10 17:23:50 -0400233void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
234 SkTArray<SkPoint>* sampleLocations) {
235 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
236 // These are transformed from D3D's integer coordinate system with origin at the center,
237 // to our normalized coordinate system with origin at the upper left.
238 // This ends up corresponding with Vulkan's sample locations.
239 SkASSERT(this->caps()->sampleLocationsSupport());
240 static constexpr SkPoint kStandardSampleLocations_1[1] = {
241 {0.5f, 0.5f} };
242 static constexpr SkPoint kStandardSampleLocations_2[2] = {
243 {0.75f, 0.75f}, {0.25f, 0.25f} };
244 static constexpr SkPoint kStandardSampleLocations_4[4] = {
245 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
246 static constexpr SkPoint kStandardSampleLocations_8[8] = {
247 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
248 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
249 static constexpr SkPoint kStandardSampleLocations_16[16] = {
250 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
251 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
252 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
253 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
254
255 int numSamples = renderTarget->numSamples();
256 // TODO: support mixed samples?
257 SkASSERT(numSamples > 1);
258 SkASSERT(!renderTarget->getStencilAttachment() ||
259 numSamples == renderTarget->getStencilAttachment()->numSamples());
260
261 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
262 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
263 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
264 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
265 return;
266 }
267 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
268
269 switch (numSamples) {
270 case 2:
271 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
272 break;
273 case 4:
274 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
275 break;
276 case 8:
277 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
278 break;
279 case 16:
280 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
281 break;
282 default:
283 SK_ABORT("Invalid sample count.");
284 break;
285 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500286}
287
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400288sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
289 DXGI_FORMAT dxgiFormat,
290 GrRenderable renderable,
291 int renderTargetSampleCnt,
292 SkBudgeted budgeted,
293 GrProtected isProtected,
294 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400295 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400296 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400297 if (renderable == GrRenderable::kYes) {
298 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
299 }
300
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400301 // 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 -0400302 // requested, this describes the resolved texture. Therefore we always have samples set
303 // to 1.
304 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400305 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400306 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
307 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
308 // might want to manually set alignment to 4KB for smaller textures
309 resourceDesc.Alignment = 0;
310 resourceDesc.Width = dimensions.fWidth;
311 resourceDesc.Height = dimensions.fHeight;
312 resourceDesc.DepthOrArraySize = 1;
313 resourceDesc.MipLevels = mipLevelCount;
314 resourceDesc.Format = dxgiFormat;
315 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400316 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400317 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400318 resourceDesc.Flags = usageFlags;
319
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400320 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400321 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400322 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400323 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400324 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400325 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400326 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400327 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400328}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400329
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400330sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
331 const GrBackendFormat& format,
332 GrRenderable renderable,
333 int renderTargetSampleCnt,
334 SkBudgeted budgeted,
335 GrProtected isProtected,
336 int mipLevelCount,
337 uint32_t levelClearMask) {
338 DXGI_FORMAT dxgiFormat;
339 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
340 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
341
Brian Salomona6db5102020-07-21 09:56:23 -0400342 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
343 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400344
345 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
346 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400347 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400348 if (!tex) {
349 return nullptr;
350 }
351
352 if (levelClearMask) {
353 // TODO
354 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400355
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400356 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500357}
358
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400359static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
360 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
361 UINT* numRows, UINT64* rowSizeInBytes,
362 const void* compressedData, int numMipLevels) {
363 SkASSERT(compressedData && numMipLevels);
364 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
365 SkASSERT(mapPtr);
366
367 const char* src = static_cast<const char*>(compressedData);
368 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
369 // copy data into the buffer, skipping any trailing bytes
370 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
371 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
372 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
373 numRows[currentMipLevel]);
374 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
375 }
376}
377
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500378sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
379 const GrBackendFormat& format,
380 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400381 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500382 GrProtected isProtected,
383 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400384 DXGI_FORMAT dxgiFormat;
385 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
386 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
387
388 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
389 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400390 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400391
392 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400393 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400394 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400395 }
Brian Salomona6db5102020-07-21 09:56:23 -0400396 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
397 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400398
399 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
400 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400401 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400402 if (!d3dTex) {
403 return nullptr;
404 }
405
406 ID3D12Resource* d3dResource = d3dTex->d3dResource();
407 SkASSERT(d3dResource);
408 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
409 // Either upload only the first miplevel or all miplevels
410 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
411
412 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
413 SkAutoTMalloc<UINT> numRows(mipLevelCount);
414 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
415 UINT64 combinedBufferSize;
416 // We reset the width and height in the description to match our subrectangle size
417 // so we don't end up allocating more space than we need.
418 desc.Width = dimensions.width();
419 desc.Height = dimensions.height();
420 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
421 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
422 SkASSERT(combinedBufferSize);
423
Greg Danielcffb0622020-07-16 13:19:17 -0400424 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
425 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
426 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400427 return false;
428 }
Greg Danielcffb0622020-07-16 13:19:17 -0400429
430 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400431
432 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
433 rowSizeInBytes.get(), data, mipLevelCount);
434
Greg Danielcffb0622020-07-16 13:19:17 -0400435 // Update the offsets in the footprints to be relative to the slice's offset
436 for (int i = 0; i < mipLevelCount; ++i) {
437 placedFootprints[i].Offset += slice.fOffset;
438 }
439
Greg Daniel69267912020-07-24 10:42:53 -0400440 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400441 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
442 placedFootprints.get(), 0, 0);
443
444 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500445}
446
Jim Van Verth9145f782020-04-28 12:01:12 -0400447static int get_surface_sample_cnt(GrSurface* surf) {
448 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
449 return rt->numSamples();
450 }
451 return 0;
452}
453
454bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
455 const SkIPoint& dstPoint) {
456
457 if (src->isProtected() && !dst->isProtected()) {
458 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400459 return false;
460 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400461
462 int dstSampleCnt = get_surface_sample_cnt(dst);
463 int srcSampleCnt = get_surface_sample_cnt(src);
464
465 GrD3DTextureResource* dstTexResource;
466 GrD3DTextureResource* srcTexResource;
467 GrRenderTarget* dstRT = dst->asRenderTarget();
468 if (dstRT) {
469 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
470 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
471 } else {
472 SkASSERT(dst->asTexture());
473 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
474 }
475 GrRenderTarget* srcRT = src->asRenderTarget();
476 if (srcRT) {
477 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
478 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
479 } else {
480 SkASSERT(src->asTexture());
481 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
482 }
483
484 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
485 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
486
487 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
488 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
489 return true;
490 }
491
492 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
493 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
494 return true;
495 }
496
497 return false;
498}
499
500void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
501 GrD3DTextureResource* dstResource,
502 GrD3DTextureResource* srcResource,
503 const SkIRect& srcRect, const SkIPoint& dstPoint) {
504#ifdef SK_DEBUG
505 int dstSampleCnt = get_surface_sample_cnt(dst);
506 int srcSampleCnt = get_surface_sample_cnt(src);
507 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
508 DXGI_FORMAT srcFormat;
509 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
510 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
511#endif
512 if (src->isProtected() && !dst->isProtected()) {
513 SkDebugf("Can't copy from protected memory to non-protected");
514 return;
515 }
516
517 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
518 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
519
520 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
521 dstLocation.pResource = dstResource->d3dResource();
522 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
523 dstLocation.SubresourceIndex = 0;
524
525 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
526 srcLocation.pResource = srcResource->d3dResource();
527 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
528 srcLocation.SubresourceIndex = 0;
529
530 D3D12_BOX srcBox = {};
531 srcBox.left = srcRect.fLeft;
532 srcBox.top = srcRect.fTop;
533 srcBox.right = srcRect.fRight;
534 srcBox.bottom = srcRect.fBottom;
535 srcBox.front = 0;
536 srcBox.back = 1;
537 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400538 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
539 &dstLocation,
540 dstPoint.fX, dstPoint.fY,
541 srcResource->resource(),
542 &srcLocation,
543 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400544
545 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
546 srcRect.width(), srcRect.height());
547 // The rect is already in device space so we pass in kTopLeft so no flip is done.
548 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
549}
550
551void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
552 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400553 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
554 SkASSERT(srcRT);
555
556 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
557}
558
559void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
560 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
561 SkASSERT(dst);
562 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
563
564 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
565
566 GrD3DTextureResource* dstTextureResource;
567 GrRenderTarget* dstRT = dst->asRenderTarget();
568 if (dstRT) {
569 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
570 } else {
571 SkASSERT(dst->asTexture());
572 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
573 }
574
575 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
576 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
577
578 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
579 src->msaaTextureResource(), &srcRect);
580}
581
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400582void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400583 SkASSERT(target->numSamples() > 1);
584 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
Brian Salomon72c7b982020-10-06 10:07:38 -0400585 SkASSERT(rt->msaaTextureResource() && rt != rt->msaaTextureResource());
Jim Van Verth275f4192020-07-07 15:36:41 -0400586
587 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400588}
589
Jim Van Verthba7f2292020-04-21 08:56:47 -0400590bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
591 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
592 size_t rowBytes) {
593 SkASSERT(surface);
594
595 if (surfaceColorType != dstColorType) {
596 return false;
597 }
598
599 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400600 GrD3DTextureResource* texResource = nullptr;
601 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
602 if (rt) {
603 texResource = rt;
604 } else {
605 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
606 }
607
608 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400609 return false;
610 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400611
Jim Van Verthba7f2292020-04-21 08:56:47 -0400612 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400613 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400614 SkASSERT(srcLocation.pResource);
615 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
616 srcLocation.SubresourceIndex = 0;
617
618 D3D12_BOX srcBox = {};
619 srcBox.left = left;
620 srcBox.top = top;
621 srcBox.right = left + width;
622 srcBox.bottom = top + height;
623 srcBox.front = 0;
624 srcBox.back = 1;
625
626 // Set up dst location and create transfer buffer
627 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
628 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400629 UINT64 transferTotalBytes;
630 const UINT64 baseOffset = 0;
631 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
632 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400633 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400634 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400635 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400636 if (GrDxgiFormatBytesPerBlock(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400637 return false;
638 }
639 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400640
641 // TODO: implement some way of reusing buffers instead of making a new one every time.
642 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
643 GrGpuBufferType::kXferGpuToCpu,
644 kDynamic_GrAccessPattern);
645 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
646 dstLocation.pResource = d3dBuf->d3dResource();
647
648 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400649 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400650
Greg Daniel69267912020-07-24 10:42:53 -0400651 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
652 texResource->resource(), &srcLocation,
653 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400654 this->submitDirectCommandList(SyncQueue::kForce);
655
656 const void* mappedMemory = transferBuffer->map();
657
658 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400659 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400660
661 transferBuffer->unmap();
662
663 return true;
664}
665
666bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
667 GrColorType surfaceColorType, GrColorType srcColorType,
668 const GrMipLevel texels[], int mipLevelCount,
669 bool prepForTexSampling) {
670 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
671 if (!d3dTex) {
672 return false;
673 }
674
675 // Make sure we have at least the base level
676 if (!mipLevelCount || !texels[0].fPixels) {
677 return false;
678 }
679
680 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
681 bool success = false;
682
683 // Need to change the resource state to COPY_DEST in order to upload to it
684 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
685
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400686 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400687 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
688 mipLevelCount);
689
690 if (prepForTexSampling) {
691 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
692 }
693
694 return success;
695}
696
697bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
698 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
699 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
700 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
701 SkASSERT(1 == mipLevelCount ||
702 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
703
704 // We assume that if the texture has mip levels, we either upload to all the levels or just the
705 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400706 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400707
708 if (width == 0 || height == 0) {
709 return false;
710 }
711
712 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
713 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
714
715 ID3D12Resource* d3dResource = tex->d3dResource();
716 SkASSERT(d3dResource);
717 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
718 // Either upload only the first miplevel or all miplevels
719 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
720
721 if (1 == mipLevelCount && !texels[0].fPixels) {
722 return true; // no data to upload
723 }
724
725 for (int i = 0; i < mipLevelCount; ++i) {
726 // We do not allow any gaps in the mip data
727 if (!texels[i].fPixels) {
728 return false;
729 }
730 }
731
732 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400733 UINT64 combinedBufferSize;
734 // We reset the width and height in the description to match our subrectangle size
735 // so we don't end up allocating more space than we need.
736 desc.Width = width;
737 desc.Height = height;
738 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400739 nullptr, nullptr, &combinedBufferSize);
740 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400741 SkASSERT(combinedBufferSize);
742
Greg Danielcffb0622020-07-16 13:19:17 -0400743 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
744 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
745 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400746 return false;
747 }
Greg Danielcffb0622020-07-16 13:19:17 -0400748
749 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400750
751 int currentWidth = width;
752 int currentHeight = height;
753 int layerHeight = tex->height();
754
755 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
756 if (texels[currentMipLevel].fPixels) {
757 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
758
Jim Van Verthfdd36852020-04-21 16:29:44 -0400759 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400760 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
761
762 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
763
764 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400765 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400766 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
767 src, srcRowBytes, trimRowBytes, currentHeight);
768 }
769 currentWidth = std::max(1, currentWidth / 2);
770 currentHeight = std::max(1, currentHeight / 2);
771 layerHeight = currentHeight;
772 }
773
Greg Danielcffb0622020-07-16 13:19:17 -0400774 // Update the offsets in the footprints to be relative to the slice's offset
775 for (int i = 0; i < mipLevelCount; ++i) {
776 placedFootprints[i].Offset += slice.fOffset;
777 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400778
Greg Daniel69267912020-07-24 10:42:53 -0400779 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400780 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
781 placedFootprints.get(), left, top);
782
783 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400784 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400785 }
786
787 return true;
788}
789
Jim Van Verth9145f782020-04-28 12:01:12 -0400790static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400791 if (!info.fResource.get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400792 return false;
793 }
794 return true;
795}
796
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400797static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
798 if (!caps.isFormatTexturable(info.fFormat)) {
799 return false;
800 }
Brian Salomon718ae762020-09-29 16:52:49 -0400801 // We don't support sampling from multisampled textures.
802 if (info.fSampleCount != 1) {
803 return false;
804 }
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400805 return true;
806}
807
808static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
809 int sampleCnt) {
810 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
811 return false;
812 }
813 return true;
814}
815
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400816sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
817 GrWrapOwnership,
818 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400819 GrIOType ioType) {
820 GrD3DTextureResourceInfo textureInfo;
821 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
822 return nullptr;
823 }
824
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400825 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400826 return nullptr;
827 }
828
829 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
830 return nullptr;
831 }
832
833 // TODO: support protected context
834 if (tex.isProtected()) {
835 return nullptr;
836 }
837
838 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
839 SkASSERT(state);
840 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
841 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500842}
843
844sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400845 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500846 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400847 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500848}
849
850sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
851 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500852 GrWrapOwnership ownership,
853 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400854 GrD3DTextureResourceInfo textureInfo;
855 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
856 return nullptr;
857 }
858
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400859 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400860 return nullptr;
861 }
862
863 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
864 return nullptr;
865 }
866 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
867 return nullptr;
868 }
869
870 // TODO: support protected context
871 if (tex.isProtected()) {
872 return nullptr;
873 }
874
875 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
876
877 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
878 SkASSERT(state);
879
880 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
881 sampleCnt, cacheable,
882 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500883}
884
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400885sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400886 GrD3DTextureResourceInfo info;
887 if (!rt.getD3DTextureResourceInfo(&info)) {
888 return nullptr;
889 }
890
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400891 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400892 return nullptr;
893 }
894
895 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
896 return nullptr;
897 }
898
899 // TODO: support protected context
900 if (rt.isProtected()) {
901 return nullptr;
902 }
903
904 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
905
906 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
Brian Salomon72c7b982020-10-06 10:07:38 -0400907 this, rt.dimensions(), rt.sampleCnt(), info, std::move(state));
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400908
909 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
910 SkASSERT(!rt.stencilBits());
911 if (tgt) {
912 SkASSERT(tgt->canAttemptStencilAttachment());
913 }
914
915 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500916}
917
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500918sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400919 GrAccessPattern accessPattern, const void* data) {
920 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
921 if (data && buffer) {
922 buffer->updateData(data, sizeInBytes);
923 }
924
925 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500926}
927
Greg Danielc0d69152020-10-08 14:59:00 -0400928sk_sp<GrAttachment> GrD3DGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
929 SkISize dimensions,
930 int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400931 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400932 SkASSERT(dimensions.width() >= rt->width());
933 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400934
Greg Daniel8ade5e82020-10-07 13:09:48 -0400935 DXGI_FORMAT sFmt = this->d3dCaps().preferredStencilFormat();
Jim Van Verth4f51f472020-04-13 11:02:21 -0400936
Jim Van Verth4f51f472020-04-13 11:02:21 -0400937 fStats.incStencilAttachmentCreates();
Greg Danielc0d69152020-10-08 14:59:00 -0400938 return GrD3DAttachment::MakeStencil(this, dimensions, numStencilSamples, sFmt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500939}
940
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400941bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
942 SkISize dimensions,
943 GrTexturable texturable,
944 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400945 GrMipmapped mipMapped,
Brian Salomon72c7b982020-10-06 10:07:38 -0400946 int sampleCnt,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400947 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400948 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400949 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400950
951 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
952 return false;
953 }
954
955 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
956 return false;
957 }
958
959 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
960 return false;
961 }
962
963 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400964 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400965 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400966 }
967
968 // create the texture
969 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
970 if (renderable == GrRenderable::kYes) {
971 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
972 }
973
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400974 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400975 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400976 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400977 resourceDesc.Width = dimensions.fWidth;
978 resourceDesc.Height = dimensions.fHeight;
979 resourceDesc.DepthOrArraySize = 1;
980 resourceDesc.MipLevels = numMipLevels;
981 resourceDesc.Format = dxgiFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -0400982 resourceDesc.SampleDesc.Count = sampleCnt;
Jim Van Verth765c5922020-08-10 17:23:50 -0400983 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -0400984 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400985 resourceDesc.Flags = usageFlags;
986
Jim Van Verth280d4f72020-05-04 10:04:24 -0400987 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
988 D3D12_CLEAR_VALUE clearValue = {};
989 if (renderable == GrRenderable::kYes) {
990 clearValue.Format = dxgiFormat;
991 // Assume transparent black
992 clearValue.Color[0] = 0;
993 clearValue.Color[1] = 0;
994 clearValue.Color[2] = 0;
995 clearValue.Color[3] = 0;
996 clearValuePtr = &clearValue;
997 }
998
Greg Daniel16032b32020-05-06 15:31:10 -0400999 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1000 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1001 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001002 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001003 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001004 SkDebugf("Failed to init texture resource info\n");
1005 return false;
1006 }
1007
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001008 return true;
1009}
1010
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001011GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001012 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001013 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001014 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001015 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001016 const GrD3DCaps& caps = this->d3dCaps();
1017
1018 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1019 return {};
1020 }
1021
1022 DXGI_FORMAT dxgiFormat;
1023 if (!format.asDxgiFormat(&dxgiFormat)) {
1024 return {};
1025 }
1026
1027 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1028 if (!caps.isFormatTexturable(dxgiFormat)) {
1029 return {};
1030 }
1031
1032 GrD3DTextureResourceInfo info;
1033 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
Brian Salomon72c7b982020-10-06 10:07:38 -04001034 renderable, mipMapped, 1, &info,
1035 isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001036 return {};
1037 }
1038
1039 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001040}
1041
Brian Salomon05487ab2020-12-23 20:32:22 -05001042static void copy_src_data(char* mapPtr,
1043 DXGI_FORMAT dxgiFormat,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001044 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
Brian Salomon05487ab2020-12-23 20:32:22 -05001045 const GrPixmap srcData[],
1046 int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001047 SkASSERT(srcData && numMipLevels);
1048 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1049 SkASSERT(mapPtr);
1050
Greg Daniel0eca74c2020-10-01 13:46:00 -04001051 size_t bytesPerPixel = GrDxgiFormatBytesPerBlock(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001052
1053 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1054 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1055
1056 // copy data into the buffer, skipping any trailing bytes
1057 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1058 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1059 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1060 trimRowBytes, srcData[currentMipLevel].height());
1061 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001062}
1063
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001064static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1065 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1066 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1067 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001068 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001069 if (colorType == GrColorType::kUnknown) {
1070 return false;
1071 }
1072 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1073 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1074 return false;
1075 }
1076
1077 return true;
1078}
1079
Greg Daniel16032b32020-05-06 15:31:10 -04001080bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1081 sk_sp<GrRefCntedCallback> finishedCallback,
1082 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001083 GrD3DTextureResourceInfo info;
1084 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1085
1086 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1087 SkASSERT(state);
1088 sk_sp<GrD3DTexture> texture =
1089 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1090 GrWrapCacheable::kNo,
1091 kRW_GrIOType, info, std::move(state));
1092 if (!texture) {
1093 return false;
1094 }
1095
1096 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1097 if (!cmdList) {
1098 return false;
1099 }
1100
1101 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1102
1103 ID3D12Resource* d3dResource = texture->d3dResource();
1104 SkASSERT(d3dResource);
1105 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1106 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001107 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001108 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001109 backendTexture.dimensions().height()) + 1;
1110 }
1111 SkASSERT(mipLevelCount == info.fLevelCount);
1112 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1113 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001114 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1115 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001116 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001117 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001118 SkASSERT(combinedBufferSize);
1119 if (data->type() == BackendTextureData::Type::kColor &&
1120 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1121 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1122 combinedBufferSize =
1123 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1124 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1125 placedFootprints[i].Offset = 0;
1126 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1127 }
1128 }
1129
Greg Danielcffb0622020-07-16 13:19:17 -04001130 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1131 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1132 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001133 return false;
1134 }
Greg Danielcffb0622020-07-16 13:19:17 -04001135
1136 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001137 SkASSERT(bufferData);
1138
Jim Van Verth43a6e172020-06-23 11:59:08 -04001139 if (data->type() == BackendTextureData::Type::kPixmaps) {
Greg Daniel0eca74c2020-10-01 13:46:00 -04001140 copy_src_data(bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001141 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001142 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001143 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1144 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001145 } else {
1146 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1147 SkImage::CompressionType compression =
1148 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1149 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001150 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1151 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001152 return false;
1153 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001154 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001155 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1156 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001157 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001158 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001159 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001160 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001161 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1162 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001163 }
1164 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001165
Greg Danielcffb0622020-07-16 13:19:17 -04001166 // Update the offsets in the footprints to be relative to the slice's offset
1167 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1168 placedFootprints[i].Offset += slice.fOffset;
1169 }
1170
Greg Daniel69267912020-07-24 10:42:53 -04001171 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001172 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1173 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001174
1175 if (finishedCallback) {
1176 this->addFinishedCallback(std::move(finishedCallback));
1177 }
1178
Greg Daniel16032b32020-05-06 15:31:10 -04001179 return true;
1180}
1181
Greg Danielc1ad77c2020-05-06 11:40:03 -04001182GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001183 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001184 GrProtected isProtected) {
1185 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1186 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001187}
1188
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001189bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001190 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001191 const BackendTextureData* data) {
1192 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001193}
1194
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001195void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001196 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1197 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001198}
1199
Robert Phillips979b2232020-02-20 10:47:29 -05001200bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1201 return false;
1202}
1203
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001204#if GR_TEST_UTILS
1205bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001206 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1207
1208 GrD3DTextureResourceInfo info;
1209 if (!tex.getD3DTextureResourceInfo(&info)) {
1210 return false;
1211 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001212 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001213 if (!textureResource) {
1214 return false;
1215 }
1216 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001217}
1218
Brian Salomon72c7b982020-10-06 10:07:38 -04001219GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
1220 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04001221 int sampleCnt,
1222 GrProtected isProtected) {
Brian Salomon72c7b982020-10-06 10:07:38 -04001223 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
1224 dimensions.height() > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001225 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001226 }
1227
1228 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1229
1230 GrD3DTextureResourceInfo info;
Brian Salomon72c7b982020-10-06 10:07:38 -04001231 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001232 GrRenderable::kYes, GrMipmapped::kNo,
Brian Salomonf9b00422020-10-08 16:00:14 -04001233 sampleCnt, &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001234 return {};
1235 }
1236
Brian Salomon72c7b982020-10-06 10:07:38 -04001237 return GrBackendRenderTarget(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001238}
1239
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001240void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1241 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1242
1243 GrD3DTextureResourceInfo info;
1244 if (rt.getD3DTextureResourceInfo(&info)) {
Brian Salomon725f1582021-02-12 12:10:43 -05001245 this->submitToGpu(true);
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001246 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1247 // is deleted.
1248 }
1249}
1250
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001251void GrD3DGpu::testingOnly_startCapture() {
1252 if (fGraphicsAnalysis) {
1253 fGraphicsAnalysis->BeginCapture();
1254 }
1255}
1256
1257void GrD3DGpu::testingOnly_endCapture() {
1258 if (fGraphicsAnalysis) {
1259 fGraphicsAnalysis->EndCapture();
1260 }
1261}
1262#endif
1263
Jim Van Verthc632aa62020-04-17 16:58:20 -04001264///////////////////////////////////////////////////////////////////////////////
1265
Greg Daniela5a6b322020-04-23 12:52:27 -04001266void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001267 int numBarriers,
1268 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1269 SkASSERT(fCurrentDirectCommandList);
1270 SkASSERT(resource);
1271
Greg Daniela5a6b322020-04-23 12:52:27 -04001272 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001273}
1274
Greg Daniel69267912020-07-24 10:42:53 -04001275void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1276 int numBarriers,
1277 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1278 SkASSERT(fCurrentDirectCommandList);
1279 SkASSERT(buffer);
1280
1281 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1282 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1283}
1284
1285
Greg Daniel9efe3862020-06-11 11:51:06 -04001286void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001287 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -04001288 SkSurface::BackendSurfaceAccess access,
1289 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001290 // prepare proxies by transitioning to PRESENT renderState
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001291 if (!proxies.empty() && access == SkSurface::BackendSurfaceAccess::kPresent) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001292 GrD3DTextureResource* resource;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001293 for (GrSurfaceProxy* proxy : proxies) {
1294 SkASSERT(proxy->isInstantiated());
1295 if (GrTexture* tex = proxy->peekTexture()) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001296 resource = static_cast<GrD3DTexture*>(tex);
1297 } else {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001298 GrRenderTarget* rt = proxy->peekRenderTarget();
Jim Van Verth682a2f42020-05-13 16:54:09 -04001299 SkASSERT(rt);
1300 resource = static_cast<GrD3DRenderTarget*>(rt);
1301 }
1302 resource->prepareForPresent(this);
1303 }
1304 }
1305}
1306
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001307void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001308 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001309}
1310
Jim Van Verthc632aa62020-04-17 16:58:20 -04001311bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1312 if (syncCpu) {
1313 return this->submitDirectCommandList(SyncQueue::kForce);
1314 } else {
1315 return this->submitDirectCommandList(SyncQueue::kSkip);
1316 }
1317}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001318
1319std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1320 return GrD3DSemaphore::Make(this);
1321}
1322std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1323 const GrBackendSemaphore& semaphore,
1324 GrResourceProvider::SemaphoreWrapType,
1325 GrWrapOwnership) {
1326 SkASSERT(this->caps()->semaphoreSupport());
1327 GrD3DFenceInfo fenceInfo;
1328 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1329 return nullptr;
1330 }
1331 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1332}
1333
1334void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001335 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001336 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1337 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1338 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1339}
1340
1341void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001342 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001343 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1344 // TODO: Do we need to track the lifetime of this?
1345 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1346}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001347
1348GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001349 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001350 return fCurrentFenceValue;
1351}
1352
1353bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001354 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001355}
Greg Daniela89b4302021-01-29 10:48:40 -05001356
1357void GrD3DGpu::finishOutstandingGpuWork() {
1358 this->waitForQueueCompletion();
1359}