Nameless Engine
Loading...
Searching...
No Matches
Material.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <unordered_map>
6
7// Custom.
8#include "render/general/pipeline/PipelineManager.h"
9#include "io/Serializable.h"
10#include "shader/general/ShaderMacro.h"
11#include "math/GLMath.hpp"
12#include "shader/general/resources/cpuwrite/ShaderCpuWriteResourceUniquePtr.h"
13#include "shader/general/resources/texture/ShaderTextureResourceUniquePtr.h"
14#include "shader/VulkanAlignmentConstants.hpp"
15#include "material/TextureManager.h"
16
17#include "Material.generated.h"
18
19namespace ne RNAMESPACE() {
20 class MeshNode;
21 class GpuResource;
22
27
35 this->pIndexBuffer = pIndexBuffer;
36 this->iIndexCount = iIndexCount;
37 }
38
41
43 unsigned int iIndexCount = 0;
44 };
45
53 size_t getTotalSize() const { return visibleMeshNodes.size() + invisibleMeshNodes.size(); }
54
62 bool isMeshNodeAdded(MeshNode* pMeshNode) {
63 auto it = visibleMeshNodes.find(pMeshNode);
64 if (it != visibleMeshNodes.end()) {
65 return true;
66 }
67
68 it = invisibleMeshNodes.find(pMeshNode);
69 if (it != invisibleMeshNodes.end()) {
70 return true;
71 }
72
73 return false;
74 }
75
77 std::unordered_map<MeshNode*, std::vector<MeshIndexBufferInfo>> visibleMeshNodes;
78
80 std::unordered_map<MeshNode*, std::vector<MeshIndexBufferInfo>> invisibleMeshNodes;
81 };
82
84 class RCLASS(Guid("a603fa3a-e9c2-4c38-bb4c-76384ef001f4")) Material : public Serializable {
85 // Mesh node will notify the material when it's spawned/despawned.
86 friend class MeshNode;
87
88 public:
90 struct GpuResources {
91 GpuResources() = default;
92
95 ShaderResources() = default;
96
98 std::unordered_map<std::string, ShaderCpuWriteResourceUniquePtr> shaderCpuWriteResources;
99
101 std::unordered_map<std::string, ShaderTextureResourceUniquePtr> shaderTextureResources;
102 };
103
106 };
107
109 Material();
110
111 Material(const Material&) = delete;
112 Material& operator=(const Material&) = delete;
113
114 virtual ~Material() override;
115
121 static size_t getCurrentAliveMaterialCount();
122
136 static std::variant<std::unique_ptr<Material>, Error> create(
137 const std::string& sVertexShaderName,
138 const std::string& sPixelShaderName,
139 bool bUseTransparency,
140 const std::string& sMaterialName = "Material");
141
147 void setEnableTransparency(bool bEnable);
148
154 void setDiffuseColor(const glm::vec3& diffuseColor);
155
169 void setDiffuseTexture(const std::string& sTextureResourcePathRelativeRes);
170
176 void setSpecularColor(const glm::vec3& specularColor);
177
184 void setRoughness(float roughness);
185
194 void setOpacity(float opacity = 1.0F);
195
201 bool isTransparencyEnabled();
202
208 glm::vec3 getDiffuseColor() const;
209
215 glm::vec3 getSpecularColor() const;
216
223 std::string getPathToDiffuseTextureResource();
224
230 float getRoughness() const;
231
237 float getOpacity() const;
238
245 std::pair<std::mutex, MeshNodesThatUseThisMaterial>* getSpawnedMeshNodesThatUseThisMaterial();
246
252 std::string getMaterialName() const;
253
259 bool isUsingTransparency() const;
260
269 Pipeline* getColorPipeline() const;
270
279 Pipeline* getDepthOnlyPipeline() const;
280
290 Pipeline* getShadowMappingDirectionalSpotPipeline() const;
291
301 Pipeline* getShadowMappingPointPipeline() const;
302
308 inline std::pair<std::recursive_mutex, GpuResources>* getMaterialGpuResources() {
309 return &mtxGpuResources;
310 }
311
317 std::string getVertexShaderName() const;
318
324 std::string getPixelShaderName() const;
325
326 protected:
334 virtual void onAfterDeserialized() override;
335
336 private:
339 InternalResources() = default;
340
348
356
365
373 };
374
381 MaterialShaderConstants() = default;
382
384 alignas(iVkVec4Alignment) glm::vec4 diffuseColor = glm::vec4(1.0F, 1.0F, 1.0F, 1.0F);
385
387 alignas(iVkVec4Alignment) glm::vec4 specularColor = glm::vec4(1.0F, 1.0F, 1.0F, 1.0F);
388
390 alignas(iVkScalarAlignment) float roughness = 0.0F;
391 };
392
404 static std::variant<PipelineManager*, Error> getPipelineManagerForNewMaterial(
405 const std::string& sVertexShaderName, const std::string& sPixelShaderName);
406
419 Material(
420 const std::string& sVertexShaderName,
421 const std::string& sPixelShaderName,
422 bool bUseTransparency,
423 PipelineManager* pPipelineManager,
424 const std::string& sMaterialName = "Material");
425
434 void onMeshNodeSpawning(
435 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferToDisplay);
436
446 void onSpawnedMeshNodeStartedUsingMaterial(
447 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferToDisplay);
448
459 void onSpawnedMeshNodeRecreatedIndexBuffer(
460 MeshNode* pMeshNode,
461 const std::pair<GpuResource*, unsigned int>& deletedIndexBuffer,
462 const std::pair<GpuResource*, unsigned int>& newIndexBuffer);
463
472 void onSpawnedMeshNodeChangedVisibility(MeshNode* pMeshNode, bool bOldVisibility);
473
483 void onSpawnedMeshNodeStoppedUsingMaterial(
484 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferDisplayed);
485
494 void onMeshNodeDespawning(
495 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferDisplayed);
496
504 [[nodiscard]] std::optional<Error> initializePipelines();
505
507 void resetPipelines();
508
514 void allocateShaderResources();
515
521 void deallocateShaderResources();
522
549 void setShaderCpuWriteResourceBindingData(
550 const std::string& sShaderResourceName,
551 size_t iResourceSizeInBytes,
552 const std::function<void*()>& onStartedUpdatingResource,
553 const std::function<void()>& onFinishedUpdatingResource);
554
566 void setShaderTextureResourceBindingData(
567 const std::string& sShaderResourceName, const std::string& sPathToTextureResourceRelativeRes);
568
588 void markShaderCpuWriteResourceAsNeedsUpdate(const std::string& sShaderResourceName);
589
594 void updateToNewPipeline();
595
601 void* onStartUpdatingShaderMeshConstants();
602
604 void onFinishedUpdatingShaderMeshConstants();
605
612 std::set<ShaderMacro> getVertexShaderMacrosForCurrentState();
613
620 std::set<ShaderMacro> getPixelShaderMacrosForCurrentState();
621
626 std::pair<std::mutex, MeshNodesThatUseThisMaterial> mtxSpawnedMeshNodesThatUseThisMaterial;
627
629 std::pair<std::recursive_mutex, InternalResources> mtxInternalResources;
630
632 std::pair<std::recursive_mutex, GpuResources> mtxGpuResources;
633
635 std::pair<std::recursive_mutex, MaterialShaderConstants> mtxShaderMaterialDataConstants;
636
638 PipelineManager* pPipelineManager = nullptr;
639
641 RPROPERTY(Serialize)
642 std::string sVertexShaderName;
643
645 RPROPERTY(Serialize)
646 std::string sPixelShaderName;
647
649 RPROPERTY(Serialize)
650 std::string sMaterialName;
651
656 RPROPERTY(Serialize)
657 std::string sDiffuseTexturePathRelativeRes;
658
660 RPROPERTY(Serialize)
661 glm::vec3 diffuseColor = glm::vec3(1.0F, 1.0F, 1.0F);
662
664 RPROPERTY(Serialize)
665 glm::vec3 specularColor = glm::vec3(1.0F, 1.0F, 1.0F);
666
668 RPROPERTY(Serialize)
669 float roughness = 0.7F;
670
676 RPROPERTY(Serialize)
677 float opacity = 0.6F;
678
680 RPROPERTY(Serialize)
681 bool bUseTransparency = false;
682
684 bool bIsShaderResourcesAllocated = false;
685
687 static inline const auto sMaterialShaderConstantBufferName = "materialData";
688
690 static inline const auto sMaterialShaderDiffuseTextureName = "diffuseTexture";
691
692 ne_Material_GENERATED
693 };
694} // namespace ne RNAMESPACE()
695
696File_Material_GENERATED
Definition: Error.h:27
Definition: GpuResource.h:14
Definition: GuidProperty.h:30
Definition: Material.h:84
std::pair< std::recursive_mutex, GpuResources > mtxGpuResources
Definition: Material.h:632
std::pair< std::recursive_mutex, GpuResources > * getMaterialGpuResources()
Definition: Material.h:308
std::pair< std::recursive_mutex, MaterialShaderConstants > mtxShaderMaterialDataConstants
Definition: Material.h:635
std::pair< std::mutex, MeshNodesThatUseThisMaterial > mtxSpawnedMeshNodesThatUseThisMaterial
Definition: Material.h:626
std::pair< std::recursive_mutex, InternalResources > mtxInternalResources
Definition: Material.h:629
Definition: MeshNode.h:148
Definition: PipelineManager.h:89
Definition: PipelineSharedPtr.h:15
Definition: Pipeline.h:20
Definition: Serializable.h:113
Definition: SerializeProperty.h:42
std::unordered_map< std::string, ShaderCpuWriteResourceUniquePtr > shaderCpuWriteResources
Definition: Material.h:98
std::unordered_map< std::string, ShaderTextureResourceUniquePtr > shaderTextureResources
Definition: Material.h:101
Definition: Material.h:90
ShaderResources shaderResources
Definition: Material.h:105
Definition: Material.h:338
PipelineSharedPtr pColorPipeline
Definition: Material.h:347
PipelineSharedPtr pShadowMappingPointPipeline
Definition: Material.h:372
PipelineSharedPtr pShadowMappingDirectionalSpotPipeline
Definition: Material.h:364
PipelineSharedPtr pDepthOnlyPipeline
Definition: Material.h:355
Definition: Material.h:380
Definition: Material.h:24
MeshIndexBufferInfo(GpuResource *pIndexBuffer, unsigned int iIndexCount)
Definition: Material.h:34
GpuResource * pIndexBuffer
Definition: Material.h:40
unsigned int iIndexCount
Definition: Material.h:43
Definition: Material.h:47
bool isMeshNodeAdded(MeshNode *pMeshNode)
Definition: Material.h:62
size_t getTotalSize() const
Definition: Material.h:53
std::unordered_map< MeshNode *, std::vector< MeshIndexBufferInfo > > visibleMeshNodes
Definition: Material.h:77
std::unordered_map< MeshNode *, std::vector< MeshIndexBufferInfo > > invisibleMeshNodes
Definition: Material.h:80