-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
5173 lines (4263 loc) · 166 KB
/
main.cpp
File metadata and controls
5173 lines (4263 loc) · 166 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
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <inttypes.h>
#include <memory.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <x86intrin.h>
#include <cpuid.h>
#include <pthread.h>
#include <sched.h>
#include <valgrind/callgrind.h>
//#include "gf_profiling.c"
#include "imgui.h"
#include "fb.h"
#define eps 1e-8f
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#define ARRSIZEi(x) ((int)(sizeof(x)/sizeof(x[0])))
uint64_t get_time_usec()
{
static struct timeval _time_stamp;
gettimeofday(&_time_stamp, nullptr);
return _time_stamp.tv_sec * 1000000 + _time_stamp.tv_usec;
}
uint64_t get_time_nsec() {
static struct timespec _time_stamp;
clock_gettime(CLOCK_MONOTONIC, &_time_stamp);
return _time_stamp.tv_sec * 1000000000 + _time_stamp.tv_nsec;
}
uint64_t rdtsc() {
uint32_t low, high;
__asm__ volatile("RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t"
"CPUID\n\t": "=r" (high), "=r" (low):: "%rax", "%rbx", "%rcx", "%rdx");
uint64_t res = high;
return (res<<32)|low;
//return __rdtsc();
//__asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
}
template<typename T> T inline min(const T& a, const T& b) {
return a < b ? a : b;
}
template<typename T> T inline max(const T& a, const T& b) {
return a > b ? a : b;
}
template<typename T> T inline lerp(const T& a, const T& b, const T& v) {
//return a + (b - a)*v;
// better precision if b and a are very different( e.g. 1e+8f - 2 = 1e+8f)
return a*(1 - v) + b*v;
}
template<typename T> T inline clamp(const T& a, const T& vmin, const T& vmax) {
return min(max(a, vmin), vmax);
}
template<typename T> T inline min3(T a, T b, T c) {
return min(a, min(b, c));
}
template<typename T> T inline max3(T a, T b, T c) {
return max(a, max(b, c));
}
template<typename T> void inline swap(T& a, T& b) {
T t = a;
a = b;
b = t;
}
#define cswap(a,b) do { if(a > b) { float tmp = a; a = b; b = tmp; } } while(0)
void sort3(int v[3]) {
cswap(v[0], v[1]);
cswap(v[1], v[2]);
cswap(v[0], v[1]);
}
#define arrsize(a) (sizeof(a)/sizeof(a[0]))
struct vec2 {
vec2(float xx, float yy):x(xx), y(yy) {}
vec2()/*:x(0), y(0)*/ {}
explicit vec2(float v):x(v), y(v) {}
float x,y;
};
vec2 operator*(const vec2& v, float x) {
return vec2(v.x*x, v.y*x);
}
vec2 operator*(float x, const vec2& v) {
return vec2(v.x*x, v.y*x);
}
vec2 operator*(const vec2& a, const vec2& b) {
return vec2(a.x*b.x, a.y*b.y);
}
vec2 operator+(const vec2& a, const vec2& b) {
return vec2(a.x + b.x, a.y + b.y);
}
vec2 operator-(const vec2& a, const vec2& b) {
return vec2(a.x - b.x, a.y - b.y);
}
vec2 floor(const vec2& v) {
return vec2(floor(v.x), floor(v.y));
}
vec2 fract(const vec2& v) {
return vec2(v.x - floor(v.x), v.y - floor(v.y));
}
float dot(const vec2& a, const vec2& b) {
return a.x*b.x + a.y*b.y;
}
float len(const vec2& v) {
return sqrtf(dot(v, v));
}
vec2 lerp(const vec2& a, const vec2& b, float v) {
return vec2(lerp(a.x, b.x, v), lerp(a.y, b.y, v));
}
//TODO:https://thebookofshaders.com/13/
float random(const vec2& st) {
float v = sinf(dot(st, vec2(12.9898f,78.233f))) * 43758.5453123f;
return v - floor(v);
}
struct vec3 {
vec3(float xx, float yy, float zz):x(xx), y(yy), z(zz) {}
vec3(float xx, float yy):x(xx), y(yy), z(0) {}
//vec3():x(0), y(0), z(0) {}
vec3(){}
explicit vec3(float v):x(v),y(v),z(v) {}
union {
struct {
float x,y,z;
};
float v[3];
};
operator float*() { return &x; }
float operator[](int i) const { assert(i>=0 && i<3); return v[i]; }
};
vec3 operator*(const vec3& v, float x) {
return vec3(v.x*x, v.y*x, v.z*x);
}
vec3 operator*(float x, const vec3& v) {
return vec3(v.x*x, v.y*x, v.z*x);
}
vec3 operator+(const vec3& a, const vec3& b) {
return vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}
vec3 operator-(const vec3& a, const vec3& b) {
return vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}
float dot(const vec3& a, const vec3& b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
vec3 cross(const vec3& a, const vec3& b) {
return vec3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
float len(const vec3& v) {
return sqrtf(dot(v, v));
}
vec3 normalize(const vec3& v) {
float len_sq = dot(v, v);
assert(len_sq > eps);
return (1.0f/sqrtf(len_sq)) * v;
}
vec3 gamma(vec3 v, float g) {
return vec3(pow(v.x, g), pow(v.y, g), pow(v.z, g));
}
struct vec4 {
vec4(float xx, float yy, float zz, float ww):x(xx), y(yy), z(zz), w(ww) {}
vec4(float xx, float yy, float zz):x(xx), y(yy), z(zz), w(0) {}
vec4(float xx, float yy):x(xx), y(yy), z(0), w(0) {}
vec4(vec3 v, float ww):x(v.x), y(v.y), z(v.z), w(ww) {}
vec4(vec3 v):x(v.x), y(v.y), z(v.z), w(0) {}
vec4(vec2 v):x(v.x), y(v.y), z(0), w(0) {}
explicit vec4(float v):x(v), y(v),z(v),w(v) {}
vec4()/*:x(0), y(0), z(0), w(0)*/ {}
//vec4& operator=(const vec4& o) = default;
//x = o.x;y=o.y;z=o.z;w=o.w;
//return *this;
//}
union {
struct {
float x,y,z,w;
};
float v[4];
};
vec3 xyz() const { return vec3(x, y, z); }
vec2 xy() const { return vec2(x, y); }
operator float*() { return &x; }
float operator[](int i) const { assert(i>=0 && i < 4); return v[i]; }
float& operator[](int i) { assert(i>=0 && i < 4); return v[i]; }
};
vec4 operator*(const vec4& v, float x) {
return vec4(v.x*x, v.y*x, v.z*x, v.w*x);
}
vec4 operator*(float x, const vec4& v) {
return vec4(v.x*x, v.y*x, v.z*x, v.w*x);
}
vec4 operator*(const vec4& a, const vec4& b) {
return vec4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
vec4 operator+(const vec4& a, const vec4& b) {
return vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
vec4 operator-(const vec4& a, const vec4& b) {
return vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
float dot(const vec4& a, const vec4& b) {
return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
}
float len(const vec4& v) {
return sqrtf(dot(v, v));
}
vec4 lerp(const vec4& a, const vec4 b, float t) {
return (b - a)*t + a;
}
vec4 normalize(const vec4& v) {
float len_sq = dot(v, v);
assert(len_sq > eps);
return (1.0f/sqrtf(len_sq)) * v;
}
vec4 floor(const vec4& v) {
return vec4(floor(v.x), floor(v.y), floor(v.z), floor(v.w));
}
vec4 fract(const vec4& v) {
return v - floor(v);
}
struct m44 {
union {
vec4 r[4];
float m[16];
};
m44() {}
m44(const m44& o) {
r[0] = o.r[0]; r[1] = o.r[1]; r[2] = o.r[2]; r[3] = o.r[3];
}
static m44 identity() {
m44 m;
memset(m.m, 0, sizeof(m));
m.r[0].x = 1;
m.r[1].y = 1;
m.r[2].z = 1;
m.r[3].w = 1;
return m;
}
m44& operator=(m44&& o) {
*this = o;
return *this;
}
m44& operator=(const m44& o) {
memcpy(m, o.m, sizeof(m));
return *this;
}
float operator[](int i) const { assert(i>=0 && i < 16); return m[i]; }
float& operator[](int i) { assert(i>=0 && i < 16); return m[i]; }
vec4 col(int i) const { return vec4(m[i], m[i + 4], m[i + 8], m[i + 12]); }
};
template <typename T>
struct Pt {
Pt(T x_, T y_):x(x_),y(y_) {}
Pt() {}
T x, y;
Pt<T>& operator+(const Pt<T>& other) {
x += other.x;
y += other.y;
return *this;
}
};
vec4 mul(const m44& m, const vec4 v) {
vec4 r;
r.x = dot(m.r[0], v);
r.y = dot(m.r[1], v);
r.z = dot(m.r[2], v);
r.w = dot(m.r[3], v);
return r;
}
// res <= a * b;
m44 mul(const m44& a, const m44 b) {
m44 m;
for(int j=0;j<4; j++) {
vec4 row_j = a.r[j];
for(int i=0;i<4; i++) {
vec4 col_i = b.col(i);
m.m[4*j + i] = dot(row_j, col_i);
}
}
return m;
}
m44 translation(const vec3& t) {
m44 r = m44::identity();
r.r[0][3] = t.x;
r.r[1][3] = t.y;
r.r[2][3] = t.z;
r.r[3][3] = 1.0f;
return r;
}
m44 transpose(const m44& m) {
m44 r;
r.r[0] = m.col(0);
r.r[1] = m.col(1);
r.r[2] = m.col(2);
r.r[3] = m.col(3);
return r;
}
m44 rotateXZ(const float angle_rad) {
float c,s;
sincosf(angle_rad, &s, &c);
m44 m;
m.r[0] = { c, 0, s, 0};
m.r[1] = { 0, 1, 0, 0};
m.r[2] = {-s, 0, c, 0};
m.r[3] = { 0, 0, 0, 1};
return m;
}
m44 get_rotation(m44 m) {
m.r[0][3] = 0;
m.r[1][3] = 0;
m.r[2][3] = 0;
m.r[3][3] = 1.0f;
return m;
}
// rotate about X, pitch - rotate about Y, yaw - rotate about Z
m44 rotateXYZ(float roll, float pitch, float yaw) {
float cx,sx; sincosf(roll, &sx, &cx);
float cy,sy; sincosf(pitch, &sy, &cy);
float cz,sz; sincosf(yaw, &sz, &cz);
m44 m;
m.r[0] = { cy*cz, sx*sy*cz - cx*sz, cx*sy*cz + sx*sz, 0};
m.r[1] = { cy*sz, sx*sy*sz + cx*cz, cx*sy*sz - sx*cz, 0};
m.r[2] = {-sy, sx*cy, cx*cy, 0};
m.r[3] = { 0, 0, 0, 1};
return m;
}
m44 mat_from_quat(const vec4 q) {
m44 m;
m.r[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
m.r[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
m.r[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
m.r[0][3] = 0.0f;
m.r[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
m.r[1][1]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
m.r[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
m.r[1][3] = 0.0f;
m.r[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
m.r[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
m.r[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
m.r[2][3] = 0.0f;
m.r[3][0] = 0.0f;
m.r[3][1] = 0.0f;
m.r[3][2] = 0.0f;
m.r[3][3] = 1.0f;
return m;
}
m44 make_proj(float left, float right, float top, float bottom, float near, float far) {
m44 m;
m.r[0] = { 2*near/(right - left), 0 , (right+left)/(right - left) , 0 };
m.r[1] = { 0 , 2*near/(top - bottom), (top + bottom )/(top - bottom) , 0 };
m.r[2] = { 0 , 0 , -(far + near)/(far - near) , -2*far*near/(far - near) };
m.r[3] = { 0 , 0 , -1 , 0 };
return m;
}
Pt<float> view_xy(float xp, float yp, float zv, const m44 mproj) {
Pt<float> pv;
pv.x = (xp - mproj.m[4*0 + 2] * zv) / mproj.m[4*0 + 0];
pv.y = (yp - mproj.m[4*1 + 2] * zv) / mproj.m[4*1 + 1];
return pv;
}
m44 make_frustum(float fov_y_deg, float aspect_w_by_h, float near, float far) {
const float deg2rad = M_PIf / 180.0f;
const float half_vert_fov = fov_y_deg / 2.0f;
float tangent = tan(half_vert_fov * deg2rad);
float half_top = near * tangent; // half height of near plane
float half_right = half_top * aspect_w_by_h; // half width of near plane
//
return make_proj(-half_right, half_right, -half_top, half_top, near, far);
#if 0
m44 m;
m.m[0] = near / half_right;
m.m[5] = far / half_top;
m.m[10] = -(near + far) / (far - near);
m.m[11] = -1;
m.m[14] = -(2 * far * near) / (far - near);
m.m[15] = 0;
return m;
#endif
}
// takes advantage knowing that it is a view matrix (normalized rotation + translation parts)
m44 view_invert(const m44& v) {
m44 t;
t.r[0] = vec4(v.col(0).xyz(), -v[3]);
t.r[1] = vec4(v.col(1).xyz(), -v[7]);
t.r[2] = vec4(v.col(2).xyz(), -v[11]);
t.r[3] = vec4(0,0,0,1);
return t;
}
vec3 view_get_world_pos(const m44& v) {
vec3 wpos;
vec3 tr = v.col(3).xyz();
wpos.x = -dot(v.col(0).xyz(), tr);
wpos.y = -dot(v.col(1).xyz(), tr);
wpos.z = -dot(v.col(2).xyz(), tr);
return wpos;
}
///////////////////////////////////////////////////////////////////////////////
m44 make_lookat(const vec3& eye, const vec3& target, const vec3& up_dir)
{
// -z direction because right handed coord system
vec3 fwd = normalize(eye - target);
vec3 left = cross(up_dir, fwd);
left = normalize(left);
// recompute the orthonormal up vector
vec3 up = cross(fwd, left);
m44 m = m44::identity();
// set rotation part, inverse rotation matrix: M^-1 = M^T for Euclidean transform
m[0] = left.x;
m[1] = left.y;
m[2] = left.z;
m[4] = up.x;
m[5] = up.y;
m[6] = up.z;
m[8] = fwd.x;
m[9] = fwd.y;
m[10]= fwd.z;
// set translation part
m[3]= -dot(left, eye);
m[7]= -dot(up, eye);
m[11]= -dot(fwd, eye);
return m;
}
typedef uint8_t u8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
// Based on Morgan McGuire @morgan3d https://www.shadertoy.com/view/4dS3Wd
float noise (vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (vec2(3.0f) - 2.0f * f);
return (b - a) * u.x + a +
(c - a) * u.y * (1.0f - u.x) +
(d - b) * u.x * u.y;
}
#define OCTAVES 6
float fbm (vec2 st) {
// Initial values
float value = 0.0;
float amplitude = .5;
//float frequency = 0.;
//
// Loop of octaves
for (int i = 0; i < OCTAVES; i++) {
value += amplitude * noise(st);
st = 2.0f*st;
amplitude *= .5f;
}
return value;
}
// sohgho.ca: https://www.songho.ca/opengl/files/trackball.c
// Given an axis and angle, compute quaternion.
vec4 axis_to_quat(const vec3 a, float phi) {
float s,c;
sincosf(phi/2.0f, &s, &c);
vec3 t = normalize(a);
return vec4(s * t.x, s * t.y, s * t.z, c);
}
/*
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
float tb_project_to_sphere(float r, float x, float y) {
float t, z;
const float d = sqrtf(x*x + y*y);
if (d < r * 0.70710678118654752440) { /* Inside sphere */
z = sqrtf(r*r - d*d);
} else { /* On hyperbola */
t = r / 1.41421356237309504880f;
z = t*t / d;
}
return z;
}
/*
* This size should really be based on the distance from the center of
* rotation to the point on the object underneath the mouse. That
* point would then track the mouse as closely as possible. This is a
* simple example, though, so that is left as an Exercise for the
* Programmer.
*/
#define TRACKBALLSIZE (0.8f)
/*
* Ok, simulate a track-ball. Project the points onto the virtual
* trackball, then figure out the axis of rotation, which is the cross
* product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
* Note: This is a deformed trackball-- is a trackball in the center,
* but is deformed into a hyperbolic sheet of rotation away from the
* center. This particular function was chosen after trying out
* several variations.
*
* It is assumed that the arguments to this routine are in the range
* (-1.0 ... 1.0)
*/
vec4 trackball(float p1x, float p1y, float p2x, float p2y) {
if (p1x == p2x && p1y == p2y) {
return vec4(0,0,0,1); // zero rotation
}
//figure out z-coordinates for projection of P1 and P2 to
// deformed sphere
vec3 p1 = vec3(p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
vec3 p2 = vec3(p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
// Now, we want the cross product of P1 and P2
vec3 axis = cross(p2,p1);
// Figure out how much to rotate around that axis.
float t = len(p1 - p2) / (2.0*TRACKBALLSIZE);
// Avoid problems with out-of-control values...
if (t > 1.0) t = 1.0f;
if (t < -1.0) t = -1.0f;
const float phi = 2.0f * asin(t);
return axis_to_quat(axis, phi);
}
typedef short FP16; // 8.8
// POD only
template<typename T, typename TSize = size_t>
class BufferT {
enum { elSize = sizeof(T) };
T* data_;
TSize size_;
TSize capacity_;
public:
BufferT():data_(nullptr), size_(0), capacity_(0) { }
void reset() {
size_ = 0;
}
bool resize(TSize new_cap, bool b_shrink = false) {
if(new_cap > capacity_ || (new_cap < capacity_ && b_shrink)) {
T* new_data = (T*)malloc(elSize*new_cap);
if(!new_data && new_cap!=0) {
return false;
}
capacity_ = new_cap;
size_ = new_cap < size_ ? new_cap : size_;
if(data_) {
if(size_) {
memcpy(new_data, data_, size_*elSize);
}
free(data_);
}
data_ = new_data;
}
return true;
}
const T& operator[](TSize i) const {
assert(i < size_);
return data_[i];
}
T& operator[](TSize i) {
assert(i < size_);
return data_[i];
}
TSize push(T el) {
if(size_ == capacity_) {
resize(capacity_ == 0 ? 16 : 2*capacity_);
}
data_[size_++] = el;
return size_ - 1;
}
void remove_swap(int i) {
assert(i < size_);
data_[i] = data_[size_-1];
size_--;
}
T& last() {
assert(size_);
return data_[size_ - 1];
}
T* data() { return data_; }
const T* data() const { return data_; }
TSize size() const { return size_; }
};
//////////////////////////////////////////////////////////////////////////
/// Str - string
struct Str {
const char* data;
ptrdiff_t len;
};
struct Cut {
Str head;
Str tail;
bool b_ok;
};
Str from_charptr(const char* ptr) {
return { ptr, (ptrdiff_t)(ptr ? strlen(ptr) + 1: 0) };
}
ptrdiff_t findleft(Str s, char c) {
ptrdiff_t i=0;
for(; i<s.len; ++i) {
if(s.data[i] == c) {
return i;
}
}
return i;
}
ptrdiff_t findright(Str s, char c) {
ptrdiff_t i=0;
for(i=s.len-1; i>=0; --i) {
if(s.data[i] == c) {
return i;
}
}
return i;
}
Str trimleft(Str s, char c) {
for(; s.len && *s.data == c; s.data++, s.len--) {}
return s;
}
Str trimright(Str s, char c) {
for(; s.len && s.data[s.len-1] == c; s.len--) {}
return s;
}
Str trimleft_le(Str s, char c) {
for(; s.len && *s.data <= c; s.data++, s.len--) {}
return s;
}
Str trimright_le(Str s, char c) {
for(; s.len && s.data[s.len-1] <= c; s.len--) {}
return s;
}
Str trim_le(Str s, char c) {
return trimright_le(trimleft_le(s, c), c);
}
Str substr(Str s, ptrdiff_t offset) {
ptrdiff_t off = min(offset, s.len);
s.data += off;
s.len -= off;
return s;
}
bool streq(Str a, Str b) {
return a.len == b.len && 0 == memcmp(a.data, b.data, a.len);
}
Cut cut(Str s, char c) {
Cut rv;
ptrdiff_t i = findleft(s, c);
rv.b_ok = i != s.len;
rv.head = { s.data, i };
rv.tail = substr(s, i + (rv.b_ok?1:0));
return rv;
}
Cut cutr(Str s, char c) {
Cut rv;
ptrdiff_t i = findright(s, c);
rv.b_ok = i != s.len;
rv.head = { s.data, i };
rv.tail = substr(s, i + (rv.b_ok?1:0));
return rv;
}
void serialize(const Str& s, FILE* fp) {
fwrite(&s.len, sizeof(s.len), 1, fp);
fwrite(s.data, s.len, 1, fp);
}
extern struct StrArena g_string_arena;
Str alloc_str_from_arena(struct StrArena& arena, const int len, char** strptr);
Str deserialize(FILE* fp) {
Str s;
fread(&s.len, sizeof(s.len), 1, fp);
char* strptr = 0;
s = alloc_str_from_arena(g_string_arena, s.len, &strptr);
fread(strptr, s.len, 1, fp);
return s;
}
char* print(char* buf, Str s) {
memcpy(buf, s.data, s.len);
buf[s.len] = '\0';
return buf;
}
void test_str() {
const char* t[] = {
" some string to test the stuff ",
"some test the stuff ",
" some string",
" frontspaces",
"endspaces ",
"lalala",
"hello, world",
"just,a,string, to , split ,me, in,parts ",
nullptr
};
int idx = 0;
char buf[128];
char srcbuf[128];
while(t[idx]) {
printf("Original: %s\n", t[idx]);
const ptrdiff_t len = strlen(t[idx]);
memcpy(srcbuf, t[idx], len);
Str s = { srcbuf, len };
Str tl = trimleft(s, ' ');
printf("tl:'%s'\n", print(buf, tl));
Str tr = trimright(tl, ' ');
printf("tr:'%s'\n", print(buf, tr));
Cut c;
c.tail = s;
c.b_ok = true;
while(c.b_ok) {
c = cut(trimleft(c.tail, ' '), ' ');
printf("head:%s\n", print(buf, c.head));
if(c.b_ok) {
printf("tail:%s\n", print(buf, c.tail));
}
}
c.tail = s;
c.b_ok = true;
while(c.b_ok) {
c = cut(trimleft(c.tail, ' '), ',');
printf("\thead:'%s'\n", print(buf, c.head));
if(c.b_ok) {
printf("\ttail:'%s'\n", print(buf, c.tail));
}
}
idx++;
}
}
struct StrArena {
char* mem;
int size;
int capacity;
} g_string_arena;
StrArena make_str_arena(int size) {
return { (char*)malloc(size), 0, size };
};
void destroy_str_arena(StrArena& a) {
free(a.mem);
a.size = a.capacity = 0;
}
Str alloc_str_from_arena(StrArena& arena, const int len, char** strptr) {
char* const arena_free = arena.mem + arena.size;
assert(arena.size + len < arena.capacity);
arena.size += len;
if(strptr) *strptr = arena_free;
return { arena_free, len };
}
// always allocates null terminated string
Str alloc_str_from_arena(StrArena& arena, const char* str) {
char* const arena_free = arena.mem + arena.size;
const int len = strlen(str) + 1;
assert(arena.size + len < arena.capacity);
memcpy(arena_free, str, len-1);
arena_free[len-1] = '\0';
Str s = { arena_free, len };
arena.size += len;
return s;
}
// always allocates null terminated string
Str alloc_str_from_arena(StrArena& arena, const char* str, int len) {
char* start = nullptr;
Str s = alloc_str_from_arena(arena, len, &start);
memcpy(start, str, len - 1);
start[len - 1] = '\0';
return s;
}
// always allocates null terminated string
Str alloc_concat_from_arena(StrArena& arena, Str a, Str b) {
char* pstr = nullptr;
Str s = alloc_str_from_arena(arena, a.len + b.len + 1, &pstr);
memcpy(pstr, a.data, a.len);
memcpy(pstr + a.len, b.data, b.len);
pstr[a.len + b.len] = '\0';
return s;
}
///////////////////////////////////////////////////////////////////////////
/// TGA Loader
// http://www.paulbourke.net/dataformats/tga/
#pragma pack(push, 1)
typedef struct {
char idlength;
char colourmaptype;
char datatypecode;
short int colourmaporigin;
short int colourmaplength;
char colourmapdepth;
short int x_origin;
short int y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
} TGA_HEADER;
#pragma pack(pop)
enum class eTGADataType:char {
kNoData = 0, // - No image data included.
kUncompressedColorMapped = 1, // - Uncompressed, color-mapped images.
kUncompressedRGB = 2, // - Uncompressed, RGB images.
kUncompressedBW = 3, // - Uncompressed, black and white images.
kRLEColorMapped = 9,// - Runlength encoded color-mapped images.
kRLERGB = 10, // - Runlength encoded RGB images.
kRLEBW = 11, // - Compressed, black and white images.
kCompressedColorMaped = 32, // - Compressed color-mapped data, using Huffman, Delta, and runlength encoding.
kCompressed4Pass = 33, // - Compressed color-mapped data, using Huffman, Delta, and runlength encoding. 4-pass quadtree-type process.
};
struct Image {
static void destroy(Image* img) {
free(img->data);
free(img);
}
struct Pixel {
u8 r, g, b, a;
u32 asU32() const {
return (u32)b | ((u32)g<<8) | ((u32)r<<16) | ((u32)a<<24);
}
vec4 asVec4() const {
return (1.0f/255.0f)*vec4(r, g, b, a);
}
};
u32 width, height, bpp;
Pixel* data;
Str filename_;
};
Image::Pixel tga_merge_bytes(const u8 *p, int bytes) {
Image::Pixel pix;
if (bytes == 4) {
pix.r = p[2];
pix.g = p[1];
pix.b = p[0];
pix.a = p[3];
} else if (bytes == 3) {
pix.r = p[2];
pix.g = p[1];
pix.b = p[0];
pix.a = 255;
} else if (bytes == 2) {
pix.r = (p[1] & 0x7c) << 1;
pix.g = ((p[1] & 0x03) << 6) | ((p[0] & 0xe0) >> 2);
pix.b = (p[0] & 0x1f) << 3;
pix.a = (p[1] & 0x80);
}
return pix;