Skip to content

Commit

Permalink
Merge branch 'sultimt/rr-leak-fix' into 'main'
Browse files Browse the repository at this point in the history
[REMIX-3446] Fix VRAM leak: on disable, free Ray Reconstruction NGX context, so NGX would free its internal resources

See merge request lightspeedrtx/dxvk-remix-nv!955
  • Loading branch information
sultim-t-nv committed Aug 19, 2024
2 parents e73a003 + 8168f99 commit 7a7db19
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
75 changes: 53 additions & 22 deletions src/dxvk/rtx_render/rtx_ngx_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ namespace dxvk {
class NGXDLSSContext;
class NGXRayReconstructionContext;
class NGXDLFGContext;
class NGXContext {
class NGXContext final {
public:
NGXContext(DxvkDevice* device);
explicit NGXContext(DxvkDevice* device);

~NGXContext() {
shutdown();
}

NGXContext(const NGXContext&) = delete;
NGXContext(NGXContext&&) noexcept = delete;
NGXContext& operator=(const NGXContext&) = delete;
NGXContext& operator=(NGXContext&&) noexcept = delete;

void shutdown();

bool supportsDLSS() {
Expand Down Expand Up @@ -113,17 +118,22 @@ namespace dxvk {

class NGXFeatureContext {
public:
~NGXFeatureContext();
NGXFeatureContext(const NGXFeatureContext&) = delete;
NGXFeatureContext(NGXFeatureContext&&) noexcept = delete;
NGXFeatureContext& operator=(const NGXFeatureContext&) = delete;
NGXFeatureContext& operator=(NGXFeatureContext&&) noexcept = delete;

virtual ~NGXFeatureContext();
virtual void releaseNGXFeature() = 0;

protected:
NGXFeatureContext(DxvkDevice* device);
explicit NGXFeatureContext(DxvkDevice* device);

DxvkDevice* m_device = nullptr;
NVSDK_NGX_Parameter* m_parameters = nullptr;
};

class NGXDLSSContext : public NGXFeatureContext {
class NGXDLSSContext final : public NGXFeatureContext {
public:
struct OptimalSettings {
float sharpness;
Expand Down Expand Up @@ -167,7 +177,7 @@ namespace dxvk {

/** Release DLSS.
*/
virtual void releaseNGXFeature();
void releaseNGXFeature() override;

/** Checks if DLSS is initialized.
*/
Expand All @@ -177,10 +187,6 @@ namespace dxvk {
*/
bool evaluateDLSS(Rc<DxvkContext> renderContext, const NGXBuffers& buffers, const NGXSettings& settings) const;

// note: ctor is public due to make_unique/unique_ptr, but not intended as public --- use NGXWrapper::createDLSSContext instead
NGXDLSSContext(DxvkDevice* device);
~NGXDLSSContext();

void setWorldToViewMatrix(const Matrix4& worldToView) {
m_worldToViewMatrix = worldToView;
}
Expand All @@ -189,13 +195,24 @@ namespace dxvk {
m_viewToProjectionMatrix = viewToProjection;
}

public:
// note: ctor is public due to make_unique/unique_ptr, but not intended as public --- use NGXWrapper::createDLSSContext instead
explicit NGXDLSSContext(DxvkDevice* device);
~NGXDLSSContext() override;

NGXDLSSContext(const NGXDLSSContext&) = delete;
NGXDLSSContext(NGXDLSSContext&&) noexcept = delete;
NGXDLSSContext& operator=(const NGXDLSSContext&) = delete;
NGXDLSSContext& operator=(NGXDLSSContext&&) noexcept = delete;

private:
bool m_initialized = false;
NVSDK_NGX_Handle* m_featureDLSS = nullptr;
Matrix4 m_worldToViewMatrix;
Matrix4 m_viewToProjectionMatrix;
};

class NGXRayReconstructionContext : public NGXFeatureContext {
class NGXRayReconstructionContext final : public NGXFeatureContext {
public:
struct QuerySettings {
float sharpness;
Expand Down Expand Up @@ -247,7 +264,7 @@ namespace dxvk {

/** Release DLSS-RR
*/
virtual void releaseNGXFeature();
void releaseNGXFeature() override;

/** Checks if DLSS is initialized.
*/
Expand All @@ -259,10 +276,6 @@ namespace dxvk {
*/
bool evaluateRayReconstruction(Rc<DxvkContext> renderContext, const NGXBuffers& buffers, const NGXSettings& settings) const;

// note: ctor is public due to make_unique/unique_ptr, but not intended as public --- use NGXWrapper::createRayReconstructionContext instead
NGXRayReconstructionContext(DxvkDevice* device);
~NGXRayReconstructionContext();;

void setWorldToViewMatrix(const Matrix4& worldToView) {
m_worldToViewMatrix = worldToView;
}
Expand All @@ -271,13 +284,24 @@ namespace dxvk {
m_viewToProjectionMatrix = viewToProjection;
}

public:
// note: ctor is public due to make_unique/unique_ptr, but not intended as public --- use NGXWrapper::createRayReconstructionContext instead
explicit NGXRayReconstructionContext(DxvkDevice* device);
~NGXRayReconstructionContext() override;

NGXRayReconstructionContext(const NGXRayReconstructionContext&) = delete;
NGXRayReconstructionContext(NGXRayReconstructionContext&&) noexcept = delete;
NGXRayReconstructionContext& operator=(const NGXRayReconstructionContext&) = delete;
NGXRayReconstructionContext& operator=(NGXRayReconstructionContext&&) noexcept = delete;

private:
bool m_initialized = false;
NVSDK_NGX_Handle* m_featureRayReconstruction = nullptr;
Matrix4 m_worldToViewMatrix;
Matrix4 m_viewToProjectionMatrix;
};

class NGXDLFGContext : public NGXFeatureContext {
class NGXDLFGContext final : public NGXFeatureContext {
public:
typedef void (__cdecl* AppCreateTimelineSyncObjectsCallback_t)(void* app_context,
void** pp_sync_obj_signal,
Expand Down Expand Up @@ -309,7 +333,7 @@ namespace dxvk {

void initialize(
Rc<DxvkContext> renderContext,
VkCommandBuffer commandList,
VkCommandBuffer commandList,
uint32_t displayOutSize[2],
VkFormat outputFormat,
AppCreateTimelineSyncObjectsCallback_t createTimelineSyncObjectsCallback,
Expand All @@ -324,18 +348,25 @@ VkCommandBuffer commandList,
// the first kNumWarmUpFrames won't be interpolated so interpolatedOutput may not be valid, this function returns true if interpolation happened
EvaluateResult evaluate(
Rc<DxvkContext> renderContext,
VkCommandBuffer clientCommandList,
VkCommandBuffer clientCommandList,
Rc<DxvkImageView> interpolatedOutput,
Rc<DxvkImageView> compositedColorBuffer,
Rc<DxvkImageView> motionVectors,
Rc<DxvkImageView> depth,
const RtCamera& camera, Vector2 motionVectorScale, bool resetHistory);

void releaseNGXFeature() override;

public:
// note: ctor is public due to make_unique/unique_ptr, but not intended as public --- use NGXWrapper::createDLFGContext instead
NGXDLFGContext(DxvkDevice* device);
~NGXDLFGContext();
explicit NGXDLFGContext(DxvkDevice* device);
~NGXDLFGContext() override;

NGXDLFGContext(const NGXDLFGContext&) = delete;
NGXDLFGContext(NGXDLFGContext&&) noexcept = delete;
NGXDLFGContext& operator=(const NGXDLFGContext&) = delete;
NGXDLFGContext& operator=(NGXDLFGContext&&) noexcept = delete;

virtual void releaseNGXFeature();
private:
VkCommandPool m_ngxInternalCommandPool = nullptr;
NVSDK_NGX_Handle* m_feature = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/dxvk/rtx_render/rtx_ray_reconstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ namespace dxvk {
}

void DxvkRayReconstruction::release() {
m_rayReconstructionContext = {};
mRecreate = true;
m_normals.image = nullptr;
m_normals.view = nullptr;

}

void DxvkRayReconstruction::onDestroy() {
Expand Down

0 comments on commit 7a7db19

Please sign in to comment.