Nameless Engine
Loading...
Searching...
No Matches
MeshNode.h
1#pragma once
2
3// Standard.
4#include <atomic>
5
6// Custom.
7#include "game/nodes/SpatialNode.h"
8#include "math/GLMath.hpp"
9#include "render/general/resources/GpuResource.h"
10#include "shader/general/resources/ShaderResource.h"
11#include "shader/general/resources/cpuwrite/ShaderCpuWriteResourceUniquePtr.h"
12#include "shader/general/resources/texture/ShaderTextureResourceUniquePtr.h"
13#include "shader/VulkanAlignmentConstants.hpp"
14#include "misc/shapes/AABB.h"
15#include "material/Material.h"
16
17#include "MeshNode.generated.h"
18
19namespace ne RNAMESPACE() {
20 class UploadBuffer;
21
27 struct MeshVertex { // not using inheritance to avoid extra fields that are not related to vertex
28 MeshVertex() = default;
29 ~MeshVertex() = default;
30
32 MeshVertex(const MeshVertex&) = default;
33
39 MeshVertex& operator=(const MeshVertex&) = default;
40
42 MeshVertex(MeshVertex&&) noexcept = default;
43
49 MeshVertex& operator=(MeshVertex&&) noexcept = default;
50
58 bool operator==(const MeshVertex& other) const;
59
60 // --------------------------------------------------------------------------------------
61
63 glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
64
66 glm::vec3 normal = glm::vec3(0.0f, 0.0f, 0.0f);
67
69 glm::vec2 uv = glm::vec2(0.0f, 0.0f);
70
71 // --------------------------------------------------------------------------------------
72
73 // ! only vertex related fields (same as in shader) can be added here !
74 // (not deriving from `Serializable` to avoid extra fields that are not related to vertex)
75
76 // --------------------------------------------------------------------------------------
77 };
78
80 class RCLASS(Guid("b60e4b47-b1e6-4001-87a8-b7885b4e8383")) MeshData : public Serializable {
81 public:
83 using meshindex_t = unsigned int; // if making this dynamic (changes depending on the number of
84 // indices) change hardcoded FORMAT in the renderer
85
86 MeshData();
87 virtual ~MeshData() override = default;
88
90 MeshData(const MeshData&) = default;
91
97 MeshData& operator=(const MeshData&) = default;
98
100 MeshData(MeshData&&) noexcept = default;
101
107 MeshData& operator=(MeshData&&) noexcept = default;
108
114 std::vector<MeshVertex>* getVertices();
115
123 std::vector<std::vector<meshindex_t>>* getIndices();
124
125 private:
127 RPROPERTY(Serialize)
128 std::vector<MeshVertex> vVertices;
129
137 RPROPERTY(Serialize)
138 std::vector<std::vector<meshindex_t>> vIndices;
139
140 ne_MeshData_GENERATED
141 };
142
148 class RCLASS(Guid("d5407ca4-3c2e-4a5a-9ff3-1262b6a4d264")) MeshNode : public SpatialNode {
149 // Material notifies us when it changes its pipeline.
150 friend class Material;
151
152 public:
159 MeshShaderConstants() = default;
160
162 alignas(iVkMat4Alignment) glm::mat4x4 worldMatrix = glm::identity<glm::mat4x4>();
163
169 alignas(iVkMat4Alignment) glm::mat4x4 normalMatrix = glm::identity<glm::mat4x4>();
170 };
171
174 GpuResources() = default;
175
177 struct Mesh {
178 Mesh() = default;
179
181 std::unique_ptr<GpuResource> pVertexBuffer;
182
184 std::vector<std::unique_ptr<GpuResource>> vIndexBuffers;
185 };
186
189 ShaderResources() = default;
190
196 std::unordered_map<std::string, ShaderCpuWriteResourceUniquePtr> shaderCpuWriteResources;
197
199 std::unordered_map<std::string, ShaderTextureResourceUniquePtr> shaderTextureResources;
200 };
201
204
207 };
208
214 static inline const char* getMeshShaderConstantBufferName() { return sMeshShaderConstantBufferName; }
215
216 MeshNode();
217
223 MeshNode(const std::string& sNodeName);
224
225 virtual ~MeshNode() override = default;
226
239 void setMaterial(std::unique_ptr<Material> pMaterial, size_t iMaterialSlot = 0);
240
248 void setMeshData(const MeshData& meshData);
249
257 void setMeshData(MeshData&& meshData);
258
260 void onMeshDataChanged();
261
267 void setIsVisible(bool bVisible);
268
279 Material* getMaterial(size_t iMaterialSlot = 0);
280
287 size_t getAvailableMaterialSlotCount();
288
304 inline std::pair<std::recursive_mutex*, MeshData*> getMeshData() {
305 return std::make_pair(&mtxMeshData, &meshData);
306 }
307
313 inline std::pair<std::recursive_mutex, GpuResources>* getMeshGpuResources() {
314 return &mtxGpuResources;
315 }
316
325 inline std::pair<std::recursive_mutex, MeshShaderConstants>* getMeshShaderConstants() {
326 return &mtxShaderMeshDataConstants;
327 }
328
334 inline AABB* getAABB() { return &aabb; }
335
341 bool isVisible() const;
342
343 protected:
351 virtual void onAfterDeserialized() override;
352
363 virtual void onSpawning() override;
364
374 virtual void onDespawning() override;
375
382 virtual void onWorldLocationRotationScaleChanged() override;
383
411 void setShaderCpuWriteResourceBindingData(
412 const std::string& sShaderResourceName,
413 size_t iResourceSizeInBytes,
414 const std::function<void*()>& onStartedUpdatingResource,
415 const std::function<void()>& onFinishedUpdatingResource);
416
428 void setShaderTextureResourceBindingData(
429 const std::string& sShaderResourceName, const std::string& sPathToTextureResourceRelativeRes);
430
450 void markShaderCpuWriteResourceToBeCopiedToGpu(const std::string& sShaderResourceName);
451
452 private:
458 static std::unique_ptr<Material> getDefaultMaterial();
459
465 void allocateShaderResources();
466
468 void allocateGeometryBuffers();
469
471 void deallocateShaderResources();
472
474 void deallocateGeometryBuffers();
475
481 void* onStartedUpdatingShaderMeshConstants();
482
484 void onFinishedUpdatingShaderMeshConstants();
485
496 void updateShaderResourcesToUseChangedMaterialPipelines();
497
508 std::pair<GpuResource*, unsigned int> getIndexBufferInfoForMaterialSlot(size_t iMaterialSlot);
509
516 RPROPERTY(Serialize)
517 std::vector<std::unique_ptr<Material>> vMaterials;
518
524 RPROPERTY(Serialize(FST_AS_EXTERNAL_BINARY_FILE)) // allow VCSs to treat this file in a special way
525 MeshData meshData;
526
528 AABB aabb;
529
531 std::recursive_mutex mtxMeshData;
532
534 std::pair<std::recursive_mutex, GpuResources> mtxGpuResources;
535
537 std::pair<std::recursive_mutex, MeshShaderConstants> mtxShaderMeshDataConstants;
538
540 bool bIsVisible = true;
541
543 static inline const auto sMeshShaderConstantBufferName = "meshData";
544
545 ne_MeshNode_GENERATED
546 };
547} // namespace ne RNAMESPACE()
548
549File_MeshNode_GENERATED
Definition: GuidProperty.h:30
Definition: Material.h:84
Definition: MeshNode.h:80
MeshData(const MeshData &)=default
MeshData & operator=(const MeshData &)=default
unsigned int meshindex_t
Definition: MeshNode.h:83
MeshData(MeshData &&) noexcept=default
Definition: MeshNode.h:148
std::pair< std::recursive_mutex *, MeshData * > getMeshData()
Definition: MeshNode.h:304
std::pair< std::recursive_mutex, GpuResources > * getMeshGpuResources()
Definition: MeshNode.h:313
AABB * getAABB()
Definition: MeshNode.h:334
std::pair< std::recursive_mutex, MeshShaderConstants > * getMeshShaderConstants()
Definition: MeshNode.h:325
static const char * getMeshShaderConstantBufferName()
Definition: MeshNode.h:214
Definition: Serializable.h:113
Definition: SerializeProperty.h:42
Definition: SpatialNode.h:12
Definition: AABB.h:14
Definition: MeshNode.h:177
std::unique_ptr< GpuResource > pVertexBuffer
Definition: MeshNode.h:181
std::vector< std::unique_ptr< GpuResource > > vIndexBuffers
Definition: MeshNode.h:184
std::unordered_map< std::string, ShaderTextureResourceUniquePtr > shaderTextureResources
Definition: MeshNode.h:199
std::unordered_map< std::string, ShaderCpuWriteResourceUniquePtr > shaderCpuWriteResources
Definition: MeshNode.h:196
Definition: MeshNode.h:173
Mesh mesh
Definition: MeshNode.h:203
ShaderResources shaderResources
Definition: MeshNode.h:206
Definition: MeshNode.h:158
Definition: MeshNode.h:27
glm::vec3 normal
Definition: MeshNode.h:66
glm::vec2 uv
Definition: MeshNode.h:69
MeshVertex(MeshVertex &&) noexcept=default
MeshVertex & operator=(const MeshVertex &)=default
glm::vec3 position
Definition: MeshNode.h:63
MeshVertex(const MeshVertex &)=default