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