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