blob: db9179b3d0b0b72d452e01ac039ca90342007e94 [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) {
Jim Van Verth8ec13302020-02-26 12:59:56 -050068 fCaps.reset(new GrD3DCaps(contextOptions,
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040069 backendContext.fAdapter.get(),
70 backendContext.fDevice.get()));
Brian Osmand7e76592020-11-02 12:26:22 -050071 fCompiler.reset(new SkSL::Compiler(fCaps->shaderCaps()));
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
Greg Daniel65476e02020-10-27 09:20:20 -0400118GrOpsRenderPass* GrD3DGpu::onGetOpsRenderPass(
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);
Brian Salomon694ff172020-11-04 16:54:28 -0500216 this->addFinishedCallback(GrRefCntedCallback::Make(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400217}
218
219void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
220 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400221 // Besides the current command list, we also add the finishedCallback to the newest outstanding
222 // command list. Our contract for calling the proc is that all previous submitted command lists
223 // have finished when we call it. However, if our current command list has no work when it is
224 // flushed it will drop its ref to the callback immediately. But the previous work may not have
225 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
226 // must finish after all previously submitted command lists.
227 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
228 if (back) {
229 back->fCommandList->addFinishedCallback(finishedCallback);
230 }
231 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
232}
233
Jim Van Verth765c5922020-08-10 17:23:50 -0400234void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
235 SkTArray<SkPoint>* sampleLocations) {
236 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
237 // These are transformed from D3D's integer coordinate system with origin at the center,
238 // to our normalized coordinate system with origin at the upper left.
239 // This ends up corresponding with Vulkan's sample locations.
240 SkASSERT(this->caps()->sampleLocationsSupport());
241 static constexpr SkPoint kStandardSampleLocations_1[1] = {
242 {0.5f, 0.5f} };
243 static constexpr SkPoint kStandardSampleLocations_2[2] = {
244 {0.75f, 0.75f}, {0.25f, 0.25f} };
245 static constexpr SkPoint kStandardSampleLocations_4[4] = {
246 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
247 static constexpr SkPoint kStandardSampleLocations_8[8] = {
248 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
249 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
250 static constexpr SkPoint kStandardSampleLocations_16[16] = {
251 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
252 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
253 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
254 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
255
256 int numSamples = renderTarget->numSamples();
257 // TODO: support mixed samples?
258 SkASSERT(numSamples > 1);
259 SkASSERT(!renderTarget->getStencilAttachment() ||
260 numSamples == renderTarget->getStencilAttachment()->numSamples());
261
262 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
263 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
264 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
265 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
266 return;
267 }
268 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
269
270 switch (numSamples) {
271 case 2:
272 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
273 break;
274 case 4:
275 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
276 break;
277 case 8:
278 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
279 break;
280 case 16:
281 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
282 break;
283 default:
284 SK_ABORT("Invalid sample count.");
285 break;
286 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500287}
288
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400289sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
290 DXGI_FORMAT dxgiFormat,
291 GrRenderable renderable,
292 int renderTargetSampleCnt,
293 SkBudgeted budgeted,
294 GrProtected isProtected,
295 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400296 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400297 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400298 if (renderable == GrRenderable::kYes) {
299 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
300 }
301
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400302 // This desc refers to a texture that will be read by the client. Thus even if msaa is
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400303 // requested, this describes the resolved texture. Therefore we always have samples set
304 // to 1.
305 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400306 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400307 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
308 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
309 // might want to manually set alignment to 4KB for smaller textures
310 resourceDesc.Alignment = 0;
311 resourceDesc.Width = dimensions.fWidth;
312 resourceDesc.Height = dimensions.fHeight;
313 resourceDesc.DepthOrArraySize = 1;
314 resourceDesc.MipLevels = mipLevelCount;
315 resourceDesc.Format = dxgiFormat;
316 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400317 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400318 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400319 resourceDesc.Flags = usageFlags;
320
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400321 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400322 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400323 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400324 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400325 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400326 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400327 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400328 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400329}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400330
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
332 const GrBackendFormat& format,
333 GrRenderable renderable,
334 int renderTargetSampleCnt,
335 SkBudgeted budgeted,
336 GrProtected isProtected,
337 int mipLevelCount,
338 uint32_t levelClearMask) {
339 DXGI_FORMAT dxgiFormat;
340 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
341 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
342
Brian Salomona6db5102020-07-21 09:56:23 -0400343 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
344 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400345
346 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
347 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400348 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400349 if (!tex) {
350 return nullptr;
351 }
352
353 if (levelClearMask) {
354 // TODO
355 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400356
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400357 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500358}
359
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400360static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
361 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
362 UINT* numRows, UINT64* rowSizeInBytes,
363 const void* compressedData, int numMipLevels) {
364 SkASSERT(compressedData && numMipLevels);
365 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
366 SkASSERT(mapPtr);
367
368 const char* src = static_cast<const char*>(compressedData);
369 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
370 // copy data into the buffer, skipping any trailing bytes
371 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
372 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
373 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
374 numRows[currentMipLevel]);
375 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
376 }
377}
378
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500379sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
380 const GrBackendFormat& format,
381 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400382 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500383 GrProtected isProtected,
384 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400385 DXGI_FORMAT dxgiFormat;
386 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
387 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
388
389 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
390 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400391 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400392
393 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400394 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400395 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400396 }
Brian Salomona6db5102020-07-21 09:56:23 -0400397 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
398 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400399
400 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
401 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400402 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400403 if (!d3dTex) {
404 return nullptr;
405 }
406
407 ID3D12Resource* d3dResource = d3dTex->d3dResource();
408 SkASSERT(d3dResource);
409 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
410 // Either upload only the first miplevel or all miplevels
411 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
412
413 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
414 SkAutoTMalloc<UINT> numRows(mipLevelCount);
415 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
416 UINT64 combinedBufferSize;
417 // We reset the width and height in the description to match our subrectangle size
418 // so we don't end up allocating more space than we need.
419 desc.Width = dimensions.width();
420 desc.Height = dimensions.height();
421 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
422 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
423 SkASSERT(combinedBufferSize);
424
Greg Danielcffb0622020-07-16 13:19:17 -0400425 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
426 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
427 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400428 return false;
429 }
Greg Danielcffb0622020-07-16 13:19:17 -0400430
431 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400432
433 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
434 rowSizeInBytes.get(), data, mipLevelCount);
435
Greg Danielcffb0622020-07-16 13:19:17 -0400436 // Update the offsets in the footprints to be relative to the slice's offset
437 for (int i = 0; i < mipLevelCount; ++i) {
438 placedFootprints[i].Offset += slice.fOffset;
439 }
440
Greg Daniel69267912020-07-24 10:42:53 -0400441 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400442 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
443 placedFootprints.get(), 0, 0);
444
445 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500446}
447
Jim Van Verth9145f782020-04-28 12:01:12 -0400448static int get_surface_sample_cnt(GrSurface* surf) {
449 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
450 return rt->numSamples();
451 }
452 return 0;
453}
454
455bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
456 const SkIPoint& dstPoint) {
457
458 if (src->isProtected() && !dst->isProtected()) {
459 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400460 return false;
461 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400462
463 int dstSampleCnt = get_surface_sample_cnt(dst);
464 int srcSampleCnt = get_surface_sample_cnt(src);
465
466 GrD3DTextureResource* dstTexResource;
467 GrD3DTextureResource* srcTexResource;
468 GrRenderTarget* dstRT = dst->asRenderTarget();
469 if (dstRT) {
470 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
471 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
472 } else {
473 SkASSERT(dst->asTexture());
474 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
475 }
476 GrRenderTarget* srcRT = src->asRenderTarget();
477 if (srcRT) {
478 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
479 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
480 } else {
481 SkASSERT(src->asTexture());
482 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
483 }
484
485 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
486 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
487
488 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
489 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
490 return true;
491 }
492
493 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
494 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
495 return true;
496 }
497
498 return false;
499}
500
501void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
502 GrD3DTextureResource* dstResource,
503 GrD3DTextureResource* srcResource,
504 const SkIRect& srcRect, const SkIPoint& dstPoint) {
505#ifdef SK_DEBUG
506 int dstSampleCnt = get_surface_sample_cnt(dst);
507 int srcSampleCnt = get_surface_sample_cnt(src);
508 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
509 DXGI_FORMAT srcFormat;
510 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
511 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
512#endif
513 if (src->isProtected() && !dst->isProtected()) {
514 SkDebugf("Can't copy from protected memory to non-protected");
515 return;
516 }
517
518 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
519 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
520
521 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
522 dstLocation.pResource = dstResource->d3dResource();
523 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
524 dstLocation.SubresourceIndex = 0;
525
526 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
527 srcLocation.pResource = srcResource->d3dResource();
528 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
529 srcLocation.SubresourceIndex = 0;
530
531 D3D12_BOX srcBox = {};
532 srcBox.left = srcRect.fLeft;
533 srcBox.top = srcRect.fTop;
534 srcBox.right = srcRect.fRight;
535 srcBox.bottom = srcRect.fBottom;
536 srcBox.front = 0;
537 srcBox.back = 1;
538 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400539 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
540 &dstLocation,
541 dstPoint.fX, dstPoint.fY,
542 srcResource->resource(),
543 &srcLocation,
544 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400545
546 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
547 srcRect.width(), srcRect.height());
548 // The rect is already in device space so we pass in kTopLeft so no flip is done.
549 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
550}
551
552void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
553 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400554 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
555 SkASSERT(srcRT);
556
557 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
558}
559
560void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
561 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
562 SkASSERT(dst);
563 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
564
565 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
566
567 GrD3DTextureResource* dstTextureResource;
568 GrRenderTarget* dstRT = dst->asRenderTarget();
569 if (dstRT) {
570 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
571 } else {
572 SkASSERT(dst->asTexture());
573 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
574 }
575
576 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
577 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
578
579 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
580 src->msaaTextureResource(), &srcRect);
581}
582
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400583void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400584 SkASSERT(target->numSamples() > 1);
585 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
Brian Salomon72c7b982020-10-06 10:07:38 -0400586 SkASSERT(rt->msaaTextureResource() && rt != rt->msaaTextureResource());
Jim Van Verth275f4192020-07-07 15:36:41 -0400587
588 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400589}
590
Jim Van Verthba7f2292020-04-21 08:56:47 -0400591bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
592 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
593 size_t rowBytes) {
594 SkASSERT(surface);
595
596 if (surfaceColorType != dstColorType) {
597 return false;
598 }
599
600 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400601 GrD3DTextureResource* texResource = nullptr;
602 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
603 if (rt) {
604 texResource = rt;
605 } else {
606 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
607 }
608
609 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400610 return false;
611 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400612
Jim Van Verthba7f2292020-04-21 08:56:47 -0400613 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400614 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400615 SkASSERT(srcLocation.pResource);
616 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
617 srcLocation.SubresourceIndex = 0;
618
619 D3D12_BOX srcBox = {};
620 srcBox.left = left;
621 srcBox.top = top;
622 srcBox.right = left + width;
623 srcBox.bottom = top + height;
624 srcBox.front = 0;
625 srcBox.back = 1;
626
627 // Set up dst location and create transfer buffer
628 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
629 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400630 UINT64 transferTotalBytes;
631 const UINT64 baseOffset = 0;
632 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
633 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400634 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400635 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400636 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400637 if (GrDxgiFormatBytesPerBlock(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400638 return false;
639 }
640 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400641
642 // TODO: implement some way of reusing buffers instead of making a new one every time.
643 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
644 GrGpuBufferType::kXferGpuToCpu,
645 kDynamic_GrAccessPattern);
646 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
647 dstLocation.pResource = d3dBuf->d3dResource();
648
649 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400650 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400651
Greg Daniel69267912020-07-24 10:42:53 -0400652 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
653 texResource->resource(), &srcLocation,
654 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400655 this->submitDirectCommandList(SyncQueue::kForce);
656
657 const void* mappedMemory = transferBuffer->map();
658
659 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400660 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400661
662 transferBuffer->unmap();
663
664 return true;
665}
666
667bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
668 GrColorType surfaceColorType, GrColorType srcColorType,
669 const GrMipLevel texels[], int mipLevelCount,
670 bool prepForTexSampling) {
671 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
672 if (!d3dTex) {
673 return false;
674 }
675
676 // Make sure we have at least the base level
677 if (!mipLevelCount || !texels[0].fPixels) {
678 return false;
679 }
680
681 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
682 bool success = false;
683
684 // Need to change the resource state to COPY_DEST in order to upload to it
685 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
686
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400687 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400688 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
689 mipLevelCount);
690
691 if (prepForTexSampling) {
692 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
693 }
694
695 return success;
696}
697
698bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
699 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
700 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
701 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
702 SkASSERT(1 == mipLevelCount ||
703 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
704
705 // We assume that if the texture has mip levels, we either upload to all the levels or just the
706 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400707 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400708
709 if (width == 0 || height == 0) {
710 return false;
711 }
712
713 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
714 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
715
716 ID3D12Resource* d3dResource = tex->d3dResource();
717 SkASSERT(d3dResource);
718 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
719 // Either upload only the first miplevel or all miplevels
720 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
721
722 if (1 == mipLevelCount && !texels[0].fPixels) {
723 return true; // no data to upload
724 }
725
726 for (int i = 0; i < mipLevelCount; ++i) {
727 // We do not allow any gaps in the mip data
728 if (!texels[i].fPixels) {
729 return false;
730 }
731 }
732
733 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400734 UINT64 combinedBufferSize;
735 // We reset the width and height in the description to match our subrectangle size
736 // so we don't end up allocating more space than we need.
737 desc.Width = width;
738 desc.Height = height;
739 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400740 nullptr, nullptr, &combinedBufferSize);
741 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400742 SkASSERT(combinedBufferSize);
743
Greg Danielcffb0622020-07-16 13:19:17 -0400744 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
745 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
746 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400747 return false;
748 }
Greg Danielcffb0622020-07-16 13:19:17 -0400749
750 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400751
752 int currentWidth = width;
753 int currentHeight = height;
754 int layerHeight = tex->height();
755
756 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
757 if (texels[currentMipLevel].fPixels) {
758 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
759
Jim Van Verthfdd36852020-04-21 16:29:44 -0400760 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400761 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
762
763 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
764
765 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400766 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400767 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
768 src, srcRowBytes, trimRowBytes, currentHeight);
769 }
770 currentWidth = std::max(1, currentWidth / 2);
771 currentHeight = std::max(1, currentHeight / 2);
772 layerHeight = currentHeight;
773 }
774
Greg Danielcffb0622020-07-16 13:19:17 -0400775 // Update the offsets in the footprints to be relative to the slice's offset
776 for (int i = 0; i < mipLevelCount; ++i) {
777 placedFootprints[i].Offset += slice.fOffset;
778 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400779
Greg Daniel69267912020-07-24 10:42:53 -0400780 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400781 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
782 placedFootprints.get(), left, top);
783
784 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400785 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400786 }
787
788 return true;
789}
790
Jim Van Verth9145f782020-04-28 12:01:12 -0400791static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400792 if (!info.fResource.get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400793 return false;
794 }
795 return true;
796}
797
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400798static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
799 if (!caps.isFormatTexturable(info.fFormat)) {
800 return false;
801 }
Brian Salomon718ae762020-09-29 16:52:49 -0400802 // We don't support sampling from multisampled textures.
803 if (info.fSampleCount != 1) {
804 return false;
805 }
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400806 return true;
807}
808
809static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
810 int sampleCnt) {
811 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
812 return false;
813 }
814 return true;
815}
816
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400817sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
818 GrWrapOwnership,
819 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400820 GrIOType ioType) {
821 GrD3DTextureResourceInfo textureInfo;
822 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
823 return nullptr;
824 }
825
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400826 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400827 return nullptr;
828 }
829
830 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
831 return nullptr;
832 }
833
834 // TODO: support protected context
835 if (tex.isProtected()) {
836 return nullptr;
837 }
838
839 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
840 SkASSERT(state);
841 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
842 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500843}
844
845sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400846 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500847 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400848 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500849}
850
851sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
852 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500853 GrWrapOwnership ownership,
854 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400855 GrD3DTextureResourceInfo textureInfo;
856 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
857 return nullptr;
858 }
859
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400860 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400861 return nullptr;
862 }
863
864 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
865 return nullptr;
866 }
867 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
868 return nullptr;
869 }
870
871 // TODO: support protected context
872 if (tex.isProtected()) {
873 return nullptr;
874 }
875
876 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
877
878 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
879 SkASSERT(state);
880
881 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
882 sampleCnt, cacheable,
883 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500884}
885
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400886sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400887 GrD3DTextureResourceInfo info;
888 if (!rt.getD3DTextureResourceInfo(&info)) {
889 return nullptr;
890 }
891
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400892 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400893 return nullptr;
894 }
895
896 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
897 return nullptr;
898 }
899
900 // TODO: support protected context
901 if (rt.isProtected()) {
902 return nullptr;
903 }
904
905 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
906
907 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
Brian Salomon72c7b982020-10-06 10:07:38 -0400908 this, rt.dimensions(), rt.sampleCnt(), info, std::move(state));
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400909
910 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
911 SkASSERT(!rt.stencilBits());
912 if (tgt) {
913 SkASSERT(tgt->canAttemptStencilAttachment());
914 }
915
916 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500917}
918
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500919sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400920 GrAccessPattern accessPattern, const void* data) {
921 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
922 if (data && buffer) {
923 buffer->updateData(data, sizeInBytes);
924 }
925
926 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500927}
928
Greg Danielc0d69152020-10-08 14:59:00 -0400929sk_sp<GrAttachment> GrD3DGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
930 SkISize dimensions,
931 int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400932 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400933 SkASSERT(dimensions.width() >= rt->width());
934 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400935
Greg Daniel8ade5e82020-10-07 13:09:48 -0400936 DXGI_FORMAT sFmt = this->d3dCaps().preferredStencilFormat();
Jim Van Verth4f51f472020-04-13 11:02:21 -0400937
Jim Van Verth4f51f472020-04-13 11:02:21 -0400938 fStats.incStencilAttachmentCreates();
Greg Danielc0d69152020-10-08 14:59:00 -0400939 return GrD3DAttachment::MakeStencil(this, dimensions, numStencilSamples, sFmt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500940}
941
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400942bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
943 SkISize dimensions,
944 GrTexturable texturable,
945 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400946 GrMipmapped mipMapped,
Brian Salomon72c7b982020-10-06 10:07:38 -0400947 int sampleCnt,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400948 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400949 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400950 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400951
952 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
953 return false;
954 }
955
956 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
957 return false;
958 }
959
960 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
961 return false;
962 }
963
964 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400965 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400966 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400967 }
968
969 // create the texture
970 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
971 if (renderable == GrRenderable::kYes) {
972 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
973 }
974
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400975 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400976 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400977 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400978 resourceDesc.Width = dimensions.fWidth;
979 resourceDesc.Height = dimensions.fHeight;
980 resourceDesc.DepthOrArraySize = 1;
981 resourceDesc.MipLevels = numMipLevels;
982 resourceDesc.Format = dxgiFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -0400983 resourceDesc.SampleDesc.Count = sampleCnt;
Jim Van Verth765c5922020-08-10 17:23:50 -0400984 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -0400985 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400986 resourceDesc.Flags = usageFlags;
987
Jim Van Verth280d4f72020-05-04 10:04:24 -0400988 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
989 D3D12_CLEAR_VALUE clearValue = {};
990 if (renderable == GrRenderable::kYes) {
991 clearValue.Format = dxgiFormat;
992 // Assume transparent black
993 clearValue.Color[0] = 0;
994 clearValue.Color[1] = 0;
995 clearValue.Color[2] = 0;
996 clearValue.Color[3] = 0;
997 clearValuePtr = &clearValue;
998 }
999
Greg Daniel16032b32020-05-06 15:31:10 -04001000 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1001 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1002 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001003 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001004 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001005 SkDebugf("Failed to init texture resource info\n");
1006 return false;
1007 }
1008
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001009 return true;
1010}
1011
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001012GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001013 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001014 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001015 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001016 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001017 this->handleDirtyContext();
1018
1019 const GrD3DCaps& caps = this->d3dCaps();
1020
1021 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1022 return {};
1023 }
1024
1025 DXGI_FORMAT dxgiFormat;
1026 if (!format.asDxgiFormat(&dxgiFormat)) {
1027 return {};
1028 }
1029
1030 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1031 if (!caps.isFormatTexturable(dxgiFormat)) {
1032 return {};
1033 }
1034
1035 GrD3DTextureResourceInfo info;
1036 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
Brian Salomon72c7b982020-10-06 10:07:38 -04001037 renderable, mipMapped, 1, &info,
1038 isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001039 return {};
1040 }
1041
1042 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001043}
1044
Greg Daniel0eca74c2020-10-01 13:46:00 -04001045static void copy_src_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001046 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1047 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001048 SkASSERT(srcData && numMipLevels);
1049 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1050 SkASSERT(mapPtr);
1051
Greg Daniel0eca74c2020-10-01 13:46:00 -04001052 size_t bytesPerPixel = GrDxgiFormatBytesPerBlock(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001053
1054 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1055 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1056
1057 // copy data into the buffer, skipping any trailing bytes
1058 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1059 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1060 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1061 trimRowBytes, srcData[currentMipLevel].height());
1062 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001063}
1064
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001065static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1066 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1067 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1068 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001069 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001070 if (colorType == GrColorType::kUnknown) {
1071 return false;
1072 }
1073 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1074 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1075 return false;
1076 }
1077
1078 return true;
1079}
1080
Greg Daniel16032b32020-05-06 15:31:10 -04001081bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1082 sk_sp<GrRefCntedCallback> finishedCallback,
1083 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001084 GrD3DTextureResourceInfo info;
1085 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1086
1087 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1088 SkASSERT(state);
1089 sk_sp<GrD3DTexture> texture =
1090 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1091 GrWrapCacheable::kNo,
1092 kRW_GrIOType, info, std::move(state));
1093 if (!texture) {
1094 return false;
1095 }
1096
1097 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1098 if (!cmdList) {
1099 return false;
1100 }
1101
1102 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1103
1104 ID3D12Resource* d3dResource = texture->d3dResource();
1105 SkASSERT(d3dResource);
1106 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1107 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001108 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001109 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001110 backendTexture.dimensions().height()) + 1;
1111 }
1112 SkASSERT(mipLevelCount == info.fLevelCount);
1113 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1114 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001115 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1116 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001117 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001118 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001119 SkASSERT(combinedBufferSize);
1120 if (data->type() == BackendTextureData::Type::kColor &&
1121 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1122 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1123 combinedBufferSize =
1124 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1125 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1126 placedFootprints[i].Offset = 0;
1127 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1128 }
1129 }
1130
Greg Danielcffb0622020-07-16 13:19:17 -04001131 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1132 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1133 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001134 return false;
1135 }
Greg Danielcffb0622020-07-16 13:19:17 -04001136
1137 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001138 SkASSERT(bufferData);
1139
Jim Van Verth43a6e172020-06-23 11:59:08 -04001140 if (data->type() == BackendTextureData::Type::kPixmaps) {
Greg Daniel0eca74c2020-10-01 13:46:00 -04001141 copy_src_data(bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001142 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001143 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001144 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1145 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001146 } else {
1147 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1148 SkImage::CompressionType compression =
1149 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1150 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001151 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1152 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001153 return false;
1154 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001155 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001156 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1157 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001158 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001159 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001160 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001161 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001162 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1163 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001164 }
1165 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001166
Greg Danielcffb0622020-07-16 13:19:17 -04001167 // Update the offsets in the footprints to be relative to the slice's offset
1168 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1169 placedFootprints[i].Offset += slice.fOffset;
1170 }
1171
Greg Daniel69267912020-07-24 10:42:53 -04001172 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001173 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1174 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001175
1176 if (finishedCallback) {
1177 this->addFinishedCallback(std::move(finishedCallback));
1178 }
1179
Greg Daniel16032b32020-05-06 15:31:10 -04001180 return true;
1181}
1182
Greg Danielc1ad77c2020-05-06 11:40:03 -04001183GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001184 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001185 GrProtected isProtected) {
1186 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1187 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001188}
1189
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001190bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001191 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001192 const BackendTextureData* data) {
1193 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001194}
1195
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001196void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001197 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1198 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001199}
1200
Robert Phillips979b2232020-02-20 10:47:29 -05001201bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1202 return false;
1203}
1204
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001205#if GR_TEST_UTILS
1206bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001207 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1208
1209 GrD3DTextureResourceInfo info;
1210 if (!tex.getD3DTextureResourceInfo(&info)) {
1211 return false;
1212 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001213 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001214 if (!textureResource) {
1215 return false;
1216 }
1217 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001218}
1219
Brian Salomon72c7b982020-10-06 10:07:38 -04001220GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
1221 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04001222 int sampleCnt,
1223 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001224 this->handleDirtyContext();
1225
Brian Salomon72c7b982020-10-06 10:07:38 -04001226 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
1227 dimensions.height() > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001228 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001229 }
1230
1231 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1232
1233 GrD3DTextureResourceInfo info;
Brian Salomon72c7b982020-10-06 10:07:38 -04001234 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001235 GrRenderable::kYes, GrMipmapped::kNo,
Brian Salomonf9b00422020-10-08 16:00:14 -04001236 sampleCnt, &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001237 return {};
1238 }
1239
Brian Salomon72c7b982020-10-06 10:07:38 -04001240 return GrBackendRenderTarget(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001241}
1242
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001243void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1244 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1245
1246 GrD3DTextureResourceInfo info;
1247 if (rt.getD3DTextureResourceInfo(&info)) {
1248 this->testingOnly_flushGpuAndSync();
1249 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1250 // is deleted.
1251 }
1252}
1253
1254void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001255 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001256}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001257
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001258void GrD3DGpu::testingOnly_startCapture() {
1259 if (fGraphicsAnalysis) {
1260 fGraphicsAnalysis->BeginCapture();
1261 }
1262}
1263
1264void GrD3DGpu::testingOnly_endCapture() {
1265 if (fGraphicsAnalysis) {
1266 fGraphicsAnalysis->EndCapture();
1267 }
1268}
1269#endif
1270
Jim Van Verthc632aa62020-04-17 16:58:20 -04001271///////////////////////////////////////////////////////////////////////////////
1272
Greg Daniela5a6b322020-04-23 12:52:27 -04001273void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001274 int numBarriers,
1275 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1276 SkASSERT(fCurrentDirectCommandList);
1277 SkASSERT(resource);
1278
Greg Daniela5a6b322020-04-23 12:52:27 -04001279 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001280}
1281
Greg Daniel69267912020-07-24 10:42:53 -04001282void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1283 int numBarriers,
1284 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1285 SkASSERT(fCurrentDirectCommandList);
1286 SkASSERT(buffer);
1287
1288 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1289 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1290}
1291
1292
Greg Daniel9efe3862020-06-11 11:51:06 -04001293void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001294 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -04001295 SkSurface::BackendSurfaceAccess access,
1296 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001297 // prepare proxies by transitioning to PRESENT renderState
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001298 if (!proxies.empty() && access == SkSurface::BackendSurfaceAccess::kPresent) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001299 GrD3DTextureResource* resource;
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001300 for (GrSurfaceProxy* proxy : proxies) {
1301 SkASSERT(proxy->isInstantiated());
1302 if (GrTexture* tex = proxy->peekTexture()) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001303 resource = static_cast<GrD3DTexture*>(tex);
1304 } else {
Adlai Hollerc2bfcff2020-11-06 15:39:36 -05001305 GrRenderTarget* rt = proxy->peekRenderTarget();
Jim Van Verth682a2f42020-05-13 16:54:09 -04001306 SkASSERT(rt);
1307 resource = static_cast<GrD3DRenderTarget*>(rt);
1308 }
1309 resource->prepareForPresent(this);
1310 }
1311 }
1312}
1313
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001314void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001315 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001316}
1317
Jim Van Verthc632aa62020-04-17 16:58:20 -04001318bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1319 if (syncCpu) {
1320 return this->submitDirectCommandList(SyncQueue::kForce);
1321 } else {
1322 return this->submitDirectCommandList(SyncQueue::kSkip);
1323 }
1324}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001325
1326std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1327 return GrD3DSemaphore::Make(this);
1328}
1329std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1330 const GrBackendSemaphore& semaphore,
1331 GrResourceProvider::SemaphoreWrapType,
1332 GrWrapOwnership) {
1333 SkASSERT(this->caps()->semaphoreSupport());
1334 GrD3DFenceInfo fenceInfo;
1335 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1336 return nullptr;
1337 }
1338 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1339}
1340
1341void GrD3DGpu::insertSemaphore(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? How do we know it's done?
1345 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1346}
1347
1348void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001349 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001350 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1351 // TODO: Do we need to track the lifetime of this?
1352 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1353}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001354
1355GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001356 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001357 return fCurrentFenceValue;
1358}
1359
1360bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001361 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001362}