Skip to content

Commit

Permalink
[add] cleanup and enable sol safeties for function objects
Browse files Browse the repository at this point in the history
  • Loading branch information
begla committed Aug 18, 2024
1 parent 42211b0 commit c3bfd57
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 169 deletions.
2 changes: 1 addition & 1 deletion iolite_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ add_library(IoliteLuaPlugin SHARED
)

list(APPEND PLUGINS IoliteLuaPlugin)
target_compile_definitions(IoliteLuaPlugin PUBLIC SOL_LUAJIT=1 SOL_ALL_SAFETIES_ON=1)
target_compile_definitions(IoliteLuaPlugin PUBLIC SOL_LUAJIT=1 SOL_ALL_SAFETIES_ON=1 SOL_SAFE_FUNCTION_OBJECTS=1)
target_link_libraries(IoliteLuaPlugin ${LUA_JIT_LIBRARIES})

if(MSVC)
Expand Down
4 changes: 0 additions & 4 deletions iolite_plugins/benchmark_plugin/benchmark_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@

// Dependencies
#include <vector>
#include <glm.hpp>
#include <gtc/quaternion.hpp>
#include <gtx/norm.hpp>
#include "ext/scalar_constants.hpp"
#include "geometric.hpp"
#include "imgui.h"
#include "IconsFontAwesome6.h"

Expand Down
197 changes: 38 additions & 159 deletions iolite_plugins/denoiser_oidn_plugin/denoiser_oidn_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
// SOFTWARE.

// Dependencies
#include "oidn.h"
#include <cstring>

// API
#define _IO_PLUGIN_NAME "DenoiserOIDN"
#include "iolite_api.h"
#include "iolite_plugins_common.h"
#include "iolite_plugins_libraries.h"

// Interfaces we use
//----------------------------------------------------------------------------//
Expand All @@ -39,125 +38,7 @@ static const io_logging_i* io_logging = nullptr;
static io_user_denoiser_i io_user_denoiser = {};

//----------------------------------------------------------------------------//
static struct oidn_t
{
// NOLINTBEGIN
using oidn_set_device_int_t = void (*)(OIDNDevice device, const char* name,
int value);
using oidn_new_device_t = OIDNDevice (*)(OIDNDeviceType type);
using oidn_commit_device_t = void (*)(OIDNDevice device);
using oidn_new_filter_t = OIDNFilter (*)(OIDNDevice device, const char* type);
using oidn_new_buffer_t = OIDNBuffer (*)(OIDNDevice device, size_t byteSize);
using oidn_set_filter_image_t = void (*)(OIDNFilter filter, const char* name,
OIDNBuffer buffer, OIDNFormat format,
size_t width, size_t height,
size_t byteOffset,
size_t pixelByteStride,
size_t rowByteStride);
using oidn_set_shared_filter_image_t =
void (*)(OIDNFilter filter, const char* name, void* devPtr,
OIDNFormat format, size_t width, size_t height,
size_t byteOffset, size_t pixelByteStride, size_t rowByteStride);
using oidn_set_filter_bool_t = void (*)(OIDNFilter filter, const char* name,
bool value);
using oidn_set_filter_int_t = void (*)(OIDNFilter filter, const char* name,
int value);
using oidn_set_filter_progress_monitor_function_t = void (*)(
OIDNFilter filter, OIDNProgressMonitorFunction func, void* userPtr);
using oidn_commit_filter_t = void (*)(OIDNFilter filter);
using oidn_execute_filter_t = void (*)(OIDNFilter filter);
using oidn_get_device_error_t = OIDNError (*)(OIDNDevice device,
const char** outMessage);
using oidn_get_buffer_data_t = void* (*)(OIDNBuffer buffer);
using oidn_read_buffer_t = void (*)(OIDNBuffer buffer, size_t byteOffset,
size_t byteSize, void* dstHostPtr);
using oidn_write_buffer_t = void (*)(OIDNBuffer buffer, size_t byteOffset,
size_t byteSize, const void* srcHostPtr);
using oidn_release_filter_t = void (*)(OIDNFilter filter);
using oidn_release_buffer_t = void (*)(OIDNBuffer buffer);
using oidn_release_device_t = void (*)(OIDNDevice device);

oidn_set_device_int_t oidnSetDeviceInt;
oidn_new_device_t oidnNewDevice;
oidn_commit_device_t oidnCommitDevice;
oidn_new_filter_t oidnNewFilter;
oidn_new_buffer_t oidnNewBuffer;
oidn_set_filter_image_t oidnSetFilterImage;
oidn_set_shared_filter_image_t oidnSetSharedFilterImage;
oidn_set_filter_bool_t oidnSetFilterBool;
oidn_set_filter_int_t oidnSetFilterInt;
oidn_set_filter_progress_monitor_function_t
oidnSetFilterProgressMonitorFunction;
oidn_commit_filter_t oidnCommitFilter;
oidn_execute_filter_t oidnExecuteFilter;
oidn_get_device_error_t oidnGetDeviceError;
oidn_read_buffer_t oidnReadBuffer;
oidn_write_buffer_t oidnWriteBuffer;
oidn_get_buffer_data_t oidnGetBufferData;
oidn_release_filter_t oidnReleaseFilter;
oidn_release_buffer_t oidnReleaseBuffer;
oidn_release_device_t oidnReleaseDevice;
// NOLINTEND

void* oidn{nullptr};

bool init{false};
} oidn;

