Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
mesh_data_gpu.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <string>
9#include <memory>
10#include <vector>
12
13namespace rendering_engine
14{
15class IRenderer;
16class IMeshRenderResources;
17class Model;
18
19/**
20 * @brief Types of supported mesh layouts for GPU upload and rendering.
21 */
22enum class MeshType
23{
24 None, ///< Undefined or not yet set.
25 Sprite2D, ///< 2D sprite mesh (typically for UI or simple quads).
26 Billboard, ///< 3D billboard mesh (sprites that face the camera).
27 Surface ///< Full 3D surface mesh (models with normals/tangents, etc.).
28};
29
30/**
31 * @brief Manages mesh data in RAM and GPU, including upload and release operations.
32 *
33 * Supports on-demand composition of different vertex buffer formats and maintains
34 * all intermediate mesh data required for rendering or further processing (e.g., collision).
35 */
37{
38public:
39 /**
40 * @brief Constructs an empty MeshDataGpu object.
41 *
42 * This constructor is typically used when the mesh data will be filled
43 * or generated procedurally before being uploaded to the GPU.
44 *
45 * @param renderer Pointer to the rendering backend interface.
46 */
47 MeshDataGpu(IRenderer* renderer);
48 /**
49 * @brief Construct a MeshDataGpu instance.
50 * @param filename Path to the model file
51 * @param renderer Pointer to the rendering backend interface.
52 */
53 MeshDataGpu(const std::string& filename, IRenderer* renderer);
54
55 /**
56 * @brief Constructs a MeshDataGpu instance from in-memory model data.
57 *
58 * This version loads a model directly from a raw file buffer in memory.
59 * It allows meshes to be created from packed assets, archives, network
60 * transfers, or any other non-filesystem source. After loading the model,
61 * vertex and index data are uploaded to GPU buffers the same way as in
62 * the file-based constructor.
63 *
64 * @param fileBytes Raw contents of the model file.
65 * @param renderer Pointer to the rendering backend interface.
66 */
67 MeshDataGpu(std::vector<uint8_t> const& fileBytes, IRenderer* renderer);
68
69 /**
70 * @brief Destructor. Releases GPU resources if allocated.
71 */
73
74 /**
75 * @brief Upload mesh data to the GPU using the current mesh type.
76 */
77 void UploadToGPU();
78
79 /**
80 * @brief Release GPU resources associated with this mesh.
81 */
82 void ReleaseFromGPU();
83
84 /**
85 * @brief Check if mesh data is currently uploaded to GPU.
86 * @return True if GPU resources are resident, false otherwise.
87 */
88 bool IsOnGPU() const;
89
90 /**
91 * @brief Creates a 1�1 unit quad centered at the origin.
92 *
93 * The quad spans from -0.5 to +0.5 on both X and Y axes and uses
94 * counterclockwise winding. Suitable as a base mesh for sprites
95 * or 2D UI elements.
96 */
97 void CreateQuad2D();
98
99 void LoadCustomMesh(std::vector<glm::vec2> positions2D,
100 std::vector<glm::vec2> texCoords,
101 std::vector<glm::vec4> colors,
102 std::vector<std::uint32_t> indices);
103
104 /**
105 * @brief Get the size (in bytes) of the vertex buffer on GPU.
106 * @return Size in bytes of the GPU vertex buffer.
107 */
108 size_t GetGpuVertexBufferSize() const;
109
110 /**
111 * @brief Get the size (in bytes) of the index buffer on GPU.
112 * @return Size in bytes of the GPU index buffer.
113 */
114 size_t GetGpuIndexBufferSize() const;
115
116 /**
117 * @brief Get the size (in bytes) of the vertex buffer in RAM.
118 * @return Size in bytes of the CPU-side vertex data.
119 */
120 size_t GetCpuVertexBufferSize() const;
121
122 /**
123 * @brief Get the size (in bytes) of the index buffer in RAM.
124 * @return Size in bytes of the CPU-side index data.
125 */
126 size_t GetCpuIndexBufferSize() const;
127
128 /**
129 * @brief Get the interface for mesh GPU resources (Vulkan or other backend).
130 * @return Pointer to the backend-specific IMeshRenderResources implementation.
131 */
133
134 /**
135 * @brief Returns a constant reference to the 2D vertex positions.
136 */
137 const std::vector<glm::vec2>& GetPositions2D() const { return mPositions2D; }
138
139 /**
140 * @brief Returns a constant reference to the 3D vertex positions.
141 */
142 const std::vector<glm::vec3>& GetPositions() const { return mPositions; }
143
144 /**
145 * @brief Returns a constant reference to the vertex colors.
146 */
147 const std::vector<glm::vec4>& GetColors() const { return mColor; }
148
149 /**
150 * @brief Returns a constant reference to the vertex normals.
151 */
152 const std::vector<glm::vec3>& GetNormals() const { return mNormals; }
153
154 /**
155 * @brief Returns a constant reference to the texture coordinates.
156 */
157 const std::vector<glm::vec2>& GetTexCoords() const { return mTexCoords; }
158
159 /**
160 * @brief Returns a constant reference to the vertex tangents.
161 */
162 const std::vector<glm::vec3>& GetTangents() const { return mTangents; }
163
164 /**
165 * @brief Returns a constant reference to the mesh indices.
166 */
167 const std::vector<uint32_t>& GetIndices() const { return mIndices; }
168
169protected:
170 /**
171 * @brief Load a model from file and extract mesh data to RAM.
172 * @param path Path to the model file (e.g., FBX).
173 */
174 void LoadModel(std::string path);
175
176 void LoadModel(std::vector<uint8_t> const& fileBytes);
177 /**
178 * @brief Calculate and cache sizes of RAM buffers for statistics or debugging.
179 */
181
182 /**
183 * @brief Compose a vertex buffer for 2D meshes (e.g., sprites).
184 * @return Vector of Vertex2D structures.
185 */
186 std::vector<Vertex2D> ComposeVertex2DBuffer();
187
188 /**
189 * @brief Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles).
190 * @return Vector of VertexPositionTextureColor structures.
191 */
192 std::vector<VertexPositionColorTexture> ComposeUnlitBuffer();
193
194 /**
195 * @brief Compose a vertex buffer for lit 3D meshes (with normals, tangents).
196 * @return Vector of VertexPositionTextureColorNormalTangent structures.
197 */
198 std::vector<VertexPositionColorTextureNormalTangent> ComposeLitBuffer();
199
200private:
201 std::string mPath;
202 IRenderer* mRenderer;
203
204 MeshType mMeshType;
205
206 std::vector<glm::vec2> mPositions2D;
207 std::vector<glm::vec3> mPositions;
208 std::vector<glm::vec4> mColor;
209 std::vector<glm::vec3> mNormals;
210 std::vector<glm::vec2> mTexCoords;
211 std::vector<glm::vec3> mTangents;
212 std::vector<uint32_t> mIndices;
213
214 size_t mSizeOfVerticesBytes;
215 size_t mSizeOfIndicesBytes;
216
217 std::unique_ptr<IMeshRenderResources> mGpuHandle;
218};
219
220} // namespace rendering_engine
Interface for GPU mesh resource management.
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Manages mesh data in RAM and GPU, including upload and release operations.
size_t GetCpuIndexBufferSize() const
Get the size (in bytes) of the index buffer in RAM.
void CreateQuad2D()
Creates a 1�1 unit quad centered at the origin.
std::vector< VertexPositionColorTextureNormalTangent > ComposeLitBuffer()
Compose a vertex buffer for lit 3D meshes (with normals, tangents).
void CalculateMeshParameters()
Calculate and cache sizes of RAM buffers for statistics or debugging.
const std::vector< uint32_t > & GetIndices() const
Returns a constant reference to the mesh indices.
void LoadModel(std::string path)
Load a model from file and extract mesh data to RAM.
const std::vector< glm::vec3 > & GetTangents() const
Returns a constant reference to the vertex tangents.
size_t GetGpuIndexBufferSize() const
Get the size (in bytes) of the index buffer on GPU.
std::vector< Vertex2D > ComposeVertex2DBuffer()
Compose a vertex buffer for 2D meshes (e.g., sprites).
const std::vector< glm::vec3 > & GetNormals() const
Returns a constant reference to the vertex normals.
const std::vector< glm::vec3 > & GetPositions() const
Returns a constant reference to the 3D vertex positions.
IMeshRenderResources * GetMeshRenderResources()
Get the interface for mesh GPU resources (Vulkan or other backend).
const std::vector< glm::vec2 > & GetTexCoords() const
Returns a constant reference to the texture coordinates.
void UploadToGPU()
Upload mesh data to the GPU using the current mesh type.
void LoadCustomMesh(std::vector< glm::vec2 > positions2D, std::vector< glm::vec2 > texCoords, std::vector< glm::vec4 > colors, std::vector< std::uint32_t > indices)
bool IsOnGPU() const
Check if mesh data is currently uploaded to GPU.
std::vector< VertexPositionColorTexture > ComposeUnlitBuffer()
Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles).
const std::vector< glm::vec4 > & GetColors() const
Returns a constant reference to the vertex colors.
MeshDataGpu(IRenderer *renderer)
Constructs an empty MeshDataGpu object.
size_t GetCpuVertexBufferSize() const
Get the size (in bytes) of the vertex buffer in RAM.
~MeshDataGpu()
Destructor. Releases GPU resources if allocated.
const std::vector< glm::vec2 > & GetPositions2D() const
Returns a constant reference to the 2D vertex positions.
size_t GetGpuVertexBufferSize() const
Get the size (in bytes) of the vertex buffer on GPU.
void ReleaseFromGPU()
Release GPU resources associated with this mesh.
2D drawable component for rendering textured quad.
Definition: sprite_2d.hpp:22
MeshType
Types of supported mesh layouts for GPU upload and rendering.
@ None
Undefined or not yet set.
@ Surface
Full 3D surface mesh (models with normals/tangents, etc.).
@ Billboard
3D billboard mesh (sprites that face the camera).