Skip to content

Commit 2de02d4

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

11 files changed

+252
-185
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

+27-26
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,7 @@ static void D3D11_DestroyDevice(FNA3D_Device *device)
10671072
swapchainData = renderer->swapchainDatas[i];
10681073
ID3D11RenderTargetView_Release(swapchainData->swapchainRTView);
10691074
IDXGISwapChain_Release(swapchainData->swapchain);
1070-
SDL_SetWindowData(
1071-
(SDL_Window*) swapchainData->windowHandle,
1072-
WINDOW_SWAPCHAIN_DATA,
1073-
NULL
1074-
);
1075+
SDL_ClearProperty(SDL_GetWindowProperties(swapchainData->windowHandle), WINDOW_SWAPCHAIN_DATA);
10751076
SDL_free(renderer->swapchainDatas[i]);
10761077
}
10771078
SDL_free(renderer->swapchainDatas);
@@ -1532,9 +1533,10 @@ static void D3D11_SwapBuffers(
15321533
}
15331534
}
15341535

1535-
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
1536-
(SDL_Window*) overrideWindowHandle,
1537-
WINDOW_SWAPCHAIN_DATA
1536+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
1537+
SDL_GetWindowProperties(overrideWindowHandle),
1538+
WINDOW_SWAPCHAIN_DATA,
1539+
NULL
15381540
);
15391541
if (swapchainData == NULL)
15401542
{
@@ -1544,9 +1546,10 @@ static void D3D11_SwapBuffers(
15441546
(SDL_Window*) overrideWindowHandle,
15451547
NULL
15461548
);
1547-
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
1548-
(SDL_Window*) overrideWindowHandle,
1549-
WINDOW_SWAPCHAIN_DATA
1549+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
1550+
SDL_GetWindowProperties(overrideWindowHandle),
1551+
WINDOW_SWAPCHAIN_DATA,
1552+
NULL
15501553
);
15511554
D3D11_INTERNAL_UpdateSwapchainRT(
15521555
renderer,
@@ -2545,10 +2548,7 @@ static void D3D11_INTERNAL_CreateSwapChain(
25452548
#ifdef FNA3D_DXVK_NATIVE
25462549
dxgiHandle = (HWND) windowHandle;
25472550
#else
2548-
SDL_SysWMinfo info;
2549-
SDL_VERSION(&info.version);
2550-
SDL_GetWindowWMInfo((SDL_Window*) windowHandle, &info);
2551-
dxgiHandle = info.info.win.window;
2551+
dxgiHandle = (HWND) SDL_GetProperty(SDL_GetWindowProperties(windowHandle), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
25522552
#endif /* FNA3D_DXVK_NATIVE */
25532553

25542554
/* Initialize swapchain buffer descriptor */
@@ -2676,7 +2676,7 @@ static void D3D11_INTERNAL_CreateSwapChain(
26762676
swapchainData->windowHandle = windowHandle;
26772677
swapchainData->swapchainRTView = NULL;
26782678
swapchainData->format = backBufferFormat;
2679-
SDL_SetWindowData((SDL_Window*) windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData);
2679+
SDL_SetProperty(SDL_GetWindowProperties(windowHandle), WINDOW_SWAPCHAIN_DATA, swapchainData);
26802680
if (growSwapchains)
26812681
{
26822682
if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity)
@@ -2771,9 +2771,10 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27712771
/* Create or update the swapchain */
27722772
if (parameters->deviceWindowHandle != NULL)
27732773
{
2774-
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
2775-
(SDL_Window*) parameters->deviceWindowHandle,
2776-
WINDOW_SWAPCHAIN_DATA
2774+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
2775+
SDL_GetWindowProperties(parameters->deviceWindowHandle),
2776+
WINDOW_SWAPCHAIN_DATA,
2777+
NULL
27772778
);
27782779
if (swapchainData == NULL)
27792780
{
@@ -2783,9 +2784,10 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27832784
parameters->deviceWindowHandle,
27842785
NULL
27852786
);
2786-
swapchainData = (D3D11SwapchainData*) SDL_GetWindowData(
2787-
(SDL_Window*) parameters->deviceWindowHandle,
2788-
WINDOW_SWAPCHAIN_DATA
2787+
swapchainData = (D3D11SwapchainData*) SDL_GetProperty(
2788+
SDL_GetWindowProperties(parameters->deviceWindowHandle),
2789+
WINDOW_SWAPCHAIN_DATA,
2790+
NULL
27892791
);
27902792
}
27912793
else
@@ -2796,7 +2798,7 @@ static void D3D11_INTERNAL_CreateBackbuffer(
27962798
/* Surface format changed, recreate entirely */
27972799
IDXGISwapChain_Release(swapchainData->swapchain);
27982800

2799-
/*
2801+
/*
28002802
* DXGI will crash in some cases if we don't flush deferred swapchain destruction:
28012803
*
28022804
* DXGI ERROR: IDXGIFactory::CreateSwapChain: Only one flip model swap chain can be
@@ -5212,7 +5214,6 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
52125214
}
52135215

52145216
/* No window flags required */
5215-
SDL_SetHint(SDL_HINT_VIDEO_EXTERNAL_CONTEXT, "1");
52165217
#ifdef FNA3D_DXVK_NATIVE
52175218
/* ... unless this is DXVK */
52185219
*flags = SDL_WINDOW_VULKAN;
@@ -5544,7 +5545,7 @@ static FNA3D_Device* D3D11_CreateDevice(
55445545
{
55455546
res = D3D11CreateDeviceFunc(
55465547
(driverType == D3D_DRIVER_TYPE_WARP) ? NULL : (IDXGIAdapter*) renderer->adapter,
5547-
driverType,
5548+
driverType,
55485549
NULL,
55495550
flags,
55505551
&levels[i],

0 commit comments

Comments
 (0)