-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_tests.cpp
More file actions
272 lines (224 loc) · 8.11 KB
/
app_tests.cpp
File metadata and controls
272 lines (224 loc) · 8.11 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
//
// Created by André Leite on 02/11/2025.
//
#include "app_tests.hpp"
#include "nstl/base/base_include.hpp"
#include "nstl/base/base_log.hpp"
#include "nstl/base/base_job_system.hpp"
#include "app_state.hpp"
#define APP_MAX_JOB_SLOTS 8u
#define APP_TEST_ENTITY_COUNT 128u
struct TestEntity {
F32 x;
F32 y;
F32 speed;
F32 padding;
};
struct AppTestsState {
U32 entityCount;
TestEntity entities[APP_TEST_ENTITY_COUNT];
F32 expectedSpeedSum;
F32 lastVelocitySum;
F32 lastVelocityError;
F32 lastPositionSum;
U64 jobDispatchCount;
F32 chunkSums[APP_MAX_JOB_SLOTS + 1u];
Vec4F32 drawColor;
};
struct EntityJobParams {
AppTestsState* tests;
U32 startIndex;
U32 count;
F32 deltaSeconds;
Arena* programArena;
F32* sumOut;
};
static void app_tests_step_entities_single_thread(AppMemory* memory, AppCoreState* core, AppTestsState* tests,
F32 deltaSeconds);
static void app_tests_step_entities_jobs(AppMemory* memory, AppCoreState* core, AppTestsState* tests, F32 deltaSeconds);
static void app_tests_job_update(void* userData);
U64 app_tests_permanent_size(void) {
return sizeof(AppTestsState);
}
void app_tests_initialize(AppMemory* memory, AppCoreState* core, AppTestsState* tests) {
(void) memory;
if (!core || !tests) {
return;
}
MEMSET(tests, 0, sizeof(AppTestsState));
tests->entityCount = APP_TEST_ENTITY_COUNT;
for (U32 i = 0; i < tests->entityCount; ++i) {
TestEntity* entity = &tests->entities[i];
entity->x = (F32) i;
entity->y = 0.0f;
entity->speed = 1.0f + (F32) i * 0.25f;
entity->padding = 0.0f;
tests->expectedSpeedSum += entity->speed;
}
LOG_INFO("tests", "Initialized test entities (count={})", tests->entityCount);
(void) core;
}
void app_tests_reload(AppMemory* memory, AppCoreState* core, AppTestsState* tests) {
(void) memory;
if (!core || !tests) {
return;
}
LOG_INFO("tests", "Reloaded module (dispatches={} totalPos={:.2f})",
tests->jobDispatchCount, tests->lastPositionSum);
}
void app_tests_tick(AppMemory* memory, AppCoreState* core, AppTestsState* tests, F32 deltaSeconds) {
if (!core || !tests) {
return;
}
if (core->jobSystem) {
app_tests_step_entities_jobs(memory, core, tests, deltaSeconds);
} else {
app_tests_step_entities_single_thread(memory, core, tests, deltaSeconds);
}
if (tests->lastVelocityError > 0.05f) {
LOG_WARNING("tests", "Velocity sum drift ({:.3f} vs expected {:.3f})",
tests->lastVelocitySum, tests->expectedSpeedSum);
}
if ((core->frameCounter % 120ull) == 0ull) {
TestEntity* first = (tests->entityCount > 0u) ? &tests->entities[0] : 0;
F32 x = first ? first->x : 0.0f;
F32 y = first ? first->y : 0.0f;
LOG_INFO("tests", "Frame {} | jobs={} | vSum={:.2f} | pos0=({:.2f}, {:.2f})",
core->frameCounter, tests->jobDispatchCount, tests->lastVelocitySum, x, y);
}
}
void app_tests_shutdown(AppMemory* memory, AppCoreState* core, AppTestsState* tests) {
(void) memory;
if (!core || !tests) {
return;
}
LOG_INFO("tests", "Shutdown (frames={} dispatches={} lastPos={:.2f})",
core->frameCounter, tests->jobDispatchCount, tests->lastPositionSum);
}
static void app_tests_step_entities_single_thread(AppMemory* memory, AppCoreState* core, AppTestsState* tests,
F32 deltaSeconds) {
(void) core;
if (!tests || tests->entityCount == 0u) {
tests->lastVelocitySum = 0.0f;
tests->lastVelocityError = tests->expectedSpeedSum;
tests->lastPositionSum = 0.0f;
return;
}
Arena* excludes[1] = {memory ? memory->programArena : 0};
Temp scratch = get_scratch(excludes, ARRAY_COUNT(excludes));
F32* snapshot = (tests->entityCount > 0u)
? ARENA_PUSH_ARRAY(scratch.arena, F32, tests->entityCount)
: NULL;
F64 positionSum = 0.0;
F32 speedSum = 0.0f;
for (U32 i = 0; i < tests->entityCount; ++i) {
TestEntity* entity = &tests->entities[i];
entity->x += entity->speed * deltaSeconds;
entity->y += 0.5f * deltaSeconds;
speedSum += entity->speed;
if (snapshot) {
snapshot[i] = entity->x;
}
positionSum += (F64) entity->x;
}
tests->lastVelocitySum = speedSum;
tests->lastVelocityError = ABS_F32(tests->expectedSpeedSum - speedSum);
tests->lastPositionSum = (F32) positionSum;
temp_end(&scratch);
}
static void app_tests_job_update(void* userData) {
EntityJobParams* params = (EntityJobParams*) userData;
if (!params || !params->tests || params->count == 0u) {
return;
}
AppTestsState* tests = params->tests;
if (params->sumOut) {
*params->sumOut = 0.0f;
}
Arena* excludes[1] = {params->programArena};
Temp scratch = get_scratch(excludes, ARRAY_COUNT(excludes));
F32* displacements = ARENA_PUSH_ARRAY(scratch.arena, F32, params->count);
F32 localSpeedSum = 0.0f;
for (U32 i = 0; i < params->count; ++i) {
U32 entityIndex = params->startIndex + i;
if (entityIndex >= tests->entityCount) {
break;
}
TestEntity* entity = &tests->entities[entityIndex];
F32 distance = entity->speed * params->deltaSeconds;
displacements[i] = distance;
entity->x += distance;
entity->y += 0.5f * params->deltaSeconds;
localSpeedSum += entity->speed;
}
if (params->sumOut) {
*params->sumOut = localSpeedSum;
}
temp_end(&scratch);
}
static void app_tests_step_entities_jobs(AppMemory* memory, AppCoreState* core, AppTestsState* tests,
F32 deltaSeconds) {
if (!memory || !core || !tests || !core->jobSystem || tests->entityCount == 0u) {
app_tests_step_entities_single_thread(memory, core, tests, deltaSeconds);
return;
}
U32 jobSlots = core->workerCount + 1u;
if (jobSlots > (APP_MAX_JOB_SLOTS + 1u)) {
jobSlots = APP_MAX_JOB_SLOTS + 1u;
}
if (jobSlots == 0u) {
jobSlots = 1u;
}
U32 chunkSize = (tests->entityCount + jobSlots - 1u) / jobSlots;
if (chunkSize == 0u) {
chunkSize = tests->entityCount;
}
Job rootJob = {};
rootJob.remainingJobs = 0;
U32 submittedJobs = 0u;
for (U32 jobIndex = 0; jobIndex < jobSlots; ++jobIndex) {
U32 start = jobIndex * chunkSize;
if (start >= tests->entityCount) {
break;
}
U32 count = tests->entityCount - start;
if (count > chunkSize) {
count = chunkSize;
}
tests->chunkSums[jobIndex] = 0.0f;
EntityJobParams params = {
.tests = tests,
.startIndex = start,
.count = count,
.deltaSeconds = deltaSeconds,
.programArena = memory ? memory->programArena : 0,
.sumOut = &tests->chunkSums[jobIndex],
};
job_system_submit((.function = app_tests_job_update, .parent = &rootJob), params);
submittedJobs += 1u;
}
if (submittedJobs > 0u) {
job_system_wait(core->jobSystem, &rootJob);
tests->jobDispatchCount += 1ull;
}
F32 totalSpeed = 0.0f;
for (U32 i = 0; i < submittedJobs; ++i) {
totalSpeed += tests->chunkSums[i];
}
tests->lastVelocitySum = totalSpeed;
tests->lastVelocityError = ABS_F32(tests->expectedSpeedSum - totalSpeed);
Arena* excludes[1] = {memory ? memory->programArena : 0};
Temp scratch = get_scratch(excludes, ARRAY_COUNT(excludes));
F32* snapshot = (tests->entityCount > 0u)
? ARENA_PUSH_ARRAY(scratch.arena, F32, tests->entityCount)
: NULL;
F64 positionSum = 0.0;
for (U32 i = 0; i < tests->entityCount; ++i) {
if (snapshot) {
snapshot[i] = tests->entities[i].x;
}
positionSum += (F64) tests->entities[i].x;
}
tests->lastPositionSum = (F32) positionSum;
temp_end(&scratch);
}