Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 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 | |
| 8 | #include "src/gpu/d3d/GrD3DTextureRenderTarget.h" |
| 9 | |
| 10 | #include "src/gpu/GrTexturePriv.h" |
| 11 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 12 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 13 | GrD3DTextureRenderTarget::GrD3DTextureRenderTarget( |
| 14 | GrD3DGpu* gpu, SkBudgeted budgeted, SkISize dimensions, int sampleCnt, |
| 15 | const GrD3DTextureResourceInfo& info, sk_sp<GrD3DResourceState> state, |
| 16 | const GrD3DTextureResourceInfo& msaaInfo, sk_sp<GrD3DResourceState> msaaState, |
| 17 | const D3D12_CPU_DESCRIPTOR_HANDLE& colorRenderTargetView, |
| 18 | const D3D12_CPU_DESCRIPTOR_HANDLE& resolveRenderTargetView, |
| 19 | GrMipMapsStatus mipMapsStatus) |
| 20 | : GrSurface(gpu, dimensions, info.fProtected) |
| 21 | , GrD3DTextureResource(info, state) |
| 22 | , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus) |
| 23 | , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo, |
| 24 | std::move(msaaState), colorRenderTargetView, resolveRenderTargetView) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 25 | SkASSERT(info.fProtected == msaaInfo.fProtected); |
| 26 | this->registerWithCache(budgeted); |
| 27 | } |
| 28 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 29 | GrD3DTextureRenderTarget::GrD3DTextureRenderTarget( |
| 30 | GrD3DGpu* gpu, SkBudgeted budgeted, SkISize dimensions, |
| 31 | const GrD3DTextureResourceInfo& info, sk_sp<GrD3DResourceState> state, |
| 32 | const D3D12_CPU_DESCRIPTOR_HANDLE& renderTargetView, |
| 33 | GrMipMapsStatus mipMapsStatus) |
| 34 | : GrSurface(gpu, dimensions, info.fProtected) |
| 35 | , GrD3DTextureResource(info, state) |
| 36 | , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus) |
| 37 | , GrD3DRenderTarget(gpu, dimensions, info, state, renderTargetView) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 38 | this->registerWithCache(budgeted); |
| 39 | } |
| 40 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 41 | GrD3DTextureRenderTarget::GrD3DTextureRenderTarget( |
| 42 | GrD3DGpu* gpu, SkISize dimensions, int sampleCnt, |
| 43 | const GrD3DTextureResourceInfo& info, sk_sp<GrD3DResourceState> state, |
| 44 | const GrD3DTextureResourceInfo& msaaInfo, sk_sp<GrD3DResourceState> msaaState, |
| 45 | const D3D12_CPU_DESCRIPTOR_HANDLE& colorRenderTargetView, |
| 46 | const D3D12_CPU_DESCRIPTOR_HANDLE& resolveRenderTargetView, |
| 47 | GrMipMapsStatus mipMapsStatus, |
| 48 | GrWrapCacheable cacheable) |
| 49 | : GrSurface(gpu, dimensions, info.fProtected) |
| 50 | , GrD3DTextureResource(info, state) |
| 51 | , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus) |
| 52 | , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo, |
| 53 | std::move(msaaState), colorRenderTargetView, resolveRenderTargetView) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 54 | SkASSERT(info.fProtected == msaaInfo.fProtected); |
| 55 | this->registerWithCacheWrapped(cacheable); |
| 56 | } |
| 57 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 58 | GrD3DTextureRenderTarget::GrD3DTextureRenderTarget( |
| 59 | GrD3DGpu* gpu, SkISize dimensions, |
| 60 | const GrD3DTextureResourceInfo& info, sk_sp<GrD3DResourceState> state, |
| 61 | const D3D12_CPU_DESCRIPTOR_HANDLE& renderTargetView, |
| 62 | GrMipMapsStatus mipMapsStatus, |
| 63 | GrWrapCacheable cacheable) |
| 64 | : GrSurface(gpu, dimensions, info.fProtected) |
| 65 | , GrD3DTextureResource(info, state) |
| 66 | , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus) |
| 67 | , GrD3DRenderTarget(gpu, dimensions, info, state, renderTargetView) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 68 | this->registerWithCacheWrapped(cacheable); |
| 69 | } |
| 70 | |
| 71 | static std::pair<GrD3DTextureResourceInfo, sk_sp<GrD3DResourceState>> create_msaa_resource( |
| 72 | GrD3DGpu* gpu, SkISize dimensions, int sampleCnt, const GrD3DTextureResourceInfo& info) { |
| 73 | GrD3DTextureResourceInfo msInfo; |
| 74 | sk_sp<GrD3DResourceState> msState; |
| 75 | |
| 76 | // create msaa surface |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 77 | D3D12_RESOURCE_DESC msTextureDesc = {}; |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 78 | msTextureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 79 | msTextureDesc.Alignment = 0; // Default alignment (64KB) |
| 80 | msTextureDesc.Width = dimensions.fWidth; |
| 81 | msTextureDesc.Height = dimensions.fHeight; |
| 82 | msTextureDesc.DepthOrArraySize = 1; |
| 83 | msTextureDesc.MipLevels = 1; |
| 84 | msTextureDesc.Format = info.fFormat; |
| 85 | msTextureDesc.SampleDesc.Count = sampleCnt; |
Greg Daniel | c9624d5 | 2020-04-13 15:36:31 -0400 | [diff] [blame] | 86 | // quality levels are only supported for tiled resources so ignore for now |
| 87 | msTextureDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel; |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 88 | msTextureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // Use default for dxgi format |
Jim Van Verth | 6b1aabd | 2020-04-22 16:38:55 -0400 | [diff] [blame] | 89 | msTextureDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 90 | |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 91 | if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, msTextureDesc, |
| 92 | D3D12_RESOURCE_STATE_RENDER_TARGET, |
| 93 | info.fProtected, &msInfo)) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 94 | return {}; |
| 95 | } |
| 96 | |
| 97 | msState.reset(new GrD3DResourceState( |
| 98 | static_cast<D3D12_RESOURCE_STATES>(msInfo.fResourceState))); |
| 99 | |
| 100 | return std::make_pair(msInfo, msState); |
| 101 | } |
| 102 | |
| 103 | sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeNewTextureRenderTarget( |
| 104 | GrD3DGpu* gpu, |
| 105 | SkBudgeted budgeted, |
| 106 | SkISize dimensions, |
| 107 | int sampleCnt, |
| 108 | const D3D12_RESOURCE_DESC& resourceDesc, |
| 109 | GrProtected isProtected, |
| 110 | GrMipMapsStatus mipMapsStatus) { |
| 111 | |
| 112 | GrD3DTextureResourceInfo info; |
Jim Van Verth | 2b9f53e | 2020-04-14 11:47:34 -0400 | [diff] [blame] | 113 | D3D12_RESOURCE_STATES initialState = sampleCnt > 1 ? D3D12_RESOURCE_STATE_RESOLVE_DEST |
| 114 | : D3D12_RESOURCE_STATE_RENDER_TARGET; |
| 115 | if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc, initialState, |
| 116 | isProtected, &info)) { |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 117 | return nullptr; |
| 118 | } |
| 119 | sk_sp<GrD3DResourceState> state(new GrD3DResourceState( |
| 120 | static_cast<D3D12_RESOURCE_STATES>(info.fResourceState))); |
| 121 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 122 | const D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = |
| 123 | gpu->resourceProvider().createRenderTargetView(info.fResource.get()); |
| 124 | |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 125 | if (sampleCnt > 1) { |
| 126 | GrD3DTextureResourceInfo msInfo; |
| 127 | sk_sp<GrD3DResourceState> msState; |
| 128 | |
| 129 | std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info); |
| 130 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 131 | const D3D12_CPU_DESCRIPTOR_HANDLE msaaRenderTargetView = |
| 132 | gpu->resourceProvider().createRenderTargetView(msInfo.fResource.get()); |
| 133 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 134 | GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget( |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 135 | gpu, budgeted, dimensions, sampleCnt, info, std::move(state), |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 136 | msInfo, std::move(msState), msaaRenderTargetView, renderTargetView, mipMapsStatus); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 137 | return sk_sp<GrD3DTextureRenderTarget>(trt); |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 138 | } else { |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 139 | GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget( |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 140 | gpu, budgeted, dimensions, info, std::move(state), renderTargetView, mipMapsStatus); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 141 | return sk_sp<GrD3DTextureRenderTarget>(trt); |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget( |
| 146 | GrD3DGpu* gpu, |
| 147 | SkISize dimensions, |
| 148 | int sampleCnt, |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 149 | GrWrapCacheable cacheable, |
| 150 | const GrD3DTextureResourceInfo& info, |
| 151 | sk_sp<GrD3DResourceState> state) { |
| 152 | // TODO: If a client uses their own heap to allocate, how do we manage that? |
| 153 | // Adopted textures require both image and allocation because we're responsible for freeing |
| 154 | //SkASSERT(VK_NULL_HANDLE != info.fImage && |
| 155 | // (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory)); |
| 156 | |
| 157 | GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kDirty |
| 158 | : GrMipMapsStatus::kNotAllocated; |
| 159 | |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 160 | const D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = |
| 161 | gpu->resourceProvider().createRenderTargetView(info.fResource.get()); |
| 162 | |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 163 | if (sampleCnt > 1) { |
| 164 | GrD3DTextureResourceInfo msInfo; |
| 165 | sk_sp<GrD3DResourceState> msState; |
| 166 | |
| 167 | std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info); |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 168 | const D3D12_CPU_DESCRIPTOR_HANDLE msaaRenderTargetView = |
| 169 | gpu->resourceProvider().createRenderTargetView(msInfo.fResource.get()); |
| 170 | |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 171 | GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget( |
| 172 | gpu, dimensions, sampleCnt, info, std::move(state), msInfo, std::move(msState), |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 173 | msaaRenderTargetView, renderTargetView, mipMapsStatus, cacheable); |
Jim Van Verth | aa90dad | 2020-03-30 15:00:39 -0400 | [diff] [blame] | 174 | return sk_sp<GrD3DTextureRenderTarget>(trt); |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 175 | } else { |
| 176 | return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget( |
Jim Van Verth | 5ce9440 | 2020-05-01 14:19:13 -0400 | [diff] [blame^] | 177 | gpu, dimensions, info, std::move(state), renderTargetView, mipMapsStatus, |
| 178 | cacheable)); |
Jim Van Verth | fd89e0b | 2020-03-26 15:33:02 -0400 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | size_t GrD3DTextureRenderTarget::onGpuMemorySize() const { |
| 183 | int numColorSamples = this->numSamples(); |
| 184 | if (numColorSamples > 1) { |
| 185 | // Add one to account for the resolve VkImage. |
| 186 | ++numColorSamples; |
| 187 | } |
| 188 | const GrCaps& caps = *this->getGpu()->caps(); |
| 189 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), |
| 190 | numColorSamples, // TODO: this still correct? |
| 191 | this->texturePriv().mipMapped()); |
| 192 | } |