Cogs.Core
Material.h
1#pragma once
2
3#include "ResourceBase.h"
4
5#include "MaterialDefinition.h"
6#include "MaterialPropertyBuffer.h"
7#include "MaterialProperty.h"
8#include "MaterialOptions.h"
9#include "ConstantBuffers.h"
10#include "MeshStreamsLayout.h"
11
12#include "Rendering/SamplerState.h"
13
14#include "Foundation/StringView.h"
15
16#include <glm/vec2.hpp>
17#include <glm/vec3.hpp>
18#include <glm/vec4.hpp>
19#include <glm/mat4x4.hpp>
20#include <glm/gtc/type_ptr.hpp>
21
22#include <vector>
23#include <string>
24
25namespace Cogs
26{
27 namespace Core
28 {
29 struct EnginePermutation;
30 struct MaterialInstance;
31 struct RenderPassOptions;
32 enum struct ClipShapeType : uint32_t;
33
34 void COGSCORE_DLL_API enforcePropertyFlags(const MaterialProperty & prop, MaterialPropertyFlags flags, void * data);
35
38 {
41 {
43 None = 0x0000,
45 Default = 0x0001,
50 Backdrop = 0x0004,
52 OverrideColor = 0x0008,
54 OverrideAlpha = 0x0010,
62 Sprite = 0x0100,
64 CustomBucket = 0x0200
65 };
66 };
67
81 struct COGSCORE_DLL_API Material : public ResourceBase
82 {
83 static const constexpr size_t NoVariantIndex = size_t(-1);
84
88 Material() = default;
89
91 Material(Material && other) noexcept = default;
92
94 Material & operator=(Material && other) noexcept = default;
95
101 void setMaterialFlag(MaterialFlags::EMaterialFlags flag) { this->materialFlags |= flag; }
102
108 void unsetMaterialFlag(MaterialFlags::EMaterialFlags flag) { this->materialFlags &= ~flag; }
109
110 bool isDefaultMaterial() const { return materialFlags & MaterialFlags::Default ? true : false; }
111
124 TextureHandle defaultValue,
125 TextureDimensions dimensions = TextureDimensions::Texture2D,
126 SamplerState::AddressMode addressMode = SamplerState::Wrap,
127 SamplerState::FilterMode filterMode = SamplerState::FilterMode::MinMagMipLinear,
128 bool isPerInstance = true,
129 bool isArray = false, uint32_t arraySize = 0)
130 {
131 TextureWithSampler texture;
132 texture.handle = defaultValue;
133 texture.sMode = addressMode;
134 texture.tMode = addressMode;
135 texture.uMode = addressMode;
136 texture.filterMode = filterMode;
137 texture.dimensions = dimensions;
138
139 const VariableKey key = static_cast<VariableKey>(textureProperties.size());
140 auto & textureProperty = textureProperties.emplace_back();
141
142 textureProperty.key = key;
143 textureProperty.isPerInstance = isPerInstance;
144 textureProperty.name = name;
145 textureProperty.texture = texture;
146 textureProperty.isArray = isArray;
147 textureProperty.arraySize = arraySize;
148
149 return key;
150 }
151
153 MaterialDataType getPropertyDataType(const StringView& key);
154
155 void setProperty(const StringView& name, const void* data, const size_t sizeInBytes);
156
171 template<typename T>
172 void setProperty(const VariableKey key, T value)
173 {
174 constantBuffers.setProperty(key, value);
175
176 setChanged();
177 }
178
185 void setVec2Property(const VariableKey key, glm::vec2 value)
186 {
187 setProperty(key, value);
188 }
189
196 void setVec3Property(const VariableKey key, glm::vec3 value)
197 {
198 setProperty(key, value);
199 }
200
207 void setVec4Property(const VariableKey key, glm::vec4 value)
208 {
209 setProperty(key, value);
210 }
211
218 void setInt4Property(const VariableKey key, glm::ivec4 value)
219 {
220 setProperty(key, value);
221 }
222
229 void setMat4Property(const VariableKey key, glm::mat4 value)
230 {
231 setProperty(key, value);
232 }
233
240 void setFloatProperty(const VariableKey key, float value)
241 {
242 setProperty(key, value);
243 }
244
245 void setIntProperty(const VariableKey key, int value)
246 {
247 setProperty(key, value);
248 }
249
250 void setUIntProperty(const VariableKey key, uint32_t value)
251 {
252 setProperty(key, value);
253 }
254
261 void setBoolProperty(const VariableKey key, bool value)
262 {
263 setProperty(key, value);
264 }
265
272 void setTextureProperty(const VariableKey key, TextureHandle value);
273
280 void setTextureAddressMode(const VariableKey key, SamplerState::AddressMode mode);
281
290 void setTextureAddressMode(const VariableKey key, SamplerState::AddressMode smode, SamplerState::AddressMode tmode, SamplerState::AddressMode umode);
291
292
294 void setTextureFilterMode(const VariableKey key, SamplerState::FilterMode filterMode);
295
296 VariableKey getBoolKey(const StringView & name) const
297 {
298 return constantBuffers.getPropertyKey(name);
299 }
300
301 VariableKey getFloatKey(const StringView & name) const
302 {
303 return constantBuffers.getPropertyKey(name);
304 }
305
306 VariableKey getIntKey(const StringView & name) const
307 {
308 return constantBuffers.getPropertyKey(name);
309 }
310
311 VariableKey getUIntKey(const StringView & name) const
312 {
313 return constantBuffers.getPropertyKey(name);
314 }
315
316 VariableKey getVec3Key(const StringView & name) const
317 {
318 return constantBuffers.getPropertyKey(name);
319 }
320
321 VariableKey getVec4Key(const StringView & name) const
322 {
323 return constantBuffers.getPropertyKey(name);
324 }
325
326 VariableKey getInt4Key(const StringView& name) const
327 {
328 return constantBuffers.getPropertyKey(name);
329 }
330
331 VariableKey getMat4Key(const StringView & name) const
332 {
333 return constantBuffers.getPropertyKey(name);
334 }
335
336 VariableKey getVec2Key(const StringView & name) const
337 {
338 return constantBuffers.getPropertyKey(name);
339 }
340
341 VariableKey getTextureKey(const StringView & name) const
342 {
343 for (auto & prop : textureProperties) {
344 if (name == prop.name) {
345 return prop.key;
346 }
347 }
348
349 return NoProperty;
350 }
351
352 size_t getVariantIndex(const StringView& key) const;
353
354 void setVariant(size_t index, int value);
355
356 void setVariant(const StringView& key, int value);
357
358 MaterialDefinition definition;
359
360 size_t variantGeneration = 0; // Changed when definition.variant default values change.
361
362 std::vector<std::string> permutationKeys;
363
365 {
366 EffectHandle handle;
367 size_t code;
368 };
369
371 EffectHandle getEffect(size_t code,
372 const MaterialInstance * materialInstance,
373 const MeshStreamsLayout* streamsLayout,
374 const EnginePermutation * enginePermutation,
375 const RenderPassOptions& passOptions,
376 const ClipShapeType clipShape);
377
378 std::vector<EffectInstance> effects;
379
380 ConstantBuffers constantBuffers;
381
384
386 uint16_t materialFlags = MaterialFlags::None;
387
388 uint64_t enginePermutationMask = 0x1 | 0x2 | 0x3 | 0x4;
389
390 std::vector<TextureProperty> textureProperties;
391 };
392 }
393}
394
395template<> inline Cogs::StringView getName<Cogs::Core::MaterialFlags::EMaterialFlags>() { return "MaterialFlags"; }
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
ClipShapeType
Specifices what kind of shape a clip shape has.
TextureDimensions
Texture dimensions.
MaterialDataType
Defines available data types for material properties.
Definition: MaterialTypes.h:20
std::string PropertyName
Typedef for property names.
uint16_t VariableKey
Used to lookup material properties.
Definition: Resources.h:46
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
void setChanged(Cogs::Core::Context *context, Cogs::ComponentModel::Component *component, Reflection::FieldId fieldId)
Must be Called after changing a Component field. Mark field changed. Request engine update.
Definition: FieldSetter.h:25
Defines material flags controlling the behavior of Materials when rendering.
Definition: Material.h:38
EMaterialFlags
Material flags.
Definition: Material.h:41
@ Override
Override all properties of any inheriting materials.
Definition: Material.h:60
@ OverrideAlpha
Override alpha of any inheriting materials.
Definition: Material.h:54
@ Default
Default material.
Definition: Material.h:45
@ OverrideProperties
Override properties of any inheriting materials.
Definition: Material.h:58
@ OverrideColor
Override color of any inheriting materials.
Definition: Material.h:52
@ OverrideTextures
Override textures of any inheriting materials.
Definition: Material.h:56
@ CustomBucket
Items with this flag should be put in Custom rendering buckets.
Definition: Material.h:64
@ Sprite
The material is used for sprite rendering only.
Definition: Material.h:62
@ None
No material flags.
Definition: Material.h:43
@ MasterTransparency
Material contains transparency.
Definition: Material.h:47
Material instances represent a specialized Material combined with state for all its buffers and prope...
Defines options for rendering using a material instance.
Material resources define the how of geometry rendering (the what is defined by Mesh and Texture reso...
Definition: Material.h:82
Material & operator=(Material &&other) noexcept=default
Move assign material from other.
void setMaterialFlag(MaterialFlags::EMaterialFlags flag)
Set the given material flag.
Definition: Material.h:101
void setBoolProperty(const VariableKey key, bool value)
Set the bool property with the given key to value.
Definition: Material.h:261
Material(Material &&other) noexcept=default
Move construct Material from other.
void unsetMaterialFlag(MaterialFlags::EMaterialFlags flag)
Unset the given material flag.
Definition: Material.h:108
void setMat4Property(const VariableKey key, glm::mat4 value)
Set the mat4 property with the given key to value.
Definition: Material.h:229
Material()=default
Constructs a new material.
void setProperty(const VariableKey key, T value)
Set the property value of the property with the given key in the given collection.
Definition: Material.h:172
void setFloatProperty(const VariableKey key, float value)
Set the float property with the given key to value.
Definition: Material.h:240
void setVec4Property(const VariableKey key, glm::vec4 value)
Set the vec4 property with the given key to value.
Definition: Material.h:207
void setInt4Property(const VariableKey key, glm::ivec4 value)
Set the ivec4 property with the given key to value.
Definition: Material.h:218
void setVec2Property(const VariableKey key, glm::vec2 value)
Set the vec2 property with the given key to value.
Definition: Material.h:185
VariableKey addTextureProperty(const PropertyName &name, TextureHandle defaultValue, TextureDimensions dimensions=TextureDimensions::Texture2D, SamplerState::AddressMode addressMode=SamplerState::Wrap, SamplerState::FilterMode filterMode=SamplerState::FilterMode::MinMagMipLinear, bool isPerInstance=true, bool isArray=false, uint32_t arraySize=0)
Adds a texture property to the material.
Definition: Material.h:123
void setVec3Property(const VariableKey key, glm::vec3 value)
Set the vec3 property with the given key to value.
Definition: Material.h:196
MaterialOptions options
Material rendering options.
Definition: Material.h:383
Base class for engine resources.
Definition: ResourceBase.h:107
SamplerState::FilterMode filterMode
Filter mode to use when rendering with this texture.
SamplerState::AddressMode sMode
Address mode to use when rendering with this texture.
TextureHandle handle
Handle to a texture resource, or TextureHandle::NoHandle if texture should be disabled.
TextureDimensions dimensions
Expected dimensions of the texture property.
AddressMode
Addressing modes to use when sampling textures.
Definition: SamplerState.h:15
FilterMode
Filter modes to specify how texture data is treated when sampled.
Definition: SamplerState.h:31