summaryrefslogtreecommitdiff
path: root/src/VBox/GuestHost/OpenGL/state_tracker/state_buffer.c
blob: 90917ab5bedb74c53867a4b906396b4f0e9ea3a8 (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
/* Copyright (c) 2001, Stanford University
 * All rights reserved
 *
 * See the file LICENSE.txt for information on redistributing this software.
 */

#include <stdio.h>
#include "state.h"
#include "state/cr_statetypes.h"
#include "state_internals.h"

void crStateBufferInit (CRContext *ctx)
{
    CRBufferState *b = &ctx->buffer;
    CRStateBits *sb          = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);
    GLcolorf zero_colorf = {0.0f, 0.0f, 0.0f, 0.0f};

    b->width = 640;
    b->height = 480;
    b->storedWidth = 0;
    b->storedHeight = 0;
    b->pFrontImg = NULL;
    b->pBackImg = NULL;

    b->depthTest = GL_FALSE;
    b->blend     = GL_FALSE;
    b->alphaTest = GL_FALSE;
    b->dither    = GL_TRUE;
    RESET(bb->enable, ctx->bitid);

    b->logicOp   = GL_FALSE;
    RESET(bb->logicOp, ctx->bitid);
    b->indexLogicOp   = GL_FALSE;
    RESET(bb->indexLogicOp, ctx->bitid);
    b->depthMask = GL_TRUE;
    RESET(bb->depthMask, ctx->bitid);

    b->alphaTestFunc = GL_ALWAYS;
    b->alphaTestRef = 0;
    RESET(bb->alphaFunc, ctx->bitid);
    b->depthFunc = GL_LESS;
    RESET(bb->depthFunc, ctx->bitid);
    b->blendSrcRGB = GL_ONE;
    b->blendDstRGB = GL_ZERO;
    RESET(bb->blendFunc, ctx->bitid);
#ifdef CR_EXT_blend_func_separate
    b->blendSrcA = GL_ONE;
    b->blendDstA = GL_ZERO;
    RESET(bb->blendFuncSeparate, ctx->bitid);
#endif
    b->logicOpMode = GL_COPY;
    b->drawBuffer = GL_BACK;
    RESET(bb->drawBuffer, ctx->bitid);
    b->readBuffer = GL_BACK;
    RESET(bb->readBuffer, ctx->bitid);
    b->indexWriteMask = 0xffffffff;
    RESET(bb->indexMask, ctx->bitid);
    b->colorWriteMask.r = GL_TRUE;
    b->colorWriteMask.g = GL_TRUE;
    b->colorWriteMask.b = GL_TRUE;
    b->colorWriteMask.a = GL_TRUE;
    RESET(bb->colorWriteMask, ctx->bitid);
    b->colorClearValue = zero_colorf;
    RESET(bb->clearColor, ctx->bitid);
    b->indexClearValue = 0;
    RESET(bb->clearIndex, ctx->bitid);
    b->depthClearValue = (GLdefault) 1.0;
    RESET(bb->clearDepth, ctx->bitid);
    b->accumClearValue = zero_colorf;
    RESET(bb->clearAccum, ctx->bitid);

#ifdef CR_EXT_blend_color
    b->blendColor = zero_colorf;
    RESET(bb->blendColor, ctx->bitid);
#endif
#if defined(CR_EXT_blend_minmax) || defined(CR_EXT_blend_subtract) || defined(CR_EXT_blend_logic_op)
    b->blendEquation = GL_FUNC_ADD_EXT;
    RESET(bb->blendEquation, ctx->bitid);
#endif

    RESET(bb->dirty, ctx->bitid);
}

void STATE_APIENTRY crStateAlphaFunc (GLenum func, GLclampf ref) 
{
    CRContext *g             = GetCurrentContext();
    CRBufferState *b         = &(g->buffer);
    CRStateBits *sb          = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glAlphaFunc called in begin/end");
        return;
    }

    FLUSH();

    switch (func) 
    {
        case GL_NEVER:
        case GL_LESS:
        case GL_EQUAL:
        case GL_LEQUAL:
        case GL_GREATER:
        case GL_GEQUAL:
        case GL_NOTEQUAL:
        case GL_ALWAYS:
            break;
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glAlphaFunc:  Invalid func: %d", func);
            return;
    }

    if (ref < 0.0f) ref = 0.0f;
    if (ref > 1.0f) ref = 1.0f;

    b->alphaTestFunc = func;
    b->alphaTestRef = ref;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->alphaFunc, g->neg_bitid);
}

