blob: 507aed6642418fa4c4bb7fb2512265ee1f764c0a [file] [log] [blame]
Jim Van Verthfd89e0b2020-03-26 15:33:02 -04001/*
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
13GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
14 SkBudgeted budgeted,
15 SkISize dimensions,
16 int sampleCnt,
17 const GrD3DTextureResourceInfo& info,
18 sk_sp<GrD3DResourceState> state,
19 const GrD3DTextureResourceInfo& msaaInfo,
20 sk_sp<GrD3DResourceState> msaaState,
21 GrMipMapsStatus mipMapsStatus)
22 : GrSurface(gpu, dimensions, info.fProtected)
Jim Van Verthaa90dad2020-03-30 15:00:39 -040023 , GrD3DTextureResource(info, state)
24 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus)
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040025 , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo,
Jim Van Verthaa90dad2020-03-30 15:00:39 -040026 std::move(msaaState)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040027 SkASSERT(info.fProtected == msaaInfo.fProtected);
28 this->registerWithCache(budgeted);
29}
30
31GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
32 SkBudgeted budgeted,
33 SkISize dimensions,
34 const GrD3DTextureResourceInfo& info,
35 sk_sp<GrD3DResourceState> state,
36 GrMipMapsStatus mipMapsStatus)
37 : GrSurface(gpu, dimensions, info.fProtected)
Jim Van Verthaa90dad2020-03-30 15:00:39 -040038 , GrD3DTextureResource(info, state)
39 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus)
40 , GrD3DRenderTarget(gpu, dimensions, info, state) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040041 this->registerWithCache(budgeted);
42}
43
44GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
45 SkISize dimensions,
46 int sampleCnt,
47 const GrD3DTextureResourceInfo& info,
48 sk_sp<GrD3DResourceState> state,
49 const GrD3DTextureResourceInfo& msaaInfo,
50 sk_sp<GrD3DResourceState> msaaState,
51 GrMipMapsStatus mipMapsStatus,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040052 GrWrapCacheable cacheable)
53 : GrSurface(gpu, dimensions, info.fProtected)
Jim Van Verthaa90dad2020-03-30 15:00:39 -040054 , GrD3DTextureResource(info, state)
55 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus)
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040056 , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo,
Jim Van Verthaa90dad2020-03-30 15:00:39 -040057 std::move(msaaState)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040058 SkASSERT(info.fProtected == msaaInfo.fProtected);
59 this->registerWithCacheWrapped(cacheable);
60}
61
62GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
63 SkISize dimensions,
64 const GrD3DTextureResourceInfo& info,
65 sk_sp<GrD3DResourceState> state,
66 GrMipMapsStatus mipMapsStatus,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040067 GrWrapCacheable cacheable)
68 : GrSurface(gpu, dimensions, info.fProtected)
Jim Van Verthaa90dad2020-03-30 15:00:39 -040069 , GrD3DTextureResource(info, state)
70 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus)
71 , GrD3DRenderTarget(gpu, dimensions, info, state) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040072 this->registerWithCacheWrapped(cacheable);
73}
74
75static std::pair<GrD3DTextureResourceInfo, sk_sp<GrD3DResourceState>> create_msaa_resource(
76 GrD3DGpu* gpu, SkISize dimensions, int sampleCnt, const GrD3DTextureResourceInfo& info) {
77 GrD3DTextureResourceInfo msInfo;
78 sk_sp<GrD3DResourceState> msState;
79
80 // create msaa surface
81 D3D12_RESOURCE_DESC msTextureDesc;
82 msTextureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
83 msTextureDesc.Alignment = 0; // Default alignment (64KB)
84 msTextureDesc.Width = dimensions.fWidth;
85 msTextureDesc.Height = dimensions.fHeight;
86 msTextureDesc.DepthOrArraySize = 1;
87 msTextureDesc.MipLevels = 1;
88 msTextureDesc.Format = info.fFormat;
89 msTextureDesc.SampleDesc.Count = sampleCnt;
90 msTextureDesc.SampleDesc.Quality = 0; // TODO: only valid for tiled renderers
91 msTextureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // Use default for dxgi format
92 msTextureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
93
94 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, msTextureDesc, info.fProtected,
95 &msInfo)) {
96 return {};
97 }
98
99 msState.reset(new GrD3DResourceState(
100 static_cast<D3D12_RESOURCE_STATES>(msInfo.fResourceState)));
101
102 return std::make_pair(msInfo, msState);
103}
104
105sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
106 GrD3DGpu* gpu,
107 SkBudgeted budgeted,
108 SkISize dimensions,
109 int sampleCnt,
110 const D3D12_RESOURCE_DESC& resourceDesc,
111 GrProtected isProtected,
112 GrMipMapsStatus mipMapsStatus) {
113
114 GrD3DTextureResourceInfo info;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400115 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc, isProtected, &info)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400116 return nullptr;
117 }
118 sk_sp<GrD3DResourceState> state(new GrD3DResourceState(
119 static_cast<D3D12_RESOURCE_STATES>(info.fResourceState)));
120
121 if (sampleCnt > 1) {
122 GrD3DTextureResourceInfo msInfo;
123 sk_sp<GrD3DResourceState> msState;
124
125 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
126
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400127 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400128 gpu, budgeted, dimensions, sampleCnt, info, std::move(state),
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400129 msInfo, std::move(msState), mipMapsStatus);
130
131 // The GrD3DTextureRenderTarget takes a ref on the textures so we need to release ours
132 GrD3DTextureResource::ReleaseTextureResourceInfo(&msInfo);
133 GrD3DTextureResource::ReleaseTextureResourceInfo(&info);
134
135 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400136 } else {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400137 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
138 gpu, budgeted, dimensions, info, std::move(state), mipMapsStatus);
139 // The GrD3DTextureRenderTarget takes a ref on the texture so we need to release ours
140 GrD3DTextureResource::ReleaseTextureResourceInfo(&info);
141 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400142 }
143}
144
145sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(
146 GrD3DGpu* gpu,
147 SkISize dimensions,
148 int sampleCnt,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400149 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 Verthfd89e0b2020-03-26 15:33:02 -0400160 if (sampleCnt > 1) {
161 GrD3DTextureResourceInfo msInfo;
162 sk_sp<GrD3DResourceState> msState;
163
164 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400165 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
166 gpu, dimensions, sampleCnt, info, std::move(state), msInfo, std::move(msState),
167 mipMapsStatus, cacheable);
168 // The GrD3DTexture takes a ref on the msaa texture so we need to release ours
169 GrD3DTextureResource::ReleaseTextureResourceInfo(&msInfo);
170
171 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400172 } else {
173 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400174 gpu, dimensions, info, std::move(state), mipMapsStatus, cacheable));
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400175 }
176}
177
178size_t GrD3DTextureRenderTarget::onGpuMemorySize() const {
179 int numColorSamples = this->numSamples();
180 if (numColorSamples > 1) {
181 // Add one to account for the resolve VkImage.
182 ++numColorSamples;
183 }
184 const GrCaps& caps = *this->getGpu()->caps();
185 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
186 numColorSamples, // TODO: this still correct?
187 this->texturePriv().mipMapped());
188}