Plasma Engine  2.0
Loading...
Searching...
No Matches
MaterialResource.h
1#pragma once
2
3#include <Core/ResourceManager/Resource.h>
4#include <Foundation/Containers/HashTable.h>
5#include <Foundation/Strings/HashedString.h>
6#include <RendererCore/Declarations.h>
7#include <RendererCore/Pipeline/RenderData.h>
8#include <RendererCore/Shader/ConstantBufferStorage.h>
9#include <RendererCore/Shader/ShaderResource.h>
10
14
16{
17 struct Parameter
18 {
19 plHashedString m_Name;
20 plVariant m_Value;
21
22 PL_FORCE_INLINE bool operator==(const Parameter& other) const { return m_Name == other.m_Name && m_Value == other.m_Value; }
23 };
24
26 {
27 plHashedString m_Name;
29
30 PL_FORCE_INLINE bool operator==(const Texture2DBinding& other) const { return m_Name == other.m_Name && m_Value == other.m_Value; }
31 };
32
34 {
35 plHashedString m_Name;
37
38 PL_FORCE_INLINE bool operator==(const TextureCubeBinding& other) const { return m_Name == other.m_Name && m_Value == other.m_Value; }
39 };
40
41 void Clear();
42
43 bool operator==(const plMaterialResourceDescriptor& other) const;
44 PL_FORCE_INLINE bool operator!=(const plMaterialResourceDescriptor& other) const { return !(*this == other); }
45
46 plMaterialResourceHandle m_hBaseMaterial;
47 // plSurfaceResource is not linked into this project (not true anymore -> could be changed)
48 // this is not used for game purposes but rather for automatic collision mesh generation, so we only store the asset ID here
49 plHashedString m_sSurface;
50 plShaderResourceHandle m_hShader;
51 plDynamicArray<plPermutationVar> m_PermutationVars;
52 plDynamicArray<Parameter> m_Parameters;
53 plDynamicArray<Texture2DBinding> m_Texture2DBindings;
54 plDynamicArray<TextureCubeBinding> m_TextureCubeBindings;
55 plRenderData::Category m_RenderDataCategory;
56};
57
58class PL_RENDERERCORE_DLL plMaterialResource final : public plResource
59{
60 PL_ADD_DYNAMIC_REFLECTION(plMaterialResource, plResource);
61 PL_RESOURCE_DECLARE_COMMON_CODE(plMaterialResource);
62 PL_RESOURCE_DECLARE_CREATEABLE(plMaterialResource, plMaterialResourceDescriptor);
63
64public:
67
68 plHashedString GetPermutationValue(const plTempHashedString& sName);
69 plHashedString GetSurface() const;
70
71 void SetParameter(const plHashedString& sName, const plVariant& value);
72 void SetParameter(const char* szName, const plVariant& value);
73 plVariant GetParameter(const plTempHashedString& sName);
74
75 void SetTexture2DBinding(const plHashedString& sName, const plTexture2DResourceHandle& value);
76 void SetTexture2DBinding(const char* szName, const plTexture2DResourceHandle& value);
77 plTexture2DResourceHandle GetTexture2DBinding(const plTempHashedString& sName);
78
79 void SetTextureCubeBinding(const plHashedString& sName, const plTextureCubeResourceHandle& value);
80 void SetTextureCubeBinding(const char* szName, const plTextureCubeResourceHandle& value);
81 plTextureCubeResourceHandle GetTextureCubeBinding(const plTempHashedString& sName);
82
83 plRenderData::Category GetRenderDataCategory();
84
86 void PreserveCurrentDesc();
87 virtual void ResetResource() override;
88
89 const plMaterialResourceDescriptor& GetCurrentDesc() const;
90
93 {
94 Fullbright,
95 FullbrightAlphaTest,
96 Lit,
97 LitAlphaTest,
98 Sky,
99 MissingMaterial
100 };
101
103 static const char* GetDefaultMaterialFileName(DefaultMaterialType materialType);
104
105private:
106 virtual plResourceLoadDesc UnloadData(Unload WhatToUnload) override;
107 virtual plResourceLoadDesc UpdateContent(plStreamReader* Stream) override;
108 virtual void UpdateMemoryUsage(MemoryUsage& out_NewMemoryUsage) override;
109
110private:
111 plMaterialResourceDescriptor m_mOriginalDesc; // stores the state at loading, such that SetParameter etc. calls can be reset later
113
114 friend class plRenderContext;
115 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(RendererCore, MaterialResource);
116
118 void OnBaseMaterialModified(const plMaterialResource* pModifiedMaterial);
119 void OnResourceEvent(const plResourceEvent& resourceEvent);
120
121 void AddPermutationVar(plStringView sName, plStringView sValue);
122
123 plAtomicInteger32 m_iLastModified;
124 plAtomicInteger32 m_iLastConstantsModified;
125 plInt32 m_iLastUpdated;
126 plInt32 m_iLastConstantsUpdated;
127
128 bool IsModified();
129 bool AreConstantsModified();
130
131 void UpdateConstantBuffer(plShaderPermutationResource* pShaderPermutation);
132
133 plConstantBufferStorageHandle m_hConstantBufferStorage;
134
135 struct CachedValues
136 {
137 plShaderResourceHandle m_hShader;
142 plRenderData::Category m_RenderDataCategory;
143
144 void Reset();
145 };
146
147 plUInt32 m_uiCacheIndex;
148 CachedValues* m_pCachedValues;
149
150 CachedValues* GetOrUpdateCachedValues();
151 static CachedValues* AllocateCache(plUInt32& inout_uiCacheIndex);
152 static void DeallocateCache(plUInt32 uiCacheIndex);
153
154 plMutex m_UpdateCacheMutex;
155 static plDeque<plMaterialResource::CachedValues> s_CachedValues;
156
157 static void ClearCache();
158};
Definition ConstantBufferStorage.h:54
Definition Deque.h:270
Definition DynamicArray.h:81
Definition Event.h:177
Definition HashTable.h:333
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Definition MaterialResource.h:59
DefaultMaterialType
Use these enum values together with GetDefaultMaterialFileName() to get the default file names for th...
Definition MaterialResource.h:93
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Definition RenderContext.h:30
The base class for all resources.
Definition Resource.h:10
virtual plResourceLoadDesc UnloadData(Unload WhatToUnload)=0
Requests the resource to unload another quality level. If bFullUnload is true, the resource should un...
virtual void ResetResource()
If the resource has modifications from the original state, it should reset itself to that state now (...
Definition Resource.h:128
virtual plResourceLoadDesc UpdateContent(plStreamReader *pStream)=0
Called whenever more data for the resource is available. The resource must read the stream to update ...
virtual void UpdateMemoryUsage(MemoryUsage &out_NewMemoryUsage)=0
This function must be overridden by all resource types.
Definition ShaderPermutationResource.h:18
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
Definition MaterialResource.h:18
Definition MaterialResource.h:26
Definition MaterialResource.h:34
Definition MaterialResource.h:16
Definition RenderData.h:19
These events may be sent by a specific plResource or by the plResourceManager.
Definition Declarations.h:22
Describes in which loading state a resource currently is, and how many different quality levels there...
Definition Declarations.h:102