Nameless Engine
Loading...
Searching...
No Matches
MeshNode.h
1#pragma once
2
3// Standard.
4#include <atomic>
5
6// Custom.
7#include "game/node/SpatialNode.h"
8#include "math/GLMath.hpp"
9#include "render/general/resource/GpuResource.h"
10#include "shader/general/resource/binding/ShaderResourceBinding.h"
11#include "shader/general/resource/binding/cpuwrite/ShaderCpuWriteResourceBindingUniquePtr.h"
12#include "shader/general/resource/binding/texture/ShaderTextureResourceBindingUniquePtr.h"
13#include "shader/VulkanAlignmentConstants.hpp"
14#include "misc/shapes/AABB.h"
15#include "material/Material.h"
16#include "render/general/resource/MeshData.h"
17
18#include "MeshNode.generated.h"
19
20namespace ne RNAMESPACE() {
21 class UploadBuffer;
22
28 class RCLASS(Guid("d5407ca4-3c2e-4a5a-9ff3-1262b6a4d264")) MeshNode : public SpatialNode {
29 // Material notifies us when it changes its pipeline.
30 friend class Material;
31
32 public:
39 MeshShaderConstants() = default;
40
42 alignas(iVkMat4Alignment) glm::mat4x4 worldMatrix = glm::identity<glm::mat4x4>();
43
49 alignas(iVkMat4Alignment) glm::mat4x4 normalMatrix = glm::identity<glm::mat4x4>();
50 };
51
53 struct GpuResources {
54 GpuResources() = default;
55
57 struct Mesh {
58 Mesh() = default;
59
61 std::unique_ptr<GpuResource> pVertexBuffer;
62
64 std::vector<std::unique_ptr<GpuResource>> vIndexBuffers;
65 };
66
69 ShaderResources() = default;
70
72 std::unordered_map<std::string, ShaderCpuWriteResourceBindingUniquePtr>
74
76 std::unordered_map<std::string, ShaderTextureResourceBindingUniquePtr> shaderTextureResources;
77 };
78
81
84 };
85
91 static inline const char* getMeshShaderConstantBufferName() { return sMeshShaderConstantBufferName; }
92
93 MeshNode();
94
100 MeshNode(const std::string& sNodeName);
101
102 virtual ~MeshNode() override = default;
103
116 void setMaterial(std::unique_ptr<Material> pMaterial, size_t iMaterialSlot = 0);
117
125 void setMeshData(const MeshData& meshData);
126
134 void setMeshData(MeshData&& meshData);
135
137 void onMeshDataChanged();
138
144 void setIsVisible(bool bVisible);
145
156 Material* getMaterial(size_t iMaterialSlot = 0);
157
164 size_t getAvailableMaterialSlotCount();
165
181 inline std::pair<std::recursive_mutex*, MeshData*> getMeshData() {
182 return std::make_pair(&mtxMeshData, &meshData);
183 }
184
190 inline std::pair<std::recursive_mutex, GpuResources>* getMeshGpuResources() {
191 return &mtxGpuResources;
192 }
193
202 inline std::pair<std::recursive_mutex, MeshShaderConstants>* getMeshShaderConstants() {
203 return &mtxShaderMeshDataConstants;
204 }
205
211 inline AABB* getAABB() { return &aabb; }
212
218 bool isVisible() const;
219
220 protected:
228 virtual void onAfterDeserialized() override;
229
240 virtual void onSpawning() override;
241
251 virtual void onDespawning() override;
252
259 virtual void onWorldLocationRotationScaleChanged() override;
260
288 void setShaderCpuWriteResourceBinding(
289 const std::string& sShaderResourceName,
290 size_t iResourceSizeInBytes,
291 const std::function<void*()>& onStartedUpdatingResource,
292 const std::function<void()>& onFinishedUpdatingResource);
293
305 void setShaderTextureResourceBinding(
306 const std::string& sShaderResourceName, const std::string& sPathToTextureResourceRelativeRes);
307
327 void markShaderCpuWriteResourceToBeCopiedToGpu(const std::string& sShaderResourceName);
328
329 private:
335 static std::unique_ptr<Material> getDefaultMaterial();
336
342 void allocateShaderResources();
343
345 void allocateGeometryBuffers();
346
348 void deallocateShaderResources();
349
351 void deallocateGeometryBuffers();
352
358 void* onStartedUpdatingShaderMeshConstants();
359
361 void onFinishedUpdatingShaderMeshConstants();
362
373 void updateShaderResourcesToUseChangedMaterialPipelines();
374
385 std::pair<GpuResource*, unsigned int> getIndexBufferInfoForMaterialSlot(size_t iMaterialSlot);
386
393 RPROPERTY(Serialize)
394 std::vector<std::unique_ptr<Material>> vMaterials;
395
401 RPROPERTY(Serialize(FST_AS_EXTERNAL_BINARY_FILE)) // allow VCSs to treat this file in a special way
402 MeshData meshData;
403
405 AABB aabb;
406
408 std::recursive_mutex mtxMeshData;
409
411 std::pair<std::recursive_mutex, GpuResources> mtxGpuResources;
412
414 std::pair<std::recursive_mutex, MeshShaderConstants> mtxShaderMeshDataConstants;
415
417 bool bIsVisible = true;
418
420 static inline const auto sMeshShaderConstantBufferName = "meshData";
421
422 ne_MeshNode_GENERATED
423 };
424}
425
426File_MeshNode_GENERATED
Definition: GuidProperty.h:30
Definition: Material.h:63
Definition: MeshData.h:93
Definition: MeshNode.h:28
std::pair< std::recursive_mutex *, MeshData * > getMeshData()
Definition: MeshNode.h:181
std::pair< std::recursive_mutex, GpuResources > * getMeshGpuResources()
Definition: MeshNode.h:190
AABB * getAABB()
Definition: MeshNode.h:211
std::pair< std::recursive_mutex, MeshShaderConstants > * getMeshShaderConstants()
Definition: MeshNode.h:202
static const char * getMeshShaderConstantBufferName()
Definition: MeshNode.h:91
Definition: SerializeProperty.h:42
Definition: SpatialNode.h:12
Definition: AABB.h:14
Definition: MeshNode.h:57
std::unique_ptr< GpuResource > pVertexBuffer
Definition: MeshNode.h:61
std::vector< std::unique_ptr< GpuResource > > vIndexBuffers
Definition: MeshNode.h:64
std::unordered_map< std::string, ShaderTextureResourceBindingUniquePtr > shaderTextureResources
Definition: MeshNode.h:76
std::unordered_map< std::string, ShaderCpuWriteResourceBindingUniquePtr > shaderCpuWriteResourceBindings
Definition: MeshNode.h:73
Definition: MeshNode.h:53
Mesh mesh
Definition: MeshNode.h:80
ShaderResources shaderResources
Definition: MeshNode.h:83
Definition: MeshNode.h:38