Formalize material schema and constant layout contract

This commit is contained in:
2026-04-03 16:49:30 +08:00
parent 052ac28aa3
commit 03bd755e0a
10 changed files with 1841 additions and 87 deletions

View File

@@ -3,8 +3,11 @@
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
namespace XCEngine {
@@ -14,6 +17,10 @@ namespace {
constexpr size_t kMaterialConstantSlotSize = 16;
bool HasVirtualPathScheme(const Containers::String& path) {
return std::string(path.CStr()).find("://") != std::string::npos;
}
bool IsPackedMaterialPropertyType(MaterialPropertyType type) {
switch (type) {
case MaterialPropertyType::Float:
@@ -33,6 +40,28 @@ bool IsPackedMaterialPropertyType(MaterialPropertyType type) {
}
}
Core::uint32 GetPackedMaterialPropertySize(MaterialPropertyType type) {
switch (type) {
case MaterialPropertyType::Float:
case MaterialPropertyType::Int:
case MaterialPropertyType::Bool:
return sizeof(Core::uint32);
case MaterialPropertyType::Float2:
case MaterialPropertyType::Int2:
return sizeof(Core::uint32) * 2;
case MaterialPropertyType::Float3:
case MaterialPropertyType::Int3:
return sizeof(Core::uint32) * 3;
case MaterialPropertyType::Float4:
case MaterialPropertyType::Int4:
return sizeof(Core::uint32) * 4;
case MaterialPropertyType::Texture:
case MaterialPropertyType::Cubemap:
default:
return 0;
}
}
void RemoveTextureBindingByName(
Containers::Array<MaterialTextureBinding>& textureBindings,
const Containers::String& name) {
@@ -53,14 +82,187 @@ void RemoveTextureBindingByName(
}
void EnsureTextureProperty(Containers::HashMap<Containers::String, MaterialProperty>& properties,
const Containers::String& name) {
const Containers::String& name,
MaterialPropertyType type = MaterialPropertyType::Texture) {
MaterialProperty prop;
prop.name = name;
prop.type = MaterialPropertyType::Texture;
prop.type = type;
prop.refCount = 1;
properties.Insert(name, prop);
}
bool IsTextureMaterialPropertyType(MaterialPropertyType type) {
return type == MaterialPropertyType::Texture || type == MaterialPropertyType::Cubemap;
}
MaterialPropertyType GetMaterialPropertyTypeForShaderProperty(ShaderPropertyType type) {
switch (type) {
case ShaderPropertyType::Float:
case ShaderPropertyType::Range:
return MaterialPropertyType::Float;
case ShaderPropertyType::Int:
return MaterialPropertyType::Int;
case ShaderPropertyType::Vector:
case ShaderPropertyType::Color:
return MaterialPropertyType::Float4;
case ShaderPropertyType::TextureCube:
return MaterialPropertyType::Cubemap;
case ShaderPropertyType::Texture2D:
default:
return MaterialPropertyType::Texture;
}
}
bool IsMaterialPropertyCompatibleWithShaderProperty(
MaterialPropertyType materialType,
ShaderPropertyType shaderType) {
switch (shaderType) {
case ShaderPropertyType::Float:
case ShaderPropertyType::Range:
return materialType == MaterialPropertyType::Float;
case ShaderPropertyType::Int:
return materialType == MaterialPropertyType::Int;
case ShaderPropertyType::Vector:
case ShaderPropertyType::Color:
return materialType == MaterialPropertyType::Float4;
case ShaderPropertyType::Texture2D:
return materialType == MaterialPropertyType::Texture;
case ShaderPropertyType::TextureCube:
return materialType == MaterialPropertyType::Texture ||
materialType == MaterialPropertyType::Cubemap;
default:
return false;
}
}
std::string TrimCopy(const std::string& text) {
const auto begin = std::find_if_not(text.begin(), text.end(), [](unsigned char ch) {
return std::isspace(ch) != 0;
});
if (begin == text.end()) {
return std::string();
}
const auto end = std::find_if_not(text.rbegin(), text.rend(), [](unsigned char ch) {
return std::isspace(ch) != 0;
}).base();
return std::string(begin, end);
}
bool TryParseFloatList(const Containers::String& value,
float* outValues,
size_t maxValues,
size_t& outCount) {
outCount = 0;
std::string text = TrimCopy(std::string(value.CStr()));
if (text.empty()) {
return false;
}
if ((text.front() == '(' && text.back() == ')') ||
(text.front() == '[' && text.back() == ']') ||
(text.front() == '{' && text.back() == '}')) {
text = text.substr(1, text.size() - 2);
}
const char* cursor = text.c_str();
char* endPtr = nullptr;
while (*cursor != '\0' && outCount < maxValues) {
while (*cursor != '\0' &&
(std::isspace(static_cast<unsigned char>(*cursor)) != 0 || *cursor == ',')) {
++cursor;
}
if (*cursor == '\0') {
break;
}
const float parsed = std::strtof(cursor, &endPtr);
if (endPtr == cursor) {
return false;
}
outValues[outCount++] = parsed;
cursor = endPtr;
}
while (*cursor != '\0') {
if (std::isspace(static_cast<unsigned char>(*cursor)) == 0 && *cursor != ',') {
return false;
}
++cursor;
}
return outCount > 0;
}
bool TryParseFloatDefault(const Containers::String& value, float& outValue) {
float values[4] = {};
size_t count = 0;
if (!TryParseFloatList(value, values, 4, count)) {
return false;
}
outValue = values[0];
return true;
}
bool TryParseIntDefault(const Containers::String& value, Core::int32& outValue) {
const std::string text = TrimCopy(std::string(value.CStr()));
if (text.empty()) {
return false;
}
char* endPtr = nullptr;
const long parsed = std::strtol(text.c_str(), &endPtr, 10);
if (endPtr == text.c_str()) {
return false;
}
while (*endPtr != '\0') {
if (std::isspace(static_cast<unsigned char>(*endPtr)) == 0) {
return false;
}
++endPtr;
}
outValue = static_cast<Core::int32>(parsed);
return true;
}
bool TryBuildDefaultMaterialProperty(const ShaderPropertyDesc& shaderProperty,
MaterialProperty& outProperty) {
outProperty = MaterialProperty();
outProperty.name = shaderProperty.name;
outProperty.type = GetMaterialPropertyTypeForShaderProperty(shaderProperty.type);
outProperty.refCount = 1;
switch (shaderProperty.type) {
case ShaderPropertyType::Float:
case ShaderPropertyType::Range:
TryParseFloatDefault(shaderProperty.defaultValue, outProperty.value.floatValue[0]);
return true;
case ShaderPropertyType::Int:
TryParseIntDefault(shaderProperty.defaultValue, outProperty.value.intValue[0]);
return true;
case ShaderPropertyType::Vector:
case ShaderPropertyType::Color: {
float values[4] = {};
size_t count = 0;
if (TryParseFloatList(shaderProperty.defaultValue, values, 4, count)) {
for (size_t index = 0; index < count && index < 4; ++index) {
outProperty.value.floatValue[index] = values[index];
}
}
return true;
}
case ShaderPropertyType::Texture2D:
case ShaderPropertyType::TextureCube:
return true;
default:
return false;
}
}
void WritePackedMaterialProperty(Core::uint8* destination, const MaterialProperty& property) {
std::memset(destination, 0, kMaterialConstantSlotSize);
@@ -114,6 +316,7 @@ void Material::Release() {
m_shaderPass.Clear();
m_tags.Clear();
m_properties.Clear();
m_constantLayout.Clear();
m_textureBindings.Clear();
m_constantBufferData.Clear();
m_changeVersion = 1;
@@ -123,7 +326,8 @@ void Material::Release() {
void Material::SetShader(const ResourceHandle<Shader>& shader) {
m_shader = shader;
MarkChanged(false);
SyncShaderSchemaProperties(true);
MarkChanged(true);
}
void Material::SetRenderQueue(Core::int32 renderQueue) {
@@ -208,6 +412,10 @@ Containers::String Material::GetTagValue(Core::uint32 index) const {
}
void Material::SetFloat(const Containers::String& name, float value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Float)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -219,6 +427,10 @@ void Material::SetFloat(const Containers::String& name, float value) {
}
void Material::SetFloat2(const Containers::String& name, const Math::Vector2& value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Float2)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -231,6 +443,10 @@ void Material::SetFloat2(const Containers::String& name, const Math::Vector2& va
}
void Material::SetFloat3(const Containers::String& name, const Math::Vector3& value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Float3)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -244,6 +460,10 @@ void Material::SetFloat3(const Containers::String& name, const Math::Vector3& va
}
void Material::SetFloat4(const Containers::String& name, const Math::Vector4& value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Float4)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -258,6 +478,10 @@ void Material::SetFloat4(const Containers::String& name, const Math::Vector4& va
}
void Material::SetInt(const Containers::String& name, Core::int32 value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Int)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -269,6 +493,10 @@ void Material::SetInt(const Containers::String& name, Core::int32 value) {
}
void Material::SetBool(const Containers::String& name, bool value) {
if (!CanAssignPropertyType(name, MaterialPropertyType::Bool)) {
return;
}
RemoveTextureBindingByName(m_textureBindings, name);
MaterialProperty prop;
prop.name = name;
@@ -280,38 +508,27 @@ void Material::SetBool(const Containers::String& name, bool value) {
}
void Material::SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture) {
EnsureTextureProperty(m_properties, name);
const ShaderPropertyDesc* shaderProperty = FindShaderPropertyDesc(name);
const MaterialPropertyType propertyType =
shaderProperty != nullptr
? GetMaterialPropertyTypeForShaderProperty(shaderProperty->type)
: MaterialPropertyType::Texture;
if (!CanAssignPropertyType(name, propertyType)) {
return;
}
EnsureTextureProperty(m_properties, name, propertyType);
AssetRef textureRef;
Containers::String texturePath = texture.Get() != nullptr ? texture->GetPath() : Containers::String();
if (!texturePath.Empty() && !HasVirtualPathScheme(texturePath)) {
ResourceManager::Get().TryGetAssetRef(texturePath, ResourceType::Texture, textureRef);
}
for (auto& binding : m_textureBindings) {
if (binding.name == name) {
binding.texture = texture;
binding.texturePath = texture.Get() != nullptr ? texture->GetPath() : Containers::String();
binding.pendingLoad.reset();
MarkChanged(false);
return;
}
}
MaterialTextureBinding binding;
binding.name = name;
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
binding.texture = texture;
binding.texturePath = texture.Get() != nullptr ? texture->GetPath() : Containers::String();
m_textureBindings.PushBack(binding);
MarkChanged(false);
}
void Material::SetTexturePath(const Containers::String& name, const Containers::String& texturePath) {
if (texturePath.Empty()) {
RemoveProperty(name);
return;
}
EnsureTextureProperty(m_properties, name);
for (auto& binding : m_textureBindings) {
if (binding.name == name) {
binding.texture.Reset();
binding.textureRef = textureRef;
binding.texturePath = texturePath;
binding.pendingLoad.reset();
MarkChanged(false);
@@ -322,6 +539,84 @@ void Material::SetTexturePath(const Containers::String& name, const Containers::
MaterialTextureBinding binding;
binding.name = name;
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
binding.texture = texture;
binding.textureRef = textureRef;
binding.texturePath = texturePath;
m_textureBindings.PushBack(binding);
MarkChanged(false);
}
void Material::SetTextureAssetRef(const Containers::String& name,
const AssetRef& textureRef,
const Containers::String& texturePath) {
if (!textureRef.IsValid() && texturePath.Empty()) {
RemoveProperty(name);
return;
}
const ShaderPropertyDesc* shaderProperty = FindShaderPropertyDesc(name);
const MaterialPropertyType propertyType =
shaderProperty != nullptr
? GetMaterialPropertyTypeForShaderProperty(shaderProperty->type)
: MaterialPropertyType::Texture;
if (!CanAssignPropertyType(name, propertyType)) {
return;
}
EnsureTextureProperty(m_properties, name, propertyType);
for (auto& binding : m_textureBindings) {
if (binding.name == name) {
binding.texture.Reset();
binding.textureRef = textureRef;
binding.texturePath = texturePath;
binding.pendingLoad.reset();
MarkChanged(false);
return;
}
}
MaterialTextureBinding binding;
binding.name = name;
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
binding.textureRef = textureRef;
binding.texturePath = texturePath;
m_textureBindings.PushBack(binding);
MarkChanged(false);
}
void Material::SetTexturePath(const Containers::String& name, const Containers::String& texturePath) {
if (texturePath.Empty()) {
RemoveProperty(name);
return;
}
const ShaderPropertyDesc* shaderProperty = FindShaderPropertyDesc(name);
const MaterialPropertyType propertyType =
shaderProperty != nullptr
? GetMaterialPropertyTypeForShaderProperty(shaderProperty->type)
: MaterialPropertyType::Texture;
if (!CanAssignPropertyType(name, propertyType)) {
return;
}
EnsureTextureProperty(m_properties, name, propertyType);
for (auto& binding : m_textureBindings) {
if (binding.name == name) {
binding.texture.Reset();
binding.textureRef.Reset();
binding.texturePath = texturePath;
binding.pendingLoad.reset();
MarkChanged(false);
return;
}
}
MaterialTextureBinding binding;
binding.name = name;
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
binding.textureRef.Reset();
binding.texturePath = texturePath;
m_textureBindings.PushBack(binding);
MarkChanged(false);
@@ -384,7 +679,7 @@ ResourceHandle<Texture> Material::GetTexture(const Containers::String& name) con
if (binding.name == name) {
if (binding.texture.Get() == nullptr &&
binding.pendingLoad == nullptr &&
!binding.texturePath.Empty()) {
(!binding.texturePath.Empty() || binding.textureRef.IsValid())) {
material->BeginAsyncTextureLoad(bindingIndex);
}
return binding.texture;
@@ -397,10 +692,18 @@ Containers::String Material::GetTextureBindingName(Core::uint32 index) const {
return index < m_textureBindings.Size() ? m_textureBindings[index].name : Containers::String();
}
AssetRef Material::GetTextureBindingAssetRef(Core::uint32 index) const {
return index < m_textureBindings.Size() ? m_textureBindings[index].textureRef : AssetRef();
}
Containers::String Material::GetTextureBindingPath(Core::uint32 index) const {
return index < m_textureBindings.Size() ? m_textureBindings[index].texturePath : Containers::String();
}
ResourceHandle<Texture> Material::GetTextureBindingLoadedTexture(Core::uint32 index) const {
return index < m_textureBindings.Size() ? m_textureBindings[index].texture : ResourceHandle<Texture>();
}
ResourceHandle<Texture> Material::GetTextureBindingTexture(Core::uint32 index) const {
Material* material = const_cast<Material*>(this);
material->ResolvePendingTextureBinding(index);
@@ -408,7 +711,7 @@ ResourceHandle<Texture> Material::GetTextureBindingTexture(Core::uint32 index) c
MaterialTextureBinding& binding = material->m_textureBindings[index];
if (binding.texture.Get() == nullptr &&
binding.pendingLoad == nullptr &&
!binding.texturePath.Empty()) {
(!binding.texturePath.Empty() || binding.textureRef.IsValid())) {
material->BeginAsyncTextureLoad(index);
}
return binding.texture;
@@ -427,25 +730,63 @@ std::vector<MaterialProperty> Material::GetProperties() const {
return properties;
}
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);
const MaterialConstantFieldDesc* Material::FindConstantField(const Containers::String& name) const {
for (const MaterialConstantFieldDesc& field : m_constantLayout) {
if (field.name == name) {
return &field;
}
}
std::sort(
packedProperties.begin(),
packedProperties.end(),
[](const MaterialProperty* left, const MaterialProperty* right) {
return std::strcmp(left->name.CStr(), right->name.CStr()) < 0;
});
return nullptr;
}
void Material::UpdateConstantBuffer() {
std::vector<MaterialProperty> packedProperties;
if (m_shader.Get() != nullptr && !m_shader->GetProperties().Empty()) {
packedProperties.reserve(m_shader->GetProperties().Size());
for (const ShaderPropertyDesc& shaderProperty : m_shader->GetProperties()) {
const MaterialProperty* property = m_properties.Find(shaderProperty.name);
if (property == nullptr ||
!IsPackedMaterialPropertyType(property->type) ||
!IsMaterialPropertyCompatibleWithShaderProperty(property->type, shaderProperty.type)) {
continue;
}
packedProperties.push_back(*property);
}
} else {
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_constantLayout.Clear();
m_constantLayout.Reserve(packedProperties.size());
Core::uint32 currentOffset = 0;
for (const MaterialProperty& property : packedProperties) {
MaterialConstantFieldDesc field;
field.name = property.name;
field.type = property.type;
field.offset = currentOffset;
field.size = GetPackedMaterialPropertySize(property.type);
field.alignedSize = static_cast<Core::uint32>(kMaterialConstantSlotSize);
m_constantLayout.PushBack(field);
currentOffset += field.alignedSize;
}
m_constantBufferData.Clear();
m_constantBufferData.Resize(packedProperties.size() * kMaterialConstantSlotSize);
m_constantBufferData.Resize(static_cast<size_t>(currentOffset));
if (!packedProperties.empty()) {
std::memset(m_constantBufferData.Data(), 0, m_constantBufferData.Size());
}
@@ -453,7 +794,7 @@ void Material::UpdateConstantBuffer() {
for (size_t propertyIndex = 0; propertyIndex < packedProperties.size(); ++propertyIndex) {
WritePackedMaterialProperty(
m_constantBufferData.Data() + propertyIndex * kMaterialConstantSlotSize,
*packedProperties[propertyIndex]);
packedProperties[propertyIndex]);
}
UpdateMemorySize();
}
@@ -468,7 +809,15 @@ void Material::BeginAsyncTextureLoad(Core::uint32 index) {
}
MaterialTextureBinding& binding = m_textureBindings[index];
if (binding.texture.Get() != nullptr || binding.texturePath.Empty() || binding.pendingLoad != nullptr) {
if (binding.texture.Get() != nullptr || binding.pendingLoad != nullptr) {
return;
}
if (binding.texturePath.Empty() && binding.textureRef.IsValid()) {
ResourceManager::Get().TryResolveAssetPath(binding.textureRef, binding.texturePath);
}
if (binding.texturePath.Empty()) {
return;
}
@@ -505,7 +854,9 @@ void Material::ResolvePendingTextureBinding(Core::uint32 index) {
binding.texture = ResourceHandle<Texture>(static_cast<Texture*>(completedLoad->resource));
if (binding.texture.Get() != nullptr) {
binding.texturePath = binding.texture->GetPath();
if (binding.texturePath.Empty()) {
binding.texturePath = binding.texture->GetPath();
}
}
}
@@ -520,6 +871,11 @@ bool Material::HasProperty(const Containers::String& name) const {
}
void Material::RemoveProperty(const Containers::String& name) {
if (ResetPropertyToShaderDefault(name)) {
MarkChanged(true);
return;
}
const MaterialProperty* property = m_properties.Find(name);
const bool removeTextureBinding =
property != nullptr &&
@@ -536,9 +892,79 @@ void Material::RemoveProperty(const Containers::String& name) {
void Material::ClearAllProperties() {
m_properties.Clear();
m_constantLayout.Clear();
m_textureBindings.Clear();
m_constantBufferData.Clear();
MarkChanged(false);
SyncShaderSchemaProperties(false);
MarkChanged(true);
}
const ShaderPropertyDesc* Material::FindShaderPropertyDesc(const Containers::String& name) const {
if (m_shader.Get() == nullptr) {
return nullptr;
}
return m_shader->FindProperty(name);
}
bool Material::CanAssignPropertyType(const Containers::String& name, MaterialPropertyType type) const {
const ShaderPropertyDesc* shaderProperty = FindShaderPropertyDesc(name);
if (shaderProperty == nullptr) {
return m_shader.Get() == nullptr;
}
return IsMaterialPropertyCompatibleWithShaderProperty(type, shaderProperty->type);
}
bool Material::ResetPropertyToShaderDefault(const Containers::String& name) {
const ShaderPropertyDesc* shaderProperty = FindShaderPropertyDesc(name);
if (shaderProperty == nullptr) {
return false;
}
MaterialProperty defaultProperty;
if (!TryBuildDefaultMaterialProperty(*shaderProperty, defaultProperty)) {
return false;
}
RemoveTextureBindingByName(m_textureBindings, name);
m_properties.Insert(name, defaultProperty);
return true;
}
void Material::SyncShaderSchemaProperties(bool removeUnknownProperties) {
if (m_shader.Get() == nullptr) {
return;
}
if (removeUnknownProperties) {
std::vector<Containers::String> unknownProperties;
const auto pairs = m_properties.GetPairs();
unknownProperties.reserve(pairs.Size());
for (const auto& pair : pairs) {
if (FindShaderPropertyDesc(pair.first) == nullptr) {
unknownProperties.push_back(pair.first);
}
}
for (const Containers::String& propertyName : unknownProperties) {
m_properties.Erase(propertyName);
RemoveTextureBindingByName(m_textureBindings, propertyName);
}
}
for (const ShaderPropertyDesc& shaderProperty : m_shader->GetProperties()) {
const MaterialProperty* property = m_properties.Find(shaderProperty.name);
if (property == nullptr ||
!IsMaterialPropertyCompatibleWithShaderProperty(property->type, shaderProperty.type)) {
ResetPropertyToShaderDefault(shaderProperty.name);
continue;
}
if (!IsTextureMaterialPropertyType(property->type)) {
RemoveTextureBindingByName(m_textureBindings, shaderProperty.name);
}
}
}
void Material::MarkChanged(bool updateConstantBuffer) {
@@ -553,6 +979,7 @@ void Material::MarkChanged(bool updateConstantBuffer) {
void Material::UpdateMemorySize() {
m_memorySize = m_constantBufferData.Size() +
m_constantLayout.Size() * sizeof(MaterialConstantFieldDesc) +
sizeof(MaterialRenderState) +
m_shaderPass.Length() +
m_tags.Size() * sizeof(MaterialTagEntry) +
@@ -566,6 +993,10 @@ void Material::UpdateMemorySize() {
m_memorySize += tag.value.Length();
}
for (const MaterialConstantFieldDesc& field : m_constantLayout) {
m_memorySize += field.name.Length();
}
for (const auto& binding : m_textureBindings) {
m_memorySize += binding.name.Length();
m_memorySize += binding.texturePath.Length();