blob: 1f91bf9a91d04bdee098c1580b342b465768d328 [file] [log] [blame]
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001/*
Jim Van Verth03b8ab22020-02-24 11:36:15 -05002 * Copyright 2020 Google LLC
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05008#include "src/gpu/d3d/GrD3DGpu.h"
9
Jim Van Verth96bfeff2020-04-09 14:36:12 -040010#include "include/gpu/GrBackendSurface.h"
Jim Van Verth9aa9a682020-04-01 10:13:48 -040011#include "include/gpu/d3d/GrD3DBackendContext.h"
Jim Van Verthc632aa62020-04-17 16:58:20 -040012#include "src/core/SkConvertPixels.h"
Mike Reed13711eb2020-07-14 17:16:32 -040013#include "src/core/SkMipmap.h"
Jim Van Verth43a6e172020-06-23 11:59:08 -040014#include "src/gpu/GrBackendUtils.h"
Jim Van Verthc632aa62020-04-17 16:58:20 -040015#include "src/gpu/GrDataUtils.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040016#include "src/gpu/GrTexture.h"
Jim Van Verthd6ad4802020-04-03 14:59:20 -040017#include "src/gpu/d3d/GrD3DBuffer.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050018#include "src/gpu/d3d/GrD3DCaps.h"
19#include "src/gpu/d3d/GrD3DOpsRenderPass.h"
Jim Van Verthc1a67b52020-06-25 13:10:29 -040020#include "src/gpu/d3d/GrD3DSemaphore.h"
Jim Van Verth4f51f472020-04-13 11:02:21 -040021#include "src/gpu/d3d/GrD3DStencilAttachment.h"
Jim Van Verthaa90dad2020-03-30 15:00:39 -040022#include "src/gpu/d3d/GrD3DTexture.h"
23#include "src/gpu/d3d/GrD3DTextureRenderTarget.h"
24#include "src/gpu/d3d/GrD3DUtil.h"
Greg Daniel5fc5c812020-04-23 10:30:23 -040025#include "src/sksl/SkSLCompiler.h"
Greg Daniel31a7b072020-02-26 15:31:49 -050026
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040027#if GR_TEST_UTILS
28#include <DXProgrammableCapture.h>
29#endif
30
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050031sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext,
Adlai Holler3d0359a2020-07-09 15:35:55 -040032 const GrContextOptions& contextOptions, GrDirectContext* direct) {
33 return sk_sp<GrGpu>(new GrD3DGpu(direct, contextOptions, backendContext));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050034}
35
Greg Daniel83ed2132020-03-24 13:15:33 -040036// This constant determines how many OutstandingCommandLists are allocated together as a block in
37// the deque. As such it needs to balance allocating too much memory vs. incurring
38// allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding
39// command lists we expect to see.
40static const int kDefaultOutstandingAllocCnt = 8;
41
Jim Van Verth711b0392020-07-23 16:45:00 -040042// constants have to be aligned to 256
43constexpr int kConstantAlignment = 256;
44
Adlai Holler3d0359a2020-07-09 15:35:55 -040045GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050046 const GrD3DBackendContext& backendContext)
Adlai Holler3d0359a2020-07-09 15:35:55 -040047 : INHERITED(direct)
Jim Van Verth03b8ab22020-02-24 11:36:15 -050048 , fDevice(backendContext.fDevice)
Greg Daniel02c45902020-03-09 10:58:09 -040049 , fQueue(backendContext.fQueue)
Greg Daniel83ed2132020-03-24 13:15:33 -040050 , fResourceProvider(this)
Greg Danielcffb0622020-07-16 13:19:17 -040051 , fStagingBufferManager(this)
Jim Van Verth711b0392020-07-23 16:45:00 -040052 , fConstantsRingBuffer(this, 128 * 1024, kConstantAlignment, GrGpuBufferType::kVertex)
Greg Daniel5fc5c812020-04-23 10:30:23 -040053 , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt)
54 , fCompiler(new SkSL::Compiler()) {
Jim Van Verth8ec13302020-02-26 12:59:56 -050055 fCaps.reset(new GrD3DCaps(contextOptions,
Jim Van Verth9aa9a682020-04-01 10:13:48 -040056 backendContext.fAdapter.get(),
57 backendContext.fDevice.get()));
Greg Daniel85da3362020-03-09 15:18:35 -040058
59 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
Greg Daniele52c9782020-03-23 14:18:37 -040060 SkASSERT(fCurrentDirectCommandList);
Greg Daniel83ed2132020-03-24 13:15:33 -040061
62 SkASSERT(fCurrentFenceValue == 0);
Jim Van Verthe3810362020-07-01 10:12:11 -040063 GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE,
64 IID_PPV_ARGS(&fFence)));
Jim Van Verth9b5e16c2020-04-20 10:45:52 -040065
66#if GR_TEST_UTILS
67 HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis));
68 if (FAILED(getAnalysis)) {
69 fGraphicsAnalysis = nullptr;
70 }
71#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050072}
73
Greg Daniel83ed2132020-03-24 13:15:33 -040074GrD3DGpu::~GrD3DGpu() {
75 this->destroyResources();
76}
77
78void GrD3DGpu::destroyResources() {
79 if (fCurrentDirectCommandList) {
80 fCurrentDirectCommandList->close();
Jim Van Verthba7f2292020-04-21 08:56:47 -040081 fCurrentDirectCommandList->reset();
Greg Daniel83ed2132020-03-24 13:15:33 -040082 }
83
84 // We need to make sure everything has finished on the queue.
Jim Van Verth3b0d7d12020-07-06 11:52:42 -040085 this->waitForQueueCompletion();
Greg Daniel83ed2132020-03-24 13:15:33 -040086
87 SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();)
88
89 // We used a placement new for each object in fOutstandingCommandLists, so we're responsible
90 // for calling the destructor on each of them as well.
91 while (!fOutstandingCommandLists.empty()) {
Jim Van Verthf43da142020-06-09 16:34:43 -040092 OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -040093 SkASSERT(list->fFenceValue <= fenceValue);
94 // No reason to recycle the command lists since we are destroying all resources anyways.
95 list->~OutstandingCommandList();
Jim Van Verthf43da142020-06-09 16:34:43 -040096 fOutstandingCommandLists.pop_front();
Greg Daniel83ed2132020-03-24 13:15:33 -040097 }
Jim Van Verthf2788862020-06-03 17:33:12 -040098
Greg Danielcffb0622020-07-16 13:19:17 -040099 fStagingBufferManager.reset();
100
Jim Van Verthf2788862020-06-03 17:33:12 -0400101 fResourceProvider.destroyResources();
Greg Daniel83ed2132020-03-24 13:15:33 -0400102}
Greg Daniel31a7b072020-02-26 15:31:49 -0500103
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500104GrOpsRenderPass* GrD3DGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -0400105 GrRenderTarget* rt, GrStencilAttachment*,
106 GrSurfaceOrigin origin, const SkIRect& bounds,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500107 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Daniel31a7b072020-02-26 15:31:49 -0500108 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500109 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel31a7b072020-02-26 15:31:49 -0500110 if (!fCachedOpsRenderPass) {
111 fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this));
112 }
113
114 if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) {
115 return nullptr;
116 }
117 return fCachedOpsRenderPass.get();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500118}
119
Jim Van Verthc632aa62020-04-17 16:58:20 -0400120bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) {
Greg Daniel83ed2132020-03-24 13:15:33 -0400121 SkASSERT(fCurrentDirectCommandList);
122
Jim Van Verth3eadce22020-06-01 11:34:49 -0400123 fResourceProvider.prepForSubmit();
124
Jim Van Verthc632aa62020-04-17 16:58:20 -0400125 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
126 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
127 return false;
128 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
129 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400130 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400131 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400132 }
133 return true;
134 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400135
Greg Daniela581a8b2020-06-19 15:22:18 -0400136 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
137 // uniform data as dirty.
138 fResourceProvider.markPipelineStateUniformsDirty();
139
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400140 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400141 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400142 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400143
Jim Van Verthc632aa62020-04-17 16:58:20 -0400144 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400145 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400146 }
147
Greg Daniel83ed2132020-03-24 13:15:33 -0400148 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
149
150 // This should be done after we have a new command list in case the freeing of any resources
151 // held by a finished command list causes us send a new command to the gpu (like changing the
152 // resource state.
153 this->checkForFinishedCommandLists();
154
155 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400156 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400157}
158
159void GrD3DGpu::checkForFinishedCommandLists() {
160 uint64_t currentFenceValue = fFence->GetCompletedValue();
161
162 // Iterate over all the outstanding command lists to see if any have finished. The commands
163 // lists are in order from oldest to newest, so we start at the front to check if their fence
164 // value is less than the last signaled value. If so we pop it off and move onto the next.
165 // Repeat till we find a command list that has not finished yet (and all others afterwards are
166 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400167 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
168 while (front && front->fFenceValue <= currentFenceValue) {
169 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400170 // Since we used placement new we are responsible for calling the destructor manually.
171 front->~OutstandingCommandList();
172 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400173 fResourceProvider.recycleDirectCommandList(std::move(currList));
174 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400175 }
176}
177
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400178void GrD3DGpu::waitForQueueCompletion() {
179 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
180 HANDLE fenceEvent;
181 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
182 SkASSERT(fenceEvent);
183 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
184 WaitForSingleObject(fenceEvent, INFINITE);
185 CloseHandle(fenceEvent);
186 }
187}
188
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500189void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400190 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
191
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500192 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400193 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500194}
195
Greg Danield4928d02020-06-19 11:13:26 -0400196void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
197 GrGpuFinishedContext finishedContext) {
198 SkASSERT(finishedProc);
199 sk_sp<GrRefCntedCallback> finishedCallback(
200 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400201 this->addFinishedCallback(std::move(finishedCallback));
202}
203
204void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
205 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400206 // Besides the current command list, we also add the finishedCallback to the newest outstanding
207 // command list. Our contract for calling the proc is that all previous submitted command lists
208 // have finished when we call it. However, if our current command list has no work when it is
209 // flushed it will drop its ref to the callback immediately. But the previous work may not have
210 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
211 // must finish after all previously submitted command lists.
212 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
213 if (back) {
214 back->fCommandList->addFinishedCallback(finishedCallback);
215 }
216 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
217}
218
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500219void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) {
220 // TODO
221}
222
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400223sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
224 DXGI_FORMAT dxgiFormat,
225 GrRenderable renderable,
226 int renderTargetSampleCnt,
227 SkBudgeted budgeted,
228 GrProtected isProtected,
229 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400230 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400231 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400232 if (renderable == GrRenderable::kYes) {
233 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
234 }
235
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400236 // 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 -0400237 // requested, this describes the resolved texture. Therefore we always have samples set
238 // to 1.
239 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400240 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400241 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
242 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
243 // might want to manually set alignment to 4KB for smaller textures
244 resourceDesc.Alignment = 0;
245 resourceDesc.Width = dimensions.fWidth;
246 resourceDesc.Height = dimensions.fHeight;
247 resourceDesc.DepthOrArraySize = 1;
248 resourceDesc.MipLevels = mipLevelCount;
249 resourceDesc.Format = dxgiFormat;
250 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400251 // quality levels are only supported for tiled resources so ignore for now
252 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400253 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400254 resourceDesc.Flags = usageFlags;
255
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400256 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400257 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400258 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400259 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400260 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400261 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400262 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400263 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400264}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400265
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400266sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
267 const GrBackendFormat& format,
268 GrRenderable renderable,
269 int renderTargetSampleCnt,
270 SkBudgeted budgeted,
271 GrProtected isProtected,
272 int mipLevelCount,
273 uint32_t levelClearMask) {
274 DXGI_FORMAT dxgiFormat;
275 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
276 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
277
Brian Salomona6db5102020-07-21 09:56:23 -0400278 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
279 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400280
281 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
282 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400283 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400284 if (!tex) {
285 return nullptr;
286 }
287
288 if (levelClearMask) {
289 // TODO
290 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400291
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400292 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500293}
294
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400295static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
296 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
297 UINT* numRows, UINT64* rowSizeInBytes,
298 const void* compressedData, int numMipLevels) {
299 SkASSERT(compressedData && numMipLevels);
300 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
301 SkASSERT(mapPtr);
302
303 const char* src = static_cast<const char*>(compressedData);
304 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
305 // copy data into the buffer, skipping any trailing bytes
306 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
307 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
308 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
309 numRows[currentMipLevel]);
310 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
311 }
312}
313
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500314sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
315 const GrBackendFormat& format,
316 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400317 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500318 GrProtected isProtected,
319 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400320 DXGI_FORMAT dxgiFormat;
321 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
322 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
323
324 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
325 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400326 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400327
328 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400329 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400330 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400331 }
Brian Salomona6db5102020-07-21 09:56:23 -0400332 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
333 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400334
335 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
336 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400337 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400338 if (!d3dTex) {
339 return nullptr;
340 }
341
342 ID3D12Resource* d3dResource = d3dTex->d3dResource();
343 SkASSERT(d3dResource);
344 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
345 // Either upload only the first miplevel or all miplevels
346 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
347
348 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
349 SkAutoTMalloc<UINT> numRows(mipLevelCount);
350 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
351 UINT64 combinedBufferSize;
352 // We reset the width and height in the description to match our subrectangle size
353 // so we don't end up allocating more space than we need.
354 desc.Width = dimensions.width();
355 desc.Height = dimensions.height();
356 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
357 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
358 SkASSERT(combinedBufferSize);
359
Greg Danielcffb0622020-07-16 13:19:17 -0400360 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
361 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
362 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400363 return false;
364 }
Greg Danielcffb0622020-07-16 13:19:17 -0400365
366 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400367
368 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
369 rowSizeInBytes.get(), data, mipLevelCount);
370
Greg Danielcffb0622020-07-16 13:19:17 -0400371 // Update the offsets in the footprints to be relative to the slice's offset
372 for (int i = 0; i < mipLevelCount; ++i) {
373 placedFootprints[i].Offset += slice.fOffset;
374 }
375
Greg Daniel69267912020-07-24 10:42:53 -0400376 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400377 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
378 placedFootprints.get(), 0, 0);
379
380 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500381}
382
Jim Van Verth9145f782020-04-28 12:01:12 -0400383static int get_surface_sample_cnt(GrSurface* surf) {
384 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
385 return rt->numSamples();
386 }
387 return 0;
388}
389
390bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
391 const SkIPoint& dstPoint) {
392
393 if (src->isProtected() && !dst->isProtected()) {
394 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400395 return false;
396 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400397
398 int dstSampleCnt = get_surface_sample_cnt(dst);
399 int srcSampleCnt = get_surface_sample_cnt(src);
400
401 GrD3DTextureResource* dstTexResource;
402 GrD3DTextureResource* srcTexResource;
403 GrRenderTarget* dstRT = dst->asRenderTarget();
404 if (dstRT) {
405 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
406 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
407 } else {
408 SkASSERT(dst->asTexture());
409 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
410 }
411 GrRenderTarget* srcRT = src->asRenderTarget();
412 if (srcRT) {
413 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
414 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
415 } else {
416 SkASSERT(src->asTexture());
417 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
418 }
419
420 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
421 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
422
423 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
424 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
425 return true;
426 }
427
428 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
429 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
430 return true;
431 }
432
433 return false;
434}
435
436void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
437 GrD3DTextureResource* dstResource,
438 GrD3DTextureResource* srcResource,
439 const SkIRect& srcRect, const SkIPoint& dstPoint) {
440#ifdef SK_DEBUG
441 int dstSampleCnt = get_surface_sample_cnt(dst);
442 int srcSampleCnt = get_surface_sample_cnt(src);
443 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
444 DXGI_FORMAT srcFormat;
445 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
446 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
447#endif
448 if (src->isProtected() && !dst->isProtected()) {
449 SkDebugf("Can't copy from protected memory to non-protected");
450 return;
451 }
452
453 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
454 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
455
456 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
457 dstLocation.pResource = dstResource->d3dResource();
458 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
459 dstLocation.SubresourceIndex = 0;
460
461 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
462 srcLocation.pResource = srcResource->d3dResource();
463 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
464 srcLocation.SubresourceIndex = 0;
465
466 D3D12_BOX srcBox = {};
467 srcBox.left = srcRect.fLeft;
468 srcBox.top = srcRect.fTop;
469 srcBox.right = srcRect.fRight;
470 srcBox.bottom = srcRect.fBottom;
471 srcBox.front = 0;
472 srcBox.back = 1;
473 // TODO: use copyResource if copying full resource and sizes match
Greg Daniel69267912020-07-24 10:42:53 -0400474 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
475 &dstLocation,
476 dstPoint.fX, dstPoint.fY,
477 srcResource->resource(),
478 &srcLocation,
479 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400480
481 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
482 srcRect.width(), srcRect.height());
483 // The rect is already in device space so we pass in kTopLeft so no flip is done.
484 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
485}
486
487void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
488 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400489 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
490 SkASSERT(srcRT);
491
492 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
493}
494
495void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
496 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
497 SkASSERT(dst);
498 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
499
500 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
501
502 GrD3DTextureResource* dstTextureResource;
503 GrRenderTarget* dstRT = dst->asRenderTarget();
504 if (dstRT) {
505 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
506 } else {
507 SkASSERT(dst->asTexture());
508 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
509 }
510
511 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
512 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
513
514 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
515 src->msaaTextureResource(), &srcRect);
516}
517
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400518void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400519 SkASSERT(target->numSamples() > 1);
520 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
521 SkASSERT(rt->msaaTextureResource());
522
523 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400524}
525
Jim Van Verthba7f2292020-04-21 08:56:47 -0400526bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
527 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
528 size_t rowBytes) {
529 SkASSERT(surface);
530
531 if (surfaceColorType != dstColorType) {
532 return false;
533 }
534
535 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400536 GrD3DTextureResource* texResource = nullptr;
537 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
538 if (rt) {
539 texResource = rt;
540 } else {
541 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
542 }
543
544 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400545 return false;
546 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400547
Jim Van Verthba7f2292020-04-21 08:56:47 -0400548 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400549 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400550 SkASSERT(srcLocation.pResource);
551 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
552 srcLocation.SubresourceIndex = 0;
553
554 D3D12_BOX srcBox = {};
555 srcBox.left = left;
556 srcBox.top = top;
557 srcBox.right = left + width;
558 srcBox.bottom = top + height;
559 srcBox.front = 0;
560 srcBox.back = 1;
561
562 // Set up dst location and create transfer buffer
563 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
564 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400565 UINT64 transferTotalBytes;
566 const UINT64 baseOffset = 0;
567 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
568 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400569 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400570 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400571 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400572 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400573 return false;
574 }
575 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400576
577 // TODO: implement some way of reusing buffers instead of making a new one every time.
578 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
579 GrGpuBufferType::kXferGpuToCpu,
580 kDynamic_GrAccessPattern);
581 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
582 dstLocation.pResource = d3dBuf->d3dResource();
583
584 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400585 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400586
Greg Daniel69267912020-07-24 10:42:53 -0400587 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
588 texResource->resource(), &srcLocation,
589 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400590 this->submitDirectCommandList(SyncQueue::kForce);
591
592 const void* mappedMemory = transferBuffer->map();
593
594 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400595 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400596
597 transferBuffer->unmap();
598
599 return true;
600}
601
602bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
603 GrColorType surfaceColorType, GrColorType srcColorType,
604 const GrMipLevel texels[], int mipLevelCount,
605 bool prepForTexSampling) {
606 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
607 if (!d3dTex) {
608 return false;
609 }
610
611 // Make sure we have at least the base level
612 if (!mipLevelCount || !texels[0].fPixels) {
613 return false;
614 }
615
616 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
617 bool success = false;
618
619 // Need to change the resource state to COPY_DEST in order to upload to it
620 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
621
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400622 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400623 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
624 mipLevelCount);
625
626 if (prepForTexSampling) {
627 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
628 }
629
630 return success;
631}
632
633bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
634 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
635 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
636 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
637 SkASSERT(1 == mipLevelCount ||
638 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
639
640 // We assume that if the texture has mip levels, we either upload to all the levels or just the
641 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400642 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400643
644 if (width == 0 || height == 0) {
645 return false;
646 }
647
648 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
649 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
650
651 ID3D12Resource* d3dResource = tex->d3dResource();
652 SkASSERT(d3dResource);
653 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
654 // Either upload only the first miplevel or all miplevels
655 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
656
657 if (1 == mipLevelCount && !texels[0].fPixels) {
658 return true; // no data to upload
659 }
660
661 for (int i = 0; i < mipLevelCount; ++i) {
662 // We do not allow any gaps in the mip data
663 if (!texels[i].fPixels) {
664 return false;
665 }
666 }
667
668 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400669 UINT64 combinedBufferSize;
670 // We reset the width and height in the description to match our subrectangle size
671 // so we don't end up allocating more space than we need.
672 desc.Width = width;
673 desc.Height = height;
674 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400675 nullptr, nullptr, &combinedBufferSize);
676 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400677 SkASSERT(combinedBufferSize);
678
Greg Danielcffb0622020-07-16 13:19:17 -0400679 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
680 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
681 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400682 return false;
683 }
Greg Danielcffb0622020-07-16 13:19:17 -0400684
685 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400686
687 int currentWidth = width;
688 int currentHeight = height;
689 int layerHeight = tex->height();
690
691 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
692 if (texels[currentMipLevel].fPixels) {
693 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
694
Jim Van Verthfdd36852020-04-21 16:29:44 -0400695 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400696 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
697
698 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
699
700 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400701 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400702 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
703 src, srcRowBytes, trimRowBytes, currentHeight);
704 }
705 currentWidth = std::max(1, currentWidth / 2);
706 currentHeight = std::max(1, currentHeight / 2);
707 layerHeight = currentHeight;
708 }
709
Greg Danielcffb0622020-07-16 13:19:17 -0400710 // Update the offsets in the footprints to be relative to the slice's offset
711 for (int i = 0; i < mipLevelCount; ++i) {
712 placedFootprints[i].Offset += slice.fOffset;
713 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400714
Greg Daniel69267912020-07-24 10:42:53 -0400715 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400716 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
717 placedFootprints.get(), left, top);
718
719 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400720 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400721 }
722
723 return true;
724}
725
Jim Van Verth9145f782020-04-28 12:01:12 -0400726static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
727 if (!info.fResource.get()) {
728 return false;
729 }
730 return true;
731}
732
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400733static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
734 if (!caps.isFormatTexturable(info.fFormat)) {
735 return false;
736 }
737 return true;
738}
739
740static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
741 int sampleCnt) {
742 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
743 return false;
744 }
745 return true;
746}
747
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400748sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
749 GrWrapOwnership,
750 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400751 GrIOType ioType) {
752 GrD3DTextureResourceInfo textureInfo;
753 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
754 return nullptr;
755 }
756
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400757 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400758 return nullptr;
759 }
760
761 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
762 return nullptr;
763 }
764
765 // TODO: support protected context
766 if (tex.isProtected()) {
767 return nullptr;
768 }
769
770 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
771 SkASSERT(state);
772 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
773 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500774}
775
776sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400777 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500778 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400779 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500780}
781
782sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
783 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500784 GrWrapOwnership ownership,
785 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400786 GrD3DTextureResourceInfo textureInfo;
787 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
788 return nullptr;
789 }
790
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400791 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400792 return nullptr;
793 }
794
795 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
796 return nullptr;
797 }
798 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
799 return nullptr;
800 }
801
802 // TODO: support protected context
803 if (tex.isProtected()) {
804 return nullptr;
805 }
806
807 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
808
809 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
810 SkASSERT(state);
811
812 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
813 sampleCnt, cacheable,
814 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500815}
816
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400817sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400818 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
819 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
820 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
821 // creating and owning the MSAA images.
822 if (rt.sampleCnt() > 1) {
823 return nullptr;
824 }
825
826 GrD3DTextureResourceInfo info;
827 if (!rt.getD3DTextureResourceInfo(&info)) {
828 return nullptr;
829 }
830
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400831 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400832 return nullptr;
833 }
834
835 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
836 return nullptr;
837 }
838
839 // TODO: support protected context
840 if (rt.isProtected()) {
841 return nullptr;
842 }
843
844 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
845
846 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
847 this, rt.dimensions(), 1, info, std::move(state));
848
849 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
850 SkASSERT(!rt.stencilBits());
851 if (tgt) {
852 SkASSERT(tgt->canAttemptStencilAttachment());
853 }
854
855 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500856}
857
858sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400859 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400860
861 GrD3DTextureResourceInfo textureInfo;
862 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
863 return nullptr;
864 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400865 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400866 return nullptr;
867 }
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 if (!sampleCnt) {
880 return nullptr;
881 }
882
883 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
884 SkASSERT(state);
885
886 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
887 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500888}
889
890sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400891 GrAccessPattern accessPattern, const void* data) {
892 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
893 if (data && buffer) {
894 buffer->updateData(data, sizeInBytes);
895 }
896
897 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500898}
899
900GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
901 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400902 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
903 SkASSERT(width >= rt->width());
904 SkASSERT(height >= rt->height());
905
906 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
907
908 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
909 width,
910 height,
911 numStencilSamples,
912 sFmt));
913 fStats.incStencilAttachmentCreates();
914 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500915}
916
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400917bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
918 SkISize dimensions,
919 GrTexturable texturable,
920 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400921 GrMipmapped mipMapped,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400922 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400923 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400924 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400925
926 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
927 return false;
928 }
929
930 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
931 return false;
932 }
933
934 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
935 return false;
936 }
937
938 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400939 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400940 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400941 }
942
943 // create the texture
944 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
945 if (renderable == GrRenderable::kYes) {
946 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
947 }
948
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400949 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400950 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400951 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400952 resourceDesc.Width = dimensions.fWidth;
953 resourceDesc.Height = dimensions.fHeight;
954 resourceDesc.DepthOrArraySize = 1;
955 resourceDesc.MipLevels = numMipLevels;
956 resourceDesc.Format = dxgiFormat;
957 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400958 // quality levels are only supported for tiled resources so ignore for now
959 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400960 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400961 resourceDesc.Flags = usageFlags;
962
Jim Van Verth280d4f72020-05-04 10:04:24 -0400963 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
964 D3D12_CLEAR_VALUE clearValue = {};
965 if (renderable == GrRenderable::kYes) {
966 clearValue.Format = dxgiFormat;
967 // Assume transparent black
968 clearValue.Color[0] = 0;
969 clearValue.Color[1] = 0;
970 clearValue.Color[2] = 0;
971 clearValue.Color[3] = 0;
972 clearValuePtr = &clearValue;
973 }
974
Greg Daniel16032b32020-05-06 15:31:10 -0400975 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
976 ? D3D12_RESOURCE_STATE_RENDER_TARGET
977 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400978 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400979 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400980 SkDebugf("Failed to init texture resource info\n");
981 return false;
982 }
983
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400984 return true;
985}
986
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500987GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400988 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400989 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400990 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400991 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400992 this->handleDirtyContext();
993
994 const GrD3DCaps& caps = this->d3dCaps();
995
996 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
997 return {};
998 }
999
1000 DXGI_FORMAT dxgiFormat;
1001 if (!format.asDxgiFormat(&dxgiFormat)) {
1002 return {};
1003 }
1004
1005 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1006 if (!caps.isFormatTexturable(dxgiFormat)) {
1007 return {};
1008 }
1009
1010 GrD3DTextureResourceInfo info;
1011 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1012 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001013 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001014 return {};
1015 }
1016
1017 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001018}
1019
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001020static void copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
1021 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1022 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001023 SkASSERT(srcData && numMipLevels);
1024 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1025 SkASSERT(mapPtr);
1026
1027 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
1028
1029 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1030 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1031
1032 // copy data into the buffer, skipping any trailing bytes
1033 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1034 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1035 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1036 trimRowBytes, srcData[currentMipLevel].height());
1037 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001038}
1039
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001040static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1041 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1042 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1043 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001044 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001045 if (colorType == GrColorType::kUnknown) {
1046 return false;
1047 }
1048 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1049 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1050 return false;
1051 }
1052
1053 return true;
1054}
1055
Greg Daniel16032b32020-05-06 15:31:10 -04001056bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1057 sk_sp<GrRefCntedCallback> finishedCallback,
1058 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001059 GrD3DTextureResourceInfo info;
1060 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1061
1062 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1063 SkASSERT(state);
1064 sk_sp<GrD3DTexture> texture =
1065 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1066 GrWrapCacheable::kNo,
1067 kRW_GrIOType, info, std::move(state));
1068 if (!texture) {
1069 return false;
1070 }
1071
1072 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1073 if (!cmdList) {
1074 return false;
1075 }
1076
1077 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1078
1079 ID3D12Resource* d3dResource = texture->d3dResource();
1080 SkASSERT(d3dResource);
1081 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1082 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001083 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001084 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001085 backendTexture.dimensions().height()) + 1;
1086 }
1087 SkASSERT(mipLevelCount == info.fLevelCount);
1088 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1089 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001090 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1091 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001092 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001093 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001094 SkASSERT(combinedBufferSize);
1095 if (data->type() == BackendTextureData::Type::kColor &&
1096 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1097 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1098 combinedBufferSize =
1099 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1100 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1101 placedFootprints[i].Offset = 0;
1102 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1103 }
1104 }
1105
Greg Danielcffb0622020-07-16 13:19:17 -04001106 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1107 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1108 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001109 return false;
1110 }
Greg Danielcffb0622020-07-16 13:19:17 -04001111
1112 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001113 SkASSERT(bufferData);
1114
Jim Van Verth43a6e172020-06-23 11:59:08 -04001115 if (data->type() == BackendTextureData::Type::kPixmaps) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001116 copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
1117 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001118 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001119 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1120 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001121 } else {
1122 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1123 SkImage::CompressionType compression =
1124 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1125 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001126 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1127 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001128 return false;
1129 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001130 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001131 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1132 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001133 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001134 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001135 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001136 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001137 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1138 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001139 }
1140 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001141
Greg Danielcffb0622020-07-16 13:19:17 -04001142 // Update the offsets in the footprints to be relative to the slice's offset
1143 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1144 placedFootprints[i].Offset += slice.fOffset;
1145 }
1146
Greg Daniel69267912020-07-24 10:42:53 -04001147 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001148 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1149 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001150
1151 if (finishedCallback) {
1152 this->addFinishedCallback(std::move(finishedCallback));
1153 }
1154
Greg Daniel16032b32020-05-06 15:31:10 -04001155 return true;
1156}
1157
Greg Danielc1ad77c2020-05-06 11:40:03 -04001158GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001159 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001160 GrProtected isProtected) {
1161 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1162 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001163}
1164
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001165bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001166 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001167 const BackendTextureData* data) {
1168 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001169}
1170
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001171void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001172 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1173 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001174}
1175
Robert Phillips979b2232020-02-20 10:47:29 -05001176bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1177 return false;
1178}
1179
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001180#if GR_TEST_UTILS
1181bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001182 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1183
1184 GrD3DTextureResourceInfo info;
1185 if (!tex.getD3DTextureResourceInfo(&info)) {
1186 return false;
1187 }
1188 ID3D12Resource* textureResource = info.fResource.get();
1189 if (!textureResource) {
1190 return false;
1191 }
1192 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001193}
1194
1195GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001196 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001197 this->handleDirtyContext();
1198
1199 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001200 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001201 }
1202
1203 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1204
1205 GrD3DTextureResourceInfo info;
1206 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001207 GrRenderable::kYes, GrMipmapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001208 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001209 return {};
1210 }
1211
1212 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001213}
1214
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001215void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1216 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1217
1218 GrD3DTextureResourceInfo info;
1219 if (rt.getD3DTextureResourceInfo(&info)) {
1220 this->testingOnly_flushGpuAndSync();
1221 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1222 // is deleted.
1223 }
1224}
1225
1226void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001227 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001228}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001229
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001230void GrD3DGpu::testingOnly_startCapture() {
1231 if (fGraphicsAnalysis) {
1232 fGraphicsAnalysis->BeginCapture();
1233 }
1234}
1235
1236void GrD3DGpu::testingOnly_endCapture() {
1237 if (fGraphicsAnalysis) {
1238 fGraphicsAnalysis->EndCapture();
1239 }
1240}
1241#endif
1242
Jim Van Verthc632aa62020-04-17 16:58:20 -04001243///////////////////////////////////////////////////////////////////////////////
1244
Greg Daniela5a6b322020-04-23 12:52:27 -04001245void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001246 int numBarriers,
1247 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1248 SkASSERT(fCurrentDirectCommandList);
1249 SkASSERT(resource);
1250
Greg Daniela5a6b322020-04-23 12:52:27 -04001251 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001252}
1253
Greg Daniel69267912020-07-24 10:42:53 -04001254void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1255 int numBarriers,
1256 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1257 SkASSERT(fCurrentDirectCommandList);
1258 SkASSERT(buffer);
1259
1260 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1261 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1262}
1263
1264
Greg Daniel9efe3862020-06-11 11:51:06 -04001265void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1266 GrSurfaceProxy* proxies[],
1267 int numProxies,
1268 SkSurface::BackendSurfaceAccess access,
1269 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001270 SkASSERT(numProxies >= 0);
1271 SkASSERT(!numProxies || proxies);
1272
1273 // prepare proxies by transitioning to PRESENT renderState
1274 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1275 GrD3DTextureResource* resource;
1276 for (int i = 0; i < numProxies; ++i) {
1277 SkASSERT(proxies[i]->isInstantiated());
1278 if (GrTexture* tex = proxies[i]->peekTexture()) {
1279 resource = static_cast<GrD3DTexture*>(tex);
1280 } else {
1281 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1282 SkASSERT(rt);
1283 resource = static_cast<GrD3DRenderTarget*>(rt);
1284 }
1285 resource->prepareForPresent(this);
1286 }
1287 }
1288}
1289
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001290void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001291 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001292}
1293
Jim Van Verthc632aa62020-04-17 16:58:20 -04001294bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1295 if (syncCpu) {
1296 return this->submitDirectCommandList(SyncQueue::kForce);
1297 } else {
1298 return this->submitDirectCommandList(SyncQueue::kSkip);
1299 }
1300}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001301
1302std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1303 return GrD3DSemaphore::Make(this);
1304}
1305std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1306 const GrBackendSemaphore& semaphore,
1307 GrResourceProvider::SemaphoreWrapType,
1308 GrWrapOwnership) {
1309 SkASSERT(this->caps()->semaphoreSupport());
1310 GrD3DFenceInfo fenceInfo;
1311 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1312 return nullptr;
1313 }
1314 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1315}
1316
1317void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001318 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001319 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1320 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1321 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1322}
1323
1324void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001325 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001326 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1327 // TODO: Do we need to track the lifetime of this?
1328 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1329}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001330
1331GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001332 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001333 return fCurrentFenceValue;
1334}
1335
1336bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001337 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001338}