Skip to content

Reworking material properties #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void OvCore::Rendering::PostProcess::AutoExposureEffect::Draw(

// Luminance calculation
m_luminanceBuffer.Resize(kLuminanceBufferResolution, kLuminanceBufferResolution);
m_luminanceMaterial.Set("_CenterWeightBias", autoExposureSettings.centerWeightBias, true);
m_luminanceMaterial.SetProperty("_CenterWeightBias", autoExposureSettings.centerWeightBias, true);
m_renderer.Blit(p_pso, p_src, m_luminanceBuffer, m_luminanceMaterial,
OvRendering::Settings::EBlitFlags::DEFAULT & ~OvRendering::Settings::EBlitFlags::RESIZE_DST_TO_MATCH_SRC);
const auto luminanceTex = m_luminanceBuffer.GetAttachment<OvRendering::HAL::Texture>(OvRendering::Settings::EFramebufferAttachment::COLOR);
Expand All @@ -76,18 +76,18 @@ void OvCore::Rendering::PostProcess::AutoExposureEffect::Draw(
m_exposurePingPongIndex = (m_exposurePingPongIndex + 1) % 2;

// Exposure adaptation
m_exposureMaterial.Set("_LuminanceTexture", luminanceTex, true);
m_exposureMaterial.Set("_MinLuminanceEV", autoExposureSettings.minLuminanceEV, true);
m_exposureMaterial.Set("_MaxLuminanceEV", autoExposureSettings.maxLuminanceEV, true);
m_exposureMaterial.Set("_ExposureCompensationEV", autoExposureSettings.exposureCompensationEV, true);
m_exposureMaterial.Set("_ElapsedTime", elapsedTime, true);
m_exposureMaterial.Set("_Progressive", static_cast<int>(autoExposureSettings.progressive), true);
m_exposureMaterial.Set("_SpeedUp", autoExposureSettings.speedUp, true);
m_exposureMaterial.Set("_SpeedDown", autoExposureSettings.speedDown, true);
m_exposureMaterial.SetProperty("_LuminanceTexture", &luminanceTex.value(), true);
m_exposureMaterial.SetProperty("_MinLuminanceEV", autoExposureSettings.minLuminanceEV, true);
m_exposureMaterial.SetProperty("_MaxLuminanceEV", autoExposureSettings.maxLuminanceEV, true);
m_exposureMaterial.SetProperty("_ExposureCompensationEV", autoExposureSettings.exposureCompensationEV, true);
m_exposureMaterial.SetProperty("_ElapsedTime", elapsedTime, true);
m_exposureMaterial.SetProperty("_Progressive", static_cast<int>(autoExposureSettings.progressive), true);
m_exposureMaterial.SetProperty("_SpeedUp", autoExposureSettings.speedUp, true);
m_exposureMaterial.SetProperty("_SpeedDown", autoExposureSettings.speedDown, true);
m_renderer.Blit(p_pso, previousExposure, currentExposure, m_exposureMaterial);

// Apply the exposure to the final image
const auto exposureTex = currentExposure.GetAttachment<OvRendering::HAL::Texture>(OvRendering::Settings::EFramebufferAttachment::COLOR);
m_compensationMaterial.Set("_ExposureTexture", exposureTex, true);
m_compensationMaterial.SetProperty("_ExposureTexture", &exposureTex.value(), true);
m_renderer.Blit(p_pso, p_src, p_dst, m_compensationMaterial);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void OvCore::Rendering::PostProcess::BloomEffect::Draw(
const auto& bloomSettings = static_cast<const BloomSettings&>(p_settings);

// Step 1: Extract bright spots from the source
m_brightnessMaterial.Set("_Threshold", bloomSettings.threshold, true);
m_brightnessMaterial.SetProperty("_Threshold", bloomSettings.threshold, true);
m_renderer.Blit(p_pso, p_src, m_bloomPingPong[0], m_brightnessMaterial);

// Step 2: Apply gaussian blur on bright spots (horizontal and vertical)
Expand All @@ -60,17 +60,17 @@ void OvCore::Rendering::PostProcess::BloomEffect::Draw(
auto& currentSrc = horizontal ? m_bloomPingPong[0] : m_bloomPingPong[1];
auto& currentDst = horizontal ? m_bloomPingPong[1] : m_bloomPingPong[0];

m_blurMaterial.Set("_Horizontal", horizontal ? true : false, true);
m_blurMaterial.Set("_BlurSize", bloomSettings.radius, true);
m_blurMaterial.Set("_KernelSize", bloomSettings.kernelSize, true);
m_blurMaterial.SetProperty("_Horizontal", horizontal ? true : false, true);
m_blurMaterial.SetProperty("_BlurSize", bloomSettings.radius, true);
m_blurMaterial.SetProperty("_KernelSize", bloomSettings.kernelSize, true);
m_renderer.Blit(p_pso, currentSrc, currentDst, m_blurMaterial);

horizontal = !horizontal;
}

// Step 3: Combine bloom with original framebuffer
const auto bloomTex = m_bloomPingPong[0].GetAttachment<OvRendering::HAL::Texture>(OvRendering::Settings::EFramebufferAttachment::COLOR);
m_bloomMaterial.Set("_BloomTexture", bloomTex, true);
m_bloomMaterial.Set("_BloomIntensity", bloomSettings.intensity, true);
m_bloomMaterial.SetProperty("_BloomTexture", &bloomTex.value(), true);
m_bloomMaterial.SetProperty("_BloomIntensity", bloomSettings.intensity, true);
m_renderer.Blit(p_pso, p_src, p_dst, m_bloomMaterial);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void OvCore::Rendering::PostProcess::TonemappingEffect::Draw(
const auto& tonemappingSettings = static_cast<const TonemappingSettings&>(p_settings);

// Tonemapping
m_tonemappingMaterial.Set("_Exposure", tonemappingSettings.exposure, true);
m_tonemappingMaterial.Set("_Mode", static_cast<int>(tonemappingSettings.mode), true);
m_tonemappingMaterial.Set("_GammaCorrection", static_cast<int>(tonemappingSettings.gammaCorrection), true);
m_tonemappingMaterial.SetProperty("_Exposure", tonemappingSettings.exposure, true);
m_tonemappingMaterial.SetProperty("_Mode", static_cast<int>(tonemappingSettings.mode), true);
m_tonemappingMaterial.SetProperty("_GammaCorrection", static_cast<int>(tonemappingSettings.gammaCorrection), true);
m_renderer.Blit(p_pso, p_src, p_dst, m_tonemappingMaterial);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
* @licence: MIT
*/


#include <OvCore/ECS/Components/CMaterialRenderer.h>
#include <OvCore/Rendering/ShadowRenderFeature.h>

#include <OvDebug/Logger.h>

#include <OvRendering/Features/LightingRenderFeature.h>

constexpr uint8_t kMaxShadowMaps = 1;
Expand Down Expand Up @@ -38,8 +42,8 @@ void OvCore::Rendering::ShadowRenderFeature::OnBeforeDraw(OvRendering::Data::Pip
if (light.type == OvRendering::Settings::ELightType::DIRECTIONAL)
{
const auto shadowTex = light.GetShadowBuffer().GetAttachment<OvRendering::HAL::Texture>(OvRendering::Settings::EFramebufferAttachment::DEPTH);
material.Set("_ShadowMap", shadowTex, true); // Single use material property
material.Set("_LightSpaceMatrix", light.GetLightSpaceMatrix());
material.SetProperty("_ShadowMap", &shadowTex.value(), true); // Single use material property
material.SetProperty("_LightSpaceMatrix", light.GetLightSpaceMatrix());
++lightIndex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void OvCore::Rendering::ShadowRenderPass::Draw(OvRendering::Data::PipelineState
light.UpdateShadowData(frameDescriptor.camera.value());
const auto& lightSpaceMatrix = light.GetLightSpaceMatrix();
const auto& shadowBuffer = light.GetShadowBuffer();
m_shadowMaterial.Set("_LightSpaceMatrix", lightSpaceMatrix);
m_shadowMaterial.SetProperty("_LightSpaceMatrix", lightSpaceMatrix);
shadowBuffer.Bind();
m_renderer.SetViewport(0, 0, light.shadowMapResolution, light.shadowMapResolution);
m_renderer.Clear(true, true, true);
Expand Down Expand Up @@ -111,7 +111,7 @@ void OvCore::Rendering::ShadowRenderPass::DrawOpaques(
drawable.material = m_shadowMaterial;
drawable.stateMask = m_shadowMaterial.GenerateStateMask();

drawable.material.value().Set("_ModelMatrix", modelMatrix);
drawable.material.value().SetProperty("_ModelMatrix", modelMatrix);

m_renderer.DrawEntity(p_pso, drawable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* @licence: MIT
*/

#include "OvCore/Resources/Loaders/MaterialLoader.h"
#include <OvCore/Resources/Loaders/MaterialLoader.h>

#include <OvDebug/Logger.h>

OvCore::Resources::Material * OvCore::Resources::Loaders::MaterialLoader::Create(const std::string & p_path)
{
Expand Down
147 changes: 75 additions & 72 deletions Sources/Overload/OvCore/src/OvCore/Resources/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* @licence: MIT
*/

#include "OvCore/Resources/Material.h"
#include <OvCore/Resources/Material.h>

void OvCore::Resources::Material::OnSerialize(tinyxml2::XMLDocument & p_doc, tinyxml2::XMLNode * p_node)
void OvCore::Resources::Material::OnSerialize(tinyxml2::XMLDocument& p_doc, tinyxml2::XMLNode* p_node)
{
using namespace OvCore::Helpers;
using namespace OvRendering::Settings;
using namespace OvRendering::Resources;
using namespace OvMaths;

Serializer::SerializeShader(p_doc, p_node, "shader", m_shader);
Expand All @@ -33,54 +32,80 @@ void OvCore::Resources::Material::OnSerialize(tinyxml2::XMLDocument & p_doc, tin
tinyxml2::XMLNode* uniformsNode = p_doc.NewElement("uniforms");
p_node->InsertEndChild(uniformsNode);

for (const auto&[name, prop] : m_properties)
for (const auto& [name, prop] : m_properties)
{
auto& value = prop.value;

tinyxml2::XMLNode* uniform = p_doc.NewElement("uniform");
uniformsNode->InsertEndChild(uniform); // Instead of p_node, use uniformNode (To create)
uniformsNode->InsertEndChild(uniform);

const auto uniformInfo = m_shader->GetProgram().GetUniformInfo(name);

Serializer::SerializeString(p_doc, uniform, "name", name);

if (uniformInfo && value.has_value())
if (uniformInfo && !std::holds_alternative<std::monostate>(value))
{
switch (uniformInfo->type)
{
case EUniformType::BOOL:
if (value.type() == typeid(bool)) Serializer::SerializeInt(p_doc, uniform, "value", std::any_cast<bool>(value));
break;

case EUniformType::INT:
if (value.type() == typeid(int)) Serializer::SerializeInt(p_doc, uniform, "value", std::any_cast<int>(value));
break;

case EUniformType::FLOAT:
if (value.type() == typeid(float)) Serializer::SerializeFloat(p_doc, uniform, "value", std::any_cast<float>(value));
break;

case EUniformType::FLOAT_VEC2:
if (value.type() == typeid(FVector2)) Serializer::SerializeVec2(p_doc, uniform, "value", std::any_cast<FVector2>(value));
break;
auto visitor = [&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
using enum EUniformType;
const auto type = uniformInfo->type;

case EUniformType::FLOAT_VEC3:
if (value.type() == typeid(FVector3)) Serializer::SerializeVec3(p_doc, uniform, "value", std::any_cast<FVector3>(value));
break;

case EUniformType::FLOAT_VEC4:
if (value.type() == typeid(FVector4)) Serializer::SerializeVec4(p_doc, uniform, "value", std::any_cast<FVector4>(value));
break;
if constexpr (std::same_as<T, bool>)
{
if (type == BOOL)
{
Serializer::SerializeInt(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, int>)
{
if (type == INT)
{
Serializer::SerializeInt(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, float>)
{
if (type == FLOAT)
{
Serializer::SerializeFloat(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, FVector2>)
{
if (type == FLOAT_VEC2)
{
Serializer::SerializeVec2(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, FVector3>)
{
if (type == FLOAT_VEC3)
{
Serializer::SerializeVec3(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, FVector4>)
{
if (type == FLOAT_VEC4)
{
Serializer::SerializeVec4(p_doc, uniform, "value", arg);
}
}
else if constexpr (std::same_as<T, OvRendering::Resources::Texture*>)
{
if (type == SAMPLER_2D)
{
Serializer::SerializeTexture(p_doc, uniform, "value", arg);
}
}
// No need to handle TextureHandle* here as it's not serializable (only texture assets are)
};

case EUniformType::SAMPLER_2D:
if (value.type() == typeid(Texture*)) Serializer::SerializeTexture(p_doc, uniform, "value", std::any_cast<Texture*>(value));
break;
}
std::visit(visitor, value);
}
}
}

void OvCore::Resources::Material::OnDeserialize(tinyxml2::XMLDocument & p_doc, tinyxml2::XMLNode * p_node)
void OvCore::Resources::Material::OnDeserialize(tinyxml2::XMLDocument& p_doc, tinyxml2::XMLNode* p_node)
{
using namespace OvCore::Helpers;

Expand All @@ -101,13 +126,13 @@ void OvCore::Resources::Material::OnDeserialize(tinyxml2::XMLDocument & p_doc, t
}

/* We get the shader with Deserialize method */
OvRendering::Resources::Shader* deserializedShader = OvCore::Helpers::Serializer::DeserializeShader(p_doc, p_node, "shader");
const auto shader = Serializer::DeserializeShader(p_doc, p_node, "shader");

/* We verify that the shader is valid (Not null) */
if (deserializedShader)
if (shader)
{
/* If the shader is valid, we set it to the material (Modify m_shader pointer + Query + FillUniforms) */
SetShader(deserializedShader);
SetShader(shader);

tinyxml2::XMLNode* uniformsNode = p_node->FirstChildElement("uniforms"); // Access to "Uniforms" (Every uniform will be attached to "Uniforms")

Expand All @@ -130,41 +155,19 @@ void OvCore::Resources::Material::OnDeserialize(tinyxml2::XMLDocument & p_doc, t
/* Deserialization of the uniform value depending on the uniform type (Deserialization result to std::any) */
switch (uniformInfo->type)
{
case OvRendering::Settings::EUniformType::BOOL:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeBoolean(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::INT:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeInt(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::FLOAT:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeFloat(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::FLOAT_VEC2:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeVec2(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::FLOAT_VEC3:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeVec3(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::FLOAT_VEC4:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeVec4(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::FLOAT_MAT4:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeMat4(p_doc, uniform, "value");
break;

case OvRendering::Settings::EUniformType::SAMPLER_2D:
m_properties[uniformInfo->name] = OvCore::Helpers::Serializer::DeserializeTexture(p_doc, uniform, "value");
break;
using enum OvRendering::Settings::EUniformType;
case BOOL: SetProperty(uniformInfo->name, Serializer::DeserializeBoolean(p_doc, uniform, "value")); break;
case INT: SetProperty(uniformInfo->name, Serializer::DeserializeInt(p_doc, uniform, "value")); break;
case FLOAT: SetProperty(uniformInfo->name, Serializer::DeserializeFloat(p_doc, uniform, "value")); break;
case FLOAT_VEC2: SetProperty(uniformInfo->name, Serializer::DeserializeVec2(p_doc, uniform, "value")); break;
case FLOAT_VEC3: SetProperty(uniformInfo->name, Serializer::DeserializeVec3(p_doc, uniform, "value")); break;
case FLOAT_VEC4: SetProperty(uniformInfo->name, Serializer::DeserializeVec4(p_doc, uniform, "value")); break;
case FLOAT_MAT4: SetProperty(uniformInfo->name, Serializer::DeserializeMat4(p_doc, uniform, "value")); break;
case SAMPLER_2D: SetProperty(uniformInfo->name, Serializer::DeserializeTexture(p_doc, uniform, "value")); break;
}
}
}
}
}
}
}
}
18 changes: 14 additions & 4 deletions Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,22 @@ void OvEditor::Core::EditorActions::PropagateFileRename(std::string p_previousNa
{
if (auto texture = OvCore::Global::ServiceLocator::Get<OvCore::ResourceManagement::TextureManager>().GetResource(p_previousName, false))
{
for (auto[name, instance] : OvCore::Global::ServiceLocator::Get<OvCore::ResourceManagement::MaterialManager>().GetResources())
for (auto [name, instance] : OvCore::Global::ServiceLocator::Get<OvCore::ResourceManagement::MaterialManager>().GetResources())
{
if (instance)
for (auto&[name, prop] : instance->GetProperties())
if (prop.value.has_value() && prop.value.type() == typeid(OvRendering::Resources::Texture*))
if (std::any_cast<OvRendering::Resources::Texture*>(prop.value) == texture)
{
for (auto& [name, prop] : instance->GetProperties())
{
if (const auto pval = std::get_if<OvRendering::Resources::Texture*>(&prop.value); pval && *pval)
{
if (*pval == texture)
{
prop.value = static_cast<OvRendering::Resources::Texture*>(nullptr);
}
}
}
}
}

auto& assetView = EDITOR_PANEL(Panels::AssetView, "Asset View");
auto assetViewRes = assetView.GetResource();
Expand Down
12 changes: 6 additions & 6 deletions Sources/Overload/OvEditor/src/OvEditor/Panels/AssetView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ OvEditor::Panels::AssetView::AssetView

/* Default Material */
m_defaultMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Standard.ovfx"]);
m_defaultMaterial.Set("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f));
m_defaultMaterial.Set("u_Shininess", 100.0f);
m_defaultMaterial.Set<OvRendering::Resources::Texture*>("u_DiffuseMap", nullptr);
m_defaultMaterial.SetProperty("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f));
m_defaultMaterial.SetProperty("u_Shininess", 100.0f);
m_defaultMaterial.SetProperty("u_DiffuseMap", static_cast<OvRendering::Resources::Texture*>(nullptr));

/* Texture Material */
m_textureMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]);
m_textureMaterial.Set("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f));
m_textureMaterial.SetProperty("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f));
m_textureMaterial.SetBackfaceCulling(false);
m_textureMaterial.SetBlendable(true);
m_textureMaterial.Set<OvRendering::Resources::Texture*>("u_DiffuseMap", nullptr);
m_textureMaterial.SetProperty("u_DiffuseMap", static_cast<OvRendering::Resources::Texture*>(nullptr));

m_image->AddPlugin<OvUI::Plugins::DDTarget<std::pair<std::string, OvUI::Widgets::Layout::Group*>>>("File").DataReceivedEvent += [this](auto p_data)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ void OvEditor::Panels::AssetView::SetTexture(OvRendering::Resources::Texture& p_
m_assetActor->transform.SetLocalRotation(OvMaths::FQuaternion({ -90.0f, 0.0f, 0.0f }));
m_assetActor->transform.SetLocalScale(OvMaths::FVector3::One * 3.0f);
m_modelRenderer->SetModel(EDITOR_CONTEXT(editorResources)->GetModel("Plane"));
m_textureMaterial.Set<OvRendering::Resources::Texture*>("u_DiffuseMap", &p_texture);
m_textureMaterial.SetProperty("u_DiffuseMap", &p_texture);
m_materialRenderer->FillWithMaterial(m_textureMaterial);

m_cameraController.MoveToTarget(*m_assetActor);
Expand Down
Loading