-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHello.c
More file actions
359 lines (288 loc) · 11.3 KB
/
Copy pathHello.c
File metadata and controls
359 lines (288 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* ObjectivelyGPU: Object oriented graphics framework for SDL3 and C.
* Copyright (C) 2026 Jay Dolan <jay@jaydolan.com>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>
#include <SDL3/SDL.h>
#include <Objectively.h>
#include <ObjectivelyGPU.h>
#ifdef SDL_PLATFORM_IOS
# define HELLO_WINDOW_W 0
# define HELLO_WINDOW_H 0
# define HELLO_WINDOW_FLAGS (SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN)
#else
# define HELLO_WINDOW_W 1024
# define HELLO_WINDOW_H 720
# define HELLO_WINDOW_FLAGS SDL_WINDOW_HIGH_PIXEL_DENSITY
#endif
/**
* @brief MSAA sample count for the scene framebuffer.
*/
#define HELLO_MSAA SDL_GPU_SAMPLECOUNT_4
/**
* @brief The Scene type.
*/
typedef struct {
/**
* @brief The cube vertex buffer.
*/
Buffer *vertexBuffer;
/**
* @brief The graphics pipeline.
*/
GraphicsPipeline *pipeline;
/**
* @brief The cube angles.
*/
vec2 angles;
} Scene;
/**
* @brief SDL application state passed via pointer to callbacks.
*/
typedef struct {
/**
* @brief The @c SDL_Window.
*/
SDL_Window *window;
/**
* @brief The ObjectivelyGPU @c RenderDevice.
*/
RenderDevice *renderDevice;
/**
* @brief The @c Framebuffer for scene rendering.
*/
Framebuffer *framebuffer;
/**
* @brief Simulation time in milliseconds.
*/
Uint64 ticks;
/**
* @brief The @c Scene.
*/
Scene scene;
/**
* @brief Occlusion query proof-of-concept: pool, readback buffer, and frame counter.
*/
QueryPool *occlusionQueryPool;
TransferBuffer *occlusionQueryTransfer;
Uint32 frames;
} AppState;
static AppState application;
#pragma mark - Scene management
typedef struct {
vec3 position;
vec3 color;
} Vertex;
static const Vertex vertices[] = {
{ { -0.5f, 0.5f, -0.5f }, { 1, 0, 0 } }, { { 0.5f, -0.5f, -0.5f }, { 0, 0, 1 } }, { { -0.5f, -0.5f, -0.5f }, { 0, 1, 0 } },
{ { -0.5f, 0.5f, -0.5f }, { 1, 0, 0 } }, { { 0.5f, 0.5f, -0.5f }, { 1, 1, 0 } }, { { 0.5f, -0.5f, -0.5f }, { 0, 0, 1 } },
{ { -0.5f, 0.5f, 0.5f }, { 1, 1, 1 } }, { { -0.5f, -0.5f, -0.5f }, { 0, 1, 0 } }, { { -0.5f, -0.5f, 0.5f }, { 0, 1, 1 } },
{ { -0.5f, 0.5f, 0.5f }, { 1, 1, 1 } }, { { -0.5f, 0.5f, -0.5f }, { 1, 0, 0 } }, { { -0.5f, -0.5f, -0.5f }, { 0, 1, 0 } },
{ { -0.5f, 0.5f, 0.5f }, { 1, 1, 1 } }, { { 0.5f, 0.5f, -0.5f }, { 1, 1, 0 } }, { { -0.5f, 0.5f, -0.5f }, { 1, 0, 0 } },
{ { -0.5f, 0.5f, 0.5f }, { 1, 1, 1 } }, { { 0.5f, 0.5f, 0.5f }, { 0, 0, 0 } }, { { 0.5f, 0.5f, -0.5f }, { 1, 1, 0 } },
{ { 0.5f, 0.5f, -0.5f }, { 1, 1, 0 } }, { { 0.5f, -0.5f, 0.5f }, { 1, 0, 1 } }, { { 0.5f, -0.5f, -0.5f }, { 0, 0, 1 } },
{ { 0.5f, 0.5f, -0.5f }, { 1, 1, 0 } }, { { 0.5f, 0.5f, 0.5f }, { 0, 0, 0 } }, { { 0.5f, -0.5f, 0.5f }, { 1, 0, 1 } },
{ { 0.5f, 0.5f, 0.5f }, { 0, 0, 0 } }, { { -0.5f, -0.5f, 0.5f }, { 0, 1, 1 } }, { { 0.5f, -0.5f, 0.5f }, { 1, 0, 1 } },
{ { 0.5f, 0.5f, 0.5f }, { 0, 0, 0 } }, { { -0.5f, 0.5f, 0.5f }, { 1, 1, 1 } }, { { -0.5f, -0.5f, 0.5f }, { 0, 1, 1 } },
{ { -0.5f, -0.5f, -0.5f }, { 0, 1, 0 } }, { { 0.5f, -0.5f, 0.5f }, { 1, 0, 1 } }, { { -0.5f, -0.5f, 0.5f }, { 0, 1, 1 } },
{ { -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 } },
};
/**
* @brief Initializes the @c Scene.
*/
static void initScene(AppState *app) {
Scene *scene = &app->scene;
scene->vertexBuffer = $(app->renderDevice, createBufferWithConstMem, SDL_GPU_BUFFERUSAGE_VERTEX, vertices, sizeof(vertices));
Shader *vertexShader = $(app->renderDevice, loadShader, "Hello.vert", &(SDL_GPUShaderCreateInfo) {
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
.num_uniform_buffers = 1,
});
Shader *fragmentShader = $(app->renderDevice, loadShader, "Hello.frag", &(SDL_GPUShaderCreateInfo) {
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
});
const SDL_GPUVertexBufferDescription vbd = {
.slot = 0,
.pitch = sizeof(Vertex),
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
};
const SDL_GPUVertexAttribute attrs[] = {
{
.location = 0,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, position),
},
{
.location = 1,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, color),
},
};
SDL_GPUGraphicsPipelineCreateInfo pipelineInfo = GPU_GraphicsPipeline3D;
pipelineInfo.vertex_shader = vertexShader->shader;
pipelineInfo.fragment_shader = fragmentShader->shader;
pipelineInfo.vertex_input_state = (SDL_GPUVertexInputState) {
.vertex_buffer_descriptions = &vbd,
.num_vertex_buffers = 1,
.vertex_attributes = attrs,
.num_vertex_attributes = (Uint32) SDL_arraysize(attrs),
};
pipelineInfo.multisample_state.sample_count = app->framebuffer->sampleCount;
pipelineInfo.target_info = (SDL_GPUGraphicsPipelineTargetInfo) {
.color_target_descriptions = &(SDL_GPUColorTargetDescription) {
.format = app->framebuffer->colorTextures[0]->format,
.blend_state = GPU_BlendStateOpaque,
},
.num_color_targets = 1,
.depth_stencil_format = app->framebuffer->depthTexture->format,
.has_depth_stencil_target = true,
};
scene->pipeline = $(app->renderDevice, createGraphicsPipeline, &pipelineInfo);
release(vertexShader);
release(fragmentShader);
}
/**
* @brief Renders a single frame of the @c Scene.
*/
static void drawScene(AppState *app, CommandBuffer *commands) {
const Uint64 ticks = SDL_GetTicks();
const float dt = (ticks - app->ticks) / 1000.f;
app->ticks = ticks;
Scene *scene = &app->scene;
scene->angles.x = SDL_fmodf(scene->angles.x + dt * 30.f, 360.f);
scene->angles.y = SDL_fmodf(scene->angles.y + dt * 60.f, 360.f);
mat4 modelView = mat4_rotation(scene->angles.x, vec3_new(1.f, 0.f, 0.f));
modelView = mat4_mul(mat4_rotation(scene->angles.y, vec3_new(0.f, 1.f, 0.f)), modelView);
modelView = mat4_mul(mat4_translation(vec3_new(0.f, 0.f, -2.5f)), modelView);
const mat4 projection = mat4_perspective(45.f, (float) app->framebuffer->size.w / (float) app->framebuffer->size.h, 0.01f, 100.f);
const mat4 modelViewProjection = mat4_mul(projection, modelView);
$(commands, pushVertexUniformData, 0, modelViewProjection.f, sizeof(modelViewProjection));
const SDL_FColor clearColor = { 0.1f, 0.1f, 0.2f, 1.f };
const SDL_GPUColorTargetInfo color = $(app->framebuffer, colorTargetInfo, 0, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_STORE, &clearColor);
const SDL_GPUDepthStencilTargetInfo depth = $(app->framebuffer, depthTargetInfo, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
RenderPass *pass = $(commands, beginRenderPass, &color, 1, &depth);
$(pass, bindPipeline, scene->pipeline);
$(pass, bindVertexBuffers, 0, &(SDL_GPUBufferBinding) { .buffer = scene->vertexBuffer->buffer }, 1);
$(pass, beginQuery, app->occlusionQueryPool, 0);
$(pass, drawPrimitives, (Uint32) SDL_arraysize(vertices), 1, 0, 0);
$(pass, endQuery, app->occlusionQueryPool, 0);
release(pass);
}
/**
* @brief Occlusion query proof-of-concept: downloads and logs this frame's result.
* @details A real application would defer the readback to a later frame instead
* of blocking on the GPU like this. When occlusion queries are unsupported by
* the linked SDL3, this always reports a "not occluded" sentinel value.
*/
static void logOcclusionQueryResult(AppState *app) {
CommandBuffer *commands = $(app->renderDevice, acquireCommandBuffer);
CopyPass *copyPass = $(commands, beginCopyPass);
$(copyPass, downloadQueryResults, app->occlusionQueryPool, 0, 1, &(SDL_GPUTransferBufferLocation) {
.transfer_buffer = app->occlusionQueryTransfer->buffer,
});
release(copyPass);
Fence *fence = $(commands, submitAndFence);
release(commands);
$(fence, wait);
release(fence);
const Uint64 *result = $(app->occlusionQueryTransfer, map, false);
SDL_Log("Occlusion query result: %" SDL_PRIu64 " samples passed", *result);
$(app->occlusionQueryTransfer, unmap);
}
#pragma mark - SDL application callbacks
/**
* @brief SDL3 application initialization callback.
*/
SDL_AppResult SDL_AppInit(void **appState, int argc, char *argv[]) {
AppState *app = *appState = &application;
GPU_Assert(SDL_Init(SDL_INIT_VIDEO), "SDL_Init");
#ifdef EXAMPLES
$$(Resource, addResourcePath, EXAMPLES);
#endif
app->window = SDL_CreateWindow("Hello ObjectivelyGPU", HELLO_WINDOW_W, HELLO_WINDOW_H, HELLO_WINDOW_FLAGS);
GPU_Assert(app->window, "SDL_CreateWindow");
app->renderDevice = $(alloc(RenderDevice), initWithWindow, app->window);
int w = 0, h = 0;
SDL_GetWindowSizeInPixels(app->window, &w, &h);
const SDL_GPUTextureFormat colorFormat = $(app->renderDevice, getSwapchainTextureFormat);
app->framebuffer = $(app->renderDevice, createFramebuffer, &(GPU_FramebufferCreateInfo) {
.size = MakeSize(w, h),
.colorFormats = { colorFormat },
.numColorTargets = 1,
.depthFormat = SDL_GPU_TEXTUREFORMAT_D16_UNORM,
.sampleCount = HELLO_MSAA,
});
$(app->renderDevice, setFramebuffer, app->framebuffer);
initScene(app);
app->occlusionQueryPool = $(app->renderDevice, createQueryPool, &(SDL_GPUQueryPoolCreateInfo) {
.type = SDL_GPU_QUERY_PRECISE_OCCLUSION,
.query_count = 1,
});
app->occlusionQueryTransfer = $(app->renderDevice, createTransferBuffer, &(SDL_GPUTransferBufferCreateInfo) {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD,
.size = sizeof(Uint64),
});
return SDL_APP_CONTINUE;
}
/**
* @brief SDL3 frame iteration callback.
*/
SDL_AppResult SDL_AppIterate(void *appState) {
AppState *app = appState;
CommandBuffer *commands = $(app->renderDevice, beginFrame);
if (commands) {
drawScene(app, commands);
$(app->renderDevice, endFrame);
if ((app->frames++ % 60) == 0) {
logOcclusionQueryResult(app);
}
}
return SDL_APP_CONTINUE;
}
/**
* @brief SDL3 event callback.
*/
SDL_AppResult SDL_AppEvent(void *appState, SDL_Event *event) {
switch (event->type) {
case SDL_EVENT_QUIT:
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
return SDL_APP_SUCCESS;
default:
return SDL_APP_CONTINUE;
}
}
/**
* @brief SDL3 quit callback.
*/
void SDL_AppQuit(void *appState, SDL_AppResult result) {
AppState *app = appState;
$(app->renderDevice, waitForIdle);
release(app->scene.pipeline);
release(app->scene.vertexBuffer);
release(app->occlusionQueryPool);
release(app->occlusionQueryTransfer);
release(app->framebuffer);
release(app->renderDevice);
SDL_DestroyWindow(app->window);
SDL_Quit();
}