Skip to content

Commit

Permalink
Fix heap corruption under debug, and update USD to latest version (Re…
Browse files Browse the repository at this point in the history
…quires manual wipe of all _Comp* dirs)
  • Loading branch information
AlexDunn committed Sep 28, 2023
1 parent a60b141 commit d67eefc
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 54 deletions.
11 changes: 5 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ project(
'dxvk', ['c', 'cpp'],
version : 'remix-main',
meson_version : '>= 0.58',
default_options : ['werror=true', 'b_vscrt=static_from_buildtype']
default_options : ['werror=true', 'b_vscrt=from_buildtype']
)

global_src_root_norm = meson.global_source_root().replace('\\', '/')
Expand Down Expand Up @@ -386,9 +386,8 @@ embedding_compiler = generator(xxd_exe,

mdl_include_dir = include_directories(join_paths(meson.global_source_root(), 'external/omni_core_materials'))

# we always use the release headers to avoid having two copies of Boost in the tree
nv_usd_include_path = include_directories('external/nv_usd/release/include/')
boost_include_path = include_directories('external/nv_usd/release/include/boost-1_70/')
nv_usd_include_path = include_directories('external/nv_usd/include/')
boost_include_path = include_directories('external/nv_usd/include/boost-1_78/')

usd_include_paths = [nv_usd_include_path, boost_include_path]

Expand All @@ -397,7 +396,7 @@ nvapi_lib_path = join_paths(meson.global_source_root(), 'external/nvapi/amd64')
nvapi_lib = dxvk_compiler.find_library('nvapi64', dirs : nvapi_lib_path)

# Cache lib locations
usd_lib_path = meson.global_source_root() + '/external/nv_usd/'+ debug_or_release + '/lib/'
usd_lib_path = meson.global_source_root() + '/external/nv_usd/'+ debug_or_release + '/'
usd_lib = dxvk_compiler.find_library('usd_ms', dirs : usd_lib_path)
if get_option('buildtype') == 'debug'
tbb_lib = dxvk_compiler.find_library('tbb_debug', dirs : usd_lib_path)
Expand Down Expand Up @@ -435,7 +434,7 @@ if not dxvk_is_ninja
recursive_copy_usd_target = custom_target('recursive_copy_usd',
output : ['recursive_copy_usd'],
build_by_default : true,
command : [recursive_copy_path, usd_lib_path + 'usd/', output_dir + 'usd/'] )
command : [recursive_copy_path, usd_lib_path + '/usd/', output_dir + 'usd/'] )

