Skip to content

Commit 72023f1

Browse files
jdolanCopilot
andcommitted
Hello.c: remove goto-based cleanup, GPU_Assert all resource allocations
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8304e73 commit 72023f1

2 files changed

Lines changed: 23 additions & 42 deletions

File tree

Examples/Hello.c

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24-
#include <assert.h>
25-
#include <stdlib.h>
26-
2724
#include <SDL3/SDL.h>
2825

2926
#include <Objectively.h>
@@ -58,23 +55,21 @@ static const VertexData vertex_data[] = {
5855
*/
5956
int main(int argc, char **argv) {
6057

61-
int status = EXIT_FAILURE;
62-
63-
SDL_Init(SDL_INIT_VIDEO);
58+
GPU_Assert(SDL_Init(SDL_INIT_VIDEO), "SDL_Init");
6459

6560
SDL_Window *window = SDL_CreateWindow("ObjectivelyGPU Hello", 800, 600, SDL_WINDOW_HIGH_PIXEL_DENSITY);
66-
GPU_Assert(window, "Failed to create window");
61+
GPU_Assert(window, "SDL_CreateWindow");
6762

6863
$$(Resource, addResourcePath, EXAMPLES);
6964

7065
RenderDevice *renderDevice = $(alloc(RenderDevice), initWithWindow, window);
71-
assert(renderDevice);
66+
GPU_Assert(renderDevice, "RenderDevice init");
7267

73-
SDL_GPUBuffer *vertexBuffer = $(renderDevice, createBufferWithConstMem,
74-
SDL_GPU_BUFFERUSAGE_VERTEX, vertex_data, sizeof(vertex_data));
68+
SDL_GPUBuffer *vertexBuffer = $(renderDevice, createBufferWithConstMem,
69+
SDL_GPU_BUFFERUSAGE_VERTEX, vertex_data, sizeof(vertex_data));
7570

76-
int w = 0;
77-
int h = 0;
71+
int w = 0;
72+
int h = 0;
7873
SDL_GetWindowSizeInPixels(window, &w, &h);
7974

8075
Framebuffer *framebuffer = $(alloc(Framebuffer), initWithDevice, renderDevice,
@@ -230,35 +225,13 @@ int main(int argc, char **argv) {
230225
release(cmd);
231226
}
232227

233-
status = EXIT_SUCCESS;
234-
235-
cleanup:
236-
if (renderDevice) {
237-
$(renderDevice, waitForIdle);
238-
}
239-
240-
if (pipeline) {
241-
$(renderDevice, releaseGraphicsPipeline, pipeline);
242-
}
243-
if (vertexShader) {
244-
$(renderDevice, releaseShader, vertexShader);
245-
}
246-
if (fragmentShader) {
247-
$(renderDevice, releaseShader, fragmentShader);
248-
}
249-
if (vertexBuffer) {
250-
$(renderDevice, releaseBuffer, vertexBuffer);
251-
}
252-
if (framebuffer) {
253-
release(framebuffer);
254-
}
255-
if (renderDevice) {
256-
release(renderDevice);
257-
}
258-
if (window) {
259-
SDL_DestroyWindow(window);
260-
}
261-
228+
$(renderDevice, waitForIdle);
229+
$(renderDevice, releaseGraphicsPipeline, pipeline);
230+
$(renderDevice, releaseBuffer, vertexBuffer);
231+
release(framebuffer);
232+
release(renderDevice);
233+
SDL_DestroyWindow(window);
262234
SDL_Quit();
263-
return status;
235+
236+
return EXIT_SUCCESS;
264237
}

ObjectivelyGPU.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
CECF83C22FEF765400CB0CCA /* HelloCompute.vert.hlsl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83B02FEF712F00CB0CCA /* HelloCompute.vert.hlsl */; };
6363
CECF83C32FEF765400CB0CCA /* HelloCompute.vert.msl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83B12FEF712F00CB0CCA /* HelloCompute.vert.msl */; };
6464
CECF83C42FEF765400CB0CCA /* HelloCompute.vert.spv in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83B22FEF712F00CB0CCA /* HelloCompute.vert.spv */; };
65+
CECF83C72FF03A3500CB0CCA /* Framebuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = CECF83C52FF03A3500CB0CCA /* Framebuffer.h */; };
66+
CECF83C82FF03A3500CB0CCA /* Framebuffer.c in Sources */ = {isa = PBXBuildFile; fileRef = CECF83C62FF03A3500CB0CCA /* Framebuffer.c */; };
6567
DF73824CC25298378B451056 /* CopyPass.h in Headers */ = {isa = PBXBuildFile; fileRef = 59334BDAED542AB6DE565EDD /* CopyPass.h */; settings = {ATTRIBUTES = (Public, ); }; };
6668
EE00100000000000000000B1 /* Hello.c in Sources */ = {isa = PBXBuildFile; fileRef = EE00010000000000000000B1 /* Hello.c */; };
6769
EE00110000000000000000B2 /* HelloCompute.c in Sources */ = {isa = PBXBuildFile; fileRef = EE00020000000000000000B2 /* HelloCompute.c */; };
@@ -191,6 +193,8 @@
191193
CECF83B02FEF712F00CB0CCA /* HelloCompute.vert.hlsl */ = {isa = PBXFileReference; lastKnownFileType = text; path = HelloCompute.vert.hlsl; sourceTree = "<group>"; };
192194
CECF83B12FEF712F00CB0CCA /* HelloCompute.vert.msl */ = {isa = PBXFileReference; lastKnownFileType = text; path = HelloCompute.vert.msl; sourceTree = "<group>"; };
193195
CECF83B22FEF712F00CB0CCA /* HelloCompute.vert.spv */ = {isa = PBXFileReference; lastKnownFileType = file; path = HelloCompute.vert.spv; sourceTree = "<group>"; };
196+
CECF83C52FF03A3500CB0CCA /* Framebuffer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Framebuffer.h; sourceTree = "<group>"; };
197+
CECF83C62FF03A3500CB0CCA /* Framebuffer.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = Framebuffer.c; sourceTree = "<group>"; };
194198
CEEAAFF72FED833A00FFEBE6 /* COPYING */ = {isa = PBXFileReference; lastKnownFileType = text; path = COPYING; sourceTree = "<group>"; };
195199
CEEAAFF82FED833A00FFEBE6 /* Makefile.am */ = {isa = PBXFileReference; lastKnownFileType = text; path = Makefile.am; sourceTree = "<group>"; };
196200
CEEAAFFA2FED833A00FFEBE6 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
@@ -291,6 +295,8 @@
291295
09B8DD26CDB7926D89445C91 /* ComputePass.h */,
292296
C862954A9DD435C1FF7CD31D /* CopyPass.c */,
293297
59334BDAED542AB6DE565EDD /* CopyPass.h */,
298+
CECF83C62FF03A3500CB0CCA /* Framebuffer.c */,
299+
CECF83C52FF03A3500CB0CCA /* Framebuffer.h */,
294300
CECF83972FEF6A7200CB0CCA /* Mathlib.h */,
295301
77315F9BC357C97B885E883C /* RenderDevice.c */,
296302
9220C57BBAA3C951D8083799 /* RenderDevice.h */,
@@ -377,6 +383,7 @@
377383
files = (
378384
6F351DFD232DA78A42D8B494 /* Config.h in Headers */,
379385
2A17F0E01E5CC25A431FFBAD /* CommandBuffer.h in Headers */,
386+
CECF83C72FF03A3500CB0CCA /* Framebuffer.h in Headers */,
380387
034285033FECAB2D6BD6AE5B /* ComputePass.h in Headers */,
381388
DF73824CC25298378B451056 /* CopyPass.h in Headers */,
382389
CECF83982FEF6A7200CB0CCA /* Mathlib.h in Headers */,
@@ -582,6 +589,7 @@
582589
36FBB92ACB08F2078D50C84D /* CommandBuffer.c in Sources */,
583590
F73340493879CD8034F99D51 /* ComputePass.c in Sources */,
584591
CCCB11DB70E4A962360A2119 /* CopyPass.c in Sources */,
592+
CECF83C82FF03A3500CB0CCA /* Framebuffer.c in Sources */,
585593
86E5132F5DCCB6B674151343 /* RenderDevice.c in Sources */,
586594
FF30FF2D56F9E4EA3C492CBB /* RenderPass.c in Sources */,
587595
);

0 commit comments

Comments
 (0)