feat(materialx): Update MaterialX library handling and improve shader generation logging

This commit is contained in:
2026-01-06 16:12:53 +00:00
parent 06e31cf536
commit da126a535f
4 changed files with 64 additions and 12 deletions
+12 -9
View File
@@ -154,20 +154,23 @@ if(TARGET bimg::bimg)
endif()
set(SDL3CPP_MATERIALX_LIBS)
if(TARGET MaterialX::MaterialX)
list(APPEND SDL3CPP_MATERIALX_LIBS MaterialX::MaterialX)
else()
foreach(candidate IN ITEMS
MaterialX::MaterialX
materialx::MaterialX
MaterialX::MaterialXCore
MaterialX::MaterialXFormat
MaterialX::MaterialXGenShader
MaterialX::MaterialXGenGlsl
MaterialX::MaterialXRender)
if(TARGET ${candidate})
list(APPEND SDL3CPP_MATERIALX_LIBS ${candidate})
endif()
endforeach()
endif()
MaterialX::MaterialXRender
materialx::MaterialXCore
materialx::MaterialXFormat
materialx::MaterialXGenShader
materialx::MaterialXGenGlsl
materialx::MaterialXRender)
if(TARGET ${candidate})
list(APPEND SDL3CPP_MATERIALX_LIBS ${candidate})
endif()
endforeach()
if(SDL3CPP_MATERIALX_LIBS)
list(APPEND SDL3CPP_RENDER_STACK_LIBS ${SDL3CPP_MATERIALX_LIBS})
endif()
@@ -2,8 +2,11 @@
#include <MaterialXCore/Document.h>
#include <MaterialXFormat/File.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>
#include <MaterialXGenGlsl/VkShaderGenerator.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/Shader.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXRender/Util.h>
@@ -39,6 +42,9 @@ ShaderPaths MaterialXShaderGenerator::Generate(const MaterialXConfig& config,
if (!config.enabled) {
return {};
}
if (logger_) {
logger_->Trace("MaterialXShaderGenerator", "Generate", "enabled=true");
}
mx::FileSearchPath searchPath;
std::filesystem::path libraryPath = ResolvePath(config.libraryPath, scriptDirectory);
@@ -51,6 +57,11 @@ ShaderPaths MaterialXShaderGenerator::Generate(const MaterialXConfig& config,
if (!libraryPath.empty()) {
searchPath.append(mx::FilePath(libraryPath.string()));
}
if (logger_) {
logger_->Trace("MaterialXShaderGenerator", "Generate",
"libraryPath=" + libraryPath.string() +
", libraryFolders=" + std::to_string(config.libraryFolders.size()));
}
mx::DocumentPtr stdLib = mx::createDocument();
if (!config.libraryFolders.empty()) {
@@ -69,6 +80,10 @@ ShaderPaths MaterialXShaderGenerator::Generate(const MaterialXConfig& config,
if (config.useConstantColor) {
mx::Color3 color(config.constantColor[0], config.constantColor[1], config.constantColor[2]);
shader = mx::createConstantShader(context, stdLib, config.shaderKey, color);
if (logger_) {
logger_->Trace("MaterialXShaderGenerator", "Generate",
"usingConstantColor=true, shaderKey=" + config.shaderKey);
}
} else {
if (config.documentPath.empty()) {
throw std::runtime_error("MaterialX document path is required when use_constant_color is false");
@@ -78,14 +93,38 @@ ShaderPaths MaterialXShaderGenerator::Generate(const MaterialXConfig& config,
if (documentPath.empty()) {
throw std::runtime_error("MaterialX document path could not be resolved");
}
if (logger_) {
logger_->Trace("MaterialXShaderGenerator", "Generate",
"documentPath=" + documentPath.string() +
", materialName=" + config.materialName);
}
mx::DocumentPtr document = mx::createDocument();
mx::readFromXmlFile(document, mx::FilePath(documentPath.string()), &searchPath);
mx::readFromXmlFile(document, mx::FilePath(documentPath.string()), searchPath);
document->importLibrary(stdLib);
mx::TypedElementPtr element;
if (!config.materialName.empty()) {
element = document->getMaterial(config.materialName);
auto renderables = mx::findRenderableElements(document);
for (const auto& candidate : renderables) {
if (candidate && candidate->getName() == config.materialName) {
element = candidate;
break;
}
}
if (!element) {
mx::NodePtr node = document->getNode(config.materialName);
if (node && (node->getCategory() == "surfacematerial"
|| node->getType() == "surfaceshader")) {
element = node;
}
}
if (!element) {
mx::OutputPtr output = document->getOutput(config.materialName);
if (output) {
element = output;
}
}
}
if (!element) {
auto renderables = mx::findRenderableElements(document);
@@ -96,6 +135,12 @@ ShaderPaths MaterialXShaderGenerator::Generate(const MaterialXConfig& config,
if (!element) {
throw std::runtime_error("MaterialX document has no renderable elements");
}
if (logger_) {
logger_->Trace("MaterialXShaderGenerator", "Generate",
"selectedElement=" + element->getName() +
", category=" + element->getCategory() +
", type=" + element->getType());
}
shader = mx::createShader(config.shaderKey, context, element);
}
+2
View File
@@ -1,5 +1,7 @@
#include "null_gui_service.hpp"
#include "../interfaces/gui_types.hpp"
#include <string>
namespace sdl3cpp::services::impl {
+3 -1
View File
@@ -8,6 +8,8 @@
#include <MaterialXCore/Document.h>
#include <MaterialXCore/Types.h>
#include <MaterialXFormat/File.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>
#include <SDL3/SDL.h>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
@@ -79,7 +81,7 @@ mx::DocumentPtr LoadMaterialXDocument(const std::filesystem::path& documentPath,
const std::filesystem::path& scriptDirectory) {
mx::DocumentPtr document = mx::createDocument();
mx::FileSearchPath searchPath = BuildMaterialXSearchPath(config, scriptDirectory);
mx::readFromXmlFile(document, mx::FilePath(documentPath.string()), &searchPath);
mx::readFromXmlFile(document, mx::FilePath(documentPath.string()), searchPath);
if (!config.libraryFolders.empty()) {
mx::DocumentPtr stdLib = mx::createDocument();