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