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