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
|
#include "device.h"
#include "rasterizer.h"
#include "clip.h"
#include "shader.h"
#include "../util/assert.h"
#include "framebuffer.h"
#include "depth.h"
#include "stencil.h"
ssr_Config config;
typedef enum {
VERTEXATTR_POS = 1,
VERTEXATTR_COLOR = 1 << 1,
VERTEXATTR_UV = 1 << 2,
VERTEXATTR_NORMAL = 1 << 3,
} ssr_VertexAttrMask;
typedef struct {
int start;
int offset;
} VertexAttr;
static struct {
Mat4 matrices[3][MATRIX_DEPTH];
uint matrix_top[3];
ssr_MatrixMode matrix_mode;
Vec4 viewport;
Color* target;
Color* color_buffer; /*screen color buffer*/
uint buffer_size;
float* depth_buffer;
float* depth_screen_buffer;
DepthFunc depth_func;
byte* stencil_buffer;
byte* stencil_screen_buffer;
byte stencil_read_mask;
uint stencil_ref;
byte stencil_write_mask;
StencilFunc stencil_func;
StencilOp stencil_failop, stencil_dpfailop, stencil_passop;
Vert* verts; uint vert_count;
uint* indices; uint prim_count;
Program* program;
UniformCollection uniforms;
FrameBuffer* frame_buffer;
uint enable;
struct {
ssr_BlendFactor src;
ssr_BlendFactor dst;
} blend_factor;
} state;
#define MATRIXTOP state.matrix_top[state.matrix_mode]
#define MATRIX state.matrices[state.matrix_mode][MATRIXTOP]
#define MATRIXSTACK state.matrices[state.matrix_mode]
#define GETMATRIX(MODE) state.matrices[MODE][state.matrix_top[MODE]]
#define BUFFER (state.color_buffer)
/*topleft*/
#define contains(x, y, l, r, t, b) (x >= l && x <= r && y <= b && y >= t)
void ssr_init(ssr_Config* conf) {
config = *conf;
ssrM_zero(state.matrix_top, sizeof(state.matrix_top));
state.target = conf->target;
int size = config.width * config.height;
if (config.dbuffer) {
state.color_buffer = ssrM_newvector(Color, size);
}
else {
state.color_buffer = conf->target;
}
state.buffer_size = sizeof(Color) * size;
state.depth_screen_buffer = ssrM_newvector(float, size);
state.depth_buffer = state.depth_screen_buffer;
state.depth_func = depth_leuqal;
state.blend_factor.src = BLEND_SRC_ALPHA;
state.blend_factor.dst = BLEND_ONE_MINUS_SRC_ALPHA;
state.stencil_screen_buffer = ssrM_newvector(byte, size);
state.stencil_buffer = state.stencil_screen_buffer;
state.stencil_ref = 1;
state.stencil_read_mask = 1;
state.stencil_write_mask = 0;
state.stencil_func = stencil_equal;
state.stencil_failop = stencilop_keep;
state.stencil_dpfailop = stencilop_keep;
state.stencil_passop = stencilop_replace;
state.frame_buffer = NULL;
ssr_viewport(0, config.width, config.height, 0);
}
float ssr_getaspect() {
return (float)config.width / config.height;
}
int ssr_getframebufferw() {
return config.width;
}
int ssr_getframebufferh() {
return config.height;
}
void ssr_bindframebuffer(FrameBuffer* fbo) {
ssr_assert(fbo);
state.frame_buffer = fbo;
state.depth_buffer = fbo->depth_buffer;
state.stencil_buffer = fbo->stencil_buffer;
}
void ssr_unbindframebuffer() {
state.frame_buffer = NULL;
state.depth_buffer = state.depth_screen_buffer;
state.stencil_buffer = state.stencil_screen_buffer;
}
void ssr_setblendfunc(ssr_BlendFactor sfactor, ssr_BlendFactor dfactor) {
state.blend_factor.src = sfactor;
state.blend_factor.dst = dfactor;
}
void ssrU_blendscreencolor(uint x, uint y, Color32* src) {
Color c = state.color_buffer[x + y * config.width];
Color32 dst; color_tocolor32(c, &dst);
ssr_blend(src, &dst, src);
}
void ssr_matrixmode(ssr_MatrixMode mode) {
state.matrix_mode = mode;
}
void ssr_loadidentity() {
internal_mat4_setidentity(&MATRIX);
}
void ssr_pushmatrix() {
ssr_assert(MATRIXTOP < MATRIX_DEPTH - 1);
++MATRIXTOP;
MATRIX = MATRIXSTACK[MATRIXTOP - 1];
}
void ssr_popmatrix() {
MATRIXTOP = clamp(--MATRIXTOP, 0, MATRIX_DEPTH - 1);
}
/*http://warmcat.org/chai/blog/?p=559*/
void ssr_lookat(Vec3* pos, Vec3* target, Vec3* up) {
ssr_assert(pos && target && up);
Vec3 z;
internal_vec3_minus(pos, target, &z); internal_vec3_normalize(&z, &z); /*去除缩放影响*/
Vec3 x;
internal_vec3_cross(up, &z, &x); internal_vec3_normalize(&x, &x);
Vec3 y;
internal_vec3_cross(&z, &x, &y); internal_vec3_normalize(&y, &y);
Mat4 m = { /*注意这里是列主序*/
x.x, y.x, z.x, 0,
x.y, y.y, z.y, 0,
x.z, y.z, z.z, 0,
-internal_vec3_dot(&x, pos), -internal_vec3_dot(&y, pos), -internal_vec3_dot(&z, pos), 1
};
internal_mat4_multiply(&MATRIX, &m, &MATRIX);
}
void ssr_scale(float sx, float sy, float sz) {
Vec3 scale = { sx, sy, sz };
internal_mat4_scale(&MATRIX, &scale, &MATRIX);
}
void ssr_rotate(float angle, float x, float y, float z) {
Vec3 axis = { x, y, z };
internal_vec3_normalize(&axis, &axis);
internal_mat4_rotate(&MATRIX, angle, &axis, &MATRIX);
}
void ssr_translate(float x, float y, float z) {
Vec3 trans = { x, y, z };
internal_mat4_translate(&MATRIX, &trans, &MATRIX);
}
void ssr_loadmatrix(Mat4* m) {
ssr_assert(m);
MATRIX = *m;
}
void ssr_multmatrix(Mat4* m) {
ssr_assert(m);
internal_mat4_multiply(&MATRIX, m, &MATRIX);
}
void ssr_getmvp(Mat4* out) {
ssr_assert(out);
internal_mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out);
internal_mat4_multiply(&GETMATRIX(MATRIX_PROJECTION), out, out);
}
void ssr_getm(Mat4* out) {
ssr_assert(out);
*out = GETMATRIX(MATRIX_MODEL);
}
void ssr_getmv(Mat4* out) {
ssr_assert(out);
internal_mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out);
}
void ssr_enable(uint mask) {
state.enable |= mask;
}
void ssr_disable(uint mask) {
state.enable &= (~mask);
}
bool ssr_isenable(uint mask) {
return state.enable & mask;
}
void ssr_viewport(float l, float r, float b, float t) {
state.viewport.x = l;
state.viewport.y = r;
state.viewport.z = b;
state.viewport.w = t;
}
void ssr_ortho(float l, float r, float b, float t, float n, float f) {
Mat4 m;
internal_mat4_setortho(l, r, b, t, n, f, &m);
internal_mat4_multiply(&MATRIX, &m, &MATRIX);
}
void ssr_frustum(float l, float r, float b, float t, float n, float f) {
ssr_assert(n > 0 && f > 0 && f > n);
Mat4 m;
internal_mat4_setfrustum(l, r, b, t, n, f, &m);
internal_mat4_multiply(&MATRIX, &m, &MATRIX);
}
void ssr_perspective(float fov, float aspect, float n, float f) {
ssr_assert(n > 0 && f > 0 && f > n);
Mat4 m;
internal_mat4_setperspective(fov, aspect, n, f, &m);
internal_mat4_multiply(&MATRIX, &m, &MATRIX);
}
void ssr_present() {
if (config.dbuffer) {
memcpy(state.target, state.color_buffer, state.buffer_size);
}
}
static void _clearscreen(Color color) {
if (color == 0x00) {
memset(state.color_buffer, 0, state.buffer_size);
}
else {
uint size = config.width * sizeof(Color);
for (int x = 0; x < config.width; ++x)
state.color_buffer[x] = color;
for (int y = 1; y < config.height; ++y)
ssrM_copy(&state.color_buffer[config.width * y], state.color_buffer, size);
}
}
static void _clearcolorbuffer(Color32 color) {
Texture* rt;
Color32* pixels;
for (int i = 0; i < RENDER_TARGET_COUNT; ++i) {
rt = state.frame_buffer->render_textures[i];
if (rt == NULL) continue;
pixels = rt->pixels;
uint size = rt->width * sizeof(Color32);
for (int x = 0; x < rt->width; ++x)
pixels[x] = color;
for (int y = 1; y < rt->height; ++y)
ssrM_copy(&pixels[rt->width * y], pixels, size);
}
}
void ssr_clearcolor(Color color) {
if (state.frame_buffer == NULL) {
_clearscreen(color);
}
else {
Color32 c; color_tocolor32(color, &c);
_clearcolorbuffer(c);
}
}
void ssr_cleardepth() {
ssr_assert(state.depth_buffer);
for (int i = 0; i < config.width * config.height; ++i) {
state.depth_buffer[i] = 1;
}
}
void ssr_clearstencil(byte val) {
ssr_assert(state.stencil_buffer);
memset(state.stencil_buffer, val, config.width * config.height);
}
/*directly put point onto screen*/
void ssr_putpoint(uint screenx, uint screeny, Color color) {
if (!contains(screenx, screeny, 0, config.width - 1, 0, config.height - 1))
return;
BUFFER[screeny * config.width + screenx] = color;
}
void ssr_putpoint32(uint screenx, uint screeny, Color32* c32) {
Color c = color32_tocolor(c32);
ssr_putpoint(screenx, screeny, c);
}
Color ssr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
unsigned int c = (a << 24) | (r << 16) | (g << 8) | b;
return c;
}
bool ssr_testdepth(uint x, uint y, float depth){
ssr_assert(state.depth_buffer);
uint off = x + y * config.width;
return state.depth_func(depth, state.depth_buffer[off]);
}
void ssr_writedepth(uint x, uint y, float depth) {
ssr_assert(state.depth_buffer);
uint off = x + y * config.width;
state.depth_buffer[off] = depth;
}
void ssr_setdepthfunc(ssr_DepthFunc func) {
switch (func)
{
case DEPTHFUNC_ALWAYS: state.depth_func = depth_always; break;
case DEPTHFUNC_NEVER: state.depth_func = depth_never; break;
case DEPTHFUNC_LESS: state.depth_func = depth_less; break;
case DEPTHFUNC_EQUAL: state.depth_func = depth_equal; break;
case DEPTHFUNC_LEQUAL: state.depth_func = depth_leuqal; break;
case DEPTHFUNC_GREATER: state.depth_func = depth_greater; break;
case DEPTHFUNC_NOTEQUAL: state.depth_func = depth_notequal; break;
case DEPTHFUNC_GEQUAL: state.depth_func = depth_gequer; break;
default: ssr_assert(FALSE);
}
}
void ssrU_viewport(Vec2* p, Vec2* out) {
ssr_assert(p && out);
float l = state.viewport.x,
r = state.viewport.y,
b = state.viewport.z,
t = state.viewport.w,
w2 = (r - l) * 0.5f,
c2 = (b - t) * 0.5f;
out->x = (int)round((p->x + 1) * (w2 - 0.5f) + l);
out->y = (int)round((1 - p->y) * (c2 - 0.5f) + t);
}
void ssr_blend(Color32* src, Color32* dst, Color32* out) {
ssrU_blend(state.blend_factor.src, state.blend_factor.dst, src, dst, out);
}
static _blend(ssr_BlendFactor factor, Color32* src, Color32* dst, Color32* out) {
switch (factor) {
case BLEND_ONE:
break;
case BLEND_ZERO:
out->r = out->g = out->b = out->a = 0;
break;
case BLEND_SRC_COLOR:
out->r *= src->r;
out->g *= src->g;
out->b *= src->b;
out->a *= src->a;
break;
case BLEND_ONE_MINUS_SRC_COLOR:
out->r *= (1 - src->r);
out->g *= (1 - src->g);
out->b *= (1 - src->b);
out->a *= (1 - src->a);
break;
case BLEND_DST_COLOR:
out->r *= dst->r;
out->g *= dst->g;
out->b *= dst->b;
out->a *= dst->a;
break;
case BLEND_ONE_MINUS_DST_COLOR:
out->r *= (1 - dst->r);
out->g *= (1 - dst->g);
out->b *= (1 - dst->b);
out->a *= (1 - dst->a);
break;
case BLEND_SRC_ALPHA:
out->r *= src->a;
out->g *= src->a;
out->b *= src->a;
out->a *= src->a;
break;
case BLEND_ONE_MINUS_SRC_ALPHA:
out->r *= (1 - src->a);
out->g *= (1 - src->a);
out->b *= (1 - src->a);
out->a *= (1 - src->a);
break;
case BLEND_DST_ALPHA:
out->r *= dst->a;
out->g *= dst->a;
out->b *= dst->a;
out->a *= dst->a;
break;
case BLEND_ONE_MINUS_DST_ALPHA:
out->r *= (1 - dst->a);
out->g *= (1 - dst->a);
out->b *= (1 - dst->a);
out->a *= (1 - dst->a);
break;
default:
ssr_assert(FALSE);
break;
}
}
void ssrU_blend(
ssr_BlendFactor sfactor
, ssr_BlendFactor dfactor
, Color32* src
, Color32* dst
, Color32* out
) {
ssr_assert(src && dst && out);
Color32 s0; s0 = *src;
_blend(sfactor, src, dst, src);
_blend(dfactor, &s0, dst, dst);
out->r = src->r + dst->r;
out->g = src->g + dst->g;
out->b = src->b + dst->b;
out->a = src->a + dst->a;
}
void ssr_bindvertices(Vert* verts, int vert_count, uint* indices, int prim_count) {
ssr_assert(verts && indices);
state.verts = verts;
state.vert_count = vert_count;
state.indices = indices;
state.prim_count = prim_count;
}
void ssr_useprogram(Program* program) {
ssr_assert(program);
state.program = program;
}
void ssr_unuseprogram() {
state.program = NULL;
}
VertexShaderIn ssr_vert_in;
FragmentShaderIn ssr_frag_in;
static struct {
Vec4* coords;
uint length;
} clip_coords;
extern ClippedBuffer clip_buffer; /*clipping result*/
static void render_prims_triangle(uint varying_flag) {
uint i0, i1, i2;
Vec4 *c0, *c1, *c2;
bool reset_active_reg = FALSE;
bool clipped = FALSE;
for (int i = 0; i < state.prim_count; ++i) {
i0 = state.indices[i * 3];
i1 = state.indices[i * 3 + 1];
i2 = state.indices[i * 3 + 2];
c0 = &clip_coords.coords[i0];
c1 = &clip_coords.coords[i1];
c2 = &clip_coords.coords[i2];
/*early back face culling*/
bool early_culled = FALSE;
if (ssr_isenable(ENABLE_BACKFACECULL)) {
/*cull in ndc*/
float w0 = 1 / c0->w, w1 = 1 / c1->w, w2 = 1 / c2->w;
bool all_front = w0 > 0 && w1 > 0 && w2 > 0;
if (all_front)
{
early_culled = TRUE;
Vec2 ab, ac;
ab.x = c1->x * w1 - c0->x * w0;
ab.y = c1->y * w1 - c0->y * w0;
ac.x = c2->x * w2 - c0->x * w0;
ac.y = c2->y * w2 - c0->y * w0;
if (ab.x * ac.y - ab.y * ac.x <= 0) {
continue; /*cull*/
}
}
//OpenGL algrithm:
//float w0 = max(1 / c0->w, 0), w1 = 1 / c1->w, w2 = 1 / c2->w;
//Vec2 a = { c0->x * w0, c0->y * w0 };
//Vec2 b = { c1->x * w1, c1->y * w1 };
//Vec2 c = { c2->x * w2, c2->y * w2 };
//float signed_area = 0;
//signed_area += a.x * b.y - a.y * b.x;
//signed_area += b.x * c.y - b.y * c.x;
//signed_area += c.x * a.y - c.y * a.x;
//if (signed_area <= 0) {
// continue; /*cull*/
//}
}
/*clipping*/
clipped = clip_triangle(c0, c1, c2, i0, i1, i2, varying_flag, &clip_buffer);
/*rasterization*/
if (!clipped) {
if (reset_active_reg) {
reset_active_reg = FALSE;
ssrS_setactiveregr();
}
ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, &state.uniforms, early_culled);
}
else {
if (!reset_active_reg) {
reset_active_reg = TRUE;
ssrS_setactiveregc();
}
if (clip_buffer.count >= 3) {
ClippedVert* vt0 = &clip_buffer.vertices[0], *vt1, *vt2;
for (int i = 1; i <= clip_buffer.count - 2; ++i) {
vt1 = &clip_buffer.vertices[i];
vt2 = &clip_buffer.vertices[i + 1];
c0 = &vt0->clip_coord; c1 = &vt1->clip_coord; c2 = &vt2->clip_coord;
i0 = vt0->index; i1 = vt1->index; i2 = vt2->index;
ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, &state.uniforms, early_culled);
}
}
}
}
}
static void render_prims_line(uint varying_flag) {
uint i0, i1;
Vec4 *c0, *c1;
bool reset_active_reg = FALSE;
bool clipped = FALSE;
for (int i = 0; i < state.prim_count; ++i) {
i0 = state.indices[i * 2];
i1 = state.indices[i * 2 + 1];
c0 = &clip_coords.coords[i0];
c1 = &clip_coords.coords[i1];
clipped = clip_line(c0, c1, i0, i1, varying_flag, &clip_buffer);
if(!clipped) {
if (reset_active_reg) {
reset_active_reg = FALSE;
ssrS_setactiveregr();
}
ssrR_line(c0, c1, i0, i1, state.program, &state.uniforms);
}
else {
if (!reset_active_reg) {
reset_active_reg = TRUE;
ssrS_setactiveregc();
}
if (clip_buffer.count >= 2) {
ClippedVert* vt0, *vt1;
for (int i = 0; i < clip_buffer.count - 1; ++i) {
vt0 = &clip_buffer.vertices[i];
vt1 = &clip_buffer.vertices[i + 1];
c0 = &vt0->clip_coord; c1 = &vt1->clip_coord;
i0 = vt0->index; i1 = vt1->index;
ssrR_line(c0, c1, i0, i1, state.program, &state.uniforms);
}
}
}
}
}
static void render_prims_point(uint varying_flag) {
uint i0;
Vec4 *c0;
bool reset_active_reg = FALSE;
bool clipped = FALSE;
for (int i = 0; i < state.prim_count; ++i) {
i0 = state.indices[i];
c0 = &clip_coords.coords[i0];
clipped = clip_point(c0, i0, varying_flag, &clip_buffer);
if (!clipped) {
ssrR_point(c0, i0, state.program, &state.uniforms);
}
else {
/*clipped*/
}
}
}
void ssr_draw(ssr_PrimitiveType primitive) {
ssr_assert(state.verts && state.indices);
static Mat4 mvp, mv;
/*set built-in uniforms*/
ssr_getmvp(&mvp);
ssr_getmv(&mv);
state.uniforms.model = &GETMATRIX(MATRIX_MODEL);
state.uniforms.view = &GETMATRIX(MATRIX_VIEW);
state.uniforms.projection = &GETMATRIX(MATRIX_PROJECTION);
state.uniforms.mvp = &mvp;
state.uniforms.mv = &mv;
uint varying_flag = state.program->varying_flag;
bool use_extra_varyings = (varying_flag & VARYING_ANY) != 0;
/*prepare registers if necessary*/
if (use_extra_varyings) {
ssrS_openregs(varying_flag);
ssrS_setregisters(state.vert_count);
ssrS_setactiveregr();
}
/*resize clipping coords buffer*/
if (clip_coords.length < state.vert_count) {
ssrM_resizevector(
Vec4,
clip_coords.coords,
clip_coords.length,
state.vert_count,
FALSE
);
}
/*vertex operation*/
VertexShader vert_shader = state.program->vertexshader;
for (int i = 0; i < state.vert_count; ++i) {
Vert* vert = &state.verts[i];
ssr_vert_in.vertex = vert;
/*set register pointers*/
if (use_extra_varyings) {
ssrS_setupregisterpointers(vert->index);
}
vert_shader(&state.uniforms, &ssr_vert_in, &clip_coords.coords[i]);
}
/*set register pointer to frag-in*/
if (use_extra_varyings) {
ssrS_setregtofragin(varying_flag, &ssr_frag_in);
}
/*primitive assembly and rendering*/
switch (primitive) {
case PRIMITIVE_TRIANGLE:
render_prims_triangle(varying_flag);
break;
case PRIMITIVE_LINE:
render_prims_line(varying_flag);
break;
case PRIMITIVE_POINT:
render_prims_point(varying_flag);
break;
}
}
void ssr_setuniformmat4(uint idx, Mat4* src) {
ssr_assert(src);
if (idx < 0 || idx > 7) return;
state.uniforms.var_mat4[idx] = *src;
}
void ssr_setuniformvec4(uint idx, Vec4* src) {
ssr_assert(src);
if (idx < 0 || idx > 7) return;
state.uniforms.var_vec4[idx] = *src;
}
void ssr_setuniformvec3(uint idx, Vec3* src) {
ssr_assert(src);
if (idx < 0 || idx > 7) return;
state.uniforms.var_vec3[idx] = *src;
}
void ssr_setuniformvec2(uint idx, Vec2* src) {
ssr_assert(src);
if (idx < 0 || idx > 7) return;
state.uniforms.var_vec2[idx] = *src;
}
void ssr_setuniformtex(uint idx, Texture* tex) {
ssr_assert(tex);
if (idx < 0 || idx > 11) return;
state.uniforms.var_tex[idx] = tex;
}
void ssr_setuniformu(void* userdata) {
state.uniforms.userdata = userdata;
}
bool ssr_teststencil(uint x, uint y, bool pass_depth_test) {
int index = x + config.width * y;
byte mask = state.stencil_read_mask;
byte val = state.stencil_buffer[index];
return state.stencil_func(val, state.stencil_ref);
}
void ssr_writestencil(uint x, uint y, bool pass_depth_test, bool pass_stencil_test) {
byte write_mask = state.stencil_write_mask;
if (write_mask == 0) return;/*dont write*/
int index = x + config.width * y;
byte read_mask = state.stencil_read_mask;
byte val = state.stencil_buffer[index];
if (pass_stencil_test && pass_depth_test) {
val = state.stencil_passop(val & read_mask, state.stencil_ref & read_mask);
}
else if (pass_stencil_test && !pass_depth_test) {
val = state.stencil_dpfailop(val & read_mask, state.stencil_ref & read_mask);
}
else if (!pass_stencil_test) {
val = state.stencil_failop(val & read_mask, state.stencil_ref & read_mask);
}
/*https://stackoverflow.com/questions/31660747/what-exactly-does-glstencilmask-do*/
state.stencil_buffer[index] &= ~write_mask;
state.stencil_buffer[index] |= (val & write_mask);
}
void ssr_setstencilfunc(ssr_StencilFunc func, byte ref, uint mask) {
switch (func)
{
case STENCILFUNC_ALWAYS: state.stencil_func = stencil_always; break;
case STENCILFUNC_NEVER: state.stencil_func = stencil_never; break;
case STENCILFUNC_LESS: state.stencil_func = stencil_less; break;
case STENCILFUNC_EQUAL: state.stencil_func = stencil_equal; break;
case STENCILFUNC_LEQUAL: state.stencil_func = stencil_leuqal; break;
case STENCILFUNC_GREATER: state.stencil_func = stencil_greater; break;
case STENCILFUNC_NOTEQUAL: state.stencil_func = stencil_notequal; break;
case STENCILFUNC_GEQUAL: state.stencil_func = stencil_gequer; break;
default: ssr_assert(FALSE);
}
state.stencil_ref = ref;
state.stencil_read_mask = mask;
}
static void setstencilop(ssr_StencilOp v, StencilOp* op) {
switch (v) {
case STENCILOP_KEEP: *op = stencilop_keep; break;
case STENCILOP_ZERO: *op = stencilop_zero; break;
case STENCILOP_REPLACE: *op = stencilop_replace; break;
case STENCILOP_INCR: *op = stencilop_incr; break;
case STENCILOP_INCR_WRAP: *op = stencilop_incrwrap; break;
case STENCILOP_DECR: *op = stencilop_decr; break;
case STENCILOP_DECR_WRAP: *op = stencilop_decrwrap; break;
case STENCILOP_INVERT: *op = stencilop_invert; break;
default:
ssr_assert(FALSE);
}
}
void ssr_setstencilop(ssr_StencilOp fail, ssr_StencilOp dpfail, ssr_StencilOp pass) {
setstencilop(fail, &state.stencil_failop);
setstencilop(dpfail, &state.stencil_dpfailop);
setstencilop(pass, &state.stencil_passop);
}
void ssr_setstencilmask(byte mask) {
state.stencil_write_mask = mask;
}
bool ssr_iswritesencil() {
return state.stencil_write_mask != 0x00;
}
void ssr_blendandputpoint(int x, int y, bool blend) {
if (!state.frame_buffer) { /*directly onto screen*/
if (blend) {
ssrU_blendscreencolor(x, y, out_color[0]);
}
color32_saturate(out_color[0]);
ssr_putpoint32(x, y, out_color[0]);
}
else { /*onto framebuffer color buffer*/
Color32 *src, *dst;
Texture* rt;
for (int i = 0; i < RENDER_TARGET_COUNT; ++i) {
rt = state.frame_buffer->render_textures[i];
if (rt == NULL || !contains(x, y, 0, rt->width, 0, rt->height))
continue;
dst = &rt->pixels[x + y * rt->width];
src = out_color[i];
if (blend)
ssr_blend(src, dst, src);
*dst = *src;
}
}
}
FrameBuffer* fbo_create() {
FrameBuffer* fb = ssrM_new(FrameBuffer);
fbo_attachdepthbuffer(fb, 0);
fbo_attachstencilbuffer(fb, 0);
ssrM_zero(fb->render_textures, sizeof(Texture*) * RENDER_TARGET_COUNT);
return fb;
}
void fbo_attachrendertexture(FrameBuffer* fb, uint attachment, Texture* rt) {
fb->render_textures[attachment] = rt;
}
void fbo_attachdepthbuffer(FrameBuffer* fb, float* depthbuffer) {
if (depthbuffer == NULL)
fb->depth_buffer = state.depth_screen_buffer;
else
fb->depth_buffer = depthbuffer;
}
void fbo_attachstencilbuffer(FrameBuffer* fb, byte* stencilbuffer) {
if (stencilbuffer == NULL)
fb->stencil_buffer = state.stencil_screen_buffer;
else
fb->stencil_buffer = stencilbuffer;
}
void fbo_free(FrameBuffer* fb) {
ssrM_free(fb);
}
|