mesa/main: R10G10B10_(A2) formats are not color renderable in ES
[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_RGB9_E5:
733 case GL_RG8_SNORM:
734 case GL_R8_SNORM:
735 return GL_FALSE;
736 default:
737 break;
738 }
739
740 if (internalFormat != GL_RGB10_A2 &&
741 (format == MESA_FORMAT_B10G10R10A2_UNORM ||
742 format == MESA_FORMAT_B10G10R10X2_UNORM ||
743 format == MESA_FORMAT_R10G10B10A2_UNORM ||
744 format == MESA_FORMAT_R10G10B10X2_UNORM)) {
745 return GL_FALSE;
746 }
747
748 return GL_TRUE;
749 }
750
751
752 /**
753 * Is the given base format a legal format for a depth/stencil renderbuffer?
754 */
755 static GLboolean
756 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
757 {
758 switch (baseFormat) {
759 case GL_DEPTH_COMPONENT:
760 case GL_DEPTH_STENCIL_EXT:
761 return GL_TRUE;
762 default:
763 return GL_FALSE;
764 }
765 }
766
767
768 /**
769 * Test if an attachment point is complete and update its Complete field.
770 * \param format if GL_COLOR, this is a color attachment point,
771 * if GL_DEPTH, this is a depth component attachment point,
772 * if GL_STENCIL, this is a stencil component attachment point.
773 */
774 static void
775 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
776 struct gl_renderbuffer_attachment *att)
777 {
778 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
779
780 /* assume complete */
781 att->Complete = GL_TRUE;
782
783 /* Look for reasons why the attachment might be incomplete */
784 if (att->Type == GL_TEXTURE) {
785 const struct gl_texture_object *texObj = att->Texture;
786 struct gl_texture_image *texImage;
787 GLenum baseFormat;
788
789 if (!texObj) {
790 att_incomplete("no texobj");
791 att->Complete = GL_FALSE;
792 return;
793 }
794
795 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
796 if (!texImage) {
797 att_incomplete("no teximage");
798 att->Complete = GL_FALSE;
799 return;
800 }
801 if (texImage->Width < 1 || texImage->Height < 1) {
802 att_incomplete("teximage width/height=0");
803 att->Complete = GL_FALSE;
804 return;
805 }
806
807 switch (texObj->Target) {
808 case GL_TEXTURE_3D:
809 if (att->Zoffset >= texImage->Depth) {
810 att_incomplete("bad z offset");
811 att->Complete = GL_FALSE;
812 return;
813 }
814 break;
815 case GL_TEXTURE_1D_ARRAY:
816 if (att->Zoffset >= texImage->Height) {
817 att_incomplete("bad 1D-array layer");
818 att->Complete = GL_FALSE;
819 return;
820 }
821 break;
822 case GL_TEXTURE_2D_ARRAY:
823 if (att->Zoffset >= texImage->Depth) {
824 att_incomplete("bad 2D-array layer");
825 att->Complete = GL_FALSE;
826 return;
827 }
828 break;
829 case GL_TEXTURE_CUBE_MAP_ARRAY:
830 if (att->Zoffset >= texImage->Depth) {
831 att_incomplete("bad cube-array layer");
832 att->Complete = GL_FALSE;
833 return;
834 }
835 break;
836 }
837
838 baseFormat = texImage->_BaseFormat;
839
840 if (format == GL_COLOR) {
841 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
842 att_incomplete("bad format");
843 att->Complete = GL_FALSE;
844 return;
845 }
846 if (_mesa_is_format_compressed(texImage->TexFormat)) {
847 att_incomplete("compressed internalformat");
848 att->Complete = GL_FALSE;
849 return;
850 }
851
852 /* OES_texture_float allows creation and use of floating point
853 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
854 * these textures to be used as a render target, this is done via
855 * GL_EXT_color_buffer(_half)_float with set of new sized types.
856 */
857 if (_mesa_is_gles(ctx) && (texImage->TexObject->_IsFloat ||
858 texImage->TexObject->_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, GLenum target, GLint level,
3227 const char *caller)
3228 {
3229 if ((level < 0) ||
3230 (level >= _mesa_max_texture_levels(ctx, target))) {
3231 _mesa_error(ctx, GL_INVALID_VALUE,
3232 "%s(invalid level %d)", caller, level);
3233 return false;
3234 }
3235
3236 return true;
3237 }
3238
3239
3240 struct gl_renderbuffer_attachment *
3241 _mesa_get_and_validate_attachment(struct gl_context *ctx,
3242 struct gl_framebuffer *fb,
3243 GLenum attachment, const char *caller)
3244 {
3245 /* The window-system framebuffer object is immutable */
3246 if (_mesa_is_winsys_fbo(fb)) {
3247 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3248 caller);
3249 return NULL;
3250 }
3251
3252 /* Not a hash lookup, so we can afford to get the attachment here. */
3253 bool is_color_attachment;
3254 struct gl_renderbuffer_attachment *att =
3255 get_attachment(ctx, fb, attachment, &is_color_attachment);
3256 if (att == NULL) {
3257 if (is_color_attachment) {
3258 _mesa_error(ctx, GL_INVALID_OPERATION,
3259 "%s(invalid color attachment %s)", caller,
3260 _mesa_enum_to_string(attachment));
3261 } else {
3262 _mesa_error(ctx, GL_INVALID_ENUM,
3263 "%s(invalid attachment %s)", caller,
3264 _mesa_enum_to_string(attachment));
3265 }
3266 return NULL;
3267 }
3268
3269 return att;
3270 }
3271
3272
3273 void
3274 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3275 GLenum attachment,
3276 struct gl_renderbuffer_attachment *att,
3277 struct gl_texture_object *texObj, GLenum textarget,
3278 GLint level, GLuint layer, GLboolean layered)
3279 {
3280 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3281
3282 mtx_lock(&fb->Mutex);
3283 if (texObj) {
3284 if (attachment == GL_DEPTH_ATTACHMENT &&
3285 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3286 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3287 _mesa_tex_target_to_face(textarget) ==
3288 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
3289 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
3290 /* The texture object is already attached to the stencil attachment
3291 * point. Don't create a new renderbuffer; just reuse the stencil
3292 * attachment's. This is required to prevent a GL error in
3293 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3294 */
3295 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3296 BUFFER_STENCIL);
3297 } else if (attachment == GL_STENCIL_ATTACHMENT &&
3298 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
3299 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3300 _mesa_tex_target_to_face(textarget) ==
3301 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
3302 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
3303 /* As above, but with depth and stencil transposed. */
3304 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3305 BUFFER_DEPTH);
3306 } else {
3307 set_texture_attachment(ctx, fb, att, texObj, textarget,
3308 level, layer, layered);
3309
3310 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3311 /* Above we created a new renderbuffer and attached it to the
3312 * depth attachment point. Now attach it to the stencil attachment
3313 * point too.
3314 */
3315 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3316 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3317 BUFFER_DEPTH);
3318 }
3319 }
3320
3321 /* Set the render-to-texture flag. We'll check this flag in
3322 * glTexImage() and friends to determine if we need to revalidate
3323 * any FBOs that might be rendering into this texture.
3324 * This flag never gets cleared since it's non-trivial to determine
3325 * when all FBOs might be done rendering to this texture. That's OK
3326 * though since it's uncommon to render to a texture then repeatedly
3327 * call glTexImage() to change images in the texture.
3328 */
3329 texObj->_RenderToTexture = GL_TRUE;
3330 }
3331 else {
3332 remove_attachment(ctx, att);
3333 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3334 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3335 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
3336 }
3337 }
3338
3339 invalidate_framebuffer(fb);
3340
3341 mtx_unlock(&fb->Mutex);
3342 }
3343
3344
3345 static void
3346 framebuffer_texture_with_dims_no_error(GLenum target, GLenum attachment,
3347 GLenum textarget, GLuint texture,
3348 GLint level, GLint layer)
3349 {
3350 GET_CURRENT_CONTEXT(ctx);
3351
3352 /* Get the framebuffer object */
3353 struct gl_framebuffer *fb = get_framebuffer_target(ctx, target);
3354
3355 /* Get the texture object */
3356 struct gl_texture_object *texObj =
3357 get_texture_for_framebuffer(ctx, texture);
3358
3359 struct gl_renderbuffer_attachment *att =
3360 get_attachment(ctx, fb, attachment, NULL);
3361
3362 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
3363 level, layer, GL_FALSE);
3364 }
3365
3366
3367 static void
3368 framebuffer_texture_with_dims(int dims, GLenum target,
3369 GLenum attachment, GLenum textarget,
3370 GLuint texture, GLint level, GLint layer,
3371 const char *caller)
3372 {
3373 GET_CURRENT_CONTEXT(ctx);
3374 struct gl_framebuffer *fb;
3375 struct gl_texture_object *texObj;
3376
3377 /* Get the framebuffer object */
3378 fb = get_framebuffer_target(ctx, target);
3379 if (!fb) {
3380 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
3381 _mesa_enum_to_string(target));
3382 return;
3383 }
3384
3385 /* Get the texture object */
3386 if (!get_texture_for_framebuffer_err(ctx, texture, false, caller, &texObj))
3387 return;
3388
3389 if (texObj) {
3390 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3391 return;
3392
3393 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3394 return;
3395
3396 if (!check_level(ctx, textarget, level, caller))
3397 return;
3398 }
3399
3400 struct gl_renderbuffer_attachment *att =
3401 _mesa_get_and_validate_attachment(ctx, fb, attachment, caller);
3402 if (!att)
3403 return;
3404
3405 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
3406 level, layer, GL_FALSE);
3407 }
3408
3409
3410 void GLAPIENTRY
3411 _mesa_FramebufferTexture1D_no_error(GLenum target, GLenum attachment,
3412 GLenum textarget, GLuint texture,
3413 GLint level)
3414 {
3415 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3416 texture, level, 0);
3417 }
3418
3419
3420 void GLAPIENTRY
3421 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3422 GLenum textarget, GLuint texture, GLint level)
3423 {
3424 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3425 level, 0, "glFramebufferTexture1D");
3426 }
3427
3428
3429 void GLAPIENTRY
3430 _mesa_FramebufferTexture2D_no_error(GLenum target, GLenum attachment,
3431 GLenum textarget, GLuint texture,
3432 GLint level)
3433 {
3434 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3435 texture, level, 0);
3436 }
3437
3438
3439 void GLAPIENTRY
3440 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
3441 GLenum textarget, GLuint texture, GLint level)
3442 {
3443 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3444 level, 0, "glFramebufferTexture2D");
3445 }
3446
3447
3448 void GLAPIENTRY
3449 _mesa_FramebufferTexture3D_no_error(GLenum target, GLenum attachment,
3450 GLenum textarget, GLuint texture,
3451 GLint level, GLint layer)
3452 {
3453 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3454 texture, level, layer);
3455 }
3456
3457
3458 void GLAPIENTRY
3459 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
3460 GLenum textarget, GLuint texture,
3461 GLint level, GLint layer)
3462 {
3463 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3464 level, layer, "glFramebufferTexture3D");
3465 }
3466
3467
3468 static ALWAYS_INLINE void
3469 frame_buffer_texture(GLuint framebuffer, GLenum target,
3470 GLenum attachment, GLuint texture,
3471 GLint level, GLint layer, const char *func,
3472 bool dsa, bool no_error, bool check_layered)
3473 {
3474 GET_CURRENT_CONTEXT(ctx);
3475 GLboolean layered = GL_FALSE;
3476
3477 if (!no_error && check_layered) {
3478 if (!_mesa_has_geometry_shaders(ctx)) {
3479 _mesa_error(ctx, GL_INVALID_OPERATION,
3480 "unsupported function (%s) called", func);
3481 return;
3482 }
3483 }
3484
3485 /* Get the framebuffer object */
3486 struct gl_framebuffer *fb;
3487 if (no_error) {
3488 if (dsa) {
3489 fb = _mesa_lookup_framebuffer(ctx, framebuffer);
3490 } else {
3491 fb = get_framebuffer_target(ctx, target);
3492 }
3493 } else {
3494 if (dsa) {
3495 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3496 if (!fb)
3497 return;
3498 } else {
3499 fb = get_framebuffer_target(ctx, target);
3500 if (!fb) {
3501 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)",
3502 func, _mesa_enum_to_string(target));
3503 return;
3504 }
3505 }
3506 }
3507
3508 /* Get the texture object and framebuffer attachment*/
3509 struct gl_renderbuffer_attachment *att;
3510 struct gl_texture_object *texObj;
3511 if (no_error) {
3512 texObj = get_texture_for_framebuffer(ctx, texture);
3513 att = get_attachment(ctx, fb, attachment, NULL);
3514 } else {
3515 if (!get_texture_for_framebuffer_err(ctx, texture, check_layered, func,
3516 &texObj))
3517 return;
3518
3519 att = _mesa_get_and_validate_attachment(ctx, fb, attachment, func);
3520 if (!att)
3521 return;
3522 }
3523
3524 GLenum textarget = 0;
3525 if (texObj) {
3526 if (check_layered) {
3527 /* We do this regardless of no_error because this sets layered */
3528 if (!check_layered_texture_target(ctx, texObj->Target, func,
3529 &layered))
3530 return;
3531 }
3532
3533 if (!no_error) {
3534 if (!check_layered) {
3535 if (!check_texture_target(ctx, texObj->Target, func))
3536 return;
3537
3538 if (!check_layer(ctx, texObj->Target, layer, func))
3539 return;
3540 }
3541
3542 if (!check_level(ctx, texObj->Target, level, func))
3543 return;
3544 }
3545
3546 if (!check_layered && texObj->Target == GL_TEXTURE_CUBE_MAP) {
3547 assert(layer >= 0 && layer < 6);
3548 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3549 layer = 0;
3550 }
3551 }
3552
3553 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
3554 level, layer, layered);
3555 }
3556
3557 void GLAPIENTRY
3558 _mesa_FramebufferTextureLayer_no_error(GLenum target, GLenum attachment,
3559 GLuint texture, GLint level,
3560 GLint layer)
3561 {
3562 frame_buffer_texture(0, target, attachment, texture, level, layer,
3563 "glFramebufferTextureLayer", false, true, false);
3564 }
3565
3566
3567 void GLAPIENTRY
3568 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3569 GLuint texture, GLint level, GLint layer)
3570 {
3571 frame_buffer_texture(0, target, attachment, texture, level, layer,
3572 "glFramebufferTextureLayer", false, false, false);
3573 }
3574
3575
3576 void GLAPIENTRY
3577 _mesa_NamedFramebufferTextureLayer_no_error(GLuint framebuffer,
3578 GLenum attachment,
3579 GLuint texture, GLint level,
3580 GLint layer)
3581 {
3582 frame_buffer_texture(framebuffer, 0, attachment, texture, level, layer,
3583 "glNamedFramebufferTextureLayer", true, true, false);
3584 }
3585
3586
3587 void GLAPIENTRY
3588 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3589 GLuint texture, GLint level, GLint layer)
3590 {
3591 frame_buffer_texture(framebuffer, 0, attachment, texture, level, layer,
3592 "glNamedFramebufferTextureLayer", true, false, false);
3593 }
3594
3595
3596 void GLAPIENTRY
3597 _mesa_FramebufferTexture_no_error(GLenum target, GLenum attachment,
3598 GLuint texture, GLint level)
3599 {
3600 frame_buffer_texture(0, target, attachment, texture, level, 0,
3601 "glFramebufferTexture", false, true, true);
3602 }
3603
3604
3605 void GLAPIENTRY
3606 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3607 GLuint texture, GLint level)
3608 {
3609 frame_buffer_texture(0, target, attachment, texture, level, 0,
3610 "glFramebufferTexture", false, false, true);
3611 }
3612
3613 void GLAPIENTRY
3614 _mesa_NamedFramebufferTexture_no_error(GLuint framebuffer, GLenum attachment,
3615 GLuint texture, GLint level)
3616 {
3617 frame_buffer_texture(framebuffer, 0, attachment, texture, level, 0,
3618 "glNamedFramebufferTexture", true, true, true);
3619 }
3620
3621
3622 void GLAPIENTRY
3623 _mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3624 GLuint texture, GLint level)
3625 {
3626 frame_buffer_texture(framebuffer, 0, attachment, texture, level, 0,
3627 "glNamedFramebufferTexture", true, false, true);
3628 }
3629
3630
3631 void
3632 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3633 struct gl_framebuffer *fb,
3634 GLenum attachment,
3635 struct gl_renderbuffer *rb)
3636 {
3637 assert(!_mesa_is_winsys_fbo(fb));
3638
3639 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3640
3641 assert(ctx->Driver.FramebufferRenderbuffer);
3642 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3643
3644 /* Some subsequent GL commands may depend on the framebuffer's visual
3645 * after the binding is updated. Update visual info now.
3646 */
3647 _mesa_update_framebuffer_visual(ctx, fb);
3648 }
3649
3650 static ALWAYS_INLINE void
3651 framebuffer_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
3652 GLenum attachment, GLenum renderbuffertarget,
3653 GLuint renderbuffer, const char *func, bool no_error)
3654 {
3655 struct gl_renderbuffer_attachment *att;
3656 struct gl_renderbuffer *rb;
3657 bool is_color_attachment;
3658
3659 if (!no_error && renderbuffertarget != GL_RENDERBUFFER) {
3660 _mesa_error(ctx, GL_INVALID_ENUM,
3661 "%s(renderbuffertarget is not GL_RENDERBUFFER)", func);
3662 return;
3663 }
3664
3665 if (renderbuffer) {
3666 if (!no_error) {
3667 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer, func);
3668 if (!rb)
3669 return;
3670 } else {
3671 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
3672 }
3673 } else {
3674 /* remove renderbuffer attachment */
3675 rb = NULL;
3676 }
3677
3678 if (!no_error) {
3679 if (_mesa_is_winsys_fbo(fb)) {
3680 /* Can't attach new renderbuffers to a window system framebuffer */
3681 _mesa_error(ctx, GL_INVALID_OPERATION,
3682 "%s(window-system framebuffer)", func);
3683 return;
3684 }
3685
3686 att = get_attachment(ctx, fb, attachment, &is_color_attachment);
3687 if (att == NULL) {
3688 /*
3689 * From OpenGL 4.5 spec, section 9.2.7 "Attaching Renderbuffer Images
3690 * to a Framebuffer":
3691 *
3692 * "An INVALID_OPERATION error is generated if attachment is
3693 * COLOR_- ATTACHMENTm where m is greater than or equal to the
3694 * value of MAX_COLOR_- ATTACHMENTS ."
3695 *
3696 * If we are at this point, is because the attachment is not valid, so
3697 * if is_color_attachment is true, is because of the previous reason.
3698 */
3699 if (is_color_attachment) {
3700 _mesa_error(ctx, GL_INVALID_OPERATION,
3701 "%s(invalid color attachment %s)", func,
3702 _mesa_enum_to_string(attachment));
3703 } else {
3704 _mesa_error(ctx, GL_INVALID_ENUM,
3705 "%s(invalid attachment %s)", func,
3706 _mesa_enum_to_string(attachment));
3707 }
3708
3709 return;
3710 }
3711
3712 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3713 rb && rb->Format != MESA_FORMAT_NONE) {
3714 /* make sure the renderbuffer is a depth/stencil format */
3715 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3716 if (baseFormat != GL_DEPTH_STENCIL) {
3717 _mesa_error(ctx, GL_INVALID_OPERATION,
3718 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3719 return;
3720 }
3721 }
3722 }
3723
3724 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
3725 }
3726
3727 static void
3728 framebuffer_renderbuffer_error(struct gl_context *ctx,
3729 struct gl_framebuffer *fb, GLenum attachment,
3730 GLenum renderbuffertarget,
3731 GLuint renderbuffer, const char *func)
3732 {
3733 framebuffer_renderbuffer(ctx, fb, attachment, renderbuffertarget,
3734 renderbuffer, func, false);
3735 }
3736
3737 static void
3738 framebuffer_renderbuffer_no_error(struct gl_context *ctx,
3739 struct gl_framebuffer *fb, GLenum attachment,
3740 GLenum renderbuffertarget,
3741 GLuint renderbuffer, const char *func)
3742 {
3743 framebuffer_renderbuffer(ctx, fb, attachment, renderbuffertarget,
3744 renderbuffer, func, true);
3745 }
3746
3747 void GLAPIENTRY
3748 _mesa_FramebufferRenderbuffer_no_error(GLenum target, GLenum attachment,
3749 GLenum renderbuffertarget,
3750 GLuint renderbuffer)
3751 {
3752 GET_CURRENT_CONTEXT(ctx);
3753
3754 struct gl_framebuffer *fb = get_framebuffer_target(ctx, target);
3755 framebuffer_renderbuffer_no_error(ctx, fb, attachment, renderbuffertarget,
3756 renderbuffer, "glFramebufferRenderbuffer");
3757 }
3758
3759 void GLAPIENTRY
3760 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3761 GLenum renderbuffertarget,
3762 GLuint renderbuffer)
3763 {
3764 struct gl_framebuffer *fb;
3765 GET_CURRENT_CONTEXT(ctx);
3766
3767 fb = get_framebuffer_target(ctx, target);
3768 if (!fb) {
3769 _mesa_error(ctx, GL_INVALID_ENUM,
3770 "glFramebufferRenderbuffer(invalid target %s)",
3771 _mesa_enum_to_string(target));
3772 return;
3773 }
3774
3775 framebuffer_renderbuffer_error(ctx, fb, attachment, renderbuffertarget,
3776 renderbuffer, "glFramebufferRenderbuffer");
3777 }
3778
3779 void GLAPIENTRY
3780 _mesa_NamedFramebufferRenderbuffer_no_error(GLuint framebuffer,
3781 GLenum attachment,
3782 GLenum renderbuffertarget,
3783 GLuint renderbuffer)
3784 {
3785 GET_CURRENT_CONTEXT(ctx);
3786
3787 struct gl_framebuffer *fb = _mesa_lookup_framebuffer(ctx, framebuffer);
3788 framebuffer_renderbuffer_no_error(ctx, fb, attachment, renderbuffertarget,
3789 renderbuffer,
3790 "glNamedFramebufferRenderbuffer");
3791 }
3792
3793 void GLAPIENTRY
3794 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3795 GLenum renderbuffertarget,
3796 GLuint renderbuffer)
3797 {
3798 struct gl_framebuffer *fb;
3799 GET_CURRENT_CONTEXT(ctx);
3800
3801 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3802 "glNamedFramebufferRenderbuffer");
3803 if (!fb)
3804 return;
3805
3806 framebuffer_renderbuffer_error(ctx, fb, attachment, renderbuffertarget,
3807 renderbuffer,
3808 "glNamedFramebufferRenderbuffer");
3809 }
3810
3811
3812 static void
3813 get_framebuffer_attachment_parameter(struct gl_context *ctx,
3814 struct gl_framebuffer *buffer,
3815 GLenum attachment, GLenum pname,
3816 GLint *params, const char *caller)
3817 {
3818 const struct gl_renderbuffer_attachment *att;
3819 bool is_color_attachment = false;
3820 GLenum err;
3821
3822 /* The error code for an attachment type of GL_NONE differs between APIs.
3823 *
3824 * From the ES 2.0.25 specification, page 127:
3825 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3826 * querying any other pname will generate INVALID_ENUM."
3827 *
3828 * From the OpenGL 3.0 specification, page 337, or identically,
3829 * the OpenGL ES 3.0.4 specification, page 240:
3830 *
3831 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3832 * framebuffer is bound to target. In this case querying pname
3833 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3834 * queries will generate an INVALID_OPERATION error."
3835 */
3836 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3837 GL_INVALID_ENUM : GL_INVALID_OPERATION;
3838
3839 if (_mesa_is_winsys_fbo(buffer)) {
3840 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3841 * says:
3842 *
3843 * "If the framebuffer currently bound to target is zero, then
3844 * INVALID_OPERATION is generated."
3845 *
3846 * The EXT_framebuffer_object spec has the same wording, and the
3847 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3848 * spec.
3849 */
3850 if ((!_mesa_is_desktop_gl(ctx) ||
3851 !ctx->Extensions.ARB_framebuffer_object)
3852 && !_mesa_is_gles3(ctx)) {
3853 _mesa_error(ctx, GL_INVALID_OPERATION,
3854 "%s(window-system framebuffer)", caller);
3855 return;
3856 }
3857
3858 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3859 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3860 _mesa_error(ctx, GL_INVALID_ENUM,
3861 "%s(invalid attachment %s)", caller,
3862 _mesa_enum_to_string(attachment));
3863 return;
3864 }
3865
3866 /* The specs are not clear about how to handle
3867 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3868 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3869 * discussed in:
3870 *
3871 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3872 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3873 */
3874 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3875 _mesa_error(ctx, GL_INVALID_ENUM,
3876 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3877 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3878 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3879 return;
3880 }
3881
3882 /* the default / window-system FBO */
3883 att = get_fb0_attachment(ctx, buffer, attachment);
3884 }
3885 else {
3886 /* user-created framebuffer FBO */
3887 att = get_attachment(ctx, buffer, attachment, &is_color_attachment);
3888 }
3889
3890 if (att == NULL) {
3891 /*
3892 * From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries":
3893 *
3894 * "An INVALID_OPERATION error is generated if a framebuffer object
3895 * is bound to target and attachment is COLOR_ATTACHMENTm where m is
3896 * greater than or equal to the value of MAX_COLOR_ATTACHMENTS."
3897 *
3898 * If we are at this point, is because the attachment is not valid, so
3899 * if is_color_attachment is true, is because of the previous reason.
3900 */
3901 if (is_color_attachment) {
3902 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid color attachment %s)",
3903 caller, _mesa_enum_to_string(attachment));
3904 } else {
3905 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3906 _mesa_enum_to_string(attachment));
3907 }
3908 return;
3909 }
3910
3911 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3912 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3913 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3914 /* This behavior is first specified in OpenGL 4.4 specification.
3915 *
3916 * From the OpenGL 4.4 spec page 275:
3917 * "This query cannot be performed for a combined depth+stencil
3918 * attachment, since it does not have a single format."
3919 */
3920 _mesa_error(ctx, GL_INVALID_OPERATION,
3921 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3922 " is invalid for depth+stencil attachment)", caller);
3923 return;
3924 }
3925 /* the depth and stencil attachments must point to the same buffer */
3926 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT, NULL);
3927 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT, NULL);
3928 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3929 _mesa_error(ctx, GL_INVALID_OPERATION,
3930 "%s(DEPTH/STENCIL attachments differ)", caller);
3931 return;
3932 }
3933 }
3934
3935 /* No need to flush here */
3936
3937 switch (pname) {
3938 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3939 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3940 *
3941 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3942 * either no framebuffer is bound to target; or the default framebuffer
3943 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3944 * stencil bits, respectively, is zero."
3945 *
3946 * Note that we don't need explicit checks on DEPTH and STENCIL, because
3947 * on the case the spec is pointing, att->Type is already NONE, so we
3948 * just need to check att->Type.
3949 */
3950 *params = (_mesa_is_winsys_fbo(buffer) && att->Type != GL_NONE) ?
3951 GL_FRAMEBUFFER_DEFAULT : att->Type;
3952 return;
3953 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3954 if (att->Type == GL_RENDERBUFFER_EXT) {
3955 *params = att->Renderbuffer->Name;
3956 }
3957 else if (att->Type == GL_TEXTURE) {
3958 *params = att->Texture->Name;
3959 }
3960 else {
3961 assert(att->Type == GL_NONE);
3962 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3963 *params = 0;
3964 } else {
3965 goto invalid_pname_enum;
3966 }
3967 }
3968 return;
3969 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3970 if (att->Type == GL_TEXTURE) {
3971 *params = att->TextureLevel;
3972 }
3973 else if (att->Type == GL_NONE) {
3974 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3975 _mesa_enum_to_string(pname));
3976 }
3977 else {
3978 goto invalid_pname_enum;
3979 }
3980 return;
3981 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3982 if (att->Type == GL_TEXTURE) {
3983 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3984 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3985 }
3986 else {
3987 *params = 0;
3988 }
3989 }
3990 else if (att->Type == GL_NONE) {
3991 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3992 _mesa_enum_to_string(pname));
3993 }
3994 else {
3995 goto invalid_pname_enum;
3996 }
3997 return;
3998 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3999 if (ctx->API == API_OPENGLES) {
4000 goto invalid_pname_enum;
4001 } else if (att->Type == GL_NONE) {
4002 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4003 _mesa_enum_to_string(pname));
4004 } else if (att->Type == GL_TEXTURE) {
4005 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
4006 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
4007 *params = att->Zoffset;
4008 }
4009 else {
4010 *params = 0;
4011 }
4012 }
4013 else {
4014 goto invalid_pname_enum;
4015 }
4016 return;
4017 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
4018 if ((!_mesa_is_desktop_gl(ctx) ||
4019 !ctx->Extensions.ARB_framebuffer_object)
4020 && !_mesa_is_gles3(ctx)) {
4021 goto invalid_pname_enum;
4022 }
4023 else if (att->Type == GL_NONE) {
4024 if (_mesa_is_winsys_fbo(buffer) &&
4025 (attachment == GL_DEPTH || attachment == GL_STENCIL)) {
4026 *params = GL_LINEAR;
4027 } else {
4028 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4029 _mesa_enum_to_string(pname));
4030 }
4031 }
4032 else {
4033 if (ctx->Extensions.EXT_framebuffer_sRGB) {
4034 *params =
4035 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
4036 }
4037 else {
4038 /* According to ARB_framebuffer_sRGB, we should return LINEAR
4039 * if the sRGB conversion is unsupported. */
4040 *params = GL_LINEAR;
4041 }
4042 }
4043 return;
4044 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
4045 if ((ctx->API != API_OPENGL_COMPAT ||
4046 !ctx->Extensions.ARB_framebuffer_object)
4047 && ctx->API != API_OPENGL_CORE
4048 && !_mesa_is_gles3(ctx)) {
4049 goto invalid_pname_enum;
4050 }
4051 else if (att->Type == GL_NONE) {
4052 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4053 _mesa_enum_to_string(pname));
4054 }
4055 else {
4056 mesa_format format = att->Renderbuffer->Format;
4057
4058 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
4059 * 3.0.1 spec says:
4060 *
4061 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
4062 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
4063 * generate an INVALID_OPERATION error.
4064 */
4065 if (_mesa_is_gles3(ctx) &&
4066 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
4067 _mesa_error(ctx, GL_INVALID_OPERATION,
4068 "%s(cannot query "
4069 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
4070 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
4071 return;
4072 }
4073
4074 if (format == MESA_FORMAT_S_UINT8) {
4075 /* special cases */
4076 *params = GL_INDEX;
4077 }
4078 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
4079 /* depends on the attachment parameter */
4080 if (attachment == GL_STENCIL_ATTACHMENT) {
4081 *params = GL_INDEX;
4082 }
4083 else {
4084 *params = GL_FLOAT;
4085 }
4086 }
4087 else {
4088 *params = _mesa_get_format_datatype(format);
4089 }
4090 }
4091 return;
4092 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
4093 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
4094 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
4095 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
4096 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
4097 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
4098 if ((!_mesa_is_desktop_gl(ctx) ||
4099 !ctx->Extensions.ARB_framebuffer_object)
4100 && !_mesa_is_gles3(ctx)) {
4101 goto invalid_pname_enum;
4102 }
4103 else if (att->Texture) {
4104 const struct gl_texture_image *texImage =
4105 _mesa_select_tex_image(att->Texture, att->Texture->Target,
4106 att->TextureLevel);
4107 if (texImage) {
4108 *params = get_component_bits(pname, texImage->_BaseFormat,
4109 texImage->TexFormat);
4110 }
4111 else {
4112 *params = 0;
4113 }
4114 }
4115 else if (att->Renderbuffer) {
4116 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
4117 att->Renderbuffer->Format);
4118 }
4119 else {
4120 assert(att->Type == GL_NONE);
4121 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4122 _mesa_enum_to_string(pname));
4123 }
4124 return;
4125 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
4126 if (!_mesa_has_geometry_shaders(ctx)) {
4127 goto invalid_pname_enum;
4128 } else if (att->Type == GL_TEXTURE) {
4129 *params = att->Layered;
4130 } else if (att->Type == GL_NONE) {
4131 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4132 _mesa_enum_to_string(pname));
4133 } else {
4134 goto invalid_pname_enum;
4135 }
4136 return;
4137 default:
4138 goto invalid_pname_enum;
4139 }
4140
4141 return;
4142
4143 invalid_pname_enum:
4144 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
4145 _mesa_enum_to_string(pname));
4146 return;
4147 }
4148
4149
4150 void GLAPIENTRY
4151 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
4152 GLenum pname, GLint *params)
4153 {
4154 GET_CURRENT_CONTEXT(ctx);
4155 struct gl_framebuffer *buffer;
4156
4157 buffer = get_framebuffer_target(ctx, target);
4158 if (!buffer) {
4159 _mesa_error(ctx, GL_INVALID_ENUM,
4160 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
4161 _mesa_enum_to_string(target));
4162 return;
4163 }
4164
4165 get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4166 params,
4167 "glGetFramebufferAttachmentParameteriv");
4168 }
4169
4170
4171 void GLAPIENTRY
4172 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
4173 GLenum attachment,
4174 GLenum pname, GLint *params)
4175 {
4176 GET_CURRENT_CONTEXT(ctx);
4177 struct gl_framebuffer *buffer;
4178
4179 if (framebuffer) {
4180 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4181 "glGetNamedFramebufferAttachmentParameteriv");
4182 if (!buffer)
4183 return;
4184 }
4185 else {
4186 /*
4187 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4188 * 4.5 core spec (30.10.2014, PDF page 314):
4189 * "If framebuffer is zero, then the default draw framebuffer is
4190 * queried."
4191 */
4192 buffer = ctx->WinSysDrawBuffer;
4193 }
4194
4195 get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4196 params,
4197 "glGetNamedFramebufferAttachmentParameteriv");
4198 }
4199
4200
4201 void GLAPIENTRY
4202 _mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
4203 GLint param)
4204 {
4205 GET_CURRENT_CONTEXT(ctx);
4206 struct gl_framebuffer *fb = NULL;
4207
4208 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4209 _mesa_error(ctx, GL_INVALID_OPERATION,
4210 "glNamedFramebufferParameteri("
4211 "ARB_framebuffer_no_attachments not implemented)");
4212 return;
4213 }
4214
4215 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4216 "glNamedFramebufferParameteri");
4217
4218 if (fb) {
4219 framebuffer_parameteri(ctx, fb, pname, param,
4220 "glNamedFramebufferParameteriv");
4221 }
4222 }
4223
4224
4225 void GLAPIENTRY
4226 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
4227 GLint *param)
4228 {
4229 GET_CURRENT_CONTEXT(ctx);
4230 struct gl_framebuffer *fb;
4231
4232 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4233 _mesa_error(ctx, GL_INVALID_OPERATION,
4234 "glNamedFramebufferParameteriv("
4235 "ARB_framebuffer_no_attachments not implemented)");
4236 return;
4237 }
4238
4239 if (framebuffer) {
4240 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4241 "glGetNamedFramebufferParameteriv");
4242 } else {
4243 fb = ctx->WinSysDrawBuffer;
4244 }
4245
4246 if (fb) {
4247 get_framebuffer_parameteriv(ctx, fb, pname, param,
4248 "glGetNamedFramebufferParameteriv");
4249 }
4250 }
4251
4252
4253 static void
4254 invalidate_framebuffer_storage(struct gl_context *ctx,
4255 struct gl_framebuffer *fb,
4256 GLsizei numAttachments,
4257 const GLenum *attachments, GLint x, GLint y,
4258 GLsizei width, GLsizei height, const char *name)
4259 {
4260 int i;
4261
4262 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4263 * Spec (2.2.2015, PDF page 522) says:
4264 * "An INVALID_VALUE error is generated if numAttachments, width, or
4265 * height is negative."
4266 */
4267 if (numAttachments < 0) {
4268 _mesa_error(ctx, GL_INVALID_VALUE,
4269 "%s(numAttachments < 0)", name);
4270 return;
4271 }
4272
4273 if (width < 0) {
4274 _mesa_error(ctx, GL_INVALID_VALUE,
4275 "%s(width < 0)", name);
4276 return;
4277 }
4278
4279 if (height < 0) {
4280 _mesa_error(ctx, GL_INVALID_VALUE,
4281 "%s(height < 0)", name);
4282 return;
4283 }
4284
4285 /* The GL_ARB_invalidate_subdata spec says:
4286 *
4287 * "If an attachment is specified that does not exist in the
4288 * framebuffer bound to <target>, it is ignored."
4289 *
4290 * It also says:
4291 *
4292 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4293 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4294 * INVALID_OPERATION is generated."
4295 *
4296 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4297 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4298 * set of retrictions).
4299 */
4300 for (i = 0; i < numAttachments; i++) {
4301 if (_mesa_is_winsys_fbo(fb)) {
4302 switch (attachments[i]) {
4303 case GL_ACCUM:
4304 case GL_AUX0:
4305 case GL_AUX1:
4306 case GL_AUX2:
4307 case GL_AUX3:
4308 /* Accumulation buffers and auxilary buffers were removed in
4309 * OpenGL 3.1, and they never existed in OpenGL ES.
4310 */
4311 if (ctx->API != API_OPENGL_COMPAT)
4312 goto invalid_enum;
4313 break;
4314 case GL_COLOR:
4315 case GL_DEPTH:
4316 case GL_STENCIL:
4317 break;
4318 case GL_BACK_LEFT:
4319 case GL_BACK_RIGHT:
4320 case GL_FRONT_LEFT:
4321 case GL_FRONT_RIGHT:
4322 if (!_mesa_is_desktop_gl(ctx))
4323 goto invalid_enum;
4324 break;
4325 default:
4326 goto invalid_enum;
4327 }
4328 } else {
4329 switch (attachments[i]) {
4330 case GL_DEPTH_ATTACHMENT:
4331 case GL_STENCIL_ATTACHMENT:
4332 break;
4333 case GL_DEPTH_STENCIL_ATTACHMENT:
4334 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4335 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4336 * extension does not make this attachment point valid on ES 2.0.
4337 */
4338 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4339 break;
4340 /* fallthrough */
4341 case GL_COLOR_ATTACHMENT0:
4342 case GL_COLOR_ATTACHMENT1:
4343 case GL_COLOR_ATTACHMENT2:
4344 case GL_COLOR_ATTACHMENT3:
4345 case GL_COLOR_ATTACHMENT4:
4346 case GL_COLOR_ATTACHMENT5:
4347 case GL_COLOR_ATTACHMENT6:
4348 case GL_COLOR_ATTACHMENT7:
4349 case GL_COLOR_ATTACHMENT8:
4350 case GL_COLOR_ATTACHMENT9:
4351 case GL_COLOR_ATTACHMENT10:
4352 case GL_COLOR_ATTACHMENT11:
4353 case GL_COLOR_ATTACHMENT12:
4354 case GL_COLOR_ATTACHMENT13:
4355 case GL_COLOR_ATTACHMENT14:
4356 case GL_COLOR_ATTACHMENT15: {
4357 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
4358 if (k >= ctx->Const.MaxColorAttachments) {
4359 _mesa_error(ctx, GL_INVALID_OPERATION,
4360 "%s(attachment >= max. color attachments)", name);
4361 return;
4362 }
4363 break;
4364 }
4365 default:
4366 goto invalid_enum;
4367 }
4368 }
4369 }
4370
4371 /* We don't actually do anything for this yet. Just return after
4372 * validating the parameters and generating the required errors.
4373 */
4374 return;
4375
4376 invalid_enum:
4377 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
4378 _mesa_enum_to_string(attachments[i]));
4379 return;
4380 }
4381
4382
4383 void GLAPIENTRY
4384 _mesa_InvalidateSubFramebuffer_no_error(GLenum target, GLsizei numAttachments,
4385 const GLenum *attachments, GLint x,
4386 GLint y, GLsizei width, GLsizei height)
4387 {
4388 /* no-op */
4389 }
4390
4391
4392 void GLAPIENTRY
4393 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4394 const GLenum *attachments, GLint x, GLint y,
4395 GLsizei width, GLsizei height)
4396 {
4397 struct gl_framebuffer *fb;
4398 GET_CURRENT_CONTEXT(ctx);
4399
4400 fb = get_framebuffer_target(ctx, target);
4401 if (!fb) {
4402 _mesa_error(ctx, GL_INVALID_ENUM,
4403 "glInvalidateSubFramebuffer(invalid target %s)",
4404 _mesa_enum_to_string(target));
4405 return;
4406 }
4407
4408 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4409 x, y, width, height,
4410 "glInvalidateSubFramebuffer");
4411 }
4412
4413
4414 void GLAPIENTRY
4415 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4416 GLsizei numAttachments,
4417 const GLenum *attachments,
4418 GLint x, GLint y,
4419 GLsizei width, GLsizei height)
4420 {
4421 struct gl_framebuffer *fb;
4422 GET_CURRENT_CONTEXT(ctx);
4423
4424 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4425 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4426 * default draw framebuffer is affected."
4427 */
4428 if (framebuffer) {
4429 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4430 "glInvalidateNamedFramebufferSubData");
4431 if (!fb)
4432 return;
4433 }
4434 else
4435 fb = ctx->WinSysDrawBuffer;
4436
4437 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4438 x, y, width, height,
4439 "glInvalidateNamedFramebufferSubData");
4440 }
4441
4442
4443 void GLAPIENTRY
4444 _mesa_InvalidateFramebuffer_no_error(GLenum target, GLsizei numAttachments,
4445 const GLenum *attachments)
4446 {
4447 /* no-op */
4448 }
4449
4450
4451 void GLAPIENTRY
4452 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4453 const GLenum *attachments)
4454 {
4455 struct gl_framebuffer *fb;
4456 GET_CURRENT_CONTEXT(ctx);
4457
4458 fb = get_framebuffer_target(ctx, target);
4459 if (!fb) {
4460 _mesa_error(ctx, GL_INVALID_ENUM,
4461 "glInvalidateFramebuffer(invalid target %s)",
4462 _mesa_enum_to_string(target));
4463 return;
4464 }
4465
4466 /* The GL_ARB_invalidate_subdata spec says:
4467 *
4468 * "The command
4469 *
4470 * void InvalidateFramebuffer(enum target,
4471 * sizei numAttachments,
4472 * const enum *attachments);
4473 *
4474 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4475 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4476 * <MAX_VIEWPORT_DIMS[1]> respectively."
4477 */
4478 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4479 0, 0,
4480 ctx->Const.MaxViewportWidth,
4481 ctx->Const.MaxViewportHeight,
4482 "glInvalidateFramebuffer");
4483 }
4484
4485
4486 void GLAPIENTRY
4487 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4488 GLsizei numAttachments,
4489 const GLenum *attachments)
4490 {
4491 struct gl_framebuffer *fb;
4492 GET_CURRENT_CONTEXT(ctx);
4493
4494 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4495 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4496 * default draw framebuffer is affected."
4497 */
4498 if (framebuffer) {
4499 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4500 "glInvalidateNamedFramebufferData");
4501 if (!fb)
4502 return;
4503 }
4504 else
4505 fb = ctx->WinSysDrawBuffer;
4506
4507 /* The GL_ARB_invalidate_subdata spec says:
4508 *
4509 * "The command
4510 *
4511 * void InvalidateFramebuffer(enum target,
4512 * sizei numAttachments,
4513 * const enum *attachments);
4514 *
4515 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4516 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4517 * <MAX_VIEWPORT_DIMS[1]> respectively."
4518 */
4519 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4520 0, 0,
4521 ctx->Const.MaxViewportWidth,
4522 ctx->Const.MaxViewportHeight,
4523 "glInvalidateNamedFramebufferData");
4524 }
4525
4526
4527 void GLAPIENTRY
4528 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4529 const GLenum *attachments)
4530 {
4531 struct gl_framebuffer *fb;
4532 GLint i;
4533
4534 GET_CURRENT_CONTEXT(ctx);
4535
4536 fb = get_framebuffer_target(ctx, target);
4537 if (!fb) {
4538 _mesa_error(ctx, GL_INVALID_ENUM,
4539 "glDiscardFramebufferEXT(target %s)",
4540 _mesa_enum_to_string(target));
4541 return;
4542 }
4543
4544 if (numAttachments < 0) {
4545 _mesa_error(ctx, GL_INVALID_VALUE,
4546 "glDiscardFramebufferEXT(numAttachments < 0)");
4547 return;
4548 }
4549
4550 for (i = 0; i < numAttachments; i++) {
4551 switch (attachments[i]) {
4552 case GL_COLOR:
4553 case GL_DEPTH:
4554 case GL_STENCIL:
4555 if (_mesa_is_user_fbo(fb))
4556 goto invalid_enum;
4557 break;
4558 case GL_COLOR_ATTACHMENT0:
4559 case GL_DEPTH_ATTACHMENT:
4560 case GL_STENCIL_ATTACHMENT:
4561 if (_mesa_is_winsys_fbo(fb))
4562 goto invalid_enum;
4563 break;
4564 default:
4565 goto invalid_enum;
4566 }
4567 }
4568
4569 if (ctx->Driver.DiscardFramebuffer)
4570 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4571
4572 return;
4573
4574 invalid_enum:
4575 _mesa_error(ctx, GL_INVALID_ENUM,
4576 "glDiscardFramebufferEXT(attachment %s)",
4577 _mesa_enum_to_string(attachments[i]));
4578 }