blob: 636458f184b37f39bfcfc1ddd0446a3dc6bd07f8 [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
Jim Van Verth2b9f53e2020-04-14 11:47:34 -040081 D3D12_RESOURCE_DESC msTextureDesc = {};
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040082 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
Jim Van Verth2b9f53e2020-04-14 11:47:34 -040095 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, msTextureDesc,
96 D3D12_RESOURCE_STATE_RENDER_TARGET,
97 info.fProtected, &msInfo)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -040098 return {};
99 }
100
101 msState.reset(new GrD3DResourceState(
102 static_cast<D3D12_RESOURCE_STATES>(msInfo.fResourceState)));
103
104 return std::make_pair(msInfo, msState);
105}
106
107sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeNewTextureRenderTarget(
108 GrD3DGpu* gpu,
109 SkBudgeted budgeted,
110 SkISize dimensions,
111 int sampleCnt,
112 const D3D12_RESOURCE_DESC& resourceDesc,
113 GrProtected isProtected,
114 GrMipMapsStatus mipMapsStatus) {
115
116 GrD3DTextureResourceInfo info;
Jim Van Verth2b9f53e2020-04-14 11:47:34 -0400117 D3D12_RESOURCE_STATES initialState = sampleCnt > 1 ? D3D12_RESOURCE_STATE_RESOLVE_DEST
118 : D3D12_RESOURCE_STATE_RENDER_TARGET;
119 if (!GrD3DTextureResource::InitTextureResourceInfo(gpu, resourceDesc, initialState,
120 isProtected, &info)) {
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400121 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
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400132 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400133 gpu, budgeted, dimensions, sampleCnt, info, std::move(state),
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400134 msInfo, std::move(msState), mipMapsStatus);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400135 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);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400139 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400140 }
141}
142
143sk_sp<GrD3DTextureRenderTarget> GrD3DTextureRenderTarget::MakeWrappedTextureRenderTarget(
144 GrD3DGpu* gpu,
145 SkISize dimensions,
146 int sampleCnt,
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400147 GrWrapCacheable cacheable,
148 const GrD3DTextureResourceInfo& info,
149 sk_sp<GrD3DResourceState> state) {
150 // TODO: If a client uses their own heap to allocate, how do we manage that?
151 // Adopted textures require both image and allocation because we're responsible for freeing
152 //SkASSERT(VK_NULL_HANDLE != info.fImage &&
153 // (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
154
155 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kDirty
156 : GrMipMapsStatus::kNotAllocated;
157
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400158 if (sampleCnt > 1) {
159 GrD3DTextureResourceInfo msInfo;
160 sk_sp<GrD3DResourceState> msState;
161
162 std::tie(msInfo, msState) = create_msaa_resource(gpu, dimensions, sampleCnt, info);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400163 GrD3DTextureRenderTarget* trt = new GrD3DTextureRenderTarget(
164 gpu, dimensions, sampleCnt, info, std::move(state), msInfo, std::move(msState),
165 mipMapsStatus, cacheable);
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400166 return sk_sp<GrD3DTextureRenderTarget>(trt);
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400167 } else {
168 return sk_sp<GrD3DTextureRenderTarget>(new GrD3DTextureRenderTarget(
Jim Van Verthaa90dad2020-03-30 15:00:39 -0400169 gpu, dimensions, info, std::move(state), mipMapsStatus, cacheable));
Jim Van Verthfd89e0b2020-03-26 15:33:02 -0400170 }
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}