-
Notifications
You must be signed in to change notification settings - Fork 2
/
render_thread_test_nanotime_step.c
367 lines (330 loc) · 12.3 KB
/
render_thread_test_nanotime_step.c
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
360
361
362
363
364
365
366
367
/*
* You can choose this license, if possible in your jurisdiction:
*
* Unlicense
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute
* this software, either in source code form or as a compiled binary, for any
* purpose, commercial or non-commercial, and by any means.
*
* In jurisdictions that recognize copyright laws, the author or authors of
* this software dedicate any and all copyright interest in the software to the
* public domain. We make this dedication for the benefit of the public at
* large and to the detriment of our heirs and successors. We intend this
* dedication to be an overt act of relinquishment in perpetuity of all present
* and future rights to this software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*
*
* Alternative license choice, if works can't be directly submitted to the
* public domain in your jurisdiction:
*
* The MIT License (MIT)
*
* Copyright © 2022 Brandon McGriff <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
// It seems this scheme of creating a render thread works as expected at least
// for the OpenGL backend on ARM64/x86_64 Linux, x64 Windows, and Apple Silicon
// macOS. TODO: Test on more platforms, then add the tested platforms here.
//
// The initial setup does as much as possible to ensure an OpenGL render driver
// is used, of course. And the initial setup fails gracefully if no OpenGL
// driver is available.
//
// The setup is accomplished via:
//
// 1. Initialize the window etc. first in the main thread.
//
// 2. Make the OpenGL context for the renderer not current in the main
// thread.
//
// 3. Create the render thread, relying upon the creation being a full memory
// barrier between the main thread and render thread.
//
// 4. Make the context current in the render thread before doing any render API
// calls in the render thread.
//
// 5. Make the context not current in the render thread before the render
// thread closes.
//
// 7. Wait-thread on the render thread in the main thread.
//
// 8. Make the context current in the main thread upon closure of the render
// thread, relying upon the closure being a full memory barrier between the
// main thread and render thread.
//
// 9. Destroy everything as usual in the main thread.
//
// Between making the context not current in the main thread and later making
// it current in the main thread before shutdown, no render APIs are called in
// the main thread.
//
// Perhaps some dance of sync between the threads is required if the main
// thread does some changes on the window object via non-render APIs (a mutex
// over the window object might suffice). Or it's outright unsafe to operate
// upon the window object in the main thread, while the context is current in
// the render thread, in which case the main thread would have to signal to the
// render thread to do the window operations, and window state would have to be
// read in the render thread then communicated to the main thread.
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#define NANOTIME_IMPLEMENTATION
#include "nanotime.h"
#include "SDL.h"
#define TICK_RATE 60.0
static SDL_atomic_t quit_now = { 0 };
static SDL_atomic_t ticks = { 0 };
// We rely on thread create and wait being full memory barriers between a
// spawning thread and spawned thread, so no sync is required for these. And,
// since all writes of them are only done in the spawning thread (the main
// thread), all reads in the spawned thread (the render thread) are guaranteed
// valid.
static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;
static SDL_GLContext context = NULL;
static SDL_sem* render_wake = NULL;
static int SDLCALL render(void* data) {
SDL_assert(window != NULL);
SDL_assert(renderer != NULL);
SDL_assert(context != NULL);
SDL_assert(render_wake != NULL);
if (SDL_GL_MakeCurrent(window, context) < 0) {
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&quit_now, 1);
return -1;
}
while (true) {
if (SDL_SemWait(render_wake) < 0) {
SDL_GL_MakeCurrent(window, NULL);
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&quit_now, 1);
return -2;
}
// quit_now is set true before waking this thread by the main
// thread when the main thread determines it's time to quit, so
// we always have to acquire to be sure we get the correct
// value each wakeup that the main thread expects this thread
// to observe.
const int current_quit_now = SDL_AtomicGet(&quit_now);
SDL_MemoryBarrierAcquire();
if (current_quit_now) {
break;
}
// We want to be sure the ticks count monotonically proceeds
// through its range, not observing old values before new
// values, so we have to acquire here.
const int current_ticks = SDL_AtomicGet(&ticks);
SDL_MemoryBarrierAcquire();
const Uint8 shade = (Uint8)(((SDL_sin(2.0 * M_PI * (current_ticks / TICK_RATE)) + 1.0) / 2.0) * 255.0);
if (
SDL_SetRenderDrawColor(renderer, shade, shade, shade, SDL_ALPHA_OPAQUE) < 0 ||
SDL_RenderClear(renderer) < 0
) {
SDL_GL_MakeCurrent(window, NULL);
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&quit_now, 1);
return -3;
}
SDL_RenderPresent(renderer);
}
SDL_GL_MakeCurrent(window, NULL);
return 0;
}
int main(int argc, char** argv) {
if (SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed\n");
return EXIT_FAILURE;
}
const int num_render_drivers = SDL_GetNumRenderDrivers();
if (num_render_drivers < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get number of render drivers\n");
SDL_Quit();
return EXIT_FAILURE;
}
int render_driver;
for (render_driver = 0; render_driver < num_render_drivers; render_driver++) {
SDL_RendererInfo info;
if (SDL_GetRenderDriverInfo(render_driver, &info) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get render driver info for index %d\n", render_driver);
SDL_Quit();
return EXIT_FAILURE;
}
if (!SDL_strncmp(info.name, "opengl", 6)) {
break;
}
}
if (render_driver == num_render_drivers) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find some OpenGL render driver, which is required\n");
SDL_Quit();
return EXIT_FAILURE;
}
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&quit_now, 0);
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&ticks, 0);
window = SDL_CreateWindow("render_thread_test_nanotime_step", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (!window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow failed\n");
SDL_Quit();
return EXIT_FAILURE;
}
renderer = SDL_CreateRenderer(window, render_driver, SDL_RENDERER_ACCELERATED);
if (!renderer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateRenderer failed\n");
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_FAILURE;
}
context = SDL_GL_GetCurrentContext();
if (context == NULL || SDL_GL_MakeCurrent(window, NULL) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Context release failed, context is %s\n", context == NULL ? "NULL" : "not NULL");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_FAILURE;
}
render_wake = SDL_CreateSemaphore(0);
if (!render_wake) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create render_wake semaphore\n");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_FAILURE;
}
SDL_Thread* const render_thread = SDL_CreateThread(render, "render_thread", NULL);
if (!render_thread) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create the render thread\n");
SDL_DestroySemaphore(render_wake);
if (SDL_GL_MakeCurrent(window, context) >= 0) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to make context current in main thread\n");
}
SDL_Quit();
return EXIT_FAILURE;
}
nanotime_step_data stepper;
#ifdef REALTIME
SDL_SetHint(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, "1");
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
#endif
nanotime_step_init(&stepper, (uint64_t)(NANOTIME_NSEC_PER_SEC / TICK_RATE), nanotime_now_max(), nanotime_now, nanotime_sleep);
uint64_t last_point = stepper.sleep_point;
uint64_t sleep_total = 0;
uint64_t num_ticks = 0;
int status = 0;
while (true) {
const int current_quit_now = SDL_AtomicGet(&quit_now);
SDL_MemoryBarrierAcquire();
if (current_quit_now) {
goto end;
}
SDL_PumpEvents();
SDL_Event event;
while ((status = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) > 0) {
if (event.type == SDL_QUIT) {
goto end;
}
if (event.type == SDL_KEYDOWN) {
sleep_total = 0;
num_ticks = 0;
}
}
if (status < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error while dequeueing events\n");
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
goto end;
}
if (SDL_AtomicGet(&ticks) == (int)TICK_RATE - 1) {
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&ticks, 0);
}
else {
SDL_MemoryBarrierRelease();
SDL_AtomicIncRef(&ticks);
}
if (SDL_SemPost(render_wake) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error waking render thread during main thread ticks\n");
abort();
}
nanotime_step(&stepper);
#ifdef SHOW_LOG
const uint64_t current_sleep = nanotime_interval(last_point, stepper.sleep_point, stepper.now_max);
sleep_total += current_sleep;
num_ticks++;
SDL_Log("%" PRIu64 " ns/tick current, %" PRIu64 " ns/tick average, %" PRId64 " ns off, accumulated %" PRIu64 " ns\n",
current_sleep,
sleep_total / num_ticks,
(int64_t)(current_sleep - stepper.sleep_duration),
stepper.accumulator
);
last_point = stepper.sleep_point;
#endif
}
end:
SDL_MemoryBarrierRelease();
SDL_AtomicSet(&quit_now, 1);
if (SDL_SemPost(render_wake) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error waking render thread at quit\n");
abort();
}
SDL_WaitThread(render_thread, &status);
switch (status) {
default:
if (status != 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid status value returned from render thread of %d\n", status);
abort();
}
break;
case -1:
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error making context current in render thread\n");
break;
case -2:
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error waiting on semaphore in render thread\n");
abort();
case -3:
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error rendering in render thread\n");
break;
}
if (SDL_GL_MakeCurrent(window, context) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error making context current at quit\n");
abort();
}
SDL_DestroySemaphore(render_wake);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}