blob: 0daeca534f738d25cbeafb072ddc8d0fb8cbb490 [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 Verthb8ae7fa2020-09-08 15:51:14 -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(
Greg Daniel9a18b082020-08-14 14:03:50 -0400105 GrRenderTarget* rt, GrStencilAttachment*,
106 GrSurfaceOrigin origin, const SkIRect& bounds,
107 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
108 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
109 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -0400110 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500111 if (!fCachedOpsRenderPass) {
112 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
113 }
114
115 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
116 return nullptr;
117 }
118 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500119}
120
Jim Van Verthc632aa62020-04-17 16:58:20 -0400121bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400122 SkASSERT(fCurrentDirectCommandList);
123
Jim Van Verth3eadce22020-06-01 11:34:49 -0400124 fResourceProvider.prepForSubmit();
125
Jim Van Verthb8ae7fa2020-09-08 15:51:14 -0400126 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.Get());
Jim Van Verthc632aa62020-04-17 16:58:20 -0400127 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
128 return false;
129 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
130 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400131 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400132 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400133 }
134 return true;
135 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400136
Greg Daniela581a8b2020-06-19 15:22:18 -0400137 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
138 // uniform data as dirty.
139 fResourceProvider.markPipelineStateUniformsDirty();
140
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400141 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400142 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400143 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400144
Jim Van Verthc632aa62020-04-17 16:58:20 -0400145 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400146 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400147 }
148
Greg Daniel83ed2132020-03-24 13:15:33 -0400149 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
150
151 // This should be done after we have a new command list in case the freeing of any resources
152 // held by a finished command list causes us send a new command to the gpu (like changing the
153 // resource state.
154 this->checkForFinishedCommandLists();
155
156 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400157 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400158}
159
160void GrD3DGpu::checkForFinishedCommandLists() {
161 uint64_t currentFenceValue = fFence->GetCompletedValue();
162
163 // Iterate over all the outstanding command lists to see if any have finished. The commands
164 // lists are in order from oldest to newest, so we start at the front to check if their fence
165 // value is less than the last signaled value. If so we pop it off and move onto the next.
166 // Repeat till we find a command list that has not finished yet (and all others afterwards are
167 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400168 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
169 while (front && front->fFenceValue <= currentFenceValue) {
170 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400171 // Since we used placement new we are responsible for calling the destructor manually.
172 front->~OutstandingCommandList();
173 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400174 fResourceProvider.recycleDirectCommandList(std::move(currList));
175 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400176 }
177}
178
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400179void GrD3DGpu::waitForQueueCompletion() {
180 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
181 HANDLE fenceEvent;
182 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
183 SkASSERT(fenceEvent);
184 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
185 WaitForSingleObject(fenceEvent, INFINITE);
186 CloseHandle(fenceEvent);
187 }
188}
189
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500190void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400191 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
192
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500193 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400194 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500195}
196
Greg Danield4928d02020-06-19 11:13:26 -0400197void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
198 GrGpuFinishedContext finishedContext) {
199 SkASSERT(finishedProc);
200 sk_sp<GrRefCntedCallback> finishedCallback(
201 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400202 this->addFinishedCallback(std::move(finishedCallback));
203}
204
205void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
206 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400207 // Besides the current command list, we also add the finishedCallback to the newest outstanding
208 // command list. Our contract for calling the proc is that all previous submitted command lists
209 // have finished when we call it. However, if our current command list has no work when it is
210 // flushed it will drop its ref to the callback immediately. But the previous work may not have
211 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
212 // must finish after all previously submitted command lists.
213 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
214 if (back) {
215 back->fCommandList->addFinishedCallback(finishedCallback);
216 }
217 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
218}
219
Jim Van Verth765c5922020-08-10 17:23:50 -0400220void GrD3DGpu::querySampleLocations(GrRenderTarget* renderTarget,
221 SkTArray<SkPoint>* sampleLocations) {
222 // By default, the Direct3D backend uses the standard sample locations defined by the docs.
223 // These are transformed from D3D's integer coordinate system with origin at the center,
224 // to our normalized coordinate system with origin at the upper left.
225 // This ends up corresponding with Vulkan's sample locations.
226 SkASSERT(this->caps()->sampleLocationsSupport());
227 static constexpr SkPoint kStandardSampleLocations_1[1] = {
228 {0.5f, 0.5f} };
229 static constexpr SkPoint kStandardSampleLocations_2[2] = {
230 {0.75f, 0.75f}, {0.25f, 0.25f} };
231 static constexpr SkPoint kStandardSampleLocations_4[4] = {
232 {0.375f, 0.125f}, {0.875f, 0.375f}, {0.125f, 0.625f}, {0.625f, 0.875f} };
233 static constexpr SkPoint kStandardSampleLocations_8[8] = {
234 {0.5625f, 0.3125f}, {0.4375f, 0.6875f}, {0.8125f, 0.5625f}, {0.3125f, 0.1875f},
235 {0.1875f, 0.8125f}, {0.0625f, 0.4375f}, {0.6875f, 0.9375f}, {0.9375f, 0.0625f} };
236 static constexpr SkPoint kStandardSampleLocations_16[16] = {
237 {0.5625f, 0.5625f}, {0.4375f, 0.3125f}, {0.3125f, 0.625f}, {0.75f, 0.4375f},
238 {0.1875f, 0.375f}, {0.625f, 0.8125f}, {0.8125f, 0.6875f}, {0.6875f, 0.1875f},
239 {0.375f, 0.875f}, {0.5f, 0.0625f}, {0.25f, 0.125f}, {0.125f, 0.75f},
240 {0.0f, 0.5f}, {0.9375f, 0.25f}, {0.875f, 0.9375f}, {0.0625f, 0.0f} };
241
242 int numSamples = renderTarget->numSamples();
243 // TODO: support mixed samples?
244 SkASSERT(numSamples > 1);
245 SkASSERT(!renderTarget->getStencilAttachment() ||
246 numSamples == renderTarget->getStencilAttachment()->numSamples());
247
248 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(renderTarget);
249 unsigned int pattern = d3dRT->msaaTextureResource()->sampleQualityPattern();
250 if (pattern == DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN) {
251 sampleLocations->push_back_n(numSamples, kStandardSampleLocations_1[0]);
252 return;
253 }
254 SkASSERT(pattern == DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN);
255
256 switch (numSamples) {
257 case 2:
258 sampleLocations->push_back_n(2, kStandardSampleLocations_2);
259 break;
260 case 4:
261 sampleLocations->push_back_n(4, kStandardSampleLocations_4);
262 break;
263 case 8:
264 sampleLocations->push_back_n(8, kStandardSampleLocations_8);
265 break;
266 case 16:
267 sampleLocations->push_back_n(16, kStandardSampleLocations_16);
268 break;
269 default:
270 SK_ABORT("Invalid sample count.");
271 break;
272 }
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500273}
274
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400275sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
276 DXGI_FORMAT dxgiFormat,
277 GrRenderable renderable,
278 int renderTargetSampleCnt,
279 SkBudgeted budgeted,
280 GrProtected isProtected,
281 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400282 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400283 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400284 if (renderable == GrRenderable::kYes) {
285 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
286 }
287
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400288 // 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 -0400289 // requested, this describes the resolved texture. Therefore we always have samples set
290 // to 1.
291 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400292 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400293 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
294 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
295 // might want to manually set alignment to 4KB for smaller textures
296 resourceDesc.Alignment = 0;
297 resourceDesc.Width = dimensions.fWidth;
298 resourceDesc.Height = dimensions.fHeight;
299 resourceDesc.DepthOrArraySize = 1;
300 resourceDesc.MipLevels = mipLevelCount;
301 resourceDesc.Format = dxgiFormat;
302 resourceDesc.SampleDesc.Count = 1;
Jim Van Verth765c5922020-08-10 17:23:50 -0400303 resourceDesc.SampleDesc.Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400304 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400305 resourceDesc.Flags = usageFlags;
306
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400307 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400308 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400309 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400310 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400311 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400312 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400313 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400314 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400315}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400316
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400317sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
318 const GrBackendFormat& format,
319 GrRenderable renderable,
320 int renderTargetSampleCnt,
321 SkBudgeted budgeted,
322 GrProtected isProtected,
323 int mipLevelCount,
324 uint32_t levelClearMask) {
325 DXGI_FORMAT dxgiFormat;
326 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
327 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
328
Brian Salomona6db5102020-07-21 09:56:23 -0400329 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
330 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331
332 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
333 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400334 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400335 if (!tex) {
336 return nullptr;
337 }
338
339 if (levelClearMask) {
340 // TODO
341 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400342
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400343 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500344}
345
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400346static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
347 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
348 UINT* numRows, UINT64* rowSizeInBytes,
349 const void* compressedData, int numMipLevels) {
350 SkASSERT(compressedData && numMipLevels);
351 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
352 SkASSERT(mapPtr);
353
354 const char* src = static_cast<const char*>(compressedData);
355 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
356 // copy data into the buffer, skipping any trailing bytes
357 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
358 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
359 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
360 numRows[currentMipLevel]);
361 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
362 }
363}
364
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500365sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
366 const GrBackendFormat& format,
367 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400368 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500369 GrProtected isProtected,
370 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400371 DXGI_FORMAT dxgiFormat;
372 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
373 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
374
375 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
376 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400377 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400378
379 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400380 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400381 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400382 }
Brian Salomona6db5102020-07-21 09:56:23 -0400383 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
384 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400385
386 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
387 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400388 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400389 if (!d3dTex) {
390 return nullptr;
391 }
392
393 ID3D12Resource* d3dResource = d3dTex->d3dResource();
394 SkASSERT(d3dResource);
395 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
396 // Either upload only the first miplevel or all miplevels
397 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
398
399 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
400 SkAutoTMalloc<UINT> numRows(mipLevelCount);
401 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
402 UINT64 combinedBufferSize;
403 // We reset the width and height in the description to match our subrectangle size
404 // so we don't end up allocating more space than we need.
405 desc.Width = dimensions.width();
406 desc.Height = dimensions.height();
407 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
408 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
409 SkASSERT(combinedBufferSize);
410
Greg Danielcffb0622020-07-16 13:19:17 -0400411 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
412 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
413 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400414 return false;
415 }
Greg Danielcffb0622020-07-16 13:19:17 -0400416
417 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400418
419 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
420 rowSizeInBytes.get(), data, mipLevelCount);
421
Greg Danielcffb0622020-07-16 13:19:17 -0400422 // Update the offsets in the footprints to be relative to the slice's offset
423 for (int i = 0; i < mipLevelCount; ++i) {
424 placedFootprints[i].Offset += slice.fOffset;
425 }
426
Greg Daniel69267912020-07-24 10:42:53 -0400427 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400428 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
429 placedFootprints.get(), 0, 0);
430
431 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500432}
433
Jim Van Verth9145f782020-04-28 12:01:12 -0400434static int get_surface_sample_cnt(GrSurface* surf) {
435 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
436 return rt->numSamples();
437 }
438 return 0;
439}
440
441bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
442 const SkIPoint& dstPoint) {
443
444 if (src->isProtected() && !dst->isProtected()) {
445 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400446 return false;
447 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400448
449 int dstSampleCnt = get_surface_sample_cnt(dst);
450 int srcSampleCnt = get_surface_sample_cnt(src);
451
452 GrD3DTextureResource* dstTexResource;
453 GrD3DTextureResource* srcTexResource;
454 GrRenderTarget* dstRT = dst->asRenderTarget();
455 if (dstRT) {
456 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
457 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
458 } else {
459 SkASSERT(dst->asTexture());
460 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
461 }
462 GrRenderTarget* srcRT = src->asRenderTarget();
463 if (srcRT) {
464 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
465 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
466 } else {
467 SkASSERT(src->asTexture());
468 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
469 }
470
471 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
472 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
473
474 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
475 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
476 return true;
477 }
478
479 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
480 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
481 return true;
482 }
483
484 return false;
485}
486
487void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
488 GrD3DTextureResource* dstResource,
489 GrD3DTextureResource* srcResource,
490 const SkIRect& srcRect, const SkIPoint& dstPoint) {
491#ifdef SK_DEBUG
492 int dstSampleCnt = get_surface_sample_cnt(dst);
493 int srcSampleCnt = get_surface_sample_cnt(src);
494 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
495 DXGI_FORMAT srcFormat;
496 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
497 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
498#endif
499 if (src->isProtected() && !dst->isProtected()) {
500 SkDebugf("Can't copy from protected memory to non-protected");
501 return;
502 }
503
504 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
505 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
506
507 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
508 dstLocation.pResource = dstResource->d3dResource();
509 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
510 dstLocation.SubresourceIndex = 0;
511
512 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
513 srcLocation.pResource = srcResource->d3dResource();
514 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
515 srcLocation.SubresourceIndex = 0;
516
517 D3D12_BOX srcBox = {};
518 srcBox.left = srcRect.fLeft;
519 srcBox.top = srcRect.fTop;
520 srcBox.right = srcRect.fRight;
521 srcBox.bottom = srcRect.fBottom;
522 srcBox.front = 0;
523 srcBox.back = 1;
524 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400525 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
526 &dstLocation,
527 dstPoint.fX, dstPoint.fY,
528 srcResource->resource(),
529 &srcLocation,
530 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400531
532 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
533 srcRect.width(), srcRect.height());
534 // The rect is already in device space so we pass in kTopLeft so no flip is done.
535 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
536}
537
538void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
539 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400540 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
541 SkASSERT(srcRT);
542
543 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
544}
545
546void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
547 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
548 SkASSERT(dst);
549 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
550
551 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
552
553 GrD3DTextureResource* dstTextureResource;
554 GrRenderTarget* dstRT = dst->asRenderTarget();
555 if (dstRT) {
556 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
557 } else {
558 SkASSERT(dst->asTexture());
559 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
560 }
561
562 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
563 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
564
565 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
566 src->msaaTextureResource(), &srcRect);
567}
568
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400569void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400570 SkASSERT(target->numSamples() > 1);
571 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
572 SkASSERT(rt->msaaTextureResource());
573
574 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400575}
576
Jim Van Verthba7f2292020-04-21 08:56:47 -0400577bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
578 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
579 size_t rowBytes) {
580 SkASSERT(surface);
581
582 if (surfaceColorType != dstColorType) {
583 return false;
584 }
585
586 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400587 GrD3DTextureResource* texResource = nullptr;
588 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
589 if (rt) {
590 texResource = rt;
591 } else {
592 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
593 }
594
595 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400596 return false;
597 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400598
Jim Van Verthba7f2292020-04-21 08:56:47 -0400599 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400600 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400601 SkASSERT(srcLocation.pResource);
602 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
603 srcLocation.SubresourceIndex = 0;
604
605 D3D12_BOX srcBox = {};
606 srcBox.left = left;
607 srcBox.top = top;
608 srcBox.right = left + width;
609 srcBox.bottom = top + height;
610 srcBox.front = 0;
611 srcBox.back = 1;
612
613 // Set up dst location and create transfer buffer
614 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
615 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400616 UINT64 transferTotalBytes;
617 const UINT64 baseOffset = 0;
618 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
619 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400620 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400621 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400622 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400623 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400624 return false;
625 }
626 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400627
628 // TODO: implement some way of reusing buffers instead of making a new one every time.
629 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
630 GrGpuBufferType::kXferGpuToCpu,
631 kDynamic_GrAccessPattern);
632 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
633 dstLocation.pResource = d3dBuf->d3dResource();
634
635 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400636 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400637
Greg Daniel69267912020-07-24 10:42:53 -0400638 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
639 texResource->resource(), &srcLocation,
640 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400641 this->submitDirectCommandList(SyncQueue::kForce);
642
643 const void* mappedMemory = transferBuffer->map();
644
645 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400646 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400647
648 transferBuffer->unmap();
649
650 return true;
651}
652
653bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
654 GrColorType surfaceColorType, GrColorType srcColorType,
655 const GrMipLevel texels[], int mipLevelCount,
656 bool prepForTexSampling) {
657 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
658 if (!d3dTex) {
659 return false;
660 }
661
662 // Make sure we have at least the base level
663 if (!mipLevelCount || !texels[0].fPixels) {
664 return false;
665 }
666
667 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
668 bool success = false;
669
670 // Need to change the resource state to COPY_DEST in order to upload to it
671 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
672
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400673 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400674 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
675 mipLevelCount);
676
677 if (prepForTexSampling) {
678 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
679 }
680
681 return success;
682}
683
684bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
685 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
686 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
687 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
688 SkASSERT(1 == mipLevelCount ||
689 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
690
691 // We assume that if the texture has mip levels, we either upload to all the levels or just the
692 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400693 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400694
695 if (width == 0 || height == 0) {
696 return false;
697 }
698
699 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
700 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
701
702 ID3D12Resource* d3dResource = tex->d3dResource();
703 SkASSERT(d3dResource);
704 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
705 // Either upload only the first miplevel or all miplevels
706 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
707
708 if (1 == mipLevelCount && !texels[0].fPixels) {
709 return true; // no data to upload
710 }
711
712 for (int i = 0; i < mipLevelCount; ++i) {
713 // We do not allow any gaps in the mip data
714 if (!texels[i].fPixels) {
715 return false;
716 }
717 }
718
719 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400720 UINT64 combinedBufferSize;
721 // We reset the width and height in the description to match our subrectangle size
722 // so we don't end up allocating more space than we need.
723 desc.Width = width;
724 desc.Height = height;
725 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400726 nullptr, nullptr, &combinedBufferSize);
727 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400728 SkASSERT(combinedBufferSize);
729
Greg Danielcffb0622020-07-16 13:19:17 -0400730 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
731 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
732 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400733 return false;
734 }
Greg Danielcffb0622020-07-16 13:19:17 -0400735
736 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400737
738 int currentWidth = width;
739 int currentHeight = height;
740 int layerHeight = tex->height();
741
742 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
743 if (texels[currentMipLevel].fPixels) {
744 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
745
Jim Van Verthfdd36852020-04-21 16:29:44 -0400746 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400747 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
748
749 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
750
751 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400752 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400753 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
754 src, srcRowBytes, trimRowBytes, currentHeight);
755 }
756 currentWidth = std::max(1, currentWidth / 2);
757 currentHeight = std::max(1, currentHeight / 2);
758 layerHeight = currentHeight;
759 }
760
Greg Danielcffb0622020-07-16 13:19:17 -0400761 // Update the offsets in the footprints to be relative to the slice's offset
762 for (int i = 0; i < mipLevelCount; ++i) {
763 placedFootprints[i].Offset += slice.fOffset;
764 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400765
Greg Daniel69267912020-07-24 10:42:53 -0400766 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400767 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
768 placedFootprints.get(), left, top);
769
770 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400771 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400772 }
773
774 return true;
775}
776
Jim Van Verth9145f782020-04-28 12:01:12 -0400777static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
Jim Van Verthb8ae7fa2020-09-08 15:51:14 -0400778 if (!info.fResource.Get()) {
Jim Van Verth9145f782020-04-28 12:01:12 -0400779 return false;
780 }
781 return true;
782}
783
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400784static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
785 if (!caps.isFormatTexturable(info.fFormat)) {
786 return false;
787 }
788 return true;
789}
790
791static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
792 int sampleCnt) {
793 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
794 return false;
795 }
796 return true;
797}
798
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400799sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
800 GrWrapOwnership,
801 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400802 GrIOType ioType) {
803 GrD3DTextureResourceInfo textureInfo;
804 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
805 return nullptr;
806 }
807
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400808 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400809 return nullptr;
810 }
811
812 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
813 return nullptr;
814 }
815
816 // TODO: support protected context
817 if (tex.isProtected()) {
818 return nullptr;
819 }
820
821 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
822 SkASSERT(state);
823 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
824 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500825}
826
827sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400828 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500829 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400830 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500831}
832
833sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
834 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500835 GrWrapOwnership ownership,
836 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400837 GrD3DTextureResourceInfo textureInfo;
838 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
839 return nullptr;
840 }
841
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400842 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400843 return nullptr;
844 }
845
846 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
847 return nullptr;
848 }
849 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
850 return nullptr;
851 }
852
853 // TODO: support protected context
854 if (tex.isProtected()) {
855 return nullptr;
856 }
857
858 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
859
860 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
861 SkASSERT(state);
862
863 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
864 sampleCnt, cacheable,
865 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500866}
867
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400868sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400869 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
870 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
871 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
872 // creating and owning the MSAA images.
873 if (rt.sampleCnt() > 1) {
874 return nullptr;
875 }
876
877 GrD3DTextureResourceInfo info;
878 if (!rt.getD3DTextureResourceInfo(&info)) {
879 return nullptr;
880 }
881
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400882 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400883 return nullptr;
884 }
885
886 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
887 return nullptr;
888 }
889
890 // TODO: support protected context
891 if (rt.isProtected()) {
892 return nullptr;
893 }
894
895 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
896
897 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
898 this, rt.dimensions(), 1, info, std::move(state));
899
900 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
901 SkASSERT(!rt.stencilBits());
902 if (tgt) {
903 SkASSERT(tgt->canAttemptStencilAttachment());
904 }
905
906 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500907}
908
909sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400910 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400911
912 GrD3DTextureResourceInfo textureInfo;
913 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
914 return nullptr;
915 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400916 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400917 return nullptr;
918 }
919
920 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
921 return nullptr;
922 }
923
924 // TODO: support protected context
925 if (tex.isProtected()) {
926 return nullptr;
927 }
928
929 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
930 if (!sampleCnt) {
931 return nullptr;
932 }
933
934 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
935 SkASSERT(state);
936
937 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
938 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500939}
940
941sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400942 GrAccessPattern accessPattern, const void* data) {
943 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
944 if (data && buffer) {
945 buffer->updateData(data, sizeInBytes);
946 }
947
948 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500949}
950
951GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
Greg Daniele77162e2020-09-21 15:32:11 -0400952 const GrRenderTarget* rt, SkISize dimensions, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400953 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
Greg Daniele77162e2020-09-21 15:32:11 -0400954 SkASSERT(dimensions.width() >= rt->width());
955 SkASSERT(dimensions.height() >= rt->height());
Jim Van Verth4f51f472020-04-13 11:02:21 -0400956
957 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
958
959 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
Greg Daniele77162e2020-09-21 15:32:11 -0400960 dimensions,
Jim Van Verth4f51f472020-04-13 11:02:21 -0400961 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 }
Jim Van Verthb8ae7fa2020-09-08 15:51:14 -04001237 ID3D12Resource* textureResource = info.fResource.Get();
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001238 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 Verthb8ae7fa2020-09-08 15:51:14 -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}