Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
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) 2025 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;
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 /**
100 * @brief Get the size (in bytes) of the vertex buffer on GPU.
101 * @return Size in bytes of the GPU vertex buffer.
102 */
103 size_t GetGpuVertexBufferSize() const;
104
105 /**
106 * @brief Get the size (in bytes) of the index buffer on GPU.
107 * @return Size in bytes of the GPU index buffer.
108 */
109 size_t GetGpuIndexBufferSize() const;
110
111 /**
112 * @brief Get the size (in bytes) of the vertex buffer in RAM.
113 * @return Size in bytes of the CPU-side vertex data.
114 */
115 size_t GetCpuVertexBufferSize() const;
116
117 /**
118 * @brief Get the size (in bytes) of the index buffer in RAM.
119 * @return Size in bytes of the CPU-side index data.
120 */
121 size_t GetCpuIndexBufferSize() const;
122
123 /**
124 * @brief Get the interface for mesh GPU resources (Vulkan or other backend).
125 * @return Pointer to the backend-specific IMeshRenderResources implementation.
126 */
128
129 /**
130 * @brief Returns a constant reference to the 2D vertex positions.
131 */
132 const std::vector<glm::vec2>& GetPositions2D() const { return mPositions2D; }
133
134 /**
135 * @brief Returns a constant reference to the 3D vertex positions.
136 */
137 const std::vector<glm::vec3>& GetPositions() const { return mPositions; }
138
139 /**
140 * @brief Returns a constant reference to the vertex colors.
141 */
142 const std::vector<glm::vec4>& GetColors() const { return mColor; }
143
144 /**
145 * @brief Returns a constant reference to the vertex normals.
146 */
147 const std::vector<glm::vec3>& GetNormals() const { return mNormals; }
148
149 /**
150 * @brief Returns a constant reference to the texture coordinates.
151 */
152 const std::vector<glm::vec2>& GetTexCoords() const { return mTexCoords; }
153
154 /**
155 * @brief Returns a constant reference to the vertex tangents.
156 */
157 const std::vector<glm::vec3>& GetTangents() const { return mTangents; }
158
159 /**
160 * @brief Returns a constant reference to the mesh indices.
161 */
162 const std::vector<uint32_t>& GetIndices() const { return mIndices; }
163
164protected:
165 /**
166 * @brief Load a model from file and extract mesh data to RAM.
167 * @param path Path to the model file (e.g., FBX).
168 */
169 void LoadModel(std::string path);
170
171 void LoadModel(std::vector<uint8_t> const& fileBytes);
172 /**
173 * @brief Calculate and cache sizes of RAM buffers for statistics or debugging.
174 */
176
177 /**
178 * @brief Compose a vertex buffer for 2D meshes (e.g., sprites).
179 * @return Vector of Vertex2D structures.
180 */
181 std::vector<Vertex2D> ComposeVertex2DBuffer();
182
183 /**
184 * @brief Compose a vertex buffer for unlit 3D meshes (e.g., billboards, particles).
185 * @return Vector of VertexPositionTextureColor structures.
186 */
187 std::vector<VertexPositionColorTexture> ComposeUnlitBuffer();
188
189 /**
190 * @brief Compose a vertex buffer for lit 3D meshes (with normals, tangents).
191 * @return Vector of VertexPositionTextureColorNormalTangent structures.
192 */
193 std::vector<VertexPositionColorTextureNormalTangent> ComposeLitBuffer();
194
195private:
196 std::string mPath;
197 IRenderer* mRenderer;
198
199 MeshType mMeshType;
200
201 std::vector<glm::vec2> mPositions2D;
202 std::vector<glm::vec3> mPositions;
203 std::vector<glm::vec4> mColor;
204 std::vector<glm::vec3> mNormals;
205 std::vector<glm::vec2> mTexCoords;
206 std::vector<glm::vec3> mTangents;
207 std::vector<uint32_t> mIndices;
208
209 size_t mSizeOfVerticesBytes;
210 size_t mSizeOfIndicesBytes;
211
212 std::unique_ptr<IMeshRenderResources> mGpuHandle;
213};
214
215} // namespace rendering_engine
Interface for GPU mesh resource management.
Defines an abstract interface for rendering backends.
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.
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.
Represents a 3D model composed of multiple meshes and materials.
Definition model.hpp:30
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).