mesa: add missing breaks for GL_TEXTURE_CUBE_MAP_SEAMLESS queries
[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_DEPTH_COMPONENT32F:
1135 if (ctx->Extensions.ARB_depth_buffer_float)
1136 return GL_DEPTH_COMPONENT;
1137 else
1138 return 0;
1139 case GL_DEPTH32F_STENCIL8:
1140 if (ctx->Extensions.ARB_depth_buffer_float)
1141 return GL_DEPTH_STENCIL;
1142 else
1143 return 0;
1144 case GL_RED:
1145 case GL_R8:
1146 case GL_R16:
1147 return ctx->Extensions.ARB_texture_rg ? GL_RED : 0;
1148 case GL_RG:
1149 case GL_RG8:
1150 case GL_RG16:
1151 return ctx->Extensions.ARB_texture_rg ? GL_RG : 0;
1152 /* signed normalized texture formats */
1153 case GL_RED_SNORM:
1154 case GL_R8_SNORM:
1155 case GL_R16_SNORM:
1156 return ctx->Extensions.EXT_texture_snorm ? GL_RED : 0;
1157 case GL_RG_SNORM:
1158 case GL_RG8_SNORM:
1159 case GL_RG16_SNORM:
1160 return ctx->Extensions.EXT_texture_snorm ? GL_RG : 0;
1161 case GL_RGB_SNORM:
1162 case GL_RGB8_SNORM:
1163 case GL_RGB16_SNORM:
1164 return ctx->Extensions.EXT_texture_snorm ? GL_RGB : 0;
1165 case GL_RGBA_SNORM:
1166 case GL_RGBA8_SNORM:
1167 case GL_RGBA16_SNORM:
1168 return ctx->Extensions.EXT_texture_snorm ? GL_RGBA : 0;
1169 case GL_ALPHA_SNORM:
1170 case GL_ALPHA8_SNORM:
1171 case GL_ALPHA16_SNORM:
1172 return ctx->Extensions.EXT_texture_snorm &&
1173 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1174 case GL_LUMINANCE_SNORM:
1175 case GL_LUMINANCE8_SNORM:
1176 case GL_LUMINANCE16_SNORM:
1177 return ctx->Extensions.EXT_texture_snorm &&
1178 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1179 case GL_LUMINANCE_ALPHA_SNORM:
1180 case GL_LUMINANCE8_ALPHA8_SNORM:
1181 case GL_LUMINANCE16_ALPHA16_SNORM:
1182 return ctx->Extensions.EXT_texture_snorm &&
1183 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1184 case GL_INTENSITY_SNORM:
1185 case GL_INTENSITY8_SNORM:
1186 case GL_INTENSITY16_SNORM:
1187 return ctx->Extensions.EXT_texture_snorm &&
1188 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1189 case GL_R16F:
1190 case GL_R32F:
1191 return ctx->Extensions.ARB_texture_rg &&
1192 ctx->Extensions.ARB_texture_float ? GL_RED : 0;
1193 case GL_RG16F:
1194 case GL_RG32F:
1195 return ctx->Extensions.ARB_texture_rg &&
1196 ctx->Extensions.ARB_texture_float ? GL_RG : 0;
1197 case GL_RGB16F:
1198 case GL_RGB32F:
1199 return ctx->Extensions.ARB_texture_float ? GL_RGB : 0;
1200 case GL_RGBA16F:
1201 case GL_RGBA32F:
1202 return ctx->Extensions.ARB_texture_float ? GL_RGBA : 0;
1203 case GL_ALPHA16F_ARB:
1204 case GL_ALPHA32F_ARB:
1205 return ctx->Extensions.ARB_texture_float &&
1206 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1207 case GL_LUMINANCE16F_ARB:
1208 case GL_LUMINANCE32F_ARB:
1209 return ctx->Extensions.ARB_texture_float &&
1210 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1211 case GL_LUMINANCE_ALPHA16F_ARB:
1212 case GL_LUMINANCE_ALPHA32F_ARB:
1213 return ctx->Extensions.ARB_texture_float &&
1214 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1215 case GL_INTENSITY16F_ARB:
1216 case GL_INTENSITY32F_ARB:
1217 return ctx->Extensions.ARB_texture_float &&
1218 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1219 case GL_RGB9_E5:
1220 return ctx->Extensions.EXT_texture_shared_exponent ? GL_RGB : 0;
1221 case GL_R11F_G11F_B10F:
1222 return ctx->Extensions.EXT_packed_float ? GL_RGB : 0;
1223 /* XXX add integer formats eventually */
1224 default:
1225 return 0;
1226 }
1227 }
1228
1229
1230 /**
1231 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1232 */
1233 static void
1234 invalidate_rb(GLuint key, void *data, void *userData)
1235 {
1236 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1237 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1238
1239 /* If this is a user-created FBO */
1240 if (is_user_fbo(fb)) {
1241 GLuint i;
1242 for (i = 0; i < BUFFER_COUNT; i++) {
1243 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1244 if (att->Type == GL_RENDERBUFFER &&
1245 att->Renderbuffer == rb) {
1246 /* Mark fb status as indeterminate to force re-validation */
1247 fb->_Status = 0;
1248 return;
1249 }
1250 }
1251 }
1252 }
1253
1254
1255 /** sentinal value, see below */
1256 #define NO_SAMPLES 1000
1257
1258
1259 /**
1260 * Helper function used by _mesa_RenderbufferStorageEXT() and
1261 * _mesa_RenderbufferStorageMultisample().
1262 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
1263 */
1264 static void
1265 renderbuffer_storage(GLenum target, GLenum internalFormat,
1266 GLsizei width, GLsizei height, GLsizei samples)
1267 {
1268 const char *func = samples == NO_SAMPLES ?
1269 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
1270 struct gl_renderbuffer *rb;
1271 GLenum baseFormat;
1272 GET_CURRENT_CONTEXT(ctx);
1273
1274 ASSERT_OUTSIDE_BEGIN_END(ctx);
1275
1276 if (target != GL_RENDERBUFFER_EXT) {
1277 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1278 return;
1279 }
1280
1281 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1282 if (baseFormat == 0) {
1283 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
1284 return;
1285 }
1286
1287 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1288 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1289 return;
1290 }
1291
1292 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1293 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1294 return;
1295 }
1296
1297 if (samples == NO_SAMPLES) {
1298 /* NumSamples == 0 indicates non-multisampling */
1299 samples = 0;
1300 }
1301 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1302 /* note: driver may choose to use more samples than what's requested */
1303 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1304 return;
1305 }
1306
1307 rb = ctx->CurrentRenderbuffer;
1308 if (!rb) {
1309 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1310 return;
1311 }
1312
1313 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1314
1315 if (rb->InternalFormat == internalFormat &&
1316 rb->Width == (GLuint) width &&
1317 rb->Height == (GLuint) height) {
1318 /* no change in allocation needed */
1319 return;
1320 }
1321
1322 /* These MUST get set by the AllocStorage func */
1323 rb->Format = MESA_FORMAT_NONE;
1324 rb->NumSamples = samples;
1325
1326 /* Now allocate the storage */
1327 ASSERT(rb->AllocStorage);
1328 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1329 /* No error - check/set fields now */
1330 assert(rb->Format != MESA_FORMAT_NONE);
1331 assert(rb->Width == (GLuint) width);
1332 assert(rb->Height == (GLuint) height);
1333 rb->InternalFormat = internalFormat;
1334 rb->_BaseFormat = baseFormat;
1335 assert(rb->_BaseFormat != 0);
1336 }
1337 else {
1338 /* Probably ran out of memory - clear the fields */
1339 rb->Width = 0;
1340 rb->Height = 0;
1341 rb->Format = MESA_FORMAT_NONE;
1342 rb->InternalFormat = GL_NONE;
1343 rb->_BaseFormat = GL_NONE;
1344 rb->NumSamples = 0;
1345 }
1346
1347 /* Invalidate the framebuffers the renderbuffer is attached in. */
1348 if (rb->AttachedAnytime) {
1349 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1350 }
1351 }
1352
1353
1354 #if FEATURE_OES_EGL_image
1355 void GLAPIENTRY
1356 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1357 {
1358 struct gl_renderbuffer *rb;
1359 GET_CURRENT_CONTEXT(ctx);
1360 ASSERT_OUTSIDE_BEGIN_END(ctx);
1361
1362 if (!ctx->Extensions.OES_EGL_image) {
1363 _mesa_error(ctx, GL_INVALID_OPERATION,
1364 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1365 return;
1366 }
1367
1368 if (target != GL_RENDERBUFFER) {
1369 _mesa_error(ctx, GL_INVALID_ENUM,
1370 "EGLImageTargetRenderbufferStorageOES");
1371 return;
1372 }
1373
1374 rb = ctx->CurrentRenderbuffer;
1375 if (!rb) {
1376 _mesa_error(ctx, GL_INVALID_OPERATION,
1377 "EGLImageTargetRenderbufferStorageOES");
1378 return;
1379 }
1380
1381 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1382
1383 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1384 }
1385 #endif
1386
1387
1388 /**
1389 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1390 * _mesa_GetFramebufferAttachmentParameterivEXT()
1391 * We have to be careful to respect the base format. For example, if a
1392 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1393 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1394 * we need to return zero.
1395 */
1396 static GLint
1397 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1398 {
1399 switch (pname) {
1400 case GL_RENDERBUFFER_RED_SIZE_EXT:
1401 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1402 if (baseFormat == GL_RGB || baseFormat == GL_RGBA ||
1403 baseFormat == GL_RG || baseFormat == GL_RED)
1404 return _mesa_get_format_bits(format, pname);
1405 else
1406 return 0;
1407 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1408 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1409 if (baseFormat == GL_RGB || baseFormat == GL_RGBA || baseFormat == GL_RG)
1410 return _mesa_get_format_bits(format, pname);
1411 else
1412 return 0;
1413 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1414 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1415 if (baseFormat == GL_RGB || baseFormat == GL_RGBA)
1416 return _mesa_get_format_bits(format, pname);
1417 else
1418 return 0;
1419 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1420 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1421 if (baseFormat == GL_RGBA || baseFormat == GL_ALPHA ||
1422 baseFormat == GL_LUMINANCE_ALPHA)
1423 return _mesa_get_format_bits(format, pname);
1424 else
1425 return 0;
1426 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1427 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1428 if (baseFormat == GL_DEPTH_COMPONENT || baseFormat == GL_DEPTH_STENCIL)
1429 return _mesa_get_format_bits(format, pname);
1430 else
1431 return 0;
1432 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1433 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1434 if (baseFormat == GL_STENCIL_INDEX || baseFormat == GL_DEPTH_STENCIL)
1435 return _mesa_get_format_bits(format, pname);
1436 else
1437 return 0;
1438 default:
1439 return 0;
1440 }
1441 }
1442
1443
1444
1445 void GLAPIENTRY
1446 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1447 GLsizei width, GLsizei height)
1448 {
1449 /* GL_ARB_fbo says calling this function is equivalent to calling
1450 * glRenderbufferStorageMultisample() with samples=0. We pass in
1451 * a token value here just for error reporting purposes.
1452 */
1453 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1454 }
1455
1456
1457 void GLAPIENTRY
1458 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1459 GLenum internalFormat,
1460 GLsizei width, GLsizei height)
1461 {
1462 renderbuffer_storage(target, internalFormat, width, height, samples);
1463 }
1464
1465
1466 /**
1467 * OpenGL ES version of glRenderBufferStorage.
1468 */
1469 void GLAPIENTRY
1470 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1471 GLsizei width, GLsizei height)
1472 {
1473 switch (internalFormat) {
1474 case GL_RGB565:
1475 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1476 /* choose a closest format */
1477 internalFormat = GL_RGB5;
1478 break;
1479 default:
1480 break;
1481 }
1482
1483 renderbuffer_storage(target, internalFormat, width, height, 0);
1484 }
1485
1486
1487 void GLAPIENTRY
1488 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1489 {
1490 struct gl_renderbuffer *rb;
1491 GET_CURRENT_CONTEXT(ctx);
1492
1493 ASSERT_OUTSIDE_BEGIN_END(ctx);
1494
1495 if (target != GL_RENDERBUFFER_EXT) {
1496 _mesa_error(ctx, GL_INVALID_ENUM,
1497 "glGetRenderbufferParameterivEXT(target)");
1498 return;
1499 }
1500
1501 rb = ctx->CurrentRenderbuffer;
1502 if (!rb) {
1503 _mesa_error(ctx, GL_INVALID_OPERATION,
1504 "glGetRenderbufferParameterivEXT");
1505 return;
1506 }
1507
1508 /* No need to flush here since we're just quering state which is
1509 * not effected by rendering.
1510 */
1511
1512 switch (pname) {
1513 case GL_RENDERBUFFER_WIDTH_EXT:
1514 *params = rb->Width;
1515 return;
1516 case GL_RENDERBUFFER_HEIGHT_EXT:
1517 *params = rb->Height;
1518 return;
1519 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1520 *params = rb->InternalFormat;
1521 return;
1522 case GL_RENDERBUFFER_RED_SIZE_EXT:
1523 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1524 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1525 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1526 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1527 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1528 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1529 break;
1530 case GL_RENDERBUFFER_SAMPLES:
1531 if (ctx->Extensions.ARB_framebuffer_object) {
1532 *params = rb->NumSamples;
1533 break;
1534 }
1535 /* fallthrough */
1536 default:
1537 _mesa_error(ctx, GL_INVALID_ENUM,
1538 "glGetRenderbufferParameterivEXT(target)");
1539 return;
1540 }
1541 }
1542
1543
1544 GLboolean GLAPIENTRY
1545 _mesa_IsFramebufferEXT(GLuint framebuffer)
1546 {
1547 GET_CURRENT_CONTEXT(ctx);
1548 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1549 if (framebuffer) {
1550 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1551 if (rb != NULL && rb != &DummyFramebuffer)
1552 return GL_TRUE;
1553 }
1554 return GL_FALSE;
1555 }
1556
1557
1558 /**
1559 * Check if any of the attachments of the given framebuffer are textures
1560 * (render to texture). Call ctx->Driver.RenderTexture() for such
1561 * attachments.
1562 */
1563 static void
1564 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1565 {
1566 GLuint i;
1567 ASSERT(ctx->Driver.RenderTexture);
1568
1569 if (is_winsys_fbo(fb))
1570 return; /* can't render to texture with winsys framebuffers */
1571
1572 for (i = 0; i < BUFFER_COUNT; i++) {
1573 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1574 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1575 ctx->Driver.RenderTexture(ctx, fb, att);
1576 }
1577 }
1578 }
1579
1580
1581 /**
1582 * Examine all the framebuffer's attachments to see if any are textures.
1583 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1584 * notify the device driver that the texture image may have changed.
1585 */
1586 static void
1587 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1588 {
1589 if (is_winsys_fbo(fb))
1590 return; /* can't render to texture with winsys framebuffers */
1591
1592 if (ctx->Driver.FinishRenderTexture) {
1593 GLuint i;
1594 for (i = 0; i < BUFFER_COUNT; i++) {
1595 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1596 if (att->Texture && att->Renderbuffer) {
1597 ctx->Driver.FinishRenderTexture(ctx, att);
1598 }
1599 }
1600 }
1601 }
1602
1603
1604 void GLAPIENTRY
1605 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1606 {
1607 struct gl_framebuffer *newDrawFb, *newReadFb;
1608 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1609 GLboolean bindReadBuf, bindDrawBuf;
1610 GET_CURRENT_CONTEXT(ctx);
1611
1612 #ifdef DEBUG
1613 if (ctx->Extensions.ARB_framebuffer_object) {
1614 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1615 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1616 }
1617 #endif
1618
1619 ASSERT_OUTSIDE_BEGIN_END(ctx);
1620
1621 if (!ctx->Extensions.EXT_framebuffer_object) {
1622 _mesa_error(ctx, GL_INVALID_OPERATION,
1623 "glBindFramebufferEXT(unsupported)");
1624 return;
1625 }
1626
1627 switch (target) {
1628 #if FEATURE_EXT_framebuffer_blit
1629 case GL_DRAW_FRAMEBUFFER_EXT:
1630 if (!ctx->Extensions.EXT_framebuffer_blit) {
1631 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1632 return;
1633 }
1634 bindDrawBuf = GL_TRUE;
1635 bindReadBuf = GL_FALSE;
1636 break;
1637 case GL_READ_FRAMEBUFFER_EXT:
1638 if (!ctx->Extensions.EXT_framebuffer_blit) {
1639 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1640 return;
1641 }
1642 bindDrawBuf = GL_FALSE;
1643 bindReadBuf = GL_TRUE;
1644 break;
1645 #endif
1646 case GL_FRAMEBUFFER_EXT:
1647 bindDrawBuf = GL_TRUE;
1648 bindReadBuf = GL_TRUE;
1649 break;
1650 default:
1651 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1652 return;
1653 }
1654
1655 if (framebuffer) {
1656 /* Binding a user-created framebuffer object */
1657 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1658 if (newDrawFb == &DummyFramebuffer) {
1659 /* ID was reserved, but no real framebuffer object made yet */
1660 newDrawFb = NULL;
1661 }
1662 else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1663 /* All FBO IDs must be Gen'd */
1664 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1665 return;
1666 }
1667
1668 if (!newDrawFb) {
1669 /* create new framebuffer object */
1670 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1671 if (!newDrawFb) {
1672 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1673 return;
1674 }
1675 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1676 }
1677 newReadFb = newDrawFb;
1678 }
1679 else {
1680 /* Binding the window system framebuffer (which was originally set
1681 * with MakeCurrent).
1682 */
1683 newDrawFb = ctx->WinSysDrawBuffer;
1684 newReadFb = ctx->WinSysReadBuffer;
1685 }
1686
1687 ASSERT(newDrawFb);
1688 ASSERT(newDrawFb != &DummyFramebuffer);
1689
1690 /* save pointers to current/old framebuffers */
1691 oldDrawFb = ctx->DrawBuffer;
1692 oldReadFb = ctx->ReadBuffer;
1693
1694 /* check if really changing bindings */
1695 if (oldDrawFb == newDrawFb)
1696 bindDrawBuf = GL_FALSE;
1697 if (oldReadFb == newReadFb)
1698 bindReadBuf = GL_FALSE;
1699
1700 /*
1701 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1702 *
1703 * We also check if we're beginning and/or ending render-to-texture.
1704 * When a framebuffer with texture attachments is unbound, call
1705 * ctx->Driver.FinishRenderTexture().
1706 * When a framebuffer with texture attachments is bound, call
1707 * ctx->Driver.RenderTexture().
1708 *
1709 * Note that if the ReadBuffer has texture attachments we don't consider
1710 * that a render-to-texture case.
1711 */
1712 if (bindReadBuf) {
1713 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1714
1715 /* check if old readbuffer was render-to-texture */
1716 check_end_texture_render(ctx, oldReadFb);
1717
1718 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1719 }
1720
1721 if (bindDrawBuf) {
1722 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1723
1724 /* check if old read/draw buffers were render-to-texture */
1725 if (!bindReadBuf)
1726 check_end_texture_render(ctx, oldReadFb);
1727
1728 if (oldDrawFb != oldReadFb)
1729 check_end_texture_render(ctx, oldDrawFb);
1730
1731 /* check if newly bound framebuffer has any texture attachments */
1732 check_begin_texture_render(ctx, newDrawFb);
1733
1734 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1735 }
1736
1737 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1738 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1739 }
1740 }
1741
1742
1743 void GLAPIENTRY
1744 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1745 {
1746 GLint i;
1747 GET_CURRENT_CONTEXT(ctx);
1748
1749 ASSERT_OUTSIDE_BEGIN_END(ctx);
1750 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1751
1752 for (i = 0; i < n; i++) {
1753 if (framebuffers[i] > 0) {
1754 struct gl_framebuffer *fb;
1755 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1756 if (fb) {
1757 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1758
1759 /* check if deleting currently bound framebuffer object */
1760 if (ctx->Extensions.EXT_framebuffer_blit) {
1761 /* separate draw/read binding points */
1762 if (fb == ctx->DrawBuffer) {
1763 /* bind default */
1764 ASSERT(fb->RefCount >= 2);
1765 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1766 }
1767 if (fb == ctx->ReadBuffer) {
1768 /* bind default */
1769 ASSERT(fb->RefCount >= 2);
1770 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1771 }
1772 }
1773 else {
1774 /* only one binding point for read/draw buffers */
1775 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1776 /* bind default */
1777 ASSERT(fb->RefCount >= 2);
1778 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1779 }
1780 }
1781
1782 /* remove from hash table immediately, to free the ID */
1783 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1784
1785 if (fb != &DummyFramebuffer) {
1786 /* But the object will not be freed until it's no longer
1787 * bound in any context.
1788 */
1789 _mesa_reference_framebuffer(&fb, NULL);
1790 }
1791 }
1792 }
1793 }
1794 }
1795
1796
1797 void GLAPIENTRY
1798 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1799 {
1800 GET_CURRENT_CONTEXT(ctx);
1801 GLuint first;
1802 GLint i;
1803
1804 ASSERT_OUTSIDE_BEGIN_END(ctx);
1805
1806 if (n < 0) {
1807 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1808 return;
1809 }
1810
1811 if (!framebuffers)
1812 return;
1813
1814 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1815
1816 for (i = 0; i < n; i++) {
1817 GLuint name = first + i;
1818 framebuffers[i] = name;
1819 /* insert dummy placeholder into hash table */
1820 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1821 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1822 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1823 }
1824 }
1825
1826
1827
1828 GLenum GLAPIENTRY
1829 _mesa_CheckFramebufferStatusEXT(GLenum target)
1830 {
1831 struct gl_framebuffer *buffer;
1832 GET_CURRENT_CONTEXT(ctx);
1833
1834 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1835
1836 buffer = get_framebuffer_target(ctx, target);
1837 if (!buffer) {
1838 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1839 return 0;
1840 }
1841
1842 if (is_winsys_fbo(buffer)) {
1843 /* The window system / default framebuffer is always complete */
1844 return GL_FRAMEBUFFER_COMPLETE_EXT;
1845 }
1846
1847 /* No need to flush here */
1848
1849 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1850 _mesa_test_framebuffer_completeness(ctx, buffer);
1851 }
1852
1853 return buffer->_Status;
1854 }
1855
1856
1857
1858 /**
1859 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1860 */
1861 static void
1862 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
1863 GLenum attachment, GLenum textarget, GLuint texture,
1864 GLint level, GLint zoffset)
1865 {
1866 struct gl_renderbuffer_attachment *att;
1867 struct gl_texture_object *texObj = NULL;
1868 struct gl_framebuffer *fb;
1869
1870 ASSERT_OUTSIDE_BEGIN_END(ctx);
1871
1872 fb = get_framebuffer_target(ctx, target);
1873 if (!fb) {
1874 _mesa_error(ctx, GL_INVALID_ENUM,
1875 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1876 return;
1877 }
1878
1879 /* check framebuffer binding */
1880 if (is_winsys_fbo(fb)) {
1881 _mesa_error(ctx, GL_INVALID_OPERATION,
1882 "glFramebufferTexture%sEXT", caller);
1883 return;
1884 }
1885
1886
1887 /* The textarget, level, and zoffset parameters are only validated if
1888 * texture is non-zero.
1889 */
1890 if (texture) {
1891 GLboolean err = GL_TRUE;
1892
1893 texObj = _mesa_lookup_texture(ctx, texture);
1894 if (texObj != NULL) {
1895 if (textarget == 0) {
1896 /* XXX what's the purpose of this? */
1897 err = (texObj->Target != GL_TEXTURE_3D) &&
1898 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1899 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1900 }
1901 else {
1902 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1903 ? !is_cube_face(textarget)
1904 : (texObj->Target != textarget);
1905 }
1906 }
1907 else {
1908 /* can't render to a non-existant texture */
1909 _mesa_error(ctx, GL_INVALID_OPERATION,
1910 "glFramebufferTexture%sEXT(non existant texture)",
1911 caller);
1912 return;
1913 }
1914
1915 if (err) {
1916 _mesa_error(ctx, GL_INVALID_OPERATION,
1917 "glFramebufferTexture%sEXT(texture target mismatch)",
1918 caller);
1919 return;
1920 }
1921
1922 if (texObj->Target == GL_TEXTURE_3D) {
1923 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1924 if (zoffset < 0 || zoffset >= maxSize) {
1925 _mesa_error(ctx, GL_INVALID_VALUE,
1926 "glFramebufferTexture%sEXT(zoffset)", caller);
1927 return;
1928 }
1929 }
1930 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1931 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1932 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1933 _mesa_error(ctx, GL_INVALID_VALUE,
1934 "glFramebufferTexture%sEXT(layer)", caller);
1935 return;
1936 }
1937 }
1938
1939 if ((level < 0) ||
1940 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1941 _mesa_error(ctx, GL_INVALID_VALUE,
1942 "glFramebufferTexture%sEXT(level)", caller);
1943 return;
1944 }
1945 }
1946
1947 att = _mesa_get_attachment(ctx, fb, attachment);
1948 if (att == NULL) {
1949 _mesa_error(ctx, GL_INVALID_ENUM,
1950 "glFramebufferTexture%sEXT(attachment)", caller);
1951 return;
1952 }
1953
1954 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1955
1956 _glthread_LOCK_MUTEX(fb->Mutex);
1957 if (texObj) {
1958 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1959 level, zoffset);
1960 /* Set the render-to-texture flag. We'll check this flag in
1961 * glTexImage() and friends to determine if we need to revalidate
1962 * any FBOs that might be rendering into this texture.
1963 * This flag never gets cleared since it's non-trivial to determine
1964 * when all FBOs might be done rendering to this texture. That's OK
1965 * though since it's uncommon to render to a texture then repeatedly
1966 * call glTexImage() to change images in the texture.
1967 */
1968 texObj->_RenderToTexture = GL_TRUE;
1969 }
1970 else {
1971 _mesa_remove_attachment(ctx, att);
1972 }
1973
1974 invalidate_framebuffer(fb);
1975
1976 _glthread_UNLOCK_MUTEX(fb->Mutex);
1977 }
1978
1979
1980
1981 void GLAPIENTRY
1982 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1983 GLenum textarget, GLuint texture, GLint level)
1984 {
1985 GET_CURRENT_CONTEXT(ctx);
1986
1987 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1988 _mesa_error(ctx, GL_INVALID_ENUM,
1989 "glFramebufferTexture1DEXT(textarget)");
1990 return;
1991 }
1992
1993 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1994 level, 0);
1995 }
1996
1997
1998 void GLAPIENTRY
1999 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
2000 GLenum textarget, GLuint texture, GLint level)
2001 {
2002 GET_CURRENT_CONTEXT(ctx);
2003
2004 if ((texture != 0) &&
2005 (textarget != GL_TEXTURE_2D) &&
2006 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
2007 (!is_cube_face(textarget))) {
2008 _mesa_error(ctx, GL_INVALID_OPERATION,
2009 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
2010 return;
2011 }
2012
2013 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2014 level, 0);
2015 }
2016
2017
2018 void GLAPIENTRY
2019 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
2020 GLenum textarget, GLuint texture,
2021 GLint level, GLint zoffset)
2022 {
2023 GET_CURRENT_CONTEXT(ctx);
2024
2025 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2026 _mesa_error(ctx, GL_INVALID_ENUM,
2027 "glFramebufferTexture3DEXT(textarget)");
2028 return;
2029 }
2030
2031 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2032 level, zoffset);
2033 }
2034
2035
2036 void GLAPIENTRY
2037 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
2038 GLuint texture, GLint level, GLint layer)
2039 {
2040 GET_CURRENT_CONTEXT(ctx);
2041
2042 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2043 level, layer);
2044 }
2045
2046
2047 void GLAPIENTRY
2048 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
2049 GLenum renderbufferTarget,
2050 GLuint renderbuffer)
2051 {
2052 struct gl_renderbuffer_attachment *att;
2053 struct gl_framebuffer *fb;
2054 struct gl_renderbuffer *rb;
2055 GET_CURRENT_CONTEXT(ctx);
2056
2057 ASSERT_OUTSIDE_BEGIN_END(ctx);
2058
2059 fb = get_framebuffer_target(ctx, target);
2060 if (!fb) {
2061 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2062 return;
2063 }
2064
2065 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2066 _mesa_error(ctx, GL_INVALID_ENUM,
2067 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2068 return;
2069 }
2070
2071 if (is_winsys_fbo(fb)) {
2072 /* Can't attach new renderbuffers to a window system framebuffer */
2073 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2074 return;
2075 }
2076
2077 att = _mesa_get_attachment(ctx, fb, attachment);
2078 if (att == NULL) {
2079 _mesa_error(ctx, GL_INVALID_ENUM,
2080 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2081 _mesa_lookup_enum_by_nr(attachment));
2082 return;
2083 }
2084
2085 if (renderbuffer) {
2086 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2087 if (!rb) {
2088 _mesa_error(ctx, GL_INVALID_OPERATION,
2089 "glFramebufferRenderbufferEXT(non-existant"
2090 " renderbuffer %u)", renderbuffer);
2091 return;
2092 }
2093 else if (rb == &DummyRenderbuffer) {
2094 /* This is what NVIDIA does */
2095 _mesa_error(ctx, GL_INVALID_VALUE,
2096 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2097 renderbuffer);
2098 return;
2099 }
2100 }
2101 else {
2102 /* remove renderbuffer attachment */
2103 rb = NULL;
2104 }
2105
2106 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2107 rb && rb->Format != MESA_FORMAT_NONE) {
2108 /* make sure the renderbuffer is a depth/stencil format */
2109 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2110 if (baseFormat != GL_DEPTH_STENCIL) {
2111 _mesa_error(ctx, GL_INVALID_OPERATION,
2112 "glFramebufferRenderbufferEXT(renderbuffer"
2113 " is not DEPTH_STENCIL format)");
2114 return;
2115 }
2116 }
2117
2118
2119 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2120
2121 assert(ctx->Driver.FramebufferRenderbuffer);
2122 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2123
2124 /* Some subsequent GL commands may depend on the framebuffer's visual
2125 * after the binding is updated. Update visual info now.
2126 */
2127 _mesa_update_framebuffer_visual(ctx, fb);
2128 }
2129
2130
2131 void GLAPIENTRY
2132 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
2133 GLenum pname, GLint *params)
2134 {
2135 const struct gl_renderbuffer_attachment *att;
2136 struct gl_framebuffer *buffer;
2137 GLenum err;
2138 GET_CURRENT_CONTEXT(ctx);
2139
2140 ASSERT_OUTSIDE_BEGIN_END(ctx);
2141
2142 /* The error differs in GL andd GLES. */
2143 err = ctx->API == API_OPENGL ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2144
2145 buffer = get_framebuffer_target(ctx, target);
2146 if (!buffer) {
2147 _mesa_error(ctx, GL_INVALID_ENUM,
2148 "glGetFramebufferAttachmentParameterivEXT(target)");
2149 return;
2150 }
2151
2152 if (is_winsys_fbo(buffer)) {
2153 /* the default / window-system FBO */
2154 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2155 }
2156 else {
2157 /* user-created framebuffer FBO */
2158 att = _mesa_get_attachment(ctx, buffer, attachment);
2159 }
2160
2161 if (att == NULL) {
2162 _mesa_error(ctx, GL_INVALID_ENUM,
2163 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2164 return;
2165 }
2166
2167 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2168 /* the depth and stencil attachments must point to the same buffer */
2169 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2170 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2171 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2172 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2173 _mesa_error(ctx, GL_INVALID_OPERATION,
2174 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2175 " attachments differ)");
2176 return;
2177 }
2178 }
2179
2180 /* No need to flush here */
2181
2182 switch (pname) {
2183 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2184 *params = is_winsys_fbo(buffer) ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2185 return;
2186 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2187 if (att->Type == GL_RENDERBUFFER_EXT) {
2188 *params = att->Renderbuffer->Name;
2189 }
2190 else if (att->Type == GL_TEXTURE) {
2191 *params = att->Texture->Name;
2192 }
2193 else {
2194 assert(att->Type == GL_NONE);
2195 if (ctx->API == API_OPENGL) {
2196 *params = 0;
2197 } else {
2198 _mesa_error(ctx, GL_INVALID_ENUM,
2199 "glGetFramebufferAttachmentParameterivEXT(pname)");
2200 }
2201 }
2202 return;
2203 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2204 if (att->Type == GL_TEXTURE) {
2205 *params = att->TextureLevel;
2206 }
2207 else if (att->Type == GL_NONE) {
2208 _mesa_error(ctx, err,
2209 "glGetFramebufferAttachmentParameterivEXT(pname)");
2210 }
2211 else {
2212 _mesa_error(ctx, GL_INVALID_ENUM,
2213 "glGetFramebufferAttachmentParameterivEXT(pname)");
2214 }
2215 return;
2216 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2217 if (att->Type == GL_TEXTURE) {
2218 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2219 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2220 }
2221 else {
2222 *params = 0;
2223 }
2224 }
2225 else if (att->Type == GL_NONE) {
2226 _mesa_error(ctx, err,
2227 "glGetFramebufferAttachmentParameterivEXT(pname)");
2228 }
2229 else {
2230 _mesa_error(ctx, GL_INVALID_ENUM,
2231 "glGetFramebufferAttachmentParameterivEXT(pname)");
2232 }
2233 return;
2234 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2235 if (att->Type == GL_TEXTURE) {
2236 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2237 *params = att->Zoffset;
2238 }
2239 else {
2240 *params = 0;
2241 }
2242 }
2243 else if (att->Type == GL_NONE) {
2244 _mesa_error(ctx, err,
2245 "glGetFramebufferAttachmentParameterivEXT(pname)");
2246 }
2247 else {
2248 _mesa_error(ctx, GL_INVALID_ENUM,
2249 "glGetFramebufferAttachmentParameterivEXT(pname)");
2250 }
2251 return;
2252 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2253 if (!ctx->Extensions.ARB_framebuffer_object) {
2254 _mesa_error(ctx, GL_INVALID_ENUM,
2255 "glGetFramebufferAttachmentParameterivEXT(pname)");
2256 }
2257 else if (att->Type == GL_NONE) {
2258 _mesa_error(ctx, err,
2259 "glGetFramebufferAttachmentParameterivEXT(pname)");
2260 }
2261 else {
2262 if (ctx->Extensions.EXT_framebuffer_sRGB && ctx->Const.sRGBCapable) {
2263 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2264 }
2265 else {
2266 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2267 * if the sRGB conversion is unsupported. */
2268 *params = GL_LINEAR;
2269 }
2270 }
2271 return;
2272 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2273 if (!ctx->Extensions.ARB_framebuffer_object) {
2274 _mesa_error(ctx, GL_INVALID_ENUM,
2275 "glGetFramebufferAttachmentParameterivEXT(pname)");
2276 return;
2277 }
2278 else if (att->Type == GL_NONE) {
2279 _mesa_error(ctx, err,
2280 "glGetFramebufferAttachmentParameterivEXT(pname)");
2281 }
2282 else {
2283 gl_format format = att->Renderbuffer->Format;
2284 if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
2285 /* special cases */
2286 *params = GL_INDEX;
2287 }
2288 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2289 /* depends on the attachment parameter */
2290 if (attachment == GL_STENCIL_ATTACHMENT) {
2291 *params = GL_INDEX;
2292 }
2293 else {
2294 *params = GL_FLOAT;
2295 }
2296 }
2297 else {
2298 *params = _mesa_get_format_datatype(format);
2299 }
2300 }
2301 return;
2302 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2303 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2304 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2305 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2306 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2307 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2308 if (!ctx->Extensions.ARB_framebuffer_object) {
2309 _mesa_error(ctx, GL_INVALID_ENUM,
2310 "glGetFramebufferAttachmentParameterivEXT(pname)");
2311 }
2312 else if (att->Type == GL_NONE) {
2313 _mesa_error(ctx, err,
2314 "glGetFramebufferAttachmentParameterivEXT(pname)");
2315 }
2316 else if (att->Texture) {
2317 const struct gl_texture_image *texImage =
2318 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2319 att->TextureLevel);
2320 if (texImage) {
2321 *params = get_component_bits(pname, texImage->_BaseFormat,
2322 texImage->TexFormat);
2323 }
2324 else {
2325 *params = 0;
2326 }
2327 }
2328 else if (att->Renderbuffer) {
2329 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2330 att->Renderbuffer->Format);
2331 }
2332 else {
2333 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2334 " invalid FBO attachment structure");
2335 }
2336 return;
2337 default:
2338 _mesa_error(ctx, GL_INVALID_ENUM,
2339 "glGetFramebufferAttachmentParameterivEXT(pname)");
2340 return;
2341 }
2342 }
2343
2344
2345 void GLAPIENTRY
2346 _mesa_GenerateMipmapEXT(GLenum target)
2347 {
2348 struct gl_texture_object *texObj;
2349 GET_CURRENT_CONTEXT(ctx);
2350
2351 ASSERT_OUTSIDE_BEGIN_END(ctx);
2352 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2353
2354 switch (target) {
2355 case GL_TEXTURE_1D:
2356 case GL_TEXTURE_2D:
2357 case GL_TEXTURE_3D:
2358 case GL_TEXTURE_CUBE_MAP:
2359 /* OK, legal value */
2360 break;
2361 default:
2362 /* XXX need to implement GL_TEXTURE_1D_ARRAY and GL_TEXTURE_2D_ARRAY */
2363 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
2364 return;
2365 }
2366
2367 texObj = _mesa_get_current_tex_object(ctx, target);
2368
2369 if (texObj->BaseLevel >= texObj->MaxLevel) {
2370 /* nothing to do */
2371 return;
2372 }
2373
2374 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2375 !_mesa_cube_complete(texObj)) {
2376 _mesa_error(ctx, GL_INVALID_OPERATION,
2377 "glGenerateMipmap(incomplete cube map)");
2378 return;
2379 }
2380
2381 _mesa_lock_texture(ctx, texObj);
2382 if (target == GL_TEXTURE_CUBE_MAP) {
2383 GLuint face;
2384 for (face = 0; face < 6; face++)
2385 ctx->Driver.GenerateMipmap(ctx,
2386 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2387 texObj);
2388 }
2389 else {
2390 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2391 }
2392 _mesa_unlock_texture(ctx, texObj);
2393 }
2394
2395
2396 #if FEATURE_EXT_framebuffer_blit
2397
2398 static const struct gl_renderbuffer_attachment *
2399 find_attachment(const struct gl_framebuffer *fb,
2400 const struct gl_renderbuffer *rb)
2401 {
2402 GLuint i;
2403 for (i = 0; i < Elements(fb->Attachment); i++) {
2404 if (fb->Attachment[i].Renderbuffer == rb)
2405 return &fb->Attachment[i];
2406 }
2407 return NULL;
2408 }
2409
2410
2411
2412 /**
2413 * Blit rectangular region, optionally from one framebuffer to another.
2414 *
2415 * Note, if the src buffer is multisampled and the dest is not, this is
2416 * when the samples must be resolved to a single color.
2417 */
2418 void GLAPIENTRY
2419 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2420 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2421 GLbitfield mask, GLenum filter)
2422 {
2423 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2424 GL_DEPTH_BUFFER_BIT |
2425 GL_STENCIL_BUFFER_BIT);
2426 const struct gl_framebuffer *readFb, *drawFb;
2427 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2428 GET_CURRENT_CONTEXT(ctx);
2429
2430 ASSERT_OUTSIDE_BEGIN_END(ctx);
2431 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2432
2433 if (MESA_VERBOSE & VERBOSE_API)
2434 _mesa_debug(ctx,
2435 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2436 srcX0, srcY0, srcX1, srcY1,
2437 dstX0, dstY0, dstX1, dstY1,
2438 mask, _mesa_lookup_enum_by_nr(filter));
2439
2440 if (ctx->NewState) {
2441 _mesa_update_state(ctx);
2442 }
2443
2444 readFb = ctx->ReadBuffer;
2445 drawFb = ctx->DrawBuffer;
2446
2447 if (!readFb || !drawFb) {
2448 /* This will normally never happen but someday we may want to
2449 * support MakeCurrent() with no drawables.
2450 */
2451 return;
2452 }
2453
2454 /* check for complete framebuffers */
2455 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2456 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2457 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2458 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2459 return;
2460 }
2461
2462 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2463 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2464 return;
2465 }
2466
2467 if (mask & ~legalMaskBits) {
2468 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2469 return;
2470 }
2471
2472 /* depth/stencil must be blitted with nearest filtering */
2473 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2474 && filter != GL_NEAREST) {
2475 _mesa_error(ctx, GL_INVALID_OPERATION,
2476 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2477 return;
2478 }
2479
2480 /* get color read/draw renderbuffers */
2481 if (mask & GL_COLOR_BUFFER_BIT) {
2482 colorReadRb = readFb->_ColorReadBuffer;
2483 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2484
2485 /* From the EXT_framebuffer_object spec:
2486 *
2487 * "If a buffer is specified in <mask> and does not exist in both
2488 * the read and draw framebuffers, the corresponding bit is silently
2489 * ignored."
2490 */
2491 if ((colorReadRb == NULL) || (colorDrawRb == NULL)) {
2492 colorReadRb = colorDrawRb = NULL;
2493 mask &= ~GL_COLOR_BUFFER_BIT;
2494 }
2495 }
2496 else {
2497 colorReadRb = colorDrawRb = NULL;
2498 }
2499
2500 if (mask & GL_STENCIL_BUFFER_BIT) {
2501 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2502 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2503
2504 /* From the EXT_framebuffer_object spec:
2505 *
2506 * "If a buffer is specified in <mask> and does not exist in both
2507 * the read and draw framebuffers, the corresponding bit is silently
2508 * ignored."
2509 */
2510 if ((readRb == NULL) || (drawRb == NULL)) {
2511 readRb = drawRb = NULL;
2512 mask &= ~GL_STENCIL_BUFFER_BIT;
2513 }
2514 else if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2515 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2516 _mesa_error(ctx, GL_INVALID_OPERATION,
2517 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2518 return;
2519 }
2520 }
2521
2522 if (mask & GL_DEPTH_BUFFER_BIT) {
2523 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2524 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2525
2526 /* From the EXT_framebuffer_object spec:
2527 *
2528 * "If a buffer is specified in <mask> and does not exist in both
2529 * the read and draw framebuffers, the corresponding bit is silently
2530 * ignored."
2531 */
2532 if ((readRb == NULL) || (drawRb == NULL)) {
2533 readRb = drawRb = NULL;
2534 mask &= ~GL_DEPTH_BUFFER_BIT;
2535 }
2536 else if (_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2537 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2538 _mesa_error(ctx, GL_INVALID_OPERATION,
2539 "glBlitFramebufferEXT(depth buffer size mismatch)");
2540 return;
2541 }
2542 }
2543
2544 if (readFb->Visual.samples > 0 &&
2545 drawFb->Visual.samples > 0 &&
2546 readFb->Visual.samples != drawFb->Visual.samples) {
2547 _mesa_error(ctx, GL_INVALID_OPERATION,
2548 "glBlitFramebufferEXT(mismatched samples");
2549 return;
2550 }
2551
2552 /* extra checks for multisample copies... */
2553 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2554 /* src and dest region sizes must be the same */
2555 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2556 srcY1 - srcY0 != dstY1 - dstY0) {
2557 _mesa_error(ctx, GL_INVALID_OPERATION,
2558 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2559 return;
2560 }
2561
2562 /* color formats must match */
2563 if (colorReadRb &&
2564 colorDrawRb &&
2565 colorReadRb->Format != colorDrawRb->Format) {
2566 _mesa_error(ctx, GL_INVALID_OPERATION,
2567 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2568 return;
2569 }
2570 }
2571
2572 if (!ctx->Extensions.EXT_framebuffer_blit) {
2573 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2574 return;
2575 }
2576
2577 /* Debug code */
2578 if (DEBUG_BLIT) {
2579 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2580 " 0x%x, 0x%x)\n",
2581 srcX0, srcY0, srcX1, srcY1,
2582 dstX0, dstY0, dstX1, dstY1,
2583 mask, filter);
2584 if (colorReadRb) {
2585 const struct gl_renderbuffer_attachment *att;
2586
2587 att = find_attachment(readFb, colorReadRb);
2588 printf(" Src FBO %u RB %u (%dx%d) ",
2589 readFb->Name, colorReadRb->Name,
2590 colorReadRb->Width, colorReadRb->Height);
2591 if (att && att->Texture) {
2592 printf("Tex %u tgt 0x%x level %u face %u",
2593 att->Texture->Name,
2594 att->Texture->Target,
2595 att->TextureLevel,
2596 att->CubeMapFace);
2597 }
2598 printf("\n");
2599
2600 att = find_attachment(drawFb, colorDrawRb);
2601 printf(" Dst FBO %u RB %u (%dx%d) ",
2602 drawFb->Name, colorDrawRb->Name,
2603 colorDrawRb->Width, colorDrawRb->Height);
2604 if (att && att->Texture) {
2605 printf("Tex %u tgt 0x%x level %u face %u",
2606 att->Texture->Name,
2607 att->Texture->Target,
2608 att->TextureLevel,
2609 att->CubeMapFace);
2610 }
2611 printf("\n");
2612 }
2613 }
2614
2615 if (!mask) {
2616 return;
2617 }
2618
2619 ASSERT(ctx->Driver.BlitFramebuffer);
2620 ctx->Driver.BlitFramebuffer(ctx,
2621 srcX0, srcY0, srcX1, srcY1,
2622 dstX0, dstY0, dstX1, dstY1,
2623 mask, filter);
2624 }
2625 #endif /* FEATURE_EXT_framebuffer_blit */
2626
2627 #if FEATURE_ARB_geometry_shader4
2628 void GLAPIENTRY
2629 _mesa_FramebufferTextureARB(GLenum target, GLenum attachment,
2630 GLuint texture, GLint level)
2631 {
2632 GET_CURRENT_CONTEXT(ctx);
2633 _mesa_error(ctx, GL_INVALID_OPERATION,
2634 "glFramebufferTextureARB "
2635 "not implemented!");
2636 }
2637
2638 void GLAPIENTRY
2639 _mesa_FramebufferTextureFaceARB(GLenum target, GLenum attachment,
2640 GLuint texture, GLint level, GLenum face)
2641 {
2642 GET_CURRENT_CONTEXT(ctx);
2643 _mesa_error(ctx, GL_INVALID_OPERATION,
2644 "glFramebufferTextureFaceARB "
2645 "not implemented!");
2646 }
2647 #endif /* FEATURE_ARB_geometry_shader4 */