-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugBody.h
705 lines (556 loc) · 19.7 KB
/
DebugBody.h
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#ifndef RR_DEBUG_BODY
#define RR_DEBUG_BODY
inline b32 ModRangeIntersect(u32 first1, u32 last1, u32 first2, u32 last2, u32 mod)
{
return ((first2 - first1) % mod) <= ((last1 - first1) % mod) || ((first1 - first2) % mod) <= ((last2 - first2) % mod);
}
inline void AdvanceMod(u32 *a, u32 mod) // todo can make fast version of this by using shift up, shift down if mod is a known power of 2
{
*a = ((*a + 1) % mod);
}
static void LoadDebugVariablesFile() //todo strings?
{
File *file = PushStruct(globalDebugState.arena, File);
*file = LoadFile("src/DebugVariables.txt", globalAlloc);
globalTweekers.tweekerFile = file;
}
#if 0
static String CreateSaneFileName(Char *fileName)
{
Assert(fileName);
Char *lastSanePlace = fileName;
Char *it = fileName;
while (*it)
{
if (*it == '\\')
{
while(*++it == '\\');
lastSanePlace = it;
}
it++;
}
return CreateString(lastSanePlace);
}
#endif
enum DebugUIElementType
{
DebugUIType_Graph,
DebugUItype_Tweekers
};
enum DebugUIElementFlag
{
DebugUIFlag_Closed = 0x0,
DebugUIFlag_Open = 0x1,
};
static void DrawTweekers(RenderGroup *rg)
{
Tweekable(f32, tweekerFontSize);
DebugUITweekerFile *stack = NULL;
For (globalTweekers.tweekers)
{
auto s = stack;
while (s)
{
// this should work as we get these of the string table and never change 'em.
if (s->function == it->function.data)
{
// todo maybe lex sorting here if we feel fancy
auto data = PushStruct(frameArena, TweekerRenderList);
data->next = s->first;
data->tweeker = it;
s->first = data;
break;
}
s = s->next;
}
if (!s)
{
auto data = PushStruct(frameArena, DebugUITweekerFile);
data->function = it->function.data;
data->next = stack;
stack = data;
data->first = PushStruct(frameArena, TweekerRenderList);
data->first->next = NULL;
data->first->tweeker = it;
}
}
u32 i = 1;
while (stack)
{
PushString(rg, V2(tweekerFontSize, i++ * 1.1f * tweekerFontSize), 0, (char *)stack->function, tweekerFontSize);
for (auto it = stack->first; it; it = it->next)
{
PushString(rg, V2(3.0f * tweekerFontSize, i++ * 1.1f * tweekerFontSize), 0, it->tweeker->name, tweekerFontSize);
}
stack = stack->next;
}
}
static void InitDebug()
{
DebugState *s = &globalDebugState;
RandomSeries series = GetRandomSeries();
for (u32 i = 0; i < debugRecordsAmount; i++)
{
debugInfoArray[i].color = RandomColorU32(&series);
}
globalTweekers.tweekers = TweekerCreateDynamicArray(globalAlloc);
s->lastFrameTime = 1.0f;
for (u32 i = 0; i < DEBUG_AMOUNT_OF_DEBUG_FRAMES; i++)
{
s->debugFrames[i] = PushArray(s->arena, FrameTimeInfo, debugRecordsAmount);
}
LoadDebugVariablesFile();
s->uiElements = PushArray(s->arena, DebugUIElement, 0);
//BuildStaticArray(s->arena, s->uiElements, )
}
// We want three informations
// 1: how long took a scope (including every child)
// 2: how long took a scope (excluding every child)
// 3: how long took a block (might be between two children of the scope) (more for drawing)
// last one gives the top two, so we will for now only compute that one
//this really does not do much now i guess...
static bool DebugHandleEvents(KeyStateMessage message, Input input)
{
if (message.key == Key_leftMouse && (message.flag & KeyState_PressedThisFrame) && PointInRectangle(V2(), 200, 40, input.mousePos))
{
globalDebugState.drawDebug = !globalDebugState.drawDebug;
return true;
}
return false;
}
static void CollectDebugRecords(f32 frameTime)
{
TimedBlock;
if (globalDebugState.paused) return;
DebugState *s = &globalDebugState;
u32 thisFrameIndex = s->rollingFrameIndex ? (s->rollingFrameIndex - 1) : (DEBUG_AMOUNT_OF_DEBUG_FRAMES - 1);
u32 amountOfEventsToHandle = globalDebugState.amountOfEventsLastFrame;
DebugEvent *firstEvent = globalDebugState.events[thisFrameIndex];
u64 startCycles = firstEvent->cycles;
s->lastFrameTime = frameTime;
auto frame = s->debugFrames[thisFrameIndex];
For(frame.times)
{
it->cycles = 0;
it->hitCount = 0;
}
DeferRestore(globalDebugState.arena);
Arena *arena = globalDebugState.arena;
for (DebugEvent *it = firstEvent; it < firstEvent + amountOfEventsToHandle; it++)
{
switch (it->type)
{
case DebugEvent_BeginTimedBlock:
{
*PushStruct(arena, DebugEvent) = *it;
}break;
case DebugEvent_EndTimedBlock:
{
DebugEvent e = PopStruct(arena, DebugEvent);
Assert(e.debugRecordIndex == it->debugRecordIndex);
u32 cycleD = it->cycles - e.cycles;
frame.times[it->debugRecordIndex].cycles += cycleD;
frame.times[it->debugRecordIndex].hitCount += e.hitMultiplicity;
}break;
default:
{
}break;
}
}
}
//todo make left bound render for strings
//todo maybe make it so that we can click on a frame and jump to it, by saving the gamestate. has a lot of issues I guess (rendering and such)... but may work
static void DrawDebugRecords(RenderGroup *rg, f32 secondsPerFrame, Input input)
{
TimedBlock;
Tweekable(u32, renderCycleThreshhold);
Tweekable(f32, debugDisplayBarSize, 0.03f);
DebugState *s = &globalDebugState;
if (!s->drawDebug) return;
// last frames stuff
u32 thisFrameIndex = s->rollingFrameIndex ? (s->rollingFrameIndex - 1) : (DEBUG_AMOUNT_OF_DEBUG_FRAMES - 1);
u32 amountOfEventsToHandle = globalDebugState.amountOfEventsLastFrame;
auto frameInfo = s->debugFrames[thisFrameIndex];
DebugEvent *firstEvent = globalDebugState.events[thisFrameIndex];
DebugEvent *lastEvent = amountOfEventsToHandle ? firstEvent + (amountOfEventsToHandle - 1) : firstEvent;
DeferRestore(globalDebugState.arena);
Arena *arena = globalDebugState.arena;
u32 startCycles = firstEvent->cycles;
u32 endCycles = lastEvent->cycles;
u32 deltaCycles = endCycles - startCycles;
f32 lastFrameTime = s->lastFrameTime;
f64 secondsPerCycle = deltaCycles ? (f64)lastFrameTime / (f64)deltaCycles : 0.0;
f32 amountOfTimeToDisplay = Max(4.0f *secondsPerFrame, 1.1f * lastFrameTime);
f64 amountOfCyclesToDisplay = amountOfTimeToDisplay / secondsPerCycle;
#if 0
FrameTimeInfoArray frameCopy = PushArray(frameArena, FrameTimeInfo, frameInfo.times.amount); // todo can we just sort frameTimeInfo?
memcpy(frameCopy.data, frameInfo.times.data, frameInfo.times.amount * sizeof(FrameTimeInfo));
#endif // todo sort
PushRectangle(rg, V2(0.1f, 0.2f), 0.75f, debugDisplayBarSize * (f32)frameInfo.times.amount, 2, V4(0.95f, 0.0f, 0.0f, 0.0f));
For(frameInfo.times)
{
u32 it_index = (u32)(it - frameInfo.times.data);
if (!debugInfoArray[it_index].function) continue;
String toDisplay = FormatString("%25lbc* %9rbf32ms, %2rbu32hit", debugInfoArray[it_index].function, (f32)((f64)it->cycles * secondsPerCycle * 1000.0), it->hitCount);
PushString(rg, V2(0.1f, 0.2f + it_index * debugDisplayBarSize), 0, toDisplay, debugDisplayBarSize);
}
f64 cycleMult = 1.0 / amountOfCyclesToDisplay;
for (u32 i = 0; i < 4; i++)
{
f32 percent = (f32)i * secondsPerFrame / amountOfTimeToDisplay;
PushLine(rg, V2(percent, 0.0f), V2(percent, 0.1f));
}
u32 depth = 0;
PushOrthogonalSetup(rg, ShaderFlags_None);
for (DebugEvent *it = firstEvent; it < firstEvent + amountOfEventsToHandle; it++)
{
switch (it->type)
{
case DebugEvent_BeginTimedBlock:
{
*PushStruct(arena, DebugEvent) = *it;
depth++;
}break;
case DebugEvent_EndTimedBlock:
{
DebugEvent e = PopStruct(arena, DebugEvent);
Assert(e.debugRecordIndex == it->debugRecordIndex);
u32 cycleD = it->cycles - e.cycles;
v2 pos = V2((f32)((e.cycles - startCycles) * cycleMult), (f32)(depth * debugDisplayBarSize));
f32 width = (f32)(cycleD * cycleMult);
u32 color = debugInfoArray[it->debugRecordIndex].color;
v2 p1 = pos;
v2 p2 = V2(pos.x + width, pos.y);
v2 p3 = V2(pos.x, pos.y + debugDisplayBarSize);
v2 p4 = V2(pos.x + width, pos.y + debugDisplayBarSize);
PushTriangle(rg, p1, p2, p3, 0, color);
PushTriangle(rg, p3, p2, p4, 0, color);
if (PointInRectangle(pos, width, debugDisplayBarSize, input.mouseZeroToOne))
{
PushOrthogonalSetup(rg, ShaderFlags_MultiTextured);
PushString(rg, input.mouseZeroToOne, -1, debugInfoArray[it->debugRecordIndex].function, debugDisplayBarSize);
PushOrthogonalSetup(rg, ShaderFlags_None);
}
depth--;
}break;
default:
{
}break;
}
}
}
static void ReportLoadDebugVariablesError(char *string, u32 lineNumber)
{
ConsoleOutputError("ExtractTweekerFromFile: Line : %u32. %c*", lineNumber, string);
}
// todo hex
// tweekers are of the form:
// name value
// u32 = 342432
// b32 = true
// v3 = (1, 23.4, 0.1)
// f32 = 1.2
// todo interpret everything we can't parse as strings? I think I want to do ""
// could save lineNumbers and such, but I think saveing will be "rare" so we are not to concerned with speed
static Tweeker ExtractTweekerFromFile(String toFind)
{
Assert(toFind.length);
File *file = globalTweekers.tweekerFile;
char *data = (char *)file->memory;
String inputString = CreateString(data);
//Sanitize(&inputString);
u32 lineNumber = 0;
while (inputString.length)
{
String head = ConsumeNextLine(&inputString); // could have like a eat all spaces version of sanitize and then every thing is a bit easier... thinking.
EatSpaces(&head);
if (!head.length) { lineNumber++; continue; }
String name = EatToNextSpaceReturnHead(&head);
if (!(name == toFind)) { lineNumber++; continue; }
EatNewLinesFromEnd(&head);
EatSpaces(&head);
if (!head.length)
{
ReportLoadDebugVariablesError("Value not found!", lineNumber++);
continue;
}
if (head[0] == '(') //vector
{
Eat1(&head);
EatSpaces(&head);
String val1 = EatToCharReturnHead(&head, ','); // for now it has to be 1.0, not 1.0 ,
b32 success = true;
f32 a1 = StoF(val1, &success);
if (!head.length)
{
ReportLoadDebugVariablesError("Expected ',' after first value!", lineNumber++);
continue;
}
Eat1(&head);
EatSpaces(&head);
String val2 = EatToCharReturnHead(&head, ',', ')');
f32 a2 = StoF(val2, &success);
if (!head.length)
{
ReportLoadDebugVariablesError("Expected ',' or ')' after second value!", lineNumber++);
continue;
}
if (head[0] == ')')
{
if (!success)
{
ReportLoadDebugVariablesError("Error reading values of v2!", lineNumber++);
continue;
}
Tweeker t;
t.type = Tweeker_v2;
t.vec2 = V2(a1, a2);
t.name = CopyString(name, globalDebugState.arena);
return t;
}
Eat1(&head);
EatSpaces(&head);
String val3 = EatToCharReturnHead(&head, ',', ')');
f32 a3 = StoF(val3, &success);
if (!head.length || !((head[0] == ',') | (head[0] == ')')))
{
ReportLoadDebugVariablesError("Expected ',' or ')' after first value!", lineNumber++);
continue;
}
if (head[0] == ')')
{
if (!success)
{
ReportLoadDebugVariablesError("Error reading values of v3!", lineNumber++);
continue;
}
Tweeker t;
t.type = Tweeker_v3;
t.vec3 = V3(a1, a2, a3);
t.name = CopyString(name, globalDebugState.arena);
return t;
}
Eat1(&head);
EatSpaces(&head);
String val4 = EatToCharReturnHead(&head, ')');
f32 a4 = StoF(val4, &success);
if (!head.length)
{
ReportLoadDebugVariablesError("Expected ',' or ')' after first value!", lineNumber++);
continue;
}
if (!success)
{
ReportLoadDebugVariablesError("Error reading values of v4!", lineNumber++);
continue;
}
Tweeker t;
t.type = Tweeker_v4;
t.vec4 = V4(a1, a2, a3, a4);
t.name = CopyString(name, globalDebugState.arena);
return t;
}
else
{
//non vector
b32 success = true;
EatSpacesFromEnd(&head);
u32 u = StoU(head, &success);
if (success)
{
Tweeker t;
t.type = Tweeker_u32;
t.u = u;
t.name = name;
t.name = CopyString(name, globalDebugState.arena);
return t;
}
success = true;
f32 f = StoF(head, &success);
if (success)
{
Tweeker t;
t.type = Tweeker_f32;
t.f = f;
t.name = name;
t.name = CopyString(name, globalDebugState.arena);
return t;
}
success = true;
b32 b = StoB(head, &success);
if (success)
{
Tweeker t;
t.type = Tweeker_b32;
t.b = b;
t.name = name;
t.name = CopyString(name, globalDebugState.arena);
return t;
}
//here string. probably with ""
ReportLoadDebugVariablesError("Could not read value", lineNumber++);
}
}
Tweeker error;
error.type = Tweeker_Invalid;
return error;
}
//todo maybe speed pass for all tweeker stuff, pretty janky for now.
static void *MaybeAddTweekerReturnValue(char *_name, TweekerType type, char *_function) // @scope speedup part3 minute 30
{
String name = S(_name);
String function = S(_function);
Tweeker *toTweek = GetTweeker(name);
if (!toTweek)
{
Tweeker t = ExtractTweekerFromFile(name);
if (t.type == Tweeker_Invalid)
{
t.type = type;
t.vec4 = V4();
t.name = name;
WriteSingleTweeker(t);
}
Assert(t.type == type); // right now we can't have multiple tweekers of the same name, and thats fine
t.name = name;
t.function = function;
u32 addedIndex = ArrayAdd(&globalTweekers.tweekers, t);
return (void *)(&(globalTweekers.tweekers.data + addedIndex)->u);
}
return (void *)(&toTweek->u);
}
static void *MaybeAddTweekerReturnValue(char *_name, TweekerType type, char *_function, TweekerValue value)
{
String name = S(_name);
String function = S(_function);
Tweeker *toTweek = GetTweeker(name);
if (!toTweek)
{
Tweeker t = ExtractTweekerFromFile(name);
if (t.type == Tweeker_Invalid)
{
t.type = type;
t.vec4 = value.vec4;
t.name = name;
WriteSingleTweeker(t);
}
Assert(t.type == type); // right now we can't have multiple tweekers of the same name, and thats fine
t.name = name;
t.function = function;
u32 addedIndex = ArrayAdd(&globalTweekers.tweekers, t);
return (void *)(&(globalTweekers.tweekers.data + addedIndex)->u);
}
return (void *)(&toTweek->u);
}
static void WriteSingleTweeker(Tweeker tweeker)
{
File *file = globalTweekers.tweekerFile;
char *data = (char *)file->memory;
String inputString = CreateString(data);
u32 initialLength = inputString.length;
//Sanitize(&inputString); should allready be sane
while (inputString.length)
{
u32 remainingLength = inputString.length;
String head = ConsumeNextLine(&inputString);
EatSpaces(&head);
if (!head.length) { continue; }
String name = EatToNextSpaceReturnHead(&head);
if (!(name == tweeker.name)) { continue; }
EatSpaces(&head);
String toWrite = TweekerToString(tweeker);
if (toWrite.length < head.length) // there should be endlines after every single one
{
u32 headLenghtSave = head.length;
CopyStringToString(toWrite, &head);
for (u32 i = head.length; i < headLenghtSave - 1; i++)
{
head.data[i] = ' ';
}
Assert(WriteEntireFile("src/DebugVariables.txt", *file));
ConsoleOutput("Saved to file.");
}
else
{
u32 headLength = initialLength - remainingLength;
u32 tailLength = inputString.length;
String toAdd = FormatString("%s %s\n", tweeker.name, TweekerToString(tweeker));
File newFile;
newFile.fileSize = (headLength + tailLength + toAdd.length)* sizeof(*toAdd.data);
newFile.memory = PushData(frameArena, u8, newFile.fileSize);
// source
u8 *headMem = (u8 *)file->memory;
u8 *tailMem = (u8 *)file->memory + (initialLength - inputString.length);
// dest
u8 *destHead = (u8 *)newFile.memory;
u8 *destInsertLine = (u8 *)newFile.memory + headLength * sizeof(*toAdd.data);
u8 *destTail = (u8 *)newFile.memory + headLength * sizeof(Char) + toAdd.length * sizeof(*toAdd.data);
memcpy(destHead, headMem, headLength * sizeof(Char));
memcpy(destInsertLine, toAdd.data, toAdd.length * sizeof(*toAdd.data));
memcpy(destTail, tailMem, tailLength * sizeof(Char));
Assert(WriteEntireFile("src/DebugVariables.txt", newFile));
//todo : hack
FreeFile(globalAlloc, *globalTweekers.tweekerFile);
*globalTweekers.tweekerFile = LoadFile("src/DebugVariables.txt", globalAlloc);
ConsoleOutput("Saved to file."); // todo somehow there no failing right now
}
return;
}
String toAdd = FormatString("%s %s\n", tweeker.name, TweekerToString(tweeker));
File newFile;
newFile.fileSize = file->fileSize + toAdd.length * sizeof(*toAdd.data);
newFile.memory = PushData(frameArena, u8, newFile.fileSize);
memcpy(newFile.memory, file->memory, file->fileSize);
memcpy((u8 *)newFile.memory + file->fileSize, toAdd.data, toAdd.length * sizeof(*toAdd.data));
Assert(WriteEntireFile("src/DebugVariables.txt", newFile));
//todo : hack
FreeFile(globalAlloc, *globalTweekers.tweekerFile);
*globalTweekers.tweekerFile = LoadFile("DebugVariables.txt", globalAlloc);
ConsoleOutputError("Saved to file."); // todo somehow there no failing right now
}
// only for hard writes. i.e kinda depricated....
static void WriteDebugVariables()
{
u8 *data = PushData(frameArena, u8, 0);
// todo : sorting by file and then lex add comments to make it pretty,
For(globalTweekers.tweekers)
{
CopyString(it->name, frameArena);
S(" ", frameArena);
TweekerToString(*it, frameArena);
S(" ", frameArena); //todo hack, true and false are differantly long. just for altering on the fly, maybe I should make it above so that we replace the line.
S('\n', frameArena);
}
S('\0'); // do not know if I need this, seemed to work without it, but that might just be because i call virtualAlloc... todo: test
File file = CreateFile(data, (u32)(frameArena->current - data));
WriteEntireFile("DebugVariables.txt", file);
//clearing back for the lulz
frameArena->current = data;
}
// welp this kind of allocator is really terrible I guess (fixedSizePool)
// this one is fine
#if 1
static void TestAllocator(BuddyAllocator *buddyAlloc)
{
RandomSeries series = { RandomSeed() };
const u32 sizeOfTest = 100;
u8 *allocated[sizeOfTest];
u32 numberOfAllocatedBytes = 0;
for (u32 i = 0; i < sizeOfTest; i++)
{
u32 a = (RandomU32(&series) % MegaBytes(3)) + 1;
allocated[i] = (u8 *)BuddyAlloc(buddyAlloc, a);
numberOfAllocatedBytes += a;
}
#if 1
for (u32 i = 0; i < 1000000000; i++)
{
u32 index = (RandomU32(&series) % sizeOfTest);
BuddyFree(buddyAlloc, allocated[index]);
u32 a = (RandomU32(&series) % MegaBytes(3)) + 1;
allocated[index] = (u8 *)BuddyAlloc(buddyAlloc, a);
}
#endif
}
#endif
#endif // !RR_DEBUG_BODY