summaryrefslogtreecommitdiff
path: root/src/swgfx.c
blob: 1901057b112c94eacb798fa752b815800cbf2678 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
/*
Matrices:
  - Column-major math convention.
  - Column-major memory storage.

Coordinate systems:
  - Right-handed.
  - NDC in [-1, +1].
  - Viewport goes up and to the right.
  - Window goes down and to the right.
  - (x,y) is the center of a pixel.
    - Top-left:     (x - 1/2, y - 1/2)
    - Bottom-right: (x + 1/2, y + 1/2)
*/
#include <swgfx.h>

#include <assert.h>
#include <math.h>   // sqrt
#include <stdint.h>
#include <string.h>

static constexpr sgTextureId DefaultTextureId = SWGFX_MAX_TEXTURES;
static constexpr size_t SWGFX_TEXTURE_REGISTER_SIZE = SWGFX_MAX_TEXTURES + 1;

static constexpr R DepthClearValue = 1.0f;

static constexpr sgVec3 Up3   = (sgVec3){0,1,0};
static constexpr sgVec3 Zero3 = (sgVec3){0,0,0};

typedef struct sgViewport_t { int x0, y0, width, height; } sgViewport_t;
typedef struct sgAABB2      { sgVec2 pmin, pmax; }         sgAABB2;

// Column-major math, column-major storage.
typedef struct sgMat4 {
  R val[4][4]; // (col, row)
} sgMat4;

typedef struct sgTexture {
  const sgImage*  image;
  sgTextureFilter filter;
} sgTexture;

typedef struct swgfx {
  sgVec2i       dims;      // Colour buffer dimensions.
  sgColour4*    colour;    // Colour buffer.
  R*            depth;     // Depth  buffer.
  sgTextureId*  texture;   // Texture ID buffer.
  sgVec2*       texcoords; // Texture coords buffer.
  sgColour3*    albedo;    // Albedo buffer.
  sgVec3*       normals;   // Normals buffer.
  sgViewport_t  viewport;
  sgMat4        model;     // Model matrix.
  sgMat4        view;      // View matrix.
  sgMat4        proj;      // Projection matrix.
  // Pre-multiplied matrices.
  // The model matrix changes once per object, more frequently than view or
  // projection. View and projection are expected to change infrequently, maybe
  // once per frame.
  // Make it so that changing the model matrix only requires one matrix
  // multiplication (mvp = model * viewProj) and not two (mvp = model * view * projection)
  // before rendering the model's triangles.
  sgMat4          viewProj;        // View-projection matrix.
  sgMat4          mvp;             // Model-view-projection matrix.
  sgTexture*      textureRegister; // Indexed by texture id.
  sgTextureId     activeTexture;
  sgTexel         defaultPixel;    // The single-pixel of the default texture.
  sgImage         defaultImage;    // Image for the default texture.
  sgCounters      counters;
} swgfx;

static inline int mod(int a, int m) { return (m + (a % m)) % m; }
static inline R   frac(R a) { return a - (R)((int)a); }
static inline int imin(int a, int b) { return (a <= b) ? a : b; }
static inline int imax(int a, int b) { return (a >= b) ? a : b; }

static inline R   rmin(R a, R b) { return (a <= b) ? a : b; }
static inline R   rmax(R a, R b) { return (a >= b) ? a : b; }
static inline R   lerp(R a, R b, R t) { return a + t*(b-a); }
static inline R   mod1(R a, R m) { return fmodf(1.f + fmodf(a, m), 1.f); }

static inline sgVec2i min2i(sgVec2i a, sgVec2i b) { return (sgVec2i){.x = imin(a.x, b.x), .y = imin(a.y, b.y) }; }
static inline sgVec2i max2i(sgVec2i a, sgVec2i b) { return (sgVec2i){.x = imax(a.x, b.x), .y = imax(a.y, b.y) }; }
static inline sgVec2  min2(sgVec2 a, sgVec2 b) { return (sgVec2){.x = rmin(a.x, b.x), .y = rmin(a.y, b.y) }; }
static inline sgVec2  max2(sgVec2 a, sgVec2 b) { return (sgVec2){.x = rmax(a.x, b.x), .y = rmax(a.y, b.y) }; }
static inline sgVec2  add2(sgVec2 a, sgVec2 b) { return (sgVec2){a.x + b.x, a.y + b.y}; }
static inline sgVec2  sub2(sgVec2 a, sgVec2 b) { return (sgVec2){a.x - b.x, a.y - b.y}; }
static inline sgVec2  scale2(sgVec2 v, R s) { return (sgVec2){v.x * s, v.y * s}; }
static inline sgVec2  frac2(sgVec2 v) { return (sgVec2){frac(v.x), frac(v.y)}; }
static inline sgVec2  lerp2(sgVec2 a, sgVec2 b, R t) { return add2(a, scale2(sub2(b,a), t)); }
static inline sgVec2  mod2(sgVec2 v, R m) { return (sgVec2){mod1(v.x, m), mod1(v.y, m)}; }

