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