Hydra render engine #104712

Closed
Bogdan Nagirniak wants to merge 142 commits from BogdanNagirniak/blender:hydra-render into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
7 changed files with 135 additions and 2 deletions
Showing only changes of commit 85864b39a4 - Show all commits

View File

@ -794,7 +794,7 @@ set(POSTCONFIGURE_SCRIPT "" CACHE FILEPATH "Run given CMake script as the last s
mark_as_advanced(POSTCONFIGURE_SCRIPT)
# USD Hydra plugin.
if(WIN32 AND WITH_USD)
if(WIN32 AND WITH_USD AND WITH_MATERIALX)
BogdanNagirniak marked this conversation as resolved Outdated

We would not land this only for Windows. So remove any WIN32 checks and if it fails to build on other platforms that should be fixed at some point.

We would not land this only for Windows. So remove any WIN32 checks and if it fails to build on other platforms that should be fixed at some point.
option(WITH_HYDRA "Enable USD Hydra plugin" ON)
endif()

View File

@ -899,6 +899,13 @@ if(WITH_USD)
endif()
endif()
if(WITH_MATERIALX)
windows_find_package(MaterialX)
if(NOT MaterialX_FOUND)
include("${LIBDIR}/MaterialX/lib/cmake/MaterialX/MaterialXTargets.cmake")
BogdanNagirniak marked this conversation as resolved Outdated

This looks like a workaround for something but there is no comment explaining it.

This looks like a workaround for something but there is no comment explaining it.

windows_find_package is a no-op out of the box, we offer an option to make it work, but that's off by default, the construct here is to be expected, don't think it needs a comment?

including MaterialXTargets.cmake rather than hardcoding values our selves, is a bit different from other targets but it's not the worst idea, less for us to maintain, could use a check the file exists perhaps just in case the filename changes in the future.

`windows_find_package` is a no-op out of the box, we offer an option to make it work, but that's off by default, the construct here is to be expected, don't think it needs a comment? including `MaterialXTargets.cmake` rather than hardcoding values our selves, is a bit different from other targets but it's not the worst idea, less for us to maintain, could use a check the file exists perhaps just in case the filename changes in the future.
endif()
endif()
if(WINDOWS_PYTHON_DEBUG)
# Include the system scripts in the blender_python_system_scripts project.
file(GLOB_RECURSE inFiles "${CMAKE_SOURCE_DIR}/release/scripts/*.*" )

View File

@ -1,6 +1,10 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2011-2022 Blender Foundation
if(NOT TARGET MaterialXCore OR NOT TARGET MaterialXFormat)
message(FATAL_ERROR "Hydra Scene Delegate requires MaterialX")
endif()
if(WIN32)
add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN -DBOOST_DEBUG_PYTHON)
endif()
@ -72,6 +76,8 @@ set(SRC
sceneDelegate/material.cc
sceneDelegate/mesh.h
sceneDelegate/mesh.cc
sceneDelegate/mtlxHydraAdapter.h
sceneDelegate/mtlxHydraAdapter.cc
sceneDelegate/light.h
sceneDelegate/light.cc
sceneDelegate/world.h
@ -88,4 +94,8 @@ set(LIB
blender_add_lib(bf_render_hydra "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
target_link_libraries(bf_render_hydra PRIVATE
MaterialXCore
MaterialXFormat)
add_dependencies(bf_render_hydra bf_rna)

View File

@ -5,6 +5,7 @@
#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/material.h>
#include <pxr/imaging/hd/renderDelegate.h>
#include "glog/logging.h"
@ -12,6 +13,7 @@
#include "BKE_lib_id.h"
#include "material.h"
#include "mtlxHydraAdapter.h"
using namespace pxr;
@ -49,7 +51,18 @@ VtValue MaterialData::get_data(TfToken const &key)
pxr::VtValue MaterialData::material_resource()
{
/* TODO: Implement return of HdMaterialNetwork */
std::string const &path = mtlx_path.GetResolvedPath();
if (!path.empty()) {
HdRenderDelegate *render_delegate = scene_delegate->GetRenderIndex().GetRenderDelegate();
TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes();
TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts();
HdMaterialNetworkMap material_network_map;
HdMtlxConvertToMaterialNetworkMap(
path, shader_source_types, render_contexts, &material_network_map);
return VtValue(material_network_map);
}
return pxr::VtValue();
}

View File

@ -0,0 +1,77 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include "mtlxHydraAdapter.h"
#include <pxr/base/arch/fileSystem.h>
#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/ar/resolverContextBinder.h>
#include <pxr/usd/ar/resolverScopedCache.h>
#include <pxr/usd/usdMtlx/reader.h>
#include <pxr/usd/usdMtlx/utils.h>
#include <pxr/usd/usdShade/material.h>
#include <pxr/usd/usdShade/shader.h>
#include <pxr/usdImaging/usdImaging/materialParamUtils.h>
#include <pxr/imaging/hd/material.h>
#include <pxr/imaging/hd/tokens.h>
namespace mx = MaterialX;
PXR_NAMESPACE_OPEN_SCOPE
void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlxPath,
TfTokenVector const &shaderSourceTypes,
TfTokenVector const &renderContexts,
HdMaterialNetworkMap *out)
{
if (mtlxPath.empty()) {
return;
}
std::string basePath = TfGetPathName(mtlxPath);
ArResolver &resolver = ArGetResolver();
const ArResolverContext context = resolver.CreateDefaultContextForAsset(mtlxPath);
ArResolverContextBinder binder(context);
ArResolverScopedCache resolverCache;
std::string mtlxName = TfGetBaseName(mtlxPath);
std::string stageId = TfStringPrintf(
"%s%s%s.usda", basePath.c_str(), ARCH_PATH_SEP, mtlxName.c_str());
UsdStageRefPtr stage = UsdStage::CreateInMemory(stageId, context);
try {
mx::DocumentPtr doc = UsdMtlxReadDocument(mtlxPath);
UsdMtlxRead(doc, stage);
}
catch (mx::ExceptionFoundCycle &x) {
TF_RUNTIME_ERROR("MaterialX cycle found: %s\n", x.what());
return;
}
catch (mx::Exception &x) {
TF_RUNTIME_ERROR("MaterialX error: %s\n", x.what());
return;
}
if (UsdPrim materials = stage->GetPrimAtPath(SdfPath("/MaterialX/Materials"))) {
if (UsdPrimSiblingRange children = materials.GetChildren()) {
if (auto material = UsdShadeMaterial(*children.begin())) {
if (UsdShadeShader mtlxSurface = material.ComputeSurfaceSource(renderContexts)) {
UsdImagingBuildHdMaterialNetworkFromTerminal(mtlxSurface.GetPrim(),
HdMaterialTerminalTokens->surface,
shaderSourceTypes,
renderContexts,
out,
UsdTimeCode::Default());
}
}
}
}
}
PXR_NAMESPACE_CLOSE_SCOPE

View File

@ -0,0 +1,20 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include <pxr/base/tf/token.h>
#include <pxr/pxr.h>
#include <string>
PXR_NAMESPACE_OPEN_SCOPE
struct HdMaterialNetworkMap;
void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlxPath,
TfTokenVector const &shaderSourceTypes,
TfTokenVector const &renderContexts,
HdMaterialNetworkMap *out);
PXR_NAMESPACE_CLOSE_SCOPE

View File

@ -980,6 +980,9 @@ elseif(WIN32)
${LIBDIR}/materialx/bin/MaterialXGenMdl.dll
${LIBDIR}/materialx/bin/MaterialXGenOsl.dll
${LIBDIR}/materialx/bin/MaterialXGenShader.dll
${LIBDIR}/materialx/bin/MaterialXRender.dll
${LIBDIR}/materialx/bin/MaterialXRenderHw.dll
${LIBDIR}/materialx/bin/MaterialXRenderGlsl.dll
BogdanNagirniak marked this conversation as resolved Outdated

We intentionally built MaterialX without these libraries. Are they needed for Hydra? For reference:
https://archive.blender.org/developer/D15989

If we need them then we have to ensure that they work with Vulkan and Metal, and that any Vulkan or OpenGL library is loaded with dlopen so that Blender can run in headless mode without a GPU.

We intentionally built MaterialX without these libraries. Are they needed for Hydra? For reference: https://archive.blender.org/developer/D15989 If we need them then we have to ensure that they work with Vulkan and Metal, and that any Vulkan or OpenGL library is loaded with dlopen so that Blender can run in headless mode without a GPU.
RELEASE
)
windows_install_shared_manifest(
@ -990,6 +993,9 @@ elseif(WIN32)
${LIBDIR}/materialx/bin/MaterialXGenMdl_d.dll
${LIBDIR}/materialx/bin/MaterialXGenOsl_d.dll
${LIBDIR}/materialx/bin/MaterialXGenShader_d.dll
${LIBDIR}/materialx/bin/MaterialXRender_d.dll
${LIBDIR}/materialx/bin/MaterialXRenderHw_d.dll
${LIBDIR}/materialx/bin/MaterialXRenderGlsl_d.dll
DEBUG
)