mesa/es3: Apply stricter multisample blit rules for ES3.
[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 if (target != GL_RENDERBUFFER_EXT) {
971 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
972 return;
973 }
974
975 /* No need to flush here since the render buffer binding has no
976 * effect on rendering state.
977 */
978
979 if (renderbuffer) {
980 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
981 if (newRb == &DummyRenderbuffer) {
982 /* ID was reserved, but no real renderbuffer object made yet */
983 newRb = NULL;
984 }
985 else if (!newRb
986 && _mesa_is_desktop_gl(ctx)
987 && ctx->Extensions.ARB_framebuffer_object) {
988 /* All RB IDs must be Gen'd */
989 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
990 return;
991 }
992
993 if (!newRb) {
994 /* create new renderbuffer object */
995 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
996 if (!newRb) {
997 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
998 return;
999 }
1000 ASSERT(newRb->AllocStorage);
1001 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1002 newRb->RefCount = 1; /* referenced by hash table */
1003 }
1004 }
1005 else {
1006 newRb = NULL;
1007 }
1008
1009 ASSERT(newRb != &DummyRenderbuffer);
1010
1011 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1012 }
1013
1014
1015 /**
1016 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1017 * the renderbuffer.
1018 * This is used when a renderbuffer object is deleted.
1019 * The spec calls for unbinding.
1020 */
1021 static void
1022 detach_renderbuffer(struct gl_context *ctx,
1023 struct gl_framebuffer *fb,
1024 struct gl_renderbuffer *rb)
1025 {
1026 GLuint i;
1027 for (i = 0; i < BUFFER_COUNT; i++) {
1028 if (fb->Attachment[i].Renderbuffer == rb) {
1029 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
1030 }
1031 }
1032 invalidate_framebuffer(fb);
1033 }
1034
1035
1036 void GLAPIENTRY
1037 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1038 {
1039 GLint i;
1040 GET_CURRENT_CONTEXT(ctx);
1041
1042 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1043
1044 for (i = 0; i < n; i++) {
1045 if (renderbuffers[i] > 0) {
1046 struct gl_renderbuffer *rb;
1047 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1048 if (rb) {
1049 /* check if deleting currently bound renderbuffer object */
1050 if (rb == ctx->CurrentRenderbuffer) {
1051 /* bind default */
1052 ASSERT(rb->RefCount >= 2);
1053 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1054 }
1055
1056 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1057 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1058 }
1059 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1060 && ctx->ReadBuffer != ctx->DrawBuffer) {
1061 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1062 }
1063
1064 /* Remove from hash table immediately, to free the ID.
1065 * But the object will not be freed until it's no longer
1066 * referenced anywhere else.
1067 */
1068 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1069
1070 if (rb != &DummyRenderbuffer) {
1071 /* no longer referenced by hash table */
1072 _mesa_reference_renderbuffer(&rb, NULL);
1073 }
1074 }
1075 }
1076 }
1077 }
1078
1079
1080 void GLAPIENTRY
1081 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1082 {
1083 GET_CURRENT_CONTEXT(ctx);
1084 GLuint first;
1085 GLint i;
1086
1087 if (n < 0) {
1088 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1089 return;
1090 }
1091
1092 if (!renderbuffers)
1093 return;
1094
1095 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1096
1097 for (i = 0; i < n; i++) {
1098 GLuint name = first + i;
1099 renderbuffers[i] = name;
1100 /* insert dummy placeholder into hash table */
1101 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1102 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1103 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1104 }
1105 }
1106
1107
1108 /**
1109 * Given an internal format token for a render buffer, return the
1110 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1111 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1112 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1113 *
1114 * This is similar to _mesa_base_tex_format() but the set of valid
1115 * internal formats is different.
1116 *
1117 * Note that even if a format is determined to be legal here, validation
1118 * of the FBO may fail if the format is not supported by the driver/GPU.
1119 *
1120 * \param internalFormat as passed to glRenderbufferStorage()
1121 * \return the base internal format, or 0 if internalFormat is illegal
1122 */
1123 GLenum
1124 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1125 {
1126 /*
1127 * Notes: some formats such as alpha, luminance, etc. were added
1128 * with GL_ARB_framebuffer_object.
1129 */
1130 switch (internalFormat) {
1131 case GL_ALPHA:
1132 case GL_ALPHA4:
1133 case GL_ALPHA8:
1134 case GL_ALPHA12:
1135 case GL_ALPHA16:
1136 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1137 ? GL_ALPHA : 0;
1138 case GL_LUMINANCE:
1139 case GL_LUMINANCE4:
1140 case GL_LUMINANCE8:
1141 case GL_LUMINANCE12:
1142 case GL_LUMINANCE16:
1143 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1144 ? GL_LUMINANCE : 0;
1145 case GL_LUMINANCE_ALPHA:
1146 case GL_LUMINANCE4_ALPHA4:
1147 case GL_LUMINANCE6_ALPHA2:
1148 case GL_LUMINANCE8_ALPHA8:
1149 case GL_LUMINANCE12_ALPHA4:
1150 case GL_LUMINANCE12_ALPHA12:
1151 case GL_LUMINANCE16_ALPHA16:
1152 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1153 ? GL_LUMINANCE_ALPHA : 0;
1154 case GL_INTENSITY:
1155 case GL_INTENSITY4:
1156 case GL_INTENSITY8:
1157 case GL_INTENSITY12:
1158 case GL_INTENSITY16:
1159 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1160 ? GL_INTENSITY : 0;
1161 case GL_RGB8:
1162 return GL_RGB;
1163 case GL_RGB:
1164 case GL_R3_G3_B2:
1165 case GL_RGB4:
1166 case GL_RGB5:
1167 case GL_RGB10:
1168 case GL_RGB12:
1169 case GL_RGB16:
1170 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1171 case GL_SRGB8_EXT:
1172 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1173 case GL_RGBA4:
1174 case GL_RGB5_A1:
1175 case GL_RGBA8:
1176 return GL_RGBA;
1177 case GL_RGBA:
1178 case GL_RGBA2:
1179 case GL_RGBA12:
1180 case GL_RGBA16:
1181 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1182 case GL_RGB10_A2:
1183 case GL_SRGB8_ALPHA8_EXT:
1184 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1185 case GL_STENCIL_INDEX:
1186 case GL_STENCIL_INDEX1_EXT:
1187 case GL_STENCIL_INDEX4_EXT:
1188 case GL_STENCIL_INDEX16_EXT:
1189 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1190 * OpenGL ES, but Mesa does not currently support them.
1191 */
1192 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1193 case GL_STENCIL_INDEX8_EXT:
1194 return GL_STENCIL_INDEX;
1195 case GL_DEPTH_COMPONENT:
1196 case GL_DEPTH_COMPONENT32:
1197 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1198 case GL_DEPTH_COMPONENT16:
1199 case GL_DEPTH_COMPONENT24:
1200 return GL_DEPTH_COMPONENT;
1201 case GL_DEPTH_STENCIL_EXT:
1202 return _mesa_is_desktop_gl(ctx)
1203 && ctx->Extensions.EXT_packed_depth_stencil
1204 ? GL_DEPTH_STENCIL_EXT : 0;
1205 case GL_DEPTH24_STENCIL8_EXT:
1206 return ctx->Extensions.EXT_packed_depth_stencil
1207 ? GL_DEPTH_STENCIL_EXT : 0;
1208 case GL_DEPTH_COMPONENT32F:
1209 return ctx->Version >= 30
1210 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1211 ? GL_DEPTH_COMPONENT : 0;
1212 case GL_DEPTH32F_STENCIL8:
1213 return ctx->Version >= 30
1214 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1215 ? GL_DEPTH_STENCIL : 0;
1216 case GL_RED:
1217 case GL_R16:
1218 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1219 ? GL_RED : 0;
1220 case GL_R8:
1221 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1222 ? GL_RED : 0;
1223 case GL_RG:
1224 case GL_RG16:
1225 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1226 ? GL_RG : 0;
1227 case GL_RG8:
1228 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1229 ? GL_RG : 0;
1230 /* signed normalized texture formats */
1231 case GL_RED_SNORM:
1232 case GL_R8_SNORM:
1233 case GL_R16_SNORM:
1234 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1235 ? GL_RED : 0;
1236 case GL_RG_SNORM:
1237 case GL_RG8_SNORM:
1238 case GL_RG16_SNORM:
1239 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1240 ? GL_RG : 0;
1241 case GL_RGB_SNORM:
1242 case GL_RGB8_SNORM:
1243 case GL_RGB16_SNORM:
1244 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1245 ? GL_RGB : 0;
1246 case GL_RGBA_SNORM:
1247 case GL_RGBA8_SNORM:
1248 case GL_RGBA16_SNORM:
1249 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1250 ? GL_RGBA : 0;
1251 case GL_ALPHA_SNORM:
1252 case GL_ALPHA8_SNORM:
1253 case GL_ALPHA16_SNORM:
1254 return ctx->API == API_OPENGL_COMPAT &&
1255 ctx->Extensions.EXT_texture_snorm &&
1256 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1257 case GL_R16F:
1258 case GL_R32F:
1259 return ((_mesa_is_desktop_gl(ctx) &&
1260 ctx->Extensions.ARB_texture_rg &&
1261 ctx->Extensions.ARB_texture_float) ||
1262 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1263 ? GL_RED : 0;
1264 case GL_RG16F:
1265 case GL_RG32F:
1266 return ((_mesa_is_desktop_gl(ctx) &&
1267 ctx->Extensions.ARB_texture_rg &&
1268 ctx->Extensions.ARB_texture_float) ||
1269 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1270 ? GL_RG : 0;
1271 case GL_RGB16F:
1272 case GL_RGB32F:
1273 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1274 ? GL_RGB : 0;
1275 case GL_RGBA16F:
1276 case GL_RGBA32F:
1277 return ((_mesa_is_desktop_gl(ctx) &&
1278 ctx->Extensions.ARB_texture_float) ||
1279 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1280 ? GL_RGBA : 0;
1281 case GL_ALPHA16F_ARB:
1282 case GL_ALPHA32F_ARB:
1283 return ctx->API == API_OPENGL_COMPAT &&
1284 ctx->Extensions.ARB_texture_float &&
1285 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1286 case GL_LUMINANCE16F_ARB:
1287 case GL_LUMINANCE32F_ARB:
1288 return ctx->API == API_OPENGL_COMPAT &&
1289 ctx->Extensions.ARB_texture_float &&
1290 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1291 case GL_LUMINANCE_ALPHA16F_ARB:
1292 case GL_LUMINANCE_ALPHA32F_ARB:
1293 return ctx->API == API_OPENGL_COMPAT &&
1294 ctx->Extensions.ARB_texture_float &&
1295 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1296 case GL_INTENSITY16F_ARB:
1297 case GL_INTENSITY32F_ARB:
1298 return ctx->API == API_OPENGL_COMPAT &&
1299 ctx->Extensions.ARB_texture_float &&
1300 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1301 case GL_RGB9_E5:
1302 return (_mesa_is_desktop_gl(ctx)
1303 && ctx->Extensions.EXT_texture_shared_exponent)
1304 ? GL_RGB : 0;
1305 case GL_R11F_G11F_B10F:
1306 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1307 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1308 ? GL_RGB : 0;
1309
1310 case GL_RGBA8UI_EXT:
1311 case GL_RGBA16UI_EXT:
1312 case GL_RGBA32UI_EXT:
1313 case GL_RGBA8I_EXT:
1314 case GL_RGBA16I_EXT:
1315 case GL_RGBA32I_EXT:
1316 return ctx->Version >= 30
1317 || (_mesa_is_desktop_gl(ctx) &&
1318 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1319
1320 case GL_RGB8UI_EXT:
1321 case GL_RGB16UI_EXT:
1322 case GL_RGB32UI_EXT:
1323 case GL_RGB8I_EXT:
1324 case GL_RGB16I_EXT:
1325 case GL_RGB32I_EXT:
1326 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1327 ? GL_RGB : 0;
1328 case GL_R8UI:
1329 case GL_R8I:
1330 case GL_R16UI:
1331 case GL_R16I:
1332 case GL_R32UI:
1333 case GL_R32I:
1334 return ctx->Version >= 30
1335 || (_mesa_is_desktop_gl(ctx) &&
1336 ctx->Extensions.ARB_texture_rg &&
1337 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1338
1339 case GL_RG8UI:
1340 case GL_RG8I:
1341 case GL_RG16UI:
1342 case GL_RG16I:
1343 case GL_RG32UI:
1344 case GL_RG32I:
1345 return ctx->Version >= 30
1346 || (_mesa_is_desktop_gl(ctx) &&
1347 ctx->Extensions.ARB_texture_rg &&
1348 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1349
1350 case GL_INTENSITY8I_EXT:
1351 case GL_INTENSITY8UI_EXT:
1352 case GL_INTENSITY16I_EXT:
1353 case GL_INTENSITY16UI_EXT:
1354 case GL_INTENSITY32I_EXT:
1355 case GL_INTENSITY32UI_EXT:
1356 return ctx->API == API_OPENGL_COMPAT &&
1357 ctx->Extensions.EXT_texture_integer &&
1358 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1359
1360 case GL_LUMINANCE8I_EXT:
1361 case GL_LUMINANCE8UI_EXT:
1362 case GL_LUMINANCE16I_EXT:
1363 case GL_LUMINANCE16UI_EXT:
1364 case GL_LUMINANCE32I_EXT:
1365 case GL_LUMINANCE32UI_EXT:
1366 return ctx->API == API_OPENGL_COMPAT &&
1367 ctx->Extensions.EXT_texture_integer &&
1368 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1369
1370 case GL_LUMINANCE_ALPHA8I_EXT:
1371 case GL_LUMINANCE_ALPHA8UI_EXT:
1372 case GL_LUMINANCE_ALPHA16I_EXT:
1373 case GL_LUMINANCE_ALPHA16UI_EXT:
1374 case GL_LUMINANCE_ALPHA32I_EXT:
1375 case GL_LUMINANCE_ALPHA32UI_EXT:
1376 return ctx->API == API_OPENGL_COMPAT &&
1377 ctx->Extensions.EXT_texture_integer &&
1378 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1379
1380 case GL_ALPHA8I_EXT:
1381 case GL_ALPHA8UI_EXT:
1382 case GL_ALPHA16I_EXT:
1383 case GL_ALPHA16UI_EXT:
1384 case GL_ALPHA32I_EXT:
1385 case GL_ALPHA32UI_EXT:
1386 return ctx->API == API_OPENGL_COMPAT &&
1387 ctx->Extensions.EXT_texture_integer &&
1388 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1389
1390 case GL_RGB10_A2UI:
1391 return (_mesa_is_desktop_gl(ctx) &&
1392 ctx->Extensions.ARB_texture_rgb10_a2ui)
1393 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1394
1395 case GL_RGB565:
1396 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1397 ? GL_RGB : 0;
1398 default:
1399 return 0;
1400 }
1401 }
1402
1403
1404 /**
1405 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1406 */
1407 static void
1408 invalidate_rb(GLuint key, void *data, void *userData)
1409 {
1410 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1411 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1412
1413 /* If this is a user-created FBO */
1414 if (_mesa_is_user_fbo(fb)) {
1415 GLuint i;
1416 for (i = 0; i < BUFFER_COUNT; i++) {
1417 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1418 if (att->Type == GL_RENDERBUFFER &&
1419 att->Renderbuffer == rb) {
1420 /* Mark fb status as indeterminate to force re-validation */
1421 fb->_Status = 0;
1422 return;
1423 }
1424 }
1425 }
1426 }
1427
1428
1429 /** sentinal value, see below */
1430 #define NO_SAMPLES 1000
1431
1432
1433 /**
1434 * Helper function used by _mesa_RenderbufferStorage() and
1435 * _mesa_RenderbufferStorageMultisample().
1436 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1437 */
1438 static void
1439 renderbuffer_storage(GLenum target, GLenum internalFormat,
1440 GLsizei width, GLsizei height, GLsizei samples)
1441 {
1442 const char *func = samples == NO_SAMPLES ?
1443 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1444 struct gl_renderbuffer *rb;
1445 GLenum baseFormat;
1446 GET_CURRENT_CONTEXT(ctx);
1447
1448 if (MESA_VERBOSE & VERBOSE_API) {
1449 if (samples == NO_SAMPLES)
1450 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1451 func,
1452 _mesa_lookup_enum_by_nr(target),
1453 _mesa_lookup_enum_by_nr(internalFormat),
1454 width, height);
1455 else
1456 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1457 func,
1458 _mesa_lookup_enum_by_nr(target),
1459 _mesa_lookup_enum_by_nr(internalFormat),
1460 width, height, samples);
1461 }
1462
1463 if (target != GL_RENDERBUFFER_EXT) {
1464 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1465 return;
1466 }
1467
1468 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1469 if (baseFormat == 0) {
1470 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1471 func, _mesa_lookup_enum_by_nr(internalFormat));
1472 return;
1473 }
1474
1475 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1476 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1477 return;
1478 }
1479
1480 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1481 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1482 return;
1483 }
1484
1485 if (samples == NO_SAMPLES) {
1486 /* NumSamples == 0 indicates non-multisampling */
1487 samples = 0;
1488 }
1489 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1490 /* note: driver may choose to use more samples than what's requested */
1491 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1492 return;
1493 }
1494
1495 rb = ctx->CurrentRenderbuffer;
1496 if (!rb) {
1497 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1498 return;
1499 }
1500
1501 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1502
1503 if (rb->InternalFormat == internalFormat &&
1504 rb->Width == (GLuint) width &&
1505 rb->Height == (GLuint) height &&
1506 rb->NumSamples == samples) {
1507 /* no change in allocation needed */
1508 return;
1509 }
1510
1511 /* These MUST get set by the AllocStorage func */
1512 rb->Format = MESA_FORMAT_NONE;
1513 rb->NumSamples = samples;
1514
1515 /* Now allocate the storage */
1516 ASSERT(rb->AllocStorage);
1517 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1518 /* No error - check/set fields now */
1519 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1520 assert(rb->Width == (GLuint) width);
1521 assert(rb->Height == (GLuint) height);
1522 rb->InternalFormat = internalFormat;
1523 rb->_BaseFormat = baseFormat;
1524 assert(rb->_BaseFormat != 0);
1525 }
1526 else {
1527 /* Probably ran out of memory - clear the fields */
1528 rb->Width = 0;
1529 rb->Height = 0;
1530 rb->Format = MESA_FORMAT_NONE;
1531 rb->InternalFormat = GL_NONE;
1532 rb->_BaseFormat = GL_NONE;
1533 rb->NumSamples = 0;
1534 }
1535
1536 /* Invalidate the framebuffers the renderbuffer is attached in. */
1537 if (rb->AttachedAnytime) {
1538 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1539 }
1540 }
1541
1542
1543 void GLAPIENTRY
1544 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1545 {
1546 struct gl_renderbuffer *rb;
1547 GET_CURRENT_CONTEXT(ctx);
1548
1549 if (!ctx->Extensions.OES_EGL_image) {
1550 _mesa_error(ctx, GL_INVALID_OPERATION,
1551 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1552 return;
1553 }
1554
1555 if (target != GL_RENDERBUFFER) {
1556 _mesa_error(ctx, GL_INVALID_ENUM,
1557 "EGLImageTargetRenderbufferStorageOES");
1558 return;
1559 }
1560
1561 rb = ctx->CurrentRenderbuffer;
1562 if (!rb) {
1563 _mesa_error(ctx, GL_INVALID_OPERATION,
1564 "EGLImageTargetRenderbufferStorageOES");
1565 return;
1566 }
1567
1568 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1569
1570 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1571 }
1572
1573
1574 /**
1575 * Helper function for _mesa_GetRenderbufferParameteriv() and
1576 * _mesa_GetFramebufferAttachmentParameteriv()
1577 * We have to be careful to respect the base format. For example, if a
1578 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1579 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1580 * we need to return zero.
1581 */
1582 static GLint
1583 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1584 {
1585 if (_mesa_base_format_has_channel(baseFormat, pname))
1586 return _mesa_get_format_bits(format, pname);
1587 else
1588 return 0;
1589 }
1590
1591
1592
1593 void GLAPIENTRY
1594 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1595 GLsizei width, GLsizei height)
1596 {
1597 /* GL_ARB_fbo says calling this function is equivalent to calling
1598 * glRenderbufferStorageMultisample() with samples=0. We pass in
1599 * a token value here just for error reporting purposes.
1600 */
1601 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1602 }
1603
1604
1605 void GLAPIENTRY
1606 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1607 GLenum internalFormat,
1608 GLsizei width, GLsizei height)
1609 {
1610 renderbuffer_storage(target, internalFormat, width, height, samples);
1611 }
1612
1613
1614 /**
1615 * OpenGL ES version of glRenderBufferStorage.
1616 */
1617 void GLAPIENTRY
1618 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1619 GLsizei width, GLsizei height)
1620 {
1621 switch (internalFormat) {
1622 case GL_RGB565:
1623 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1624 /* choose a closest format */
1625 internalFormat = GL_RGB5;
1626 break;
1627 default:
1628 break;
1629 }
1630
1631 renderbuffer_storage(target, internalFormat, width, height, 0);
1632 }
1633
1634
1635 void GLAPIENTRY
1636 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1637 {
1638 struct gl_renderbuffer *rb;
1639 GET_CURRENT_CONTEXT(ctx);
1640
1641 if (target != GL_RENDERBUFFER_EXT) {
1642 _mesa_error(ctx, GL_INVALID_ENUM,
1643 "glGetRenderbufferParameterivEXT(target)");
1644 return;
1645 }
1646
1647 rb = ctx->CurrentRenderbuffer;
1648 if (!rb) {
1649 _mesa_error(ctx, GL_INVALID_OPERATION,
1650 "glGetRenderbufferParameterivEXT");
1651 return;
1652 }
1653
1654 /* No need to flush here since we're just quering state which is
1655 * not effected by rendering.
1656 */
1657
1658 switch (pname) {
1659 case GL_RENDERBUFFER_WIDTH_EXT:
1660 *params = rb->Width;
1661 return;
1662 case GL_RENDERBUFFER_HEIGHT_EXT:
1663 *params = rb->Height;
1664 return;
1665 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1666 *params = rb->InternalFormat;
1667 return;
1668 case GL_RENDERBUFFER_RED_SIZE_EXT:
1669 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1670 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1671 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1672 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1673 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1674 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1675 break;
1676 case GL_RENDERBUFFER_SAMPLES:
1677 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1678 || _mesa_is_gles3(ctx)) {
1679 *params = rb->NumSamples;
1680 break;
1681 }
1682 /* fallthrough */
1683 default:
1684 _mesa_error(ctx, GL_INVALID_ENUM,
1685 "glGetRenderbufferParameterivEXT(target)");
1686 return;
1687 }
1688 }
1689
1690
1691 GLboolean GLAPIENTRY
1692 _mesa_IsFramebuffer(GLuint framebuffer)
1693 {
1694 GET_CURRENT_CONTEXT(ctx);
1695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1696 if (framebuffer) {
1697 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1698 if (rb != NULL && rb != &DummyFramebuffer)
1699 return GL_TRUE;
1700 }
1701 return GL_FALSE;
1702 }
1703
1704
1705 /**
1706 * Check if any of the attachments of the given framebuffer are textures
1707 * (render to texture). Call ctx->Driver.RenderTexture() for such
1708 * attachments.
1709 */
1710 static void
1711 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1712 {
1713 GLuint i;
1714 ASSERT(ctx->Driver.RenderTexture);
1715
1716 if (_mesa_is_winsys_fbo(fb))
1717 return; /* can't render to texture with winsys framebuffers */
1718
1719 for (i = 0; i < BUFFER_COUNT; i++) {
1720 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1721 if (att->Texture && _mesa_get_attachment_teximage(att)) {
1722 ctx->Driver.RenderTexture(ctx, fb, att);
1723 }
1724 }
1725 }
1726
1727
1728 /**
1729 * Examine all the framebuffer's attachments to see if any are textures.
1730 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1731 * notify the device driver that the texture image may have changed.
1732 */
1733 static void
1734 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1735 {
1736 if (_mesa_is_winsys_fbo(fb))
1737 return; /* can't render to texture with winsys framebuffers */
1738
1739 if (ctx->Driver.FinishRenderTexture) {
1740 GLuint i;
1741 for (i = 0; i < BUFFER_COUNT; i++) {
1742 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1743 if (att->Texture && att->Renderbuffer) {
1744 ctx->Driver.FinishRenderTexture(ctx, att);
1745 }
1746 }
1747 }
1748 }
1749
1750
1751 void GLAPIENTRY
1752 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1753 {
1754 struct gl_framebuffer *newDrawFb, *newReadFb;
1755 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1756 GLboolean bindReadBuf, bindDrawBuf;
1757 GET_CURRENT_CONTEXT(ctx);
1758
1759 #ifdef DEBUG
1760 if (ctx->Extensions.ARB_framebuffer_object) {
1761 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1762 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1763 }
1764 #endif
1765
1766 if (!ctx->Extensions.EXT_framebuffer_object) {
1767 _mesa_error(ctx, GL_INVALID_OPERATION,
1768 "glBindFramebufferEXT(unsupported)");
1769 return;
1770 }
1771
1772 switch (target) {
1773 case GL_DRAW_FRAMEBUFFER_EXT:
1774 if (!ctx->Extensions.EXT_framebuffer_blit) {
1775 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1776 return;
1777 }
1778 bindDrawBuf = GL_TRUE;
1779 bindReadBuf = GL_FALSE;
1780 break;
1781 case GL_READ_FRAMEBUFFER_EXT:
1782 if (!ctx->Extensions.EXT_framebuffer_blit) {
1783 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1784 return;
1785 }
1786 bindDrawBuf = GL_FALSE;
1787 bindReadBuf = GL_TRUE;
1788 break;
1789 case GL_FRAMEBUFFER_EXT:
1790 bindDrawBuf = GL_TRUE;
1791 bindReadBuf = GL_TRUE;
1792 break;
1793 default:
1794 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1795 return;
1796 }
1797
1798 if (framebuffer) {
1799 /* Binding a user-created framebuffer object */
1800 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1801 if (newDrawFb == &DummyFramebuffer) {
1802 /* ID was reserved, but no real framebuffer object made yet */
1803 newDrawFb = NULL;
1804 }
1805 else if (!newDrawFb
1806 && _mesa_is_desktop_gl(ctx)
1807 && ctx->Extensions.ARB_framebuffer_object) {
1808 /* All FBO IDs must be Gen'd */
1809 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1810 return;
1811 }
1812
1813 if (!newDrawFb) {
1814 /* create new framebuffer object */
1815 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1816 if (!newDrawFb) {
1817 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1818 return;
1819 }
1820 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1821 }
1822 newReadFb = newDrawFb;
1823 }
1824 else {
1825 /* Binding the window system framebuffer (which was originally set
1826 * with MakeCurrent).
1827 */
1828 newDrawFb = ctx->WinSysDrawBuffer;
1829 newReadFb = ctx->WinSysReadBuffer;
1830 }
1831
1832 ASSERT(newDrawFb);
1833 ASSERT(newDrawFb != &DummyFramebuffer);
1834
1835 /* save pointers to current/old framebuffers */
1836 oldDrawFb = ctx->DrawBuffer;
1837 oldReadFb = ctx->ReadBuffer;
1838
1839 /* check if really changing bindings */
1840 if (oldDrawFb == newDrawFb)
1841 bindDrawBuf = GL_FALSE;
1842 if (oldReadFb == newReadFb)
1843 bindReadBuf = GL_FALSE;
1844
1845 /*
1846 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1847 *
1848 * We also check if we're beginning and/or ending render-to-texture.
1849 * When a framebuffer with texture attachments is unbound, call
1850 * ctx->Driver.FinishRenderTexture().
1851 * When a framebuffer with texture attachments is bound, call
1852 * ctx->Driver.RenderTexture().
1853 *
1854 * Note that if the ReadBuffer has texture attachments we don't consider
1855 * that a render-to-texture case.
1856 */
1857 if (bindReadBuf) {
1858 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1859
1860 /* check if old readbuffer was render-to-texture */
1861 check_end_texture_render(ctx, oldReadFb);
1862
1863 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1864 }
1865
1866 if (bindDrawBuf) {
1867 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1868
1869 /* check if old framebuffer had any texture attachments */
1870 if (oldDrawFb)
1871 check_end_texture_render(ctx, oldDrawFb);
1872
1873 /* check if newly bound framebuffer has any texture attachments */
1874 check_begin_texture_render(ctx, newDrawFb);
1875
1876 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1877 }
1878
1879 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1880 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1881 }
1882 }
1883
1884
1885 void GLAPIENTRY
1886 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1887 {
1888 GLint i;
1889 GET_CURRENT_CONTEXT(ctx);
1890
1891 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1892
1893 for (i = 0; i < n; i++) {
1894 if (framebuffers[i] > 0) {
1895 struct gl_framebuffer *fb;
1896 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1897 if (fb) {
1898 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1899
1900 /* check if deleting currently bound framebuffer object */
1901 if (ctx->Extensions.EXT_framebuffer_blit) {
1902 /* separate draw/read binding points */
1903 if (fb == ctx->DrawBuffer) {
1904 /* bind default */
1905 ASSERT(fb->RefCount >= 2);
1906 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1907 }
1908 if (fb == ctx->ReadBuffer) {
1909 /* bind default */
1910 ASSERT(fb->RefCount >= 2);
1911 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1912 }
1913 }
1914 else {
1915 /* only one binding point for read/draw buffers */
1916 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1917 /* bind default */
1918 ASSERT(fb->RefCount >= 2);
1919 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1920 }
1921 }
1922
1923 /* remove from hash table immediately, to free the ID */
1924 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1925
1926 if (fb != &DummyFramebuffer) {
1927 /* But the object will not be freed until it's no longer
1928 * bound in any context.
1929 */
1930 _mesa_reference_framebuffer(&fb, NULL);
1931 }
1932 }
1933 }
1934 }
1935 }
1936
1937
1938 void GLAPIENTRY
1939 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1940 {
1941 GET_CURRENT_CONTEXT(ctx);
1942 GLuint first;
1943 GLint i;
1944
1945 if (n < 0) {
1946 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1947 return;
1948 }
1949
1950 if (!framebuffers)
1951 return;
1952
1953 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1954
1955 for (i = 0; i < n; i++) {
1956 GLuint name = first + i;
1957 framebuffers[i] = name;
1958 /* insert dummy placeholder into hash table */
1959 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1960 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1961 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1962 }
1963 }
1964
1965
1966
1967 GLenum GLAPIENTRY
1968 _mesa_CheckFramebufferStatus(GLenum target)
1969 {
1970 struct gl_framebuffer *buffer;
1971 GET_CURRENT_CONTEXT(ctx);
1972
1973 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1974
1975 if (MESA_VERBOSE & VERBOSE_API)
1976 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1977 _mesa_lookup_enum_by_nr(target));
1978
1979 buffer = get_framebuffer_target(ctx, target);
1980 if (!buffer) {
1981 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1982 return 0;
1983 }
1984
1985 if (_mesa_is_winsys_fbo(buffer)) {
1986 /* The window system / default framebuffer is always complete */
1987 return GL_FRAMEBUFFER_COMPLETE_EXT;
1988 }
1989
1990 /* No need to flush here */
1991
1992 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1993 _mesa_test_framebuffer_completeness(ctx, buffer);
1994 }
1995
1996 return buffer->_Status;
1997 }
1998
1999
2000 /**
2001 * Replicate the src attachment point. Used by framebuffer_texture() when
2002 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2003 * GL_STENCIL_ATTACHMENT.
2004 */
2005 static void
2006 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2007 gl_buffer_index dst,
2008 gl_buffer_index src)
2009 {
2010 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2011 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2012
2013 assert(src_att->Texture != NULL);
2014 assert(src_att->Renderbuffer != NULL);
2015
2016 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2017 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2018 dst_att->Type = src_att->Type;
2019 dst_att->Complete = src_att->Complete;
2020 dst_att->TextureLevel = src_att->TextureLevel;
2021 dst_att->Zoffset = src_att->Zoffset;
2022 }
2023
2024
2025 /**
2026 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2027 * glFramebufferTextureLayerEXT().
2028 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2029 * get textarget=0 in that case.
2030 */
2031 static void
2032 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
2033 GLenum attachment, GLenum textarget, GLuint texture,
2034 GLint level, GLint zoffset)
2035 {
2036 struct gl_renderbuffer_attachment *att;
2037 struct gl_texture_object *texObj = NULL;
2038 struct gl_framebuffer *fb;
2039 GLenum maxLevelsTarget;
2040
2041 fb = get_framebuffer_target(ctx, target);
2042 if (!fb) {
2043 _mesa_error(ctx, GL_INVALID_ENUM,
2044 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2045 return;
2046 }
2047
2048 /* check framebuffer binding */
2049 if (_mesa_is_winsys_fbo(fb)) {
2050 _mesa_error(ctx, GL_INVALID_OPERATION,
2051 "glFramebufferTexture%sEXT", caller);
2052 return;
2053 }
2054
2055 /* The textarget, level, and zoffset parameters are only validated if
2056 * texture is non-zero.
2057 */
2058 if (texture) {
2059 GLboolean err = GL_TRUE;
2060
2061 texObj = _mesa_lookup_texture(ctx, texture);
2062 if (texObj != NULL) {
2063 if (textarget == 0) {
2064 /* If textarget == 0 it means we're being called by
2065 * glFramebufferTextureLayer() and textarget is not used.
2066 * The only legal texture types for that function are 3D and
2067 * 1D/2D arrays textures.
2068 */
2069 err = (texObj->Target != GL_TEXTURE_3D) &&
2070 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2071 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2072 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2073 }
2074 else {
2075 /* Make sure textarget is consistent with the texture's type */
2076 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2077 ? !_mesa_is_cube_face(textarget)
2078 : (texObj->Target != textarget);
2079 }
2080 }
2081 else {
2082 /* can't render to a non-existant texture */
2083 _mesa_error(ctx, GL_INVALID_OPERATION,
2084 "glFramebufferTexture%sEXT(non existant texture)",
2085 caller);
2086 return;
2087 }
2088
2089 if (err) {
2090 _mesa_error(ctx, GL_INVALID_OPERATION,
2091 "glFramebufferTexture%sEXT(texture target mismatch)",
2092 caller);
2093 return;
2094 }
2095
2096 if (texObj->Target == GL_TEXTURE_3D) {
2097 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2098 if (zoffset < 0 || zoffset >= maxSize) {
2099 _mesa_error(ctx, GL_INVALID_VALUE,
2100 "glFramebufferTexture%sEXT(zoffset)", caller);
2101 return;
2102 }
2103 }
2104 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2105 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2106 (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2107 if (zoffset < 0 ||
2108 zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2109 _mesa_error(ctx, GL_INVALID_VALUE,
2110 "glFramebufferTexture%sEXT(layer)", caller);
2111 return;
2112 }
2113 }
2114
2115 maxLevelsTarget = textarget ? textarget : texObj->Target;
2116 if ((level < 0) ||
2117 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2118 _mesa_error(ctx, GL_INVALID_VALUE,
2119 "glFramebufferTexture%sEXT(level)", caller);
2120 return;
2121 }
2122 }
2123
2124 att = _mesa_get_attachment(ctx, fb, attachment);
2125 if (att == NULL) {
2126 _mesa_error(ctx, GL_INVALID_ENUM,
2127 "glFramebufferTexture%sEXT(attachment)", caller);
2128 return;
2129 }
2130
2131 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2132
2133 _glthread_LOCK_MUTEX(fb->Mutex);
2134 if (texObj) {
2135 if (attachment == GL_DEPTH_ATTACHMENT &&
2136 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2137 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2138 _mesa_tex_target_to_face(textarget) ==
2139 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2140 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2141 /* The texture object is already attached to the stencil attachment
2142 * point. Don't create a new renderbuffer; just reuse the stencil
2143 * attachment's. This is required to prevent a GL error in
2144 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2145 */
2146 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2147 BUFFER_STENCIL);
2148 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2149 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2150 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2151 _mesa_tex_target_to_face(textarget) ==
2152 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2153 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2154 /* As above, but with depth and stencil transposed. */
2155 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2156 BUFFER_DEPTH);
2157 } else {
2158 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2159 level, zoffset);
2160 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2161 /* Above we created a new renderbuffer and attached it to the
2162 * depth attachment point. Now attach it to the stencil attachment
2163 * point too.
2164 */
2165 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2166 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2167 BUFFER_DEPTH);
2168 }
2169 }
2170
2171 /* Set the render-to-texture flag. We'll check this flag in
2172 * glTexImage() and friends to determine if we need to revalidate
2173 * any FBOs that might be rendering into this texture.
2174 * This flag never gets cleared since it's non-trivial to determine
2175 * when all FBOs might be done rendering to this texture. That's OK
2176 * though since it's uncommon to render to a texture then repeatedly
2177 * call glTexImage() to change images in the texture.
2178 */
2179 texObj->_RenderToTexture = GL_TRUE;
2180 }
2181 else {
2182 _mesa_remove_attachment(ctx, att);
2183 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2184 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2185 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2186 }
2187 }
2188
2189 invalidate_framebuffer(fb);
2190
2191 _glthread_UNLOCK_MUTEX(fb->Mutex);
2192 }
2193
2194
2195
2196 void GLAPIENTRY
2197 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2198 GLenum textarget, GLuint texture, GLint level)
2199 {
2200 GET_CURRENT_CONTEXT(ctx);
2201
2202 if (texture != 0) {
2203 GLboolean error;
2204
2205 switch (textarget) {
2206 case GL_TEXTURE_1D:
2207 error = GL_FALSE;
2208 break;
2209 case GL_TEXTURE_1D_ARRAY:
2210 error = !ctx->Extensions.EXT_texture_array;
2211 break;
2212 default:
2213 error = GL_TRUE;
2214 }
2215
2216 if (error) {
2217 _mesa_error(ctx, GL_INVALID_OPERATION,
2218 "glFramebufferTexture1DEXT(textarget=%s)",
2219 _mesa_lookup_enum_by_nr(textarget));
2220 return;
2221 }
2222 }
2223
2224 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2225 level, 0);
2226 }
2227
2228
2229 void GLAPIENTRY
2230 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2231 GLenum textarget, GLuint texture, GLint level)
2232 {
2233 GET_CURRENT_CONTEXT(ctx);
2234
2235 if (texture != 0) {
2236 GLboolean error;
2237
2238 switch (textarget) {
2239 case GL_TEXTURE_2D:
2240 error = GL_FALSE;
2241 break;
2242 case GL_TEXTURE_RECTANGLE:
2243 error = _mesa_is_gles(ctx)
2244 || !ctx->Extensions.NV_texture_rectangle;
2245 break;
2246 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2250 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2251 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2252 error = !ctx->Extensions.ARB_texture_cube_map;
2253 break;
2254 case GL_TEXTURE_2D_ARRAY:
2255 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2256 || !ctx->Extensions.EXT_texture_array;
2257 break;
2258 default:
2259 error = GL_TRUE;
2260 }
2261
2262 if (error) {
2263 _mesa_error(ctx, GL_INVALID_OPERATION,
2264 "glFramebufferTexture2DEXT(textarget=%s)",
2265 _mesa_lookup_enum_by_nr(textarget));
2266 return;
2267 }
2268 }
2269
2270 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2271 level, 0);
2272 }
2273
2274
2275 void GLAPIENTRY
2276 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2277 GLenum textarget, GLuint texture,
2278 GLint level, GLint zoffset)
2279 {
2280 GET_CURRENT_CONTEXT(ctx);
2281
2282 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2283 _mesa_error(ctx, GL_INVALID_OPERATION,
2284 "glFramebufferTexture3DEXT(textarget)");
2285 return;
2286 }
2287
2288 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2289 level, zoffset);
2290 }
2291
2292
2293 void GLAPIENTRY
2294 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2295 GLuint texture, GLint level, GLint layer)
2296 {
2297 GET_CURRENT_CONTEXT(ctx);
2298
2299 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2300 level, layer);
2301 }
2302
2303
2304 void GLAPIENTRY
2305 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2306 GLenum renderbufferTarget,
2307 GLuint renderbuffer)
2308 {
2309 struct gl_renderbuffer_attachment *att;
2310 struct gl_framebuffer *fb;
2311 struct gl_renderbuffer *rb;
2312 GET_CURRENT_CONTEXT(ctx);
2313
2314 fb = get_framebuffer_target(ctx, target);
2315 if (!fb) {
2316 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2317 return;
2318 }
2319
2320 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2321 _mesa_error(ctx, GL_INVALID_ENUM,
2322 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2323 return;
2324 }
2325
2326 if (_mesa_is_winsys_fbo(fb)) {
2327 /* Can't attach new renderbuffers to a window system framebuffer */
2328 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2329 return;
2330 }
2331
2332 att = _mesa_get_attachment(ctx, fb, attachment);
2333 if (att == NULL) {
2334 _mesa_error(ctx, GL_INVALID_ENUM,
2335 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2336 _mesa_lookup_enum_by_nr(attachment));
2337 return;
2338 }
2339
2340 if (renderbuffer) {
2341 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2342 if (!rb) {
2343 _mesa_error(ctx, GL_INVALID_OPERATION,
2344 "glFramebufferRenderbufferEXT(non-existant"
2345 " renderbuffer %u)", renderbuffer);
2346 return;
2347 }
2348 else if (rb == &DummyRenderbuffer) {
2349 /* This is what NVIDIA does */
2350 _mesa_error(ctx, GL_INVALID_VALUE,
2351 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2352 renderbuffer);
2353 return;
2354 }
2355 }
2356 else {
2357 /* remove renderbuffer attachment */
2358 rb = NULL;
2359 }
2360
2361 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2362 rb && rb->Format != MESA_FORMAT_NONE) {
2363 /* make sure the renderbuffer is a depth/stencil format */
2364 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2365 if (baseFormat != GL_DEPTH_STENCIL) {
2366 _mesa_error(ctx, GL_INVALID_OPERATION,
2367 "glFramebufferRenderbufferEXT(renderbuffer"
2368 " is not DEPTH_STENCIL format)");
2369 return;
2370 }
2371 }
2372
2373
2374 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2375
2376 assert(ctx->Driver.FramebufferRenderbuffer);
2377 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2378
2379 /* Some subsequent GL commands may depend on the framebuffer's visual
2380 * after the binding is updated. Update visual info now.
2381 */
2382 _mesa_update_framebuffer_visual(ctx, fb);
2383 }
2384
2385
2386 void GLAPIENTRY
2387 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2388 GLenum pname, GLint *params)
2389 {
2390 const struct gl_renderbuffer_attachment *att;
2391 struct gl_framebuffer *buffer;
2392 GLenum err;
2393 GET_CURRENT_CONTEXT(ctx);
2394
2395 /* The error differs in GL and GLES. */
2396 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2397
2398 buffer = get_framebuffer_target(ctx, target);
2399 if (!buffer) {
2400 _mesa_error(ctx, GL_INVALID_ENUM,
2401 "glGetFramebufferAttachmentParameterivEXT(target)");
2402 return;
2403 }
2404
2405 if (_mesa_is_winsys_fbo(buffer)) {
2406 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2407 * says:
2408 *
2409 * "If the framebuffer currently bound to target is zero, then
2410 * INVALID_OPERATION is generated."
2411 *
2412 * The EXT_framebuffer_object spec has the same wording, and the
2413 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2414 * spec.
2415 */
2416 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2417 && !_mesa_is_gles3(ctx)) {
2418 _mesa_error(ctx, GL_INVALID_OPERATION,
2419 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2420 return;
2421 }
2422
2423 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2424 attachment != GL_DEPTH && attachment != GL_STENCIL) {
2425 _mesa_error(ctx, GL_INVALID_OPERATION,
2426 "glGetFramebufferAttachmentParameteriv(attachment)");
2427 return;
2428 }
2429 /* the default / window-system FBO */
2430 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2431 }
2432 else {
2433 /* user-created framebuffer FBO */
2434 att = _mesa_get_attachment(ctx, buffer, attachment);
2435 }
2436
2437 if (att == NULL) {
2438 _mesa_error(ctx, GL_INVALID_ENUM,
2439 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2440 return;
2441 }
2442
2443 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2444 /* the depth and stencil attachments must point to the same buffer */
2445 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2446 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2447 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2448 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2449 _mesa_error(ctx, GL_INVALID_OPERATION,
2450 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2451 " attachments differ)");
2452 return;
2453 }
2454 }
2455
2456 /* No need to flush here */
2457
2458 switch (pname) {
2459 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2460 *params = _mesa_is_winsys_fbo(buffer)
2461 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2462 return;
2463 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2464 if (att->Type == GL_RENDERBUFFER_EXT) {
2465 *params = att->Renderbuffer->Name;
2466 }
2467 else if (att->Type == GL_TEXTURE) {
2468 *params = att->Texture->Name;
2469 }
2470 else {
2471 assert(att->Type == GL_NONE);
2472 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2473 *params = 0;
2474 } else {
2475 goto invalid_pname_enum;
2476 }
2477 }
2478 return;
2479 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2480 if (att->Type == GL_TEXTURE) {
2481 *params = att->TextureLevel;
2482 }
2483 else if (att->Type == GL_NONE) {
2484 _mesa_error(ctx, err,
2485 "glGetFramebufferAttachmentParameterivEXT(pname)");
2486 }
2487 else {
2488 goto invalid_pname_enum;
2489 }
2490 return;
2491 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2492 if (att->Type == GL_TEXTURE) {
2493 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2494 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2495 }
2496 else {
2497 *params = 0;
2498 }
2499 }
2500 else if (att->Type == GL_NONE) {
2501 _mesa_error(ctx, err,
2502 "glGetFramebufferAttachmentParameterivEXT(pname)");
2503 }
2504 else {
2505 goto invalid_pname_enum;
2506 }
2507 return;
2508 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2509 if (ctx->API == API_OPENGLES) {
2510 goto invalid_pname_enum;
2511 } else if (att->Type == GL_NONE) {
2512 _mesa_error(ctx, err,
2513 "glGetFramebufferAttachmentParameterivEXT(pname)");
2514 } else if (att->Type == GL_TEXTURE) {
2515 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2516 *params = att->Zoffset;
2517 }
2518 else {
2519 *params = 0;
2520 }
2521 }
2522 else {
2523 goto invalid_pname_enum;
2524 }
2525 return;
2526 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2527 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2528 && !_mesa_is_gles3(ctx)) {
2529 goto invalid_pname_enum;
2530 }
2531 else if (att->Type == GL_NONE) {
2532 _mesa_error(ctx, err,
2533 "glGetFramebufferAttachmentParameterivEXT(pname)");
2534 }
2535 else {
2536 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2537 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2538 }
2539 else {
2540 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2541 * if the sRGB conversion is unsupported. */
2542 *params = GL_LINEAR;
2543 }
2544 }
2545 return;
2546 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2547 if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2548 && ctx->API != API_OPENGL_CORE
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 gl_format format = att->Renderbuffer->Format;
2558
2559 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2560 * 3.0.1 spec says:
2561 *
2562 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2563 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2564 * generate an INVALID_OPERATION error.
2565 */
2566 if (_mesa_is_gles3(ctx) && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2567 _mesa_error(ctx, GL_INVALID_OPERATION,
2568 "glGetFramebufferAttachmentParameteriv(cannot query "
2569 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2570 "GL_DEPTH_STENCIL_ATTACHMENT");
2571 return;
2572 }
2573
2574 if (format == MESA_FORMAT_S8) {
2575 /* special cases */
2576 *params = GL_INDEX;
2577 }
2578 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2579 /* depends on the attachment parameter */
2580 if (attachment == GL_STENCIL_ATTACHMENT) {
2581 *params = GL_INDEX;
2582 }
2583 else {
2584 *params = GL_FLOAT;
2585 }
2586 }
2587 else {
2588 *params = _mesa_get_format_datatype(format);
2589 }
2590 }
2591 return;
2592 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2593 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2594 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2595 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2596 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2597 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2598 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2599 && !_mesa_is_gles3(ctx)) {
2600 goto invalid_pname_enum;
2601 }
2602 else if (att->Type == GL_NONE) {
2603 _mesa_error(ctx, err,
2604 "glGetFramebufferAttachmentParameterivEXT(pname)");
2605 }
2606 else if (att->Texture) {
2607 const struct gl_texture_image *texImage =
2608 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2609 att->TextureLevel);
2610 if (texImage) {
2611 *params = get_component_bits(pname, texImage->_BaseFormat,
2612 texImage->TexFormat);
2613 }
2614 else {
2615 *params = 0;
2616 }
2617 }
2618 else if (att->Renderbuffer) {
2619 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2620 att->Renderbuffer->Format);
2621 }
2622 else {
2623 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2624 " invalid FBO attachment structure");
2625 }
2626 return;
2627 default:
2628 goto invalid_pname_enum;
2629 }
2630
2631 return;
2632
2633 invalid_pname_enum:
2634 _mesa_error(ctx, GL_INVALID_ENUM,
2635 "glGetFramebufferAttachmentParameteriv(pname)");
2636 return;
2637 }
2638
2639
2640 void GLAPIENTRY
2641 _mesa_GenerateMipmap(GLenum target)
2642 {
2643 struct gl_texture_image *srcImage;
2644 struct gl_texture_object *texObj;
2645 GLboolean error;
2646
2647 GET_CURRENT_CONTEXT(ctx);
2648
2649 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2650
2651 switch (target) {
2652 case GL_TEXTURE_1D:
2653 error = _mesa_is_gles(ctx);
2654 break;
2655 case GL_TEXTURE_2D:
2656 error = GL_FALSE;
2657 break;
2658 case GL_TEXTURE_3D:
2659 error = ctx->API == API_OPENGLES;
2660 break;
2661 case GL_TEXTURE_CUBE_MAP:
2662 error = !ctx->Extensions.ARB_texture_cube_map;
2663 break;
2664 case GL_TEXTURE_1D_ARRAY:
2665 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2666 break;
2667 case GL_TEXTURE_2D_ARRAY:
2668 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2669 || !ctx->Extensions.EXT_texture_array;
2670 break;
2671 default:
2672 error = GL_TRUE;
2673 }
2674
2675 if (error) {
2676 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2677 _mesa_lookup_enum_by_nr(target));
2678 return;
2679 }
2680
2681 texObj = _mesa_get_current_tex_object(ctx, target);
2682
2683 if (texObj->BaseLevel >= texObj->MaxLevel) {
2684 /* nothing to do */
2685 return;
2686 }
2687
2688 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2689 !_mesa_cube_complete(texObj)) {
2690 _mesa_error(ctx, GL_INVALID_OPERATION,
2691 "glGenerateMipmap(incomplete cube map)");
2692 return;
2693 }
2694
2695 _mesa_lock_texture(ctx, texObj);
2696
2697 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2698 if (!srcImage) {
2699 _mesa_unlock_texture(ctx, texObj);
2700 _mesa_error(ctx, GL_INVALID_OPERATION,
2701 "glGenerateMipmap(zero size base image)");
2702 return;
2703 }
2704
2705 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2706 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2707 _mesa_is_stencil_format(srcImage->InternalFormat)) {
2708 _mesa_unlock_texture(ctx, texObj);
2709 _mesa_error(ctx, GL_INVALID_OPERATION,
2710 "glGenerateMipmap(invalid internal format)");
2711 return;
2712 }
2713
2714 if (target == GL_TEXTURE_CUBE_MAP) {
2715 GLuint face;
2716 for (face = 0; face < 6; face++)
2717 ctx->Driver.GenerateMipmap(ctx,
2718 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2719 texObj);
2720 }
2721 else {
2722 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2723 }
2724 _mesa_unlock_texture(ctx, texObj);
2725 }
2726
2727
2728 static const struct gl_renderbuffer_attachment *
2729 find_attachment(const struct gl_framebuffer *fb,
2730 const struct gl_renderbuffer *rb)
2731 {
2732 GLuint i;
2733 for (i = 0; i < Elements(fb->Attachment); i++) {
2734 if (fb->Attachment[i].Renderbuffer == rb)
2735 return &fb->Attachment[i];
2736 }
2737 return NULL;
2738 }
2739
2740
2741 /**
2742 * Helper function for checking if the datatypes of color buffers are
2743 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2744 *
2745 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2746 * and any of the following conditions hold:
2747 * - The read buffer contains fixed-point or floating-point values and any
2748 * draw buffer contains neither fixed-point nor floating-point values.
2749 * - The read buffer contains unsigned integer values and any draw buffer
2750 * does not contain unsigned integer values.
2751 * - The read buffer contains signed integer values and any draw buffer
2752 * does not contain signed integer values."
2753 */
2754 static GLboolean
2755 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2756 {
2757 GLenum srcType = _mesa_get_format_datatype(srcFormat);
2758 GLenum dstType = _mesa_get_format_datatype(dstFormat);
2759
2760 if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2761 assert(srcType == GL_UNSIGNED_NORMALIZED ||
2762 srcType == GL_SIGNED_NORMALIZED ||
2763 srcType == GL_FLOAT);
2764 /* Boil any of those types down to GL_FLOAT */
2765 srcType = GL_FLOAT;
2766 }
2767
2768 if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2769 assert(dstType == GL_UNSIGNED_NORMALIZED ||
2770 dstType == GL_SIGNED_NORMALIZED ||
2771 dstType == GL_FLOAT);
2772 /* Boil any of those types down to GL_FLOAT */
2773 dstType = GL_FLOAT;
2774 }
2775
2776 return srcType == dstType;
2777 }
2778
2779
2780 static GLboolean
2781 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2782 const struct gl_renderbuffer *drawRb)
2783 {
2784 GLenum readFormat, drawFormat;
2785
2786 /* The simple case where we know the backing Mesa formats are the same.
2787 */
2788 if (_mesa_get_srgb_format_linear(readRb->Format) ==
2789 _mesa_get_srgb_format_linear(drawRb->Format)) {
2790 return GL_TRUE;
2791 }
2792
2793 /* The Mesa formats are different, so we must check whether the internal
2794 * formats are compatible.
2795 *
2796 * Under some circumstances, the user may request e.g. two GL_RGBA8
2797 * textures and get two entirely different Mesa formats like RGBA8888 and
2798 * ARGB8888. Drivers behaving like that should be able to cope with
2799 * non-matching formats by themselves, because it's not the user's fault.
2800 *
2801 * Blits between linear and sRGB formats are also allowed.
2802 */
2803 readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2804 drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2805 readFormat = _mesa_get_linear_internalformat(readFormat);
2806 drawFormat = _mesa_get_linear_internalformat(drawFormat);
2807
2808 if (readFormat == drawFormat) {
2809 return GL_TRUE;
2810 }
2811
2812 return GL_FALSE;
2813 }
2814
2815
2816 /**
2817 * Blit rectangular region, optionally from one framebuffer to another.
2818 *
2819 * Note, if the src buffer is multisampled and the dest is not, this is
2820 * when the samples must be resolved to a single color.
2821 */
2822 void GLAPIENTRY
2823 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2824 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2825 GLbitfield mask, GLenum filter)
2826 {
2827 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2828 GL_DEPTH_BUFFER_BIT |
2829 GL_STENCIL_BUFFER_BIT);
2830 const struct gl_framebuffer *readFb, *drawFb;
2831 GET_CURRENT_CONTEXT(ctx);
2832
2833 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2834
2835 if (MESA_VERBOSE & VERBOSE_API)
2836 _mesa_debug(ctx,
2837 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
2838 srcX0, srcY0, srcX1, srcY1,
2839 dstX0, dstY0, dstX1, dstY1,
2840 mask, _mesa_lookup_enum_by_nr(filter));
2841
2842 if (ctx->NewState) {
2843 _mesa_update_state(ctx);
2844 }
2845
2846 readFb = ctx->ReadBuffer;
2847 drawFb = ctx->DrawBuffer;
2848
2849 if (!readFb || !drawFb) {
2850 /* This will normally never happen but someday we may want to
2851 * support MakeCurrent() with no drawables.
2852 */
2853 return;
2854 }
2855
2856 /* check for complete framebuffers */
2857 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2858 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2859 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2860 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2861 return;
2862 }
2863
2864 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2865 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2866 return;
2867 }
2868
2869 if (mask & ~legalMaskBits) {
2870 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2871 return;
2872 }
2873
2874 /* depth/stencil must be blitted with nearest filtering */
2875 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2876 && filter != GL_NEAREST) {
2877 _mesa_error(ctx, GL_INVALID_OPERATION,
2878 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2879 return;
2880 }
2881
2882 /* get color read/draw renderbuffers */
2883 if (mask & GL_COLOR_BUFFER_BIT) {
2884 const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2885 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
2886 const struct gl_renderbuffer *colorDrawRb = NULL;
2887 GLuint i;
2888
2889 /* From the EXT_framebuffer_object spec:
2890 *
2891 * "If a buffer is specified in <mask> and does not exist in both
2892 * the read and draw framebuffers, the corresponding bit is silently
2893 * ignored."
2894 */
2895 if (!colorReadRb || numColorDrawBuffers == 0) {
2896 mask &= ~GL_COLOR_BUFFER_BIT;
2897 }
2898 else {
2899 for (i = 0; i < numColorDrawBuffers; i++) {
2900 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2901 if (!colorDrawRb)
2902 continue;
2903
2904 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
2905 * ES 3.0.1 spec says:
2906 *
2907 * "If the source and destination buffers are identical, an
2908 * INVALID_OPERATION error is generated. Different mipmap
2909 * levels of a texture, different layers of a three-
2910 * dimensional texture or two-dimensional array texture, and
2911 * different faces of a cube map texture do not constitute
2912 * identical buffers."
2913 */
2914 if (_mesa_is_gles3(ctx) && (colorDrawRb == colorReadRb)) {
2915 _mesa_error(ctx, GL_INVALID_OPERATION,
2916 "glBlitFramebuffer(source and destination color "
2917 "buffer cannot be the same)");
2918 return;
2919 }
2920
2921 if (!compatible_color_datatypes(colorReadRb->Format,
2922 colorDrawRb->Format)) {
2923 _mesa_error(ctx, GL_INVALID_OPERATION,
2924 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2925 return;
2926 }
2927 /* extra checks for multisample copies... */
2928 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2929 /* color formats must match */
2930 if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2931 _mesa_error(ctx, GL_INVALID_OPERATION,
2932 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2933 return;
2934 }
2935 }
2936 }
2937 if (filter == GL_LINEAR) {
2938 /* 3.1 spec, page 199:
2939 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2940 * if filter is LINEAR and read buffer contains integer data."
2941 */
2942 GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2943 if (type == GL_INT || type == GL_UNSIGNED_INT) {
2944 _mesa_error(ctx, GL_INVALID_OPERATION,
2945 "glBlitFramebufferEXT(integer color type)");
2946 return;
2947 }
2948 }
2949 }
2950 }
2951
2952 if (mask & GL_STENCIL_BUFFER_BIT) {
2953 struct gl_renderbuffer *readRb =
2954 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2955 struct gl_renderbuffer *drawRb =
2956 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2957
2958 /* From the EXT_framebuffer_object spec:
2959 *
2960 * "If a buffer is specified in <mask> and does not exist in both
2961 * the read and draw framebuffers, the corresponding bit is silently
2962 * ignored."
2963 */
2964 if ((readRb == NULL) || (drawRb == NULL)) {
2965 mask &= ~GL_STENCIL_BUFFER_BIT;
2966 }
2967 else {
2968 int read_z_bits, draw_z_bits;
2969
2970 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
2971 _mesa_error(ctx, GL_INVALID_OPERATION,
2972 "glBlitFramebuffer(source and destination stencil "
2973 "buffer cannot be the same)");
2974 return;
2975 }
2976
2977 if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2978 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2979 /* There is no need to check the stencil datatype here, because
2980 * there is only one: GL_UNSIGNED_INT.
2981 */
2982 _mesa_error(ctx, GL_INVALID_OPERATION,
2983 "glBlitFramebuffer(stencil attachment format mismatch)");
2984 return;
2985 }
2986
2987 read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
2988 draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
2989
2990 /* If both buffers also have depth data, the depth formats must match
2991 * as well. If one doesn't have depth, it's not blitted, so we should
2992 * ignore the depth format check.
2993 */
2994 if (read_z_bits > 0 && draw_z_bits > 0 &&
2995 (read_z_bits != draw_z_bits ||
2996 _mesa_get_format_datatype(readRb->Format) !=
2997 _mesa_get_format_datatype(drawRb->Format))) {
2998
2999 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3000 "(stencil attachment depth format mismatch)");
3001 return;
3002 }
3003 }
3004 }
3005
3006 if (mask & GL_DEPTH_BUFFER_BIT) {
3007 struct gl_renderbuffer *readRb =
3008 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3009 struct gl_renderbuffer *drawRb =
3010 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3011
3012 /* From the EXT_framebuffer_object spec:
3013 *
3014 * "If a buffer is specified in <mask> and does not exist in both
3015 * the read and draw framebuffers, the corresponding bit is silently
3016 * ignored."
3017 */
3018 if ((readRb == NULL) || (drawRb == NULL)) {
3019 mask &= ~GL_DEPTH_BUFFER_BIT;
3020 }
3021 else {
3022 int read_s_bit, draw_s_bit;
3023
3024 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
3025 _mesa_error(ctx, GL_INVALID_OPERATION,
3026 "glBlitFramebuffer(source and destination depth "
3027 "buffer cannot be the same)");
3028 return;
3029 }
3030
3031 if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
3032 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
3033 (_mesa_get_format_datatype(readRb->Format) !=
3034 _mesa_get_format_datatype(drawRb->Format))) {
3035 _mesa_error(ctx, GL_INVALID_OPERATION,
3036 "glBlitFramebuffer(depth attachment format mismatch)");
3037 return;
3038 }
3039
3040 read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
3041 draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
3042
3043 /* If both buffers also have stencil data, the stencil formats must
3044 * match as well. If one doesn't have stencil, it's not blitted, so
3045 * we should ignore the stencil format check.
3046 */
3047 if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
3048 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3049 "(depth attachment stencil bits mismatch)");
3050 return;
3051 }
3052 }
3053 }
3054
3055
3056 if (_mesa_is_gles3(ctx)) {
3057 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3058 * 3.0.1 spec says:
3059 *
3060 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
3061 * an INVALID_OPERATION error is generated."
3062 */
3063 if (drawFb->Visual.samples > 0) {
3064 _mesa_error(ctx, GL_INVALID_OPERATION,
3065 "glBlitFramebuffer(destination samples must be 0)");
3066 return;
3067 }
3068
3069 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3070 * 3.0.1 spec says:
3071 *
3072 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
3073 * no copy is performed and an INVALID_OPERATION error is generated
3074 * if the formats of the read and draw framebuffers are not
3075 * identical or if the source and destination rectangles are not
3076 * defined with the same (X0, Y0) and (X1, Y1) bounds."
3077 *
3078 * The format check was made above because desktop OpenGL has the same
3079 * requirement.
3080 */
3081 if (readFb->Visual.samples > 0
3082 && (srcX0 != dstX0 || srcY0 != dstY0
3083 || srcX1 != dstX1 || srcY1 != dstY1)) {
3084 _mesa_error(ctx, GL_INVALID_OPERATION,
3085 "glBlitFramebuffer(bad src/dst multisample region)");
3086 return;
3087 }
3088 } else {
3089 if (readFb->Visual.samples > 0 &&
3090 drawFb->Visual.samples > 0 &&
3091 readFb->Visual.samples != drawFb->Visual.samples) {
3092 _mesa_error(ctx, GL_INVALID_OPERATION,
3093 "glBlitFramebufferEXT(mismatched samples)");
3094 return;
3095 }
3096
3097 /* extra checks for multisample copies... */
3098 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
3099 /* src and dest region sizes must be the same */
3100 if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
3101 abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3102 _mesa_error(ctx, GL_INVALID_OPERATION,
3103 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3104 return;
3105 }
3106 }
3107 }
3108
3109 if (!ctx->Extensions.EXT_framebuffer_blit) {
3110 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3111 return;
3112 }
3113
3114 /* Debug code */
3115 if (DEBUG_BLIT) {
3116 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3117 const struct gl_renderbuffer *colorDrawRb = NULL;
3118 GLuint i = 0;
3119
3120 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3121 " 0x%x, 0x%x)\n",
3122 srcX0, srcY0, srcX1, srcY1,
3123 dstX0, dstY0, dstX1, dstY1,
3124 mask, filter);
3125 if (colorReadRb) {
3126 const struct gl_renderbuffer_attachment *att;
3127
3128 att = find_attachment(readFb, colorReadRb);
3129 printf(" Src FBO %u RB %u (%dx%d) ",
3130 readFb->Name, colorReadRb->Name,
3131 colorReadRb->Width, colorReadRb->Height);
3132 if (att && att->Texture) {
3133 printf("Tex %u tgt 0x%x level %u face %u",
3134 att->Texture->Name,
3135 att->Texture->Target,
3136 att->TextureLevel,
3137 att->CubeMapFace);
3138 }
3139 printf("\n");
3140
3141 /* Print all active color render buffers */
3142 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3143 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3144 if (!colorDrawRb)
3145 continue;
3146
3147 att = find_attachment(drawFb, colorDrawRb);
3148 printf(" Dst FBO %u RB %u (%dx%d) ",
3149 drawFb->Name, colorDrawRb->Name,
3150 colorDrawRb->Width, colorDrawRb->Height);
3151 if (att && att->Texture) {
3152 printf("Tex %u tgt 0x%x level %u face %u",
3153 att->Texture->Name,
3154 att->Texture->Target,
3155 att->TextureLevel,
3156 att->CubeMapFace);
3157 }
3158 printf("\n");
3159 }
3160 }
3161 }
3162
3163 if (!mask) {
3164 return;
3165 }
3166
3167 ASSERT(ctx->Driver.BlitFramebuffer);
3168 ctx->Driver.BlitFramebuffer(ctx,
3169 srcX0, srcY0, srcX1, srcY1,
3170 dstX0, dstY0, dstX1, dstY1,
3171 mask, filter);
3172 }
3173
3174
3175 static void
3176 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3177 const GLenum *attachments, GLint x, GLint y,
3178 GLsizei width, GLsizei height, const char *name)
3179 {
3180 int i;
3181 struct gl_framebuffer *fb;
3182 GET_CURRENT_CONTEXT(ctx);
3183
3184 fb = get_framebuffer_target(ctx, target);
3185 if (!fb) {
3186 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3187 return;
3188 }
3189
3190 if (numAttachments < 0) {
3191 _mesa_error(ctx, GL_INVALID_VALUE,
3192 "%s(numAttachments < 0)", name);
3193 return;
3194 }
3195
3196 /* The GL_ARB_invalidate_subdata spec says:
3197 *
3198 * "If an attachment is specified that does not exist in the
3199 * framebuffer bound to <target>, it is ignored."
3200 *
3201 * It also says:
3202 *
3203 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3204 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3205 * INVALID_OPERATION is generated."
3206 *
3207 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3208 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3209 * set of retrictions).
3210 */
3211 for (i = 0; i < numAttachments; i++) {
3212 if (_mesa_is_winsys_fbo(fb)) {
3213 switch (attachments[i]) {
3214 case GL_ACCUM:
3215 case GL_AUX0:
3216 case GL_AUX1:
3217 case GL_AUX2:
3218 case GL_AUX3:
3219 /* Accumulation buffers and auxilary buffers were removed in
3220 * OpenGL 3.1, and they never existed in OpenGL ES.
3221 */
3222 if (ctx->API != API_OPENGL_COMPAT)
3223 goto invalid_enum;
3224 break;
3225 case GL_COLOR:
3226 case GL_DEPTH:
3227 case GL_STENCIL:
3228 break;
3229 case GL_BACK_LEFT:
3230 case GL_BACK_RIGHT:
3231 case GL_FRONT_LEFT:
3232 case GL_FRONT_RIGHT:
3233 if (!_mesa_is_desktop_gl(ctx))
3234 goto invalid_enum;
3235 break;
3236 default:
3237 goto invalid_enum;
3238 }
3239 } else {
3240 switch (attachments[i]) {
3241 case GL_DEPTH_ATTACHMENT:
3242 case GL_STENCIL_ATTACHMENT:
3243 break;
3244 case GL_COLOR_ATTACHMENT0:
3245 case GL_COLOR_ATTACHMENT1:
3246 case GL_COLOR_ATTACHMENT2:
3247 case GL_COLOR_ATTACHMENT3:
3248 case GL_COLOR_ATTACHMENT4:
3249 case GL_COLOR_ATTACHMENT5:
3250 case GL_COLOR_ATTACHMENT6:
3251 case GL_COLOR_ATTACHMENT7:
3252 case GL_COLOR_ATTACHMENT8:
3253 case GL_COLOR_ATTACHMENT9:
3254 case GL_COLOR_ATTACHMENT10:
3255 case GL_COLOR_ATTACHMENT11:
3256 case GL_COLOR_ATTACHMENT12:
3257 case GL_COLOR_ATTACHMENT13:
3258 case GL_COLOR_ATTACHMENT14:
3259 case GL_COLOR_ATTACHMENT15: {
3260 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3261 if (k >= ctx->Const.MaxColorAttachments) {
3262 _mesa_error(ctx, GL_INVALID_OPERATION,
3263 "%s(attachment >= max. color attachments)", name);
3264 return;
3265 }
3266 }
3267 default:
3268 goto invalid_enum;
3269 }
3270 }
3271 }
3272
3273 /* We don't actually do anything for this yet. Just return after
3274 * validating the parameters and generating the required errors.
3275 */
3276 return;
3277
3278 invalid_enum:
3279 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3280 return;
3281 }
3282
3283 void GLAPIENTRY
3284 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3285 const GLenum *attachments, GLint x, GLint y,
3286 GLsizei width, GLsizei height)
3287 {
3288 invalidate_framebuffer_storage(target, numAttachments, attachments,
3289 x, y, width, height,
3290 "glInvalidateSubFramebuffer");
3291 }
3292
3293 void GLAPIENTRY
3294 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3295 const GLenum *attachments)
3296 {
3297 /* The GL_ARB_invalidate_subdata spec says:
3298 *
3299 * "The command
3300 *
3301 * void InvalidateFramebuffer(enum target,
3302 * sizei numAttachments,
3303 * const enum *attachments);
3304 *
3305 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3306 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3307 * <MAX_VIEWPORT_DIMS[1]> respectively."
3308 */
3309 invalidate_framebuffer_storage(target, numAttachments, attachments,
3310 0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3311 "glInvalidateFramebuffer");
3312 }