Skip to content

Commit 092cea2

Browse files
jdolanCopilot
andcommitted
Improve Hello.c style: GPU_Assert, AppState rename, Doxygen, waitForIdle ordering
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 80e7b74 commit 092cea2

2 files changed

Lines changed: 73 additions & 65 deletions

File tree

Examples/Hello.c

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,68 +59,69 @@ static const Vertex vertexes[] = {
5959
{ -0.5f, -0.5f, -0.5f, 0, 1, 0 }, { 0.5f, -0.5f, -0.5f, 0, 0, 1 }, { 0.5f, -0.5f, 0.5f, 1, 0, 1 },
6060
};
6161

62+
/**
63+
* @brief SDL application state passed via pointer to callbacks.
64+
*/
6265
typedef struct {
6366
SDL_Window *window;
6467
RenderDevice *renderDevice;
6568
Framebuffer *framebuffer;
6669
SDL_GPUBuffer *vertexBuffer;
6770
SDL_GPUGraphicsPipeline *pipeline;
6871
vec2 angles;
69-
Uint64 lastTicks;
70-
} App;
72+
Uint64 ticks;
73+
} AppState;
7174

72-
static App app;
75+
static AppState app;
7376

77+
/**
78+
* @brief SDL3 application initialization callback.
79+
*/
7480
SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
75-
(void) unused; (void) argc; (void) argv;
76-
77-
if (!SDL_Init(SDL_INIT_VIDEO)) {
78-
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init: %s", SDL_GetError());
79-
return SDL_APP_FAILURE;
80-
}
8181

82-
SDL_memset(&app, 0, sizeof(app));
82+
GPU_Assert(SDL_Init(SDL_INIT_VIDEO), "SDL_Init");
8383

8484
#ifdef EXAMPLES
8585
$$(Resource, addResourcePath, EXAMPLES);
8686
#endif
87+
8788
const char *basePath = SDL_GetBasePath();
8889
if (basePath) {
8990
$$(Resource, addResourcePath, basePath);
9091
}
9192

92-
app.window = SDL_CreateWindow("Hello ObjectivelyGPU",
93-
HELLO_WINDOW_W, HELLO_WINDOW_H, HELLO_WINDOW_FLAGS);
94-
if (!app.window) {
95-
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow: %s", SDL_GetError());
96-
return SDL_APP_FAILURE;
97-
}
93+
SDL_Window *window = SDL_CreateWindow("Hello ObjectivelyGPU", HELLO_WINDOW_W, HELLO_WINDOW_H, HELLO_WINDOW_FLAGS);
94+
GPU_Assert(window, "SDL_CreateWindow");
9895

99-
app.renderDevice = $(alloc(RenderDevice), initWithWindow, app.window);
96+
RenderDevice *renderDevice = $(alloc(RenderDevice), initWithWindow, window);
10097

