Allow RBUG to start blocked
[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) {
1988 GLboolean error;
1989
1990 switch (textarget) {
1991 case GL_TEXTURE_1D:
1992 error = GL_FALSE;
1993 break;
1994 case GL_TEXTURE_1D_ARRAY:
1995 error = !ctx->Extensions.EXT_texture_array;
1996 break;
1997 default:
1998 error = GL_TRUE;
1999 }
2000
2001 if (error) {
2002 _mesa_error(ctx, GL_INVALID_OPERATION,
2003 "glFramebufferTexture1DEXT(textarget=%s)",
2004 _mesa_lookup_enum_by_nr(textarget));
2005 return;
2006 }
2007 }
2008
2009 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2010 level, 0);
2011 }
2012
2013
2014 void GLAPIENTRY
2015 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
2016 GLenum textarget, GLuint texture, GLint level)
2017 {
2018 GET_CURRENT_CONTEXT(ctx);
2019
2020 if (texture != 0) {
2021 GLboolean error;
2022
2023 switch (textarget) {
2024 case GL_TEXTURE_2D:
2025 error = GL_FALSE;
2026 break;
2027 case GL_TEXTURE_RECTANGLE:
2028 error = !ctx->Extensions.NV_texture_rectangle;
2029 break;
2030 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2031 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2032 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2033 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2034 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2035 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2036 error = !ctx->Extensions.ARB_texture_cube_map;
2037 break;
2038 case GL_TEXTURE_2D_ARRAY:
2039 error = !ctx->Extensions.EXT_texture_array;
2040 break;
2041 default:
2042 error = GL_FALSE;
2043 }
2044
2045 if (error) {
2046 _mesa_error(ctx, GL_INVALID_OPERATION,
2047 "glFramebufferTexture2DEXT(textarget=%s)",
2048 _mesa_lookup_enum_by_nr(textarget));
2049 return;
2050 }
2051 }
2052
2053 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2054 level, 0);
2055 }
2056
2057
2058 void GLAPIENTRY
2059 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
2060 GLenum textarget, GLuint texture,
2061 GLint level, GLint zoffset)
2062 {
2063 GET_CURRENT_CONTEXT(ctx);
2064
2065 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2066 _mesa_error(ctx, GL_INVALID_OPERATION,
2067 "glFramebufferTexture3DEXT(textarget)");
2068 return;
2069 }
2070
2071 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2072 level, zoffset);
2073 }
2074
2075
2076 void GLAPIENTRY
2077 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
2078 GLuint texture, GLint level, GLint layer)
2079 {
2080 GET_CURRENT_CONTEXT(ctx);
2081
2082 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2083 level, layer);
2084 }
2085
2086
2087 void GLAPIENTRY
2088 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
2089 GLenum renderbufferTarget,
2090 GLuint renderbuffer)
2091 {
2092 struct gl_renderbuffer_attachment *att;
2093 struct gl_framebuffer *fb;
2094 struct gl_renderbuffer *rb;
2095 GET_CURRENT_CONTEXT(ctx);
2096
2097 ASSERT_OUTSIDE_BEGIN_END(ctx);
2098
2099 fb = get_framebuffer_target(ctx, target);
2100 if (!fb) {
2101 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2102 return;
2103 }
2104
2105 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2106 _mesa_error(ctx, GL_INVALID_ENUM,
2107 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2108 return;
2109 }
2110
2111 if (is_winsys_fbo(fb)) {
2112 /* Can't attach new renderbuffers to a window system framebuffer */
2113 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2114 return;
2115 }
2116
2117 att = _mesa_get_attachment(ctx, fb, attachment);
2118 if (att == NULL) {
2119 _mesa_error(ctx, GL_INVALID_ENUM,
2120 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2121 _mesa_lookup_enum_by_nr(attachment));
2122 return;
2123 }
2124
2125 if (renderbuffer) {
2126 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2127 if (!rb) {
2128 _mesa_error(ctx, GL_INVALID_OPERATION,
2129 "glFramebufferRenderbufferEXT(non-existant"
2130 " renderbuffer %u)", renderbuffer);
2131 return;
2132 }
2133 else if (rb == &DummyRenderbuffer) {
2134 /* This is what NVIDIA does */
2135 _mesa_error(ctx, GL_INVALID_VALUE,
2136 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2137 renderbuffer);
2138 return;
2139 }
2140 }
2141 else {
2142 /* remove renderbuffer attachment */
2143 rb = NULL;
2144 }
2145
2146 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2147 rb && rb->Format != MESA_FORMAT_NONE) {
2148 /* make sure the renderbuffer is a depth/stencil format */
2149 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2150 if (baseFormat != GL_DEPTH_STENCIL) {
2151 _mesa_error(ctx, GL_INVALID_OPERATION,
2152 "glFramebufferRenderbufferEXT(renderbuffer"
2153 " is not DEPTH_STENCIL format)");
2154 return;
2155 }
2156 }
2157
2158
2159 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2160
2161 assert(ctx->Driver.FramebufferRenderbuffer);
2162 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2163
2164 /* Some subsequent GL commands may depend on the framebuffer's visual
2165 * after the binding is updated. Update visual info now.
2166 */
2167 _mesa_update_framebuffer_visual(ctx, fb);
2168 }
2169
2170
2171 void GLAPIENTRY
2172 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
2173 GLenum pname, GLint *params)
2174 {
2175 const struct gl_renderbuffer_attachment *att;
2176 struct gl_framebuffer *buffer;
2177 GLenum err;
2178 GET_CURRENT_CONTEXT(ctx);
2179
2180 ASSERT_OUTSIDE_BEGIN_END(ctx);
2181
2182 /* The error differs in GL andd GLES. */
2183 err = ctx->API == API_OPENGL ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2184
2185 buffer = get_framebuffer_target(ctx, target);
2186 if (!buffer) {
2187 _mesa_error(ctx, GL_INVALID_ENUM,
2188 "glGetFramebufferAttachmentParameterivEXT(target)");
2189 return;
2190 }
2191
2192 if (is_winsys_fbo(buffer)) {
2193 /* the default / window-system FBO */
2194 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2195 }
2196 else {
2197 /* user-created framebuffer FBO */
2198 att = _mesa_get_attachment(ctx, buffer, attachment);
2199 }
2200
2201 if (att == NULL) {
2202 _mesa_error(ctx, GL_INVALID_ENUM,
2203 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2204 return;
2205 }
2206
2207 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2208 /* the depth and stencil attachments must point to the same buffer */
2209 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2210 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2211 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2212 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2213 _mesa_error(ctx, GL_INVALID_OPERATION,
2214 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2215 " attachments differ)");
2216 return;
2217 }
2218 }
2219
2220 /* No need to flush here */
2221
2222 switch (pname) {
2223 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2224 *params = is_winsys_fbo(buffer) ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2225 return;
2226 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2227 if (att->Type == GL_RENDERBUFFER_EXT) {
2228 *params = att->Renderbuffer->Name;
2229 }
2230 else if (att->Type == GL_TEXTURE) {
2231 *params = att->Texture->Name;
2232 }
2233 else {
2234 assert(att->Type == GL_NONE);
2235 if (ctx->API == API_OPENGL) {
2236 *params = 0;
2237 } else {
2238 _mesa_error(ctx, GL_INVALID_ENUM,
2239 "glGetFramebufferAttachmentParameterivEXT(pname)");
2240 }
2241 }
2242 return;
2243 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2244 if (att->Type == GL_TEXTURE) {
2245 *params = att->TextureLevel;
2246 }
2247 else if (att->Type == GL_NONE) {
2248 _mesa_error(ctx, err,
2249 "glGetFramebufferAttachmentParameterivEXT(pname)");
2250 }
2251 else {
2252 _mesa_error(ctx, GL_INVALID_ENUM,
2253 "glGetFramebufferAttachmentParameterivEXT(pname)");
2254 }
2255 return;
2256 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2257 if (att->Type == GL_TEXTURE) {
2258 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2259 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2260 }
2261 else {
2262 *params = 0;
2263 }
2264 }
2265 else if (att->Type == GL_NONE) {
2266 _mesa_error(ctx, err,
2267 "glGetFramebufferAttachmentParameterivEXT(pname)");
2268 }
2269 else {
2270 _mesa_error(ctx, GL_INVALID_ENUM,
2271 "glGetFramebufferAttachmentParameterivEXT(pname)");
2272 }
2273 return;
2274 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2275 if (att->Type == GL_TEXTURE) {
2276 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2277 *params = att->Zoffset;
2278 }
2279 else {
2280 *params = 0;
2281 }
2282 }
2283 else if (att->Type == GL_NONE) {
2284 _mesa_error(ctx, err,
2285 "glGetFramebufferAttachmentParameterivEXT(pname)");
2286 }
2287 else {
2288 _mesa_error(ctx, GL_INVALID_ENUM,
2289 "glGetFramebufferAttachmentParameterivEXT(pname)");
2290 }
2291 return;
2292 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2293 if (!ctx->Extensions.ARB_framebuffer_object) {
2294 _mesa_error(ctx, GL_INVALID_ENUM,
2295 "glGetFramebufferAttachmentParameterivEXT(pname)");
2296 }
2297 else if (att->Type == GL_NONE) {
2298 _mesa_error(ctx, err,
2299 "glGetFramebufferAttachmentParameterivEXT(pname)");
2300 }
2301 else {
2302 if (ctx->Extensions.EXT_framebuffer_sRGB && ctx->Const.sRGBCapable) {
2303 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2304 }
2305 else {
2306 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2307 * if the sRGB conversion is unsupported. */
2308 *params = GL_LINEAR;
2309 }
2310 }
2311 return;
2312 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2313 if (!ctx->Extensions.ARB_framebuffer_object) {
2314 _mesa_error(ctx, GL_INVALID_ENUM,
2315 "glGetFramebufferAttachmentParameterivEXT(pname)");
2316 return;
2317 }
2318 else if (att->Type == GL_NONE) {
2319 _mesa_error(ctx, err,
2320 "glGetFramebufferAttachmentParameterivEXT(pname)");
2321 }
2322 else {
2323 gl_format format = att->Renderbuffer->Format;
2324 if (format == MESA_FORMAT_S8) {
2325 /* special cases */
2326 *params = GL_INDEX;
2327 }
2328 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2329 /* depends on the attachment parameter */
2330 if (attachment == GL_STENCIL_ATTACHMENT) {
2331 *params = GL_INDEX;
2332 }
2333 else {
2334 *params = GL_FLOAT;
2335 }
2336 }
2337 else {
2338 *params = _mesa_get_format_datatype(format);
2339 }
2340 }
2341 return;
2342 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2343 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2344 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2345 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2346 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2347 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2348 if (!ctx->Extensions.ARB_framebuffer_object) {
2349 _mesa_error(ctx, GL_INVALID_ENUM,
2350 "glGetFramebufferAttachmentParameterivEXT(pname)");
2351 }
2352 else if (att->Type == GL_NONE) {
2353 _mesa_error(ctx, err,
2354 "glGetFramebufferAttachmentParameterivEXT(pname)");
2355 }
2356 else if (att->Texture) {
2357 const struct gl_texture_image *texImage =
2358 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2359 att->TextureLevel);
2360 if (texImage) {
2361 *params = get_component_bits(pname, texImage->_BaseFormat,
2362 texImage->TexFormat);
2363 }
2364 else {
2365 *params = 0;
2366 }
2367 }
2368 else if (att->Renderbuffer) {
2369 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2370 att->Renderbuffer->Format);
2371 }
2372 else {
2373 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2374 " invalid FBO attachment structure");
2375 }
2376 return;
2377 default:
2378 _mesa_error(ctx, GL_INVALID_ENUM,
2379 "glGetFramebufferAttachmentParameterivEXT(pname)");
2380 return;
2381 }
2382 }
2383
2384
2385 void GLAPIENTRY
2386 _mesa_GenerateMipmapEXT(GLenum target)
2387 {
2388 struct gl_texture_object *texObj;
2389 GLboolean error;
2390
2391 GET_CURRENT_CONTEXT(ctx);
2392
2393 ASSERT_OUTSIDE_BEGIN_END(ctx);
2394 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2395
2396 switch (target) {
2397 case GL_TEXTURE_1D:
2398 case GL_TEXTURE_2D:
2399 case GL_TEXTURE_3D:
2400 error = GL_FALSE;
2401 break;
2402 case GL_TEXTURE_CUBE_MAP:
2403 error = !ctx->Extensions.ARB_texture_cube_map;
2404 break;
2405 case GL_TEXTURE_1D_ARRAY:
2406 case GL_TEXTURE_2D_ARRAY:
2407 error = !ctx->Extensions.EXT_texture_array;
2408 break;
2409 default:
2410 error = GL_TRUE;
2411 }
2412
2413 if (error) {
2414 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2415 _mesa_lookup_enum_by_nr(target));
2416 return;
2417 }
2418
2419 texObj = _mesa_get_current_tex_object(ctx, target);
2420
2421 if (texObj->BaseLevel >= texObj->MaxLevel) {
2422 /* nothing to do */
2423 return;
2424 }
2425
2426 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2427 !_mesa_cube_complete(texObj)) {
2428 _mesa_error(ctx, GL_INVALID_OPERATION,
2429 "glGenerateMipmap(incomplete cube map)");
2430 return;
2431 }
2432
2433 _mesa_lock_texture(ctx, texObj);
2434 if (target == GL_TEXTURE_CUBE_MAP) {
2435 GLuint face;
2436 for (face = 0; face < 6; face++)
2437 ctx->Driver.GenerateMipmap(ctx,
2438 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2439 texObj);
2440 }
2441 else {
2442 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2443 }
2444 _mesa_unlock_texture(ctx, texObj);
2445 }
2446
2447
2448 #if FEATURE_EXT_framebuffer_blit
2449
2450 static const struct gl_renderbuffer_attachment *
2451 find_attachment(const struct gl_framebuffer *fb,
2452 const struct gl_renderbuffer *rb)
2453 {
2454 GLuint i;
2455 for (i = 0; i < Elements(fb->Attachment); i++) {
2456 if (fb->Attachment[i].Renderbuffer == rb)
2457 return &fb->Attachment[i];
2458 }
2459 return NULL;
2460 }
2461
2462
2463
2464 /**
2465 * Blit rectangular region, optionally from one framebuffer to another.
2466 *
2467 * Note, if the src buffer is multisampled and the dest is not, this is
2468 * when the samples must be resolved to a single color.
2469 */
2470 void GLAPIENTRY
2471 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2472 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2473 GLbitfield mask, GLenum filter)
2474 {
2475 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2476 GL_DEPTH_BUFFER_BIT |
2477 GL_STENCIL_BUFFER_BIT);
2478 const struct gl_framebuffer *readFb, *drawFb;
2479 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2480 GET_CURRENT_CONTEXT(ctx);
2481
2482 ASSERT_OUTSIDE_BEGIN_END(ctx);
2483 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2484
2485 if (MESA_VERBOSE & VERBOSE_API)
2486 _mesa_debug(ctx,
2487 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2488 srcX0, srcY0, srcX1, srcY1,
2489 dstX0, dstY0, dstX1, dstY1,
2490 mask, _mesa_lookup_enum_by_nr(filter));
2491
2492 if (ctx->NewState) {
2493 _mesa_update_state(ctx);
2494 }
2495
2496 readFb = ctx->ReadBuffer;
2497 drawFb = ctx->DrawBuffer;
2498
2499 if (!readFb || !drawFb) {
2500 /* This will normally never happen but someday we may want to
2501 * support MakeCurrent() with no drawables.
2502 */
2503 return;
2504 }
2505
2506 /* check for complete framebuffers */
2507 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2508 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2509 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2510 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2511 return;
2512 }
2513
2514 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2515 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2516 return;
2517 }
2518
2519 if (mask & ~legalMaskBits) {
2520 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2521 return;
2522 }
2523
2524 /* depth/stencil must be blitted with nearest filtering */
2525 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2526 && filter != GL_NEAREST) {
2527 _mesa_error(ctx, GL_INVALID_OPERATION,
2528 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2529 return;
2530 }
2531
2532 /* get color read/draw renderbuffers */
2533 if (mask & GL_COLOR_BUFFER_BIT) {
2534 colorReadRb = readFb->_ColorReadBuffer;
2535 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2536
2537 /* From the EXT_framebuffer_object spec:
2538 *
2539 * "If a buffer is specified in <mask> and does not exist in both
2540 * the read and draw framebuffers, the corresponding bit is silently
2541 * ignored."
2542 */
2543 if ((colorReadRb == NULL) || (colorDrawRb == NULL)) {
2544 colorReadRb = colorDrawRb = NULL;
2545 mask &= ~GL_COLOR_BUFFER_BIT;
2546 }
2547 }
2548 else {
2549 colorReadRb = colorDrawRb = NULL;
2550 }
2551
2552 if (mask & GL_STENCIL_BUFFER_BIT) {
2553 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2554 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2555
2556 /* From the EXT_framebuffer_object spec:
2557 *
2558 * "If a buffer is specified in <mask> and does not exist in both
2559 * the read and draw framebuffers, the corresponding bit is silently
2560 * ignored."
2561 */
2562 if ((readRb == NULL) || (drawRb == NULL)) {
2563 readRb = drawRb = NULL;
2564 mask &= ~GL_STENCIL_BUFFER_BIT;
2565 }
2566 else if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2567 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2568 _mesa_error(ctx, GL_INVALID_OPERATION,
2569 "glBlitFramebufferEXT(stencil buffer size mismatch)");
2570 return;
2571 }
2572 }
2573
2574 if (mask & GL_DEPTH_BUFFER_BIT) {
2575 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2576 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2577
2578 /* From the EXT_framebuffer_object spec:
2579 *
2580 * "If a buffer is specified in <mask> and does not exist in both
2581 * the read and draw framebuffers, the corresponding bit is silently
2582 * ignored."
2583 */
2584 if ((readRb == NULL) || (drawRb == NULL)) {
2585 readRb = drawRb = NULL;
2586 mask &= ~GL_DEPTH_BUFFER_BIT;
2587 }
2588 else if (_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2589 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2590 _mesa_error(ctx, GL_INVALID_OPERATION,
2591 "glBlitFramebufferEXT(depth buffer size mismatch)");
2592 return;
2593 }
2594 }
2595
2596 if (readFb->Visual.samples > 0 &&
2597 drawFb->Visual.samples > 0 &&
2598 readFb->Visual.samples != drawFb->Visual.samples) {
2599 _mesa_error(ctx, GL_INVALID_OPERATION,
2600 "glBlitFramebufferEXT(mismatched samples");
2601 return;
2602 }
2603
2604 /* extra checks for multisample copies... */
2605 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2606 /* src and dest region sizes must be the same */
2607 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2608 srcY1 - srcY0 != dstY1 - dstY0) {
2609 _mesa_error(ctx, GL_INVALID_OPERATION,
2610 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2611 return;
2612 }
2613
2614 /* color formats must match */
2615 if (colorReadRb &&
2616 colorDrawRb &&
2617 colorReadRb->Format != colorDrawRb->Format) {
2618 _mesa_error(ctx, GL_INVALID_OPERATION,
2619 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2620 return;
2621 }
2622 }
2623
2624 if (!ctx->Extensions.EXT_framebuffer_blit) {
2625 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2626 return;
2627 }
2628
2629 /* Debug code */
2630 if (DEBUG_BLIT) {
2631 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2632 " 0x%x, 0x%x)\n",
2633 srcX0, srcY0, srcX1, srcY1,
2634 dstX0, dstY0, dstX1, dstY1,
2635 mask, filter);
2636 if (colorReadRb) {
2637 const struct gl_renderbuffer_attachment *att;
2638
2639 att = find_attachment(readFb, colorReadRb);
2640 printf(" Src FBO %u RB %u (%dx%d) ",
2641 readFb->Name, colorReadRb->Name,
2642 colorReadRb->Width, colorReadRb->Height);
2643 if (att && att->Texture) {
2644 printf("Tex %u tgt 0x%x level %u face %u",
2645 att->Texture->Name,
2646 att->Texture->Target,
2647 att->TextureLevel,
2648 att->CubeMapFace);
2649 }
2650 printf("\n");
2651
2652 att = find_attachment(drawFb, colorDrawRb);
2653 printf(" Dst FBO %u RB %u (%dx%d) ",
2654 drawFb->Name, colorDrawRb->Name,
2655 colorDrawRb->Width, colorDrawRb->Height);
2656 if (att && att->Texture) {
2657 printf("Tex %u tgt 0x%x level %u face %u",
2658 att->Texture->Name,
2659 att->Texture->Target,
2660 att->TextureLevel,
2661 att->CubeMapFace);
2662 }
2663 printf("\n");
2664 }
2665 }
2666
2667 if (!mask) {
2668 return;
2669 }
2670
2671 ASSERT(ctx->Driver.BlitFramebuffer);
2672 ctx->Driver.BlitFramebuffer(ctx,
2673 srcX0, srcY0, srcX1, srcY1,
2674 dstX0, dstY0, dstX1, dstY1,
2675 mask, filter);
2676 }
2677 #endif /* FEATURE_EXT_framebuffer_blit */
2678
2679 #if FEATURE_ARB_geometry_shader4
2680 void GLAPIENTRY
2681 _mesa_FramebufferTextureARB(GLenum target, GLenum attachment,
2682 GLuint texture, GLint level)
2683 {
2684 GET_CURRENT_CONTEXT(ctx);
2685 _mesa_error(ctx, GL_INVALID_OPERATION,
2686 "glFramebufferTextureARB "
2687 "not implemented!");
2688 }
2689
2690 void GLAPIENTRY
2691 _mesa_FramebufferTextureFaceARB(GLenum target, GLenum attachment,
2692 GLuint texture, GLint level, GLenum face)
2693 {
2694 GET_CURRENT_CONTEXT(ctx);
2695 _mesa_error(ctx, GL_INVALID_OPERATION,
2696 "glFramebufferTextureFaceARB "
2697 "not implemented!");
2698 }
2699 #endif /* FEATURE_ARB_geometry_shader4 */