copy_usd_dll_target = custom_target('copy_usd_dll',
output : ['copy_usd_dll'],
Expand Down
2 changes: 1 addition & 1 deletion packman-external.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<package name="rtx-remix-nvapi" version="1" />
</dependency>
<dependency name="nv_usd" linkPath="external/nv_usd">
<package name="rtx-remix-nv_usd" version="2" />
<package name="rtx-remix-nv_usd" version="5" />
</dependency>
<dependency name="omni_core_materials" linkPath="external/omni_core_materials">
<package name="rtx-remix-omni_core_materials" version="4" />
Expand Down
4 changes: 2 additions & 2 deletions src/dxvk/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ dlss_include_dir = include_directories(join_paths(meson.global_source_root(), dl
dlss_lib_path = join_paths(meson.global_source_root(), dlss_root_path + '/Lib/Windows_x86_64/x86_64')
dlfg_include_dir = include_directories(join_paths(meson.global_source_root(), dlfg_root_path + '/Include'))
cc = meson.get_compiler('c')
dlss_lib_name = 'nvsdk_ngx_s'
dlss_lib_name = 'nvsdk_ngx_d'
if get_option('buildtype') == 'debug'
dlss_lib_name = 'nvsdk_ngx_s_dbg'
dlss_lib_name = 'nvsdk_ngx_d_dbg'
endif
dlss_lib = cc.find_library(dlss_lib_name, dirs : dlss_lib_path)
dlss_dep = declare_dependency(
Expand Down
5 changes: 4 additions & 1 deletion src/dxvk/rtx_render/rtx_matrix_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

#include "MathLib/MathLib.h"
#include "../util/util_matrix.h"
#include "../../../external/nv_usd/release/include/pxr/base/arch/math.h"

#include "../../lssusd/usd_include_begin.h"
#include <pxr/base/arch/math.h>
#include "../../lssusd/usd_include_end.h"

static inline void copyDxvkMatrix4ToDouble4x4(const dxvk::Matrix4& src, double dest[][4]) {
for (int i = 0; i < 4; i++) {
Expand Down
16 changes: 12 additions & 4 deletions src/dxvk/rtx_render/rtx_mod_usd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include <pxr/usd/usdLux/diskLight.h>
#include <pxr/usd/usdLux/cylinderLight.h>
#include <pxr/usd/usdLux/distantLight.h>
#include "pxr/usd/usdLux/light.h"
#include "pxr/usd/usdLux/lightapi.h"
#include <pxr/usd/usdLux/blackbody.h>
#include <pxr/usd/usdSkel/bindingAPI.h>
#include <pxr/usd/usdShade/materialBindingAPI.h>
Expand Down Expand Up @@ -719,6 +719,14 @@ void UsdMod::Impl::processPrim(Args& args, pxr::UsdPrim& prim) {
}
}

bool IsLight(const pxr::UsdPrim& lightPrim) {
return lightPrim.IsA<pxr::UsdLuxSphereLight>()
|| lightPrim.IsA<pxr::UsdLuxRectLight>()
|| lightPrim.IsA<pxr::UsdLuxDiskLight>()
|| lightPrim.IsA<pxr::UsdLuxCylinderLight>()
|| lightPrim.IsA<pxr::UsdLuxDistantLight>();
}

void UsdMod::Impl::processLight(Args& args, const pxr::UsdPrim& lightPrim) {
static const pxr::TfToken kRadiusToken("radius");
static const pxr::TfToken kWidthToken("width");
Expand All @@ -742,7 +750,7 @@ void UsdMod::Impl::processLight(Args& args, const pxr::UsdPrim& lightPrim) {
// Lights being replaced are instances that need to exist in the same place as the drawcall they're replacing.
// Meshes being replaced are assets that may have multiple instances, so any children need to be offset from the
// asset root, instead of the world root.
if (args.rootPrim.IsA<pxr::UsdLuxLight>()) {
if (IsLight(args.rootPrim)) {
localToRoot = pxr::GfMatrix4f(args.xformCache.GetLocalToWorldTransform(lightPrim));
} else {
bool resetXformStack; // unused
Expand Down Expand Up @@ -838,14 +846,14 @@ void UsdMod::Impl::processReplacement(Args& args) {

if (args.rootPrim.IsA<pxr::UsdGeomMesh>()) {
processPrim(args, args.rootPrim);
} else if (args.rootPrim.IsA<pxr::UsdLuxLight>()) {
} else if (IsLight(args.rootPrim)) {
processLight(args, args.rootPrim);
}
auto descendents = args.rootPrim.GetFilteredDescendants(pxr::UsdPrimIsActive);
for (auto desc : descendents) {
if (desc.IsA<pxr::UsdGeomMesh>()) {
processPrim(args, desc);
} else if (desc.IsA<pxr::UsdLuxLight>()) {
} else if (IsLight(desc)) {
processLight(args, desc);
}
}
Expand Down
82 changes: 45 additions & 37 deletions src/lssusd/game_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "usd_include_begin.h"
#include <pxr/usd/ar/defaultResolver.h>
#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/kind/registry.h>
#include <pxr/usd/sdf/types.h>
#include <pxr/usd/usd/attribute.h>
Expand All @@ -39,6 +40,7 @@
#include <pxr/usd/usdGeom/camera.h>
#include <pxr/usd/usdGeom/xform.h>
#include <pxr/usd/usdGeom/xformCommonAPI.h>
#include <pxr/usd/usdLux/lightapi.h>
#include <pxr/usd/usdLux/sphereLight.h>
#include <pxr/usd/usdLux/distantLight.h>
#include <pxr/usd/usdLux/domeLight.h>
Expand Down Expand Up @@ -165,21 +167,27 @@ namespace lss {
bool GameExporter::s_bMultiThreadSafety = false;
std::mutex GameExporter::s_mutex;

std::string computeLocalPath(const std::string& assetPath) {
static pxr::ArResolver& resolver = pxr::ArGetResolver();
const std::string identifier = resolver.CreateIdentifierForNewAsset(assetPath);
pxr::ArResolvedPath resolvedPath = resolver.ResolveForNewAsset(identifier);
return resolvedPath.GetPathString();
}

bool GameExporter::loadUsdPlugins(const std::string& path) {
static auto& pluginRegistry = pxr::PlugRegistry::GetInstance();
static pxr::ArDefaultResolver arDefResolver;
const auto fullPath = arDefResolver.ComputeLocalPath(path);
std::string fullPath = computeLocalPath(path);
static auto plugins = pluginRegistry.RegisterPlugins(fullPath);
for(auto plugin : plugins) {
for (auto plugin : plugins) {
if (plugin == nullptr)
continue;

if(!plugin->IsLoaded() && !plugin->Load()) {
if (!plugin->IsLoaded() && !plugin->Load()) {
return false;
}
dxvk::Logger::info("[GameExporter] Load plugin: " + plugin->GetName());
}
return plugins.size() > 0;
return plugins.size() > 0;
}

void GameExporter::exportUsd(const Export& exportData) {
Expand Down Expand Up @@ -270,9 +278,9 @@ void GameExporter::createApertureMdls(const std::string& baseExportPath) {

void GameExporter::exportMaterials(const Export& exportData, ExportContext& ctx) {
dxvk::Logger::debug("[GameExporter][" + exportData.debugId + "][exportMaterials] Begin");
static pxr::ArDefaultResolver arDefResolver;
const std::string matDirPath = exportData.baseExportPath + "/" + commonDirName::matDir;
const std::string fullMaterialBasePath = arDefResolver.ComputeLocalPath(matDirPath);
const std::string fullMaterialBasePath = computeLocalPath(matDirPath);

dxvk::env::createDirectory(matDirPath);
for(const auto& [matId, matData] : exportData.materials) {
// Build material stage
Expand Down Expand Up @@ -324,8 +332,7 @@ void GameExporter::exportMaterials(const Export& exportData, ExportContext& ctx)
{
const auto attr = shaderPrim.CreateAttribute(pxr::TfToken(attrName), pxr::SdfValueTypeNames->Asset, false, pxr::SdfVariabilityVarying);
assert(attr);
static pxr::ArDefaultResolver arDefResolver;
const auto fullTexturePath = arDefResolver.ComputeLocalPath(relTexPath);
const auto fullTexturePath = computeLocalPath(relTexPath);
const auto relToMaterialsTexPath = std::filesystem::relative(fullTexturePath,fullMaterialBasePath).string();
const bool bSetSuccessful = attr.Set(pxr::SdfAssetPath(relToMaterialsTexPath));
assert(bSetSuccessful);
Expand Down Expand Up @@ -399,11 +406,10 @@ void GameExporter::exportMaterials(const Export& exportData, ExportContext& ctx)

void GameExporter::exportSkeletons(const Export& exportData, ExportContext& ctx) {
dxvk::Logger::debug("[GameExporter][" + exportData.debugId + "][exportSkeletons] Begin");
static pxr::ArDefaultResolver arDefResolver;
static const pxr::GfMatrix4d identity(1);
const std::string relDirPath = commonDirName::skeletonDir + "/";
const std::string dirPath = exportData.baseExportPath + "/" + relDirPath;
const std::string fullStagePath = arDefResolver.ComputeLocalPath(dirPath);
const std::string fullStagePath = computeLocalPath(dirPath);
dxvk::env::createDirectory(dirPath);
for (const auto& [meshId, mesh] : exportData.meshes) {
if (mesh.numBones == 0) {
Expand Down Expand Up @@ -476,11 +482,10 @@ void GameExporter::exportSkeletons(const Export& exportData, ExportContext& ctx)

void GameExporter::exportMeshes(const Export& exportData, ExportContext& ctx) {
dxvk::Logger::debug("[GameExporter][" + exportData.debugId + "][exportMeshes] Begin");
static pxr::ArDefaultResolver arDefResolver;
static const pxr::GfMatrix4d identity(1);
const std::string relMeshDirPath = commonDirName::meshDir + "/";
const std::string meshDirPath = exportData.baseExportPath + "/" + relMeshDirPath;
const std::string fullMeshStagePath = arDefResolver.ComputeLocalPath(meshDirPath);
const std::string fullMeshStagePath = computeLocalPath(meshDirPath);
dxvk::env::createDirectory(meshDirPath);
for(const auto& [meshId,mesh] : exportData.meshes) {
assert(mesh.numVertices > 0);
Expand Down Expand Up @@ -517,7 +522,9 @@ void GameExporter::exportMeshes(const Export& exportData, ExportContext& ctx) {

// Build mesh geometry prim under above xform
const auto meshSchemaSdfPath = meshXformSdfPath.AppendChild(gTokMesh);
auto meshSchema = pxr::UsdGeomMesh::Define(meshStage, meshSchemaSdfPath);
pxr::UsdGeomMesh meshSchema = pxr::UsdGeomMesh::Define(meshStage, meshSchemaSdfPath);
pxr::UsdGeomPrimvarsAPI primvarsAPI(meshSchema.GetPrim());

assert(meshSchema);
auto meshVisibilityAttr = meshSchema.CreateVisibilityAttr();
assert(meshVisibilityAttr);
Expand Down Expand Up @@ -562,7 +569,7 @@ void GameExporter::exportMeshes(const Export& exportData, ExportContext& ctx) {
subdivAttr.Set(pxr::UsdGeomTokens->none);
// Texture Coordinates
static const pxr::TfToken kTokSt("st");
auto stAttr = meshSchema.CreatePrimvar(kTokSt, pxr::SdfValueTypeNames->TexCoord2fArray, pxr::UsdGeomTokens->vertex);
auto stAttr = primvarsAPI.CreatePrimvar(kTokSt, pxr::SdfValueTypeNames->TexCoord2fArray, pxr::UsdGeomTokens->vertex);
assert(stAttr);
exportBufferSet(reduce ? reduceBufferSet(mesh.buffers.texcoordBufs, reducedIdxBufSet) : mesh.buffers.texcoordBufs, stAttr);

Expand Down Expand Up @@ -610,7 +617,7 @@ void GameExporter::exportMeshes(const Export& exportData, ExportContext& ctx) {
const auto shaderMatSchema = pxr::UsdShadeMaterial::Define(meshStage, matLssReference.ogSdfPath);
assert(shaderMatSchema);
auto shaderMatUsdReferences = shaderMatSchema.GetPrim().GetReferences();
const std::string fullMatStagePath = arDefResolver.ComputeLocalPath(matLssReference.stagePath);
const std::string fullMatStagePath = computeLocalPath(matLssReference.stagePath);
const std::string relMatRefStagePath = std::filesystem::relative(fullMatStagePath,fullMeshStagePath).string();
shaderMatUsdReferences.AddReference(relMatRefStagePath, matLssReference.ogSdfPath);
pxr::UsdShadeMaterialBindingAPI(meshXformSchema.GetPrim()).Bind(shaderMatSchema);
Expand Down Expand Up @@ -854,15 +861,14 @@ void GameExporter::exportCamera(const Export& exportData, ExportContext& ctx) {
}

void GameExporter::exportSphereLights(const Export& exportData, ExportContext& ctx) {
static pxr::ArDefaultResolver arDefResolver;
const std::string relLightDirPath = commonDirName::lightDir + "/";
const std::string lightDirPath = exportData.baseExportPath + "/" + relLightDirPath;
const std::string fullLightStagePath = arDefResolver.ComputeLocalPath(lightDirPath);
const std::string fullLightStagePath = computeLocalPath(lightDirPath);
dxvk::Logger::debug("[GameExporter][" + exportData.debugId + "][exportSphereLights] Begin");
for(const auto& [id,sphereLightData] : exportData.sphereLights) {
// Build light stage
const std::string lightName = prefix::light + sphereLightData.lightName;
const std::string lightStagePath = lightDirPath + lightName + lss::ext::usd;
const std::string lightStagePath = lightDirPath + lightName + lss::ext::usda;
pxr::UsdStageRefPtr lightStage = findOpenOrCreateStage(lightStagePath, true);
assert(lightStage);
setCommonStageMetaData(lightStage, exportData);
Expand Down Expand Up @@ -898,13 +904,17 @@ void GameExporter::exportSphereLights(const Export& exportData, ExportContext& c
auto FocusExponentAttr = shaping.CreateShapingFocusAttr();
assert(FocusExponentAttr);
FocusExponentAttr.Set(sphereLightData.focusExponent);

shaping.Apply(sphereLight.GetPrim());
}

setTimeSampledXforms<false>(lightStage, lightAssetSdfPath, sphereLightData.firstTime, sphereLightData.finalTime, sphereLightData.xforms, exportData.meta);

setLightIntensityOnTimeSpan(lightStage, lightAssetSdfPath, sphereLightData.intensity, sphereLightData.firstTime, sphereLightData.finalTime, exportData.meta.numFramesCaptured);

lightStage->Save();
pxr::UsdLuxLightAPI lightAPI(sphereLight.GetPrim());
setLightIntensityOnTimeSpan(lightAPI, sphereLightData.intensity, sphereLightData.firstTime, sphereLightData.finalTime, exportData.meta.numFramesCaptured);
lightAPI.Apply(sphereLight.GetPrim());

lightStage->Save();

// Cache light reference
Reference lightLssReference;
Expand All @@ -916,7 +926,7 @@ void GameExporter::exportSphereLights(const Export& exportData, ExportContext& c
const pxr::SdfPath fullSphereLightPath = gRootLightsPath.AppendElementString(lightName);
auto sphereLightInstance = pxr::UsdLuxSphereLight::Define(ctx.instanceStage, fullSphereLightPath);

const std::string relLightStagePath = relLightDirPath + lightName + lss::ext::usd;
const std::string relLightStagePath = relLightDirPath + lightName + lss::ext::usda;
auto lightInstanceUsdReferences = sphereLightInstance.GetPrim().GetReferences();
lightInstanceUsdReferences.AddReference(relLightStagePath);
}
Expand Down Expand Up @@ -950,7 +960,9 @@ void GameExporter::exportDistantLights(const Export& exportData, ExportContext&
assert(orientAttr);
orientAttr.Set(directionQuatF);

setLightIntensityOnTimeSpan(ctx.instanceStage, distantLightPath, distantLightData.intensity, distantLightData.firstTime, distantLightData.finalTime, exportData.meta.numFramesCaptured);
pxr::UsdLuxLightAPI lightAPI(distantLightSchema.GetPrim());
setLightIntensityOnTimeSpan(lightAPI, distantLightData.intensity, distantLightData.firstTime, distantLightData.finalTime, exportData.meta.numFramesCaptured);
lightAPI.Apply(distantLightSchema.GetPrim());
}
dxvk::Logger::debug("[GameExporter][" + exportData.debugId + "][exportDistantLights] End");
}
Expand All @@ -969,9 +981,8 @@ void GameExporter::exportSky(const Export& exportData, ExportContext& ctx) {
auto textureAttr = domeLightSchema.CreateTextureFileAttr();
assert(textureAttr);

static pxr::ArDefaultResolver arDefResolver;
const auto fullBasePath = arDefResolver.ComputeLocalPath(exportData.baseExportPath);
const auto fullTexturePath = arDefResolver.ComputeLocalPath(exportData.bakedSkyProbePath);
const auto fullBasePath = computeLocalPath(exportData.baseExportPath);
const auto fullTexturePath = computeLocalPath(exportData.bakedSkyProbePath);
const auto relToMaterialsTexPath = std::filesystem::relative(fullTexturePath, fullBasePath).string();
const bool bSetSuccessful = textureAttr.Set(pxr::SdfAssetPath(relToMaterialsTexPath));
assert(bSetSuccessful);
Expand Down Expand Up @@ -1179,18 +1190,15 @@ void GameExporter::setVisibilityTimeSpan(const pxr::UsdStageRefPtr stage,
}
}

void GameExporter::setLightIntensityOnTimeSpan(const pxr::UsdStageRefPtr stage,
const pxr::SdfPath sdfPath,
const float defaultLightIntensity,
const double firstTime,
const double finalTime,
const size_t numFramesCaptured) {
void GameExporter::setLightIntensityOnTimeSpan(const pxr::UsdLuxLightAPI& luxLight,
const float defaultLightIntensity,
const double firstTime,
const double finalTime,
const size_t numFramesCaptured) {
const bool isSingleFrame = numFramesCaptured == 1;
auto luxLightSchema = pxr::UsdLuxLight::Get(stage, sdfPath);
assert(luxLightSchema);
auto intensityAttr = luxLightSchema.GetIntensityAttr();
auto intensityAttr = luxLight.GetIntensityAttr();
if(!intensityAttr) {
intensityAttr = luxLightSchema.CreateIntensityAttr();
intensityAttr = luxLight.CreateIntensityAttr();
}
assert(intensityAttr);
if (isSingleFrame) {
Expand Down
4 changes: 2 additions & 2 deletions src/lssusd/game_exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "usd_include_begin.h"
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usd/attribute.h>
#include <pxr/usd/usdLux/lightapi.h>
#include "usd_include_end.h"

#include "game_exporter_common.h"
Expand Down Expand Up @@ -94,8 +95,7 @@ class GameExporter
const double firstTime,
const double finalTime,
const size_t numFramesCaptured);
static void setLightIntensityOnTimeSpan(const pxr::UsdStageRefPtr stage,
const pxr::SdfPath sdfPath,
static void setLightIntensityOnTimeSpan(const pxr::UsdLuxLightAPI& luxLight,
const float defaultLightIntensity,
const double firstTime,
const double finalTime,
Expand Down
2 changes: 1 addition & 1 deletion src/lssusd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lssUsd_lib = static_library('lssUsd', lssUsd_src,[libusd_generated_embedded_file
dependencies : [ usd_dep, embedded_dep ],
include_directories : [ usd_include_paths ],
override_options : ['cpp_std='+dxvk_cpp_std],
cpp_args : lss_Usd_args,)
cpp_args : lss_Usd_args)

lssUsd_dep = declare_dependency(
link_with : [ lssUsd_lib ])
1 change: 1 addition & 0 deletions src/lssusd/usd_include_begin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
#pragma warning(disable:4305) // argument truncation from double to float
#pragma warning(disable:4800) // int to bool
#pragma warning(disable:4996) // call to std::copy with parameters that may be unsafe
#pragma warning(disable:4251) // struct 'std::atomic<T *>' needs to have dll-interface to be used by clients of class
#endif

0 comments on commit d67eefc

Please sign in to comment.