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