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