blob: 07b8a08060cceed8653abd8f21330af29e5b0e45 [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
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400124 fConstantsRingBuffer.startSubmit(this);
Jim Van Verth711b0392020-07-23 16:45:00 -0400125
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
Greg Daniel69267912020-07-24 10:42:53 -0400379 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
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
Greg Daniel69267912020-07-24 10:42:53 -0400477 fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(),
478 &dstLocation,
479 dstPoint.fX, dstPoint.fY,
480 srcResource->resource(),
481 &srcLocation,
482 &srcBox);
Jim Van Verth9145f782020-04-28 12:01:12 -0400483
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
Greg Daniel69267912020-07-24 10:42:53 -0400590 fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0,
591 texResource->resource(), &srcLocation,
592 &srcBox);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400593 this->submitDirectCommandList(SyncQueue::kForce);
594
595 const void* mappedMemory = transferBuffer->map();
596
597 SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
Jim Van Verthfdd36852020-04-21 16:29:44 -0400598 tightRowBytes, height);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400599
600 transferBuffer->unmap();
601
602 return true;
603}
604
605bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
606 GrColorType surfaceColorType, GrColorType srcColorType,
607 const GrMipLevel texels[], int mipLevelCount,
608 bool prepForTexSampling) {
609 GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture());
610 if (!d3dTex) {
611 return false;
612 }
613
614 // Make sure we have at least the base level
615 if (!mipLevelCount || !texels[0].fPixels) {
616 return false;
617 }
618
619 SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat()));
620 bool success = false;
621
622 // Need to change the resource state to COPY_DEST in order to upload to it
623 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
624
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400625 SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400626 success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels,
627 mipLevelCount);
628
629 if (prepForTexSampling) {
630 d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
631 }
632
633 return success;
634}
635
636bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height,
637 GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) {
638 SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat()));
639 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
640 SkASSERT(1 == mipLevelCount ||
641 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
642
643 // We assume that if the texture has mip levels, we either upload to all the levels or just the
644 // first.
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400645 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1));
Jim Van Verthba7f2292020-04-21 08:56:47 -0400646
647 if (width == 0 || height == 0) {
648 return false;
649 }
650
651 SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex));
652 SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat()));
653
654 ID3D12Resource* d3dResource = tex->d3dResource();
655 SkASSERT(d3dResource);
656 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
657 // Either upload only the first miplevel or all miplevels
658 SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels);
659
660 if (1 == mipLevelCount && !texels[0].fPixels) {
661 return true; // no data to upload
662 }
663
664 for (int i = 0; i < mipLevelCount; ++i) {
665 // We do not allow any gaps in the mip data
666 if (!texels[i].fPixels) {
667 return false;
668 }
669 }
670
671 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400672 UINT64 combinedBufferSize;
673 // We reset the width and height in the description to match our subrectangle size
674 // so we don't end up allocating more space than we need.
675 desc.Width = width;
676 desc.Height = height;
677 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthfdd36852020-04-21 16:29:44 -0400678 nullptr, nullptr, &combinedBufferSize);
679 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Jim Van Verthba7f2292020-04-21 08:56:47 -0400680 SkASSERT(combinedBufferSize);
681
Greg Danielcffb0622020-07-16 13:19:17 -0400682 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
683 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
684 if (!slice.fBuffer) {
Jim Van Verthba7f2292020-04-21 08:56:47 -0400685 return false;
686 }
Greg Danielcffb0622020-07-16 13:19:17 -0400687
688 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400689
690 int currentWidth = width;
691 int currentHeight = height;
692 int layerHeight = tex->height();
693
694 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
695 if (texels[currentMipLevel].fPixels) {
696 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
697
Jim Van Verthfdd36852020-04-21 16:29:44 -0400698 const size_t trimRowBytes = currentWidth * bpp;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400699 const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
700
701 char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
702
703 // copy data into the buffer, skipping any trailing bytes
Jim Van Verthba7f2292020-04-21 08:56:47 -0400704 const char* src = (const char*)texels[currentMipLevel].fPixels;
Jim Van Verthba7f2292020-04-21 08:56:47 -0400705 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
706 src, srcRowBytes, trimRowBytes, currentHeight);
707 }
708 currentWidth = std::max(1, currentWidth / 2);
709 currentHeight = std::max(1, currentHeight / 2);
710 layerHeight = currentHeight;
711 }
712
Greg Danielcffb0622020-07-16 13:19:17 -0400713 // Update the offsets in the footprints to be relative to the slice's offset
714 for (int i = 0; i < mipLevelCount; ++i) {
715 placedFootprints[i].Offset += slice.fOffset;
716 }
Jim Van Verthba7f2292020-04-21 08:56:47 -0400717
Greg Daniel69267912020-07-24 10:42:53 -0400718 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400719 fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount,
720 placedFootprints.get(), left, top);
721
722 if (mipLevelCount < (int)desc.MipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400723 tex->markMipmapsDirty();
Jim Van Verthba7f2292020-04-21 08:56:47 -0400724 }
725
726 return true;
727}
728
Jim Van Verth9145f782020-04-28 12:01:12 -0400729static bool check_resource_info(const GrD3DTextureResourceInfo& info) {
730 if (!info.fResource.get()) {
731 return false;
732 }
733 return true;
734}
735
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400736static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) {
737 if (!caps.isFormatTexturable(info.fFormat)) {
738 return false;
739 }
740 return true;
741}
742
743static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info,
744 int sampleCnt) {
745 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
746 return false;
747 }
748 return true;
749}
750
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400751sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex,
752 GrWrapOwnership,
753 GrWrapCacheable wrapType,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400754 GrIOType ioType) {
755 GrD3DTextureResourceInfo textureInfo;
756 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
757 return nullptr;
758 }
759
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400760 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400761 return nullptr;
762 }
763
764 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
765 return nullptr;
766 }
767
768 // TODO: support protected context
769 if (tex.isProtected()) {
770 return nullptr;
771 }
772
773 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
774 SkASSERT(state);
775 return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo,
776 std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500777}
778
779sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400780 GrWrapOwnership ownership,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500781 GrWrapCacheable wrapType) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -0400782 return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500783}
784
785sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
786 int sampleCnt,
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500787 GrWrapOwnership ownership,
788 GrWrapCacheable cacheable) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400789 GrD3DTextureResourceInfo textureInfo;
790 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
791 return nullptr;
792 }
793
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400794 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400795 return nullptr;
796 }
797
798 if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) {
799 return nullptr;
800 }
801 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
802 return nullptr;
803 }
804
805 // TODO: support protected context
806 if (tex.isProtected()) {
807 return nullptr;
808 }
809
810 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
811
812 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
813 SkASSERT(state);
814
815 return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(),
816 sampleCnt, cacheable,
817 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500818}
819
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400820sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400821 // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In
822 // general this is not an issue since swapchain images in D3D are never multisampled. Thus if
823 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
824 // creating and owning the MSAA images.
825 if (rt.sampleCnt() > 1) {
826 return nullptr;
827 }
828
829 GrD3DTextureResourceInfo info;
830 if (!rt.getD3DTextureResourceInfo(&info)) {
831 return nullptr;
832 }
833
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400834 if (!check_resource_info(info)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400835 return nullptr;
836 }
837
838 if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) {
839 return nullptr;
840 }
841
842 // TODO: support protected context
843 if (rt.isProtected()) {
844 return nullptr;
845 }
846
847 sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState();
848
849 sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget(
850 this, rt.dimensions(), 1, info, std::move(state));
851
852 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
853 SkASSERT(!rt.stencilBits());
854 if (tgt) {
855 SkASSERT(tgt->canAttemptStencilAttachment());
856 }
857
858 return std::move(tgt);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500859}
860
861sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400862 int sampleCnt) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400863
864 GrD3DTextureResourceInfo textureInfo;
865 if (!tex.getD3DTextureResourceInfo(&textureInfo)) {
866 return nullptr;
867 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400868 if (!check_resource_info(textureInfo)) {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400869 return nullptr;
870 }
871
872 if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) {
873 return nullptr;
874 }
875
876 // TODO: support protected context
877 if (tex.isProtected()) {
878 return nullptr;
879 }
880
881 sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat);
882 if (!sampleCnt) {
883 return nullptr;
884 }
885
886 sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState();
887 SkASSERT(state);
888
889 return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt,
890 textureInfo, std::move(state));
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500891}
892
893sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
Jim Van Verthd6ad4802020-04-03 14:59:20 -0400894 GrAccessPattern accessPattern, const void* data) {
895 sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern);
896 if (data && buffer) {
897 buffer->updateData(data, sizeInBytes);
898 }
899
900 return std::move(buffer);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500901}
902
903GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget(
904 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
Jim Van Verth4f51f472020-04-13 11:02:21 -0400905 SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport());
906 SkASSERT(width >= rt->width());
907 SkASSERT(height >= rt->height());
908
909 const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat();
910
911 GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this,
912 width,
913 height,
914 numStencilSamples,
915 sFmt));
916 fStats.incStencilAttachmentCreates();
917 return stencil;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500918}
919
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400920bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat,
921 SkISize dimensions,
922 GrTexturable texturable,
923 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400924 GrMipmapped mipMapped,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400925 GrD3DTextureResourceInfo* info,
Greg Daniel16032b32020-05-06 15:31:10 -0400926 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400927 SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes);
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400928
929 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
930 return false;
931 }
932
933 if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) {
934 return false;
935 }
936
937 if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) {
938 return false;
939 }
940
941 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400942 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400943 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400944 }
945
946 // create the texture
947 D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE;
948 if (renderable == GrRenderable::kYes) {
949 usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
950 }
951
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400952 D3D12_RESOURCE_DESC resourceDesc = {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400953 resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Greg Daniel16032b32020-05-06 15:31:10 -0400954 resourceDesc.Alignment = 0; // use default alignment
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400955 resourceDesc.Width = dimensions.fWidth;
956 resourceDesc.Height = dimensions.fHeight;
957 resourceDesc.DepthOrArraySize = 1;
958 resourceDesc.MipLevels = numMipLevels;
959 resourceDesc.Format = dxgiFormat;
960 resourceDesc.SampleDesc.Count = 1;
Greg Danielc9624d52020-04-13 15:36:31 -0400961 // quality levels are only supported for tiled resources so ignore for now
962 resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Greg Daniel16032b32020-05-06 15:31:10 -0400963 resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400964 resourceDesc.Flags = usageFlags;
965
Jim Van Verth280d4f72020-05-04 10:04:24 -0400966 D3D12_CLEAR_VALUE* clearValuePtr = nullptr;
967 D3D12_CLEAR_VALUE clearValue = {};
968 if (renderable == GrRenderable::kYes) {
969 clearValue.Format = dxgiFormat;
970 // Assume transparent black
971 clearValue.Color[0] = 0;
972 clearValue.Color[1] = 0;
973 clearValue.Color[2] = 0;
974 clearValue.Color[3] = 0;
975 clearValuePtr = &clearValue;
976 }
977
Greg Daniel16032b32020-05-06 15:31:10 -0400978 D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes)
979 ? D3D12_RESOURCE_STATE_RENDER_TARGET
980 : D3D12_RESOURCE_STATE_COPY_DEST;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400981 if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState,
Jim Van Verth280d4f72020-05-04 10:04:24 -0400982 isProtected, clearValuePtr, info)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400983 SkDebugf("Failed to init texture resource info\n");
984 return false;
985 }
986
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400987 return true;
988}
989
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500990GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions,
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400991 const GrBackendFormat& format,
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400992 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400993 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400994 GrProtected isProtected) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -0400995 this->handleDirtyContext();
996
997 const GrD3DCaps& caps = this->d3dCaps();
998
999 if (this->protectedContext() != (isProtected == GrProtected::kYes)) {
1000 return {};
1001 }
1002
1003 DXGI_FORMAT dxgiFormat;
1004 if (!format.asDxgiFormat(&dxgiFormat)) {
1005 return {};
1006 }
1007
1008 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
1009 if (!caps.isFormatTexturable(dxgiFormat)) {
1010 return {};
1011 }
1012
1013 GrD3DTextureResourceInfo info;
1014 if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes,
1015 renderable, mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04001016 &info, isProtected)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001017 return {};
1018 }
1019
1020 return GrBackendTexture(dimensions.width(), dimensions.height(), info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001021}
1022
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001023static void copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat,
1024 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1025 const SkPixmap srcData[], int numMipLevels) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001026 SkASSERT(srcData && numMipLevels);
1027 SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat));
1028 SkASSERT(mapPtr);
1029
1030 size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat);
1031
1032 for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) {
1033 const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel;
1034
1035 // copy data into the buffer, skipping any trailing bytes
1036 char* dst = mapPtr + placedFootprints[currentMipLevel].Offset;
1037 SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
1038 srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(),
1039 trimRowBytes, srcData[currentMipLevel].height());
1040 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001041}
1042
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001043static bool copy_color_data(const GrD3DCaps& caps, char* mapPtr,
1044 DXGI_FORMAT dxgiFormat, SkISize dimensions,
1045 D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints,
1046 SkColor4f color) {
Greg Daniel746460e2020-06-30 13:13:53 -04001047 auto colorType = caps.getFormatColorType(dxgiFormat);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001048 if (colorType == GrColorType::kUnknown) {
1049 return false;
1050 }
1051 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
1052 if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) {
1053 return false;
1054 }
1055
1056 return true;
1057}
1058
Greg Daniel16032b32020-05-06 15:31:10 -04001059bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
1060 sk_sp<GrRefCntedCallback> finishedCallback,
1061 const BackendTextureData* data) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001062 GrD3DTextureResourceInfo info;
1063 SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info));
1064
1065 sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState();
1066 SkASSERT(state);
1067 sk_sp<GrD3DTexture> texture =
1068 GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(),
1069 GrWrapCacheable::kNo,
1070 kRW_GrIOType, info, std::move(state));
1071 if (!texture) {
1072 return false;
1073 }
1074
1075 GrD3DDirectCommandList* cmdList = this->currentCommandList();
1076 if (!cmdList) {
1077 return false;
1078 }
1079
1080 texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST);
1081
1082 ID3D12Resource* d3dResource = texture->d3dResource();
1083 SkASSERT(d3dResource);
1084 D3D12_RESOURCE_DESC desc = d3dResource->GetDesc();
1085 unsigned int mipLevelCount = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04001086 if (backendTexture.fMipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001087 mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(),
Jim Van Verth43a6e172020-06-23 11:59:08 -04001088 backendTexture.dimensions().height()) + 1;
1089 }
1090 SkASSERT(mipLevelCount == info.fLevelCount);
1091 SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
1092 UINT64 combinedBufferSize;
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001093 SkAutoTMalloc<UINT> numRows(mipLevelCount);
1094 SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001095 fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001096 numRows.get(), rowSizeInBytes.get(), &combinedBufferSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001097 SkASSERT(combinedBufferSize);
1098 if (data->type() == BackendTextureData::Type::kColor &&
1099 !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) {
1100 // For a single uncompressed color, we reuse the same top-level buffer area for all levels.
1101 combinedBufferSize =
1102 placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height;
1103 for (unsigned int i = 1; i < mipLevelCount; ++i) {
1104 placedFootprints[i].Offset = 0;
1105 placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch;
1106 }
1107 }
1108
Greg Danielcffb0622020-07-16 13:19:17 -04001109 GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice(
1110 combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
1111 if (!slice.fBuffer) {
Jim Van Verth43a6e172020-06-23 11:59:08 -04001112 return false;
1113 }
Greg Danielcffb0622020-07-16 13:19:17 -04001114
1115 char* bufferData = (char*)slice.fOffsetMapPtr;
Jim Van Verth43a6e172020-06-23 11:59:08 -04001116 SkASSERT(bufferData);
1117
Jim Van Verth43a6e172020-06-23 11:59:08 -04001118 if (data->type() == BackendTextureData::Type::kPixmaps) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001119 copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(),
1120 info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001121 } else if (data->type() == BackendTextureData::Type::kCompressed) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001122 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1123 rowSizeInBytes.get(), data->compressedData(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001124 } else {
1125 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1126 SkImage::CompressionType compression =
1127 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
1128 if (SkImage::CompressionType::kNone == compression) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001129 if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat,
1130 backendTexture.dimensions(), placedFootprints, data->color())) {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001131 return false;
1132 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001133 } else {
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001134 size_t totalCompressedSize = SkCompressedFormatDataSize(compression,
1135 backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001136 backendTexture.hasMipmaps());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001137 SkAutoTMalloc<char> tempData(totalCompressedSize);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001138 GrFillInCompressedData(compression, backendTexture.dimensions(),
Brian Salomon40a40622020-07-21 10:32:07 -04001139 backendTexture.fMipmapped, tempData, data->color());
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001140 copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(),
1141 rowSizeInBytes.get(), tempData.get(), info.fLevelCount);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001142 }
1143 }
Jim Van Verth43a6e172020-06-23 11:59:08 -04001144
Greg Danielcffb0622020-07-16 13:19:17 -04001145 // Update the offsets in the footprints to be relative to the slice's offset
1146 for (unsigned int i = 0; i < mipLevelCount; ++i) {
1147 placedFootprints[i].Offset += slice.fOffset;
1148 }
1149
Greg Daniel69267912020-07-24 10:42:53 -04001150 ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource();
Greg Danielcffb0622020-07-16 13:19:17 -04001151 cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0,
1152 0);
Jim Van Verth43a6e172020-06-23 11:59:08 -04001153
1154 if (finishedCallback) {
1155 this->addFinishedCallback(std::move(finishedCallback));
1156 }
1157
Greg Daniel16032b32020-05-06 15:31:10 -04001158 return true;
1159}
1160
Greg Danielc1ad77c2020-05-06 11:40:03 -04001161GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001162 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001163 GrProtected isProtected) {
1164 return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped,
1165 isProtected);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001166}
1167
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001168bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielaaf738c2020-07-10 09:30:33 -04001169 sk_sp<GrRefCntedCallback> finishedCallback,
Jim Van Verthbb80fcd2020-07-14 10:04:40 -04001170 const BackendTextureData* data) {
1171 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Greg Danielaaf738c2020-07-10 09:30:33 -04001172}
1173
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001174void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001175 SkASSERT(GrBackendApi::kDirect3D == tex.fBackend);
1176 // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001177}
1178
Robert Phillips979b2232020-02-20 10:47:29 -05001179bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) {
1180 return false;
1181}
1182
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001183#if GR_TEST_UTILS
1184bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001185 SkASSERT(GrBackendApi::kDirect3D == tex.backend());
1186
1187 GrD3DTextureResourceInfo info;
1188 if (!tex.getD3DTextureResourceInfo(&info)) {
1189 return false;
1190 }
1191 ID3D12Resource* textureResource = info.fResource.get();
1192 if (!textureResource) {
1193 return false;
1194 }
1195 return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001196}
1197
1198GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Jim Van Verthaa90dad2020-03-30 15:00:39 -04001199 GrColorType colorType) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001200 this->handleDirtyContext();
1201
1202 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
Jim Van Verth2b9f53e2020-04-14 11:47:34 -04001203 return {};
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001204 }
1205
1206 DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType);
1207
1208 GrD3DTextureResourceInfo info;
1209 if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001210 GrRenderable::kYes, GrMipmapped::kNo,
Greg Daniel16032b32020-05-06 15:31:10 -04001211 &info, GrProtected::kNo)) {
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001212 return {};
1213 }
1214
1215 return GrBackendRenderTarget(w, h, 1, info);
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001216}
1217
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001218void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1219 SkASSERT(GrBackendApi::kDirect3D == rt.backend());
1220
1221 GrD3DTextureResourceInfo info;
1222 if (rt.getD3DTextureResourceInfo(&info)) {
1223 this->testingOnly_flushGpuAndSync();
1224 // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget
1225 // is deleted.
1226 }
1227}
1228
1229void GrD3DGpu::testingOnly_flushGpuAndSync() {
Jim Van Verthc632aa62020-04-17 16:58:20 -04001230 SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce));
Jim Van Verth96bfeff2020-04-09 14:36:12 -04001231}
Jim Van Verthc632aa62020-04-17 16:58:20 -04001232
Jim Van Verth9b5e16c2020-04-20 10:45:52 -04001233void GrD3DGpu::testingOnly_startCapture() {
1234 if (fGraphicsAnalysis) {
1235 fGraphicsAnalysis->BeginCapture();
1236 }
1237}
1238
1239void GrD3DGpu::testingOnly_endCapture() {
1240 if (fGraphicsAnalysis) {
1241 fGraphicsAnalysis->EndCapture();
1242 }
1243}
1244#endif
1245
Jim Van Verthc632aa62020-04-17 16:58:20 -04001246///////////////////////////////////////////////////////////////////////////////
1247
Greg Daniela5a6b322020-04-23 12:52:27 -04001248void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource,
Jim Van Verthc632aa62020-04-17 16:58:20 -04001249 int numBarriers,
1250 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1251 SkASSERT(fCurrentDirectCommandList);
1252 SkASSERT(resource);
1253
Greg Daniela5a6b322020-04-23 12:52:27 -04001254 fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers);
Jim Van Verthc632aa62020-04-17 16:58:20 -04001255}
1256
Greg Daniel69267912020-07-24 10:42:53 -04001257void GrD3DGpu::addBufferResourceBarriers(GrD3DBuffer* buffer,
1258 int numBarriers,
1259 D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const {
1260 SkASSERT(fCurrentDirectCommandList);
1261 SkASSERT(buffer);
1262
1263 fCurrentDirectCommandList->resourceBarrier(nullptr, numBarriers, barriers);
1264 fCurrentDirectCommandList->addGrBuffer(sk_ref_sp<const GrBuffer>(buffer));
1265}
1266
1267
Greg Daniel9efe3862020-06-11 11:51:06 -04001268void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates(
1269 GrSurfaceProxy* proxies[],
1270 int numProxies,
1271 SkSurface::BackendSurfaceAccess access,
1272 const GrBackendSurfaceMutableState* newState) {
Jim Van Verth682a2f42020-05-13 16:54:09 -04001273 SkASSERT(numProxies >= 0);
1274 SkASSERT(!numProxies || proxies);
1275
1276 // prepare proxies by transitioning to PRESENT renderState
1277 if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) {
1278 GrD3DTextureResource* resource;
1279 for (int i = 0; i < numProxies; ++i) {
1280 SkASSERT(proxies[i]->isInstantiated());
1281 if (GrTexture* tex = proxies[i]->peekTexture()) {
1282 resource = static_cast<GrD3DTexture*>(tex);
1283 } else {
1284 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1285 SkASSERT(rt);
1286 resource = static_cast<GrD3DRenderTarget*>(rt);
1287 }
1288 resource->prepareForPresent(this);
1289 }
1290 }
1291}
1292
Jim Van Verth1aaf41b2020-07-29 09:24:29 -04001293void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
Greg Daniel426274b2020-07-20 11:37:38 -04001294 fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
Greg Danielcffb0622020-07-16 13:19:17 -04001295}
1296
Jim Van Verthc632aa62020-04-17 16:58:20 -04001297bool GrD3DGpu::onSubmitToGpu(bool syncCpu) {
1298 if (syncCpu) {
1299 return this->submitDirectCommandList(SyncQueue::kForce);
1300 } else {
1301 return this->submitDirectCommandList(SyncQueue::kSkip);
1302 }
1303}
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001304
1305std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) {
1306 return GrD3DSemaphore::Make(this);
1307}
1308std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore(
1309 const GrBackendSemaphore& semaphore,
1310 GrResourceProvider::SemaphoreWrapType,
1311 GrWrapOwnership) {
1312 SkASSERT(this->caps()->semaphoreSupport());
1313 GrD3DFenceInfo fenceInfo;
1314 if (!semaphore.getD3DFenceInfo(&fenceInfo)) {
1315 return nullptr;
1316 }
1317 return GrD3DSemaphore::MakeWrapped(fenceInfo);
1318}
1319
1320void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001321 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001322 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1323 // TODO: Do we need to track the lifetime of this? How do we know it's done?
1324 fQueue->Signal(d3dSem->fence(), d3dSem->value());
1325}
1326
1327void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04001328 SkASSERT(semaphore);
Jim Van Verthc1a67b52020-06-25 13:10:29 -04001329 GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore);
1330 // TODO: Do we need to track the lifetime of this?
1331 fQueue->Wait(d3dSem->fence(), d3dSem->value());
1332}
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001333
1334GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() {
Jim Van Verthe3810362020-07-01 10:12:11 -04001335 GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue));
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001336 return fCurrentFenceValue;
1337}
1338
1339bool GrD3DGpu::waitFence(GrFence fence) {
Jim Van Verth3b0d7d12020-07-06 11:52:42 -04001340 return (fFence->GetCompletedValue() >= fence);
Jim Van Verth1e6460d2020-06-30 15:47:52 -04001341}