File: Framebuffer.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 (136 lines) | stat: -rw-r--r-- 4,833 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
/* 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 "Framebuffer.h"

#include "renderer/backend/vulkan/Device.h"
#include "renderer/backend/vulkan/Mapping.h"
#include "renderer/backend/vulkan/RenderPassManager.h"
#include "renderer/backend/vulkan/Texture.h"
#include "renderer/backend/vulkan/Utilities.h"

namespace Renderer
{

namespace Backend
{

namespace Vulkan
{

// static
std::unique_ptr<CFramebuffer> CFramebuffer::Create(
	CDevice* device, const char* name,
	SColorAttachment* colorAttachment, SDepthStencilAttachment* depthStencilAttachment)
{
	ENSURE(colorAttachment || depthStencilAttachment);
	if (colorAttachment)
		ENSURE(colorAttachment->texture);
	if (depthStencilAttachment)
		ENSURE(depthStencilAttachment->texture);

	if (colorAttachment && depthStencilAttachment)
	{
		CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>();
		CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>();
		ENSURE(
			colorAttachmentTexture->GetWidth() == depthStencilAttachmentTexture->GetWidth() &&
			colorAttachmentTexture->GetHeight() == depthStencilAttachmentTexture->GetHeight() &&
			colorAttachmentTexture->GetSampleCount() == depthStencilAttachmentTexture->GetSampleCount());
	}

	std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer());
	framebuffer->m_Device = device;
	framebuffer->m_UID = device->GenerateNextDeviceObjectUID();
	if (colorAttachment)
		framebuffer->m_ClearColor = colorAttachment->clearColor;

	PS::StaticVector<VkImageView, 4> attachments;

	if (colorAttachment)
	{
		CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>();
		ENSURE(colorAttachmentTexture->GetUsage() & ITexture::Usage::COLOR_ATTACHMENT);

		framebuffer->m_Width = colorAttachmentTexture->GetWidth();
		framebuffer->m_Height = colorAttachmentTexture->GetHeight();
		framebuffer->m_SampleCount = colorAttachmentTexture->GetSampleCount();
		framebuffer->m_ColorAttachmentLoadOp = colorAttachment->loadOp;
		framebuffer->m_ColorAttachmentStoreOp = colorAttachment->storeOp;

		attachments.emplace_back(colorAttachmentTexture->GetAttachmentImageView());
		framebuffer->m_ColorAttachments.emplace_back(colorAttachmentTexture);
	}

	if (depthStencilAttachment)
	{
		CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>();
		ENSURE(depthStencilAttachmentTexture->GetUsage() & ITexture::Usage::DEPTH_STENCIL_ATTACHMENT);

		framebuffer->m_Width = depthStencilAttachmentTexture->GetWidth();
		framebuffer->m_Height = depthStencilAttachmentTexture->GetHeight();
		framebuffer->m_SampleCount = depthStencilAttachmentTexture->GetSampleCount();

		framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachment->loadOp;
		framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachment->storeOp;

		attachments.emplace_back(depthStencilAttachmentTexture->GetAttachmentImageView());
		framebuffer->m_DepthStencilAttachment = depthStencilAttachmentTexture;
	}

	ENSURE(framebuffer->m_Width > 0 && framebuffer->m_Height > 0);
	ENSURE(framebuffer->m_SampleCount > 0);

	framebuffer->m_RenderPass = device->GetRenderPassManager().GetOrCreateRenderPass(
		colorAttachment, depthStencilAttachment);

	VkFramebufferCreateInfo framebufferInfo{};
	framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
	framebufferInfo.renderPass = framebuffer->m_RenderPass;
	framebufferInfo.attachmentCount = attachments.size();
	framebufferInfo.pAttachments = attachments.data();
	framebufferInfo.width = framebuffer->m_Width;
	framebufferInfo.height = framebuffer->m_Height;
	framebufferInfo.layers = 1;

	ENSURE_VK_SUCCESS(vkCreateFramebuffer(
		device->GetVkDevice(), &framebufferInfo, nullptr, &framebuffer->m_Framebuffer));

	device->SetObjectName(VK_OBJECT_TYPE_FRAMEBUFFER, framebuffer->m_Framebuffer, name);

	return framebuffer;
}

CFramebuffer::~CFramebuffer()
{
	if (m_Framebuffer != VK_NULL_HANDLE)
		m_Device->ScheduleObjectToDestroy(
			VK_OBJECT_TYPE_FRAMEBUFFER, m_Framebuffer, VK_NULL_HANDLE);
}

IDevice* CFramebuffer::GetDevice()
{
	return m_Device;
}

} // namespace Vulkan

} // namespace Backend

} // namespace Renderer