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