Cogs.Core
MaterialInstance.h
1#pragma once
2#include "Material.h"
3
4#include "Foundation/StringView.h"
5
6namespace Cogs
7{
8 namespace Core
9 {
16 struct COGSCORE_DLL_API MaterialInstance : public ResourceBase
17 {
20 MaterialInstance() = default;
21
23 MaterialInstance(MaterialInstance && other) noexcept = default;
24
26 MaterialInstance & operator=(MaterialInstance && other) noexcept = default;
27
37 void setupInstance(Material * material);
38
52 void clone(MaterialInstance * instance);
53
62 int cloneMatchingProperties(MaterialInstance * instance);
63
67 void cloneMatchingVariants(MaterialInstance * instance);
68
72 void reset();
73
82 void setMaterialFlag(MaterialFlags::EMaterialFlags flag) { this->instanceFlags |= flag; }
83
93 void setMaterialFlag(MaterialFlags::EMaterialFlags flag, bool status) { instanceFlags = (instanceFlags & ~flag) | (-static_cast<int>(status) & flag); }
94
100 void unsetMaterialFlag(MaterialFlags::EMaterialFlags flag) { this->instanceFlags &= ~flag; }
101
103 uint16_t getMaterialFlags() const { return (material->materialFlags & instanceFlags) | instanceFlags; }
104
106 void setBackdrop() { setMaterialFlag(MaterialFlags::Backdrop); options.depthWriteEnabled = false; }
107
109 void unsetBackdrop() { unsetMaterialFlag(MaterialFlags::Backdrop); options.depthWriteEnabled &= true; }
110
116 bool isBackdrop() const { return instanceFlags & MaterialFlags::Backdrop ? true : false; }
117
124 void setTransparent();
125
130 void setOpaque();
131
137 bool hasTransparency() const;
138
142 bool isDefaultMaterial() const { return material->isDefaultMaterial(); }
143
147 void setOption(const StringView & key, const StringView & value);
148
149 void setProperty(const StringView & key, const void * value, const size_t sizeInBytes);
150 void setProperty(VariableKey key, const void * value, const size_t sizeInBytes);
151
152 bool getProperty(const StringView & key, void * value, const size_t sizeInBytes) const;
153 bool getProperty(VariableKey key, void * value, const size_t sizeInBytes) const;
154
159 void setTextureAddressMode(const StringView & key, const StringView & addressMode);
160
167 void setTextureFilterMode(const StringView& key, const StringView& filterMode);
168
181 template<typename T>
182 void setProperty(const VariableKey key, T value)
183 {
184 auto & materialProperty = material->constantBuffers.variables[key];
185 if (!materialProperty.isPerInstance)
186 {
187 material->setProperty(key, value);
188 return;
189 }
190 if (materialProperty.flags != MaterialPropertyFlags::None) {
191 enforcePropertyFlags(materialProperty, materialProperty.flags, &value);
192 }
193
194 buffers[materialProperty.buffer].setValue(materialProperty.descriptor.offset, value);
195
196 setChanged();
197 }
198
211 template<typename T>
212 T getProperty(const VariableKey key) const
213 {
214 T t;
215 getProperty(key, &t, sizeof(T));
216 return t;
217 }
218
225 void setVec2Property(const VariableKey key, glm::vec2 value)
226 {
227 setProperty(key, value);
228 }
229
236 void setVec3Property(const VariableKey key, glm::vec3 value)
237 {
238 setProperty(key, value);
239 }
240
247 void setVec4Property(const VariableKey key, glm::vec4 value)
248 {
249 setProperty(key, value);
250 }
251
258 void setInt4Property(const VariableKey key, glm::ivec4 value)
259 {
260 setProperty(key, value);
261 }
262
269 void setMat4Property(const VariableKey key, glm::mat4 value)
270 {
271 setProperty(key, value);
272 }
273
280 void setFloatProperty(const VariableKey key, float value)
281 {
282 setProperty(key, value);
283 }
284
285 void setIntProperty(const VariableKey key, int value)
286 {
287 setProperty(key, value);
288 }
289
290 void setUIntProperty(const VariableKey key, uint32_t value)
291 {
292 setProperty(key, value);
293 }
294
301 void setBoolProperty(const VariableKey key, bool value)
302 {
303 setProperty(key, value);
304 }
305
312 void setTextureProperty(const StringView & key, TextureHandle value);
313 void setTextureProperty(const VariableKey key, TextureHandle value);
314
321 void setTextureAddressMode(const VariableKey key, SamplerState::AddressMode mode);
322 void setTextureAddressMode(const VariableKey key, SamplerState::AddressMode sMode, SamplerState::AddressMode tMode, SamplerState::AddressMode uMode);
323
325 void setTextureFilterMode(const VariableKey key, SamplerState::FilterMode filterMode);
326
334 glm::vec2 getVec2Property(const VariableKey key) const
335 {
336 return getProperty<glm::vec2>(key);
337 }
338
346 glm::vec3 getVec3Property(const VariableKey key) const
347 {
348 return getProperty<glm::vec3>(key);
349 }
350
358 glm::vec4 getVec4Property(const VariableKey key) const
359 {
360 return getProperty<glm::vec4>(key);
361 }
362
370 glm::mat4 getMat4Property(const VariableKey key) const
371 {
372 return getProperty<glm::mat4>(key);
373 }
374
382 bool getBoolProperty(const VariableKey key) const
383 {
384 return getProperty<bool>(key);
385 }
386
394 float getFloatProperty(const VariableKey key) const
395 {
396 return getProperty<float>(key);
397 }
398
407 {
408 if (key != NoProperty) {
409 return textureVariables[key];
410 }
411 return TextureValue();
412 }
413
414 size_t getPermutationIndex(const StringView & key) const;
415
416 void setPermutation(const StringView & key);
417
418 StringView getPermutation() const;
419
420 void setVariant(size_t index, int value);
421 void setVariant(size_t index, bool value);
422 void setVariant(size_t index, const StringView & value);
423
424 void setVariant(const StringView & key, const StringView & value);
425 void setVariant(const StringView & key, const char* value) { setVariant(key, StringView(value)); }
426 void setVariant(const StringView & key, bool value);
427 void setVariant(const StringView & key, int value);
428
429 std::string getVariant(const StringView & key) const;
430
432 Material * material = nullptr;
433
436
438 std::vector<MaterialPropertyBufferInstance> buffers;
439
441 std::vector<TextureValue> textureVariables;
442
444 uint16_t instanceFlags = MaterialFlags::None;
445
448
450 ShaderVariantSelectors variantSelectors;
451
453 std::vector<std::string> variantStrings;
454
456 size_t permutationIndex = 0;
457
459 size_t variantGeneration = 0;
460
462 uint16_t buffersGeneration = 1;
463 };
464 }
465}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
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
EMaterialFlags
Material flags.
Definition: Material.h:41
Material instances represent a specialized Material combined with state for all its buffers and prope...
std::vector< TextureValue > textureVariables
Texture property values for this instance.
uint16_t getMaterialFlags() const
Gets flags of the instance, combined from material flags and overrides from instance.
bool isDefaultMaterial() const
Gets if this material instance is created from the default material.
void setBackdrop()
Set the material instance to act as backdrop when rendering.
MaterialInstance(MaterialInstance &&other) noexcept=default
Move construct MaterialInstance from other.
glm::vec4 getVec4Property(const VariableKey key) const
Get the value of the property with the given key.
void setFloatProperty(const VariableKey key, float value)
Set the float property with the given key to value.
bool isBackdrop() const
Get if geometry rendered with this material instance is to be treated as backdrops.
void setInt4Property(const VariableKey key, glm::ivec4 value)
Set the ivec4 property with the given key to value.
T getProperty(const VariableKey key) const
Get the property value of the property with the given key in the given collection.
void unsetBackdrop()
Set the material instance to act as regular geometry, not backdrop, when rendering.
bool getBoolProperty(const VariableKey key) const
Get the value of the property with the given key.
TextureValue getTextureProperty(const VariableKey key) const
Get the value of the property with the given key.
std::vector< MaterialPropertyBufferInstance > buffers
Buffer instances matching the buffers and layout of the parent material.
void setVec3Property(const VariableKey key, glm::vec3 value)
Set the vec3 property with the given key to value.
std::vector< std::string > variantStrings
String storage for string variants.
float getFloatProperty(const VariableKey key) const
Get the value of the property with the given key.
glm::vec3 getVec3Property(const VariableKey key) const
Get the value of the property with the given key.
void setProperty(const VariableKey key, T value)
Set the property value of the property with the given key.
void setMaterialFlag(MaterialFlags::EMaterialFlags flag)
Set the given material flag.
MaterialInstanceHandle masterInstance
Master material instance overriding properties in this instance if override is enabled.
void unsetMaterialFlag(MaterialFlags::EMaterialFlags flag)
Unset the given material flag.
void setVec2Property(const VariableKey key, glm::vec2 value)
Set the vec2 property with the given key to value.
MaterialOptions options
Material rendering options used by this instance.
void setVec4Property(const VariableKey key, glm::vec4 value)
Set the vec4 property with the given key to value.
void setMaterialFlag(MaterialFlags::EMaterialFlags flag, bool status)
Set the given material flag to the given state.
void setBoolProperty(const VariableKey key, bool value)
Set the bool property with the given key to value.
MaterialInstance & operator=(MaterialInstance &&other) noexcept=default
Move assign MaterialInstance from other.
glm::vec2 getVec2Property(const VariableKey key) const
Get the value of the property with the given key.
ShaderVariantSelectors variantSelectors
Variant selectors.
void setMat4Property(const VariableKey key, glm::mat4 value)
Set the mat4 property with the given key to value.
glm::mat4 getMat4Property(const VariableKey key) const
Get the value of the property with the given key.
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
Base class for engine resources.
Definition: ResourceBase.h:107
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