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