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" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -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" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrTexture.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 | |
Jim Van Verth | 711b039 | 2020-07-23 16:45:00 -0400 | [diff] [blame] | 42 | // constants have to be aligned to 256 |
| 43 | constexpr int kConstantAlignment = 256; |
| 44 | |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame] | 45 | GrD3DGpu::GrD3DGpu(GrDirectContext* direct, const GrContextOptions& contextOptions, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 46 | const GrD3DBackendContext& backendContext) |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame] | 47 | : INHERITED(direct) |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 48 | , fDevice(backendContext.fDevice) |
Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 49 | , fQueue(backendContext.fQueue) |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 50 | , fResourceProvider(this) |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 51 | , fStagingBufferManager(this) |
Jim Van Verth | 711b039 | 2020-07-23 16:45:00 -0400 | [diff] [blame] | 52 | , fConstantsRingBuffer(this, 128 * 1024, kConstantAlignment, GrGpuBufferType::kVertex) |
Greg Daniel | 5fc5c81 | 2020-04-23 10:30:23 -0400 | [diff] [blame] | 53 | , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt) |
| 54 | , fCompiler(new SkSL::Compiler()) { |
Jim Van Verth | 8ec1330 | 2020-02-26 12:59:56 -0500 | [diff] [blame] | 55 | fCaps.reset(new GrD3DCaps(contextOptions, |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 56 | backendContext.fAdapter.get(), |
| 57 | backendContext.fDevice.get())); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 58 | |
| 59 | fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList(); |
Greg Daniel | e52c978 | 2020-03-23 14:18:37 -0400 | [diff] [blame] | 60 | SkASSERT(fCurrentDirectCommandList); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 61 | |
| 62 | SkASSERT(fCurrentFenceValue == 0); |
Jim Van Verth | e381036 | 2020-07-01 10:12:11 -0400 | [diff] [blame] | 63 | GR_D3D_CALL_ERRCHECK(fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE, |
| 64 | IID_PPV_ARGS(&fFence))); |
Jim Van Verth | 9b5e16c | 2020-04-20 10:45:52 -0400 | [diff] [blame] | 65 | |
| 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 74 | GrD3DGpu::~GrD3DGpu() { |
| 75 | this->destroyResources(); |
| 76 | } |
| 77 | |
| 78 | void GrD3DGpu::destroyResources() { |
| 79 | if (fCurrentDirectCommandList) { |
| 80 | fCurrentDirectCommandList->close(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 81 | fCurrentDirectCommandList->reset(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // We need to make sure everything has finished on the queue. |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 85 | this->waitForQueueCompletion(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 86 | |
| 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 Verth | f43da14 | 2020-06-09 16:34:43 -0400 | [diff] [blame] | 92 | OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 93 | SkASSERT(list->fFenceValue <= fenceValue); |
| 94 | // No reason to recycle the command lists since we are destroying all resources anyways. |
| 95 | list->~OutstandingCommandList(); |
Jim Van Verth | f43da14 | 2020-06-09 16:34:43 -0400 | [diff] [blame] | 96 | fOutstandingCommandLists.pop_front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 97 | } |
Jim Van Verth | f278886 | 2020-06-03 17:33:12 -0400 | [diff] [blame] | 98 | |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 99 | fStagingBufferManager.reset(); |
| 100 | |
Jim Van Verth | f278886 | 2020-06-03 17:33:12 -0400 | [diff] [blame] | 101 | fResourceProvider.destroyResources(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 102 | } |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 103 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 104 | GrOpsRenderPass* GrD3DGpu::getOpsRenderPass( |
Robert Phillips | 96f2237 | 2020-05-20 12:31:18 -0400 | [diff] [blame] | 105 | GrRenderTarget* rt, GrStencilAttachment*, |
| 106 | GrSurfaceOrigin origin, const SkIRect& bounds, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 107 | const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 108 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 109 | const SkTArray<GrSurfaceProxy*, true>& sampledProxies) { |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 110 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 120 | bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) { |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 121 | SkASSERT(fCurrentDirectCommandList); |
| 122 | |
Jim Van Verth | 711b039 | 2020-07-23 16:45:00 -0400 | [diff] [blame] | 123 | // set up constant data |
Jim Van Verth | 1aaf41b | 2020-07-29 09:24:29 -0400 | [diff] [blame^] | 124 | fConstantsRingBuffer.startSubmit(this); |
Jim Van Verth | 711b039 | 2020-07-23 16:45:00 -0400 | [diff] [blame] | 125 | |
Jim Van Verth | 3eadce2 | 2020-06-01 11:34:49 -0400 | [diff] [blame] | 126 | fResourceProvider.prepForSubmit(); |
| 127 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 128 | 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 Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 133 | this->waitForQueueCompletion(); |
Jim Van Verth | 682a2f4 | 2020-05-13 16:54:09 -0400 | [diff] [blame] | 134 | this->checkForFinishedCommandLists(); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 135 | } |
| 136 | return true; |
| 137 | } |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 138 | |
Greg Daniel | a581a8b | 2020-06-19 15:22:18 -0400 | [diff] [blame] | 139 | // 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 Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 143 | GrFence fence = this->insertFence(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 144 | new (fOutstandingCommandLists.push_back()) OutstandingCommandList( |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 145 | std::move(fCurrentDirectCommandList), fence); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 146 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 147 | if (sync == SyncQueue::kForce) { |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 148 | this->waitForQueueCompletion(); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 151 | 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 Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 159 | return true; |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void 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 Verth | dd3b401 | 2020-06-30 15:28:17 -0400 | [diff] [blame] | 170 | OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
| 171 | while (front && front->fFenceValue <= currentFenceValue) { |
| 172 | std::unique_ptr<GrD3DDirectCommandList> currList(std::move(front->fCommandList)); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 173 | // Since we used placement new we are responsible for calling the destructor manually. |
| 174 | front->~OutstandingCommandList(); |
| 175 | fOutstandingCommandLists.pop_front(); |
Jim Van Verth | dd3b401 | 2020-06-30 15:28:17 -0400 | [diff] [blame] | 176 | fResourceProvider.recycleDirectCommandList(std::move(currList)); |
| 177 | front = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 181 | void 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 192 | void GrD3DGpu::submit(GrOpsRenderPass* renderPass) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 193 | SkASSERT(fCachedOpsRenderPass.get() == renderPass); |
| 194 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 195 | // TODO: actually submit something here |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 196 | fCachedOpsRenderPass.reset(); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 199 | void GrD3DGpu::addFinishedProc(GrGpuFinishedProc finishedProc, |
| 200 | GrGpuFinishedContext finishedContext) { |
| 201 | SkASSERT(finishedProc); |
| 202 | sk_sp<GrRefCntedCallback> finishedCallback( |
| 203 | new GrRefCntedCallback(finishedProc, finishedContext)); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 204 | this->addFinishedCallback(std::move(finishedCallback)); |
| 205 | } |
| 206 | |
| 207 | void GrD3DGpu::addFinishedCallback(sk_sp<GrRefCntedCallback> finishedCallback) { |
| 208 | SkASSERT(finishedCallback); |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 209 | // 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 222 | void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) { |
| 223 | // TODO |
| 224 | } |
| 225 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 226 | sk_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 Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 233 | GrMipmapStatus mipmapStatus) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 234 | D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE; |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 235 | if (renderable == GrRenderable::kYes) { |
| 236 | usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 237 | } |
| 238 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 239 | // This desc refers to a texture that will be read by the client. Thus even if msaa is |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 240 | // requested, this describes the resolved texture. Therefore we always have samples set |
| 241 | // to 1. |
| 242 | SkASSERT(mipLevelCount > 0); |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 243 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 244 | 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 Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 254 | // quality levels are only supported for tiled resources so ignore for now |
| 255 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 256 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 257 | resourceDesc.Flags = usageFlags; |
| 258 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 259 | if (renderable == GrRenderable::kYes) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 260 | return GrD3DTextureRenderTarget::MakeNewTextureRenderTarget( |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 261 | this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 262 | mipmapStatus); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 263 | } else { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 264 | return GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 265 | mipmapStatus); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 266 | } |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 267 | } |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 268 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 269 | sk_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 Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 281 | GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kDirty |
| 282 | : GrMipmapStatus::kNotAllocated; |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 283 | |
| 284 | sk_sp<GrD3DTexture> tex = this->createD3DTexture(dimensions, dxgiFormat, renderable, |
| 285 | renderTargetSampleCnt, budgeted, isProtected, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 286 | mipLevelCount, mipmapStatus); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 287 | if (!tex) { |
| 288 | return nullptr; |
| 289 | } |
| 290 | |
| 291 | if (levelClearMask) { |
| 292 | // TODO |
| 293 | } |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 294 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 295 | return std::move(tex); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 298 | static 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 317 | sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions, |
| 318 | const GrBackendFormat& format, |
| 319 | SkBudgeted budgeted, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 320 | GrMipmapped mipMapped, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 321 | GrProtected isProtected, |
| 322 | const void* data, size_t dataSize) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 323 | 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 Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 329 | mipMapped == GrMipmapped::kYes)); |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 330 | |
| 331 | int mipLevelCount = 1; |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 332 | if (mipMapped == GrMipmapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 333 | mipLevelCount = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 334 | } |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 335 | GrMipmapStatus mipmapStatus = mipLevelCount > 1 ? GrMipmapStatus::kValid |
| 336 | : GrMipmapStatus::kNotAllocated; |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 337 | |
| 338 | sk_sp<GrD3DTexture> d3dTex = this->createD3DTexture(dimensions, dxgiFormat, GrRenderable::kNo, |
| 339 | 1, budgeted, isProtected, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 340 | mipLevelCount, mipmapStatus); |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 341 | 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 Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 363 | GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice( |
| 364 | combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); |
| 365 | if (!slice.fBuffer) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 366 | return false; |
| 367 | } |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 368 | |
| 369 | char* bufferData = (char*)slice.fOffsetMapPtr; |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 370 | |
| 371 | copy_compressed_data(bufferData, desc.Format, placedFootprints.get(), numRows.get(), |
| 372 | rowSizeInBytes.get(), data, mipLevelCount); |
| 373 | |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 374 | // 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 Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 379 | ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource(); |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 380 | fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, d3dTex.get(), mipLevelCount, |
| 381 | placedFootprints.get(), 0, 0); |
| 382 | |
| 383 | return std::move(d3dTex); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 386 | static int get_surface_sample_cnt(GrSurface* surf) { |
| 387 | if (const GrRenderTarget* rt = surf->asRenderTarget()) { |
| 388 | return rt->numSamples(); |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | bool 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 Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 398 | return false; |
| 399 | } |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 400 | |
| 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 | |
| 439 | void 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 Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 477 | fCurrentDirectCommandList->copyTextureRegionToTexture(dstResource->resource(), |
| 478 | &dstLocation, |
| 479 | dstPoint.fX, dstPoint.fY, |
| 480 | srcResource->resource(), |
| 481 | &srcLocation, |
| 482 | &srcBox); |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 483 | |
| 484 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 485 | srcRect.width(), srcRect.height()); |
| 486 | // The rect is already in device space so we pass in kTopLeft so no flip is done. |
| 487 | this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect); |
| 488 | } |
| 489 | |
| 490 | void GrD3DGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
| 491 | const SkIPoint& dstPoint) { |
Jim Van Verth | 275f419 | 2020-07-07 15:36:41 -0400 | [diff] [blame] | 492 | GrD3DRenderTarget* srcRT = static_cast<GrD3DRenderTarget*>(src->asRenderTarget()); |
| 493 | SkASSERT(srcRT); |
| 494 | |
| 495 | this->resolveTexture(dst, dstPoint.fX, dstPoint.fY, srcRT, srcRect); |
| 496 | } |
| 497 | |
| 498 | void 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 Verth | bb61fe3 | 2020-07-07 16:39:04 -0400 | [diff] [blame] | 521 | void GrD3DGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) { |
Jim Van Verth | 275f419 | 2020-07-07 15:36:41 -0400 | [diff] [blame] | 522 | 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 Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 529 | bool 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 Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 539 | 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 Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 548 | return false; |
| 549 | } |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 550 | |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 551 | D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 552 | srcLocation.pResource = texResource->d3dResource(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 553 | 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 Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 568 | 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 Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 572 | nullptr, nullptr, &transferTotalBytes); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 573 | SkASSERT(transferTotalBytes); |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 574 | size_t bpp = GrColorTypeBytesPerPixel(dstColorType); |
Jim Van Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 575 | if (this->d3dCaps().bytesPerPixel(texResource->dxgiFormat()) != bpp) { |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | size_t tightRowBytes = bpp * width; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 579 | |
| 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 Verth | c12aad9 | 2020-04-24 16:19:01 -0400 | [diff] [blame] | 588 | texResource->setResourceState(this, D3D12_RESOURCE_STATE_COPY_SOURCE); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 589 | |
Greg Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 590 | fCurrentDirectCommandList->copyTextureRegionToBuffer(transferBuffer, &dstLocation, 0, 0, |
| 591 | texResource->resource(), &srcLocation, |
| 592 | &srcBox); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 593 | this->submitDirectCommandList(SyncQueue::kForce); |
| 594 | |
| 595 | const void* mappedMemory = transferBuffer->map(); |
| 596 | |
| 597 | SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch, |
Jim Van Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 598 | tightRowBytes, height); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 599 | |
| 600 | transferBuffer->unmap(); |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | bool 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 Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 625 | SkASSERT(mipLevelCount <= d3dTex->maxMipmapLevel() + 1); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 626 | 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 | |
| 636 | bool 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 Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 645 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->maxMipmapLevel() + 1)); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 646 | |
| 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 Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 672 | 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 Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 678 | nullptr, nullptr, &combinedBufferSize); |
| 679 | size_t bpp = GrColorTypeBytesPerPixel(colorType); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 680 | SkASSERT(combinedBufferSize); |
| 681 | |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 682 | GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice( |
| 683 | combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); |
| 684 | if (!slice.fBuffer) { |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 685 | return false; |
| 686 | } |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 687 | |
| 688 | char* bufferData = (char*)slice.fOffsetMapPtr; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 689 | |
| 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 Verth | fdd3685 | 2020-04-21 16:29:44 -0400 | [diff] [blame] | 698 | const size_t trimRowBytes = currentWidth * bpp; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 699 | 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 Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 704 | const char* src = (const char*)texels[currentMipLevel].fPixels; |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 705 | 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 Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 713 | // 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 Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 717 | |
Greg Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 718 | ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 719 | fCurrentDirectCommandList->copyBufferToTexture(d3dBuffer, tex, mipLevelCount, |
| 720 | placedFootprints.get(), left, top); |
| 721 | |
| 722 | if (mipLevelCount < (int)desc.MipLevels) { |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 723 | tex->markMipmapsDirty(); |
Jim Van Verth | ba7f229 | 2020-04-21 08:56:47 -0400 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return true; |
| 727 | } |
| 728 | |
Jim Van Verth | 9145f78 | 2020-04-28 12:01:12 -0400 | [diff] [blame] | 729 | static bool check_resource_info(const GrD3DTextureResourceInfo& info) { |
| 730 | if (!info.fResource.get()) { |
| 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 736 | static 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 | |
| 743 | static 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 Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 751 | sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex, |
| 752 | GrWrapOwnership, |
| 753 | GrWrapCacheable wrapType, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 754 | GrIOType ioType) { |
| 755 | GrD3DTextureResourceInfo textureInfo; |
| 756 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 757 | return nullptr; |
| 758 | } |
| 759 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 760 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 761 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex, |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 780 | GrWrapOwnership ownership, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 781 | GrWrapCacheable wrapType) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 782 | return this->onWrapBackendTexture(tex, ownership, wrapType, kRead_GrIOType); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex, |
| 786 | int sampleCnt, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 787 | GrWrapOwnership ownership, |
| 788 | GrWrapCacheable cacheable) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 789 | GrD3DTextureResourceInfo textureInfo; |
| 790 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 791 | return nullptr; |
| 792 | } |
| 793 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 794 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 795 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 818 | } |
| 819 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 820 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 821 | // 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 Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 834 | if (!check_resource_info(info)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 835 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 862 | int sampleCnt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 863 | |
| 864 | GrD3DTextureResourceInfo textureInfo; |
| 865 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 866 | return nullptr; |
| 867 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 868 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 869 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type, |
Jim Van Verth | d6ad480 | 2020-04-03 14:59:20 -0400 | [diff] [blame] | 894 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget( |
| 904 | const GrRenderTarget* rt, int width, int height, int numStencilSamples) { |
Jim Van Verth | 4f51f47 | 2020-04-13 11:02:21 -0400 | [diff] [blame] | 905 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 918 | } |
| 919 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 920 | bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat, |
| 921 | SkISize dimensions, |
| 922 | GrTexturable texturable, |
| 923 | GrRenderable renderable, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 924 | GrMipmapped mipMapped, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 925 | GrD3DTextureResourceInfo* info, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 926 | GrProtected isProtected) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 927 | SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes); |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 928 | |
| 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 Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 942 | if (mipMapped == GrMipmapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 943 | numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 944 | } |
| 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 Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 952 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 953 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 954 | resourceDesc.Alignment = 0; // use default alignment |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 955 | 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 Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 961 | // quality levels are only supported for tiled resources so ignore for now |
| 962 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 963 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 964 | resourceDesc.Flags = usageFlags; |
| 965 | |
Jim Van Verth | 280d4f7 | 2020-05-04 10:04:24 -0400 | [diff] [blame] | 966 | 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 Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 978 | D3D12_RESOURCE_STATES initialState = (renderable == GrRenderable::kYes) |
| 979 | ? D3D12_RESOURCE_STATE_RENDER_TARGET |
| 980 | : D3D12_RESOURCE_STATE_COPY_DEST; |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 981 | if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState, |
Jim Van Verth | 280d4f7 | 2020-05-04 10:04:24 -0400 | [diff] [blame] | 982 | isProtected, clearValuePtr, info)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 983 | SkDebugf("Failed to init texture resource info\n"); |
| 984 | return false; |
| 985 | } |
| 986 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 987 | return true; |
| 988 | } |
| 989 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 990 | GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 991 | const GrBackendFormat& format, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 992 | GrRenderable renderable, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 993 | GrMipmapped mipMapped, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 994 | GrProtected isProtected) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 995 | 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 Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1016 | &info, isProtected)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1017 | return {}; |
| 1018 | } |
| 1019 | |
| 1020 | return GrBackendTexture(dimensions.width(), dimensions.height(), info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1021 | } |
| 1022 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1023 | static 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 Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1026 | 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 Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1041 | } |
| 1042 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1043 | static 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 Daniel | 746460e | 2020-06-30 13:13:53 -0400 | [diff] [blame] | 1047 | auto colorType = caps.getFormatColorType(dxgiFormat); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1048 | 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 Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1059 | bool GrD3DGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture, |
| 1060 | sk_sp<GrRefCntedCallback> finishedCallback, |
| 1061 | const BackendTextureData* data) { |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1062 | 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 Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 1086 | if (backendTexture.fMipmapped == GrMipmapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 1087 | mipLevelCount = SkMipmap::ComputeLevelCount(backendTexture.dimensions().width(), |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1088 | backendTexture.dimensions().height()) + 1; |
| 1089 | } |
| 1090 | SkASSERT(mipLevelCount == info.fLevelCount); |
| 1091 | SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount); |
| 1092 | UINT64 combinedBufferSize; |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1093 | SkAutoTMalloc<UINT> numRows(mipLevelCount); |
| 1094 | SkAutoTMalloc<UINT64> rowSizeInBytes(mipLevelCount); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1095 | fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(), |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1096 | numRows.get(), rowSizeInBytes.get(), &combinedBufferSize); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1097 | 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 Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 1109 | GrStagingBufferManager::Slice slice = fStagingBufferManager.allocateStagingBufferSlice( |
| 1110 | combinedBufferSize, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); |
| 1111 | if (!slice.fBuffer) { |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 1114 | |
| 1115 | char* bufferData = (char*)slice.fOffsetMapPtr; |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1116 | SkASSERT(bufferData); |
| 1117 | |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1118 | if (data->type() == BackendTextureData::Type::kPixmaps) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1119 | copy_src_data(this, bufferData, info.fFormat, placedFootprints.get(), data->pixmaps(), |
| 1120 | info.fLevelCount); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1121 | } else if (data->type() == BackendTextureData::Type::kCompressed) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1122 | copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(), |
| 1123 | rowSizeInBytes.get(), data->compressedData(), info.fLevelCount); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1124 | } else { |
| 1125 | SkASSERT(data->type() == BackendTextureData::Type::kColor); |
| 1126 | SkImage::CompressionType compression = |
| 1127 | GrBackendFormatToCompressionType(backendTexture.getBackendFormat()); |
| 1128 | if (SkImage::CompressionType::kNone == compression) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1129 | if (!copy_color_data(this->d3dCaps(), bufferData, info.fFormat, |
| 1130 | backendTexture.dimensions(), placedFootprints, data->color())) { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1133 | } else { |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1134 | size_t totalCompressedSize = SkCompressedFormatDataSize(compression, |
| 1135 | backendTexture.dimensions(), |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 1136 | backendTexture.hasMipmaps()); |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1137 | SkAutoTMalloc<char> tempData(totalCompressedSize); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1138 | GrFillInCompressedData(compression, backendTexture.dimensions(), |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 1139 | backendTexture.fMipmapped, tempData, data->color()); |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1140 | copy_compressed_data(bufferData, info.fFormat, placedFootprints.get(), numRows.get(), |
| 1141 | rowSizeInBytes.get(), tempData.get(), info.fLevelCount); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1142 | } |
| 1143 | } |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1144 | |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 1145 | // 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 Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 1150 | ID3D12Resource* d3dBuffer = static_cast<GrD3DBuffer*>(slice.fBuffer)->d3dResource(); |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 1151 | cmdList->copyBufferToTexture(d3dBuffer, texture.get(), mipLevelCount, placedFootprints.get(), 0, |
| 1152 | 0); |
Jim Van Verth | 43a6e17 | 2020-06-23 11:59:08 -0400 | [diff] [blame] | 1153 | |
| 1154 | if (finishedCallback) { |
| 1155 | this->addFinishedCallback(std::move(finishedCallback)); |
| 1156 | } |
| 1157 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1158 | return true; |
| 1159 | } |
| 1160 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 1161 | GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 1162 | SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped, |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1163 | GrProtected isProtected) { |
| 1164 | return this->onCreateBackendTexture(dimensions, format, GrRenderable::kNo, mipMapped, |
| 1165 | isProtected); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1166 | } |
| 1167 | |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1168 | bool GrD3DGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture, |
Greg Daniel | aaf738c | 2020-07-10 09:30:33 -0400 | [diff] [blame] | 1169 | sk_sp<GrRefCntedCallback> finishedCallback, |
Jim Van Verth | bb80fcd | 2020-07-14 10:04:40 -0400 | [diff] [blame] | 1170 | const BackendTextureData* data) { |
| 1171 | return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data); |
Greg Daniel | aaf738c | 2020-07-10 09:30:33 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1174 | void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1175 | SkASSERT(GrBackendApi::kDirect3D == tex.fBackend); |
| 1176 | // 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] | 1177 | } |
| 1178 | |
Robert Phillips | 979b223 | 2020-02-20 10:47:29 -0500 | [diff] [blame] | 1179 | bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) { |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1183 | #if GR_TEST_UTILS |
| 1184 | bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1185 | 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 Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 1199 | GrColorType colorType) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1200 | this->handleDirtyContext(); |
| 1201 | |
| 1202 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 1203 | return {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType); |
| 1207 | |
| 1208 | GrD3DTextureResourceInfo info; |
| 1209 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 1210 | GrRenderable::kYes, GrMipmapped::kNo, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame] | 1211 | &info, GrProtected::kNo)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1212 | return {}; |
| 1213 | } |
| 1214 | |
| 1215 | return GrBackendRenderTarget(w, h, 1, info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1216 | } |
| 1217 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1218 | void 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 | |
| 1229 | void GrD3DGpu::testingOnly_flushGpuAndSync() { |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1230 | SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce)); |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 1231 | } |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1232 | |
Jim Van Verth | 9b5e16c | 2020-04-20 10:45:52 -0400 | [diff] [blame] | 1233 | void GrD3DGpu::testingOnly_startCapture() { |
| 1234 | if (fGraphicsAnalysis) { |
| 1235 | fGraphicsAnalysis->BeginCapture(); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | void GrD3DGpu::testingOnly_endCapture() { |
| 1240 | if (fGraphicsAnalysis) { |
| 1241 | fGraphicsAnalysis->EndCapture(); |
| 1242 | } |
| 1243 | } |
| 1244 | #endif |
| 1245 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1246 | /////////////////////////////////////////////////////////////////////////////// |
| 1247 | |
Greg Daniel | a5a6b32 | 2020-04-23 12:52:27 -0400 | [diff] [blame] | 1248 | void GrD3DGpu::addResourceBarriers(sk_sp<GrManagedResource> resource, |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1249 | int numBarriers, |
| 1250 | D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const { |
| 1251 | SkASSERT(fCurrentDirectCommandList); |
| 1252 | SkASSERT(resource); |
| 1253 | |
Greg Daniel | a5a6b32 | 2020-04-23 12:52:27 -0400 | [diff] [blame] | 1254 | fCurrentDirectCommandList->resourceBarrier(std::move(resource), numBarriers, barriers); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
Greg Daniel | 6926791 | 2020-07-24 10:42:53 -0400 | [diff] [blame] | 1257 | void 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 Daniel | 9efe386 | 2020-06-11 11:51:06 -0400 | [diff] [blame] | 1268 | void GrD3DGpu::prepareSurfacesForBackendAccessAndStateUpdates( |
| 1269 | GrSurfaceProxy* proxies[], |
| 1270 | int numProxies, |
| 1271 | SkSurface::BackendSurfaceAccess access, |
| 1272 | const GrBackendSurfaceMutableState* newState) { |
Jim Van Verth | 682a2f4 | 2020-05-13 16:54:09 -0400 | [diff] [blame] | 1273 | 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 Verth | 1aaf41b | 2020-07-29 09:24:29 -0400 | [diff] [blame^] | 1293 | void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) { |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 1294 | fCurrentDirectCommandList->addGrBuffer(std::move(buffer)); |
Greg Daniel | cffb062 | 2020-07-16 13:19:17 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame] | 1297 | bool 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 Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1304 | |
| 1305 | std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrD3DGpu::makeSemaphore(bool) { |
| 1306 | return GrD3DSemaphore::Make(this); |
| 1307 | } |
| 1308 | std::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 | |
| 1320 | void GrD3DGpu::insertSemaphore(GrSemaphore* semaphore) { |
Greg Daniel | 0106fcc | 2020-07-01 17:40:12 -0400 | [diff] [blame] | 1321 | SkASSERT(semaphore); |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1322 | 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 | |
| 1327 | void GrD3DGpu::waitSemaphore(GrSemaphore* semaphore) { |
Greg Daniel | 0106fcc | 2020-07-01 17:40:12 -0400 | [diff] [blame] | 1328 | SkASSERT(semaphore); |
Jim Van Verth | c1a67b5 | 2020-06-25 13:10:29 -0400 | [diff] [blame] | 1329 | 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 Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1333 | |
| 1334 | GrFence SK_WARN_UNUSED_RESULT GrD3DGpu::insertFence() { |
Jim Van Verth | e381036 | 2020-07-01 10:12:11 -0400 | [diff] [blame] | 1335 | GR_D3D_CALL_ERRCHECK(fQueue->Signal(fFence.get(), ++fCurrentFenceValue)); |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1336 | return fCurrentFenceValue; |
| 1337 | } |
| 1338 | |
| 1339 | bool GrD3DGpu::waitFence(GrFence fence) { |
Jim Van Verth | 3b0d7d1 | 2020-07-06 11:52:42 -0400 | [diff] [blame] | 1340 | return (fFence->GetCompletedValue() >= fence); |
Jim Van Verth | 1e6460d | 2020-06-30 15:47:52 -0400 | [diff] [blame] | 1341 | } |