FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_vertexBuffer.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_VULKAN_C_VERTEXBUFFER_HPP_INCLUDED
18#define _FGE_VULKAN_C_VERTEXBUFFER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_vertex.hpp"
22#include "FastEngine/C_rect.hpp"
23#include "FastEngine/vulkan/C_contextAware.hpp"
24#include "FastEngine/vulkan/vulkanGlobal.hpp"
25#include <limits>
26#include <vector>
27
28#define FGE_VULKAN_VERTEX_DEFAULT_TOPOLOGY VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
29
30namespace fge::vulkan
31{
32
33class CommandBuffer;
34
35enum class BufferTypes
36{
37 UNINITIALIZED,
38 LOCAL,
39 DEVICE,
40
41 DEFAULT = LOCAL
42};
43
44class FGE_API VertexBuffer : public ContextAware
45{
46public:
47 explicit VertexBuffer(Context const& context);
48 VertexBuffer(VertexBuffer const& r);
49 VertexBuffer(VertexBuffer&& r) noexcept;
50 ~VertexBuffer() override;
51
52 VertexBuffer& operator=(VertexBuffer const& r);
53 VertexBuffer& operator=(VertexBuffer&& r) noexcept;
54
55 void create(std::size_t vertexSize, VkPrimitiveTopology topology, BufferTypes type = BufferTypes::DEFAULT);
56
57 void clear();
58 void resize(std::size_t vertexSize);
59 void append(Vertex const& vertex);
60
61 void destroy() final;
62
63 void bind(CommandBuffer& commandBuffer) const;
64
65 [[nodiscard]] std::size_t getCount() const;
66
67 [[nodiscard]] Vertex* getVertices();
68 [[nodiscard]] Vertex const* getVertices() const;
69
70 [[nodiscard]] Vertex& operator[](std::size_t index);
71 [[nodiscard]] Vertex const& operator[](std::size_t index) const;
72
73 void setPrimitiveTopology(VkPrimitiveTopology topology);
74 [[nodiscard]] VkPrimitiveTopology getPrimitiveTopology() const;
75
76 [[nodiscard]] VkBuffer getVerticesBuffer() const;
77 [[nodiscard]] VmaAllocation getVerticesBufferAllocation() const;
78
79 [[nodiscard]] BufferTypes getType() const;
80
81 [[nodiscard]] fge::RectFloat getBounds() const;
82
83private:
84 void mapBuffer() const;
85 void cleanBuffer() const;
86 void updateBuffer() const;
87
88 std::vector<Vertex> g_vertices;
89
90 mutable VkBuffer g_buffer;
91 mutable VkBuffer g_stagingBuffer;
92 mutable VmaAllocation g_bufferAllocation;
93 mutable VmaAllocation g_stagingBufferAllocation;
94 mutable std::size_t g_bufferCapacity;
95
96 mutable bool g_needUpdate;
97
98 BufferTypes g_type;
99
100 mutable VkPrimitiveTopology g_primitiveTopology;
101};
102
103class FGE_API IndexBuffer : public ContextAware
104{
105public:
106 explicit IndexBuffer(Context const& context);
107 IndexBuffer(IndexBuffer const& r);
108 IndexBuffer(IndexBuffer&& r) noexcept;
109 ~IndexBuffer() override;
110
111 IndexBuffer& operator=(IndexBuffer const& r);
112 IndexBuffer& operator=(IndexBuffer&& r) noexcept;
113
114 void create(std::size_t indexSize, BufferTypes type = BufferTypes::DEFAULT);
115
116 void clear();
117 void resize(std::size_t indexSize);
118 void append(uint16_t index = std::numeric_limits<uint16_t>::max());
119
120 void destroy() final;
121
122 void bind(CommandBuffer& commandBuffer) const;
123
124 [[nodiscard]] std::size_t getCount() const;
125
126 [[nodiscard]] uint16_t* getIndices();
127 [[nodiscard]] uint16_t const* getIndices() const;
128
129 [[nodiscard]] uint16_t& operator[](std::size_t index);
130 [[nodiscard]] uint16_t const& operator[](std::size_t index) const;
131
132 [[nodiscard]] VkBuffer getIndicesBuffer() const;
133 [[nodiscard]] VmaAllocation getIndicesBufferAllocation() const;
134
135 [[nodiscard]] BufferTypes getType() const;
136
137private:
138 void mapBuffer() const;
139 void cleanBuffer() const;
140 void updateBuffer() const;
141
142 std::vector<uint16_t> g_indices;
143
144 mutable VkBuffer g_buffer;
145 mutable VkBuffer g_stagingBuffer;
146 mutable VmaAllocation g_bufferAllocation;
147 mutable VmaAllocation g_stagingBufferAllocation;
148 mutable std::size_t g_bufferCapacity;
149
150 mutable bool g_needUpdate;
151
152 BufferTypes g_type;
153};
154
155} // namespace fge::vulkan
156
157#endif //_FGE_VULKAN_C_VERTEXBUFFER_HPP_INCLUDED
Vulkan command buffer wrapper.
Definition C_commandBuffer.hpp:36
Definition C_contextAware.hpp:28
Vulkan context.
Definition C_context.hpp:70
Definition C_vertexBuffer.hpp:104
Definition C_vertexBuffer.hpp:45
Definition C_vertex.hpp:29