//----------------------------------------------------------------------------//
static io_bool_t unload_oidn(bool error = false)
{
if (error)
common::log_message(io_logging, "Failed to load OIDN library!");

if (oidn.oidn)
common::unload_library(oidn.oidn);

memset(&oidn, 0, sizeof(oidn));
return !error;
};

//----------------------------------------------------------------------------//
static io_bool_t load_oidn()
{
if (oidn.init)
return true;

oidn.oidn = common::load_library("OpenImageDenoise");
if (!oidn.oidn)
return unload_oidn(true);

#define LOAD_FUNCTION(_function, _type) \
oidn._function = \
(oidn_t::_type)common::load_function(oidn.oidn, #_function); \
if (!oidn._function) \
return unload_oidn(true);

LOAD_FUNCTION(oidnSetDeviceInt, oidn_set_device_int_t);
LOAD_FUNCTION(oidnNewDevice, oidn_new_device_t);
LOAD_FUNCTION(oidnCommitDevice, oidn_commit_device_t);
LOAD_FUNCTION(oidnNewFilter, oidn_new_filter_t);
LOAD_FUNCTION(oidnNewBuffer, oidn_new_buffer_t);
LOAD_FUNCTION(oidnSetFilterImage, oidn_set_filter_image_t);
LOAD_FUNCTION(oidnSetSharedFilterImage, oidn_set_shared_filter_image_t);
LOAD_FUNCTION(oidnSetFilterBool, oidn_set_filter_bool_t);
LOAD_FUNCTION(oidnSetFilterInt, oidn_set_filter_int_t);
LOAD_FUNCTION(oidnSetFilterProgressMonitorFunction,
oidn_set_filter_progress_monitor_function_t);
LOAD_FUNCTION(oidnCommitFilter, oidn_commit_filter_t);
LOAD_FUNCTION(oidnExecuteFilter, oidn_execute_filter_t);
LOAD_FUNCTION(oidnGetDeviceError, oidn_get_device_error_t);
LOAD_FUNCTION(oidnGetBufferData, oidn_get_buffer_data_t);
LOAD_FUNCTION(oidnReadBuffer, oidn_read_buffer_t);
LOAD_FUNCTION(oidnWriteBuffer, oidn_write_buffer_t);
LOAD_FUNCTION(oidnReleaseFilter, oidn_release_filter_t);
LOAD_FUNCTION(oidnReleaseBuffer, oidn_release_buffer_t);
LOAD_FUNCTION(oidnReleaseDevice, oidn_release_device_t);

#undef LOAD_FUNCTION

return true;
};
static const char* get_name() { return "OIDN"; }

//----------------------------------------------------------------------------//
static bool on_progress_report(void* user_ptr, double n)
Expand All @@ -170,62 +51,62 @@ static bool on_progress_report(void* user_ptr, double n)
static void denoise(io_uint32_t width, io_uint32_t height,
const io_vec4_t* input, io_vec4_t* output)
{
if (!load_oidn())
if (!libraries::load_oidn(io_logging))
return;

common::log_message(io_logging, "Denoising image...");

OIDNDevice device = oidn.oidnNewDevice(OIDN_DEVICE_TYPE_DEFAULT);
// oidn.oidnSetDeviceInt(device, "verbose", 4);
oidn.oidnCommitDevice(device);
OIDNDevice device = libraries::oidn.oidnNewDevice(OIDN_DEVICE_TYPE_DEFAULT);
// libraries::oidn.oidnSetDeviceInt(device, "verbose", 4);
libraries::oidn.oidnCommitDevice(device);

OIDNBuffer input_buffer =
oidn.oidnNewBuffer(device, width * height * sizeof(*input));
libraries::oidn.oidnNewBuffer(device, width * height * sizeof(*input));
OIDNBuffer output_buffer =
oidn.oidnNewBuffer(device, width * height * sizeof(*output));

oidn.oidnWriteBuffer(input_buffer, 0u, width * height * sizeof(*input),
input);
oidn.oidnWriteBuffer(output_buffer, 0u, width * height * sizeof(*input),
input);

OIDNFilter filter = oidn.oidnNewFilter(device, "RT");
oidn.oidnSetFilterImage(filter, "color", input_buffer, OIDN_FORMAT_FLOAT3,
width, height, 0, 16u, 0);
oidn.oidnSetFilterImage(filter, "output", output_buffer, OIDN_FORMAT_FLOAT3,
width, height, 0, 16u, 0);
oidn.oidnSetFilterBool(filter, "hdr", true);
// oidn.oidnSetFilterInt(filter, "quality", OIDN_QUALITY_HIGH);
oidn.oidnSetFilterProgressMonitorFunction(filter, on_progress_report,
nullptr);
oidn.oidnCommitFilter(filter);
oidn.oidnExecuteFilter(filter);
libraries::oidn.oidnNewBuffer(device, width * height * sizeof(*output));

libraries::oidn.oidnWriteBuffer(input_buffer, 0u,
width * height * sizeof(*input), input);
libraries::oidn.oidnWriteBuffer(output_buffer, 0u,
width * height * sizeof(*input), input);

OIDNFilter filter = libraries::oidn.oidnNewFilter(device, "RT");
libraries::oidn.oidnSetFilterImage(filter, "color", input_buffer,
OIDN_FORMAT_FLOAT3, width, height, 0, 16u,
0);
libraries::oidn.oidnSetFilterImage(filter, "output", output_buffer,
OIDN_FORMAT_FLOAT3, width, height, 0, 16u,
0);
libraries::oidn.oidnSetFilterBool(filter, "hdr", true);
// libraries::oidn.oidnSetFilterInt(filter, "quality", OIDN_QUALITY_HIGH);
libraries::oidn.oidnSetFilterProgressMonitorFunction(
filter, on_progress_report, nullptr);
libraries::oidn.oidnCommitFilter(filter);
libraries::oidn.oidnExecuteFilter(filter);

const char* error_message;
if (oidn.oidnGetDeviceError(device, &error_message) != OIDN_ERROR_NONE)
if (libraries::oidn.oidnGetDeviceError(device, &error_message) !=
OIDN_ERROR_NONE)
{
common::log_message(io_logging, "OIDN ERROR: %s", error_message);
}
else
{
oidn.oidnReadBuffer(output_buffer, 0u, width * height * sizeof(*output),
output);
libraries::oidn.oidnReadBuffer(output_buffer, 0u,
width * height * sizeof(*output), output);
}

oidn.oidnReleaseBuffer(input_buffer);
oidn.oidnReleaseBuffer(output_buffer);
libraries::oidn.oidnReleaseBuffer(input_buffer);
libraries::oidn.oidnReleaseBuffer(output_buffer);

oidn.oidnReleaseFilter(filter);
oidn.oidnReleaseDevice(device);
libraries::oidn.oidnReleaseFilter(filter);
libraries::oidn.oidnReleaseDevice(device);

common::log_message(io_logging, "Done!");

unload_oidn();
libraries::unload_oidn(io_logging);
}

//----------------------------------------------------------------------------//
static const char* get_name() { return "OIDN"; }

//----------------------------------------------------------------------------//
IO_API_EXPORT io_uint32_t IO_API_CALL get_api_version()
{
Expand All @@ -241,12 +122,10 @@ IO_API_EXPORT int IO_API_CALL load_plugin(void* api_manager)
io_logging =
(const io_logging_i*)io_api_manager->find_first(IO_LOGGING_API_NAME);

if (!load_oidn())
{
if (!libraries::load_oidn(io_logging))
// Unsupported if OIDN is not available
return -1;
}
unload_oidn();
libraries::unload_oidn(io_logging);

// Register the interfaces we provide
{
Expand Down
2 changes: 1 addition & 1 deletion iolite_plugins/lua_plugin/init_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2905,7 +2905,7 @@ void script_init_state(sol::state& s)
// @function fill
// @summary Fills all voxels in the range defined by min and max.
// @param component Ref The voxel shape component.
// @param min U8Vec3 The min voxel coorindate of the area.
// @param min U8Vec3 The min voxel coordinate of the area.
// @param max U8Vec3 The max voxel coordinate of the area.
// @param palette_index number The palette index to set.
s["VoxelShape"]["fill"] = io_component_voxel_shape->fill;
Expand Down
9 changes: 5 additions & 4 deletions iolite_plugins/shared/iolite_plugins_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "iolite_api.h"

// Dependencies
#include <vector>
#include <string>
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
Expand Down Expand Up @@ -53,7 +54,7 @@ void* load_library(const char* name)
#ifdef _WIN32
return LoadLibraryA(name);
#else
void * handle = dlopen(name, RTLD_LAZY);
void* handle = dlopen(name, RTLD_LAZY);
if (!handle)
handle = dlopen((std::string("lib") + name).c_str(), RTLD_LAZY);
if (!handle)
Expand Down Expand Up @@ -88,14 +89,14 @@ void unload_library(void* handle)
inline static void log_message(const io_logging_i* io_logging, const char* fmt,
...)
{
char buffer[256];
std::vector<char> buffer(128u * 1024u);

va_list args;
va_start(args, fmt);
stbsp_vsnprintf(buffer, 256, fmt, args);
stbsp_vsnprintf(buffer.data(), buffer.size(), fmt, args);
va_end(args);

io_logging->log_plugin(_IO_PLUGIN_NAME, buffer);
io_logging->log_plugin(_IO_PLUGIN_NAME, buffer.data());
}

//----------------------------------------------------------------------------//
Expand Down
Loading

0 comments on commit c3bfd57

Please sign in to comment.