void STATE_APIENTRY crStateDepthFunc (GLenum func) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDepthFunc called in begin/end");
        return;
    }

    FLUSH();

    switch (func) 
    {
        case GL_NEVER:
        case GL_LESS:
        case GL_EQUAL:
        case GL_LEQUAL:
        case GL_GREATER:
        case GL_NOTEQUAL:
        case GL_GEQUAL:
        case GL_ALWAYS:
            break;
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glDepthFunc:  Invalid func: %d", func);
            return;
    }


    b->depthFunc = func;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->depthFunc, g->neg_bitid);
}

void STATE_APIENTRY crStateBlendFunc (GLenum sfactor, GLenum dfactor) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBlendFunc called in begin/end");
        return;
    }

    FLUSH();

    switch (sfactor) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_DST_COLOR:
        case GL_ONE_MINUS_DST_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
        case GL_SRC_ALPHA_SATURATE:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactor passed to glBlendFunc: %d", sfactor);
            return;
    }

    switch (dfactor) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_SRC_COLOR:
        case GL_ONE_MINUS_SRC_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactor passed to glBlendFunc: %d", dfactor);
            return;
    }

    b->blendSrcRGB = sfactor;
    b->blendDstRGB = dfactor;
    b->blendSrcA = sfactor;
    b->blendDstA = dfactor;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->blendFunc, g->neg_bitid);
}

void STATE_APIENTRY crStateBlendColorEXT( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendColorEXT called inside a Begin/End" );
        return;
    }

    b->blendColor.r = red;
    b->blendColor.g = green;
    b->blendColor.b = blue;
    b->blendColor.a = alpha;
    DIRTY(bb->blendColor, g->neg_bitid);
    DIRTY(bb->dirty, g->neg_bitid);
}

#ifdef CR_EXT_blend_func_separate
void STATE_APIENTRY crStateBlendFuncSeparateEXT( GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorA, GLenum dfactorA )
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendFuncSeparateEXT called inside a Begin/End" );
        return;
    }

    FLUSH();

    switch (sfactorRGB) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_DST_COLOR:
        case GL_ONE_MINUS_DST_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
        case GL_SRC_ALPHA_SATURATE:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorRGB passed to glBlendFuncSeparateEXT: %d", sfactorRGB);
            return;
    }

    switch (sfactorA) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_DST_COLOR:
        case GL_ONE_MINUS_DST_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
        case GL_SRC_ALPHA_SATURATE:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorA passed to glBlendFuncSeparateEXT: %d", sfactorA);
            return;
    }

    switch (dfactorRGB) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_SRC_COLOR:
        case GL_DST_COLOR:
        case GL_ONE_MINUS_DST_COLOR:
        case GL_ONE_MINUS_SRC_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
        case GL_SRC_ALPHA_SATURATE:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorRGB passed to glBlendFuncSeparateEXT: %d", dfactorRGB);
            return;
    }

    switch (dfactorA) 
    {
        case GL_ZERO:
        case GL_ONE:
        case GL_DST_COLOR:
        case GL_SRC_COLOR:
        case GL_ONE_MINUS_SRC_COLOR:
        case GL_ONE_MINUS_DST_COLOR:
        case GL_SRC_ALPHA:
        case GL_ONE_MINUS_SRC_ALPHA:
        case GL_DST_ALPHA:
        case GL_ONE_MINUS_DST_ALPHA:
        case GL_SRC_ALPHA_SATURATE:
            break; /* OK */
#ifdef CR_EXT_blend_color
        case GL_CONSTANT_COLOR_EXT:
        case GL_ONE_MINUS_CONSTANT_COLOR_EXT:
        case GL_CONSTANT_ALPHA_EXT:
        case GL_ONE_MINUS_CONSTANT_ALPHA_EXT:
            if (g->extensions.EXT_blend_color)
                break; /* OK */
#endif
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorA passed to glBlendFuncSeparateEXT: %d", dfactorA);
            return;
    }

    b->blendSrcRGB = sfactorRGB;
    b->blendDstRGB = dfactorRGB;
    b->blendSrcA = sfactorA;
    b->blendDstA = dfactorA;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->blendFuncSeparate, g->neg_bitid);
}
#endif

