From 1d394818f453bdb9226f686fa11dc5990fe8a669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Sun, 2 Jul 2023 20:05:17 +0200 Subject: [PATCH] added support for OpenImageDenoise 2.0 GPU API --- WickedEngine/wiRenderPath3D_PathTracing.cpp | 40 ++++++++++++++------- WickedEngine/wiScene_Components.cpp | 27 +++++++++----- WickedEngine/wiVersion.cpp | 2 +- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/WickedEngine/wiRenderPath3D_PathTracing.cpp b/WickedEngine/wiRenderPath3D_PathTracing.cpp index c14432b24..097efb3db 100644 --- a/WickedEngine/wiRenderPath3D_PathTracing.cpp +++ b/WickedEngine/wiRenderPath3D_PathTracing.cpp @@ -9,19 +9,20 @@ #include "wiBacklog.h" #include "wiGraphicsDevice_Vulkan.h" +#if __has_include("OpenImageDenoise/oidn.hpp") +#define OPEN_IMAGE_DENOISE +#include "OpenImageDenoise/oidn.hpp" +#pragma comment(lib,"OpenImageDenoise.lib") +// Also provide the required DLL files from OpenImageDenoise release near the exe! +#endif // __has_include("OpenImageDenoise/oidn.hpp") + using namespace wi::graphics; using namespace wi::scene; namespace wi { - -#if __has_include("OpenImageDenoise/oidn.hpp") -#define OPEN_IMAGE_DENOISE -#include "OpenImageDenoise/oidn.hpp" -#pragma comment(lib,"OpenImageDenoise.lib") -#pragma comment(lib,"tbb.lib") - // Also provide OpenImageDenoise.dll and tbb.dll near the exe! +#ifdef OPEN_IMAGE_DENOISE bool DenoiserCallback(void* userPtr, double n) { auto renderpath = (RenderPath3D_PathTracing*)userPtr; @@ -36,7 +37,7 @@ namespace wi bool RenderPath3D_PathTracing::isDenoiserAvailable() const { return true; } #else bool RenderPath3D_PathTracing::isDenoiserAvailable() const { return false; } -#endif +#endif // OPEN_IMAGE_DENOISE void RenderPath3D_PathTracing::ResizeBuffers() { @@ -204,18 +205,29 @@ namespace wi init = true; } + oidn::BufferRef texturedata_src_buffer = device.newBuffer(texturedata_src.size()); + oidn::BufferRef texturedata_dst_buffer = device.newBuffer(texturedata_dst.size()); + oidn::BufferRef texturedata_albedo_buffer; + oidn::BufferRef texturedata_normal_buffer; + + texturedata_src_buffer.write(0, texturedata_src.size(), texturedata_src.data()); + // Create a denoising filter oidn::FilterRef filter = device.newFilter("RT"); // generic ray tracing filter - filter.setImage("color", texturedata_src.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); + filter.setImage("color", texturedata_src_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); if (!texturedata_albedo.empty()) { - filter.setImage("albedo", texturedata_albedo.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); // optional + texturedata_albedo_buffer = device.newBuffer(texturedata_albedo.size()); + texturedata_albedo_buffer.write(0, texturedata_albedo.size(), texturedata_albedo.data()); + filter.setImage("albedo", texturedata_albedo_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); // optional } if (!texturedata_normal.empty()) { - filter.setImage("normal", texturedata_normal.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); // optional + texturedata_normal_buffer = device.newBuffer(texturedata_normal.size()); + texturedata_normal_buffer.write(0, texturedata_normal.size(), texturedata_normal.data()); + filter.setImage("normal", texturedata_normal_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); // optional } - filter.setImage("output", texturedata_dst.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); + filter.setImage("output", texturedata_dst_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); filter.set("hdr", true); // image is HDR //filter.set("cleanAux", true); filter.commit(); @@ -233,6 +245,10 @@ namespace wi { wi::backlog::post(std::string("[OpenImageDenoise error] ") + errorMessage); } + else + { + texturedata_dst_buffer.read(0, texturedata_dst.size(), texturedata_dst.data()); + } } GraphicsDevice* device = wi::graphics::GetDevice(); diff --git a/WickedEngine/wiScene_Components.cpp b/WickedEngine/wiScene_Components.cpp index e881ef97e..bcb4ba41f 100644 --- a/WickedEngine/wiScene_Components.cpp +++ b/WickedEngine/wiScene_Components.cpp @@ -12,6 +12,13 @@ #include "wiUnorderedMap.h" #include "wiLua.h" +#if __has_include("OpenImageDenoise/oidn.hpp") +#define OPEN_IMAGE_DENOISE +#include "OpenImageDenoise/oidn.hpp" +#pragma comment(lib,"OpenImageDenoise.lib") +// Also provide the required DLL files from OpenImageDenoise release near the exe! +#endif // __has_include("OpenImageDenoise/oidn.hpp") + using namespace wi::ecs; using namespace wi::enums; using namespace wi::graphics; @@ -1379,13 +1386,6 @@ namespace wi::scene SetLightmapRenderRequest(false); } -#if __has_include("OpenImageDenoise/oidn.hpp") -#define OPEN_IMAGE_DENOISE -#include "OpenImageDenoise/oidn.hpp" -#pragma comment(lib,"OpenImageDenoise.lib") -#pragma comment(lib,"tbb.lib") - // Also provide OpenImageDenoise.dll and tbb.dll near the exe! -#endif void ObjectComponent::SaveLightmap() { if (lightmap.IsValid() && has_flag(lightmap.desc.bind_flags, BindFlag::RENDER_TARGET)) @@ -1414,10 +1414,15 @@ namespace wi::scene init = true; } + oidn::BufferRef lightmapTextureData_buffer = device.newBuffer(lightmapTextureData.size()); + oidn::BufferRef texturedata_dst_buffer = device.newBuffer(texturedata_dst.size()); + + lightmapTextureData_buffer.write(0, lightmapTextureData.size(), lightmapTextureData.data()); + // Create a denoising filter oidn::FilterRef filter = device.newFilter("RTLightmap"); - filter.setImage("color", lightmapTextureData.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); - filter.setImage("output", texturedata_dst.data(), oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); + filter.setImage("color", lightmapTextureData_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); + filter.setImage("output", texturedata_dst_buffer, oidn::Format::Float3, width, height, 0, sizeof(XMFLOAT4)); filter.commit(); // Filter the image @@ -1430,6 +1435,10 @@ namespace wi::scene { wi::backlog::post(std::string("[OpenImageDenoise error] ") + errorMessage); } + else + { + texturedata_dst_buffer.read(0, texturedata_dst.size(), texturedata_dst.data()); + } } lightmapTextureData = std::move(texturedata_dst); // replace old (raw) data with denoised data diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 6d6af6bcf..1a8594768 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 237; + const int revision = 238; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);