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