diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index 3743a8f..0cad8e0 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 + # 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}) 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/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 2c8c8a4..5da1206 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 e4d6596..0daa255 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 9cedd60..40bbbbd 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 @@ -35,6 +40,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 @@ -58,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); diff --git a/src/ruisapp/glue/windows/window.cxx b/src/ruisapp/glue/windows/window.cxx index 5f52e9a..19c9fc0 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 e49f7d4..2c499f7 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(