From 85fd6a3a446a5286f5583e99b5d5eff9e7add782 Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Mon, 28 Mar 2022 09:15:31 +0200 Subject: [PATCH] Conditional compilation (#1367) --- CMakePresets.json | 36 +++++ apps/CMakeLists.txt | 3 + apps/ycolorgrade.cpp | 5 + apps/ycutrace.cpp | 4 + apps/ytonemap.cpp | 4 + apps/ytrace.cpp | 4 + apps/yview.cpp | 4 + libs/yocto/yocto_bvh.cpp | 6 + libs/yocto/yocto_bvh.h | 3 + libs/yocto/yocto_cutrace.cpp | 170 +-------------------- libs/yocto/yocto_cutrace.h | 34 +---- libs/yocto/yocto_gui.cpp | 283 ----------------------------------- libs/yocto/yocto_gui.h | 4 + libs/yocto/yocto_trace.cpp | 2 +- 14 files changed, 80 insertions(+), 482 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 4e6a695ca..23cd16f39 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -78,6 +78,42 @@ "strategy": "external" } }, + { + "name": "release-nogl", + "description": "Release build - no OpenGL", + "generator": "Ninja", + "binaryDir": "${sourceDir}/build/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "RelWithDebInfo", + "YOCTO_APPS": "ON", + "YOCTO_EMBREE": "OFF", + "YOCTO_DENOISE": "OFF", + "YOCTO_OPENGL": "OFF", + "YOCTO_CUDA": "OFF" + }, + "architecture": { + "value": "x64", + "strategy": "external" + } + }, + { + "name": "debug-nogl", + "description": "Debug build - no OpenGL", + "generator": "Ninja", + "binaryDir": "${sourceDir}/build/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "YOCTO_APPS": "ON", + "YOCTO_EMBREE": "OFF", + "YOCTO_DENOISE": "OFF", + "YOCTO_OPENGL": "OFF", + "YOCTO_CUDA": "OFF" + }, + "architecture": { + "value": "x64", + "strategy": "external" + } + }, { "name": "release-cuda", "description": "Release build", diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index dfe0aa48d..9831056d9 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -9,7 +9,10 @@ add_yapp(ytonemap) add_yapp(ycolorgrade) add_yapp(ytrace) + +if(YOCTO_CUDA) add_yapp(ycutrace) +endif() add_yapp(yview) diff --git a/apps/ycolorgrade.cpp b/apps/ycolorgrade.cpp index 902383e95..2e5399aee 100644 --- a/apps/ycolorgrade.cpp +++ b/apps/ycolorgrade.cpp @@ -77,6 +77,8 @@ void run(const vector& args) { // save image save_image(outname, image); } else { +#ifdef YOCTO_OPENGL + // color grading parameters auto params = colorgrade_params{}; @@ -133,6 +135,9 @@ void run(const vector& args) { // run ui show_gui_window({1280 + 320, 720}, "ycolorgrade - " + imagename, callbacks); +#else + throw io_error{"Interactive requires OpenGL"}; +#endif } } diff --git a/apps/ycutrace.cpp b/apps/ycutrace.cpp index eb2f5e670..e88432453 100644 --- a/apps/ycutrace.cpp +++ b/apps/ycutrace.cpp @@ -172,6 +172,7 @@ void run(const vector& args) { save_image(outname, image); print_info("save image: {}", elapsed_formatted(timer)); } else { +#ifdef YOCTO_OPENGL // preview state auto pparams = params; pparams.resolution /= params.pratio; @@ -260,6 +261,9 @@ void run(const vector& args) { // run ui show_gui_window({1280 + 320, 720}, "ycutrace - " + scenename, callbacks); +#else + throw io_error{"Interactive requires OpenGL"}; +#endif } } diff --git a/apps/ytonemap.cpp b/apps/ytonemap.cpp index f9c84a786..495a0184b 100644 --- a/apps/ytonemap.cpp +++ b/apps/ytonemap.cpp @@ -76,6 +76,7 @@ void run(const vector& args) { // save save_image(outname, image); } else { +#ifdef YOCTO_OPENGL // display image auto display = make_image(image.width, image.height, false); float exposure = 0; @@ -110,6 +111,9 @@ void run(const vector& args) { // run ui show_gui_window({1280 + 320, 720}, "ytonemap - " + imagename, callbacks); +#else + throw io_error{"Interactive requires OpenGL"}; +#endif } } diff --git a/apps/ytrace.cpp b/apps/ytrace.cpp index 90b128e39..ed3802c59 100644 --- a/apps/ytrace.cpp +++ b/apps/ytrace.cpp @@ -159,6 +159,7 @@ void run(const vector& args) { save_image(outname, image); print_info("save image: {}", elapsed_formatted(timer)); } else { +#ifdef YOCTO_OPENGL // rendering context auto context = make_trace_context(params); @@ -258,6 +259,9 @@ void run(const vector& args) { // done render_cancel(); +#else + throw io_error{"Interactive requires OpenGL"}; +#endif } } diff --git a/apps/yview.cpp b/apps/yview.cpp index 302b0419e..0fbe01bd0 100644 --- a/apps/yview.cpp +++ b/apps/yview.cpp @@ -39,6 +39,7 @@ using namespace std::string_literals; // main function void run(const vector& args) { +#ifdef YOCTO_OPENGL // parameters auto scenename = "scene.json"s; auto camname = ""s; @@ -66,6 +67,9 @@ void run(const vector& args) { // run viewer show_shade_gui("yview", scenename, scene, params); +#else + throw io_error{"Interactive requires OpenGL"}; +#endif } // Run diff --git a/libs/yocto/yocto_bvh.cpp b/libs/yocto/yocto_bvh.cpp index 02595091e..2fc5ddb17 100644 --- a/libs/yocto/yocto_bvh.cpp +++ b/libs/yocto/yocto_bvh.cpp @@ -836,6 +836,9 @@ struct embree_error : std::runtime_error { #ifdef YOCTO_EMBREE +// Check if embree is supported +bool embree_supported() { return true; } + // Get Embree device atomic embree_memory = 0; static RTCDevice embree_device() { @@ -1096,6 +1099,9 @@ scene_intersection intersect_instance_embree_bvh(const scene_embree_bvh& bvh, #else +// Check if embree is supported +bool embree_supported() { return false; } + // Not implemented shape_embree_bvh make_shape_embree_bvh( const shape_data& shape, bool highquality) { diff --git a/libs/yocto/yocto_bvh.h b/libs/yocto/yocto_bvh.h index 499bc16c0..52898f2d2 100644 --- a/libs/yocto/yocto_bvh.h +++ b/libs/yocto/yocto_bvh.h @@ -161,6 +161,9 @@ struct scene_embree_bvh { unique_ptr embree_bvh = {nullptr, nullptr}; // embree }; +// Check if embree is supported +bool embree_supported(); + // Build the bvh acceleration structure. shape_embree_bvh make_shape_embree_bvh( const shape_data& shape, bool highquality = false); diff --git a/libs/yocto/yocto_cutrace.cpp b/libs/yocto/yocto_cutrace.cpp index 6e65d25bd..1097f1f63 100644 --- a/libs/yocto/yocto_cutrace.cpp +++ b/libs/yocto/yocto_cutrace.cpp @@ -26,19 +26,12 @@ // SOFTWARE. // -// -// TODO: upload -// TODO: path tracing -// TODO: environments -// TODO: interactive rendering -// - #include "yocto_cutrace.h" -#include "yocto_sampling.h" - #if YOCTO_CUDA +#include "yocto_sampling.h" + // do not reorder #include // do not reorder @@ -1139,163 +1132,4 @@ bool is_display(const cutrace_context& context) { } // namespace yocto -#else - -// ----------------------------------------------------------------------------- -// NO CUDA -// ----------------------------------------------------------------------------- -namespace yocto { - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4722) -#endif - -static void exit_nocuda() { throw std::runtime_error{"Cuda not linked"}; } - -cuscene_data::cuscene_data(cuscene_data&& other) { exit_nocuda(); } -cuscene_data& cuscene_data::operator=(cuscene_data&& other) { - exit_nocuda(); - return *this; -} -cuscene_data::~cuscene_data() { exit_nocuda(); }; - -cubvh_data::cubvh_data(cubvh_data&& other) { exit_nocuda(); } -cubvh_data& cubvh_data::operator=(cubvh_data&& other) { - exit_nocuda(); - return *this; -} -cubvh_data::~cubvh_data() { exit_nocuda(); } - -cutrace_context::cutrace_context(cutrace_context&& other) { exit_nocuda(); } -cutrace_context& cutrace_context::operator=(cutrace_context&& other) { - exit_nocuda(); - return *this; -} -cutrace_context::~cutrace_context() { exit_nocuda(); } - -cutrace_state::~cutrace_state() { exit_nocuda(); } -cutrace_lights::~cutrace_lights() { exit_nocuda(); } - -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -image_data cutrace_image(const scene_data& scene, const trace_params& params) { - exit_nocuda(); - return {}; -} - -// Initialize GPU context. -cutrace_context make_cutrace_context(const trace_params& params) { - exit_nocuda(); - return {}; -} - -// Upload the scene to the GPU. -cuscene_data make_cutrace_scene(cutrace_context& context, - const scene_data& scene, const trace_params& params) { - exit_nocuda(); - return {}; -} - -// Update cameras -void update_cutrace_cameras(cutrace_context& context, cuscene_data& cuscene, - const scene_data& scene, const trace_params& params) { - exit_nocuda(); -} - -// Build the bvh acceleration structure. -cubvh_data make_cutrace_bvh(cutrace_context& context, - const cuscene_data& cuscene, const trace_params& params) { - exit_nocuda(); - return {}; -} - -// Initialize state. -cutrace_state make_cutrace_state(cutrace_context& context, - const scene_data& scene, const trace_params& params) { - exit_nocuda(); - return {}; -} -void reset_cutrace_state(cutrace_context& context, cutrace_state& state, - const scene_data& scene, const trace_params& params) { - exit_nocuda(); -} - -// Initialize lights. -cutrace_lights make_cutrace_lights(cutrace_context& context, - const scene_data& scene, const trace_params& params) { - exit_nocuda(); - return {}; -} - -// Start rendering an image. -void trace_start(cutrace_context& context, cutrace_state& state, - const cuscene_data& cuscene, const cubvh_data& bvh, - const cutrace_lights& lights, const scene_data& scene, - const trace_params& params) { - exit_nocuda(); -} - -// Progressively computes an image. -void trace_samples(cutrace_context& context, cutrace_state& state, - const cuscene_data& cuscene, const cubvh_data& bvh, - const cutrace_lights& lights, const scene_data& scene, - const trace_params& params) { - exit_nocuda(); -} - -// render preview -void trace_preview(image_data& image, cutrace_context& context, - cutrace_state& state, const cuscene_data& cuscene, const cubvh_data& bvh, - const cutrace_lights& lights, const scene_data& scene, - const trace_params& params) { - exit_nocuda(); - return; -} - -// Get render -image_data get_image(const cutrace_state& state) { - exit_nocuda(); - return {}; -} -void get_image(image_data& image, const cutrace_state& state) { exit_nocuda(); } - -// Get resulting render -image_data get_rendered_image(const cutrace_state& state) { - exit_nocuda(); - return {}; -} -void get_rendered_image(image_data& image, const cutrace_state& state) { - exit_nocuda(); -} - -// Get denoised result -image_data get_denoised_image(const cutrace_state& state) { - exit_nocuda(); - return {}; -} -void get_denoised_image(image_data& image, const cutrace_state& state) { - exit_nocuda(); -} - -// Get denoising buffers -image_data get_albedo_image(const cutrace_state& state) { - exit_nocuda(); - return {}; -} -void get_albedo_image(image_data& image, const cutrace_state& state) { - exit_nocuda(); -} -image_data get_normal_image(const cutrace_state& state) { - exit_nocuda(); - return {}; -} -void get_normal_image(image_data& image, const cutrace_state& state) { - exit_nocuda(); -} - -} // namespace yocto - #endif \ No newline at end of file diff --git a/libs/yocto/yocto_cutrace.h b/libs/yocto/yocto_cutrace.h index a96e9dbec..d373d44ef 100644 --- a/libs/yocto/yocto_cutrace.h +++ b/libs/yocto/yocto_cutrace.h @@ -36,6 +36,8 @@ #ifndef _YOCTO_CUTRACE_H_ #define _YOCTO_CUTRACE_H_ +#ifdef YOCTO_CUDA + // ----------------------------------------------------------------------------- // INCLUDES // ----------------------------------------------------------------------------- @@ -171,32 +173,11 @@ inline const auto cutrace_falsecolor_labels = trace_falsecolor_labels; // // ----------------------------------------------------------------------------- -#if YOCTO_CUDA - // do not reorder #include // do not reorder #include -#else - -// placeholder types -using CUdeviceptr = void*; -using OptixInstance = yocto::instance_data; -using OptixTraversableHandle = unsigned long long; -using CUcontext = void*; -using CUstream = void*; -using OptixDeviceContext = void*; -using OptixPipeline = void*; -using OptixProgramGroup = void*; -using OptixModule = void*; -using OptixShaderBindingTable = void*; -using CUarray = void*; -using CUtexObject = void*; -using OptixDenoiser = void*; - -#endif - // ----------------------------------------------------------------------------- // DATA DEFINITIONS // ----------------------------------------------------------------------------- @@ -359,21 +340,12 @@ struct cutrace_globals { trace_params params = {}; }; -#if YOCTO_CUDA - // empty stb record struct __declspec(align(OPTIX_SBT_RECORD_ALIGNMENT)) cutrace_stbrecord { __declspec(align( OPTIX_SBT_RECORD_ALIGNMENT)) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; }; -#else - -// empty stb record -struct cutrace_stbrecord {}; - -#endif - struct cutrace_context { // context CUcontext cuda_context = nullptr; @@ -410,3 +382,5 @@ struct cutrace_context { } // namespace yocto #endif + +#endif diff --git a/libs/yocto/yocto_gui.cpp b/libs/yocto/yocto_gui.cpp index cc7131a6d..873dfe9b8 100644 --- a/libs/yocto/yocto_gui.cpp +++ b/libs/yocto/yocto_gui.cpp @@ -2904,287 +2904,4 @@ bool draw_gui_params(const string& name, glwidgets_params& params) { } // namespace yocto -#else - -// ----------------------------------------------------------------------------- -// NO OPENGL -// ----------------------------------------------------------------------------- -namespace yocto { - -static void exit_nogl() { throw std::runtime_error{"opengl not linked\n"}; } - -// create image drawing program -bool init_image(glimage_state& glimage) { exit_nogl(); } - -// clear image -void clear_image(glimage_state& glimage) { exit_nogl(); } - -// update image data -void set_image(glimage_state& glimage, const image_data& image) { exit_nogl(); } - -// draw image -void draw_image(glimage_state& image, const glimage_params& params) { - exit_nogl(); -} - -// draw tonemap params -bool draw_tonemap_widgets( - const gui_input& input, float& exposure, bool& filmic) { - exit_nogl(); - return false; -} - -// draw image inspector -bool draw_image_widgets(const gui_input& input, const image_data& image, - const image_data& display, glimage_params& glparams) { - exit_nogl(); - return false; -} -bool draw_image_widgets( - const gui_input& input, const image_data& image, glimage_params& glparams) { - exit_nogl(); - return false; -} - -// update image params -void update_image_params( - const gui_input& input, const image_data& image, glimage_params& glparams) { - exit_nogl(); -} - -// update image params from mouse -bool uiupdate_image_params(const gui_input& input, glimage_params& glparams) { - exit_nogl(); - return false; -} - -// update camera from mouse -bool uiupdate_camera_params(const gui_input& input, camera_data& camera) { - exit_nogl(); - return false; -} - -// draw trace params -bool draw_trace_widgets(const gui_input& input, int sample, - trace_params& params, const vector& camera_names) { - exit_nogl(); - return false; -} - -// draw scene editor -bool draw_scene_widgets(scene_data& scene, scene_selection& selection, - const function& before_edit) { - exit_nogl(); - return false; -} - -// Open a window and show an image -void show_image_gui( - const string& title, const string& name, const image_data& image) { - exit_nogl(); -} - -// Open a window and show a set of images -void show_image_gui(const string& title, const vector& names, - const vector& images) { - exit_nogl(); -} - -// Open a window and show an image for color grading -void show_colorgrade_gui( - const string& title, const string& name, const image_data& image) { - exit_nogl(); -} - -// Open a window and show an scene via path tracing -void show_trace_gui(const string& title, const string& name, scene_data& scene, - const trace_params& params, bool print, bool edit) { - exit_nogl(); -} - -// Open a window and show an scene via OpenGL shading -struct shade_params; -void show_shade_gui(const string& title, const string& name, scene_data& scene, - const shade_params& params, const glview_callback& widgets_callback, - const glview_callback& uiupdate_callback, - const glview_callback& update_callback) { - exit_nogl(); -} - -} // namespace yocto - -// ----------------------------------------------------------------------------- -// WINDOW -// ----------------------------------------------------------------------------- -namespace yocto { - -// run the user interface with the give callbacks -void show_gui_window(const vec2i& size, const string& title, - const gui_callbacks& callbaks, int widgets_width, bool widgets_left) { - exit_nogl(); -} - -} // namespace yocto - -// ----------------------------------------------------------------------------- -// WIDGETS -// ----------------------------------------------------------------------------- -namespace yocto { - -// Headers -bool draw_gui_header(const char* title) { - exit_nogl(); - return false; -} -void end_gui_header() { exit_nogl(); } - -// Labels -void draw_gui_label(const char* lbl, const string& text) { exit_nogl(); } -void draw_gui_label(const char* lbl, int value) { exit_nogl(); } -void draw_gui_label(const char* lbl, bool value) { exit_nogl(); } - -// Lines -void draw_gui_separator() { exit_nogl(); } -void continue_gui_line() { exit_nogl(); } - -// Buttons -bool draw_gui_button(const char* lbl, bool enabled) { - exit_nogl(); - return false; -} - -// Text -bool draw_gui_textinput(const char* lbl, string& value) { - exit_nogl(); - return false; -} - -// Slider -bool draw_gui_slider(const char* lbl, float& value, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec2f& value, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec3f& value, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec4f& value, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, int& value, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec2i& value, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec3i& value, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_slider(const char* lbl, vec4i& value, int min, int max) { - exit_nogl(); - return false; -} - -// Dragger -bool draw_gui_dragger( - const char* lbl, float& value, float speed, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec2f& value, float speed, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec3f& value, float speed, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec4f& value, float speed, float min, float max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, int& value, float speed, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec2i& value, float speed, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec3i& value, float speed, int min, int max) { - exit_nogl(); - return false; -} -bool draw_gui_dragger( - const char* lbl, vec4i& value, float speed, int min, int max) { - exit_nogl(); - return false; -} - -// Checkbox -bool draw_gui_checkbox(const char* lbl, bool& value) { - exit_nogl(); - return false; -} -bool draw_gui_checkbox(const char* lbl, bool& value, bool invert) { - exit_nogl(); - return false; -} - -// Color editor -bool draw_gui_coloredit(const char* lbl, vec3f& value) { - exit_nogl(); - return false; -} -bool draw_gui_coloredit(const char* lbl, vec4f& value) { - exit_nogl(); - return false; -} -bool draw_gui_coloredit(const char* lbl, vec4b& value) { - exit_nogl(); - return false; -} -bool draw_gui_coloredithdr(const char* lbl, vec3f& value) { - exit_nogl(); - return false; -} -bool draw_gui_coloredithdr(const char* lbl, vec4f& value) { - exit_nogl(); - return false; -} - -// Combo box -bool draw_gui_combobox(const char* lbl, int& idx, const vector& labels, - bool include_null) { - exit_nogl(); - return false; -} -bool draw_gui_combobox(const char* lbl, string& value, - const vector& labels, bool include_null) { - exit_nogl(); - return false; -} - -// Progress bar -void draw_gui_progressbar(const char* lbl, float fraction) { exit_nogl(); } -void draw_gui_progressbar(const char* lbl, int current, int total) { - exit_nogl(); -} - -} // namespace yocto - #endif \ No newline at end of file diff --git a/libs/yocto/yocto_gui.h b/libs/yocto/yocto_gui.h index 8124e8b8c..50b984f79 100644 --- a/libs/yocto/yocto_gui.h +++ b/libs/yocto/yocto_gui.h @@ -30,6 +30,8 @@ #ifndef _YOCTO_GLVIEW_ #define _YOCTO_GLVIEW_ +#ifdef YOCTO_OPENGL + // ----------------------------------------------------------------------------- // INCLUDES // ----------------------------------------------------------------------------- @@ -328,3 +330,5 @@ bool draw_scene_widgets(scene_data& scene, scene_selection& selection, } // namespace yocto #endif + +#endif diff --git a/libs/yocto/yocto_trace.cpp b/libs/yocto/yocto_trace.cpp index 8e21f1b27..dfc72c9f5 100644 --- a/libs/yocto/yocto_trace.cpp +++ b/libs/yocto/yocto_trace.cpp @@ -86,7 +86,7 @@ namespace yocto { // Build the Bvh acceleration structure. trace_bvh make_trace_bvh(const scene_data& scene, const trace_params& params) { - if (params.embreebvh) { + if (params.embreebvh && embree_supported()) { return {{}, make_scene_embree_bvh(scene, params.highqualitybvh, params.noparallel)}; } else {