Revert "mesa: Simplify _mesa_base_fbo_format by making it exceptions to teximages."
[mesa.git] / src / mesa / main / fbobject.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * GL_EXT/ARB_framebuffer_object extensions
29 *
30 * Authors:
31 * Brian Paul
32 */
33
34
35 #include "buffers.h"
36 #include "context.h"
37 #include "enums.h"
38 #include "fbobject.h"
39 #include "formats.h"
40 #include "framebuffer.h"
41 #include "hash.h"
42 #include "macros.h"
43 #include "mfeatures.h"
44 #include "mtypes.h"
45 #include "renderbuffer.h"
46 #include "state.h"
47 #include "teximage.h"
48 #include "texobj.h"
49
50
51 /** Set this to 1 to help debug FBO incompleteness problems */
52 #define DEBUG_FBO 0
53
54 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
55 #define DEBUG_BLIT 0
56
57
58 /**
59 * Notes:
60 *
61 * None of the GL_EXT_framebuffer_object functions are compiled into
62 * display lists.
63 */
64
65
66
67 /*
68 * When glGenRender/FramebuffersEXT() is called we insert pointers to
69 * these placeholder objects into the hash table.
70 * Later, when the object ID is first bound, we replace the placeholder
71 * with the real frame/renderbuffer.
72 */
73 static struct gl_framebuffer DummyFramebuffer;
74 static struct gl_renderbuffer DummyRenderbuffer;
75
76 /* We bind this framebuffer when applications pass a NULL
77 * drawable/surface in make current. */
78 static struct gl_framebuffer IncompleteFramebuffer;
79
80
81 #define IS_CUBE_FACE(TARGET) \
82 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
83 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
84
85
86 static void
87 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
88 {
89 /* no op */
90 }
91
92 static void
93 delete_dummy_framebuffer(struct gl_framebuffer *fb)
94 {
95 /* no op */
96 }
97
98
99 void
100 _mesa_init_fbobjects(struct gl_context *ctx)
101 {
102 _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
103 _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
104 _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
105 DummyFramebuffer.Delete = delete_dummy_framebuffer;
106 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
107 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
108 }
109
110 struct gl_framebuffer *
111 _mesa_get_incomplete_framebuffer(void)
112 {
113 return &IncompleteFramebuffer;
114 }
115
116 /**
117 * Helper routine for getting a gl_renderbuffer.
118 */
119 struct gl_renderbuffer *
120 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
121 {
122 struct gl_renderbuffer *rb;
123
124 if (id == 0)
125 return NULL;
126
127 rb = (struct gl_renderbuffer *)
128 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
129 return rb;
130 }
131
132
133 /**
134 * Helper routine for getting a gl_framebuffer.
135 */
136 struct gl_framebuffer *
137 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
138 {
139 struct gl_framebuffer *fb;
140
141 if (id == 0)
142 return NULL;
143
144 fb = (struct gl_framebuffer *)
145 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
146 return fb;
147 }
148
149
150 /**
151 * Mark the given framebuffer as invalid. This will force the
152 * test for framebuffer completeness to be done before the framebuffer
153 * is used.
154 */
155 static void
156 invalidate_framebuffer(struct gl_framebuffer *fb)
157 {
158 fb->_Status = 0; /* "indeterminate" */
159 }
160
161
162 /**
163 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
164 * gl_renderbuffer_attachment object.
165 * This function is only used for user-created FB objects, not the
166 * default / window-system FB object.
167 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
168 * the depth buffer attachment point.
169 */
170 struct gl_renderbuffer_attachment *
171 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
172 GLenum attachment)
173 {
174 GLuint i;
175
176 assert(fb->Name > 0);
177
178 switch (attachment) {
179 case GL_COLOR_ATTACHMENT0_EXT:
180 case GL_COLOR_ATTACHMENT1_EXT:
181 case GL_COLOR_ATTACHMENT2_EXT:
182 case GL_COLOR_ATTACHMENT3_EXT:
183 case GL_COLOR_ATTACHMENT4_EXT:
184 case GL_COLOR_ATTACHMENT5_EXT:
185 case GL_COLOR_ATTACHMENT6_EXT:
186 case GL_COLOR_ATTACHMENT7_EXT:
187 case GL_COLOR_ATTACHMENT8_EXT:
188 case GL_COLOR_ATTACHMENT9_EXT:
189 case GL_COLOR_ATTACHMENT10_EXT:
190 case GL_COLOR_ATTACHMENT11_EXT:
191 case GL_COLOR_ATTACHMENT12_EXT:
192 case GL_COLOR_ATTACHMENT13_EXT:
193 case GL_COLOR_ATTACHMENT14_EXT:
194 case GL_COLOR_ATTACHMENT15_EXT:
195 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
196 if (i >= ctx->Const.MaxColorAttachments) {
197 return NULL;
198 }
199 return &fb->Attachment[BUFFER_COLOR0 + i];
200 case GL_DEPTH_STENCIL_ATTACHMENT:
201 /* fall-through */
202 case GL_DEPTH_BUFFER:
203 /* fall-through / new in GL 3.0 */
204 case GL_DEPTH_ATTACHMENT_EXT:
205 return &fb->Attachment[BUFFER_DEPTH];
206 case GL_STENCIL_BUFFER:
207 /* fall-through / new in GL 3.0 */
208 case GL_STENCIL_ATTACHMENT_EXT:
209 return &fb->Attachment[BUFFER_STENCIL];
210 default:
211 return NULL;
212 }
213 }
214
215
216 /**
217 * As above, but only used for getting attachments of the default /
218 * window-system framebuffer (not user-created framebuffer objects).
219 */
220 static struct gl_renderbuffer_attachment *
221 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
222 GLenum attachment)
223 {
224 assert(fb->Name == 0);
225
226 switch (attachment) {
227 case GL_FRONT_LEFT:
228 return &fb->Attachment[BUFFER_FRONT_LEFT];
229 case GL_FRONT_RIGHT:
230 return &fb->Attachment[BUFFER_FRONT_RIGHT];
231 case GL_BACK_LEFT:
232 return &fb->Attachment[BUFFER_BACK_LEFT];
233 case GL_BACK_RIGHT:
234 return &fb->Attachment[BUFFER_BACK_RIGHT];
235 case GL_AUX0:
236 if (fb->Visual.numAuxBuffers == 1) {
237 return &fb->Attachment[BUFFER_AUX0];
238 }
239 return NULL;
240 case GL_DEPTH_BUFFER:
241 /* fall-through / new in GL 3.0 */
242 case GL_DEPTH_ATTACHMENT_EXT:
243 return &fb->Attachment[BUFFER_DEPTH];
244 case GL_STENCIL_BUFFER:
245 /* fall-through / new in GL 3.0 */
246 case GL_STENCIL_ATTACHMENT_EXT:
247 return &fb->Attachment[BUFFER_STENCIL];
248 default:
249 return NULL;
250 }
251 }
252
253
254
255 /**
256 * Remove any texture or renderbuffer attached to the given attachment
257 * point. Update reference counts, etc.
258 */
259 void
260 _mesa_remove_attachment(struct gl_context *ctx,
261 struct gl_renderbuffer_attachment *att)
262 {
263 if (att->Type == GL_TEXTURE) {
264 ASSERT(att->Texture);
265 if (ctx->Driver.FinishRenderTexture) {
266 /* tell driver that we're done rendering to this texture. */
267 ctx->Driver.FinishRenderTexture(ctx, att);
268 }
269 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
270 ASSERT(!att->Texture);
271 }
272 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
273 ASSERT(!att->Texture);
274 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
275 ASSERT(!att->Renderbuffer);
276 }
277 att->Type = GL_NONE;
278 att->Complete = GL_TRUE;
279 }
280
281
282 /**
283 * Bind a texture object to an attachment point.
284 * The previous binding, if any, will be removed first.
285 */
286 void
287 _mesa_set_texture_attachment(struct gl_context *ctx,
288 struct gl_framebuffer *fb,
289 struct gl_renderbuffer_attachment *att,
290 struct gl_texture_object *texObj,
291 GLenum texTarget, GLuint level, GLuint zoffset)
292 {
293 if (att->Texture == texObj) {
294 /* re-attaching same texture */
295 ASSERT(att->Type == GL_TEXTURE);
296 if (ctx->Driver.FinishRenderTexture)
297 ctx->Driver.FinishRenderTexture(ctx, att);
298 }
299 else {
300 /* new attachment */
301 if (ctx->Driver.FinishRenderTexture && att->Texture)
302 ctx->Driver.FinishRenderTexture(ctx, att);
303 _mesa_remove_attachment(ctx, att);
304 att->Type = GL_TEXTURE;
305 assert(!att->Texture);
306 _mesa_reference_texobj(&att->Texture, texObj);
307 }
308
309 /* always update these fields */
310 att->TextureLevel = level;
311 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
312 att->Zoffset = zoffset;
313 att->Complete = GL_FALSE;
314
315 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
316 ctx->Driver.RenderTexture(ctx, fb, att);
317 }
318
319 invalidate_framebuffer(fb);
320 }
321
322
323 /**
324 * Bind a renderbuffer to an attachment point.
325 * The previous binding, if any, will be removed first.
326 */
327 void
328 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
329 struct gl_renderbuffer_attachment *att,
330 struct gl_renderbuffer *rb)
331 {
332 /* XXX check if re-doing same attachment, exit early */
333 _mesa_remove_attachment(ctx, att);
334 att->Type = GL_RENDERBUFFER_EXT;
335 att->Texture = NULL; /* just to be safe */
336 att->Complete = GL_FALSE;
337 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
338 }
339
340
341 /**
342 * Fallback for ctx->Driver.FramebufferRenderbuffer()
343 * Attach a renderbuffer object to a framebuffer object.
344 */
345 void
346 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
347 struct gl_framebuffer *fb,
348 GLenum attachment, struct gl_renderbuffer *rb)
349 {
350 struct gl_renderbuffer_attachment *att;
351
352 _glthread_LOCK_MUTEX(fb->Mutex);
353
354 att = _mesa_get_attachment(ctx, fb, attachment);
355 ASSERT(att);
356 if (rb) {
357 _mesa_set_renderbuffer_attachment(ctx, att, rb);
358 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
359 /* do stencil attachment here (depth already done above) */
360 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
361 assert(att);
362 _mesa_set_renderbuffer_attachment(ctx, att, rb);
363 }
364 }
365 else {
366 _mesa_remove_attachment(ctx, att);
367 }
368
369 invalidate_framebuffer(fb);
370
371 _glthread_UNLOCK_MUTEX(fb->Mutex);
372 }
373
374
375 /**
376 * For debug only.
377 */
378 static void
379 att_incomplete(const char *msg)
380 {
381 #if DEBUG_FBO
382 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
383 #else
384 (void) msg;
385 #endif
386 }
387
388
389 /**
390 * For debug only.
391 */
392 static void
393 fbo_incomplete(const char *msg, int index)
394 {
395 #if DEBUG_FBO
396 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
397 #else
398 (void) msg;
399 (void) index;
400 #endif
401 }
402
403
404 /**
405 * Is the given base format a legal format for a color renderbuffer?
406 */
407 GLboolean
408 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
409 {
410 switch (baseFormat) {
411 case GL_RGB:
412 case GL_RGBA:
413 return GL_TRUE;
414 case GL_LUMINANCE:
415 case GL_LUMINANCE_ALPHA:
416 case GL_INTENSITY:
417 case GL_ALPHA:
418 return ctx->Extensions.ARB_framebuffer_object;
419 case GL_RED:
420 case GL_RG:
421 return ctx->Extensions.ARB_texture_rg;
422 default:
423 return GL_FALSE;
424 }
425 }
426
427
428 /**
429 * Is the given base format a legal format for a depth/stencil renderbuffer?
430 */
431 static GLboolean
432 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
433 {
434 switch (baseFormat) {
435 case GL_DEPTH_COMPONENT:
436 case GL_DEPTH_STENCIL_EXT:
437 return GL_TRUE;
438 default:
439 return GL_FALSE;
440 }
441 }
442
443
444 /**
445 * Test if an attachment point is complete and update its Complete field.
446 * \param format if GL_COLOR, this is a color attachment point,
447 * if GL_DEPTH, this is a depth component attachment point,
448 * if GL_STENCIL, this is a stencil component attachment point.
449 */
450 static void
451 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
452 struct gl_renderbuffer_attachment *att)
453 {
454 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
455
456 /* assume complete */
457 att->Complete = GL_TRUE;
458
459 /* Look for reasons why the attachment might be incomplete */
460 if (att->Type == GL_TEXTURE) {
461 const struct gl_texture_object *texObj = att->Texture;
462 struct gl_texture_image *texImage;
463 GLenum baseFormat;
464
465 if (!texObj) {
466 att_incomplete("no texobj");
467 att->Complete = GL_FALSE;
468 return;
469 }
470
471 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
472 if (!texImage) {
473 att_incomplete("no teximage");
474 att->Complete = GL_FALSE;
475 return;
476 }
477 if (texImage->Width < 1 || texImage->Height < 1) {
478 att_incomplete("teximage width/height=0");
479 printf("texobj = %u\n", texObj->Name);
480 printf("level = %d\n", att->TextureLevel);
481 att->Complete = GL_FALSE;
482 return;
483 }
484 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
485 att_incomplete("bad z offset");
486 att->Complete = GL_FALSE;
487 return;
488 }
489
490 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
491
492 if (format == GL_COLOR) {
493 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
494 att_incomplete("bad format");
495 att->Complete = GL_FALSE;
496 return;
497 }
498 if (_mesa_is_format_compressed(texImage->TexFormat)) {
499 att_incomplete("compressed internalformat");
500 att->Complete = GL_FALSE;
501 return;
502 }
503 }
504 else if (format == GL_DEPTH) {
505 if (baseFormat == GL_DEPTH_COMPONENT) {
506 /* OK */
507 }
508 else if (ctx->Extensions.EXT_packed_depth_stencil &&
509 ctx->Extensions.ARB_depth_texture &&
510 baseFormat == GL_DEPTH_STENCIL_EXT) {
511 /* OK */
512 }
513 else {
514 att->Complete = GL_FALSE;
515 att_incomplete("bad depth format");
516 return;
517 }
518 }
519 else {
520 ASSERT(format == GL_STENCIL);
521 if (ctx->Extensions.EXT_packed_depth_stencil &&
522 ctx->Extensions.ARB_depth_texture &&
523 baseFormat == GL_DEPTH_STENCIL_EXT) {
524 /* OK */
525 }
526 else {
527 /* no such thing as stencil-only textures */
528 att_incomplete("illegal stencil texture");
529 att->Complete = GL_FALSE;
530 return;
531 }
532 }
533 }
534 else if (att->Type == GL_RENDERBUFFER_EXT) {
535 const GLenum baseFormat =
536 _mesa_get_format_base_format(att->Renderbuffer->Format);
537
538 ASSERT(att->Renderbuffer);
539 if (!att->Renderbuffer->InternalFormat ||
540 att->Renderbuffer->Width < 1 ||
541 att->Renderbuffer->Height < 1) {
542 att_incomplete("0x0 renderbuffer");
543 att->Complete = GL_FALSE;
544 return;
545 }
546 if (format == GL_COLOR) {
547 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
548 att_incomplete("bad renderbuffer color format");
549 att->Complete = GL_FALSE;
550 return;
551 }
552 }
553 else if (format == GL_DEPTH) {
554 if (baseFormat == GL_DEPTH_COMPONENT) {
555 /* OK */
556 }
557 else if (ctx->Extensions.EXT_packed_depth_stencil &&
558 baseFormat == GL_DEPTH_STENCIL_EXT) {
559 /* OK */
560 }
561 else {
562 att_incomplete("bad renderbuffer depth format");
563 att->Complete = GL_FALSE;
564 return;
565 }
566 }
567 else {
568 assert(format == GL_STENCIL);
569 if (baseFormat == GL_STENCIL_INDEX) {
570 /* OK */
571 }
572 else if (ctx->Extensions.EXT_packed_depth_stencil &&
573 baseFormat == GL_DEPTH_STENCIL_EXT) {
574 /* OK */
575 }
576 else {
577 att->Complete = GL_FALSE;
578 att_incomplete("bad renderbuffer stencil format");
579 return;
580 }
581 }
582 }
583 else {
584 ASSERT(att->Type == GL_NONE);
585 /* complete */
586 return;
587 }
588 }
589
590
591 /**
592 * Test if the given framebuffer object is complete and update its
593 * Status field with the results.
594 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
595 * driver to make hardware-specific validation/completeness checks.
596 * Also update the framebuffer's Width and Height fields if the
597 * framebuffer is complete.
598 */
599 void
600 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
601 struct gl_framebuffer *fb)
602 {
603 GLuint numImages;
604 GLenum intFormat = GL_NONE; /* color buffers' internal format */
605 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
606 GLint numSamples = -1;
607 GLint i;
608 GLuint j;
609
610 assert(fb->Name != 0);
611
612 numImages = 0;
613 fb->Width = 0;
614 fb->Height = 0;
615
616 /* Start at -2 to more easily loop over all attachment points.
617 * -2: depth buffer
618 * -1: stencil buffer
619 * >=0: color buffer
620 */
621 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
622 struct gl_renderbuffer_attachment *att;
623 GLenum f;
624 gl_format mesaFormat;
625
626 /*
627 * XXX for ARB_fbo, only check color buffers that are named by
628 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
629 */
630
631 /* check for attachment completeness
632 */
633 if (i == -2) {
634 att = &fb->Attachment[BUFFER_DEPTH];
635 test_attachment_completeness(ctx, GL_DEPTH, att);
636 if (!att->Complete) {
637 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
638 fbo_incomplete("depth attachment incomplete", -1);
639 return;
640 }
641 }
642 else if (i == -1) {
643 att = &fb->Attachment[BUFFER_STENCIL];
644 test_attachment_completeness(ctx, GL_STENCIL, att);
645 if (!att->Complete) {
646 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
647 fbo_incomplete("stencil attachment incomplete", -1);
648 return;
649 }
650 }
651 else {
652 att = &fb->Attachment[BUFFER_COLOR0 + i];
653 test_attachment_completeness(ctx, GL_COLOR, att);
654 if (!att->Complete) {
655 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
656 fbo_incomplete("color attachment incomplete", i);
657 return;
658 }
659 }
660
661 /* get width, height, format of the renderbuffer/texture
662 */
663 if (att->Type == GL_TEXTURE) {
664 const struct gl_texture_image *texImg
665 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
666 minWidth = MIN2(minWidth, texImg->Width);
667 maxWidth = MAX2(maxWidth, texImg->Width);
668 minHeight = MIN2(minHeight, texImg->Height);
669 maxHeight = MAX2(maxHeight, texImg->Height);
670 f = texImg->_BaseFormat;
671 mesaFormat = texImg->TexFormat;
672 numImages++;
673 if (!_mesa_is_legal_color_format(ctx, f) &&
674 !is_legal_depth_format(ctx, f)) {
675 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
676 fbo_incomplete("texture attachment incomplete", -1);
677 return;
678 }
679 }
680 else if (att->Type == GL_RENDERBUFFER_EXT) {
681 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
682 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
683 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
684 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
685 f = att->Renderbuffer->InternalFormat;
686 mesaFormat = att->Renderbuffer->Format;
687 numImages++;
688 }
689 else {
690 assert(att->Type == GL_NONE);
691 continue;
692 }
693
694 if (numSamples < 0) {
695 /* first buffer */
696 numSamples = att->Renderbuffer->NumSamples;
697 }
698
699 /* check if integer color */
700 fb->_IntegerColor = _mesa_is_format_integer_color(mesaFormat);
701
702 /* Error-check width, height, format, samples
703 */
704 if (numImages == 1) {
705 /* save format, num samples */
706 if (i >= 0) {
707 intFormat = f;
708 }
709 }
710 else {
711 if (!ctx->Extensions.ARB_framebuffer_object) {
712 /* check that width, height, format are same */
713 if (minWidth != maxWidth || minHeight != maxHeight) {
714 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
715 fbo_incomplete("width or height mismatch", -1);
716 return;
717 }
718 /* check that all color buffer have same format */
719 if (intFormat != GL_NONE && f != intFormat) {
720 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
721 fbo_incomplete("format mismatch", -1);
722 return;
723 }
724 }
725 if (att->Renderbuffer &&
726 att->Renderbuffer->NumSamples != numSamples) {
727 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
728 fbo_incomplete("inconsistant number of samples", i);
729 return;
730 }
731
732 }
733 }
734
735 #if FEATURE_GL
736 if (ctx->API == API_OPENGL) {
737 /* Check that all DrawBuffers are present */
738 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
739 if (fb->ColorDrawBuffer[j] != GL_NONE) {
740 const struct gl_renderbuffer_attachment *att
741 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
742 assert(att);
743 if (att->Type == GL_NONE) {
744 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
745 fbo_incomplete("missing drawbuffer", j);
746 return;
747 }
748 }
749 }
750
751 /* Check that the ReadBuffer is present */
752 if (fb->ColorReadBuffer != GL_NONE) {
753 const struct gl_renderbuffer_attachment *att
754 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
755 assert(att);
756 if (att->Type == GL_NONE) {
757 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
758 fbo_incomplete("missing readbuffer", -1);
759 return;
760 }
761 }
762 }
763 #else
764 (void) j;
765 #endif
766
767 if (numImages == 0) {
768 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
769 fbo_incomplete("no attachments", -1);
770 return;
771 }
772
773 /* Provisionally set status = COMPLETE ... */
774 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
775
776 /* ... but the driver may say the FB is incomplete.
777 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
778 * if anything.
779 */
780 if (ctx->Driver.ValidateFramebuffer) {
781 ctx->Driver.ValidateFramebuffer(ctx, fb);
782 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
783 fbo_incomplete("driver marked FBO as incomplete", -1);
784 }
785 }
786
787 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
788 /*
789 * Note that if ARB_framebuffer_object is supported and the attached
790 * renderbuffers/textures are different sizes, the framebuffer
791 * width/height will be set to the smallest width/height.
792 */
793 fb->Width = minWidth;
794 fb->Height = minHeight;
795
796 /* finally, update the visual info for the framebuffer */
797 _mesa_update_framebuffer_visual(ctx, fb);
798 }
799 }
800
801
802 GLboolean GLAPIENTRY
803 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
804 {
805 GET_CURRENT_CONTEXT(ctx);
806 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
807 if (renderbuffer) {
808 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
809 if (rb != NULL && rb != &DummyRenderbuffer)
810 return GL_TRUE;
811 }
812 return GL_FALSE;
813 }
814
815
816 void GLAPIENTRY
817 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
818 {
819 struct gl_renderbuffer *newRb;
820 GET_CURRENT_CONTEXT(ctx);
821
822 ASSERT_OUTSIDE_BEGIN_END(ctx);
823
824 if (target != GL_RENDERBUFFER_EXT) {
825 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
826 return;
827 }
828
829 /* No need to flush here since the render buffer binding has no
830 * effect on rendering state.
831 */
832
833 if (renderbuffer) {
834 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
835 if (newRb == &DummyRenderbuffer) {
836 /* ID was reserved, but no real renderbuffer object made yet */
837 newRb = NULL;
838 }
839 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
840 /* All RB IDs must be Gen'd */
841 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
842 return;
843 }
844
845 if (!newRb) {
846 /* create new renderbuffer object */
847 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
848 if (!newRb) {
849 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
850 return;
851 }
852 ASSERT(newRb->AllocStorage);
853 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
854 newRb->RefCount = 1; /* referenced by hash table */
855 }
856 }
857 else {
858 newRb = NULL;
859 }
860
861 ASSERT(newRb != &DummyRenderbuffer);
862
863 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
864 }
865
866
867 /**
868 * If the given renderbuffer is anywhere attached to the framebuffer, detach
869 * the renderbuffer.
870 * This is used when a renderbuffer object is deleted.
871 * The spec calls for unbinding.
872 */
873 static void
874 detach_renderbuffer(struct gl_context *ctx,
875 struct gl_framebuffer *fb,
876 struct gl_renderbuffer *rb)
877 {
878 GLuint i;
879 for (i = 0; i < BUFFER_COUNT; i++) {
880 if (fb->Attachment[i].Renderbuffer == rb) {
881 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
882 }
883 }
884 invalidate_framebuffer(fb);
885 }
886
887
888 void GLAPIENTRY
889 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
890 {
891 GLint i;
892 GET_CURRENT_CONTEXT(ctx);
893
894 ASSERT_OUTSIDE_BEGIN_END(ctx);
895 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
896
897 for (i = 0; i < n; i++) {
898 if (renderbuffers[i] > 0) {
899 struct gl_renderbuffer *rb;
900 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
901 if (rb) {
902 /* check if deleting currently bound renderbuffer object */
903 if (rb == ctx->CurrentRenderbuffer) {
904 /* bind default */
905 ASSERT(rb->RefCount >= 2);
906 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
907 }
908
909 if (ctx->DrawBuffer->Name) {
910 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
911 }
912 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
913 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
914 }
915
916 /* Remove from hash table immediately, to free the ID.
917 * But the object will not be freed until it's no longer
918 * referenced anywhere else.
919 */
920 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
921
922 if (rb != &DummyRenderbuffer) {
923 /* no longer referenced by hash table */
924 _mesa_reference_renderbuffer(&rb, NULL);
925 }
926 }
927 }
928 }
929 }
930
931
932 void GLAPIENTRY
933 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
934 {
935 GET_CURRENT_CONTEXT(ctx);
936 GLuint first;
937 GLint i;
938
939 ASSERT_OUTSIDE_BEGIN_END(ctx);
940
941 if (n < 0) {
942 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
943 return;
944 }
945
946 if (!renderbuffers)
947 return;
948
949 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
950
951 for (i = 0; i < n; i++) {
952 GLuint name = first + i;
953 renderbuffers[i] = name;
954 /* insert dummy placeholder into hash table */
955 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
956 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
957 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
958 }
959 }
960
961
962 /**
963 * Given an internal format token for a render buffer, return the
964 * corresponding base format.
965 * This is very similar to _mesa_base_tex_format() but the set of valid
966 * internal formats is somewhat different.
967 *
968 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
969 * GL_DEPTH_STENCIL_EXT or zero if error.
970 *
971 * XXX in the future when we support red-only and red-green formats
972 * we'll also return GL_RED and GL_RG.
973 */
974 GLenum
975 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
976 {
977 switch (internalFormat) {
978 case GL_ALPHA:
979 case GL_ALPHA4:
980 case GL_ALPHA8:
981 case GL_ALPHA12:
982 case GL_ALPHA16:
983 return GL_ALPHA;
984 case GL_RGB:
985 case GL_R3_G3_B2:
986 case GL_RGB4:
987 case GL_RGB5:
988 case GL_RGB8:
989 case GL_RGB10:
990 case GL_RGB12:
991 case GL_RGB16:
992 case GL_SRGB8_EXT:
993 return GL_RGB;
994 case GL_RGBA:
995 case GL_RGBA2:
996 case GL_RGBA4:
997 case GL_RGB5_A1:
998 case GL_RGBA8:
999 case GL_RGB10_A2:
1000 case GL_RGBA12:
1001 case GL_RGBA16:
1002 case GL_RGBA16_SNORM:
1003 case GL_SRGB8_ALPHA8_EXT:
1004 return GL_RGBA;
1005 case GL_STENCIL_INDEX:
1006 case GL_STENCIL_INDEX1_EXT:
1007 case GL_STENCIL_INDEX4_EXT:
1008 case GL_STENCIL_INDEX8_EXT:
1009 case GL_STENCIL_INDEX16_EXT:
1010 return GL_STENCIL_INDEX;
1011 case GL_DEPTH_COMPONENT:
1012 case GL_DEPTH_COMPONENT16:
1013 case GL_DEPTH_COMPONENT24:
1014 case GL_DEPTH_COMPONENT32:
1015 return GL_DEPTH_COMPONENT;
1016 case GL_DEPTH_STENCIL_EXT:
1017 case GL_DEPTH24_STENCIL8_EXT:
1018 if (ctx->Extensions.EXT_packed_depth_stencil)
1019 return GL_DEPTH_STENCIL_EXT;
1020 else
1021 return 0;
1022 /* XXX add floating point formats eventually */
1023 default:
1024 return 0;
1025 }
1026 }
1027
1028
1029 /** sentinal value, see below */
1030 #define NO_SAMPLES 1000
1031
1032
1033 /**
1034 * Helper function used by _mesa_RenderbufferStorageEXT() and
1035 * _mesa_RenderbufferStorageMultisample().
1036 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1037 */
1038 static void
1039 renderbuffer_storage(GLenum target, GLenum internalFormat,
1040 GLsizei width, GLsizei height, GLsizei samples)
1041 {
1042 const char *func = samples == NO_SAMPLES ?
1043 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1044 struct gl_renderbuffer *rb;
1045 GLenum baseFormat;
1046 GET_CURRENT_CONTEXT(ctx);
1047
1048 ASSERT_OUTSIDE_BEGIN_END(ctx);
1049
1050 if (target != GL_RENDERBUFFER_EXT) {
1051 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1052 return;
1053 }
1054
1055 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1056 if (baseFormat == 0) {
1057 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
1058 return;
1059 }
1060
1061 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1062 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1063 return;
1064 }
1065
1066 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1067 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1068 return;
1069 }
1070
1071 if (samples == NO_SAMPLES) {
1072 /* NumSamples == 0 indicates non-multisampling */
1073 samples = 0;
1074 }
1075 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1076 /* note: driver may choose to use more samples than what's requested */
1077 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1078 return;
1079 }
1080
1081 rb = ctx->CurrentRenderbuffer;
1082 if (!rb) {
1083 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1084 return;
1085 }
1086
1087 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1088
1089 if (rb->InternalFormat == internalFormat &&
1090 rb->Width == (GLuint) width &&
1091 rb->Height == (GLuint) height) {
1092 /* no change in allocation needed */
1093 return;
1094 }
1095
1096 /* These MUST get set by the AllocStorage func */
1097 rb->Format = MESA_FORMAT_NONE;
1098 rb->NumSamples = samples;
1099
1100 /* Now allocate the storage */
1101 ASSERT(rb->AllocStorage);
1102 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1103 /* No error - check/set fields now */
1104 assert(rb->Format != MESA_FORMAT_NONE);
1105 assert(rb->Width == (GLuint) width);
1106 assert(rb->Height == (GLuint) height);
1107 rb->InternalFormat = internalFormat;
1108 rb->_BaseFormat = baseFormat;
1109 assert(rb->_BaseFormat != 0);
1110 }
1111 else {
1112 /* Probably ran out of memory - clear the fields */
1113 rb->Width = 0;
1114 rb->Height = 0;
1115 rb->Format = MESA_FORMAT_NONE;
1116 rb->InternalFormat = GL_NONE;
1117 rb->_BaseFormat = GL_NONE;
1118 rb->NumSamples = 0;
1119 }
1120
1121 /*
1122 test_framebuffer_completeness(ctx, fb);
1123 */
1124 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1125 * points???
1126 */
1127 }
1128
1129
1130 #if FEATURE_OES_EGL_image
1131 void GLAPIENTRY
1132 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1133 {
1134 struct gl_renderbuffer *rb;
1135 GET_CURRENT_CONTEXT(ctx);
1136 ASSERT_OUTSIDE_BEGIN_END(ctx);
1137
1138 if (!ctx->Extensions.OES_EGL_image) {
1139 _mesa_error(ctx, GL_INVALID_OPERATION,
1140 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1141 return;
1142 }
1143
1144 if (target != GL_RENDERBUFFER) {
1145 _mesa_error(ctx, GL_INVALID_ENUM,
1146 "EGLImageTargetRenderbufferStorageOES");
1147 return;
1148 }
1149
1150 rb = ctx->CurrentRenderbuffer;
1151 if (!rb) {
1152 _mesa_error(ctx, GL_INVALID_OPERATION,
1153 "EGLImageTargetRenderbufferStorageOES");
1154 return;
1155 }
1156
1157 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1158
1159 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1160 }
1161 #endif
1162
1163
1164 /**
1165 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1166 * _mesa_GetFramebufferAttachmentParameterivEXT()
1167 * We have to be careful to respect the base format. For example, if a
1168 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1169 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1170 * we need to return zero.
1171 */
1172 static GLint
1173 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1174 {
1175 switch (pname) {
1176 case GL_RENDERBUFFER_RED_SIZE_EXT:
1177 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1178 if (baseFormat == GL_RGB || baseFormat == GL_RGBA ||
1179 baseFormat == GL_RG || baseFormat == GL_RED)
1180 return _mesa_get_format_bits(format, pname);
1181 else
1182 return 0;
1183 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1184 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1185 if (baseFormat == GL_RGB || baseFormat == GL_RGBA || baseFormat == GL_RG)
1186 return _mesa_get_format_bits(format, pname);
1187 else
1188 return 0;
1189 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1190 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1191 if (baseFormat == GL_RGB || baseFormat == GL_RGBA)
1192 return _mesa_get_format_bits(format, pname);
1193 else
1194 return 0;
1195 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1196 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1197 if (baseFormat == GL_RGBA || baseFormat == GL_ALPHA ||
1198 baseFormat == GL_LUMINANCE_ALPHA)
1199 return _mesa_get_format_bits(format, pname);
1200 else
1201 return 0;
1202 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1203 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1204 if (baseFormat == GL_DEPTH_COMPONENT || baseFormat == GL_DEPTH_STENCIL)
1205 return _mesa_get_format_bits(format, pname);
1206 else
1207 return 0;
1208 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1209 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1210 if (baseFormat == GL_STENCIL_INDEX || baseFormat == GL_DEPTH_STENCIL)
1211 return _mesa_get_format_bits(format, pname);
1212 else
1213 return 0;
1214 default:
1215 return 0;
1216 }
1217 }
1218
1219
1220
1221 void GLAPIENTRY
1222 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1223 GLsizei width, GLsizei height)
1224 {
1225 /* GL_ARB_fbo says calling this function is equivalent to calling
1226 * glRenderbufferStorageMultisample() with samples=0. We pass in
1227 * a token value here just for error reporting purposes.
1228 */
1229 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1230 }
1231
1232
1233 void GLAPIENTRY
1234 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1235 GLenum internalFormat,
1236 GLsizei width, GLsizei height)
1237 {
1238 renderbuffer_storage(target, internalFormat, width, height, samples);
1239 }
1240
1241
1242 /**
1243 * OpenGL ES version of glRenderBufferStorage.
1244 */
1245 void GLAPIENTRY
1246 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1247 GLsizei width, GLsizei height)
1248 {
1249 switch (internalFormat) {
1250 case GL_RGB565:
1251 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1252 /* choose a closest format */
1253 internalFormat = GL_RGB5;
1254 break;
1255 default:
1256 break;
1257 }
1258
1259 renderbuffer_storage(target, internalFormat, width, height, 0);
1260 }
1261
1262
1263 void GLAPIENTRY
1264 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1265 {
1266 struct gl_renderbuffer *rb;
1267 GET_CURRENT_CONTEXT(ctx);
1268
1269 ASSERT_OUTSIDE_BEGIN_END(ctx);
1270
1271 if (target != GL_RENDERBUFFER_EXT) {
1272 _mesa_error(ctx, GL_INVALID_ENUM,
1273 "glGetRenderbufferParameterivEXT(target)");
1274 return;
1275 }
1276
1277 rb = ctx->CurrentRenderbuffer;
1278 if (!rb) {
1279 _mesa_error(ctx, GL_INVALID_OPERATION,
1280 "glGetRenderbufferParameterivEXT");
1281 return;
1282 }
1283
1284 /* No need to flush here since we're just quering state which is
1285 * not effected by rendering.
1286 */
1287
1288 switch (pname) {
1289 case GL_RENDERBUFFER_WIDTH_EXT:
1290 *params = rb->Width;
1291 return;
1292 case GL_RENDERBUFFER_HEIGHT_EXT:
1293 *params = rb->Height;
1294 return;
1295 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1296 *params = rb->InternalFormat;
1297 return;
1298 case GL_RENDERBUFFER_RED_SIZE_EXT:
1299 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1300 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1301 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1302 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1303 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1304 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1305 break;
1306 case GL_RENDERBUFFER_SAMPLES:
1307 if (ctx->Extensions.ARB_framebuffer_object) {
1308 *params = rb->NumSamples;
1309 break;
1310 }
1311 /* fallthrough */
1312 default:
1313 _mesa_error(ctx, GL_INVALID_ENUM,
1314 "glGetRenderbufferParameterivEXT(target)");
1315 return;
1316 }
1317 }
1318
1319
1320 GLboolean GLAPIENTRY
1321 _mesa_IsFramebufferEXT(GLuint framebuffer)
1322 {
1323 GET_CURRENT_CONTEXT(ctx);
1324 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1325 if (framebuffer) {
1326 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1327 if (rb != NULL && rb != &DummyFramebuffer)
1328 return GL_TRUE;
1329 }
1330 return GL_FALSE;
1331 }
1332
1333
1334 /**
1335 * Check if any of the attachments of the given framebuffer are textures
1336 * (render to texture). Call ctx->Driver.RenderTexture() for such
1337 * attachments.
1338 */
1339 static void
1340 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1341 {
1342 GLuint i;
1343 ASSERT(ctx->Driver.RenderTexture);
1344
1345 if (fb->Name == 0)
1346 return; /* can't render to texture with winsys framebuffers */
1347
1348 for (i = 0; i < BUFFER_COUNT; i++) {
1349 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1350 struct gl_texture_object *texObj = att->Texture;
1351 if (texObj
1352 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1353 ctx->Driver.RenderTexture(ctx, fb, att);
1354 }
1355 }
1356 }
1357
1358
1359 /**
1360 * Examine all the framebuffer's attachments to see if any are textures.
1361 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1362 * notify the device driver that the texture image may have changed.
1363 */
1364 static void
1365 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1366 {
1367 if (fb->Name == 0)
1368 return; /* can't render to texture with winsys framebuffers */
1369
1370 if (ctx->Driver.FinishRenderTexture) {
1371 GLuint i;
1372 for (i = 0; i < BUFFER_COUNT; i++) {
1373 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1374 if (att->Texture && att->Renderbuffer) {
1375 ctx->Driver.FinishRenderTexture(ctx, att);
1376 }
1377 }
1378 }
1379 }
1380
1381
1382 void GLAPIENTRY
1383 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1384 {
1385 struct gl_framebuffer *newDrawFb, *newReadFb;
1386 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1387 GLboolean bindReadBuf, bindDrawBuf;
1388 GET_CURRENT_CONTEXT(ctx);
1389
1390 #ifdef DEBUG
1391 if (ctx->Extensions.ARB_framebuffer_object) {
1392 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1393 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1394 }
1395 #endif
1396
1397 ASSERT_OUTSIDE_BEGIN_END(ctx);
1398
1399 if (!ctx->Extensions.EXT_framebuffer_object) {
1400 _mesa_error(ctx, GL_INVALID_OPERATION,
1401 "glBindFramebufferEXT(unsupported)");
1402 return;
1403 }
1404
1405 switch (target) {
1406 #if FEATURE_EXT_framebuffer_blit
1407 case GL_DRAW_FRAMEBUFFER_EXT:
1408 if (!ctx->Extensions.EXT_framebuffer_blit) {
1409 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1410 return;
1411 }
1412 bindDrawBuf = GL_TRUE;
1413 bindReadBuf = GL_FALSE;
1414 break;
1415 case GL_READ_FRAMEBUFFER_EXT:
1416 if (!ctx->Extensions.EXT_framebuffer_blit) {
1417 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1418 return;
1419 }
1420 bindDrawBuf = GL_FALSE;
1421 bindReadBuf = GL_TRUE;
1422 break;
1423 #endif
1424 case GL_FRAMEBUFFER_EXT:
1425 bindDrawBuf = GL_TRUE;
1426 bindReadBuf = GL_TRUE;
1427 break;
1428 default:
1429 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1430 return;
1431 }
1432
1433 if (framebuffer) {
1434 /* Binding a user-created framebuffer object */
1435 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1436 if (newDrawFb == &DummyFramebuffer) {
1437 /* ID was reserved, but no real framebuffer object made yet */
1438 newDrawFb = NULL;
1439 }
1440 else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1441 /* All FBO IDs must be Gen'd */
1442 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1443 return;
1444 }
1445
1446 if (!newDrawFb) {
1447 /* create new framebuffer object */
1448 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1449 if (!newDrawFb) {
1450 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1451 return;
1452 }
1453 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1454 }
1455 newReadFb = newDrawFb;
1456 }
1457 else {
1458 /* Binding the window system framebuffer (which was originally set
1459 * with MakeCurrent).
1460 */
1461 newDrawFb = ctx->WinSysDrawBuffer;
1462 newReadFb = ctx->WinSysReadBuffer;
1463 }
1464
1465 ASSERT(newDrawFb);
1466 ASSERT(newDrawFb != &DummyFramebuffer);
1467
1468 /* save pointers to current/old framebuffers */
1469 oldDrawFb = ctx->DrawBuffer;
1470 oldReadFb = ctx->ReadBuffer;
1471
1472 /* check if really changing bindings */
1473 if (oldDrawFb == newDrawFb)
1474 bindDrawBuf = GL_FALSE;
1475 if (oldReadFb == newReadFb)
1476 bindReadBuf = GL_FALSE;
1477
1478 /*
1479 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1480 *
1481 * We also check if we're beginning and/or ending render-to-texture.
1482 * When a framebuffer with texture attachments is unbound, call
1483 * ctx->Driver.FinishRenderTexture().
1484 * When a framebuffer with texture attachments is bound, call
1485 * ctx->Driver.RenderTexture().
1486 *
1487 * Note that if the ReadBuffer has texture attachments we don't consider
1488 * that a render-to-texture case.
1489 */
1490 if (bindReadBuf) {
1491 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1492
1493 /* check if old readbuffer was render-to-texture */
1494 check_end_texture_render(ctx, oldReadFb);
1495
1496 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1497 }
1498
1499 if (bindDrawBuf) {
1500 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1501
1502 /* check if old read/draw buffers were render-to-texture */
1503 if (!bindReadBuf)
1504 check_end_texture_render(ctx, oldReadFb);
1505
1506 if (oldDrawFb != oldReadFb)
1507 check_end_texture_render(ctx, oldDrawFb);
1508
1509 /* check if newly bound framebuffer has any texture attachments */
1510 check_begin_texture_render(ctx, newDrawFb);
1511
1512 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1513 }
1514
1515 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1516 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1517 }
1518 }
1519
1520
1521 void GLAPIENTRY
1522 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1523 {
1524 GLint i;
1525 GET_CURRENT_CONTEXT(ctx);
1526
1527 ASSERT_OUTSIDE_BEGIN_END(ctx);
1528 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1529
1530 for (i = 0; i < n; i++) {
1531 if (framebuffers[i] > 0) {
1532 struct gl_framebuffer *fb;
1533 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1534 if (fb) {
1535 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1536
1537 /* check if deleting currently bound framebuffer object */
1538 if (ctx->Extensions.EXT_framebuffer_blit) {
1539 /* separate draw/read binding points */
1540 if (fb == ctx->DrawBuffer) {
1541 /* bind default */
1542 ASSERT(fb->RefCount >= 2);
1543 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1544 }
1545 if (fb == ctx->ReadBuffer) {
1546 /* bind default */
1547 ASSERT(fb->RefCount >= 2);
1548 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1549 }
1550 }
1551 else {
1552 /* only one binding point for read/draw buffers */
1553 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1554 /* bind default */
1555 ASSERT(fb->RefCount >= 2);
1556 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1557 }
1558 }
1559
1560 /* remove from hash table immediately, to free the ID */
1561 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1562
1563 if (fb != &DummyFramebuffer) {
1564 /* But the object will not be freed until it's no longer
1565 * bound in any context.
1566 */
1567 _mesa_reference_framebuffer(&fb, NULL);
1568 }
1569 }
1570 }
1571 }
1572 }
1573
1574
1575 void GLAPIENTRY
1576 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1577 {
1578 GET_CURRENT_CONTEXT(ctx);
1579 GLuint first;
1580 GLint i;
1581
1582 ASSERT_OUTSIDE_BEGIN_END(ctx);
1583
1584 if (n < 0) {
1585 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1586 return;
1587 }
1588
1589 if (!framebuffers)
1590 return;
1591
1592 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1593
1594 for (i = 0; i < n; i++) {
1595 GLuint name = first + i;
1596 framebuffers[i] = name;
1597 /* insert dummy placeholder into hash table */
1598 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1599 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1600 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1601 }
1602 }
1603
1604
1605
1606 GLenum GLAPIENTRY
1607 _mesa_CheckFramebufferStatusEXT(GLenum target)
1608 {
1609 struct gl_framebuffer *buffer;
1610 GET_CURRENT_CONTEXT(ctx);
1611
1612 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1613
1614 switch (target) {
1615 #if FEATURE_EXT_framebuffer_blit
1616 case GL_DRAW_FRAMEBUFFER_EXT:
1617 if (!ctx->Extensions.EXT_framebuffer_blit) {
1618 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1619 return 0;
1620 }
1621 buffer = ctx->DrawBuffer;
1622 break;
1623 case GL_READ_FRAMEBUFFER_EXT:
1624 if (!ctx->Extensions.EXT_framebuffer_blit) {
1625 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1626 return 0;
1627 }
1628 buffer = ctx->ReadBuffer;
1629 break;
1630 #endif
1631 case GL_FRAMEBUFFER_EXT:
1632 buffer = ctx->DrawBuffer;
1633 break;
1634 default:
1635 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1636 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1637 }
1638
1639 if (buffer->Name == 0) {
1640 /* The window system / default framebuffer is always complete */
1641 return GL_FRAMEBUFFER_COMPLETE_EXT;
1642 }
1643
1644 /* No need to flush here */
1645
1646 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1647 _mesa_test_framebuffer_completeness(ctx, buffer);
1648 }
1649
1650 return buffer->_Status;
1651 }
1652
1653
1654
1655 /**
1656 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1657 */
1658 static void
1659 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
1660 GLenum attachment, GLenum textarget, GLuint texture,
1661 GLint level, GLint zoffset)
1662 {
1663 struct gl_renderbuffer_attachment *att;
1664 struct gl_texture_object *texObj = NULL;
1665 struct gl_framebuffer *fb;
1666 GLboolean error = GL_FALSE;
1667
1668 ASSERT_OUTSIDE_BEGIN_END(ctx);
1669
1670 switch (target) {
1671 case GL_READ_FRAMEBUFFER_EXT:
1672 error = !ctx->Extensions.EXT_framebuffer_blit;
1673 fb = ctx->ReadBuffer;
1674 break;
1675 case GL_DRAW_FRAMEBUFFER_EXT:
1676 error = !ctx->Extensions.EXT_framebuffer_blit;
1677 /* fall-through */
1678 case GL_FRAMEBUFFER_EXT:
1679 fb = ctx->DrawBuffer;
1680 break;
1681 default:
1682 error = GL_TRUE;
1683 }
1684
1685 if (error) {
1686 _mesa_error(ctx, GL_INVALID_ENUM,
1687 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1688 return;
1689 }
1690
1691 ASSERT(fb);
1692
1693 /* check framebuffer binding */
1694 if (fb->Name == 0) {
1695 _mesa_error(ctx, GL_INVALID_OPERATION,
1696 "glFramebufferTexture%sEXT", caller);
1697 return;
1698 }
1699
1700
1701 /* The textarget, level, and zoffset parameters are only validated if
1702 * texture is non-zero.
1703 */
1704 if (texture) {
1705 GLboolean err = GL_TRUE;
1706
1707 texObj = _mesa_lookup_texture(ctx, texture);
1708 if (texObj != NULL) {
1709 if (textarget == 0) {
1710 /* XXX what's the purpose of this? */
1711 err = (texObj->Target != GL_TEXTURE_3D) &&
1712 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1713 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1714 }
1715 else {
1716 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1717 ? !IS_CUBE_FACE(textarget)
1718 : (texObj->Target != textarget);
1719 }
1720 }
1721 else {
1722 /* can't render to a non-existant texture */
1723 _mesa_error(ctx, GL_INVALID_OPERATION,
1724 "glFramebufferTexture%sEXT(non existant texture)",
1725 caller);
1726 return;
1727 }
1728
1729 if (err) {
1730 _mesa_error(ctx, GL_INVALID_OPERATION,
1731 "glFramebufferTexture%sEXT(texture target mismatch)",
1732 caller);
1733 return;
1734 }
1735
1736 if (texObj->Target == GL_TEXTURE_3D) {
1737 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1738 if (zoffset < 0 || zoffset >= maxSize) {
1739 _mesa_error(ctx, GL_INVALID_VALUE,
1740 "glFramebufferTexture%sEXT(zoffset)", caller);
1741 return;
1742 }
1743 }
1744 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1745 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1746 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1747 _mesa_error(ctx, GL_INVALID_VALUE,
1748 "glFramebufferTexture%sEXT(layer)", caller);
1749 return;
1750 }
1751 }
1752
1753 if ((level < 0) ||
1754 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1755 _mesa_error(ctx, GL_INVALID_VALUE,
1756 "glFramebufferTexture%sEXT(level)", caller);
1757 return;
1758 }
1759 }
1760
1761 att = _mesa_get_attachment(ctx, fb, attachment);
1762 if (att == NULL) {
1763 _mesa_error(ctx, GL_INVALID_ENUM,
1764 "glFramebufferTexture%sEXT(attachment)", caller);
1765 return;
1766 }
1767
1768 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1769
1770 _glthread_LOCK_MUTEX(fb->Mutex);
1771 if (texObj) {
1772 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1773 level, zoffset);
1774 /* Set the render-to-texture flag. We'll check this flag in
1775 * glTexImage() and friends to determine if we need to revalidate
1776 * any FBOs that might be rendering into this texture.
1777 * This flag never gets cleared since it's non-trivial to determine
1778 * when all FBOs might be done rendering to this texture. That's OK
1779 * though since it's uncommon to render to a texture then repeatedly
1780 * call glTexImage() to change images in the texture.
1781 */
1782 texObj->_RenderToTexture = GL_TRUE;
1783 }
1784 else {
1785 _mesa_remove_attachment(ctx, att);
1786 }
1787
1788 invalidate_framebuffer(fb);
1789
1790 _glthread_UNLOCK_MUTEX(fb->Mutex);
1791 }
1792
1793
1794
1795 void GLAPIENTRY
1796 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1797 GLenum textarget, GLuint texture, GLint level)
1798 {
1799 GET_CURRENT_CONTEXT(ctx);
1800
1801 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1802 _mesa_error(ctx, GL_INVALID_ENUM,
1803 "glFramebufferTexture1DEXT(textarget)");
1804 return;
1805 }
1806
1807 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1808 level, 0);
1809 }
1810
1811
1812 void GLAPIENTRY
1813 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1814 GLenum textarget, GLuint texture, GLint level)
1815 {
1816 GET_CURRENT_CONTEXT(ctx);
1817
1818 if ((texture != 0) &&
1819 (textarget != GL_TEXTURE_2D) &&
1820 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1821 (!IS_CUBE_FACE(textarget))) {
1822 _mesa_error(ctx, GL_INVALID_OPERATION,
1823 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1824 return;
1825 }
1826
1827 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1828 level, 0);
1829 }
1830
1831
1832 void GLAPIENTRY
1833 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1834 GLenum textarget, GLuint texture,
1835 GLint level, GLint zoffset)
1836 {
1837 GET_CURRENT_CONTEXT(ctx);
1838
1839 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1840 _mesa_error(ctx, GL_INVALID_ENUM,
1841 "glFramebufferTexture3DEXT(textarget)");
1842 return;
1843 }
1844
1845 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1846 level, zoffset);
1847 }
1848
1849
1850 void GLAPIENTRY
1851 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1852 GLuint texture, GLint level, GLint layer)
1853 {
1854 GET_CURRENT_CONTEXT(ctx);
1855
1856 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1857 level, layer);
1858 }
1859
1860
1861 void GLAPIENTRY
1862 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1863 GLenum renderbufferTarget,
1864 GLuint renderbuffer)
1865 {
1866 struct gl_renderbuffer_attachment *att;
1867 struct gl_framebuffer *fb;
1868 struct gl_renderbuffer *rb;
1869 GET_CURRENT_CONTEXT(ctx);
1870
1871 ASSERT_OUTSIDE_BEGIN_END(ctx);
1872
1873 switch (target) {
1874 #if FEATURE_EXT_framebuffer_blit
1875 case GL_DRAW_FRAMEBUFFER_EXT:
1876 if (!ctx->Extensions.EXT_framebuffer_blit) {
1877 _mesa_error(ctx, GL_INVALID_ENUM,
1878 "glFramebufferRenderbufferEXT(target)");
1879 return;
1880 }
1881 fb = ctx->DrawBuffer;
1882 break;
1883 case GL_READ_FRAMEBUFFER_EXT:
1884 if (!ctx->Extensions.EXT_framebuffer_blit) {
1885 _mesa_error(ctx, GL_INVALID_ENUM,
1886 "glFramebufferRenderbufferEXT(target)");
1887 return;
1888 }
1889 fb = ctx->ReadBuffer;
1890 break;
1891 #endif
1892 case GL_FRAMEBUFFER_EXT:
1893 fb = ctx->DrawBuffer;
1894 break;
1895 default:
1896 _mesa_error(ctx, GL_INVALID_ENUM,
1897 "glFramebufferRenderbufferEXT(target)");
1898 return;
1899 }
1900
1901 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1902 _mesa_error(ctx, GL_INVALID_ENUM,
1903 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1904 return;
1905 }
1906
1907 if (fb->Name == 0) {
1908 /* Can't attach new renderbuffers to a window system framebuffer */
1909 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1910 return;
1911 }
1912
1913 att = _mesa_get_attachment(ctx, fb, attachment);
1914 if (att == NULL) {
1915 _mesa_error(ctx, GL_INVALID_ENUM,
1916 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1917 _mesa_lookup_enum_by_nr(attachment));
1918 return;
1919 }
1920
1921 if (renderbuffer) {
1922 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1923 if (!rb) {
1924 _mesa_error(ctx, GL_INVALID_OPERATION,
1925 "glFramebufferRenderbufferEXT(non-existant"
1926 " renderbuffer %u)", renderbuffer);
1927 return;
1928 }
1929 else if (rb == &DummyRenderbuffer) {
1930 /* This is what NVIDIA does */
1931 _mesa_error(ctx, GL_INVALID_VALUE,
1932 "glFramebufferRenderbufferEXT(renderbuffer %u)",
1933 renderbuffer);
1934 return;
1935 }
1936 }
1937 else {
1938 /* remove renderbuffer attachment */
1939 rb = NULL;
1940 }
1941
1942 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
1943 rb && rb->Format != MESA_FORMAT_NONE) {
1944 /* make sure the renderbuffer is a depth/stencil format */
1945 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
1946 if (baseFormat != GL_DEPTH_STENCIL) {
1947 _mesa_error(ctx, GL_INVALID_OPERATION,
1948 "glFramebufferRenderbufferEXT(renderbuffer"
1949 " is not DEPTH_STENCIL format)");
1950 return;
1951 }
1952 }
1953
1954
1955 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1956
1957 assert(ctx->Driver.FramebufferRenderbuffer);
1958 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1959
1960 /* Some subsequent GL commands may depend on the framebuffer's visual
1961 * after the binding is updated. Update visual info now.
1962 */
1963 _mesa_update_framebuffer_visual(ctx, fb);
1964 }
1965
1966
1967 void GLAPIENTRY
1968 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1969 GLenum pname, GLint *params)
1970 {
1971 const struct gl_renderbuffer_attachment *att;
1972 struct gl_framebuffer *buffer;
1973 GET_CURRENT_CONTEXT(ctx);
1974
1975 ASSERT_OUTSIDE_BEGIN_END(ctx);
1976
1977 switch (target) {
1978 #if FEATURE_EXT_framebuffer_blit
1979 case GL_DRAW_FRAMEBUFFER_EXT:
1980 if (!ctx->Extensions.EXT_framebuffer_blit) {
1981 _mesa_error(ctx, GL_INVALID_ENUM,
1982 "glGetFramebufferAttachmentParameterivEXT(target)");
1983 return;
1984 }
1985 buffer = ctx->DrawBuffer;
1986 break;
1987 case GL_READ_FRAMEBUFFER_EXT:
1988 if (!ctx->Extensions.EXT_framebuffer_blit) {
1989 _mesa_error(ctx, GL_INVALID_ENUM,
1990 "glGetFramebufferAttachmentParameterivEXT(target)");
1991 return;
1992 }
1993 buffer = ctx->ReadBuffer;
1994 break;
1995 #endif
1996 case GL_FRAMEBUFFER_EXT:
1997 buffer = ctx->DrawBuffer;
1998 break;
1999 default:
2000 _mesa_error(ctx, GL_INVALID_ENUM,
2001 "glGetFramebufferAttachmentParameterivEXT(target)");
2002 return;
2003 }
2004
2005 if (buffer->Name == 0) {
2006 /* the default / window-system FBO */
2007 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2008 }
2009 else {
2010 /* user-created framebuffer FBO */
2011 att = _mesa_get_attachment(ctx, buffer, attachment);
2012 }
2013
2014 if (att == NULL) {
2015 _mesa_error(ctx, GL_INVALID_ENUM,
2016 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2017 return;
2018 }
2019
2020 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2021 /* the depth and stencil attachments must point to the same buffer */
2022 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2023 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2024 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2025 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2026 _mesa_error(ctx, GL_INVALID_OPERATION,
2027 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2028 " attachments differ)");
2029 return;
2030 }
2031 }
2032
2033 /* No need to flush here */
2034
2035 switch (pname) {
2036 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2037 *params = buffer->Name == 0 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2038 return;
2039 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2040 if (att->Type == GL_RENDERBUFFER_EXT) {
2041 *params = att->Renderbuffer->Name;
2042 }
2043 else if (att->Type == GL_TEXTURE) {
2044 *params = att->Texture->Name;
2045 }
2046 else {
2047 assert(att->Type == GL_NONE);
2048 *params = 0;
2049 }
2050 return;
2051 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2052 if (att->Type == GL_TEXTURE) {
2053 *params = att->TextureLevel;
2054 }
2055 else {
2056 _mesa_error(ctx, GL_INVALID_ENUM,
2057 "glGetFramebufferAttachmentParameterivEXT(pname)");
2058 }
2059 return;
2060 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2061 if (att->Type == GL_TEXTURE) {
2062 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2063 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2064 }
2065 else {
2066 *params = 0;
2067 }
2068 }
2069 else {
2070 _mesa_error(ctx, GL_INVALID_ENUM,
2071 "glGetFramebufferAttachmentParameterivEXT(pname)");
2072 }
2073 return;
2074 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2075 if (att->Type == GL_TEXTURE) {
2076 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2077 *params = att->Zoffset;
2078 }
2079 else {
2080 *params = 0;
2081 }
2082 }
2083 else {
2084 _mesa_error(ctx, GL_INVALID_ENUM,
2085 "glGetFramebufferAttachmentParameterivEXT(pname)");
2086 }
2087 return;
2088 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2089 if (!ctx->Extensions.ARB_framebuffer_object) {
2090 _mesa_error(ctx, GL_INVALID_ENUM,
2091 "glGetFramebufferAttachmentParameterivEXT(pname)");
2092 }
2093 else {
2094 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2095 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2096 }
2097 else {
2098 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2099 * if the sRGB conversion is unsupported. */
2100 *params = GL_LINEAR;
2101 }
2102 }
2103 return;
2104 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2105 if (!ctx->Extensions.ARB_framebuffer_object) {
2106 _mesa_error(ctx, GL_INVALID_ENUM,
2107 "glGetFramebufferAttachmentParameterivEXT(pname)");
2108 return;
2109 }
2110 else {
2111 gl_format format = att->Renderbuffer->Format;
2112 if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
2113 /* special cases */
2114 *params = GL_INDEX;
2115 }
2116 else {
2117 *params = _mesa_get_format_datatype(format);
2118 }
2119 }
2120 return;
2121 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2122 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2123 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2124 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2125 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2126 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2127 if (!ctx->Extensions.ARB_framebuffer_object) {
2128 _mesa_error(ctx, GL_INVALID_ENUM,
2129 "glGetFramebufferAttachmentParameterivEXT(pname)");
2130 }
2131 else if (att->Texture) {
2132 const struct gl_texture_image *texImage =
2133 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2134 att->TextureLevel);
2135 if (texImage) {
2136 *params = get_component_bits(pname, texImage->_BaseFormat,
2137 texImage->TexFormat);
2138 }
2139 else {
2140 *params = 0;
2141 }
2142 }
2143 else if (att->Renderbuffer) {
2144 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2145 att->Renderbuffer->Format);
2146 }
2147 else {
2148 *params = 0;
2149 }
2150 return;
2151 default:
2152 _mesa_error(ctx, GL_INVALID_ENUM,
2153 "glGetFramebufferAttachmentParameterivEXT(pname)");
2154 return;
2155 }
2156 }
2157
2158
2159 void GLAPIENTRY
2160 _mesa_GenerateMipmapEXT(GLenum target)
2161 {
2162 struct gl_texture_object *texObj;
2163 GET_CURRENT_CONTEXT(ctx);
2164
2165 ASSERT_OUTSIDE_BEGIN_END(ctx);
2166 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2167
2168 switch (target) {
2169 case GL_TEXTURE_1D:
2170 case GL_TEXTURE_2D:
2171 case GL_TEXTURE_3D:
2172 case GL_TEXTURE_CUBE_MAP:
2173 /* OK, legal value */
2174 break;
2175 default:
2176 /* XXX need to implement GL_TEXTURE_1D_ARRAY and GL_TEXTURE_2D_ARRAY */
2177 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
2178 return;
2179 }
2180
2181 texObj = _mesa_get_current_tex_object(ctx, target);
2182
2183 if (texObj->BaseLevel >= texObj->MaxLevel) {
2184 /* nothing to do */
2185 return;
2186 }
2187
2188 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2189 !_mesa_cube_complete(texObj)) {
2190 _mesa_error(ctx, GL_INVALID_OPERATION,
2191 "glGenerateMipmap(incomplete cube map)");
2192 return;
2193 }
2194
2195 _mesa_lock_texture(ctx, texObj);
2196 if (target == GL_TEXTURE_CUBE_MAP) {
2197 GLuint face;
2198 for (face = 0; face < 6; face++)
2199 ctx->Driver.GenerateMipmap(ctx,
2200 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2201 texObj);
2202 }
2203 else {
2204 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2205 }
2206 _mesa_unlock_texture(ctx, texObj);
2207 }
2208
2209
2210 #if FEATURE_EXT_framebuffer_blit
2211
2212 static const struct gl_renderbuffer_attachment *
2213 find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
2214 {
2215 GLuint i;
2216 for (i = 0; i < Elements(fb->Attachment); i++) {
2217 if (fb->Attachment[i].Renderbuffer == rb)
2218 return &fb->Attachment[i];
2219 }
2220 return NULL;
2221 }
2222
2223
2224
2225 /**
2226 * Blit rectangular region, optionally from one framebuffer to another.
2227 *
2228 * Note, if the src buffer is multisampled and the dest is not, this is
2229 * when the samples must be resolved to a single color.
2230 */
2231 void GLAPIENTRY
2232 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2233 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2234 GLbitfield mask, GLenum filter)
2235 {
2236 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2237 GL_DEPTH_BUFFER_BIT |
2238 GL_STENCIL_BUFFER_BIT);
2239 const struct gl_framebuffer *readFb, *drawFb;
2240 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2241 GET_CURRENT_CONTEXT(ctx);
2242
2243 ASSERT_OUTSIDE_BEGIN_END(ctx);
2244 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2245
2246 if (ctx->NewState) {
2247 _mesa_update_state(ctx);
2248 }
2249
2250 readFb = ctx->ReadBuffer;
2251 drawFb = ctx->DrawBuffer;
2252
2253 if (!readFb || !drawFb) {
2254 /* This will normally never happen but someday we may want to
2255 * support MakeCurrent() with no drawables.
2256 */
2257 return;
2258 }
2259
2260 /* check for complete framebuffers */
2261 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2262 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2263 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2264 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2265 return;
2266 }
2267
2268 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2269 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2270 return;
2271 }
2272
2273 if (mask & ~legalMaskBits) {
2274 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2275 return;
2276 }
2277
2278 /* depth/stencil must be blitted with nearest filtering */
2279 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2280 && filter != GL_NEAREST) {
2281 _mesa_error(ctx, GL_INVALID_OPERATION,
2282 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2283 return;
2284 }
2285
2286 /* get color read/draw renderbuffers */
2287 if (mask & GL_COLOR_BUFFER_BIT) {
2288 colorReadRb = readFb->_ColorReadBuffer;
2289 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2290 }
2291 else {
2292 colorReadRb = colorDrawRb = NULL;
2293 }
2294
2295 if (mask & GL_STENCIL_BUFFER_BIT) {
2296 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2297 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2298 if (!readRb ||
2299 !drawRb ||
2300 _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2301 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2302 _mesa_error(ctx, GL_INVALID_OPERATION,
2303 "glBlitFramebufferEXT(stencil buffer size mismatch");
2304 return;
2305 }
2306 }
2307
2308 if (mask & GL_DEPTH_BUFFER_BIT) {
2309 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2310 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2311 if (!readRb ||
2312 !drawRb ||
2313 _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2314 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2315 _mesa_error(ctx, GL_INVALID_OPERATION,
2316 "glBlitFramebufferEXT(depth buffer size mismatch");
2317 return;
2318 }
2319 }
2320
2321 if (readFb->Visual.samples > 0 &&
2322 drawFb->Visual.samples > 0 &&
2323 readFb->Visual.samples != drawFb->Visual.samples) {
2324 _mesa_error(ctx, GL_INVALID_OPERATION,
2325 "glBlitFramebufferEXT(mismatched samples");
2326 return;
2327 }
2328
2329 /* extra checks for multisample copies... */
2330 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2331 /* src and dest region sizes must be the same */
2332 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2333 srcY1 - srcY0 != dstY1 - dstY0) {
2334 _mesa_error(ctx, GL_INVALID_OPERATION,
2335 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2336 return;
2337 }
2338
2339 /* color formats must match */
2340 if (colorReadRb &&
2341 colorDrawRb &&
2342 colorReadRb->Format != colorDrawRb->Format) {
2343 _mesa_error(ctx, GL_INVALID_OPERATION,
2344 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2345 return;
2346 }
2347 }
2348
2349 if (!ctx->Extensions.EXT_framebuffer_blit) {
2350 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2351 return;
2352 }
2353
2354 /* Debug code */
2355 if (DEBUG_BLIT) {
2356 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2357 " 0x%x, 0x%x)\n",
2358 srcX0, srcY0, srcX1, srcY1,
2359 dstX0, dstY0, dstX1, dstY1,
2360 mask, filter);
2361 if (colorReadRb) {
2362 const struct gl_renderbuffer_attachment *att;
2363
2364 att = find_attachment(readFb, colorReadRb);
2365 printf(" Src FBO %u RB %u (%dx%d) ",
2366 readFb->Name, colorReadRb->Name,
2367 colorReadRb->Width, colorReadRb->Height);
2368 if (att && att->Texture) {
2369 printf("Tex %u tgt 0x%x level %u face %u",
2370 att->Texture->Name,
2371 att->Texture->Target,
2372 att->TextureLevel,
2373 att->CubeMapFace);
2374 }
2375 printf("\n");
2376
2377 att = find_attachment(drawFb, colorDrawRb);
2378 printf(" Dst FBO %u RB %u (%dx%d) ",
2379 drawFb->Name, colorDrawRb->Name,
2380 colorDrawRb->Width, colorDrawRb->Height);
2381 if (att && att->Texture) {
2382 printf("Tex %u tgt 0x%x level %u face %u",
2383 att->Texture->Name,
2384 att->Texture->Target,
2385 att->TextureLevel,
2386 att->CubeMapFace);
2387 }
2388 printf("\n");
2389 }
2390 }
2391
2392 ASSERT(ctx->Driver.BlitFramebuffer);
2393 ctx->Driver.BlitFramebuffer(ctx,
2394 srcX0, srcY0, srcX1, srcY1,
2395 dstX0, dstY0, dstX1, dstY1,
2396 mask, filter);
2397 }
2398 #endif /* FEATURE_EXT_framebuffer_blit */
2399
2400 #if FEATURE_ARB_geometry_shader4
2401 void GLAPIENTRY
2402 _mesa_FramebufferTextureARB(GLenum target, GLenum attachment,
2403 GLuint texture, GLint level)
2404 {
2405 GET_CURRENT_CONTEXT(ctx);
2406 _mesa_error(ctx, GL_INVALID_OPERATION,
2407 "glFramebufferTextureARB "
2408 "not implemented!");
2409 }
2410
2411 void GLAPIENTRY
2412 _mesa_FramebufferTextureFaceARB(GLenum target, GLenum attachment,
2413 GLuint texture, GLint level, GLenum face)
2414 {
2415 GET_CURRENT_CONTEXT(ctx);
2416 _mesa_error(ctx, GL_INVALID_OPERATION,
2417 "glFramebufferTextureFaceARB "
2418 "not implemented!");
2419 }
2420 #endif /* FEATURE_ARB_geometry_shader4 */