-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstate.c
772 lines (667 loc) · 25.3 KB
/
state.c
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
#include <string.h>
#include <pbkit/pbkit.h>
#include "GL/gl.h"
#include "GL/glext.h"
#include "types.h"
#include "matrix.h"
#include "texture.h"
#include "array.h"
#include "error.h"
#include "light.h"
#include "texenv.h"
#include "push.h"
#include "misc.h"
#include "state.h"
// TODO: what do the bits mean?
#define NV_TEX_DISABLE 0x0003FFC0
#define NV_TEX_ENABLE 0x40000000
#define NV_TEX_ALPHAKILL (1 << 2)
#define FLAG_DIRTY_IF_CHANGED(state, flag, value) \
if (pbgl.flags.flag != value) \
pbgl.state_dirty = pbgl.state.dirty = GL_TRUE
gl_state_t pbgl;
void pbgl_state_init(void) {
memset(&pbgl, 0, sizeof(pbgl));
/* set default state */
pbgl.fb_width = pb_back_buffer_width();
pbgl.fb_height = pb_back_buffer_height();
pbgl.flags.dither = GL_TRUE;
pbgl.error = GL_NO_ERROR;
// nv2a compare func index = GL func index - 0x200
pbgl.alpha.func = GL_ALWAYS & 0xF;
pbgl.alpha.dirty = GL_TRUE;
// src and dst blend factors are equal between nv2a and GL
pbgl.blend.src = GL_ONE;
pbgl.blend.dst = GL_ZERO;
pbgl.blend.eqn = GL_FUNC_ADD; // same goes for the equation indices
pbgl.blend.color = 0x00000000;
pbgl.blend.dirty = GL_TRUE;
pbgl.depth.func = GL_LESS & 0xF;
pbgl.depth.writemask = GL_TRUE;
pbgl.depth.dirty = GL_TRUE;
pbgl.stencil.func = GL_ALWAYS & 0xF;
pbgl.stencil.op_sfail = GL_KEEP; // and for stencil ops
pbgl.stencil.op_zfail = GL_KEEP;
pbgl.stencil.op_zpass = GL_KEEP;
pbgl.stencil.funcmask = 0xFFFFFFFF;
pbgl.stencil.writemask = 0xFFFFFFFF;
pbgl.stencil.dirty = GL_TRUE;
pbgl.cullface.frontface = GL_CCW; // and for front face
pbgl.cullface.cullface = GL_BACK; // and for cull face
pbgl.cullface.dirty = GL_TRUE;
pbgl.polyofs.dirty = GL_TRUE;
pbgl.colormask.value =
NV097_SET_COLOR_MASK_RED_WRITE_ENABLE
| NV097_SET_COLOR_MASK_GREEN_WRITE_ENABLE
| NV097_SET_COLOR_MASK_BLUE_WRITE_ENABLE
| NV097_SET_COLOR_MASK_ALPHA_WRITE_ENABLE;
pbgl.colormask.dirty = GL_TRUE;
for (int i = 1; i < LIGHT_COUNT; ++i) {
pbgl.light[i].ambient = (vec4f){{ 0.f, 0.f, 0.f, 1.f }};
pbgl.light[i].diffuse = (vec4f){{ 0.f, 0.f, 0.f, 1.f }};
pbgl.light[i].specular = (vec4f){{ 0.f, 0.f, 0.f, 1.f }};
pbgl.light[i].pos = (vec4f){{ 0.f, 0.f, 1.f, 0.f }};
pbgl.light[i].factor[0] = 1.f; // GL_CONSTANT_ATTENUATION
pbgl.light[i].dirty = GL_TRUE;
}
// light #0 has a different initial state for some reason
pbgl.light[0].ambient = (vec4f){{ 1.f, 1.f, 1.f, 1.f }};
pbgl.light[0].diffuse = (vec4f){{ 1.f, 1.f, 1.f, 1.f }};
pbgl.light[0].specular = (vec4f){{ 1.f, 1.f, 1.f, 1.f }};
pbgl.light[0].pos = (vec4f){{ 0.f, 0.f, 1.f, 0.f }};
pbgl.light[0].factor[0] = 1.f; // GL_CONSTANT_ATTENUATION
pbgl.light[0].dirty = GL_TRUE;
pbgl.light_any_dirty = GL_TRUE;
pbgl.lightmodel.ambient = (vec4f){{ 0.2f, 0.2f, 0.2f, 1.0f }};
pbgl.lightmodel.dirty = GL_TRUE;
pbgl.varray[VARR_COLOR1].value = (vec4f) {{ 1.f, 1.f, 1.f, 1.f }};
pbgl.varray[VARR_NORMAL].value = (vec4f) {{ 0.f, 0.f, 1.f, 0.f }};
pbgl.varray[VARR_TEXCOORD].value = (vec4f) {{ 0.f, 0.f, 0.f, 1.f }};
for (int i = 0; i < VARR_COUNT; ++i)
pbgl.varray[i].dirty = GL_TRUE;
for (int i = 0; i < TEXUNIT_COUNT; ++i) {
pbgl.tex[i].dirty = GL_TRUE;
pbgl.texenv[i] = (texenv_state_t) {
.mode = GL_MODULATE,
.combine_rgb = GL_MODULATE,
.combine_a = GL_MODULATE,
.src_rgb = { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
.src_a = { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
.operand_rgb = { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA },
.operand_a = { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
.color = 0x00000000,
.scale_rgb = 1.f,
.scale_a = 1.f,
.dirty = GL_TRUE,
};
}
pbgl.tex_any_dirty = GL_TRUE;
pbgl.texenv_dirty = GL_TRUE;
for (int i = 0; i < MTX_COUNT; ++i)
pbgl_mtx_reset(i);
pbgl.mtx_any_dirty = GL_TRUE;
pbgl.mtx_current = MTX_MODELVIEW;
pbgl.view.x = 0;
pbgl.view.y = 0;
pbgl.view.w = pbgl.fb_width;
pbgl.view.h = pbgl.fb_height;
pbgl.view.znear = 0.f; // this is multiplied by appropriate Z scales below
pbgl.view.zfar = 1.f;
pbgl.view.dirty = GL_TRUE;
pbgl.material[0].dirty = GL_TRUE;
pbgl.material[1].dirty = GL_TRUE;
pbgl.scissor.dirty = GL_TRUE;
pbgl.scissor.x = 0;
pbgl.scissor.y = 0;
pbgl.scissor.w = pbgl.fb_width;
pbgl.scissor.h = pbgl.fb_height;
pbgl.polymode.dirty = GL_TRUE;
pbgl.polymode.front_mode = NV097_SET_FRONT_POLYGON_MODE_V_FILL;
pbgl.polymode.back_mode = NV097_SET_FRONT_POLYGON_MODE_V_FILL;
pbgl.line.dirty = GL_TRUE;
pbgl.clear_zstencil = 0xFFFFFF00; // TODO: this assumes 24-bit z + 8-bit stencil
pbgl.state_dirty = GL_TRUE;
pbgl.active = GL_TRUE; // this can only be used when we're live
}
static inline GLuint *flush_texunit(GLuint *p, GLuint i) {
texunit_state_t *tu = pbgl.tex + i;
if (tu->dirty) {
GLuint enable;
// TODO: properly handle texture_1d, texture_3d, texture_cube
const GLboolean tex_enabled = tu->flags.texture_1d || tu->flags.texture_2d;
if (tu->enabled && tex_enabled) {
enable = NV_TEX_ENABLE;
if (pbgl.flags.alpha_test)
enable |= NV_TEX_ALPHAKILL;
} else {
enable = NV_TEX_DISABLE;
}
const GLuint tidx = i * 64;
if (tu->tex && enable != NV_TEX_DISABLE) {
// note: we only operate on swizzled textures
p = push_command_parameter(p, NV097_SET_TEXTURE_OFFSET + tidx, tu->tex->nv.addr);
p = push_command_parameter(p, NV097_SET_TEXTURE_FORMAT + tidx, tu->tex->nv.format);
p = push_command_parameter(p, NV097_SET_TEXTURE_ADDRESS + tidx, tu->tex->nv.wrap);
p = push_command_parameter(p, NV097_SET_TEXTURE_CONTROL0 + tidx, enable |
PBGL_MASK(NV097_SET_TEXTURE_CONTROL0_MIN_LOD_CLAMP, 0) |
PBGL_MASK(NV097_SET_TEXTURE_CONTROL0_MAX_LOD_CLAMP, tu->tex->mipmax - 1));
p = push_command_parameter(p, NV097_SET_TEXTURE_FILTER + tidx, tu->tex->nv.filter);
p = push_command_boolean(p, NV097_SET_TEXTURE_MATRIX_ENABLE + i * 4, GL_TRUE);
} else {
p = push_command_parameter(p, NV097_SET_TEXTURE_CONTROL0 + tidx, NV_TEX_DISABLE);
p = push_command_boolean(p, NV097_SET_TEXTURE_MATRIX_ENABLE + i * 4, GL_FALSE);
}
tu->dirty = GL_FALSE;
}
return p;
}
static inline void mark_active_texunits_dirty(void) {
for (GLuint i = 0; i < TEXUNIT_COUNT; ++i)
if (pbgl.tex[i].enabled)
pbgl.tex[i].dirty = GL_TRUE;
}
GLboolean pbgl_state_flush(void) {
if (!pbgl.state_dirty) return GL_FALSE;
GLuint *p = pb_begin();
if (pbgl.scissor.dirty && pbgl.flags.scissor_test) {
#define NV097_SET_WINDOW_CLIP_TYPE 0x000002B4
#define NV097_SET_WINDOW_CLIP_HORIZONTAL 0x000002C0
#define NV097_SET_WINDOW_CLIP_VERTICAL 0x000002E0
GLint xmin = pbgl.scissor.x;
GLint xmax = pbgl.scissor.x + pbgl.scissor.w - 1;
GLint ymax = pbgl.view.h - pbgl.scissor.y - 1;
GLint ymin = pbgl.view.h - pbgl.scissor.y - pbgl.scissor.h;
xmin = imax(xmin, 0);
ymin = imax(ymin, 0);
xmax = imin(xmax, pbgl.view.w - 1);
ymax = imin(ymax, pbgl.view.h - 1);
p = push_command_boolean(p, NV097_SET_WINDOW_CLIP_TYPE, false);
p = push_command_parameter(p, NV097_SET_WINDOW_CLIP_HORIZONTAL, xmin | (xmax << 16));
p = push_command_parameter(p, NV097_SET_WINDOW_CLIP_VERTICAL, ymin | (ymax << 16));
pbgl.scissor.dirty = GL_FALSE;
}
if (pbgl.line.dirty) {
p = push_command_boolean(p, NV20_TCL_PRIMITIVE_3D_LINE_SMOOTH_ENABLE, pbgl.flags.line_smooth);
pbgl.line.dirty = GL_FALSE;
}
if (pbgl.alpha.dirty) {
// alpha kill might have changed
for (int i = 0; i < TEXUNIT_COUNT; ++i)
if (pbgl.tex[i].enabled)
pbgl.tex[i].dirty = GL_TRUE;
pbgl.tex_any_dirty = GL_TRUE;
p = push_command_boolean(p, NV097_SET_ALPHA_TEST_ENABLE, pbgl.flags.alpha_test);
p = push_command_parameter(p, NV097_SET_ALPHA_FUNC, pbgl.alpha.func);
p = push_command_parameter(p, NV097_SET_ALPHA_REF, pbgl.alpha.ref);
pbgl.alpha.dirty = GL_FALSE;
}
if (pbgl.blend.dirty) {
p = push_command_boolean(p, NV097_SET_BLEND_ENABLE, pbgl.flags.blend);
p = push_command_parameter(p, NV097_SET_BLEND_FUNC_SFACTOR, pbgl.blend.src);
p = push_command_parameter(p, NV097_SET_BLEND_FUNC_DFACTOR, pbgl.blend.dst);
p = push_command_parameter(p, NV097_SET_BLEND_EQUATION, pbgl.blend.eqn);
p = push_command_parameter(p, NV097_SET_BLEND_COLOR, pbgl.blend.color);
pbgl.blend.dirty = GL_FALSE;
}
if (pbgl.depth.dirty) {
p = push_command_boolean(p, NV097_SET_DEPTH_TEST_ENABLE, pbgl.flags.depth_test);
p = push_command_boolean(p, NV097_SET_DEPTH_MASK, pbgl.depth.writemask);
p = push_command_parameter(p, NV097_SET_DEPTH_FUNC, pbgl.depth.func);
pbgl.depth.dirty = GL_FALSE;
}
if (pbgl.stencil.dirty) {
p = push_command_boolean(p, NV097_SET_STENCIL_TEST_ENABLE, pbgl.flags.stencil_test);
p = push_command_parameter(p, NV097_SET_STENCIL_OP_FAIL, pbgl.stencil.op_sfail);
p = push_command_parameter(p, NV097_SET_STENCIL_OP_ZFAIL, pbgl.stencil.op_zfail);
p = push_command_parameter(p, NV097_SET_STENCIL_OP_ZPASS, pbgl.stencil.op_zpass);
p = push_command_parameter(p, NV097_SET_STENCIL_FUNC, pbgl.stencil.func);
p = push_command_parameter(p, NV097_SET_STENCIL_FUNC_MASK, pbgl.stencil.funcmask);
p = push_command_parameter(p, NV097_SET_STENCIL_MASK, pbgl.stencil.writemask);
pbgl.stencil.dirty = GL_FALSE;
}
if (pbgl.polyofs.dirty) {
p = push_command_boolean(p, NV097_SET_POLY_OFFSET_FILL_ENABLE, pbgl.flags.poly_offset);
p = push_command_float(p, NV097_SET_POLYGON_OFFSET_SCALE_FACTOR, pbgl.polyofs.factor);
p = push_command_float(p, NV097_SET_POLYGON_OFFSET_BIAS, pbgl.polyofs.units);
pbgl.polyofs.dirty = GL_FALSE;
}
if (pbgl.polymode.dirty) {
p = push_command_parameter(p, NV097_SET_FRONT_POLYGON_MODE, pbgl.polymode.front_mode);
p = push_command_parameter(p, NV097_SET_BACK_POLYGON_MODE, pbgl.polymode.back_mode);
pbgl.polymode.dirty = GL_FALSE;
}
if (pbgl.cullface.dirty) {
p = push_command_parameter(p, NV097_SET_FRONT_FACE, pbgl.cullface.frontface);
p = push_command_boolean(p, NV097_SET_CULL_FACE_ENABLE, pbgl.flags.cull_face);
p = push_command_parameter(p, NV097_SET_CULL_FACE, pbgl.cullface.cullface);
pbgl.cullface.dirty = GL_FALSE;
}
if (pbgl.fog.dirty) {
p = push_command_boolean(p, NV097_SET_FOG_ENABLE, pbgl.flags.fog);
p = push_command_parameter(p, NV097_SET_FOG_MODE, pbgl.fog.mode);
p = push_command_parameter(p, NV097_SET_FOG_COLOR, pbgl.fog.color);
// TODO
pbgl.fog.dirty = GL_FALSE;
}
if (pbgl.colormask.dirty) {
p = push_command_parameter(p, NV097_SET_COLOR_MASK, pbgl.colormask.value);
pbgl.colormask.dirty = GL_FALSE;
}
if (pbgl.view.dirty) {
const GLfloat znear = pbgl.view.znear * (GLfloat)PBGL_Z_MAX;
const GLfloat zfar = pbgl.view.zfar * (GLfloat)PBGL_Z_MAX;
mat4_viewport(&pbgl.view.mtx, pbgl.view.x, pbgl.view.y, pbgl.view.w, pbgl.view.h, znear, zfar);
p = push_command_float(p, NV097_SET_CLIP_MIN, znear);
p = push_command_float(p, NV097_SET_CLIP_MAX, zfar);
// this goes into the projection matrix, so flag it too
pbgl.view.dirty = GL_FALSE;
pbgl.mtx_any_dirty = pbgl.mtx[MTX_PROJECTION].dirty = GL_TRUE;
}
if (pbgl.lightmodel.dirty) {
p = push_command_parameter(p, NV097_SET_LIGHT_CONTROL,
PBGL_MASK(NV097_SET_LIGHT_CONTROL_SEPARATE_SPECULAR_EN, GL_FALSE) | // TODO: this is probably a GL setting
PBGL_MASK(NV097_SET_LIGHT_CONTROL_LOCALEYE, pbgl.lightmodel.local) |
PBGL_MASK(NV097_SET_LIGHT_CONTROL_SOUT, NV097_SET_LIGHT_CONTROL_SOUT_ZERO_OUT));
p = push_command_boolean(p, NV097_SET_TWO_SIDE_LIGHT_EN, pbgl.lightmodel.twosided);
// set both front and back colors just in case
p = push_command(p, NV097_SET_SCENE_AMBIENT_COLOR, 3);
p = push_floats(p, pbgl.lightmodel.ambient.v, 3);
p = push_command(p, NV097_SET_BACK_SCENE_AMBIENT_COLOR, 3);
p = push_floats(p, pbgl.lightmodel.ambient.v, 3);
pbgl.lightmodel.dirty = GL_FALSE;
}
for (int i = 0; i < 2; ++i) {
if (pbgl.material[i].dirty) {
// TODO:
pbgl.material[i].dirty = GL_FALSE;
}
}
pb_end(p);
if (pbgl.light_any_dirty) {
p = pb_begin();
p = push_command_boolean(p, NV097_SET_LIGHTING_ENABLE, pbgl.flags.lighting);
pb_end(p);
if (pbgl.flags.lighting)
pbgl_light_flush_all();
pbgl.light_any_dirty = GL_FALSE;
}
if (pbgl.tex_any_dirty) {
p = pb_begin();
for (GLuint i = 0; i < TEXUNIT_COUNT; ++i)
p = flush_texunit(p, i);
pb_end(p);
pbgl.tex_any_dirty = GL_FALSE;
}
if (pbgl.mtx_any_dirty) {
p = pb_begin();
if (pbgl.mtx[MTX_PROJECTION].dirty || pbgl.mtx[MTX_MODELVIEW].dirty) {
mat4f tmp;
mat4_mul(&tmp, &pbgl.view.mtx, &pbgl.mtx[MTX_PROJECTION].mtx);
if (pbgl.mtx[MTX_PROJECTION].dirty)
p = push_command_matrix4x4_transposed(p, NV097_SET_PROJECTION_MATRIX, tmp.v);
if (pbgl.mtx[MTX_MODELVIEW].dirty) {
// inverse modelview matrix only required for lighting, only calculate it when required
// lighting toggle will set the dirty flag if needed
if (pbgl.flags.lighting) {
mat4f invmv;
mat4_invert(&invmv, &pbgl.mtx[MTX_MODELVIEW].mtx);
p = push_command_matrix4x4(p, NV097_SET_INVERSE_MODEL_VIEW_MATRIX, invmv.v);
}
p = push_command_matrix4x4_transposed(p, NV097_SET_MODEL_VIEW_MATRIX, pbgl.mtx[MTX_MODELVIEW].mtx.v);
}
mat4_mul(&tmp, &tmp, &pbgl.mtx[MTX_MODELVIEW].mtx);
p = push_command_matrix4x4_transposed(p, NV097_SET_COMPOSITE_MATRIX, tmp.v);
pbgl.mtx[MTX_PROJECTION].dirty = pbgl.mtx[MTX_MODELVIEW].dirty = GL_FALSE;
}
pb_end(p);
if (pbgl.mtx[MTX_TEXTURE].dirty) {
// push same texture matrix for all textures
// FIXME: each texunit should have its own
p = pb_begin();
for (GLuint i = 0, ofs = 0; i < TEXUNIT_COUNT; ++i, ofs += 4 * 4 * 4)
if (pbgl.tex[i].enabled)
p = push_command_matrix4x4(p, NV097_SET_TEXTURE_MATRIX + ofs, pbgl.mtx[MTX_TEXTURE].mtx.v);
pb_end(p);
pbgl.mtx[MTX_TEXTURE].dirty = GL_FALSE;
}
pbgl.mtx_any_dirty = GL_FALSE;
}
if (pbgl.texenv_dirty) {
p = pb_begin();
p = pbgl_texenv_push(p);
pb_end(p);
pbgl.texenv_dirty = GL_FALSE;
}
pbgl.state_dirty = GL_FALSE;
return GL_TRUE;
}
static inline void set_feature(GLenum feature, GLboolean value) {
if (pbgl.imm.active) {
// can't do this during model creation
pbgl_set_error(GL_INVALID_OPERATION);
return;
}
GLuint idx = 0;
switch (feature) {
case GL_ALPHA_TEST:
FLAG_DIRTY_IF_CHANGED(alpha, alpha_test, value);
pbgl.flags.alpha_test = value;
break;
case GL_DEPTH_TEST:
FLAG_DIRTY_IF_CHANGED(depth, depth_test, value);
pbgl.flags.depth_test = value;
break;
case GL_SCISSOR_TEST:
FLAG_DIRTY_IF_CHANGED(scissor, scissor_test, value);
pbgl.flags.scissor_test = value;
break;
case GL_STENCIL_TEST:
FLAG_DIRTY_IF_CHANGED(stencil, stencil_test, value);
pbgl.flags.stencil_test = value;
break;
case GL_BLEND:
FLAG_DIRTY_IF_CHANGED(blend, blend, value);
pbgl.flags.blend = value;
break;
case GL_CULL_FACE:
FLAG_DIRTY_IF_CHANGED(cullface, cull_face, value);
pbgl.flags.cull_face = value;
break;
case GL_DITHER:
// TODO
pbgl.flags.dither = value;
break;
case GL_FOG:
FLAG_DIRTY_IF_CHANGED(fog, fog, value);
pbgl.flags.fog = value;
break;
case GL_LIGHT0 ... GL_LIGHT7:
idx = feature - GL_LIGHT0;
if (pbgl.light[idx].enabled != value)
pbgl.state_dirty = pbgl.light_any_dirty = pbgl.light[idx].dirty = GL_TRUE;
pbgl.light[idx].enabled = value;
break;
case GL_LIGHTING:
if (pbgl.flags.lighting != value)
pbgl.state_dirty = pbgl.light_any_dirty = GL_TRUE;
pbgl.flags.lighting = value;
// if we're enabling lighting, we'll need to upload the inverse modelview matrix
if (value) pbgl.mtx_any_dirty = pbgl.mtx[MTX_MODELVIEW].dirty = GL_TRUE;
break;
case GL_POLYGON_OFFSET_FILL:
FLAG_DIRTY_IF_CHANGED(polyofs, poly_offset, value);
pbgl.flags.poly_offset = value;
break;
case GL_TEXTURE_1D:
if (pbgl.tex[pbgl.active_tex_sv].flags.texture_1d != value) {
pbgl.tex_any_dirty = GL_TRUE;
pbgl.texenv_dirty = GL_TRUE;
pbgl.state_dirty = GL_TRUE;
pbgl.tex[pbgl.active_tex_sv].dirty = GL_TRUE;
}
pbgl.tex[pbgl.active_tex_sv].flags.texture_1d = value;
break;
case GL_TEXTURE_2D:
if (pbgl.tex[pbgl.active_tex_sv].flags.texture_2d != value) {
pbgl.tex_any_dirty = GL_TRUE;
pbgl.texenv_dirty = GL_TRUE;
pbgl.state_dirty = GL_TRUE;
pbgl.tex[pbgl.active_tex_sv].dirty = GL_TRUE;
}
pbgl.tex[pbgl.active_tex_sv].flags.texture_2d = value;
break;
case GL_TEXTURE_GEN_S:
pbgl.flags.texgen_s = value;
break;
case GL_TEXTURE_GEN_T:
pbgl.flags.texgen_t = value;
break;
case GL_TEXTURE_GEN_R:
pbgl.flags.texgen_r = value;
break;
case GL_TEXTURE_GEN_Q:
pbgl.flags.texgen_q = value;
break;
case GL_LINE_SMOOTH:
if (pbgl.flags.line_smooth != value) {
pbgl.flags.line_smooth = value;
pbgl.line.dirty = GL_TRUE;
}
break;
default:
pbgl_set_error(GL_INVALID_ENUM);
break;
}
}
static inline void set_client_state(GLenum state, GLboolean value) {
if (pbgl.imm.active) {
// can't do this during model creation
pbgl_set_error(GL_INVALID_OPERATION);
return;
}
const GLenum arr = glarr_to_varr(state);
if (arr == GL_INVALID_ENUM) {
pbgl_set_error(GL_INVALID_ENUM);
return;
}
if (arr == VARR_TEXCOORD) {
pbgl.tex[pbgl.active_tex_cl].varray.enabled = value;
pbgl.tex[pbgl.active_tex_cl].varray.dirty = GL_TRUE;
}
pbgl.varray[arr].enabled = value;
pbgl.varray[arr].dirty = GL_TRUE;
}
/* GL FUNCTIONS BEGIN */
GL_API void glEnable(GLenum feature) {
set_feature(feature, GL_TRUE);
}
GL_API void glDisable(GLenum feature) {
set_feature(feature, GL_FALSE);
}
GL_API GLboolean glIsEnabled(GLenum feature) {
if (pbgl.imm.active) {
// can't do this during model creation
pbgl_set_error(GL_INVALID_OPERATION);
return GL_FALSE;
}
switch (feature) {
case GL_ALPHA_TEST: return pbgl.flags.alpha_test;
case GL_DEPTH_TEST: return pbgl.flags.depth_test;
case GL_SCISSOR_TEST: return pbgl.flags.scissor_test;
case GL_STENCIL_TEST: return pbgl.flags.stencil_test;
case GL_BLEND: return pbgl.flags.blend;
case GL_CULL_FACE: return pbgl.flags.cull_face;
case GL_DITHER: return pbgl.flags.dither;
case GL_FOG: return pbgl.flags.fog;
case GL_LIGHT0 ... GL_LIGHT7: return pbgl.light[feature - GL_LIGHT0].enabled;
case GL_LIGHTING: return pbgl.flags.lighting;
case GL_POLYGON_OFFSET_FILL: return pbgl.flags.poly_offset;
case GL_TEXTURE_1D: return pbgl.tex[pbgl.active_tex_sv].flags.texture_1d;
case GL_TEXTURE_2D: return pbgl.tex[pbgl.active_tex_sv].flags.texture_2d;
case GL_TEXTURE_GEN_S: return pbgl.flags.texgen_s;
case GL_TEXTURE_GEN_T: return pbgl.flags.texgen_t;
case GL_TEXTURE_GEN_R: return pbgl.flags.texgen_r;
case GL_TEXTURE_GEN_Q: return pbgl.flags.texgen_q;
case GL_LINE_SMOOTH: return pbgl.flags.line_smooth;
default:
pbgl_set_error(GL_INVALID_ENUM);
return GL_FALSE;
}
}
GL_API void glEnableClientState(GLenum state) {
set_client_state(state, GL_TRUE);
}
GL_API void glDisableClientState(GLenum state) {
set_client_state(state, GL_FALSE);
}
// technically all of these should produce various errors, but fuck all those branches
GL_API void glAlphaFunc(GLenum func, GLclampf ref) {
const GLubyte uref = ref * 255.f;
if (pbgl.alpha.func != func || pbgl.alpha.ref != uref)
pbgl.state_dirty = pbgl.alpha.dirty = GL_TRUE;
pbgl.alpha.func = func & 0xF;
pbgl.alpha.ref = uref;
}
GL_API void glBlendColor(GLclampf rf, GLclampf gf, GLclampf bf, GLclampf af) {
const GLubyte r = rf * 255.f;
const GLubyte g = gf * 255.f;
const GLubyte b = bf * 255.f;
const GLubyte a = af * 255.f;
const GLuint color = (a << 24) | (r << 16) | (g << 8) | b;
if (pbgl.blend.color != color)
pbgl.state_dirty = pbgl.blend.dirty = GL_TRUE;
pbgl.blend.color = color;
}
GL_API void glBlendEquation(GLenum eqn) {
if (pbgl.blend.eqn != eqn)
pbgl.state_dirty = pbgl.blend.dirty = GL_TRUE;
pbgl.blend.eqn = eqn;
}
GL_API void glBlendFunc(GLenum sfactor, GLenum dfactor) {
if (pbgl.blend.src != sfactor || pbgl.blend.dst != dfactor)
pbgl.state_dirty = pbgl.blend.dirty = GL_TRUE;
pbgl.blend.src = sfactor;
pbgl.blend.dst = dfactor;
}
GL_API void glClearColor(GLclampf rf, GLclampf gf, GLclampf bf, GLclampf af) {
const GLubyte r = rf * 255.f;
const GLubyte g = gf * 255.f;
const GLubyte b = bf * 255.f;
const GLubyte a = af * 255.f;
pbgl.clear_color = (a << 24) | (r << 16) | (g << 8) | b;
}
GL_API void glClearDepth(GLclampd val) {
const GLuint uval = (GLuint)(val * PBGL_Z_MAX);
pbgl.clear_zstencil = (pbgl.clear_zstencil & 0x000000FF) | (uval << 8);
}
GL_API void glClearStencil(GLint val) {
const GLuint uval = ((GLuint)val) & 0xFF;
pbgl.clear_zstencil = (pbgl.clear_zstencil & 0xFFFFFF00) | uval;
}
GL_API void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a) {
const GLuint oldmask = pbgl.colormask.value;
pbgl.colormask.value = 0;
if (r) pbgl.colormask.value |= NV097_SET_COLOR_MASK_RED_WRITE_ENABLE;
if (g) pbgl.colormask.value |= NV097_SET_COLOR_MASK_GREEN_WRITE_ENABLE;
if (b) pbgl.colormask.value |= NV097_SET_COLOR_MASK_BLUE_WRITE_ENABLE;
if (a) pbgl.colormask.value |= NV097_SET_COLOR_MASK_ALPHA_WRITE_ENABLE;
if (oldmask != pbgl.colormask.value)
pbgl.state_dirty = pbgl.colormask.dirty = GL_TRUE;
}
GL_API void glCullFace(GLenum face) {
if (pbgl.cullface.cullface != face)
pbgl.state_dirty = pbgl.cullface.dirty = GL_TRUE;
pbgl.cullface.cullface = face;
}
GL_API void glDepthFunc(GLenum func) {
if (pbgl.depth.func != func)
pbgl.state_dirty = pbgl.depth.dirty = GL_TRUE;
pbgl.depth.func = func & 0xF;
}
GL_API void glDepthMask(GLboolean write) {
if (pbgl.depth.writemask != write)
pbgl.state_dirty = pbgl.depth.dirty = GL_TRUE;
pbgl.depth.writemask = write;
}
GL_API void glDepthRange(GLclampd znear, GLclampd zfar) {
if (pbgl.view.znear != znear || pbgl.view.zfar != zfar)
pbgl.state_dirty = pbgl.view.dirty = GL_TRUE;
pbgl.view.znear = znear;
pbgl.view.zfar = zfar;
}
GL_API void glFrontFace(GLenum face) {
if (pbgl.cullface.frontface != face)
pbgl.state_dirty = pbgl.cullface.dirty = GL_TRUE;
pbgl.cullface.frontface = face;
}
GL_API void glLineWidth(GLfloat width) {
// TODO:
}
GL_API void glPixelStorei(GLenum pname, GLint param) {
// TODO:
}
GL_API void glPolygonMode(GLenum face, GLenum mode) {
if (pbgl.imm.active) {
// can't do this during model creation
pbgl_set_error(GL_INVALID_OPERATION);
return;
}
// Front and back seem to use the same defines?
uint32_t xbox_mode = NV097_SET_FRONT_POLYGON_MODE_V_FILL;
switch (mode) {
case GL_POINT:
xbox_mode = NV097_SET_FRONT_POLYGON_MODE_V_POINT;
break;
case GL_LINE:
xbox_mode = NV097_SET_FRONT_POLYGON_MODE_V_LINE;
break;
case GL_FILL:
// Default value
break;
default:
pbgl_set_error(GL_INVALID_ENUM);
return;
}
switch (face) {
case GL_FRONT:
pbgl.polymode.front_mode = xbox_mode;
break;
case GL_BACK:
pbgl.polymode.back_mode = xbox_mode;
break;
case GL_FRONT_AND_BACK:
pbgl.polymode.front_mode = xbox_mode;
pbgl.polymode.back_mode = xbox_mode;
break;
default:
pbgl_set_error(GL_INVALID_ENUM);
return;
}
pbgl.polymode.dirty = GL_TRUE;
}
GL_API void glPointSize(GLfloat size) {
// TODO:
}
GL_API void glPolygonOffset(GLfloat factor, GLfloat units) {
if (pbgl.polyofs.factor != factor || pbgl.polyofs.units != units)
pbgl.state_dirty = pbgl.polyofs.dirty = GL_TRUE;
pbgl.polyofs.factor = factor;
pbgl.polyofs.units = units;
}
GL_API void glScissor(GLint x, GLint y, GLint width, GLint height) {
pbgl.scissor.x = x;
pbgl.scissor.y = y;
pbgl.scissor.w = width;
pbgl.scissor.h = height;
pbgl.state_dirty = pbgl.scissor.dirty = GL_TRUE;
}
GL_API void glShadeModel(GLenum model) {
// TODO
}
GL_API void glStencilFunc(GLenum func, GLint ref, GLuint mask) {
if (pbgl.stencil.func != func || pbgl.stencil.ref != ref || pbgl.stencil.funcmask != mask)
pbgl.state_dirty = pbgl.stencil.dirty = GL_TRUE;
pbgl.stencil.func = func & 0xF;
pbgl.stencil.ref = ref;
pbgl.stencil.funcmask = mask;
}
GL_API void glStencilMask(GLuint mask) {
if (pbgl.stencil.writemask != mask)
pbgl.state_dirty = pbgl.stencil.dirty = GL_TRUE;
pbgl.stencil.writemask = mask;
}
GL_API void glStencilOp(GLenum sfail, GLenum zfail, GLenum zpass) {
if (pbgl.stencil.op_sfail != sfail || pbgl.stencil.op_zfail != zfail || pbgl.stencil.op_zpass != zpass)
pbgl.state_dirty = pbgl.stencil.dirty = GL_TRUE;
pbgl.stencil.op_sfail = sfail;
pbgl.stencil.op_zfail = zfail;
pbgl.stencil.op_zpass = zpass;
}
GL_API void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) {
if (pbgl.view.x != x || pbgl.view.y != y || pbgl.view.w != width || pbgl.view.h != height)
pbgl.state_dirty = pbgl.view.dirty = GL_TRUE;
pbgl.view.x = x;
pbgl.view.y = y;
pbgl.view.w = width;
pbgl.view.h = height;
}