2026-03-24 16:14:05 +08:00
|
|
|
#include <XCEngine/Resources/Material/Material.h>
|
|
|
|
|
#include <XCEngine/Resources/Shader/Shader.h>
|
|
|
|
|
#include <XCEngine/Core/Asset/ResourceManager.h>
|
2026-03-17 22:32:27 +08:00
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Resources {
|
|
|
|
|
|
|
|
|
|
Material::Material() = default;
|
|
|
|
|
|
|
|
|
|
Material::~Material() = default;
|
|
|
|
|
|
|
|
|
|
void Material::Release() {
|
|
|
|
|
m_shader.Reset();
|
2026-03-27 00:30:49 +08:00
|
|
|
m_renderQueue = static_cast<Core::int32>(MaterialRenderQueue::Geometry);
|
2026-03-27 12:18:04 +08:00
|
|
|
m_renderState = MaterialRenderState();
|
2026-03-27 00:30:49 +08:00
|
|
|
m_shaderPass.Clear();
|
|
|
|
|
m_tags.Clear();
|
2026-03-17 22:32:27 +08:00
|
|
|
m_properties.Clear();
|
|
|
|
|
m_textureBindings.Clear();
|
|
|
|
|
m_constantBufferData.Clear();
|
|
|
|
|
m_isValid = false;
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetShader(const ResourceHandle<Shader>& shader) {
|
|
|
|
|
m_shader = shader;
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:30:49 +08:00
|
|
|
void Material::SetRenderQueue(Core::int32 renderQueue) {
|
|
|
|
|
m_renderQueue = renderQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetRenderQueue(MaterialRenderQueue renderQueue) {
|
|
|
|
|
SetRenderQueue(static_cast<Core::int32>(renderQueue));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 12:18:04 +08:00
|
|
|
void Material::SetRenderState(const MaterialRenderState& renderState) {
|
|
|
|
|
m_renderState = renderState;
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:30:49 +08:00
|
|
|
void Material::SetShaderPass(const Containers::String& shaderPass) {
|
|
|
|
|
m_shaderPass = shaderPass;
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetTag(const Containers::String& name, const Containers::String& value) {
|
|
|
|
|
for (TagEntry& tag : m_tags) {
|
|
|
|
|
if (tag.name == name) {
|
|
|
|
|
tag.value = value;
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TagEntry tag;
|
|
|
|
|
tag.name = name;
|
|
|
|
|
tag.value = value;
|
|
|
|
|
m_tags.PushBack(tag);
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Containers::String Material::GetTag(const Containers::String& name) const {
|
|
|
|
|
for (const TagEntry& tag : m_tags) {
|
|
|
|
|
if (tag.name == name) {
|
|
|
|
|
return tag.value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Containers::String();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Material::HasTag(const Containers::String& name) const {
|
|
|
|
|
for (const TagEntry& tag : m_tags) {
|
|
|
|
|
if (tag.name == name) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::RemoveTag(const Containers::String& name) {
|
|
|
|
|
for (size_t tagIndex = 0; tagIndex < m_tags.Size(); ++tagIndex) {
|
|
|
|
|
if (m_tags[tagIndex].name == name) {
|
|
|
|
|
if (tagIndex != m_tags.Size() - 1) {
|
|
|
|
|
m_tags[tagIndex] = std::move(m_tags.Back());
|
|
|
|
|
}
|
|
|
|
|
m_tags.PopBack();
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::ClearTags() {
|
|
|
|
|
m_tags.Clear();
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 22:32:27 +08:00
|
|
|
void Material::SetFloat(const Containers::String& name, float value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Float;
|
|
|
|
|
prop.value.floatValue[0] = value;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetFloat2(const Containers::String& name, const Math::Vector2& value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Float2;
|
|
|
|
|
prop.value.floatValue[0] = value.x;
|
|
|
|
|
prop.value.floatValue[1] = value.y;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetFloat3(const Containers::String& name, const Math::Vector3& value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Float3;
|
|
|
|
|
prop.value.floatValue[0] = value.x;
|
|
|
|
|
prop.value.floatValue[1] = value.y;
|
|
|
|
|
prop.value.floatValue[2] = value.z;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetFloat4(const Containers::String& name, const Math::Vector4& value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Float4;
|
|
|
|
|
prop.value.floatValue[0] = value.x;
|
|
|
|
|
prop.value.floatValue[1] = value.y;
|
|
|
|
|
prop.value.floatValue[2] = value.z;
|
|
|
|
|
prop.value.floatValue[3] = value.w;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetInt(const Containers::String& name, Core::int32 value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Int;
|
|
|
|
|
prop.value.intValue[0] = value;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetBool(const Containers::String& name, bool value) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Bool;
|
|
|
|
|
prop.value.boolValue = value;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture) {
|
|
|
|
|
MaterialProperty prop;
|
|
|
|
|
prop.name = name;
|
|
|
|
|
prop.type = MaterialPropertyType::Texture;
|
|
|
|
|
prop.refCount = 1;
|
|
|
|
|
m_properties.Insert(name, prop);
|
2026-03-26 16:22:24 +08:00
|
|
|
|
|
|
|
|
for (auto& binding : m_textureBindings) {
|
|
|
|
|
if (binding.name == name) {
|
|
|
|
|
binding.texture = texture;
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 22:32:27 +08:00
|
|
|
TextureBinding binding;
|
|
|
|
|
binding.name = name;
|
|
|
|
|
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
|
|
|
|
|
binding.texture = texture;
|
|
|
|
|
m_textureBindings.PushBack(binding);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Material::GetFloat(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Float) {
|
|
|
|
|
return prop->value.floatValue[0];
|
|
|
|
|
}
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Math::Vector2 Material::GetFloat2(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Float2) {
|
|
|
|
|
return Math::Vector2(prop->value.floatValue[0], prop->value.floatValue[1]);
|
|
|
|
|
}
|
|
|
|
|
return Math::Vector2::Zero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Math::Vector3 Material::GetFloat3(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Float3) {
|
|
|
|
|
return Math::Vector3(prop->value.floatValue[0], prop->value.floatValue[1], prop->value.floatValue[2]);
|
|
|
|
|
}
|
|
|
|
|
return Math::Vector3::Zero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Math::Vector4 Material::GetFloat4(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Float4) {
|
|
|
|
|
return Math::Vector4(prop->value.floatValue[0], prop->value.floatValue[1],
|
|
|
|
|
prop->value.floatValue[2], prop->value.floatValue[3]);
|
|
|
|
|
}
|
|
|
|
|
return Math::Vector4::Zero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::int32 Material::GetInt(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Int) {
|
|
|
|
|
return prop->value.intValue[0];
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Material::GetBool(const Containers::String& name) const {
|
|
|
|
|
auto* prop = m_properties.Find(name);
|
|
|
|
|
if (prop && prop->type == MaterialPropertyType::Bool) {
|
|
|
|
|
return prop->value.boolValue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResourceHandle<Texture> Material::GetTexture(const Containers::String& name) const {
|
|
|
|
|
for (const auto& binding : m_textureBindings) {
|
|
|
|
|
if (binding.name == name) {
|
|
|
|
|
return binding.texture;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ResourceHandle<Texture>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::UpdateConstantBuffer() {
|
|
|
|
|
m_constantBufferData.Clear();
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:30:49 +08:00
|
|
|
void Material::RecalculateMemorySize() {
|
|
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 22:32:27 +08:00
|
|
|
bool Material::HasProperty(const Containers::String& name) const {
|
|
|
|
|
return m_properties.Contains(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::RemoveProperty(const Containers::String& name) {
|
|
|
|
|
m_properties.Erase(name);
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::ClearAllProperties() {
|
|
|
|
|
m_properties.Clear();
|
|
|
|
|
m_textureBindings.Clear();
|
|
|
|
|
m_constantBufferData.Clear();
|
2026-03-26 16:22:24 +08:00
|
|
|
UpdateMemorySize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Material::UpdateMemorySize() {
|
|
|
|
|
m_memorySize = m_constantBufferData.Size() +
|
2026-03-27 12:18:04 +08:00
|
|
|
sizeof(MaterialRenderState) +
|
2026-03-27 00:30:49 +08:00
|
|
|
m_shaderPass.Length() +
|
|
|
|
|
m_tags.Size() * sizeof(TagEntry) +
|
2026-03-26 16:22:24 +08:00
|
|
|
m_textureBindings.Size() * sizeof(TextureBinding) +
|
|
|
|
|
m_properties.Size() * sizeof(MaterialProperty) +
|
|
|
|
|
m_name.Length() +
|
|
|
|
|
m_path.Length();
|
|
|
|
|
|
2026-03-27 00:30:49 +08:00
|
|
|
for (const TagEntry& tag : m_tags) {
|
|
|
|
|
m_memorySize += tag.name.Length();
|
|
|
|
|
m_memorySize += tag.value.Length();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 16:22:24 +08:00
|
|
|
for (const auto& binding : m_textureBindings) {
|
|
|
|
|
m_memorySize += binding.name.Length();
|
|
|
|
|
}
|
2026-03-17 22:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Resources
|
|
|
|
|
} // namespace XCEngine
|