blob: 558412e258208beadfb70b47e299147adf278d07 [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 Verthd6ad4802020-04-03 14:59:20 -040017#include "src/gpu/d3d/GrD3DBuffer.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050018#include "src/gpu/d3d/GrD3DCaps.h"
19#include "src/gpu/d3d/GrD3DOpsRenderPass.h"
Jim Van Verthc1a67b52020-06-25 13:10:29 -040020#include "src/gpu/d3d/GrD3DSemaphore.h"
Jim Van Verth4f51f472020-04-13 11:02:21 -040021#include "src/gpu/d3d/GrD3DStencilAttachment.h"
Jim Van Verthaa90dad2020-03-30 15:00:39 -040022#include "src/gpu/d3d/GrD3DTexture.h"
23#include "src/gpu/d3d/GrD3DTextureRenderTarget.h"
24#include "src/gpu/d3d/GrD3DUtil.h"
Greg Daniel5fc5c812020-04-23 10:30:23 -040025#include "src/sksl/SkSLCompiler.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050026
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040027#if GR_TEST_UTILS
28#include <DXProgrammableCapture.h>
29#endif
30
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050031sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext,
Adlai Holler3d0359a2020-07-09 15:35:55 -040032 const GrContextOptions& contextOptions, GrDirectContext* direct) {
33 return sk_sp<GrGpu>(new GrD3DGpu(direct, contextOptions, backendContext));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050034}
35
Greg Daniel83ed2132020-03-24 13:15:33 -040036// This constant determines how many OutstandingCommandLists are allocated together as a block in
37// the deque. As such it needs to balance allocating too much memory vs. incurring
38// allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding
39// command lists we expect to see.
40static const int kDefaultOutstandingAllocCnt = 8;
41
Jim Van Verth711b0392020-07-23 16:45:00 -040042// constants have to be aligned to 256
43constexpr int kConstantAlignment = 256;
44
Adlai Holler3d0359a2020-07-09 15:35:55 -040045GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050046 const GrD3DBackendContext& backendContext)
Adlai Holler3d0359a2020-07-09 15:35:55 -040047 : INHERITED(direct)
Jim Van Verth03b8ab22020-02-24 11:36:15 -050048 , fDevice(backendContext.fDevice)
Greg Daniel02c45902020-03-09 10:58:09 -040049 , fQueue(backendContext.fQueue)
Greg Daniel83ed2132020-03-24 13:15:33 -040050 , fResourceProvider(this)
Greg Danielcffb0622020-07-16 13:19:17 -040051 , fStagingBufferManager(this)
Jim Van Verth711b0392020-07-23 16:45:00 -040052 , fConstantsRingBuffer(this, 128 * 1024, kConstantAlignment, GrGpuBufferType::kVertex)
Greg Daniel5fc5c812020-04-23 10:30:23 -040053 , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt)
54 , fCompiler(new SkSL::Compiler()) {
Jim Van Verth8ec13302020-02-26 12:59:56 -050055 fCaps.reset(new GrD3DCaps(contextOptions,
Jim Van Verth9aa9a682020-04-01 10:13:48 -040056 backendContext.fAdapter.get(),
57 backendContext.fDevice.get()));
Greg Daniel85da3362020-03-09 15:18:35 -040058
59 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
Greg Daniele52c9782020-03-23 14:18:37 -040060 SkASSERT(fCurrentDirectCommandList);
Greg Daniel83ed2132020-03-24 13:15:33 -040061
62 SkASSERT(fCurrentFenceValue == 0);
Jim Van Verthe3810362020-07-01 10:12:11 -040063 GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE,
64 IID_PPV_ARGS(&fFence)));
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040065
66#if GR_TEST_UTILS
67 HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis));
68 if (FAILED(getAnalysis)) {
69 fGraphicsAnalysis = nullptr;
70 }
71#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050072}
73
Greg Daniel83ed2132020-03-24 13:15:33 -040074GrD3DGpu::~GrD3DGpu() {
75 this->destroyResources();
76}
77
78void GrD3DGpu::destroyResources() {
79 if (fCurrentDirectCommandList) {
80 fCurrentDirectCommandList->close();
Jim Van Verthba7f2292020-04-21 08:56:47 -040081 fCurrentDirectCommandList->reset();
Greg Daniel83ed2132020-03-24 13:15:33 -040082 }
83
84 // We need to make sure everything has finished on the queue.
Jim Van Verth3b0d7d12020-07-06 11:52:42 -040085 this->waitForQueueCompletion();
Greg Daniel83ed2132020-03-24 13:15:33 -040086
87 SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();)
88
89 // We used a placement new for each object in fOutstandingCommandLists, so we're responsible
90 // for calling the destructor on each of them as well.
91 while (!fOutstandingCommandLists.empty()) {
Jim Van Verthf43da142020-06-09 16:34:43 -040092 OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -040093 SkASSERT(list->fFenceValue <= fenceValue);
94 // No reason to recycle the command lists since we are destroying all resources anyways.
95 list->~OutstandingCommandList();
Jim Van Verthf43da142020-06-09 16:34:43 -040096 fOutstandingCommandLists.pop_front();
Greg Daniel83ed2132020-03-24 13:15:33 -040097 }
Jim Van Verthf2788862020-06-03 17:33:12 -040098
Greg Danielcffb0622020-07-16 13:19:17 -040099 fStagingBufferManager.reset();
100
Jim Van Verthf2788862020-06-03 17:33:12 -0400101 fResourceProvider.destroyResources();
Greg Daniel83ed2132020-03-24 13:15:33 -0400102}
Greg Daniel31a7b072020-02-26 15:31:49 -0500103
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500104GrOpsRenderPass* GrD3DGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -0400105 GrRenderTarget* rt, GrStencilAttachment*,
106 GrSurfaceOrigin origin, const SkIRect& bounds,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500107 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Daniel31a7b072020-02-26 15:31:49 -0500108 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500109 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500110 if (!fCachedOpsRenderPass) {
111 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
112 }
113
114 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
115 return nullptr;
116 }
117 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500118}
119
Jim Van Verthc632aa62020-04-17 16:58:20 -0400120bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400121 SkASSERT(fCurrentDirectCommandList);
122
Jim Van Verth3eadce22020-06-01 11:34:49 -0400123 fResourceProvider.prepForSubmit();
124
Jim Van Verthc632aa62020-04-17 16:58:20 -0400125 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
126 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
127 return false;
128 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
129 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400130 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400131 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400132 }
133 return true;
134 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400135
Greg Daniela581a8b2020-06-19 15:22:18 -0400136 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
137 // uniform data as dirty.
138 fResourceProvider.markPipelineStateUniformsDirty();
139
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400140 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400141 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400142 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400143
Jim Van Verthc632aa62020-04-17 16:58:20 -0400144 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400145 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400146 }
147
Greg Daniel83ed2132020-03-24 13:15:33 -0400148 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
149
150 // This should be done after we have a new command list in case the freeing of any resources
151 // held by a finished command list causes us send a new command to the gpu (like changing the
152 // resource state.
153 this->checkForFinishedCommandLists();
154
155 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400156 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400157}
158
159void GrD3DGpu::checkForFinishedCommandLists() {
160 uint64_t currentFenceValue = fFence->GetCompletedValue();
161
162 // Iterate over all the outstanding command lists to see if any have finished. The commands
163 // lists are in order from oldest to newest, so we start at the front to check if their fence
164 // value is less than the last signaled value. If so we pop it off and move onto the next.
165 // Repeat till we find a command list that has not finished yet (and all others afterwards are
166 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400167 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
168 while (front && front->fFenceValue <= currentFenceValue) {
169 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400170 // Since we used placement new we are responsible for calling the destructor manually.
171 front->~OutstandingCommandList();
172 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400173 fResourceProvider.recycleDirectCommandList(std::move(currList));
174 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400175 }
176}
177
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400178void GrD3DGpu::waitForQueueCompletion() {
179 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
180 HANDLE fenceEvent;
181 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
182 SkASSERT(fenceEvent);
183 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
184 WaitForSingleObject(fenceEvent, INFINITE);
185 CloseHandle(fenceEvent);
186 }
187}
188
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500189void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400190 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
191
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500192 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400193 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500194}
195
Greg Danield4928d02020-06-19 11:13:26 -0400196void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
197 GrGpuFinishedContext finishedContext) {
198 SkASSERT(finishedProc);
199 sk_sp<GrRefCntedCallback> finishedCallback(
200 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400201 this->addFinishedCallback(std::move(finishedCallback));
202}
203
204void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
205 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400206 // Besides the current command list, we also add the finishedCallback to the newest outstanding
207 // command list. Our contract for calling the proc is that all previous submitted command lists
208 // have finished when we call it. However, if our current command list has no work when it is
209 // flushed it will drop its ref to the callback immediately. But the previous work may not have
210 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
211 // must finish after all previously submitted command lists.
212 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
213 if (back) {
214 back->fCommandList->addFinishedCallback(finishedCallback);
215 }
216 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
217}
218
Jim Van Verth765c5922020-08-10 17:23:50 -0400219void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
220 SkTArray<SkPoint>* sampleLocations) {
221 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
222 // These are transformed from D3D's integer coordinate system with origin at the center,
223 // to our normalized coordinate system with origin at the upper left.
224 // This ends up corresponding with Vulkan's sample locations.
225 SkASSERT(this->caps()->sampleLocationsSupport());
226 static constexpr SkPoint kStandardSampleLocations_1[1] = {
227 {0.5f, 0.5f} };
228 static constexpr SkPoint kStandardSampleLocations_2[2] = {
229 {0.75f, 0.75f}, {0.25f, 0.25f} };
230 static constexpr SkPoint kStandardSampleLocations_4[4] = {
231 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
232 static constexpr SkPoint kStandardSampleLocations_8[8] = {
233 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
234 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
235 static constexpr SkPoint kStandardSampleLocations_16[16] = {
236 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
237 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
238 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
239 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
240
241 int numSamples = renderTarget->numSamples();
242 // TODO: support mixed samples?
243 SkASSERT(numSamples > 1);
244 SkASSERT(!renderTarget->getStencilAttachment() ||
245 numSamples == renderTarget->getStencilAttachment()->numSamples());
246
247 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
248 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
249 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
250 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
251 return;
252 }
253 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
254
255 switch (numSamples) {
256 case 2:
257 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
258 break;
259 case 4:
260 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
261 break;
262 case 8:
263 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
264 break;
265 case 16:
266 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
267 break;
268 default:
269 SK_ABORT("Invalid sample count.");
270 break;
271 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500272}
273
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400274sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
275 DXGI_FORMAT dxgiFormat,
276 GrRenderable renderable,
277 int renderTargetSampleCnt,
278 SkBudgeted budgeted,
279 GrProtected isProtected,
280 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400281 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400282 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400283 if (renderable == GrRenderable::kYes) {
284 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
285 }
286
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400287 // 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 -0400288 // requested, this describes the resolved texture. Therefore we always have samples set
289 // to 1.
290 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400291 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400292 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
293 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
294 // might want to manually set alignment to 4KB for smaller textures
295 resourceDesc.Alignment = 0;
296 resourceDesc.Width = dimensions.fWidth;
297 resourceDesc.Height = dimensions.fHeight;
298 resourceDesc.DepthOrArraySize = 1;
299 resourceDesc.MipLevels = mipLevelCount;
300 resourceDesc.Format = dxgiFormat;
301 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400302 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400303 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400304 resourceDesc.Flags = usageFlags;
305
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400306 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400307 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400308 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400309 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400310 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400311 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400312 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400313 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400314}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400315
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400316sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
317 const GrBackendFormat& format,
318 GrRenderable renderable,
319 int renderTargetSampleCnt,
320 SkBudgeted budgeted,
321 GrProtected isProtected,
322 int mipLevelCount,
323 uint32_t levelClearMask) {
324 DXGI_FORMAT dxgiFormat;
325 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
326 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
327
Brian Salomona6db5102020-07-21 09:56:23 -0400328 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
329 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400330
331 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
332 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400333 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400334 if (!tex) {
335 return nullptr;
336 }
337
338 if (levelClearMask) {
339 // TODO
340 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400341
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400342 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500343}
344
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400345static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
346 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
347 UINT* numRows, UINT64* rowSizeInBytes,
348 const void* compressedData, int numMipLevels) {
349 SkASSERT(compressedData && numMipLevels);
350 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
351 SkASSERT(mapPtr);
352
353 const char* src = static_cast<const char*>(compressedData);
354 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
355 // copy data into the buffer, skipping any trailing bytes
356 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
357 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
358 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
359 numRows[currentMipLevel]);
360 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
361 }
362}
363
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500364sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
365 const GrBackendFormat& format,
366 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400367 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500368 GrProtected isProtected,
369 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400370 DXGI_FORMAT dxgiFormat;
371 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
372 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
373
374 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
375 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400376 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400377
378 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400379 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400380 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400381 }
Brian Salomona6db5102020-07-21 09:56:23 -0400382 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
383 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400384
385 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
386 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400387 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400388 if (!d3dTex) {
389 return nullptr;
390 }
391
392 ID3D12Resource* d3dResource = d3dTex->d3dResource();
393 SkASSERT(d3dResource);
394 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
395 // Either upload only the first miplevel or all miplevels
396 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
397
398 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
399 SkAutoTMalloc<UINT> numRows(mipLevelCount);
400 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
401 UINT64 combinedBufferSize;
402 // We reset the width and height in the description to match our subrectangle size
403 // so we don't end up allocating more space than we need.
404 desc.Width = dimensions.width();
405 desc.Height = dimensions.height();
406 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
407 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
408 SkASSERT(combinedBufferSize);
409
Greg Danielcffb0622020-07-16 13:19:17 -0400410 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
411 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
412 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400413 return false;
414 }
Greg Danielcffb0622020-07-16 13:19:17 -0400415
416 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400417
418 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
419 rowSizeInBytes.get(), data, mipLevelCount);
420
Greg Danielcffb0622020-07-16 13:19:17 -0400421 // Update the offsets in the footprints to be relative to the slice's offset
422 for (int i = 0; i < mipLevelCount; ++i) {
423 placedFootprints[i].Offset += slice.fOffset;
424 }
425
Greg Daniel69267912020-07-24 10:42:53 -0400426 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400427 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
428 placedFootprints.get(), 0, 0);
429
430 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500431}
432
Jim Van Verth9145f782020-04-28 12:01:12 -0400433static int get_surface_sample_cnt(GrSurface* surf) {
434 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
435 return rt->numSamples();
436 }
437 return 0;
438}
439
440bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
441 const SkIPoint& dstPoint) {
442
443 if (src->isProtected() && !dst->isProtected()) {
444 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400445 return false;
446 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400447
448 int dstSampleCnt = get_surface_sample_cnt(dst);
449 int srcSampleCnt = get_surface_sample_cnt(src);
450
451 GrD3DTextureResource* dstTexResource;
452 GrD3DTextureResource* srcTexResource;
453 GrRenderTarget* dstRT = dst->asRenderTarget();
454 if (dstRT) {
455 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
456 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
457 } else {
458 SkASSERT(dst->asTexture());
459 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
460 }
461 GrRenderTarget* srcRT = src->asRenderTarget();
462 if (srcRT) {
463 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
464 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
465 } else {
466 SkASSERT(src->asTexture());
467 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
468 }
469
470 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
471 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
472
473 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
474 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
475 return true;
476 }
477
478 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
479 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
480 return true;
481 }
482
483 return false;
484}
485
486void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
487 GrD3DTextureResource* dstResource,
488 GrD3DTextureResource* srcResource,
489 const SkIRect& srcRect, const SkIPoint& dstPoint) {
490#ifdef SK_DEBUG
491 int dstSampleCnt = get_surface_sample_cnt(dst);
492 int srcSampleCnt = get_surface_sample_cnt(src);
493 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
494 DXGI_FORMAT srcFormat;
495 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
496 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
497#endif
498 if (src->isProtected() && !dst->isProtected()) {
499 SkDebugf("Can't copy from protected memory to non-protected");
500 return;
501 }
502
503 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
504 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
505
506 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
507 dstLocation.pResource = dstResource->d3dResource();
508 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
509 dstLocation.SubresourceIndex = 0;
510
511 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
512 srcLocation.pResource = srcResource->d3dResource();
513 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
514 srcLocation.SubresourceIndex = 0;
515
516 D3D12_BOX srcBox = {};
517 srcBox.left = srcRect.fLeft;
518 srcBox.top = srcRect.fTop;
519 srcBox.right = srcRect.fRight;
520 srcBox.bottom = srcRect.fBottom;
521 srcBox.front = 0;
522 srcBox.back = 1;
523 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400524 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
525 &dstLocation,
526 dstPoint.fX, dstPoint.fY,
527 srcResource->resource(),
528 &srcLocation,
529 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400530
531 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
532 srcRect.width(), srcRect.height());
533 // The rect is already in device space so we pass in kTopLeft so no flip is done.
534 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
535}
536
537void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
538 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400539 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
540 SkASSERT(srcRT);
541
542 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
543}
544
545void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
546 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
547 SkASSERT(dst);
548 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
549
550 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
551
552 GrD3DTextureResource* dstTextureResource;
553 GrRenderTarget* dstRT = dst->asRenderTarget();
554 if (dstRT) {
555 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
556 } else {
557 SkASSERT(dst->asTexture());
558 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
559 }
560
561 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
562 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
563
564 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
565 src->msaaTextureResource(), &srcRect);
566}
567
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400568void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400569 SkASSERT(target->numSamples() > 1);
570 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
571 SkASSERT(rt->msaaTextureResource());
572
573 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400574}
575
Jim Van Verthba7f2292020-04-21 08:56:47 -0400576bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
577 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
578 size_t rowBytes) {
579 SkASSERT(surface);
580
581 if (surfaceColorType != dstColorType) {
582 return false;
583 }
584
585 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400586 GrD3DTextureResource* texResource = nullptr;
587 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
588 if (rt) {
589 texResource = rt;
590 } else {
591 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
592 }
593
594 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400595 return false;
596 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400597
Jim Van Verthba7f2292020-04-21 08:56:47 -0400598 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400599 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400600 SkASSERT(srcLocation.pResource);
601 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
602 srcLocation.SubresourceIndex = 0;
603
604 D3D12_BOX srcBox = {};
605 srcBox.left = left;
606 srcBox.top = top;
607 srcBox.right = left + width;
608 srcBox.bottom = top + height;
609 srcBox.front = 0;
610 srcBox.back = 1;
611
612 // Set up dst location and create transfer buffer
613 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
614 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400615 UINT64 transferTotalBytes;
616 const UINT64 baseOffset = 0;
617 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
618 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400619 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400620 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400621 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400622 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400623 return false;
624 }
625 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400626
627 // TODO: implement some way of reusing buffers instead of making a new one every time.
628 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
629 GrGpuBufferType::kXferGpuToCpu,
630 kDynamic_GrAccessPattern);
631 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
632 dstLocation.pResource = d3dBuf->d3dResource();
633
634 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400635 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400636
Greg Daniel69267912020-07-24 10:42:53 -0400637 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
638 texResource->resource(), &srcLocation,
639 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400640 this->submitDirectCommandList(SyncQueue::kForce);
641
642 const void* mappedMemory = transferBuffer->map();
643
644 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400645 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400646
647 transferBuffer->unmap();
648
649 return true;
650}
651
652bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
653 GrColorType surfaceColorType, GrColorType srcColorType,
654 const GrMipLevel texels[], int mipLevelCount,
655 bool prepForTexSampling) {
656 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
657 if (!d3dTex) {
658 return false;
659 }
660
661 // Make sure we have at least the base level
662 if (!mipLevelCount || !texels[0].fPixels) {
663 return false;
664 }
665
666 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
667 bool success = false;
668
669 // Need to change the resource state to COPY_DEST in order to upload to it
670 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
671
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400672 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400673 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
674 mipLevelCount);
675
676 if (prepForTexSampling) {
677 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
678 }
679
680 return success;
681}
682
683bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
684 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
685 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
686 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
687 SkASSERT(1 == mipLevelCount ||
688 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
689
690 // We assume that if the texture has mip levels, we either upload to all the levels or just the
691 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400692 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400693
694 if (width == 0 || height == 0) {
695 return false;
696 }
697
698 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
699 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
700
701 ID3D12Resource* d3dResource = tex->d3dResource();
702 SkASSERT(d3dResource);
703 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
704 // Either upload only the first miplevel or all miplevels
705 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
706
707 if (1 == mipLevelCount && !texels[0].fPixels) {
708 return true; // no data to upload
709 }
710
711 for (int i = 0; i < mipLevelCount; ++i) {
712 // We do not allow any gaps in the mip data
713 if (!texels[i].fPixels) {
714 return false;
715 }
716 }
717
718 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400719 UINT64 combinedBufferSize;
720 // We reset the width and height in the description to match our subrectangle size
721 // so we don't end up allocating more space than we need.
722 desc.Width = width;
723 desc.Height = height;
724 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400725 nullptr, nullptr, &combinedBufferSize);
726 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400727 SkASSERT(combinedBufferSize);
728
Greg Danielcffb0622020-07-16 13:19:17 -0400729 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
730 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
731 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400732 return false;
733 }
Greg Danielcffb0622020-07-16 13:19:17 -0400734
735 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400736
737 int currentWidth = width;
738 int currentHeight = height;
739 int layerHeight = tex->height();
740
741 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
742 if (texels[currentMipLevel].fPixels) {
743 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
744
Jim Van Verthfdd36852020-04-21 16:29:44 -0400745 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400746 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
747
748 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
749
750 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400751 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400752 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
753 src, srcRowBytes, trimRowBytes, currentHeight);
754 }
755 currentWidth = std::max(1, currentWidth / 2);
756 currentHeight = std::max(1, currentHeight / 2);
757 layerHeight = currentHeight;
758 }
759
Greg Danielcffb0622020-07-16 13:19:17 -0400760 // Update the offsets in the footprints to be relative to the slice's offset
761 for (int i = 0; i < mipLevelCount; ++i) {
762 placedFootprints[i].Offset += slice.fOffset;
763 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400764
Greg Daniel69267912020-07-24 10:42:53 -0400765 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400766 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
767 placedFootprints.get(), left, top);
768
769 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400770 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400771 }
772
773 return true;
774}
775
Jim Van Verth9145f782020-04-28 12:01:12 -0400776static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
777 if (!info.fResource.get()) {
778 return false;
779 }
780 return true;
781}
782
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400783static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
784 if (!caps.isFormatTexturable(info.fFormat)) {
785 return false;
786 }
787 return true;
788}
789
790static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
791 int sampleCnt) {
792 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
793 return false;
794 }
795 return true;
796}
797
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400798sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
799 GrWrapOwnership,
800 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400801 GrIOType ioType) {
802 GrD3DTextureResourceInfo textureInfo;
803 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
804 return nullptr;
805 }
806
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400807 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400808 return nullptr;
809 }
810
811 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
812 return nullptr;
813 }
814
815 // TODO: support protected context
816 if (tex.isProtected()) {
817 return nullptr;
818 }
819
820 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
821 SkASSERT(state);
822 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
823 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500824}
825
826sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400827 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500828 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400829 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500830}
831
832sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
833 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500834 GrWrapOwnership ownership,
835 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400836 GrD3DTextureResourceInfo textureInfo;
837 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
838 return nullptr;
839 }
840
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400841 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400842 return nullptr;
843 }
844
845 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
846 return nullptr;
847 }
848 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
849 return nullptr;
850 }
851
852 // TODO: support protected context
853 if (tex.isProtected()) {
854 return nullptr;
855 }
856
857 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
858
859 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
860 SkASSERT(state);
861
862 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
863 sampleCnt, cacheable,
864 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500865}
866
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400867sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400868 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
869 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
870 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
871 // creating and owning the MSAA images.
872 if (rt.sampleCnt() > 1) {
873 return nullptr;
874 }
875
876 GrD3DTextureResourceInfo info;
877 if (!rt.getD3DTextureResourceInfo(&info)) {
878 return nullptr;
879 }
880
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400881 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400882 return nullptr;
883 }
884
885 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
886 return nullptr;
887 }
888
889 // TODO: support protected context
890 if (rt.isProtected()) {
891 return nullptr;
892 }
893
894 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
895
896 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
897 this, rt.dimensions(), 1, info, std::move(state));
898
899 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
900 SkASSERT(!rt.stencilBits());
901 if (tgt) {
902 SkASSERT(tgt->canAttemptStencilAttachment());
903 }
904
905 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500906}
907
908sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400909 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400910
911 GrD3DTextureResourceInfo textureInfo;
912 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
913 return nullptr;
914 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400915 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400916 return nullptr;
917 }
918
919 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
920 return nullptr;
921 }
922
923 // TODO: support protected context
924 if (tex.isProtected()) {
925 return nullptr;
926 }
927
928 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
929 if (!sampleCnt) {
930 return nullptr;
931 }
932
933 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
934 SkASSERT(state);
935
936 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
937 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500938}
939
940sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400941 GrAccessPattern accessPattern, const void* data) {
942 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
943 if (data && buffer) {
944 buffer->updateData(data, sizeInBytes);
945 }
946
947 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500948}
949
950GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
951 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400952 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
953 SkASSERT(width >= rt->width());
954 SkASSERT(height >= rt->height());
955
956 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
957
958 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
959 width,
960 height,
961 numStencilSamples,
962 sFmt));
963 fStats.incStencilAttachmentCreates();
964 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500965}
966
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400967bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
968 SkISize dimensions,
969 GrTexturable texturable,
970 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400971 GrMipmapped mipMapped,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400972 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400973 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400974 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400975
976 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
977 return false;
978 }
979
980 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
981 return false;
982 }
983
984 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
985 return false;
986 }
987
988 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400989 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400990 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400991 }
992
993 // create the texture
994 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
995 if (renderable == GrRenderable::kYes) {
996 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
997 }
998
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400999 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001000 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -04001001 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001002 resourceDesc.Width = dimensions.fWidth;
1003 resourceDesc.Height = dimensions.fHeight;
1004 resourceDesc.DepthOrArraySize = 1;
1005 resourceDesc.MipLevels = numMipLevels;
1006 resourceDesc.Format = dxgiFormat;
1007 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -04001008 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -04001009 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001010 resourceDesc.Flags = usageFlags;
1011
Jim Van Verth280d4f72020-05-04 10:04:24 -04001012 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
1013 D3D12_CLEAR_VALUE clearValue = {};
1014 if (renderable == GrRenderable::kYes) {
1015 clearValue.Format = dxgiFormat;
1016 // Assume transparent black
1017 clearValue.Color[0] = 0;
1018 clearValue.Color[1] = 0;
1019 clearValue.Color[2] = 0;
1020 clearValue.Color[3] = 0;
1021 clearValuePtr = &clearValue;
1022 }
1023
Greg Daniel16032b32020-05-06 15:31:10 -04001024 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1025 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1026 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001027 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001028 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001029 SkDebugf("Failed to init texture resource info\n");
1030 return false;
1031 }
1032
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001033 return true;
1034}
1035
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001036GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001037 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001038 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001039 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001040 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001041 this->handleDirtyContext();
1042
1043 const GrD3DCaps& caps = this->d3dCaps();
1044
1045 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1046 return {};
1047 }
1048
1049 DXGI_FORMAT dxgiFormat;
1050 if (!format.asDxgiFormat(&dxgiFormat)) {
1051 return {};
1052 }
1053
1054 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1055 if (!caps.isFormatTexturable(dxgiFormat)) {
1056 return {};
1057 }
1058
1059 GrD3DTextureResourceInfo info;
1060 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1061 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001062 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001063 return {};
1064 }
1065
1066 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001067}
1068
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001069static void copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
1070 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1071 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001072 SkASSERT(srcData && numMipLevels);
1073 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1074 SkASSERT(mapPtr);
1075
1076 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
1077
1078 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1079 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1080
1081 // copy data into the buffer, skipping any trailing bytes
1082 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1083 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1084 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1085 trimRowBytes, srcData[currentMipLevel].height());
1086 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001087}
1088
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001089static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1090 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1091 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1092 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001093 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001094 if (colorType == GrColorType::kUnknown) {
1095 return false;
1096 }
1097 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1098 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1099 return false;
1100 }
1101
1102 return true;
1103}
1104
Greg Daniel16032b32020-05-06 15:31:10 -04001105bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1106 sk_sp<GrRefCntedCallback> finishedCallback,
1107 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001108 GrD3DTextureResourceInfo info;
1109 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1110
1111 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1112 SkASSERT(state);
1113 sk_sp<GrD3DTexture> texture =
1114 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1115 GrWrapCacheable::kNo,
1116 kRW_GrIOType, info, std::move(state));
1117 if (!texture) {
1118 return false;
1119 }
1120
1121 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1122 if (!cmdList) {
1123 return false;
1124 }
1125
1126 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1127
1128 ID3D12Resource* d3dResource = texture->d3dResource();
1129 SkASSERT(d3dResource);
1130 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1131 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001132 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001133 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001134 backendTexture.dimensions().height()) + 1;
1135 }
1136 SkASSERT(mipLevelCount == info.fLevelCount);
1137 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1138 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001139 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1140 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001141 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001142 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001143 SkASSERT(combinedBufferSize);
1144 if (data->type() == BackendTextureData::Type::kColor &&
1145 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1146 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1147 combinedBufferSize =
1148 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1149 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1150 placedFootprints[i].Offset = 0;
1151 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1152 }
1153 }
1154
Greg Danielcffb0622020-07-16 13:19:17 -04001155 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1156 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1157 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001158 return false;
1159 }
Greg Danielcffb0622020-07-16 13:19:17 -04001160
1161 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001162 SkASSERT(bufferData);
1163
Jim Van Verth43a6e172020-06-23 11:59:08 -04001164 if (data->type() == BackendTextureData::Type::kPixmaps) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001165 copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
1166 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001167 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001168 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1169 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001170 } else {
1171 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1172 SkImage::CompressionType compression =
1173 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1174 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001175 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1176 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001177 return false;
1178 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001179 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001180 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1181 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001182 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001183 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001184 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001185 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001186 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1187 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001188 }
1189 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001190
Greg Danielcffb0622020-07-16 13:19:17 -04001191 // Update the offsets in the footprints to be relative to the slice's offset
1192 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1193 placedFootprints[i].Offset += slice.fOffset;
1194 }
1195
Greg Daniel69267912020-07-24 10:42:53 -04001196 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001197 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1198 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001199
1200 if (finishedCallback) {
1201 this->addFinishedCallback(std::move(finishedCallback));
1202 }
1203
Greg Daniel16032b32020-05-06 15:31:10 -04001204 return true;
1205}
1206
Greg Danielc1ad77c2020-05-06 11:40:03 -04001207GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001208 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001209 GrProtected isProtected) {
1210 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1211 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001212}
1213
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001214bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001215 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001216 const BackendTextureData* data) {
1217 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001218}
1219
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001220void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001221 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1222 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001223}
1224
Robert Phillips979b2232020-02-20 10:47:29 -05001225bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1226 return false;
1227}
1228
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001229#if GR_TEST_UTILS
1230bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001231 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1232
1233 GrD3DTextureResourceInfo info;
1234 if (!tex.getD3DTextureResourceInfo(&info)) {
1235 return false;
1236 }
1237 ID3D12Resource* textureResource = info.fResource.get();
1238 if (!textureResource) {
1239 return false;
1240 }
1241 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001242}
1243
1244GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001245 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001246 this->handleDirtyContext();
1247
1248 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001249 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001250 }
1251
1252 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1253
1254 GrD3DTextureResourceInfo info;
1255 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001256 GrRenderable::kYes, GrMipmapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001257 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001258 return {};
1259 }
1260
1261 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001262}
1263
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001264void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1265 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1266
1267 GrD3DTextureResourceInfo info;
1268 if (rt.getD3DTextureResourceInfo(&info)) {
1269 this->testingOnly_flushGpuAndSync();
1270 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1271 // is deleted.
1272 }
1273}
1274
1275void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001276 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001277}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001278
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001279void GrD3DGpu::testingOnly_startCapture() {
1280 if (fGraphicsAnalysis) {
1281 fGraphicsAnalysis->BeginCapture();
1282 }
1283}
1284
1285void GrD3DGpu::testingOnly_endCapture() {
1286 if (fGraphicsAnalysis) {
1287 fGraphicsAnalysis->EndCapture();
1288 }
1289}
1290#endif
1291
Jim Van Verthc632aa62020-04-17 16:58:20 -04001292///////////////////////////////////////////////////////////////////////////////
1293
Greg Daniela5a6b322020-04-23 12:52:27 -04001294void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001295 int numBarriers,
1296 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1297 SkASSERT(fCurrentDirectCommandList);
1298 SkASSERT(resource);
1299
Greg Daniela5a6b322020-04-23 12:52:27 -04001300 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001301}
1302
Greg Daniel69267912020-07-24 10:42:53 -04001303void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1304 int numBarriers,
1305 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1306 SkASSERT(fCurrentDirectCommandList);
1307 SkASSERT(buffer);
1308
1309 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1310 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1311}
1312
1313
Greg Daniel9efe3862020-06-11 11:51:06 -04001314void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1315 GrSurfaceProxy* proxies[],
1316 int numProxies,
1317 SkSurface::BackendSurfaceAccess access,
1318 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001319 SkASSERT(numProxies >= 0);
1320 SkASSERT(!numProxies || proxies);
1321
1322 // prepare proxies by transitioning to PRESENT renderState
1323 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1324 GrD3DTextureResource* resource;
1325 for (int i = 0; i < numProxies; ++i) {
1326 SkASSERT(proxies[i]->isInstantiated());
1327 if (GrTexture* tex = proxies[i]->peekTexture()) {
1328 resource = static_cast<GrD3DTexture*>(tex);
1329 } else {
1330 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1331 SkASSERT(rt);
1332 resource = static_cast<GrD3DRenderTarget*>(rt);
1333 }
1334 resource->prepareForPresent(this);
1335 }
1336 }
1337}
1338
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001339void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001340 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001341}
1342
Jim Van Verthc632aa62020-04-17 16:58:20 -04001343bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1344 if (syncCpu) {
1345 return this->submitDirectCommandList(SyncQueue::kForce);
1346 } else {
1347 return this->submitDirectCommandList(SyncQueue::kSkip);
1348 }
1349}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001350
1351std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1352 return GrD3DSemaphore::Make(this);
1353}
1354std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1355 const GrBackendSemaphore& semaphore,
1356 GrResourceProvider::SemaphoreWrapType,
1357 GrWrapOwnership) {
1358 SkASSERT(this->caps()->semaphoreSupport());
1359 GrD3DFenceInfo fenceInfo;
1360 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1361 return nullptr;
1362 }
1363 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1364}
1365
1366void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001367 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001368 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1369 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1370 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1371}
1372
1373void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001374 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001375 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1376 // TODO: Do we need to track the lifetime of this?
1377 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1378}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001379
1380GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001381 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001382 return fCurrentFenceValue;
1383}
1384
1385bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001386 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001387}