File: Buffer.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 (150 lines) | stat: -rw-r--r-- 4,630 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
/* Copyright (C) 2024 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 "Buffer.h"

#include "renderer/backend/vulkan/Device.h"
#include "renderer/backend/vulkan/Utilities.h"

#include <tuple>

namespace Renderer
{

namespace Backend
{

namespace Vulkan
{

VkBufferUsageFlags ToVkBufferUsageFlags(const uint32_t usage)
{
	VkBufferUsageFlags usageFlags = 0;
	if (usage & IBuffer::Usage::TRANSFER_SRC)
		usageFlags |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
	if (usage & IBuffer::Usage::TRANSFER_DST)
		usageFlags |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
	if (usage & IBuffer::Usage::STORAGE)
		usageFlags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
	return usageFlags;
}

std::tuple<VkBufferUsageFlags, VkMemoryPropertyFlags, VmaMemoryUsage> MakeCreationFlags(
	const IBuffer::Type type, const uint32_t usage)
{
	const VkBufferUsageFlags commonFlags = ToVkBufferUsageFlags(usage);

	switch (type)
	{
	case IBuffer::Type::VERTEX:
		ENSURE(usage & (IBuffer::Usage::TRANSFER_DST | IBuffer::Usage::STORAGE));
		return {
			commonFlags | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
			VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
			VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
	case IBuffer::Type::INDEX:
		ENSURE(usage & (IBuffer::Usage::TRANSFER_DST | IBuffer::Usage::STORAGE));
		return {
			commonFlags | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
			VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
			VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
	case IBuffer::Type::UPLOAD:
		ENSURE(usage & IBuffer::Usage::TRANSFER_SRC);
		ENSURE(!(usage & IBuffer::Usage::STORAGE));
		return {
			commonFlags,
			VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
			VMA_MEMORY_USAGE_AUTO};
	case IBuffer::Type::UNIFORM:
		ENSURE(usage & IBuffer::Usage::TRANSFER_DST);
		return {
			commonFlags | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
			VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
			VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
	}

	return {0, 0, VMA_MEMORY_USAGE_AUTO};
}

// static
std::unique_ptr<CBuffer> CBuffer::Create(
	CDevice* device, const char* name, const Type type, const uint32_t size,
	const uint32_t usage)
{
	std::unique_ptr<CBuffer> buffer(new CBuffer());
	buffer->m_Device = device;
	buffer->m_UID = device->GenerateNextDeviceObjectUID();
	buffer->m_Type = type;
	buffer->m_Size = size;
	buffer->m_Usage = usage;

	const auto [usageFlags, memoryProperties, memoryUsage] = MakeCreationFlags(type, usage);
	// According to the Vulkan spec usage must not be 0.
	ENSURE(usageFlags != 0);

	VkBufferCreateInfo bufferCreateInfo{};
	bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	bufferCreateInfo.size = size;
	bufferCreateInfo.usage = usageFlags;
	bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

	VmaAllocationCreateInfo allocationCreateInfo{};
	if (type == Type::UPLOAD)
		allocationCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
#ifndef NDEBUG
	allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
	allocationCreateInfo.pUserData = const_cast<char*>(name);
#endif
	allocationCreateInfo.requiredFlags = memoryProperties;
	allocationCreateInfo.usage = memoryUsage;
	const VkResult createBufferResult = vmaCreateBuffer(
		device->GetVMAAllocator(), &bufferCreateInfo, &allocationCreateInfo,
		&buffer->m_Buffer, &buffer->m_Allocation, &buffer->m_AllocationInfo);
	if (createBufferResult != VK_SUCCESS)
	{
		LOGERROR("Failed to create VkBuffer: %d (%s)", static_cast<int>(createBufferResult), Utilities::GetVkResultName(createBufferResult));
		return nullptr;
	}

	device->SetObjectName(VK_OBJECT_TYPE_BUFFER, buffer->m_Buffer, name);

	return buffer;
}

CBuffer::CBuffer() = default;

CBuffer::~CBuffer()
{
	if (m_Allocation != VK_NULL_HANDLE)
		m_Device->ScheduleObjectToDestroy(
			VK_OBJECT_TYPE_BUFFER, m_Buffer, m_Allocation);

	m_Device->ScheduleBufferToDestroy(m_UID);
}

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

} // namespace Vulkan

} // namespace Backend

} // namespace Renderer