Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1 | /* |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 2 | * Copyright 2020 Google LLC |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 3 | * |
| 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 8 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 9 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrBackendSurface.h" |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 11 | #include "include/gpu/d3d/GrD3DBackendContext.h" |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 12 | #include "src/core/SkConvertPixels.h" |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 13 | #include "src/core/SkMipMap.h" |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrBackendUtils.h" |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDataUtils.h" |
| 16 | #include "src/gpu/GrTexturePriv.h" |
Jim Van Verth | d6ad480 | 2020-04-03 14:59:20 -0400 | [diff] [blame] | 17 | #include "src/gpu/d3d/GrD3DBuffer.h" |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 18 | #include "src/gpu/d3d/GrD3DCaps.h" |
| 19 | #include "src/gpu/d3d/GrD3DOpsRenderPass.h" |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 20 | #include "src/gpu/d3d/GrD3DSemaphore.h" |
Jim Van Verth | 4f51f47 | 2020-04-13 11:02:21 -0400 | [diff] [blame] | 21 | #include "src/gpu/d3d/GrD3DStencilAttachment.h" |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 22 | #include "src/gpu/d3d/GrD3DTexture.h" |
| 23 | #include "src/gpu/d3d/GrD3DTextureRenderTarget.h" |
| 24 | #include "src/gpu/d3d/GrD3DUtil.h" |
Greg Daniel | 5fc5c81 | 2020-04-23 10:30:23 -0400 | [diff] [blame] | 25 | #include "src/sksl/SkSLCompiler.h" |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 26 | |
Jim Van Verth | 9b5e16c | 2020-04-20 10:45:52 -0400 | [diff] [blame] | 27 | #if GR_TEST_UTILS |
| 28 | #include <DXProgrammableCapture.h> |
| 29 | #endif |
| 30 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 31 | sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext, |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame^] | 32 | const GrContextOptions& contextOptions, GrDirectContext* direct) { |
| 33 | return sk_sp<GrGpu>(new GrD3DGpu(direct, contextOptions, backendContext)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 36 | // 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. |
| 40 | static const int kDefaultOutstandingAllocCnt = 8; |
| 41 | |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame^] | 42 | GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 43 | const GrD3DBackendContext& backendContext) |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame^] | 44 | : INHERITED(direct) |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 45 | , fDevice(backendContext.fDevice) |
Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 46 | |
| 47 | , fQueue(backendContext.fQueue) |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 48 | , fResourceProvider(this) |
Greg Daniel | 5fc5c81 | 2020-04-23 10:30:23 -0400 | [diff] [blame] | 49 | , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt) |
| 50 | , fCompiler(new SkSL::Compiler()) { |
Jim Van Verth | 8ec1330 | 2020-02-26 12:59:56 -0500 | [diff] [blame] | 51 | fCaps.reset(new GrD3DCaps(contextOptions, |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 52 | backendContext.fAdapter.get(), |
| 53 | backendContext.fDevice.get())); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 54 | |
| 55 | fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList(); |
Greg Daniel | e52c978 | 2020-03-23 14:18:37 -0400 | [diff] [blame] | 56 | SkASSERT(fCurrentDirectCommandList); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 57 | |
| 58 | SkASSERT(fCurrentFenceValue == 0); |
Jim Van Verth | e381036 | 2020-07-01 10:12:11 -0400 | [diff] [blame] | 59 | GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE, |
| 60 | IID_PPV_ARGS(&fFence))); |
Jim Van Verth | 9b5e16c | 2020-04-20 10:45:52 -0400 | [diff] [blame] | 61 | |
| 62 | #if GR_TEST_UTILS |
| 63 | HRESULT getAnalysis = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&fGraphicsAnalysis)); |
| 64 | if (FAILED(getAnalysis)) { |
| 65 | fGraphicsAnalysis = nullptr; |
| 66 | } |
| 67 | #endif |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 70 | GrD3DGpu::~GrD3DGpu() { |
| 71 | this->destroyResources(); |
| 72 | } |
| 73 | |
| 74 | void GrD3DGpu::destroyResources() { |
| 75 | if (fCurrentDirectCommandList) { |
| 76 | fCurrentDirectCommandList->close(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 77 | fCurrentDirectCommandList->reset(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // We need to make sure everything has finished on the queue. |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 81 | this->waitForQueueCompletion(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 82 | |
| 83 | SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();) |
| 84 | |
| 85 | // We used a placement new for each object in fOutstandingCommandLists, so we're responsible |
| 86 | // for calling the destructor on each of them as well. |
| 87 | while (!fOutstandingCommandLists.empty()) { |
Jim Van Verth | f43da14 | 2020-06-09 16:34:43 -0400 | [diff] [blame] | 88 | OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 89 | SkASSERT(list->fFenceValue <= fenceValue); |
| 90 | // No reason to recycle the command lists since we are destroying all resources anyways. |
| 91 | list->~OutstandingCommandList(); |
Jim Van Verth | f43da14 | 2020-06-09 16:34:43 -0400 | [diff] [blame] | 92 | fOutstandingCommandLists.pop_front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 93 | } |
Jim Van Verth | f278886 | 2020-06-03 17:33:12 -0400 | [diff] [blame] | 94 | |
| 95 | fResourceProvider.destroyResources(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 96 | } |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 97 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 98 | GrOpsRenderPass* GrD3DGpu::getOpsRenderPass( |
Robert Phillips | 96f2237 | 2020-05-20 12:31:18 -0400 | [diff] [blame] | 99 | GrRenderTarget* rt, GrStencilAttachment*, |
| 100 | GrSurfaceOrigin origin, const SkIRect& bounds, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 101 | const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 102 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 103 | const SkTArray<GrSurfaceProxy*, true>& sampledProxies) { |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 104 | if (!fCachedOpsRenderPass) { |
| 105 | fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this)); |
| 106 | } |
| 107 | |
| 108 | if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) { |
| 109 | return nullptr; |
| 110 | } |
| 111 | return fCachedOpsRenderPass.get(); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 114 | bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) { |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 115 | SkASSERT(fCurrentDirectCommandList); |
| 116 | |
Jim Van Verth | 3eadce2 | 2020-06-01 11:34:49 -0400 | [diff] [blame] | 117 | fResourceProvider.prepForSubmit(); |
| 118 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 119 | GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get()); |
| 120 | if (result == GrD3DDirectCommandList::SubmitResult::kFailure) { |
| 121 | return false; |
| 122 | } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) { |
| 123 | if (sync == SyncQueue::kForce) { |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 124 | this->waitForQueueCompletion(); |
Jim Van Verth | 682a2f4 | 2020-05-13 16:54:09 -0400 | [diff] [blame] | 125 | this->checkForFinishedCommandLists(); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 126 | } |
| 127 | return true; |
| 128 | } |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 129 | |
Greg Daniel | a581a8b | 2020-06-19 15:22:18 -0400 | [diff] [blame] | 130 | // We just submitted the command list so make sure all GrD3DPipelineState's mark their cached |
| 131 | // uniform data as dirty. |
| 132 | fResourceProvider.markPipelineStateUniformsDirty(); |
| 133 | |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 134 | GrFence fence = this->insertFence(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 135 | new (fOutstandingCommandLists.push_back()) OutstandingCommandList( |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 136 | std::move(fCurrentDirectCommandList), fence); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 137 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 138 | if (sync == SyncQueue::kForce) { |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 139 | this->waitForQueueCompletion(); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 142 | fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList(); |
| 143 | |
| 144 | // This should be done after we have a new command list in case the freeing of any resources |
| 145 | // held by a finished command list causes us send a new command to the gpu (like changing the |
| 146 | // resource state. |
| 147 | this->checkForFinishedCommandLists(); |
| 148 | |
| 149 | SkASSERT(fCurrentDirectCommandList); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 150 | return true; |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void GrD3DGpu::checkForFinishedCommandLists() { |
| 154 | uint64_t currentFenceValue = fFence->GetCompletedValue(); |
| 155 | |
| 156 | // Iterate over all the outstanding command lists to see if any have finished. The commands |
| 157 | // lists are in order from oldest to newest, so we start at the front to check if their fence |
| 158 | // value is less than the last signaled value. If so we pop it off and move onto the next. |
| 159 | // Repeat till we find a command list that has not finished yet (and all others afterwards are |
| 160 | // also guaranteed to not have finished). |
Jim Van Verth | dd3b401 | 2020-06-30 15:28:17 -0400 | [diff] [blame] | 161 | OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
| 162 | while (front && front->fFenceValue <= currentFenceValue) { |
| 163 | std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList)); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 164 | // Since we used placement new we are responsible for calling the destructor manually. |
| 165 | front->~OutstandingCommandList(); |
| 166 | fOutstandingCommandLists.pop_front(); |
Jim Van Verth | dd3b401 | 2020-06-30 15:28:17 -0400 | [diff] [blame] | 167 | fResourceProvider.recycleDirectCommandList(std::move(currList)); |
| 168 | front = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 172 | void GrD3DGpu::waitForQueueCompletion() { |
| 173 | if (fFence->GetCompletedValue() < fCurrentFenceValue) { |
| 174 | HANDLE fenceEvent; |
| 175 | fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 176 | SkASSERT(fenceEvent); |
| 177 | GR_D3D_CALL_ERRCHECK(fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent)); |
| 178 | WaitForSingleObject(fenceEvent, INFINITE); |
| 179 | CloseHandle(fenceEvent); |
| 180 | } |
| 181 | } |
| 182 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 183 | void GrD3DGpu::submit(GrOpsRenderPass* renderPass) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 184 | SkASSERT(fCachedOpsRenderPass.get() == renderPass); |
| 185 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 186 | // TODO: actually submit something here |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 187 | fCachedOpsRenderPass.reset(); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 190 | void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc, |
| 191 | GrGpuFinishedContext finishedContext) { |
| 192 | SkASSERT(finishedProc); |
| 193 | sk_sp<GrRefCntedCallback> finishedCallback( |
| 194 | new GrRefCntedCallback(finishedProc, finishedContext)); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 195 | this->addFinishedCallback(std::move(finishedCallback)); |
| 196 | } |
| 197 | |
| 198 | void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) { |
| 199 | SkASSERT(finishedCallback); |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 200 | // Besides the current command list, we also add the finishedCallback to the newest outstanding |
| 201 | // command list. Our contract for calling the proc is that all previous submitted command lists |
| 202 | // have finished when we call it. However, if our current command list has no work when it is |
| 203 | // flushed it will drop its ref to the callback immediately. But the previous work may not have |
| 204 | // finished. It is safe to only add the proc to the newest outstanding commandlist cause that |
| 205 | // must finish after all previously submitted command lists. |
| 206 | OutstandingCommandList* back = (OutstandingCommandList*)fOutstandingCommandLists.back(); |
| 207 | if (back) { |
| 208 | back->fCommandList->addFinishedCallback(finishedCallback); |
| 209 | } |
| 210 | fCurrentDirectCommandList->addFinishedCallback(std::move(finishedCallback)); |
| 211 | } |
| 212 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 213 | void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) { |
| 214 | // TODO |
| 215 | } |
| 216 | |
| 217 | sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions, |
| 218 | const GrBackendFormat& format, |
| 219 | GrRenderable renderable, |
| 220 | int renderTargetSampleCnt, |
| 221 | SkBudgeted budgeted, |
| 222 | GrProtected isProtected, |
| 223 | int mipLevelCount, |
| 224 | uint32_t levelClearMask) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 225 | DXGI_FORMAT dxgiFormat; |
| 226 | SkAssertResult(format.asDxgiFormat(&dxgiFormat)); |
| 227 | SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat)); |
| 228 | |
| 229 | D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE; |
| 230 | |
| 231 | if (renderable == GrRenderable::kYes) { |
| 232 | usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 233 | } |
| 234 | |
| 235 | // This desc refers to the texture that will be read by the client. Thus even if msaa is |
| 236 | // requested, this describes the resolved texture. Therefore we always have samples set |
| 237 | // to 1. |
| 238 | SkASSERT(mipLevelCount > 0); |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 239 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 240 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 241 | // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else |
| 242 | // might want to manually set alignment to 4KB for smaller textures |
| 243 | resourceDesc.Alignment = 0; |
| 244 | resourceDesc.Width = dimensions.fWidth; |
| 245 | resourceDesc.Height = dimensions.fHeight; |
| 246 | resourceDesc.DepthOrArraySize = 1; |
| 247 | resourceDesc.MipLevels = mipLevelCount; |
| 248 | resourceDesc.Format = dxgiFormat; |
| 249 | resourceDesc.SampleDesc.Count = 1; |
Greg Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 250 | // quality levels are only supported for tiled resources so ignore for now |
| 251 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 252 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 253 | resourceDesc.Flags = usageFlags; |
| 254 | |
| 255 | GrMipMapsStatus mipMapsStatus = |
| 256 | mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated; |
| 257 | |
| 258 | sk_sp<GrD3DTexture> tex; |
| 259 | if (renderable == GrRenderable::kYes) { |
| 260 | tex = GrD3DTextureRenderTarget::MakeNewTextureRenderTarget( |
| 261 | this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected, |
| 262 | mipMapsStatus); |
| 263 | } else { |
| 264 | tex = GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected, |
| 265 | mipMapsStatus); |
| 266 | } |
| 267 | |
| 268 | if (!tex) { |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
| 272 | if (levelClearMask) { |
| 273 | // TODO |
| 274 | } |
| 275 | return std::move(tex); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions, |
| 279 | const GrBackendFormat& format, |
| 280 | SkBudgeted budgeted, |
| 281 | GrMipMapped mipMapped, |
| 282 | GrProtected isProtected, |
| 283 | const void* data, size_t dataSize) { |
| 284 | // TODO |
| 285 | return nullptr; |
| 286 | } |
| 287 | |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 288 | static int get_surface_sample_cnt(GrSurface* surf) { |
| 289 | if (const GrRenderTarget* rt = surf->asRenderTarget()) { |
| 290 | return rt->numSamples(); |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | bool GrD3DGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
| 296 | const SkIPoint& dstPoint) { |
| 297 | |
| 298 | if (src->isProtected() && !dst->isProtected()) { |
| 299 | SkDebugf("Can't copy from protected memory to non-protected"); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 300 | return false; |
| 301 | } |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 302 | |
| 303 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 304 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 305 | |
| 306 | GrD3DTextureResource* dstTexResource; |
| 307 | GrD3DTextureResource* srcTexResource; |
| 308 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 309 | if (dstRT) { |
| 310 | GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(dstRT); |
| 311 | dstTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT; |
| 312 | } else { |
| 313 | SkASSERT(dst->asTexture()); |
| 314 | dstTexResource = static_cast<GrD3DTexture*>(dst->asTexture()); |
| 315 | } |
| 316 | GrRenderTarget* srcRT = src->asRenderTarget(); |
| 317 | if (srcRT) { |
| 318 | GrD3DRenderTarget* d3dRT = static_cast<GrD3DRenderTarget*>(srcRT); |
| 319 | srcTexResource = d3dRT->numSamples() > 1 ? d3dRT->msaaTextureResource() : d3dRT; |
| 320 | } else { |
| 321 | SkASSERT(src->asTexture()); |
| 322 | srcTexResource = static_cast<GrD3DTexture*>(src->asTexture()); |
| 323 | } |
| 324 | |
| 325 | DXGI_FORMAT dstFormat = dstTexResource->dxgiFormat(); |
| 326 | DXGI_FORMAT srcFormat = srcTexResource->dxgiFormat(); |
| 327 | |
| 328 | if (this->d3dCaps().canCopyAsResolve(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) { |
| 329 | this->copySurfaceAsResolve(dst, src, srcRect, dstPoint); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | if (this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)) { |
| 334 | this->copySurfaceAsCopyTexture(dst, src, dstTexResource, srcTexResource, srcRect, dstPoint); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | void GrD3DGpu::copySurfaceAsCopyTexture(GrSurface* dst, GrSurface* src, |
| 342 | GrD3DTextureResource* dstResource, |
| 343 | GrD3DTextureResource* srcResource, |
| 344 | const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 345 | #ifdef SK_DEBUG |
| 346 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 347 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 348 | DXGI_FORMAT dstFormat = dstResource->dxgiFormat(); |
| 349 | DXGI_FORMAT srcFormat; |
| 350 | SkAssertResult(dst->backendFormat().asDxgiFormat(&srcFormat)); |
| 351 | SkASSERT(this->d3dCaps().canCopyTexture(dstFormat, dstSampleCnt, srcFormat, srcSampleCnt)); |
| 352 | #endif |
| 353 | if (src->isProtected() && !dst->isProtected()) { |
| 354 | SkDebugf("Can't copy from protected memory to non-protected"); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | dstResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST); |
| 359 | srcResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE); |
| 360 | |
| 361 | D3D12_TEXTURE_COPY_LOCATION dstLocation = {}; |
| 362 | dstLocation.pResource = dstResource->d3dResource(); |
| 363 | dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; |
| 364 | dstLocation.SubresourceIndex = 0; |
| 365 | |
| 366 | D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; |
| 367 | srcLocation.pResource = srcResource->d3dResource(); |
| 368 | srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; |
| 369 | srcLocation.SubresourceIndex = 0; |
| 370 | |
| 371 | D3D12_BOX srcBox = {}; |
| 372 | srcBox.left = srcRect.fLeft; |
| 373 | srcBox.top = srcRect.fTop; |
| 374 | srcBox.right = srcRect.fRight; |
| 375 | srcBox.bottom = srcRect.fBottom; |
| 376 | srcBox.front = 0; |
| 377 | srcBox.back = 1; |
| 378 | // TODO: use copyResource if copying full resource and sizes match |
| 379 | fCurrentDirectCommandList->copyTextureRegion(dstResource->resource(), |
| 380 | &dstLocation, |
| 381 | dstPoint.fX, dstPoint.fY, |
| 382 | srcResource->resource(), |
| 383 | &srcLocation, |
| 384 | &srcBox); |
| 385 | |
| 386 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 387 | srcRect.width(), srcRect.height()); |
| 388 | // The rect is already in device space so we pass in kTopLeft so no flip is done. |
| 389 | this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect); |
| 390 | } |
| 391 | |
| 392 | void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
| 393 | const SkIPoint& dstPoint) { |
Jim Van Verth | 275f419 | 2020-07-07 15:36:41 -0400 | [diff] [blame] | 394 | GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget()); |
| 395 | SkASSERT(srcRT); |
| 396 | |
| 397 | this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect); |
| 398 | } |
| 399 | |
| 400 | void GrD3DGpu::resolveTexture(GrSurface* dst, int32_t dstX, int32_t dstY, |
| 401 | GrD3DRenderTarget* src, const SkIRect& srcIRect) { |
| 402 | SkASSERT(dst); |
| 403 | SkASSERT(src && src->numSamples() > 1 && src->msaaTextureResource()); |
| 404 | |
| 405 | D3D12_RECT srcRect = { srcIRect.fLeft, srcIRect.fTop, srcIRect.fRight, srcIRect.fBottom }; |
| 406 | |
| 407 | GrD3DTextureResource* dstTextureResource; |
| 408 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 409 | if (dstRT) { |
| 410 | dstTextureResource = static_cast<GrD3DRenderTarget*>(dstRT); |
| 411 | } else { |
| 412 | SkASSERT(dst->asTexture()); |
| 413 | dstTextureResource = static_cast<GrD3DTexture*>(dst->asTexture()); |
| 414 | } |
| 415 | |
| 416 | dstTextureResource->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_DEST); |
| 417 | src->msaaTextureResource()->setResourceState(this, D3D12_RESOURCE_STATE_RESOLVE_SOURCE); |
| 418 | |
| 419 | fCurrentDirectCommandList->resolveSubresourceRegion(dstTextureResource, dstX, dstY, |
| 420 | src->msaaTextureResource(), &srcRect); |
| 421 | } |
| 422 | |
Jim Van Verth | bb61fe3 | 2020-07-07 16:39:04 -0400 | [diff] [blame] | 423 | void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) { |
Jim Van Verth | 275f419 | 2020-07-07 15:36:41 -0400 | [diff] [blame] | 424 | SkASSERT(target->numSamples() > 1); |
| 425 | GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(target); |
| 426 | SkASSERT(rt->msaaTextureResource()); |
| 427 | |
| 428 | this->resolveTexture(target, resolveRect.fLeft, resolveRect.fTop, rt, resolveRect); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 431 | bool GrD3DGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height, |
| 432 | GrColorType surfaceColorType, GrColorType dstColorType, void* buffer, |
| 433 | size_t rowBytes) { |
| 434 | SkASSERT(surface); |
| 435 | |
| 436 | if (surfaceColorType != dstColorType) { |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | // Set up src location and box |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 441 | GrD3DTextureResource* texResource = nullptr; |
| 442 | GrD3DRenderTarget* rt = static_cast<GrD3DRenderTarget*>(surface->asRenderTarget()); |
| 443 | if (rt) { |
| 444 | texResource = rt; |
| 445 | } else { |
| 446 | texResource = static_cast<GrD3DTexture*>(surface->asTexture()); |
| 447 | } |
| 448 | |
| 449 | if (!texResource) { |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 450 | return false; |
| 451 | } |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 452 | |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 453 | D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 454 | srcLocation.pResource = texResource->d3dResource(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 455 | SkASSERT(srcLocation.pResource); |
| 456 | srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; |
| 457 | srcLocation.SubresourceIndex = 0; |
| 458 | |
| 459 | D3D12_BOX srcBox = {}; |
| 460 | srcBox.left = left; |
| 461 | srcBox.top = top; |
| 462 | srcBox.right = left + width; |
| 463 | srcBox.bottom = top + height; |
| 464 | srcBox.front = 0; |
| 465 | srcBox.back = 1; |
| 466 | |
| 467 | // Set up dst location and create transfer buffer |
| 468 | D3D12_TEXTURE_COPY_LOCATION dstLocation = {}; |
| 469 | dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 470 | UINT64 transferTotalBytes; |
| 471 | const UINT64 baseOffset = 0; |
| 472 | D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc(); |
| 473 | fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint, |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 474 | nullptr, nullptr, &transferTotalBytes); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 475 | SkASSERT(transferTotalBytes); |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 476 | size_t bpp = GrColorTypeBytesPerPixel(dstColorType); |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 477 | if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) { |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | size_t tightRowBytes = bpp * width; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 481 | |
| 482 | // TODO: implement some way of reusing buffers instead of making a new one every time. |
| 483 | sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes, |
| 484 | GrGpuBufferType::kXferGpuToCpu, |
| 485 | kDynamic_GrAccessPattern); |
| 486 | GrD3DBuffer* d3dBuf = static_cast<GrD3DBuffer*>(transferBuffer.get()); |
| 487 | dstLocation.pResource = d3dBuf->d3dResource(); |
| 488 | |
| 489 | // Need to change the resource state to COPY_SOURCE in order to download from it |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 490 | texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 491 | |
| 492 | fCurrentDirectCommandList->copyTextureRegion(d3dBuf->resource(), &dstLocation, 0, 0, |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 493 | texResource->resource(), &srcLocation, &srcBox); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 494 | this->submitDirectCommandList(SyncQueue::kForce); |
| 495 | |
| 496 | const void* mappedMemory = transferBuffer->map(); |
| 497 | |
| 498 | SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch, |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 499 | tightRowBytes, height); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 500 | |
| 501 | transferBuffer->unmap(); |
| 502 | |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | bool GrD3DGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height, |
| 507 | GrColorType surfaceColorType, GrColorType srcColorType, |
| 508 | const GrMipLevel texels[], int mipLevelCount, |
| 509 | bool prepForTexSampling) { |
| 510 | GrD3DTexture* d3dTex = static_cast<GrD3DTexture*>(surface->asTexture()); |
| 511 | if (!d3dTex) { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | // Make sure we have at least the base level |
| 516 | if (!mipLevelCount || !texels[0].fPixels) { |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | SkASSERT(!GrDxgiFormatIsCompressed(d3dTex->dxgiFormat())); |
| 521 | bool success = false; |
| 522 | |
| 523 | // Need to change the resource state to COPY_DEST in order to upload to it |
| 524 | d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST); |
| 525 | |
| 526 | SkASSERT(mipLevelCount <= d3dTex->texturePriv().maxMipMapLevel() + 1); |
| 527 | success = this->uploadToTexture(d3dTex, left, top, width, height, srcColorType, texels, |
| 528 | mipLevelCount); |
| 529 | |
| 530 | if (prepForTexSampling) { |
| 531 | d3dTex->setResourceState(this, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); |
| 532 | } |
| 533 | |
| 534 | return success; |
| 535 | } |
| 536 | |
| 537 | bool GrD3DGpu::uploadToTexture(GrD3DTexture* tex, int left, int top, int width, int height, |
| 538 | GrColorType colorType, const GrMipLevel* texels, int mipLevelCount) { |
| 539 | SkASSERT(this->caps()->isFormatTexturable(tex->backendFormat())); |
| 540 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 541 | SkASSERT(1 == mipLevelCount || |
| 542 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 543 | |
| 544 | // We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 545 | // first. |
| 546 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1)); |
| 547 | |
| 548 | if (width == 0 || height == 0) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | SkASSERT(this->d3dCaps().surfaceSupportsWritePixels(tex)); |
| 553 | SkASSERT(this->d3dCaps().areColorTypeAndFormatCompatible(colorType, tex->backendFormat())); |
| 554 | |
| 555 | ID3D12Resource* d3dResource = tex->d3dResource(); |
| 556 | SkASSERT(d3dResource); |
| 557 | D3D12_RESOURCE_DESC desc = d3dResource->GetDesc(); |
| 558 | // Either upload only the first miplevel or all miplevels |
| 559 | SkASSERT(1 == mipLevelCount || mipLevelCount == (int)desc.MipLevels); |
| 560 | |
| 561 | if (1 == mipLevelCount && !texels[0].fPixels) { |
| 562 | return true; // no data to upload |
| 563 | } |
| 564 | |
| 565 | for (int i = 0; i < mipLevelCount; ++i) { |
| 566 | // We do not allow any gaps in the mip data |
| 567 | if (!texels[i].fPixels) { |
| 568 | return false; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 573 | UINT64 combinedBufferSize; |
| 574 | // We reset the width and height in the description to match our subrectangle size |
| 575 | // so we don't end up allocating more space than we need. |
| 576 | desc.Width = width; |
| 577 | desc.Height = height; |
| 578 | fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(), |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 579 | nullptr, nullptr, &combinedBufferSize); |
| 580 | size_t bpp = GrColorTypeBytesPerPixel(colorType); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 581 | SkASSERT(combinedBufferSize); |
| 582 | |
| 583 | // TODO: do this until we have slices of buttery buffers |
| 584 | sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize, |
| 585 | GrGpuBufferType::kXferCpuToGpu, |
| 586 | kDynamic_GrAccessPattern); |
| 587 | if (!transferBuffer) { |
| 588 | return false; |
| 589 | } |
| 590 | char* bufferData = (char*)transferBuffer->map(); |
| 591 | |
| 592 | int currentWidth = width; |
| 593 | int currentHeight = height; |
| 594 | int layerHeight = tex->height(); |
| 595 | |
| 596 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 597 | if (texels[currentMipLevel].fPixels) { |
| 598 | SkASSERT(1 == mipLevelCount || currentHeight == layerHeight); |
| 599 | |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 600 | const size_t trimRowBytes = currentWidth * bpp; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 601 | const size_t srcRowBytes = texels[currentMipLevel].fRowBytes; |
| 602 | |
| 603 | char* dst = bufferData + placedFootprints[currentMipLevel].Offset; |
| 604 | |
| 605 | // copy data into the buffer, skipping any trailing bytes |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 606 | const char* src = (const char*)texels[currentMipLevel].fPixels; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 607 | SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch, |
| 608 | src, srcRowBytes, trimRowBytes, currentHeight); |
| 609 | } |
| 610 | currentWidth = std::max(1, currentWidth / 2); |
| 611 | currentHeight = std::max(1, currentHeight / 2); |
| 612 | layerHeight = currentHeight; |
| 613 | } |
| 614 | |
| 615 | transferBuffer->unmap(); |
| 616 | |
| 617 | GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get()); |
| 618 | fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount, |
| 619 | placedFootprints.get(), left, top); |
| 620 | |
| 621 | if (mipLevelCount < (int)desc.MipLevels) { |
| 622 | tex->texturePriv().markMipMapsDirty(); |
| 623 | } |
| 624 | |
| 625 | return true; |
| 626 | } |
| 627 | |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 628 | static bool check_resource_info(const GrD3DTextureResourceInfo& info) { |
| 629 | if (!info.fResource.get()) { |
| 630 | return false; |
| 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 635 | static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) { |
| 636 | if (!caps.isFormatTexturable(info.fFormat)) { |
| 637 | return false; |
| 638 | } |
| 639 | return true; |
| 640 | } |
| 641 | |
| 642 | static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info, |
| 643 | int sampleCnt) { |
| 644 | if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) { |
| 645 | return false; |
| 646 | } |
| 647 | return true; |
| 648 | } |
| 649 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 650 | sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex, |
| 651 | GrWrapOwnership, |
| 652 | GrWrapCacheable wrapType, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 653 | GrIOType ioType) { |
| 654 | GrD3DTextureResourceInfo textureInfo; |
| 655 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 656 | return nullptr; |
| 657 | } |
| 658 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 659 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 660 | return nullptr; |
| 661 | } |
| 662 | |
| 663 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 664 | return nullptr; |
| 665 | } |
| 666 | |
| 667 | // TODO: support protected context |
| 668 | if (tex.isProtected()) { |
| 669 | return nullptr; |
| 670 | } |
| 671 | |
| 672 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 673 | SkASSERT(state); |
| 674 | return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo, |
| 675 | std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 679 | GrWrapOwnership, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 680 | GrWrapCacheable wrapType) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 681 | GrD3DTextureResourceInfo textureInfo; |
| 682 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 683 | return nullptr; |
| 684 | } |
| 685 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 686 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 687 | return nullptr; |
| 688 | } |
| 689 | |
| 690 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 691 | return nullptr; |
| 692 | } |
| 693 | |
| 694 | // TODO: support protected context |
| 695 | if (tex.isProtected()) { |
| 696 | return nullptr; |
| 697 | } |
| 698 | |
| 699 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 700 | SkASSERT(state); |
| 701 | return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, kRead_GrIOType, |
| 702 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex, |
| 706 | int sampleCnt, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 707 | GrWrapOwnership ownership, |
| 708 | GrWrapCacheable cacheable) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 709 | GrD3DTextureResourceInfo textureInfo; |
| 710 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 711 | return nullptr; |
| 712 | } |
| 713 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 714 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 715 | return nullptr; |
| 716 | } |
| 717 | |
| 718 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 719 | return nullptr; |
| 720 | } |
| 721 | if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) { |
| 722 | return nullptr; |
| 723 | } |
| 724 | |
| 725 | // TODO: support protected context |
| 726 | if (tex.isProtected()) { |
| 727 | return nullptr; |
| 728 | } |
| 729 | |
| 730 | sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat); |
| 731 | |
| 732 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 733 | SkASSERT(state); |
| 734 | |
| 735 | return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(), |
| 736 | sampleCnt, cacheable, |
| 737 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 738 | } |
| 739 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 740 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 741 | // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In |
| 742 | // general this is not an issue since swapchain images in D3D are never multisampled. Thus if |
| 743 | // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle |
| 744 | // creating and owning the MSAA images. |
| 745 | if (rt.sampleCnt() > 1) { |
| 746 | return nullptr; |
| 747 | } |
| 748 | |
| 749 | GrD3DTextureResourceInfo info; |
| 750 | if (!rt.getD3DTextureResourceInfo(&info)) { |
| 751 | return nullptr; |
| 752 | } |
| 753 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 754 | if (!check_resource_info(info)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 755 | return nullptr; |
| 756 | } |
| 757 | |
| 758 | if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) { |
| 759 | return nullptr; |
| 760 | } |
| 761 | |
| 762 | // TODO: support protected context |
| 763 | if (rt.isProtected()) { |
| 764 | return nullptr; |
| 765 | } |
| 766 | |
| 767 | sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState(); |
| 768 | |
| 769 | sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget( |
| 770 | this, rt.dimensions(), 1, info, std::move(state)); |
| 771 | |
| 772 | // We don't allow the client to supply a premade stencil buffer. We always create one if needed. |
| 773 | SkASSERT(!rt.stencilBits()); |
| 774 | if (tgt) { |
| 775 | SkASSERT(tgt->canAttemptStencilAttachment()); |
| 776 | } |
| 777 | |
| 778 | return std::move(tgt); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 782 | int sampleCnt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 783 | |
| 784 | GrD3DTextureResourceInfo textureInfo; |
| 785 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 786 | return nullptr; |
| 787 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 788 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 789 | return nullptr; |
| 790 | } |
| 791 | |
| 792 | if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) { |
| 793 | return nullptr; |
| 794 | } |
| 795 | |
| 796 | // TODO: support protected context |
| 797 | if (tex.isProtected()) { |
| 798 | return nullptr; |
| 799 | } |
| 800 | |
| 801 | sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat); |
| 802 | if (!sampleCnt) { |
| 803 | return nullptr; |
| 804 | } |
| 805 | |
| 806 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 807 | SkASSERT(state); |
| 808 | |
| 809 | return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt, |
| 810 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type, |
Jim Van Verth | d6ad480 | 2020-04-03 14:59:20 -0400 | [diff] [blame] | 814 | GrAccessPattern accessPattern, const void* data) { |
| 815 | sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern); |
| 816 | if (data && buffer) { |
| 817 | buffer->updateData(data, sizeInBytes); |
| 818 | } |
| 819 | |
| 820 | return std::move(buffer); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget( |
| 824 | const GrRenderTarget* rt, int width, int height, int numStencilSamples) { |
Jim Van Verth | 4f51f47 | 2020-04-13 11:02:21 -0400 | [diff] [blame] | 825 | SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport()); |
| 826 | SkASSERT(width >= rt->width()); |
| 827 | SkASSERT(height >= rt->height()); |
| 828 | |
| 829 | const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat(); |
| 830 | |
| 831 | GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this, |
| 832 | width, |
| 833 | height, |
| 834 | numStencilSamples, |
| 835 | sFmt)); |
| 836 | fStats.incStencilAttachmentCreates(); |
| 837 | return stencil; |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 838 | } |
| 839 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 840 | bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat, |
| 841 | SkISize dimensions, |
| 842 | GrTexturable texturable, |
| 843 | GrRenderable renderable, |
| 844 | GrMipMapped mipMapped, |
| 845 | GrD3DTextureResourceInfo* info, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 846 | GrProtected isProtected) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 847 | SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes); |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 848 | |
| 849 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) { |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) { |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | int numMipLevels = 1; |
| 862 | if (mipMapped == GrMipMapped::kYes) { |
| 863 | numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
| 864 | } |
| 865 | |
| 866 | // create the texture |
| 867 | D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE; |
| 868 | if (renderable == GrRenderable::kYes) { |
| 869 | usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 870 | } |
| 871 | |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 872 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 873 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 874 | resourceDesc.Alignment = 0; // use default alignment |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 875 | resourceDesc.Width = dimensions.fWidth; |
| 876 | resourceDesc.Height = dimensions.fHeight; |
| 877 | resourceDesc.DepthOrArraySize = 1; |
| 878 | resourceDesc.MipLevels = numMipLevels; |
| 879 | resourceDesc.Format = dxgiFormat; |
| 880 | resourceDesc.SampleDesc.Count = 1; |
Greg Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 881 | // quality levels are only supported for tiled resources so ignore for now |
| 882 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 883 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 884 | resourceDesc.Flags = usageFlags; |
| 885 | |
Jim Van Verth | 280d4f7 | 2020-05-04 10:04:24 -0400 | [diff] [blame] | 886 | D3D12_CLEAR_VALUE* clearValuePtr = nullptr; |
| 887 | D3D12_CLEAR_VALUE clearValue = {}; |
| 888 | if (renderable == GrRenderable::kYes) { |
| 889 | clearValue.Format = dxgiFormat; |
| 890 | // Assume transparent black |
| 891 | clearValue.Color[0] = 0; |
| 892 | clearValue.Color[1] = 0; |
| 893 | clearValue.Color[2] = 0; |
| 894 | clearValue.Color[3] = 0; |
| 895 | clearValuePtr = &clearValue; |
| 896 | } |
| 897 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 898 | D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes) |
| 899 | ? D3D12_RESOURCE_STATE_RENDER_TARGET |
| 900 | : D3D12_RESOURCE_STATE_COPY_DEST; |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 901 | if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState, |
Jim Van Verth | 280d4f7 | 2020-05-04 10:04:24 -0400 | [diff] [blame] | 902 | isProtected, clearValuePtr, info)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 903 | SkDebugf("Failed to init texture resource info\n"); |
| 904 | return false; |
| 905 | } |
| 906 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 907 | return true; |
| 908 | } |
| 909 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 910 | GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 911 | const GrBackendFormat& format, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 912 | GrRenderable renderable, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 913 | GrMipMapped mipMapped, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 914 | GrProtected isProtected) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 915 | this->handleDirtyContext(); |
| 916 | |
| 917 | const GrD3DCaps& caps = this->d3dCaps(); |
| 918 | |
| 919 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 920 | return {}; |
| 921 | } |
| 922 | |
| 923 | DXGI_FORMAT dxgiFormat; |
| 924 | if (!format.asDxgiFormat(&dxgiFormat)) { |
| 925 | return {}; |
| 926 | } |
| 927 | |
| 928 | // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here |
| 929 | if (!caps.isFormatTexturable(dxgiFormat)) { |
| 930 | return {}; |
| 931 | } |
| 932 | |
| 933 | GrD3DTextureResourceInfo info; |
| 934 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes, |
| 935 | renderable, mipMapped, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 936 | &info, isProtected)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 937 | return {}; |
| 938 | } |
| 939 | |
| 940 | return GrBackendTexture(dimensions.width(), dimensions.height(), info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 941 | } |
| 942 | |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 943 | bool copy_src_data(GrD3DGpu* gpu, char* mapPtr, DXGI_FORMAT dxgiFormat, |
| 944 | D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints, |
| 945 | const SkPixmap srcData[], int numMipLevels) { |
| 946 | SkASSERT(srcData && numMipLevels); |
| 947 | SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat)); |
| 948 | SkASSERT(mapPtr); |
| 949 | |
| 950 | size_t bytesPerPixel = gpu->d3dCaps().bytesPerPixel(dxgiFormat); |
| 951 | |
| 952 | for (int currentMipLevel = 0; currentMipLevel < numMipLevels; currentMipLevel++) { |
| 953 | const size_t trimRowBytes = srcData[currentMipLevel].width() * bytesPerPixel; |
| 954 | |
| 955 | // copy data into the buffer, skipping any trailing bytes |
| 956 | char* dst = mapPtr + placedFootprints[currentMipLevel].Offset; |
| 957 | SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch, |
| 958 | srcData[currentMipLevel].addr(), srcData[currentMipLevel].rowBytes(), |
| 959 | trimRowBytes, srcData[currentMipLevel].height()); |
| 960 | } |
| 961 | |
| 962 | return true; |
| 963 | } |
| 964 | |
Greg Daniel | 746460e | 2020-06-30 13:13:53 -0400 | [diff] [blame] | 965 | bool copy_color_data(const GrD3DCaps& caps, char* mapPtr, DXGI_FORMAT dxgiFormat, |
| 966 | SkISize dimensions, D3D12_PLACED_SUBRESOURCE_FOOTPRINT* placedFootprints, |
| 967 | SkColor4f color) { |
| 968 | auto colorType = caps.getFormatColorType(dxgiFormat); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 969 | if (colorType == GrColorType::kUnknown) { |
| 970 | return false; |
| 971 | } |
| 972 | GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
| 973 | if (!GrClearImage(ii, mapPtr, placedFootprints[0].Footprint.RowPitch, color)) { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | return true; |
| 978 | } |
| 979 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 980 | bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture, |
| 981 | sk_sp<GrRefCntedCallback> finishedCallback, |
| 982 | const BackendTextureData* data) { |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 983 | GrD3DTextureResourceInfo info; |
| 984 | SkAssertResult(backendTexture.getD3DTextureResourceInfo(&info)); |
| 985 | |
| 986 | sk_sp<GrD3DResourceState> state = backendTexture.getGrD3DResourceState(); |
| 987 | SkASSERT(state); |
| 988 | sk_sp<GrD3DTexture> texture = |
| 989 | GrD3DTexture::MakeWrappedTexture(this, backendTexture.dimensions(), |
| 990 | GrWrapCacheable::kNo, |
| 991 | kRW_GrIOType, info, std::move(state)); |
| 992 | if (!texture) { |
| 993 | return false; |
| 994 | } |
| 995 | |
| 996 | GrD3DDirectCommandList* cmdList = this->currentCommandList(); |
| 997 | if (!cmdList) { |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | texture->setResourceState(this, D3D12_RESOURCE_STATE_COPY_DEST); |
| 1002 | |
| 1003 | ID3D12Resource* d3dResource = texture->d3dResource(); |
| 1004 | SkASSERT(d3dResource); |
| 1005 | D3D12_RESOURCE_DESC desc = d3dResource->GetDesc(); |
| 1006 | unsigned int mipLevelCount = 1; |
| 1007 | if (backendTexture.fMipMapped == GrMipMapped::kYes) { |
| 1008 | mipLevelCount = SkMipMap::ComputeLevelCount(backendTexture.dimensions().width(), |
| 1009 | backendTexture.dimensions().height()) + 1; |
| 1010 | } |
| 1011 | SkASSERT(mipLevelCount == info.fLevelCount); |
| 1012 | SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount); |
| 1013 | UINT64 combinedBufferSize; |
| 1014 | fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(), |
| 1015 | nullptr, nullptr, &combinedBufferSize); |
| 1016 | SkASSERT(combinedBufferSize); |
| 1017 | if (data->type() == BackendTextureData::Type::kColor && |
| 1018 | !GrDxgiFormatIsCompressed(info.fFormat) && mipLevelCount > 1) { |
| 1019 | // For a single uncompressed color, we reuse the same top-level buffer area for all levels. |
| 1020 | combinedBufferSize = |
| 1021 | placedFootprints[0].Footprint.RowPitch * placedFootprints[0].Footprint.Height; |
| 1022 | for (unsigned int i = 1; i < mipLevelCount; ++i) { |
| 1023 | placedFootprints[i].Offset = 0; |
| 1024 | placedFootprints[i].Footprint.RowPitch = placedFootprints[0].Footprint.RowPitch; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // TODO: do this until we have slices of buttery buffers |
| 1029 | sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(combinedBufferSize, |
| 1030 | GrGpuBufferType::kXferCpuToGpu, |
| 1031 | kDynamic_GrAccessPattern); |
| 1032 | if (!transferBuffer) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | char* bufferData = (char*)transferBuffer->map(); |
| 1036 | SkASSERT(bufferData); |
| 1037 | |
| 1038 | bool result; |
| 1039 | if (data->type() == BackendTextureData::Type::kPixmaps) { |
| 1040 | result = copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), |
| 1041 | data->pixmaps(), info.fLevelCount); |
| 1042 | } else if (data->type() == BackendTextureData::Type::kCompressed) { |
| 1043 | memcpy(bufferData, data->compressedData(), data->compressedSize()); |
| 1044 | result = true; |
| 1045 | } else { |
| 1046 | SkASSERT(data->type() == BackendTextureData::Type::kColor); |
| 1047 | SkImage::CompressionType compression = |
| 1048 | GrBackendFormatToCompressionType(backendTexture.getBackendFormat()); |
| 1049 | if (SkImage::CompressionType::kNone == compression) { |
Greg Daniel | 746460e | 2020-06-30 13:13:53 -0400 | [diff] [blame] | 1050 | result = copy_color_data(this->d3dCaps(), bufferData, info.fFormat, |
| 1051 | backendTexture.dimensions(), |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1052 | placedFootprints, data->color()); |
| 1053 | } else { |
| 1054 | GrFillInCompressedData(compression, backendTexture.dimensions(), |
| 1055 | backendTexture.fMipMapped, bufferData, data->color()); |
| 1056 | result = true; |
| 1057 | } |
| 1058 | } |
| 1059 | transferBuffer->unmap(); |
| 1060 | |
| 1061 | GrD3DBuffer* d3dBuffer = static_cast<GrD3DBuffer*>(transferBuffer.get()); |
| 1062 | cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), |
| 1063 | 0, 0); |
| 1064 | |
| 1065 | if (finishedCallback) { |
| 1066 | this->addFinishedCallback(std::move(finishedCallback)); |
| 1067 | } |
| 1068 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
| 1071 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 1072 | GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture( |
| 1073 | SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped, |
| 1074 | GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback, |
| 1075 | const BackendTextureData* data) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1076 | this->handleDirtyContext(); |
| 1077 | |
| 1078 | const GrD3DCaps& caps = this->d3dCaps(); |
| 1079 | |
| 1080 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 1081 | return {}; |
| 1082 | } |
| 1083 | |
| 1084 | DXGI_FORMAT dxgiFormat; |
| 1085 | if (!format.asDxgiFormat(&dxgiFormat)) { |
| 1086 | return {}; |
| 1087 | } |
| 1088 | |
| 1089 | // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here |
| 1090 | if (!caps.isFormatTexturable(dxgiFormat)) { |
| 1091 | return {}; |
| 1092 | } |
| 1093 | |
| 1094 | GrD3DTextureResourceInfo info; |
| 1095 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes, |
| 1096 | GrRenderable::kNo, mipMapped, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1097 | &info, isProtected)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1098 | return {}; |
| 1099 | } |
| 1100 | |
| 1101 | return GrBackendTexture(dimensions.width(), dimensions.height(), info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1105 | SkASSERT(GrBackendApi::kDirect3D == tex.fBackend); |
| 1106 | // Nothing to do here, will get cleaned up when the GrBackendTexture object goes away |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1107 | } |
| 1108 | |
Robert Phillips | 979b223 | 2020-02-20 10:47:29 -0500 | [diff] [blame] | 1109 | bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) { |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1113 | #if GR_TEST_UTILS |
| 1114 | bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1115 | SkASSERT(GrBackendApi::kDirect3D == tex.backend()); |
| 1116 | |
| 1117 | GrD3DTextureResourceInfo info; |
| 1118 | if (!tex.getD3DTextureResourceInfo(&info)) { |
| 1119 | return false; |
| 1120 | } |
| 1121 | ID3D12Resource* textureResource = info.fResource.get(); |
| 1122 | if (!textureResource) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 1129 | GrColorType colorType) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1130 | this->handleDirtyContext(); |
| 1131 | |
| 1132 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 1133 | return {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType); |
| 1137 | |
| 1138 | GrD3DTextureResourceInfo info; |
| 1139 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo, |
| 1140 | GrRenderable::kYes, GrMipMapped::kNo, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1141 | &info, GrProtected::kNo)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1142 | return {}; |
| 1143 | } |
| 1144 | |
| 1145 | return GrBackendRenderTarget(w, h, 1, info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1146 | } |
| 1147 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1148 | void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) { |
| 1149 | SkASSERT(GrBackendApi::kDirect3D == rt.backend()); |
| 1150 | |
| 1151 | GrD3DTextureResourceInfo info; |
| 1152 | if (rt.getD3DTextureResourceInfo(&info)) { |
| 1153 | this->testingOnly_flushGpuAndSync(); |
| 1154 | // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget |
| 1155 | // is deleted. |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | void GrD3DGpu::testingOnly_flushGpuAndSync() { |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1160 | SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce)); |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1161 | } |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1162 | |
Jim Van Verth | 9b5e16c | 2020-04-20 10:45:52 -0400 | [diff] [blame] | 1163 | void GrD3DGpu::testingOnly_startCapture() { |
| 1164 | if (fGraphicsAnalysis) { |
| 1165 | fGraphicsAnalysis->BeginCapture(); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | void GrD3DGpu::testingOnly_endCapture() { |
| 1170 | if (fGraphicsAnalysis) { |
| 1171 | fGraphicsAnalysis->EndCapture(); |
| 1172 | } |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1176 | /////////////////////////////////////////////////////////////////////////////// |
| 1177 | |
Greg Daniel | a5a6b32 | 2020-04-23 12:52:27 -0400 | [diff] [blame] | 1178 | void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource, |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1179 | int numBarriers, |
| 1180 | D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const { |
| 1181 | SkASSERT(fCurrentDirectCommandList); |
| 1182 | SkASSERT(resource); |
| 1183 | |
Greg Daniel | a5a6b32 | 2020-04-23 12:52:27 -0400 | [diff] [blame] | 1184 | fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1185 | } |
| 1186 | |
Greg Daniel | 9efe386 | 2020-06-11 11:51:06 -0400 | [diff] [blame] | 1187 | void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates( |
| 1188 | GrSurfaceProxy* proxies[], |
| 1189 | int numProxies, |
| 1190 | SkSurface::BackendSurfaceAccess access, |
| 1191 | const GrBackendSurfaceMutableState* newState) { |
Jim Van Verth | 682a2f4 | 2020-05-13 16:54:09 -0400 | [diff] [blame] | 1192 | SkASSERT(numProxies >= 0); |
| 1193 | SkASSERT(!numProxies || proxies); |
| 1194 | |
| 1195 | // prepare proxies by transitioning to PRESENT renderState |
| 1196 | if (numProxies && access == SkSurface::BackendSurfaceAccess::kPresent) { |
| 1197 | GrD3DTextureResource* resource; |
| 1198 | for (int i = 0; i < numProxies; ++i) { |
| 1199 | SkASSERT(proxies[i]->isInstantiated()); |
| 1200 | if (GrTexture* tex = proxies[i]->peekTexture()) { |
| 1201 | resource = static_cast<GrD3DTexture*>(tex); |
| 1202 | } else { |
| 1203 | GrRenderTarget* rt = proxies[i]->peekRenderTarget(); |
| 1204 | SkASSERT(rt); |
| 1205 | resource = static_cast<GrD3DRenderTarget*>(rt); |
| 1206 | } |
| 1207 | resource->prepareForPresent(this); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1212 | bool GrD3DGpu::onSubmitToGpu(bool syncCpu) { |
| 1213 | if (syncCpu) { |
| 1214 | return this->submitDirectCommandList(SyncQueue::kForce); |
| 1215 | } else { |
| 1216 | return this->submitDirectCommandList(SyncQueue::kSkip); |
| 1217 | } |
| 1218 | } |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1219 | |
| 1220 | std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) { |
| 1221 | return GrD3DSemaphore::Make(this); |
| 1222 | } |
| 1223 | std::unique_ptr<GrSemaphore> GrD3DGpu::wrapBackendSemaphore( |
| 1224 | const GrBackendSemaphore& semaphore, |
| 1225 | GrResourceProvider::SemaphoreWrapType, |
| 1226 | GrWrapOwnership) { |
| 1227 | SkASSERT(this->caps()->semaphoreSupport()); |
| 1228 | GrD3DFenceInfo fenceInfo; |
| 1229 | if (!semaphore.getD3DFenceInfo(&fenceInfo)) { |
| 1230 | return nullptr; |
| 1231 | } |
| 1232 | return GrD3DSemaphore::MakeWrapped(fenceInfo); |
| 1233 | } |
| 1234 | |
| 1235 | void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) { |
Greg Daniel | 0106fcc | 2020-07-01 17:40:12 -0400 | [diff] [blame] | 1236 | SkASSERT(semaphore); |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1237 | GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore); |
| 1238 | // TODO: Do we need to track the lifetime of this? How do we know it's done? |
| 1239 | fQueue->Signal(d3dSem->fence(), d3dSem->value()); |
| 1240 | } |
| 1241 | |
| 1242 | void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) { |
Greg Daniel | 0106fcc | 2020-07-01 17:40:12 -0400 | [diff] [blame] | 1243 | SkASSERT(semaphore); |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1244 | GrD3DSemaphore* d3dSem = static_cast<GrD3DSemaphore*>(semaphore); |
| 1245 | // TODO: Do we need to track the lifetime of this? |
| 1246 | fQueue->Wait(d3dSem->fence(), d3dSem->value()); |
| 1247 | } |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1248 | |
| 1249 | GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() { |
Jim Van Verth | e381036 | 2020-07-01 10:12:11 -0400 | [diff] [blame] | 1250 | GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue)); |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1251 | return fCurrentFenceValue; |
| 1252 | } |
| 1253 | |
| 1254 | bool GrD3DGpu::waitFence(GrFence fence) { |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 1255 | return (fFence->GetCompletedValue() >= fence); |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1256 | } |