1#include "Rendering/IGraphicsDevice.h"
2#include "Rendering/ICapabilities.h"
4#include "Foundation/Logging/Logger.h"
6#include "MaterialBuilder.h"
8#include "EffectManager.h"
12#include "TextureManager.h"
14#include "ShaderBuilder.h"
15#include "Renderer/IRenderer.h"
22bool Cogs::Core::applyConstantBuffers(
class Context * context,
const MaterialProperties & materialProperties, ConstantBuffers & constantBuffers)
24 for (
auto & buffer : materialProperties.buffers) {
25 auto bufferKey = constantBuffers.addBuffer(buffer.name, buffer.isPerInstance);
27 for (
auto & parsedValue : buffer.values) {
28 auto & propertyName = parsedValue.name;
30 auto flags = parsedValue.flags;
32 switch (parsedValue.type) {
33 case MaterialDataType::Float:
34 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.floatValue, MaterialDataType::Float, buffer.isPerInstance);
36 case MaterialDataType::Float2:
37 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float2Value, MaterialDataType::Float2, buffer.isPerInstance);
39 case MaterialDataType::Float3:
40 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float3Value, MaterialDataType::Float3, buffer.isPerInstance);
42 case MaterialDataType::Float4:
43 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float4Value, MaterialDataType::Float4, buffer.isPerInstance);
45 case MaterialDataType::Float4x4:
46 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float4x4Value, MaterialDataType::Float4x4, buffer.isPerInstance);
48 case MaterialDataType::Float4Array:
49 if (parsedValue.dimension !=
static_cast<size_t>(-1)) {
50 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float4Value, MaterialDataType::Float4Array, buffer.isPerInstance, parsedValue.dimension);
53 LOG_WARNING(logger,
"Constant buffer float4[]-member must have a known size");
57 case MaterialDataType::Float4x4Array:
58 if (parsedValue.dimension !=
static_cast<size_t>(-1)) {
59 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.float4x4Value, MaterialDataType::Float4x4Array, buffer.isPerInstance, parsedValue.dimension);
62 LOG_WARNING(logger,
"Constant buffer float4x4[]-member must have a known size");
66 case MaterialDataType::Bool:
67 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.boolValue, MaterialDataType::Bool, buffer.isPerInstance);
69 case MaterialDataType::Int:
70 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.intValue, MaterialDataType::Int, buffer.isPerInstance);
72 case MaterialDataType::Int2:
73 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.int2Value, MaterialDataType::Int2, buffer.isPerInstance);
75 case MaterialDataType::Int3:
76 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.int3Value, MaterialDataType::Int3, buffer.isPerInstance);
78 case MaterialDataType::Int4:
79 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.int4Value, MaterialDataType::Int4, buffer.isPerInstance);
81 case MaterialDataType::UInt:
82 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.uintValue, MaterialDataType::UInt, buffer.isPerInstance);
84 case MaterialDataType::UInt2:
85 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.uint2Value, MaterialDataType::UInt2, buffer.isPerInstance);
87 case MaterialDataType::UInt3:
88 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.uint3Value, MaterialDataType::UInt3, buffer.isPerInstance);
90 case MaterialDataType::UInt4:
91 constantBuffers.addVariable(bufferKey, flags, propertyName, parsedValue.uint4Value, MaterialDataType::UInt4, buffer.isPerInstance);
94 LOG_WARNING(logger,
"Unsupported constant buffer type %s", DataTypeNames[
size_t(parsedValue.type)]);
99 constantBuffers.finalizeBuffer(context, bufferKey);
104bool Cogs::Core::applyMaterialProperties(Context * context,
const MaterialProperties & materialProperties, Material & material)
106 if (!applyConstantBuffers(context, materialProperties, material.constantBuffers))
return false;
108 if (!materialProperties.buffers.size()) {
111 material.constantBuffers.buffersGeneration++;
114 TextureHandle defaultTexture[] = {
115 context->textureManager->white,
116 context->textureManager->whiteCube,
117 TextureHandle::NoHandle,
118 TextureHandle::NoHandle,
121 for (
const MaterialTextureDefinition& texture : materialProperties.textures) {
122 assert((
size_t)texture.dimensions <
sizeof(defaultTexture)/
sizeof(defaultTexture[0]));
124 for(uint32_t i=0; i<texture.arraySize; i++){
125 VariableKey key = material.addTextureProperty(texture.name+
'['+std::to_string(i)+
']',
126 defaultTexture[
static_cast<size_t>(texture.dimensions)],
128 SamplerState::AddressMode::Wrap,
129 SamplerState::FilterMode::MinMagMipLinear,
130 texture.isPerInstance, texture.isArray, texture.arraySize);
132 if (texture.isSrgb) {
133 material.textureProperties[key].flags |= MaterialPropertyFlags::sRGB;
138 VariableKey key = material.addTextureProperty(texture.name,
139 defaultTexture[
static_cast<size_t>(texture.dimensions)],
141 SamplerState::AddressMode::Wrap,
142 SamplerState::FilterMode::MinMagMipLinear,
143 texture.isPerInstance,
146 if (texture.isSrgb) {
147 material.textureProperties[key].flags |= MaterialPropertyFlags::sRGB;
154bool Cogs::Core::applyMaterialDefinition(
class Context * context,
const MaterialDefinition & materialDefinition, Material & material)
156 material.setName(materialDefinition.name);
158 material.enginePermutationMask = materialDefinition.enginePermutationMask;
160 for (
auto & opt : materialDefinition.options) {
161 applyMaterialOption(material.options, opt.first, opt.second);
164 if (!applyMaterialProperties(context, materialDefinition.properties, material))
return false;
Log implementation class.
uint16_t VariableKey
Used to lookup material properties.
constexpr Log getLogger(const char(&name)[LEN]) noexcept