CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
RendererMaterial.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
12#include "Resources/Forward.hpp"
13
15
17
19
25
26namespace CeresEngine {
27
28 class RendererMaterialManager;
29
32 protected:
35
38
42
43 bool mDirty = true;
44
45 public:
48
50 virtual ~RendererMaterial() = default;
51
52 public:
59 virtual void prepare(RendererContext& context) = 0;
60
65
66 public:
68 [[nodiscard]] virtual const GPUShaderProgramPtr& getShaderProgram() const = 0;
69
71 [[nodiscard]] virtual const GPUUniformBuffer& getUniformBuffer() const = 0;
72
74 [[nodiscard]] virtual const GPUResourceSetPtr& getResourceSet() const = 0;
75
78
79 public:
82
84 [[nodiscard]] bool isDirty() const noexcept { return mDirty; }
85
87 void markAsDirty() noexcept { mDirty = true; }
88
89 public:
90 virtual void bind(GPUCommandBuffer& commandBuffer) = 0;
91
92 private:
94
96 void setID(const UInt32 id) noexcept { mID = id; }
97 };
98
100 struct alignas(16) PBRMaterialParams {
101 alignas(16) Vector4f baseColor;
103 // alignas(4) UInt32 baseColorSamplerID;
104 alignas(16) Matrix4f baseColorTextureMask;
105
106 alignas(4) float roughness;
108 // alignas(4) UInt32 roughnessSamplerID;
109 alignas(16) Vector4f roughnessTextureMask;
110
111 alignas(4) float metallic;
113 // alignas(4) UInt32 metallicSamplerID;
114 alignas(16) Vector4f metallicTextureMask;
115
116 alignas(16) Vector3f normal;
118 // alignas(4) UInt32 normalSamplerID;
119 alignas(16) Matrix3x4f normalTextureMask;
120
121 alignas(16) Vector4f emissive;
123 // alignas(4) UInt32 emissiveSamplerID;
124 alignas(16) Matrix4f emissiveTextureMask;
125
126 alignas(16) Vector3f displacement;
128 // alignas(4) UInt32 displacementSamplerID;
129 alignas(16) Matrix3x4f displacementTextureMask;
130
131 alignas(4) float ambientOcclusion;
133 // alignas(4) UInt32 ambientOcclusionSamplerID;
134 float pad2;
135 float pad3;
136 alignas(16) Vector4f ambientOcclusionTextureMask;
137
139 };
140
144
145 private:
148
151
154
158
161
163
166
168
169 public:
170 template<typename ValueType> class Property : public MaterialProperty<ValueType, RendererTexturePtr> {
172
173 private:
176
177 public:
180
181 public:
182 using super::super;
183
190 };
191
194
197
200
203
206
209
212
213 public:
217
218 public:
220 void prepare(RendererContext& context) override;
221
224
225 public:
228
231
234
237
240
241 public:
243 void bind(GPUCommandBuffer& commandBuffer) final;
244
245 private:
248
251 };
252
253 // ---------------------------------------------------------------------------------------------
254
325
326} // namespace CeresEngine
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
Scoped connection class.
Definition Event.hpp:104
A GPU bindless buffer.
Definition GPUBufferObject.hpp:516
Definition GPUCommandBuffer.hpp:77
Definition GPUDevice.hpp:357
Definition GPUBufferObject.hpp:562
Definition GPUResourceSet.hpp:457
Definition GPUBufferObject.hpp:221
The CeresEngine renderer.
Definition Renderer.hpp:35
An abstract class that represents a renderer material.
Definition RendererMaterial.hpp:31
virtual void bind(GPUCommandBuffer &commandBuffer)=0
UInt32 getID() const noexcept
A unique ID that can be used to uniquely identify the material in the bindless material resource set.
Definition RendererMaterial.hpp:81
Renderer & mRenderer
The renderer that owns the material.
Definition RendererMaterial.hpp:37
void setID(const UInt32 id) noexcept
A unique ID that can be used to uniquely identify the material in the bindless material resource set.
Definition RendererMaterial.hpp:96
RendererMaterialManager & mManager
The material manager that owns and manages this material instance.
Definition RendererMaterial.hpp:34
bool isDirty() const noexcept
Definition RendererMaterial.hpp:84
RendererMaterial(RendererMaterialManager &manager)
Creates a new renderer material.
virtual const GPUShaderProgramPtr & getShaderProgram() const =0
Gets the shader program used by the material.
UInt32 mID
A unique ID that can be used to uniquely identify the material in the bindless material resource set.
Definition RendererMaterial.hpp:41
virtual const GPUResourceSetPtr & getResourceSet() const =0
Returns the ResourceSet that holds material parameters.
virtual void fillResourceSetDescriptor(GPUShaderResourceSetBindings &bindings) const =0
Fills a ReflectedResourceSetDescriptor with the material parameters.
virtual void prepare(RendererContext &context)=0
Prepares the material for the rendering of the current frame.
virtual const GPUUniformBuffer & getUniformBuffer() const =0
Returns the uniform buffer that holds material parameters.
virtual GPUResourceSetDescriptor getResourceSetDescriptor() const =0
Returns the ResourceSet bindings that holds material parameters.
virtual ~RendererMaterial()=default
Virtual defaulted destructor.
bool mDirty
Definition RendererMaterial.hpp:43
void markAsDirty() noexcept
Definition RendererMaterial.hpp:87
A manager that controls all materials currently in use by the renderer.
Definition RendererMaterial.hpp:256
HashMap< HMaterial, WPtr< RendererMaterial > > mMaterials
A list of materials currently registered with the renderer.
Definition RendererMaterial.hpp:269
SPtr< RendererMaterial > mDefaultMaterial
The default material to be used as a fallback in case no material is defined by the user.
Definition RendererMaterial.hpp:273
Renderer & getRenderer() const
The renderer that owns this manager instance.
Definition RendererMaterial.hpp:316
RendererMaterialManager(Renderer &renderer, GPUDevice &device)
Creates a new instance of the RendererMaterialManager.
Renderer & mRenderer
The renderer that owns this manager instance.
Definition RendererMaterial.hpp:262
Async prepare(RendererContext &context)
Prepares the materials for the rendering of the current frame.
UniformBufferType mUniformBuffer
The uniform buffer that holds GPU parameters for all materials.
Definition RendererMaterial.hpp:277
GPUDevice & mDevice
The device the renderer will render to.
Definition RendererMaterial.hpp:266
~RendererMaterialManager()
Destroys the RendererMaterialManager and releases all GPU resources managed and allocated by it.
RendererMaterial & getDefaultMaterial() const
The default material to be used as a fallback in case no material is defined by the user.
Definition RendererMaterial.hpp:319
const GPUBindlessBuffer & getUniformBuffer() const noexcept
The uniform buffer that holds GPU parameters for all materials.
Definition RendererMaterial.hpp:323
SPtr< RendererMaterial > get(const HMaterial &material)
Gets a renderer material instance for the given material resource.
Definition RendererMaterial.hpp:170
Property(const MaterialProperty< ValueType > &other, const RendererTexturePtr &texture, const GPUSamplerPtr &sampler)
Definition RendererMaterial.hpp:184
GPUSamplerPtr sampler
A descriptor that describes how material sampling should be done.
Definition RendererMaterial.hpp:179
EventConnection mDidChangeImageConnection
Definition RendererMaterial.hpp:175
A renderer material that represents a PBR model material.
Definition RendererMaterial.hpp:142
const GPUResourceSetPtr & getResourceSet() const final
Returns the ResourceSet that holds material parameters.
Definition RendererMaterial.hpp:233
void didChangeMaterialModel(const PBRMaterialModel &newModel)
An event called whenever the associated PBRMaterialModel has a property changed.
GPUResourceSetPtr mResourceSet
The ResourceSet with renderer material parameters.
Definition RendererMaterial.hpp:165
GPUPipelineLayoutPtr mPipelineLayout
The deferred PBR shader pipeline layout.
Definition RendererMaterial.hpp:157
const Property< double > roughness
A material channel that represents the roughness map of the material.
Definition RendererMaterial.hpp:196
void prepare(RendererContext &context) override
Prepares the material for the rendering of the current frame.
const Property< Vector3 > displacement
The material displacement property.
Definition RendererMaterial.hpp:208
const PBRMaterialModel & getModel() const noexcept
A copy of the PBR material model structure.
Definition RendererMaterial.hpp:239
GPUDynamicUniformBuffer< PBRMaterialParams > mUniformBuffer
The uniform buffer that holds GPU parameters for the material.
Definition RendererMaterial.hpp:160
const Property< double > ambientOcclusion
The material ambient occlusion property.
Definition RendererMaterial.hpp:211
const Property< Color > baseColor
A material channel that represents the base color (or albedo) of the material.
Definition RendererMaterial.hpp:193
UInt64 mHash
Definition RendererMaterial.hpp:167
SPtr< PBRMaterial > mMaterial
The material resource referneced by this renderer material.
Definition RendererMaterial.hpp:147
const Property< Color > emission
The material emission property.
Definition RendererMaterial.hpp:205
GPUResourceSetDescriptor getResourceSetDescriptor() const final
Returns the ResourceSet bindings that holds material parameters.
Definition RendererMaterial.hpp:236
GPUShaderProgramPtr mShaderProgram
The deferred PBR shader program.
Definition RendererMaterial.hpp:153
const GPUUniformBuffer & getUniformBuffer() const final
Returns the uniform buffer that holds material parameters.
Definition RendererMaterial.hpp:230
RendererMaterialPBR(RendererMaterialManager &manager, const SPtr< PBRMaterial > &material)
Creates a new renderer material from the given model on the given render API device.
const GPUShaderProgramPtr & getShaderProgram() const final
Gets the shader program used by the material.
Definition RendererMaterial.hpp:227
void fillResourceSetDescriptor(GPUShaderResourceSetBindings &bindings) const final
Fills a ReflectedResourceSetDescriptor with the material parameters.
const Property< double > metallic
A material channel that represents the metallic map of the material.
Definition RendererMaterial.hpp:199
GPUResourceSetDescriptor mResourceSetDescriptor
Definition RendererMaterial.hpp:162
const Property< Vector3 > normal
A material channel that represents the normal map of the material.
Definition RendererMaterial.hpp:202
PBRMaterialModel mModel
A copy of the PBR material model structure.
Definition RendererMaterial.hpp:150
void bind(GPUCommandBuffer &commandBuffer) final
Binds the material to the given command buffer.
Definition Application.hpp:19
std::uint64_t UInt64
Definition DataTypes.hpp:26
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition GPUResourceSet.hpp:716
A material property with an associated value and texture.
Definition MaterialModel.hpp:155
RendererTexturePtr texture
The texture associated with the material property.
Definition MaterialModel.hpp:160
ValueType value
The value associated with the material property.
Definition MaterialModel.hpp:157
MaterialPropertySwizzle< ValueType > swizzle
A property that allows mixing channels in a material property.
Definition MaterialModel.hpp:171
GPUSamplerDescriptor sampler
A descriptor that describes how material sampling should be done.
Definition MaterialModel.hpp:163
A material model that represents a PBR surface.
Definition MaterialModel.hpp:256
A structure that mirrors the GPU uniform data for the material.
Definition RendererMaterial.hpp:100
float metallic
Definition RendererMaterial.hpp:111
Vector4f baseColor
Definition RendererMaterial.hpp:101
float pad2
Definition RendererMaterial.hpp:134
Matrix4f emissiveTextureMask
Definition RendererMaterial.hpp:124
UInt32 metallicTextureID
Definition RendererMaterial.hpp:112
Vector3f normal
Definition RendererMaterial.hpp:116
float pad3
Definition RendererMaterial.hpp:135
Vector4f emissive
Definition RendererMaterial.hpp:121
Vector4f ambientOcclusionTextureMask
Definition RendererMaterial.hpp:136
Vector3f displacement
Definition RendererMaterial.hpp:126
UInt32 displacementTextureID
Definition RendererMaterial.hpp:127
float roughness
Definition RendererMaterial.hpp:106
Matrix3x4f normalTextureMask
Definition RendererMaterial.hpp:119
UInt32 roughnessTextureID
Definition RendererMaterial.hpp:107
Matrix3x4f displacementTextureMask
Definition RendererMaterial.hpp:129
Matrix4f baseColorTextureMask
Definition RendererMaterial.hpp:104
UInt32 emissiveTextureID
Definition RendererMaterial.hpp:122
Vector4f metallicTextureMask
Definition RendererMaterial.hpp:114
UInt32 isNormalsInTangentSpace
Definition RendererMaterial.hpp:138
float ambientOcclusion
Definition RendererMaterial.hpp:131
UInt32 baseColorTextureID
Definition RendererMaterial.hpp:102
Vector4f roughnessTextureMask
Definition RendererMaterial.hpp:109
UInt32 normalTextureID
Definition RendererMaterial.hpp:117
UInt32 ambientOcclusionTextureID
Definition RendererMaterial.hpp:132
A structure that contains context given by the renderer to render it's objects.
Definition Renderer.hpp:252