bed5b2573e4d6048d0a5595d143e2d1875b78a6f
[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 void
1481 get_framebuffer_parameteriv(struct gl_context *ctx, struct gl_framebuffer *fb,
1482 GLenum pname, GLint *params, const char *func)
1483 {
1484 switch (pname) {
1485 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1486 *params = fb->DefaultGeometry.Width;
1487 break;
1488 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1489 *params = fb->DefaultGeometry.Height;
1490 break;
1491 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1492 /*
1493 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1494 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1495 */
1496 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
1497 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1498 break;
1499 }
1500 *params = fb->DefaultGeometry.Layers;
1501 break;
1502 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1503 *params = fb->DefaultGeometry.NumSamples;
1504 break;
1505 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1506 *params = fb->DefaultGeometry.FixedSampleLocations;
1507 break;
1508 case GL_DOUBLEBUFFER:
1509 *params = fb->Visual.doubleBufferMode;
1510 break;
1511 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1512 *params = _mesa_get_color_read_format(ctx, fb, func);
1513 break;
1514 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1515 *params = _mesa_get_color_read_type(ctx, fb, func);
1516 break;
1517 case GL_SAMPLES:
1518 *params = _mesa_geometric_samples(fb);
1519 break;
1520 case GL_SAMPLE_BUFFERS:
1521 *params = _mesa_geometric_samples(fb) > 0;
1522 break;
1523 case GL_STEREO:
1524 *params = fb->Visual.stereoMode;
1525 break;
1526 default:
1527 _mesa_error(ctx, GL_INVALID_ENUM,
1528 "%s(pname=0x%x)", func, pname);
1529 }
1530 }
1531
1532 void GLAPIENTRY
1533 _mesa_GetFramebufferParameteriv(GLenum target, GLenum pname, GLint *params)
1534 {
1535 GET_CURRENT_CONTEXT(ctx);
1536 struct gl_framebuffer *fb;
1537
1538 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1539 _mesa_error(ctx, GL_INVALID_OPERATION,
1540 "glGetFramebufferParameteriv not supported "
1541 "(ARB_framebuffer_no_attachments not implemented)");
1542 return;
1543 }
1544
1545 fb = get_framebuffer_target(ctx, target);
1546 if (!fb) {
1547 _mesa_error(ctx, GL_INVALID_ENUM,
1548 "glGetFramebufferParameteriv(target=0x%x)", target);
1549 return;
1550 }
1551
1552 /* check framebuffer binding */
1553 if (_mesa_is_winsys_fbo(fb)) {
1554 _mesa_error(ctx, GL_INVALID_OPERATION,
1555 "glGetFramebufferParameteriv");
1556 return;
1557 }
1558
1559 get_framebuffer_parameteriv(ctx, fb, pname, params,
1560 "glGetFramebufferParameteriv");
1561 }
1562
1563
1564 /**
1565 * Remove the specified renderbuffer or texture from any attachment point in
1566 * the framebuffer.
1567 *
1568 * \returns
1569 * \c true if the renderbuffer was detached from an attachment point. \c
1570 * false otherwise.
1571 */
1572 bool
1573 _mesa_detach_renderbuffer(struct gl_context *ctx,
1574 struct gl_framebuffer *fb,
1575 const void *att)
1576 {
1577 unsigned i;
1578 bool progress = false;
1579
1580 for (i = 0; i < BUFFER_COUNT; i++) {
1581 if (fb->Attachment[i].Texture == att
1582 || fb->Attachment[i].Renderbuffer == att) {
1583 remove_attachment(ctx, &fb->Attachment[i]);
1584 progress = true;
1585 }
1586 }
1587
1588 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1589 * Completeness," of the OpenGL 3.1 spec says:
1590 *
1591 * "Performing any of the following actions may change whether the
1592 * framebuffer is considered complete or incomplete:
1593 *
1594 * ...
1595 *
1596 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1597 * containing an image that is attached to a framebuffer object
1598 * that is bound to the framebuffer."
1599 */
1600 if (progress)
1601 invalidate_framebuffer(fb);
1602
1603 return progress;
1604 }
1605
1606
1607 void GLAPIENTRY
1608 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1609 {
1610 GLint i;
1611 GET_CURRENT_CONTEXT(ctx);
1612
1613 if (n < 0) {
1614 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1615 return;
1616 }
1617
1618 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1619
1620 for (i = 0; i < n; i++) {
1621 if (renderbuffers[i] > 0) {
1622 struct gl_renderbuffer *rb;
1623 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1624 if (rb) {
1625 /* check if deleting currently bound renderbuffer object */
1626 if (rb == ctx->CurrentRenderbuffer) {
1627 /* bind default */
1628 assert(rb->RefCount >= 2);
1629 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1630 }
1631
1632 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1633 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1634 * of the OpenGL 3.1 spec says:
1635 *
1636 * "If a renderbuffer object is deleted while its image is
1637 * attached to one or more attachment points in the currently
1638 * bound framebuffer, then it is as if FramebufferRenderbuffer
1639 * had been called, with a renderbuffer of 0, for each
1640 * attachment point to which this image was attached in the
1641 * currently bound framebuffer. In other words, this
1642 * renderbuffer image is first detached from all attachment
1643 * points in the currently bound framebuffer. Note that the
1644 * renderbuffer image is specifically not detached from any
1645 * non-bound framebuffers. Detaching the image from any
1646 * non-bound framebuffers is the responsibility of the
1647 * application.
1648 */
1649 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1650 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1651 }
1652 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1653 && ctx->ReadBuffer != ctx->DrawBuffer) {
1654 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1655 }
1656
1657 /* Remove from hash table immediately, to free the ID.
1658 * But the object will not be freed until it's no longer
1659 * referenced anywhere else.
1660 */
1661 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1662
1663 if (rb != &DummyRenderbuffer) {
1664 /* no longer referenced by hash table */
1665 _mesa_reference_renderbuffer(&rb, NULL);
1666 }
1667 }
1668 }
1669 }
1670 }
1671
1672 static void
1673 create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1674 bool dsa)
1675 {
1676 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
1677 GLuint first;
1678 GLint i;
1679
1680 if (n < 0) {
1681 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
1682 return;
1683 }
1684
1685 if (!renderbuffers)
1686 return;
1687
1688 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1689
1690 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1691
1692 for (i = 0; i < n; i++) {
1693 GLuint name = first + i;
1694 renderbuffers[i] = name;
1695
1696 if (dsa) {
1697 allocate_renderbuffer_locked(ctx, name, func);
1698 } else {
1699 /* insert a dummy renderbuffer into the hash table */
1700 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, name,
1701 &DummyRenderbuffer);
1702 }
1703 }
1704
1705 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
1706 }
1707
1708
1709 void GLAPIENTRY
1710 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1711 {
1712 GET_CURRENT_CONTEXT(ctx);
1713 create_render_buffers(ctx, n, renderbuffers, false);
1714 }
1715
1716
1717 void GLAPIENTRY
1718 _mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1719 {
1720 GET_CURRENT_CONTEXT(ctx);
1721 create_render_buffers(ctx, n, renderbuffers, true);
1722 }
1723
1724
1725 /**
1726 * Given an internal format token for a render buffer, return the
1727 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1728 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1729 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1730 *
1731 * This is similar to _mesa_base_tex_format() but the set of valid
1732 * internal formats is different.
1733 *
1734 * Note that even if a format is determined to be legal here, validation
1735 * of the FBO may fail if the format is not supported by the driver/GPU.
1736 *
1737 * \param internalFormat as passed to glRenderbufferStorage()
1738 * \return the base internal format, or 0 if internalFormat is illegal
1739 */
1740 GLenum
1741 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1742 {
1743 /*
1744 * Notes: some formats such as alpha, luminance, etc. were added
1745 * with GL_ARB_framebuffer_object.
1746 */
1747 switch (internalFormat) {
1748 case GL_ALPHA:
1749 case GL_ALPHA4:
1750 case GL_ALPHA8:
1751 case GL_ALPHA12:
1752 case GL_ALPHA16:
1753 return (ctx->API == API_OPENGL_COMPAT &&
1754 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
1755 case GL_LUMINANCE:
1756 case GL_LUMINANCE4:
1757 case GL_LUMINANCE8:
1758 case GL_LUMINANCE12:
1759 case GL_LUMINANCE16:
1760 return (ctx->API == API_OPENGL_COMPAT &&
1761 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
1762 case GL_LUMINANCE_ALPHA:
1763 case GL_LUMINANCE4_ALPHA4:
1764 case GL_LUMINANCE6_ALPHA2:
1765 case GL_LUMINANCE8_ALPHA8:
1766 case GL_LUMINANCE12_ALPHA4:
1767 case GL_LUMINANCE12_ALPHA12:
1768 case GL_LUMINANCE16_ALPHA16:
1769 return (ctx->API == API_OPENGL_COMPAT &&
1770 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
1771 case GL_INTENSITY:
1772 case GL_INTENSITY4:
1773 case GL_INTENSITY8:
1774 case GL_INTENSITY12:
1775 case GL_INTENSITY16:
1776 return (ctx->API == API_OPENGL_COMPAT &&
1777 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
1778 case GL_RGB8:
1779 return GL_RGB;
1780 case GL_RGB:
1781 case GL_R3_G3_B2:
1782 case GL_RGB4:
1783 case GL_RGB5:
1784 case GL_RGB10:
1785 case GL_RGB12:
1786 case GL_RGB16:
1787 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1788 case GL_SRGB8_EXT:
1789 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1790 case GL_RGBA4:
1791 case GL_RGB5_A1:
1792 case GL_RGBA8:
1793 return GL_RGBA;
1794 case GL_RGBA:
1795 case GL_RGBA2:
1796 case GL_RGBA12:
1797 case GL_RGBA16:
1798 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1799 case GL_RGB10_A2:
1800 case GL_SRGB8_ALPHA8_EXT:
1801 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1802 case GL_STENCIL_INDEX:
1803 case GL_STENCIL_INDEX1_EXT:
1804 case GL_STENCIL_INDEX4_EXT:
1805 case GL_STENCIL_INDEX16_EXT:
1806 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1807 * OpenGL ES, but Mesa does not currently support them.
1808 */
1809 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1810 case GL_STENCIL_INDEX8_EXT:
1811 return GL_STENCIL_INDEX;
1812 case GL_DEPTH_COMPONENT:
1813 case GL_DEPTH_COMPONENT32:
1814 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1815 case GL_DEPTH_COMPONENT16:
1816 case GL_DEPTH_COMPONENT24:
1817 return GL_DEPTH_COMPONENT;
1818 case GL_DEPTH_STENCIL:
1819 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1820 case GL_DEPTH24_STENCIL8:
1821 return GL_DEPTH_STENCIL;
1822 case GL_DEPTH_COMPONENT32F:
1823 return ctx->Version >= 30
1824 || (ctx->API == API_OPENGL_COMPAT &&
1825 ctx->Extensions.ARB_depth_buffer_float)
1826 ? GL_DEPTH_COMPONENT : 0;
1827 case GL_DEPTH32F_STENCIL8:
1828 return ctx->Version >= 30
1829 || (ctx->API == API_OPENGL_COMPAT &&
1830 ctx->Extensions.ARB_depth_buffer_float)
1831 ? GL_DEPTH_STENCIL : 0;
1832 case GL_RED:
1833 case GL_R16:
1834 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1835 ? GL_RED : 0;
1836 case GL_R8:
1837 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1838 ? GL_RED : 0;
1839 case GL_RG:
1840 case GL_RG16:
1841 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1842 ? GL_RG : 0;
1843 case GL_RG8:
1844 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1845 ? GL_RG : 0;
1846 /* signed normalized texture formats */
1847 case GL_RED_SNORM:
1848 case GL_R8_SNORM:
1849 case GL_R16_SNORM:
1850 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1851 ? GL_RED : 0;
1852 case GL_RG_SNORM:
1853 case GL_RG8_SNORM:
1854 case GL_RG16_SNORM:
1855 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1856 ? GL_RG : 0;
1857 case GL_RGB_SNORM:
1858 case GL_RGB8_SNORM:
1859 case GL_RGB16_SNORM:
1860 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1861 ? GL_RGB : 0;
1862 case GL_RGBA_SNORM:
1863 case GL_RGBA8_SNORM:
1864 case GL_RGBA16_SNORM:
1865 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1866 ? GL_RGBA : 0;
1867 case GL_ALPHA_SNORM:
1868 case GL_ALPHA8_SNORM:
1869 case GL_ALPHA16_SNORM:
1870 return ctx->API == API_OPENGL_COMPAT &&
1871 ctx->Extensions.EXT_texture_snorm &&
1872 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1873 case GL_LUMINANCE_SNORM:
1874 case GL_LUMINANCE8_SNORM:
1875 case GL_LUMINANCE16_SNORM:
1876 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1877 ? GL_LUMINANCE : 0;
1878 case GL_LUMINANCE_ALPHA_SNORM:
1879 case GL_LUMINANCE8_ALPHA8_SNORM:
1880 case GL_LUMINANCE16_ALPHA16_SNORM:
1881 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1882 ? GL_LUMINANCE_ALPHA : 0;
1883 case GL_INTENSITY_SNORM:
1884 case GL_INTENSITY8_SNORM:
1885 case GL_INTENSITY16_SNORM:
1886 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1887 ? GL_INTENSITY : 0;
1888
1889 case GL_R16F:
1890 case GL_R32F:
1891 return ((_mesa_is_desktop_gl(ctx) &&
1892 ctx->Extensions.ARB_texture_rg &&
1893 ctx->Extensions.ARB_texture_float) ||
1894 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1895 ? GL_RED : 0;
1896 case GL_RG16F:
1897 case GL_RG32F:
1898 return ((_mesa_is_desktop_gl(ctx) &&
1899 ctx->Extensions.ARB_texture_rg &&
1900 ctx->Extensions.ARB_texture_float) ||
1901 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1902 ? GL_RG : 0;
1903 case GL_RGB16F:
1904 case GL_RGB32F:
1905 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1906 ? GL_RGB : 0;
1907 case GL_RGBA16F:
1908 case GL_RGBA32F:
1909 return ((_mesa_is_desktop_gl(ctx) &&
1910 ctx->Extensions.ARB_texture_float) ||
1911 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1912 ? GL_RGBA : 0;
1913 case GL_ALPHA16F_ARB:
1914 case GL_ALPHA32F_ARB:
1915 return ctx->API == API_OPENGL_COMPAT &&
1916 ctx->Extensions.ARB_texture_float &&
1917 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1918 case GL_LUMINANCE16F_ARB:
1919 case GL_LUMINANCE32F_ARB:
1920 return ctx->API == API_OPENGL_COMPAT &&
1921 ctx->Extensions.ARB_texture_float &&
1922 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1923 case GL_LUMINANCE_ALPHA16F_ARB:
1924 case GL_LUMINANCE_ALPHA32F_ARB:
1925 return ctx->API == API_OPENGL_COMPAT &&
1926 ctx->Extensions.ARB_texture_float &&
1927 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1928 case GL_INTENSITY16F_ARB:
1929 case GL_INTENSITY32F_ARB:
1930 return ctx->API == API_OPENGL_COMPAT &&
1931 ctx->Extensions.ARB_texture_float &&
1932 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1933 case GL_R11F_G11F_B10F:
1934 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1935 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1936 ? GL_RGB : 0;
1937
1938 case GL_RGBA8UI_EXT:
1939 case GL_RGBA16UI_EXT:
1940 case GL_RGBA32UI_EXT:
1941 case GL_RGBA8I_EXT:
1942 case GL_RGBA16I_EXT:
1943 case GL_RGBA32I_EXT:
1944 return ctx->Version >= 30
1945 || (_mesa_is_desktop_gl(ctx) &&
1946 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1947
1948 case GL_RGB8UI_EXT:
1949 case GL_RGB16UI_EXT:
1950 case GL_RGB32UI_EXT:
1951 case GL_RGB8I_EXT:
1952 case GL_RGB16I_EXT:
1953 case GL_RGB32I_EXT:
1954 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1955 ? GL_RGB : 0;
1956 case GL_R8UI:
1957 case GL_R8I:
1958 case GL_R16UI:
1959 case GL_R16I:
1960 case GL_R32UI:
1961 case GL_R32I:
1962 return ctx->Version >= 30
1963 || (_mesa_is_desktop_gl(ctx) &&
1964 ctx->Extensions.ARB_texture_rg &&
1965 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1966
1967 case GL_RG8UI:
1968 case GL_RG8I:
1969 case GL_RG16UI:
1970 case GL_RG16I:
1971 case GL_RG32UI:
1972 case GL_RG32I:
1973 return ctx->Version >= 30
1974 || (_mesa_is_desktop_gl(ctx) &&
1975 ctx->Extensions.ARB_texture_rg &&
1976 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1977
1978 case GL_INTENSITY8I_EXT:
1979 case GL_INTENSITY8UI_EXT:
1980 case GL_INTENSITY16I_EXT:
1981 case GL_INTENSITY16UI_EXT:
1982 case GL_INTENSITY32I_EXT:
1983 case GL_INTENSITY32UI_EXT:
1984 return ctx->API == API_OPENGL_COMPAT &&
1985 ctx->Extensions.EXT_texture_integer &&
1986 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1987
1988 case GL_LUMINANCE8I_EXT:
1989 case GL_LUMINANCE8UI_EXT:
1990 case GL_LUMINANCE16I_EXT:
1991 case GL_LUMINANCE16UI_EXT:
1992 case GL_LUMINANCE32I_EXT:
1993 case GL_LUMINANCE32UI_EXT:
1994 return ctx->API == API_OPENGL_COMPAT &&
1995 ctx->Extensions.EXT_texture_integer &&
1996 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1997
1998 case GL_LUMINANCE_ALPHA8I_EXT:
1999 case GL_LUMINANCE_ALPHA8UI_EXT:
2000 case GL_LUMINANCE_ALPHA16I_EXT:
2001 case GL_LUMINANCE_ALPHA16UI_EXT:
2002 case GL_LUMINANCE_ALPHA32I_EXT:
2003 case GL_LUMINANCE_ALPHA32UI_EXT:
2004 return ctx->API == API_OPENGL_COMPAT &&
2005 ctx->Extensions.EXT_texture_integer &&
2006 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
2007
2008 case GL_ALPHA8I_EXT:
2009 case GL_ALPHA8UI_EXT:
2010 case GL_ALPHA16I_EXT:
2011 case GL_ALPHA16UI_EXT:
2012 case GL_ALPHA32I_EXT:
2013 case GL_ALPHA32UI_EXT:
2014 return ctx->API == API_OPENGL_COMPAT &&
2015 ctx->Extensions.EXT_texture_integer &&
2016 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
2017
2018 case GL_RGB10_A2UI:
2019 return (_mesa_is_desktop_gl(ctx) &&
2020 ctx->Extensions.ARB_texture_rgb10_a2ui)
2021 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
2022
2023 case GL_RGB565:
2024 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
2025 ? GL_RGB : 0;
2026 default:
2027 return 0;
2028 }
2029 }
2030
2031
2032 /**
2033 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
2034 */
2035 static void
2036 invalidate_rb(GLuint key, void *data, void *userData)
2037 {
2038 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2039 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
2040
2041 /* If this is a user-created FBO */
2042 if (_mesa_is_user_fbo(fb)) {
2043 GLuint i;
2044 for (i = 0; i < BUFFER_COUNT; i++) {
2045 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2046 if (att->Type == GL_RENDERBUFFER &&
2047 att->Renderbuffer == rb) {
2048 /* Mark fb status as indeterminate to force re-validation */
2049 fb->_Status = 0;
2050 return;
2051 }
2052 }
2053 }
2054 }
2055
2056
2057 /** sentinal value, see below */
2058 #define NO_SAMPLES 1000
2059
2060 void
2061 _mesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2062 GLenum internalFormat, GLsizei width,
2063 GLsizei height, GLsizei samples)
2064 {
2065 const GLenum baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2066
2067 assert(baseFormat != 0);
2068 assert(width >= 0 && width <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2069 assert(height >= 0 && height <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2070 assert(samples != NO_SAMPLES);
2071 if (samples != 0) {
2072 assert(samples > 0);
2073 assert(_mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2074 internalFormat, samples) == GL_NO_ERROR);
2075 }
2076
2077 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2078
2079 if (rb->InternalFormat == internalFormat &&
2080 rb->Width == (GLuint) width &&
2081 rb->Height == (GLuint) height &&
2082 rb->NumSamples == samples) {
2083 /* no change in allocation needed */
2084 return;
2085 }
2086
2087 /* These MUST get set by the AllocStorage func */
2088 rb->Format = MESA_FORMAT_NONE;
2089 rb->NumSamples = samples;
2090
2091 /* Now allocate the storage */
2092 assert(rb->AllocStorage);
2093 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
2094 /* No error - check/set fields now */
2095 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2096 assert(rb->Width == (GLuint) width);
2097 assert(rb->Height == (GLuint) height);
2098 rb->InternalFormat = internalFormat;
2099 rb->_BaseFormat = baseFormat;
2100 assert(rb->_BaseFormat != 0);
2101 }
2102 else {
2103 /* Probably ran out of memory - clear the fields */
2104 rb->Width = 0;
2105 rb->Height = 0;
2106 rb->Format = MESA_FORMAT_NONE;
2107 rb->InternalFormat = GL_NONE;
2108 rb->_BaseFormat = GL_NONE;
2109 rb->NumSamples = 0;
2110 }
2111
2112 /* Invalidate the framebuffers the renderbuffer is attached in. */
2113 if (rb->AttachedAnytime) {
2114 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
2115 }
2116 }
2117
2118 /**
2119 * Helper function used by renderbuffer_storage_direct() and
2120 * renderbuffer_storage_target().
2121 * samples will be NO_SAMPLES if called by a non-multisample function.
2122 */
2123 static void
2124 renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2125 GLenum internalFormat, GLsizei width,
2126 GLsizei height, GLsizei samples, const char *func)
2127 {
2128 GLenum baseFormat;
2129 GLenum sample_count_error;
2130
2131 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2132 if (baseFormat == 0) {
2133 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
2134 func, _mesa_enum_to_string(internalFormat));
2135 return;
2136 }
2137
2138 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2139 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
2140 width);
2141 return;
2142 }
2143
2144 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2145 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
2146 height);
2147 return;
2148 }
2149
2150 if (samples == NO_SAMPLES) {
2151 /* NumSamples == 0 indicates non-multisampling */
2152 samples = 0;
2153 }
2154 else {
2155 /* check the sample count;
2156 * note: driver may choose to use more samples than what's requested
2157 */
2158 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2159 internalFormat, samples);
2160
2161 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2162 *
2163 * "If a negative number is provided where an argument of type sizei or
2164 * sizeiptr is specified, the error INVALID VALUE is generated."
2165 */
2166 if (samples < 0) {
2167 sample_count_error = GL_INVALID_VALUE;
2168 }
2169
2170 if (sample_count_error != GL_NO_ERROR) {
2171 _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
2172 return;
2173 }
2174 }
2175
2176 _mesa_renderbuffer_storage(ctx, rb, internalFormat, width, height, samples);
2177 }
2178
2179 /**
2180 * Helper function used by _mesa_NamedRenderbufferStorage*().
2181 * samples will be NO_SAMPLES if called by a non-multisample function.
2182 */
2183 static void
2184 renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
2185 GLsizei width, GLsizei height, GLsizei samples,
2186 const char *func)
2187 {
2188 GET_CURRENT_CONTEXT(ctx);
2189
2190 if (MESA_VERBOSE & VERBOSE_API) {
2191 if (samples == NO_SAMPLES)
2192 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
2193 func, renderbuffer,
2194 _mesa_enum_to_string(internalFormat),
2195 width, height);
2196 else
2197 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
2198 func, renderbuffer,
2199 _mesa_enum_to_string(internalFormat),
2200 width, height, samples);
2201 }
2202
2203 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2204 if (!rb || rb == &DummyRenderbuffer) {
2205 /* ID was reserved, but no real renderbuffer object made yet */
2206 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
2207 func, renderbuffer);
2208 return;
2209 }
2210
2211 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
2212 }
2213
2214 /**
2215 * Helper function used by _mesa_RenderbufferStorage() and
2216 * _mesa_RenderbufferStorageMultisample().
2217 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2218 */
2219 static void
2220 renderbuffer_storage_target(GLenum target, GLenum internalFormat,
2221 GLsizei width, GLsizei height, GLsizei samples,
2222 const char *func)
2223 {
2224 GET_CURRENT_CONTEXT(ctx);
2225
2226 if (MESA_VERBOSE & VERBOSE_API) {
2227 if (samples == NO_SAMPLES)
2228 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
2229 func,
2230 _mesa_enum_to_string(target),
2231 _mesa_enum_to_string(internalFormat),
2232 width, height);
2233 else
2234 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
2235 func,
2236 _mesa_enum_to_string(target),
2237 _mesa_enum_to_string(internalFormat),
2238 width, height, samples);
2239 }
2240
2241 if (target != GL_RENDERBUFFER_EXT) {
2242 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
2243 return;
2244 }
2245
2246 if (!ctx->CurrentRenderbuffer) {
2247 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
2248 func);
2249 return;
2250 }
2251
2252 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
2253 height, samples, func);
2254 }
2255
2256
2257 void GLAPIENTRY
2258 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
2259 {
2260 struct gl_renderbuffer *rb;
2261 GET_CURRENT_CONTEXT(ctx);
2262
2263 if (!ctx->Extensions.OES_EGL_image) {
2264 _mesa_error(ctx, GL_INVALID_OPERATION,
2265 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2266 return;
2267 }
2268
2269 if (target != GL_RENDERBUFFER) {
2270 _mesa_error(ctx, GL_INVALID_ENUM,
2271 "EGLImageTargetRenderbufferStorageOES");
2272 return;
2273 }
2274
2275 rb = ctx->CurrentRenderbuffer;
2276 if (!rb) {
2277 _mesa_error(ctx, GL_INVALID_OPERATION,
2278 "EGLImageTargetRenderbufferStorageOES");
2279 return;
2280 }
2281
2282 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2283
2284 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2285 }
2286
2287
2288 /**
2289 * Helper function for _mesa_GetRenderbufferParameteriv() and
2290 * _mesa_GetFramebufferAttachmentParameteriv()
2291 * We have to be careful to respect the base format. For example, if a
2292 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2293 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2294 * we need to return zero.
2295 */
2296 static GLint
2297 get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
2298 {
2299 if (_mesa_base_format_has_channel(baseFormat, pname))
2300 return _mesa_get_format_bits(format, pname);
2301 else
2302 return 0;
2303 }
2304
2305
2306
2307 void GLAPIENTRY
2308 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
2309 GLsizei width, GLsizei height)
2310 {
2311 /* GL_ARB_fbo says calling this function is equivalent to calling
2312 * glRenderbufferStorageMultisample() with samples=0. We pass in
2313 * a token value here just for error reporting purposes.
2314 */
2315 renderbuffer_storage_target(target, internalFormat, width, height,
2316 NO_SAMPLES, "glRenderbufferStorage");
2317 }
2318
2319
2320 void GLAPIENTRY
2321 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
2322 GLenum internalFormat,
2323 GLsizei width, GLsizei height)
2324 {
2325 renderbuffer_storage_target(target, internalFormat, width, height,
2326 samples, "glRenderbufferStorageMultisample");
2327 }
2328
2329
2330 /**
2331 * OpenGL ES version of glRenderBufferStorage.
2332 */
2333 void GLAPIENTRY
2334 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
2335 GLsizei width, GLsizei height)
2336 {
2337 switch (internalFormat) {
2338 case GL_RGB565:
2339 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2340 /* choose a closest format */
2341 internalFormat = GL_RGB5;
2342 break;
2343 default:
2344 break;
2345 }
2346
2347 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2348 "glRenderbufferStorageEXT");
2349 }
2350
2351 void GLAPIENTRY
2352 _mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2353 GLsizei width, GLsizei height)
2354 {
2355 /* GL_ARB_fbo says calling this function is equivalent to calling
2356 * glRenderbufferStorageMultisample() with samples=0. We pass in
2357 * a token value here just for error reporting purposes.
2358 */
2359 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2360 NO_SAMPLES, "glNamedRenderbufferStorage");
2361 }
2362
2363 void GLAPIENTRY
2364 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2365 GLenum internalformat,
2366 GLsizei width, GLsizei height)
2367 {
2368 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2369 samples,
2370 "glNamedRenderbufferStorageMultisample");
2371 }
2372
2373
2374 static void
2375 get_render_buffer_parameteriv(struct gl_context *ctx,
2376 struct gl_renderbuffer *rb, GLenum pname,
2377 GLint *params, const char *func)
2378 {
2379 /* No need to flush here since we're just quering state which is
2380 * not effected by rendering.
2381 */
2382
2383 switch (pname) {
2384 case GL_RENDERBUFFER_WIDTH_EXT:
2385 *params = rb->Width;
2386 return;
2387 case GL_RENDERBUFFER_HEIGHT_EXT:
2388 *params = rb->Height;
2389 return;
2390 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
2391 *params = rb->InternalFormat;
2392 return;
2393 case GL_RENDERBUFFER_RED_SIZE_EXT:
2394 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
2395 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
2396 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
2397 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
2398 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
2399 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
2400 break;
2401 case GL_RENDERBUFFER_SAMPLES:
2402 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2403 || _mesa_is_gles3(ctx)) {
2404 *params = rb->NumSamples;
2405 break;
2406 }
2407 /* fallthrough */
2408 default:
2409 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
2410 _mesa_enum_to_string(pname));
2411 return;
2412 }
2413 }
2414
2415
2416 void GLAPIENTRY
2417 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2418 {
2419 GET_CURRENT_CONTEXT(ctx);
2420
2421 if (target != GL_RENDERBUFFER_EXT) {
2422 _mesa_error(ctx, GL_INVALID_ENUM,
2423 "glGetRenderbufferParameterivEXT(target)");
2424 return;
2425 }
2426
2427 if (!ctx->CurrentRenderbuffer) {
2428 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2429 "(no renderbuffer bound)");
2430 return;
2431 }
2432
2433 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2434 params, "glGetRenderbufferParameteriv");
2435 }
2436
2437
2438 void GLAPIENTRY
2439 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2440 GLint *params)
2441 {
2442 GET_CURRENT_CONTEXT(ctx);
2443
2444 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2445 if (!rb || rb == &DummyRenderbuffer) {
2446 /* ID was reserved, but no real renderbuffer object made yet */
2447 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2448 "(invalid renderbuffer %i)", renderbuffer);
2449 return;
2450 }
2451
2452 get_render_buffer_parameteriv(ctx, rb, pname, params,
2453 "glGetNamedRenderbufferParameteriv");
2454 }
2455
2456
2457 GLboolean GLAPIENTRY
2458 _mesa_IsFramebuffer(GLuint framebuffer)
2459 {
2460 GET_CURRENT_CONTEXT(ctx);
2461 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2462 if (framebuffer) {
2463 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
2464 if (rb != NULL && rb != &DummyFramebuffer)
2465 return GL_TRUE;
2466 }
2467 return GL_FALSE;
2468 }
2469
2470
2471 /**
2472 * Check if any of the attachments of the given framebuffer are textures
2473 * (render to texture). Call ctx->Driver.RenderTexture() for such
2474 * attachments.
2475 */
2476 static void
2477 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2478 {
2479 GLuint i;
2480 assert(ctx->Driver.RenderTexture);
2481
2482 if (_mesa_is_winsys_fbo(fb))
2483 return; /* can't render to texture with winsys framebuffers */
2484
2485 for (i = 0; i < BUFFER_COUNT; i++) {
2486 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2487 if (att->Texture && att->Renderbuffer->TexImage
2488 && driver_RenderTexture_is_safe(att)) {
2489 ctx->Driver.RenderTexture(ctx, fb, att);
2490 }
2491 }
2492 }
2493
2494
2495 /**
2496 * Examine all the framebuffer's attachments to see if any are textures.
2497 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2498 * notify the device driver that the texture image may have changed.
2499 */
2500 static void
2501 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2502 {
2503 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2504 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2505 return;
2506
2507 if (ctx->Driver.FinishRenderTexture) {
2508 GLuint i;
2509 for (i = 0; i < BUFFER_COUNT; i++) {
2510 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2511 struct gl_renderbuffer *rb = att->Renderbuffer;
2512 if (rb && rb->NeedsFinishRenderTexture) {
2513 ctx->Driver.FinishRenderTexture(ctx, rb);
2514 }
2515 }
2516 }
2517 }
2518
2519
2520 static void
2521 bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
2522 {
2523 struct gl_framebuffer *newDrawFb, *newReadFb;
2524 GLboolean bindReadBuf, bindDrawBuf;
2525 GET_CURRENT_CONTEXT(ctx);
2526
2527 switch (target) {
2528 case GL_DRAW_FRAMEBUFFER_EXT:
2529 bindDrawBuf = GL_TRUE;
2530 bindReadBuf = GL_FALSE;
2531 break;
2532 case GL_READ_FRAMEBUFFER_EXT:
2533 bindDrawBuf = GL_FALSE;
2534 bindReadBuf = GL_TRUE;
2535 break;
2536 case GL_FRAMEBUFFER_EXT:
2537 bindDrawBuf = GL_TRUE;
2538 bindReadBuf = GL_TRUE;
2539 break;
2540 default:
2541 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
2542 return;
2543 }
2544
2545 if (framebuffer) {
2546 /* Binding a user-created framebuffer object */
2547 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2548 if (newDrawFb == &DummyFramebuffer) {
2549 /* ID was reserved, but no real framebuffer object made yet */
2550 newDrawFb = NULL;
2551 }
2552 else if (!newDrawFb && !allow_user_names) {
2553 /* All FBO IDs must be Gen'd */
2554 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2555 return;
2556 }
2557
2558 if (!newDrawFb) {
2559 /* create new framebuffer object */
2560 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2561 if (!newDrawFb) {
2562 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2563 return;
2564 }
2565 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
2566 }
2567 newReadFb = newDrawFb;
2568 }
2569 else {
2570 /* Binding the window system framebuffer (which was originally set
2571 * with MakeCurrent).
2572 */
2573 newDrawFb = ctx->WinSysDrawBuffer;
2574 newReadFb = ctx->WinSysReadBuffer;
2575 }
2576
2577 _mesa_bind_framebuffers(ctx,
2578 bindDrawBuf ? newDrawFb : ctx->DrawBuffer,
2579 bindReadBuf ? newReadFb : ctx->ReadBuffer);
2580 }
2581
2582 void
2583 _mesa_bind_framebuffers(struct gl_context *ctx,
2584 struct gl_framebuffer *newDrawFb,
2585 struct gl_framebuffer *newReadFb)
2586 {
2587 struct gl_framebuffer *const oldDrawFb = ctx->DrawBuffer;
2588 struct gl_framebuffer *const oldReadFb = ctx->ReadBuffer;
2589 const bool bindDrawBuf = oldDrawFb != newDrawFb;
2590 const bool bindReadBuf = oldReadFb != newReadFb;
2591
2592 assert(newDrawFb);
2593 assert(newDrawFb != &DummyFramebuffer);
2594
2595 /*
2596 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2597 *
2598 * We also check if we're beginning and/or ending render-to-texture.
2599 * When a framebuffer with texture attachments is unbound, call
2600 * ctx->Driver.FinishRenderTexture().
2601 * When a framebuffer with texture attachments is bound, call
2602 * ctx->Driver.RenderTexture().
2603 *
2604 * Note that if the ReadBuffer has texture attachments we don't consider
2605 * that a render-to-texture case.
2606 */
2607 if (bindReadBuf) {
2608 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2609
2610 /* check if old readbuffer was render-to-texture */
2611 check_end_texture_render(ctx, oldReadFb);
2612
2613 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2614 }
2615
2616 if (bindDrawBuf) {
2617 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2618
2619 /* check if old framebuffer had any texture attachments */
2620 if (oldDrawFb)
2621 check_end_texture_render(ctx, oldDrawFb);
2622
2623 /* check if newly bound framebuffer has any texture attachments */
2624 check_begin_texture_render(ctx, newDrawFb);
2625
2626 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2627 }
2628
2629 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2630 /* The few classic drivers that actually hook this function really only
2631 * want to know if the draw framebuffer changed.
2632 */
2633 ctx->Driver.BindFramebuffer(ctx,
2634 bindDrawBuf ? GL_FRAMEBUFFER : GL_READ_FRAMEBUFFER,
2635 newDrawFb, newReadFb);
2636 }
2637 }
2638
2639 void GLAPIENTRY
2640 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
2641 {
2642 GET_CURRENT_CONTEXT(ctx);
2643
2644 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2645 * point, but they allow the use of user-generated names.
2646 */
2647 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
2648 }
2649
2650
2651 void GLAPIENTRY
2652 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2653 {
2654 /* This function should not be in the dispatch table for core profile /
2655 * OpenGL 3.1, so execution should never get here in those cases -- no
2656 * need for an explicit test.
2657 */
2658 bind_framebuffer(target, framebuffer, true);
2659 }
2660
2661
2662 void GLAPIENTRY
2663 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2664 {
2665 GLint i;
2666 GET_CURRENT_CONTEXT(ctx);
2667
2668 if (n < 0) {
2669 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2670 return;
2671 }
2672
2673 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2674
2675 for (i = 0; i < n; i++) {
2676 if (framebuffers[i] > 0) {
2677 struct gl_framebuffer *fb;
2678 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2679 if (fb) {
2680 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2681
2682 /* check if deleting currently bound framebuffer object */
2683 if (fb == ctx->DrawBuffer) {
2684 /* bind default */
2685 assert(fb->RefCount >= 2);
2686 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2687 }
2688 if (fb == ctx->ReadBuffer) {
2689 /* bind default */
2690 assert(fb->RefCount >= 2);
2691 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2692 }
2693
2694 /* remove from hash table immediately, to free the ID */
2695 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2696
2697 if (fb != &DummyFramebuffer) {
2698 /* But the object will not be freed until it's no longer
2699 * bound in any context.
2700 */
2701 _mesa_reference_framebuffer(&fb, NULL);
2702 }
2703 }
2704 }
2705 }
2706 }
2707
2708
2709 /**
2710 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2711 * It is not exposed to the rest of Mesa to encourage the use of
2712 * nameless buffers in driver internals.
2713 */
2714 static void
2715 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
2716 {
2717 GET_CURRENT_CONTEXT(ctx);
2718 GLuint first;
2719 GLint i;
2720 struct gl_framebuffer *fb;
2721
2722 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
2723
2724 if (n < 0) {
2725 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
2726 return;
2727 }
2728
2729 if (!framebuffers)
2730 return;
2731
2732 _mesa_HashLockMutex(ctx->Shared->FrameBuffers);
2733
2734 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2735
2736 for (i = 0; i < n; i++) {
2737 GLuint name = first + i;
2738 framebuffers[i] = name;
2739
2740 if (dsa) {
2741 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2742 if (!fb) {
2743 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2744 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2745 return;
2746 }
2747 }
2748 else
2749 fb = &DummyFramebuffer;
2750
2751 _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, name, fb);
2752 }
2753
2754 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2755 }
2756
2757
2758 void GLAPIENTRY
2759 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2760 {
2761 create_framebuffers(n, framebuffers, false);
2762 }
2763
2764
2765 void GLAPIENTRY
2766 _mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2767 {
2768 create_framebuffers(n, framebuffers, true);
2769 }
2770
2771
2772 GLenum
2773 _mesa_check_framebuffer_status(struct gl_context *ctx,
2774 struct gl_framebuffer *buffer)
2775 {
2776 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2777
2778 if (_mesa_is_winsys_fbo(buffer)) {
2779 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2780 if (buffer != &IncompleteFramebuffer) {
2781 return GL_FRAMEBUFFER_COMPLETE_EXT;
2782 } else {
2783 return GL_FRAMEBUFFER_UNDEFINED;
2784 }
2785 }
2786
2787 /* No need to flush here */
2788
2789 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2790 _mesa_test_framebuffer_completeness(ctx, buffer);
2791 }
2792
2793 return buffer->_Status;
2794 }
2795
2796
2797 GLenum GLAPIENTRY
2798 _mesa_CheckFramebufferStatus(GLenum target)
2799 {
2800 struct gl_framebuffer *fb;
2801 GET_CURRENT_CONTEXT(ctx);
2802
2803 if (MESA_VERBOSE & VERBOSE_API)
2804 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2805 _mesa_enum_to_string(target));
2806
2807 fb = get_framebuffer_target(ctx, target);
2808 if (!fb) {
2809 _mesa_error(ctx, GL_INVALID_ENUM,
2810 "glCheckFramebufferStatus(invalid target %s)",
2811 _mesa_enum_to_string(target));
2812 return 0;
2813 }
2814
2815 return _mesa_check_framebuffer_status(ctx, fb);
2816 }
2817
2818
2819 GLenum GLAPIENTRY
2820 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer, GLenum target)
2821 {
2822 struct gl_framebuffer *fb;
2823 GET_CURRENT_CONTEXT(ctx);
2824
2825 /* Validate the target (for conformance's sake) and grab a reference to the
2826 * default framebuffer in case framebuffer = 0.
2827 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2828 * (30.10.2014, PDF page 336) says:
2829 * "If framebuffer is zero, then the status of the default read or
2830 * draw framebuffer (as determined by target) is returned."
2831 */
2832 switch (target) {
2833 case GL_DRAW_FRAMEBUFFER:
2834 case GL_FRAMEBUFFER:
2835 fb = ctx->WinSysDrawBuffer;
2836 break;
2837 case GL_READ_FRAMEBUFFER:
2838 fb = ctx->WinSysReadBuffer;
2839 break;
2840 default:
2841 _mesa_error(ctx, GL_INVALID_ENUM,
2842 "glCheckNamedFramebufferStatus(invalid target %s)",
2843 _mesa_enum_to_string(target));
2844 return 0;
2845 }
2846
2847 if (framebuffer) {
2848 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
2849 "glCheckNamedFramebufferStatus");
2850 if (!fb)
2851 return 0;
2852 }
2853
2854 return _mesa_check_framebuffer_status(ctx, fb);
2855 }
2856
2857
2858 /**
2859 * Replicate the src attachment point. Used by framebuffer_texture() when
2860 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2861 * GL_STENCIL_ATTACHMENT.
2862 */
2863 static void
2864 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2865 gl_buffer_index dst,
2866 gl_buffer_index src)
2867 {
2868 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2869 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2870
2871 assert(src_att->Texture != NULL);
2872 assert(src_att->Renderbuffer != NULL);
2873
2874 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2875 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2876 dst_att->Type = src_att->Type;
2877 dst_att->Complete = src_att->Complete;
2878 dst_att->TextureLevel = src_att->TextureLevel;
2879 dst_att->CubeMapFace = src_att->CubeMapFace;
2880 dst_att->Zoffset = src_att->Zoffset;
2881 dst_att->Layered = src_att->Layered;
2882 }
2883
2884
2885 /**
2886 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2887 * texture object pointer.
2888 *
2889 * \param texObj where the pointer to the texture object is returned. Note
2890 * that a successful call may return texObj = NULL.
2891 *
2892 * \return true if no errors, false if errors
2893 */
2894 static bool
2895 get_texture_for_framebuffer(struct gl_context *ctx, GLuint texture,
2896 bool layered, const char *caller,
2897 struct gl_texture_object **texObj)
2898 {
2899 *texObj = NULL; /* This will get returned if texture = 0. */
2900
2901 if (!texture)
2902 return true;
2903
2904 *texObj = _mesa_lookup_texture(ctx, texture);
2905 if (*texObj == NULL || (*texObj)->Target == 0) {
2906 /* Can't render to a non-existent texture object.
2907 *
2908 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2909 * Managing Framebuffer Objects specifies a different error
2910 * depending upon the calling function (PDF pages 325-328).
2911 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
2912 * value, while the other commands throw invalid operation (where
2913 * layered = GL_FALSE).
2914 */
2915 const GLenum error = layered ? GL_INVALID_VALUE :
2916 GL_INVALID_OPERATION;
2917 _mesa_error(ctx, error,
2918 "%s(non-existent texture %u)", caller, texture);
2919 return false;
2920 }
2921
2922 return true;
2923 }
2924
2925
2926 /**
2927 * Common code called by gl*FramebufferTexture() to verify the texture target
2928 * and decide whether or not the attachment should truly be considered
2929 * layered.
2930 *
2931 * \param layered true if attachment should be considered layered, false if
2932 * not
2933 *
2934 * \return true if no errors, false if errors
2935 */
2936 static bool
2937 check_layered_texture_target(struct gl_context *ctx, GLenum target,
2938 const char *caller, GLboolean *layered)
2939 {
2940 *layered = GL_TRUE;
2941
2942 switch (target) {
2943 case GL_TEXTURE_3D:
2944 case GL_TEXTURE_1D_ARRAY_EXT:
2945 case GL_TEXTURE_2D_ARRAY_EXT:
2946 case GL_TEXTURE_CUBE_MAP:
2947 case GL_TEXTURE_CUBE_MAP_ARRAY:
2948 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2949 return true;
2950 case GL_TEXTURE_1D:
2951 case GL_TEXTURE_2D:
2952 case GL_TEXTURE_RECTANGLE:
2953 case GL_TEXTURE_2D_MULTISAMPLE:
2954 /* These texture types are valid to pass to
2955 * glFramebufferTexture(), but since they aren't layered, it
2956 * is equivalent to calling glFramebufferTexture{1D,2D}().
2957 */
2958 *layered = GL_FALSE;
2959 return true;
2960 }
2961
2962 _mesa_error(ctx, GL_INVALID_OPERATION,
2963 "%s(invalid texture target %s)", caller,
2964 _mesa_enum_to_string(target));
2965 return false;
2966 }
2967
2968
2969 /**
2970 * Common code called by gl*FramebufferTextureLayer() to verify the texture
2971 * target.
2972 *
2973 * \return true if no errors, false if errors
2974 */
2975 static bool
2976 check_texture_target(struct gl_context *ctx, GLenum target,
2977 const char *caller)
2978 {
2979 /* We're being called by glFramebufferTextureLayer().
2980 * The only legal texture types for that function are 3D,
2981 * cube-map, and 1D/2D/cube-map array textures.
2982 *
2983 * We don't need to check for GL_ARB_texture_cube_map_array because the
2984 * application wouldn't have been able to create a texture with a
2985 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
2986 */
2987 switch (target) {
2988 case GL_TEXTURE_3D:
2989 case GL_TEXTURE_1D_ARRAY:
2990 case GL_TEXTURE_2D_ARRAY:
2991 case GL_TEXTURE_CUBE_MAP_ARRAY:
2992 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2993 return true;
2994 case GL_TEXTURE_CUBE_MAP:
2995 /* We don't need to check the extension (GL_ARB_direct_state_access) or
2996 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
2997 * enabled in core profile. This can be called from
2998 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
2999 * so we do have to check the profile.
3000 */
3001 return ctx->API == API_OPENGL_CORE;
3002 }
3003
3004 _mesa_error(ctx, GL_INVALID_OPERATION,
3005 "%s(invalid texture target %s)", caller,
3006 _mesa_enum_to_string(target));
3007 return false;
3008 }
3009
3010
3011 /**
3012 * Common code called by glFramebufferTexture*D() to verify the texture
3013 * target.
3014 *
3015 * \return true if no errors, false if errors
3016 */
3017 static bool
3018 check_textarget(struct gl_context *ctx, int dims, GLenum target,
3019 GLenum textarget, const char *caller)
3020 {
3021 bool err = false;
3022
3023 switch (textarget) {
3024 case GL_TEXTURE_1D:
3025 err = dims != 1;
3026 break;
3027 case GL_TEXTURE_1D_ARRAY:
3028 err = dims != 1 || !ctx->Extensions.EXT_texture_array;
3029 break;
3030 case GL_TEXTURE_2D:
3031 err = dims != 2;
3032 break;
3033 case GL_TEXTURE_2D_ARRAY:
3034 err = dims != 2 || !ctx->Extensions.EXT_texture_array ||
3035 (_mesa_is_gles(ctx) && ctx->Version < 30);
3036 break;
3037 case GL_TEXTURE_2D_MULTISAMPLE:
3038 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3039 err = dims != 2 ||
3040 !ctx->Extensions.ARB_texture_multisample ||
3041 (_mesa_is_gles(ctx) && ctx->Version < 31);
3042 break;
3043 case GL_TEXTURE_RECTANGLE:
3044 err = dims != 2 || _mesa_is_gles(ctx) ||
3045 !ctx->Extensions.NV_texture_rectangle;
3046 break;
3047 case GL_TEXTURE_CUBE_MAP:
3048 case GL_TEXTURE_CUBE_MAP_ARRAY:
3049 err = true;
3050 break;
3051 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3052 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3053 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3054 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3055 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3056 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3057 err = dims != 2 || !ctx->Extensions.ARB_texture_cube_map;
3058 break;
3059 case GL_TEXTURE_3D:
3060 err = dims != 3;
3061 break;
3062 default:
3063 _mesa_error(ctx, GL_INVALID_ENUM,
3064 "%s(unknown textarget 0x%x)", caller, textarget);
3065 return false;
3066 }
3067
3068 if (err) {
3069 _mesa_error(ctx, GL_INVALID_OPERATION,
3070 "%s(invalid textarget %s)",
3071 caller, _mesa_enum_to_string(textarget));
3072 return false;
3073 }
3074
3075 /* Make sure textarget is consistent with the texture's type */
3076 err = (target == GL_TEXTURE_CUBE_MAP) ?
3077 !_mesa_is_cube_face(textarget): (target != textarget);
3078
3079 if (err) {
3080 _mesa_error(ctx, GL_INVALID_OPERATION,
3081 "%s(mismatched texture target)", caller);
3082 return false;
3083 }
3084
3085 return true;
3086 }
3087
3088
3089 /**
3090 * Common code called by gl*FramebufferTextureLayer() and
3091 * glFramebufferTexture3D() to validate the layer.
3092 *
3093 * \return true if no errors, false if errors
3094 */
3095 static bool
3096 check_layer(struct gl_context *ctx, GLenum target, GLint layer,
3097 const char *caller)
3098 {
3099 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3100 * spec says:
3101 *
3102 * "An INVALID_VALUE error is generated if texture is non-zero
3103 * and layer is negative."
3104 */
3105 if (layer < 0) {
3106 _mesa_error(ctx, GL_INVALID_VALUE,
3107 "%s(layer %u < 0)", caller, layer);
3108 return false;
3109 }
3110
3111 if (target == GL_TEXTURE_3D) {
3112 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
3113 if (layer >= maxSize) {
3114 _mesa_error(ctx, GL_INVALID_VALUE,
3115 "%s(invalid layer %u)", caller, layer);
3116 return false;
3117 }
3118 }
3119 else if ((target == GL_TEXTURE_1D_ARRAY) ||
3120 (target == GL_TEXTURE_2D_ARRAY) ||
3121 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
3122 (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
3123 if (layer >= ctx->Const.MaxArrayTextureLayers) {
3124 _mesa_error(ctx, GL_INVALID_VALUE,
3125 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3126 caller, layer);
3127 return false;
3128 }
3129 }
3130 else if (target == GL_TEXTURE_CUBE_MAP) {
3131 if (layer >= 6) {
3132 _mesa_error(ctx, GL_INVALID_VALUE,
3133 "%s(layer %u >= 6)", caller, layer);
3134 return false;
3135 }
3136 }
3137
3138 return true;
3139 }
3140
3141
3142 /**
3143 * Common code called by all gl*FramebufferTexture*() entry points to verify
3144 * the level.
3145 *
3146 * \return true if no errors, false if errors
3147 */
3148 static bool
3149 check_level(struct gl_context *ctx, GLenum target, GLint level,
3150 const char *caller)
3151 {
3152 if ((level < 0) ||
3153 (level >= _mesa_max_texture_levels(ctx, target))) {
3154 _mesa_error(ctx, GL_INVALID_VALUE,
3155 "%s(invalid level %d)", caller, level);
3156 return false;
3157 }
3158
3159 return true;
3160 }
3161
3162
3163 void
3164 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3165 GLenum attachment,
3166 struct gl_texture_object *texObj, GLenum textarget,
3167 GLint level, GLuint layer, GLboolean layered,
3168 const char *caller)
3169 {
3170 struct gl_renderbuffer_attachment *att;
3171 bool is_color_attachment;
3172
3173 /* The window-system framebuffer object is immutable */
3174 if (_mesa_is_winsys_fbo(fb)) {
3175 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3176 caller);
3177 return;
3178 }
3179
3180 /* Not a hash lookup, so we can afford to get the attachment here. */
3181 att = get_attachment(ctx, fb, attachment, &is_color_attachment);
3182 if (att == NULL) {
3183 if (is_color_attachment) {
3184 _mesa_error(ctx, GL_INVALID_OPERATION,
3185 "%s(invalid color attachment %s)", caller,
3186 _mesa_enum_to_string(attachment));
3187 } else {
3188 _mesa_error(ctx, GL_INVALID_ENUM,
3189 "%s(invalid attachment %s)", caller,
3190 _mesa_enum_to_string(attachment));
3191 }
3192 return;
3193 }
3194
3195 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3196
3197 mtx_lock(&fb->Mutex);
3198 if (texObj) {
3199 if (attachment == GL_DEPTH_ATTACHMENT &&
3200 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3201 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3202 _mesa_tex_target_to_face(textarget) ==
3203 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
3204 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
3205 /* The texture object is already attached to the stencil attachment
3206 * point. Don't create a new renderbuffer; just reuse the stencil
3207 * attachment's. This is required to prevent a GL error in
3208 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3209 */
3210 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3211 BUFFER_STENCIL);
3212 } else if (attachment == GL_STENCIL_ATTACHMENT &&
3213 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
3214 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3215 _mesa_tex_target_to_face(textarget) ==
3216 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
3217 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
3218 /* As above, but with depth and stencil transposed. */
3219 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3220 BUFFER_DEPTH);
3221 } else {
3222 set_texture_attachment(ctx, fb, att, texObj, textarget,
3223 level, layer, layered);
3224
3225 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3226 /* Above we created a new renderbuffer and attached it to the
3227 * depth attachment point. Now attach it to the stencil attachment
3228 * point too.
3229 */
3230 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3231 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3232 BUFFER_DEPTH);
3233 }
3234 }
3235
3236 /* Set the render-to-texture flag. We'll check this flag in
3237 * glTexImage() and friends to determine if we need to revalidate
3238 * any FBOs that might be rendering into this texture.
3239 * This flag never gets cleared since it's non-trivial to determine
3240 * when all FBOs might be done rendering to this texture. That's OK
3241 * though since it's uncommon to render to a texture then repeatedly
3242 * call glTexImage() to change images in the texture.
3243 */
3244 texObj->_RenderToTexture = GL_TRUE;
3245 }
3246 else {
3247 remove_attachment(ctx, att);
3248 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3249 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3250 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
3251 }
3252 }
3253
3254 invalidate_framebuffer(fb);
3255
3256 mtx_unlock(&fb->Mutex);
3257 }
3258
3259
3260 static void
3261 framebuffer_texture_with_dims(int dims, GLenum target,
3262 GLenum attachment, GLenum textarget,
3263 GLuint texture, GLint level, GLint layer,
3264 const char *caller)
3265 {
3266 GET_CURRENT_CONTEXT(ctx);
3267 struct gl_framebuffer *fb;
3268 struct gl_texture_object *texObj;
3269
3270 /* Get the framebuffer object */
3271 fb = get_framebuffer_target(ctx, target);
3272 if (!fb) {
3273 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
3274 _mesa_enum_to_string(target));
3275 return;
3276 }
3277
3278 /* Get the texture object */
3279 if (!get_texture_for_framebuffer(ctx, texture, false, caller, &texObj))
3280 return;
3281
3282 if (texObj) {
3283 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3284 return;
3285
3286 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3287 return;
3288
3289 if (!check_level(ctx, textarget, level, caller))
3290 return;
3291 }
3292
3293 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3294 layer, GL_FALSE, caller);
3295 }
3296
3297
3298 void GLAPIENTRY
3299 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3300 GLenum textarget, GLuint texture, GLint level)
3301 {
3302 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3303 level, 0, "glFramebufferTexture1D");
3304 }
3305
3306
3307 void GLAPIENTRY
3308 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
3309 GLenum textarget, GLuint texture, GLint level)
3310 {
3311 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3312 level, 0, "glFramebufferTexture2D");
3313 }
3314
3315
3316 void GLAPIENTRY
3317 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
3318 GLenum textarget, GLuint texture,
3319 GLint level, GLint layer)
3320 {
3321 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3322 level, layer, "glFramebufferTexture3D");
3323 }
3324
3325
3326 void GLAPIENTRY
3327 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3328 GLuint texture, GLint level, GLint layer)
3329 {
3330 GET_CURRENT_CONTEXT(ctx);
3331 struct gl_framebuffer *fb;
3332 struct gl_texture_object *texObj;
3333 GLenum textarget = 0;
3334
3335 const char *func = "glFramebufferTextureLayer";
3336
3337 /* Get the framebuffer object */
3338 fb = get_framebuffer_target(ctx, target);
3339 if (!fb) {
3340 _mesa_error(ctx, GL_INVALID_ENUM,
3341 "glFramebufferTextureLayer(invalid target %s)",
3342 _mesa_enum_to_string(target));
3343 return;
3344 }
3345
3346 /* Get the texture object */
3347 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3348 return;
3349
3350 if (texObj) {
3351 if (!check_texture_target(ctx, texObj->Target, func))
3352 return;
3353
3354 if (!check_layer(ctx, texObj->Target, layer, func))
3355 return;
3356
3357 if (!check_level(ctx, texObj->Target, level, func))
3358 return;
3359
3360 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3361 assert(layer >= 0 && layer < 6);
3362 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3363 layer = 0;
3364 }
3365 }
3366
3367 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3368 layer, GL_FALSE, func);
3369 }
3370
3371
3372 void GLAPIENTRY
3373 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3374 GLuint texture, GLint level, GLint layer)
3375 {
3376 GET_CURRENT_CONTEXT(ctx);
3377 struct gl_framebuffer *fb;
3378 struct gl_texture_object *texObj;
3379 GLenum textarget = 0;
3380
3381 const char *func = "glNamedFramebufferTextureLayer";
3382
3383 /* Get the framebuffer object */
3384 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3385 if (!fb)
3386 return;
3387
3388 /* Get the texture object */
3389 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3390 return;
3391
3392 if (texObj) {
3393 if (!check_texture_target(ctx, texObj->Target, func))
3394 return;
3395
3396 if (!check_layer(ctx, texObj->Target, layer, func))
3397 return;
3398
3399 if (!check_level(ctx, texObj->Target, level, func))
3400 return;
3401
3402 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3403 assert(layer >= 0 && layer < 6);
3404 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3405 layer = 0;
3406 }
3407 }
3408
3409 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3410 layer, GL_FALSE, func);
3411 }
3412
3413
3414 void GLAPIENTRY
3415 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3416 GLuint texture, GLint level)
3417 {
3418 GET_CURRENT_CONTEXT(ctx);
3419 struct gl_framebuffer *fb;
3420 struct gl_texture_object *texObj;
3421 GLboolean layered = GL_FALSE;
3422
3423 const char *func = "FramebufferTexture";
3424
3425 if (!_mesa_has_geometry_shaders(ctx)) {
3426 _mesa_error(ctx, GL_INVALID_OPERATION,
3427 "unsupported function (glFramebufferTexture) called");
3428 return;
3429 }
3430
3431 /* Get the framebuffer object */
3432 fb = get_framebuffer_target(ctx, target);
3433 if (!fb) {
3434 _mesa_error(ctx, GL_INVALID_ENUM,
3435 "glFramebufferTexture(invalid target %s)",
3436 _mesa_enum_to_string(target));
3437 return;
3438 }
3439
3440 /* Get the texture object */
3441 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3442 return;
3443
3444 if (texObj) {
3445 if (!check_layered_texture_target(ctx, texObj->Target, func, &layered))
3446 return;
3447
3448 if (!check_level(ctx, texObj->Target, level, func))
3449 return;
3450 }
3451
3452 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3453 0, layered, func);
3454 }
3455
3456
3457 void GLAPIENTRY
3458 _mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3459 GLuint texture, GLint level)
3460 {
3461 GET_CURRENT_CONTEXT(ctx);
3462 struct gl_framebuffer *fb;
3463 struct gl_texture_object *texObj;
3464 GLboolean layered = GL_FALSE;
3465
3466 const char *func = "glNamedFramebufferTexture";
3467
3468 if (!_mesa_has_geometry_shaders(ctx)) {
3469 _mesa_error(ctx, GL_INVALID_OPERATION,
3470 "unsupported function (glNamedFramebufferTexture) called");
3471 return;
3472 }
3473
3474 /* Get the framebuffer object */
3475 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3476 if (!fb)
3477 return;
3478
3479 /* Get the texture object */
3480 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3481 return;
3482
3483 if (texObj) {
3484 if (!check_layered_texture_target(ctx, texObj->Target, func,
3485 &layered))
3486 return;
3487
3488 if (!check_level(ctx, texObj->Target, level, func))
3489 return;
3490 }
3491
3492 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3493 0, layered, func);
3494 }
3495
3496
3497 void
3498 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3499 struct gl_framebuffer *fb,
3500 GLenum attachment,
3501 struct gl_renderbuffer *rb)
3502 {
3503 assert(!_mesa_is_winsys_fbo(fb));
3504
3505 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3506
3507 assert(ctx->Driver.FramebufferRenderbuffer);
3508 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3509
3510 /* Some subsequent GL commands may depend on the framebuffer's visual
3511 * after the binding is updated. Update visual info now.
3512 */
3513 _mesa_update_framebuffer_visual(ctx, fb);
3514 }
3515
3516 static void
3517 framebuffer_renderbuffer(struct gl_context *ctx,
3518 struct gl_framebuffer *fb,
3519 GLenum attachment,
3520 struct gl_renderbuffer *rb,
3521 const char *func)
3522 {
3523 struct gl_renderbuffer_attachment *att;
3524 bool is_color_attachment;
3525
3526 if (_mesa_is_winsys_fbo(fb)) {
3527 /* Can't attach new renderbuffers to a window system framebuffer */
3528 _mesa_error(ctx, GL_INVALID_OPERATION,
3529 "%s(window-system framebuffer)", func);
3530 return;
3531 }
3532
3533 att = get_attachment(ctx, fb, attachment, &is_color_attachment);
3534 if (att == NULL) {
3535 /*
3536 * From OpenGL 4.5 spec, section 9.2.7 "Attaching Renderbuffer Images to
3537 * a Framebuffer":
3538 *
3539 * "An INVALID_OPERATION error is generated if attachment is COLOR_-
3540 * ATTACHMENTm where m is greater than or equal to the value of
3541 * MAX_COLOR_- ATTACHMENTS ."
3542 *
3543 * If we are at this point, is because the attachment is not valid, so
3544 * if is_color_attachment is true, is because of the previous reason.
3545 */
3546 if (is_color_attachment) {
3547 _mesa_error(ctx, GL_INVALID_OPERATION,
3548 "%s(invalid color attachment %s)", func,
3549 _mesa_enum_to_string(attachment));
3550 } else {
3551 _mesa_error(ctx, GL_INVALID_ENUM,
3552 "%s(invalid attachment %s)", func,
3553 _mesa_enum_to_string(attachment));
3554 }
3555
3556 return;
3557 }
3558
3559 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3560 rb && rb->Format != MESA_FORMAT_NONE) {
3561 /* make sure the renderbuffer is a depth/stencil format */
3562 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3563 if (baseFormat != GL_DEPTH_STENCIL) {
3564 _mesa_error(ctx, GL_INVALID_OPERATION,
3565 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3566 return;
3567 }
3568 }
3569
3570 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
3571 }
3572
3573 void GLAPIENTRY
3574 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3575 GLenum renderbuffertarget,
3576 GLuint renderbuffer)
3577 {
3578 struct gl_framebuffer *fb;
3579 struct gl_renderbuffer *rb;
3580 GET_CURRENT_CONTEXT(ctx);
3581
3582 fb = get_framebuffer_target(ctx, target);
3583 if (!fb) {
3584 _mesa_error(ctx, GL_INVALID_ENUM,
3585 "glFramebufferRenderbuffer(invalid target %s)",
3586 _mesa_enum_to_string(target));
3587 return;
3588 }
3589
3590 if (renderbuffertarget != GL_RENDERBUFFER) {
3591 _mesa_error(ctx, GL_INVALID_ENUM,
3592 "glFramebufferRenderbuffer(renderbuffertarget is not "
3593 "GL_RENDERBUFFER)");
3594 return;
3595 }
3596
3597 if (renderbuffer) {
3598 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3599 "glFramebufferRenderbuffer");
3600 if (!rb)
3601 return;
3602 }
3603 else {
3604 /* remove renderbuffer attachment */
3605 rb = NULL;
3606 }
3607
3608 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3609 "glFramebufferRenderbuffer");
3610 }
3611
3612
3613 void GLAPIENTRY
3614 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3615 GLenum renderbuffertarget,
3616 GLuint renderbuffer)
3617 {
3618 struct gl_framebuffer *fb;
3619 struct gl_renderbuffer *rb;
3620 GET_CURRENT_CONTEXT(ctx);
3621
3622 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3623 "glNamedFramebufferRenderbuffer");
3624 if (!fb)
3625 return;
3626
3627 if (renderbuffertarget != GL_RENDERBUFFER) {
3628 _mesa_error(ctx, GL_INVALID_ENUM,
3629 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3630 "GL_RENDERBUFFER)");
3631 return;
3632 }
3633
3634 if (renderbuffer) {
3635 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3636 "glNamedFramebufferRenderbuffer");
3637 if (!rb)
3638 return;
3639 }
3640 else {
3641 /* remove renderbuffer attachment */
3642 rb = NULL;
3643 }
3644
3645 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3646 "glNamedFramebufferRenderbuffer");
3647 }
3648
3649
3650 void
3651 _mesa_get_framebuffer_attachment_parameter(struct gl_context *ctx,
3652 struct gl_framebuffer *buffer,
3653 GLenum attachment, GLenum pname,
3654 GLint *params, const char *caller)
3655 {
3656 const struct gl_renderbuffer_attachment *att;
3657 bool is_color_attachment;
3658 GLenum err;
3659
3660 /* The error code for an attachment type of GL_NONE differs between APIs.
3661 *
3662 * From the ES 2.0.25 specification, page 127:
3663 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3664 * querying any other pname will generate INVALID_ENUM."
3665 *
3666 * From the OpenGL 3.0 specification, page 337, or identically,
3667 * the OpenGL ES 3.0.4 specification, page 240:
3668 *
3669 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3670 * framebuffer is bound to target. In this case querying pname
3671 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3672 * queries will generate an INVALID_OPERATION error."
3673 */
3674 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3675 GL_INVALID_ENUM : GL_INVALID_OPERATION;
3676
3677 if (_mesa_is_winsys_fbo(buffer)) {
3678 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3679 * says:
3680 *
3681 * "If the framebuffer currently bound to target is zero, then
3682 * INVALID_OPERATION is generated."
3683 *
3684 * The EXT_framebuffer_object spec has the same wording, and the
3685 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3686 * spec.
3687 */
3688 if ((!_mesa_is_desktop_gl(ctx) ||
3689 !ctx->Extensions.ARB_framebuffer_object)
3690 && !_mesa_is_gles3(ctx)) {
3691 _mesa_error(ctx, GL_INVALID_OPERATION,
3692 "%s(window-system framebuffer)", caller);
3693 return;
3694 }
3695
3696 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3697 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3698 _mesa_error(ctx, GL_INVALID_ENUM,
3699 "%s(invalid attachment %s)", caller,
3700 _mesa_enum_to_string(attachment));
3701 return;
3702 }
3703
3704 /* The specs are not clear about how to handle
3705 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3706 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3707 * discussed in:
3708 *
3709 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3710 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3711 */
3712 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3713 _mesa_error(ctx, GL_INVALID_ENUM,
3714 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3715 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3716 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3717 return;
3718 }
3719
3720 /* the default / window-system FBO */
3721 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
3722 }
3723 else {
3724 /* user-created framebuffer FBO */
3725 att = get_attachment(ctx, buffer, attachment, &is_color_attachment);
3726 }
3727
3728 if (att == NULL) {
3729 /*
3730 * From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries":
3731 *
3732 * "An INVALID_OPERATION error is generated if a framebuffer object
3733 * is bound to target and attachment is COLOR_ATTACHMENTm where m is
3734 * greater than or equal to the value of MAX_COLOR_ATTACHMENTS."
3735 *
3736 * If we are at this point, is because the attachment is not valid, so
3737 * if is_color_attachment is true, is because of the previous reason.
3738 */
3739 if (is_color_attachment) {
3740 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid color attachment %s)",
3741 caller, _mesa_enum_to_string(attachment));
3742 } else {
3743 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3744 _mesa_enum_to_string(attachment));
3745 }
3746 return;
3747 }
3748
3749 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3750 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3751 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3752 /* This behavior is first specified in OpenGL 4.4 specification.
3753 *
3754 * From the OpenGL 4.4 spec page 275:
3755 * "This query cannot be performed for a combined depth+stencil
3756 * attachment, since it does not have a single format."
3757 */
3758 _mesa_error(ctx, GL_INVALID_OPERATION,
3759 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3760 " is invalid for depth+stencil attachment)", caller);
3761 return;
3762 }
3763 /* the depth and stencil attachments must point to the same buffer */
3764 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT, NULL);
3765 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT, NULL);
3766 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3767 _mesa_error(ctx, GL_INVALID_OPERATION,
3768 "%s(DEPTH/STENCIL attachments differ)", caller);
3769 return;
3770 }
3771 }
3772
3773 /* No need to flush here */
3774
3775 switch (pname) {
3776 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3777 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3778 *
3779 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3780 * either no framebuffer is bound to target; or the default framebuffer
3781 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3782 * stencil bits, respectively, is zero."
3783 *
3784 * Note that we don't need explicit checks on DEPTH and STENCIL, because
3785 * on the case the spec is pointing, att->Type is already NONE, so we
3786 * just need to check att->Type.
3787 */
3788 *params = (_mesa_is_winsys_fbo(buffer) && att->Type != GL_NONE) ?
3789 GL_FRAMEBUFFER_DEFAULT : att->Type;
3790 return;
3791 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3792 if (att->Type == GL_RENDERBUFFER_EXT) {
3793 *params = att->Renderbuffer->Name;
3794 }
3795 else if (att->Type == GL_TEXTURE) {
3796 *params = att->Texture->Name;
3797 }
3798 else {
3799 assert(att->Type == GL_NONE);
3800 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3801 *params = 0;
3802 } else {
3803 goto invalid_pname_enum;
3804 }
3805 }
3806 return;
3807 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3808 if (att->Type == GL_TEXTURE) {
3809 *params = att->TextureLevel;
3810 }
3811 else if (att->Type == GL_NONE) {
3812 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3813 _mesa_enum_to_string(pname));
3814 }
3815 else {
3816 goto invalid_pname_enum;
3817 }
3818 return;
3819 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3820 if (att->Type == GL_TEXTURE) {
3821 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3822 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3823 }
3824 else {
3825 *params = 0;
3826 }
3827 }
3828 else if (att->Type == GL_NONE) {
3829 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3830 _mesa_enum_to_string(pname));
3831 }
3832 else {
3833 goto invalid_pname_enum;
3834 }
3835 return;
3836 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3837 if (ctx->API == API_OPENGLES) {
3838 goto invalid_pname_enum;
3839 } else if (att->Type == GL_NONE) {
3840 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3841 _mesa_enum_to_string(pname));
3842 } else if (att->Type == GL_TEXTURE) {
3843 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3844 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
3845 *params = att->Zoffset;
3846 }
3847 else {
3848 *params = 0;
3849 }
3850 }
3851 else {
3852 goto invalid_pname_enum;
3853 }
3854 return;
3855 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3856 if ((!_mesa_is_desktop_gl(ctx) ||
3857 !ctx->Extensions.ARB_framebuffer_object)
3858 && !_mesa_is_gles3(ctx)) {
3859 goto invalid_pname_enum;
3860 }
3861 else if (att->Type == GL_NONE) {
3862 if (_mesa_is_winsys_fbo(buffer) &&
3863 (attachment == GL_DEPTH || attachment == GL_STENCIL)) {
3864 *params = GL_LINEAR;
3865 } else {
3866 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3867 _mesa_enum_to_string(pname));
3868 }
3869 }
3870 else {
3871 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3872 *params =
3873 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
3874 }
3875 else {
3876 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3877 * if the sRGB conversion is unsupported. */
3878 *params = GL_LINEAR;
3879 }
3880 }
3881 return;
3882 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3883 if ((ctx->API != API_OPENGL_COMPAT ||
3884 !ctx->Extensions.ARB_framebuffer_object)
3885 && ctx->API != API_OPENGL_CORE
3886 && !_mesa_is_gles3(ctx)) {
3887 goto invalid_pname_enum;
3888 }
3889 else if (att->Type == GL_NONE) {
3890 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3891 _mesa_enum_to_string(pname));
3892 }
3893 else {
3894 mesa_format format = att->Renderbuffer->Format;
3895
3896 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3897 * 3.0.1 spec says:
3898 *
3899 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3900 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3901 * generate an INVALID_OPERATION error.
3902 */
3903 if (_mesa_is_gles3(ctx) &&
3904 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3905 _mesa_error(ctx, GL_INVALID_OPERATION,
3906 "%s(cannot query "
3907 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3908 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
3909 return;
3910 }
3911
3912 if (format == MESA_FORMAT_S_UINT8) {
3913 /* special cases */
3914 *params = GL_INDEX;
3915 }
3916 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
3917 /* depends on the attachment parameter */
3918 if (attachment == GL_STENCIL_ATTACHMENT) {
3919 *params = GL_INDEX;
3920 }
3921 else {
3922 *params = GL_FLOAT;
3923 }
3924 }
3925 else {
3926 *params = _mesa_get_format_datatype(format);
3927 }
3928 }
3929 return;
3930 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3931 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3932 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3933 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3934 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3935 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3936 if ((!_mesa_is_desktop_gl(ctx) ||
3937 !ctx->Extensions.ARB_framebuffer_object)
3938 && !_mesa_is_gles3(ctx)) {
3939 goto invalid_pname_enum;
3940 }
3941 else if (att->Type == GL_NONE) {
3942 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3943 _mesa_enum_to_string(pname));
3944 }
3945 else if (att->Texture) {
3946 const struct gl_texture_image *texImage =
3947 _mesa_select_tex_image(att->Texture, att->Texture->Target,
3948 att->TextureLevel);
3949 if (texImage) {
3950 *params = get_component_bits(pname, texImage->_BaseFormat,
3951 texImage->TexFormat);
3952 }
3953 else {
3954 *params = 0;
3955 }
3956 }
3957 else if (att->Renderbuffer) {
3958 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
3959 att->Renderbuffer->Format);
3960 }
3961 else {
3962 _mesa_problem(ctx, "%s: invalid FBO attachment structure", caller);
3963 }
3964 return;
3965 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
3966 if (!_mesa_has_geometry_shaders(ctx)) {
3967 goto invalid_pname_enum;
3968 } else if (att->Type == GL_TEXTURE) {
3969 *params = att->Layered;
3970 } else if (att->Type == GL_NONE) {
3971 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3972 _mesa_enum_to_string(pname));
3973 } else {
3974 goto invalid_pname_enum;
3975 }
3976 return;
3977 default:
3978 goto invalid_pname_enum;
3979 }
3980
3981 return;
3982
3983 invalid_pname_enum:
3984 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
3985 _mesa_enum_to_string(pname));
3986 return;
3987 }
3988
3989
3990 void GLAPIENTRY
3991 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
3992 GLenum pname, GLint *params)
3993 {
3994 GET_CURRENT_CONTEXT(ctx);
3995 struct gl_framebuffer *buffer;
3996
3997 buffer = get_framebuffer_target(ctx, target);
3998 if (!buffer) {
3999 _mesa_error(ctx, GL_INVALID_ENUM,
4000 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
4001 _mesa_enum_to_string(target));
4002 return;
4003 }
4004
4005 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4006 params,
4007 "glGetFramebufferAttachmentParameteriv");
4008 }
4009
4010
4011 void GLAPIENTRY
4012 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
4013 GLenum attachment,
4014 GLenum pname, GLint *params)
4015 {
4016 GET_CURRENT_CONTEXT(ctx);
4017 struct gl_framebuffer *buffer;
4018
4019 if (framebuffer) {
4020 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4021 "glGetNamedFramebufferAttachmentParameteriv");
4022 if (!buffer)
4023 return;
4024 }
4025 else {
4026 /*
4027 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4028 * 4.5 core spec (30.10.2014, PDF page 314):
4029 * "If framebuffer is zero, then the default draw framebuffer is
4030 * queried."
4031 */
4032 buffer = ctx->WinSysDrawBuffer;
4033 }
4034
4035 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4036 params,
4037 "glGetNamedFramebufferAttachmentParameteriv");
4038 }
4039
4040
4041 void GLAPIENTRY
4042 _mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
4043 GLint param)
4044 {
4045 GET_CURRENT_CONTEXT(ctx);
4046 struct gl_framebuffer *fb = NULL;
4047
4048 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4049 _mesa_error(ctx, GL_INVALID_OPERATION,
4050 "glNamedFramebufferParameteri("
4051 "ARB_framebuffer_no_attachments not implemented)");
4052 return;
4053 }
4054
4055 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4056 "glNamedFramebufferParameteri");
4057
4058 if (fb) {
4059 framebuffer_parameteri(ctx, fb, pname, param,
4060 "glNamedFramebufferParameteriv");
4061 }
4062 }
4063
4064
4065 void GLAPIENTRY
4066 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
4067 GLint *param)
4068 {
4069 GET_CURRENT_CONTEXT(ctx);
4070 struct gl_framebuffer *fb;
4071
4072 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4073 _mesa_error(ctx, GL_INVALID_OPERATION,
4074 "glNamedFramebufferParameteriv("
4075 "ARB_framebuffer_no_attachments not implemented)");
4076 return;
4077 }
4078
4079 if (framebuffer) {
4080 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4081 "glGetNamedFramebufferParameteriv");
4082 } else {
4083 fb = ctx->WinSysDrawBuffer;
4084 }
4085
4086 if (fb) {
4087 get_framebuffer_parameteriv(ctx, fb, pname, param,
4088 "glGetNamedFramebufferParameteriv");
4089 }
4090 }
4091
4092
4093 static void
4094 invalidate_framebuffer_storage(struct gl_context *ctx,
4095 struct gl_framebuffer *fb,
4096 GLsizei numAttachments,
4097 const GLenum *attachments, GLint x, GLint y,
4098 GLsizei width, GLsizei height, const char *name)
4099 {
4100 int i;
4101
4102 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4103 * Spec (2.2.2015, PDF page 522) says:
4104 * "An INVALID_VALUE error is generated if numAttachments, width, or
4105 * height is negative."
4106 */
4107 if (numAttachments < 0) {
4108 _mesa_error(ctx, GL_INVALID_VALUE,
4109 "%s(numAttachments < 0)", name);
4110 return;
4111 }
4112
4113 if (width < 0) {
4114 _mesa_error(ctx, GL_INVALID_VALUE,
4115 "%s(width < 0)", name);
4116 return;
4117 }
4118
4119 if (height < 0) {
4120 _mesa_error(ctx, GL_INVALID_VALUE,
4121 "%s(height < 0)", name);
4122 return;
4123 }
4124
4125 /* The GL_ARB_invalidate_subdata spec says:
4126 *
4127 * "If an attachment is specified that does not exist in the
4128 * framebuffer bound to <target>, it is ignored."
4129 *
4130 * It also says:
4131 *
4132 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4133 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4134 * INVALID_OPERATION is generated."
4135 *
4136 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4137 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4138 * set of retrictions).
4139 */
4140 for (i = 0; i < numAttachments; i++) {
4141 if (_mesa_is_winsys_fbo(fb)) {
4142 switch (attachments[i]) {
4143 case GL_ACCUM:
4144 case GL_AUX0:
4145 case GL_AUX1:
4146 case GL_AUX2:
4147 case GL_AUX3:
4148 /* Accumulation buffers and auxilary buffers were removed in
4149 * OpenGL 3.1, and they never existed in OpenGL ES.
4150 */
4151 if (ctx->API != API_OPENGL_COMPAT)
4152 goto invalid_enum;
4153 break;
4154 case GL_COLOR:
4155 case GL_DEPTH:
4156 case GL_STENCIL:
4157 break;
4158 case GL_BACK_LEFT:
4159 case GL_BACK_RIGHT:
4160 case GL_FRONT_LEFT:
4161 case GL_FRONT_RIGHT:
4162 if (!_mesa_is_desktop_gl(ctx))
4163 goto invalid_enum;
4164 break;
4165 default:
4166 goto invalid_enum;
4167 }
4168 } else {
4169 switch (attachments[i]) {
4170 case GL_DEPTH_ATTACHMENT:
4171 case GL_STENCIL_ATTACHMENT:
4172 break;
4173 case GL_DEPTH_STENCIL_ATTACHMENT:
4174 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4175 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4176 * extension does not make this attachment point valid on ES 2.0.
4177 */
4178 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4179 break;
4180 /* fallthrough */
4181 case GL_COLOR_ATTACHMENT0:
4182 case GL_COLOR_ATTACHMENT1:
4183 case GL_COLOR_ATTACHMENT2:
4184 case GL_COLOR_ATTACHMENT3:
4185 case GL_COLOR_ATTACHMENT4:
4186 case GL_COLOR_ATTACHMENT5:
4187 case GL_COLOR_ATTACHMENT6:
4188 case GL_COLOR_ATTACHMENT7:
4189 case GL_COLOR_ATTACHMENT8:
4190 case GL_COLOR_ATTACHMENT9:
4191 case GL_COLOR_ATTACHMENT10:
4192 case GL_COLOR_ATTACHMENT11:
4193 case GL_COLOR_ATTACHMENT12:
4194 case GL_COLOR_ATTACHMENT13:
4195 case GL_COLOR_ATTACHMENT14:
4196 case GL_COLOR_ATTACHMENT15: {
4197 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
4198 if (k >= ctx->Const.MaxColorAttachments) {
4199 _mesa_error(ctx, GL_INVALID_OPERATION,
4200 "%s(attachment >= max. color attachments)", name);
4201 return;
4202 }
4203 break;
4204 }
4205 default:
4206 goto invalid_enum;
4207 }
4208 }
4209 }
4210
4211 /* We don't actually do anything for this yet. Just return after
4212 * validating the parameters and generating the required errors.
4213 */
4214 return;
4215
4216 invalid_enum:
4217 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
4218 _mesa_enum_to_string(attachments[i]));
4219 return;
4220 }
4221
4222
4223 void GLAPIENTRY
4224 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4225 const GLenum *attachments, GLint x, GLint y,
4226 GLsizei width, GLsizei height)
4227 {
4228 struct gl_framebuffer *fb;
4229 GET_CURRENT_CONTEXT(ctx);
4230
4231 fb = get_framebuffer_target(ctx, target);
4232 if (!fb) {
4233 _mesa_error(ctx, GL_INVALID_ENUM,
4234 "glInvalidateSubFramebuffer(invalid target %s)",
4235 _mesa_enum_to_string(target));
4236 return;
4237 }
4238
4239 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4240 x, y, width, height,
4241 "glInvalidateSubFramebuffer");
4242 }
4243
4244
4245 void GLAPIENTRY
4246 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4247 GLsizei numAttachments,
4248 const GLenum *attachments,
4249 GLint x, GLint y,
4250 GLsizei width, GLsizei height)
4251 {
4252 struct gl_framebuffer *fb;
4253 GET_CURRENT_CONTEXT(ctx);
4254
4255 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4256 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4257 * default draw framebuffer is affected."
4258 */
4259 if (framebuffer) {
4260 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4261 "glInvalidateNamedFramebufferSubData");
4262 if (!fb)
4263 return;
4264 }
4265 else
4266 fb = ctx->WinSysDrawBuffer;
4267
4268 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4269 x, y, width, height,
4270 "glInvalidateNamedFramebufferSubData");
4271 }
4272
4273
4274 void GLAPIENTRY
4275 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4276 const GLenum *attachments)
4277 {
4278 struct gl_framebuffer *fb;
4279 GET_CURRENT_CONTEXT(ctx);
4280
4281 fb = get_framebuffer_target(ctx, target);
4282 if (!fb) {
4283 _mesa_error(ctx, GL_INVALID_ENUM,
4284 "glInvalidateFramebuffer(invalid target %s)",
4285 _mesa_enum_to_string(target));
4286 return;
4287 }
4288
4289 /* The GL_ARB_invalidate_subdata spec says:
4290 *
4291 * "The command
4292 *
4293 * void InvalidateFramebuffer(enum target,
4294 * sizei numAttachments,
4295 * const enum *attachments);
4296 *
4297 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4298 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4299 * <MAX_VIEWPORT_DIMS[1]> respectively."
4300 */
4301 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4302 0, 0,
4303 ctx->Const.MaxViewportWidth,
4304 ctx->Const.MaxViewportHeight,
4305 "glInvalidateFramebuffer");
4306 }
4307
4308
4309 void GLAPIENTRY
4310 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4311 GLsizei numAttachments,
4312 const GLenum *attachments)
4313 {
4314 struct gl_framebuffer *fb;
4315 GET_CURRENT_CONTEXT(ctx);
4316
4317 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4318 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4319 * default draw framebuffer is affected."
4320 */
4321 if (framebuffer) {
4322 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4323 "glInvalidateNamedFramebufferData");
4324 if (!fb)
4325 return;
4326 }
4327 else
4328 fb = ctx->WinSysDrawBuffer;
4329
4330 /* The GL_ARB_invalidate_subdata spec says:
4331 *
4332 * "The command
4333 *
4334 * void InvalidateFramebuffer(enum target,
4335 * sizei numAttachments,
4336 * const enum *attachments);
4337 *
4338 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4339 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4340 * <MAX_VIEWPORT_DIMS[1]> respectively."
4341 */
4342 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4343 0, 0,
4344 ctx->Const.MaxViewportWidth,
4345 ctx->Const.MaxViewportHeight,
4346 "glInvalidateNamedFramebufferData");
4347 }
4348
4349
4350 void GLAPIENTRY
4351 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4352 const GLenum *attachments)
4353 {
4354 struct gl_framebuffer *fb;
4355 GLint i;
4356
4357 GET_CURRENT_CONTEXT(ctx);
4358
4359 fb = get_framebuffer_target(ctx, target);
4360 if (!fb) {
4361 _mesa_error(ctx, GL_INVALID_ENUM,
4362 "glDiscardFramebufferEXT(target %s)",
4363 _mesa_enum_to_string(target));
4364 return;
4365 }
4366
4367 if (numAttachments < 0) {
4368 _mesa_error(ctx, GL_INVALID_VALUE,
4369 "glDiscardFramebufferEXT(numAttachments < 0)");
4370 return;
4371 }
4372
4373 for (i = 0; i < numAttachments; i++) {
4374 switch (attachments[i]) {
4375 case GL_COLOR:
4376 case GL_DEPTH:
4377 case GL_STENCIL:
4378 if (_mesa_is_user_fbo(fb))
4379 goto invalid_enum;
4380 break;
4381 case GL_COLOR_ATTACHMENT0:
4382 case GL_DEPTH_ATTACHMENT:
4383 case GL_STENCIL_ATTACHMENT:
4384 if (_mesa_is_winsys_fbo(fb))
4385 goto invalid_enum;
4386 break;
4387 default:
4388 goto invalid_enum;
4389 }
4390 }
4391
4392 if (ctx->Driver.DiscardFramebuffer)
4393 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4394
4395 return;
4396
4397 invalid_enum:
4398 _mesa_error(ctx, GL_INVALID_ENUM,
4399 "glDiscardFramebufferEXT(attachment %s)",
4400 _mesa_enum_to_string(attachments[i]));
4401 }