blob: 1dc7078f8e4a204424b23822ee446607f0921ff4 [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 Salomon49721c82020-11-03 13:08:01 -0500216 sk_sp<GrRefCntedCallback> finishedCallback(
217 new GrRefCntedCallback(finishedProc, finishedContext));
218 this->addFinishedCallback(std::move(finishedCallback));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400219}
220
221void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
222 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400223 // Besides the current command list, we also add the finishedCallback to the newest outstanding
224 // command list. Our contract for calling the proc is that all previous submitted command lists
225 // have finished when we call it. However, if our current command list has no work when it is
226 // flushed it will drop its ref to the callback immediately. But the previous work may not have
227 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
228 // must finish after all previously submitted command lists.
229 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
230 if (back) {
231 back->fCommandList->addFinishedCallback(finishedCallback);
232 }
233 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
234}
235
Jim Van Verth765c5922020-08-10 17:23:50 -0400236void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
237 SkTArray<SkPoint>* sampleLocations) {
238 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
239 // These are transformed from D3D's integer coordinate system with origin at the center,
240 // to our normalized coordinate system with origin at the upper left.
241 // This ends up corresponding with Vulkan's sample locations.
242 SkASSERT(this->caps()->sampleLocationsSupport());
243 static constexpr SkPoint kStandardSampleLocations_1[1] = {
244 {0.5f, 0.5f} };
245 static constexpr SkPoint kStandardSampleLocations_2[2] = {
246 {0.75f, 0.75f}, {0.25f, 0.25f} };
247 static constexpr SkPoint kStandardSampleLocations_4[4] = {
248 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
249 static constexpr SkPoint kStandardSampleLocations_8[8] = {
250 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
251 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
252 static constexpr SkPoint kStandardSampleLocations_16[16] = {
253 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
254 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
255 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
256 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
257
258 int numSamples = renderTarget->numSamples();
259 // TODO: support mixed samples?
260 SkASSERT(numSamples > 1);
261 SkASSERT(!renderTarget->getStencilAttachment() ||
262 numSamples == renderTarget->getStencilAttachment()->numSamples());
263
264 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
265 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
266 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
267 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
268 return;
269 }
270 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
271
272 switch (numSamples) {
273 case 2:
274 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
275 break;
276 case 4:
277 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
278 break;
279 case 8:
280 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
281 break;
282 case 16:
283 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
284 break;
285 default:
286 SK_ABORT("Invalid sample count.");
287 break;
288 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500289}
290
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400291sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
292 DXGI_FORMAT dxgiFormat,
293 GrRenderable renderable,
294 int renderTargetSampleCnt,
295 SkBudgeted budgeted,
296 GrProtected isProtected,
297 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400298 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400299 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400300 if (renderable == GrRenderable::kYes) {
301 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
302 }
303
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400304 // This desc refers to a texture that will be read by the client. Thus even if msaa is
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400305 // requested, this describes the resolved texture. Therefore we always have samples set
306 // to 1.
307 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400308 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400309 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
310 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
311 // might want to manually set alignment to 4KB for smaller textures
312 resourceDesc.Alignment = 0;
313 resourceDesc.Width = dimensions.fWidth;
314 resourceDesc.Height = dimensions.fHeight;
315 resourceDesc.DepthOrArraySize = 1;
316 resourceDesc.MipLevels = mipLevelCount;
317 resourceDesc.Format = dxgiFormat;
318 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400319 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400320 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400321 resourceDesc.Flags = usageFlags;
322
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400323 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400324 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400325 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400326 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400327 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400328 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400329 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400330 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400332
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400333sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
334 const GrBackendFormat& format,
335 GrRenderable renderable,
336 int renderTargetSampleCnt,
337 SkBudgeted budgeted,
338 GrProtected isProtected,
339 int mipLevelCount,
340 uint32_t levelClearMask) {
341 DXGI_FORMAT dxgiFormat;
342 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
343 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
344
Brian Salomona6db5102020-07-21 09:56:23 -0400345 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
346 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400347
348 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
349 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400350 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400351 if (!tex) {
352 return nullptr;
353 }
354
355 if (levelClearMask) {
356 // TODO
357 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400358
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400359 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500360}
361
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400362static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
363 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
364 UINT* numRows, UINT64* rowSizeInBytes,
365 const void* compressedData, int numMipLevels) {
366 SkASSERT(compressedData && numMipLevels);
367 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
368 SkASSERT(mapPtr);
369
370 const char* src = static_cast<const char*>(compressedData);
371 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
372 // copy data into the buffer, skipping any trailing bytes
373 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
374 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
375 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
376 numRows[currentMipLevel]);
377 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
378 }
379}
380
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500381sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
382 const GrBackendFormat& format,
383 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400384 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500385 GrProtected isProtected,
386 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400387 DXGI_FORMAT dxgiFormat;
388 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
389 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
390
391 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
392 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400393 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400394
395 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400396 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400397 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400398 }
Brian Salomona6db5102020-07-21 09:56:23 -0400399 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
400 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400401
402 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
403 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400404 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400405 if (!d3dTex) {
406 return nullptr;
407 }
408
409 ID3D12Resource* d3dResource = d3dTex->d3dResource();
410 SkASSERT(d3dResource);
411 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
412 // Either upload only the first miplevel or all miplevels
413 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
414
415 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
416 SkAutoTMalloc<UINT> numRows(mipLevelCount);
417 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
418 UINT64 combinedBufferSize;
419 // We reset the width and height in the description to match our subrectangle size
420 // so we don't end up allocating more space than we need.
421 desc.Width = dimensions.width();
422 desc.Height = dimensions.height();
423 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
424 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
425 SkASSERT(combinedBufferSize);
426
Greg Danielcffb0622020-07-16 13:19:17 -0400427 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
428 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
429 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400430 return false;
431 }
Greg Danielcffb0622020-07-16 13:19:17 -0400432
433 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400434
435 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
436 rowSizeInBytes.get(), data, mipLevelCount);
437
Greg Danielcffb0622020-07-16 13:19:17 -0400438 // Update the offsets in the footprints to be relative to the slice's offset
439 for (int i = 0; i < mipLevelCount; ++i) {
440 placedFootprints[i].Offset += slice.fOffset;
441 }
442
Greg Daniel69267912020-07-24 10:42:53 -0400443 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400444 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
445 placedFootprints.get(), 0, 0);
446
447 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500448}
449
Jim Van Verth9145f782020-04-28 12:01:12 -0400450static int get_surface_sample_cnt(GrSurface* surf) {
451 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
452 return rt->numSamples();
453 }
454 return 0;
455}
456
457bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
458 const SkIPoint& dstPoint) {
459
460 if (src->isProtected() && !dst->isProtected()) {
461 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400462 return false;
463 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400464
465 int dstSampleCnt = get_surface_sample_cnt(dst);
466 int srcSampleCnt = get_surface_sample_cnt(src);
467
468 GrD3DTextureResource* dstTexResource;
469 GrD3DTextureResource* srcTexResource;
470 GrRenderTarget* dstRT = dst->asRenderTarget();
471 if (dstRT) {
472 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
473 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
474 } else {
475 SkASSERT(dst->asTexture());
476 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
477 }
478 GrRenderTarget* srcRT = src->asRenderTarget();
479 if (srcRT) {
480 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
481 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
482 } else {
483 SkASSERT(src->asTexture());
484 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
485 }
486
487 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
488 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
489
490 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
491 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
492 return true;
493 }
494
495 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
496 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
497 return true;
498 }
499
500 return false;
501}
502
503void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
504 GrD3DTextureResource* dstResource,
505 GrD3DTextureResource* srcResource,
506 const SkIRect& srcRect, const SkIPoint& dstPoint) {
507#ifdef SK_DEBUG
508 int dstSampleCnt = get_surface_sample_cnt(dst);
509 int srcSampleCnt = get_surface_sample_cnt(src);
510 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
511 DXGI_FORMAT srcFormat;
512 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
513 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
514#endif
515 if (src->isProtected() && !dst->isProtected()) {
516 SkDebugf("Can't copy from protected memory to non-protected");
517 return;
518 }
519
520 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
521 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
522
523 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
524 dstLocation.pResource = dstResource->d3dResource();
525 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
526 dstLocation.SubresourceIndex = 0;
527
528 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
529 srcLocation.pResource = srcResource->d3dResource();
530 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
531 srcLocation.SubresourceIndex = 0;
532
533 D3D12_BOX srcBox = {};
534 srcBox.left = srcRect.fLeft;
535 srcBox.top = srcRect.fTop;
536 srcBox.right = srcRect.fRight;
537 srcBox.bottom = srcRect.fBottom;
538 srcBox.front = 0;
539 srcBox.back = 1;
540 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400541 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
542 &dstLocation,
543 dstPoint.fX, dstPoint.fY,
544 srcResource->resource(),
545 &srcLocation,
546 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400547
548 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
549 srcRect.width(), srcRect.height());
550 // The rect is already in device space so we pass in kTopLeft so no flip is done.
551 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
552}
553
554void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
555 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400556 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
557 SkASSERT(srcRT);
558
559 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
560}
561
562void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
563 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
564 SkASSERT(dst);
565 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
566
567 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
568
569 GrD3DTextureResource* dstTextureResource;
570 GrRenderTarget* dstRT = dst->asRenderTarget();
571 if (dstRT) {
572 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
573 } else {
574 SkASSERT(dst->asTexture());
575 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
576 }
577
578 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
579 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
580
581 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
582 src->msaaTextureResource(), &srcRect);
583}
584
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400585void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400586 SkASSERT(target->numSamples() > 1);
587 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
Brian Salomon72c7b982020-10-06 10:07:38 -0400588 SkASSERT(rt->msaaTextureResource() && rt != rt->msaaTextureResource());
Jim Van Verth275f4192020-07-07 15:36:41 -0400589
590 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400591}
592
Jim Van Verthba7f2292020-04-21 08:56:47 -0400593bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
594 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
595 size_t rowBytes) {
596 SkASSERT(surface);
597
598 if (surfaceColorType != dstColorType) {
599 return false;
600 }
601
602 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400603 GrD3DTextureResource* texResource = nullptr;
604 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
605 if (rt) {
606 texResource = rt;
607 } else {
608 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
609 }
610
611 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400612 return false;
613 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400614
Jim Van Verthba7f2292020-04-21 08:56:47 -0400615 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400616 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400617 SkASSERT(srcLocation.pResource);
618 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
619 srcLocation.SubresourceIndex = 0;
620
621 D3D12_BOX srcBox = {};
622 srcBox.left = left;
623 srcBox.top = top;
624 srcBox.right = left + width;
625 srcBox.bottom = top + height;
626 srcBox.front = 0;
627 srcBox.back = 1;
628
629 // Set up dst location and create transfer buffer
630 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
631 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400632 UINT64 transferTotalBytes;
633 const UINT64 baseOffset = 0;
634 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
635 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400636 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400637 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400638 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400639 if (GrDxgiFormatBytesPerBlock(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400640 return false;
641 }
642 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400643
644 // TODO: implement some way of reusing buffers instead of making a new one every time.
645 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
646 GrGpuBufferType::kXferGpuToCpu,
647 kDynamic_GrAccessPattern);
648 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
649 dstLocation.pResource = d3dBuf->d3dResource();
650
651 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400652 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400653
Greg Daniel69267912020-07-24 10:42:53 -0400654 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
655 texResource->resource(), &srcLocation,
656 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400657 this->submitDirectCommandList(SyncQueue::kForce);
658
659 const void* mappedMemory = transferBuffer->map();
660
661 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400662 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400663
664 transferBuffer->unmap();
665
666 return true;
667}
668
669bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
670 GrColorType surfaceColorType, GrColorType srcColorType,
671 const GrMipLevel texels[], int mipLevelCount,
672 bool prepForTexSampling) {
673 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
674 if (!d3dTex) {
675 return false;
676 }
677
678 // Make sure we have at least the base level
679 if (!mipLevelCount || !texels[0].fPixels) {
680 return false;
681 }
682
683 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
684 bool success = false;
685
686 // Need to change the resource state to COPY_DEST in order to upload to it
687 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
688
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400689 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400690 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
691 mipLevelCount);
692
693 if (prepForTexSampling) {
694 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
695 }
696
697 return success;
698}
699
700bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
701 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
702 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
703 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
704 SkASSERT(1 == mipLevelCount ||
705 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
706
707 // We assume that if the texture has mip levels, we either upload to all the levels or just the
708 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400709 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400710
711 if (width == 0 || height == 0) {
712 return false;
713 }
714
715 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
716 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
717
718 ID3D12Resource* d3dResource = tex->d3dResource();
719 SkASSERT(d3dResource);
720 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
721 // Either upload only the first miplevel or all miplevels
722 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
723
724 if (1 == mipLevelCount && !texels[0].fPixels) {
725 return true; // no data to upload
726 }
727
728 for (int i = 0; i < mipLevelCount; ++i) {
729 // We do not allow any gaps in the mip data
730 if (!texels[i].fPixels) {
731 return false;
732 }
733 }
734
735 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400736 UINT64 combinedBufferSize;
737 // We reset the width and height in the description to match our subrectangle size
738 // so we don't end up allocating more space than we need.
739 desc.Width = width;
740 desc.Height = height;
741 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400742 nullptr, nullptr, &combinedBufferSize);
743 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400744 SkASSERT(combinedBufferSize);
745
Greg Danielcffb0622020-07-16 13:19:17 -0400746 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
747 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
748 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400749 return false;
750 }
Greg Danielcffb0622020-07-16 13:19:17 -0400751
752 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400753
754 int currentWidth = width;
755 int currentHeight = height;
756 int layerHeight = tex->height();
757
758 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
759 if (texels[currentMipLevel].fPixels) {
760 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
761
Jim Van Verthfdd36852020-04-21 16:29:44 -0400762 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400763 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
764
765 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
766
767 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400768 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400769 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
770 src, srcRowBytes, trimRowBytes, currentHeight);
771 }
772 currentWidth = std::max(1, currentWidth / 2);
773 currentHeight = std::max(1, currentHeight / 2);
774 layerHeight = currentHeight;
775 }
776
Greg Danielcffb0622020-07-16 13:19:17 -0400777 // Update the offsets in the footprints to be relative to the slice's offset
778 for (int i = 0; i < mipLevelCount; ++i) {
779 placedFootprints[i].Offset += slice.fOffset;
780 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400781
Greg Daniel69267912020-07-24 10:42:53 -0400782 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400783 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
784 placedFootprints.get(), left, top);
785
786 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400787 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400788 }
789
790 return true;
791}
792
Jim Van Verth9145f782020-04-28 12:01:12 -0400793static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -0400794 if (!info.fResource.get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400795 return false;
796 }
797 return true;
798}
799
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400800static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
801 if (!caps.isFormatTexturable(info.fFormat)) {
802 return false;
803 }
Brian Salomon718ae762020-09-29 16:52:49 -0400804 // We don't support sampling from multisampled textures.
805 if (info.fSampleCount != 1) {
806 return false;
807 }
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400808 return true;
809}
810
811static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
812 int sampleCnt) {
813 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
814 return false;
815 }
816 return true;
817}
818
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400819sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
820 GrWrapOwnership,
821 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400822 GrIOType ioType) {
823 GrD3DTextureResourceInfo textureInfo;
824 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
825 return nullptr;
826 }
827
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400828 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400829 return nullptr;
830 }
831
832 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
833 return nullptr;
834 }
835
836 // TODO: support protected context
837 if (tex.isProtected()) {
838 return nullptr;
839 }
840
841 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
842 SkASSERT(state);
843 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
844 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500845}
846
847sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400848 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500849 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400850 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500851}
852
853sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
854 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500855 GrWrapOwnership ownership,
856 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400857 GrD3DTextureResourceInfo textureInfo;
858 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
859 return nullptr;
860 }
861
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400862 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400863 return nullptr;
864 }
865
866 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
867 return nullptr;
868 }
869 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
870 return nullptr;
871 }
872
873 // TODO: support protected context
874 if (tex.isProtected()) {
875 return nullptr;
876 }
877
878 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
879
880 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
881 SkASSERT(state);
882
883 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
884 sampleCnt, cacheable,
885 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500886}
887
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400888sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400889 GrD3DTextureResourceInfo info;
890 if (!rt.getD3DTextureResourceInfo(&info)) {
891 return nullptr;
892 }
893
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400894 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400895 return nullptr;
896 }
897
898 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
899 return nullptr;
900 }
901
902 // TODO: support protected context
903 if (rt.isProtected()) {
904 return nullptr;
905 }
906
907 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
908
909 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
Brian Salomon72c7b982020-10-06 10:07:38 -0400910 this, rt.dimensions(), rt.sampleCnt(), info, std::move(state));
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400911
912 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
913 SkASSERT(!rt.stencilBits());
914 if (tgt) {
915 SkASSERT(tgt->canAttemptStencilAttachment());
916 }
917
918 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500919}
920
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500921sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400922 GrAccessPattern accessPattern, const void* data) {
923 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
924 if (data && buffer) {
925 buffer->updateData(data, sizeInBytes);
926 }
927
928 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500929}
930
Greg Danielc0d69152020-10-08 14:59:00 -0400931sk_sp<GrAttachment> GrD3DGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
932 SkISize dimensions,
933 int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400934 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400935 SkASSERT(dimensions.width() >= rt->width());
936 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400937
Greg Daniel8ade5e82020-10-07 13:09:48 -0400938 DXGI_FORMAT sFmt = this->d3dCaps().preferredStencilFormat();
Jim Van Verth4f51f472020-04-13 11:02:21 -0400939
Jim Van Verth4f51f472020-04-13 11:02:21 -0400940 fStats.incStencilAttachmentCreates();
Greg Danielc0d69152020-10-08 14:59:00 -0400941 return GrD3DAttachment::MakeStencil(this, dimensions, numStencilSamples, sFmt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500942}
943
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400944bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
945 SkISize dimensions,
946 GrTexturable texturable,
947 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400948 GrMipmapped mipMapped,
Brian Salomon72c7b982020-10-06 10:07:38 -0400949 int sampleCnt,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400950 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400951 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400952 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400953
954 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
955 return false;
956 }
957
958 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
959 return false;
960 }
961
962 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
963 return false;
964 }
965
966 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400967 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400968 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400969 }
970
971 // create the texture
972 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
973 if (renderable == GrRenderable::kYes) {
974 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
975 }
976
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400977 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400978 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400979 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400980 resourceDesc.Width = dimensions.fWidth;
981 resourceDesc.Height = dimensions.fHeight;
982 resourceDesc.DepthOrArraySize = 1;
983 resourceDesc.MipLevels = numMipLevels;
984 resourceDesc.Format = dxgiFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -0400985 resourceDesc.SampleDesc.Count = sampleCnt;
Jim Van Verth765c5922020-08-10 17:23:50 -0400986 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Greg Daniel16032b32020-05-06 15:31:10 -0400987 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400988 resourceDesc.Flags = usageFlags;
989
Jim Van Verth280d4f72020-05-04 10:04:24 -0400990 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
991 D3D12_CLEAR_VALUE clearValue = {};
992 if (renderable == GrRenderable::kYes) {
993 clearValue.Format = dxgiFormat;
994 // Assume transparent black
995 clearValue.Color[0] = 0;
996 clearValue.Color[1] = 0;
997 clearValue.Color[2] = 0;
998 clearValue.Color[3] = 0;
999 clearValuePtr = &clearValue;
1000 }
1001
Greg Daniel16032b32020-05-06 15:31:10 -04001002 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
1003 ? D3D12_RESOURCE_STATE_RENDER_TARGET
1004 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001005 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -04001006 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001007 SkDebugf("Failed to init texture resource info\n");
1008 return false;
1009 }
1010
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001011 return true;
1012}
1013
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001014GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001015 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001016 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001017 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001018 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001019 this->handleDirtyContext();
1020
1021 const GrD3DCaps& caps = this->d3dCaps();
1022
1023 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1024 return {};
1025 }
1026
1027 DXGI_FORMAT dxgiFormat;
1028 if (!format.asDxgiFormat(&dxgiFormat)) {
1029 return {};
1030 }
1031
1032 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1033 if (!caps.isFormatTexturable(dxgiFormat)) {
1034 return {};
1035 }
1036
1037 GrD3DTextureResourceInfo info;
1038 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
Brian Salomon72c7b982020-10-06 10:07:38 -04001039 renderable, mipMapped, 1, &info,
1040 isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001041 return {};
1042 }
1043
1044 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001045}
1046
Greg Daniel0eca74c2020-10-01 13:46:00 -04001047static void copy_src_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001048 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1049 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001050 SkASSERT(srcData && numMipLevels);
1051 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1052 SkASSERT(mapPtr);
1053
Greg Daniel0eca74c2020-10-01 13:46:00 -04001054 size_t bytesPerPixel = GrDxgiFormatBytesPerBlock(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001055
1056 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1057 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1058
1059 // copy data into the buffer, skipping any trailing bytes
1060 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1061 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1062 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1063 trimRowBytes, srcData[currentMipLevel].height());
1064 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001065}
1066
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001067static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1068 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1069 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1070 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001071 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001072 if (colorType == GrColorType::kUnknown) {
1073 return false;
1074 }
1075 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1076 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1077 return false;
1078 }
1079
1080 return true;
1081}
1082
Greg Daniel16032b32020-05-06 15:31:10 -04001083bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1084 sk_sp<GrRefCntedCallback> finishedCallback,
1085 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001086 GrD3DTextureResourceInfo info;
1087 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1088
1089 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1090 SkASSERT(state);
1091 sk_sp<GrD3DTexture> texture =
1092 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1093 GrWrapCacheable::kNo,
1094 kRW_GrIOType, info, std::move(state));
1095 if (!texture) {
1096 return false;
1097 }
1098
1099 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1100 if (!cmdList) {
1101 return false;
1102 }
1103
1104 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1105
1106 ID3D12Resource* d3dResource = texture->d3dResource();
1107 SkASSERT(d3dResource);
1108 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1109 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001110 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001111 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001112 backendTexture.dimensions().height()) + 1;
1113 }
1114 SkASSERT(mipLevelCount == info.fLevelCount);
1115 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1116 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001117 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1118 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001119 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001120 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001121 SkASSERT(combinedBufferSize);
1122 if (data->type() == BackendTextureData::Type::kColor &&
1123 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1124 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1125 combinedBufferSize =
1126 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1127 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1128 placedFootprints[i].Offset = 0;
1129 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1130 }
1131 }
1132
Greg Danielcffb0622020-07-16 13:19:17 -04001133 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1134 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1135 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001136 return false;
1137 }
Greg Danielcffb0622020-07-16 13:19:17 -04001138
1139 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001140 SkASSERT(bufferData);
1141
Jim Van Verth43a6e172020-06-23 11:59:08 -04001142 if (data->type() == BackendTextureData::Type::kPixmaps) {
Greg Daniel0eca74c2020-10-01 13:46:00 -04001143 copy_src_data(bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001144 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001145 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001146 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1147 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001148 } else {
1149 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1150 SkImage::CompressionType compression =
1151 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1152 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001153 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1154 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001155 return false;
1156 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001157 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001158 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1159 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001160 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001161 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001162 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001163 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001164 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1165 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001166 }
1167 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001168
Greg Danielcffb0622020-07-16 13:19:17 -04001169 // Update the offsets in the footprints to be relative to the slice's offset
1170 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1171 placedFootprints[i].Offset += slice.fOffset;
1172 }
1173
Greg Daniel69267912020-07-24 10:42:53 -04001174 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001175 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1176 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001177
1178 if (finishedCallback) {
1179 this->addFinishedCallback(std::move(finishedCallback));
1180 }
1181
Greg Daniel16032b32020-05-06 15:31:10 -04001182 return true;
1183}
1184
Greg Danielc1ad77c2020-05-06 11:40:03 -04001185GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001186 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001187 GrProtected isProtected) {
1188 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1189 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001190}
1191
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001192bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001193 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001194 const BackendTextureData* data) {
1195 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001196}
1197
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001198void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001199 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1200 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001201}
1202
Robert Phillips979b2232020-02-20 10:47:29 -05001203bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1204 return false;
1205}
1206
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001207#if GR_TEST_UTILS
1208bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001209 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1210
1211 GrD3DTextureResourceInfo info;
1212 if (!tex.getD3DTextureResourceInfo(&info)) {
1213 return false;
1214 }
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001215 ID3D12Resource* textureResource = info.fResource.get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001216 if (!textureResource) {
1217 return false;
1218 }
1219 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001220}
1221
Brian Salomon72c7b982020-10-06 10:07:38 -04001222GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
1223 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04001224 int sampleCnt,
1225 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001226 this->handleDirtyContext();
1227
Brian Salomon72c7b982020-10-06 10:07:38 -04001228 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
1229 dimensions.height() > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001230 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001231 }
1232
1233 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1234
1235 GrD3DTextureResourceInfo info;
Brian Salomon72c7b982020-10-06 10:07:38 -04001236 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001237 GrRenderable::kYes, GrMipmapped::kNo,
Brian Salomonf9b00422020-10-08 16:00:14 -04001238 sampleCnt, &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001239 return {};
1240 }
1241
Brian Salomon72c7b982020-10-06 10:07:38 -04001242 return GrBackendRenderTarget(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001243}
1244
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001245void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1246 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1247
1248 GrD3DTextureResourceInfo info;
1249 if (rt.getD3DTextureResourceInfo(&info)) {
1250 this->testingOnly_flushGpuAndSync();
1251 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1252 // is deleted.
1253 }
1254}
1255
1256void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001257 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001258}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001259
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001260void GrD3DGpu::testingOnly_startCapture() {
1261 if (fGraphicsAnalysis) {
1262 fGraphicsAnalysis->BeginCapture();
1263 }
1264}
1265
1266void GrD3DGpu::testingOnly_endCapture() {
1267 if (fGraphicsAnalysis) {
1268 fGraphicsAnalysis->EndCapture();
1269 }
1270}
1271#endif
1272
Jim Van Verthc632aa62020-04-17 16:58:20 -04001273///////////////////////////////////////////////////////////////////////////////
1274
Greg Daniela5a6b322020-04-23 12:52:27 -04001275void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001276 int numBarriers,
1277 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1278 SkASSERT(fCurrentDirectCommandList);
1279 SkASSERT(resource);
1280
Greg Daniela5a6b322020-04-23 12:52:27 -04001281 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001282}
1283
Greg Daniel69267912020-07-24 10:42:53 -04001284void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1285 int numBarriers,
1286 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1287 SkASSERT(fCurrentDirectCommandList);
1288 SkASSERT(buffer);
1289
1290 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1291 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1292}
1293
1294
Greg Daniel9efe3862020-06-11 11:51:06 -04001295void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1296 GrSurfaceProxy* proxies[],
1297 int numProxies,
1298 SkSurface::BackendSurfaceAccess access,
1299 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001300 SkASSERT(numProxies >= 0);
1301 SkASSERT(!numProxies || proxies);
1302
1303 // prepare proxies by transitioning to PRESENT renderState
1304 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1305 GrD3DTextureResource* resource;
1306 for (int i = 0; i < numProxies; ++i) {
1307 SkASSERT(proxies[i]->isInstantiated());
1308 if (GrTexture* tex = proxies[i]->peekTexture()) {
1309 resource = static_cast<GrD3DTexture*>(tex);
1310 } else {
1311 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1312 SkASSERT(rt);
1313 resource = static_cast<GrD3DRenderTarget*>(rt);
1314 }
1315 resource->prepareForPresent(this);
1316 }
1317 }
1318}
1319
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001320void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001321 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001322}
1323
Jim Van Verthc632aa62020-04-17 16:58:20 -04001324bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1325 if (syncCpu) {
1326 return this->submitDirectCommandList(SyncQueue::kForce);
1327 } else {
1328 return this->submitDirectCommandList(SyncQueue::kSkip);
1329 }
1330}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001331
1332std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1333 return GrD3DSemaphore::Make(this);
1334}
1335std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1336 const GrBackendSemaphore& semaphore,
1337 GrResourceProvider::SemaphoreWrapType,
1338 GrWrapOwnership) {
1339 SkASSERT(this->caps()->semaphoreSupport());
1340 GrD3DFenceInfo fenceInfo;
1341 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1342 return nullptr;
1343 }
1344 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1345}
1346
1347void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001348 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001349 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1350 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1351 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1352}
1353
1354void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001355 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001356 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1357 // TODO: Do we need to track the lifetime of this?
1358 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1359}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001360
1361GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -04001362 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001363 return fCurrentFenceValue;
1364}
1365
1366bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001367 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001368}