mesa/fbobject: Update CubeMapFace when reusing textures
[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 /**
54 * Notes:
55 *
56 * None of the GL_EXT_framebuffer_object functions are compiled into
57 * display lists.
58 */
59
60
61
62 /*
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
67 */
68 static struct gl_framebuffer DummyFramebuffer;
69 static struct gl_renderbuffer DummyRenderbuffer;
70
71 /* We bind this framebuffer when applications pass a NULL
72 * drawable/surface in make current. */
73 static struct gl_framebuffer IncompleteFramebuffer;
74
75
76 static void
77 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
78 {
79 /* no op */
80 }
81
82 static void
83 delete_dummy_framebuffer(struct gl_framebuffer *fb)
84 {
85 /* no op */
86 }
87
88
89 void
90 _mesa_init_fbobjects(struct gl_context *ctx)
91 {
92 mtx_init(&DummyFramebuffer.Mutex, mtx_plain);
93 mtx_init(&DummyRenderbuffer.Mutex, mtx_plain);
94 mtx_init(&IncompleteFramebuffer.Mutex, mtx_plain);
95 DummyFramebuffer.Delete = delete_dummy_framebuffer;
96 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
97 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
98 }
99
100 struct gl_framebuffer *
101 _mesa_get_incomplete_framebuffer(void)
102 {
103 return &IncompleteFramebuffer;
104 }
105
106 /**
107 * Helper routine for getting a gl_renderbuffer.
108 */
109 struct gl_renderbuffer *
110 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
111 {
112 struct gl_renderbuffer *rb;
113
114 if (id == 0)
115 return NULL;
116
117 rb = (struct gl_renderbuffer *)
118 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
119 return rb;
120 }
121
122
123 /**
124 * A convenience function for direct state access that throws
125 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
126 */
127 struct gl_renderbuffer *
128 _mesa_lookup_renderbuffer_err(struct gl_context *ctx, GLuint id,
129 const char *func)
130 {
131 struct gl_renderbuffer *rb;
132
133 rb = _mesa_lookup_renderbuffer(ctx, id);
134 if (!rb || rb == &DummyRenderbuffer) {
135 _mesa_error(ctx, GL_INVALID_OPERATION,
136 "%s(non-existent renderbuffer %u)", func, id);
137 return NULL;
138 }
139
140 return rb;
141 }
142
143
144 /**
145 * Helper routine for getting a gl_framebuffer.
146 */
147 struct gl_framebuffer *
148 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
149 {
150 struct gl_framebuffer *fb;
151
152 if (id == 0)
153 return NULL;
154
155 fb = (struct gl_framebuffer *)
156 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
157 return fb;
158 }
159
160
161 /**
162 * A convenience function for direct state access that throws
163 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
164 */
165 struct gl_framebuffer *
166 _mesa_lookup_framebuffer_err(struct gl_context *ctx, GLuint id,
167 const char *func)
168 {
169 struct gl_framebuffer *fb;
170
171 fb = _mesa_lookup_framebuffer(ctx, id);
172 if (!fb || fb == &DummyFramebuffer) {
173 _mesa_error(ctx, GL_INVALID_OPERATION,
174 "%s(non-existent framebuffer %u)", func, id);
175 return NULL;
176 }
177
178 return fb;
179 }
180
181
182 /**
183 * Mark the given framebuffer as invalid. This will force the
184 * test for framebuffer completeness to be done before the framebuffer
185 * is used.
186 */
187 static void
188 invalidate_framebuffer(struct gl_framebuffer *fb)
189 {
190 fb->_Status = 0; /* "indeterminate" */
191 }
192
193
194 /**
195 * Return the gl_framebuffer object which corresponds to the given
196 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
197 * Check support for GL_EXT_framebuffer_blit to determine if certain
198 * targets are legal.
199 * \return gl_framebuffer pointer or NULL if target is illegal
200 */
201 static struct gl_framebuffer *
202 get_framebuffer_target(struct gl_context *ctx, GLenum target)
203 {
204 bool have_fb_blit = _mesa_is_gles3(ctx) || _mesa_is_desktop_gl(ctx);
205 switch (target) {
206 case GL_DRAW_FRAMEBUFFER:
207 return have_fb_blit ? ctx->DrawBuffer : NULL;
208 case GL_READ_FRAMEBUFFER:
209 return have_fb_blit ? ctx->ReadBuffer : NULL;
210 case GL_FRAMEBUFFER_EXT:
211 return ctx->DrawBuffer;
212 default:
213 return NULL;
214 }
215 }
216
217
218 /**
219 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
220 * gl_renderbuffer_attachment object.
221 * This function is only used for user-created FB objects, not the
222 * default / window-system FB object.
223 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
224 * the depth buffer attachment point.
225 */
226 static struct gl_renderbuffer_attachment *
227 get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
228 GLenum attachment)
229 {
230 GLuint i;
231
232 assert(_mesa_is_user_fbo(fb));
233
234 switch (attachment) {
235 case GL_COLOR_ATTACHMENT0_EXT:
236 case GL_COLOR_ATTACHMENT1_EXT:
237 case GL_COLOR_ATTACHMENT2_EXT:
238 case GL_COLOR_ATTACHMENT3_EXT:
239 case GL_COLOR_ATTACHMENT4_EXT:
240 case GL_COLOR_ATTACHMENT5_EXT:
241 case GL_COLOR_ATTACHMENT6_EXT:
242 case GL_COLOR_ATTACHMENT7_EXT:
243 case GL_COLOR_ATTACHMENT8_EXT:
244 case GL_COLOR_ATTACHMENT9_EXT:
245 case GL_COLOR_ATTACHMENT10_EXT:
246 case GL_COLOR_ATTACHMENT11_EXT:
247 case GL_COLOR_ATTACHMENT12_EXT:
248 case GL_COLOR_ATTACHMENT13_EXT:
249 case GL_COLOR_ATTACHMENT14_EXT:
250 case GL_COLOR_ATTACHMENT15_EXT:
251 /* Only OpenGL ES 1.x forbids color attachments other than
252 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
253 * hardware is used.
254 */
255 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
256 if (i >= ctx->Const.MaxColorAttachments
257 || (i > 0 && ctx->API == API_OPENGLES)) {
258 return NULL;
259 }
260 return &fb->Attachment[BUFFER_COLOR0 + i];
261 case GL_DEPTH_STENCIL_ATTACHMENT:
262 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
263 return NULL;
264 /* fall-through */
265 case GL_DEPTH_ATTACHMENT_EXT:
266 return &fb->Attachment[BUFFER_DEPTH];
267 case GL_STENCIL_ATTACHMENT_EXT:
268 return &fb->Attachment[BUFFER_STENCIL];
269 default:
270 return NULL;
271 }
272 }
273
274
275 /**
276 * As above, but only used for getting attachments of the default /
277 * window-system framebuffer (not user-created framebuffer objects).
278 */
279 static struct gl_renderbuffer_attachment *
280 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
281 GLenum attachment)
282 {
283 assert(_mesa_is_winsys_fbo(fb));
284
285 if (_mesa_is_gles3(ctx)) {
286 assert(attachment == GL_BACK ||
287 attachment == GL_DEPTH ||
288 attachment == GL_STENCIL);
289 switch (attachment) {
290 case GL_BACK:
291 /* Since there is no stereo rendering in ES 3.0, only return the
292 * LEFT bits.
293 */
294 if (ctx->DrawBuffer->Visual.doubleBufferMode)
295 return &fb->Attachment[BUFFER_BACK_LEFT];
296 return &fb->Attachment[BUFFER_FRONT_LEFT];
297 case GL_DEPTH:
298 return &fb->Attachment[BUFFER_DEPTH];
299 case GL_STENCIL:
300 return &fb->Attachment[BUFFER_STENCIL];
301 }
302 }
303
304 switch (attachment) {
305 case GL_FRONT_LEFT:
306 /* Front buffers can be allocated on the first use, but
307 * glGetFramebufferAttachmentParameteriv must work even if that
308 * allocation hasn't happened yet. In such case, use the back buffer,
309 * which should be the same.
310 */
311 if (fb->Attachment[BUFFER_FRONT_LEFT].Type == GL_NONE)
312 return &fb->Attachment[BUFFER_BACK_LEFT];
313 else
314 return &fb->Attachment[BUFFER_FRONT_LEFT];
315 case GL_FRONT_RIGHT:
316 /* Same as above. */
317 if (fb->Attachment[BUFFER_FRONT_RIGHT].Type == GL_NONE)
318 return &fb->Attachment[BUFFER_BACK_RIGHT];
319 else
320 return &fb->Attachment[BUFFER_FRONT_RIGHT];
321 case GL_BACK_LEFT:
322 return &fb->Attachment[BUFFER_BACK_LEFT];
323 case GL_BACK_RIGHT:
324 return &fb->Attachment[BUFFER_BACK_RIGHT];
325 case GL_AUX0:
326 if (fb->Visual.numAuxBuffers == 1) {
327 return &fb->Attachment[BUFFER_AUX0];
328 }
329 return NULL;
330
331 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
332 *
333 * "If the default framebuffer is bound to target, then attachment must
334 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
335 * identifying a color buffer; DEPTH, identifying the depth buffer; or
336 * STENCIL, identifying the stencil buffer."
337 *
338 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
339 * language. However, revision #33 of the ARB_framebuffer_object spec
340 * says:
341 *
342 * "If the default framebuffer is bound to <target>, then <attachment>
343 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
344 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
345 * depth buffer, or the stencil buffer, and <pname> may be
346 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
347 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
348 *
349 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
350 * from glext.h, so shipping apps should not use those values.
351 *
352 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
353 * support queries of the window system FBO.
354 */
355 case GL_DEPTH:
356 return &fb->Attachment[BUFFER_DEPTH];
357 case GL_STENCIL:
358 return &fb->Attachment[BUFFER_STENCIL];
359 default:
360 return NULL;
361 }
362 }
363
364
365
366 /**
367 * Remove any texture or renderbuffer attached to the given attachment
368 * point. Update reference counts, etc.
369 */
370 static void
371 remove_attachment(struct gl_context *ctx,
372 struct gl_renderbuffer_attachment *att)
373 {
374 struct gl_renderbuffer *rb = att->Renderbuffer;
375
376 /* tell driver that we're done rendering to this texture. */
377 if (rb && rb->NeedsFinishRenderTexture)
378 ctx->Driver.FinishRenderTexture(ctx, rb);
379
380 if (att->Type == GL_TEXTURE) {
381 assert(att->Texture);
382 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
383 assert(!att->Texture);
384 }
385 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
386 assert(!att->Texture);
387 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
388 assert(!att->Renderbuffer);
389 }
390 att->Type = GL_NONE;
391 att->Complete = GL_TRUE;
392 }
393
394 /**
395 * Verify a couple error conditions that will lead to an incomplete FBO and
396 * may cause problems for the driver's RenderTexture path.
397 */
398 static bool
399 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment *att)
400 {
401 const struct gl_texture_image *const texImage =
402 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
403
404 if (!texImage ||
405 texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
406 return false;
407
408 if ((texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY
409 && att->Zoffset >= texImage->Height)
410 || (texImage->TexObject->Target != GL_TEXTURE_1D_ARRAY
411 && att->Zoffset >= texImage->Depth))
412 return false;
413
414 return true;
415 }
416
417 /**
418 * Create a renderbuffer which will be set up by the driver to wrap the
419 * texture image slice.
420 *
421 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
422 * to share most of their framebuffer rendering code between winsys,
423 * renderbuffer, and texture attachments.
424 *
425 * The allocated renderbuffer uses a non-zero Name so that drivers can check
426 * it for determining vertical orientation, but we use ~0 to make it fairly
427 * unambiguous with actual user (non-texture) renderbuffers.
428 */
429 void
430 _mesa_update_texture_renderbuffer(struct gl_context *ctx,
431 struct gl_framebuffer *fb,
432 struct gl_renderbuffer_attachment *att)
433 {
434 struct gl_texture_image *texImage;
435 struct gl_renderbuffer *rb;
436
437 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
438
439 rb = att->Renderbuffer;
440 if (!rb) {
441 rb = ctx->Driver.NewRenderbuffer(ctx, ~0);
442 if (!rb) {
443 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
444 return;
445 }
446 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
447
448 /* This can't get called on a texture renderbuffer, so set it to NULL
449 * for clarity compared to user renderbuffers.
450 */
451 rb->AllocStorage = NULL;
452
453 rb->NeedsFinishRenderTexture = ctx->Driver.FinishRenderTexture != NULL;
454 }
455
456 if (!texImage)
457 return;
458
459 rb->_BaseFormat = texImage->_BaseFormat;
460 rb->Format = texImage->TexFormat;
461 rb->InternalFormat = texImage->InternalFormat;
462 rb->Width = texImage->Width2;
463 rb->Height = texImage->Height2;
464 rb->Depth = texImage->Depth2;
465 rb->NumSamples = texImage->NumSamples;
466 rb->TexImage = texImage;
467
468 if (driver_RenderTexture_is_safe(att))
469 ctx->Driver.RenderTexture(ctx, fb, att);
470 }
471
472 /**
473 * Bind a texture object to an attachment point.
474 * The previous binding, if any, will be removed first.
475 */
476 static void
477 set_texture_attachment(struct gl_context *ctx,
478 struct gl_framebuffer *fb,
479 struct gl_renderbuffer_attachment *att,
480 struct gl_texture_object *texObj,
481 GLenum texTarget, GLuint level, GLuint layer,
482 GLboolean layered)
483 {
484 struct gl_renderbuffer *rb = att->Renderbuffer;
485
486 if (rb && rb->NeedsFinishRenderTexture)
487 ctx->Driver.FinishRenderTexture(ctx, rb);
488
489 if (att->Texture == texObj) {
490 /* re-attaching same texture */
491 assert(att->Type == GL_TEXTURE);
492 }
493 else {
494 /* new attachment */
495 remove_attachment(ctx, att);
496 att->Type = GL_TEXTURE;
497 assert(!att->Texture);
498 _mesa_reference_texobj(&att->Texture, texObj);
499 }
500 invalidate_framebuffer(fb);
501
502 /* always update these fields */
503 att->TextureLevel = level;
504 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
505 att->Zoffset = layer;
506 att->Layered = layered;
507 att->Complete = GL_FALSE;
508
509 _mesa_update_texture_renderbuffer(ctx, fb, att);
510 }
511
512
513 /**
514 * Bind a renderbuffer to an attachment point.
515 * The previous binding, if any, will be removed first.
516 */
517 static void
518 set_renderbuffer_attachment(struct gl_context *ctx,
519 struct gl_renderbuffer_attachment *att,
520 struct gl_renderbuffer *rb)
521 {
522 /* XXX check if re-doing same attachment, exit early */
523 remove_attachment(ctx, att);
524 att->Type = GL_RENDERBUFFER_EXT;
525 att->Texture = NULL; /* just to be safe */
526 att->Layered = GL_FALSE;
527 att->Complete = GL_FALSE;
528 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
529 }
530
531
532 /**
533 * Fallback for ctx->Driver.FramebufferRenderbuffer()
534 * Attach a renderbuffer object to a framebuffer object.
535 */
536 void
537 _mesa_FramebufferRenderbuffer_sw(struct gl_context *ctx,
538 struct gl_framebuffer *fb,
539 GLenum attachment,
540 struct gl_renderbuffer *rb)
541 {
542 struct gl_renderbuffer_attachment *att;
543
544 mtx_lock(&fb->Mutex);
545
546 att = get_attachment(ctx, fb, attachment);
547 assert(att);
548 if (rb) {
549 set_renderbuffer_attachment(ctx, att, rb);
550 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
551 /* do stencil attachment here (depth already done above) */
552 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
553 assert(att);
554 set_renderbuffer_attachment(ctx, att, rb);
555 }
556 rb->AttachedAnytime = GL_TRUE;
557 }
558 else {
559 remove_attachment(ctx, att);
560 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
561 /* detach stencil (depth was detached above) */
562 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
563 assert(att);
564 remove_attachment(ctx, att);
565 }
566 }
567
568 invalidate_framebuffer(fb);
569
570 mtx_unlock(&fb->Mutex);
571 }
572
573
574 /**
575 * Fallback for ctx->Driver.ValidateFramebuffer()
576 * Check if the renderbuffer's formats are supported by the software
577 * renderer.
578 * Drivers should probably override this.
579 */
580 void
581 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
582 {
583 gl_buffer_index buf;
584 for (buf = 0; buf < BUFFER_COUNT; buf++) {
585 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
586 if (rb) {
587 switch (rb->_BaseFormat) {
588 case GL_ALPHA:
589 case GL_LUMINANCE_ALPHA:
590 case GL_LUMINANCE:
591 case GL_INTENSITY:
592 case GL_RED:
593 case GL_RG:
594 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
595 return;
596
597 default:
598 switch (rb->Format) {
599 /* XXX This list is likely incomplete. */
600 case MESA_FORMAT_R9G9B9E5_FLOAT:
601 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
602 return;
603 default:;
604 /* render buffer format is supported by software rendering */
605 }
606 }
607 }
608 }
609 }
610
611
612 /**
613 * Return true if the framebuffer has a combined depth/stencil
614 * renderbuffer attached.
615 */
616 GLboolean
617 _mesa_has_depthstencil_combined(const struct gl_framebuffer *fb)
618 {
619 const struct gl_renderbuffer_attachment *depth =
620 &fb->Attachment[BUFFER_DEPTH];
621 const struct gl_renderbuffer_attachment *stencil =
622 &fb->Attachment[BUFFER_STENCIL];
623
624 if (depth->Type == stencil->Type) {
625 if (depth->Type == GL_RENDERBUFFER_EXT &&
626 depth->Renderbuffer == stencil->Renderbuffer)
627 return GL_TRUE;
628
629 if (depth->Type == GL_TEXTURE &&
630 depth->Texture == stencil->Texture)
631 return GL_TRUE;
632 }
633
634 return GL_FALSE;
635 }
636
637
638 /**
639 * For debug only.
640 */
641 static void
642 att_incomplete(const char *msg)
643 {
644 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
645 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
646 }
647 }
648
649
650 /**
651 * For debug only.
652 */
653 static void
654 fbo_incomplete(struct gl_context *ctx, const char *msg, int index)
655 {
656 static GLuint msg_id;
657
658 _mesa_gl_debug(ctx, &msg_id,
659 MESA_DEBUG_SOURCE_API,
660 MESA_DEBUG_TYPE_OTHER,
661 MESA_DEBUG_SEVERITY_MEDIUM,
662 "FBO incomplete: %s [%d]\n", msg, index);
663
664 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
665 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
666 }
667 }
668
669
670 /**
671 * Is the given base format a legal format for a color renderbuffer?
672 */
673 GLboolean
674 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
675 {
676 switch (baseFormat) {
677 case GL_RGB:
678 case GL_RGBA:
679 return GL_TRUE;
680 case GL_LUMINANCE:
681 case GL_LUMINANCE_ALPHA:
682 case GL_INTENSITY:
683 case GL_ALPHA:
684 return ctx->API == API_OPENGL_COMPAT &&
685 ctx->Extensions.ARB_framebuffer_object;
686 case GL_RED:
687 case GL_RG:
688 return ctx->Extensions.ARB_texture_rg;
689 default:
690 return GL_FALSE;
691 }
692 }
693
694
695 /**
696 * Is the given base format a legal format for a color renderbuffer?
697 */
698 static GLboolean
699 is_format_color_renderable(const struct gl_context *ctx, mesa_format format,
700 GLenum internalFormat)
701 {
702 const GLenum baseFormat =
703 _mesa_get_format_base_format(format);
704 GLboolean valid;
705
706 valid = _mesa_is_legal_color_format(ctx, baseFormat);
707 if (!valid || _mesa_is_desktop_gl(ctx)) {
708 return valid;
709 }
710
711 /* Reject additional cases for GLES */
712 switch (internalFormat) {
713 case GL_RGBA8_SNORM:
714 case GL_RGB32F:
715 case GL_RGB32I:
716 case GL_RGB32UI:
717 case GL_RGB16F:
718 case GL_RGB16I:
719 case GL_RGB16UI:
720 case GL_RGB8_SNORM:
721 case GL_RGB8I:
722 case GL_RGB8UI:
723 case GL_SRGB8:
724 case GL_RGB9_E5:
725 case GL_RG8_SNORM:
726 case GL_R8_SNORM:
727 return GL_FALSE;
728 default:
729 break;
730 }
731
732 if (format == MESA_FORMAT_B10G10R10A2_UNORM &&
733 internalFormat != GL_RGB10_A2) {
734 return GL_FALSE;
735 }
736
737 return GL_TRUE;
738 }
739
740
741 /**
742 * Is the given base format a legal format for a depth/stencil renderbuffer?
743 */
744 static GLboolean
745 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
746 {
747 switch (baseFormat) {
748 case GL_DEPTH_COMPONENT:
749 case GL_DEPTH_STENCIL_EXT:
750 return GL_TRUE;
751 default:
752 return GL_FALSE;
753 }
754 }
755
756
757 /**
758 * Test if an attachment point is complete and update its Complete field.
759 * \param format if GL_COLOR, this is a color attachment point,
760 * if GL_DEPTH, this is a depth component attachment point,
761 * if GL_STENCIL, this is a stencil component attachment point.
762 */
763 static void
764 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
765 struct gl_renderbuffer_attachment *att)
766 {
767 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
768
769 /* assume complete */
770 att->Complete = GL_TRUE;
771
772 /* Look for reasons why the attachment might be incomplete */
773 if (att->Type == GL_TEXTURE) {
774 const struct gl_texture_object *texObj = att->Texture;
775 struct gl_texture_image *texImage;
776 GLenum baseFormat;
777
778 if (!texObj) {
779 att_incomplete("no texobj");
780 att->Complete = GL_FALSE;
781 return;
782 }
783
784 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
785 if (!texImage) {
786 att_incomplete("no teximage");
787 att->Complete = GL_FALSE;
788 return;
789 }
790 if (texImage->Width < 1 || texImage->Height < 1) {
791 att_incomplete("teximage width/height=0");
792 att->Complete = GL_FALSE;
793 return;
794 }
795
796 switch (texObj->Target) {
797 case GL_TEXTURE_3D:
798 if (att->Zoffset >= texImage->Depth) {
799 att_incomplete("bad z offset");
800 att->Complete = GL_FALSE;
801 return;
802 }
803 break;
804 case GL_TEXTURE_1D_ARRAY:
805 if (att->Zoffset >= texImage->Height) {
806 att_incomplete("bad 1D-array layer");
807 att->Complete = GL_FALSE;
808 return;
809 }
810 break;
811 case GL_TEXTURE_2D_ARRAY:
812 if (att->Zoffset >= texImage->Depth) {
813 att_incomplete("bad 2D-array layer");
814 att->Complete = GL_FALSE;
815 return;
816 }
817 break;
818 case GL_TEXTURE_CUBE_MAP_ARRAY:
819 if (att->Zoffset >= texImage->Depth) {
820 att_incomplete("bad cube-array layer");
821 att->Complete = GL_FALSE;
822 return;
823 }
824 break;
825 }
826
827 baseFormat = texImage->_BaseFormat;
828
829 if (format == GL_COLOR) {
830 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
831 att_incomplete("bad format");
832 att->Complete = GL_FALSE;
833 return;
834 }
835 if (_mesa_is_format_compressed(texImage->TexFormat)) {
836 att_incomplete("compressed internalformat");
837 att->Complete = GL_FALSE;
838 return;
839 }
840
841 /* OES_texture_float allows creation and use of floating point
842 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
843 * these textures to be used as a render target, this is done via
844 * GL_EXT_color_buffer(_half)_float with set of new sized types.
845 */
846 if (_mesa_is_gles(ctx) && (texImage->TexObject->_IsFloat ||
847 texImage->TexObject->_IsHalfFloat)) {
848 att_incomplete("bad internal format");
849 att->Complete = GL_FALSE;
850 return;
851 }
852 }
853 else if (format == GL_DEPTH) {
854 if (baseFormat == GL_DEPTH_COMPONENT) {
855 /* OK */
856 }
857 else if (ctx->Extensions.ARB_depth_texture &&
858 baseFormat == GL_DEPTH_STENCIL) {
859 /* OK */
860 }
861 else {
862 att->Complete = GL_FALSE;
863 att_incomplete("bad depth format");
864 return;
865 }
866 }
867 else {
868 assert(format == GL_STENCIL);
869 if (ctx->Extensions.ARB_depth_texture &&
870 baseFormat == GL_DEPTH_STENCIL) {
871 /* OK */
872 } else if (ctx->Extensions.ARB_texture_stencil8 &&
873 baseFormat == GL_STENCIL_INDEX) {
874 /* OK */
875 } else {
876 /* no such thing as stencil-only textures */
877 att_incomplete("illegal stencil texture");
878 att->Complete = GL_FALSE;
879 return;
880 }
881 }
882 }
883 else if (att->Type == GL_RENDERBUFFER_EXT) {
884 const GLenum baseFormat = att->Renderbuffer->_BaseFormat;
885
886 assert(att->Renderbuffer);
887 if (!att->Renderbuffer->InternalFormat ||
888 att->Renderbuffer->Width < 1 ||
889 att->Renderbuffer->Height < 1) {
890 att_incomplete("0x0 renderbuffer");
891 att->Complete = GL_FALSE;
892 return;
893 }
894 if (format == GL_COLOR) {
895 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
896 att_incomplete("bad renderbuffer color format");
897 att->Complete = GL_FALSE;
898 return;
899 }
900 }
901 else if (format == GL_DEPTH) {
902 if (baseFormat == GL_DEPTH_COMPONENT) {
903 /* OK */
904 }
905 else if (baseFormat == GL_DEPTH_STENCIL) {
906 /* OK */
907 }
908 else {
909 att_incomplete("bad renderbuffer depth format");
910 att->Complete = GL_FALSE;
911 return;
912 }
913 }
914 else {
915 assert(format == GL_STENCIL);
916 if (baseFormat == GL_STENCIL_INDEX ||
917 baseFormat == GL_DEPTH_STENCIL) {
918 /* OK */
919 }
920 else {
921 att->Complete = GL_FALSE;
922 att_incomplete("bad renderbuffer stencil format");
923 return;
924 }
925 }
926 }
927 else {
928 assert(att->Type == GL_NONE);
929 /* complete */
930 return;
931 }
932 }
933
934
935 /**
936 * Test if the given framebuffer object is complete and update its
937 * Status field with the results.
938 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
939 * driver to make hardware-specific validation/completeness checks.
940 * Also update the framebuffer's Width and Height fields if the
941 * framebuffer is complete.
942 */
943 void
944 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
945 struct gl_framebuffer *fb)
946 {
947 GLuint numImages;
948 GLenum intFormat = GL_NONE; /* color buffers' internal format */
949 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
950 GLint numSamples = -1;
951 GLint fixedSampleLocations = -1;
952 GLint i;
953 GLuint j;
954 /* Covers max_layer_count, is_layered, and layer_tex_target */
955 bool layer_info_valid = false;
956 GLuint max_layer_count = 0, att_layer_count;
957 bool is_layered = false;
958 GLenum layer_tex_target = 0;
959 bool has_depth_attachment = false;
960 bool has_stencil_attachment = false;
961
962 assert(_mesa_is_user_fbo(fb));
963
964 /* we're changing framebuffer fields here */
965 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
966
967 numImages = 0;
968 fb->Width = 0;
969 fb->Height = 0;
970 fb->_AllColorBuffersFixedPoint = GL_TRUE;
971 fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
972 fb->_HasAttachments = true;
973 fb->_IntegerBuffers = 0;
974
975 /* Start at -2 to more easily loop over all attachment points.
976 * -2: depth buffer
977 * -1: stencil buffer
978 * >=0: color buffer
979 */
980 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
981 struct gl_renderbuffer_attachment *att;
982 GLenum f;
983 mesa_format attFormat;
984 GLenum att_tex_target = GL_NONE;
985
986 /*
987 * XXX for ARB_fbo, only check color buffers that are named by
988 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
989 */
990
991 /* check for attachment completeness
992 */
993 if (i == -2) {
994 att = &fb->Attachment[BUFFER_DEPTH];
995 test_attachment_completeness(ctx, GL_DEPTH, att);
996 if (!att->Complete) {
997 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
998 fbo_incomplete(ctx, "depth attachment incomplete", -1);
999 return;
1000 } else if (att->Type != GL_NONE) {
1001 has_depth_attachment = true;
1002 }
1003 }
1004 else if (i == -1) {
1005 att = &fb->Attachment[BUFFER_STENCIL];
1006 test_attachment_completeness(ctx, GL_STENCIL, att);
1007 if (!att->Complete) {
1008 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
1009 fbo_incomplete(ctx, "stencil attachment incomplete", -1);
1010 return;
1011 } else if (att->Type != GL_NONE) {
1012 has_stencil_attachment = true;
1013 }
1014 }
1015 else {
1016 att = &fb->Attachment[BUFFER_COLOR0 + i];
1017 test_attachment_completeness(ctx, GL_COLOR, att);
1018 if (!att->Complete) {
1019 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
1020 fbo_incomplete(ctx, "color attachment incomplete", i);
1021 return;
1022 }
1023 }
1024
1025 /* get width, height, format of the renderbuffer/texture
1026 */
1027 if (att->Type == GL_TEXTURE) {
1028 const struct gl_texture_image *texImg = att->Renderbuffer->TexImage;
1029 att_tex_target = att->Texture->Target;
1030 minWidth = MIN2(minWidth, texImg->Width);
1031 maxWidth = MAX2(maxWidth, texImg->Width);
1032 minHeight = MIN2(minHeight, texImg->Height);
1033 maxHeight = MAX2(maxHeight, texImg->Height);
1034 f = texImg->_BaseFormat;
1035 attFormat = texImg->TexFormat;
1036 numImages++;
1037
1038 if (!is_format_color_renderable(ctx, attFormat,
1039 texImg->InternalFormat) &&
1040 !is_legal_depth_format(ctx, f) &&
1041 f != GL_STENCIL_INDEX) {
1042 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1043 fbo_incomplete(ctx, "texture attachment incomplete", -1);
1044 return;
1045 }
1046
1047 if (numSamples < 0)
1048 numSamples = texImg->NumSamples;
1049 else if (numSamples != texImg->NumSamples) {
1050 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1051 fbo_incomplete(ctx, "inconsistent sample count", -1);
1052 return;
1053 }
1054
1055 if (fixedSampleLocations < 0)
1056 fixedSampleLocations = texImg->FixedSampleLocations;
1057 else if (fixedSampleLocations != texImg->FixedSampleLocations) {
1058 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1059 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
1060 return;
1061 }
1062 }
1063 else if (att->Type == GL_RENDERBUFFER_EXT) {
1064 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
1065 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
1066 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
1067 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
1068 f = att->Renderbuffer->InternalFormat;
1069 attFormat = att->Renderbuffer->Format;
1070 numImages++;
1071
1072 if (numSamples < 0)
1073 numSamples = att->Renderbuffer->NumSamples;
1074 else if (numSamples != att->Renderbuffer->NumSamples) {
1075 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1076 fbo_incomplete(ctx, "inconsistent sample count", -1);
1077 return;
1078 }
1079
1080 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1081 if (fixedSampleLocations < 0)
1082 fixedSampleLocations = GL_TRUE;
1083 else if (fixedSampleLocations != GL_TRUE) {
1084 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1085 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
1086 return;
1087 }
1088 }
1089 else {
1090 assert(att->Type == GL_NONE);
1091 continue;
1092 }
1093
1094 /* Update flags describing color buffer datatypes */
1095 if (i >= 0) {
1096 GLenum type = _mesa_get_format_datatype(attFormat);
1097
1098 /* check if integer color */
1099 if (_mesa_is_format_integer_color(attFormat))
1100 fb->_IntegerBuffers |= (1 << i);
1101
1102 fb->_AllColorBuffersFixedPoint =
1103 fb->_AllColorBuffersFixedPoint &&
1104 (type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED);
1105
1106 fb->_HasSNormOrFloatColorBuffer =
1107 fb->_HasSNormOrFloatColorBuffer ||
1108 type == GL_SIGNED_NORMALIZED || type == GL_FLOAT;
1109 }
1110
1111 /* Error-check width, height, format */
1112 if (numImages == 1) {
1113 /* save format */
1114 if (i >= 0) {
1115 intFormat = f;
1116 }
1117 }
1118 else {
1119 if (!ctx->Extensions.ARB_framebuffer_object) {
1120 /* check that width, height, format are same */
1121 if (minWidth != maxWidth || minHeight != maxHeight) {
1122 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
1123 fbo_incomplete(ctx, "width or height mismatch", -1);
1124 return;
1125 }
1126 /* check that all color buffers are the same format */
1127 if (intFormat != GL_NONE && f != intFormat) {
1128 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
1129 fbo_incomplete(ctx, "format mismatch", -1);
1130 return;
1131 }
1132 }
1133 }
1134
1135 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1136 */
1137 if (att->Type == GL_RENDERBUFFER &&
1138 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
1139 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1140 fbo_incomplete(ctx, "unsupported renderbuffer format", i);
1141 return;
1142 }
1143
1144 /* Check that layered rendering is consistent. */
1145 if (att->Layered) {
1146 if (att_tex_target == GL_TEXTURE_CUBE_MAP)
1147 att_layer_count = 6;
1148 else if (att_tex_target == GL_TEXTURE_1D_ARRAY)
1149 att_layer_count = att->Renderbuffer->Height;
1150 else
1151 att_layer_count = att->Renderbuffer->Depth;
1152 } else {
1153 att_layer_count = 0;
1154 }
1155 if (!layer_info_valid) {
1156 is_layered = att->Layered;
1157 max_layer_count = att_layer_count;
1158 layer_tex_target = att_tex_target;
1159 layer_info_valid = true;
1160 } else if (max_layer_count > 0 && layer_tex_target != att_tex_target) {
1161 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1162 fbo_incomplete(ctx, "layered framebuffer has mismatched targets", i);
1163 return;
1164 } else if (is_layered != att->Layered) {
1165 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1166 fbo_incomplete(ctx,
1167 "framebuffer attachment layer mode is inconsistent",
1168 i);
1169 return;
1170 } else if (att_layer_count > max_layer_count) {
1171 max_layer_count = att_layer_count;
1172 }
1173
1174 /*
1175 * The extension GL_ARB_framebuffer_no_attachments places additional
1176 * requirement on each attachment. Those additional requirements are
1177 * tighter that those of previous versions of GL. In interest of better
1178 * compatibility, we will not enforce these restrictions. For the record
1179 * those additional restrictions are quoted below:
1180 *
1181 * "The width and height of image are greater than zero and less than or
1182 * equal to the values of the implementation-dependent limits
1183 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1184 *
1185 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1186 * array texture and the attachment is layered, the depth or layer count
1187 * of the texture is less than or equal to the implementation-dependent
1188 * limit MAX_FRAMEBUFFER_LAYERS."
1189 *
1190 * "If image has multiple samples, its sample count is less than or equal
1191 * to the value of the implementation-dependent limit
1192 * MAX_FRAMEBUFFER_SAMPLES."
1193 *
1194 * The same requirements are also in place for GL 4.5,
1195 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1196 */
1197 }
1198
1199 fb->MaxNumLayers = max_layer_count;
1200
1201 if (numImages == 0) {
1202 fb->_HasAttachments = false;
1203
1204 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1205 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1206 fbo_incomplete(ctx, "no attachments", -1);
1207 return;
1208 }
1209
1210 if (fb->DefaultGeometry.Width == 0 || fb->DefaultGeometry.Height == 0) {
1211 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1212 fbo_incomplete(ctx, "no attachments and default width or height is 0", -1);
1213 return;
1214 }
1215 }
1216
1217 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
1218 /* Check that all DrawBuffers are present */
1219 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
1220 if (fb->ColorDrawBuffer[j] != GL_NONE) {
1221 const struct gl_renderbuffer_attachment *att
1222 = get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
1223 assert(att);
1224 if (att->Type == GL_NONE) {
1225 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
1226 fbo_incomplete(ctx, "missing drawbuffer", j);
1227 return;
1228 }
1229 }
1230 }
1231
1232 /* Check that the ReadBuffer is present */
1233 if (fb->ColorReadBuffer != GL_NONE) {
1234 const struct gl_renderbuffer_attachment *att
1235 = get_attachment(ctx, fb, fb->ColorReadBuffer);
1236 assert(att);
1237 if (att->Type == GL_NONE) {
1238 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
1239 fbo_incomplete(ctx, "missing readbuffer", -1);
1240 return;
1241 }
1242 }
1243 }
1244
1245 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1246 *
1247 * "Depth and stencil attachments, if present, are the same image."
1248 *
1249 * This restriction is not present in the OpenGL ES2 spec.
1250 */
1251 if (_mesa_is_gles3(ctx) &&
1252 has_stencil_attachment && has_depth_attachment &&
1253 !_mesa_has_depthstencil_combined(fb)) {
1254 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1255 fbo_incomplete(ctx, "Depth and stencil attachments must be the same image", -1);
1256 return;
1257 }
1258
1259 /* Provisionally set status = COMPLETE ... */
1260 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
1261
1262 /* ... but the driver may say the FB is incomplete.
1263 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1264 * if anything.
1265 */
1266 if (ctx->Driver.ValidateFramebuffer) {
1267 ctx->Driver.ValidateFramebuffer(ctx, fb);
1268 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1269 fbo_incomplete(ctx, "driver marked FBO as incomplete", -1);
1270 return;
1271 }
1272 }
1273
1274 /*
1275 * Note that if ARB_framebuffer_object is supported and the attached
1276 * renderbuffers/textures are different sizes, the framebuffer
1277 * width/height will be set to the smallest width/height.
1278 */
1279 if (numImages != 0) {
1280 fb->Width = minWidth;
1281 fb->Height = minHeight;
1282 }
1283
1284 /* finally, update the visual info for the framebuffer */
1285 _mesa_update_framebuffer_visual(ctx, fb);
1286 }
1287
1288
1289 GLboolean GLAPIENTRY
1290 _mesa_IsRenderbuffer(GLuint renderbuffer)
1291 {
1292 GET_CURRENT_CONTEXT(ctx);
1293 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1294 if (renderbuffer) {
1295 struct gl_renderbuffer *rb =
1296 _mesa_lookup_renderbuffer(ctx, renderbuffer);
1297 if (rb != NULL && rb != &DummyRenderbuffer)
1298 return GL_TRUE;
1299 }
1300 return GL_FALSE;
1301 }
1302
1303
1304 static struct gl_renderbuffer *
1305 allocate_renderbuffer_locked(struct gl_context *ctx, GLuint renderbuffer,
1306 const char *func)
1307 {
1308 struct gl_renderbuffer *newRb;
1309
1310 /* create new renderbuffer object */
1311 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
1312 if (!newRb) {
1313 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1314 return NULL;
1315 }
1316 assert(newRb->AllocStorage);
1317 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1318 newRb->RefCount = 1; /* referenced by hash table */
1319
1320 return newRb;
1321 }
1322
1323
1324 static void
1325 bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
1326 {
1327 struct gl_renderbuffer *newRb;
1328 GET_CURRENT_CONTEXT(ctx);
1329
1330 if (target != GL_RENDERBUFFER_EXT) {
1331 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
1332 return;
1333 }
1334
1335 /* No need to flush here since the render buffer binding has no
1336 * effect on rendering state.
1337 */
1338
1339 if (renderbuffer) {
1340 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1341 if (newRb == &DummyRenderbuffer) {
1342 /* ID was reserved, but no real renderbuffer object made yet */
1343 newRb = NULL;
1344 }
1345 else if (!newRb && !allow_user_names) {
1346 /* All RB IDs must be Gen'd */
1347 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1348 return;
1349 }
1350
1351 if (!newRb) {
1352 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1353 newRb = allocate_renderbuffer_locked(ctx, renderbuffer,
1354 "glBindRenderbufferEXT");
1355 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
1356 }
1357 }
1358 else {
1359 newRb = NULL;
1360 }
1361
1362 assert(newRb != &DummyRenderbuffer);
1363
1364 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1365 }
1366
1367 void GLAPIENTRY
1368 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1369 {
1370 GET_CURRENT_CONTEXT(ctx);
1371
1372 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1373 * entry point, but they allow the use of user-generated names.
1374 */
1375 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1376 }
1377
1378 void GLAPIENTRY
1379 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1380 {
1381 /* This function should not be in the dispatch table for core profile /
1382 * OpenGL 3.1, so execution should never get here in those cases -- no
1383 * need for an explicit test.
1384 */
1385 bind_renderbuffer(target, renderbuffer, true);
1386 }
1387
1388 /**
1389 * ARB_framebuffer_no_attachment - Application passes requested param's
1390 * here. NOTE: NumSamples requested need not be _NumSamples which is
1391 * what the hw supports.
1392 */
1393 static void
1394 framebuffer_parameteri(struct gl_context *ctx, struct gl_framebuffer *fb,
1395 GLenum pname, GLint param, const char *func)
1396 {
1397 switch (pname) {
1398 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1399 if (param < 0 || param > ctx->Const.MaxFramebufferWidth)
1400 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1401 else
1402 fb->DefaultGeometry.Width = param;
1403 break;
1404 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1405 if (param < 0 || param > ctx->Const.MaxFramebufferHeight)
1406 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1407 else
1408 fb->DefaultGeometry.Height = param;
1409 break;
1410 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1411 /*
1412 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1413 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1414 */
1415 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
1416 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1417 break;
1418 }
1419 if (param < 0 || param > ctx->Const.MaxFramebufferLayers)
1420 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1421 else
1422 fb->DefaultGeometry.Layers = param;
1423 break;
1424 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1425 if (param < 0 || param > ctx->Const.MaxFramebufferSamples)
1426 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1427 else
1428 fb->DefaultGeometry.NumSamples = param;
1429 break;
1430 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1431 fb->DefaultGeometry.FixedSampleLocations = param;
1432 break;
1433 default:
1434 _mesa_error(ctx, GL_INVALID_ENUM,
1435 "%s(pname=0x%x)", func, pname);
1436 }
1437
1438 invalidate_framebuffer(fb);
1439 ctx->NewState |= _NEW_BUFFERS;
1440 }
1441
1442 void GLAPIENTRY
1443 _mesa_FramebufferParameteri(GLenum target, GLenum pname, GLint param)
1444 {
1445 GET_CURRENT_CONTEXT(ctx);
1446 struct gl_framebuffer *fb;
1447
1448 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1449 _mesa_error(ctx, GL_INVALID_OPERATION,
1450 "glFramebufferParameteriv not supported "
1451 "(ARB_framebuffer_no_attachments not implemented)");
1452 return;
1453 }
1454
1455 fb = get_framebuffer_target(ctx, target);
1456 if (!fb) {
1457 _mesa_error(ctx, GL_INVALID_ENUM,
1458 "glFramebufferParameteri(target=0x%x)", target);
1459 return;
1460 }
1461
1462 /* check framebuffer binding */
1463 if (_mesa_is_winsys_fbo(fb)) {
1464 _mesa_error(ctx, GL_INVALID_OPERATION,
1465 "glFramebufferParameteri");
1466 return;
1467 }
1468
1469 framebuffer_parameteri(ctx, fb, pname, param, "glFramebufferParameteri");
1470 }
1471
1472 static void
1473 get_framebuffer_parameteriv(struct gl_context *ctx, struct gl_framebuffer *fb,
1474 GLenum pname, GLint *params, const char *func)
1475 {
1476 switch (pname) {
1477 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1478 *params = fb->DefaultGeometry.Width;
1479 break;
1480 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1481 *params = fb->DefaultGeometry.Height;
1482 break;
1483 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1484 /*
1485 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1486 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1487 */
1488 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
1489 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1490 break;
1491 }
1492 *params = fb->DefaultGeometry.Layers;
1493 break;
1494 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1495 *params = fb->DefaultGeometry.NumSamples;
1496 break;
1497 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1498 *params = fb->DefaultGeometry.FixedSampleLocations;
1499 break;
1500 default:
1501 _mesa_error(ctx, GL_INVALID_ENUM,
1502 "%s(pname=0x%x)", func, pname);
1503 }
1504 }
1505
1506 void GLAPIENTRY
1507 _mesa_GetFramebufferParameteriv(GLenum target, GLenum pname, GLint *params)
1508 {
1509 GET_CURRENT_CONTEXT(ctx);
1510 struct gl_framebuffer *fb;
1511
1512 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1513 _mesa_error(ctx, GL_INVALID_OPERATION,
1514 "glGetFramebufferParameteriv not supported "
1515 "(ARB_framebuffer_no_attachments not implemented)");
1516 return;
1517 }
1518
1519 fb = get_framebuffer_target(ctx, target);
1520 if (!fb) {
1521 _mesa_error(ctx, GL_INVALID_ENUM,
1522 "glGetFramebufferParameteriv(target=0x%x)", target);
1523 return;
1524 }
1525
1526 /* check framebuffer binding */
1527 if (_mesa_is_winsys_fbo(fb)) {
1528 _mesa_error(ctx, GL_INVALID_OPERATION,
1529 "glGetFramebufferParameteriv");
1530 return;
1531 }
1532
1533 get_framebuffer_parameteriv(ctx, fb, pname, params,
1534 "glGetFramebufferParameteriv");
1535 }
1536
1537
1538 /**
1539 * Remove the specified renderbuffer or texture from any attachment point in
1540 * the framebuffer.
1541 *
1542 * \returns
1543 * \c true if the renderbuffer was detached from an attachment point. \c
1544 * false otherwise.
1545 */
1546 bool
1547 _mesa_detach_renderbuffer(struct gl_context *ctx,
1548 struct gl_framebuffer *fb,
1549 const void *att)
1550 {
1551 unsigned i;
1552 bool progress = false;
1553
1554 for (i = 0; i < BUFFER_COUNT; i++) {
1555 if (fb->Attachment[i].Texture == att
1556 || fb->Attachment[i].Renderbuffer == att) {
1557 remove_attachment(ctx, &fb->Attachment[i]);
1558 progress = true;
1559 }
1560 }
1561
1562 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1563 * Completeness," of the OpenGL 3.1 spec says:
1564 *
1565 * "Performing any of the following actions may change whether the
1566 * framebuffer is considered complete or incomplete:
1567 *
1568 * ...
1569 *
1570 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1571 * containing an image that is attached to a framebuffer object
1572 * that is bound to the framebuffer."
1573 */
1574 if (progress)
1575 invalidate_framebuffer(fb);
1576
1577 return progress;
1578 }
1579
1580
1581 void GLAPIENTRY
1582 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1583 {
1584 GLint i;
1585 GET_CURRENT_CONTEXT(ctx);
1586
1587 if (n < 0) {
1588 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1589 return;
1590 }
1591
1592 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1593
1594 for (i = 0; i < n; i++) {
1595 if (renderbuffers[i] > 0) {
1596 struct gl_renderbuffer *rb;
1597 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1598 if (rb) {
1599 /* check if deleting currently bound renderbuffer object */
1600 if (rb == ctx->CurrentRenderbuffer) {
1601 /* bind default */
1602 assert(rb->RefCount >= 2);
1603 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1604 }
1605
1606 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1607 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1608 * of the OpenGL 3.1 spec says:
1609 *
1610 * "If a renderbuffer object is deleted while its image is
1611 * attached to one or more attachment points in the currently
1612 * bound framebuffer, then it is as if FramebufferRenderbuffer
1613 * had been called, with a renderbuffer of 0, for each
1614 * attachment point to which this image was attached in the
1615 * currently bound framebuffer. In other words, this
1616 * renderbuffer image is first detached from all attachment
1617 * points in the currently bound framebuffer. Note that the
1618 * renderbuffer image is specifically not detached from any
1619 * non-bound framebuffers. Detaching the image from any
1620 * non-bound framebuffers is the responsibility of the
1621 * application.
1622 */
1623 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1624 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1625 }
1626 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1627 && ctx->ReadBuffer != ctx->DrawBuffer) {
1628 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1629 }
1630
1631 /* Remove from hash table immediately, to free the ID.
1632 * But the object will not be freed until it's no longer
1633 * referenced anywhere else.
1634 */
1635 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1636
1637 if (rb != &DummyRenderbuffer) {
1638 /* no longer referenced by hash table */
1639 _mesa_reference_renderbuffer(&rb, NULL);
1640 }
1641 }
1642 }
1643 }
1644 }
1645
1646 static void
1647 create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1648 bool dsa)
1649 {
1650 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
1651 GLuint first;
1652 GLint i;
1653
1654 if (n < 0) {
1655 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
1656 return;
1657 }
1658
1659 if (!renderbuffers)
1660 return;
1661
1662 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1663
1664 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1665
1666 for (i = 0; i < n; i++) {
1667 GLuint name = first + i;
1668 renderbuffers[i] = name;
1669
1670 if (dsa) {
1671 allocate_renderbuffer_locked(ctx, name, func);
1672 } else {
1673 /* insert a dummy renderbuffer into the hash table */
1674 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, name,
1675 &DummyRenderbuffer);
1676 }
1677 }
1678
1679 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
1680 }
1681
1682
1683 void GLAPIENTRY
1684 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1685 {
1686 GET_CURRENT_CONTEXT(ctx);
1687 create_render_buffers(ctx, n, renderbuffers, false);
1688 }
1689
1690
1691 void GLAPIENTRY
1692 _mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1693 {
1694 GET_CURRENT_CONTEXT(ctx);
1695 create_render_buffers(ctx, n, renderbuffers, true);
1696 }
1697
1698
1699 /**
1700 * Given an internal format token for a render buffer, return the
1701 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1702 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1703 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1704 *
1705 * This is similar to _mesa_base_tex_format() but the set of valid
1706 * internal formats is different.
1707 *
1708 * Note that even if a format is determined to be legal here, validation
1709 * of the FBO may fail if the format is not supported by the driver/GPU.
1710 *
1711 * \param internalFormat as passed to glRenderbufferStorage()
1712 * \return the base internal format, or 0 if internalFormat is illegal
1713 */
1714 GLenum
1715 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1716 {
1717 /*
1718 * Notes: some formats such as alpha, luminance, etc. were added
1719 * with GL_ARB_framebuffer_object.
1720 */
1721 switch (internalFormat) {
1722 case GL_ALPHA:
1723 case GL_ALPHA4:
1724 case GL_ALPHA8:
1725 case GL_ALPHA12:
1726 case GL_ALPHA16:
1727 return (ctx->API == API_OPENGL_COMPAT &&
1728 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
1729 case GL_LUMINANCE:
1730 case GL_LUMINANCE4:
1731 case GL_LUMINANCE8:
1732 case GL_LUMINANCE12:
1733 case GL_LUMINANCE16:
1734 return (ctx->API == API_OPENGL_COMPAT &&
1735 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
1736 case GL_LUMINANCE_ALPHA:
1737 case GL_LUMINANCE4_ALPHA4:
1738 case GL_LUMINANCE6_ALPHA2:
1739 case GL_LUMINANCE8_ALPHA8:
1740 case GL_LUMINANCE12_ALPHA4:
1741 case GL_LUMINANCE12_ALPHA12:
1742 case GL_LUMINANCE16_ALPHA16:
1743 return (ctx->API == API_OPENGL_COMPAT &&
1744 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
1745 case GL_INTENSITY:
1746 case GL_INTENSITY4:
1747 case GL_INTENSITY8:
1748 case GL_INTENSITY12:
1749 case GL_INTENSITY16:
1750 return (ctx->API == API_OPENGL_COMPAT &&
1751 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
1752 case GL_RGB8:
1753 return GL_RGB;
1754 case GL_RGB:
1755 case GL_R3_G3_B2:
1756 case GL_RGB4:
1757 case GL_RGB5:
1758 case GL_RGB10:
1759 case GL_RGB12:
1760 case GL_RGB16:
1761 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1762 case GL_SRGB8_EXT:
1763 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1764 case GL_RGBA4:
1765 case GL_RGB5_A1:
1766 case GL_RGBA8:
1767 return GL_RGBA;
1768 case GL_RGBA:
1769 case GL_RGBA2:
1770 case GL_RGBA12:
1771 case GL_RGBA16:
1772 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1773 case GL_RGB10_A2:
1774 case GL_SRGB8_ALPHA8_EXT:
1775 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1776 case GL_STENCIL_INDEX:
1777 case GL_STENCIL_INDEX1_EXT:
1778 case GL_STENCIL_INDEX4_EXT:
1779 case GL_STENCIL_INDEX16_EXT:
1780 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1781 * OpenGL ES, but Mesa does not currently support them.
1782 */
1783 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1784 case GL_STENCIL_INDEX8_EXT:
1785 return GL_STENCIL_INDEX;
1786 case GL_DEPTH_COMPONENT:
1787 case GL_DEPTH_COMPONENT32:
1788 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1789 case GL_DEPTH_COMPONENT16:
1790 case GL_DEPTH_COMPONENT24:
1791 return GL_DEPTH_COMPONENT;
1792 case GL_DEPTH_STENCIL:
1793 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1794 case GL_DEPTH24_STENCIL8:
1795 return GL_DEPTH_STENCIL;
1796 case GL_DEPTH_COMPONENT32F:
1797 return ctx->Version >= 30
1798 || (ctx->API == API_OPENGL_COMPAT &&
1799 ctx->Extensions.ARB_depth_buffer_float)
1800 ? GL_DEPTH_COMPONENT : 0;
1801 case GL_DEPTH32F_STENCIL8:
1802 return ctx->Version >= 30
1803 || (ctx->API == API_OPENGL_COMPAT &&
1804 ctx->Extensions.ARB_depth_buffer_float)
1805 ? GL_DEPTH_STENCIL : 0;
1806 case GL_RED:
1807 case GL_R16:
1808 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1809 ? GL_RED : 0;
1810 case GL_R8:
1811 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1812 ? GL_RED : 0;
1813 case GL_RG:
1814 case GL_RG16:
1815 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1816 ? GL_RG : 0;
1817 case GL_RG8:
1818 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1819 ? GL_RG : 0;
1820 /* signed normalized texture formats */
1821 case GL_RED_SNORM:
1822 case GL_R8_SNORM:
1823 case GL_R16_SNORM:
1824 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1825 ? GL_RED : 0;
1826 case GL_RG_SNORM:
1827 case GL_RG8_SNORM:
1828 case GL_RG16_SNORM:
1829 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1830 ? GL_RG : 0;
1831 case GL_RGB_SNORM:
1832 case GL_RGB8_SNORM:
1833 case GL_RGB16_SNORM:
1834 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1835 ? GL_RGB : 0;
1836 case GL_RGBA_SNORM:
1837 case GL_RGBA8_SNORM:
1838 case GL_RGBA16_SNORM:
1839 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1840 ? GL_RGBA : 0;
1841 case GL_ALPHA_SNORM:
1842 case GL_ALPHA8_SNORM:
1843 case GL_ALPHA16_SNORM:
1844 return ctx->API == API_OPENGL_COMPAT &&
1845 ctx->Extensions.EXT_texture_snorm &&
1846 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1847 case GL_LUMINANCE_SNORM:
1848 case GL_LUMINANCE8_SNORM:
1849 case GL_LUMINANCE16_SNORM:
1850 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1851 ? GL_LUMINANCE : 0;
1852 case GL_LUMINANCE_ALPHA_SNORM:
1853 case GL_LUMINANCE8_ALPHA8_SNORM:
1854 case GL_LUMINANCE16_ALPHA16_SNORM:
1855 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1856 ? GL_LUMINANCE_ALPHA : 0;
1857 case GL_INTENSITY_SNORM:
1858 case GL_INTENSITY8_SNORM:
1859 case GL_INTENSITY16_SNORM:
1860 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1861 ? GL_INTENSITY : 0;
1862
1863 case GL_R16F:
1864 case GL_R32F:
1865 return ((_mesa_is_desktop_gl(ctx) &&
1866 ctx->Extensions.ARB_texture_rg &&
1867 ctx->Extensions.ARB_texture_float) ||
1868 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1869 ? GL_RED : 0;
1870 case GL_RG16F:
1871 case GL_RG32F:
1872 return ((_mesa_is_desktop_gl(ctx) &&
1873 ctx->Extensions.ARB_texture_rg &&
1874 ctx->Extensions.ARB_texture_float) ||
1875 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1876 ? GL_RG : 0;
1877 case GL_RGB16F:
1878 case GL_RGB32F:
1879 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1880 ? GL_RGB : 0;
1881 case GL_RGBA16F:
1882 case GL_RGBA32F:
1883 return ((_mesa_is_desktop_gl(ctx) &&
1884 ctx->Extensions.ARB_texture_float) ||
1885 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1886 ? GL_RGBA : 0;
1887 case GL_ALPHA16F_ARB:
1888 case GL_ALPHA32F_ARB:
1889 return ctx->API == API_OPENGL_COMPAT &&
1890 ctx->Extensions.ARB_texture_float &&
1891 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1892 case GL_LUMINANCE16F_ARB:
1893 case GL_LUMINANCE32F_ARB:
1894 return ctx->API == API_OPENGL_COMPAT &&
1895 ctx->Extensions.ARB_texture_float &&
1896 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1897 case GL_LUMINANCE_ALPHA16F_ARB:
1898 case GL_LUMINANCE_ALPHA32F_ARB:
1899 return ctx->API == API_OPENGL_COMPAT &&
1900 ctx->Extensions.ARB_texture_float &&
1901 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1902 case GL_INTENSITY16F_ARB:
1903 case GL_INTENSITY32F_ARB:
1904 return ctx->API == API_OPENGL_COMPAT &&
1905 ctx->Extensions.ARB_texture_float &&
1906 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1907 case GL_R11F_G11F_B10F:
1908 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1909 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1910 ? GL_RGB : 0;
1911
1912 case GL_RGBA8UI_EXT:
1913 case GL_RGBA16UI_EXT:
1914 case GL_RGBA32UI_EXT:
1915 case GL_RGBA8I_EXT:
1916 case GL_RGBA16I_EXT:
1917 case GL_RGBA32I_EXT:
1918 return ctx->Version >= 30
1919 || (_mesa_is_desktop_gl(ctx) &&
1920 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1921
1922 case GL_RGB8UI_EXT:
1923 case GL_RGB16UI_EXT:
1924 case GL_RGB32UI_EXT:
1925 case GL_RGB8I_EXT:
1926 case GL_RGB16I_EXT:
1927 case GL_RGB32I_EXT:
1928 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1929 ? GL_RGB : 0;
1930 case GL_R8UI:
1931 case GL_R8I:
1932 case GL_R16UI:
1933 case GL_R16I:
1934 case GL_R32UI:
1935 case GL_R32I:
1936 return ctx->Version >= 30
1937 || (_mesa_is_desktop_gl(ctx) &&
1938 ctx->Extensions.ARB_texture_rg &&
1939 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1940
1941 case GL_RG8UI:
1942 case GL_RG8I:
1943 case GL_RG16UI:
1944 case GL_RG16I:
1945 case GL_RG32UI:
1946 case GL_RG32I:
1947 return ctx->Version >= 30
1948 || (_mesa_is_desktop_gl(ctx) &&
1949 ctx->Extensions.ARB_texture_rg &&
1950 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1951
1952 case GL_INTENSITY8I_EXT:
1953 case GL_INTENSITY8UI_EXT:
1954 case GL_INTENSITY16I_EXT:
1955 case GL_INTENSITY16UI_EXT:
1956 case GL_INTENSITY32I_EXT:
1957 case GL_INTENSITY32UI_EXT:
1958 return ctx->API == API_OPENGL_COMPAT &&
1959 ctx->Extensions.EXT_texture_integer &&
1960 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1961
1962 case GL_LUMINANCE8I_EXT:
1963 case GL_LUMINANCE8UI_EXT:
1964 case GL_LUMINANCE16I_EXT:
1965 case GL_LUMINANCE16UI_EXT:
1966 case GL_LUMINANCE32I_EXT:
1967 case GL_LUMINANCE32UI_EXT:
1968 return ctx->API == API_OPENGL_COMPAT &&
1969 ctx->Extensions.EXT_texture_integer &&
1970 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1971
1972 case GL_LUMINANCE_ALPHA8I_EXT:
1973 case GL_LUMINANCE_ALPHA8UI_EXT:
1974 case GL_LUMINANCE_ALPHA16I_EXT:
1975 case GL_LUMINANCE_ALPHA16UI_EXT:
1976 case GL_LUMINANCE_ALPHA32I_EXT:
1977 case GL_LUMINANCE_ALPHA32UI_EXT:
1978 return ctx->API == API_OPENGL_COMPAT &&
1979 ctx->Extensions.EXT_texture_integer &&
1980 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1981
1982 case GL_ALPHA8I_EXT:
1983 case GL_ALPHA8UI_EXT:
1984 case GL_ALPHA16I_EXT:
1985 case GL_ALPHA16UI_EXT:
1986 case GL_ALPHA32I_EXT:
1987 case GL_ALPHA32UI_EXT:
1988 return ctx->API == API_OPENGL_COMPAT &&
1989 ctx->Extensions.EXT_texture_integer &&
1990 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1991
1992 case GL_RGB10_A2UI:
1993 return (_mesa_is_desktop_gl(ctx) &&
1994 ctx->Extensions.ARB_texture_rgb10_a2ui)
1995 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1996
1997 case GL_RGB565:
1998 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1999 ? GL_RGB : 0;
2000 default:
2001 return 0;
2002 }
2003 }
2004
2005
2006 /**
2007 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
2008 */
2009 static void
2010 invalidate_rb(GLuint key, void *data, void *userData)
2011 {
2012 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2013 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
2014
2015 /* If this is a user-created FBO */
2016 if (_mesa_is_user_fbo(fb)) {
2017 GLuint i;
2018 for (i = 0; i < BUFFER_COUNT; i++) {
2019 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2020 if (att->Type == GL_RENDERBUFFER &&
2021 att->Renderbuffer == rb) {
2022 /* Mark fb status as indeterminate to force re-validation */
2023 fb->_Status = 0;
2024 return;
2025 }
2026 }
2027 }
2028 }
2029
2030
2031 /** sentinal value, see below */
2032 #define NO_SAMPLES 1000
2033
2034 void
2035 _mesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2036 GLenum internalFormat, GLsizei width,
2037 GLsizei height, GLsizei samples)
2038 {
2039 const GLenum baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2040
2041 assert(baseFormat != 0);
2042 assert(width >= 0 && width <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2043 assert(height >= 0 && height <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2044 assert(samples != NO_SAMPLES);
2045 if (samples != 0) {
2046 assert(samples > 0);
2047 assert(_mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2048 internalFormat, samples) == GL_NO_ERROR);
2049 }
2050
2051 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2052
2053 if (rb->InternalFormat == internalFormat &&
2054 rb->Width == (GLuint) width &&
2055 rb->Height == (GLuint) height &&
2056 rb->NumSamples == samples) {
2057 /* no change in allocation needed */
2058 return;
2059 }
2060
2061 /* These MUST get set by the AllocStorage func */
2062 rb->Format = MESA_FORMAT_NONE;
2063 rb->NumSamples = samples;
2064
2065 /* Now allocate the storage */
2066 assert(rb->AllocStorage);
2067 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
2068 /* No error - check/set fields now */
2069 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2070 assert(rb->Width == (GLuint) width);
2071 assert(rb->Height == (GLuint) height);
2072 rb->InternalFormat = internalFormat;
2073 rb->_BaseFormat = baseFormat;
2074 assert(rb->_BaseFormat != 0);
2075 }
2076 else {
2077 /* Probably ran out of memory - clear the fields */
2078 rb->Width = 0;
2079 rb->Height = 0;
2080 rb->Format = MESA_FORMAT_NONE;
2081 rb->InternalFormat = GL_NONE;
2082 rb->_BaseFormat = GL_NONE;
2083 rb->NumSamples = 0;
2084 }
2085
2086 /* Invalidate the framebuffers the renderbuffer is attached in. */
2087 if (rb->AttachedAnytime) {
2088 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
2089 }
2090 }
2091
2092 /**
2093 * Helper function used by renderbuffer_storage_direct() and
2094 * renderbuffer_storage_target().
2095 * samples will be NO_SAMPLES if called by a non-multisample function.
2096 */
2097 static void
2098 renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2099 GLenum internalFormat, GLsizei width,
2100 GLsizei height, GLsizei samples, const char *func)
2101 {
2102 GLenum baseFormat;
2103 GLenum sample_count_error;
2104
2105 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2106 if (baseFormat == 0) {
2107 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
2108 func, _mesa_enum_to_string(internalFormat));
2109 return;
2110 }
2111
2112 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2113 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
2114 width);
2115 return;
2116 }
2117
2118 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2119 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
2120 height);
2121 return;
2122 }
2123
2124 if (samples == NO_SAMPLES) {
2125 /* NumSamples == 0 indicates non-multisampling */
2126 samples = 0;
2127 }
2128 else {
2129 /* check the sample count;
2130 * note: driver may choose to use more samples than what's requested
2131 */
2132 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2133 internalFormat, samples);
2134
2135 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2136 *
2137 * "If a negative number is provided where an argument of type sizei or
2138 * sizeiptr is specified, the error INVALID VALUE is generated."
2139 */
2140 if (samples < 0) {
2141 sample_count_error = GL_INVALID_VALUE;
2142 }
2143
2144 if (sample_count_error != GL_NO_ERROR) {
2145 _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
2146 return;
2147 }
2148 }
2149
2150 _mesa_renderbuffer_storage(ctx, rb, internalFormat, width, height, samples);
2151 }
2152
2153 /**
2154 * Helper function used by _mesa_NamedRenderbufferStorage*().
2155 * samples will be NO_SAMPLES if called by a non-multisample function.
2156 */
2157 static void
2158 renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
2159 GLsizei width, GLsizei height, GLsizei samples,
2160 const char *func)
2161 {
2162 GET_CURRENT_CONTEXT(ctx);
2163
2164 if (MESA_VERBOSE & VERBOSE_API) {
2165 if (samples == NO_SAMPLES)
2166 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
2167 func, renderbuffer,
2168 _mesa_enum_to_string(internalFormat),
2169 width, height);
2170 else
2171 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
2172 func, renderbuffer,
2173 _mesa_enum_to_string(internalFormat),
2174 width, height, samples);
2175 }
2176
2177 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2178 if (!rb || rb == &DummyRenderbuffer) {
2179 /* ID was reserved, but no real renderbuffer object made yet */
2180 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
2181 func, renderbuffer);
2182 return;
2183 }
2184
2185 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
2186 }
2187
2188 /**
2189 * Helper function used by _mesa_RenderbufferStorage() and
2190 * _mesa_RenderbufferStorageMultisample().
2191 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2192 */
2193 static void
2194 renderbuffer_storage_target(GLenum target, GLenum internalFormat,
2195 GLsizei width, GLsizei height, GLsizei samples,
2196 const char *func)
2197 {
2198 GET_CURRENT_CONTEXT(ctx);
2199
2200 if (MESA_VERBOSE & VERBOSE_API) {
2201 if (samples == NO_SAMPLES)
2202 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
2203 func,
2204 _mesa_enum_to_string(target),
2205 _mesa_enum_to_string(internalFormat),
2206 width, height);
2207 else
2208 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
2209 func,
2210 _mesa_enum_to_string(target),
2211 _mesa_enum_to_string(internalFormat),
2212 width, height, samples);
2213 }
2214
2215 if (target != GL_RENDERBUFFER_EXT) {
2216 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
2217 return;
2218 }
2219
2220 if (!ctx->CurrentRenderbuffer) {
2221 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
2222 func);
2223 return;
2224 }
2225
2226 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
2227 height, samples, func);
2228 }
2229
2230
2231 void GLAPIENTRY
2232 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
2233 {
2234 struct gl_renderbuffer *rb;
2235 GET_CURRENT_CONTEXT(ctx);
2236
2237 if (!ctx->Extensions.OES_EGL_image) {
2238 _mesa_error(ctx, GL_INVALID_OPERATION,
2239 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2240 return;
2241 }
2242
2243 if (target != GL_RENDERBUFFER) {
2244 _mesa_error(ctx, GL_INVALID_ENUM,
2245 "EGLImageTargetRenderbufferStorageOES");
2246 return;
2247 }
2248
2249 rb = ctx->CurrentRenderbuffer;
2250 if (!rb) {
2251 _mesa_error(ctx, GL_INVALID_OPERATION,
2252 "EGLImageTargetRenderbufferStorageOES");
2253 return;
2254 }
2255
2256 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2257
2258 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2259 }
2260
2261
2262 /**
2263 * Helper function for _mesa_GetRenderbufferParameteriv() and
2264 * _mesa_GetFramebufferAttachmentParameteriv()
2265 * We have to be careful to respect the base format. For example, if a
2266 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2267 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2268 * we need to return zero.
2269 */
2270 static GLint
2271 get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
2272 {
2273 if (_mesa_base_format_has_channel(baseFormat, pname))
2274 return _mesa_get_format_bits(format, pname);
2275 else
2276 return 0;
2277 }
2278
2279
2280
2281 void GLAPIENTRY
2282 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
2283 GLsizei width, GLsizei height)
2284 {
2285 /* GL_ARB_fbo says calling this function is equivalent to calling
2286 * glRenderbufferStorageMultisample() with samples=0. We pass in
2287 * a token value here just for error reporting purposes.
2288 */
2289 renderbuffer_storage_target(target, internalFormat, width, height,
2290 NO_SAMPLES, "glRenderbufferStorage");
2291 }
2292
2293
2294 void GLAPIENTRY
2295 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
2296 GLenum internalFormat,
2297 GLsizei width, GLsizei height)
2298 {
2299 renderbuffer_storage_target(target, internalFormat, width, height,
2300 samples, "glRenderbufferStorageMultisample");
2301 }
2302
2303
2304 /**
2305 * OpenGL ES version of glRenderBufferStorage.
2306 */
2307 void GLAPIENTRY
2308 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
2309 GLsizei width, GLsizei height)
2310 {
2311 switch (internalFormat) {
2312 case GL_RGB565:
2313 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2314 /* choose a closest format */
2315 internalFormat = GL_RGB5;
2316 break;
2317 default:
2318 break;
2319 }
2320
2321 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2322 "glRenderbufferStorageEXT");
2323 }
2324
2325 void GLAPIENTRY
2326 _mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2327 GLsizei width, GLsizei height)
2328 {
2329 /* GL_ARB_fbo says calling this function is equivalent to calling
2330 * glRenderbufferStorageMultisample() with samples=0. We pass in
2331 * a token value here just for error reporting purposes.
2332 */
2333 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2334 NO_SAMPLES, "glNamedRenderbufferStorage");
2335 }
2336
2337 void GLAPIENTRY
2338 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2339 GLenum internalformat,
2340 GLsizei width, GLsizei height)
2341 {
2342 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2343 samples,
2344 "glNamedRenderbufferStorageMultisample");
2345 }
2346
2347
2348 static void
2349 get_render_buffer_parameteriv(struct gl_context *ctx,
2350 struct gl_renderbuffer *rb, GLenum pname,
2351 GLint *params, const char *func)
2352 {
2353 /* No need to flush here since we're just quering state which is
2354 * not effected by rendering.
2355 */
2356
2357 switch (pname) {
2358 case GL_RENDERBUFFER_WIDTH_EXT:
2359 *params = rb->Width;
2360 return;
2361 case GL_RENDERBUFFER_HEIGHT_EXT:
2362 *params = rb->Height;
2363 return;
2364 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
2365 *params = rb->InternalFormat;
2366 return;
2367 case GL_RENDERBUFFER_RED_SIZE_EXT:
2368 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
2369 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
2370 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
2371 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
2372 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
2373 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
2374 break;
2375 case GL_RENDERBUFFER_SAMPLES:
2376 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2377 || _mesa_is_gles3(ctx)) {
2378 *params = rb->NumSamples;
2379 break;
2380 }
2381 /* fallthrough */
2382 default:
2383 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
2384 _mesa_enum_to_string(pname));
2385 return;
2386 }
2387 }
2388
2389
2390 void GLAPIENTRY
2391 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2392 {
2393 GET_CURRENT_CONTEXT(ctx);
2394
2395 if (target != GL_RENDERBUFFER_EXT) {
2396 _mesa_error(ctx, GL_INVALID_ENUM,
2397 "glGetRenderbufferParameterivEXT(target)");
2398 return;
2399 }
2400
2401 if (!ctx->CurrentRenderbuffer) {
2402 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2403 "(no renderbuffer bound)");
2404 return;
2405 }
2406
2407 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2408 params, "glGetRenderbufferParameteriv");
2409 }
2410
2411
2412 void GLAPIENTRY
2413 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2414 GLint *params)
2415 {
2416 GET_CURRENT_CONTEXT(ctx);
2417
2418 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2419 if (!rb || rb == &DummyRenderbuffer) {
2420 /* ID was reserved, but no real renderbuffer object made yet */
2421 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2422 "(invalid renderbuffer %i)", renderbuffer);
2423 return;
2424 }
2425
2426 get_render_buffer_parameteriv(ctx, rb, pname, params,
2427 "glGetNamedRenderbufferParameteriv");
2428 }
2429
2430
2431 GLboolean GLAPIENTRY
2432 _mesa_IsFramebuffer(GLuint framebuffer)
2433 {
2434 GET_CURRENT_CONTEXT(ctx);
2435 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2436 if (framebuffer) {
2437 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
2438 if (rb != NULL && rb != &DummyFramebuffer)
2439 return GL_TRUE;
2440 }
2441 return GL_FALSE;
2442 }
2443
2444
2445 /**
2446 * Check if any of the attachments of the given framebuffer are textures
2447 * (render to texture). Call ctx->Driver.RenderTexture() for such
2448 * attachments.
2449 */
2450 static void
2451 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2452 {
2453 GLuint i;
2454 assert(ctx->Driver.RenderTexture);
2455
2456 if (_mesa_is_winsys_fbo(fb))
2457 return; /* can't render to texture with winsys framebuffers */
2458
2459 for (i = 0; i < BUFFER_COUNT; i++) {
2460 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2461 if (att->Texture && att->Renderbuffer->TexImage
2462 && driver_RenderTexture_is_safe(att)) {
2463 ctx->Driver.RenderTexture(ctx, fb, att);
2464 }
2465 }
2466 }
2467
2468
2469 /**
2470 * Examine all the framebuffer's attachments to see if any are textures.
2471 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2472 * notify the device driver that the texture image may have changed.
2473 */
2474 static void
2475 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2476 {
2477 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2478 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2479 return;
2480
2481 if (ctx->Driver.FinishRenderTexture) {
2482 GLuint i;
2483 for (i = 0; i < BUFFER_COUNT; i++) {
2484 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2485 struct gl_renderbuffer *rb = att->Renderbuffer;
2486 if (rb && rb->NeedsFinishRenderTexture) {
2487 ctx->Driver.FinishRenderTexture(ctx, rb);
2488 }
2489 }
2490 }
2491 }
2492
2493
2494 static void
2495 bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
2496 {
2497 struct gl_framebuffer *newDrawFb, *newReadFb;
2498 GLboolean bindReadBuf, bindDrawBuf;
2499 GET_CURRENT_CONTEXT(ctx);
2500
2501 switch (target) {
2502 case GL_DRAW_FRAMEBUFFER_EXT:
2503 bindDrawBuf = GL_TRUE;
2504 bindReadBuf = GL_FALSE;
2505 break;
2506 case GL_READ_FRAMEBUFFER_EXT:
2507 bindDrawBuf = GL_FALSE;
2508 bindReadBuf = GL_TRUE;
2509 break;
2510 case GL_FRAMEBUFFER_EXT:
2511 bindDrawBuf = GL_TRUE;
2512 bindReadBuf = GL_TRUE;
2513 break;
2514 default:
2515 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
2516 return;
2517 }
2518
2519 if (framebuffer) {
2520 /* Binding a user-created framebuffer object */
2521 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2522 if (newDrawFb == &DummyFramebuffer) {
2523 /* ID was reserved, but no real framebuffer object made yet */
2524 newDrawFb = NULL;
2525 }
2526 else if (!newDrawFb && !allow_user_names) {
2527 /* All FBO IDs must be Gen'd */
2528 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2529 return;
2530 }
2531
2532 if (!newDrawFb) {
2533 /* create new framebuffer object */
2534 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2535 if (!newDrawFb) {
2536 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2537 return;
2538 }
2539 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
2540 }
2541 newReadFb = newDrawFb;
2542 }
2543 else {
2544 /* Binding the window system framebuffer (which was originally set
2545 * with MakeCurrent).
2546 */
2547 newDrawFb = ctx->WinSysDrawBuffer;
2548 newReadFb = ctx->WinSysReadBuffer;
2549 }
2550
2551 _mesa_bind_framebuffers(ctx,
2552 bindDrawBuf ? newDrawFb : ctx->DrawBuffer,
2553 bindReadBuf ? newReadFb : ctx->ReadBuffer);
2554 }
2555
2556 void
2557 _mesa_bind_framebuffers(struct gl_context *ctx,
2558 struct gl_framebuffer *newDrawFb,
2559 struct gl_framebuffer *newReadFb)
2560 {
2561 struct gl_framebuffer *const oldDrawFb = ctx->DrawBuffer;
2562 struct gl_framebuffer *const oldReadFb = ctx->ReadBuffer;
2563 const bool bindDrawBuf = oldDrawFb != newDrawFb;
2564 const bool bindReadBuf = oldReadFb != newReadFb;
2565
2566 assert(newDrawFb);
2567 assert(newDrawFb != &DummyFramebuffer);
2568
2569 /*
2570 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2571 *
2572 * We also check if we're beginning and/or ending render-to-texture.
2573 * When a framebuffer with texture attachments is unbound, call
2574 * ctx->Driver.FinishRenderTexture().
2575 * When a framebuffer with texture attachments is bound, call
2576 * ctx->Driver.RenderTexture().
2577 *
2578 * Note that if the ReadBuffer has texture attachments we don't consider
2579 * that a render-to-texture case.
2580 */
2581 if (bindReadBuf) {
2582 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2583
2584 /* check if old readbuffer was render-to-texture */
2585 check_end_texture_render(ctx, oldReadFb);
2586
2587 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2588 }
2589
2590 if (bindDrawBuf) {
2591 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2592
2593 /* check if old framebuffer had any texture attachments */
2594 if (oldDrawFb)
2595 check_end_texture_render(ctx, oldDrawFb);
2596
2597 /* check if newly bound framebuffer has any texture attachments */
2598 check_begin_texture_render(ctx, newDrawFb);
2599
2600 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2601 }
2602
2603 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2604 /* The few classic drivers that actually hook this function really only
2605 * want to know if the draw framebuffer changed.
2606 */
2607 ctx->Driver.BindFramebuffer(ctx,
2608 bindDrawBuf ? GL_FRAMEBUFFER : GL_READ_FRAMEBUFFER,
2609 newDrawFb, newReadFb);
2610 }
2611 }
2612
2613 void GLAPIENTRY
2614 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
2615 {
2616 GET_CURRENT_CONTEXT(ctx);
2617
2618 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2619 * point, but they allow the use of user-generated names.
2620 */
2621 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
2622 }
2623
2624
2625 void GLAPIENTRY
2626 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2627 {
2628 /* This function should not be in the dispatch table for core profile /
2629 * OpenGL 3.1, so execution should never get here in those cases -- no
2630 * need for an explicit test.
2631 */
2632 bind_framebuffer(target, framebuffer, true);
2633 }
2634
2635
2636 void GLAPIENTRY
2637 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2638 {
2639 GLint i;
2640 GET_CURRENT_CONTEXT(ctx);
2641
2642 if (n < 0) {
2643 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2644 return;
2645 }
2646
2647 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2648
2649 for (i = 0; i < n; i++) {
2650 if (framebuffers[i] > 0) {
2651 struct gl_framebuffer *fb;
2652 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2653 if (fb) {
2654 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2655
2656 /* check if deleting currently bound framebuffer object */
2657 if (fb == ctx->DrawBuffer) {
2658 /* bind default */
2659 assert(fb->RefCount >= 2);
2660 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2661 }
2662 if (fb == ctx->ReadBuffer) {
2663 /* bind default */
2664 assert(fb->RefCount >= 2);
2665 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2666 }
2667
2668 /* remove from hash table immediately, to free the ID */
2669 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2670
2671 if (fb != &DummyFramebuffer) {
2672 /* But the object will not be freed until it's no longer
2673 * bound in any context.
2674 */
2675 _mesa_reference_framebuffer(&fb, NULL);
2676 }
2677 }
2678 }
2679 }
2680 }
2681
2682
2683 /**
2684 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2685 * It is not exposed to the rest of Mesa to encourage the use of
2686 * nameless buffers in driver internals.
2687 */
2688 static void
2689 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
2690 {
2691 GET_CURRENT_CONTEXT(ctx);
2692 GLuint first;
2693 GLint i;
2694 struct gl_framebuffer *fb;
2695
2696 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
2697
2698 if (n < 0) {
2699 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
2700 return;
2701 }
2702
2703 if (!framebuffers)
2704 return;
2705
2706 _mesa_HashLockMutex(ctx->Shared->FrameBuffers);
2707
2708 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2709
2710 for (i = 0; i < n; i++) {
2711 GLuint name = first + i;
2712 framebuffers[i] = name;
2713
2714 if (dsa) {
2715 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2716 if (!fb) {
2717 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2718 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2719 return;
2720 }
2721 }
2722 else
2723 fb = &DummyFramebuffer;
2724
2725 _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, name, fb);
2726 }
2727
2728 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2729 }
2730
2731
2732 void GLAPIENTRY
2733 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2734 {
2735 create_framebuffers(n, framebuffers, false);
2736 }
2737
2738
2739 void GLAPIENTRY
2740 _mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2741 {
2742 create_framebuffers(n, framebuffers, true);
2743 }
2744
2745
2746 GLenum
2747 _mesa_check_framebuffer_status(struct gl_context *ctx,
2748 struct gl_framebuffer *buffer)
2749 {
2750 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2751
2752 if (_mesa_is_winsys_fbo(buffer)) {
2753 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2754 if (buffer != &IncompleteFramebuffer) {
2755 return GL_FRAMEBUFFER_COMPLETE_EXT;
2756 } else {
2757 return GL_FRAMEBUFFER_UNDEFINED;
2758 }
2759 }
2760
2761 /* No need to flush here */
2762
2763 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2764 _mesa_test_framebuffer_completeness(ctx, buffer);
2765 }
2766
2767 return buffer->_Status;
2768 }
2769
2770
2771 GLenum GLAPIENTRY
2772 _mesa_CheckFramebufferStatus(GLenum target)
2773 {
2774 struct gl_framebuffer *fb;
2775 GET_CURRENT_CONTEXT(ctx);
2776
2777 if (MESA_VERBOSE & VERBOSE_API)
2778 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2779 _mesa_enum_to_string(target));
2780
2781 fb = get_framebuffer_target(ctx, target);
2782 if (!fb) {
2783 _mesa_error(ctx, GL_INVALID_ENUM,
2784 "glCheckFramebufferStatus(invalid target %s)",
2785 _mesa_enum_to_string(target));
2786 return 0;
2787 }
2788
2789 return _mesa_check_framebuffer_status(ctx, fb);
2790 }
2791
2792
2793 GLenum GLAPIENTRY
2794 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer, GLenum target)
2795 {
2796 struct gl_framebuffer *fb;
2797 GET_CURRENT_CONTEXT(ctx);
2798
2799 /* Validate the target (for conformance's sake) and grab a reference to the
2800 * default framebuffer in case framebuffer = 0.
2801 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2802 * (30.10.2014, PDF page 336) says:
2803 * "If framebuffer is zero, then the status of the default read or
2804 * draw framebuffer (as determined by target) is returned."
2805 */
2806 switch (target) {
2807 case GL_DRAW_FRAMEBUFFER:
2808 case GL_FRAMEBUFFER:
2809 fb = ctx->WinSysDrawBuffer;
2810 break;
2811 case GL_READ_FRAMEBUFFER:
2812 fb = ctx->WinSysReadBuffer;
2813 break;
2814 default:
2815 _mesa_error(ctx, GL_INVALID_ENUM,
2816 "glCheckNamedFramebufferStatus(invalid target %s)",
2817 _mesa_enum_to_string(target));
2818 return 0;
2819 }
2820
2821 if (framebuffer) {
2822 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
2823 "glCheckNamedFramebufferStatus");
2824 if (!fb)
2825 return 0;
2826 }
2827
2828 return _mesa_check_framebuffer_status(ctx, fb);
2829 }
2830
2831
2832 /**
2833 * Replicate the src attachment point. Used by framebuffer_texture() when
2834 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2835 * GL_STENCIL_ATTACHMENT.
2836 */
2837 static void
2838 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2839 gl_buffer_index dst,
2840 gl_buffer_index src)
2841 {
2842 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2843 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2844
2845 assert(src_att->Texture != NULL);
2846 assert(src_att->Renderbuffer != NULL);
2847
2848 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2849 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2850 dst_att->Type = src_att->Type;
2851 dst_att->Complete = src_att->Complete;
2852 dst_att->TextureLevel = src_att->TextureLevel;
2853 dst_att->CubeMapFace = src_att->CubeMapFace;
2854 dst_att->Zoffset = src_att->Zoffset;
2855 dst_att->Layered = src_att->Layered;
2856 }
2857
2858
2859 /**
2860 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2861 * texture object pointer.
2862 *
2863 * \param texObj where the pointer to the texture object is returned. Note
2864 * that a successful call may return texObj = NULL.
2865 *
2866 * \return true if no errors, false if errors
2867 */
2868 static bool
2869 get_texture_for_framebuffer(struct gl_context *ctx, GLuint texture,
2870 bool layered, const char *caller,
2871 struct gl_texture_object **texObj)
2872 {
2873 *texObj = NULL; /* This will get returned if texture = 0. */
2874
2875 if (!texture)
2876 return true;
2877
2878 *texObj = _mesa_lookup_texture(ctx, texture);
2879 if (*texObj == NULL || (*texObj)->Target == 0) {
2880 /* Can't render to a non-existent texture object.
2881 *
2882 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2883 * Managing Framebuffer Objects specifies a different error
2884 * depending upon the calling function (PDF pages 325-328).
2885 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
2886 * value, while the other commands throw invalid operation (where
2887 * layered = GL_FALSE).
2888 */
2889 const GLenum error = layered ? GL_INVALID_VALUE :
2890 GL_INVALID_OPERATION;
2891 _mesa_error(ctx, error,
2892 "%s(non-existent texture %u)", caller, texture);
2893 return false;
2894 }
2895
2896 return true;
2897 }
2898
2899
2900 /**
2901 * Common code called by gl*FramebufferTexture() to verify the texture target
2902 * and decide whether or not the attachment should truly be considered
2903 * layered.
2904 *
2905 * \param layered true if attachment should be considered layered, false if
2906 * not
2907 *
2908 * \return true if no errors, false if errors
2909 */
2910 static bool
2911 check_layered_texture_target(struct gl_context *ctx, GLenum target,
2912 const char *caller, GLboolean *layered)
2913 {
2914 *layered = GL_TRUE;
2915
2916 switch (target) {
2917 case GL_TEXTURE_3D:
2918 case GL_TEXTURE_1D_ARRAY_EXT:
2919 case GL_TEXTURE_2D_ARRAY_EXT:
2920 case GL_TEXTURE_CUBE_MAP:
2921 case GL_TEXTURE_CUBE_MAP_ARRAY:
2922 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2923 return true;
2924 case GL_TEXTURE_1D:
2925 case GL_TEXTURE_2D:
2926 case GL_TEXTURE_RECTANGLE:
2927 case GL_TEXTURE_2D_MULTISAMPLE:
2928 /* These texture types are valid to pass to
2929 * glFramebufferTexture(), but since they aren't layered, it
2930 * is equivalent to calling glFramebufferTexture{1D,2D}().
2931 */
2932 *layered = GL_FALSE;
2933 return true;
2934 }
2935
2936 _mesa_error(ctx, GL_INVALID_OPERATION,
2937 "%s(invalid texture target %s)", caller,
2938 _mesa_enum_to_string(target));
2939 return false;
2940 }
2941
2942
2943 /**
2944 * Common code called by gl*FramebufferTextureLayer() to verify the texture
2945 * target.
2946 *
2947 * \return true if no errors, false if errors
2948 */
2949 static bool
2950 check_texture_target(struct gl_context *ctx, GLenum target,
2951 const char *caller)
2952 {
2953 /* We're being called by glFramebufferTextureLayer().
2954 * The only legal texture types for that function are 3D,
2955 * cube-map, and 1D/2D/cube-map array textures.
2956 *
2957 * We don't need to check for GL_ARB_texture_cube_map_array because the
2958 * application wouldn't have been able to create a texture with a
2959 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
2960 */
2961 switch (target) {
2962 case GL_TEXTURE_3D:
2963 case GL_TEXTURE_1D_ARRAY:
2964 case GL_TEXTURE_2D_ARRAY:
2965 case GL_TEXTURE_CUBE_MAP_ARRAY:
2966 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2967 return true;
2968 case GL_TEXTURE_CUBE_MAP:
2969 /* We don't need to check the extension (GL_ARB_direct_state_access) or
2970 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
2971 * enabled in core profile. This can be called from
2972 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
2973 * so we do have to check the profile.
2974 */
2975 return ctx->API == API_OPENGL_CORE;
2976 }
2977
2978 _mesa_error(ctx, GL_INVALID_OPERATION,
2979 "%s(invalid texture target %s)", caller,
2980 _mesa_enum_to_string(target));
2981 return false;
2982 }
2983
2984
2985 /**
2986 * Common code called by glFramebufferTexture*D() to verify the texture
2987 * target.
2988 *
2989 * \return true if no errors, false if errors
2990 */
2991 static bool
2992 check_textarget(struct gl_context *ctx, int dims, GLenum target,
2993 GLenum textarget, const char *caller)
2994 {
2995 bool err = false;
2996
2997 switch (textarget) {
2998 case GL_TEXTURE_1D:
2999 err = dims != 1;
3000 break;
3001 case GL_TEXTURE_1D_ARRAY:
3002 err = dims != 1 || !ctx->Extensions.EXT_texture_array;
3003 break;
3004 case GL_TEXTURE_2D:
3005 err = dims != 2;
3006 break;
3007 case GL_TEXTURE_2D_ARRAY:
3008 err = dims != 2 || !ctx->Extensions.EXT_texture_array ||
3009 (_mesa_is_gles(ctx) && ctx->Version < 30);
3010 break;
3011 case GL_TEXTURE_2D_MULTISAMPLE:
3012 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3013 err = dims != 2 ||
3014 !ctx->Extensions.ARB_texture_multisample ||
3015 (_mesa_is_gles(ctx) && ctx->Version < 31);
3016 break;
3017 case GL_TEXTURE_RECTANGLE:
3018 err = dims != 2 || _mesa_is_gles(ctx) ||
3019 !ctx->Extensions.NV_texture_rectangle;
3020 break;
3021 case GL_TEXTURE_CUBE_MAP:
3022 case GL_TEXTURE_CUBE_MAP_ARRAY:
3023 err = true;
3024 break;
3025 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3026 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3027 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3028 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3029 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3030 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3031 err = dims != 2 || !ctx->Extensions.ARB_texture_cube_map;
3032 break;
3033 case GL_TEXTURE_3D:
3034 err = dims != 3;
3035 break;
3036 default:
3037 _mesa_error(ctx, GL_INVALID_ENUM,
3038 "%s(unknown textarget 0x%x)", caller, textarget);
3039 return false;
3040 }
3041
3042 if (err) {
3043 _mesa_error(ctx, GL_INVALID_OPERATION,
3044 "%s(invalid textarget %s)",
3045 caller, _mesa_enum_to_string(textarget));
3046 return false;
3047 }
3048
3049 /* Make sure textarget is consistent with the texture's type */
3050 err = (target == GL_TEXTURE_CUBE_MAP) ?
3051 !_mesa_is_cube_face(textarget): (target != textarget);
3052
3053 if (err) {
3054 _mesa_error(ctx, GL_INVALID_OPERATION,
3055 "%s(mismatched texture target)", caller);
3056 return false;
3057 }
3058
3059 return true;
3060 }
3061
3062
3063 /**
3064 * Common code called by gl*FramebufferTextureLayer() and
3065 * glFramebufferTexture3D() to validate the layer.
3066 *
3067 * \return true if no errors, false if errors
3068 */
3069 static bool
3070 check_layer(struct gl_context *ctx, GLenum target, GLint layer,
3071 const char *caller)
3072 {
3073 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3074 * spec says:
3075 *
3076 * "An INVALID_VALUE error is generated if texture is non-zero
3077 * and layer is negative."
3078 */
3079 if (layer < 0) {
3080 _mesa_error(ctx, GL_INVALID_VALUE,
3081 "%s(layer %u < 0)", caller, layer);
3082 return false;
3083 }
3084
3085 if (target == GL_TEXTURE_3D) {
3086 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
3087 if (layer >= maxSize) {
3088 _mesa_error(ctx, GL_INVALID_VALUE,
3089 "%s(invalid layer %u)", caller, layer);
3090 return false;
3091 }
3092 }
3093 else if ((target == GL_TEXTURE_1D_ARRAY) ||
3094 (target == GL_TEXTURE_2D_ARRAY) ||
3095 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
3096 (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
3097 if (layer >= ctx->Const.MaxArrayTextureLayers) {
3098 _mesa_error(ctx, GL_INVALID_VALUE,
3099 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3100 caller, layer);
3101 return false;
3102 }
3103 }
3104 else if (target == GL_TEXTURE_CUBE_MAP) {
3105 if (layer >= 6) {
3106 _mesa_error(ctx, GL_INVALID_VALUE,
3107 "%s(layer %u >= 6)", caller, layer);
3108 return false;
3109 }
3110 }
3111
3112 return true;
3113 }
3114
3115
3116 /**
3117 * Common code called by all gl*FramebufferTexture*() entry points to verify
3118 * the level.
3119 *
3120 * \return true if no errors, false if errors
3121 */
3122 static bool
3123 check_level(struct gl_context *ctx, GLenum target, GLint level,
3124 const char *caller)
3125 {
3126 if ((level < 0) ||
3127 (level >= _mesa_max_texture_levels(ctx, target))) {
3128 _mesa_error(ctx, GL_INVALID_VALUE,
3129 "%s(invalid level %d)", caller, level);
3130 return false;
3131 }
3132
3133 return true;
3134 }
3135
3136
3137 void
3138 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3139 GLenum attachment,
3140 struct gl_texture_object *texObj, GLenum textarget,
3141 GLint level, GLuint layer, GLboolean layered,
3142 const char *caller)
3143 {
3144 struct gl_renderbuffer_attachment *att;
3145
3146 /* The window-system framebuffer object is immutable */
3147 if (_mesa_is_winsys_fbo(fb)) {
3148 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3149 caller);
3150 return;
3151 }
3152
3153 /* Not a hash lookup, so we can afford to get the attachment here. */
3154 att = get_attachment(ctx, fb, attachment);
3155 if (att == NULL) {
3156 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3157 _mesa_enum_to_string(attachment));
3158 return;
3159 }
3160
3161 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3162
3163 mtx_lock(&fb->Mutex);
3164 if (texObj) {
3165 if (attachment == GL_DEPTH_ATTACHMENT &&
3166 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3167 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3168 _mesa_tex_target_to_face(textarget) ==
3169 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
3170 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
3171 /* The texture object is already attached to the stencil attachment
3172 * point. Don't create a new renderbuffer; just reuse the stencil
3173 * attachment's. This is required to prevent a GL error in
3174 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3175 */
3176 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3177 BUFFER_STENCIL);
3178 } else if (attachment == GL_STENCIL_ATTACHMENT &&
3179 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
3180 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3181 _mesa_tex_target_to_face(textarget) ==
3182 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
3183 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
3184 /* As above, but with depth and stencil transposed. */
3185 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3186 BUFFER_DEPTH);
3187 } else {
3188 set_texture_attachment(ctx, fb, att, texObj, textarget,
3189 level, layer, layered);
3190
3191 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3192 /* Above we created a new renderbuffer and attached it to the
3193 * depth attachment point. Now attach it to the stencil attachment
3194 * point too.
3195 */
3196 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3197 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3198 BUFFER_DEPTH);
3199 }
3200 }
3201
3202 /* Set the render-to-texture flag. We'll check this flag in
3203 * glTexImage() and friends to determine if we need to revalidate
3204 * any FBOs that might be rendering into this texture.
3205 * This flag never gets cleared since it's non-trivial to determine
3206 * when all FBOs might be done rendering to this texture. That's OK
3207 * though since it's uncommon to render to a texture then repeatedly
3208 * call glTexImage() to change images in the texture.
3209 */
3210 texObj->_RenderToTexture = GL_TRUE;
3211 }
3212 else {
3213 remove_attachment(ctx, att);
3214 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3215 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3216 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
3217 }
3218 }
3219
3220 invalidate_framebuffer(fb);
3221
3222 mtx_unlock(&fb->Mutex);
3223 }
3224
3225
3226 static void
3227 framebuffer_texture_with_dims(int dims, GLenum target,
3228 GLenum attachment, GLenum textarget,
3229 GLuint texture, GLint level, GLint layer,
3230 const char *caller)
3231 {
3232 GET_CURRENT_CONTEXT(ctx);
3233 struct gl_framebuffer *fb;
3234 struct gl_texture_object *texObj;
3235
3236 /* Get the framebuffer object */
3237 fb = get_framebuffer_target(ctx, target);
3238 if (!fb) {
3239 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
3240 _mesa_enum_to_string(target));
3241 return;
3242 }
3243
3244 /* Get the texture object */
3245 if (!get_texture_for_framebuffer(ctx, texture, false, caller, &texObj))
3246 return;
3247
3248 if (texObj) {
3249 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3250 return;
3251
3252 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3253 return;
3254
3255 if (!check_level(ctx, textarget, level, caller))
3256 return;
3257 }
3258
3259 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3260 layer, GL_FALSE, caller);
3261 }
3262
3263
3264 void GLAPIENTRY
3265 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3266 GLenum textarget, GLuint texture, GLint level)
3267 {
3268 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3269 level, 0, "glFramebufferTexture1D");
3270 }
3271
3272
3273 void GLAPIENTRY
3274 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
3275 GLenum textarget, GLuint texture, GLint level)
3276 {
3277 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3278 level, 0, "glFramebufferTexture2D");
3279 }
3280
3281
3282 void GLAPIENTRY
3283 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
3284 GLenum textarget, GLuint texture,
3285 GLint level, GLint layer)
3286 {
3287 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3288 level, layer, "glFramebufferTexture3D");
3289 }
3290
3291
3292 void GLAPIENTRY
3293 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3294 GLuint texture, GLint level, GLint layer)
3295 {
3296 GET_CURRENT_CONTEXT(ctx);
3297 struct gl_framebuffer *fb;
3298 struct gl_texture_object *texObj;
3299 GLenum textarget = 0;
3300
3301 const char *func = "glFramebufferTextureLayer";
3302
3303 /* Get the framebuffer object */
3304 fb = get_framebuffer_target(ctx, target);
3305 if (!fb) {
3306 _mesa_error(ctx, GL_INVALID_ENUM,
3307 "glFramebufferTextureLayer(invalid target %s)",
3308 _mesa_enum_to_string(target));
3309 return;
3310 }
3311
3312 /* Get the texture object */
3313 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3314 return;
3315
3316 if (texObj) {
3317 if (!check_texture_target(ctx, texObj->Target, func))
3318 return;
3319
3320 if (!check_layer(ctx, texObj->Target, layer, func))
3321 return;
3322
3323 if (!check_level(ctx, texObj->Target, level, func))
3324 return;
3325
3326 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3327 assert(layer >= 0 && layer < 6);
3328 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3329 layer = 0;
3330 }
3331 }
3332
3333 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3334 layer, GL_FALSE, func);
3335 }
3336
3337
3338 void GLAPIENTRY
3339 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3340 GLuint texture, GLint level, GLint layer)
3341 {
3342 GET_CURRENT_CONTEXT(ctx);
3343 struct gl_framebuffer *fb;
3344 struct gl_texture_object *texObj;
3345 GLenum textarget = 0;
3346
3347 const char *func = "glNamedFramebufferTextureLayer";
3348
3349 /* Get the framebuffer object */
3350 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3351 if (!fb)
3352 return;
3353
3354 /* Get the texture object */
3355 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3356 return;
3357
3358 if (texObj) {
3359 if (!check_texture_target(ctx, texObj->Target, func))
3360 return;
3361
3362 if (!check_layer(ctx, texObj->Target, layer, func))
3363 return;
3364
3365 if (!check_level(ctx, texObj->Target, level, func))
3366 return;
3367
3368 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3369 assert(layer >= 0 && layer < 6);
3370 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3371 layer = 0;
3372 }
3373 }
3374
3375 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3376 layer, GL_FALSE, func);
3377 }
3378
3379
3380 void GLAPIENTRY
3381 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3382 GLuint texture, GLint level)
3383 {
3384 GET_CURRENT_CONTEXT(ctx);
3385 struct gl_framebuffer *fb;
3386 struct gl_texture_object *texObj;
3387 GLboolean layered = GL_FALSE;
3388
3389 const char *func = "FramebufferTexture";
3390
3391 if (!_mesa_has_geometry_shaders(ctx)) {
3392 _mesa_error(ctx, GL_INVALID_OPERATION,
3393 "unsupported function (glFramebufferTexture) called");
3394 return;
3395 }
3396
3397 /* Get the framebuffer object */
3398 fb = get_framebuffer_target(ctx, target);
3399 if (!fb) {
3400 _mesa_error(ctx, GL_INVALID_ENUM,
3401 "glFramebufferTexture(invalid target %s)",
3402 _mesa_enum_to_string(target));
3403 return;
3404 }
3405
3406 /* Get the texture object */
3407 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3408 return;
3409
3410 if (texObj) {
3411 if (!check_layered_texture_target(ctx, texObj->Target, func, &layered))
3412 return;
3413
3414 if (!check_level(ctx, texObj->Target, level, func))
3415 return;
3416 }
3417
3418 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3419 0, layered, func);
3420 }
3421
3422
3423 void GLAPIENTRY
3424 _mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3425 GLuint texture, GLint level)
3426 {
3427 GET_CURRENT_CONTEXT(ctx);
3428 struct gl_framebuffer *fb;
3429 struct gl_texture_object *texObj;
3430 GLboolean layered = GL_FALSE;
3431
3432 const char *func = "glNamedFramebufferTexture";
3433
3434 if (!_mesa_has_geometry_shaders(ctx)) {
3435 _mesa_error(ctx, GL_INVALID_OPERATION,
3436 "unsupported function (glNamedFramebufferTexture) called");
3437 return;
3438 }
3439
3440 /* Get the framebuffer object */
3441 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3442 if (!fb)
3443 return;
3444
3445 /* Get the texture object */
3446 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3447 return;
3448
3449 if (texObj) {
3450 if (!check_layered_texture_target(ctx, texObj->Target, func,
3451 &layered))
3452 return;
3453
3454 if (!check_level(ctx, texObj->Target, level, func))
3455 return;
3456 }
3457
3458 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3459 0, layered, func);
3460 }
3461
3462
3463 void
3464 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3465 struct gl_framebuffer *fb,
3466 GLenum attachment,
3467 struct gl_renderbuffer *rb)
3468 {
3469 assert(!_mesa_is_winsys_fbo(fb));
3470
3471 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3472
3473 assert(ctx->Driver.FramebufferRenderbuffer);
3474 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3475
3476 /* Some subsequent GL commands may depend on the framebuffer's visual
3477 * after the binding is updated. Update visual info now.
3478 */
3479 _mesa_update_framebuffer_visual(ctx, fb);
3480 }
3481
3482 static void
3483 framebuffer_renderbuffer(struct gl_context *ctx,
3484 struct gl_framebuffer *fb,
3485 GLenum attachment,
3486 struct gl_renderbuffer *rb,
3487 const char *func)
3488 {
3489 struct gl_renderbuffer_attachment *att;
3490
3491 if (_mesa_is_winsys_fbo(fb)) {
3492 /* Can't attach new renderbuffers to a window system framebuffer */
3493 _mesa_error(ctx, GL_INVALID_OPERATION,
3494 "%s(window-system framebuffer)", func);
3495 return;
3496 }
3497
3498 att = get_attachment(ctx, fb, attachment);
3499 if (att == NULL) {
3500 _mesa_error(ctx, GL_INVALID_ENUM,
3501 "%s(invalid attachment %s)", func,
3502 _mesa_enum_to_string(attachment));
3503 return;
3504 }
3505
3506 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3507 rb && rb->Format != MESA_FORMAT_NONE) {
3508 /* make sure the renderbuffer is a depth/stencil format */
3509 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3510 if (baseFormat != GL_DEPTH_STENCIL) {
3511 _mesa_error(ctx, GL_INVALID_OPERATION,
3512 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3513 return;
3514 }
3515 }
3516
3517 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
3518 }
3519
3520 void GLAPIENTRY
3521 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3522 GLenum renderbuffertarget,
3523 GLuint renderbuffer)
3524 {
3525 struct gl_framebuffer *fb;
3526 struct gl_renderbuffer *rb;
3527 GET_CURRENT_CONTEXT(ctx);
3528
3529 fb = get_framebuffer_target(ctx, target);
3530 if (!fb) {
3531 _mesa_error(ctx, GL_INVALID_ENUM,
3532 "glFramebufferRenderbuffer(invalid target %s)",
3533 _mesa_enum_to_string(target));
3534 return;
3535 }
3536
3537 if (renderbuffertarget != GL_RENDERBUFFER) {
3538 _mesa_error(ctx, GL_INVALID_ENUM,
3539 "glFramebufferRenderbuffer(renderbuffertarget is not "
3540 "GL_RENDERBUFFER)");
3541 return;
3542 }
3543
3544 if (renderbuffer) {
3545 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3546 "glFramebufferRenderbuffer");
3547 if (!rb)
3548 return;
3549 }
3550 else {
3551 /* remove renderbuffer attachment */
3552 rb = NULL;
3553 }
3554
3555 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3556 "glFramebufferRenderbuffer");
3557 }
3558
3559
3560 void GLAPIENTRY
3561 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3562 GLenum renderbuffertarget,
3563 GLuint renderbuffer)
3564 {
3565 struct gl_framebuffer *fb;
3566 struct gl_renderbuffer *rb;
3567 GET_CURRENT_CONTEXT(ctx);
3568
3569 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3570 "glNamedFramebufferRenderbuffer");
3571 if (!fb)
3572 return;
3573
3574 if (renderbuffertarget != GL_RENDERBUFFER) {
3575 _mesa_error(ctx, GL_INVALID_ENUM,
3576 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3577 "GL_RENDERBUFFER)");
3578 return;
3579 }
3580
3581 if (renderbuffer) {
3582 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3583 "glNamedFramebufferRenderbuffer");
3584 if (!rb)
3585 return;
3586 }
3587 else {
3588 /* remove renderbuffer attachment */
3589 rb = NULL;
3590 }
3591
3592 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3593 "glNamedFramebufferRenderbuffer");
3594 }
3595
3596
3597 void
3598 _mesa_get_framebuffer_attachment_parameter(struct gl_context *ctx,
3599 struct gl_framebuffer *buffer,
3600 GLenum attachment, GLenum pname,
3601 GLint *params, const char *caller)
3602 {
3603 const struct gl_renderbuffer_attachment *att;
3604 GLenum err;
3605
3606 /* The error code for an attachment type of GL_NONE differs between APIs.
3607 *
3608 * From the ES 2.0.25 specification, page 127:
3609 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3610 * querying any other pname will generate INVALID_ENUM."
3611 *
3612 * From the OpenGL 3.0 specification, page 337, or identically,
3613 * the OpenGL ES 3.0.4 specification, page 240:
3614 *
3615 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3616 * framebuffer is bound to target. In this case querying pname
3617 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3618 * queries will generate an INVALID_OPERATION error."
3619 */
3620 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3621 GL_INVALID_ENUM : GL_INVALID_OPERATION;
3622
3623 if (_mesa_is_winsys_fbo(buffer)) {
3624 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3625 * says:
3626 *
3627 * "If the framebuffer currently bound to target is zero, then
3628 * INVALID_OPERATION is generated."
3629 *
3630 * The EXT_framebuffer_object spec has the same wording, and the
3631 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3632 * spec.
3633 */
3634 if ((!_mesa_is_desktop_gl(ctx) ||
3635 !ctx->Extensions.ARB_framebuffer_object)
3636 && !_mesa_is_gles3(ctx)) {
3637 _mesa_error(ctx, GL_INVALID_OPERATION,
3638 "%s(window-system framebuffer)", caller);
3639 return;
3640 }
3641
3642 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3643 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3644 _mesa_error(ctx, GL_INVALID_ENUM,
3645 "%s(invalid attachment %s)", caller,
3646 _mesa_enum_to_string(attachment));
3647 return;
3648 }
3649
3650 /* The specs are not clear about how to handle
3651 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3652 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3653 * discussed in:
3654 *
3655 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3656 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3657 */
3658 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3659 _mesa_error(ctx, GL_INVALID_ENUM,
3660 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3661 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3662 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3663 return;
3664 }
3665
3666 /* the default / window-system FBO */
3667 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
3668 }
3669 else {
3670 /* user-created framebuffer FBO */
3671 att = get_attachment(ctx, buffer, attachment);
3672 }
3673
3674 if (att == NULL) {
3675 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3676 _mesa_enum_to_string(attachment));
3677 return;
3678 }
3679
3680 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3681 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3682 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3683 /* This behavior is first specified in OpenGL 4.4 specification.
3684 *
3685 * From the OpenGL 4.4 spec page 275:
3686 * "This query cannot be performed for a combined depth+stencil
3687 * attachment, since it does not have a single format."
3688 */
3689 _mesa_error(ctx, GL_INVALID_OPERATION,
3690 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3691 " is invalid for depth+stencil attachment)", caller);
3692 return;
3693 }
3694 /* the depth and stencil attachments must point to the same buffer */
3695 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
3696 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
3697 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3698 _mesa_error(ctx, GL_INVALID_OPERATION,
3699 "%s(DEPTH/STENCIL attachments differ)", caller);
3700 return;
3701 }
3702 }
3703
3704 /* No need to flush here */
3705
3706 switch (pname) {
3707 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3708 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3709 *
3710 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3711 * either no framebuffer is bound to target; or the default framebuffer
3712 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3713 * stencil bits, respectively, is zero."
3714 */
3715 *params = (_mesa_is_winsys_fbo(buffer) &&
3716 ((attachment != GL_DEPTH && attachment != GL_STENCIL) ||
3717 (att->Type != GL_NONE)))
3718 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
3719 return;
3720 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3721 if (att->Type == GL_RENDERBUFFER_EXT) {
3722 *params = att->Renderbuffer->Name;
3723 }
3724 else if (att->Type == GL_TEXTURE) {
3725 *params = att->Texture->Name;
3726 }
3727 else {
3728 assert(att->Type == GL_NONE);
3729 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3730 *params = 0;
3731 } else {
3732 goto invalid_pname_enum;
3733 }
3734 }
3735 return;
3736 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3737 if (att->Type == GL_TEXTURE) {
3738 *params = att->TextureLevel;
3739 }
3740 else if (att->Type == GL_NONE) {
3741 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3742 _mesa_enum_to_string(pname));
3743 }
3744 else {
3745 goto invalid_pname_enum;
3746 }
3747 return;
3748 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3749 if (att->Type == GL_TEXTURE) {
3750 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3751 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3752 }
3753 else {
3754 *params = 0;
3755 }
3756 }
3757 else if (att->Type == GL_NONE) {
3758 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3759 _mesa_enum_to_string(pname));
3760 }
3761 else {
3762 goto invalid_pname_enum;
3763 }
3764 return;
3765 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3766 if (ctx->API == API_OPENGLES) {
3767 goto invalid_pname_enum;
3768 } else if (att->Type == GL_NONE) {
3769 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3770 _mesa_enum_to_string(pname));
3771 } else if (att->Type == GL_TEXTURE) {
3772 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3773 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
3774 *params = att->Zoffset;
3775 }
3776 else {
3777 *params = 0;
3778 }
3779 }
3780 else {
3781 goto invalid_pname_enum;
3782 }
3783 return;
3784 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3785 if ((!_mesa_is_desktop_gl(ctx) ||
3786 !ctx->Extensions.ARB_framebuffer_object)
3787 && !_mesa_is_gles3(ctx)) {
3788 goto invalid_pname_enum;
3789 }
3790 else if (att->Type == GL_NONE) {
3791 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3792 _mesa_enum_to_string(pname));
3793 }
3794 else {
3795 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3796 *params =
3797 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
3798 }
3799 else {
3800 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3801 * if the sRGB conversion is unsupported. */
3802 *params = GL_LINEAR;
3803 }
3804 }
3805 return;
3806 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3807 if ((ctx->API != API_OPENGL_COMPAT ||
3808 !ctx->Extensions.ARB_framebuffer_object)
3809 && ctx->API != API_OPENGL_CORE
3810 && !_mesa_is_gles3(ctx)) {
3811 goto invalid_pname_enum;
3812 }
3813 else if (att->Type == GL_NONE) {
3814 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3815 _mesa_enum_to_string(pname));
3816 }
3817 else {
3818 mesa_format format = att->Renderbuffer->Format;
3819
3820 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3821 * 3.0.1 spec says:
3822 *
3823 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3824 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3825 * generate an INVALID_OPERATION error.
3826 */
3827 if (_mesa_is_gles3(ctx) &&
3828 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3829 _mesa_error(ctx, GL_INVALID_OPERATION,
3830 "%s(cannot query "
3831 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3832 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
3833 return;
3834 }
3835
3836 if (format == MESA_FORMAT_S_UINT8) {
3837 /* special cases */
3838 *params = GL_INDEX;
3839 }
3840 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
3841 /* depends on the attachment parameter */
3842 if (attachment == GL_STENCIL_ATTACHMENT) {
3843 *params = GL_INDEX;
3844 }
3845 else {
3846 *params = GL_FLOAT;
3847 }
3848 }
3849 else {
3850 *params = _mesa_get_format_datatype(format);
3851 }
3852 }
3853 return;
3854 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3855 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3856 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3857 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3858 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3859 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3860 if ((!_mesa_is_desktop_gl(ctx) ||
3861 !ctx->Extensions.ARB_framebuffer_object)
3862 && !_mesa_is_gles3(ctx)) {
3863 goto invalid_pname_enum;
3864 }
3865 else if (att->Type == GL_NONE) {
3866 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3867 _mesa_enum_to_string(pname));
3868 }
3869 else if (att->Texture) {
3870 const struct gl_texture_image *texImage =
3871 _mesa_select_tex_image(att->Texture, att->Texture->Target,
3872 att->TextureLevel);
3873 if (texImage) {
3874 *params = get_component_bits(pname, texImage->_BaseFormat,
3875 texImage->TexFormat);
3876 }
3877 else {
3878 *params = 0;
3879 }
3880 }
3881 else if (att->Renderbuffer) {
3882 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
3883 att->Renderbuffer->Format);
3884 }
3885 else {
3886 _mesa_problem(ctx, "%s: invalid FBO attachment structure", caller);
3887 }
3888 return;
3889 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
3890 if (!_mesa_has_geometry_shaders(ctx)) {
3891 goto invalid_pname_enum;
3892 } else if (att->Type == GL_TEXTURE) {
3893 *params = att->Layered;
3894 } else if (att->Type == GL_NONE) {
3895 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3896 _mesa_enum_to_string(pname));
3897 } else {
3898 goto invalid_pname_enum;
3899 }
3900 return;
3901 default:
3902 goto invalid_pname_enum;
3903 }
3904
3905 return;
3906
3907 invalid_pname_enum:
3908 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
3909 _mesa_enum_to_string(pname));
3910 return;
3911 }
3912
3913
3914 void GLAPIENTRY
3915 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
3916 GLenum pname, GLint *params)
3917 {
3918 GET_CURRENT_CONTEXT(ctx);
3919 struct gl_framebuffer *buffer;
3920
3921 buffer = get_framebuffer_target(ctx, target);
3922 if (!buffer) {
3923 _mesa_error(ctx, GL_INVALID_ENUM,
3924 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
3925 _mesa_enum_to_string(target));
3926 return;
3927 }
3928
3929 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3930 params,
3931 "glGetFramebufferAttachmentParameteriv");
3932 }
3933
3934
3935 void GLAPIENTRY
3936 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
3937 GLenum attachment,
3938 GLenum pname, GLint *params)
3939 {
3940 GET_CURRENT_CONTEXT(ctx);
3941 struct gl_framebuffer *buffer;
3942
3943 if (framebuffer) {
3944 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3945 "glGetNamedFramebufferAttachmentParameteriv");
3946 if (!buffer)
3947 return;
3948 }
3949 else {
3950 /*
3951 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
3952 * 4.5 core spec (30.10.2014, PDF page 314):
3953 * "If framebuffer is zero, then the default draw framebuffer is
3954 * queried."
3955 */
3956 buffer = ctx->WinSysDrawBuffer;
3957 }
3958
3959 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3960 params,
3961 "glGetNamedFramebufferAttachmentParameteriv");
3962 }
3963
3964
3965 void GLAPIENTRY
3966 _mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
3967 GLint param)
3968 {
3969 GET_CURRENT_CONTEXT(ctx);
3970 struct gl_framebuffer *fb = NULL;
3971
3972 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3973 _mesa_error(ctx, GL_INVALID_OPERATION,
3974 "glNamedFramebufferParameteri("
3975 "ARB_framebuffer_no_attachments not implemented)");
3976 return;
3977 }
3978
3979 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3980 "glNamedFramebufferParameteri");
3981
3982 if (fb) {
3983 framebuffer_parameteri(ctx, fb, pname, param,
3984 "glNamedFramebufferParameteriv");
3985 }
3986 }
3987
3988
3989 void GLAPIENTRY
3990 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
3991 GLint *param)
3992 {
3993 GET_CURRENT_CONTEXT(ctx);
3994 struct gl_framebuffer *fb;
3995
3996 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3997 _mesa_error(ctx, GL_INVALID_OPERATION,
3998 "glNamedFramebufferParameteriv("
3999 "ARB_framebuffer_no_attachments not implemented)");
4000 return;
4001 }
4002
4003 if (framebuffer) {
4004 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4005 "glGetNamedFramebufferParameteriv");
4006 } else {
4007 fb = ctx->WinSysDrawBuffer;
4008 }
4009
4010 if (fb) {
4011 get_framebuffer_parameteriv(ctx, fb, pname, param,
4012 "glGetNamedFramebufferParameteriv");
4013 }
4014 }
4015
4016
4017 static void
4018 invalidate_framebuffer_storage(struct gl_context *ctx,
4019 struct gl_framebuffer *fb,
4020 GLsizei numAttachments,
4021 const GLenum *attachments, GLint x, GLint y,
4022 GLsizei width, GLsizei height, const char *name)
4023 {
4024 int i;
4025
4026 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4027 * Spec (2.2.2015, PDF page 522) says:
4028 * "An INVALID_VALUE error is generated if numAttachments, width, or
4029 * height is negative."
4030 */
4031 if (numAttachments < 0) {
4032 _mesa_error(ctx, GL_INVALID_VALUE,
4033 "%s(numAttachments < 0)", name);
4034 return;
4035 }
4036
4037 if (width < 0) {
4038 _mesa_error(ctx, GL_INVALID_VALUE,
4039 "%s(width < 0)", name);
4040 return;
4041 }
4042
4043 if (height < 0) {
4044 _mesa_error(ctx, GL_INVALID_VALUE,
4045 "%s(height < 0)", name);
4046 return;
4047 }
4048
4049 /* The GL_ARB_invalidate_subdata spec says:
4050 *
4051 * "If an attachment is specified that does not exist in the
4052 * framebuffer bound to <target>, it is ignored."
4053 *
4054 * It also says:
4055 *
4056 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4057 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4058 * INVALID_OPERATION is generated."
4059 *
4060 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4061 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4062 * set of retrictions).
4063 */
4064 for (i = 0; i < numAttachments; i++) {
4065 if (_mesa_is_winsys_fbo(fb)) {
4066 switch (attachments[i]) {
4067 case GL_ACCUM:
4068 case GL_AUX0:
4069 case GL_AUX1:
4070 case GL_AUX2:
4071 case GL_AUX3:
4072 /* Accumulation buffers and auxilary buffers were removed in
4073 * OpenGL 3.1, and they never existed in OpenGL ES.
4074 */
4075 if (ctx->API != API_OPENGL_COMPAT)
4076 goto invalid_enum;
4077 break;
4078 case GL_COLOR:
4079 case GL_DEPTH:
4080 case GL_STENCIL:
4081 break;
4082 case GL_BACK_LEFT:
4083 case GL_BACK_RIGHT:
4084 case GL_FRONT_LEFT:
4085 case GL_FRONT_RIGHT:
4086 if (!_mesa_is_desktop_gl(ctx))
4087 goto invalid_enum;
4088 break;
4089 default:
4090 goto invalid_enum;
4091 }
4092 } else {
4093 switch (attachments[i]) {
4094 case GL_DEPTH_ATTACHMENT:
4095 case GL_STENCIL_ATTACHMENT:
4096 break;
4097 case GL_DEPTH_STENCIL_ATTACHMENT:
4098 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4099 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4100 * extension does not make this attachment point valid on ES 2.0.
4101 */
4102 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4103 break;
4104 /* fallthrough */
4105 case GL_COLOR_ATTACHMENT0:
4106 case GL_COLOR_ATTACHMENT1:
4107 case GL_COLOR_ATTACHMENT2:
4108 case GL_COLOR_ATTACHMENT3:
4109 case GL_COLOR_ATTACHMENT4:
4110 case GL_COLOR_ATTACHMENT5:
4111 case GL_COLOR_ATTACHMENT6:
4112 case GL_COLOR_ATTACHMENT7:
4113 case GL_COLOR_ATTACHMENT8:
4114 case GL_COLOR_ATTACHMENT9:
4115 case GL_COLOR_ATTACHMENT10:
4116 case GL_COLOR_ATTACHMENT11:
4117 case GL_COLOR_ATTACHMENT12:
4118 case GL_COLOR_ATTACHMENT13:
4119 case GL_COLOR_ATTACHMENT14:
4120 case GL_COLOR_ATTACHMENT15: {
4121 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
4122 if (k >= ctx->Const.MaxColorAttachments) {
4123 _mesa_error(ctx, GL_INVALID_OPERATION,
4124 "%s(attachment >= max. color attachments)", name);
4125 return;
4126 }
4127 break;
4128 }
4129 default:
4130 goto invalid_enum;
4131 }
4132 }
4133 }
4134
4135 /* We don't actually do anything for this yet. Just return after
4136 * validating the parameters and generating the required errors.
4137 */
4138 return;
4139
4140 invalid_enum:
4141 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
4142 _mesa_enum_to_string(attachments[i]));
4143 return;
4144 }
4145
4146
4147 void GLAPIENTRY
4148 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4149 const GLenum *attachments, GLint x, GLint y,
4150 GLsizei width, GLsizei height)
4151 {
4152 struct gl_framebuffer *fb;
4153 GET_CURRENT_CONTEXT(ctx);
4154
4155 fb = get_framebuffer_target(ctx, target);
4156 if (!fb) {
4157 _mesa_error(ctx, GL_INVALID_ENUM,
4158 "glInvalidateSubFramebuffer(invalid target %s)",
4159 _mesa_enum_to_string(target));
4160 return;
4161 }
4162
4163 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4164 x, y, width, height,
4165 "glInvalidateSubFramebuffer");
4166 }
4167
4168
4169 void GLAPIENTRY
4170 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4171 GLsizei numAttachments,
4172 const GLenum *attachments,
4173 GLint x, GLint y,
4174 GLsizei width, GLsizei height)
4175 {
4176 struct gl_framebuffer *fb;
4177 GET_CURRENT_CONTEXT(ctx);
4178
4179 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4180 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4181 * default draw framebuffer is affected."
4182 */
4183 if (framebuffer) {
4184 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4185 "glInvalidateNamedFramebufferSubData");
4186 if (!fb)
4187 return;
4188 }
4189 else
4190 fb = ctx->WinSysDrawBuffer;
4191
4192 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4193 x, y, width, height,
4194 "glInvalidateNamedFramebufferSubData");
4195 }
4196
4197
4198 void GLAPIENTRY
4199 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4200 const GLenum *attachments)
4201 {
4202 struct gl_framebuffer *fb;
4203 GET_CURRENT_CONTEXT(ctx);
4204
4205 fb = get_framebuffer_target(ctx, target);
4206 if (!fb) {
4207 _mesa_error(ctx, GL_INVALID_ENUM,
4208 "glInvalidateFramebuffer(invalid target %s)",
4209 _mesa_enum_to_string(target));
4210 return;
4211 }
4212
4213 /* The GL_ARB_invalidate_subdata spec says:
4214 *
4215 * "The command
4216 *
4217 * void InvalidateFramebuffer(enum target,
4218 * sizei numAttachments,
4219 * const enum *attachments);
4220 *
4221 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4222 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4223 * <MAX_VIEWPORT_DIMS[1]> respectively."
4224 */
4225 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4226 0, 0,
4227 ctx->Const.MaxViewportWidth,
4228 ctx->Const.MaxViewportHeight,
4229 "glInvalidateFramebuffer");
4230 }
4231
4232
4233 void GLAPIENTRY
4234 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4235 GLsizei numAttachments,
4236 const GLenum *attachments)
4237 {
4238 struct gl_framebuffer *fb;
4239 GET_CURRENT_CONTEXT(ctx);
4240
4241 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4242 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4243 * default draw framebuffer is affected."
4244 */
4245 if (framebuffer) {
4246 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4247 "glInvalidateNamedFramebufferData");
4248 if (!fb)
4249 return;
4250 }
4251 else
4252 fb = ctx->WinSysDrawBuffer;
4253
4254 /* The GL_ARB_invalidate_subdata spec says:
4255 *
4256 * "The command
4257 *
4258 * void InvalidateFramebuffer(enum target,
4259 * sizei numAttachments,
4260 * const enum *attachments);
4261 *
4262 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4263 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4264 * <MAX_VIEWPORT_DIMS[1]> respectively."
4265 */
4266 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4267 0, 0,
4268 ctx->Const.MaxViewportWidth,
4269 ctx->Const.MaxViewportHeight,
4270 "glInvalidateNamedFramebufferData");
4271 }
4272
4273
4274 void GLAPIENTRY
4275 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4276 const GLenum *attachments)
4277 {
4278 struct gl_framebuffer *fb;
4279 GLint i;
4280
4281 GET_CURRENT_CONTEXT(ctx);
4282
4283 fb = get_framebuffer_target(ctx, target);
4284 if (!fb) {
4285 _mesa_error(ctx, GL_INVALID_ENUM,
4286 "glDiscardFramebufferEXT(target %s)",
4287 _mesa_enum_to_string(target));
4288 return;
4289 }
4290
4291 if (numAttachments < 0) {
4292 _mesa_error(ctx, GL_INVALID_VALUE,
4293 "glDiscardFramebufferEXT(numAttachments < 0)");
4294 return;
4295 }
4296
4297 for (i = 0; i < numAttachments; i++) {
4298 switch (attachments[i]) {
4299 case GL_COLOR:
4300 case GL_DEPTH:
4301 case GL_STENCIL:
4302 if (_mesa_is_user_fbo(fb))
4303 goto invalid_enum;
4304 break;
4305 case GL_COLOR_ATTACHMENT0:
4306 case GL_DEPTH_ATTACHMENT:
4307 case GL_STENCIL_ATTACHMENT:
4308 if (_mesa_is_winsys_fbo(fb))
4309 goto invalid_enum;
4310 break;
4311 default:
4312 goto invalid_enum;
4313 }
4314 }
4315
4316 if (ctx->Driver.DiscardFramebuffer)
4317 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4318
4319 return;
4320
4321 invalid_enum:
4322 _mesa_error(ctx, GL_INVALID_ENUM,
4323 "glDiscardFramebufferEXT(attachment %s)",
4324 _mesa_enum_to_string(attachments[i]));
4325 }