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