void STATE_APIENTRY crStateBlendEquationEXT( GLenum mode )
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if( g->current.inBeginEnd )
    {
        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendEquationEXT called inside a Begin/End" );
        return;
    }
    switch( mode )
    {
#if defined(CR_EXT_blend_minmax) || defined(CR_EXT_blend_subtract) || defined(CR_EXT_blend_logic_op)
        case GL_FUNC_ADD_EXT:
#ifdef CR_EXT_blend_subtract
        case GL_FUNC_SUBTRACT_EXT:
        case GL_FUNC_REVERSE_SUBTRACT_EXT:
#endif /* CR_EXT_blend_subtract */
#ifdef CR_EXT_blend_minmax
        case GL_MIN_EXT:
        case GL_MAX_EXT:
#endif /* CR_EXT_blend_minmax */
#ifdef CR_EXT_blend_logic_op
        case GL_LOGIC_OP:
#endif /* CR_EXT_blend_logic_op */
            b->blendEquation = mode;
            break;
#endif /* defined(CR_EXT_blend_minmax) || defined(CR_EXT_blend_subtract) || defined(CR_EXT_blend_logic_op) */
        default:
            crStateError( __LINE__, __FILE__, GL_INVALID_ENUM,
                "BlendEquationEXT: mode called with illegal parameter: 0x%x", (GLenum) mode );
            return;
    }
    DIRTY(bb->blendEquation, g->neg_bitid);
    DIRTY(bb->dirty, g->neg_bitid);
}

void STATE_APIENTRY crStateLogicOp (GLenum opcode) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glLogicOp called in begin/end");
        return;
    }

    FLUSH();

    switch (opcode) 
    {
        case GL_CLEAR:
        case GL_SET:
        case GL_COPY:
        case GL_COPY_INVERTED:
        case GL_NOOP:
        case GL_INVERT:
        case GL_AND:
        case GL_NAND:
        case GL_OR:
        case GL_NOR:
        case GL_XOR:
        case GL_EQUIV:
        case GL_AND_REVERSE:
        case GL_AND_INVERTED:
        case GL_OR_REVERSE:
        case GL_OR_INVERTED:
            break;
        default:
            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glLogicOp called with bogus opcode: %d", opcode);
            return;
    }

    b->logicOpMode = opcode;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->logicOp, g->neg_bitid);
    DIRTY(bb->indexLogicOp, g->neg_bitid);
}

void STATE_APIENTRY crStateDrawBuffer (GLenum mode) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer called in begin/end");
        return;
    }

    FLUSH();

    switch (mode) 
    {
        case GL_NONE:
            break;
        case GL_FRONT_LEFT:
        case GL_FRONT_RIGHT:
        case GL_BACK_LEFT:
        case GL_BACK_RIGHT:
        case GL_FRONT:
        case GL_BACK:
        case GL_LEFT:
        case GL_RIGHT:
        case GL_FRONT_AND_BACK:
        case GL_AUX0:
        case GL_AUX1:
        case GL_AUX2:
        case GL_AUX3:
            if (g->framebufferobject.drawFB)
            {
                crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is active");
                return;
            }
            break;
        default:
            if (mode>=GL_COLOR_ATTACHMENT0_EXT && mode<=GL_COLOR_ATTACHMENT15_EXT)
            {
                if (!g->framebufferobject.drawFB)
                {
                    crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is inactive");
                    return;
                }
            }
            else
            {
                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glDrawBuffer called with bogus mode: %d", mode);
                return;
            }
    }

    if (g->framebufferobject.drawFB)
    {
        g->framebufferobject.drawFB->drawbuffer[0] = mode;
    }
    else
    {
        b->drawBuffer = mode;
        DIRTY(bb->dirty, g->neg_bitid);
        DIRTY(bb->drawBuffer, g->neg_bitid);
    }
}

