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