Skip to content

Commit 35c40f4

Browse files
Add support for building with SDL3
Co-authored-by: Ethan Lee <flibitijibibo@gmail.com>
1 parent 2b04410 commit 35c40f4

11 files changed

+362
-125
lines changed

CMakeLists.txt

+48-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ project(FNA3D C)
77
option(BUILD_SHARED_LIBS "Build shared library" ON)
88
option(TRACING_SUPPORT "Build with tracing enabled" OFF)
99
option(BUILD_DXVK_NATIVE "Enable support for dxvk-native" OFF)
10+
option(BUILD_SDL3 "Build against SDL 3.0" OFF)
1011

1112
# Version
1213
SET(LIB_MAJOR_VERSION "0")
@@ -59,6 +60,9 @@ add_definitions(
5960
if(TRACING_SUPPORT)
6061
add_definitions(-DFNA3D_TRACING)
6162
endif()
63+
if(BUILD_SDL3)
64+
add_definitions(-DUSE_SDL3)
65+
endif()
6266

6367
if(WIN32 OR BUILD_DXVK_NATIVE)
6468
add_definitions(
@@ -170,27 +174,53 @@ set_target_properties(FNA3D PROPERTIES OUTPUT_NAME "FNA3D"
170174
# Internal Dependencies
171175
target_link_libraries(FNA3D PRIVATE mojoshader ${LOBJC})
172176

173-
# SDL2 Dependency
174-
if (DEFINED SDL2_INCLUDE_DIRS AND DEFINED SDL2_LIBRARIES)
175-
message(STATUS "using pre-defined SDL2 variables SDL2_INCLUDE_DIRS and SDL2_LIBRARIES")
176-
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
177-
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
178-
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
179-
else()
180-
# Only try to autodetect if both SDL2 variables aren't explicitly set
181-
find_package(SDL2 CONFIG)
182-
if (TARGET SDL2::SDL2)
183-
message(STATUS "using TARGET SDL2::SDL2")
184-
target_link_libraries(FNA3D PUBLIC SDL2::SDL2)
185-
target_link_libraries(mojoshader PUBLIC SDL2::SDL2)
186-
elseif (TARGET SDL2)
187-
message(STATUS "using TARGET SDL2")
188-
target_link_libraries(FNA3D PUBLIC SDL2)
189-
target_link_libraries(mojoshader PUBLIC SDL2)
177+
# SDL Dependency
178+
if (BUILD_SDL3)
179+
if (DEFINED SDL3_INCLUDE_DIRS AND DEFINED SDL3_LIBRARIES)
180+
message(STATUS "using pre-defined SDL3 variables SDL3_INCLUDE_DIRS and SDL3_LIBRARIES")
181+
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
182+
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
183+
target_link_libraries(FNA3D PUBLIC ${SDL3_LIBRARIES})
190184
else()
191-
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
185+
# Only try to autodetect if both SDL3 variables aren't explicitly set
186+
find_package(SDL3 CONFIG)
187+
if (TARGET SDL3::SDL3)
188+
message(STATUS "using TARGET SDL3::SDL3")
189+
target_link_libraries(FNA3D PUBLIC SDL3::SDL3)
190+
target_link_libraries(mojoshader PUBLIC SDL3::SDL3)
191+
elseif (TARGET SDL3)
192+
message(STATUS "using TARGET SDL3")
193+
target_link_libraries(FNA3D PUBLIC SDL3)
194+
target_link_libraries(mojoshader PUBLIC SDL3)
195+
else()
196+
message(STATUS "no TARGET SDL3::SDL3, or SDL3, using variables")
197+
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
198+
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL3_INCLUDE_DIRS}>")
199+
target_link_libraries(FNA3D PUBLIC ${SDL3_LIBRARIES})
200+
endif()
201+
endif()
202+
else()
203+
if (DEFINED SDL2_INCLUDE_DIRS AND DEFINED SDL2_LIBRARIES)
204+
message(STATUS "using pre-defined SDL2 variables SDL2_INCLUDE_DIRS and SDL2_LIBRARIES")
192205
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
193206
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
194207
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
208+
else()
209+
# Only try to autodetect if both SDL2 variables aren't explicitly set
210+
find_package(SDL2 CONFIG)
211+
if (TARGET SDL2::SDL2)
212+
message(STATUS "using TARGET SDL2::SDL2")
213+
target_link_libraries(FNA3D PUBLIC SDL2::SDL2)
214+
target_link_libraries(mojoshader PUBLIC SDL2::SDL2)
215+
elseif (TARGET SDL2)
216+
message(STATUS "using TARGET SDL2")
217+
target_link_libraries(FNA3D PUBLIC SDL2)
218+
target_link_libraries(mojoshader PUBLIC SDL2)
219+
else()
220+
message(STATUS "no TARGET SDL2::SDL2, or SDL2, using variables")
221+
target_include_directories(FNA3D PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
222+
target_include_directories(mojoshader PUBLIC "$<BUILD_INTERFACE:${SDL2_INCLUDE_DIRS}>")
223+
target_link_libraries(FNA3D PUBLIC ${SDL2_LIBRARIES})
224+
endif()
195225
endif()
196226
endif()

MojoShader

src/FNA3D.c

+4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
#include "FNA3D_Driver.h"
2828
#include "FNA3D_Tracing.h"
2929

30+
#ifdef USE_SDL3
31+
#include <SDL3/SDL.h>
32+
#else
3033
#include <SDL.h>
34+
#endif
3135

3236
#if !SDL_VERSION_ATLEAST(2, 26, 0)
3337
#error "SDL version older than 2.26.0"

src/FNA3D_CommandBuffer.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626

2727
#include "FNA3D_CommandBuffer.h"
2828

29+
#ifdef USE_SDL3
30+
#include <SDL3/SDL.h>
31+
#else
2932
#include <SDL.h>
33+
#define SDL_Mutex SDL_mutex
34+
#endif
3035

3136
#define STARTING_TRANSFER_BUFFER_SIZE 8000000 /* 8MB */
3237
#define FAST_TRANSFER_SIZE 64000000 /* 64MB */
@@ -93,8 +98,8 @@ struct FNA3D_CommandBufferManager
9398

9499
FNA3D_TransferBufferPool transferBufferPool;
95100

96-
SDL_mutex *commandLock;
97-
SDL_mutex *transferLock;
101+
SDL_Mutex *commandLock;
102+
SDL_Mutex *transferLock;
98103
};
99104

100105
static FNA3D_CommandBufferContainer* FNA3D_INTERNAL_CreateCommandBufferContainer(

src/FNA3D_Driver_D3D11.c

+61-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@
3131
#include "FNA3D_Driver_D3D11.h"
3232
#include "FNA3D_Driver_D3D11_shaders.h"
3333

34+
#ifdef USE_SDL3
35+
#include <SDL3/SDL.h>
36+
#else
3437
#include <SDL.h>
3538
#ifndef FNA3D_DXVK_NATIVE
3639
#include <SDL_syswm.h>
3740
#endif /* !FNA3D_DXVK_NATIVE */
41+
#define SDL_Mutex SDL_mutex
42+
#endif
3843

3944
/* D3D11 Libraries */
4045

@@ -213,7 +218,7 @@ typedef struct D3D11Renderer /* Cast FNA3D_Renderer* to this! */
213218
IDXGIAdapter1 *adapter;
214219
ID3DUserDefinedAnnotation *annotation;
215220
BOOL supportsTearing;
216-
SDL_mutex *ctxLock;
221+
SDL_Mutex *ctxLock;
217222
SDL_iconv_t iconv;
218223

219224
/* Window surfaces */
@@ -1067,11 +1072,18 @@ static void D3D11_DestroyDevice(FNA3D_Device *device)
10671072
swapchainData = renderer->swapchainDatas[i];
10681073
ID3D11RenderTargetView_Release(swapchainData->swapchainRTView);
10691074
IDXGISwapChain_Release(swapchainData->swapchain);
1075+
#if SDL_MAJOR_VERSION >= 3
1076+
SDL_ClearProperty(
1077+
SDL_GetWindowProperties(swapchainData->windowHandle),
1078+
WINDOW_SWAPCHAIN_DATA
1079+
);
1080+
#else
10701081
SDL_SetWindowData(
10711082
(SDL_Window*) swapchainData->windowHandle,
10721083
WINDOW_SWAPCHAIN_DATA,
10731084
NULL
10741085
);
1086+
#endif
10751087
SDL_free(renderer->swapchainDatas[i]);
10761088
}
10771089
SDL_free(renderer->swapchainDatas);
@@ -1532,10 +1544,18 @@ static void D3D11_SwapBuffers(
15321544
}
15331545
}
15341546

1547+
#if SDL_MAJOR_VERSION >= 3
1548+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
1549+
SDL_GetWindowProperties(overrideWindowHandle),
1550+
WINDOW_SWAPCHAIN_DATA,
1551+
NULL
1552+
);
1553+
#else
15351554
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
15361555
(SDL_Window*) overrideWindowHandle,
15371556
WINDOW_SWAPCHAIN_DATA
15381557
);
1558+
#endif
15391559
if (swapchainData == NULL)
15401560
{
15411561
D3D11_INTERNAL_CreateSwapChain(
@@ -1544,10 +1564,18 @@ static void D3D11_SwapBuffers(
15441564
(SDL_Window*) overrideWindowHandle,
15451565
NULL
15461566
);
1567+
#if SDL_MAJOR_VERSION >= 3
1568+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
1569+
SDL_GetWindowProperties(overrideWindowHandle),
1570+
WINDOW_SWAPCHAIN_DATA,
1571+
NULL
1572+
);
1573+
#else
15471574
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
15481575
(SDL_Window*) overrideWindowHandle,
15491576
WINDOW_SWAPCHAIN_DATA
15501577
);
1578+
#endif
15511579
D3D11_INTERNAL_UpdateSwapchainRT(
15521580
renderer,
15531581
swapchainData,
@@ -2544,11 +2572,19 @@ static void D3D11_INTERNAL_CreateSwapChain(
25442572

25452573
#ifdef FNA3D_DXVK_NATIVE
25462574
dxgiHandle = (HWND) windowHandle;
2575+
#else
2576+
#if SDL_MAJOR_VERSION >= 3
2577+
dxgiHandle = (HWND) SDL_GetProperty(
2578+
SDL_GetWindowProperties(windowHandle),
2579+
SDL_PROP_WINDOW_WIN32_HWND_POINTER,
2580+
NULL
2581+
);
25472582
#else
25482583
SDL_SysWMinfo info;
25492584
SDL_VERSION(&info.version);
25502585
SDL_GetWindowWMInfo((SDL_Window*) windowHandle, &info);
25512586
dxgiHandle = info.info.win.window;
2587+
#endif
25522588
#endif /* FNA3D_DXVK_NATIVE */
25532589

25542590
/* Initialize swapchain buffer descriptor */
@@ -2676,7 +2712,11 @@ static void D3D11_INTERNAL_CreateSwapChain(
26762712
swapchainData->windowHandle = windowHandle;
26772713
swapchainData->swapchainRTView = NULL;
26782714
swapchainData->format = backBufferFormat;
2715+
#if SDL_MAJOR_VERSION >= 3
2716+
SDL_SetProperty(SDL_GetWindowProperties(windowHandle), WINDOW_SWAPCHAIN_DATA, swapchainData);
2717+
#else
26792718
SDL_SetWindowData((SDL_Window*) windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData);
2719+
#endif
26802720
if (growSwapchains)
26812721
{
26822722
if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity)
@@ -2771,10 +2811,18 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27712811
/* Create or update the swapchain */
27722812
if (parameters->deviceWindowHandle != NULL)
27732813
{
2814+
#if SDL_MAJOR_VERSION >= 3
2815+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
2816+
SDL_GetWindowProperties(parameters->deviceWindowHandle),
2817+
WINDOW_SWAPCHAIN_DATA,
2818+
NULL
2819+
);
2820+
#else
27742821
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
27752822
(SDL_Window*) parameters->deviceWindowHandle,
27762823
WINDOW_SWAPCHAIN_DATA
27772824
);
2825+
#endif
27782826
if (swapchainData == NULL)
27792827
{
27802828
D3D11_INTERNAL_CreateSwapChain(
@@ -2783,10 +2831,18 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27832831
parameters->deviceWindowHandle,
27842832
NULL
27852833
);
2834+
#if SDL_MAJOR_VERSION >= 3
2835+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
2836+
SDL_GetWindowProperties(parameters->deviceWindowHandle),
2837+
WINDOW_SWAPCHAIN_DATA,
2838+
NULL
2839+
);
2840+
#else
27862841
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
27872842
(SDL_Window*) parameters->deviceWindowHandle,
27882843
WINDOW_SWAPCHAIN_DATA
27892844
);
2845+
#endif
27902846
}
27912847
else
27922848
{
@@ -2796,7 +2852,7 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27962852
/* Surface format changed, recreate entirely */
27972853
IDXGISwapChain_Release(swapchainData->swapchain);
27982854

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

52145270
/* No window flags required */
5271+
#if SDL_MAJOR_VERSION < 3
52155272
SDL_SetHint(SDL_HINT_VIDEO_EXTERNAL_CONTEXT, "1");
5273+
#endif
52165274
#ifdef FNA3D_DXVK_NATIVE
52175275
/* ... unless this is DXVK */
52185276
*flags = SDL_WINDOW_VULKAN;
@@ -5544,7 +5602,7 @@ static FNA3D_Device* D3D11_CreateDevice(
55445602
{
55455603
res = D3D11CreateDeviceFunc(
55465604
(driverType == D3D_DRIVER_TYPE_WARP) ? NULL : (IDXGIAdapter*) renderer->adapter,
5547-
driverType,
5605+
driverType,
55485606
NULL,
55495607
flags,
55505608
&levels[i],

0 commit comments

Comments
 (0)