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