refactor: add minimal material gpu binding

This commit is contained in:
2026-04-02 17:13:53 +08:00
parent 33f16597fa
commit dd08d8969e
7 changed files with 455 additions and 81 deletions

View File

@@ -2,9 +2,98 @@
#include <XCEngine/Resources/Shader/Shader.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <vector>
namespace XCEngine {
namespace Resources {
namespace {
constexpr size_t kMaterialConstantSlotSize = 16;
bool IsPackedMaterialPropertyType(MaterialPropertyType type) {
switch (type) {
case MaterialPropertyType::Float:
case MaterialPropertyType::Float2:
case MaterialPropertyType::Float3:
case MaterialPropertyType::Float4:
case MaterialPropertyType::Int:
case MaterialPropertyType::Int2:
case MaterialPropertyType::Int3:
case MaterialPropertyType::Int4:
case MaterialPropertyType::Bool:
return true;
case MaterialPropertyType::Texture:
case MaterialPropertyType::Cubemap:
default:
return false;
}
}
void RemoveTextureBindingByName(
Containers::Array<MaterialTextureBinding>& textureBindings,
const Containers::String& name) {
for (size_t bindingIndex = 0; bindingIndex < textureBindings.Size(); ++bindingIndex) {
if (textureBindings[bindingIndex].name == name) {
if (bindingIndex != textureBindings.Size() - 1) {
textureBindings[bindingIndex] = std::move(textureBindings.Back());
textureBindings[bindingIndex].slot = static_cast<Core::uint32>(bindingIndex);
}
textureBindings.PopBack();
break;
}
}
for (size_t bindingIndex = 0; bindingIndex < textureBindings.Size(); ++bindingIndex) {
textureBindings[bindingIndex].slot = static_cast<Core::uint32>(bindingIndex);
}
}
void WritePackedMaterialProperty(Core::uint8* destination, const MaterialProperty& property) {
std::memset(destination, 0, kMaterialConstantSlotSize);
switch (property.type) {
case MaterialPropertyType::Float:
std::memcpy(destination, property.value.floatValue, sizeof(float));
break;
case MaterialPropertyType::Float2:
std::memcpy(destination, property.value.floatValue, sizeof(float) * 2);
break;
case MaterialPropertyType::Float3:
std::memcpy(destination, property.value.floatValue, sizeof(float) * 3);
break;
case MaterialPropertyType::Float4:
std::memcpy(destination, property.value.floatValue, sizeof(float) * 4);
break;
case MaterialPropertyType::Int:
std::memcpy(destination, property.value.intValue, sizeof(Core::int32));
break;
case MaterialPropertyType::Int2:
std::memcpy(destination, property.value.intValue, sizeof(Core::int32) * 2);
break;
case MaterialPropertyType::Int3:
std::memcpy(destination, property.value.intValue, sizeof(Core::int32) * 3);
break;
case MaterialPropertyType::Int4:
std::memcpy(destination, property.value.intValue, sizeof(Core::int32) * 4);
break;
case MaterialPropertyType::Bool: {
const Core::uint32 boolValue = property.value.boolValue ? 1u : 0u;
std::memcpy(destination, &boolValue, sizeof(boolValue));
break;
}
case MaterialPropertyType::Texture:
case MaterialPropertyType::Cubemap:
default:
break;
}
}
} // namespace
Material::Material() = default;
Material::~Material() = default;
@@ -18,17 +107,19 @@ void Material::Release() {
m_properties.Clear();
m_textureBindings.Clear();
m_constantBufferData.Clear();
m_changeVersion = 1;
m_isValid = false;
UpdateMemorySize();
}
void Material::SetShader(const ResourceHandle<Shader>& shader) {
m_shader = shader;
UpdateMemorySize();
MarkChanged(false);
}
void Material::SetRenderQueue(Core::int32 renderQueue) {
m_renderQueue = renderQueue;
MarkChanged(false);
}
void Material::SetRenderQueue(MaterialRenderQueue renderQueue) {
@@ -37,19 +128,19 @@ void Material::SetRenderQueue(MaterialRenderQueue renderQueue) {
void Material::SetRenderState(const MaterialRenderState& renderState) {
m_renderState = renderState;
UpdateMemorySize();
MarkChanged(false);
}
void Material::SetShaderPass(const Containers::String& shaderPass) {
m_shaderPass = shaderPass;
UpdateMemorySize();
MarkChanged(false);
}
void Material::SetTag(const Containers::String& name, const Containers::String& value) {
for (MaterialTagEntry& tag : m_tags) {
if (tag.name == name) {
tag.value = value;
UpdateMemorySize();
MarkChanged(false);
return;
}
}
@@ -58,7 +149,7 @@ void Material::SetTag(const Containers::String& name, const Containers::String&
tag.name = name;
tag.value = value;
m_tags.PushBack(tag);
UpdateMemorySize();
MarkChanged(false);
}
Containers::String Material::GetTag(const Containers::String& name) const {
@@ -88,7 +179,7 @@ void Material::RemoveTag(const Containers::String& name) {
m_tags[tagIndex] = std::move(m_tags.Back());
}
m_tags.PopBack();
UpdateMemorySize();
MarkChanged(false);
return;
}
}
@@ -96,7 +187,7 @@ void Material::RemoveTag(const Containers::String& name) {
void Material::ClearTags() {
m_tags.Clear();
UpdateMemorySize();
MarkChanged(false);
}
Containers::String Material::GetTagName(Core::uint32 index) const {
@@ -108,16 +199,18 @@ Containers::String Material::GetTagValue(Core::uint32 index) const {
}
void Material::SetFloat(const Containers::String& name, float value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Float;
prop.value.floatValue[0] = value;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetFloat2(const Containers::String& name, const Math::Vector2& value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Float2;
@@ -125,10 +218,11 @@ void Material::SetFloat2(const Containers::String& name, const Math::Vector2& va
prop.value.floatValue[1] = value.y;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetFloat3(const Containers::String& name, const Math::Vector3& value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Float3;
@@ -137,10 +231,11 @@ void Material::SetFloat3(const Containers::String& name, const Math::Vector3& va
prop.value.floatValue[2] = value.z;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetFloat4(const Containers::String& name, const Math::Vector4& value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Float4;
@@ -150,27 +245,29 @@ void Material::SetFloat4(const Containers::String& name, const Math::Vector4& va
prop.value.floatValue[3] = value.w;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetInt(const Containers::String& name, Core::int32 value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Int;
prop.value.intValue[0] = value;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetBool(const Containers::String& name, bool value) {
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Bool;
prop.value.boolValue = value;
prop.refCount = 1;
m_properties.Insert(name, prop);
UpdateMemorySize();
MarkChanged(true);
}
void Material::SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture) {
@@ -183,7 +280,7 @@ void Material::SetTexture(const Containers::String& name, const ResourceHandle<T
for (auto& binding : m_textureBindings) {
if (binding.name == name) {
binding.texture = texture;
UpdateMemorySize();
MarkChanged(false);
return;
}
}
@@ -193,7 +290,7 @@ void Material::SetTexture(const Containers::String& name, const ResourceHandle<T
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
binding.texture = texture;
m_textureBindings.PushBack(binding);
UpdateMemorySize();
MarkChanged(false);
}
float Material::GetFloat(const Containers::String& name) const {
@@ -273,7 +370,33 @@ std::vector<MaterialProperty> Material::GetProperties() const {
}
void Material::UpdateConstantBuffer() {
std::vector<const MaterialProperty*> packedProperties;
const auto pairs = m_properties.GetPairs();
packedProperties.reserve(pairs.Size());
for (const auto& pair : pairs) {
if (IsPackedMaterialPropertyType(pair.second.type)) {
packedProperties.push_back(&pair.second);
}
}
std::sort(
packedProperties.begin(),
packedProperties.end(),
[](const MaterialProperty* left, const MaterialProperty* right) {
return std::strcmp(left->name.CStr(), right->name.CStr()) < 0;
});
m_constantBufferData.Clear();
m_constantBufferData.Resize(packedProperties.size() * kMaterialConstantSlotSize);
if (!packedProperties.empty()) {
std::memset(m_constantBufferData.Data(), 0, m_constantBufferData.Size());
}
for (size_t propertyIndex = 0; propertyIndex < packedProperties.size(); ++propertyIndex) {
WritePackedMaterialProperty(
m_constantBufferData.Data() + propertyIndex * kMaterialConstantSlotSize,
*packedProperties[propertyIndex]);
}
UpdateMemorySize();
}
@@ -286,15 +409,35 @@ bool Material::HasProperty(const Containers::String& name) const {
}
void Material::RemoveProperty(const Containers::String& name) {
const MaterialProperty* property = m_properties.Find(name);
const bool removeTextureBinding =
property != nullptr &&
(property->type == MaterialPropertyType::Texture || property->type == MaterialPropertyType::Cubemap);
m_properties.Erase(name);
UpdateMemorySize();
if (removeTextureBinding) {
RemoveTextureBindingByName(m_textureBindings, name);
}
MarkChanged(true);
}
void Material::ClearAllProperties() {
m_properties.Clear();
m_textureBindings.Clear();
m_constantBufferData.Clear();
UpdateMemorySize();
MarkChanged(false);
}
void Material::MarkChanged(bool updateConstantBuffer) {
if (updateConstantBuffer) {
UpdateConstantBuffer();
} else {
UpdateMemorySize();
}
++m_changeVersion;
}
void Material::UpdateMemorySize() {