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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
|
/*
* Mesa 3-D graphics library
* Version: 6.3
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Authors:
* Brian Paul
*/
#include "context.h"
#include "fbobject.h"
#include "hash.h"
#include "teximage.h"
#include "texstore.h"
/*
* When glGenRender/FramebuffersEXT() is called we insert pointers to
* these placeholder objects into the hash table.
* Later, when the object ID is first bound, we replace the placeholder
* with the real frame/renderbuffer.
*/
static struct gl_frame_buffer_object DummyFramebuffer;
static struct gl_render_buffer_object DummyRenderbuffer;
#define IS_CUBE_FACE(TARGET) \
((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
(TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
/**
* Helper routine for getting a gl_render_buffer_object.
*/
static struct gl_render_buffer_object *
lookup_renderbuffer(GLcontext *ctx, GLuint id)
{
struct gl_render_buffer_object *rb;
if (id == 0)
return NULL;
rb = (struct gl_render_buffer_object *)
_mesa_HashLookup(ctx->Shared->RenderBuffers, id);
return rb;
}
/**
* Helper routine for getting a gl_frame_buffer_object.
*/
static struct gl_frame_buffer_object *
lookup_framebuffer(GLcontext *ctx, GLuint id)
{
struct gl_frame_buffer_object *fb;
if (id == 0)
return NULL;
fb = (struct gl_frame_buffer_object *)
_mesa_HashLookup(ctx->Shared->FrameBuffers, id);
return fb;
}
/**
* Allocate a new gl_render_buffer_object.
* XXX make this a device driver function.
*/
static struct gl_render_buffer_object *
new_renderbuffer(GLcontext *ctx, GLuint name)
{
struct gl_render_buffer_object *rb = CALLOC_STRUCT(gl_render_buffer_object);
if (rb) {
rb->Name = name;
rb->RefCount = 1;
/* other fields are zero */
}
return rb;
}
/**
* Allocate a new gl_frame_buffer_object.
* XXX make this a device driver function.
*/
static struct gl_frame_buffer_object *
new_framebuffer(GLcontext *ctx, GLuint name)
{
struct gl_frame_buffer_object *fb = CALLOC_STRUCT(gl_frame_buffer_object);
if (fb) {
fb->Name = name;
fb->RefCount = 1;
}
return fb;
}
static struct gl_render_buffer_attachment *
get_attachment(GLcontext *ctx, GLenum attachment)
{
GLuint i;
switch (attachment) {
case GL_COLOR_ATTACHMENT0_EXT:
case GL_COLOR_ATTACHMENT1_EXT:
case GL_COLOR_ATTACHMENT2_EXT:
case GL_COLOR_ATTACHMENT3_EXT:
case GL_COLOR_ATTACHMENT4_EXT:
case GL_COLOR_ATTACHMENT5_EXT:
case GL_COLOR_ATTACHMENT6_EXT:
case GL_COLOR_ATTACHMENT7_EXT:
case GL_COLOR_ATTACHMENT8_EXT:
case GL_COLOR_ATTACHMENT9_EXT:
case GL_COLOR_ATTACHMENT10_EXT:
case GL_COLOR_ATTACHMENT11_EXT:
case GL_COLOR_ATTACHMENT12_EXT:
case GL_COLOR_ATTACHMENT13_EXT:
case GL_COLOR_ATTACHMENT14_EXT:
case GL_COLOR_ATTACHMENT15_EXT:
i = attachment - GL_COLOR_ATTACHMENT0_EXT;
if (i >= ctx->Const.MaxColorAttachments) {
return NULL;
}
return &ctx->CurrentFramebuffer->ColorAttachment[i];
case GL_DEPTH_ATTACHMENT_EXT:
return &ctx->CurrentFramebuffer->DepthAttachment;
case GL_STENCIL_ATTACHMENT_EXT:
return &ctx->CurrentFramebuffer->StencilAttachment;
default:
return NULL;
}
}
static void
remove_attachment(GLcontext *ctx, struct gl_render_buffer_attachment *att)
{
if (att->Type == GL_TEXTURE) {
ASSERT(att->Texture);
ASSERT(!att->Renderbuffer);
att->Texture->RefCount--;
if (att->Texture->RefCount == 0) {
ctx->Driver.DeleteTexture(ctx, att->Texture);
}
att->Texture = NULL;
}
else if (att->Type == GL_RENDERBUFFER_EXT) {
ASSERT(att->Renderbuffer);
ASSERT(!att->Texture);
att->Renderbuffer->RefCount--;
if (att->Renderbuffer->RefCount == 0) {
_mesa_free(att->Renderbuffer); /* XXX driver free */
}
att->Renderbuffer = NULL;
}
att->Type = GL_NONE;
att->Complete = GL_TRUE;
}
static void
set_texture_attachment(GLcontext *ctx,
struct gl_render_buffer_attachment *att,
struct gl_texture_object *texObj,
GLenum texTarget, GLuint level, GLuint zoffset)
{
remove_attachment(ctx, att);
att->Type = GL_TEXTURE;
att->Texture = texObj;
att->TextureLevel = level;
if (IS_CUBE_FACE(texTarget)) {
att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
}
else {
att->CubeMapFace = 0;
}
att->Zoffset = zoffset;
att->Complete = GL_FALSE;
texObj->RefCount++;
}
static void
set_renderbuffer_attachment(GLcontext *ctx,
struct gl_render_buffer_attachment *att,
struct gl_render_buffer_object *rb)
{
remove_attachment(ctx, att);
att->Type = GL_RENDERBUFFER_EXT;
att->Renderbuffer = rb;
att->Complete = GL_FALSE;
rb->RefCount++;
}
/**
* Test if an attachment point is complete and update its Complete field.
* \param format if GL_COLOR, this is a color attachment point,
* if GL_DEPTH, this is a depth component attachment point,
* if GL_STENCIL, this is a stencil component attachment point.
*/
static void
test_attachment_completeness(const GLcontext *ctx, GLenum format,
struct gl_render_buffer_attachment *att)
{
assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
/* assume complete */
att->Complete = GL_TRUE;
if (att->Type == GL_NONE)
return; /* complete */
/* Look for reasons why the attachment might be incomplete */
if (att->Type == GL_TEXTURE) {
struct gl_texture_object *texObj = att->Texture;
struct gl_texture_image *texImage;
assert(texObj);
texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
if (!texImage) {
att->Complete = GL_FALSE;
return;
}
if (texImage->Width < 1 || texImage->Height < 1) {
att->Complete = GL_FALSE;
return;
}
if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
att->Complete = GL_FALSE;
return;
}
if (format == GL_COLOR) {
if (texImage->Format != GL_RGB && texImage->Format != GL_RGBA) {
att->Complete = GL_FALSE;
return;
}
}
else if (format == GL_DEPTH) {
if (texImage->Format != GL_DEPTH_COMPONENT) {
att->Complete = GL_FALSE;
return;
}
}
else {
/* no such thing as stencil textures */
att->Complete = GL_FALSE;
return;
}
}
else {
assert(att->Type == GL_RENDERBUFFER_EXT);
if (att->Renderbuffer->Width < 1 || att->Renderbuffer->Height < 1) {
att->Complete = GL_FALSE;
return;
}
if (format == GL_COLOR) {
if (att->Renderbuffer->_BaseFormat != GL_RGB &&
att->Renderbuffer->_BaseFormat != GL_RGBA) {
att->Complete = GL_FALSE;
return;
}
}
else if (format == GL_DEPTH) {
if (att->Renderbuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
att->Complete = GL_FALSE;
return;
}
}
else {
assert(format == GL_STENCIL);
if (att->Renderbuffer->_BaseFormat != GL_STENCIL_INDEX) {
att->Complete = GL_FALSE;
return;
}
}
}
}
/**
* Test if the given framebuffer object is complete and update its
* Status field with the results.
*/
static void
test_framebuffer_completeness(GLcontext *ctx,
struct gl_frame_buffer_object *fb)
{
GLint i;
GLuint numImages, width, height;
GLenum intFormat;
/* Set to COMPLETE status, then try to find reasons for being incomplete */
fb->Status = GL_FRAMEBUFFER_COMPLETE_EXT;
numImages = 0;
/* Start at -2 to more easily loop over all attachment points */
for (i = -2; i < ctx->Const.MaxColorAttachments; i++) {
struct gl_render_buffer_attachment *att;
GLuint w, h;
GLenum f;
if (i == -2) {
att = &fb->DepthAttachment;
test_attachment_completeness(ctx, GL_DEPTH, att);
if (!att->Complete) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
else if (i == -1) {
att = &fb->StencilAttachment;
test_attachment_completeness(ctx, GL_STENCIL, att);
if (!att->Complete) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
else {
att = &fb->ColorAttachment[i];
test_attachment_completeness(ctx, GL_COLOR, att);
if (!att->Complete) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
return;
}
}
if (att->Type == GL_TEXTURE) {
w = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Width;
h = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Height;
f = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Format;
numImages++;
}
else if (att->Type == GL_RENDERBUFFER_EXT) {
w = att->Renderbuffer->Width;
h = att->Renderbuffer->Height;
f = att->Renderbuffer->InternalFormat;
numImages++;
}
else {
assert(att->Type == GL_NONE);
continue;
}
if (numImages == 1) {
/* set required width, height and format */
width = w;
height = h;
if (i >= 0)
intFormat = f;
}
else {
/* check that width, height, format are same */
if (w != width || h != height) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
return;
}
if (i >= 0 && f != intFormat) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
return;
}
}
}
/* Check that all DrawBuffers are present */
for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
if (fb->DrawBuffer[i] != GL_NONE) {
struct gl_render_buffer_attachment *att
= get_attachment(ctx, fb->DrawBuffer[i]);
if (att->Type == GL_NONE) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
return;
}
}
}
/* Check that the ReadBuffer is present */
if (fb->ReadBuffer != GL_NONE) {
struct gl_render_buffer_attachment *att
= get_attachment(ctx, fb->ReadBuffer);
if (att->Type == GL_NONE) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
return;
}
}
if (numImages == 0) {
fb->Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
return;
}
}
GLboolean GLAPIENTRY
_mesa_IsRenderbufferEXT(GLuint renderbuffer)
{
const struct gl_render_buffer_object *rb;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
rb = lookup_renderbuffer(ctx, renderbuffer);
return rb ? GL_TRUE : GL_FALSE;
}
void GLAPIENTRY
_mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
{
struct gl_render_buffer_object *newRb, *oldRb;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_RENDERBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glBindRenderbufferEXT(target)");
return;
}
if (renderbuffer) {
newRb = lookup_renderbuffer(ctx, renderbuffer);
if (newRb == &DummyRenderbuffer) {
/* ID was reserved, but no real renderbuffer object made yet */
newRb = NULL;
}
if (!newRb) {
/* create new renderbuffer object */
newRb = new_renderbuffer(ctx, renderbuffer);
if (!newRb) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
return;
}
_mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
}
newRb->RefCount++;
}
else {
newRb = NULL;
}
oldRb = ctx->CurrentRenderbuffer;
if (oldRb) {
oldRb->RefCount--;
if (oldRb->RefCount == 0) {
_mesa_free(oldRb); /* XXX device driver function */
}
}
ASSERT(newRb != &DummyRenderbuffer);
ctx->CurrentRenderbuffer = newRb;
}
void GLAPIENTRY
_mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
{
GLint i;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
for (i = 0; i < n; i++) {
if (renderbuffers[i]) {
struct gl_render_buffer_object *rb;
rb = lookup_renderbuffer(ctx, renderbuffers[i]);
if (rb) {
/* remove from hash table immediately, to free the ID */
_mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
if (rb != &DummyRenderbuffer) {
/* But the object will not be freed until it's no longer
* bound in any context.
*/
rb->RefCount--;
if (rb->RefCount == 0) {
_mesa_free(rb); /* XXX call device driver function */
}
}
}
}
}
}
void GLAPIENTRY
_mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
{
GET_CURRENT_CONTEXT(ctx);
GLuint first;
GLint i;
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
return;
}
if (!renderbuffers)
return;
first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
for (i = 0; i < n; i++) {
GLuint name = first + i;
renderbuffers[i] = name;
/* insert dummy placeholder into hash table */
_glthread_LOCK_MUTEX(ctx->Shared->Mutex);
_mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
_glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
}
}
static GLenum
base_internal_format(GLcontext *ctx, GLenum internalFormat)
{
switch (internalFormat) {
case GL_RGB:
case GL_R3_G3_B2:
case GL_RGB4:
case GL_RGB5:
case GL_RGB8:
case GL_RGB10:
case GL_RGB12:
case GL_RGB16:
return GL_RGB;
case GL_RGBA:
case GL_RGBA2:
case GL_RGBA4:
case GL_RGB5_A1:
case GL_RGBA8:
case GL_RGB10_A2:
case GL_RGBA12:
case GL_RGBA16:
return GL_RGBA;
case GL_STENCIL_INDEX:
case GL_STENCIL_INDEX1_EXT:
case GL_STENCIL_INDEX4_EXT:
case GL_STENCIL_INDEX8_EXT:
case GL_STENCIL_INDEX16_EXT:
return GL_STENCIL_INDEX;
case GL_DEPTH_COMPONENT:
case GL_DEPTH_COMPONENT16_SGIX:
case GL_DEPTH_COMPONENT24_SGIX:
case GL_DEPTH_COMPONENT32_SGIX:
return GL_DEPTH_COMPONENT;
default:
return 0;
}
}
void GLAPIENTRY
_mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
GLsizei width, GLsizei height)
{
GLenum baseFormat;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_RENDERBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM, "glRenderbufferStorageEXT(target)");
return;
}
baseFormat = base_internal_format(ctx, internalFormat);
if (baseFormat == 0) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glRenderbufferStorageEXT(internalFormat)");
return;
}
if (width < 1 || width > ctx->Const.MaxRenderbufferSize) {
_mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(width)");
return;
}
if (height < 1 || height > ctx->Const.MaxRenderbufferSize) {
_mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(height)");
return;
}
/* XXX this check isn't in the spec, but seems necessary */
if (!ctx->CurrentRenderbuffer) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferStorageEXT");
return;
}
if (ctx->CurrentRenderbuffer->Data) {
/* XXX device driver free */
_mesa_free(ctx->CurrentRenderbuffer->Data);
}
/* XXX device driver allocate, fix size */
ctx->CurrentRenderbuffer->Data = _mesa_malloc(width * height * 4);
if (ctx->CurrentRenderbuffer->Data == NULL) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glRenderbufferStorageEXT");
return;
}
ctx->CurrentRenderbuffer->InternalFormat = internalFormat;
ctx->CurrentRenderbuffer->Width = width;
ctx->CurrentRenderbuffer->Height = height;
ctx->CurrentRenderbuffer->_BaseFormat = baseFormat;
}
void GLAPIENTRY
_mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
{
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_RENDERBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetRenderbufferParameterivEXT(target)");
return;
}
if (!ctx->CurrentRenderbuffer) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetRenderbufferParameterivEXT");
return;
}
switch (pname) {
case GL_RENDERBUFFER_WIDTH_EXT:
*params = ctx->CurrentRenderbuffer->Width;
return;
case GL_RENDERBUFFER_HEIGHT_EXT:
*params = ctx->CurrentRenderbuffer->Height;
return;
case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
*params = ctx->CurrentRenderbuffer->InternalFormat;
return;
default:
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetRenderbufferParameterivEXT(target)");
return;
}
}
GLboolean GLAPIENTRY
_mesa_IsFramebufferEXT(GLuint framebuffer)
{
const struct gl_frame_buffer_object *fb;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
fb = lookup_framebuffer(ctx, framebuffer);
return fb ? GL_TRUE : GL_FALSE;
}
void GLAPIENTRY
_mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
{
struct gl_frame_buffer_object *newFb, *oldFb;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_FRAMEBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glBindFramebufferEXT(target)");
return;
}
if (framebuffer) {
newFb = lookup_framebuffer(ctx, framebuffer);
if (newFb == &DummyFramebuffer) {
/* ID was reserved, but no real framebuffer object made yet */
newFb = NULL;
}
if (!newFb) {
/* create new framebuffer object */
newFb = new_framebuffer(ctx, framebuffer);
if (!newFb) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
return;
}
_mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
}
newFb->RefCount++;
}
else {
newFb = NULL;
}
oldFb = ctx->CurrentFramebuffer;
if (oldFb) {
oldFb->RefCount--;
if (oldFb->RefCount == 0) {
_mesa_free(oldFb); /* XXX device driver function */
}
}
ASSERT(newFb != &DummyFramebuffer);
ctx->CurrentFramebuffer = newFb;
}
void GLAPIENTRY
_mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
{
GLint i;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
for (i = 0; i < n; i++) {
if (framebuffers[i]) {
struct gl_frame_buffer_object *fb;
fb = lookup_framebuffer(ctx, framebuffers[i]);
if (fb) {
/* remove from hash table immediately, to free the ID */
_mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
if (fb != &DummyFramebuffer) {
/* But the object will not be freed until it's no longer
* bound in any context.
*/
fb->RefCount--;
if (fb->RefCount == 0) {
_mesa_free(fb); /* XXX call device driver function */
}
}
}
}
}
}
void GLAPIENTRY
_mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
{
GET_CURRENT_CONTEXT(ctx);
GLuint first;
GLint i;
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (n < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
return;
}
if (!framebuffers)
return;
first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
for (i = 0; i < n; i++) {
GLuint name = first + i;
framebuffers[i] = name;
/* insert dummy placeholder into hash table */
_glthread_LOCK_MUTEX(ctx->Shared->Mutex);
_mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
_glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
}
}
GLenum GLAPIENTRY
_mesa_CheckFramebufferStatusEXT(GLenum target)
{
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FRAMEBUFFER_STATUS_ERROR_EXT);
if (target != GL_FRAMEBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
return GL_FRAMEBUFFER_STATUS_ERROR_EXT;
}
if (!ctx->CurrentFramebuffer) {
/* The window system / default framebuffer is always complete */
return GL_FRAMEBUFFER_COMPLETE_EXT;
}
test_framebuffer_completeness(ctx, ctx->CurrentFramebuffer);
return ctx->CurrentFramebuffer->Status;
}
/**
* Do error checking common to glFramebufferTexture1D/2D/3DEXT.
* \return GL_TRUE if any error, GL_FALSE otherwise
*/
static GLboolean
error_check_framebuffer_texture(GLcontext *ctx, GLuint dims,
GLenum target, GLenum attachment,
GLenum textarget, GLuint texture, GLint level)
{
ASSERT(dims >= 1 && dims <= 3);
if (target != GL_FRAMEBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferTexture%dDEXT(target)", dims);
return GL_TRUE;
}
if (ctx->CurrentFramebuffer == NULL) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glFramebufferTexture%dDEXT", dims);
return GL_TRUE;
}
/* only check textarget, level if texture ID is non-zero*/
if (texture) {
if ((dims == 1 && textarget != GL_TEXTURE_1D) ||
(dims == 3 && textarget != GL_TEXTURE_3D) ||
(dims == 2 && textarget != GL_TEXTURE_2D &&
textarget != GL_TEXTURE_RECTANGLE_ARB &&
!IS_CUBE_FACE(textarget))) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture%dDEXT(textarget)", dims);
return GL_TRUE;
}
if ((level < 0) || level >= _mesa_max_texture_levels(ctx, textarget)) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture%dDEXT(level)", dims);
return GL_TRUE;
}
}
return GL_FALSE;
}
void GLAPIENTRY
_mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
GLenum textarget, GLuint texture, GLint level)
{
struct gl_render_buffer_attachment *att;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (error_check_framebuffer_texture(ctx, 1, target, attachment,
textarget, texture, level))
return;
ASSERT(textarget == GL_TEXTURE_1D);
att = get_attachment(ctx, attachment);
if (att == NULL) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferTexture1DEXT(attachment)");
return;
}
if (texture) {
struct gl_texture_object *texObj = (struct gl_texture_object *)
_mesa_HashLookup(ctx->Shared->TexObjects, texture);
if (!texObj) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture1DEXT(texture)");
return;
}
if (texObj->Target != textarget) {
_mesa_error(ctx, GL_INVALID_OPERATION, /* XXX correct error? */
"glFramebufferTexture1DEXT(texture target)");
return;
}
set_texture_attachment(ctx, att, texObj, textarget, level, 0);
}
else {
remove_attachment(ctx, att);
}
/* XXX call a driver function to signal new attachment? */
}
void GLAPIENTRY
_mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
GLenum textarget, GLuint texture, GLint level)
{
struct gl_render_buffer_attachment *att;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (error_check_framebuffer_texture(ctx, 2, target, attachment,
textarget, texture, level))
return;
ASSERT(textarget == GL_TEXTURE_2D ||
textarget == GL_TEXTURE_RECTANGLE_ARB ||
IS_CUBE_FACE(textarget));
att = get_attachment(ctx, attachment);
if (att == NULL) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferTexture2DEXT(attachment)");
return;
}
if (texture) {
struct gl_texture_object *texObj = (struct gl_texture_object *)
_mesa_HashLookup(ctx->Shared->TexObjects, texture);
if (!texObj) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture2DEXT(texture)");
return;
}
if ((texObj->Target == GL_TEXTURE_2D && textarget != GL_TEXTURE_2D) ||
(texObj->Target == GL_TEXTURE_RECTANGLE_ARB
&& textarget != GL_TEXTURE_RECTANGLE_ARB) ||
(texObj->Target == GL_TEXTURE_CUBE_MAP
&& !IS_CUBE_FACE(textarget))) {
_mesa_error(ctx, GL_INVALID_OPERATION, /* XXX correct error? */
"glFramebufferTexture2DEXT(texture target)");
return;
}
set_texture_attachment(ctx, att, texObj, textarget, level, 0);
}
else {
remove_attachment(ctx, att);
}
}
void GLAPIENTRY
_mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
GLenum textarget, GLuint texture,
GLint level, GLint zoffset)
{
struct gl_render_buffer_attachment *att;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (error_check_framebuffer_texture(ctx, 3, target, attachment,
textarget, texture, level))
return;
ASSERT(textarget == GL_TEXTURE_3D);
att = get_attachment(ctx, attachment);
if (att == NULL) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferTexture1DEXT(attachment)");
return;
}
if (texture) {
struct gl_texture_object *texObj = (struct gl_texture_object *)
_mesa_HashLookup(ctx->Shared->TexObjects, texture);
if (!texObj) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture3DEXT(texture)");
return;
}
if (texObj->Target != textarget) {
_mesa_error(ctx, GL_INVALID_OPERATION, /* XXX correct error? */
"glFramebufferTexture3DEXT(texture target)");
return;
}
if (zoffset >= texObj->Image[0][level]->Depth) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferTexture3DEXT(zoffset)");
return;
}
set_texture_attachment(ctx, att, texObj, textarget, level, zoffset);
}
else {
remove_attachment(ctx, att);
}
}
void GLAPIENTRY
_mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
GLenum renderbufferTarget,
GLuint renderbuffer)
{
struct gl_render_buffer_attachment *att;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_FRAMEBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferRenderbufferEXT(target)");
return;
}
if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferRenderbufferEXT(renderbufferTarget)");
return;
}
if (ctx->CurrentFramebuffer == NULL) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
return;
}
att = get_attachment(ctx, attachment);
if (att == NULL) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glFramebufferRenderbufferEXT(attachment)");
return;
}
if (renderbuffer) {
struct gl_render_buffer_object *rb;
rb = lookup_renderbuffer(ctx, renderbuffer);
if (!rb) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glFramebufferRenderbufferEXT(renderbuffer)");
return;
}
set_renderbuffer_attachment(ctx, att, rb);
}
else {
remove_attachment(ctx, att);
}
}
void GLAPIENTRY
_mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
GLenum pname, GLint *params)
{
const struct gl_render_buffer_attachment *att;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
if (target != GL_FRAMEBUFFER_EXT) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(target)");
return;
}
if (ctx->CurrentFramebuffer == NULL) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFramebufferAttachmentParameterivEXT");
return;
}
att = get_attachment(ctx, attachment);
if (att == NULL) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(attachment)");
return;
}
switch (pname) {
case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
*params = att->Type;
return;
case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
if (att->Type == GL_RENDERBUFFER_EXT) {
*params = att->Renderbuffer->Name;
}
else if (att->Type == GL_TEXTURE) {
*params = att->Texture->Name;
}
else {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(pname)");
}
return;
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
if (att->Type == GL_TEXTURE) {
*params = att->TextureLevel;
}
else {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(pname)");
}
return;
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
if (att->Type == GL_TEXTURE) {
*params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
}
else {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(pname)");
}
return;
case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
if (att->Type == GL_TEXTURE) {
*params = att->Zoffset;
}
else {
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(pname)");
}
return;
default:
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetFramebufferAttachmentParameterivEXT(pname)");
return;
}
}
void GLAPIENTRY
_mesa_GenerateMipmapEXT(GLenum target)
{
struct gl_texture_unit *texUnit;
struct gl_texture_object *texObj;
GET_CURRENT_CONTEXT(ctx);
ASSERT_OUTSIDE_BEGIN_END(ctx);
switch (target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
case GL_TEXTURE_CUBE_MAP:
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
return;
}
texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
texObj = _mesa_select_tex_object(ctx, texUnit, target);
/* XXX this might not handle cube maps correctly */
_mesa_generate_mipmap(ctx, target, texUnit, texObj);
}
|