Skip to content

Commit 22295c8

Browse files
committed
Remove superfluous window param from renderDevice methods.
1 parent f3297a0 commit 22295c8

7 files changed

Lines changed: 78 additions & 100 deletions

File tree

Examples/Hello.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ SDL_AppResult SDL_AppInit(void **appState, int argc, char *argv[]) {
240240
int w = 0, h = 0;
241241
SDL_GetWindowSizeInPixels(app->window, &w, &h);
242242

243-
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat, app->window);
243+
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat);
244244
app->framebuffer = $(app->renderDevice, createFramebuffer, &(GPU_FramebufferCreateInfo) {
245245
.size = MakeSize(w, h),
246246
.colorFormat = colorFormat,

Examples/HelloCompute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ SDL_AppResult SDL_AppInit(void **appState, int argc, char *argv[]) {
194194
int w = 0, h = 0;
195195
SDL_GetWindowSizeInPixels(app->window, &w, &h);
196196

197-
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat, app->window);
197+
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat);
198198
app->framebuffer = $(app->renderDevice, createFramebuffer, &(GPU_FramebufferCreateInfo) {
199199
.size = MakeSize(w, h),
200200
.colorFormat = colorFormat,

Sources/ObjectivelyGPU/Buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static void upload(Buffer *self, const void *data, Uint32 size, Uint32 offset, b
150150
cycle);
151151

152152
release(copyPass);
153-
$(self->device, submit, commands);
153+
$(commands, submit);
154154
release(commands);
155155
SDL_ReleaseGPUTransferBuffer(self->device->device, tbuf);
156156
}

Sources/ObjectivelyGPU/CommandBuffer.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ static bool acquireSwapchainTexture(const CommandBuffer *self, SwapchainTexture
5656

5757
assert(swapchain);
5858

59-
return SDL_AcquireGPUSwapchainTexture(self->commands,
60-
self->device->window,
61-
&swapchain->texture,
62-
(Uint32 *) &swapchain->size.w,
63-
(Uint32 *) &swapchain->size.h);
59+
Uint32 w = 0, h = 0;
60+
const bool ok = SDL_AcquireGPUSwapchainTexture(self->commands,
61+
self->device->window,
62+
&swapchain->texture,
63+
&w, &h);
64+
swapchain->size = (SDL_Size) { (int) w, (int) h };
65+
return ok;
6466
}
6567

6668
/**
@@ -259,11 +261,13 @@ static bool waitAndAcquireSwapchainTexture(const CommandBuffer *self, SwapchainT
259261

260262
assert(swapchain);
261263

262-
return SDL_WaitAndAcquireGPUSwapchainTexture(self->commands,
263-
self->device->window,
264-
&swapchain->texture,
265-
(Uint32 *) &swapchain->size.w,
266-
(Uint32 *) &swapchain->size.h);
264+
Uint32 w = 0, h = 0;
265+
const bool ok = SDL_WaitAndAcquireGPUSwapchainTexture(self->commands,
266+
self->device->window,
267+
&swapchain->texture,
268+
&w, &h);
269+
swapchain->size = (SDL_Size) { (int) w, (int) h };
270+
return ok;
267271
}
268272

269273
#pragma mark - Class lifecycle

Sources/ObjectivelyGPU/RenderDevice.c

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@
4040
#include "Shader.h"
4141
#include "Texture.h"
4242

43+
/**
44+
* @brief Whether to create the `SDL_GPUDevice` with backend validation/debug layers.
45+
* @details Defaults to enabled unless `NDEBUG` is defined (i.e. on for debug builds,
46+
* off for release). Override by defining `GPU_DEBUG` to `true` or `false` at build time.
47+
*/
48+
#ifndef GPU_DEBUG
49+
#ifdef NDEBUG
50+
#define GPU_DEBUG false
51+
#else
52+
#define GPU_DEBUG true
53+
#endif
54+
#endif
55+
4356
#define _Class _RenderDevice
4457

4558
#pragma mark - Object
@@ -240,8 +253,14 @@ static Texture *createTextureFromSurface(RenderDevice *self, SDL_Surface *surfac
240253

241254
return $(alloc(Texture), initWithSurface, self, surface, usage);
242255
}
243-
static SDL_GPUTextureFormat getSwapchainTextureFormat(const RenderDevice *self, SDL_Window *window) {
244-
return SDL_GetGPUSwapchainTextureFormat(self->device, window);
256+
257+
/**
258+
* @fn SDL_GPUTextureFormat RenderDevice::getSwapchainTextureFormat(const RenderDevice *self)
259+
* @memberof RenderDevice
260+
*/
261+
static SDL_GPUTextureFormat getSwapchainTextureFormat(const RenderDevice *self) {
262+
assert(self->window);
263+
return SDL_GetGPUSwapchainTextureFormat(self->device, self->window);
245264
}
246265

247266
/**
@@ -258,7 +277,7 @@ static RenderDevice *init(RenderDevice *self) {
258277
SDL_GPU_SHADERFORMAT_SPIRV |
259278
SDL_GPU_SHADERFORMAT_DXIL;
260279

261-
self->device = SDL_CreateGPUDevice(formats, false, NULL);
280+
self->device = SDL_CreateGPUDevice(formats, GPU_DEBUG, NULL);
262281
GPU_Assert(self->device, "SDL_CreateGPUDevice");
263282
}
264283

@@ -356,8 +375,9 @@ static void setFramebuffer(RenderDevice *self, Framebuffer *framebuffer) {
356375
* @fn bool RenderDevice::setSwapchainParameters(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode)
357376
* @memberof RenderDevice
358377
*/
359-
static bool setSwapchainParameters(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode) {
360-
return SDL_SetGPUSwapchainParameters(self->device, window, composition, mode);
378+
static bool setSwapchainParameters(const RenderDevice *self, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode) {
379+
assert(self->window);
380+
return SDL_SetGPUSwapchainParameters(self->device, self->window, composition, mode);
361381
}
362382

363383
/**
@@ -383,21 +403,21 @@ static void setWindow(RenderDevice *self, SDL_Window *window) {
383403
}
384404

385405
/**
386-
* @fn void RenderDevice::submit(const RenderDevice *self, CommandBuffer *commands)
406+
* @fn bool RenderDevice::supportsPresentMode(const RenderDevice *self, SDL_GPUPresentMode mode)
387407
* @memberof RenderDevice
388408
*/
389-
static void submit(const RenderDevice *self, CommandBuffer *commands) {
390-
assert(commands);
391-
$(commands, submit);
409+
static bool supportsPresentMode(const RenderDevice *self, SDL_GPUPresentMode mode) {
410+
assert(self->window);
411+
return SDL_WindowSupportsGPUPresentMode(self->device, self->window, mode);
392412
}
393413

394414
/**
395-
* @fn SDL_GPUFence *RenderDevice::submitAndFence(const RenderDevice *self, CommandBuffer *commands)
415+
* @fn bool RenderDevice::supportsSwapchainComposition(const RenderDevice *self, SDL_GPUSwapchainComposition composition)
396416
* @memberof RenderDevice
397417
*/
398-
static SDL_GPUFence *submitAndFence(const RenderDevice *self, CommandBuffer *commands) {
399-
assert(commands);
400-
return $(commands, submitAndFence);
418+
static bool supportsSwapchainComposition(const RenderDevice *self, SDL_GPUSwapchainComposition composition) {
419+
assert(self->window);
420+
return SDL_WindowSupportsGPUSwapchainComposition(self->device, self->window, composition);
401421
}
402422

403423
/**
@@ -441,27 +461,12 @@ static bool waitForIdle(const RenderDevice *self) {
441461
}
442462

443463
/**
444-
* @fn bool RenderDevice::waitForSwapchain(const RenderDevice *self, SDL_Window *window)
445-
* @memberof RenderDevice
446-
*/
447-
static bool waitForSwapchain(const RenderDevice *self, SDL_Window *window) {
448-
return SDL_WaitForGPUSwapchain(self->device, window);
449-
}
450-
451-
/**
452-
* @fn bool RenderDevice::windowSupportsPresentMode(const RenderDevice *self, SDL_Window *window, SDL_GPUPresentMode mode)
453-
* @memberof RenderDevice
454-
*/
455-
static bool windowSupportsPresentMode(const RenderDevice *self, SDL_Window *window, SDL_GPUPresentMode mode) {
456-
return SDL_WindowSupportsGPUPresentMode(self->device, window, mode);
457-
}
458-
459-
/**
460-
* @fn bool RenderDevice::windowSupportsSwapchainComposition(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition)
464+
* @fn bool RenderDevice::waitForSwapchain(const RenderDevice *self)
461465
* @memberof RenderDevice
462466
*/
463-
static bool windowSupportsSwapchainComposition(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition) {
464-
return SDL_WindowSupportsGPUSwapchainComposition(self->device, window, composition);
467+
static bool waitForSwapchain(const RenderDevice *self) {
468+
assert(self->window);
469+
return SDL_WaitForGPUSwapchain(self->device, self->window);
465470
}
466471

467472
#pragma mark - Class lifecycle
@@ -500,16 +505,14 @@ static void initialize(Class *clazz) {
500505
((RenderDeviceInterface *) clazz->interface)->setFramebuffer = setFramebuffer;
501506
((RenderDeviceInterface *) clazz->interface)->setSwapchainParameters = setSwapchainParameters;
502507
((RenderDeviceInterface *) clazz->interface)->setWindow = setWindow;
503-
((RenderDeviceInterface *) clazz->interface)->submit = submit;
504-
((RenderDeviceInterface *) clazz->interface)->submitAndFence = submitAndFence;
508+
((RenderDeviceInterface *) clazz->interface)->supportsPresentMode = supportsPresentMode;
509+
((RenderDeviceInterface *) clazz->interface)->supportsSwapchainComposition = supportsSwapchainComposition;
505510
((RenderDeviceInterface *) clazz->interface)->textureSupportsFormat = textureSupportsFormat;
506511
((RenderDeviceInterface *) clazz->interface)->textureSupportsSampleCount = textureSupportsSampleCount;
507512
((RenderDeviceInterface *) clazz->interface)->unmapTransferBuffer = unmapTransferBuffer;
508513
((RenderDeviceInterface *) clazz->interface)->waitForFences = waitForFences;
509514
((RenderDeviceInterface *) clazz->interface)->waitForIdle = waitForIdle;
510515
((RenderDeviceInterface *) clazz->interface)->waitForSwapchain = waitForSwapchain;
511-
((RenderDeviceInterface *) clazz->interface)->windowSupportsPresentMode = windowSupportsPresentMode;
512-
((RenderDeviceInterface *) clazz->interface)->windowSupportsSwapchainComposition = windowSupportsSwapchainComposition;
513516
}
514517

515518
/**

Sources/ObjectivelyGPU/RenderDevice.h

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,15 @@ struct RenderDeviceInterface {
295295
void (*endFrame)(RenderDevice *self);
296296

297297
/**
298-
* @fn SDL_GPUTextureFormat RenderDevice::getSwapchainTextureFormat(const RenderDevice *self, SDL_Window *window)
299-
* @brief Returns the pixel format of the swapchain for the given window.
298+
* @fn SDL_GPUTextureFormat RenderDevice::getSwapchainTextureFormat(const RenderDevice *self)
299+
* @brief Returns the pixel format of this device's swapchain.
300300
* @details Useful for configuring render-pass colour target formats or creating
301-
* pipelines that write to the swapchain.
301+
* pipelines that write to the swapchain. The device must have a window claimed.
302302
* @param self The RenderDevice.
303-
* @param window The window whose swapchain format to query.
304303
* @return The `SDL_GPUTextureFormat` of the window's swapchain.
305304
* @memberof RenderDevice
306305
*/
307-
SDL_GPUTextureFormat (*getSwapchainTextureFormat)(const RenderDevice *self, SDL_Window *window);
306+
SDL_GPUTextureFormat (*getSwapchainTextureFormat)(const RenderDevice *self);
308307

309308
/**
310309
* @fn RenderDevice *RenderDevice::init(RenderDevice *self)
@@ -436,18 +435,17 @@ struct RenderDeviceInterface {
436435
void (*setFramebuffer)(RenderDevice *self, Framebuffer *framebuffer);
437436

438437
/**
439-
* @fn bool RenderDevice::setSwapchainParameters(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode)
440-
* @brief Configures swapchain composition and present mode for a window.
441-
* @details Use `windowSupportsSwapchainComposition` and `windowSupportsPresentMode`
438+
* @fn bool RenderDevice::setSwapchainParameters(const RenderDevice *self, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode)
439+
* @brief Configures swapchain composition and present mode for this device's window.
440+
* @details Use `supportsSwapchainComposition` and `supportsPresentMode`
442441
* to guard against unsupported combinations before calling this.
443442
* @param self The RenderDevice.
444-
* @param window The window whose swapchain to configure.
445443
* @param composition Colour space / HDR composition mode.
446444
* @param mode Presentation mode (vsync, mailbox, immediate, etc.).
447445
* @return `true` on success, `false` if the combination is unsupported.
448446
* @memberof RenderDevice
449447
*/
450-
bool (*setSwapchainParameters)(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode);
448+
bool (*setSwapchainParameters)(const RenderDevice *self, SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode mode);
451449

452450
/**
453451
* @fn void RenderDevice::setWindow(RenderDevice *self, SDL_Window *window)
@@ -462,28 +460,24 @@ struct RenderDeviceInterface {
462460
void (*setWindow)(RenderDevice *self, SDL_Window *window);
463461

464462
/**
465-
* @fn void RenderDevice::submit(const RenderDevice *self, CommandBuffer *commands)
466-
* @brief Submits a recorded CommandBuffer to the GPU for execution.
467-
* @details The CommandBuffer's underlying `SDL_GPUCommandBuffer` is consumed;
468-
* the caller must still `release` the CommandBuffer object.
463+
* @fn bool RenderDevice::supportsPresentMode(const RenderDevice *self, SDL_GPUPresentMode mode)
464+
* @brief Queries whether this device's swapchain supports the given present mode.
469465
* @param self The RenderDevice.
470-
* @param commands The CommandBuffer to submit.
466+
* @param mode The present mode to test.
467+
* @return `true` if the present mode is supported.
471468
* @memberof RenderDevice
472469
*/
473-
void (*submit)(const RenderDevice *self, CommandBuffer *commands);
470+
bool (*supportsPresentMode)(const RenderDevice *self, SDL_GPUPresentMode mode);
474471

475472
/**
476-
* @fn SDL_GPUFence *RenderDevice::submitAndFence(const RenderDevice *self, CommandBuffer *commands)
477-
* @brief Submits a CommandBuffer and returns a fence for CPU synchronisation.
478-
* @details The fence becomes signaled when all GPU work in @p commands has completed.
479-
* Use `queryFence` or `waitForFences` to poll or block on it, then release it
480-
* with `releaseFence`.
473+
* @fn bool RenderDevice::supportsSwapchainComposition(const RenderDevice *self, SDL_GPUSwapchainComposition composition)
474+
* @brief Queries whether this device's swapchain supports the given composition (colour space / HDR mode).
481475
* @param self The RenderDevice.
482-
* @param commands The CommandBuffer to submit.
483-
* @return A new `SDL_GPUFence`. GPU_Asserts on failure. Release with `releaseFence`.
476+
* @param composition The swapchain composition to test.
477+
* @return `true` if the composition is supported.
484478
* @memberof RenderDevice
485479
*/
486-
SDL_GPUFence *(*submitAndFence)(const RenderDevice *self, CommandBuffer *commands);
480+
bool (*supportsSwapchainComposition)(const RenderDevice *self, SDL_GPUSwapchainComposition composition);
487481

488482
/**
489483
* @fn bool RenderDevice::textureSupportsFormat(const RenderDevice *self, SDL_GPUTextureFormat format, SDL_GPUTextureType type, SDL_GPUTextureUsageFlags usage)
@@ -543,36 +537,13 @@ struct RenderDeviceInterface {
543537
bool (*waitForIdle)(const RenderDevice *self);
544538

545539
/**
546-
* @fn bool RenderDevice::waitForSwapchain(const RenderDevice *self, SDL_Window *window)
547-
* @brief Blocks until the swapchain for @p window is available for the next frame.
540+
* @fn bool RenderDevice::waitForSwapchain(const RenderDevice *self)
541+
* @brief Blocks until this device's swapchain is available for the next frame.
548542
* @param self The RenderDevice.
549-
* @param window The window whose swapchain to wait for.
550543
* @return `true` on success, `false` on error.
551544
* @memberof RenderDevice
552545
*/
553-
bool (*waitForSwapchain)(const RenderDevice *self, SDL_Window *window);
554-
555-
/**
556-
* @fn bool RenderDevice::windowSupportsPresentMode(const RenderDevice *self, SDL_Window *window, SDL_GPUPresentMode mode)
557-
* @brief Queries whether a window's swapchain supports the given present mode.
558-
* @param self The RenderDevice.
559-
* @param window The window to query.
560-
* @param mode The present mode to test.
561-
* @return `true` if the present mode is supported for @p window.
562-
* @memberof RenderDevice
563-
*/
564-
bool (*windowSupportsPresentMode)(const RenderDevice *self, SDL_Window *window, SDL_GPUPresentMode mode);
565-
566-
/**
567-
* @fn bool RenderDevice::windowSupportsSwapchainComposition(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition)
568-
* @brief Queries whether a window supports the given swapchain composition (colour space / HDR mode).
569-
* @param self The RenderDevice.
570-
* @param window The window to query.
571-
* @param composition The swapchain composition to test.
572-
* @return `true` if the composition is supported for @p window.
573-
* @memberof RenderDevice
574-
*/
575-
bool (*windowSupportsSwapchainComposition)(const RenderDevice *self, SDL_Window *window, SDL_GPUSwapchainComposition composition);
546+
bool (*waitForSwapchain)(const RenderDevice *self);
576547
};
577548

578549
/**

Sources/ObjectivelyGPU/Texture.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static Texture *initWithDevice(Texture *self, RenderDevice *device, const SDL_GP
109109
false);
110110

111111
release(copyPass);
112-
$(device, submit, commands);
112+
$(commands, submit);
113113
release(commands);
114114
SDL_ReleaseGPUTransferBuffer(device->device, tbuf);
115115
}

0 commit comments

Comments
 (0)