From 67d56d07ec7863b6b3cb9f2ba554d21d4f01d2fa Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 27 Nov 2026 06:54:56 +0200 Subject: [PATCH 1/3] add the Windows DirectX 12 rendering variant Adds a RUISAPP_RENDER_DIRECTX variant alongside the existing OpenGL, OpenGL ES and Vulkan ones. It wires the ruis-render-directx renderer into the application factory and creates the DirectX render context from the window handle. The variant is Windows-only and disabled for the vcpkg port build (the ruis-render-directx vcpkg port does not exist yet). --- build/cmake/CMakeLists.txt | 46 ++++++++++++++++++++++++ src/makefile | 13 +++++-- src/ruisapp/glue/windows/application.cxx | 24 +++++++++++-- src/ruisapp/glue/windows/application.hxx | 20 ++++++++++- src/ruisapp/glue/windows/glue.cxx | 5 +++ src/ruisapp/glue/windows/window.cxx | 20 +++++++---- src/ruisapp/glue/windows/window.hxx | 6 ++-- 7 files changed, 119 insertions(+), 15 deletions(-) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 3743a8fa..784668f8 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -43,10 +43,17 @@ elseif(WIN32) set(ruisapp_build_opengl_variant True) set(ruisapp_build_opengles_variant True) set(ruisapp_build_vulkan_variant True) # VK_KHR_win32_surface + # DXGI swapchain; disabled for the vcpkg port build because the + # ruis-render-directx vcpkg port does not exist yet + set(ruisapp_build_directx_variant True) + if(IS_VCPKG_PORT_BUILD) + set(ruisapp_build_directx_variant False) + endif() set(ruisapp_opengl_install_include_directory ../../src/${PROJECT_NAME}) set(ruisapp_opengles_install_include_directory) # opengl variant is also built and will install the headers set(ruisapp_vulkan_install_include_directory) # opengl variant is also built and will install the headers + set(ruisapp_directx_install_include_directory) # opengl variant is also built and will install the headers elseif(EMSCRIPTEN) set(ruisapp_build_opengl_variant True) set(ruisapp_build_opengles_variant True) @@ -301,6 +308,30 @@ else() NO_EXPORT ) endif() + + if(ruisapp_build_directx_variant) + myci_declare_library(${PROJECT_NAME}-directx + SOURCES + ${srcs} + PUBLIC_INCLUDE_DIRECTORIES + ../../src + INSTALL_INCLUDE_DIRECTORIES + ${ruisapp_directx_install_include_directory} + PREPROCESSOR_DEFINITIONS + "RUISAPP_RENDER_DIRECTX" + DEPENDENCIES + nitki + ruis + ruis-render-directx + opros + setka + tml + NO_EXPORT + ) + + # link the Windows SDK DirectX 12 libraries (not available via vcpkg) + target_link_libraries(${PROJECT_NAME}-directx PRIVATE d3d12 dxgi d3dcompiler) + endif() endif() # ==================== @@ -329,6 +360,9 @@ else() if(ruisapp_build_vulkan_variant) list(APPEND ruisapp_export_targets ${PROJECT_NAME}-vulkan) endif() + if(ruisapp_build_directx_variant) + list(APPEND ruisapp_export_targets ${PROJECT_NAME}-directx) + endif() myci_export( TARGETS ${ruisapp_export_targets} @@ -419,4 +453,16 @@ else() ruisapp::${PROJECT_NAME}-vulkan ) endif() + + if(ruisapp_build_directx_variant) + myci_declare_application(${PROJECT_NAME}-directx-test + GUI + SOURCES + ${app_srcs} + RESOURCE_DIRECTORY + ../../tests/app/res + DEPENDENCIES + ruisapp::${PROJECT_NAME}-directx + ) + endif() endif() diff --git a/src/makefile b/src/makefile index 5b385302..9848d268 100644 --- a/src/makefile +++ b/src/makefile @@ -86,8 +86,14 @@ define ruisapp_rules else ifeq ($(os), windows) this_ldlibs += -l gdi32 - this_ldlibs += -l opengl32 - this_ldlibs += -l glew32 + ifeq ($1,directx) + this_ldlibs += -l d3d12 + this_ldlibs += -l dxgi + this_ldlibs += -l d3dcompiler + else + this_ldlibs += -l opengl32 + this_ldlibs += -l glew32 + endif else ifeq ($(os), macosx) this_ldlibs += -l GLEW @@ -153,6 +159,9 @@ ifeq ($(os), linux) else $(eval $(call ruisapp_rules,opengl,)) + ifeq ($(os), windows) + $(eval $(call ruisapp_rules,directx,)) + endif endif # clear variable diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 2c8c8a4b..5da12067 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -101,7 +101,7 @@ ruisapp::application::directories get_application_directories(std::string_view a application_glue::application_glue(const utki::version_duplet& gl_version) : gl_version(gl_version), -#ifndef RUISAPP_RENDER_VULKAN +#if !defined(RUISAPP_RENDER_VULKAN) && !defined(RUISAPP_RENDER_DIRECTX) shared_gl_context_native_window(utki::make_shared( this->display, this->gl_version, @@ -114,6 +114,9 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : #endif #ifdef RUISAPP_RENDER_VULKAN device(make_vulkan_device()), +#endif +#ifdef RUISAPP_RENDER_DIRECTX + device(make_directx_device()), #endif render_factory([&]() { #ifdef RUISAPP_RENDER_VULKAN @@ -121,6 +124,12 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : return utki::make_shared( utki::make_shared(this->device.get(), VK_NULL_HANDLE) ); +#elif defined(RUISAPP_RENDER_DIRECTX) + // DirectX 12 has no "current context" — a null surface (no window) is sufficient + // for the factory (resource creation); the swapchains are created by the render contexts. + return utki::make_shared( + utki::make_shared(this->device.get(), nullptr) + ); #else utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n'); # ifdef RUISAPP_RENDER_OPENGL @@ -196,7 +205,7 @@ app_window* application_glue::get_window(native_window::window_id_type id) ruisapp::window& application_glue::make_window(ruisapp::window_parameters window_params) { -#ifdef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_VULKAN) || defined(RUISAPP_RENDER_DIRECTX) auto ruis_native_window = utki::make_shared(this->display, window_params); #else auto ruis_native_window = utki::make_shared( @@ -235,6 +244,17 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window }(), this->render_factory ), +#elif defined(RUISAPP_RENDER_DIRECTX) + utki::make_shared( + ruis_native_window, + // The surface just holds the window handle; the render context creates + // the DXGI swapchain from it (owned by the context, destroyed with it). + utki::make_shared( + this->device.get(), // + ruis_native_window.get().get_hwnd() + ), + this->render_factory + ), #elif defined(RUISAPP_RENDER_OPENGL) utki::make_shared(ruis_native_window, this->render_factory), #elif defined(RUISAPP_RENDER_OPENGLES) diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index e4d65967..0daa2551 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -33,6 +33,9 @@ along with this program. If not, see . # include # include # include +#elif defined(RUISAPP_RENDER_DIRECTX) +# include +# include #endif #include "display.hxx" @@ -91,22 +94,37 @@ const utki::shared_ref make_vulkan_device() } #endif +#ifdef RUISAPP_RENDER_DIRECTX +// The D3D12 device and the shared command queue are created once per process +// and shared by the render factory and all the render contexts (windows). +const utki::shared_ref make_directx_device() +{ + return utki::make_shared(); +} +#endif + class application_glue : public utki::destructable { const utki::version_duplet gl_version; utki::shared_ref display = utki::make_shared(); -#ifndef RUISAPP_RENDER_VULKAN +#if !defined(RUISAPP_RENDER_VULKAN) && !defined(RUISAPP_RENDER_DIRECTX) const utki::shared_ref shared_gl_context_native_window; #endif #ifdef RUISAPP_RENDER_VULKAN // The Vulkan device is shared between the factory (resource creation) and the windows (surface creation). const utki::shared_ref device; +#endif +#ifdef RUISAPP_RENDER_DIRECTX + // The DirectX 12 device is shared between the factory (resource creation) and the windows (swapchain creation). + const utki::shared_ref device; #endif // concrete factory type, #ifdef by renderer #ifdef RUISAPP_RENDER_VULKAN const utki::shared_ref render_factory; +#elif defined(RUISAPP_RENDER_DIRECTX) + const utki::shared_ref render_factory; #elif defined(RUISAPP_RENDER_OPENGL) const utki::shared_ref render_factory; #elif defined(RUISAPP_RENDER_OPENGLES) diff --git a/src/ruisapp/glue/windows/glue.cxx b/src/ruisapp/glue/windows/glue.cxx index 9cedd602..b8f12e48 100644 --- a/src/ruisapp/glue/windows/glue.cxx +++ b/src/ruisapp/glue/windows/glue.cxx @@ -35,6 +35,11 @@ along with this program. If not, see . # include # include # include +#elif defined(RUISAPP_RENDER_DIRECTX) +# include +# include +# include +# include #elif defined(RUISAPP_RENDER_OPENGL) # include # include diff --git a/src/ruisapp/glue/windows/window.cxx b/src/ruisapp/glue/windows/window.cxx index 5f52e9a7..e270f2e0 100644 --- a/src/ruisapp/glue/windows/window.cxx +++ b/src/ruisapp/glue/windows/window.cxx @@ -323,7 +323,7 @@ native_window::opengl_context_wrapper::~opengl_context_wrapper() } #endif -#ifdef RUISAPP_RENDER_VULKAN +#if defined(RUISAPP_RENDER_VULKAN) || defined(RUISAPP_RENDER_DIRECTX) native_window::native_window( utki::shared_ref display, const ruisapp::window_parameters& window_params @@ -336,9 +336,15 @@ native_window::native_window( ), device_context(this->window) { +#ifdef RUISAPP_RENDER_VULKAN // No rendering context is created here: the Vulkan renderer creates a // VkSurfaceKHR from the window handle (see application.cxx) and presentation // is handled by the Vulkan render context (vkQueuePresentKHR). +#else + // No rendering context is created here: the render context creates the + // DXGI swapchain from the window handle (see application.cxx) and + // presentation is handled by the render context (IDXGISwapChain::Present). +#endif } #else native_window::native_window( @@ -403,9 +409,9 @@ native_window::native_window( void native_window::bind_rendering_context() { -#ifdef RUISAPP_RENDER_VULKAN - // For Vulkan, there's no concept of binding a rendering context. - // The Vulkan context is thread-safe and doesn't need to be made current. +#if defined(RUISAPP_RENDER_VULKAN) || defined(RUISAPP_RENDER_DIRECTX) + // For Vulkan and DirectX 12 there's no concept of binding a rendering context. + // The render contexts don't need to be made current. #elif defined(RUISAPP_RENDER_OPENGL) auto res = wglMakeCurrent( this->device_context.context, // @@ -431,9 +437,9 @@ void native_window::bind_rendering_context() void native_window::swap_frame_buffers() { -#ifdef RUISAPP_RENDER_VULKAN - // Not used by the Vulkan renderer: presentation is handled by the Vulkan - // render context itself (vkQueuePresentKHR). +#if defined(RUISAPP_RENDER_VULKAN) || defined(RUISAPP_RENDER_DIRECTX) + // Not used by the Vulkan and DirectX 12 renderers: presentation is handled + // by the render context itself (vkQueuePresentKHR / IDXGISwapChain::Present). #elif defined(RUISAPP_RENDER_OPENGL) SwapBuffers(this->device_context.context); #elif defined(RUISAPP_RENDER_OPENGLES) diff --git a/src/ruisapp/glue/windows/window.hxx b/src/ruisapp/glue/windows/window.hxx index e49f7d4c..2c499f7d 100644 --- a/src/ruisapp/glue/windows/window.hxx +++ b/src/ruisapp/glue/windows/window.hxx @@ -121,9 +121,9 @@ public: return this->window.handle; } -#ifdef RUISAPP_RENDER_VULKAN - // The Vulkan renderer does not create any GL context, so the constructor - // does not take a GL version or a shared GL context window. +#if defined(RUISAPP_RENDER_VULKAN) || defined(RUISAPP_RENDER_DIRECTX) + // The Vulkan and DirectX 12 renderers do not create any GL context, so the + // constructor does not take a GL version or a shared GL context window. native_window(utki::shared_ref display, const ruisapp::window_parameters& window_params); #else native_window( From cb79f5727afd41991aa847aa1e15e77a748fd799 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 27 Nov 2026 07:25:25 +0200 Subject: [PATCH 2/3] fix directx variant: CMake-only, gate on renderer target, format - Remove the directx variant from the makefile: like vulkan, the directx variant is built by CMake only. The makefile build (used by the msys2 pacman package) does not have ruis-render-directx available. - Enable the CMake directx variant only when the ruis-render-directx target is available (i.e. inside the cppfw monorepo), instead of keying off IS_VCPKG_PORT_BUILD, which does not cover standalone non-monorepo CMake builds. - clang-format the windows glue sources. --- build/cmake/CMakeLists.txt | 10 +++++----- src/makefile | 13 ++----------- src/ruisapp/glue/windows/window.cxx | 6 +++--- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 784668f8..0cad8e09 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -43,11 +43,11 @@ elseif(WIN32) set(ruisapp_build_opengl_variant True) set(ruisapp_build_opengles_variant True) set(ruisapp_build_vulkan_variant True) # VK_KHR_win32_surface - # DXGI swapchain; disabled for the vcpkg port build because the - # ruis-render-directx vcpkg port does not exist yet - set(ruisapp_build_directx_variant True) - if(IS_VCPKG_PORT_BUILD) - set(ruisapp_build_directx_variant False) + # the DirectX 12 variant requires the ruis-render-directx renderer, + # which has no vcpkg port yet and is only available as a target when + # building inside the cppfw monorepo, where it is added before ruisapp + if(TARGET ruis-render-directx) + set(ruisapp_build_directx_variant True) endif() set(ruisapp_opengl_install_include_directory ../../src/${PROJECT_NAME}) diff --git a/src/makefile b/src/makefile index 9848d268..5b385302 100644 --- a/src/makefile +++ b/src/makefile @@ -86,14 +86,8 @@ define ruisapp_rules else ifeq ($(os), windows) this_ldlibs += -l gdi32 - ifeq ($1,directx) - this_ldlibs += -l d3d12 - this_ldlibs += -l dxgi - this_ldlibs += -l d3dcompiler - else - this_ldlibs += -l opengl32 - this_ldlibs += -l glew32 - endif + this_ldlibs += -l opengl32 + this_ldlibs += -l glew32 else ifeq ($(os), macosx) this_ldlibs += -l GLEW @@ -159,9 +153,6 @@ ifeq ($(os), linux) else $(eval $(call ruisapp_rules,opengl,)) - ifeq ($(os), windows) - $(eval $(call ruisapp_rules,directx,)) - endif endif # clear variable diff --git a/src/ruisapp/glue/windows/window.cxx b/src/ruisapp/glue/windows/window.cxx index e270f2e0..19c9fc05 100644 --- a/src/ruisapp/glue/windows/window.cxx +++ b/src/ruisapp/glue/windows/window.cxx @@ -336,15 +336,15 @@ native_window::native_window( ), device_context(this->window) { -#ifdef RUISAPP_RENDER_VULKAN +# ifdef RUISAPP_RENDER_VULKAN // No rendering context is created here: the Vulkan renderer creates a // VkSurfaceKHR from the window handle (see application.cxx) and presentation // is handled by the Vulkan render context (vkQueuePresentKHR). -#else +# else // No rendering context is created here: the render context creates the // DXGI swapchain from the window handle (see application.cxx) and // presentation is handled by the render context (IDXGISwapChain::Present). -#endif +# endif } #else native_window::native_window( From 263564962225aab9f03be859f37097f234f084f4 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Fri, 27 Nov 2026 21:28:44 +0200 Subject: [PATCH 3/3] configure the process crash behavior for easier development no modal error dialogs; the CRT error and assertion messages go to the standard error stream instead of message boxes --- src/ruisapp/glue/windows/glue.cxx | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/ruisapp/glue/windows/glue.cxx b/src/ruisapp/glue/windows/glue.cxx index b8f12e48..40bbbbdf 100644 --- a/src/ruisapp/glue/windows/glue.cxx +++ b/src/ruisapp/glue/windows/glue.cxx @@ -19,6 +19,7 @@ along with this program. If not, see . /* ================ LICENSE END ================ */ +#include #include #include @@ -27,6 +28,10 @@ along with this program. If not, see . #include #include +#if CFG_COMPILER == CFG_COMPILER_MSVC +# include +#endif + #ifdef RUISAPP_RENDER_VULKAN # include # include @@ -63,11 +68,50 @@ along with this program. If not, see . using namespace ruisapp; namespace { + +// configure the process crash behavior for easier development: no modal error +// dialogs, all runtime messages go to the standard error stream +void configure_crash_behavior() +{ + // disable the windows crash and critical error dialogs + SetErrorMode( + SEM_FAILCRITICALERRORS | // + SEM_NOGPFAULTERRORBOX | // + SEM_NOOPENFILEERRORBOX + ); + + // send the CRT error messages to the standard error stream instead of message boxes +#if CFG_COMPILER == CFG_COMPILER_MSVC || CFG_COMPILER == CFG_COMPILER_GCC + _set_error_mode(_OUT_TO_STDERR); +#endif + + // MSVC-specific crash behavior +#if CFG_COMPILER == CFG_COMPILER_MSVC + // make abort() terminate without the CRT or the WER dialogs + _set_abort_behavior( + 0, // + _WRITE_ABORT_MSG | _CALL_REPORTFAULT + ); + + // send the CRT assertion and error reports to the standard error stream + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); + + _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); + + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); +#endif +} + void winmain( int argc, // const char** argv ) { + configure_crash_behavior(); + // Make the process per-monitor DPI aware (v2) as early as possible, before any windows // are created, so that the effective screen scale factor can be queried per monitor. SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);