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

@@ -65,6 +65,17 @@ private:
Math::Vector4 mainLightColorAndFlags = Math::Vector4::Zero();
};
struct PerMaterialConstants {
Math::Vector4 baseColorFactor = Math::Vector4::One();
};
struct CachedMaterialBindings {
OwnedDescriptorSet constantSet = {};
OwnedDescriptorSet textureSet = {};
Core::uint64 materialVersion = 0;
RHI::RHIResourceView* textureView = nullptr;
};
bool EnsureInitialized(const RenderContext& context);
bool CreatePipelineResources(const RenderContext& context);
void DestroyPipelineResources();
@@ -72,7 +83,9 @@ private:
const RenderContext& context,
const Resources::Material* material);
RHI::RHIDescriptorSet* GetOrCreatePerObjectSet(Core::uint64 objectId);
RHI::RHIDescriptorSet* GetOrCreateTextureSet(RHI::RHIResourceView* textureView);
CachedMaterialBindings* GetOrCreateMaterialBindings(
const Resources::Material* material,
RHI::RHIResourceView* textureView);
void DestroyOwnedDescriptorSet(OwnedDescriptorSet& descriptorSet);
const Resources::Texture* ResolveTexture(const Resources::Material* material) const;
@@ -94,7 +107,7 @@ private:
RHI::RHIPipelineLayout* m_pipelineLayout = nullptr;
std::unordered_map<Resources::MaterialRenderState, RHI::RHIPipelineState*, MaterialRenderStateHash> m_pipelineStates;
std::unordered_map<Core::uint64, OwnedDescriptorSet> m_perObjectSets;
std::unordered_map<RHI::RHIResourceView*, OwnedDescriptorSet> m_textureSets;
std::unordered_map<const Resources::Material*, CachedMaterialBindings> m_materialBindings;
RHI::RHISampler* m_sampler = nullptr;
RHI::RHITexture* m_fallbackTexture = nullptr;
RHI::RHIResourceView* m_fallbackTextureView = nullptr;

View File

@@ -118,6 +118,76 @@ inline bool ShaderPassMatchesBuiltinPass(
return hasMetadata;
}
struct BuiltinForwardMaterialData {
Math::Vector4 baseColorFactor = Math::Vector4::One();
};
inline Math::Vector4 ResolveBuiltinBaseColorFactor(const Resources::Material* material) {
if (material == nullptr) {
return Math::Vector4::One();
}
static const char* kBaseColorPropertyNames[] = {
"baseColor",
"_BaseColor",
"color",
"_Color"
};
for (const char* propertyName : kBaseColorPropertyNames) {
if (material->HasProperty(Containers::String(propertyName))) {
return material->GetFloat4(Containers::String(propertyName));
}
}
Math::Vector4 baseColor = Math::Vector4::One();
static const char* kOpacityPropertyNames[] = {
"opacity",
"_Opacity",
"alpha",
"_Alpha"
};
for (const char* propertyName : kOpacityPropertyNames) {
if (material->HasProperty(Containers::String(propertyName))) {
baseColor.w = material->GetFloat(Containers::String(propertyName));
break;
}
}
return baseColor;
}
inline const Resources::Texture* ResolveBuiltinBaseColorTexture(const Resources::Material* material) {
if (material == nullptr) {
return nullptr;
}
static const char* kTextureNames[] = {
"baseColorTexture",
"_BaseColorTexture",
"_MainTex",
"albedoTexture",
"mainTexture",
"texture"
};
for (const char* textureName : kTextureNames) {
const Resources::ResourceHandle<Resources::Texture> textureHandle =
material->GetTexture(Containers::String(textureName));
if (textureHandle.Get() != nullptr && textureHandle->IsValid()) {
return textureHandle.Get();
}
}
return nullptr;
}
inline BuiltinForwardMaterialData BuildBuiltinForwardMaterialData(const Resources::Material* material) {
BuiltinForwardMaterialData data = {};
data.baseColorFactor = ResolveBuiltinBaseColorFactor(material);
return data;
}
inline const Resources::Material* ResolveMaterial(
const Components::MeshRendererComponent* meshRenderer,
const Resources::Mesh* mesh,

View File

@@ -197,6 +197,7 @@ public:
const Containers::Array<Core::uint8>& GetConstantBufferData() const { return m_constantBufferData; }
void UpdateConstantBuffer();
Core::uint64 GetChangeVersion() const { return m_changeVersion; }
void RecalculateMemorySize();
bool HasProperty(const Containers::String& name) const;
@@ -204,6 +205,7 @@ public:
void ClearAllProperties();
private:
void MarkChanged(bool updateConstantBuffer);
void UpdateMemorySize();
ResourceHandle<class Shader> m_shader;
@@ -214,6 +216,7 @@ private:
Containers::HashMap<Containers::String, MaterialProperty> m_properties;
Containers::Array<Core::uint8> m_constantBufferData;
Containers::Array<MaterialTextureBinding> m_textureBindings;
Core::uint64 m_changeVersion = 1;
};
} // namespace Resources