Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions build/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

# ====================
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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()
24 changes: 22 additions & 2 deletions src/ruisapp/glue/windows/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<native_window>(
this->display,
this->gl_version,
Expand All @@ -114,13 +114,22 @@ 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
// Vulkan has no "current context" — a null surface is sufficient for the factory (resource creation)
return utki::make_shared<ruis::render::vulkan::factory>(
utki::make_shared<ruis::render::vulkan::surface>(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<ruis::render::directx::factory>(
utki::make_shared<const ruis::render::directx::surface>(this->device.get(), nullptr)
);
#else
utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n');
# ifdef RUISAPP_RENDER_OPENGL
Expand Down Expand Up @@ -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<native_window>(this->display, window_params);
#else
auto ruis_native_window = utki::make_shared<native_window>(
Expand Down Expand Up @@ -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::render::directx::context>(
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<const ruis::render::directx::surface>(
this->device.get(), //
ruis_native_window.get().get_hwnd()
),
this->render_factory
),
#elif defined(RUISAPP_RENDER_OPENGL)
utki::make_shared<ruis::render::opengl::context>(ruis_native_window, this->render_factory),
#elif defined(RUISAPP_RENDER_OPENGLES)
Expand Down
20 changes: 19 additions & 1 deletion src/ruisapp/glue/windows/application.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include <ruis/render/vulkan/surface.hpp>
# include <vulkan/vulkan.h>
# include <vulkan/vulkan_win32.h>
#elif defined(RUISAPP_RENDER_DIRECTX)
# include <ruis/render/directx/device.hpp>
# include <ruis/render/directx/surface.hpp>
#endif

#include "display.hxx"
Expand Down Expand Up @@ -91,22 +94,37 @@ const utki::shared_ref<ruis::render::vulkan::device> 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<ruis::render::directx::device> make_directx_device()
{
return utki::make_shared<ruis::render::directx::device>();
}
#endif

class application_glue : public utki::destructable
{
const utki::version_duplet gl_version;

utki::shared_ref<display_wrapper> display = utki::make_shared<display_wrapper>();

#ifndef RUISAPP_RENDER_VULKAN
#if !defined(RUISAPP_RENDER_VULKAN) && !defined(RUISAPP_RENDER_DIRECTX)
const utki::shared_ref<native_window> 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<ruis::render::vulkan::device> 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<ruis::render::directx::device> device;
#endif
// concrete factory type, #ifdef by renderer
#ifdef RUISAPP_RENDER_VULKAN
const utki::shared_ref<const ruis::render::vulkan::factory> render_factory;
#elif defined(RUISAPP_RENDER_DIRECTX)
const utki::shared_ref<const ruis::render::directx::factory> render_factory;
#elif defined(RUISAPP_RENDER_OPENGL)
const utki::shared_ref<const ruis::render::opengl::factory> render_factory;
#elif defined(RUISAPP_RENDER_OPENGLES)
Expand Down
49 changes: 49 additions & 0 deletions src/ruisapp/glue/windows/glue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* ================ LICENSE END ================ */

#include <cstdlib>
#include <ratio>

#include <fsif/native_file.hpp>
Expand All @@ -27,6 +28,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <utki/destructable.hpp>
#include <utki/windows.hpp>

#if CFG_COMPILER == CFG_COMPILER_MSVC
# include <crtdbg.h>
#endif

#ifdef RUISAPP_RENDER_VULKAN
# include <vulkan/vulkan.h>
# include <vulkan/vulkan_win32.h>
Expand All @@ -35,6 +40,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include <ruis/render/vulkan/factory.hpp>
# include <ruis/render/vulkan/device.hpp>
# include <ruis/render/vulkan/surface.hpp>
#elif defined(RUISAPP_RENDER_DIRECTX)
# include <ruis/render/directx/context.hpp>
# include <ruis/render/directx/factory.hpp>
# include <ruis/render/directx/device.hpp>
# include <ruis/render/directx/surface.hpp>
#elif defined(RUISAPP_RENDER_OPENGL)
# include <ruis/render/opengl/context.hpp>
# include <ruis/render/opengl/factory.hpp>
Expand All @@ -58,11 +68,50 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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);
Expand Down
20 changes: 13 additions & 7 deletions src/ruisapp/glue/windows/window.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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_wrapper> display,
const ruisapp::window_parameters& window_params
Expand All @@ -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(
Expand Down Expand Up @@ -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, //
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/ruisapp/glue/windows/window.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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_wrapper> display, const ruisapp::window_parameters& window_params);
#else
native_window(
Expand Down
Loading