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