blob: b3f22b951b0c87c0706ce7ebc7c6517578255553 [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)
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 Danielc0d69152020-10-08 14:59:00 -0400119 GrRenderTarget* rt,
120 GrAttachment*,
121 GrSurfaceOrigin origin,
122 const SkIRect& bounds,
Greg Daniel9a18b082020-08-14 14:03:50 -0400123 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
124 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
125 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400126 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500127 if (!fCachedOpsRenderPass) {
128 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
129 }
130
131 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
132 return nullptr;
133 }
134 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500135}
136
Jim Van Verthc632aa62020-04-17 16:58:20 -0400137bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400138 SkASSERT(fCurrentDirectCommandList);
139
Jim Van Verth3eadce22020-06-01 11:34:49 -0400140 fResourceProvider.prepForSubmit();
141
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400142 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
Jim Van Verthc632aa62020-04-17 16:58:20 -0400143 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
144 return false;
145 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
146 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400147 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400148 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400149 }
150 return true;
151 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400152
Greg Daniela581a8b2020-06-19 15:22:18 -0400153 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
154 // uniform data as dirty.
155 fResourceProvider.markPipelineStateUniformsDirty();
156
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400157 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400158 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400159 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400160
Jim Van Verthc632aa62020-04-17 16:58:20 -0400161 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400162 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400163 }
164
Greg Daniel83ed2132020-03-24 13:15:33 -0400165 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
166
167 // This should be done after we have a new command list in case the freeing of any resources
168 // held by a finished command list causes us send a new command to the gpu (like changing the
169 // resource state.
170 this->checkForFinishedCommandLists();
171
172 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400173 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400174}
175
176void GrD3DGpu::checkForFinishedCommandLists() {
177 uint64_t currentFenceValue = fFence->GetCompletedValue();
178
179 // Iterate over all the outstanding command lists to see if any have finished. The commands
180 // lists are in order from oldest to newest, so we start at the front to check if their fence
181 // value is less than the last signaled value. If so we pop it off and move onto the next.
182 // Repeat till we find a command list that has not finished yet (and all others afterwards are
183 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400184 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
185 while (front && front->fFenceValue <= currentFenceValue) {
186 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400187 // Since we used placement new we are responsible for calling the destructor manually.
188 front->~OutstandingCommandList();
189 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400190 fResourceProvider.recycleDirectCommandList(std::move(currList));
191 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400192 }
193}
194
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400195void GrD3DGpu::waitForQueueCompletion() {
196 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
197 HANDLE fenceEvent;
198 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
199 SkASSERT(fenceEvent);
200 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
201 WaitForSingleObject(fenceEvent, INFINITE);
202 CloseHandle(fenceEvent);
203 }
204}
205
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500206void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400207 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
208
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500209 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400210 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500211}
212
Greg Danield4928d02020-06-19 11:13:26 -0400213void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
214 GrGpuFinishedContext finishedContext) {
215 SkASSERT(finishedProc);
216 sk_sp<GrRefCntedCallback> finishedCallback(
217 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400218 this->addFinishedCallback(std::move(finishedCallback));
219}
220
221void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
222 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400223 // Besides the current command list, we also add the finishedCallback to the newest outstanding
224 // command list. Our contract for calling the proc is that all previous submitted command lists
225 // have finished when we call it. However, if our current command list has no work when it is
226 // flushed it will drop its ref to the callback immediately. But the previous work may not have
227 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
228 // must finish after all previously submitted command lists.
229 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
230 if (back) {
231 back->fCommandList->addFinishedCallback(finishedCallback);
232 }
233 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
234}
235
Jim Van Verth765c5922020-08-10 17:23:50 -0400236void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
237 SkTArray<SkPoint>* sampleLocations) {
238 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
239 // These are transformed from D3D's integer coordinate system with origin at the center,
240 // to our normalized coordinate system with origin at the upper left.
241 // This ends up corresponding with Vulkan's sample locations.
242 SkASSERT(this->caps()->sampleLocationsSupport());
243 static constexpr SkPoint kStandardSampleLocations_1[1] = {
244 {0.5f, 0.5f} };
245 static constexpr SkPoint kStandardSampleLocations_2[2] = {
246 {0.75f, 0.75f}, {0.25f, 0.25f} };
247 static constexpr SkPoint kStandardSampleLocations_4[4] = {
248 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
249 static constexpr SkPoint kStandardSampleLocations_8[8] = {
250 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
251 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
252 static constexpr SkPoint kStandardSampleLocations_16[16] = {
253 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
254 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
255 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
256 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
257
258 int numSamples = renderTarget->numSamples();
259 // TODO: support mixed samples?
260 SkASSERT(numSamples > 1);
261 SkASSERT(!renderTarget->getStencilAttachment() ||
262 numSamples == renderTarget->getStencilAttachment()->numSamples());
263
264 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
265 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
266 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
267 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
268 return;
269 }
270 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
271
272 switch (numSamples) {
273 case 2:
274 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
275 break;
276 case 4:
277 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
278 break;
279 case 8:
280 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
281 break;
282 case 16:
283 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
284 break;
285 default:
286 SK_ABORT("Invalid sample count.");
287 break;
288 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500289}
290
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400291sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
292 DXGI_FORMAT dxgiFormat,
293 GrRenderable renderable,
294 int renderTargetSampleCnt,
295 SkBudgeted budgeted,
296 GrProtected isProtected,
297 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400298 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400299 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400300 if (renderable == GrRenderable::kYes) {
301 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
302 }
303
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400304 // 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 -0400305 // requested, this describes the resolved texture. Therefore we always have samples set
306 // to 1.
307 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400308 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400309 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
310 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
311 // might want to manually set alignment to 4KB for smaller textures
312 resourceDesc.Alignment = 0;
313 resourceDesc.Width = dimensions.fWidth;
314 resourceDesc.Height = dimensions.fHeight;
315 resourceDesc.DepthOrArraySize = 1;
316 resourceDesc.MipLevels = mipLevelCount;
317 resourceDesc.Format = dxgiFormat;
318 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400319 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400320 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400321 resourceDesc.Flags = usageFlags;
322
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400323 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400324 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400325 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400326 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400327 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400328 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400329 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400330 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400332
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400333sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
334 const GrBackendFormat& format,
335 GrRenderable renderable,
336 int renderTargetSampleCnt,
337 SkBudgeted budgeted,
338 GrProtected isProtected,
339 int mipLevelCount,
340 uint32_t levelClearMask) {
341 DXGI_FORMAT dxgiFormat;
342 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
343 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
344
Brian Salomona6db5102020-07-21 09:56:23 -0400345 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
346 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400347
348 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
349 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400350 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400351 if (!tex) {
352 return nullptr;
353 }
354
355 if (levelClearMask) {
356 // TODO
357 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400358
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400359 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500360}
361
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400362static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
363 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
364 UINT* numRows, UINT64* rowSizeInBytes,
365 const void* compressedData, int numMipLevels) {
366 SkASSERT(compressedData && numMipLevels);
367 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
368 SkASSERT(mapPtr);
369
370 const char* src = static_cast<const char*>(compressedData);
371 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
372 // copy data into the buffer, skipping any trailing bytes
373 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
374 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
375 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
376 numRows[currentMipLevel]);
377 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
378 }
379}
380
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500381sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
382 const GrBackendFormat& format,
383 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400384 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500385 GrProtected isProtected,
386 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400387 DXGI_FORMAT dxgiFormat;
388 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
389 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
390
391 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
392 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400393 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400394
395 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400396 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400397 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400398 }
Brian Salomona6db5102020-07-21 09:56:23 -0400399 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
400 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400401
402 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
403 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400404 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400405 if (!d3dTex) {
406 return nullptr;
407 }
408
409 ID3D12Resource* d3dResource = d3dTex->d3dResource();
410 SkASSERT(d3dResource);
411 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
412 // Either upload only the first miplevel or all miplevels
413 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
414
415 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
416 SkAutoTMalloc<UINT> numRows(mipLevelCount);
417 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
418 UINT64 combinedBufferSize;
419 // We reset the width and height in the description to match our subrectangle size
420 // so we don't end up allocating more space than we need.
421 desc.Width = dimensions.width();
422 desc.Height = dimensions.height();
423 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
424 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
425 SkASSERT(combinedBufferSize);
426
Greg Danielcffb0622020-07-16 13:19:17 -0400427 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
428 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
429 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400430 return false;
431 }
Greg Danielcffb0622020-07-16 13:19:17 -0400432
433 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400434
435 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
436 rowSizeInBytes.get(), data, mipLevelCount);
437
Greg Danielcffb0622020-07-16 13:19:17 -0400438 // Update the offsets in the footprints to be relative to the slice's offset
439 for (int i = 0; i < mipLevelCount; ++i) {
440 placedFootprints[i].Offset += slice.fOffset;
441 }
442
Greg Daniel69267912020-07-24 10:42:53 -0400443 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400444 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
445 placedFootprints.get(), 0, 0);
446
447 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500448}
449
Jim Van Verth9145f782020-04-28 12:01:12 -0400450static int get_surface_sample_cnt(GrSurface* surf) {
451 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
452 return rt->numSamples();
453 }
454 return 0;
455}
456
457bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
458 const SkIPoint& dstPoint) {
459
460 if (src->isProtected() && !dst->isProtected()) {
461 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400462 return false;
463 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400464
465 int dstSampleCnt = get_surface_sample_cnt(dst);
466 int srcSampleCnt = get_surface_sample_cnt(src);
467
468 GrD3DTextureResource* dstTexResource;
469 GrD3DTextureResource* srcTexResource;
470 GrRenderTarget* dstRT = dst->asRenderTarget();
471 if (dstRT) {
472 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
473 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
474 } else {
475 SkASSERT(dst->asTexture());
476 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
477 }
478 GrRenderTarget* srcRT = src->asRenderTarget();
479 if (srcRT) {
480 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
481 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
482 } else {
483 SkASSERT(src->asTexture());
484 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
485 }
486
487 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
488 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
489
490 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
491 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
492 return true;
493 }
494
495 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
496 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
497 return true;
498 }
499
500 return false;
501}
502
503void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
504 GrD3DTextureResource* dstResource,
505 GrD3DTextureResource* srcResource,
506 const SkIRect& srcRect, const SkIPoint& dstPoint) {
507#ifdef SK_DEBUG
508 int dstSampleCnt = get_surface_sample_cnt(dst);
509 int srcSampleCnt = get_surface_sample_cnt(src);
510 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
511 DXGI_FORMAT srcFormat;
512 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
513 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
514#endif
515 if (src->isProtected() && !dst->isProtected()) {
516 SkDebugf("Can't copy from protected memory to non-protected");
517 return;
518 }
519
520 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
521 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
522
523 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
524 dstLocation.pResource = dstResource->d3dResource();
525 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
526 dstLocation.SubresourceIndex = 0;
527
528 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
529 srcLocation.pResource = srcResource->d3dResource();
530 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
531 srcLocation.SubresourceIndex = 0;
532
533 D3D12_BOX srcBox = {};
534 srcBox.left = srcRect.fLeft;
535 srcBox.top = srcRect.fTop;
536 srcBox.right = srcRect.fRight;
537 srcBox.bottom = srcRect.fBottom;
538 srcBox.front = 0;
539 srcBox.back = 1;
540 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400541 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
542 &dstLocation,
543 dstPoint.fX, dstPoint.fY,
544 srcResource->resource(),
545 &srcLocation,
546 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400547
548 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
549 srcRect.width(), srcRect.height());
550 // The rect is already in device space so we pass in kTopLeft so no flip is done.
551 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
552}
553
554void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
555 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400556 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
557 SkASSERT(srcRT);
558
559 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
560}
561
562void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
563 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
564 SkASSERT(dst);
565 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
566
567 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
568
569 GrD3DTextureResource* dstTextureResource;
570 GrRenderTarget* dstRT = dst->asRenderTarget();
571 if (dstRT) {
572 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
573 } else {
574 SkASSERT(dst->asTexture());
575 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
576 }
577
578 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
579 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
580
581 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
582 src->msaaTextureResource(), &srcRect);
583}
584
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400585void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400586 SkASSERT(target->numSamples() > 1);
587 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
Brian Salomon72c7b982020-10-06 10:07:38 -0400588 SkASSERT(rt->msaaTextureResource() && rt != rt->msaaTextureResource());
Jim Van Verth275f4192020-07-07 15:36:41 -0400589
590 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400591}
592
Jim Van Verthba7f2292020-04-21 08:56:47 -0400593bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
594 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
595 size_t rowBytes) {
596 SkASSERT(surface);
597
598 if (surfaceColorType != dstColorType) {
599 return false;
600 }
601
602 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400603 GrD3DTextureResource* texResource = nullptr;
604 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
605 if (rt) {
606 texResource = rt;
607 } else {
608 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
609 }
610
611 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400612 return false;
613 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400614
Jim Van Verthba7f2292020-04-21 08:56:47 -0400615 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400616 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400617 SkASSERT(srcLocation.pResource);
618 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
619 srcLocation.SubresourceIndex = 0;
620
621 D3D12_BOX srcBox = {};
622 srcBox.left = left;
623 srcBox.top = top;
624 srcBox.right = left + width;
625 srcBox.bottom = top + height;
626 srcBox.front = 0;
627 srcBox.back = 1;
628
629 // Set up dst location and create transfer buffer
630 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
631 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400632 UINT64 transferTotalBytes;
633 const UINT64 baseOffset = 0;
634 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
635 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400636 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400637 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400638 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400639 if (GrDxgiFormatBytesPerBlock(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400640 return false;
641 }
642 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400643
644 // TODO: implement some way of reusing buffers instead of making a new one every time.
645 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
646 GrGpuBufferType::kXferGpuToCpu,
647 kDynamic_GrAccessPattern);
648 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
649 dstLocation.pResource = d3dBuf->d3dResource();
650
651 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400652 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400653
Greg Daniel69267912020-07-24 10:42:53 -0400654 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
655 texResource->resource(), &srcLocation,
656 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400657 this->submitDirectCommandList(SyncQueue::kForce);
658
659 const void* mappedMemory = transferBuffer->map();
660
661 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400662 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400663
664 transferBuffer->unmap();
665
666 return true;
667}
668
669bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
670 GrColorType surfaceColorType, GrColorType srcColorType,
671 const GrMipLevel texels[], int mipLevelCount,
672 bool prepForTexSampling) {
673 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
674 if (!d3dTex) {
675 return false;
676 }
677
678 // Make sure we have at least the base level
679 if (!mipLevelCount || !texels[0].fPixels) {
680 return false;
681 }
682
683 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
684 bool success = false;
685
686 // Need to change the resource state to COPY_DEST in order to upload to it
687 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
688
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400689 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400690 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
691 mipLevelCount);
692
693 if (prepForTexSampling) {
694 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
695 }
696
697 return success;
698}
699
700bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
701 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
702 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
703 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
704 SkASSERT(1 == mipLevelCount ||
705 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
706
707 // We assume that if the texture has mip levels, we either upload to all the levels or just the
708 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400709 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400710
711 if (width == 0 || height == 0) {
712 return false;
713 }
714
715 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
716 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
717
718 ID3D12Resource* d3dResource = tex->d3dResource();
719 SkASSERT(d3dResource);
720 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
721 // Either upload only the first miplevel or all miplevels
722 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
723
724 if (1 == mipLevelCount && !texels[0].fPixels) {
725 return true; // no data to upload
726 }
727
728 for (int i = 0; i < mipLevelCount; ++i) {
729 // We do not allow any gaps in the mip data
730 if (!texels[i].fPixels) {
731 return false;
732 }
733 }
734
735 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400736 UINT64 combinedBufferSize;
737 // We reset the width and height in the description to match our subrectangle size
738 // so we don't end up allocating more space than we need.
739 desc.Width = width;
740 desc.Height = height;
741 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400742 nullptr, nullptr, &combinedBufferSize);
743 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400744 SkASSERT(combinedBufferSize);
745
Greg Danielcffb0622020-07-16 13:19:17 -0400746 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
747 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
748 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400749 return false;
750 }
Greg Danielcffb0622020-07-16 13:19:17 -0400751
752 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400753
754 int currentWidth = width;
755 int currentHeight = height;
756 int layerHeight = tex->height();
757
758 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
759 if (texels[currentMipLevel].fPixels) {
760 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
761
Jim Van Verthfdd36852020-04-21 16:29:44 -0400762 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400763 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
764
765 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
766
767 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400768 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400769 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
770 src, srcRowBytes, trimRowBytes, currentHeight);
771 }
772 currentWidth = std::max(1, currentWidth / 2);
773 currentHeight = std::max(1, currentHeight / 2);
774 layerHeight = currentHeight;
775 }
776
Greg Danielcffb0622020-07-16 13:19:17 -0400777 // Update the offsets in the footprints to be relative to the slice's offset
778 for (int i = 0; i < mipLevelCount; ++i) {
779 placedFootprints[i].Offset += slice.fOffset;
780 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400781
Greg Daniel69267912020-07-24 10:42:53 -0400782 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400783 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
784 placedFootprints.get(), left, top);
785
786 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400787 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400788 }
789
790 return true;
791}
792
Jim Van Verth9145f782020-04-28 12:01:12 -0400793static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400794 if (!info.fResource.get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400795 return false;
796 }
797 return true;
798}
799
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400800static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
801 if (!caps.isFormatTexturable(info.fFormat)) {
802 return false;
803 }
Brian Salomon718ae762020-09-29 16:52:49 -0400804 // We don't support sampling from multisampled textures.
805 if (info.fSampleCount != 1) {
806 return false;
807 }
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400808 return true;
809}
810
811static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
812 int sampleCnt) {
813 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
814 return false;
815 }
816 return true;
817}
818
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400819sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
820 GrWrapOwnership,
821 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400822 GrIOType ioType) {
823 GrD3DTextureResourceInfo textureInfo;
824 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
825 return nullptr;
826 }
827
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400828 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400829 return nullptr;
830 }
831
832 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
833 return nullptr;
834 }
835
836 // TODO: support protected context
837 if (tex.isProtected()) {
838 return nullptr;
839 }
840
841 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
842 SkASSERT(state);
843 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
844 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500845}
846
847sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400848 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500849 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400850 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500851}
852
853sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
854 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500855 GrWrapOwnership ownership,
856 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400857 GrD3DTextureResourceInfo textureInfo;
858 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
859 return nullptr;
860 }
861
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400862 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400863 return nullptr;
864 }
865
866 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
867 return nullptr;
868 }
869 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
870 return nullptr;
871 }
872
873 // TODO: support protected context
874 if (tex.isProtected()) {
875 return nullptr;
876 }
877
878 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
879
880 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
881 SkASSERT(state);
882
883 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
884 sampleCnt, cacheable,
885 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500886}
887
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400888sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400889 GrD3DTextureResourceInfo info;
890 if (!rt.getD3DTextureResourceInfo(&info)) {
891 return nullptr;
892 }
893
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400894 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400895 return nullptr;
896 }
897
898 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
899 return nullptr;
900 }
901
902 // TODO: support protected context
903 if (rt.isProtected()) {
904 return nullptr;
905 }
906
907 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
908
909 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
Brian Salomon72c7b982020-10-06 10:07:38 -0400910 this, rt.dimensions(), rt.sampleCnt(), info, std::move(state));
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400911
912 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
913 SkASSERT(!rt.stencilBits());
914 if (tgt) {
915 SkASSERT(tgt->canAttemptStencilAttachment());
916 }
917
918 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500919}
920
921sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400922 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400923
924 GrD3DTextureResourceInfo textureInfo;
925 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
926 return nullptr;
927 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400928 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400929 return nullptr;
930 }
931
Brian Salomon718ae762020-09-29 16:52:49 -0400932 // If sampleCnt is > 1 we will create an intermediate MSAA VkImage and then resolve into
933 // the wrapped VkImage. We don't yet support rendering directly to client-provided MSAA texture.
934 if (textureInfo.fSampleCount != 1) {
935 return nullptr;
936 }
937
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400938 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
939 return nullptr;
940 }
941
942 // TODO: support protected context
943 if (tex.isProtected()) {
944 return nullptr;
945 }
946
947 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
948 if (!sampleCnt) {
949 return nullptr;
950 }
951
952 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
953 SkASSERT(state);
954
955 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
956 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500957}
958
959sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400960 GrAccessPattern accessPattern, const void* data) {
961 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
962 if (data && buffer) {
963 buffer->updateData(data, sizeInBytes);
964 }
965
966 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500967}
968
Greg Danielc0d69152020-10-08 14:59:00 -0400969sk_sp<GrAttachment> GrD3DGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
970 SkISize dimensions,
971 int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400972 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400973 SkASSERT(dimensions.width() >= rt->width());
974 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400975
Greg Daniel8ade5e82020-10-07 13:09:48 -0400976 DXGI_FORMAT sFmt = this->d3dCaps().preferredStencilFormat();
Jim Van Verth4f51f472020-04-13 11:02:21 -0400977
Jim Van Verth4f51f472020-04-13 11:02:21 -0400978 fStats.incStencilAttachmentCreates();
Greg Danielc0d69152020-10-08 14:59:00 -0400979 return GrD3DAttachment::MakeStencil(this, dimensions, numStencilSamples, sFmt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500980}
981
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400982bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
983 SkISize dimensions,
984 GrTexturable texturable,
985 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400986 GrMipmapped mipMapped,
Brian Salomon72c7b982020-10-06 10:07:38 -0400987 int sampleCnt,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400988 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400989 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400990 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400991
992 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
993 return false;
994 }
995
996 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
997 return false;
998 }
999
1000 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
1001 return false;
1002 }
1003
1004 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001005 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001006 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001007 }
1008
1009 // create the texture
1010 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
1011 if (renderable == GrRenderable::kYes) {
1012 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
1013 }
1014
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001015 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001016 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -04001017 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001018 resourceDesc.Width = dimensions.fWidth;
1019 resourceDesc.Height = dimensions.fHeight;
1020 resourceDesc.DepthOrArraySize = 1;
1021 resourceDesc.MipLevels = numMipLevels;
1022 resourceDesc.Format = dxgiFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -04001023 resourceDesc.SampleDesc.Count = sampleCnt;
Jim Van Verth765c5922020-08-10 17:23:50 -04001024 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -04001025 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001026 resourceDesc.Flags = usageFlags;
1027
Jim Van Verth280d4f72020-05-04 10:04:24 -04001028 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
1029 D3D12_CLEAR_VALUE clearValue = {};
1030 if (renderable == GrRenderable::kYes) {
1031 clearValue.Format = dxgiFormat;
1032 // Assume transparent black
1033 clearValue.Color[0] = 0;
1034 clearValue.Color[1] = 0;
1035 clearValue.Color[2] = 0;
1036 clearValue.Color[3] = 0;
1037 clearValuePtr = &clearValue;
1038 }
1039
Greg Daniel16032b32020-05-06 15:31:10 -04001040 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1041 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1042 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001043 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001044 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001045 SkDebugf("Failed to init texture resource info\n");
1046 return false;
1047 }
1048
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001049 return true;
1050}
1051
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001052GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001053 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001054 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001055 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001056 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001057 this->handleDirtyContext();
1058
1059 const GrD3DCaps& caps = this->d3dCaps();
1060
1061 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1062 return {};
1063 }
1064
1065 DXGI_FORMAT dxgiFormat;
1066 if (!format.asDxgiFormat(&dxgiFormat)) {
1067 return {};
1068 }
1069
1070 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1071 if (!caps.isFormatTexturable(dxgiFormat)) {
1072 return {};
1073 }
1074
1075 GrD3DTextureResourceInfo info;
1076 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
Brian Salomon72c7b982020-10-06 10:07:38 -04001077 renderable, mipMapped, 1, &info,
1078 isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001079 return {};
1080 }
1081
1082 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001083}
1084
Greg Daniel0eca74c2020-10-01 13:46:00 -04001085static void copy_src_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001086 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1087 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001088 SkASSERT(srcData && numMipLevels);
1089 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1090 SkASSERT(mapPtr);
1091
Greg Daniel0eca74c2020-10-01 13:46:00 -04001092 size_t bytesPerPixel = GrDxgiFormatBytesPerBlock(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001093
1094 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1095 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1096
1097 // copy data into the buffer, skipping any trailing bytes
1098 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1099 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1100 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1101 trimRowBytes, srcData[currentMipLevel].height());
1102 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001103}
1104
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001105static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1106 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1107 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1108 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001109 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001110 if (colorType == GrColorType::kUnknown) {
1111 return false;
1112 }
1113 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1114 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1115 return false;
1116 }
1117
1118 return true;
1119}
1120
Greg Daniel16032b32020-05-06 15:31:10 -04001121bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1122 sk_sp<GrRefCntedCallback> finishedCallback,
1123 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001124 GrD3DTextureResourceInfo info;
1125 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1126
1127 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1128 SkASSERT(state);
1129 sk_sp<GrD3DTexture> texture =
1130 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1131 GrWrapCacheable::kNo,
1132 kRW_GrIOType, info, std::move(state));
1133 if (!texture) {
1134 return false;
1135 }
1136
1137 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1138 if (!cmdList) {
1139 return false;
1140 }
1141
1142 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1143
1144 ID3D12Resource* d3dResource = texture->d3dResource();
1145 SkASSERT(d3dResource);
1146 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1147 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001148 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001149 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001150 backendTexture.dimensions().height()) + 1;
1151 }
1152 SkASSERT(mipLevelCount == info.fLevelCount);
1153 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1154 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001155 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1156 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001157 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001158 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001159 SkASSERT(combinedBufferSize);
1160 if (data->type() == BackendTextureData::Type::kColor &&
1161 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1162 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1163 combinedBufferSize =
1164 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1165 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1166 placedFootprints[i].Offset = 0;
1167 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1168 }
1169 }
1170
Greg Danielcffb0622020-07-16 13:19:17 -04001171 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1172 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1173 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001174 return false;
1175 }
Greg Danielcffb0622020-07-16 13:19:17 -04001176
1177 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001178 SkASSERT(bufferData);
1179
Jim Van Verth43a6e172020-06-23 11:59:08 -04001180 if (data->type() == BackendTextureData::Type::kPixmaps) {
Greg Daniel0eca74c2020-10-01 13:46:00 -04001181 copy_src_data(bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001182 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001183 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001184 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1185 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001186 } else {
1187 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1188 SkImage::CompressionType compression =
1189 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1190 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001191 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1192 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001193 return false;
1194 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001195 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001196 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1197 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001198 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001199 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001200 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001201 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001202 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1203 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001204 }
1205 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001206
Greg Danielcffb0622020-07-16 13:19:17 -04001207 // Update the offsets in the footprints to be relative to the slice's offset
1208 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1209 placedFootprints[i].Offset += slice.fOffset;
1210 }
1211
Greg Daniel69267912020-07-24 10:42:53 -04001212 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001213 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1214 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001215
1216 if (finishedCallback) {
1217 this->addFinishedCallback(std::move(finishedCallback));
1218 }
1219
Greg Daniel16032b32020-05-06 15:31:10 -04001220 return true;
1221}
1222
Greg Danielc1ad77c2020-05-06 11:40:03 -04001223GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001224 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001225 GrProtected isProtected) {
1226 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1227 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001228}
1229
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001230bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001231 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001232 const BackendTextureData* data) {
1233 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001234}
1235
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001236void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001237 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1238 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001239}
1240
Robert Phillips979b2232020-02-20 10:47:29 -05001241bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1242 return false;
1243}
1244
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001245#if GR_TEST_UTILS
1246bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001247 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1248
1249 GrD3DTextureResourceInfo info;
1250 if (!tex.getD3DTextureResourceInfo(&info)) {
1251 return false;
1252 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001253 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001254 if (!textureResource) {
1255 return false;
1256 }
1257 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001258}
1259
Brian Salomon72c7b982020-10-06 10:07:38 -04001260GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
1261 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04001262 int sampleCnt,
1263 GrProtected isProtected) {
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 Salomonf9b00422020-10-08 16:00:14 -04001276 sampleCnt, &info, isProtected)) {
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}