Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building with SDL3 #200

Merged
merged 1 commit into from
Mar 20, 2024
Merged
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
66 changes: 48 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ project(FNA3D C)
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(TRACING_SUPPORT "Build with tracing enabled" OFF)
option(BUILD_DXVK_NATIVE "Enable support for dxvk-native" OFF)
option(BUILD_SDL3 "Build against SDL 3.0" OFF)

# Version
SET(LIB_MAJOR_VERSION "0")
Expand Down Expand Up @@ -59,6 +60,9 @@ add_definitions(
if(TRACING_SUPPORT)
add_definitions(-DFNA3D_TRACING)
endif()
if(BUILD_SDL3)
add_definitions(-DUSE_SDL3)
endif()

if(WIN32 OR BUILD_DXVK_NATIVE)
add_definitions(
Expand Down Expand Up @@ -170,27 +174,53 @@ set_target_properties(FNA3D PROPERTIES OUTPUT_NAME "FNA3D"
# Internal Dependencies
target_link_libraries(FNA3D PRIVATE mojoshader ${LOBJC})

# SDL2 Dependency
if (DEFINED SDL2_INCLUDE_DIRS AND DEFINED SDL2_LIBRARIES)
message(STATUS "using pre-defined SDL2 variables SDL2_INCLUDE_DIRS and SDL2_LIBRARIES")
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
else()
# Only try to autodetect if both SDL2 variables aren't explicitly set
find_package(SDL2 CONFIG)
if (TARGET SDL2::SDL2)
message(STATUS "using TARGET SDL2::SDL2")
target_link_libraries(FNA3D PUBLIC SDL2::SDL2)
target_link_libraries(mojoshader PUBLIC SDL2::SDL2)
elseif (TARGET SDL2)
message(STATUS "using TARGET SDL2")
target_link_libraries(FNA3D PUBLIC SDL2)
target_link_libraries(mojoshader PUBLIC SDL2)
# SDL Dependency
if (BUILD_SDL3)
if (DEFINED SDL3_INCLUDE_DIRS AND DEFINED SDL3_LIBRARIES)
message(STATUS "using pre-defined SDL3 variables SDL3_INCLUDE_DIRS and SDL3_LIBRARIES")
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
target_link_libraries(FNA3D PUBLIC ${SDL3_LIBRARIES})
else()
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
# Only try to autodetect if both SDL3 variables aren't explicitly set
find_package(SDL3 CONFIG)
if (TARGET SDL3::SDL3)
message(STATUS "using TARGET SDL3::SDL3")
target_link_libraries(FNA3D PUBLIC SDL3::SDL3)
target_link_libraries(mojoshader PUBLIC SDL3::SDL3)
elseif (TARGET SDL3)
message(STATUS "using TARGET SDL3")
target_link_libraries(FNA3D PUBLIC SDL3)
target_link_libraries(mojoshader PUBLIC SDL3)
else()
message(STATUS "no TARGET SDL3::SDL3, or SDL3, using variables")
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
target_link_libraries(FNA3D PUBLIC ${SDL3_LIBRARIES})
endif()
endif()
else()
if (DEFINED SDL2_INCLUDE_DIRS AND DEFINED SDL2_LIBRARIES)
message(STATUS "using pre-defined SDL2 variables SDL2_INCLUDE_DIRS and SDL2_LIBRARIES")
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
else()
# Only try to autodetect if both SDL2 variables aren't explicitly set
find_package(SDL2 CONFIG)
if (TARGET SDL2::SDL2)
message(STATUS "using TARGET SDL2::SDL2")
target_link_libraries(FNA3D PUBLIC SDL2::SDL2)
target_link_libraries(mojoshader PUBLIC SDL2::SDL2)
elseif (TARGET SDL2)
message(STATUS "using TARGET SDL2")
target_link_libraries(FNA3D PUBLIC SDL2)
target_link_libraries(mojoshader PUBLIC SDL2)
else()
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
endif()
endif()
endif()
2 changes: 1 addition & 1 deletion MojoShader
4 changes: 4 additions & 0 deletions src/FNA3D.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
#include "FNA3D_Driver.h"
#include "FNA3D_Tracing.h"

#ifdef USE_SDL3
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif

#if !SDL_VERSION_ATLEAST(2, 26, 0)
#error "SDL version older than 2.26.0"
Expand Down
9 changes: 7 additions & 2 deletions src/FNA3D_CommandBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@

#include "FNA3D_CommandBuffer.h"

#ifdef USE_SDL3
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#define SDL_Mutex SDL_mutex
#endif

#define STARTING_TRANSFER_BUFFER_SIZE 8000000 /* 8MB */
#define FAST_TRANSFER_SIZE 64000000 /* 64MB */
Expand Down Expand Up @@ -93,8 +98,8 @@ struct FNA3D_CommandBufferManager

FNA3D_TransferBufferPool transferBufferPool;

SDL_mutex *commandLock;
SDL_mutex *transferLock;
SDL_Mutex *commandLock;
SDL_Mutex *transferLock;
};

static FNA3D_CommandBufferContainer* FNA3D_INTERNAL_CreateCommandBufferContainer(
Expand Down
64 changes: 61 additions & 3 deletions src/FNA3D_Driver_D3D11.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@
#include "FNA3D_Driver_D3D11.h"
#include "FNA3D_Driver_D3D11_shaders.h"

#ifdef USE_SDL3
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#ifndef FNA3D_DXVK_NATIVE
#include <SDL_syswm.h>
#endif /* !FNA3D_DXVK_NATIVE */
#define SDL_Mutex SDL_mutex
#endif

/* D3D11 Libraries */

Expand Down Expand Up @@ -213,7 +218,7 @@ typedef struct D3D11Renderer /* Cast FNA3D_Renderer* to this! */
IDXGIAdapter1 *adapter;
ID3DUserDefinedAnnotation *annotation;
BOOL supportsTearing;
SDL_mutex *ctxLock;
SDL_Mutex *ctxLock;
SDL_iconv_t iconv;

/* Window surfaces */
Expand Down Expand Up @@ -1067,11 +1072,18 @@ static void D3D11_DestroyDevice(FNA3D_Device *device)
swapchainData = renderer->swapchainDatas[i];
ID3D11RenderTargetView_Release(swapchainData->swapchainRTView);
IDXGISwapChain_Release(swapchainData->swapchain);
#if SDL_MAJOR_VERSION >= 3
SDL_ClearProperty(
SDL_GetWindowProperties(swapchainData->windowHandle),
WINDOW_SWAPCHAIN_DATA
);
#else
SDL_SetWindowData(
(SDL_Window*) swapchainData->windowHandle,
WINDOW_SWAPCHAIN_DATA,
NULL
);
#endif
SDL_free(renderer->swapchainDatas[i]);
}
SDL_free(renderer->swapchainDatas);
Expand Down Expand Up @@ -1532,10 +1544,18 @@ static void D3D11_SwapBuffers(
}
}

#if SDL_MAJOR_VERSION >= 3
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
SDL_GetWindowProperties(overrideWindowHandle),
WINDOW_SWAPCHAIN_DATA,
NULL
);
#else
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
(SDL_Window*) overrideWindowHandle,
WINDOW_SWAPCHAIN_DATA
);
#endif
if (swapchainData == NULL)
{
D3D11_INTERNAL_CreateSwapChain(
Expand All @@ -1544,10 +1564,18 @@ static void D3D11_SwapBuffers(
(SDL_Window*) overrideWindowHandle,
NULL
);
#if SDL_MAJOR_VERSION >= 3
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
SDL_GetWindowProperties(overrideWindowHandle),
WINDOW_SWAPCHAIN_DATA,
NULL
);
#else
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
(SDL_Window*) overrideWindowHandle,
WINDOW_SWAPCHAIN_DATA
);
#endif
D3D11_INTERNAL_UpdateSwapchainRT(
renderer,
swapchainData,
Expand Down Expand Up @@ -2544,11 +2572,19 @@ static void D3D11_INTERNAL_CreateSwapChain(

#ifdef FNA3D_DXVK_NATIVE
dxgiHandle = (HWND) windowHandle;
#else
#if SDL_MAJOR_VERSION >= 3
dxgiHandle = (HWND) SDL_GetProperty(
SDL_GetWindowProperties(windowHandle),
SDL_PROP_WINDOW_WIN32_HWND_POINTER,
NULL
);
#else
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo((SDL_Window*) windowHandle, &info);
dxgiHandle = info.info.win.window;
#endif
#endif /* FNA3D_DXVK_NATIVE */

/* Initialize swapchain buffer descriptor */
Expand Down Expand Up @@ -2676,7 +2712,11 @@ static void D3D11_INTERNAL_CreateSwapChain(
swapchainData->windowHandle = windowHandle;
swapchainData->swapchainRTView = NULL;
swapchainData->format = backBufferFormat;
#if SDL_MAJOR_VERSION >= 3
SDL_SetProperty(SDL_GetWindowProperties(windowHandle), WINDOW_SWAPCHAIN_DATA, swapchainData);
#else
SDL_SetWindowData((SDL_Window*) windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData);
#endif
if (growSwapchains)
{
if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity)
Expand Down Expand Up @@ -2771,10 +2811,18 @@ static void D3D11_INTERNAL_CreateBackbuffer(
/* Create or update the swapchain */
if (parameters->deviceWindowHandle != NULL)
{
#if SDL_MAJOR_VERSION >= 3
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
SDL_GetWindowProperties(parameters->deviceWindowHandle),
WINDOW_SWAPCHAIN_DATA,
NULL
);
#else
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
(SDL_Window*) parameters->deviceWindowHandle,
WINDOW_SWAPCHAIN_DATA
);
#endif
if (swapchainData == NULL)
{
D3D11_INTERNAL_CreateSwapChain(
Expand All @@ -2783,10 +2831,18 @@ static void D3D11_INTERNAL_CreateBackbuffer(
parameters->deviceWindowHandle,
NULL
);
#if SDL_MAJOR_VERSION >= 3
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
SDL_GetWindowProperties(parameters->deviceWindowHandle),
WINDOW_SWAPCHAIN_DATA,
NULL
);
#else
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
(SDL_Window*) parameters->deviceWindowHandle,
WINDOW_SWAPCHAIN_DATA
);
#endif
}
else
{
Expand All @@ -2796,7 +2852,7 @@ static void D3D11_INTERNAL_CreateBackbuffer(
/* Surface format changed, recreate entirely */
IDXGISwapChain_Release(swapchainData->swapchain);

/*
/*
* DXGI will crash in some cases if we don't flush deferred swapchain destruction:
*
* DXGI ERROR: IDXGIFactory::CreateSwapChain: Only one flip model swap chain can be
Expand Down Expand Up @@ -5212,7 +5268,9 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
}

/* No window flags required */
#if SDL_MAJOR_VERSION < 3
SDL_SetHint(SDL_HINT_VIDEO_EXTERNAL_CONTEXT, "1");
#endif
#ifdef FNA3D_DXVK_NATIVE
/* ... unless this is DXVK */
*flags = SDL_WINDOW_VULKAN;
Expand Down Expand Up @@ -5544,7 +5602,7 @@ static FNA3D_Device* D3D11_CreateDevice(
{
res = D3D11CreateDeviceFunc(
(driverType == D3D_DRIVER_TYPE_WARP) ? NULL : (IDXGIAdapter*) renderer->adapter,
driverType,
driverType,
NULL,
flags,
&levels[i],
Expand Down
Loading
Loading