fbobject: add additional fbo completeness checks for GLES
[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 #include <stdbool.h>
35
36 #include "buffers.h"
37 #include "context.h"
38 #include "enums.h"
39 #include "fbobject.h"
40 #include "formats.h"
41 #include "framebuffer.h"
42 #include "glformats.h"
43 #include "hash.h"
44 #include "macros.h"
45 #include "mfeatures.h"
46 #include "mtypes.h"
47 #include "renderbuffer.h"
48 #include "state.h"
49 #include "teximage.h"
50 #include "texobj.h"
51
52
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
54 #define DEBUG_BLIT 0
55
56
57 /**
58 * Notes:
59 *
60 * None of the GL_EXT_framebuffer_object functions are compiled into
61 * display lists.
62 */
63
64
65
66 /*
67 * When glGenRender/FramebuffersEXT() is called we insert pointers to
68 * these placeholder objects into the hash table.
69 * Later, when the object ID is first bound, we replace the placeholder
70 * with the real frame/renderbuffer.
71 */
72 static struct gl_framebuffer DummyFramebuffer;
73 static struct gl_renderbuffer DummyRenderbuffer;
74
75 /* We bind this framebuffer when applications pass a NULL
76 * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer;
78
79
80 static void
81 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
82 {
83 /* no op */
84 }
85
86 static void
87 delete_dummy_framebuffer(struct gl_framebuffer *fb)
88 {
89 /* no op */
90 }
91
92
93 void
94 _mesa_init_fbobjects(struct gl_context *ctx)
95 {
96 _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
97 _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
98 _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
99 DummyFramebuffer.Delete = delete_dummy_framebuffer;
100 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
101 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
102 }
103
104 struct gl_framebuffer *
105 _mesa_get_incomplete_framebuffer(void)
106 {
107 return &IncompleteFramebuffer;
108 }
109
110 /**
111 * Helper routine for getting a gl_renderbuffer.
112 */
113 struct gl_renderbuffer *
114 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
115 {
116 struct gl_renderbuffer *rb;
117
118 if (id == 0)
119 return NULL;
120
121 rb = (struct gl_renderbuffer *)
122 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
123 return rb;
124 }
125
126
127 /**
128 * Helper routine for getting a gl_framebuffer.
129 */
130 struct gl_framebuffer *
131 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
132 {
133 struct gl_framebuffer *fb;
134
135 if (id == 0)
136 return NULL;
137
138 fb = (struct gl_framebuffer *)
139 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
140 return fb;
141 }
142
143
144 /**
145 * Mark the given framebuffer as invalid. This will force the
146 * test for framebuffer completeness to be done before the framebuffer
147 * is used.
148 */
149 static void
150 invalidate_framebuffer(struct gl_framebuffer *fb)
151 {
152 fb->_Status = 0; /* "indeterminate" */
153 }
154
155
156 /**
157 * Return the gl_framebuffer object which corresponds to the given
158 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159 * Check support for GL_EXT_framebuffer_blit to determine if certain
160 * targets are legal.
161 * \return gl_framebuffer pointer or NULL if target is illegal
162 */
163 static struct gl_framebuffer *
164 get_framebuffer_target(struct gl_context *ctx, GLenum target)
165 {
166 bool have_fb_blit = _mesa_is_gles3(ctx) ||
167 (ctx->Extensions.EXT_framebuffer_blit && _mesa_is_desktop_gl(ctx));
168 switch (target) {
169 case GL_DRAW_FRAMEBUFFER:
170 return have_fb_blit ? ctx->DrawBuffer : NULL;
171 case GL_READ_FRAMEBUFFER:
172 return have_fb_blit ? ctx->ReadBuffer : NULL;
173 case GL_FRAMEBUFFER_EXT:
174 return ctx->DrawBuffer;
175 default:
176 return NULL;
177 }
178 }
179
180
181 /**
182 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183 * gl_renderbuffer_attachment object.
184 * This function is only used for user-created FB objects, not the
185 * default / window-system FB object.
186 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187 * the depth buffer attachment point.
188 */
189 struct gl_renderbuffer_attachment *
190 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
191 GLenum attachment)
192 {
193 GLuint i;
194
195 assert(_mesa_is_user_fbo(fb));
196
197 switch (attachment) {
198 case GL_COLOR_ATTACHMENT0_EXT:
199 case GL_COLOR_ATTACHMENT1_EXT:
200 case GL_COLOR_ATTACHMENT2_EXT:
201 case GL_COLOR_ATTACHMENT3_EXT:
202 case GL_COLOR_ATTACHMENT4_EXT:
203 case GL_COLOR_ATTACHMENT5_EXT:
204 case GL_COLOR_ATTACHMENT6_EXT:
205 case GL_COLOR_ATTACHMENT7_EXT:
206 case GL_COLOR_ATTACHMENT8_EXT:
207 case GL_COLOR_ATTACHMENT9_EXT:
208 case GL_COLOR_ATTACHMENT10_EXT:
209 case GL_COLOR_ATTACHMENT11_EXT:
210 case GL_COLOR_ATTACHMENT12_EXT:
211 case GL_COLOR_ATTACHMENT13_EXT:
212 case GL_COLOR_ATTACHMENT14_EXT:
213 case GL_COLOR_ATTACHMENT15_EXT:
214 /* Only OpenGL ES 1.x forbids color attachments other than
215 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
216 * hardware is used.
217 */
218 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
219 if (i >= ctx->Const.MaxColorAttachments
220 || (i > 0 && ctx->API == API_OPENGLES)) {
221 return NULL;
222 }
223 return &fb->Attachment[BUFFER_COLOR0 + i];
224 case GL_DEPTH_STENCIL_ATTACHMENT:
225 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
226 return NULL;
227 /* fall-through */
228 case GL_DEPTH_ATTACHMENT_EXT:
229 return &fb->Attachment[BUFFER_DEPTH];
230 case GL_STENCIL_ATTACHMENT_EXT:
231 return &fb->Attachment[BUFFER_STENCIL];
232 default:
233 return NULL;
234 }
235 }
236
237
238 /**
239 * As above, but only used for getting attachments of the default /
240 * window-system framebuffer (not user-created framebuffer objects).
241 */
242 static struct gl_renderbuffer_attachment *
243 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
244 GLenum attachment)
245 {
246 assert(_mesa_is_winsys_fbo(fb));
247
248 if (_mesa_is_gles3(ctx)) {
249 assert(attachment == GL_BACK ||
250 attachment == GL_DEPTH ||
251 attachment == GL_STENCIL);
252 switch (attachment) {
253 case GL_BACK:
254 /* Since there is no stereo rendering in ES 3.0, only return the
255 * LEFT bits.
256 */
257 if (ctx->DrawBuffer->Visual.doubleBufferMode)
258 return &fb->Attachment[BUFFER_BACK_LEFT];
259 return &fb->Attachment[BUFFER_FRONT_LEFT];
260 case GL_DEPTH:
261 return &fb->Attachment[BUFFER_DEPTH];
262 case GL_STENCIL:
263 return &fb->Attachment[BUFFER_STENCIL];
264 }
265 }
266
267 switch (attachment) {
268 case GL_FRONT_LEFT:
269 return &fb->Attachment[BUFFER_FRONT_LEFT];
270 case GL_FRONT_RIGHT:
271 return &fb->Attachment[BUFFER_FRONT_RIGHT];
272 case GL_BACK_LEFT:
273 return &fb->Attachment[BUFFER_BACK_LEFT];
274 case GL_BACK_RIGHT:
275 return &fb->Attachment[BUFFER_BACK_RIGHT];
276 case GL_AUX0:
277 if (fb->Visual.numAuxBuffers == 1) {
278 return &fb->Attachment[BUFFER_AUX0];
279 }
280 return NULL;
281
282 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
283 *
284 * "If the default framebuffer is bound to target, then attachment must
285 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
286 * identifying a color buffer; DEPTH, identifying the depth buffer; or
287 * STENCIL, identifying the stencil buffer."
288 *
289 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
290 * language. However, revision #33 of the ARB_framebuffer_object spec
291 * says:
292 *
293 * "If the default framebuffer is bound to <target>, then <attachment>
294 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
295 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
296 * depth buffer, or the stencil buffer, and <pname> may be
297 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
298 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
299 *
300 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
301 * from glext.h, so shipping apps should not use those values.
302 *
303 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
304 * support queries of the window system FBO.
305 */
306 case GL_DEPTH:
307 return &fb->Attachment[BUFFER_DEPTH];
308 case GL_STENCIL:
309 return &fb->Attachment[BUFFER_STENCIL];
310 default:
311 return NULL;
312 }
313 }
314
315
316
317 /**
318 * Remove any texture or renderbuffer attached to the given attachment
319 * point. Update reference counts, etc.
320 */
321 void
322 _mesa_remove_attachment(struct gl_context *ctx,
323 struct gl_renderbuffer_attachment *att)
324 {
325 if (att->Type == GL_TEXTURE) {
326 ASSERT(att->Texture);
327 if (ctx->Driver.FinishRenderTexture) {
328 /* tell driver that we're done rendering to this texture. */
329 ctx->Driver.FinishRenderTexture(ctx, att);
330 }
331 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
332 ASSERT(!att->Texture);
333 }
334 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
335 ASSERT(!att->Texture);
336 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
337 ASSERT(!att->Renderbuffer);
338 }
339 att->Type = GL_NONE;
340 att->Complete = GL_TRUE;
341 }
342
343
344 /**
345 * Bind a texture object to an attachment point.
346 * The previous binding, if any, will be removed first.
347 */
348 void
349 _mesa_set_texture_attachment(struct gl_context *ctx,
350 struct gl_framebuffer *fb,
351 struct gl_renderbuffer_attachment *att,
352 struct gl_texture_object *texObj,
353 GLenum texTarget, GLuint level, GLuint zoffset)
354 {
355 if (att->Texture == texObj) {
356 /* re-attaching same texture */
357 ASSERT(att->Type == GL_TEXTURE);
358 if (ctx->Driver.FinishRenderTexture)
359 ctx->Driver.FinishRenderTexture(ctx, att);
360 }
361 else {
362 /* new attachment */
363 if (ctx->Driver.FinishRenderTexture && att->Texture)
364 ctx->Driver.FinishRenderTexture(ctx, att);
365 _mesa_remove_attachment(ctx, att);
366 att->Type = GL_TEXTURE;
367 assert(!att->Texture);
368 _mesa_reference_texobj(&att->Texture, texObj);
369 }
370
371 /* always update these fields */
372 att->TextureLevel = level;
373 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
374 att->Zoffset = zoffset;
375 att->Complete = GL_FALSE;
376
377 if (_mesa_get_attachment_teximage(att)) {
378 ctx->Driver.RenderTexture(ctx, fb, att);
379 }
380
381 invalidate_framebuffer(fb);
382 }
383
384
385 /**
386 * Bind a renderbuffer to an attachment point.
387 * The previous binding, if any, will be removed first.
388 */
389 void
390 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
391 struct gl_renderbuffer_attachment *att,
392 struct gl_renderbuffer *rb)
393 {
394 /* XXX check if re-doing same attachment, exit early */
395 _mesa_remove_attachment(ctx, att);
396 att->Type = GL_RENDERBUFFER_EXT;
397 att->Texture = NULL; /* just to be safe */
398 att->Complete = GL_FALSE;
399 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
400 }
401
402
403 /**
404 * Fallback for ctx->Driver.FramebufferRenderbuffer()
405 * Attach a renderbuffer object to a framebuffer object.
406 */
407 void
408 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
409 struct gl_framebuffer *fb,
410 GLenum attachment, struct gl_renderbuffer *rb)
411 {
412 struct gl_renderbuffer_attachment *att;
413
414 _glthread_LOCK_MUTEX(fb->Mutex);
415
416 att = _mesa_get_attachment(ctx, fb, attachment);
417 ASSERT(att);
418 if (rb) {
419 _mesa_set_renderbuffer_attachment(ctx, att, rb);
420 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
421 /* do stencil attachment here (depth already done above) */
422 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
423 assert(att);
424 _mesa_set_renderbuffer_attachment(ctx, att, rb);
425 }
426 rb->AttachedAnytime = GL_TRUE;
427 }
428 else {
429 _mesa_remove_attachment(ctx, att);
430 }
431
432 invalidate_framebuffer(fb);
433
434 _glthread_UNLOCK_MUTEX(fb->Mutex);
435 }
436
437
438 /**
439 * Fallback for ctx->Driver.ValidateFramebuffer()
440 * Check if the renderbuffer's formats are supported by the software
441 * renderer.
442 * Drivers should probably override this.
443 */
444 void
445 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
446 {
447 gl_buffer_index buf;
448 for (buf = 0; buf < BUFFER_COUNT; buf++) {
449 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
450 if (rb) {
451 switch (rb->_BaseFormat) {
452 case GL_ALPHA:
453 case GL_LUMINANCE_ALPHA:
454 case GL_LUMINANCE:
455 case GL_INTENSITY:
456 case GL_RED:
457 case GL_RG:
458 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
459 return;
460
461 default:
462 switch (rb->Format) {
463 /* XXX This list is likely incomplete. */
464 case MESA_FORMAT_RGB9_E5_FLOAT:
465 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
466 return;
467 default:;
468 /* render buffer format is supported by software rendering */
469 }
470 }
471 }
472 }
473 }
474
475
476 /**
477 * For debug only.
478 */
479 static void
480 att_incomplete(const char *msg)
481 {
482 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
483 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
484 }
485 }
486
487
488 /**
489 * For debug only.
490 */
491 static void
492 fbo_incomplete(const char *msg, int index)
493 {
494 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
495 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
496 }
497 }
498
499
500 /**
501 * Is the given base format a legal format for a color renderbuffer?
502 */
503 GLboolean
504 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
505 {
506 switch (baseFormat) {
507 case GL_RGB:
508 case GL_RGBA:
509 return GL_TRUE;
510 case GL_LUMINANCE:
511 case GL_LUMINANCE_ALPHA:
512 case GL_INTENSITY:
513 case GL_ALPHA:
514 return ctx->API == API_OPENGL_COMPAT &&
515 ctx->Extensions.ARB_framebuffer_object;
516 case GL_RED:
517 case GL_RG:
518 return ctx->Extensions.ARB_texture_rg;
519 default:
520 return GL_FALSE;
521 }
522 }
523
524
525 /**
526 * Is the given base format a legal format for a color renderbuffer?
527 */
528 static GLboolean
529 is_format_color_renderable(const struct gl_context *ctx, gl_format format, GLenum internalFormat)
530 {
531 const GLenum baseFormat =
532 _mesa_get_format_base_format(format);
533 GLboolean valid;
534
535 valid = _mesa_is_legal_color_format(ctx, baseFormat);
536 if (!valid || _mesa_is_desktop_gl(ctx)) {
537 return valid;
538 }
539
540 /* Reject additional cases for GLES */
541 switch (internalFormat) {
542 case GL_RGBA8_SNORM:
543 case GL_RGB32F:
544 case GL_RGB32I:
545 case GL_RGB32UI:
546 case GL_RGB16F:
547 case GL_RGB16I:
548 case GL_RGB16UI:
549 case GL_RGB8_SNORM:
550 case GL_RGB8I:
551 case GL_RGB8UI:
552 case GL_SRGB8:
553 case GL_RGB9_E5:
554 case GL_RG8_SNORM:
555 case GL_R8_SNORM:
556 return GL_FALSE;
557 default:
558 break;
559 }
560
561 if (format == MESA_FORMAT_ARGB2101010 && internalFormat != GL_RGB10_A2) {
562 return GL_FALSE;
563 }
564
565 return GL_TRUE;
566 }
567
568
569 /**
570 * Is the given base format a legal format for a depth/stencil renderbuffer?
571 */
572 static GLboolean
573 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
574 {
575 switch (baseFormat) {
576 case GL_DEPTH_COMPONENT:
577 case GL_DEPTH_STENCIL_EXT:
578 return GL_TRUE;
579 default:
580 return GL_FALSE;
581 }
582 }
583
584
585 /**
586 * Test if an attachment point is complete and update its Complete field.
587 * \param format if GL_COLOR, this is a color attachment point,
588 * if GL_DEPTH, this is a depth component attachment point,
589 * if GL_STENCIL, this is a stencil component attachment point.
590 */
591 static void
592 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
593 struct gl_renderbuffer_attachment *att)
594 {
595 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
596
597 /* assume complete */
598 att->Complete = GL_TRUE;
599
600 /* Look for reasons why the attachment might be incomplete */
601 if (att->Type == GL_TEXTURE) {
602 const struct gl_texture_object *texObj = att->Texture;
603 struct gl_texture_image *texImage;
604 GLenum baseFormat;
605
606 if (!texObj) {
607 att_incomplete("no texobj");
608 att->Complete = GL_FALSE;
609 return;
610 }
611
612 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
613 if (!texImage) {
614 att_incomplete("no teximage");
615 att->Complete = GL_FALSE;
616 return;
617 }
618 if (texImage->Width < 1 || texImage->Height < 1) {
619 att_incomplete("teximage width/height=0");
620 printf("texobj = %u\n", texObj->Name);
621 printf("level = %d\n", att->TextureLevel);
622 att->Complete = GL_FALSE;
623 return;
624 }
625 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
626 att_incomplete("bad z offset");
627 att->Complete = GL_FALSE;
628 return;
629 }
630
631 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
632
633 if (format == GL_COLOR) {
634 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
635 att_incomplete("bad format");
636 att->Complete = GL_FALSE;
637 return;
638 }
639 if (_mesa_is_format_compressed(texImage->TexFormat)) {
640 att_incomplete("compressed internalformat");
641 att->Complete = GL_FALSE;
642 return;
643 }
644 }
645 else if (format == GL_DEPTH) {
646 if (baseFormat == GL_DEPTH_COMPONENT) {
647 /* OK */
648 }
649 else if (ctx->Extensions.EXT_packed_depth_stencil &&
650 ctx->Extensions.ARB_depth_texture &&
651 baseFormat == GL_DEPTH_STENCIL_EXT) {
652 /* OK */
653 }
654 else {
655 att->Complete = GL_FALSE;
656 att_incomplete("bad depth format");
657 return;
658 }
659 }
660 else {
661 ASSERT(format == GL_STENCIL);
662 if (ctx->Extensions.EXT_packed_depth_stencil &&
663 ctx->Extensions.ARB_depth_texture &&
664 baseFormat == GL_DEPTH_STENCIL_EXT) {
665 /* OK */
666 }
667 else {
668 /* no such thing as stencil-only textures */
669 att_incomplete("illegal stencil texture");
670 att->Complete = GL_FALSE;
671 return;
672 }
673 }
674 }
675 else if (att->Type == GL_RENDERBUFFER_EXT) {
676 const GLenum baseFormat =
677 _mesa_get_format_base_format(att->Renderbuffer->Format);
678
679 ASSERT(att->Renderbuffer);
680 if (!att->Renderbuffer->InternalFormat ||
681 att->Renderbuffer->Width < 1 ||
682 att->Renderbuffer->Height < 1) {
683 att_incomplete("0x0 renderbuffer");
684 att->Complete = GL_FALSE;
685 return;
686 }
687 if (format == GL_COLOR) {
688 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
689 att_incomplete("bad renderbuffer color format");
690 att->Complete = GL_FALSE;
691 return;
692 }
693 }
694 else if (format == GL_DEPTH) {
695 if (baseFormat == GL_DEPTH_COMPONENT) {
696 /* OK */
697 }
698 else if (ctx->Extensions.EXT_packed_depth_stencil &&
699 baseFormat == GL_DEPTH_STENCIL_EXT) {
700 /* OK */
701 }
702 else {
703 att_incomplete("bad renderbuffer depth format");
704 att->Complete = GL_FALSE;
705 return;
706 }
707 }
708 else {
709 assert(format == GL_STENCIL);
710 if (baseFormat == GL_STENCIL_INDEX) {
711 /* OK */
712 }
713 else if (ctx->Extensions.EXT_packed_depth_stencil &&
714 baseFormat == GL_DEPTH_STENCIL_EXT) {
715 /* OK */
716 }
717 else {
718 att->Complete = GL_FALSE;
719 att_incomplete("bad renderbuffer stencil format");
720 return;
721 }
722 }
723 }
724 else {
725 ASSERT(att->Type == GL_NONE);
726 /* complete */
727 return;
728 }
729 }
730
731
732 /**
733 * Test if the given framebuffer object is complete and update its
734 * Status field with the results.
735 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
736 * driver to make hardware-specific validation/completeness checks.
737 * Also update the framebuffer's Width and Height fields if the
738 * framebuffer is complete.
739 */
740 void
741 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
742 struct gl_framebuffer *fb)
743 {
744 GLuint numImages;
745 GLenum intFormat = GL_NONE; /* color buffers' internal format */
746 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
747 GLint numSamples = -1;
748 GLint i;
749 GLuint j;
750
751 assert(_mesa_is_user_fbo(fb));
752
753 /* we're changing framebuffer fields here */
754 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
755
756 numImages = 0;
757 fb->Width = 0;
758 fb->Height = 0;
759
760 /* Start at -2 to more easily loop over all attachment points.
761 * -2: depth buffer
762 * -1: stencil buffer
763 * >=0: color buffer
764 */
765 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
766 struct gl_renderbuffer_attachment *att;
767 GLenum f;
768 gl_format attFormat;
769
770 /*
771 * XXX for ARB_fbo, only check color buffers that are named by
772 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
773 */
774
775 /* check for attachment completeness
776 */
777 if (i == -2) {
778 att = &fb->Attachment[BUFFER_DEPTH];
779 test_attachment_completeness(ctx, GL_DEPTH, att);
780 if (!att->Complete) {
781 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
782 fbo_incomplete("depth attachment incomplete", -1);
783 return;
784 }
785 }
786 else if (i == -1) {
787 att = &fb->Attachment[BUFFER_STENCIL];
788 test_attachment_completeness(ctx, GL_STENCIL, att);
789 if (!att->Complete) {
790 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
791 fbo_incomplete("stencil attachment incomplete", -1);
792 return;
793 }
794 }
795 else {
796 att = &fb->Attachment[BUFFER_COLOR0 + i];
797 test_attachment_completeness(ctx, GL_COLOR, att);
798 if (!att->Complete) {
799 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
800 fbo_incomplete("color attachment incomplete", i);
801 return;
802 }
803 }
804
805 /* get width, height, format of the renderbuffer/texture
806 */
807 if (att->Type == GL_TEXTURE) {
808 const struct gl_texture_image *texImg =
809 _mesa_get_attachment_teximage(att);
810 minWidth = MIN2(minWidth, texImg->Width);
811 maxWidth = MAX2(maxWidth, texImg->Width);
812 minHeight = MIN2(minHeight, texImg->Height);
813 maxHeight = MAX2(maxHeight, texImg->Height);
814 f = texImg->_BaseFormat;
815 attFormat = texImg->TexFormat;
816 numImages++;
817 if (!is_format_color_renderable(ctx, attFormat, texImg->InternalFormat) &&
818 !is_legal_depth_format(ctx, f)) {
819 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
820 fbo_incomplete("texture attachment incomplete", -1);
821 return;
822 }
823 }
824 else if (att->Type == GL_RENDERBUFFER_EXT) {
825 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
826 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
827 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
828 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
829 f = att->Renderbuffer->InternalFormat;
830 attFormat = att->Renderbuffer->Format;
831 numImages++;
832 }
833 else {
834 assert(att->Type == GL_NONE);
835 continue;
836 }
837
838 if (att->Renderbuffer && numSamples < 0) {
839 /* first buffer */
840 numSamples = att->Renderbuffer->NumSamples;
841 }
842
843 /* check if integer color */
844 fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
845
846 /* Error-check width, height, format, samples
847 */
848 if (numImages == 1) {
849 /* save format, num samples */
850 if (i >= 0) {
851 intFormat = f;
852 }
853 }
854 else {
855 if (!ctx->Extensions.ARB_framebuffer_object) {
856 /* check that width, height, format are same */
857 if (minWidth != maxWidth || minHeight != maxHeight) {
858 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
859 fbo_incomplete("width or height mismatch", -1);
860 return;
861 }
862 /* check that all color buffers are the same format */
863 if (intFormat != GL_NONE && f != intFormat) {
864 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
865 fbo_incomplete("format mismatch", -1);
866 return;
867 }
868 }
869 if (att->Renderbuffer &&
870 att->Renderbuffer->NumSamples != numSamples) {
871 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
872 fbo_incomplete("inconsistant number of samples", i);
873 return;
874 }
875 }
876
877 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
878 */
879 if (att->Type == GL_RENDERBUFFER &&
880 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
881 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
882 fbo_incomplete("unsupported renderbuffer format", i);
883 return;
884 }
885 }
886
887 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
888 /* Check that all DrawBuffers are present */
889 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
890 if (fb->ColorDrawBuffer[j] != GL_NONE) {
891 const struct gl_renderbuffer_attachment *att
892 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
893 assert(att);
894 if (att->Type == GL_NONE) {
895 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
896 fbo_incomplete("missing drawbuffer", j);
897 return;
898 }
899 }
900 }
901
902 /* Check that the ReadBuffer is present */
903 if (fb->ColorReadBuffer != GL_NONE) {
904 const struct gl_renderbuffer_attachment *att
905 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
906 assert(att);
907 if (att->Type == GL_NONE) {
908 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
909 fbo_incomplete("missing readbuffer", -1);
910 return;
911 }
912 }
913 }
914
915 if (numImages == 0) {
916 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
917 fbo_incomplete("no attachments", -1);
918 return;
919 }
920
921 /* Provisionally set status = COMPLETE ... */
922 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
923
924 /* ... but the driver may say the FB is incomplete.
925 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
926 * if anything.
927 */
928 if (ctx->Driver.ValidateFramebuffer) {
929 ctx->Driver.ValidateFramebuffer(ctx, fb);
930 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
931 fbo_incomplete("driver marked FBO as incomplete", -1);
932 }
933 }
934
935 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
936 /*
937 * Note that if ARB_framebuffer_object is supported and the attached
938 * renderbuffers/textures are different sizes, the framebuffer
939 * width/height will be set to the smallest width/height.
940 */
941 fb->Width = minWidth;
942 fb->Height = minHeight;
943
944 /* finally, update the visual info for the framebuffer */
945 _mesa_update_framebuffer_visual(ctx, fb);
946 }
947 }
948
949
950 GLboolean GLAPIENTRY
951 _mesa_IsRenderbuffer(GLuint renderbuffer)
952 {
953 GET_CURRENT_CONTEXT(ctx);
954 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
955 if (renderbuffer) {
956 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
957 if (rb != NULL && rb != &DummyRenderbuffer)
958 return GL_TRUE;
959 }
960 return GL_FALSE;
961 }
962
963
964 void GLAPIENTRY
965 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
966 {
967 struct gl_renderbuffer *newRb;
968 GET_CURRENT_CONTEXT(ctx);
969
970 ASSERT_OUTSIDE_BEGIN_END(ctx);
971
972 if (target != GL_RENDERBUFFER_EXT) {
973 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
974 return;
975 }
976
977 /* No need to flush here since the render buffer binding has no
978 * effect on rendering state.
979 */
980
981 if (renderbuffer) {
982 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
983 if (newRb == &DummyRenderbuffer) {
984 /* ID was reserved, but no real renderbuffer object made yet */
985 newRb = NULL;
986 }
987 else if (!newRb
988 && _mesa_is_desktop_gl(ctx)
989 && ctx->Extensions.ARB_framebuffer_object) {
990 /* All RB IDs must be Gen'd */
991 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
992 return;
993 }
994
995 if (!newRb) {
996 /* create new renderbuffer object */
997 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
998 if (!newRb) {
999 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
1000 return;
1001 }
1002 ASSERT(newRb->AllocStorage);
1003 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1004 newRb->RefCount = 1; /* referenced by hash table */
1005 }
1006 }
1007 else {
1008 newRb = NULL;
1009 }
1010
1011 ASSERT(newRb != &DummyRenderbuffer);
1012
1013 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1014 }
1015
1016
1017 /**
1018 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1019 * the renderbuffer.
1020 * This is used when a renderbuffer object is deleted.
1021 * The spec calls for unbinding.
1022 */
1023 static void
1024 detach_renderbuffer(struct gl_context *ctx,
1025 struct gl_framebuffer *fb,
1026 struct gl_renderbuffer *rb)
1027 {
1028 GLuint i;
1029 for (i = 0; i < BUFFER_COUNT; i++) {
1030 if (fb->Attachment[i].Renderbuffer == rb) {
1031 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
1032 }
1033 }
1034 invalidate_framebuffer(fb);
1035 }
1036
1037
1038 void GLAPIENTRY
1039 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1040 {
1041 GLint i;
1042 GET_CURRENT_CONTEXT(ctx);
1043
1044 ASSERT_OUTSIDE_BEGIN_END(ctx);
1045 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1046
1047 for (i = 0; i < n; i++) {
1048 if (renderbuffers[i] > 0) {
1049 struct gl_renderbuffer *rb;
1050 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1051 if (rb) {
1052 /* check if deleting currently bound renderbuffer object */
1053 if (rb == ctx->CurrentRenderbuffer) {
1054 /* bind default */
1055 ASSERT(rb->RefCount >= 2);
1056 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1057 }
1058
1059 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1060 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1061 }
1062 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1063 && ctx->ReadBuffer != ctx->DrawBuffer) {
1064 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1065 }
1066
1067 /* Remove from hash table immediately, to free the ID.
1068 * But the object will not be freed until it's no longer
1069 * referenced anywhere else.
1070 */
1071 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1072
1073 if (rb != &DummyRenderbuffer) {
1074 /* no longer referenced by hash table */
1075 _mesa_reference_renderbuffer(&rb, NULL);
1076 }
1077 }
1078 }
1079 }
1080 }
1081
1082
1083 void GLAPIENTRY
1084 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1085 {
1086 GET_CURRENT_CONTEXT(ctx);
1087 GLuint first;
1088 GLint i;
1089
1090 ASSERT_OUTSIDE_BEGIN_END(ctx);
1091
1092 if (n < 0) {
1093 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1094 return;
1095 }
1096
1097 if (!renderbuffers)
1098 return;
1099
1100 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1101
1102 for (i = 0; i < n; i++) {
1103 GLuint name = first + i;
1104 renderbuffers[i] = name;
1105 /* insert dummy placeholder into hash table */
1106 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1107 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1108 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1109 }
1110 }
1111
1112
1113 /**
1114 * Given an internal format token for a render buffer, return the
1115 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1116 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1117 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1118 *
1119 * This is similar to _mesa_base_tex_format() but the set of valid
1120 * internal formats is different.
1121 *
1122 * Note that even if a format is determined to be legal here, validation
1123 * of the FBO may fail if the format is not supported by the driver/GPU.
1124 *
1125 * \param internalFormat as passed to glRenderbufferStorage()
1126 * \return the base internal format, or 0 if internalFormat is illegal
1127 */
1128 GLenum
1129 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1130 {
1131 /*
1132 * Notes: some formats such as alpha, luminance, etc. were added
1133 * with GL_ARB_framebuffer_object.
1134 */
1135 switch (internalFormat) {
1136 case GL_ALPHA:
1137 case GL_ALPHA4:
1138 case GL_ALPHA8:
1139 case GL_ALPHA12:
1140 case GL_ALPHA16:
1141 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1142 ? GL_ALPHA : 0;
1143 case GL_LUMINANCE:
1144 case GL_LUMINANCE4:
1145 case GL_LUMINANCE8:
1146 case GL_LUMINANCE12:
1147 case GL_LUMINANCE16:
1148 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1149 ? GL_LUMINANCE : 0;
1150 case GL_LUMINANCE_ALPHA:
1151 case GL_LUMINANCE4_ALPHA4:
1152 case GL_LUMINANCE6_ALPHA2:
1153 case GL_LUMINANCE8_ALPHA8:
1154 case GL_LUMINANCE12_ALPHA4:
1155 case GL_LUMINANCE12_ALPHA12:
1156 case GL_LUMINANCE16_ALPHA16:
1157 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1158 ? GL_LUMINANCE_ALPHA : 0;
1159 case GL_INTENSITY:
1160 case GL_INTENSITY4:
1161 case GL_INTENSITY8:
1162 case GL_INTENSITY12:
1163 case GL_INTENSITY16:
1164 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1165 ? GL_INTENSITY : 0;
1166 case GL_RGB8:
1167 return GL_RGB;
1168 case GL_RGB:
1169 case GL_R3_G3_B2:
1170 case GL_RGB4:
1171 case GL_RGB5:
1172 case GL_RGB10:
1173 case GL_RGB12:
1174 case GL_RGB16:
1175 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1176 case GL_SRGB8_EXT:
1177 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1178 case GL_RGBA4:
1179 case GL_RGB5_A1:
1180 case GL_RGBA8:
1181 return GL_RGBA;
1182 case GL_RGBA:
1183 case GL_RGBA2:
1184 case GL_RGBA12:
1185 case GL_RGBA16:
1186 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1187 case GL_RGB10_A2:
1188 case GL_SRGB8_ALPHA8_EXT:
1189 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1190 case GL_STENCIL_INDEX:
1191 case GL_STENCIL_INDEX1_EXT:
1192 case GL_STENCIL_INDEX4_EXT:
1193 case GL_STENCIL_INDEX16_EXT:
1194 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1195 * OpenGL ES, but Mesa does not currently support them.
1196 */
1197 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1198 case GL_STENCIL_INDEX8_EXT:
1199 return GL_STENCIL_INDEX;
1200 case GL_DEPTH_COMPONENT:
1201 case GL_DEPTH_COMPONENT32:
1202 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1203 case GL_DEPTH_COMPONENT16:
1204 case GL_DEPTH_COMPONENT24:
1205 return GL_DEPTH_COMPONENT;
1206 case GL_DEPTH_STENCIL_EXT:
1207 return _mesa_is_desktop_gl(ctx)
1208 && ctx->Extensions.EXT_packed_depth_stencil
1209 ? GL_DEPTH_STENCIL_EXT : 0;
1210 case GL_DEPTH24_STENCIL8_EXT:
1211 return ctx->Extensions.EXT_packed_depth_stencil
1212 ? GL_DEPTH_STENCIL_EXT : 0;
1213 case GL_DEPTH_COMPONENT32F:
1214 return ctx->Version >= 30
1215 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1216 ? GL_DEPTH_COMPONENT : 0;
1217 case GL_DEPTH32F_STENCIL8:
1218 return ctx->Version >= 30
1219 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1220 ? GL_DEPTH_STENCIL : 0;
1221 case GL_RED:
1222 case GL_R16:
1223 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1224 ? GL_RED : 0;
1225 case GL_R8:
1226 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1227 ? GL_RED : 0;
1228 case GL_RG:
1229 case GL_RG16:
1230 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1231 ? GL_RG : 0;
1232 case GL_RG8:
1233 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1234 ? GL_RG : 0;
1235 /* signed normalized texture formats */
1236 case GL_RED_SNORM:
1237 case GL_R8_SNORM:
1238 case GL_R16_SNORM:
1239 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1240 ? GL_RED : 0;
1241 case GL_RG_SNORM:
1242 case GL_RG8_SNORM:
1243 case GL_RG16_SNORM:
1244 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1245 ? GL_RG : 0;
1246 case GL_RGB_SNORM:
1247 case GL_RGB8_SNORM:
1248 case GL_RGB16_SNORM:
1249 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1250 ? GL_RGB : 0;
1251 case GL_RGBA_SNORM:
1252 case GL_RGBA8_SNORM:
1253 case GL_RGBA16_SNORM:
1254 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1255 ? GL_RGBA : 0;
1256 case GL_ALPHA_SNORM:
1257 case GL_ALPHA8_SNORM:
1258 case GL_ALPHA16_SNORM:
1259 return ctx->API == API_OPENGL_COMPAT &&
1260 ctx->Extensions.EXT_texture_snorm &&
1261 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1262 case GL_R16F:
1263 case GL_R32F:
1264 return ((_mesa_is_desktop_gl(ctx) &&
1265 ctx->Extensions.ARB_texture_rg &&
1266 ctx->Extensions.ARB_texture_float) ||
1267 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1268 ? GL_RED : 0;
1269 case GL_RG16F:
1270 case GL_RG32F:
1271 return ((_mesa_is_desktop_gl(ctx) &&
1272 ctx->Extensions.ARB_texture_rg &&
1273 ctx->Extensions.ARB_texture_float) ||
1274 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1275 ? GL_RG : 0;
1276 case GL_RGB16F:
1277 case GL_RGB32F:
1278 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1279 ? GL_RGB : 0;
1280 case GL_RGBA16F:
1281 case GL_RGBA32F:
1282 return ((_mesa_is_desktop_gl(ctx) &&
1283 ctx->Extensions.ARB_texture_float) ||
1284 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1285 ? GL_RGBA : 0;
1286 case GL_ALPHA16F_ARB:
1287 case GL_ALPHA32F_ARB:
1288 return ctx->API == API_OPENGL_COMPAT &&
1289 ctx->Extensions.ARB_texture_float &&
1290 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1291 case GL_LUMINANCE16F_ARB:
1292 case GL_LUMINANCE32F_ARB:
1293 return ctx->API == API_OPENGL_COMPAT &&
1294 ctx->Extensions.ARB_texture_float &&
1295 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1296 case GL_LUMINANCE_ALPHA16F_ARB:
1297 case GL_LUMINANCE_ALPHA32F_ARB:
1298 return ctx->API == API_OPENGL_COMPAT &&
1299 ctx->Extensions.ARB_texture_float &&
1300 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1301 case GL_INTENSITY16F_ARB:
1302 case GL_INTENSITY32F_ARB:
1303 return ctx->API == API_OPENGL_COMPAT &&
1304 ctx->Extensions.ARB_texture_float &&
1305 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1306 case GL_RGB9_E5:
1307 return (_mesa_is_desktop_gl(ctx)
1308 && ctx->Extensions.EXT_texture_shared_exponent)
1309 ? GL_RGB : 0;
1310 case GL_R11F_G11F_B10F:
1311 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1312 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1313 ? GL_RGB : 0;
1314
1315 case GL_RGBA8UI_EXT:
1316 case GL_RGBA16UI_EXT:
1317 case GL_RGBA32UI_EXT:
1318 case GL_RGBA8I_EXT:
1319 case GL_RGBA16I_EXT:
1320 case GL_RGBA32I_EXT:
1321 return ctx->Version >= 30
1322 || (_mesa_is_desktop_gl(ctx) &&
1323 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1324
1325 case GL_RGB8UI_EXT:
1326 case GL_RGB16UI_EXT:
1327 case GL_RGB32UI_EXT:
1328 case GL_RGB8I_EXT:
1329 case GL_RGB16I_EXT:
1330 case GL_RGB32I_EXT:
1331 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1332 ? GL_RGB : 0;
1333 case GL_R8UI:
1334 case GL_R8I:
1335 case GL_R16UI:
1336 case GL_R16I:
1337 case GL_R32UI:
1338 case GL_R32I:
1339 return ctx->Version >= 30
1340 || (_mesa_is_desktop_gl(ctx) &&
1341 ctx->Extensions.ARB_texture_rg &&
1342 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1343
1344 case GL_RG8UI:
1345 case GL_RG8I:
1346 case GL_RG16UI:
1347 case GL_RG16I:
1348 case GL_RG32UI:
1349 case GL_RG32I:
1350 return ctx->Version >= 30
1351 || (_mesa_is_desktop_gl(ctx) &&
1352 ctx->Extensions.ARB_texture_rg &&
1353 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1354
1355 case GL_INTENSITY8I_EXT:
1356 case GL_INTENSITY8UI_EXT:
1357 case GL_INTENSITY16I_EXT:
1358 case GL_INTENSITY16UI_EXT:
1359 case GL_INTENSITY32I_EXT:
1360 case GL_INTENSITY32UI_EXT:
1361 return ctx->API == API_OPENGL_COMPAT &&
1362 ctx->Extensions.EXT_texture_integer &&
1363 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1364
1365 case GL_LUMINANCE8I_EXT:
1366 case GL_LUMINANCE8UI_EXT:
1367 case GL_LUMINANCE16I_EXT:
1368 case GL_LUMINANCE16UI_EXT:
1369 case GL_LUMINANCE32I_EXT:
1370 case GL_LUMINANCE32UI_EXT:
1371 return ctx->API == API_OPENGL_COMPAT &&
1372 ctx->Extensions.EXT_texture_integer &&
1373 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1374
1375 case GL_LUMINANCE_ALPHA8I_EXT:
1376 case GL_LUMINANCE_ALPHA8UI_EXT:
1377 case GL_LUMINANCE_ALPHA16I_EXT:
1378 case GL_LUMINANCE_ALPHA16UI_EXT:
1379 case GL_LUMINANCE_ALPHA32I_EXT:
1380 case GL_LUMINANCE_ALPHA32UI_EXT:
1381 return ctx->API == API_OPENGL_COMPAT &&
1382 ctx->Extensions.EXT_texture_integer &&
1383 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1384
1385 case GL_ALPHA8I_EXT:
1386 case GL_ALPHA8UI_EXT:
1387 case GL_ALPHA16I_EXT:
1388 case GL_ALPHA16UI_EXT:
1389 case GL_ALPHA32I_EXT:
1390 case GL_ALPHA32UI_EXT:
1391 return ctx->API == API_OPENGL_COMPAT &&
1392 ctx->Extensions.EXT_texture_integer &&
1393 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1394
1395 case GL_RGB10_A2UI:
1396 return (_mesa_is_desktop_gl(ctx) &&
1397 ctx->Extensions.ARB_texture_rgb10_a2ui)
1398 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1399
1400 case GL_RGB565:
1401 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1402 ? GL_RGB : 0;
1403 default:
1404 return 0;
1405 }
1406 }
1407
1408
1409 /**
1410 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1411 */
1412 static void
1413 invalidate_rb(GLuint key, void *data, void *userData)
1414 {
1415 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1416 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1417
1418 /* If this is a user-created FBO */
1419 if (_mesa_is_user_fbo(fb)) {
1420 GLuint i;
1421 for (i = 0; i < BUFFER_COUNT; i++) {
1422 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1423 if (att->Type == GL_RENDERBUFFER &&
1424 att->Renderbuffer == rb) {
1425 /* Mark fb status as indeterminate to force re-validation */
1426 fb->_Status = 0;
1427 return;
1428 }
1429 }
1430 }
1431 }
1432
1433
1434 /** sentinal value, see below */
1435 #define NO_SAMPLES 1000
1436
1437
1438 /**
1439 * Helper function used by _mesa_RenderbufferStorage() and
1440 * _mesa_RenderbufferStorageMultisample().
1441 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1442 */
1443 static void
1444 renderbuffer_storage(GLenum target, GLenum internalFormat,
1445 GLsizei width, GLsizei height, GLsizei samples)
1446 {
1447 const char *func = samples == NO_SAMPLES ?
1448 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1449 struct gl_renderbuffer *rb;
1450 GLenum baseFormat;
1451 GET_CURRENT_CONTEXT(ctx);
1452
1453 ASSERT_OUTSIDE_BEGIN_END(ctx);
1454
1455 if (MESA_VERBOSE & VERBOSE_API) {
1456 if (samples == NO_SAMPLES)
1457 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1458 func,
1459 _mesa_lookup_enum_by_nr(target),
1460 _mesa_lookup_enum_by_nr(internalFormat),
1461 width, height);
1462 else
1463 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1464 func,
1465 _mesa_lookup_enum_by_nr(target),
1466 _mesa_lookup_enum_by_nr(internalFormat),
1467 width, height, samples);
1468 }
1469
1470 if (target != GL_RENDERBUFFER_EXT) {
1471 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1472 return;
1473 }
1474
1475 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1476 if (baseFormat == 0) {
1477 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1478 func, _mesa_lookup_enum_by_nr(internalFormat));
1479 return;
1480 }
1481
1482 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1483 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1484 return;
1485 }
1486
1487 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1488 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1489 return;
1490 }
1491
1492 if (samples == NO_SAMPLES) {
1493 /* NumSamples == 0 indicates non-multisampling */
1494 samples = 0;
1495 }
1496 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1497 /* note: driver may choose to use more samples than what's requested */
1498 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1499 return;
1500 }
1501
1502 rb = ctx->CurrentRenderbuffer;
1503 if (!rb) {
1504 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1505 return;
1506 }
1507
1508 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1509
1510 if (rb->InternalFormat == internalFormat &&
1511 rb->Width == (GLuint) width &&
1512 rb->Height == (GLuint) height &&
1513 rb->NumSamples == samples) {
1514 /* no change in allocation needed */
1515 return;
1516 }
1517
1518 /* These MUST get set by the AllocStorage func */
1519 rb->Format = MESA_FORMAT_NONE;
1520 rb->NumSamples = samples;
1521
1522 /* Now allocate the storage */
1523 ASSERT(rb->AllocStorage);
1524 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1525 /* No error - check/set fields now */
1526 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1527 assert(rb->Width == (GLuint) width);
1528 assert(rb->Height == (GLuint) height);
1529 rb->InternalFormat = internalFormat;
1530 rb->_BaseFormat = baseFormat;
1531 assert(rb->_BaseFormat != 0);
1532 }
1533 else {
1534 /* Probably ran out of memory - clear the fields */
1535 rb->Width = 0;
1536 rb->Height = 0;
1537 rb->Format = MESA_FORMAT_NONE;
1538 rb->InternalFormat = GL_NONE;
1539 rb->_BaseFormat = GL_NONE;
1540 rb->NumSamples = 0;
1541 }
1542
1543 /* Invalidate the framebuffers the renderbuffer is attached in. */
1544 if (rb->AttachedAnytime) {
1545 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1546 }
1547 }
1548
1549
1550 void GLAPIENTRY
1551 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1552 {
1553 struct gl_renderbuffer *rb;
1554 GET_CURRENT_CONTEXT(ctx);
1555 ASSERT_OUTSIDE_BEGIN_END(ctx);
1556
1557 if (!ctx->Extensions.OES_EGL_image) {
1558 _mesa_error(ctx, GL_INVALID_OPERATION,
1559 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1560 return;
1561 }
1562
1563 if (target != GL_RENDERBUFFER) {
1564 _mesa_error(ctx, GL_INVALID_ENUM,
1565 "EGLImageTargetRenderbufferStorageOES");
1566 return;
1567 }
1568
1569 rb = ctx->CurrentRenderbuffer;
1570 if (!rb) {
1571 _mesa_error(ctx, GL_INVALID_OPERATION,
1572 "EGLImageTargetRenderbufferStorageOES");
1573 return;
1574 }
1575
1576 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1577
1578 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1579 }
1580
1581
1582 /**
1583 * Helper function for _mesa_GetRenderbufferParameteriv() and
1584 * _mesa_GetFramebufferAttachmentParameteriv()
1585 * We have to be careful to respect the base format. For example, if a
1586 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1587 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1588 * we need to return zero.
1589 */
1590 static GLint
1591 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1592 {
1593 if (_mesa_base_format_has_channel(baseFormat, pname))
1594 return _mesa_get_format_bits(format, pname);
1595 else
1596 return 0;
1597 }
1598
1599
1600
1601 void GLAPIENTRY
1602 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1603 GLsizei width, GLsizei height)
1604 {
1605 /* GL_ARB_fbo says calling this function is equivalent to calling
1606 * glRenderbufferStorageMultisample() with samples=0. We pass in
1607 * a token value here just for error reporting purposes.
1608 */
1609 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1610 }
1611
1612
1613 void GLAPIENTRY
1614 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1615 GLenum internalFormat,
1616 GLsizei width, GLsizei height)
1617 {
1618 renderbuffer_storage(target, internalFormat, width, height, samples);
1619 }
1620
1621
1622 /**
1623 * OpenGL ES version of glRenderBufferStorage.
1624 */
1625 void GLAPIENTRY
1626 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1627 GLsizei width, GLsizei height)
1628 {
1629 switch (internalFormat) {
1630 case GL_RGB565:
1631 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1632 /* choose a closest format */
1633 internalFormat = GL_RGB5;
1634 break;
1635 default:
1636 break;
1637 }
1638
1639 renderbuffer_storage(target, internalFormat, width, height, 0);
1640 }
1641
1642
1643 void GLAPIENTRY
1644 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1645 {
1646 struct gl_renderbuffer *rb;
1647 GET_CURRENT_CONTEXT(ctx);
1648
1649 ASSERT_OUTSIDE_BEGIN_END(ctx);
1650
1651 if (target != GL_RENDERBUFFER_EXT) {
1652 _mesa_error(ctx, GL_INVALID_ENUM,
1653 "glGetRenderbufferParameterivEXT(target)");
1654 return;
1655 }
1656
1657 rb = ctx->CurrentRenderbuffer;
1658 if (!rb) {
1659 _mesa_error(ctx, GL_INVALID_OPERATION,
1660 "glGetRenderbufferParameterivEXT");
1661 return;
1662 }
1663
1664 /* No need to flush here since we're just quering state which is
1665 * not effected by rendering.
1666 */
1667
1668 switch (pname) {
1669 case GL_RENDERBUFFER_WIDTH_EXT:
1670 *params = rb->Width;
1671 return;
1672 case GL_RENDERBUFFER_HEIGHT_EXT:
1673 *params = rb->Height;
1674 return;
1675 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1676 *params = rb->InternalFormat;
1677 return;
1678 case GL_RENDERBUFFER_RED_SIZE_EXT:
1679 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1680 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1681 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1682 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1683 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1684 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1685 break;
1686 case GL_RENDERBUFFER_SAMPLES:
1687 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1688 || _mesa_is_gles3(ctx)) {
1689 *params = rb->NumSamples;
1690 break;
1691 }
1692 /* fallthrough */
1693 default:
1694 _mesa_error(ctx, GL_INVALID_ENUM,
1695 "glGetRenderbufferParameterivEXT(target)");
1696 return;
1697 }
1698 }
1699
1700
1701 GLboolean GLAPIENTRY
1702 _mesa_IsFramebuffer(GLuint framebuffer)
1703 {
1704 GET_CURRENT_CONTEXT(ctx);
1705 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1706 if (framebuffer) {
1707 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1708 if (rb != NULL && rb != &DummyFramebuffer)
1709 return GL_TRUE;
1710 }
1711 return GL_FALSE;
1712 }
1713
1714
1715 /**
1716 * Check if any of the attachments of the given framebuffer are textures
1717 * (render to texture). Call ctx->Driver.RenderTexture() for such
1718 * attachments.
1719 */
1720 static void
1721 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1722 {
1723 GLuint i;
1724 ASSERT(ctx->Driver.RenderTexture);
1725
1726 if (_mesa_is_winsys_fbo(fb))
1727 return; /* can't render to texture with winsys framebuffers */
1728
1729 for (i = 0; i < BUFFER_COUNT; i++) {
1730 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1731 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1732 ctx->Driver.RenderTexture(ctx, fb, att);
1733 }
1734 }
1735 }
1736
1737
1738 /**
1739 * Examine all the framebuffer's attachments to see if any are textures.
1740 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1741 * notify the device driver that the texture image may have changed.
1742 */
1743 static void
1744 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1745 {
1746 if (_mesa_is_winsys_fbo(fb))
1747 return; /* can't render to texture with winsys framebuffers */
1748
1749 if (ctx->Driver.FinishRenderTexture) {
1750 GLuint i;
1751 for (i = 0; i < BUFFER_COUNT; i++) {
1752 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1753 if (att->Texture && att->Renderbuffer) {
1754 ctx->Driver.FinishRenderTexture(ctx, att);
1755 }
1756 }
1757 }
1758 }
1759
1760
1761 void GLAPIENTRY
1762 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1763 {
1764 struct gl_framebuffer *newDrawFb, *newReadFb;
1765 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1766 GLboolean bindReadBuf, bindDrawBuf;
1767 GET_CURRENT_CONTEXT(ctx);
1768
1769 #ifdef DEBUG
1770 if (ctx->Extensions.ARB_framebuffer_object) {
1771 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1772 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1773 }
1774 #endif
1775
1776 ASSERT_OUTSIDE_BEGIN_END(ctx);
1777
1778 if (!ctx->Extensions.EXT_framebuffer_object) {
1779 _mesa_error(ctx, GL_INVALID_OPERATION,
1780 "glBindFramebufferEXT(unsupported)");
1781 return;
1782 }
1783
1784 switch (target) {
1785 case GL_DRAW_FRAMEBUFFER_EXT:
1786 if (!ctx->Extensions.EXT_framebuffer_blit) {
1787 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1788 return;
1789 }
1790 bindDrawBuf = GL_TRUE;
1791 bindReadBuf = GL_FALSE;
1792 break;
1793 case GL_READ_FRAMEBUFFER_EXT:
1794 if (!ctx->Extensions.EXT_framebuffer_blit) {
1795 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1796 return;
1797 }
1798 bindDrawBuf = GL_FALSE;
1799 bindReadBuf = GL_TRUE;
1800 break;
1801 case GL_FRAMEBUFFER_EXT:
1802 bindDrawBuf = GL_TRUE;
1803 bindReadBuf = GL_TRUE;
1804 break;
1805 default:
1806 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1807 return;
1808 }
1809
1810 if (framebuffer) {
1811 /* Binding a user-created framebuffer object */
1812 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1813 if (newDrawFb == &DummyFramebuffer) {
1814 /* ID was reserved, but no real framebuffer object made yet */
1815 newDrawFb = NULL;
1816 }
1817 else if (!newDrawFb
1818 && _mesa_is_desktop_gl(ctx)
1819 && ctx->Extensions.ARB_framebuffer_object) {
1820 /* All FBO IDs must be Gen'd */
1821 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1822 return;
1823 }
1824
1825 if (!newDrawFb) {
1826 /* create new framebuffer object */
1827 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1828 if (!newDrawFb) {
1829 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1830 return;
1831 }
1832 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1833 }
1834 newReadFb = newDrawFb;
1835 }
1836 else {
1837 /* Binding the window system framebuffer (which was originally set
1838 * with MakeCurrent).
1839 */
1840 newDrawFb = ctx->WinSysDrawBuffer;
1841 newReadFb = ctx->WinSysReadBuffer;
1842 }
1843
1844 ASSERT(newDrawFb);
1845 ASSERT(newDrawFb != &DummyFramebuffer);
1846
1847 /* save pointers to current/old framebuffers */
1848 oldDrawFb = ctx->DrawBuffer;
1849 oldReadFb = ctx->ReadBuffer;
1850
1851 /* check if really changing bindings */
1852 if (oldDrawFb == newDrawFb)
1853 bindDrawBuf = GL_FALSE;
1854 if (oldReadFb == newReadFb)
1855 bindReadBuf = GL_FALSE;
1856
1857 /*
1858 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1859 *
1860 * We also check if we're beginning and/or ending render-to-texture.
1861 * When a framebuffer with texture attachments is unbound, call
1862 * ctx->Driver.FinishRenderTexture().
1863 * When a framebuffer with texture attachments is bound, call
1864 * ctx->Driver.RenderTexture().
1865 *
1866 * Note that if the ReadBuffer has texture attachments we don't consider
1867 * that a render-to-texture case.
1868 */
1869 if (bindReadBuf) {
1870 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1871
1872 /* check if old readbuffer was render-to-texture */
1873 check_end_texture_render(ctx, oldReadFb);
1874
1875 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1876 }
1877
1878 if (bindDrawBuf) {
1879 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1880
1881 /* check if old framebuffer had any texture attachments */
1882 if (oldDrawFb)
1883 check_end_texture_render(ctx, oldDrawFb);
1884
1885 /* check if newly bound framebuffer has any texture attachments */
1886 check_begin_texture_render(ctx, newDrawFb);
1887
1888 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1889 }
1890
1891 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1892 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1893 }
1894 }
1895
1896
1897 void GLAPIENTRY
1898 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1899 {
1900 GLint i;
1901 GET_CURRENT_CONTEXT(ctx);
1902
1903 ASSERT_OUTSIDE_BEGIN_END(ctx);
1904 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1905
1906 for (i = 0; i < n; i++) {
1907 if (framebuffers[i] > 0) {
1908 struct gl_framebuffer *fb;
1909 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1910 if (fb) {
1911 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1912
1913 /* check if deleting currently bound framebuffer object */
1914 if (ctx->Extensions.EXT_framebuffer_blit) {
1915 /* separate draw/read binding points */
1916 if (fb == ctx->DrawBuffer) {
1917 /* bind default */
1918 ASSERT(fb->RefCount >= 2);
1919 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1920 }
1921 if (fb == ctx->ReadBuffer) {
1922 /* bind default */
1923 ASSERT(fb->RefCount >= 2);
1924 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1925 }
1926 }
1927 else {
1928 /* only one binding point for read/draw buffers */
1929 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1930 /* bind default */
1931 ASSERT(fb->RefCount >= 2);
1932 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1933 }
1934 }
1935
1936 /* remove from hash table immediately, to free the ID */
1937 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1938
1939 if (fb != &DummyFramebuffer) {
1940 /* But the object will not be freed until it's no longer
1941 * bound in any context.
1942 */
1943 _mesa_reference_framebuffer(&fb, NULL);
1944 }
1945 }
1946 }
1947 }
1948 }
1949
1950
1951 void GLAPIENTRY
1952 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1953 {
1954 GET_CURRENT_CONTEXT(ctx);
1955 GLuint first;
1956 GLint i;
1957
1958 ASSERT_OUTSIDE_BEGIN_END(ctx);
1959
1960 if (n < 0) {
1961 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1962 return;
1963 }
1964
1965 if (!framebuffers)
1966 return;
1967
1968 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1969
1970 for (i = 0; i < n; i++) {
1971 GLuint name = first + i;
1972 framebuffers[i] = name;
1973 /* insert dummy placeholder into hash table */
1974 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1975 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1976 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1977 }
1978 }
1979
1980
1981
1982 GLenum GLAPIENTRY
1983 _mesa_CheckFramebufferStatus(GLenum target)
1984 {
1985 struct gl_framebuffer *buffer;
1986 GET_CURRENT_CONTEXT(ctx);
1987
1988 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1989
1990 if (MESA_VERBOSE & VERBOSE_API)
1991 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1992 _mesa_lookup_enum_by_nr(target));
1993
1994 buffer = get_framebuffer_target(ctx, target);
1995 if (!buffer) {
1996 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1997 return 0;
1998 }
1999
2000 if (_mesa_is_winsys_fbo(buffer)) {
2001 /* The window system / default framebuffer is always complete */
2002 return GL_FRAMEBUFFER_COMPLETE_EXT;
2003 }
2004
2005 /* No need to flush here */
2006
2007 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2008 _mesa_test_framebuffer_completeness(ctx, buffer);
2009 }
2010
2011 return buffer->_Status;
2012 }
2013
2014
2015 /**
2016 * Replicate the src attachment point. Used by framebuffer_texture() when
2017 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2018 * GL_STENCIL_ATTACHMENT.
2019 */
2020 static void
2021 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2022 gl_buffer_index dst,
2023 gl_buffer_index src)
2024 {
2025 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2026 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2027
2028 assert(src_att->Texture != NULL);
2029 assert(src_att->Renderbuffer != NULL);
2030
2031 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2032 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2033 dst_att->Type = src_att->Type;
2034 dst_att->Complete = src_att->Complete;
2035 dst_att->TextureLevel = src_att->TextureLevel;
2036 dst_att->Zoffset = src_att->Zoffset;
2037 }
2038
2039
2040 /**
2041 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2042 * glFramebufferTextureLayerEXT().
2043 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2044 * get textarget=0 in that case.
2045 */
2046 static void
2047 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
2048 GLenum attachment, GLenum textarget, GLuint texture,
2049 GLint level, GLint zoffset)
2050 {
2051 struct gl_renderbuffer_attachment *att;
2052 struct gl_texture_object *texObj = NULL;
2053 struct gl_framebuffer *fb;
2054 GLenum maxLevelsTarget;
2055
2056 ASSERT_OUTSIDE_BEGIN_END(ctx);
2057
2058 fb = get_framebuffer_target(ctx, target);
2059 if (!fb) {
2060 _mesa_error(ctx, GL_INVALID_ENUM,
2061 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2062 return;
2063 }
2064
2065 /* check framebuffer binding */
2066 if (_mesa_is_winsys_fbo(fb)) {
2067 _mesa_error(ctx, GL_INVALID_OPERATION,
2068 "glFramebufferTexture%sEXT", caller);
2069 return;
2070 }
2071
2072 /* The textarget, level, and zoffset parameters are only validated if
2073 * texture is non-zero.
2074 */
2075 if (texture) {
2076 GLboolean err = GL_TRUE;
2077
2078 texObj = _mesa_lookup_texture(ctx, texture);
2079 if (texObj != NULL) {
2080 if (textarget == 0) {
2081 /* If textarget == 0 it means we're being called by
2082 * glFramebufferTextureLayer() and textarget is not used.
2083 * The only legal texture types for that function are 3D and
2084 * 1D/2D arrays textures.
2085 */
2086 err = (texObj->Target != GL_TEXTURE_3D) &&
2087 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2088 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2089 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2090 }
2091 else {
2092 /* Make sure textarget is consistent with the texture's type */
2093 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2094 ? !_mesa_is_cube_face(textarget)
2095 : (texObj->Target != textarget);
2096 }
2097 }
2098 else {
2099 /* can't render to a non-existant texture */
2100 _mesa_error(ctx, GL_INVALID_OPERATION,
2101 "glFramebufferTexture%sEXT(non existant texture)",
2102 caller);
2103 return;
2104 }
2105
2106 if (err) {
2107 _mesa_error(ctx, GL_INVALID_OPERATION,
2108 "glFramebufferTexture%sEXT(texture target mismatch)",
2109 caller);
2110 return;
2111 }
2112
2113 if (texObj->Target == GL_TEXTURE_3D) {
2114 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2115 if (zoffset < 0 || zoffset >= maxSize) {
2116 _mesa_error(ctx, GL_INVALID_VALUE,
2117 "glFramebufferTexture%sEXT(zoffset)", caller);
2118 return;
2119 }
2120 }
2121 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2122 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2123 (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2124 if (zoffset < 0 ||
2125 zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2126 _mesa_error(ctx, GL_INVALID_VALUE,
2127 "glFramebufferTexture%sEXT(layer)", caller);
2128 return;
2129 }
2130 }
2131
2132 maxLevelsTarget = textarget ? textarget : texObj->Target;
2133 if ((level < 0) ||
2134 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2135 _mesa_error(ctx, GL_INVALID_VALUE,
2136 "glFramebufferTexture%sEXT(level)", caller);
2137 return;
2138 }
2139 }
2140
2141 att = _mesa_get_attachment(ctx, fb, attachment);
2142 if (att == NULL) {
2143 _mesa_error(ctx, GL_INVALID_ENUM,
2144 "glFramebufferTexture%sEXT(attachment)", caller);
2145 return;
2146 }
2147
2148 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2149
2150 _glthread_LOCK_MUTEX(fb->Mutex);
2151 if (texObj) {
2152 if (attachment == GL_DEPTH_ATTACHMENT &&
2153 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2154 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2155 _mesa_tex_target_to_face(textarget) ==
2156 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2157 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2158 /* The texture object is already attached to the stencil attachment
2159 * point. Don't create a new renderbuffer; just reuse the stencil
2160 * attachment's. This is required to prevent a GL error in
2161 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2162 */
2163 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2164 BUFFER_STENCIL);
2165 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2166 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2167 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2168 _mesa_tex_target_to_face(textarget) ==
2169 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2170 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2171 /* As above, but with depth and stencil transposed. */
2172 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2173 BUFFER_DEPTH);
2174 } else {
2175 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2176 level, zoffset);
2177 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2178 /* Above we created a new renderbuffer and attached it to the
2179 * depth attachment point. Now attach it to the stencil attachment
2180 * point too.
2181 */
2182 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2183 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2184 BUFFER_DEPTH);
2185 }
2186 }
2187
2188 /* Set the render-to-texture flag. We'll check this flag in
2189 * glTexImage() and friends to determine if we need to revalidate
2190 * any FBOs that might be rendering into this texture.
2191 * This flag never gets cleared since it's non-trivial to determine
2192 * when all FBOs might be done rendering to this texture. That's OK
2193 * though since it's uncommon to render to a texture then repeatedly
2194 * call glTexImage() to change images in the texture.
2195 */
2196 texObj->_RenderToTexture = GL_TRUE;
2197 }
2198 else {
2199 _mesa_remove_attachment(ctx, att);
2200 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2201 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2202 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2203 }
2204 }
2205
2206 invalidate_framebuffer(fb);
2207
2208 _glthread_UNLOCK_MUTEX(fb->Mutex);
2209 }
2210
2211
2212
2213 void GLAPIENTRY
2214 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2215 GLenum textarget, GLuint texture, GLint level)
2216 {
2217 GET_CURRENT_CONTEXT(ctx);
2218
2219 if (texture != 0) {
2220 GLboolean error;
2221
2222 switch (textarget) {
2223 case GL_TEXTURE_1D:
2224 error = GL_FALSE;
2225 break;
2226 case GL_TEXTURE_1D_ARRAY:
2227 error = !ctx->Extensions.EXT_texture_array;
2228 break;
2229 default:
2230 error = GL_TRUE;
2231 }
2232
2233 if (error) {
2234 _mesa_error(ctx, GL_INVALID_OPERATION,
2235 "glFramebufferTexture1DEXT(textarget=%s)",
2236 _mesa_lookup_enum_by_nr(textarget));
2237 return;
2238 }
2239 }
2240
2241 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2242 level, 0);
2243 }
2244
2245
2246 void GLAPIENTRY
2247 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2248 GLenum textarget, GLuint texture, GLint level)
2249 {
2250 GET_CURRENT_CONTEXT(ctx);
2251
2252 if (texture != 0) {
2253 GLboolean error;
2254
2255 switch (textarget) {
2256 case GL_TEXTURE_2D:
2257 error = GL_FALSE;
2258 break;
2259 case GL_TEXTURE_RECTANGLE:
2260 error = _mesa_is_gles(ctx)
2261 || !ctx->Extensions.NV_texture_rectangle;
2262 break;
2263 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2264 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2265 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2266 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2267 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2268 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2269 error = !ctx->Extensions.ARB_texture_cube_map;
2270 break;
2271 case GL_TEXTURE_2D_ARRAY:
2272 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2273 || !ctx->Extensions.EXT_texture_array;
2274 break;
2275 default:
2276 error = GL_TRUE;
2277 }
2278
2279 if (error) {
2280 _mesa_error(ctx, GL_INVALID_OPERATION,
2281 "glFramebufferTexture2DEXT(textarget=%s)",
2282 _mesa_lookup_enum_by_nr(textarget));
2283 return;
2284 }
2285 }
2286
2287 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2288 level, 0);
2289 }
2290
2291
2292 void GLAPIENTRY
2293 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2294 GLenum textarget, GLuint texture,
2295 GLint level, GLint zoffset)
2296 {
2297 GET_CURRENT_CONTEXT(ctx);
2298
2299 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2300 _mesa_error(ctx, GL_INVALID_OPERATION,
2301 "glFramebufferTexture3DEXT(textarget)");
2302 return;
2303 }
2304
2305 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2306 level, zoffset);
2307 }
2308
2309
2310 void GLAPIENTRY
2311 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2312 GLuint texture, GLint level, GLint layer)
2313 {
2314 GET_CURRENT_CONTEXT(ctx);
2315
2316 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2317 level, layer);
2318 }
2319
2320
2321 void GLAPIENTRY
2322 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2323 GLenum renderbufferTarget,
2324 GLuint renderbuffer)
2325 {
2326 struct gl_renderbuffer_attachment *att;
2327 struct gl_framebuffer *fb;
2328 struct gl_renderbuffer *rb;
2329 GET_CURRENT_CONTEXT(ctx);
2330
2331 ASSERT_OUTSIDE_BEGIN_END(ctx);
2332
2333 fb = get_framebuffer_target(ctx, target);
2334 if (!fb) {
2335 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2336 return;
2337 }
2338
2339 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2340 _mesa_error(ctx, GL_INVALID_ENUM,
2341 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2342 return;
2343 }
2344
2345 if (_mesa_is_winsys_fbo(fb)) {
2346 /* Can't attach new renderbuffers to a window system framebuffer */
2347 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2348 return;
2349 }
2350
2351 att = _mesa_get_attachment(ctx, fb, attachment);
2352 if (att == NULL) {
2353 _mesa_error(ctx, GL_INVALID_ENUM,
2354 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2355 _mesa_lookup_enum_by_nr(attachment));
2356 return;
2357 }
2358
2359 if (renderbuffer) {
2360 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2361 if (!rb) {
2362 _mesa_error(ctx, GL_INVALID_OPERATION,
2363 "glFramebufferRenderbufferEXT(non-existant"
2364 " renderbuffer %u)", renderbuffer);
2365 return;
2366 }
2367 else if (rb == &DummyRenderbuffer) {
2368 /* This is what NVIDIA does */
2369 _mesa_error(ctx, GL_INVALID_VALUE,
2370 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2371 renderbuffer);
2372 return;
2373 }
2374 }
2375 else {
2376 /* remove renderbuffer attachment */
2377 rb = NULL;
2378 }
2379
2380 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2381 rb && rb->Format != MESA_FORMAT_NONE) {
2382 /* make sure the renderbuffer is a depth/stencil format */
2383 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2384 if (baseFormat != GL_DEPTH_STENCIL) {
2385 _mesa_error(ctx, GL_INVALID_OPERATION,
2386 "glFramebufferRenderbufferEXT(renderbuffer"
2387 " is not DEPTH_STENCIL format)");
2388 return;
2389 }
2390 }
2391
2392
2393 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2394
2395 assert(ctx->Driver.FramebufferRenderbuffer);
2396 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2397
2398 /* Some subsequent GL commands may depend on the framebuffer's visual
2399 * after the binding is updated. Update visual info now.
2400 */
2401 _mesa_update_framebuffer_visual(ctx, fb);
2402 }
2403
2404
2405 void GLAPIENTRY
2406 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2407 GLenum pname, GLint *params)
2408 {
2409 const struct gl_renderbuffer_attachment *att;
2410 struct gl_framebuffer *buffer;
2411 GLenum err;
2412 GET_CURRENT_CONTEXT(ctx);
2413
2414 ASSERT_OUTSIDE_BEGIN_END(ctx);
2415
2416 /* The error differs in GL and GLES. */
2417 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2418
2419 buffer = get_framebuffer_target(ctx, target);
2420 if (!buffer) {
2421 _mesa_error(ctx, GL_INVALID_ENUM,
2422 "glGetFramebufferAttachmentParameterivEXT(target)");
2423 return;
2424 }
2425
2426 if (_mesa_is_winsys_fbo(buffer)) {
2427 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2428 * says:
2429 *
2430 * "If the framebuffer currently bound to target is zero, then
2431 * INVALID_OPERATION is generated."
2432 *
2433 * The EXT_framebuffer_object spec has the same wording, and the
2434 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2435 * spec.
2436 */
2437 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2438 && !_mesa_is_gles3(ctx)) {
2439 _mesa_error(ctx, GL_INVALID_OPERATION,
2440 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2441 return;
2442 }
2443
2444 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2445 attachment != GL_DEPTH && attachment != GL_STENCIL) {
2446 _mesa_error(ctx, GL_INVALID_OPERATION,
2447 "glGetFramebufferAttachmentParameteriv(attachment)");
2448 return;
2449 }
2450 /* the default / window-system FBO */
2451 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2452 }
2453 else {
2454 /* user-created framebuffer FBO */
2455 att = _mesa_get_attachment(ctx, buffer, attachment);
2456 }
2457
2458 if (att == NULL) {
2459 _mesa_error(ctx, GL_INVALID_ENUM,
2460 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2461 return;
2462 }
2463
2464 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2465 /* the depth and stencil attachments must point to the same buffer */
2466 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2467 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2468 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2469 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2470 _mesa_error(ctx, GL_INVALID_OPERATION,
2471 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2472 " attachments differ)");
2473 return;
2474 }
2475 }
2476
2477 /* No need to flush here */
2478
2479 switch (pname) {
2480 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2481 *params = _mesa_is_winsys_fbo(buffer)
2482 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2483 return;
2484 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2485 if (att->Type == GL_RENDERBUFFER_EXT) {
2486 *params = att->Renderbuffer->Name;
2487 }
2488 else if (att->Type == GL_TEXTURE) {
2489 *params = att->Texture->Name;
2490 }
2491 else {
2492 assert(att->Type == GL_NONE);
2493 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2494 *params = 0;
2495 } else {
2496 goto invalid_pname_enum;
2497 }
2498 }
2499 return;
2500 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2501 if (att->Type == GL_TEXTURE) {
2502 *params = att->TextureLevel;
2503 }
2504 else if (att->Type == GL_NONE) {
2505 _mesa_error(ctx, err,
2506 "glGetFramebufferAttachmentParameterivEXT(pname)");
2507 }
2508 else {
2509 goto invalid_pname_enum;
2510 }
2511 return;
2512 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2513 if (att->Type == GL_TEXTURE) {
2514 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2515 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2516 }
2517 else {
2518 *params = 0;
2519 }
2520 }
2521 else if (att->Type == GL_NONE) {
2522 _mesa_error(ctx, err,
2523 "glGetFramebufferAttachmentParameterivEXT(pname)");
2524 }
2525 else {
2526 goto invalid_pname_enum;
2527 }
2528 return;
2529 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2530 if (ctx->API == API_OPENGLES) {
2531 goto invalid_pname_enum;
2532 } else if (att->Type == GL_NONE) {
2533 _mesa_error(ctx, err,
2534 "glGetFramebufferAttachmentParameterivEXT(pname)");
2535 } else if (att->Type == GL_TEXTURE) {
2536 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2537 *params = att->Zoffset;
2538 }
2539 else {
2540 *params = 0;
2541 }
2542 }
2543 else {
2544 goto invalid_pname_enum;
2545 }
2546 return;
2547 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2548 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2549 && !_mesa_is_gles3(ctx)) {
2550 goto invalid_pname_enum;
2551 }
2552 else if (att->Type == GL_NONE) {
2553 _mesa_error(ctx, err,
2554 "glGetFramebufferAttachmentParameterivEXT(pname)");
2555 }
2556 else {
2557 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2558 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2559 }
2560 else {
2561 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2562 * if the sRGB conversion is unsupported. */
2563 *params = GL_LINEAR;
2564 }
2565 }
2566 return;
2567 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2568 if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2569 && ctx->API != API_OPENGL_CORE
2570 && !_mesa_is_gles3(ctx)) {
2571 goto invalid_pname_enum;
2572 }
2573 else if (att->Type == GL_NONE) {
2574 _mesa_error(ctx, err,
2575 "glGetFramebufferAttachmentParameterivEXT(pname)");
2576 }
2577 else {
2578 gl_format format = att->Renderbuffer->Format;
2579 if (format == MESA_FORMAT_S8) {
2580 /* special cases */
2581 *params = GL_INDEX;
2582 }
2583 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2584 /* depends on the attachment parameter */
2585 if (attachment == GL_STENCIL_ATTACHMENT) {
2586 *params = GL_INDEX;
2587 }
2588 else {
2589 *params = GL_FLOAT;
2590 }
2591 }
2592 else {
2593 *params = _mesa_get_format_datatype(format);
2594 }
2595 }
2596 return;
2597 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2598 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2599 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2600 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2601 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2602 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2603 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2604 && !_mesa_is_gles3(ctx)) {
2605 goto invalid_pname_enum;
2606 }
2607 else if (att->Type == GL_NONE) {
2608 _mesa_error(ctx, err,
2609 "glGetFramebufferAttachmentParameterivEXT(pname)");
2610 }
2611 else if (att->Texture) {
2612 const struct gl_texture_image *texImage =
2613 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2614 att->TextureLevel);
2615 if (texImage) {
2616 *params = get_component_bits(pname, texImage->_BaseFormat,
2617 texImage->TexFormat);
2618 }
2619 else {
2620 *params = 0;
2621 }
2622 }
2623 else if (att->Renderbuffer) {
2624 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2625 att->Renderbuffer->Format);
2626 }
2627 else {
2628 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2629 " invalid FBO attachment structure");
2630 }
2631 return;
2632 default:
2633 goto invalid_pname_enum;
2634 }
2635
2636 return;
2637
2638 invalid_pname_enum:
2639 _mesa_error(ctx, GL_INVALID_ENUM,
2640 "glGetFramebufferAttachmentParameteriv(pname)");
2641 return;
2642 }
2643
2644
2645 void GLAPIENTRY
2646 _mesa_GenerateMipmap(GLenum target)
2647 {
2648 struct gl_texture_image *srcImage;
2649 struct gl_texture_object *texObj;
2650 GLboolean error;
2651
2652 GET_CURRENT_CONTEXT(ctx);
2653
2654 ASSERT_OUTSIDE_BEGIN_END(ctx);
2655 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2656
2657 switch (target) {
2658 case GL_TEXTURE_1D:
2659 error = _mesa_is_gles(ctx);
2660 break;
2661 case GL_TEXTURE_2D:
2662 error = GL_FALSE;
2663 break;
2664 case GL_TEXTURE_3D:
2665 error = ctx->API == API_OPENGLES;
2666 break;
2667 case GL_TEXTURE_CUBE_MAP:
2668 error = !ctx->Extensions.ARB_texture_cube_map;
2669 break;
2670 case GL_TEXTURE_1D_ARRAY:
2671 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2672 break;
2673 case GL_TEXTURE_2D_ARRAY:
2674 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2675 || !ctx->Extensions.EXT_texture_array;
2676 break;
2677 default:
2678 error = GL_TRUE;
2679 }
2680
2681 if (error) {
2682 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2683 _mesa_lookup_enum_by_nr(target));
2684 return;
2685 }
2686
2687 texObj = _mesa_get_current_tex_object(ctx, target);
2688
2689 if (texObj->BaseLevel >= texObj->MaxLevel) {
2690 /* nothing to do */
2691 return;
2692 }
2693
2694 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2695 !_mesa_cube_complete(texObj)) {
2696 _mesa_error(ctx, GL_INVALID_OPERATION,
2697 "glGenerateMipmap(incomplete cube map)");
2698 return;
2699 }
2700
2701 _mesa_lock_texture(ctx, texObj);
2702
2703 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2704 if (!srcImage) {
2705 _mesa_unlock_texture(ctx, texObj);
2706 _mesa_error(ctx, GL_INVALID_OPERATION,
2707 "glGenerateMipmap(zero size base image)");
2708 return;
2709 }
2710
2711 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2712 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2713 _mesa_is_stencil_format(srcImage->InternalFormat)) {
2714 _mesa_unlock_texture(ctx, texObj);
2715 _mesa_error(ctx, GL_INVALID_OPERATION,
2716 "glGenerateMipmap(invalid internal format)");
2717 return;
2718 }
2719
2720 if (target == GL_TEXTURE_CUBE_MAP) {
2721 GLuint face;
2722 for (face = 0; face < 6; face++)
2723 ctx->Driver.GenerateMipmap(ctx,
2724 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2725 texObj);
2726 }
2727 else {
2728 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2729 }
2730 _mesa_unlock_texture(ctx, texObj);
2731 }
2732
2733
2734 static const struct gl_renderbuffer_attachment *
2735 find_attachment(const struct gl_framebuffer *fb,
2736 const struct gl_renderbuffer *rb)
2737 {
2738 GLuint i;
2739 for (i = 0; i < Elements(fb->Attachment); i++) {
2740 if (fb->Attachment[i].Renderbuffer == rb)
2741 return &fb->Attachment[i];
2742 }
2743 return NULL;
2744 }
2745
2746
2747 /**
2748 * Helper function for checking if the datatypes of color buffers are
2749 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2750 *
2751 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2752 * and any of the following conditions hold:
2753 * - The read buffer contains fixed-point or floating-point values and any
2754 * draw buffer contains neither fixed-point nor floating-point values.
2755 * - The read buffer contains unsigned integer values and any draw buffer
2756 * does not contain unsigned integer values.
2757 * - The read buffer contains signed integer values and any draw buffer
2758 * does not contain signed integer values."
2759 */
2760 static GLboolean
2761 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2762 {
2763 GLenum srcType = _mesa_get_format_datatype(srcFormat);
2764 GLenum dstType = _mesa_get_format_datatype(dstFormat);
2765
2766 if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2767 assert(srcType == GL_UNSIGNED_NORMALIZED ||
2768 srcType == GL_SIGNED_NORMALIZED ||
2769 srcType == GL_FLOAT);
2770 /* Boil any of those types down to GL_FLOAT */
2771 srcType = GL_FLOAT;
2772 }
2773
2774 if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2775 assert(dstType == GL_UNSIGNED_NORMALIZED ||
2776 dstType == GL_SIGNED_NORMALIZED ||
2777 dstType == GL_FLOAT);
2778 /* Boil any of those types down to GL_FLOAT */
2779 dstType = GL_FLOAT;
2780 }
2781
2782 return srcType == dstType;
2783 }
2784
2785
2786 static GLboolean
2787 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2788 const struct gl_renderbuffer *drawRb)
2789 {
2790 GLenum readFormat, drawFormat;
2791
2792 /* The simple case where we know the backing Mesa formats are the same.
2793 */
2794 if (_mesa_get_srgb_format_linear(readRb->Format) ==
2795 _mesa_get_srgb_format_linear(drawRb->Format)) {
2796 return GL_TRUE;
2797 }
2798
2799 /* The Mesa formats are different, so we must check whether the internal
2800 * formats are compatible.
2801 *
2802 * Under some circumstances, the user may request e.g. two GL_RGBA8
2803 * textures and get two entirely different Mesa formats like RGBA8888 and
2804 * ARGB8888. Drivers behaving like that should be able to cope with
2805 * non-matching formats by themselves, because it's not the user's fault.
2806 *
2807 * Blits between linear and sRGB formats are also allowed.
2808 */
2809 readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2810 drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2811 readFormat = _mesa_get_linear_internalformat(readFormat);
2812 drawFormat = _mesa_get_linear_internalformat(drawFormat);
2813
2814 if (readFormat == drawFormat) {
2815 return GL_TRUE;
2816 }
2817
2818 return GL_FALSE;
2819 }
2820
2821
2822 /**
2823 * Blit rectangular region, optionally from one framebuffer to another.
2824 *
2825 * Note, if the src buffer is multisampled and the dest is not, this is
2826 * when the samples must be resolved to a single color.
2827 */
2828 void GLAPIENTRY
2829 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2830 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2831 GLbitfield mask, GLenum filter)
2832 {
2833 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2834 GL_DEPTH_BUFFER_BIT |
2835 GL_STENCIL_BUFFER_BIT);
2836 const struct gl_framebuffer *readFb, *drawFb;
2837 GET_CURRENT_CONTEXT(ctx);
2838
2839 ASSERT_OUTSIDE_BEGIN_END(ctx);
2840 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2841
2842 if (MESA_VERBOSE & VERBOSE_API)
2843 _mesa_debug(ctx,
2844 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2845 srcX0, srcY0, srcX1, srcY1,
2846 dstX0, dstY0, dstX1, dstY1,
2847 mask, _mesa_lookup_enum_by_nr(filter));
2848
2849 if (ctx->NewState) {
2850 _mesa_update_state(ctx);
2851 }
2852
2853 readFb = ctx->ReadBuffer;
2854 drawFb = ctx->DrawBuffer;
2855
2856 if (!readFb || !drawFb) {
2857 /* This will normally never happen but someday we may want to
2858 * support MakeCurrent() with no drawables.
2859 */
2860 return;
2861 }
2862
2863 /* check for complete framebuffers */
2864 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2865 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2866 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2867 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2868 return;
2869 }
2870
2871 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2872 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2873 return;
2874 }
2875
2876 if (mask & ~legalMaskBits) {
2877 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2878 return;
2879 }
2880
2881 /* depth/stencil must be blitted with nearest filtering */
2882 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2883 && filter != GL_NEAREST) {
2884 _mesa_error(ctx, GL_INVALID_OPERATION,
2885 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2886 return;
2887 }
2888
2889 /* get color read/draw renderbuffers */
2890 if (mask & GL_COLOR_BUFFER_BIT) {
2891 const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2892 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
2893 const struct gl_renderbuffer *colorDrawRb = NULL;
2894 GLuint i;
2895
2896 /* From the EXT_framebuffer_object spec:
2897 *
2898 * "If a buffer is specified in <mask> and does not exist in both
2899 * the read and draw framebuffers, the corresponding bit is silently
2900 * ignored."
2901 */
2902 if (!colorReadRb || numColorDrawBuffers == 0) {
2903 mask &= ~GL_COLOR_BUFFER_BIT;
2904 }
2905 else {
2906 for (i = 0; i < numColorDrawBuffers; i++) {
2907 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2908 if (!colorDrawRb)
2909 continue;
2910
2911 if (!compatible_color_datatypes(colorReadRb->Format,
2912 colorDrawRb->Format)) {
2913 _mesa_error(ctx, GL_INVALID_OPERATION,
2914 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2915 return;
2916 }
2917 /* extra checks for multisample copies... */
2918 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2919 /* color formats must match */
2920 if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2921 _mesa_error(ctx, GL_INVALID_OPERATION,
2922 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2923 return;
2924 }
2925 }
2926 }
2927 if (filter == GL_LINEAR) {
2928 /* 3.1 spec, page 199:
2929 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2930 * if filter is LINEAR and read buffer contains integer data."
2931 */
2932 GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2933 if (type == GL_INT || type == GL_UNSIGNED_INT) {
2934 _mesa_error(ctx, GL_INVALID_OPERATION,
2935 "glBlitFramebufferEXT(integer color type)");
2936 return;
2937 }
2938 }
2939 }
2940 }
2941
2942 if (mask & GL_STENCIL_BUFFER_BIT) {
2943 struct gl_renderbuffer *readRb =
2944 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2945 struct gl_renderbuffer *drawRb =
2946 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2947
2948 /* From the EXT_framebuffer_object spec:
2949 *
2950 * "If a buffer is specified in <mask> and does not exist in both
2951 * the read and draw framebuffers, the corresponding bit is silently
2952 * ignored."
2953 */
2954 if ((readRb == NULL) || (drawRb == NULL)) {
2955 mask &= ~GL_STENCIL_BUFFER_BIT;
2956 }
2957 else {
2958 int read_z_bits, draw_z_bits;
2959
2960 if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2961 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2962 /* There is no need to check the stencil datatype here, because
2963 * there is only one: GL_UNSIGNED_INT.
2964 */
2965 _mesa_error(ctx, GL_INVALID_OPERATION,
2966 "glBlitFramebuffer(stencil attachment format mismatch)");
2967 return;
2968 }
2969
2970 read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
2971 draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
2972
2973 /* If both buffers also have depth data, the depth formats must match
2974 * as well. If one doesn't have depth, it's not blitted, so we should
2975 * ignore the depth format check.
2976 */
2977 if (read_z_bits > 0 && draw_z_bits > 0 &&
2978 (read_z_bits != draw_z_bits ||
2979 _mesa_get_format_datatype(readRb->Format) !=
2980 _mesa_get_format_datatype(drawRb->Format))) {
2981
2982 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
2983 "(stencil attachment depth format mismatch)");
2984 return;
2985 }
2986 }
2987 }
2988
2989 if (mask & GL_DEPTH_BUFFER_BIT) {
2990 struct gl_renderbuffer *readRb =
2991 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2992 struct gl_renderbuffer *drawRb =
2993 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2994
2995 /* From the EXT_framebuffer_object spec:
2996 *
2997 * "If a buffer is specified in <mask> and does not exist in both
2998 * the read and draw framebuffers, the corresponding bit is silently
2999 * ignored."
3000 */
3001 if ((readRb == NULL) || (drawRb == NULL)) {
3002 mask &= ~GL_DEPTH_BUFFER_BIT;
3003 }
3004 else {
3005 int read_s_bit, draw_s_bit;
3006
3007 if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
3008 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
3009 (_mesa_get_format_datatype(readRb->Format) !=
3010 _mesa_get_format_datatype(drawRb->Format))) {
3011 _mesa_error(ctx, GL_INVALID_OPERATION,
3012 "glBlitFramebuffer(depth attachment format mismatch)");
3013 return;
3014 }
3015
3016 read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
3017 draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
3018
3019 /* If both buffers also have stencil data, the stencil formats must
3020 * match as well. If one doesn't have stencil, it's not blitted, so
3021 * we should ignore the stencil format check.
3022 */
3023 if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
3024 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3025 "(depth attachment stencil bits mismatch)");
3026 return;
3027 }
3028 }
3029 }
3030
3031 if (readFb->Visual.samples > 0 &&
3032 drawFb->Visual.samples > 0 &&
3033 readFb->Visual.samples != drawFb->Visual.samples) {
3034 _mesa_error(ctx, GL_INVALID_OPERATION,
3035 "glBlitFramebufferEXT(mismatched samples)");
3036 return;
3037 }
3038
3039 /* extra checks for multisample copies... */
3040 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
3041 /* src and dest region sizes must be the same */
3042 if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
3043 abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3044 _mesa_error(ctx, GL_INVALID_OPERATION,
3045 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3046 return;
3047 }
3048 }
3049
3050 if (!ctx->Extensions.EXT_framebuffer_blit) {
3051 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3052 return;
3053 }
3054
3055 /* Debug code */
3056 if (DEBUG_BLIT) {
3057 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3058 const struct gl_renderbuffer *colorDrawRb = NULL;
3059 GLuint i = 0;
3060
3061 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3062 " 0x%x, 0x%x)\n",
3063 srcX0, srcY0, srcX1, srcY1,
3064 dstX0, dstY0, dstX1, dstY1,
3065 mask, filter);
3066 if (colorReadRb) {
3067 const struct gl_renderbuffer_attachment *att;
3068
3069 att = find_attachment(readFb, colorReadRb);
3070 printf(" Src FBO %u RB %u (%dx%d) ",
3071 readFb->Name, colorReadRb->Name,
3072 colorReadRb->Width, colorReadRb->Height);
3073 if (att && att->Texture) {
3074 printf("Tex %u tgt 0x%x level %u face %u",
3075 att->Texture->Name,
3076 att->Texture->Target,
3077 att->TextureLevel,
3078 att->CubeMapFace);
3079 }
3080 printf("\n");
3081
3082 /* Print all active color render buffers */
3083 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3084 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3085 if (!colorDrawRb)
3086 continue;
3087
3088 att = find_attachment(drawFb, colorDrawRb);
3089 printf(" Dst FBO %u RB %u (%dx%d) ",
3090 drawFb->Name, colorDrawRb->Name,
3091 colorDrawRb->Width, colorDrawRb->Height);
3092 if (att && att->Texture) {
3093 printf("Tex %u tgt 0x%x level %u face %u",
3094 att->Texture->Name,
3095 att->Texture->Target,
3096 att->TextureLevel,
3097 att->CubeMapFace);
3098 }
3099 printf("\n");
3100 }
3101 }
3102 }
3103
3104 if (!mask) {
3105 return;
3106 }
3107
3108 ASSERT(ctx->Driver.BlitFramebuffer);
3109 ctx->Driver.BlitFramebuffer(ctx,
3110 srcX0, srcY0, srcX1, srcY1,
3111 dstX0, dstY0, dstX1, dstY1,
3112 mask, filter);
3113 }
3114
3115
3116 static void
3117 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3118 const GLenum *attachments, GLint x, GLint y,
3119 GLsizei width, GLsizei height, const char *name)
3120 {
3121 int i;
3122 struct gl_framebuffer *fb;
3123 GET_CURRENT_CONTEXT(ctx);
3124
3125 ASSERT_OUTSIDE_BEGIN_END(ctx);
3126
3127 fb = get_framebuffer_target(ctx, target);
3128 if (!fb) {
3129 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3130 return;
3131 }
3132
3133 if (numAttachments < 0) {
3134 _mesa_error(ctx, GL_INVALID_VALUE,
3135 "%s(numAttachments < 0)", name);
3136 return;
3137 }
3138
3139 /* The GL_ARB_invalidate_subdata spec says:
3140 *
3141 * "If an attachment is specified that does not exist in the
3142 * framebuffer bound to <target>, it is ignored."
3143 *
3144 * It also says:
3145 *
3146 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3147 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3148 * INVALID_OPERATION is generated."
3149 *
3150 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3151 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3152 * set of retrictions).
3153 */
3154 for (i = 0; i < numAttachments; i++) {
3155 if (_mesa_is_winsys_fbo(fb)) {
3156 switch (attachments[i]) {
3157 case GL_ACCUM:
3158 case GL_AUX0:
3159 case GL_AUX1:
3160 case GL_AUX2:
3161 case GL_AUX3:
3162 /* Accumulation buffers and auxilary buffers were removed in
3163 * OpenGL 3.1, and they never existed in OpenGL ES.
3164 */
3165 if (ctx->API != API_OPENGL_COMPAT)
3166 goto invalid_enum;
3167 break;
3168 case GL_COLOR:
3169 case GL_DEPTH:
3170 case GL_STENCIL:
3171 break;
3172 case GL_BACK_LEFT:
3173 case GL_BACK_RIGHT:
3174 case GL_FRONT_LEFT:
3175 case GL_FRONT_RIGHT:
3176 if (!_mesa_is_desktop_gl(ctx))
3177 goto invalid_enum;
3178 break;
3179 default:
3180 goto invalid_enum;
3181 }
3182 } else {
3183 switch (attachments[i]) {
3184 case GL_DEPTH_ATTACHMENT:
3185 case GL_STENCIL_ATTACHMENT:
3186 break;
3187 case GL_COLOR_ATTACHMENT0:
3188 case GL_COLOR_ATTACHMENT1:
3189 case GL_COLOR_ATTACHMENT2:
3190 case GL_COLOR_ATTACHMENT3:
3191 case GL_COLOR_ATTACHMENT4:
3192 case GL_COLOR_ATTACHMENT5:
3193 case GL_COLOR_ATTACHMENT6:
3194 case GL_COLOR_ATTACHMENT7:
3195 case GL_COLOR_ATTACHMENT8:
3196 case GL_COLOR_ATTACHMENT9:
3197 case GL_COLOR_ATTACHMENT10:
3198 case GL_COLOR_ATTACHMENT11:
3199 case GL_COLOR_ATTACHMENT12:
3200 case GL_COLOR_ATTACHMENT13:
3201 case GL_COLOR_ATTACHMENT14:
3202 case GL_COLOR_ATTACHMENT15: {
3203 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3204 if (k >= ctx->Const.MaxColorAttachments) {
3205 _mesa_error(ctx, GL_INVALID_OPERATION,
3206 "%s(attachment >= max. color attachments)", name);
3207 return;
3208 }
3209 }
3210 default:
3211 goto invalid_enum;
3212 }
3213 }
3214 }
3215
3216 /* We don't actually do anything for this yet. Just return after
3217 * validating the parameters and generating the required errors.
3218 */
3219 return;
3220
3221 invalid_enum:
3222 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3223 return;
3224 }
3225
3226 void GLAPIENTRY
3227 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3228 const GLenum *attachments, GLint x, GLint y,
3229 GLsizei width, GLsizei height)
3230 {
3231 invalidate_framebuffer_storage(target, numAttachments, attachments,
3232 x, y, width, height,
3233 "glInvalidateSubFramebuffer");
3234 }
3235
3236 void GLAPIENTRY
3237 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3238 const GLenum *attachments)
3239 {
3240 /* The GL_ARB_invalidate_subdata spec says:
3241 *
3242 * "The command
3243 *
3244 * void InvalidateFramebuffer(enum target,
3245 * sizei numAttachments,
3246 * const enum *attachments);
3247 *
3248 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3249 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3250 * <MAX_VIEWPORT_DIMS[1]> respectively."
3251 */
3252 invalidate_framebuffer_storage(target, numAttachments, attachments,
3253 0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3254 "glInvalidateFramebuffer");
3255 }