Skip to content

Commit 959f76d

Browse files
committed
Resolve SwapchainTexture format on acquisition.
Also simplify beginFrame
1 parent da8a467 commit 959f76d

5 files changed

Lines changed: 34 additions & 21 deletions

File tree

Examples/Hello.c

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

243243
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat);
244+
244245
app->framebuffer = $(app->renderDevice, createFramebuffer, &(GPU_FramebufferCreateInfo) {
245246
.size = MakeSize(w, h),
246247
.colorFormats = { colorFormat },

Examples/HelloCompute.c

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

197197
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat);
198+
198199
app->framebuffer = $(app->renderDevice, createFramebuffer, &(GPU_FramebufferCreateInfo) {
199200
.size = MakeSize(w, h),
200201
.colorFormats = { colorFormat },

Sources/ObjectivelyGPU/CommandBuffer.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ static bool acquireSwapchainTexture(const CommandBuffer *self, SwapchainTexture
6161
self->device->window,
6262
&swapchain->texture,
6363
&w, &h);
64-
swapchain->size = (SDL_Size) { (int) w, (int) h };
64+
65+
if (ok) {
66+
swapchain->size = (SDL_Size) { (int) w, (int) h };
67+
swapchain->format = SDL_GetGPUSwapchainTextureFormat(self->device->device, self->device->window);
68+
}
69+
6570
return ok;
6671
}
6772

@@ -266,7 +271,11 @@ static bool waitAndAcquireSwapchainTexture(const CommandBuffer *self, SwapchainT
266271
self->device->window,
267272
&swapchain->texture,
268273
&w, &h);
269-
swapchain->size = (SDL_Size) { (int) w, (int) h };
274+
if (ok) {
275+
swapchain->size = (SDL_Size) { (int) w, (int) h };
276+
swapchain->format = SDL_GetGPUSwapchainTextureFormat(self->device->device, self->device->window);
277+
}
278+
270279
return ok;
271280
}
272281

Sources/ObjectivelyGPU/CommandBuffer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ struct SwapchainTexture {
5252
*/
5353
SDL_GPUTexture *texture;
5454

55+
/**
56+
* @brief The swapchain texture format.
57+
*/
58+
SDL_GPUTextureFormat format;
59+
5560
/**
5661
* @brief The swapchain dimensions in pixels.
5762
*/

Sources/ObjectivelyGPU/RenderDevice.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,19 @@ static CommandBuffer *acquireCommandBuffer(const RenderDevice *self) {
9898
static CommandBuffer *beginFrame(RenderDevice *self) {
9999

100100
GPU_Assert(self->framebuffer, "no framebuffer set; call setFramebuffer first");
101-
GPU_Assert(!self->commandBuffer, "beginFrame called with a frame already in flight");
101+
GPU_Assert(self->commandBuffer == NULL, "beginFrame called with a frame already in flight");
102102

103-
CommandBuffer *commands = $(self, acquireCommandBuffer);
103+
self->commandBuffer = $(self, acquireCommandBuffer);
104104

105-
SwapchainTexture swapchain = { 0 };
106-
$(commands, waitAndAcquireSwapchainTexture, &swapchain);
107-
108-
if (!swapchain.texture) {
109-
$(commands, cancel);
110-
release(commands);
111-
return NULL;
105+
const bool ok = $(self->commandBuffer, waitAndAcquireSwapchainTexture, &self->swapchain);
106+
if (ok) {
107+
$(self->framebuffer, resize, &self->swapchain.size);
108+
} else {
109+
$(self->commandBuffer, cancel);
110+
self->commandBuffer = release(self->commandBuffer);
112111
}
113112

114-
$(self->framebuffer, resize, &swapchain.size);
115-
116-
self->commandBuffer = commands;
117-
self->swapchain = swapchain;
118-
119-
return commands;
113+
return self->commandBuffer;
120114
}
121115

122116
/**
@@ -147,8 +141,8 @@ static void endFrame(RenderDevice *self) {
147141

148142
$(self->commandBuffer, submit);
149143

150-
release(self->commandBuffer);
151-
self->commandBuffer = NULL;
144+
self->commandBuffer = release(self->commandBuffer);
145+
152146
self->swapchain = (SwapchainTexture) { 0 };
153147
}
154148

@@ -366,8 +360,11 @@ static bool setAllowedFramesInFlight(const RenderDevice *self, Uint32 allowed) {
366360
static void setFramebuffer(RenderDevice *self, Framebuffer *framebuffer) {
367361

368362
if (self->framebuffer != framebuffer) {
369-
release(self->framebuffer);
370-
self->framebuffer = framebuffer ? retain(framebuffer) : NULL;
363+
self->framebuffer = release(self->framebuffer);
364+
365+
if (framebuffer) {
366+
self->framebuffer = retain(framebuffer);
367+
}
371368
}
372369
}
373370

0 commit comments

Comments
 (0)