static inline sgVec3 max3(sgVec3 a, sgVec3 b) { return (sgVec3){fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z)}; }
static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; }
static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; }
static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; }
static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; }
static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; }
static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; }
static inline sgVec3  lerp3(sgVec3 a, sgVec3 b, R t) { return add3(a, scale3(sub3(b,a), t)); }
static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};}
static inline R dot3(sgVec3 a, sgVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static inline R normsq3(sgVec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; }
static inline R norm3  (sgVec3 v) { return (R)sqrt(normsq3(v)); }

static inline sgVec4 add4(sgVec4 a, sgVec4 b) { return (sgVec4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; }
static inline sgVec4 sub4(sgVec4 a, sgVec4 b) { return (sgVec4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; }
static inline sgVec4 scale4(sgVec4 v, R s) { return (sgVec4){v.x * s, v.y * s, v.z * s, v.w * s}; }
static inline sgVec4 lerp4(sgVec4 a, sgVec4 b, R t) {
  return (sgVec4){
    .x = a.x + t * (b.x - a.x),
    .y = a.y + t * (b.y - a.y),
    .z = a.z + t * (b.z - a.z),
    .w = a.w + t * (b.w - a.w)};
}

/// Return the curl of 'a' towards 'b', which is defined as the z-coordinate of
/// the cross product a x b, or as the determinant det(a,b).
///
/// The curl of 'a' towards 'b' is positive if 'a' curls towards 'b' like the
/// positive x-axis curls towards the positive y-axis.
static inline R curl2(sgVec2 a, sgVec2 b) {
  return (a.x * b.y) - (a.y * b.x);
}
static inline sgVec3 cross3(sgVec3 a, sgVec3 b) {
  return (sgVec3) {
    a.y * b.z - a.z * b.y,
    a.z * b.x - a.x * b.z,
    a.x * b.y - a.y * b.x};
}

static inline sgVec3 normalize3(sgVec3 v) {
  const R n = norm3(v);
  return (n > 0) ? (sgVec3){v.x / n, v.y / n, v.z / n} : (sgVec3){0, 0, 0};
}

static inline sgVec3 Vec3FromVec4(sgVec4 v)      { return (sgVec3){v.x, v.y, v.z}; }
static inline sgVec4 Vec4FromVec3(sgVec3 v, R w) { return (sgVec4){v.x, v.y, v.z, w}; }

static inline sgMat4 Mat4(
    R m00, R m01, R m02, R m03,   // v0.x v1.x v2.x v3.x
    R m10, R m11, R m12, R m13,   // v0.y v1.y v2.y v3.y
    R m20, R m21, R m22, R m23,   // v0.z v1.z v2.z v3.z
    R m30, R m31, R m32, R m33) { // v0.w v1.w v2.w v3.w
  return (sgMat4) {
    .val = {{m00, m10, m20, m30},   // col 0
            {m01, m11, m21, m31},   // col 1
            {m02, m12, m22, m32},   // col 2
            {m03, m13, m23, m33}}}; // col 3
}

static inline sgMat4 Mat4FromVec3(sgVec3 right, sgVec3 up, sgVec3 forward, sgVec3 position) {
  return Mat4(
      right.x, up.x, forward.x, position.x,
      right.y, up.y, forward.y, position.y,
      right.z, up.z, forward.z, position.z,
            0,    0,         0,          1);
}

static inline R Mat4At(sgMat4 m, int row, int col) { return m.val[col][row]; }
static inline sgVec3 Mat4v0(sgMat4 m) { return *((sgVec3*)m.val[0]); }
static inline sgVec3 Mat4v1(sgMat4 m) { return *((sgVec3*)m.val[1]); }
static inline sgVec3 Mat4v2(sgMat4 m) { return *((sgVec3*)m.val[2]); }
static inline sgVec3 Mat4v3(sgMat4 m) { return *((sgVec3*)m.val[3]); }

static inline sgMat4 Mat4Mul(sgMat4 A, sgMat4 B) {
  R m00 = Mat4At(A, 0, 0) * Mat4At(B, 0, 0) +
          Mat4At(A, 0, 1) * Mat4At(B, 1, 0) +
          Mat4At(A, 0, 2) * Mat4At(B, 2, 0) +
          Mat4At(A, 0, 3) * Mat4At(B, 3, 0);
  R m01 = Mat4At(A, 0, 0) * Mat4At(B, 0, 1) +
          Mat4At(A, 0, 1) * Mat4At(B, 1, 1) +
          Mat4At(A, 0, 2) * Mat4At(B, 2, 1) +
          Mat4At(A, 0, 3) * Mat4At(B, 3, 1);
  R m02 = Mat4At(A, 0, 0) * Mat4At(B, 0, 2) +
          Mat4At(A, 0, 1) * Mat4At(B, 1, 2) +
          Mat4At(A, 0, 2) * Mat4At(B, 2, 2) +
          Mat4At(A, 0, 3) * Mat4At(B, 3, 2);
  R m03 = Mat4At(A, 0, 0) * Mat4At(B, 0, 3) +
          Mat4At(A, 0, 1) * Mat4At(B, 1, 3) +
          Mat4At(A, 0, 2) * Mat4At(B, 2, 3) +
          Mat4At(A, 0, 3) * Mat4At(B, 3, 3);

  R m10 = Mat4At(A, 1, 0) * Mat4At(B, 0, 0) +
          Mat4At(A, 1, 1) * Mat4At(B, 1, 0) +
          Mat4At(A, 1, 2) * Mat4At(B, 2, 0) +
          Mat4At(A, 1, 3) * Mat4At(B, 3, 0);
  R m11 = Mat4At(A, 1, 0) * Mat4At(B, 0, 1) +
          Mat4At(A, 1, 1) * Mat4At(B, 1, 1) +
          Mat4At(A, 1, 2) * Mat4At(B, 2, 1) +
          Mat4At(A, 1, 3) * Mat4At(B, 3, 1);
  R m12 = Mat4At(A, 1, 0) * Mat4At(B, 0, 2) +
          Mat4At(A, 1, 1) * Mat4At(B, 1, 2) +
          Mat4At(A, 1, 2) * Mat4At(B, 2, 2) +
          Mat4At(A, 1, 3) * Mat4At(B, 3, 2);
  R m13 = Mat4At(A, 1, 0) * Mat4At(B, 0, 3) +
          Mat4At(A, 1, 1) * Mat4At(B, 1, 3) +
          Mat4At(A, 1, 2) * Mat4At(B, 2, 3) +
          Mat4At(A, 1, 3) * Mat4At(B, 3, 3);

  R m20 = Mat4At(A, 2, 0) * Mat4At(B, 0, 0) +
          Mat4At(A, 2, 1) * Mat4At(B, 1, 0) +
          Mat4At(A, 2, 2) * Mat4At(B, 2, 0) +
          Mat4At(A, 2, 3) * Mat4At(B, 3, 0);
  R m21 = Mat4At(A, 2, 0) * Mat4At(B, 0, 1) +
          Mat4At(A, 2, 1) * Mat4At(B, 1, 1) +
          Mat4At(A, 2, 2) * Mat4At(B, 2, 1) +
          Mat4At(A, 2, 3) * Mat4At(B, 3, 1);
  R m22 = Mat4At(A, 2, 0) * Mat4At(B, 0, 2) +
          Mat4At(A, 2, 1) * Mat4At(B, 1, 2) +
          Mat4At(A, 2, 2) * Mat4At(B, 2, 2) +
          Mat4At(A, 2, 3) * Mat4At(B, 3, 2);
  R m23 = Mat4At(A, 2, 0) * Mat4At(B, 0, 3) +
          Mat4At(A, 2, 1) * Mat4At(B, 1, 3) +
          Mat4At(A, 2, 2) * Mat4At(B, 2, 3) +
          Mat4At(A, 2, 3) * Mat4At(B, 3, 3);

  R m30 = Mat4At(A, 3, 0) * Mat4At(B, 0, 0) +
          Mat4At(A, 3, 1) * Mat4At(B, 1, 0) +
          Mat4At(A, 3, 2) * Mat4At(B, 2, 0) +
          Mat4At(A, 3, 3) * Mat4At(B, 3, 0);
  R m31 = Mat4At(A, 3, 0) * Mat4At(B, 0, 1) +
          Mat4At(A, 3, 1) * Mat4At(B, 1, 1) +
          Mat4At(A, 3, 2) * Mat4At(B, 2, 1) +
          Mat4At(A, 3, 3) * Mat4At(B, 3, 1);
  R m32 = Mat4At(A, 3, 0) * Mat4At(B, 0, 2) +
          Mat4At(A, 3, 1) * Mat4At(B, 1, 2) +
          Mat4At(A, 3, 2) * Mat4At(B, 2, 2) +
          Mat4At(A, 3, 3) * Mat4At(B, 3, 2);
  R m33 = Mat4At(A, 3, 0) * Mat4At(B, 0, 3) +
          Mat4At(A, 3, 1) * Mat4At(B, 1, 3) +
          Mat4At(A, 3, 2) * Mat4At(B, 2, 3) +
          Mat4At(A, 3, 3) * Mat4At(B, 3, 3);

  return Mat4(
    m00, m01, m02, m03,
    m10, m11, m12, m13,
    m20, m21, m22, m23,
    m30, m31, m32, m33);
}

static inline sgVec3 Mat4MulVec3(sgMat4 m, sgVec3 v, R w) {
  return (sgVec3) {
    .x = Mat4At(m, 0, 0) * v.x + Mat4At(m, 0, 1) * v.y + Mat4At(m, 0, 2) * v.z + Mat4At(m, 0, 3) * w,
    .y = Mat4At(m, 1, 0) * v.x + Mat4At(m, 1, 1) * v.y + Mat4At(m, 1, 2) * v.z + Mat4At(m, 1, 3) * w,
    .z = Mat4At(m, 2, 0) * v.x + Mat4At(m, 2, 1) * v.y + Mat4At(m, 2, 2) * v.z + Mat4At(m, 2, 3) * w};
}

static inline sgVec4 Mat4MulVec4(sgMat4 m, sgVec4 v) {
  sgVec4 u;
  u.x = Mat4At(m, 0, 0) * v.x + Mat4At(m, 0, 1) * v.y +
        Mat4At(m, 0, 2) * v.z + Mat4At(m, 0, 3) * v.w;
  u.y = Mat4At(m, 1, 0) * v.x + Mat4At(m, 1, 1) * v.y +
        Mat4At(m, 1, 2) * v.z + Mat4At(m, 1, 3) * v.w;
  u.z = Mat4At(m, 2, 0) * v.x + Mat4At(m, 2, 1) * v.y +
        Mat4At(m, 2, 2) * v.z + Mat4At(m, 2, 3) * v.w;
  u.w = Mat4At(m, 3, 0) * v.x + Mat4At(m, 3, 1) * v.y +
        Mat4At(m, 3, 2) * v.z + Mat4At(m, 3, 3) * v.w;
  return u;
}

static inline sgMat4 Mat4InverseTransform(sgMat4 m) {
  const sgVec3 r = Mat4v0(m);
  const sgVec3 u = Mat4v1(m);
  const sgVec3 f = Mat4v2(m);
  const sgVec3 t = Mat4v3(m);
  return Mat4(
      r.x, r.y, r.z, -dot3(r, t),
      u.x, u.y, u.z, -dot3(u, t),
      f.x, f.y, f.z, -dot3(f, t),
      0.f, 0.f, 0.f, 1.f);
}

static inline sgMat4 Mat4Look(sgVec3 position, sgVec3 forward, sgVec3 up) {
  const sgVec3 right = normalize3(cross3(forward, up));
  up                 = normalize3(cross3(right, forward));
  return Mat4FromVec3(right, up, neg3(forward), position);
}

static inline sgMat4 Mat4Perspective(R fovy, R aspect, R near, R far) {
  assert(fovy > 0.f);
  assert(near < far);
  const R f = 1.f / tanf(fovy / 2.f);
  const R a = near - far;
  return Mat4(
    f / aspect, 0,                 0,                   0,
    0,          f,                 0,                   0,
    0,          0,  (far + near) / a, (2 * far * near / a),
    0,          0,                -1,                   0);
}

static inline uint8_t ColourToUint(R x) {
  return (uint8_t)(x * 255.f);
}
static inline R UintToColour(uint8_t x) {
  return (R)x / 255.f;
}
static inline sgTexel Colour3ToTexel(sgColour3 c, R a) {
  return (sgTexel){ColourToUint(c.r), ColourToUint(c.g), ColourToUint(c.b), ColourToUint(a)};
}
static inline sgColour4 TexelToColour(sgTexel p) {
  return (sgColour4){UintToColour(p.r), UintToColour(p.g), UintToColour(p.b), UintToColour(p.a)};
}
static inline sgColour3 TexelToColour3(sgTexel p) {
  return (sgColour3){UintToColour(p.r), UintToColour(p.g), UintToColour(p.b)};
}

#ifndef _NDEBUG
static bool InBounds(int width, int height, int x, int y) {
  return (0 <= x) && (x < width) &&
         (0 <= y) && (y < height);
}
#endif // _NDEBUG

static inline sgColour4* Colour(swgfx* gfx, int x, int y) {
  assert(gfx);
  assert(gfx->colour);
  assert(InBounds(gfx->dims.x, gfx->dims.y, x, y));
  return gfx->colour + (y * gfx->dims.x) + x;
}

static inline const R* Depth(swgfx* gfx, int x, int y) {
  assert(gfx);
  assert(gfx->depth);
  assert(InBounds(gfx->dims.x, gfx->dims.y, x, y));
  return gfx->depth + (y * gfx->dims.x) + x;
}

static inline void SetPixelColour(swgfx* gfx, const sgVec2i p, sgColour4 colour) {
  assert(gfx);
  *Colour(gfx, p.x, p.y) = colour;
#if SWGFX_PROFILING
  gfx->counters.pixels++;
#endif // SWGFX_PROFILING
}

static inline void SetPixelDeferred(swgfx* gfx, const sgVec2i p, R depth, sgTextureId texid, sgVec2 uv, sgVec3 normal) {
  assert(gfx);
  const int i = (p.y * gfx->dims.x) + p.x;
  gfx->depth[i]     = depth;
  gfx->texture[i]   = texid;
  gfx->texcoords[i] = uv;
  gfx->normals[i]   = normalize3(normal); // Non-unit after interpolation.
#if SWGFX_PROFILING
  gfx->counters.pixels++;
#endif // SWGFX_PROFILING
}

static inline sgTexel ReadImage(const sgImage* image, sgVec2i xy) {
  assert(image);
  assert(image->pixels);
  assert(InBounds(image->width, image->height, xy.x, xy.y));
  return image->pixels[xy.y * image->width + xy.x];
}
// Output normalized to [0,1].
static inline sgColour4 ReadImageFloat(const sgImage* image, sgVec2i xy) {
  return TexelToColour(ReadImage(image, xy));
}

static inline sgVec2i UvToIndex(const sgImage* image, sgVec2 uv) {
  assert(image);
  return (sgVec2i){
    (int)(uv.x * (R)(image->width - 1)),
    (int)(uv.y * (R)(image->height - 1))};
}

static inline sgVec2i TextureRepeat(const sgImage* texture, sgVec2i p) {
  return (sgVec2i){mod(p.x, texture->width), mod(p.y, texture->height)};
}

static inline sgColour4 FilterNearest(const sgImage* image, sgVec2 uv) {
  assert(image);
  assert(image->pixels);
  const sgVec2i xy  = UvToIndex(image, uv);
  const sgVec2i xy2 = TextureRepeat(image, xy);
  return ReadImageFloat(image, xy2);
}

static inline sgColour4 FilterBilinear(const sgImage* image, sgVec2 uv) {
  assert(image);
  assert(image->pixels);
#define ADDR(x,y) TextureRepeat(image, (sgVec2i){x,y})
  const sgVec2 uv01 = mod2(uv, 1.f);
  // Find the closest grid vertex, then interpolate the 4 neighbouring pixel
  // centers.
  const sgVec2i tl = UvToIndex(image, uv01);
  const sgVec2i tr = ADDR(tl.x+1, tl.y);
  const sgVec2i bl = ADDR(tl.x,   tl.y+1);
  const sgVec2i br = ADDR(tl.x+1, tl.y+1);
  const sgVec2   t = frac2(uv01);
  const sgVec4 tl_pix = ReadImageFloat(image, tl);
  const sgVec4 tr_pix = ReadImageFloat(image, tr);
  const sgVec4 bl_pix = ReadImageFloat(image, bl);
  const sgVec4 br_pix = ReadImageFloat(image, br);
  const sgVec4 x1 = lerp4(tl_pix, tr_pix, t.x);
  const sgVec4 x2 = lerp4(bl_pix, br_pix, t.x);
  const sgVec4  y = lerp4(x1, x2, t.y);
  return y;
}

// TODO: Mipmapping.
// TODO: Clamping and other addressing strategies.
static inline sgColour4 Sample(const sgTexture* texture, sgVec2 uv) {
  assert(texture);
  switch (texture->filter) {
  case sgNearest: return FilterNearest(texture->image, uv);
  case sgBilinear: return FilterBilinear(texture->image, uv);
  default: assert(false); return (sgColour4){0};
  }
}

static inline sgAABB2 TriangleAabb2(sgVec2 p0, sgVec2 p1, sgVec2 p2) {
  return (sgAABB2){.pmin = min2(min2(p0, p1), p2),
                   .pmax = max2(max2(p0, p1), p2)};
}

static inline sgVec2i Clip(const swgfx* gfx, const sgVec2i p) {
  assert(gfx);
  constexpr sgVec2i lower = (sgVec2i){0,0};
  const     sgVec2i upper = (sgVec2i){gfx->viewport.width  - 1,
                                      gfx->viewport.height - 1};
  return max2i(lower, min2i(upper, p));
}

static inline R BarycentricInterp(sgVec3 bar, R a, R b, R c) {
  return bar.x*a + bar.y*b + bar.z*c;
}
static inline sgVec2 BarycentricInterp2(sgVec3 bar, sgVec2 a, sgVec2 b, sgVec2 c) {
  return add2(add2(scale2(a, bar.x), scale2(b, bar.y)), scale2(c, bar.z));
}

static inline R f(sgVec2 a, sgVec2 b, sgVec2 p) {
  return (a.y - b.y)*p.x + (b.x - a.x)*p.y + a.x*b.y - b.x*a.y;
}

static inline sgVec3 Barycentric(sgVec2 p0, sgVec2 p1, sgVec2 p2, sgVec2 p) {
  // There is no need to compute the third coordinate explicitly: a + b + c = 1.
  // But this results in a worse rasterization of the triangle along one of the edges.
  // It seems we can patch it with a small epsilon, though.
  // ---
  // Division by zero is only possible if the triangle has zero area.
  /*return (sgVec3){
    f(p1, p2, p) / f(p1, p2, p0),
    f(p2, p0, p) / f(p2, p0, p1),
    f(p0, p1, p) / f(p0, p1, p2)};*/
  const R b = f(p0, p2, p) / f(p0, p2, p1);
  const R c = f(p0, p1, p) / f(p0, p1, p2);
  const R a = /*f(p1, p2, p) / f(p1, p2, p0);*/1.f - b - c - (R)1e-7;
  return (sgVec3){a,b,c};
}

static void DrawTriangle2(swgfx* gfx, const sgTri2* const tri) {
  assert(gfx);
  assert(tri);
  const sgVec2 p0 = (sgVec2){tri->p0.pos.x, tri->p0.pos.y};
  const sgVec2 p1 = (sgVec2){tri->p1.pos.x, tri->p1.pos.y};
  const sgVec2 p2 = (sgVec2){tri->p2.pos.x, tri->p2.pos.y};
  const sgAABB2 bbox = TriangleAabb2(p0, p1, p2);
  // We consider (x,y) to be the pixel center.
  // Draw all pixels touched by the bounding box. TODO: Multi-sampling.
  sgVec2i pmin = (sgVec2i){(int)bbox.pmin.x, (int)bbox.pmin.y};
  sgVec2i pmax = (sgVec2i){(int)(bbox.pmax.x + 0.5f), (int)(bbox.pmax.y + 0.5f)};
  // Clip to screen space.
  pmin = Clip(gfx, pmin);
  pmax = Clip(gfx, pmax);
  const sgTexture* texture = &gfx->textureRegister[gfx->activeTexture];
  // Draw.
  for   (int y = pmin.y; y <= pmax.y; ++y) {
    for (int x = pmin.x; x <= pmax.x; ++x) {
      const sgVec2 p = (sgVec2){(R)x, (R)y};
      // TODO: there is an incremental optimization to computing barycentric coordinates;
      //  read more about it.
      const sgVec3 bar = Barycentric(p0, p1, p2, p);
      // We need to check the third coordinate.
      //   a + b + c = 1
      //   So, e.g., if a >= 0 and b >= 0, then we have c <= 1, but we could also have c <= 0.
      //   In the case c <= 0, then point is outside the triangle.
      if ((bar.x >= 0) && (bar.y >= 0) && (bar.z >= 0)) {
        assert((bar.x + bar.y + bar.z - 1e7) <= 1.f);
        const sgVec2 uv = BarycentricInterp2(bar, tri->p0.uv, tri->p1.uv, tri->p2.uv);
        const sgColour4 colour = Sample(texture, uv);
        SetPixelColour(gfx, (sgVec2i){x,y}, colour);
      }
    }
  }
#if SWGFX_PROFILING
  gfx->counters.triangles2++;
#endif // SWGFX_PROFILING
}

static inline sgVec4 PerspDivide(sgVec4 v) {
  return (sgVec4){v.x / v.w, v.y / v.w, v.z / v.w, v.w};
}

// TODO: Compute a viewport matrix in sgViewport() instead.
static inline sgVec4 ViewportTransform(sgViewport_t vp, sgVec4 ndc) {
  return (sgVec4){
    .x = (ndc.x+1.f) * ((R)vp.width/2.f)  + (R)vp.x0,
    .y = (ndc.y+1.f) * ((R)vp.height/2.f) + (R)vp.y0,
    .z = ndc.z*0.5f + 0.5f,
    .w = ndc.w};
}

static inline sgVec4 ViewportToWindow(sgViewport_t vp, sgVec4 p) {
  return (sgVec4){p.x, (R)vp.height - p.y, p.z, p.w};
}

/// Line segment-plane intersection special-case for the near camera plane.
/// All quantities assumed to be in camera space.
/// outP = a + outT*(b-a)
static inline R IntersectSegmentPlane(R near, const sgVec3* const a, const sgVec3* const b) {
  // D = near plane distance = perpendicular distance from the origin to the plane.
  // o = line origin = a
  // d = line direction = b-a
  // Plane normal = (0, 0, +1) --- Could be -1, need to be consistent with D.
  // Point in plane: p=(0, 0, -near)
  //   <=> p dot n     + D = 0
  //   === -near * n.z + D = 0
  //   === -near * 1   + D = 0
  //   === D = near
  // Denominator = n dot d = (0,0,1) dot d = d.z = (b.z - a.z)
  const R t = (-near - a->z) / (b->z - a->z);
  assert(t >= 0.f);
  assert(t <= 1.f);
  return t;
}

/// Interpolate vertex attributes at the in/out vertex 'out'.
static void InterpolateAttributes(const sgVert4* const a, const sgVert4* const b, R t, sgVert4* out) {
  assert(a);
  assert(b);
  assert(out);
  assert(t >= 0.f);
  assert(t <= 1.f);
  const sgVec4 d = sub4(b->pos, a->pos); // Line direction.
  out->pos    = add4(a->pos, scale4(d, t));
  out->uv     = lerp2(a->uv, b->uv, t);
  out->normal = lerp3(a->normal, b->normal, t);
}

/// Clip a triangle, vertices in clip space. Return the number of output
/// triangles.
///
/// 4 possible cases:
/// 1. All vertices in front of the camera near plane => draw.
/// 2. All vertices behind   => discard.
/// 3. One vertex in front   => draw 1 clipped triangle.
/// 4. Two vertices in front => draw 2 clipped triangles.
static inline int ClipTriangle(R near, const sgTri4* const tri, sgTri4 out[2]) {
#define VERTEX(IDX) (&tri->p0)[IDX]
#define VALID(X) ((0 <= (X)) && ((X) < 3))
#define IN_FRONT(P) (P.z >= -near) // +Z points into the screen in clip space.
  const bool f[3] = {IN_FRONT(tri->p0.pos), IN_FRONT(tri->p1.pos), IN_FRONT(tri->p2.pos)};
  const int numFront = f[0] + f[1] + f[2];
  int numTris;
  if (numFront == 3) {
    numTris = 1;
    out[0] = *tri;
  } else if (numFront == 2) {
    numTris = 2;
    int back = 0;
    for (; f[back] && (back < 3); ++back) {}
    assert(VALID(back));
    assert(!f[back]);
    int front[2] = {(back+1)%3, (back+2)%3};
    assert(VALID(front[0]));
    assert(VALID(front[1]));
    const sgVert4* const backVert = &VERTEX(back);
    sgVert4 p[2];
    for (int i = 0; i < 2; ++i) {
      const R t = IntersectSegmentPlane(near, (const sgVec3*)&backVert->pos, (const sgVec3*)&VERTEX(front[i]).pos);
      InterpolateAttributes(backVert, &VERTEX(front[i]), t, &p[i]);
    }
    // We must preserve the winding order here for culling.
    // Note that p[i] corresponds to front[i] = back+(i+1).
    out[0] = (sgTri4){p[1], p[0], VERTEX(front[1])};
    out[1] = (sgTri4){p[0], VERTEX(front[0]), VERTEX(front[1])};
  } else if (numFront == 1) {
    numTris = 1;
    int front = 0;
    for (; !f[front] && (front < 3); ++front){}
    assert(VALID(front));
    assert(f[front]);
    int back[2] = {(front+1)%3, (front+2)%3};
    assert(VALID(back[0]));
    assert(VALID(back[1]));
    const sgVert4* const frontVert = &VERTEX(front);
    sgVert4 p[2];
    for (int i = 0; i < 2; ++i) {
      const R t = IntersectSegmentPlane(near, (const sgVec3*)&frontVert->pos, (const sgVec3*)&VERTEX(back[i]).pos);
      InterpolateAttributes(frontVert, &VERTEX(back[i]), t, &p[i]);
    }
    // We must preserve the winding order here for culling.
    // Note that p[i] corresponds to back[i] = front+(i+1).
    out[0] = (sgTri4){*frontVert, p[0], p[1]};
  } else {
    numTris = 0;
  }
  return numTris;
#undef IN_FRONT
#undef VALID
#undef VERTEX
}

static inline int TransformTri(const swgfx* gfx, const sgTri3* const tri, sgTri4 out[2]) {
  assert(gfx);
  assert(tri);
  // Model to clip space.
  const sgVec4 p0_clip = Mat4MulVec4(gfx->mvp, Vec4FromVec3(tri->p0.pos, 1));
  const sgVec4 p1_clip = Mat4MulVec4(gfx->mvp, Vec4FromVec3(tri->p1.pos, 1));
  const sgVec4 p2_clip = Mat4MulVec4(gfx->mvp, Vec4FromVec3(tri->p2.pos, 1));
  // Model to world space for normals.
  // This assumes the model matrix does not have non-uniform scaling.
  // It seems more convenient to put the normals in world space instead of view
  // space right now so that we don't have to transform lights to view space
  // when lighting. But we might want to revisit this later.
  const sgVec3 n0_view = Mat4MulVec3(gfx->model, tri->p0.normal, 0.f);
  const sgVec3 n1_view = Mat4MulVec3(gfx->model, tri->p1.normal, 0.f);
  const sgVec3 n2_view = Mat4MulVec3(gfx->model, tri->p2.normal, 0.f);
  const sgTri4 tri_clip = {
    (sgVert4){ p0_clip, tri->p0.uv, n0_view },
    (sgVert4){ p1_clip, tri->p1.uv, n1_view },
    (sgVert4){ p2_clip, tri->p2.uv, n2_view }};
  // Clip.
  // Our perspective matrix maps the near plane to z=-1 in clip space.
  constexpr R near_clip = -1.f;
  const int numTris = ClipTriangle(near_clip, &tri_clip, out);
  assert((0 <= numTris) && (numTris <= 2));
  for (int i = 0; i < numTris; ++i) {
    sgTri4* const tri4 = &out[i];
    // Perspective divide.
    const sgVec4 p0_ndc = PerspDivide(tri4->p0.pos);
    const sgVec4 p1_ndc = PerspDivide(tri4->p1.pos);
    const sgVec4 p2_ndc = PerspDivide(tri4->p2.pos);
    // To viewport.
    const sgVec4 p0_vp = ViewportTransform(gfx->viewport, p0_ndc);
    const sgVec4 p1_vp = ViewportTransform(gfx->viewport, p1_ndc);
    const sgVec4 p2_vp = ViewportTransform(gfx->viewport, p2_ndc);
    // To window.
    const sgVec4 p0_wn = ViewportToWindow(gfx->viewport, p0_vp);
    const sgVec4 p1_wn = ViewportToWindow(gfx->viewport, p1_vp);
    const sgVec4 p2_wn = ViewportToWindow(gfx->viewport, p2_vp);
    // Output.
    tri4->p0.pos = p0_wn;
    tri4->p1.pos = p1_wn;
    tri4->p2.pos = p2_wn;
  }
  return numTris;
}

static void DrawTriangle3PostClip(swgfx* gfx, const sgTri4* const tri) {
  assert(gfx);
  assert(tri);
  const sgVec4 p0 = tri->p0.pos;
  const sgVec4 p1 = tri->p1.pos;
  const sgVec4 p2 = tri->p2.pos;
  const sgVec2 p0_2d = (sgVec2){p0.x, p0.y};
  const sgVec2 p1_2d = (sgVec2){p1.x, p1.y};
  const sgVec2 p2_2d = (sgVec2){p2.x, p2.y};
  // Backface culling, assume front face = ccw.
  // In screen space, +Y goes down.
  // p0p1p2 is ccw <=> p0p1 curls negatively towards p0p2. If the curl is
  // positive (cw winding), cull.
  if (curl2(sub2(p1_2d, p0_2d),
            sub2(p2_2d, p0_2d)) > 0.f) {
    return;
  }
  const sgAABB2 bbox = TriangleAabb2(p0_2d, p1_2d, p2_2d);
  // We consider (x,y) to be the pixel center.
  // Draw all pixels touched by the bounding box. TODO: Multi-sampling.
  sgVec2i pmin = (sgVec2i){(int)bbox.pmin.x, (int)bbox.pmin.y};
  sgVec2i pmax = (sgVec2i){(int)(bbox.pmax.x + 0.5f), (int)(bbox.pmax.y + 0.5f)};
  // Clip to screen space.
  pmin = Clip(gfx, pmin);
  pmax = Clip(gfx, pmax);
  // Setup for perspective texture mapping.
  // 'w' is view-space z.
  const sgVec3 depths      = (sgVec3){p0.z, p1.z, p2.z};
  const sgVec3 one_over_zs = (sgVec3){1.f / p0.w, 1.f / p1.w, 1.f/ p2.w};
  const sgVec3 u_over_zs   = (sgVec3){tri->p0.uv.x / p0.w, tri->p1.uv.x / p1.w, tri->p2.uv.x / p2.w};
  const sgVec3 v_over_zs   = (sgVec3){tri->p0.uv.y / p0.w, tri->p1.uv.y / p1.w, tri->p2.uv.y / p2.w};
  // Draw.
  for   (int y = pmin.y; y <= pmax.y; ++y) {
    for (int x = pmin.x; x <= pmax.x; ++x) {
      const sgVec2 p = (sgVec2){(R)x, (R)y};
      // TODO: there is an incremental optimization to computing barycentric coordinates;
      //  read more about it.
      const sgVec3 bar = Barycentric(p0_2d, p1_2d, p2_2d, p);
      // We need to check the third coordinate.
      //   a + b + c = 1
      //   So, e.g., if a >= 0 and b >= 0, then we have c <= 1, but we could also have c <= 0.
      //   In the case c <= 0, then point is outside the triangle.
      if ((bar.x >= 0) && (bar.y >= 0) && (bar.z >= 0)) {
        assert((bar.x + bar.y + bar.z - 1e7) <= 1.f);
        const R p_one_over_z = dot3(bar, one_over_zs);
        const R p_depth      = dot3(bar, depths);
        const R z            = 1.f / p_one_over_z;
        const R* depth = Depth(gfx, x, y);
        if ((0.f <= p_depth) && (p_depth <= 1.f) && (p_depth <= *depth)) {
          const R p_u_over_z  = dot3(bar, u_over_zs);
          const R p_v_over_z  = dot3(bar, v_over_zs);
          const sgVec2 uv     = (sgVec2){p_u_over_z * z, p_v_over_z * z};
          const sgVec3 n0     = scale3(tri->p0.normal, bar.x * one_over_zs.x);
          const sgVec3 n1     = scale3(tri->p1.normal, bar.y * one_over_zs.y);
          const sgVec3 n2     = scale3(tri->p2.normal, bar.z * one_over_zs.z);
          const sgVec3 normal = add3(add3(n0,n1),n2);
          //const sgPixel colour = {(uint8_t)(bar.x*255.f), (uint8_t)(bar.y*255.f), (uint8_t)(bar.z*255.f), 255};
          //const sgPixel colour = {(int)(z*255.f), (int)(z*255.f), (int)(z*255.f), 255};
          //const sgPixel colour = {255, 0, 255, 255};
          //const sgPixel colour = {(int)(uv.x * 255.f), (int)(uv.y * 255.f), 255, 255};
          SetPixelDeferred(gfx, (sgVec2i){x,y}, p_depth, gfx->activeTexture, uv, normal);
        }
      }
    }
  }
}

static void DrawTriangle3(swgfx* gfx, const sgTri3* const tri) {
  assert(gfx);
  assert(tri);
  sgTri4 tris[2];
  const int numTris = TransformTri(gfx, tri, tris);
  assert((0 <= numTris) && (numTris <= 2));
  for (int i = 0; i < numTris; ++i) {
    DrawTriangle3PostClip(gfx, &tris[i]);
  }
#if SWGFX_PROFILING
  gfx->counters.triangles3++;
#endif // SWGFX_PROFILING
}

#define is_pow2_or_0(X) ((X & (X - 1)) == 0)
#define SG_ALIGN 64
#define SG_ALLOC(PP_MEM, COUNT, TYPE) (TYPE*)Alloc(PP_MEM, COUNT, sizeof(TYPE))

static void* AlignPtr(void* address) {
  assert(is_pow2_or_0(SG_ALIGN));
  constexpr size_t mask = SG_ALIGN - 1;
  return (void*)(((uintptr_t)address + mask) & ~mask);
}

static size_t Align(size_t size) {
  static_assert(is_pow2_or_0(SG_ALIGN));
  constexpr size_t mask = SG_ALIGN - 1;
  return (size + mask) & (~mask);
}

static void* Alloc(void** ppMem, size_t count, size_t size) {
  assert(ppMem);
  assert(*ppMem);
  assert(*ppMem == AlignPtr(*ppMem)); // Should already be aligned.
  const size_t total = Align(count * size);
  void* ptr = *ppMem;
  *ppMem = ptr + total;
  memset(ptr, 0, total);
  return ptr;
}

size_t sgMem(int width, int height) {
  const int N = width * height;
  return Align(sizeof(swgfx)) +
         Align(N * sizeof(sgColour4)) +   // Colour buffer.
         Align(N * sizeof(R)) +           // Depth buffer.
         Align(N * sizeof(sgTextureId)) + // Texture ID buffer.
         Align(N * sizeof(sgVec2)) +      // Texture coords buffer.
         Align(N * sizeof(sgColour3)) +   // Albedo buffer.
         Align(N * sizeof(sgVec3)) +      // Normals buffer.
         Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register.
         (SG_ALIGN - 1); // To make room to align allocations within the buffer.
}

swgfx* sgNew(int width, int height, void* mem) {
  const int N = width * height;
  void* aligned  = AlignPtr(mem); // Uses the extra room we made in sgMem().
  swgfx* gfx     = SG_ALLOC(&aligned, 1, swgfx);
  gfx->dims      = (sgVec2i){width, height};
  gfx->colour    = SG_ALLOC(&aligned, N, sgColour4);
  gfx->depth     = SG_ALLOC(&aligned, N, R);
  gfx->texture   = SG_ALLOC(&aligned, N, sgTextureId);
  gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2);
  gfx->albedo    = SG_ALLOC(&aligned, N, sgColour3);
  gfx->normals   = SG_ALLOC(&aligned, N, sgVec3);
  gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture);
  gfx->activeTexture = DefaultTextureId;
  gfx->defaultPixel  = (sgPixel){255, 255, 255, 255};
  gfx->defaultImage = (sgImage){
    .width  = 1,
    .height = 1,
    .pixels = &gfx->defaultPixel,
  };
  gfx->textureRegister[DefaultTextureId] = (sgTexture){
    .image  = &gfx->defaultImage,
    .filter = sgNearest,
  };
  return gfx;
}

void sgDel(swgfx** ppSwgfx) {
  assert(ppSwgfx);
  if (*ppSwgfx) {
    *ppSwgfx = nullptr;
  }
}

sgColour4* sgColourBuffer(swgfx* gfx) {
  assert(gfx);
  return gfx->colour;
}

void sgPresent(swgfx* gfx, sgVec2i dimensions, sgScreenPixel* screen) {
  assert(gfx);
  assert(screen);
  // Integer scaling only.
  assert((dimensions.x % gfx->dims.x) == 0);
  assert((dimensions.y % gfx->dims.y) == 0);

  const int sx = dimensions.x / gfx->dims.x;
  const int sy = dimensions.y / gfx->dims.y;

  const sgColour4* src = gfx->colour;

  for (int y = 0; y < gfx->dims.y; ++y, src += gfx->dims.x) {
    // Replicate each row 'sy' times.
    for (int yy = 0; yy < sy; ++yy) {
      const sgColour4* src_col = src;
      for (int x = 0; x < gfx->dims.x; ++x, ++src_col) {
        // Replicate each column 'sx' times.
        for (int xx = 0; xx < sx; ++xx, ++screen) {
          screen->r = ColourToUint(src_col->r);
          screen->g = ColourToUint(src_col->g);
          screen->b = ColourToUint(src_col->b);
          screen->a = ColourToUint(src_col->a);
        }
      }
    }
  }

#if SWGFX_PROFILING
  gfx->counters.frames++;
#endif // SWGFX_PROFILING
}

static void sgUpdateViewProjection(swgfx* gfx) {
  assert(gfx);
  gfx->viewProj = Mat4Mul(gfx->proj, gfx->view);
}

static void sgUpdateMvp(swgfx* gfx) {
  assert(gfx);
  gfx->mvp = Mat4Mul(gfx->viewProj, gfx->model);
}

void sgModelId(swgfx* gfx) {
  assert(gfx);
  sgModel(gfx,
    (sgVec3){0,0,0},
    (sgVec3){1, 0, 0},
    (sgVec3){0, 1, 0},
    (sgVec3){0, 0, 1});
}

void sgModel(swgfx* gfx, sgVec3 position, sgVec3 right, sgVec3 up, sgVec3 forward) {
  assert(gfx);
  gfx->model = Mat4FromVec3(right, up, forward, position);
  sgUpdateMvp(gfx);
}

void sgView(swgfx* gfx, sgVec3 position, sgVec3 forward) {
  assert(gfx);
  const sgMat4 camera = Mat4Look(position, forward, Up3);
  gfx->view = Mat4InverseTransform(camera);
  sgUpdateViewProjection(gfx);
  sgUpdateMvp(gfx);
}

void sgPerspective(swgfx* gfx, R fovy, R aspect, R near, R far) {
  assert(gfx);
  gfx->proj = Mat4Perspective(fovy, aspect, near, far);
  sgUpdateViewProjection(gfx);
  sgUpdateMvp(gfx);
}

void sgViewport(swgfx* gfx, int x0, int y0, int width, int height) {
  assert(gfx);
  assert(x0 >= 0);
  assert(y0 >= 0);
  assert((x0 + width)  <= gfx->dims.x);
  assert((y0 + height) <= gfx->dims.y);
  gfx->viewport = (sgViewport_t){x0, y0, width, height};
}

void sgTextureRegister(swgfx* gfx, sgTextureId id, const sgImage* image, sgTextureFilter filter) {
  assert(gfx);
  assert(id < SWGFX_MAX_TEXTURES);
  assert(id != DefaultTextureId);
  assert(image);
  gfx->textureRegister[id] = (sgTexture){image, filter};
}

void sgTextureActivate(swgfx* gfx, sgTextureId id) {
  assert(gfx);
  assert(id < SWGFX_MAX_TEXTURES);
  gfx->activeTexture = id;
}

void sgClear(swgfx* gfx) {
  assert(gfx);
  const int N = gfx->dims.x * gfx->dims.y;
  memset(gfx->colour, 0, N * sizeof(*gfx->colour));
  for (int i = 0; i < N; ++i) {
      gfx->depth[i] = DepthClearValue;
  }
}

void sgPixels(swgfx* gfx, size_t count, const sgVec2i* positions, sgColour4 colour) {
  assert(gfx);
  for (size_t i = 0; i < count; ++i) {
    SetPixelColour(gfx, positions[i], colour);
  }
}

// TODO: DrawTriangle3 with clipping. Leave DrawTriangle2 to not clip for
//       performance; assume that 2D triangles are within bounds.
// TODO: If the triangle is out of bounds, skip entirely.
// TODO: Otherwise, rasterize the triangle the simple way and check whether each
//       individual pixel is within bounds; do not explicitly clip the triangle.
// TODO: Actually, I think we can just clip the triangle's AABB and then walk
//       over those pixels instead of checking every individual pixel in the
//       non-clipped AABB. Edit: I think this doesn't work; draw it and you'll
//       see. Some pixels that should be rasterized will fall out of the clipped
//       AABB.

void sgTriangles2(swgfx* gfx, size_t count, const sgTri2* tris) {
  assert(gfx);
  for (size_t i = 0; i < count; ++i) {
    DrawTriangle2(gfx, &tris[i]);
  }
}

void sgTriangles(swgfx* gfx, size_t count, const sgTri3* tris, const sgNormal*) {
  assert(gfx);
  assert(tris);
  for (size_t i = 0; i < count; ++i) {
    const sgTri3* tri = &tris[i];
    DrawTriangle3(gfx, tri);
  }
}

void sgTrianglesIndexed(swgfx* gfx, size_t numIndices, const sgIdx* indices, const sgVec3* positions, const sgVec2* texcoords, const sgVec3* normals) {
  assert(gfx);
  assert(indices);
  assert(positions);
  assert(texcoords);
  assert(normals);
  for (size_t i = 0; i < numIndices; i+=3) {
    const sgIdx i0 = indices[i];
    const sgIdx i1 = indices[i+1];
    const sgIdx i2 = indices[i+2];
    const sgVec3 p0 = positions[i0];
    const sgVec3 p1 = positions[i1];
    const sgVec3 p2 = positions[i2];
    const sgVec2 uv0 = texcoords[i0];
    const sgVec2 uv1 = texcoords[i1];
    const sgVec2 uv2 = texcoords[i2];
    const sgVec3 n0  = normals[i0];
    const sgVec3 n1  = normals[i1];
    const sgVec3 n2  = normals[i2];
    const sgTri3 tri = (sgTri3){
      (sgVert3){p0, uv0, n0},
      (sgVert3){p1, uv1, n1},
      (sgVert3){p2, uv2, n2}};
    DrawTriangle3(gfx, &tri);
  }
}

void sgTrianglesIndexedNonUniform(swgfx* gfx, size_t numTris, const sgTriIdx* tris, const sgVec3* positions, const sgVec2* texcoords, const sgVec3* normals) {
  assert(gfx);
  assert(tris);
  assert(positions);
  assert(texcoords);
  assert(normals);
  for (size_t t = 0; t < numTris; ++t) {
    const sgTriIdx* triIdx = &tris[t];
    const sgTri3 tri = (sgTri3){
      (sgVert3){positions[triIdx->v0.pos], texcoords[triIdx->v0.uv], normals[triIdx->v0.normal]},
      (sgVert3){positions[triIdx->v1.pos], texcoords[triIdx->v1.uv], normals[triIdx->v1.normal]},
      (sgVert3){positions[triIdx->v2.pos], texcoords[triIdx->v2.uv], normals[triIdx->v2.normal]}};
    DrawTriangle3(gfx, &tri);
  }
}

void sgLighting(swgfx* gfx) {
  assert(gfx);
  const int N = gfx->dims.x * gfx->dims.y;
  for (int i = 0; i < N; ++i) {
    const R depth = gfx->depth[i];
    if (depth != DepthClearValue) {
      const sgTextureId texid  = gfx->texture[i];
      const sgTexture* texture = &gfx->textureRegister[texid];
      const sgVec2 uv          = gfx->texcoords[i];
      sgColour3* albedo        = &gfx->albedo[i];
      *albedo = Vec3FromVec4(Sample(texture, uv));
    }
  }
}

void sgAmbient(swgfx* gfx, sgColour3 ambient) {
  assert(gfx);
  const int N = gfx->dims.x * gfx->dims.y;
  for (int i = 0; i < N; ++i) {
    const R depth = gfx->depth[i];
    if (depth != DepthClearValue) {
      const sgColour3* albedo = &gfx->albedo[i];
      sgColour4* colour       = &gfx->colour[i];
      colour->rgb = add3(colour->rgb, mul3(*albedo, ambient));
    }
  }
}

void sgDirectional(swgfx* gfx, sgColour3 lightColour, sgVec3 direction) {
  assert(gfx);
  const sgVec3 L = neg3(direction);
  const int N = gfx->dims.x * gfx->dims.y;
  for (int i = 0; i < N; ++i) {
    const R depth = gfx->depth[i];
    if (depth != DepthClearValue) {
      const sgColour3* albedo = &gfx->albedo[i];
      const sgVec3*  normal   = &gfx->normals[i];
      const R        NdotL    = fmaxf(0.f, dot3(*normal, L));
      sgColour4* colour       = &gfx->colour[i];
      colour->rgb = add3(colour->rgb, scale3(mul3(*albedo, lightColour), NdotL));
    }
  }
}

void sgDepth(swgfx* gfx) {
  assert(gfx);
  const int N = gfx->dims.x * gfx->dims.y;
  for (int i = 0; i < N; ++i) {
    const R depth = gfx->depth[i];
    if (depth != DepthClearValue) {
      sgColour4* colour = &gfx->colour[i];
      const R d         = gfx->depth[i];
      *colour = (sgColour4){d,d,d, 1.f};
    }
  }
}

void sgNormals(swgfx* gfx) {
  assert(gfx);
  const int N = gfx->dims.x * gfx->dims.y;
  for (int i = 0; i < N; ++i) {
    const R depth = gfx->depth[i];
    if (depth != DepthClearValue) {
      sgColour4* colour    = &gfx->colour[i];
      const sgVec3* normal = &gfx->normals[i];
      *colour = Vec4FromVec3(max3(Zero3, *normal), 1.f);
    }
  }
}

void sgGamma(swgfx* gfx, sgTexel* texels, int width, int height) {
  assert(gfx);
  assert(texels);
  constexpr R exp = 2.2f;
  for (int i = 0; i < width * height; ++i) {
    sgTexel* const p = &texels[i];
    *p = Colour3ToTexel(exp3(TexelToColour3(*p), exp), p->a);
  }
}

void sgGammaInv(swgfx* gfx, sgColour4* pixels, int width, int height) {
  assert(gfx);
  assert(pixels);
  constexpr R exp = 1.0f/2.2f;
  for (int i = 0; i < width * height; ++i) {
    sgColour4* const p = &pixels[i];
    *p = Vec4FromVec3(exp3(Vec3FromVec4(*p), exp), p->a);
  }
}

sgCounters sgGetCounters(const swgfx* gfx) {
  assert(gfx);
  return gfx->counters;
}