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