Skip to content

Commit a17ae73

Browse files
jdolanCopilot
andcommitted
Use waitAndAcquireSwapchainTexture in examples
Non-blocking acquire leaves a command buffer in flight with no swapchain texture; submitting it is wasteful and skipping rendering while still proceeding through the frame loop is fragile. Block until the swapchain is ready instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 42bd256 commit a17ae73

2 files changed

Lines changed: 27 additions & 33 deletions

File tree

Examples/Hello.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,32 @@ int main(int argc, char **argv) {
184184
CommandBuffer *cmd = $(renderDevice, acquireCommandBuffer);
185185

186186
SwapchainTexture swapchain = { 0 };
187-
if ($(cmd, acquireSwapchainTexture, window, &swapchain)) {
188-
189-
float4x4 modelView = float4x4_rotation(angleX, float3_new(1.f, 0.f, 0.f));
190-
modelView = float4x4_mul(float4x4_rotation(angleY, float3_new(0.f, 1.f, 0.f)), modelView);
191-
modelView = float4x4_mul(float4x4_translation(float3_new(0.f, 0.f, -2.5f)), modelView);
192-
const float4x4 projection = float4x4_perspective(45.f, (float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
193-
const float4x4 modelViewProjection = float4x4_mul(projection, modelView);
194-
195-
SDL_GPUColorTargetInfo colorTarget = {
196-
.texture = swapchain.texture,
197-
.clear_color = { 0.1f, 0.1f, 0.2f, 1.0f },
198-
.load_op = SDL_GPU_LOADOP_CLEAR,
199-
.store_op = SDL_GPU_STOREOP_STORE,
200-
};
201-
202-
SDL_GPUDepthStencilTargetInfo depthTarget = $(framebuffer, depthTargetInfo, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
203-
204-
RenderPass *renderPass = $(cmd, beginRenderPass, &colorTarget, 1, &depthTarget);
205-
$(renderPass, bindPipeline, pipeline);
206-
$(renderPass, bindVertexBuffers, 0, &(SDL_GPUBufferBinding) {
207-
.buffer = vertexBuffer,
208-
.offset = 0,
209-
}, 1);
210-
$(cmd, pushVertexUniformData, 0, modelViewProjection.f, sizeof(modelViewProjection));
211-
$(renderPass, drawPrimitives, (Uint32) SDL_arraysize(vertex_data), 1, 0, 0);
212-
release(renderPass);
213-
}
187+
$(cmd, waitAndAcquireSwapchainTexture, window, &swapchain);
188+
189+
float4x4 modelView = float4x4_rotation(angleX, float3_new(1.f, 0.f, 0.f));
190+
modelView = float4x4_mul(float4x4_rotation(angleY, float3_new(0.f, 1.f, 0.f)), modelView);
191+
modelView = float4x4_mul(float4x4_translation(float3_new(0.f, 0.f, -2.5f)), modelView);
192+
const float4x4 projection = float4x4_perspective(45.f, (float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
193+
const float4x4 modelViewProjection = float4x4_mul(projection, modelView);
194+
195+
SDL_GPUColorTargetInfo colorTarget = {
196+
.texture = swapchain.texture,
197+
.clear_color = { 0.1f, 0.1f, 0.2f, 1.0f },
198+
.load_op = SDL_GPU_LOADOP_CLEAR,
199+
.store_op = SDL_GPU_STOREOP_STORE,
200+
};
201+
202+
SDL_GPUDepthStencilTargetInfo depthTarget = $(framebuffer, depthTargetInfo, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
203+
204+
RenderPass *renderPass = $(cmd, beginRenderPass, &colorTarget, 1, &depthTarget);
205+
$(renderPass, bindPipeline, pipeline);
206+
$(renderPass, bindVertexBuffers, 0, &(SDL_GPUBufferBinding) {
207+
.buffer = vertexBuffer,
208+
.offset = 0,
209+
}, 1);
210+
$(cmd, pushVertexUniformData, 0, modelViewProjection.f, sizeof(modelViewProjection));
211+
$(renderPass, drawPrimitives, (Uint32) SDL_arraysize(vertex_data), 1, 0, 0);
212+
release(renderPass);
214213

215214
$(cmd, submit);
216215
release(cmd);

Examples/HelloCompute.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,7 @@ int main(int argc, char **argv) {
144144

145145
CommandBuffer *cmd = $(renderDevice, acquireCommandBuffer);
146146
SwapchainTexture swapchain = { 0 };
147-
148-
if (!$(cmd, acquireSwapchainTexture, window, &swapchain)) {
149-
$(cmd, cancel);
150-
release(cmd);
151-
continue;
152-
}
147+
$(cmd, waitAndAcquireSwapchainTexture, window, &swapchain);
153148

154149
float time = (float) (SDL_GetTicks() - startTicks) / 1000.0f;
155150
$(cmd, pushComputeUniformData, 0, &time, sizeof(time));

0 commit comments

Comments
 (0)