10198
int w = 0, h = 0;
102-
SDL_GetWindowSizeInPixels(app.window, &w, &h);
103-
app.framebuffer = $(alloc(Framebuffer), initWithDevice, app.renderDevice,
99+
SDL_GetWindowSizeInPixels(window, &w, &h);
100+
101+
Framebuffer *framebuffer = $(alloc(Framebuffer), initWithDevice, renderDevice,
104102
&MakeSize(w, h),
105103
SDL_GPU_TEXTUREFORMAT_INVALID,
106104
SDL_GPU_TEXTUREFORMAT_D16_UNORM);
107105

108-
SDL_GPUShader *vertexShader = $(app.renderDevice, loadShader, "Hello.vert", &(SDL_GPUShaderCreateInfo) {
106+
SDL_GPUShader *vertexShader = $(renderDevice, loadShader, "Hello.vert", &(SDL_GPUShaderCreateInfo) {
109107
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
110108
.num_uniform_buffers = 1,
111109
});
112-
SDL_GPUShader *fragmentShader = $(app.renderDevice, loadShader, "Hello.frag", &(SDL_GPUShaderCreateInfo) {
110+
111+
SDL_GPUShader *fragmentShader = $(renderDevice, loadShader, "Hello.frag", &(SDL_GPUShaderCreateInfo) {
113112
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
114113
});
115114

116115
SDL_GPUColorTargetDescription colorTargetDescription = {
117-
.format = $(app.renderDevice, getSwapchainTextureFormat, app.window),
116+
.format = $(renderDevice, getSwapchainTextureFormat, window),
118117
};
118+
119119
SDL_GPUVertexBufferDescription vertexBufferDescription = {
120120
.slot = 0,
121121
.pitch = sizeof(Vertex),
122122
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
123123
};
124+
124125
SDL_GPUVertexAttribute vertexAttributes[] = {
125126
{
126127
.location = 0,
@@ -136,7 +137,7 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
136137
},
137138
};
138139

139-
app.pipeline = $(app.renderDevice, createGraphicsPipeline, &(SDL_GPUGraphicsPipelineCreateInfo) {
140+
SDL_GPUGraphicsPipeline *pipeline = $(renderDevice, createGraphicsPipeline, &(SDL_GPUGraphicsPipelineCreateInfo) {
140141
.vertex_shader = vertexShader,
141142
.fragment_shader = fragmentShader,
142143
.vertex_input_state = {
@@ -168,45 +169,49 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
168169
},
169170
});
170171

171-
$(app.renderDevice, releaseShader, vertexShader);
172-
$(app.renderDevice, releaseShader, fragmentShader);
172+
$(renderDevice, releaseShader, vertexShader);
173+
$(renderDevice, releaseShader, fragmentShader);
173174

174-
app.vertexBuffer = $(app.renderDevice, createBufferWithConstMem,
175-
SDL_GPU_BUFFERUSAGE_VERTEX, vertexes, sizeof(vertexes));
175+
SDL_GPUBuffer *vertexBuffer = $(renderDevice, createBufferWithConstMem, SDL_GPU_BUFFERUSAGE_VERTEX, vertexes, sizeof(vertexes));
176+
177+
app = (AppState) {
178+
.window = window,
179+
.renderDevice = renderDevice,
180+
.framebuffer = framebuffer,
181+
.vertexBuffer = vertexBuffer,
182+
.pipeline = pipeline,
183+
};
176184

177-
app.lastTicks = SDL_GetTicks();
178185
return SDL_APP_CONTINUE;
179186
}
180187

188+
/**
189+
* @brief SDL3 frame function callback.
190+
*/
181191
SDL_AppResult SDL_AppIterate(void *unused) {
182-
(void) unused;
183192

184193
Uint64 ticks = SDL_GetTicks();
185-
float dt = (float) (ticks - app.lastTicks) / 1000.0f;
186-
app.lastTicks = ticks;
194+
float dt = (float) (ticks - app.ticks) / 1000.0f;
195+
app.ticks = ticks;
187196

188197
app.angles.x += dt * 30.0f;
189198
app.angles.y += dt * 60.0f;
199+
190200
while (app.angles.x >= 360.0f) app.angles.x -= 360.0f;
191201
while (app.angles.y >= 360.0f) app.angles.y -= 360.0f;
192202

193203
CommandBuffer *cmd = $(app.renderDevice, acquireCommandBuffer);
194204

195205
SwapchainTexture swapchain = { 0 };
196-
if (!$(cmd, acquireSwapchainTexture, &swapchain)) {
197-
$(cmd, cancel);
198-
release(cmd);
199-
return SDL_APP_CONTINUE;
200-
}
206+
$(cmd, waitAndAcquireSwapchainTexture, &swapchain);
201207

202208
$(app.framebuffer, resize, &swapchain.size);
203209

204210
mat4 modelView = mat4_rotation(app.angles.x, vec3_new(1.f, 0.f, 0.f));
205211
modelView = mat4_mul(mat4_rotation(app.angles.y, vec3_new(0.f, 1.f, 0.f)), modelView);
206212
modelView = mat4_mul(mat4_translation(vec3_new(0.f, 0.f, -2.5f)), modelView);
207213

208-
const mat4 projection = mat4_perspective(45.f,
209-
(float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
214+
const mat4 projection = mat4_perspective(45.f, (float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
210215
const mat4 modelViewProjection = mat4_mul(projection, modelView);
211216

212217
SDL_GPUColorTargetInfo colorTarget = {
@@ -215,8 +220,8 @@ SDL_AppResult SDL_AppIterate(void *unused) {
215220
.load_op = SDL_GPU_LOADOP_CLEAR,
216221
.store_op = SDL_GPU_STOREOP_STORE,
217222
};
218-
SDL_GPUDepthStencilTargetInfo depthTarget = $(app.framebuffer, depthTargetInfo,
219-
SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
223+
224+
SDL_GPUDepthStencilTargetInfo depthTarget = $(app.framebuffer, depthTargetInfo, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
220225

221226
RenderPass *renderPass = $(cmd, beginRenderPass, &colorTarget, 1, &depthTarget);
222227
$(renderPass, bindPipeline, app.pipeline);
@@ -233,8 +238,11 @@ SDL_AppResult SDL_AppIterate(void *unused) {
233238
return SDL_APP_CONTINUE;
234239
}
235240

241+
/**
242+
* @brief SDL3 event callback.
243+
*/
236244
SDL_AppResult SDL_AppEvent(void *unused, SDL_Event *event) {
237-
(void) unused;
245+
238246
switch (event->type) {
239247
case SDL_EVENT_QUIT:
240248
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
@@ -244,26 +252,24 @@ SDL_AppResult SDL_AppEvent(void *unused, SDL_Event *event) {
244252
}
245253
}
246254

255+
/**
256+
* @brief SDL3 quit callback.
257+
*/
247258
void SDL_AppQuit(void *unused, SDL_AppResult result) {
248-
(void) unused; (void) result;
249259

250260
if (app.renderDevice) {
251261
$(app.renderDevice, waitForIdle);
262+
263+
if (app.pipeline) {
264+
$(app.renderDevice, releaseGraphicsPipeline, app.pipeline);
265+
}
266+
if (app.vertexBuffer) {
267+
$(app.renderDevice, releaseBuffer, app.vertexBuffer);
268+
}
252269
}
253-
if (app.pipeline) {
254-
$(app.renderDevice, releaseGraphicsPipeline, app.pipeline);
255-
}
256-
if (app.vertexBuffer) {
257-
$(app.renderDevice, releaseBuffer, app.vertexBuffer);
258-
}
259-
if (app.framebuffer) {
260-
release(app.framebuffer);
261-
}
262-
if (app.renderDevice) {
263-
release(app.renderDevice);
264-
}
265-
if (app.window) {
266-
SDL_DestroyWindow(app.window);
267-
}
270+
271+
release(app.framebuffer);
272+
release(app.renderDevice);
273+
268274
SDL_Quit();
269275
}

ObjectivelyGPU.xcodeproj/project.pbxproj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
CECF83B72FEF765400CB0CCA /* Hello.frag.spv in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A62FEF712F00CB0CCA /* Hello.frag.spv */; };
5252
CECF83B82FEF765400CB0CCA /* Hello.vert.glsl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A72FEF712F00CB0CCA /* Hello.vert.glsl */; };
5353
CECF83B92FEF765400CB0CCA /* Hello.vert.msl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A82FEF712F00CB0CCA /* Hello.vert.msl */; };
54-
EI00200000000000000000B1 /* Hello.frag.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A52FEF712F00CB0CCA /* Hello.frag.msl */; };
55-
EI00300000000000000000B1 /* Hello.vert.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A82FEF712F00CB0CCA /* Hello.vert.msl */; };
5654
CECF83BA2FEF765400CB0CCA /* Hello.vert.spv in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A92FEF712F00CB0CCA /* Hello.vert.spv */; };
5755
CECF83BB2FEF765400CB0CCA /* HelloCompute.c in CopyFiles */ = {isa = PBXBuildFile; fileRef = EE00020000000000000000B2 /* HelloCompute.c */; };
5856
CECF83BC2FEF765400CB0CCA /* HelloCompute.comp.glsl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83AA2FEF712F00CB0CCA /* HelloCompute.comp.glsl */; };
@@ -76,6 +74,8 @@
7674
EE00160000000000000000B7 /* SDL3.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA000000000000000000EE01 /* SDL3.xcframework */; };
7775
EE00170000000000000000B8 /* Objectively.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEA0030000000000000000F0 /* Objectively.framework */; };
7876
EI00100000000000000000B1 /* Hello.c in Sources */ = {isa = PBXBuildFile; fileRef = EE00010000000000000000B1 /* Hello.c */; };
77+
EI00200000000000000000B1 /* Hello.frag.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A52FEF712F00CB0CCA /* Hello.frag.msl */; };
78+
EI00300000000000000000B1 /* Hello.vert.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A82FEF712F00CB0CCA /* Hello.vert.msl */; };
7979
EI00300000000000000000B3 /* ObjectivelyGPU.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = EI00050000000000000000B5 /* ObjectivelyGPU.xcframework */; };
8080
EI00400000000000000000B4 /* SDL3.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA000000000000000000EE01 /* SDL3.xcframework */; };
8181
EI00500000000000000000B5 /* Objectively.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = EI00060000000000000000B6 /* Objectively.xcframework */; };
@@ -126,20 +126,20 @@
126126
dstPath = "";
127127
dstSubfolderSpec = 7;
128128
files = (
129-
CECF83B52FEF765400CB0CCA /* Hello.frag.hlsl in CopyFiles */,
129+
CECF83B52FEF765400CB0CCA /* Hello.frag.glsl in CopyFiles */,
130130
CECF83B62FEF765400CB0CCA /* Hello.frag.msl in CopyFiles */,
131131
CECF83B72FEF765400CB0CCA /* Hello.frag.spv in CopyFiles */,
132-
CECF83B82FEF765400CB0CCA /* Hello.vert.hlsl in CopyFiles */,
132+
CECF83B82FEF765400CB0CCA /* Hello.vert.glsl in CopyFiles */,
133133
CECF83B92FEF765400CB0CCA /* Hello.vert.msl in CopyFiles */,
134134
CECF83BA2FEF765400CB0CCA /* Hello.vert.spv in CopyFiles */,
135135
CECF83BB2FEF765400CB0CCA /* HelloCompute.c in CopyFiles */,
136-
CECF83BC2FEF765400CB0CCA /* HelloCompute.comp.hlsl in CopyFiles */,
136+
CECF83BC2FEF765400CB0CCA /* HelloCompute.comp.glsl in CopyFiles */,
137137
CECF83BD2FEF765400CB0CCA /* HelloCompute.comp.msl in CopyFiles */,
138138
CECF83BE2FEF765400CB0CCA /* HelloCompute.comp.spv in CopyFiles */,
139-
CECF83BF2FEF765400CB0CCA /* HelloCompute.frag.hlsl in CopyFiles */,
139+
CECF83BF2FEF765400CB0CCA /* HelloCompute.frag.glsl in CopyFiles */,
140140
CECF83C02FEF765400CB0CCA /* HelloCompute.frag.msl in CopyFiles */,
141141
CECF83C12FEF765400CB0CCA /* HelloCompute.frag.spv in CopyFiles */,
142-
CECF83C22FEF765400CB0CCA /* HelloCompute.vert.hlsl in CopyFiles */,
142+
CECF83C22FEF765400CB0CCA /* HelloCompute.vert.glsl in CopyFiles */,
143143
CECF83C32FEF765400CB0CCA /* HelloCompute.vert.msl in CopyFiles */,
144144
CECF83C42FEF765400CB0CCA /* HelloCompute.vert.spv in CopyFiles */,
145145
);
@@ -197,6 +197,7 @@
197197
CECF83B22FEF712F00CB0CCA /* HelloCompute.vert.spv */ = {isa = PBXFileReference; lastKnownFileType = file; path = HelloCompute.vert.spv; sourceTree = "<group>"; };
198198
CECF83C52FF03A3500CB0CCA /* Framebuffer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Framebuffer.h; sourceTree = "<group>"; };
199199
CECF83C62FF03A3500CB0CCA /* Framebuffer.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = Framebuffer.c; sourceTree = "<group>"; };
200+
CECF83CD2FF1671600CB0CCA /* build-xcframework.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "build-xcframework.sh"; sourceTree = "<group>"; };
200201
CEEAAFF72FED833A00FFEBE6 /* COPYING */ = {isa = PBXFileReference; lastKnownFileType = text; path = COPYING; sourceTree = "<group>"; };
201202
CEEAAFF82FED833A00FFEBE6 /* Makefile.am */ = {isa = PBXFileReference; lastKnownFileType = text; path = Makefile.am; sourceTree = "<group>"; };
202203
CEEAAFFA2FED833A00FFEBE6 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
@@ -327,6 +328,7 @@
327328
CEA0030000000000000000F0 /* Objectively.framework */,
328329
EI00050000000000000000B5 /* ObjectivelyGPU.xcframework */,
329330
EI00060000000000000000B6 /* Objectively.xcframework */,
331+
CECF83CD2FF1671600CB0CCA /* build-xcframework.sh */,
330332
);
331333
name = Frameworks;
332334
sourceTree = "<group>";
@@ -604,7 +606,7 @@
604606
isa = PBXSourcesBuildPhase;
605607
buildActionMask = 2147483647;
606608
files = (
607-
EI00100000000000000000B1 /* Hello-iOS.c in Sources */,
609+
EI00100000000000000000B1 /* Hello.c in Sources */,
608610
);
609611
runOnlyForDeploymentPostprocessing = 0;
610612
};

0 commit comments

Comments
 (0)