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
|
#include <simloop.h>
#include <test.h>
#include <stdint.h>
// -----------------------------------------------------------------------------
// Time.
static simloop_time_t time_delta_from_sec(double seconds) {
static constexpr double NANOS_PER_SEC = 1e9;
return (simloop_time_t)(seconds * NANOS_PER_SEC);
}
// -----------------------------------------------------------------------------
// Randomness.
typedef struct {
uint64_t a;
} XorShift64State;
static uint64_t xorshift64(XorShift64State* state) {
uint64_t x = state->a;
x ^= x << 7;
x ^= x >> 9;
return state->a = x;
}
// -----------------------------------------------------------------------------
// Tests.
/// At time/frame 0:
/// 1. An initial render is always triggered.
/// 2. No update is triggered (not enough time passed).
TEST_CASE(simloop_initial_render) {
Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = 10});
SimloopOut simout;
simloop_update(&simloop, 0, &simout);
TEST_TRUE(simout.should_render);
TEST_TRUE(!simout.should_update);
TEST_EQUAL(simout.frame, 0);
}
/// A frame is not re-rendered if time does not advance.
/// This applies whether rendering is frame-rate capped or unlimited, and
/// whether we are in the initial frame or a subsequent one.
void simloop_render_not_retriggered(
struct test_case_metadata* metadata, int max_render_fps,
bool initial_frame) {
Simloop simloop = simloop_make(
&(SimloopArgs){.update_fps = 10, .max_render_fps = max_render_fps});
SimloopOut simout;
simloop_update(&simloop, 0, &simout);
TEST_TRUE(simout.should_render);
TEST_TRUE(!simout.should_update);
TEST_EQUAL(simout.frame, 0);
if (!initial_frame) {
// Advance time beyond the initial frame.
simloop_update(&simloop, 1, &simout);
}
for (int i = 0; i < 10; i++) {
// Note that time does not advance here.
simloop_update(&simloop, 0, &simout);
TEST_TRUE(!simout.should_render);
TEST_TRUE(!simout.should_update);
TEST_EQUAL(simout.frame, 0);
}
}
TEST_CASE(simloop_render_not_retriggered_capped_initial_frame) {
simloop_render_not_retriggered(metadata, 10, true);
}
TEST_CASE(simloop_render_not_retriggered_unlimited_initial_frame) {
simloop_render_not_retriggered(metadata, 0, true);
}
TEST_CASE(simloop_render_not_retriggered_capped_subsequent_frame) {
simloop_render_not_retriggered(metadata, 10, false);
}
TEST_CASE(simloop_render_not_retriggered_unlimited_subsequent_frame) {
simloop_render_not_retriggered(metadata, 0, false);
}
/// A simulation loop with no render frame cap:
/// 1. Updates based on the desired update frame rate.
/// 2. Renders at every step.
TEST_CASE(simloop_no_render_frame_cap) {
constexpr int UPDATE_FPS = 10; // 100ms delta
const simloop_time_t UPDATE_DDT =
time_delta_from_sec(1.0 / (double)UPDATE_FPS);
const simloop_time_t STEP = time_delta_from_sec(0.05); // 50ms
const simloop_time_t SIM_DURATION_SEC = time_delta_from_sec(30);
// We need simulation time to be an exact multiple of the desired deltas for
// the modulo comparison below.
TEST_TRUE((UPDATE_DDT % STEP) == 0);
Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = UPDATE_FPS});
SimloopOut simout;
simloop_update(&simloop, 0, &simout);
TEST_TRUE(!simout.should_update); // Time has not advanced.
TEST_TRUE(simout.should_render); // Initial render.
for (simloop_time_t t = STEP; t <= SIM_DURATION_SEC; t += STEP) {
simloop_update(&simloop, STEP, &simout);
const bool expect_update = (t % UPDATE_DDT) == 0;
TEST_EQUAL(simout.should_update, expect_update);
TEST_TRUE(simout.should_render); // Always renders.
}
}
/// A simulation loop with a render frame cap:
/// 1. Updates based on the desired update frame rate.
/// 2. Renders based on the desired render frame rate.
TEST_CASE(simloop_with_render_frame_cap) {
constexpr int UPDATE_FPS = 10; // 100ms delta
constexpr int RENDER_FPS = 5; // 200ms delta
const simloop_time_t UPDATE_DDT =
time_delta_from_sec(1.0 / (double)UPDATE_FPS);
const simloop_time_t RENDER_DDT =
time_delta_from_sec(1.0 / (double)RENDER_FPS);
const simloop_time_t STEP = time_delta_from_sec(0.1); // 100ms
const simloop_time_t SIM_DURATION_SEC = time_delta_from_sec(30);
// We need simulation time to be an exact multiple of the desired deltas for
// the modulo comparisons below.
TEST_TRUE((UPDATE_DDT % STEP) == 0);
TEST_TRUE((RENDER_DDT % STEP) == 0);
Simloop simloop = simloop_make(
&(SimloopArgs){.update_fps = UPDATE_FPS, .max_render_fps = RENDER_FPS});
SimloopOut simout;
simloop_update(&simloop, 0, &simout);
TEST_TRUE(!simout.should_update); // Time has not advanced.
TEST_TRUE(simout.should_render); // Initial render.
for (simloop_time_t t = STEP; t <= SIM_DURATION_SEC; t += STEP) {
simloop_update(&simloop, STEP, &simout);
// A render is still expected at time 0.
TEST_EQUAL(simout.should_render, (t % RENDER_DDT) == 0);
TEST_EQUAL(simout.should_update, (t % UPDATE_DDT) == 0);
}
}
/// If the update falls behind the clock, then percent_frame can fall out of
/// range (>1) if we are not careful. This tests for this condition.
TEST_CASE(simloop_percent_frame_01_large_jump) {
constexpr int UPDATE_FPS = 10; // 100ms delta
const simloop_time_t UPDATE_DDT =
time_delta_from_sec(1.0 / (double)UPDATE_FPS);
const simloop_time_t STEP = time_delta_from_sec(1);
const simloop_time_t SIM_DURATION_SEC = time_delta_from_sec(30);
// We need simulation time to be an exact multiple of the desired deltas for
// the modulo comparison below.
TEST_TRUE((STEP % UPDATE_DDT) == 0);
Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = UPDATE_FPS});
SimloopOut simout;
simloop_update(&simloop, 0, &simout);
TEST_TRUE(!simout.should_update); // Time has not advanced.
TEST_TRUE(simout.should_render); // Initial render.
for (simloop_time_t t = STEP; t <= SIM_DURATION_SEC; t += STEP) {
simloop_update(&simloop, STEP, &simout);
TEST_TRUE(simout.should_update); // Tries to catch up to clock.
TEST_TRUE(simout.should_render);
}
}
/// One benefit of fixed over variable time deltas is determinism. Test for
/// this by getting to t=10 by different clock time increments.
///
/// Note that the time increments must be able to keep up with the desired frame
/// delta, otherwise determinism is not maintained. We can guarantee determinism
/// at the expense of re-introducing divergence.
/// TODO: Perhaps the API should return an update count instead of a boolean,
/// advance simulation time per the number of updates, then leave it up to
/// the client to decide whether to update just once or as many times as
/// requested, depending on whether they want determinism or convergence.
TEST_CASE(simloop_determinism) {
constexpr int UPDATE_FPS = 100; // 10ms delta
const simloop_time_t RANDOM_STEPS[] = {
time_delta_from_sec(0.007), // 7ms
time_delta_from_sec(0.005), // 5ms
time_delta_from_sec(0.003), // 3ms
};
constexpr uint64_t NUM_RANDOM_STEPS =
sizeof(RANDOM_STEPS) / sizeof(RANDOM_STEPS[0]);
const simloop_time_t SIM_DURATION_SEC = time_delta_from_sec(10);
constexpr float ADD = 0.123f;
typedef struct Simulation {
int iter_count;
float sum;
} Simulation;
#define UPDATE_SIMULATION(SIM) \
{ \
SIM.sum += ADD; \
SIM.iter_count++; \
}
Simulation sim[2] = {0};
XorShift64State xss = (XorShift64State){12069019817132197873ULL};
// Perform two simulations with random clock-time steps.
for (int s = 0; s < 2; ++s) {
simloop_time_t dt = 0;
Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = UPDATE_FPS});
SimloopOut simout;
for (simloop_time_t t = 0; t <= SIM_DURATION_SEC;) {
simloop_update(&simloop, dt, &simout);
if (simout.should_update) {
UPDATE_SIMULATION(sim[s]);
}
// Advance time with a random step.
const simloop_time_t step =
RANDOM_STEPS[xorshift64(&xss) % NUM_RANDOM_STEPS];
t += step;
dt = step;
}
}
// Make sure the simulations have advanced by the same number of updates so
// that we can compare them. They may not have had the same update count
// depending on the clock-time steps.
while (sim[0].iter_count < sim[1].iter_count) {
UPDATE_SIMULATION(sim[0]);
}
while (sim[1].iter_count < sim[0].iter_count) {
UPDATE_SIMULATION(sim[1]);
}
TEST_EQUAL(sim[0].iter_count, sim[1].iter_count);
// The sums should be exactly equal if determinism holds.
// Check also that they are non-zero to make sure the simulation actually
// advanced.
TEST_TRUE(sim[0].sum > 0.f);
TEST_EQUAL(sim[0].sum, sim[1].sum);
}
/// The simulation loop attempts to catch up with the clock in the event of a
/// time spike. This is possible only if the simulation loops with a frequency
/// higher than the requested update frequency given by the update delta time.
static void simloop_catch_up(
struct test_case_metadata* metadata, int update_ddt_ms, int loop_step_ms,
bool expect_catchup) {
const int UPDATE_FPS = 1000 / update_ddt_ms;
const simloop_time_t UPDATE_DDT =
time_delta_from_sec(1.0 / (double)UPDATE_FPS);
const simloop_time_t STEP =
time_delta_from_sec((double)loop_step_ms / 1000.0);
const simloop_time_t SIM_DURATION_SEC = time_delta_from_sec(30);
const int EXPECTED_TOTAL_FRAMES_WITH_CATCHUP =
(int)(SIM_DURATION_SEC / UPDATE_DDT);
Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = UPDATE_FPS});
SimloopOut simout;
int frames = 0;
// Simulate a time spike.
// Advance time to t=10s. That is a lag of 10,000ms / 100ms = 100 frames.
// The simulation now has 20s to catch up.
simloop_time_t dt = time_delta_from_sec(10);
for (simloop_time_t t = dt; t <= SIM_DURATION_SEC;) {
simloop_update(&simloop, dt, &simout);
if (simout.should_update) {
frames++;
}
// New delta is as usual.
dt = STEP;
t += dt;
}
if (expect_catchup) {
TEST_EQUAL(frames, EXPECTED_TOTAL_FRAMES_WITH_CATCHUP);
} else {
TEST_TRUE(frames < EXPECTED_TOTAL_FRAMES_WITH_CATCHUP);
}
}
/// (Loop frequency > update frequency) => successful catch-up.
TEST_CASE(simloop_catch_up_success) {
constexpr int UPDATE_DDT_MS = 100;
constexpr int LOOP_DDT_MS = 10;
simloop_catch_up(metadata, UPDATE_DDT_MS, LOOP_DDT_MS, true);
}
/// (Loop frequency < update frequency) => failed catch-up.
TEST_CASE(simloop_catch_up_failure) {
constexpr int UPDATE_DDT_MS = 10;
constexpr int LOOP_DDT_MS = 100;
simloop_catch_up(metadata, UPDATE_DDT_MS, LOOP_DDT_MS, false);
}
int main() { return 0; }
|