blob: 211b45d7d00217161f76288be88ef62c90118226 [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)
23 , GrD3DTextureResource(info, state, GrBackendObjectOwnership::kOwned)
24 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus,
25 GrBackendObjectOwnership::kOwned)
26 , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo,
27 std::move(msaaState), GrBackendObjectOwnership::kOwned) {
28 SkASSERT(info.fProtected == msaaInfo.fProtected);
29 this->registerWithCache(budgeted);
30}
31
32GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
33 SkBudgeted budgeted,
34 SkISize dimensions,
35 const GrD3DTextureResourceInfo& info,
36 sk_sp<GrD3DResourceState> state,
37 GrMipMapsStatus mipMapsStatus)
38 : GrSurface(gpu, dimensions, info.fProtected)
39 , GrD3DTextureResource(info, state, GrBackendObjectOwnership::kOwned)
40 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus,
41 GrBackendObjectOwnership::kOwned)
42 , GrD3DRenderTarget(gpu, dimensions, info, state, GrBackendObjectOwnership::kOwned) {
43 this->registerWithCache(budgeted);
44}
45
46GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
47 SkISize dimensions,
48 int sampleCnt,
49 const GrD3DTextureResourceInfo& info,
50 sk_sp<GrD3DResourceState> state,
51 const GrD3DTextureResourceInfo& msaaInfo,
52 sk_sp<GrD3DResourceState> msaaState,
53 GrMipMapsStatus mipMapsStatus,
54 GrBackendObjectOwnership ownership,
55 GrWrapCacheable cacheable)
56 : GrSurface(gpu, dimensions, info.fProtected)
57 , GrD3DTextureResource(info, state, ownership)
58 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus, ownership)
59 , GrD3DRenderTarget(gpu, dimensions, sampleCnt, info, state, msaaInfo,
60 std::move(msaaState), ownership) {
61 SkASSERT(info.fProtected == msaaInfo.fProtected);
62 this->registerWithCacheWrapped(cacheable);
63}
64
65GrD3DTextureRenderTarget::GrD3DTextureRenderTarget(GrD3DGpu* gpu,
66 SkISize dimensions,
67 const GrD3DTextureResourceInfo& info,
68 sk_sp<GrD3DResourceState> state,
69 GrMipMapsStatus mipMapsStatus,
70 GrBackendObjectOwnership ownership,
71 GrWrapCacheable cacheable)
72 : GrSurface(gpu, dimensions, info.fProtected)
73 , GrD3DTextureResource(info, state, ownership)
74 , GrD3DTexture(gpu, dimensions, info, state, mipMapsStatus, ownership)
75 , GrD3DRenderTarget(gpu, dimensions, info, state, ownership) {
76 this->registerWithCacheWrapped(cacheable);
77}
78
79static std::pair<GrD3DTextureResourceInfo, sk_sp<GrD3DResourceState>> create_msaa_resource(
80 GrD3DGpu* gpu, SkISize dimensions, int sampleCnt, const GrD3DTextureResourceInfo& info) {
81 GrD3DTextureResourceInfo msInfo;
82 sk_sp<GrD3DResourceState> msState;
83
84 // create msaa surface
85 D3D12_RESOURCE_DESC msTextureDesc;
86 msTextureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
87 msTextureDesc.Alignment = 0; // Default alignment (64KB)
88 msTextureDesc.Width = dimensions.fWidth;
89 msTextureDesc.Height = dimensions.fHeight;
90 msTextureDesc.DepthOrArraySize = 1;
91 msTextureDesc.MipLevels = 1;
92 msTextureDesc.Format = info.fFormat;
93 msTextureDesc.SampleDesc.Count = sampleCnt;
94 msTextureDesc.SampleDesc.Quality = 0; // TODO: only valid for tiled renderers
95 msTextureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // Use default for dxgi format
96 msTextureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
97
98 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, msTextureDesc, info.fProtected,
99 &msInfo)) {
100 return {};
101 }
102
103 msState.reset(new GrD3DResourceState(
104 static_cast<D3D12_RESOURCE_STATES>(msInfo.fResourceState)));
105
106 return std::make_pair(msInfo, msState);
107}
108
109sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
110 GrD3DGpu* gpu,
111 SkBudgeted budgeted,
112 SkISize dimensions,
113 int sampleCnt,
114 const D3D12_RESOURCE_DESC& resourceDesc,
115 GrProtected isProtected,
116 GrMipMapsStatus mipMapsStatus) {
117
118 GrD3DTextureResourceInfo info;
119 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc, isProtected,
120 &info)) {
121 return nullptr;
122 }
123 sk_sp<GrD3DResourceState> state(new GrD3DResourceState(
124 static_cast<D3D12_RESOURCE_STATES>(info.fResourceState)));
125
126 if (sampleCnt > 1) {
127 GrD3DTextureResourceInfo msInfo;
128 sk_sp<GrD3DResourceState> msState;
129
130 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
131
132 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
133 gpu, budgeted, dimensions, sampleCnt, info, std::move(state),
134 msInfo, std::move(msState), mipMapsStatus));
135 } else {
136 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
137 gpu, budgeted, dimensions, info, std::move(state), mipMapsStatus));
138 }
139}
140
141sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(
142 GrD3DGpu* gpu,
143 SkISize dimensions,
144 int sampleCnt,
145 GrWrapOwnership wrapOwnership,
146 GrWrapCacheable cacheable,
147 const GrD3DTextureResourceInfo& info,
148 sk_sp<GrD3DResourceState> state) {
149 // TODO: If a client uses their own heap to allocate, how do we manage that?
150 // Adopted textures require both image and allocation because we're responsible for freeing
151 //SkASSERT(VK_NULL_HANDLE != info.fImage &&
152 // (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
153
154 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kDirty
155 : GrMipMapsStatus::kNotAllocated;
156
157 GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership
158 ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned;
159 if (sampleCnt > 1) {
160 GrD3DTextureResourceInfo msInfo;
161 sk_sp<GrD3DResourceState> msState;
162
163 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
164 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
165 gpu, dimensions, sampleCnt, info, std::move(state), msInfo, msState, mipMapsStatus,
166 ownership, cacheable));
167 } else {
168 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
169 gpu, dimensions, info, std::move(state), mipMapsStatus, ownership, cacheable));
170 }
171}
172
173size_t GrD3DTextureRenderTarget::onGpuMemorySize() const {
174 int numColorSamples = this->numSamples();
175 if (numColorSamples > 1) {
176 // Add one to account for the resolve VkImage.
177 ++numColorSamples;
178 }
179 const GrCaps& caps = *this->getGpu()->caps();
180 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
181 numColorSamples, // TODO: this still correct?
182 this->texturePriv().mipMapped());
183}