ezEngine  Milestone 7
RenderContext.h
1 #pragma once
2 
3 #include <RendererCore/Declarations.h>
4 #include <Foundation/Strings/HashedString.h>
5 #include <RendererCore/ShaderCompiler/PermutationGenerator.h>
6 #include <Foundation/Strings/String.h>
7 #include <Foundation/Containers/Map.h>
8 #include <RendererFoundation/Shader/Shader.h>
9 #include <RendererFoundation/Device/Device.h>
10 #include <RendererFoundation/Context/Context.h>
11 #include <Core/ResourceManager/Resource.h>
12 #include <RendererCore/../../../Shared/Data/Shaders/Common/ConstantBufferMacros.h>
13 #include <RendererCore/../../../Shared/Data/Shaders/Common/GlobalConstants.h>
14 #include <RendererCore/Shader/ShaderStageBinary.h>
15 
16 
17 class EZ_RENDERERCORE_DLL ezRenderContext
18 {
19 private:
21  ~ezRenderContext();
22  friend class ezMemoryUtils;
23 
24  static ezRenderContext* s_DefaultInstance;
25  static ezHybridArray<ezRenderContext*, 4> s_Instances;
26 
27 public:
28  static ezRenderContext* GetDefaultInstance();
29  static ezRenderContext* CreateInstance();
30  static void DestroyInstance(ezRenderContext* pRenderer);
31 
32  void SetGALContext(ezGALContext* pContext);
33  ezGALContext* GetGALContext() const { return m_pGALContext; }
34 
35 public:
36  struct Statistics
37  {
38  Statistics();
39  void Reset();
40 
41  ezUInt32 m_uiFailedDrawcalls;
42  };
43 
44 
45  // Member Functions
46  void SetShaderPermutationVariable(const char* szVariable, const char* szValue);
47  void BindTexture(const ezTempHashedString& sSlotName, const ezTextureResourceHandle& hTexture);
48  void SetMaterialState(const ezMaterialResourceHandle& hMaterial);
49  void BindConstantBuffer(const ezTempHashedString& sSlotName, const ezConstantBufferResourceHandle& hConstantBuffer);
50  void BindMeshBuffer(const ezMeshBufferResourceHandle& hMeshBuffer);
51  void DrawMeshBuffer(ezUInt32 uiPrimitiveCount = 0xFFFFFFFF, ezUInt32 uiFirstPrimitive = 0, ezUInt32 uiInstanceCount = 1);
52  ezResult ApplyContextStates(bool bForce = false);
53  Statistics GetAndResetStatistics();
54 
58  void BindShader(ezShaderResourceHandle hShader, ezBitflags<ezShaderBindFlags> flags = ezShaderBindFlags::Default);
59 
62  ezGALShaderHandle GetActiveGALShader();
63 
64  template<typename STRUCT>
65  STRUCT* BeginModifyConstantBuffer(ezConstantBufferResourceHandle hConstantBuffer)
66  {
67  return reinterpret_cast<STRUCT*>(InternalBeginModifyConstantBuffer(hConstantBuffer));
68  }
69 
70  void EndModifyConstantBuffer();
71 
72  // Static Functions
73  static void ConfigureShaderSystem(const char* szActivePlatform, bool bEnableRuntimeCompilation, const char* szShaderCacheDirectory = "ShaderCache", const char* szPermVarSubDirectory = "Shaders/PermutationVars");
74  static const ezString& GetPermutationVarSubDirectory() { return s_sPermVarSubDir; }
75  static const ezString& GetActiveShaderPlatform() { return s_sPlatform; }
76  static const ezString& GetShaderCacheDirectory() { return s_ShaderCacheDirectory; }
77  static bool IsRuntimeShaderCompilationEnabled() { return s_bEnableRuntimeCompilation; }
78 
79 public:
80 
81  static void LoadShaderPermutationVarConfig(const char* szVariable);
82  static const ezPermutationGenerator* GetGeneratorForShaderPermutation(ezUInt32 uiPermutationHash);
83  static void PreloadShaderPermutations(ezShaderResourceHandle hShader, const ezPermutationGenerator& Generator, ezTime tShouldBeAvailableIn);
84  static ezShaderPermutationResourceHandle PreloadSingleShaderPermutation(ezShaderResourceHandle hShader, const ezHybridArray<ezPermutationGenerator::PermutationVar, 16>& UsedPermVars, ezTime tShouldBeAvailableIn);
85 
86  static GlobalConstants& WriteGlobalConstants()
87  {
88  s_bGlobalConstantsModified = true;
89  return s_GlobalConstants;
90  }
91 
92  static const GlobalConstants& ReadGlobalConstants()
93  {
94  return s_GlobalConstants;
95  }
96 
98  {
99  ezUInt64 m_LastModification;
100  ezShaderMaterialParamCB::MaterialParameter::Type m_Type;
101  ezUInt8 m_uiArrayElements;
102  ezUInt16 m_uiDataSize;
103  };
104 
105  static void SetMaterialParameter(const ezTempHashedString& sName, const float& value);
106  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec2& value);
107  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec3& value);
108  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec4& value);
109  static void SetMaterialParameter(const ezTempHashedString& sName, const ezColor& value);
110  static void SetMaterialParameter(const ezTempHashedString& sName, const ezInt32& value);
111  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec2I32& value);
112  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec3I32& value);
113  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVec4I32& value);
114  //static void SetMaterialParameter(const ezTempHashedString& sName, const ezMat3& value); /// \todo ezMat3 does not work right, ezTransform maybe neither
115  static void SetMaterialParameter(const ezTempHashedString& sName, const ezMat4& value);
116  static void SetMaterialParameter(const ezTempHashedString& sName, const ezTransform& value);
117  static void SetMaterialParameter(const ezTempHashedString& sName, const ezVariant& value);
119 
120 
121  static const MaterialParam* GetMaterialParameterPointer(ezUInt32 uiNameHash);
122 
123 private:
124  EZ_MAKE_SUBSYSTEM_STARTUP_FRIEND(Graphics, RendererContext);
125 
126  static void OnEngineShutdown();
127  static void OnCoreShutdown();
128 
129 private:
130 
131  Statistics m_Statistics;
133  ezShaderResourceHandle m_hActiveShader;
134  ezGALShaderHandle m_hActiveGALShader;
135  ezMap<ezString, ezString> m_PermutationVariables;
136  ezPermutationGenerator m_PermGenerator;
137  ezShaderPermutationResourceHandle m_hActiveShaderPermutation;
138  ezConstantBufferResource* m_pCurrentlyModifyingBuffer;
139  ezBitflags<ezShaderBindFlags> m_ShaderBindFlags;
140  ezMeshBufferResourceHandle m_hMeshBuffer;
141  ezUInt32 m_uiMeshBufferPrimitiveCount;
142 
145 
147  {
148  ezGALShaderHandle m_hShader;
149  ezUInt32 m_uiVertexDeclarationHash;
150 
151  EZ_FORCE_INLINE bool operator<(const ShaderVertexDecl& rhs) const
152  {
153  if (m_hShader < rhs.m_hShader)
154  return true;
155  if (rhs.m_hShader < m_hShader)
156  return false;
157  return m_uiVertexDeclarationHash < rhs.m_uiVertexDeclarationHash;
158  }
159 
160  EZ_FORCE_INLINE bool operator==(const ShaderVertexDecl& rhs) const
161  {
162  return (m_hShader == rhs.m_hShader && m_uiVertexDeclarationHash == rhs.m_uiVertexDeclarationHash);
163  }
164  };
165 
166  static ezResult BuildVertexDeclaration(ezGALShaderHandle hShader, const ezVertexDeclarationInfo& decl, ezGALVertexDeclarationHandle& out_Declaration);
167  ezUInt8* InternalBeginModifyConstantBuffer(ezConstantBufferResourceHandle hConstantBuffer);
168 
169  static MaterialParam* InternalSetMaterialParameter(const ezTempHashedString& sName, ezShaderMaterialParamCB::MaterialParameter::Type type, ezUInt32 uiMaxArrayElements);
170 
171  static ezPermutationGenerator s_AllowedPermutations;
172  static bool s_bEnableRuntimeCompilation;
173  static ezString s_sPlatform;
174  static ezString s_sPermVarSubDir;
175  static ezString s_ShaderCacheDirectory;
176  static ezMap<ezUInt32, ezPermutationGenerator> s_PermutationHashCache;
177  static ezMap<ShaderVertexDecl, ezGALVertexDeclarationHandle> s_GALVertexDeclarations;
178  static ezUInt64 s_LastMaterialParamModification;
179 
180  static bool s_bGlobalConstantsModified;
181  static GlobalConstants s_GlobalConstants;
182  static ezConstantBufferResourceHandle s_hGlobalConstantBuffer;
183 
184 private: // Per Renderer States
185  ezGALContext* m_pGALContext;
186 
187  // Member Functions
188  void UploadGlobalConstants();
189  void ApplyTextureBindings(ezGALShaderStage::Enum stage, const ezShaderStageBinary* pBinary);
190  void ApplyConstantBufferBindings(const ezShaderStageBinary* pBinary);
191 };
192