blob: 74d91f51a8d7c58e65abf5e8b4f160414ecf1808 [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 Verth711b0392020-07-23 16:45:00 -0400123 // set up constant data
124 fCurrentDirectCommandList->setCurrentConstantBuffer(&fConstantsRingBuffer);
125
Jim Van Verth3eadce22020-06-01 11:34:49 -0400126 fResourceProvider.prepForSubmit();
127
Jim Van Verthc632aa62020-04-17 16:58:20 -0400128 GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get());
129 if (result == GrD3DDirectCommandList::SubmitResult::kFailure) {
130 return false;
131 } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) {
132 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400133 this->waitForQueueCompletion();
Jim Van Verth682a2f42020-05-13 16:54:09 -0400134 this->checkForFinishedCommandLists();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400135 }
136 return true;
137 }
Greg Daniel83ed2132020-03-24 13:15:33 -0400138
Greg Daniela581a8b2020-06-19 15:22:18 -0400139 // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached
140 // uniform data as dirty.
141 fResourceProvider.markPipelineStateUniformsDirty();
142
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400143 GrFence fence = this->insertFence();
Greg Daniel83ed2132020-03-24 13:15:33 -0400144 new (fOutstandingCommandLists.push_back()) OutstandingCommandList(
Jim Van Verth1e6460d2020-06-30 15:47:52 -0400145 std::move(fCurrentDirectCommandList), fence);
Greg Daniel83ed2132020-03-24 13:15:33 -0400146
Jim Van Verthc632aa62020-04-17 16:58:20 -0400147 if (sync == SyncQueue::kForce) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400148 this->waitForQueueCompletion();
Jim Van Verthc632aa62020-04-17 16:58:20 -0400149 }
150
Greg Daniel83ed2132020-03-24 13:15:33 -0400151 fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList();
152
153 // This should be done after we have a new command list in case the freeing of any resources
154 // held by a finished command list causes us send a new command to the gpu (like changing the
155 // resource state.
156 this->checkForFinishedCommandLists();
157
158 SkASSERT(fCurrentDirectCommandList);
Jim Van Verthc632aa62020-04-17 16:58:20 -0400159 return true;
Greg Daniel83ed2132020-03-24 13:15:33 -0400160}
161
162void GrD3DGpu::checkForFinishedCommandLists() {
163 uint64_t currentFenceValue = fFence->GetCompletedValue();
164
165 // Iterate over all the outstanding command lists to see if any have finished. The commands
166 // lists are in order from oldest to newest, so we start at the front to check if their fence
167 // value is less than the last signaled value. If so we pop it off and move onto the next.
168 // Repeat till we find a command list that has not finished yet (and all others afterwards are
169 // also guaranteed to not have finished).
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400170 OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front();
171 while (front && front->fFenceValue <= currentFenceValue) {
172 std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList));
Greg Daniel83ed2132020-03-24 13:15:33 -0400173 // Since we used placement new we are responsible for calling the destructor manually.
174 front->~OutstandingCommandList();
175 fOutstandingCommandLists.pop_front();
Jim Van Verthdd3b4012020-06-30 15:28:17 -0400176 fResourceProvider.recycleDirectCommandList(std::move(currList));
177 front = (OutstandingCommandList*)fOutstandingCommandLists.front();
Greg Daniel83ed2132020-03-24 13:15:33 -0400178 }
179}
180
Jim Van Verth3b0d7d12020-07-06 11:52:42 -0400181void GrD3DGpu::waitForQueueCompletion() {
182 if (fFence->GetCompletedValue() < fCurrentFenceValue) {
183 HANDLE fenceEvent;
184 fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
185 SkASSERT(fenceEvent);
186 GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent));
187 WaitForSingleObject(fenceEvent, INFINITE);
188 CloseHandle(fenceEvent);
189 }
190}
191
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500192void GrD3DGpu::submit(GrOpsRenderPass* renderPass) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400193 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
194
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500195 // TODO: actually submit something here
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400196 fCachedOpsRenderPass.reset();
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500197}
198
Greg Danield4928d02020-06-19 11:13:26 -0400199void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
200 GrGpuFinishedContext finishedContext) {
201 SkASSERT(finishedProc);
202 sk_sp<GrRefCntedCallback> finishedCallback(
203 new GrRefCntedCallback(finishedProc, finishedContext));
Jim Van Verth43a6e172020-06-23 11:59:08 -0400204 this->addFinishedCallback(std::move(finishedCallback));
205}
206
207void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) {
208 SkASSERT(finishedCallback);
Greg Danield4928d02020-06-19 11:13:26 -0400209 // Besides the current command list, we also add the finishedCallback to the newest outstanding
210 // command list. Our contract for calling the proc is that all previous submitted command lists
211 // have finished when we call it. However, if our current command list has no work when it is
212 // flushed it will drop its ref to the callback immediately. But the previous work may not have
213 // finished. It is safe to only add the proc to the newest outstanding commandlist cause that
214 // must finish after all previously submitted command lists.
215 OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back();
216 if (back) {
217 back->fCommandList->addFinishedCallback(finishedCallback);
218 }
219 fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback));
220}
221
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500222void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) {
223 // TODO
224}
225
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400226sk_sp<GrD3DTexture> GrD3DGpu::createD3DTexture(SkISize dimensions,
227 DXGI_FORMAT dxgiFormat,
228 GrRenderable renderable,
229 int renderTargetSampleCnt,
230 SkBudgeted budgeted,
231 GrProtected isProtected,
232 int mipLevelCount,
Brian Salomona6db5102020-07-21 09:56:23 -0400233 GrMipmapStatus mipmapStatus) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400234 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400235 if (renderable == GrRenderable::kYes) {
236 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
237 }
238
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400239 // 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 -0400240 // requested, this describes the resolved texture. Therefore we always have samples set
241 // to 1.
242 SkASSERT(mipLevelCount > 0);
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400243 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400244 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
245 // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else
246 // might want to manually set alignment to 4KB for smaller textures
247 resourceDesc.Alignment = 0;
248 resourceDesc.Width = dimensions.fWidth;
249 resourceDesc.Height = dimensions.fHeight;
250 resourceDesc.DepthOrArraySize = 1;
251 resourceDesc.MipLevels = mipLevelCount;
252 resourceDesc.Format = dxgiFormat;
253 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400254 // quality levels are only supported for tiled resources so ignore for now
255 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400256 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400257 resourceDesc.Flags = usageFlags;
258
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400259 if (renderable == GrRenderable::kYes) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400260 return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400261 this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400262 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400263 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400264 return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400265 mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400266 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400267}
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400268
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400269sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions,
270 const GrBackendFormat& format,
271 GrRenderable renderable,
272 int renderTargetSampleCnt,
273 SkBudgeted budgeted,
274 GrProtected isProtected,
275 int mipLevelCount,
276 uint32_t levelClearMask) {
277 DXGI_FORMAT dxgiFormat;
278 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
279 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
280
Brian Salomona6db5102020-07-21 09:56:23 -0400281 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty
282 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400283
284 sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable,
285 renderTargetSampleCnt, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400286 mipLevelCount, mipmapStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400287 if (!tex) {
288 return nullptr;
289 }
290
291 if (levelClearMask) {
292 // TODO
293 }
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400294
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400295 return std::move(tex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500296}
297
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400298static void copy_compressed_data(char* mapPtr, DXGI_FORMAT dxgiFormat,
299 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
300 UINT* numRows, UINT64* rowSizeInBytes,
301 const void* compressedData, int numMipLevels) {
302 SkASSERT(compressedData && numMipLevels);
303 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
304 SkASSERT(mapPtr);
305
306 const char* src = static_cast<const char*>(compressedData);
307 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
308 // copy data into the buffer, skipping any trailing bytes
309 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
310 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
311 src, rowSizeInBytes[currentMipLevel], rowSizeInBytes[currentMipLevel],
312 numRows[currentMipLevel]);
313 src += numRows[currentMipLevel] * rowSizeInBytes[currentMipLevel];
314 }
315}
316
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500317sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions,
318 const GrBackendFormat& format,
319 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400320 GrMipmapped mipMapped,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500321 GrProtected isProtected,
322 const void* data, size_t dataSize) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400323 DXGI_FORMAT dxgiFormat;
324 SkAssertResult(format.asDxgiFormat(&dxgiFormat));
325 SkASSERT(GrDxgiFormatIsCompressed(dxgiFormat));
326
327 SkDEBUGCODE(SkImage::CompressionType compression = GrBackendFormatToCompressionType(format));
328 SkASSERT(dataSize == SkCompressedFormatDataSize(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400329 mipMapped == GrMipmapped::kYes));
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400330
331 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400332 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400333 mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400334 }
Brian Salomona6db5102020-07-21 09:56:23 -0400335 GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid
336 : GrMipmapStatus::kNotAllocated;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400337
338 sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo,
339 1, budgeted, isProtected,
Brian Salomona6db5102020-07-21 09:56:23 -0400340 mipLevelCount, mipmapStatus);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400341 if (!d3dTex) {
342 return nullptr;
343 }
344
345 ID3D12Resource* d3dResource = d3dTex->d3dResource();
346 SkASSERT(d3dResource);
347 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
348 // Either upload only the first miplevel or all miplevels
349 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
350
351 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
352 SkAutoTMalloc<UINT> numRows(mipLevelCount);
353 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
354 UINT64 combinedBufferSize;
355 // We reset the width and height in the description to match our subrectangle size
356 // so we don't end up allocating more space than we need.
357 desc.Width = dimensions.width();
358 desc.Height = dimensions.height();
359 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
360 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
361 SkASSERT(combinedBufferSize);
362
Greg Danielcffb0622020-07-16 13:19:17 -0400363 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
364 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
365 if (!slice.fBuffer) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400366 return false;
367 }
Greg Danielcffb0622020-07-16 13:19:17 -0400368
369 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400370
371 copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(),
372 rowSizeInBytes.get(), data, mipLevelCount);
373
Greg Danielcffb0622020-07-16 13:19:17 -0400374 // Update the offsets in the footprints to be relative to the slice's offset
375 for (int i = 0; i < mipLevelCount; ++i) {
376 placedFootprints[i].Offset += slice.fOffset;
377 }
378
379 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer);
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400380 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount,
381 placedFootprints.get(), 0, 0);
382
383 return std::move(d3dTex);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500384}
385
Jim Van Verth9145f782020-04-28 12:01:12 -0400386static int get_surface_sample_cnt(GrSurface* surf) {
387 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
388 return rt->numSamples();
389 }
390 return 0;
391}
392
393bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
394 const SkIPoint& dstPoint) {
395
396 if (src->isProtected() && !dst->isProtected()) {
397 SkDebugf("Can't copy from protected memory to non-protected");
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400398 return false;
399 }
Jim Van Verth9145f782020-04-28 12:01:12 -0400400
401 int dstSampleCnt = get_surface_sample_cnt(dst);
402 int srcSampleCnt = get_surface_sample_cnt(src);
403
404 GrD3DTextureResource* dstTexResource;
405 GrD3DTextureResource* srcTexResource;
406 GrRenderTarget* dstRT = dst->asRenderTarget();
407 if (dstRT) {
408 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT);
409 dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
410 } else {
411 SkASSERT(dst->asTexture());
412 dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture());
413 }
414 GrRenderTarget* srcRT = src->asRenderTarget();
415 if (srcRT) {
416 GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT);
417 srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT;
418 } else {
419 SkASSERT(src->asTexture());
420 srcTexResource = static_cast<GrD3DTexture*>(src->asTexture());
421 }
422
423 DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat();
424 DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat();
425
426 if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
427 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
428 return true;
429 }
430
431 if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) {
432 this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint);
433 return true;
434 }
435
436 return false;
437}
438
439void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src,
440 GrD3DTextureResource* dstResource,
441 GrD3DTextureResource* srcResource,
442 const SkIRect& srcRect, const SkIPoint& dstPoint) {
443#ifdef SK_DEBUG
444 int dstSampleCnt = get_surface_sample_cnt(dst);
445 int srcSampleCnt = get_surface_sample_cnt(src);
446 DXGI_FORMAT dstFormat = dstResource->dxgiFormat();
447 DXGI_FORMAT srcFormat;
448 SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat));
449 SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt));
450#endif
451 if (src->isProtected() && !dst->isProtected()) {
452 SkDebugf("Can't copy from protected memory to non-protected");
453 return;
454 }
455
456 dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
457 srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
458
459 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
460 dstLocation.pResource = dstResource->d3dResource();
461 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
462 dstLocation.SubresourceIndex = 0;
463
464 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
465 srcLocation.pResource = srcResource->d3dResource();
466 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
467 srcLocation.SubresourceIndex = 0;
468
469 D3D12_BOX srcBox = {};
470 srcBox.left = srcRect.fLeft;
471 srcBox.top = srcRect.fTop;
472 srcBox.right = srcRect.fRight;
473 srcBox.bottom = srcRect.fBottom;
474 srcBox.front = 0;
475 srcBox.back = 1;
476 // TODO: use copyResource if copying full resource and sizes match
477 fCurrentDirectCommandList->copyTextureRegion(dstResource->resource(),
478 &dstLocation,
479 dstPoint.fX, dstPoint.fY,
480 srcResource->resource(),
481 &srcLocation,
482 &srcBox);
483
484 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
485 srcRect.width(), srcRect.height());
486 // The rect is already in device space so we pass in kTopLeft so no flip is done.
487 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
488}
489
490void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
491 const SkIPoint& dstPoint) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400492 GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget());
493 SkASSERT(srcRT);
494
495 this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect);
496}
497
498void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY,
499 GrD3DRenderTarget* src, const SkIRect& srcIRect) {
500 SkASSERT(dst);
501 SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource());
502
503 D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom };
504
505 GrD3DTextureResource* dstTextureResource;
506 GrRenderTarget* dstRT = dst->asRenderTarget();
507 if (dstRT) {
508 dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT);
509 } else {
510 SkASSERT(dst->asTexture());
511 dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture());
512 }
513
514 dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST);
515 src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
516
517 fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY,
518 src->msaaTextureResource(), &srcRect);
519}
520
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400521void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Jim Van Verth275f4192020-07-07 15:36:41 -0400522 SkASSERT(target->numSamples() > 1);
523 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target);
524 SkASSERT(rt->msaaTextureResource());
525
526 this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400527}
528
Jim Van Verthba7f2292020-04-21 08:56:47 -0400529bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
530 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
531 size_t rowBytes) {
532 SkASSERT(surface);
533
534 if (surfaceColorType != dstColorType) {
535 return false;
536 }
537
538 // Set up src location and box
Jim Van Verthc12aad92020-04-24 16:19:01 -0400539 GrD3DTextureResource* texResource = nullptr;
540 GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget());
541 if (rt) {
542 texResource = rt;
543 } else {
544 texResource = static_cast<GrD3DTexture*>(surface->asTexture());
545 }
546
547 if (!texResource) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400548 return false;
549 }
Jim Van Verthc12aad92020-04-24 16:19:01 -0400550
Jim Van Verthba7f2292020-04-21 08:56:47 -0400551 D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
Jim Van Verthc12aad92020-04-24 16:19:01 -0400552 srcLocation.pResource = texResource->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400553 SkASSERT(srcLocation.pResource);
554 srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
555 srcLocation.SubresourceIndex = 0;
556
557 D3D12_BOX srcBox = {};
558 srcBox.left = left;
559 srcBox.top = top;
560 srcBox.right = left + width;
561 srcBox.bottom = top + height;
562 srcBox.front = 0;
563 srcBox.back = 1;
564
565 // Set up dst location and create transfer buffer
566 D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
567 dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400568 UINT64 transferTotalBytes;
569 const UINT64 baseOffset = 0;
570 D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
571 fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400572 nullptr, nullptr, &transferTotalBytes);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400573 SkASSERT(transferTotalBytes);
Jim Van Verthfdd36852020-04-21 16:29:44 -0400574 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
Jim Van Verthc12aad92020-04-24 16:19:01 -0400575 if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) {
Jim Van Verthfdd36852020-04-21 16:29:44 -0400576 return false;
577 }
578 size_t tightRowBytes = bpp * width;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400579
580 // TODO: implement some way of reusing buffers instead of making a new one every time.
581 sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
582 GrGpuBufferType::kXferGpuToCpu,
583 kDynamic_GrAccessPattern);
584 GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get());
585 dstLocation.pResource = d3dBuf->d3dResource();
586
587 // Need to change the resource state to COPY_SOURCE in order to download from it
Jim Van Verthc12aad92020-04-24 16:19:01 -0400588 texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400589
590 fCurrentDirectCommandList->copyTextureRegion(d3dBuf->resource(), &dstLocation, 0, 0,
Jim Van Verthc12aad92020-04-24 16:19:01 -0400591 texResource->resource(), &srcLocation, &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400592 this->submitDirectCommandList(SyncQueue::kForce);
593
594 const void* mappedMemory = transferBuffer->map();
595
596 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400597 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400598
599 transferBuffer->unmap();
600
601 return true;
602}
603
604bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
605 GrColorType surfaceColorType, GrColorType srcColorType,
606 const GrMipLevel texels[], int mipLevelCount,
607 bool prepForTexSampling) {
608 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
609 if (!d3dTex) {
610 return false;
611 }
612
613 // Make sure we have at least the base level
614 if (!mipLevelCount || !texels[0].fPixels) {
615 return false;
616 }
617
618 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
619 bool success = false;
620
621 // Need to change the resource state to COPY_DEST in order to upload to it
622 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
623
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400624 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400625 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
626 mipLevelCount);
627
628 if (prepForTexSampling) {
629 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
630 }
631
632 return success;
633}
634
635bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
636 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
637 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
638 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
639 SkASSERT(1 == mipLevelCount ||
640 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
641
642 // We assume that if the texture has mip levels, we either upload to all the levels or just the
643 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400644 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400645
646 if (width == 0 || height == 0) {
647 return false;
648 }
649
650 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
651 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
652
653 ID3D12Resource* d3dResource = tex->d3dResource();
654 SkASSERT(d3dResource);
655 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
656 // Either upload only the first miplevel or all miplevels
657 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
658
659 if (1 == mipLevelCount && !texels[0].fPixels) {
660 return true; // no data to upload
661 }
662
663 for (int i = 0; i < mipLevelCount; ++i) {
664 // We do not allow any gaps in the mip data
665 if (!texels[i].fPixels) {
666 return false;
667 }
668 }
669
670 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400671 UINT64 combinedBufferSize;
672 // We reset the width and height in the description to match our subrectangle size
673 // so we don't end up allocating more space than we need.
674 desc.Width = width;
675 desc.Height = height;
676 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400677 nullptr, nullptr, &combinedBufferSize);
678 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400679 SkASSERT(combinedBufferSize);
680
Greg Danielcffb0622020-07-16 13:19:17 -0400681 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
682 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
683 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400684 return false;
685 }
Greg Danielcffb0622020-07-16 13:19:17 -0400686
687 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400688
689 int currentWidth = width;
690 int currentHeight = height;
691 int layerHeight = tex->height();
692
693 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
694 if (texels[currentMipLevel].fPixels) {
695 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
696
Jim Van Verthfdd36852020-04-21 16:29:44 -0400697 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400698 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
699
700 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
701
702 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400703 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400704 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
705 src, srcRowBytes, trimRowBytes, currentHeight);
706 }
707 currentWidth = std::max(1, currentWidth / 2);
708 currentHeight = std::max(1, currentHeight / 2);
709 layerHeight = currentHeight;
710 }
711
Greg Danielcffb0622020-07-16 13:19:17 -0400712 // Update the offsets in the footprints to be relative to the slice's offset
713 for (int i = 0; i < mipLevelCount; ++i) {
714 placedFootprints[i].Offset += slice.fOffset;
715 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400716
Greg Danielcffb0622020-07-16 13:19:17 -0400717 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400718 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
719 placedFootprints.get(), left, top);
720
721 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400722 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400723 }
724
725 return true;
726}
727
Jim Van Verth9145f782020-04-28 12:01:12 -0400728static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
729 if (!info.fResource.get()) {
730 return false;
731 }
732 return true;
733}
734
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400735static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
736 if (!caps.isFormatTexturable(info.fFormat)) {
737 return false;
738 }
739 return true;
740}
741
742static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
743 int sampleCnt) {
744 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
745 return false;
746 }
747 return true;
748}
749
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400750sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
751 GrWrapOwnership,
752 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400753 GrIOType ioType) {
754 GrD3DTextureResourceInfo textureInfo;
755 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
756 return nullptr;
757 }
758
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400759 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400760 return nullptr;
761 }
762
763 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
764 return nullptr;
765 }
766
767 // TODO: support protected context
768 if (tex.isProtected()) {
769 return nullptr;
770 }
771
772 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
773 SkASSERT(state);
774 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
775 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500776}
777
778sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400779 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500780 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400781 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500782}
783
784sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
785 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500786 GrWrapOwnership ownership,
787 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400788 GrD3DTextureResourceInfo textureInfo;
789 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
790 return nullptr;
791 }
792
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400793 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400794 return nullptr;
795 }
796
797 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
798 return nullptr;
799 }
800 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
801 return nullptr;
802 }
803
804 // TODO: support protected context
805 if (tex.isProtected()) {
806 return nullptr;
807 }
808
809 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
810
811 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
812 SkASSERT(state);
813
814 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
815 sampleCnt, cacheable,
816 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500817}
818
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400819sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400820 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
821 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
822 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
823 // creating and owning the MSAA images.
824 if (rt.sampleCnt() > 1) {
825 return nullptr;
826 }
827
828 GrD3DTextureResourceInfo info;
829 if (!rt.getD3DTextureResourceInfo(&info)) {
830 return nullptr;
831 }
832
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400833 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400834 return nullptr;
835 }
836
837 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
838 return nullptr;
839 }
840
841 // TODO: support protected context
842 if (rt.isProtected()) {
843 return nullptr;
844 }
845
846 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
847
848 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
849 this, rt.dimensions(), 1, info, std::move(state));
850
851 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
852 SkASSERT(!rt.stencilBits());
853 if (tgt) {
854 SkASSERT(tgt->canAttemptStencilAttachment());
855 }
856
857 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500858}
859
860sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400861 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400862
863 GrD3DTextureResourceInfo textureInfo;
864 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
865 return nullptr;
866 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400867 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400868 return nullptr;
869 }
870
871 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
872 return nullptr;
873 }
874
875 // TODO: support protected context
876 if (tex.isProtected()) {
877 return nullptr;
878 }
879
880 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
881 if (!sampleCnt) {
882 return nullptr;
883 }
884
885 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
886 SkASSERT(state);
887
888 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
889 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500890}
891
892sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400893 GrAccessPattern accessPattern, const void* data) {
894 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
895 if (data && buffer) {
896 buffer->updateData(data, sizeInBytes);
897 }
898
899 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500900}
901
902GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
903 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400904 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
905 SkASSERT(width >= rt->width());
906 SkASSERT(height >= rt->height());
907
908 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
909
910 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
911 width,
912 height,
913 numStencilSamples,
914 sFmt));
915 fStats.incStencilAttachmentCreates();
916 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500917}
918
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400919bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
920 SkISize dimensions,
921 GrTexturable texturable,
922 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400923 GrMipmapped mipMapped,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400924 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400925 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400926 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400927
928 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
929 return false;
930 }
931
932 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
933 return false;
934 }
935
936 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
937 return false;
938 }
939
940 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400941 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400942 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400943 }
944
945 // create the texture
946 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
947 if (renderable == GrRenderable::kYes) {
948 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
949 }
950
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400951 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400952 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400953 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400954 resourceDesc.Width = dimensions.fWidth;
955 resourceDesc.Height = dimensions.fHeight;
956 resourceDesc.DepthOrArraySize = 1;
957 resourceDesc.MipLevels = numMipLevels;
958 resourceDesc.Format = dxgiFormat;
959 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400960 // quality levels are only supported for tiled resources so ignore for now
961 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400962 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400963 resourceDesc.Flags = usageFlags;
964
Jim Van Verth280d4f72020-05-04 10:04:24 -0400965 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
966 D3D12_CLEAR_VALUE clearValue = {};
967 if (renderable == GrRenderable::kYes) {
968 clearValue.Format = dxgiFormat;
969 // Assume transparent black
970 clearValue.Color[0] = 0;
971 clearValue.Color[1] = 0;
972 clearValue.Color[2] = 0;
973 clearValue.Color[3] = 0;
974 clearValuePtr = &clearValue;
975 }
976
Greg Daniel16032b32020-05-06 15:31:10 -0400977 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
978 ? D3D12_RESOURCE_STATE_RENDER_TARGET
979 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400980 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400981 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400982 SkDebugf("Failed to init texture resource info\n");
983 return false;
984 }
985
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400986 return true;
987}
988
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500989GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400990 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400991 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400992 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400993 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400994 this->handleDirtyContext();
995
996 const GrD3DCaps& caps = this->d3dCaps();
997
998 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
999 return {};
1000 }
1001
1002 DXGI_FORMAT dxgiFormat;
1003 if (!format.asDxgiFormat(&dxgiFormat)) {
1004 return {};
1005 }
1006
1007 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1008 if (!caps.isFormatTexturable(dxgiFormat)) {
1009 return {};
1010 }
1011
1012 GrD3DTextureResourceInfo info;
1013 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1014 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001015 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001016 return {};
1017 }
1018
1019 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001020}
1021
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001022static void copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
1023 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1024 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001025 SkASSERT(srcData && numMipLevels);
1026 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1027 SkASSERT(mapPtr);
1028
1029 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
1030
1031 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1032 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1033
1034 // copy data into the buffer, skipping any trailing bytes
1035 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1036 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1037 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1038 trimRowBytes, srcData[currentMipLevel].height());
1039 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001040}
1041
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001042static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1043 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1044 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1045 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001046 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001047 if (colorType == GrColorType::kUnknown) {
1048 return false;
1049 }
1050 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1051 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1052 return false;
1053 }
1054
1055 return true;
1056}
1057
Greg Daniel16032b32020-05-06 15:31:10 -04001058bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1059 sk_sp<GrRefCntedCallback> finishedCallback,
1060 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001061 GrD3DTextureResourceInfo info;
1062 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1063
1064 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1065 SkASSERT(state);
1066 sk_sp<GrD3DTexture> texture =
1067 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1068 GrWrapCacheable::kNo,
1069 kRW_GrIOType, info, std::move(state));
1070 if (!texture) {
1071 return false;
1072 }
1073
1074 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1075 if (!cmdList) {
1076 return false;
1077 }
1078
1079 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1080
1081 ID3D12Resource* d3dResource = texture->d3dResource();
1082 SkASSERT(d3dResource);
1083 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1084 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001085 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001086 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001087 backendTexture.dimensions().height()) + 1;
1088 }
1089 SkASSERT(mipLevelCount == info.fLevelCount);
1090 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1091 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001092 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1093 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001094 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001095 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001096 SkASSERT(combinedBufferSize);
1097 if (data->type() == BackendTextureData::Type::kColor &&
1098 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1099 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1100 combinedBufferSize =
1101 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1102 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1103 placedFootprints[i].Offset = 0;
1104 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1105 }
1106 }
1107
Greg Danielcffb0622020-07-16 13:19:17 -04001108 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1109 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1110 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001111 return false;
1112 }
Greg Danielcffb0622020-07-16 13:19:17 -04001113
1114 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001115 SkASSERT(bufferData);
1116
Jim Van Verth43a6e172020-06-23 11:59:08 -04001117 if (data->type() == BackendTextureData::Type::kPixmaps) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001118 copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
1119 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001120 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001121 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1122 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001123 } else {
1124 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1125 SkImage::CompressionType compression =
1126 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1127 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001128 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1129 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001130 return false;
1131 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001132 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001133 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1134 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001135 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001136 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001137 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001138 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001139 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1140 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001141 }
1142 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001143
Greg Danielcffb0622020-07-16 13:19:17 -04001144 // Update the offsets in the footprints to be relative to the slice's offset
1145 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1146 placedFootprints[i].Offset += slice.fOffset;
1147 }
1148
1149 GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer);
1150 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1151 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001152
1153 if (finishedCallback) {
1154 this->addFinishedCallback(std::move(finishedCallback));
1155 }
1156
Greg Daniel16032b32020-05-06 15:31:10 -04001157 return true;
1158}
1159
Greg Danielc1ad77c2020-05-06 11:40:03 -04001160GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001161 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001162 GrProtected isProtected) {
1163 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1164 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001165}
1166
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001167bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001168 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001169 const BackendTextureData* data) {
1170 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001171}
1172
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001173void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001174 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1175 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001176}
1177
Robert Phillips979b2232020-02-20 10:47:29 -05001178bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1179 return false;
1180}
1181
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001182#if GR_TEST_UTILS
1183bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001184 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1185
1186 GrD3DTextureResourceInfo info;
1187 if (!tex.getD3DTextureResourceInfo(&info)) {
1188 return false;
1189 }
1190 ID3D12Resource* textureResource = info.fResource.get();
1191 if (!textureResource) {
1192 return false;
1193 }
1194 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001195}
1196
1197GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001198 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001199 this->handleDirtyContext();
1200
1201 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001202 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001203 }
1204
1205 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1206
1207 GrD3DTextureResourceInfo info;
1208 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001209 GrRenderable::kYes, GrMipmapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001210 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001211 return {};
1212 }
1213
1214 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001215}
1216
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001217void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1218 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1219
1220 GrD3DTextureResourceInfo info;
1221 if (rt.getD3DTextureResourceInfo(&info)) {
1222 this->testingOnly_flushGpuAndSync();
1223 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1224 // is deleted.
1225 }
1226}
1227
1228void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001229 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001230}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001231
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001232void GrD3DGpu::testingOnly_startCapture() {
1233 if (fGraphicsAnalysis) {
1234 fGraphicsAnalysis->BeginCapture();
1235 }
1236}
1237
1238void GrD3DGpu::testingOnly_endCapture() {
1239 if (fGraphicsAnalysis) {
1240 fGraphicsAnalysis->EndCapture();
1241 }
1242}
1243#endif
1244
Jim Van Verthc632aa62020-04-17 16:58:20 -04001245///////////////////////////////////////////////////////////////////////////////
1246
Greg Daniela5a6b322020-04-23 12:52:27 -04001247void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001248 int numBarriers,
1249 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1250 SkASSERT(fCurrentDirectCommandList);
1251 SkASSERT(resource);
1252
Greg Daniela5a6b322020-04-23 12:52:27 -04001253 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001254}
1255
Greg Daniel9efe3862020-06-11 11:51:06 -04001256void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1257 GrSurfaceProxy* proxies[],
1258 int numProxies,
1259 SkSurface::BackendSurfaceAccess access,
1260 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001261 SkASSERT(numProxies >= 0);
1262 SkASSERT(!numProxies || proxies);
1263
1264 // prepare proxies by transitioning to PRESENT renderState
1265 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1266 GrD3DTextureResource* resource;
1267 for (int i = 0; i < numProxies; ++i) {
1268 SkASSERT(proxies[i]->isInstantiated());
1269 if (GrTexture* tex = proxies[i]->peekTexture()) {
1270 resource = static_cast<GrD3DTexture*>(tex);
1271 } else {
1272 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1273 SkASSERT(rt);
1274 resource = static_cast<GrD3DRenderTarget*>(rt);
1275 }
1276 resource->prepareForPresent(this);
1277 }
1278 }
1279}
1280
Greg Danielcffb0622020-07-16 13:19:17 -04001281void GrD3DGpu::takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001282 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001283}
1284
Jim Van Verthc632aa62020-04-17 16:58:20 -04001285bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1286 if (syncCpu) {
1287 return this->submitDirectCommandList(SyncQueue::kForce);
1288 } else {
1289 return this->submitDirectCommandList(SyncQueue::kSkip);
1290 }
1291}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001292
1293std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1294 return GrD3DSemaphore::Make(this);
1295}
1296std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1297 const GrBackendSemaphore& semaphore,
1298 GrResourceProvider::SemaphoreWrapType,
1299 GrWrapOwnership) {
1300 SkASSERT(this->caps()->semaphoreSupport());
1301 GrD3DFenceInfo fenceInfo;
1302 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1303 return nullptr;
1304 }
1305 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1306}
1307
1308void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001309 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001310 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1311 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1312 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1313}
1314
1315void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001316 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001317 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1318 // TODO: Do we need to track the lifetime of this?
1319 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1320}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001321
1322GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001323 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001324 return fCurrentFenceValue;
1325}
1326
1327bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001328 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001329}