RavEngine
Loading...
Searching...
No Matches
Material.hpp
1#pragma once
2#if !RVE_SERVER
3#include "Ref.hpp"
4#include "WeakRef.hpp"
5#include "DataStructures.hpp"
6#include "Common3D.hpp"
7#include "CTTI.hpp"
8#include "Manager.hpp"
9#include <RGL/Types.hpp>
10#include <RGL/Pipeline.hpp>
11#include <RGL/Span.hpp>
12#include <span>
13#include <variant>
14#include "MaterialShared.hpp"
15
16namespace RavEngine {
17 struct Texture;
18
21 RGL::RenderPipelineDescriptor::ColorBlendConfig colorBlendConfig, depthPrepassColorBlendConfig;
22 bool depthTestEnabled = true;
23 bool depthWriteEnabled = true;
24 RGL::DepthCompareFunction depthCompareFunction = RGL::DepthCompareFunction::Greater;
25
26 std::vector<RGL::PipelineLayoutDescriptor::LayoutBindingDesc> bindings;
27 uint32_t pushConstantSize = 0;
28 RGL::CullMode cullMode = RGL::CullMode::Back;
29 bool verbatimConfig = false; // used for Skybox
30 OpacityMode opacityMode = OpacityMode::Opaque;
31
32 MeshAttributes requiredAttributes;
33 };
34
35
36
38 RGLRenderPipelinePtr pipeline;
39 MeshAttributes attributes;
40 };
41
45 class Material : public AutoCTTI {
46 public:
47 friend class MaterialInstance;
52 virtual ~Material();
53
57 class Manager {
58 public:
59
64 template<typename T, typename ... A>
65 static inline Ref<T> Get(A&& ... args){
67 }
68
69 template<typename T, typename ... A>
70 static inline Ref<T> GetWithKey(RavEngine::CacheBase::unique_key_t key, A&& ... args) {
72 }
73
77 template<typename T>
81 };
82
83 auto GetMainRenderPipeline() const{
84 return renderPipeline;
85 }
86
87 auto GetShadowRenderPipeline() const{
88 return shadowRenderPipeline;
89 }
90
91 auto GetDepthPrepassPipeline() const {
92 return depthPrepassPipeline;
93 }
94
95 MeshAttributes GetAttributes() const {
96 return requiredAttributes;
97 }
98
99 protected:
100 RGLRenderPipelinePtr renderPipeline, shadowRenderPipeline, depthPrepassPipeline;
101 RGLPipelineLayoutPtr pipelineLayout;
102 OpacityMode opacityMode;
103 const MeshAttributes requiredAttributes;
104
105
106
107 bool IsTransparent() const {
108 return opacityMode == OpacityMode::Transparent;
109 }
110
111 Material(const std::string_view name, const MaterialConfig& config);
112 Material(const std::string_view vsh_name, const std::string_view fsh_name, const MaterialConfig& config);
113
114 friend class RenderEngine;
115 };
116
117
119 RGL::CullMode cullMode = RGL::CullMode::Back;
120 OpacityMode opacityMode = OpacityMode::Opaque;
121 MeshAttributes requiredAttributes{
122 .position = true,
123 .normal = true,
124 .tangent = true,
125 .bitangent = true,
126 .uv0 = true,
127 .lightmapUV = false
128 };
129 };
130
132 std::vector<RGL::PipelineLayoutDescriptor::LayoutBindingDesc> bindings;
133 uint32_t pushConstantSize = 0;
134 };
135
136 struct LitMaterial : public Material {
137
138 LitMaterial(const std::string_view vsh_name, const std::string_view fsh_name, const PipelineOptions& pipeOptions = {}, const MaterialRenderOptions& options = {
139 });
140 LitMaterial(const std::string_view name, const PipelineOptions& pipeOptions, const MaterialRenderOptions& options = {
141 }) : LitMaterial(name, name, pipeOptions, options) {}
142 };
143
144
145 // a material that reads no data
146 struct UnlitMaterial : public Material {
147 constexpr static MeshAttributes defaultMeshAttributes = {
148 .position = true,
149 .normal = true,
150 .tangent = true,
151 .bitangent = true,
152 .uv0 = true,
153 .lightmapUV = false,
154 };
155 UnlitMaterial(const std::string_view vsh_name, const std::string_view fsh_name, const PipelineOptions& pipeOptions = {}, const MaterialRenderOptions& options = {
156 .requiredAttributes = defaultMeshAttributes
157 });
158 UnlitMaterial(const std::string_view name, const PipelineOptions& pipeOptions, const MaterialRenderOptions& options = {
159 .requiredAttributes = defaultMeshAttributes
160 }) : UnlitMaterial(name, name, pipeOptions, options) {}
161 };
162
164 friend class RenderEngine;
165 template<typename T>
166 MaterialVariant(const T& value) : variant(value) {}
167
168 PipelineUseConfiguration GetShadowRenderPipeline() const;
169 PipelineUseConfiguration GetMainRenderPipeline()const;
170 PipelineUseConfiguration GetDepthPrepassPipeline() const;
171
172 const MaterialVariant* operator->() const {
173 return this;
174 }
175
176 private:
177 std::variant<Ref<LitMaterial>, Ref<UnlitMaterial>> variant;
178 };
179
180
185 constexpr static uint8_t maxBindingSlots = 12;
186 protected:
187 std::array<RGLBufferPtr, maxBindingSlots> bufferBindings;
188 std::array<Ref<Texture>, maxBindingSlots> textureBindings;
189 const uint32_t priority;
190 public:
191 auto GetPriority() const {
192 return priority;
193 }
194
200 return { nullptr, 0 };
201 }
202
203 const auto& GetBufferBindings() const {
204 return bufferBindings;
205 }
206
207 const auto& GetTextureBindings() const {
208 return textureBindings;
209 }
210
211 virtual ~MaterialInstance() {}
212 auto GetMat() {
213 return mat;
214 }
215
216 protected:
217 MaterialVariant mat;
218 MaterialInstance(const decltype(mat)& m, const decltype(priority) p = 0) : mat(m), priority(p) {}
219 };
220}
221#endif
Definition Span.hpp:11
Definition Material.hpp:57
static void Compact()
Definition Material.hpp:78
static Ref< T > Get(A &&... args)
Definition Material.hpp:65
Definition Material.hpp:45
Material(const std::string_view name, const MaterialConfig &config)
Definition Material.cpp:30
virtual ~Material()
Definition Material.cpp:192
Definition RenderEngine.hpp:64
Definition concurrentqueue.h:747
Definition Animation.hpp:6
Definition CTTI.hpp:14
static Ref< T > GetWithKey(const key_t &str, unique_key_t unique_key, A ... extras)
Definition Manager.hpp:45
static void Compact()
Definition Manager.hpp:70
static Ref< T > Get(const key_t &str, A ... extras)
Definition Manager.hpp:33
Definition Material.hpp:136
Definition Material.hpp:19
Definition Material.hpp:184
virtual const RGL::untyped_span GetPushConstantData() const
Definition Material.hpp:199
Definition Material.hpp:118
Definition Material.hpp:163
Definition MaterialShared.hpp:9
Definition Material.hpp:131
Definition Material.hpp:37
Definition Material.hpp:146