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