blob: 91a46aeec0a70dab56a403aab0210516bfdd2557 [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;
Greg Danielc9624d52020-04-13 15:36:31 -040090 // quality levels are only supported for tiled resources so ignore for now
91 msTextureDesc.SampleDesc.Quality = GrD3DTextureResource::kDefaultQualityLevel;
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040092 msTextureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // Use default for dxgi format
93 msTextureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
94
95 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, msTextureDesc, info.fProtected,
96 &msInfo)) {
97 return {};
98 }
99
100 msState.reset(new GrD3DResourceState(
101 static_cast<D3D12_RESOURCE_STATES>(msInfo.fResourceState)));
102
103 return std::make_pair(msInfo, msState);
104}
105
106sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
107 GrD3DGpu* gpu,
108 SkBudgeted budgeted,
109 SkISize dimensions,
110 int sampleCnt,
111 const D3D12_RESOURCE_DESC& resourceDesc,
112 GrProtected isProtected,
113 GrMipMapsStatus mipMapsStatus) {
114
115 GrD3DTextureResourceInfo info;
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400116 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc, isProtected, &info)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400117 return nullptr;
118 }
119 sk_sp<GrD3DResourceState> state(new GrD3DResourceState(
120 static_cast<D3D12_RESOURCE_STATES>(info.fResourceState)));
121
122 if (sampleCnt > 1) {
123 GrD3DTextureResourceInfo msInfo;
124 sk_sp<GrD3DResourceState> msState;
125
126 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
127
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400128 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400129 gpu, budgeted, dimensions, sampleCnt, info, std::move(state),
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400130 msInfo, std::move(msState), mipMapsStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400131 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400132 } else {
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400133 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
134 gpu, budgeted, dimensions, info, std::move(state), mipMapsStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400135 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400136 }
137}
138
139sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(
140 GrD3DGpu* gpu,
141 SkISize dimensions,
142 int sampleCnt,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400143 GrWrapCacheable cacheable,
144 const GrD3DTextureResourceInfo& info,
145 sk_sp<GrD3DResourceState> state) {
146 // TODO: If a client uses their own heap to allocate, how do we manage that?
147 // Adopted textures require both image and allocation because we're responsible for freeing
148 //SkASSERT(VK_NULL_HANDLE != info.fImage &&
149 // (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
150
151 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kDirty
152 : GrMipMapsStatus::kNotAllocated;
153
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400154 if (sampleCnt > 1) {
155 GrD3DTextureResourceInfo msInfo;
156 sk_sp<GrD3DResourceState> msState;
157
158 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400159 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
160 gpu, dimensions, sampleCnt, info, std::move(state), msInfo, std::move(msState),
161 mipMapsStatus, cacheable);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400162 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400163 } else {
164 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400165 gpu, dimensions, info, std::move(state), mipMapsStatus, cacheable));
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400166 }
167}
168
169size_t GrD3DTextureRenderTarget::onGpuMemorySize() const {
170 int numColorSamples = this->numSamples();
171 if (numColorSamples > 1) {
172 // Add one to account for the resolve VkImage.
173 ++numColorSamples;
174 }
175 const GrCaps& caps = *this->getGpu()->caps();
176 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
177 numColorSamples, // TODO: this still correct?
178 this->texturePriv().mipMapped());
179}