-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelloCompute.c
More file actions
263 lines (207 loc) · 7.37 KB
/
Copy pathHelloCompute.c
File metadata and controls
263 lines (207 loc) · 7.37 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
/*
* 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 particle framebuffer.
*/
#define HELLO_MSAA SDL_GPU_SAMPLECOUNT_4
/**
* @brief The number of particles animated by the compute shader.
*/
#define NUM_PARTICLES 256u
/**
* @brief A particle position, written by the compute shader and read by the vertex shader.
*/
typedef struct {
float x, y;
} Particle;
/**
* @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 particle rendering.
*/
Framebuffer *framebuffer;
/**
* @brief The storage buffer of particle positions; written by compute, read by the vertex stage.
*/
Buffer *particleBuffer;
/**
* @brief The compute pipeline that animates the particles.
*/
ComputePipeline *computePipeline;
/**
* @brief The graphics pipeline that renders the particles as points.
*/
GraphicsPipeline *graphicsPipeline;
/**
* @brief The application start time, in milliseconds.
*/
Uint64 ticks;
} AppState;
static AppState application;
#pragma mark - Particle system
/**
* @brief Initializes the particle buffer and the compute and graphics pipelines.
*/
static void initParticles(AppState *app) {
app->particleBuffer = $(app->renderDevice, createBuffer, &(SDL_GPUBufferCreateInfo) {
.usage = SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE | SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ,
.size = NUM_PARTICLES * sizeof(Particle),
});
app->computePipeline = $(app->renderDevice, loadComputePipeline, "HelloCompute.comp", &(SDL_GPUComputePipelineCreateInfo) {
.num_readwrite_storage_buffers = 1,
.num_uniform_buffers = 1,
.threadcount_x = NUM_PARTICLES,
.threadcount_y = 1,
.threadcount_z = 1,
});
Shader *vertexShader = $(app->renderDevice, loadShader, "HelloCompute.vert", &(SDL_GPUShaderCreateInfo) {
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
.num_storage_buffers = 1,
});
Shader *fragmentShader = $(app->renderDevice, loadShader, "HelloCompute.frag", &(SDL_GPUShaderCreateInfo) {
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
});
SDL_GPUGraphicsPipelineCreateInfo pipelineInfo = GPU_GraphicsPipeline2D;
pipelineInfo.vertex_shader = vertexShader->shader;
pipelineInfo.fragment_shader = fragmentShader->shader;
pipelineInfo.primitive_type = SDL_GPU_PRIMITIVETYPE_POINTLIST;
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,
};
app->graphicsPipeline = $(app->renderDevice, createGraphicsPipeline, &pipelineInfo);
release(vertexShader);
release(fragmentShader);
}
/**
* @brief Animates the particles via compute, then renders them as points.
*/
static void drawParticles(AppState *app, CommandBuffer *commands) {
const float time = (float) (SDL_GetTicks() - app->ticks) / 1000.f;
$(commands, pushComputeUniformData, 0, &time, sizeof(time));
ComputePass *computePass = $(commands, beginComputePass, NULL, 0,
&(SDL_GPUStorageBufferReadWriteBinding) { .buffer = app->particleBuffer->buffer }, 1);
$(computePass, bindPipeline, app->computePipeline);
$(computePass, dispatchCompute, 1, 1, 1);
release(computePass);
const SDL_FColor clearColor = { 0.05f, 0.05f, 0.1f, 1.f };
const SDL_GPUColorTargetInfo color = $(app->framebuffer, colorTargetInfo, 0, SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_STORE, &clearColor);
RenderPass *pass = $(commands, beginRenderPass, &color, 1, NULL);
$(pass, bindPipeline, app->graphicsPipeline);
$(pass, bindVertexStorageBuffers, 0, (SDL_GPUBuffer *[]) { app->particleBuffer->buffer }, 1);
$(pass, drawPrimitives, NUM_PARTICLES, 1, 0, 0);
release(pass);
}
#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 Compute 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,
.sampleCount = HELLO_MSAA,
});
$(app->renderDevice, setFramebuffer, app->framebuffer);
app->ticks = SDL_GetTicks();
initParticles(app);
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) {
drawParticles(app, commands);
$(app->renderDevice, endFrame);
}
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->graphicsPipeline);
release(app->computePipeline);
release(app->particleBuffer);
release(app->framebuffer);
release(app->renderDevice);
SDL_DestroyWindow(app->window);
SDL_Quit();
}