File: RenderPassManager.cpp

package info (click to toggle)
0ad 0.27.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 173,296 kB
  • sloc: cpp: 194,003; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,699; perl: 1,575; java: 533; xml: 482; php: 192; makefile: 99
file content (200 lines) | stat: -rw-r--r-- 6,878 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Copyright (C) 2023 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "RenderPassManager.h"

#include "lib/hash.h"
#include "ps/containers/StaticVector.h"
#include "renderer/backend/vulkan/Device.h"
#include "renderer/backend/vulkan/Mapping.h"
#include "renderer/backend/vulkan/Texture.h"
#include "renderer/backend/vulkan/Utilities.h"

namespace Renderer
{

namespace Backend
{

namespace Vulkan
{

size_t CRenderPassManager::DescHash::operator()(const Desc& desc) const
{
	size_t seed = 0;
	hash_combine(seed, desc.sampleCount);

	if (desc.colorAttachment.has_value())
	{
		hash_combine(seed, (*desc.colorAttachment).format);
		hash_combine(seed, (*desc.colorAttachment).loadOp);
		hash_combine(seed, (*desc.colorAttachment).storeOp);
	}
	else
		hash_combine(seed, VK_FORMAT_UNDEFINED);

	if (desc.depthStencilAttachment.has_value())
	{
		hash_combine(seed, (*desc.depthStencilAttachment).format);
		hash_combine(seed, (*desc.depthStencilAttachment).loadOp);
		hash_combine(seed, (*desc.depthStencilAttachment).storeOp);
	}
	else
		hash_combine(seed, VK_FORMAT_UNDEFINED);

	return seed;
}

bool CRenderPassManager::DescEqual::operator()(const Desc& lhs, const Desc& rhs) const
{
	auto compareAttachments = [](const std::optional<Attachment>& lhs, const std::optional<Attachment>& rhs) -> bool
	{
		if (lhs.has_value() != rhs.has_value())
			return false;
		if (!lhs.has_value())
			return true;
		return
			(*lhs).format == (*rhs).format &&
			(*lhs).loadOp == (*rhs).loadOp &&
			(*lhs).storeOp == (*rhs).storeOp;
	};
	if (!compareAttachments(lhs.colorAttachment, rhs.colorAttachment))
		return false;
	if (!compareAttachments(lhs.depthStencilAttachment, rhs.depthStencilAttachment))
		return false;
	return lhs.sampleCount == rhs.sampleCount;
}

CRenderPassManager::CRenderPassManager(CDevice* device)
	: m_Device(device)
{
}

CRenderPassManager::~CRenderPassManager()
{
	for (const auto& it : m_RenderPassMap)
		if (it.second != VK_NULL_HANDLE)
		{
			m_Device->ScheduleObjectToDestroy(
				VK_OBJECT_TYPE_RENDER_PASS, it.second, VK_NULL_HANDLE);
		}
	m_RenderPassMap.clear();
}

VkRenderPass CRenderPassManager::GetOrCreateRenderPass(
	SColorAttachment* colorAttachment,
	SDepthStencilAttachment* depthStencilAttachment)
{
	Desc desc{};
	if (colorAttachment)
	{
		CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>();
		desc.sampleCount = colorAttachmentTexture->GetSampleCount();
		desc.colorAttachment.emplace(Attachment{
			colorAttachmentTexture->GetVkFormat(),
			colorAttachment->loadOp,
			colorAttachment->storeOp});
	}
	if (depthStencilAttachment)
	{
		CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>();
		desc.sampleCount = depthStencilAttachmentTexture->GetSampleCount();
		desc.depthStencilAttachment.emplace(Attachment{
			depthStencilAttachmentTexture->GetVkFormat(),
			depthStencilAttachment->loadOp,
			depthStencilAttachment->storeOp});
	}
	auto it = m_RenderPassMap.find(desc);
	if (it != m_RenderPassMap.end())
		return it->second;

	uint32_t attachmentCount = 0;
	PS::StaticVector<VkAttachmentDescription, 4> attachmentDescs;
	std::optional<VkAttachmentReference> colorAttachmentRef;
	std::optional<VkAttachmentReference> depthStencilAttachmentRef;

	if (colorAttachment)
	{
		CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>();

		VkAttachmentDescription colorAttachmentDesc{};
		colorAttachmentDesc.format = colorAttachmentTexture->GetVkFormat();
		colorAttachmentDesc.samples = Mapping::FromSampleCount(colorAttachmentTexture->GetSampleCount());
		colorAttachmentDesc.loadOp = Mapping::FromAttachmentLoadOp(colorAttachment->loadOp);
		colorAttachmentDesc.storeOp = Mapping::FromAttachmentStoreOp(colorAttachment->storeOp);
		colorAttachmentDesc.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
		colorAttachmentDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

		attachmentDescs.emplace_back(std::move(colorAttachmentDesc));

		colorAttachmentRef.emplace(VkAttachmentReference{
			attachmentCount++, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL});
	}

	if (depthStencilAttachment)
	{
		CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>();

		VkAttachmentDescription depthStencilAttachmentDesc{};
		depthStencilAttachmentDesc.format = depthStencilAttachmentTexture->GetVkFormat();
		depthStencilAttachmentDesc.samples = Mapping::FromSampleCount(depthStencilAttachmentTexture->GetSampleCount());
		depthStencilAttachmentDesc.loadOp = Mapping::FromAttachmentLoadOp(depthStencilAttachment->loadOp);
		depthStencilAttachmentDesc.storeOp = Mapping::FromAttachmentStoreOp(depthStencilAttachment->storeOp);
		depthStencilAttachmentDesc.stencilLoadOp = depthStencilAttachmentDesc.loadOp;
		depthStencilAttachmentDesc.stencilStoreOp = depthStencilAttachmentDesc.storeOp;
		depthStencilAttachmentDesc.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
		depthStencilAttachmentDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

		attachmentDescs.emplace_back(std::move(depthStencilAttachmentDesc));

		depthStencilAttachmentRef.emplace(VkAttachmentReference{
			attachmentCount++, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL});
	}

	VkSubpassDescription subpassDesc{};
	subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
	if (colorAttachment)
	{
		subpassDesc.colorAttachmentCount = 1;
		subpassDesc.pColorAttachments = &(*colorAttachmentRef);
	}
	if (depthStencilAttachment)
		subpassDesc.pDepthStencilAttachment = &(*depthStencilAttachmentRef);

	VkRenderPassCreateInfo renderPassCreateInfo{};
	renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
	renderPassCreateInfo.attachmentCount = attachmentDescs.size();
	renderPassCreateInfo.pAttachments = attachmentDescs.data();
	renderPassCreateInfo.subpassCount = 1;
	renderPassCreateInfo.pSubpasses = &subpassDesc;

	VkRenderPass renderPass = VK_NULL_HANDLE;
	ENSURE_VK_SUCCESS(
		vkCreateRenderPass(
			m_Device->GetVkDevice(), &renderPassCreateInfo, nullptr, &renderPass));
	it = m_RenderPassMap.emplace(desc, renderPass).first;

	return renderPass;
}

} // namespace Vulkan

} // namespace Backend

} // namespace Renderer