void STATE_APIENTRY crStateReadBuffer (GLenum mode) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sb = GetCurrentBits();
    CRBufferBits *bb = &(sb->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
        return;
    }

    FLUSH();

    switch (mode) 
    {
        case GL_NONE:
            break;
        case GL_FRONT_LEFT:
        case GL_FRONT_RIGHT:
        case GL_BACK_LEFT:
        case GL_BACK_RIGHT:
        case GL_FRONT:
        case GL_BACK:
        case GL_LEFT:
        case GL_RIGHT:
        case GL_FRONT_AND_BACK:
        case GL_AUX0:
        case GL_AUX1:
        case GL_AUX2:
        case GL_AUX3:
            if (g->framebufferobject.readFB)
            {
                crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is active");
                return;
            }
            break;
        default:
            if (mode>=GL_COLOR_ATTACHMENT0_EXT && mode<=GL_COLOR_ATTACHMENT15_EXT)
            {
                if (!g->framebufferobject.readFB)
                {
                    crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is inactive");
                    return;
                }
                else
                {
                    /*@todo, check if fbo binding is complete*/
                }
            }
            else
            {
                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glReadBuffer called with bogus mode: %d", mode);
                return;
            }
    }

    if (g->framebufferobject.readFB)
    {
        g->framebufferobject.readFB->readbuffer = mode;
    }
    else
    {
        b->readBuffer = mode;
        DIRTY(bb->dirty, g->neg_bitid);
        DIRTY(bb->readBuffer, g->neg_bitid);
    }
}

void STATE_APIENTRY crStateIndexMask (GLuint mask) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
        return;
    }

    FLUSH();

    b->indexWriteMask = mask;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->indexMask, g->neg_bitid);
}

void STATE_APIENTRY crStateColorMask (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
        return;
    }

    FLUSH();

    b->colorWriteMask.r = red;
    b->colorWriteMask.g = green;
    b->colorWriteMask.b = blue;
    b->colorWriteMask.a = alpha;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->colorWriteMask, g->neg_bitid);
}

void STATE_APIENTRY crStateClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearColor called in begin/end");
        return;
    }

    FLUSH();

    if (red < 0.0f) red = 0.0f;
    if (red > 1.0f) red = 1.0f;
    if (green < 0.0f) green = 0.0f;
    if (green > 1.0f) green = 1.0f;
    if (blue < 0.0f) blue = 0.0f;
    if (blue > 1.0f) blue = 1.0f;
    if (alpha < 0.0f) alpha = 0.0f;
    if (alpha > 1.0f) alpha = 1.0f;

    b->colorClearValue.r = red;
    b->colorClearValue.g = green;
    b->colorClearValue.b = blue;
    b->colorClearValue.a = alpha;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->clearColor, g->neg_bitid);
}

void STATE_APIENTRY crStateClearIndex (GLfloat c) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearIndex called in begin/end");
        return;
    }

    b->indexClearValue = c;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->clearIndex, g->neg_bitid);
}

void STATE_APIENTRY crStateClearDepth (GLclampd depth) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearDepth called in begin/end");
        return;
    }

    FLUSH();

    if (depth < 0.0) depth = 0.0;
    if (depth > 1.0) depth = 1.0;

    b->depthClearValue = (GLdefault) depth;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->clearDepth, g->neg_bitid);
}

void STATE_APIENTRY crStateClearAccum (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *b = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearAccum called in begin/end");
        return;
    }
    
    FLUSH();

    if (red < -1.0f) red = 0.0f;
    if (red > 1.0f) red = 1.0f;
    if (green < -1.0f) green = 0.0f;
    if (green > 1.0f) green = 1.0f;
    if (blue < -1.0f) blue = 0.0f;
    if (blue > 1.0f) blue = 1.0f;
    if (alpha < -1.0f) alpha = 0.0f;
    if (alpha > 1.0f) alpha = 1.0f;

    b->accumClearValue.r = red;
    b->accumClearValue.g = green;
    b->accumClearValue.b = blue;
    b->accumClearValue.a = alpha;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->clearAccum, g->neg_bitid);
}

void STATE_APIENTRY crStateDepthMask (GLboolean b) 
{
    CRContext *g = GetCurrentContext();
    CRBufferState *bs = &(g->buffer);
    CRStateBits *sp = GetCurrentBits();
    CRBufferBits *bb = &(sp->buffer);

    if (g->current.inBeginEnd)
    {
        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "DepthMask called in begin/end");
        return;
    }

    FLUSH();

    bs->depthMask = b;
    DIRTY(bb->dirty, g->neg_bitid);
    DIRTY(bb->depthMask, g->neg_bitid);
}