blob: 49942e95845afbb51a4cfaba3467ac13baf8e17d [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);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400130 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400131 } else {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400132 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
133 gpu, budgeted, dimensions, info, std::move(state), mipMapsStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400134 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400135 }
136}
137
138sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(
139 GrD3DGpu* gpu,
140 SkISize dimensions,
141 int sampleCnt,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400142 GrWrapCacheable cacheable,
143 const GrD3DTextureResourceInfo& info,
144 sk_sp<GrD3DResourceState> state) {
145 // TODO: If a client uses their own heap to allocate, how do we manage that?
146 // Adopted textures require both image and allocation because we're responsible for freeing
147 //SkASSERT(VK_NULL_HANDLE != info.fImage &&
148 // (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
149
150 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kDirty
151 : GrMipMapsStatus::kNotAllocated;
152
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400153 if (sampleCnt > 1) {
154 GrD3DTextureResourceInfo msInfo;
155 sk_sp<GrD3DResourceState> msState;
156
157 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400158 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
159 gpu, dimensions, sampleCnt, info, std::move(state), msInfo, std::move(msState),
160 mipMapsStatus, cacheable);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400161 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400162 } else {
163 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400164 gpu, dimensions, info, std::move(state), mipMapsStatus, cacheable));
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400165 }
166}
167
168size_t GrD3DTextureRenderTarget::onGpuMemorySize() const {
169 int numColorSamples = this->numSamples();
170 if (numColorSamples > 1) {
171 // Add one to account for the resolve VkImage.
172 ++numColorSamples;
173 }
174 const GrCaps& caps = *this->getGpu()->caps();
175 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
176 numColorSamples, // TODO: this still correct?
177 this->texturePriv().mipMapped());
178}