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