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 | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 14 | #include "src/gpu/GrDataUtils.h" |
| 15 | #include "src/gpu/GrTexturePriv.h" |
Jim Van Verth | d6ad480 | 2020-04-03 14:59:20 -0400 | [diff] [blame] | 16 | #include "src/gpu/d3d/GrD3DBuffer.h" |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 17 | #include "src/gpu/d3d/GrD3DCaps.h" |
| 18 | #include "src/gpu/d3d/GrD3DOpsRenderPass.h" |
Jim Van Verth | 4f51f47 | 2020-04-13 11:02:21 -0400 | [diff] [blame] | 19 | #include "src/gpu/d3d/GrD3DStencilAttachment.h" |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 20 | #include "src/gpu/d3d/GrD3DTexture.h" |
| 21 | #include "src/gpu/d3d/GrD3DTextureRenderTarget.h" |
| 22 | #include "src/gpu/d3d/GrD3DUtil.h" |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 23 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 24 | sk_sp<GrGpu> GrD3DGpu::Make(const GrD3DBackendContext& backendContext, |
| 25 | const GrContextOptions& contextOptions, GrContext* context) { |
| 26 | return sk_sp<GrGpu>(new GrD3DGpu(context, contextOptions, backendContext)); |
| 27 | } |
| 28 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 29 | // This constant determines how many OutstandingCommandLists are allocated together as a block in |
| 30 | // the deque. As such it needs to balance allocating too much memory vs. incurring |
| 31 | // allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding |
| 32 | // command lists we expect to see. |
| 33 | static const int kDefaultOutstandingAllocCnt = 8; |
| 34 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 35 | GrD3DGpu::GrD3DGpu(GrContext* context, const GrContextOptions& contextOptions, |
| 36 | const GrD3DBackendContext& backendContext) |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 37 | : INHERITED(context) |
| 38 | , fDevice(backendContext.fDevice) |
Greg Daniel | 02c4590 | 2020-03-09 10:58:09 -0400 | [diff] [blame] | 39 | |
| 40 | , fQueue(backendContext.fQueue) |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 41 | , fResourceProvider(this) |
| 42 | , fOutstandingCommandLists(sizeof(OutstandingCommandList), kDefaultOutstandingAllocCnt) { |
Jim Van Verth | 8ec1330 | 2020-02-26 12:59:56 -0500 | [diff] [blame] | 43 | fCaps.reset(new GrD3DCaps(contextOptions, |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 44 | backendContext.fAdapter.get(), |
| 45 | backendContext.fDevice.get())); |
Greg Daniel | 85da336 | 2020-03-09 15:18:35 -0400 | [diff] [blame] | 46 | |
| 47 | fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList(); |
Greg Daniel | e52c978 | 2020-03-23 14:18:37 -0400 | [diff] [blame] | 48 | SkASSERT(fCurrentDirectCommandList); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 49 | |
| 50 | SkASSERT(fCurrentFenceValue == 0); |
| 51 | SkDEBUGCODE(HRESULT hr = ) fDevice->CreateFence(fCurrentFenceValue, D3D12_FENCE_FLAG_NONE, |
| 52 | IID_PPV_ARGS(&fFence)); |
| 53 | SkASSERT(SUCCEEDED(hr)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 56 | GrD3DGpu::~GrD3DGpu() { |
| 57 | this->destroyResources(); |
| 58 | } |
| 59 | |
| 60 | void GrD3DGpu::destroyResources() { |
| 61 | if (fCurrentDirectCommandList) { |
| 62 | fCurrentDirectCommandList->close(); |
| 63 | fCurrentDirectCommandList.reset(); |
| 64 | } |
| 65 | |
| 66 | // We need to make sure everything has finished on the queue. |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 67 | this->waitForQueueCompletion(); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 68 | |
| 69 | SkDEBUGCODE(uint64_t fenceValue = fFence->GetCompletedValue();) |
| 70 | |
| 71 | // We used a placement new for each object in fOutstandingCommandLists, so we're responsible |
| 72 | // for calling the destructor on each of them as well. |
| 73 | while (!fOutstandingCommandLists.empty()) { |
| 74 | OutstandingCommandList* list = (OutstandingCommandList*)fOutstandingCommandLists.back(); |
| 75 | SkASSERT(list->fFenceValue <= fenceValue); |
| 76 | // No reason to recycle the command lists since we are destroying all resources anyways. |
| 77 | list->~OutstandingCommandList(); |
| 78 | fOutstandingCommandLists.pop_back(); |
| 79 | } |
| 80 | } |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 81 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 82 | GrOpsRenderPass* GrD3DGpu::getOpsRenderPass( |
| 83 | GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds, |
| 84 | const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 85 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 86 | const SkTArray<GrSurfaceProxy*, true>& sampledProxies) { |
Greg Daniel | 31a7b07 | 2020-02-26 15:31:49 -0500 | [diff] [blame] | 87 | if (!fCachedOpsRenderPass) { |
| 88 | fCachedOpsRenderPass.reset(new GrD3DOpsRenderPass(this)); |
| 89 | } |
| 90 | |
| 91 | if (!fCachedOpsRenderPass->set(rt, origin, bounds, colorInfo, stencilInfo, sampledProxies)) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | return fCachedOpsRenderPass.get(); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 97 | bool GrD3DGpu::submitDirectCommandList(SyncQueue sync) { |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 98 | SkASSERT(fCurrentDirectCommandList); |
| 99 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 100 | GrD3DDirectCommandList::SubmitResult result = fCurrentDirectCommandList->submit(fQueue.get()); |
| 101 | if (result == GrD3DDirectCommandList::SubmitResult::kFailure) { |
| 102 | return false; |
| 103 | } else if (result == GrD3DDirectCommandList::SubmitResult::kNoWork) { |
| 104 | if (sync == SyncQueue::kForce) { |
| 105 | this->waitForQueueCompletion(); |
| 106 | } |
| 107 | return true; |
| 108 | } |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 109 | |
| 110 | new (fOutstandingCommandLists.push_back()) OutstandingCommandList( |
| 111 | std::move(fCurrentDirectCommandList), ++fCurrentFenceValue); |
| 112 | |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 113 | SkDEBUGCODE(HRESULT hr = ) fQueue->Signal(fFence.get(), fCurrentFenceValue); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 114 | SkASSERT(SUCCEEDED(hr)); |
| 115 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 116 | if (sync == SyncQueue::kForce) { |
| 117 | this->waitForQueueCompletion(); |
| 118 | } |
| 119 | |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 120 | fCurrentDirectCommandList = fResourceProvider.findOrCreateDirectCommandList(); |
| 121 | |
| 122 | // This should be done after we have a new command list in case the freeing of any resources |
| 123 | // held by a finished command list causes us send a new command to the gpu (like changing the |
| 124 | // resource state. |
| 125 | this->checkForFinishedCommandLists(); |
| 126 | |
| 127 | SkASSERT(fCurrentDirectCommandList); |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 128 | return true; |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void GrD3DGpu::checkForFinishedCommandLists() { |
| 132 | uint64_t currentFenceValue = fFence->GetCompletedValue(); |
| 133 | |
| 134 | // Iterate over all the outstanding command lists to see if any have finished. The commands |
| 135 | // lists are in order from oldest to newest, so we start at the front to check if their fence |
| 136 | // value is less than the last signaled value. If so we pop it off and move onto the next. |
| 137 | // Repeat till we find a command list that has not finished yet (and all others afterwards are |
| 138 | // also guaranteed to not have finished). |
| 139 | SkDeque::F2BIter iter(fOutstandingCommandLists); |
| 140 | const OutstandingCommandList* curList = (const OutstandingCommandList*)iter.next(); |
| 141 | while (curList && curList->fFenceValue <= currentFenceValue) { |
| 142 | curList = (const OutstandingCommandList*)iter.next(); |
| 143 | OutstandingCommandList* front = (OutstandingCommandList*)fOutstandingCommandLists.front(); |
Greg Daniel | 7a5f1fa | 2020-03-24 14:50:19 -0400 | [diff] [blame] | 144 | fResourceProvider.recycleDirectCommandList(std::move(front->fCommandList)); |
Greg Daniel | 83ed213 | 2020-03-24 13:15:33 -0400 | [diff] [blame] | 145 | // Since we used placement new we are responsible for calling the destructor manually. |
| 146 | front->~OutstandingCommandList(); |
| 147 | fOutstandingCommandLists.pop_front(); |
| 148 | } |
| 149 | } |
| 150 | |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 151 | void GrD3DGpu::waitForQueueCompletion() { |
| 152 | if (fFence->GetCompletedValue() < fCurrentFenceValue) { |
| 153 | HANDLE fenceEvent; |
| 154 | fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 155 | SkASSERT(fenceEvent); |
| 156 | SkDEBUGCODE(HRESULT hr = ) fFence->SetEventOnCompletion(fCurrentFenceValue, fenceEvent); |
| 157 | SkASSERT(SUCCEEDED(hr)); |
| 158 | WaitForSingleObject(fenceEvent, INFINITE); |
| 159 | CloseHandle(fenceEvent); |
| 160 | } |
| 161 | } |
| 162 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 163 | void GrD3DGpu::submit(GrOpsRenderPass* renderPass) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 164 | SkASSERT(fCachedOpsRenderPass.get() == renderPass); |
| 165 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 166 | // TODO: actually submit something here |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 167 | fCachedOpsRenderPass.reset(); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void GrD3DGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) { |
| 171 | // TODO |
| 172 | } |
| 173 | |
| 174 | sk_sp<GrTexture> GrD3DGpu::onCreateTexture(SkISize dimensions, |
| 175 | const GrBackendFormat& format, |
| 176 | GrRenderable renderable, |
| 177 | int renderTargetSampleCnt, |
| 178 | SkBudgeted budgeted, |
| 179 | GrProtected isProtected, |
| 180 | int mipLevelCount, |
| 181 | uint32_t levelClearMask) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 182 | DXGI_FORMAT dxgiFormat; |
| 183 | SkAssertResult(format.asDxgiFormat(&dxgiFormat)); |
| 184 | SkASSERT(!GrDxgiFormatIsCompressed(dxgiFormat)); |
| 185 | |
| 186 | D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE; |
| 187 | |
| 188 | if (renderable == GrRenderable::kYes) { |
| 189 | usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 190 | } |
| 191 | |
| 192 | // This desc refers to the texture that will be read by the client. Thus even if msaa is |
| 193 | // requested, this describes the resolved texture. Therefore we always have samples set |
| 194 | // to 1. |
| 195 | SkASSERT(mipLevelCount > 0); |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 196 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 197 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 198 | // TODO: will use 4MB alignment for MSAA textures and 64KB for everything else |
| 199 | // might want to manually set alignment to 4KB for smaller textures |
| 200 | resourceDesc.Alignment = 0; |
| 201 | resourceDesc.Width = dimensions.fWidth; |
| 202 | resourceDesc.Height = dimensions.fHeight; |
| 203 | resourceDesc.DepthOrArraySize = 1; |
| 204 | resourceDesc.MipLevels = mipLevelCount; |
| 205 | resourceDesc.Format = dxgiFormat; |
| 206 | resourceDesc.SampleDesc.Count = 1; |
Greg Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 207 | // quality levels are only supported for tiled resources so ignore for now |
| 208 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 209 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 210 | resourceDesc.Flags = usageFlags; |
| 211 | |
| 212 | GrMipMapsStatus mipMapsStatus = |
| 213 | mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated; |
| 214 | |
| 215 | sk_sp<GrD3DTexture> tex; |
| 216 | if (renderable == GrRenderable::kYes) { |
| 217 | tex = GrD3DTextureRenderTarget::MakeNewTextureRenderTarget( |
| 218 | this, budgeted, dimensions, renderTargetSampleCnt, resourceDesc, isProtected, |
| 219 | mipMapsStatus); |
| 220 | } else { |
| 221 | tex = GrD3DTexture::MakeNewTexture(this, budgeted, dimensions, resourceDesc, isProtected, |
| 222 | mipMapsStatus); |
| 223 | } |
| 224 | |
| 225 | if (!tex) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | if (levelClearMask) { |
| 230 | // TODO |
| 231 | } |
| 232 | return std::move(tex); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | sk_sp<GrTexture> GrD3DGpu::onCreateCompressedTexture(SkISize dimensions, |
| 236 | const GrBackendFormat& format, |
| 237 | SkBudgeted budgeted, |
| 238 | GrMipMapped mipMapped, |
| 239 | GrProtected isProtected, |
| 240 | const void* data, size_t dataSize) { |
| 241 | // TODO |
| 242 | return nullptr; |
| 243 | } |
| 244 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 245 | static bool check_resource_info(const GrD3DTextureResourceInfo& info) { |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 246 | if (!info.fResource.get()) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 247 | return false; |
| 248 | } |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | static bool check_tex_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info) { |
| 253 | if (!caps.isFormatTexturable(info.fFormat)) { |
| 254 | return false; |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | static bool check_rt_resource_info(const GrD3DCaps& caps, const GrD3DTextureResourceInfo& info, |
| 260 | int sampleCnt) { |
| 261 | if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) { |
| 262 | return false; |
| 263 | } |
| 264 | return true; |
| 265 | } |
| 266 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 267 | sk_sp<GrTexture> GrD3DGpu::onWrapBackendTexture(const GrBackendTexture& tex, |
| 268 | GrWrapOwnership, |
| 269 | GrWrapCacheable wrapType, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 270 | GrIOType ioType) { |
| 271 | GrD3DTextureResourceInfo textureInfo; |
| 272 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 273 | return nullptr; |
| 274 | } |
| 275 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 276 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 277 | return nullptr; |
| 278 | } |
| 279 | |
| 280 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
| 284 | // TODO: support protected context |
| 285 | if (tex.isProtected()) { |
| 286 | return nullptr; |
| 287 | } |
| 288 | |
| 289 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 290 | SkASSERT(state); |
| 291 | return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, ioType, textureInfo, |
| 292 | std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | sk_sp<GrTexture> GrD3DGpu::onWrapCompressedBackendTexture(const GrBackendTexture& tex, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 296 | GrWrapOwnership, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 297 | GrWrapCacheable wrapType) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 298 | GrD3DTextureResourceInfo textureInfo; |
| 299 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 303 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 304 | return nullptr; |
| 305 | } |
| 306 | |
| 307 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | // TODO: support protected context |
| 312 | if (tex.isProtected()) { |
| 313 | return nullptr; |
| 314 | } |
| 315 | |
| 316 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 317 | SkASSERT(state); |
| 318 | return GrD3DTexture::MakeWrappedTexture(this, tex.dimensions(), wrapType, kRead_GrIOType, |
| 319 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | sk_sp<GrTexture> GrD3DGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex, |
| 323 | int sampleCnt, |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 324 | GrWrapOwnership ownership, |
| 325 | GrWrapCacheable cacheable) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 326 | GrD3DTextureResourceInfo textureInfo; |
| 327 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 331 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | if (!check_tex_resource_info(this->d3dCaps(), textureInfo)) { |
| 336 | return nullptr; |
| 337 | } |
| 338 | if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) { |
| 339 | return nullptr; |
| 340 | } |
| 341 | |
| 342 | // TODO: support protected context |
| 343 | if (tex.isProtected()) { |
| 344 | return nullptr; |
| 345 | } |
| 346 | |
| 347 | sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat); |
| 348 | |
| 349 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 350 | SkASSERT(state); |
| 351 | |
| 352 | return GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(this, tex.dimensions(), |
| 353 | sampleCnt, cacheable, |
| 354 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 357 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 358 | // Currently the Direct3D backend does not support wrapping of msaa render targets directly. In |
| 359 | // general this is not an issue since swapchain images in D3D are never multisampled. Thus if |
| 360 | // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle |
| 361 | // creating and owning the MSAA images. |
| 362 | if (rt.sampleCnt() > 1) { |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
| 366 | GrD3DTextureResourceInfo info; |
| 367 | if (!rt.getD3DTextureResourceInfo(&info)) { |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 371 | if (!check_resource_info(info)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 372 | return nullptr; |
| 373 | } |
| 374 | |
| 375 | if (!check_rt_resource_info(this->d3dCaps(), info, rt.sampleCnt())) { |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | // TODO: support protected context |
| 380 | if (rt.isProtected()) { |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
| 384 | sk_sp<GrD3DResourceState> state = rt.getGrD3DResourceState(); |
| 385 | |
| 386 | sk_sp<GrD3DRenderTarget> tgt = GrD3DRenderTarget::MakeWrappedRenderTarget( |
| 387 | this, rt.dimensions(), 1, info, std::move(state)); |
| 388 | |
| 389 | // We don't allow the client to supply a premade stencil buffer. We always create one if needed. |
| 390 | SkASSERT(!rt.stencilBits()); |
| 391 | if (tgt) { |
| 392 | SkASSERT(tgt->canAttemptStencilAttachment()); |
| 393 | } |
| 394 | |
| 395 | return std::move(tgt); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | sk_sp<GrRenderTarget> GrD3DGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 399 | int sampleCnt) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 400 | |
| 401 | GrD3DTextureResourceInfo textureInfo; |
| 402 | if (!tex.getD3DTextureResourceInfo(&textureInfo)) { |
| 403 | return nullptr; |
| 404 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 405 | if (!check_resource_info(textureInfo)) { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 406 | return nullptr; |
| 407 | } |
| 408 | |
| 409 | if (!check_rt_resource_info(this->d3dCaps(), textureInfo, sampleCnt)) { |
| 410 | return nullptr; |
| 411 | } |
| 412 | |
| 413 | // TODO: support protected context |
| 414 | if (tex.isProtected()) { |
| 415 | return nullptr; |
| 416 | } |
| 417 | |
| 418 | sampleCnt = this->d3dCaps().getRenderTargetSampleCount(sampleCnt, textureInfo.fFormat); |
| 419 | if (!sampleCnt) { |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | sk_sp<GrD3DResourceState> state = tex.getGrD3DResourceState(); |
| 424 | SkASSERT(state); |
| 425 | |
| 426 | return GrD3DRenderTarget::MakeWrappedRenderTarget(this, tex.dimensions(), sampleCnt, |
| 427 | textureInfo, std::move(state)); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | sk_sp<GrGpuBuffer> GrD3DGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type, |
Jim Van Verth | d6ad480 | 2020-04-03 14:59:20 -0400 | [diff] [blame] | 431 | GrAccessPattern accessPattern, const void* data) { |
| 432 | sk_sp<GrD3DBuffer> buffer = GrD3DBuffer::Make(this, sizeInBytes, type, accessPattern); |
| 433 | if (data && buffer) { |
| 434 | buffer->updateData(data, sizeInBytes); |
| 435 | } |
| 436 | |
| 437 | return std::move(buffer); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | GrStencilAttachment* GrD3DGpu::createStencilAttachmentForRenderTarget( |
| 441 | const GrRenderTarget* rt, int width, int height, int numStencilSamples) { |
Jim Van Verth | 4f51f47 | 2020-04-13 11:02:21 -0400 | [diff] [blame] | 442 | SkASSERT(numStencilSamples == rt->numSamples() || this->caps()->mixedSamplesSupport()); |
| 443 | SkASSERT(width >= rt->width()); |
| 444 | SkASSERT(height >= rt->height()); |
| 445 | |
| 446 | const GrD3DCaps::StencilFormat& sFmt = this->d3dCaps().preferredStencilFormat(); |
| 447 | |
| 448 | GrD3DStencilAttachment* stencil(GrD3DStencilAttachment::Make(this, |
| 449 | width, |
| 450 | height, |
| 451 | numStencilSamples, |
| 452 | sFmt)); |
| 453 | fStats.incStencilAttachmentCreates(); |
| 454 | return stencil; |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 457 | bool GrD3DGpu::createTextureResourceForBackendSurface(DXGI_FORMAT dxgiFormat, |
| 458 | SkISize dimensions, |
| 459 | GrTexturable texturable, |
| 460 | GrRenderable renderable, |
| 461 | GrMipMapped mipMapped, |
| 462 | GrD3DTextureResourceInfo* info, |
| 463 | GrProtected isProtected, |
| 464 | const BackendTextureData* data) { |
| 465 | SkASSERT(texturable == GrTexturable::kYes || renderable == GrRenderable::kYes); |
| 466 | if (texturable == GrTexturable::kNo) { |
| 467 | SkASSERT(!data && mipMapped == GrMipMapped::kNo); |
| 468 | } |
| 469 | |
| 470 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | if (texturable == GrTexturable::kYes && !this->d3dCaps().isFormatTexturable(dxgiFormat)) { |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | if (renderable == GrRenderable::kYes && !this->d3dCaps().isFormatRenderable(dxgiFormat, 1)) { |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | int numMipLevels = 1; |
| 483 | if (mipMapped == GrMipMapped::kYes) { |
| 484 | numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
| 485 | } |
| 486 | |
| 487 | // create the texture |
| 488 | D3D12_RESOURCE_FLAGS usageFlags = D3D12_RESOURCE_FLAG_NONE; |
| 489 | if (renderable == GrRenderable::kYes) { |
| 490 | usageFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
| 491 | } |
| 492 | |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 493 | D3D12_RESOURCE_DESC resourceDesc = {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 494 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 495 | resourceDesc.Alignment = 0; // use default alignment |
| 496 | resourceDesc.Width = dimensions.fWidth; |
| 497 | resourceDesc.Height = dimensions.fHeight; |
| 498 | resourceDesc.DepthOrArraySize = 1; |
| 499 | resourceDesc.MipLevels = numMipLevels; |
| 500 | resourceDesc.Format = dxgiFormat; |
| 501 | resourceDesc.SampleDesc.Count = 1; |
Greg Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 502 | // quality levels are only supported for tiled resources so ignore for now |
| 503 | resourceDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 504 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // use driver-selected swizzle |
| 505 | resourceDesc.Flags = usageFlags; |
| 506 | |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 507 | D3D12_RESOURCE_STATES initialState = |
| 508 | (renderable == GrRenderable::kYes) && !data ? D3D12_RESOURCE_STATE_RENDER_TARGET : |
| 509 | D3D12_RESOURCE_STATE_COPY_DEST; |
| 510 | if (!GrD3DTextureResource::InitTextureResourceInfo(this, resourceDesc, initialState, |
| 511 | isProtected, info)) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 512 | SkDebugf("Failed to init texture resource info\n"); |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | if (!data) { |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | // TODO: upload the data |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 525 | GrBackendTexture GrD3DGpu::onCreateBackendTexture(SkISize dimensions, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 526 | const GrBackendFormat& format, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 527 | GrRenderable renderable, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 528 | GrMipMapped mipMapped, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 529 | GrProtected isProtected, |
| 530 | const BackendTextureData* data) { |
| 531 | this->handleDirtyContext(); |
| 532 | |
| 533 | const GrD3DCaps& caps = this->d3dCaps(); |
| 534 | |
| 535 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 536 | return {}; |
| 537 | } |
| 538 | |
| 539 | DXGI_FORMAT dxgiFormat; |
| 540 | if (!format.asDxgiFormat(&dxgiFormat)) { |
| 541 | return {}; |
| 542 | } |
| 543 | |
| 544 | // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here |
| 545 | if (!caps.isFormatTexturable(dxgiFormat)) { |
| 546 | return {}; |
| 547 | } |
| 548 | |
| 549 | GrD3DTextureResourceInfo info; |
| 550 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes, |
| 551 | renderable, mipMapped, |
| 552 | &info, isProtected, data)) { |
| 553 | return {}; |
| 554 | } |
| 555 | |
| 556 | return GrBackendTexture(dimensions.width(), dimensions.height(), info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | GrBackendTexture GrD3DGpu::onCreateCompressedBackendTexture(SkISize dimensions, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 560 | const GrBackendFormat& format, |
| 561 | GrMipMapped mipMapped, |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 562 | GrProtected isProtected, |
| 563 | const BackendTextureData* data) { |
| 564 | this->handleDirtyContext(); |
| 565 | |
| 566 | const GrD3DCaps& caps = this->d3dCaps(); |
| 567 | |
| 568 | if (this->protectedContext() != (isProtected == GrProtected::kYes)) { |
| 569 | return {}; |
| 570 | } |
| 571 | |
| 572 | DXGI_FORMAT dxgiFormat; |
| 573 | if (!format.asDxgiFormat(&dxgiFormat)) { |
| 574 | return {}; |
| 575 | } |
| 576 | |
| 577 | // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here |
| 578 | if (!caps.isFormatTexturable(dxgiFormat)) { |
| 579 | return {}; |
| 580 | } |
| 581 | |
| 582 | GrD3DTextureResourceInfo info; |
| 583 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, dimensions, GrTexturable::kYes, |
| 584 | GrRenderable::kNo, mipMapped, |
| 585 | &info, isProtected, data)) { |
| 586 | return {}; |
| 587 | } |
| 588 | |
| 589 | return GrBackendTexture(dimensions.width(), dimensions.height(), info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void GrD3DGpu::deleteBackendTexture(const GrBackendTexture& tex) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 593 | SkASSERT(GrBackendApi::kDirect3D == tex.fBackend); |
| 594 | // 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] | 595 | } |
| 596 | |
Robert Phillips | 979b223 | 2020-02-20 10:47:29 -0500 | [diff] [blame] | 597 | bool GrD3DGpu::compile(const GrProgramDesc&, const GrProgramInfo&) { |
| 598 | return false; |
| 599 | } |
| 600 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 601 | #if GR_TEST_UTILS |
| 602 | bool GrD3DGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 603 | SkASSERT(GrBackendApi::kDirect3D == tex.backend()); |
| 604 | |
| 605 | GrD3DTextureResourceInfo info; |
| 606 | if (!tex.getD3DTextureResourceInfo(&info)) { |
| 607 | return false; |
| 608 | } |
| 609 | ID3D12Resource* textureResource = info.fResource.get(); |
| 610 | if (!textureResource) { |
| 611 | return false; |
| 612 | } |
| 613 | return !(textureResource->GetDesc().Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | GrBackendRenderTarget GrD3DGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 617 | GrColorType colorType) { |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 618 | this->handleDirtyContext(); |
| 619 | |
| 620 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 621 | return {}; |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | DXGI_FORMAT dxgiFormat = this->d3dCaps().getFormatFromColorType(colorType); |
| 625 | |
| 626 | GrD3DTextureResourceInfo info; |
| 627 | if (!this->createTextureResourceForBackendSurface(dxgiFormat, { w, h }, GrTexturable::kNo, |
| 628 | GrRenderable::kYes, GrMipMapped::kNo, |
| 629 | &info, GrProtected::kNo, nullptr)) { |
| 630 | return {}; |
| 631 | } |
| 632 | |
| 633 | return GrBackendRenderTarget(w, h, 1, info); |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 634 | } |
| 635 | |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 636 | void GrD3DGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) { |
| 637 | SkASSERT(GrBackendApi::kDirect3D == rt.backend()); |
| 638 | |
| 639 | GrD3DTextureResourceInfo info; |
| 640 | if (rt.getD3DTextureResourceInfo(&info)) { |
| 641 | this->testingOnly_flushGpuAndSync(); |
| 642 | // Nothing else to do here, will get cleaned up when the GrBackendRenderTarget |
| 643 | // is deleted. |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void GrD3DGpu::testingOnly_flushGpuAndSync() { |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 648 | SkAssertResult(this->submitDirectCommandList(SyncQueue::kForce)); |
Jim Van Verth | 96bfeff | 2020-04-09 14:36:12 -0400 | [diff] [blame] | 649 | } |
Jim Van Verth | c632aa6 | 2020-04-17 16:58:20 -0400 | [diff] [blame^] | 650 | |
| 651 | /////////////////////////////////////////////////////////////////////////////// |
| 652 | |
| 653 | void GrD3DGpu::addResourceBarriers(const GrManagedResource* resource, |
| 654 | int numBarriers, |
| 655 | D3D12_RESOURCE_TRANSITION_BARRIER* barriers) const { |
| 656 | SkASSERT(fCurrentDirectCommandList); |
| 657 | SkASSERT(resource); |
| 658 | |
| 659 | fCurrentDirectCommandList->resourceBarrier(resource, numBarriers, barriers); |
| 660 | } |
| 661 | |
| 662 | bool GrD3DGpu::onSubmitToGpu(bool syncCpu) { |
| 663 | if (syncCpu) { |
| 664 | return this->submitDirectCommandList(SyncQueue::kForce); |
| 665 | } else { |
| 666 | return this->submitDirectCommandList(SyncQueue::kSkip); |
| 667 | } |
| 668 | } |
| 669 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 670 | #endif |