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