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 "io/Serializable.h"
9#include "shader/general/ShaderMacro.h"
10#include "math/GLMath.hpp"
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 "render/general/resource/MeshData.h"
15#include "render/general/pipeline/PipelineSharedPtr.h"
16
17#include "Material.generated.h"
18
19namespace ne RNAMESPACE() {
20 class MeshNode;
21 class GpuResource;
22 class Pipeline;
23 class PipelineManager;
24
32 size_t getTotalSize() const { return visibleMeshNodes.size() + invisibleMeshNodes.size(); }
33
41 bool isMeshNodeAdded(MeshNode* pMeshNode) {
42 auto it = visibleMeshNodes.find(pMeshNode);
43 if (it != visibleMeshNodes.end()) {
44 return true;
45 }
46
47 it = invisibleMeshNodes.find(pMeshNode);
48 if (it != invisibleMeshNodes.end()) {
49 return true;
50 }
51
52 return false;
53 }
54
56 std::unordered_map<MeshNode*, std::vector<MeshIndexBufferInfo>> visibleMeshNodes;
57
59 std::unordered_map<MeshNode*, std::vector<MeshIndexBufferInfo>> invisibleMeshNodes;
60 };
61
63 class RCLASS(Guid("a603fa3a-e9c2-4c38-bb4c-76384ef001f4")) Material : public Serializable {
64 // Mesh node will notify the material when it's spawned/despawned.
65 friend class MeshNode;
66
67 public:
69 struct GpuResources {
70 GpuResources() = default;
71
74 ShaderResources() = default;
75
77 std::unordered_map<std::string, ShaderCpuWriteResourceBindingUniquePtr>
79
81 std::unordered_map<std::string, ShaderTextureResourceBindingUniquePtr> shaderTextureResources;
82 };
83
86 };
87
89 Material();
90
91 Material(const Material&) = delete;
92 Material& operator=(const Material&) = delete;
93
94 virtual ~Material() override;
95
101 static size_t getCurrentAliveMaterialCount();
102
116 static std::variant<std::unique_ptr<Material>, Error> create(
117 const std::string& sVertexShaderName,
118 const std::string& sPixelShaderName,
119 bool bUseTransparency,
120 const std::string& sMaterialName = "Material");
121
127 void setEnableTransparency(bool bEnable);
128
134 void setDiffuseColor(const glm::vec3& diffuseColor);
135
149 void setDiffuseTexture(const std::string& sTextureResourcePathRelativeRes);
150
156 void setSpecularColor(const glm::vec3& specularColor);
157
164 void setRoughness(float roughness);
165
174 void setOpacity(float opacity = 1.0F);
175
181 bool isTransparencyEnabled();
182
188 glm::vec3 getDiffuseColor() const;
189
195 glm::vec3 getSpecularColor() const;
196
203 std::string getPathToDiffuseTextureResource();
204
210 float getRoughness() const;
211
217 float getOpacity() const;
218
225 std::pair<std::mutex, MeshNodesThatUseThisMaterial>* getSpawnedMeshNodesThatUseThisMaterial();
226
232 std::string getMaterialName() const;
233
239 bool isUsingTransparency() const;
240
249 Pipeline* getColorPipeline() const;
250
259 Pipeline* getDepthOnlyPipeline() const;
260
270 Pipeline* getShadowMappingDirectionalSpotPipeline() const;
271
281 Pipeline* getShadowMappingPointPipeline() const;
282
288 inline std::pair<std::recursive_mutex, GpuResources>* getMaterialGpuResources() {
289 return &mtxGpuResources;
290 }
291
297 std::string getVertexShaderName() const;
298
304 std::string getPixelShaderName() const;
305
306 protected:
314 virtual void onAfterDeserialized() override;
315
316 private:
319 InternalResources() = default;
320
328
336
345
353 };
354
361 MaterialShaderConstants() = default;
362
364 alignas(iVkVec4Alignment) glm::vec4 diffuseColor = glm::vec4(1.0F, 1.0F, 1.0F, 1.0F);
365
367 alignas(iVkVec4Alignment) glm::vec4 specularColor = glm::vec4(1.0F, 1.0F, 1.0F, 1.0F);
368
370 alignas(iVkScalarAlignment) float roughness = 0.0F;
371
373 float pad[3];
374 };
375
387 static std::variant<PipelineManager*, Error> getPipelineManagerForNewMaterial(
388 const std::string& sVertexShaderName, const std::string& sPixelShaderName);
389
402 Material(
403 const std::string& sVertexShaderName,
404 const std::string& sPixelShaderName,
405 bool bUseTransparency,
406 PipelineManager* pPipelineManager,
407 const std::string& sMaterialName = "Material");
408
417 void onMeshNodeSpawning(
418 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferToDisplay);
419
429 void onSpawnedMeshNodeStartedUsingMaterial(
430 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferToDisplay);
431
442 void onSpawnedMeshNodeRecreatedIndexBuffer(
443 MeshNode* pMeshNode,
444 const std::pair<GpuResource*, unsigned int>& deletedIndexBuffer,
445 const std::pair<GpuResource*, unsigned int>& newIndexBuffer);
446
455 void onSpawnedMeshNodeChangedVisibility(MeshNode* pMeshNode, bool bOldVisibility);
456
466 void onSpawnedMeshNodeStoppedUsingMaterial(
467 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferDisplayed);
468
477 void onMeshNodeDespawning(
478 MeshNode* pMeshNode, const std::pair<GpuResource*, unsigned int>& indexBufferDisplayed);
479
487 [[nodiscard]] std::optional<Error> initializePipelines();
488
490 void resetPipelines();
491
497 void allocateShaderResources();
498
504 void deallocateShaderResources();
505
532 void setShaderCpuWriteResourceBinding(
533 const std::string& sShaderResourceName,
534 size_t iResourceSizeInBytes,
535 const std::function<void*()>& onStartedUpdatingResource,
536 const std::function<void()>& onFinishedUpdatingResource);
537
549 void setShaderTextureResourceBinding(
550 const std::string& sShaderResourceName, const std::string& sPathToTextureResourceRelativeRes);
551
571 void markShaderCpuWriteResourceAsNeedsUpdate(const std::string& sShaderResourceName);
572
577 void updateToNewPipeline();
578
584 void* onStartUpdatingShaderMeshConstants();
585
587 void onFinishedUpdatingShaderMeshConstants();
588
595 std::set<ShaderMacro> getVertexShaderMacrosForCurrentState();
596
603 std::set<ShaderMacro> getPixelShaderMacrosForCurrentState();
604
609 std::pair<std::mutex, MeshNodesThatUseThisMaterial> mtxSpawnedMeshNodesThatUseThisMaterial;
610
612 std::pair<std::recursive_mutex, InternalResources> mtxInternalResources;
613
615 std::pair<std::recursive_mutex, GpuResources> mtxGpuResources;
616
618 std::pair<std::recursive_mutex, MaterialShaderConstants> mtxShaderMaterialDataConstants;
619
621 PipelineManager* pPipelineManager = nullptr;
622
624 RPROPERTY(Serialize)
625 std::string sVertexShaderName;
626
628 RPROPERTY(Serialize)
629 std::string sPixelShaderName;
630
632 RPROPERTY(Serialize)
633 std::string sMaterialName;
634
639 RPROPERTY(Serialize)
640 std::string sDiffuseTexturePathRelativeRes;
641
643 RPROPERTY(Serialize)
644 glm::vec3 diffuseColor = glm::vec3(1.0F, 1.0F, 1.0F);
645
647 RPROPERTY(Serialize)
648 glm::vec3 specularColor = glm::vec3(1.0F, 1.0F, 1.0F);
649
651 RPROPERTY(Serialize)
652 float roughness = 0.7F;
653
659 RPROPERTY(Serialize)
660 float opacity = 0.6F;
661
663 RPROPERTY(Serialize)
664 bool bUseTransparency = false;
665
667 bool bIsShaderResourcesAllocated = false;
668
670 static inline const auto sMaterialShaderBufferName = "materialData";
671
673 static inline const auto sMaterialShaderDiffuseTextureName = "diffuseTextures";
674
675 ne_Material_GENERATED
676 };
677} // namespace ne RNAMESPACE()
678
679File_Material_GENERATED
Definition: Error.h:27
Definition: GuidProperty.h:30
Definition: Material.h:63
std::pair< std::recursive_mutex, GpuResources > mtxGpuResources
Definition: Material.h:615
std::pair< std::recursive_mutex, GpuResources > * getMaterialGpuResources()
Definition: Material.h:288
std::pair< std::recursive_mutex, MaterialShaderConstants > mtxShaderMaterialDataConstants
Definition: Material.h:618
std::pair< std::mutex, MeshNodesThatUseThisMaterial > mtxSpawnedMeshNodesThatUseThisMaterial
Definition: Material.h:609
std::pair< std::recursive_mutex, InternalResources > mtxInternalResources
Definition: Material.h:612
Definition: MeshNode.h:28
Definition: PipelineManager.h:54
Definition: PipelineSharedPtr.h:15
Definition: Pipeline.h:20
Definition: Serializable.h:113
Definition: SerializeProperty.h:42
std::unordered_map< std::string, ShaderCpuWriteResourceBindingUniquePtr > shaderCpuWriteResources
Definition: Material.h:78
std::unordered_map< std::string, ShaderTextureResourceBindingUniquePtr > shaderTextureResources
Definition: Material.h:81
Definition: Material.h:69
ShaderResources shaderResources
Definition: Material.h:85
Definition: Material.h:318
PipelineSharedPtr pColorPipeline
Definition: Material.h:327
PipelineSharedPtr pShadowMappingPointPipeline
Definition: Material.h:352
PipelineSharedPtr pShadowMappingDirectionalSpotPipeline
Definition: Material.h:344
PipelineSharedPtr pDepthOnlyPipeline
Definition: Material.h:335
Definition: Material.h:360
Definition: Material.h:26
bool isMeshNodeAdded(MeshNode *pMeshNode)
Definition: Material.h:41
size_t getTotalSize() const
Definition: Material.h:32
std::unordered_map< MeshNode *, std::vector< MeshIndexBufferInfo > > visibleMeshNodes
Definition: Material.h:56
std::unordered_map< MeshNode *, std::vector< MeshIndexBufferInfo > > invisibleMeshNodes
Definition: Material.h:59