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

SetSwapchainParameters and PrepareDriver return SDL_bool #61

Merged
merged 3 commits into from
Jun 27, 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
3 changes: 2 additions & 1 deletion include/SDL3/SDL_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -1940,10 +1940,11 @@ extern SDL_DECLSPEC void SDLCALL SDL_GpuUnclaimWindow(
* \param window an SDL_Window that has been claimed
* \param swapchainComposition the desired composition of the swapchain
* \param presentMode the desired present mode for the swapchain
* \returns SDL_TRUE if successful, SDL_FALSE on error
*
* \since This function is available since SDL 3.x.x
*/
extern SDL_DECLSPEC void SDLCALL SDL_GpuSetSwapchainParameters(
extern SDL_DECLSPEC SDL_bool SDLCALL SDL_GpuSetSwapchainParameters(
SDL_GpuDevice *device,
SDL_Window *window,
SDL_GpuSwapchainComposition swapchainComposition,
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GpuSupportsSwapchainComposition,(SDL_GpuDevice *a,
SDL_DYNAPI_PROC(SDL_bool,SDL_GpuSupportsPresentMode,(SDL_GpuDevice *a, SDL_Window *b, SDL_GpuPresentMode c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GpuClaimWindow,(SDL_GpuDevice *a, SDL_Window *b, SDL_GpuSwapchainComposition c, SDL_GpuPresentMode d),(a,b,c,d),return)
SDL_DYNAPI_PROC(void,SDL_GpuUnclaimWindow,(SDL_GpuDevice *a, SDL_Window *b),(a,b),)
SDL_DYNAPI_PROC(void,SDL_GpuSetSwapchainParameters,(SDL_GpuDevice *a, SDL_Window *b, SDL_GpuSwapchainComposition c, SDL_GpuPresentMode d),(a,b,c,d),)
SDL_DYNAPI_PROC(SDL_bool,SDL_GpuSetSwapchainParameters,(SDL_GpuDevice *a, SDL_Window *b, SDL_GpuSwapchainComposition c, SDL_GpuPresentMode d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_GpuTextureFormat,SDL_GpuGetSwapchainTextureFormat,(SDL_GpuDevice *a, SDL_Window *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_GpuCommandBuffer*,SDL_GpuAcquireCommandBuffer,(SDL_GpuDevice *a),(a),return)
SDL_DYNAPI_PROC(SDL_GpuTexture*,SDL_GpuAcquireSwapchainTexture,(SDL_GpuCommandBuffer *a, SDL_Window *b, Uint32 *c, Uint32 *d),(a,b,c,d),return)
Expand Down
4 changes: 2 additions & 2 deletions src/gpu/SDL_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,14 +1258,14 @@ void SDL_GpuUnclaimWindow(
window);
}

void SDL_GpuSetSwapchainParameters(
SDL_bool SDL_GpuSetSwapchainParameters(
SDL_GpuDevice *device,
SDL_Window *window,
SDL_GpuSwapchainComposition swapchainFormat,
SDL_GpuPresentMode presentMode)
{
NULL_ASSERT(device);
device->SetSwapchainParameters(
return device->SetSwapchainParameters(
device->driverData,
window,
swapchainFormat,
Expand Down
4 changes: 2 additions & 2 deletions src/gpu/SDL_gpu_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ struct SDL_GpuDevice
SDL_GpuRenderer *driverData,
SDL_Window *window);

void (*SetSwapchainParameters)(
SDL_bool (*SetSwapchainParameters)(
SDL_GpuRenderer *driverData,
SDL_Window *window,
SDL_GpuSwapchainComposition swapchainComposition,
Expand Down Expand Up @@ -683,7 +683,7 @@ typedef struct SDL_GpuDriver
{
const char *Name;
const SDL_GpuBackend backendflag;
Uint8 (*PrepareDriver)(SDL_VideoDevice *_this);
SDL_bool (*PrepareDriver)(SDL_VideoDevice *_this);
SDL_GpuDevice *(*CreateDevice)(SDL_bool debugMode);
} SDL_GpuDriver;

Expand Down
77 changes: 47 additions & 30 deletions src/gpu/d3d11/SDL_gpu_d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -3274,7 +3274,7 @@ static D3D11CommandBuffer *D3D11_INTERNAL_GetInactiveCommandBufferFromPool(
return commandBuffer;
}

static Uint8 D3D11_INTERNAL_CreateFence(
static SDL_bool D3D11_INTERNAL_CreateFence(
D3D11Renderer *renderer)
{
D3D11_QUERY_DESC queryDesc;
Expand Down Expand Up @@ -3305,10 +3305,10 @@ static Uint8 D3D11_INTERNAL_CreateFence(
renderer->availableFences[renderer->availableFenceCount] = fence;
renderer->availableFenceCount += 1;

return 1;
return SDL_TRUE;
}

static Uint8 D3D11_INTERNAL_AcquireFence(
static SDL_bool D3D11_INTERNAL_AcquireFence(
D3D11CommandBuffer *commandBuffer)
{
D3D11CommandBuffer *d3d11CommandBuffer = (D3D11CommandBuffer *)commandBuffer;
Expand All @@ -3322,7 +3322,7 @@ static Uint8 D3D11_INTERNAL_AcquireFence(
if (!D3D11_INTERNAL_CreateFence(renderer)) {
SDL_UnlockMutex(renderer->fenceLock);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create fence!");
return 0;
return SDL_FALSE;
}
}

Expand All @@ -3335,7 +3335,7 @@ static Uint8 D3D11_INTERNAL_AcquireFence(
commandBuffer->fence = fence;
(void)SDL_AtomicIncRef(&commandBuffer->fence->referenceCount);

return 1;
return SDL_TRUE;
}

static SDL_GpuCommandBuffer *D3D11_AcquireCommandBuffer(
Expand Down Expand Up @@ -4984,7 +4984,7 @@ static D3D11WindowData *D3D11_INTERNAL_FetchWindowData(
return (D3D11WindowData *)SDL_GetProperty(properties, WINDOW_PROPERTY_DATA, NULL);
}

static Uint8 D3D11_INTERNAL_InitializeSwapchainTexture(
static SDL_bool D3D11_INTERNAL_InitializeSwapchainTexture(
D3D11Renderer *renderer,
IDXGISwapChain *swapchain,
DXGI_FORMAT swapchainFormat,
Expand Down Expand Up @@ -5026,7 +5026,7 @@ static Uint8 D3D11_INTERNAL_InitializeSwapchainTexture(
if (FAILED(res)) {
ID3D11Texture2D_Release(swapchainTexture);
D3D11_INTERNAL_LogError(renderer->device, "Swapchain SRV creation failed", res);
return 0;
return SDL_FALSE;
}

/* Create the RTV for the swapchain */
Expand All @@ -5043,7 +5043,7 @@ static Uint8 D3D11_INTERNAL_InitializeSwapchainTexture(
ID3D11ShaderResourceView_Release(srv);
ID3D11Texture2D_Release(swapchainTexture);
D3D11_INTERNAL_LogError(renderer->device, "Swapchain RTV creation failed", res);
return 0;
return SDL_FALSE;
}

uavDesc.Format = swapchainFormat;
Expand All @@ -5060,7 +5060,7 @@ static Uint8 D3D11_INTERNAL_InitializeSwapchainTexture(
ID3D11RenderTargetView_Release(rtv);
ID3D11Texture2D_Release(swapchainTexture);
D3D11_INTERNAL_LogError(renderer->device, "Swapchain UAV creation failed", res);
return 0;
return SDL_FALSE;
}

/* Fill out the texture struct */
Expand Down Expand Up @@ -5091,10 +5091,10 @@ static Uint8 D3D11_INTERNAL_InitializeSwapchainTexture(
/* Cleanup */
ID3D11Texture2D_Release(swapchainTexture);

return 1;
return SDL_TRUE;
}

static Uint8 D3D11_INTERNAL_CreateSwapchain(
static SDL_bool D3D11_INTERNAL_CreateSwapchain(
D3D11Renderer *renderer,
D3D11WindowData *windowData,
SDL_GpuSwapchainComposition swapchainComposition,
Expand Down Expand Up @@ -5212,7 +5212,7 @@ static Uint8 D3D11_INTERNAL_CreateSwapchain(

if (!(colorSpaceSupport & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Requested colorspace is unsupported!");
return 0;
return SDL_FALSE;
}

IDXGISwapChain3_SetColorSpace1(
Expand All @@ -5222,7 +5222,7 @@ static Uint8 D3D11_INTERNAL_CreateSwapchain(
IDXGISwapChain3_Release(swapchain3);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "DXGI 1.4 not supported, cannot use colorspace other than SDL_GPU_COLORSPACE_NONLINEAR_SRGB!");
return 0;
return SDL_FALSE;
}

/* If a you are using a FLIP model format you can't create the swapchain as DXGI_FORMAT_B8G8R8A8_UNORM_SRGB.
Expand All @@ -5235,17 +5235,17 @@ static Uint8 D3D11_INTERNAL_CreateSwapchain(
(swapchainComposition == SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR) ? DXGI_FORMAT_B8G8R8A8_UNORM_SRGB : windowData->swapchainFormat,
&windowData->texture)) {
IDXGISwapChain_Release(swapchain);
return 0;
return SDL_FALSE;
}

/* Initialize dummy container */
SDL_zerop(&windowData->textureContainer);
windowData->textureContainer.textures = SDL_calloc(1, sizeof(D3D11Texture *));

return 1;
return SDL_TRUE;
}

static Uint8 D3D11_INTERNAL_ResizeSwapchain(
static SDL_bool D3D11_INTERNAL_ResizeSwapchain(
D3D11Renderer *renderer,
D3D11WindowData *windowData,
Sint32 width,
Expand Down Expand Up @@ -5348,15 +5348,15 @@ static SDL_bool D3D11_ClaimWindow(

SDL_UnlockMutex(renderer->windowLock);

return 1;
return SDL_TRUE;
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create swapchain, failed to claim window!");
SDL_free(windowData);
return 0;
return SDL_FALSE;
}
} else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Window already claimed!");
return 0;
return SDL_FALSE;
}
}

Expand Down Expand Up @@ -5544,7 +5544,7 @@ static SDL_GpuTextureFormat D3D11_GetSwapchainTextureFormat(
}
}

static void D3D11_SetSwapchainParameters(
static SDL_bool D3D11_SetSwapchainParameters(
SDL_GpuRenderer *driverData,
SDL_Window *window,
SDL_GpuSwapchainComposition swapchainComposition,
Expand All @@ -5553,6 +5553,21 @@ static void D3D11_SetSwapchainParameters(
D3D11Renderer *renderer = (D3D11Renderer *)driverData;
D3D11WindowData *windowData = D3D11_INTERNAL_FetchWindowData(window);

if (windowData == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Cannot set swapchain parameters on unclaimed window!");
return SDL_FALSE;
}

if (!D3D11_SupportsSwapchainComposition(driverData, window, swapchainComposition)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Swapchain composition not supported!");
return SDL_FALSE;
}

if (!D3D11_SupportsPresentMode(driverData, window, presentMode)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Present mode not supported!");
return SDL_FALSE;
}

if (
swapchainComposition != windowData->swapchainComposition ||
presentMode != windowData->presentMode) {
Expand All @@ -5563,12 +5578,14 @@ static void D3D11_SetSwapchainParameters(
renderer,
windowData);

D3D11_INTERNAL_CreateSwapchain(
return D3D11_INTERNAL_CreateSwapchain(
renderer,
windowData,
swapchainComposition,
presentMode);
}

return SDL_TRUE;
}

/* Submission */
Expand Down Expand Up @@ -5771,7 +5788,7 @@ static SDL_bool D3D11_IsTextureFormatSupported(

/* Device Creation */

static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)
static SDL_bool D3D11_PrepareDriver(SDL_VideoDevice *_this)
{
void *d3d11_dll, *dxgi_dll, *d3dcompiler_dll;
PFN_D3D11_CREATE_DEVICE D3D11CreateDeviceFunc;
Expand All @@ -5785,7 +5802,7 @@ static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)
d3d11_dll = SDL_LoadObject(D3D11_DLL);
if (d3d11_dll == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find " D3D11_DLL);
return 0;
return SDL_FALSE;
}

D3D11CreateDeviceFunc = (PFN_D3D11_CREATE_DEVICE)SDL_LoadFunction(
Expand All @@ -5794,7 +5811,7 @@ static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)
if (D3D11CreateDeviceFunc == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find function " D3D11_CREATE_DEVICE_FUNC " in " D3D11_DLL);
SDL_UnloadObject(d3d11_dll);
return 0;
return SDL_FALSE;
}

/* Can we create a device? */
Expand All @@ -5815,15 +5832,15 @@ static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)

if (FAILED(res)) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not create D3D11Device with feature level 11_0");
return 0;
return SDL_FALSE;
}

/* Can we load DXGI? */

dxgi_dll = SDL_LoadObject(DXGI_DLL);
if (dxgi_dll == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find " DXGI_DLL);
return 0;
return SDL_FALSE;
}

CreateDXGIFactoryFunc = (PFN_CREATE_DXGI_FACTORY1)SDL_LoadFunction(
Expand All @@ -5832,15 +5849,15 @@ static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)
SDL_UnloadObject(dxgi_dll); /* We're not going to call this function, so we can just unload now. */
if (CreateDXGIFactoryFunc == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find function " CREATE_DXGI_FACTORY1_FUNC " in " DXGI_DLL);
return 0;
return SDL_FALSE;
}

/* Can we load D3DCompiler? */

d3dcompiler_dll = SDL_LoadObject(D3DCOMPILER_DLL);
if (d3dcompiler_dll == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find " D3DCOMPILER_DLL);
return 0;
return SDL_FALSE;
}

D3DCompileFunc = (PFN_D3DCOMPILE)SDL_LoadFunction(
Expand All @@ -5849,10 +5866,10 @@ static Uint8 D3D11_PrepareDriver(SDL_VideoDevice *_this)
SDL_UnloadObject(d3dcompiler_dll); /* We're not going to call this function, so we can just unload now. */
if (D3DCompileFunc == NULL) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D11: Could not find function D3DCompile in " D3DCOMPILER_DLL);
return 0;
return SDL_FALSE;
}

return 1;
return SDL_TRUE;
}

static void D3D11_INTERNAL_TryInitializeDXGIDebug(D3D11Renderer *renderer)
Expand Down
18 changes: 15 additions & 3 deletions src/gpu/metal/SDL_gpu_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -3376,7 +3376,7 @@ static SDL_GpuTextureFormat METAL_GetSwapchainTextureFormat(
return windowData->textureContainer.createInfo.format;
}

static void METAL_SetSwapchainParameters(
static SDL_bool METAL_SetSwapchainParameters(
SDL_GpuRenderer *driverData,
SDL_Window *window,
SDL_GpuSwapchainComposition swapchainComposition,
Expand All @@ -3387,7 +3387,17 @@ static void METAL_SetSwapchainParameters(

if (windowData == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Cannot set swapchain parameters, window has not been claimed!");
return;
return SDL_FALSE;
}

if (!METAL_SupportsSwapchainComposition(driverData, window, swapchainComposition)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Swapchain composition not supported!");
return SDL_FALSE;
}

if (!METAL_SupportsPresentMode(driverData, window, presentMode)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Present mode not supported!");
return SDL_FALSE;
}

METAL_Wait(driverData);
Expand All @@ -3405,6 +3415,8 @@ static void METAL_SetSwapchainParameters(
CGColorSpaceRelease(colorspace);

windowData->textureContainer.createInfo.format = SwapchainCompositionToFormat[swapchainComposition];

return SDL_TRUE;
}

/* Submission */
Expand Down Expand Up @@ -3549,7 +3561,7 @@ static SDL_bool METAL_IsTextureFormatSupported(

/* Device Creation */

static Uint8 METAL_PrepareDriver(SDL_VideoDevice *_this)
static SDL_bool METAL_PrepareDriver(SDL_VideoDevice *_this)
{
/* FIXME: Add a macOS / iOS version check! Maybe support >= 10.14? */
return (_this->Metal_CreateView != NULL);
Expand Down
Loading
Loading