main: Refactor get_texture_for_framebuffer.
[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 zoffset,
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 = zoffset;
493 att->Layered = layered;
494 att->Complete = GL_FALSE;
495
496 _mesa_update_texture_renderbuffer(ctx, fb, att);
497 }
498
499
500 /**
501 * Bind a renderbuffer to an attachment point.
502 * The previous binding, if any, will be removed first.
503 */
504 static void
505 set_renderbuffer_attachment(struct gl_context *ctx,
506 struct gl_renderbuffer_attachment *att,
507 struct gl_renderbuffer *rb)
508 {
509 /* XXX check if re-doing same attachment, exit early */
510 remove_attachment(ctx, att);
511 att->Type = GL_RENDERBUFFER_EXT;
512 att->Texture = NULL; /* just to be safe */
513 att->Layered = GL_FALSE;
514 att->Complete = GL_FALSE;
515 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
516 }
517
518
519 /**
520 * Fallback for ctx->Driver.FramebufferRenderbuffer()
521 * Attach a renderbuffer object to a framebuffer object.
522 */
523 void
524 _mesa_FramebufferRenderbuffer_sw(struct gl_context *ctx,
525 struct gl_framebuffer *fb,
526 GLenum attachment,
527 struct gl_renderbuffer *rb)
528 {
529 struct gl_renderbuffer_attachment *att;
530
531 mtx_lock(&fb->Mutex);
532
533 att = get_attachment(ctx, fb, attachment);
534 assert(att);
535 if (rb) {
536 set_renderbuffer_attachment(ctx, att, rb);
537 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
538 /* do stencil attachment here (depth already done above) */
539 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
540 assert(att);
541 set_renderbuffer_attachment(ctx, att, rb);
542 }
543 rb->AttachedAnytime = GL_TRUE;
544 }
545 else {
546 remove_attachment(ctx, att);
547 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
548 /* detach stencil (depth was detached above) */
549 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
550 assert(att);
551 remove_attachment(ctx, att);
552 }
553 }
554
555 invalidate_framebuffer(fb);
556
557 mtx_unlock(&fb->Mutex);
558 }
559
560
561 /**
562 * Fallback for ctx->Driver.ValidateFramebuffer()
563 * Check if the renderbuffer's formats are supported by the software
564 * renderer.
565 * Drivers should probably override this.
566 */
567 void
568 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
569 {
570 gl_buffer_index buf;
571 for (buf = 0; buf < BUFFER_COUNT; buf++) {
572 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
573 if (rb) {
574 switch (rb->_BaseFormat) {
575 case GL_ALPHA:
576 case GL_LUMINANCE_ALPHA:
577 case GL_LUMINANCE:
578 case GL_INTENSITY:
579 case GL_RED:
580 case GL_RG:
581 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
582 return;
583
584 default:
585 switch (rb->Format) {
586 /* XXX This list is likely incomplete. */
587 case MESA_FORMAT_R9G9B9E5_FLOAT:
588 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
589 return;
590 default:;
591 /* render buffer format is supported by software rendering */
592 }
593 }
594 }
595 }
596 }
597
598
599 /**
600 * Return true if the framebuffer has a combined depth/stencil
601 * renderbuffer attached.
602 */
603 GLboolean
604 _mesa_has_depthstencil_combined(const struct gl_framebuffer *fb)
605 {
606 const struct gl_renderbuffer_attachment *depth =
607 &fb->Attachment[BUFFER_DEPTH];
608 const struct gl_renderbuffer_attachment *stencil =
609 &fb->Attachment[BUFFER_STENCIL];
610
611 if (depth->Type == stencil->Type) {
612 if (depth->Type == GL_RENDERBUFFER_EXT &&
613 depth->Renderbuffer == stencil->Renderbuffer)
614 return GL_TRUE;
615
616 if (depth->Type == GL_TEXTURE &&
617 depth->Texture == stencil->Texture)
618 return GL_TRUE;
619 }
620
621 return GL_FALSE;
622 }
623
624
625 /**
626 * For debug only.
627 */
628 static void
629 att_incomplete(const char *msg)
630 {
631 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
632 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
633 }
634 }
635
636
637 /**
638 * For debug only.
639 */
640 static void
641 fbo_incomplete(struct gl_context *ctx, const char *msg, int index)
642 {
643 static GLuint msg_id;
644
645 _mesa_gl_debug(ctx, &msg_id,
646 MESA_DEBUG_SOURCE_API,
647 MESA_DEBUG_TYPE_OTHER,
648 MESA_DEBUG_SEVERITY_MEDIUM,
649 "FBO incomplete: %s [%d]\n", msg, index);
650
651 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
652 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
653 }
654 }
655
656
657 /**
658 * Is the given base format a legal format for a color renderbuffer?
659 */
660 GLboolean
661 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
662 {
663 switch (baseFormat) {
664 case GL_RGB:
665 case GL_RGBA:
666 return GL_TRUE;
667 case GL_LUMINANCE:
668 case GL_LUMINANCE_ALPHA:
669 case GL_INTENSITY:
670 case GL_ALPHA:
671 return ctx->API == API_OPENGL_COMPAT &&
672 ctx->Extensions.ARB_framebuffer_object;
673 case GL_RED:
674 case GL_RG:
675 return ctx->Extensions.ARB_texture_rg;
676 default:
677 return GL_FALSE;
678 }
679 }
680
681
682 /**
683 * Is the given base format a legal format for a color renderbuffer?
684 */
685 static GLboolean
686 is_format_color_renderable(const struct gl_context *ctx, mesa_format format,
687 GLenum internalFormat)
688 {
689 const GLenum baseFormat =
690 _mesa_get_format_base_format(format);
691 GLboolean valid;
692
693 valid = _mesa_is_legal_color_format(ctx, baseFormat);
694 if (!valid || _mesa_is_desktop_gl(ctx)) {
695 return valid;
696 }
697
698 /* Reject additional cases for GLES */
699 switch (internalFormat) {
700 case GL_RGBA8_SNORM:
701 case GL_RGB32F:
702 case GL_RGB32I:
703 case GL_RGB32UI:
704 case GL_RGB16F:
705 case GL_RGB16I:
706 case GL_RGB16UI:
707 case GL_RGB8_SNORM:
708 case GL_RGB8I:
709 case GL_RGB8UI:
710 case GL_SRGB8:
711 case GL_RGB9_E5:
712 case GL_RG8_SNORM:
713 case GL_R8_SNORM:
714 return GL_FALSE;
715 default:
716 break;
717 }
718
719 if (format == MESA_FORMAT_B10G10R10A2_UNORM &&
720 internalFormat != GL_RGB10_A2) {
721 return GL_FALSE;
722 }
723
724 return GL_TRUE;
725 }
726
727
728 /**
729 * Is the given base format a legal format for a depth/stencil renderbuffer?
730 */
731 static GLboolean
732 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
733 {
734 switch (baseFormat) {
735 case GL_DEPTH_COMPONENT:
736 case GL_DEPTH_STENCIL_EXT:
737 return GL_TRUE;
738 default:
739 return GL_FALSE;
740 }
741 }
742
743
744 /**
745 * Test if an attachment point is complete and update its Complete field.
746 * \param format if GL_COLOR, this is a color attachment point,
747 * if GL_DEPTH, this is a depth component attachment point,
748 * if GL_STENCIL, this is a stencil component attachment point.
749 */
750 static void
751 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
752 struct gl_renderbuffer_attachment *att)
753 {
754 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
755
756 /* assume complete */
757 att->Complete = GL_TRUE;
758
759 /* Look for reasons why the attachment might be incomplete */
760 if (att->Type == GL_TEXTURE) {
761 const struct gl_texture_object *texObj = att->Texture;
762 struct gl_texture_image *texImage;
763 GLenum baseFormat;
764
765 if (!texObj) {
766 att_incomplete("no texobj");
767 att->Complete = GL_FALSE;
768 return;
769 }
770
771 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
772 if (!texImage) {
773 att_incomplete("no teximage");
774 att->Complete = GL_FALSE;
775 return;
776 }
777 if (texImage->Width < 1 || texImage->Height < 1) {
778 att_incomplete("teximage width/height=0");
779 att->Complete = GL_FALSE;
780 return;
781 }
782
783 switch (texObj->Target) {
784 case GL_TEXTURE_3D:
785 if (att->Zoffset >= texImage->Depth) {
786 att_incomplete("bad z offset");
787 att->Complete = GL_FALSE;
788 return;
789 }
790 break;
791 case GL_TEXTURE_1D_ARRAY:
792 if (att->Zoffset >= texImage->Height) {
793 att_incomplete("bad 1D-array layer");
794 att->Complete = GL_FALSE;
795 return;
796 }
797 break;
798 case GL_TEXTURE_2D_ARRAY:
799 if (att->Zoffset >= texImage->Depth) {
800 att_incomplete("bad 2D-array layer");
801 att->Complete = GL_FALSE;
802 return;
803 }
804 break;
805 case GL_TEXTURE_CUBE_MAP_ARRAY:
806 if (att->Zoffset >= texImage->Depth) {
807 att_incomplete("bad cube-array layer");
808 att->Complete = GL_FALSE;
809 return;
810 }
811 break;
812 }
813
814 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
815
816 if (format == GL_COLOR) {
817 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
818 att_incomplete("bad format");
819 att->Complete = GL_FALSE;
820 return;
821 }
822 if (_mesa_is_format_compressed(texImage->TexFormat)) {
823 att_incomplete("compressed internalformat");
824 att->Complete = GL_FALSE;
825 return;
826 }
827
828 /* OES_texture_float allows creation and use of floating point
829 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
830 * these textures to be used as a render target, this is done via
831 * GL_EXT_color_buffer(_half)_float with set of new sized types.
832 */
833 if (_mesa_is_gles(ctx) && (texImage->TexObject->_IsFloat ||
834 texImage->TexObject->_IsHalfFloat)) {
835 att_incomplete("bad internal format");
836 att->Complete = GL_FALSE;
837 return;
838 }
839 }
840 else if (format == GL_DEPTH) {
841 if (baseFormat == GL_DEPTH_COMPONENT) {
842 /* OK */
843 }
844 else if (ctx->Extensions.ARB_depth_texture &&
845 baseFormat == GL_DEPTH_STENCIL) {
846 /* OK */
847 }
848 else {
849 att->Complete = GL_FALSE;
850 att_incomplete("bad depth format");
851 return;
852 }
853 }
854 else {
855 assert(format == GL_STENCIL);
856 if (ctx->Extensions.ARB_depth_texture &&
857 baseFormat == GL_DEPTH_STENCIL) {
858 /* OK */
859 } else if (ctx->Extensions.ARB_texture_stencil8 &&
860 baseFormat == GL_STENCIL_INDEX) {
861 /* OK */
862 } else {
863 /* no such thing as stencil-only textures */
864 att_incomplete("illegal stencil texture");
865 att->Complete = GL_FALSE;
866 return;
867 }
868 }
869 }
870 else if (att->Type == GL_RENDERBUFFER_EXT) {
871 const GLenum baseFormat =
872 _mesa_get_format_base_format(att->Renderbuffer->Format);
873
874 assert(att->Renderbuffer);
875 if (!att->Renderbuffer->InternalFormat ||
876 att->Renderbuffer->Width < 1 ||
877 att->Renderbuffer->Height < 1) {
878 att_incomplete("0x0 renderbuffer");
879 att->Complete = GL_FALSE;
880 return;
881 }
882 if (format == GL_COLOR) {
883 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
884 att_incomplete("bad renderbuffer color format");
885 att->Complete = GL_FALSE;
886 return;
887 }
888 }
889 else if (format == GL_DEPTH) {
890 if (baseFormat == GL_DEPTH_COMPONENT) {
891 /* OK */
892 }
893 else if (baseFormat == GL_DEPTH_STENCIL) {
894 /* OK */
895 }
896 else {
897 att_incomplete("bad renderbuffer depth format");
898 att->Complete = GL_FALSE;
899 return;
900 }
901 }
902 else {
903 assert(format == GL_STENCIL);
904 if (baseFormat == GL_STENCIL_INDEX ||
905 baseFormat == GL_DEPTH_STENCIL) {
906 /* OK */
907 }
908 else {
909 att->Complete = GL_FALSE;
910 att_incomplete("bad renderbuffer stencil format");
911 return;
912 }
913 }
914 }
915 else {
916 assert(att->Type == GL_NONE);
917 /* complete */
918 return;
919 }
920 }
921
922
923 /**
924 * Test if the given framebuffer object is complete and update its
925 * Status field with the results.
926 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
927 * driver to make hardware-specific validation/completeness checks.
928 * Also update the framebuffer's Width and Height fields if the
929 * framebuffer is complete.
930 */
931 void
932 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
933 struct gl_framebuffer *fb)
934 {
935 GLuint numImages;
936 GLenum intFormat = GL_NONE; /* color buffers' internal format */
937 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
938 GLint numSamples = -1;
939 GLint fixedSampleLocations = -1;
940 GLint i;
941 GLuint j;
942 /* Covers max_layer_count, is_layered, and layer_tex_target */
943 bool layer_info_valid = false;
944 GLuint max_layer_count = 0, att_layer_count;
945 bool is_layered = false;
946 GLenum layer_tex_target = 0;
947 bool has_depth_attachment = false;
948 bool has_stencil_attachment = false;
949
950 assert(_mesa_is_user_fbo(fb));
951
952 /* we're changing framebuffer fields here */
953 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
954
955 numImages = 0;
956 fb->Width = 0;
957 fb->Height = 0;
958 fb->_AllColorBuffersFixedPoint = GL_TRUE;
959 fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
960
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 fb->MaxNumLayers = max_layer_count;
1161
1162 if (numImages == 0) {
1163 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1164 fbo_incomplete(ctx, "no attachments", -1);
1165 return;
1166 }
1167
1168 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
1169 /* Check that all DrawBuffers are present */
1170 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
1171 if (fb->ColorDrawBuffer[j] != GL_NONE) {
1172 const struct gl_renderbuffer_attachment *att
1173 = get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
1174 assert(att);
1175 if (att->Type == GL_NONE) {
1176 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
1177 fbo_incomplete(ctx, "missing drawbuffer", j);
1178 return;
1179 }
1180 }
1181 }
1182
1183 /* Check that the ReadBuffer is present */
1184 if (fb->ColorReadBuffer != GL_NONE) {
1185 const struct gl_renderbuffer_attachment *att
1186 = get_attachment(ctx, fb, fb->ColorReadBuffer);
1187 assert(att);
1188 if (att->Type == GL_NONE) {
1189 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
1190 fbo_incomplete(ctx, "missing readbuffer", -1);
1191 return;
1192 }
1193 }
1194 }
1195
1196 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1197 *
1198 * "Depth and stencil attachments, if present, are the same image."
1199 *
1200 * This restriction is not present in the OpenGL ES2 spec.
1201 */
1202 if (_mesa_is_gles3(ctx) &&
1203 has_stencil_attachment && has_depth_attachment &&
1204 !_mesa_has_depthstencil_combined(fb)) {
1205 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1206 fbo_incomplete(ctx, "Depth and stencil attachments must be the same image", -1);
1207 return;
1208 }
1209
1210 /* Provisionally set status = COMPLETE ... */
1211 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
1212
1213 /* ... but the driver may say the FB is incomplete.
1214 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1215 * if anything.
1216 */
1217 if (ctx->Driver.ValidateFramebuffer) {
1218 ctx->Driver.ValidateFramebuffer(ctx, fb);
1219 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1220 fbo_incomplete(ctx, "driver marked FBO as incomplete", -1);
1221 }
1222 }
1223
1224 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
1225 /*
1226 * Note that if ARB_framebuffer_object is supported and the attached
1227 * renderbuffers/textures are different sizes, the framebuffer
1228 * width/height will be set to the smallest width/height.
1229 */
1230 fb->Width = minWidth;
1231 fb->Height = minHeight;
1232
1233 /* finally, update the visual info for the framebuffer */
1234 _mesa_update_framebuffer_visual(ctx, fb);
1235 }
1236 }
1237
1238
1239 GLboolean GLAPIENTRY
1240 _mesa_IsRenderbuffer(GLuint renderbuffer)
1241 {
1242 GET_CURRENT_CONTEXT(ctx);
1243 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1244 if (renderbuffer) {
1245 struct gl_renderbuffer *rb =
1246 _mesa_lookup_renderbuffer(ctx, renderbuffer);
1247 if (rb != NULL && rb != &DummyRenderbuffer)
1248 return GL_TRUE;
1249 }
1250 return GL_FALSE;
1251 }
1252
1253
1254 static struct gl_renderbuffer *
1255 allocate_renderbuffer(struct gl_context *ctx, GLuint renderbuffer,
1256 const char *func)
1257 {
1258 struct gl_renderbuffer *newRb;
1259
1260 /* create new renderbuffer object */
1261 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
1262 if (!newRb) {
1263 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1264 return NULL;
1265 }
1266 assert(newRb->AllocStorage);
1267 mtx_lock(&ctx->Shared->Mutex);
1268 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1269 newRb->RefCount = 1; /* referenced by hash table */
1270 mtx_unlock(&ctx->Shared->Mutex);
1271
1272 return newRb;
1273 }
1274
1275
1276 static void
1277 bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
1278 {
1279 struct gl_renderbuffer *newRb;
1280 GET_CURRENT_CONTEXT(ctx);
1281
1282 if (target != GL_RENDERBUFFER_EXT) {
1283 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
1284 return;
1285 }
1286
1287 /* No need to flush here since the render buffer binding has no
1288 * effect on rendering state.
1289 */
1290
1291 if (renderbuffer) {
1292 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1293 if (newRb == &DummyRenderbuffer) {
1294 /* ID was reserved, but no real renderbuffer object made yet */
1295 newRb = NULL;
1296 }
1297 else if (!newRb && !allow_user_names) {
1298 /* All RB IDs must be Gen'd */
1299 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1300 return;
1301 }
1302
1303 if (!newRb) {
1304 newRb = allocate_renderbuffer(ctx, renderbuffer, "glBindRenderbufferEXT");
1305 }
1306 }
1307 else {
1308 newRb = NULL;
1309 }
1310
1311 assert(newRb != &DummyRenderbuffer);
1312
1313 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1314 }
1315
1316 void GLAPIENTRY
1317 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1318 {
1319 GET_CURRENT_CONTEXT(ctx);
1320
1321 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1322 * entry point, but they allow the use of user-generated names.
1323 */
1324 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1325 }
1326
1327 void GLAPIENTRY
1328 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1329 {
1330 /* This function should not be in the dispatch table for core profile /
1331 * OpenGL 3.1, so execution should never get here in those cases -- no
1332 * need for an explicit test.
1333 */
1334 bind_renderbuffer(target, renderbuffer, true);
1335 }
1336
1337
1338 /**
1339 * Remove the specified renderbuffer or texture from any attachment point in
1340 * the framebuffer.
1341 *
1342 * \returns
1343 * \c true if the renderbuffer was detached from an attachment point. \c
1344 * false otherwise.
1345 */
1346 bool
1347 _mesa_detach_renderbuffer(struct gl_context *ctx,
1348 struct gl_framebuffer *fb,
1349 const void *att)
1350 {
1351 unsigned i;
1352 bool progress = false;
1353
1354 for (i = 0; i < BUFFER_COUNT; i++) {
1355 if (fb->Attachment[i].Texture == att
1356 || fb->Attachment[i].Renderbuffer == att) {
1357 remove_attachment(ctx, &fb->Attachment[i]);
1358 progress = true;
1359 }
1360 }
1361
1362 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1363 * Completeness," of the OpenGL 3.1 spec says:
1364 *
1365 * "Performing any of the following actions may change whether the
1366 * framebuffer is considered complete or incomplete:
1367 *
1368 * ...
1369 *
1370 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1371 * containing an image that is attached to a framebuffer object
1372 * that is bound to the framebuffer."
1373 */
1374 if (progress)
1375 invalidate_framebuffer(fb);
1376
1377 return progress;
1378 }
1379
1380
1381 void GLAPIENTRY
1382 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1383 {
1384 GLint i;
1385 GET_CURRENT_CONTEXT(ctx);
1386
1387 if (n < 0) {
1388 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1389 return;
1390 }
1391
1392 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1393
1394 for (i = 0; i < n; i++) {
1395 if (renderbuffers[i] > 0) {
1396 struct gl_renderbuffer *rb;
1397 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1398 if (rb) {
1399 /* check if deleting currently bound renderbuffer object */
1400 if (rb == ctx->CurrentRenderbuffer) {
1401 /* bind default */
1402 assert(rb->RefCount >= 2);
1403 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1404 }
1405
1406 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1407 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1408 * of the OpenGL 3.1 spec says:
1409 *
1410 * "If a renderbuffer object is deleted while its image is
1411 * attached to one or more attachment points in the currently
1412 * bound framebuffer, then it is as if FramebufferRenderbuffer
1413 * had been called, with a renderbuffer of 0, for each
1414 * attachment point to which this image was attached in the
1415 * currently bound framebuffer. In other words, this
1416 * renderbuffer image is first detached from all attachment
1417 * points in the currently bound framebuffer. Note that the
1418 * renderbuffer image is specifically not detached from any
1419 * non-bound framebuffers. Detaching the image from any
1420 * non-bound framebuffers is the responsibility of the
1421 * application.
1422 */
1423 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1424 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1425 }
1426 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1427 && ctx->ReadBuffer != ctx->DrawBuffer) {
1428 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1429 }
1430
1431 /* Remove from hash table immediately, to free the ID.
1432 * But the object will not be freed until it's no longer
1433 * referenced anywhere else.
1434 */
1435 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1436
1437 if (rb != &DummyRenderbuffer) {
1438 /* no longer referenced by hash table */
1439 _mesa_reference_renderbuffer(&rb, NULL);
1440 }
1441 }
1442 }
1443 }
1444 }
1445
1446 static void
1447 create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1448 bool dsa)
1449 {
1450 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
1451 GLuint first;
1452 GLint i;
1453
1454 if (n < 0) {
1455 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
1456 return;
1457 }
1458
1459 if (!renderbuffers)
1460 return;
1461
1462 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1463
1464 for (i = 0; i < n; i++) {
1465 GLuint name = first + i;
1466 renderbuffers[i] = name;
1467
1468 if (dsa) {
1469 allocate_renderbuffer(ctx, name, func);
1470 } else {
1471 /* insert a dummy renderbuffer into the hash table */
1472 mtx_lock(&ctx->Shared->Mutex);
1473 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1474 mtx_unlock(&ctx->Shared->Mutex);
1475 }
1476 }
1477 }
1478
1479
1480 void GLAPIENTRY
1481 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1482 {
1483 GET_CURRENT_CONTEXT(ctx);
1484 create_render_buffers(ctx, n, renderbuffers, false);
1485 }
1486
1487
1488 void GLAPIENTRY
1489 _mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1490 {
1491 GET_CURRENT_CONTEXT(ctx);
1492 create_render_buffers(ctx, n, renderbuffers, true);
1493 }
1494
1495
1496 /**
1497 * Given an internal format token for a render buffer, return the
1498 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1499 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1500 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1501 *
1502 * This is similar to _mesa_base_tex_format() but the set of valid
1503 * internal formats is different.
1504 *
1505 * Note that even if a format is determined to be legal here, validation
1506 * of the FBO may fail if the format is not supported by the driver/GPU.
1507 *
1508 * \param internalFormat as passed to glRenderbufferStorage()
1509 * \return the base internal format, or 0 if internalFormat is illegal
1510 */
1511 GLenum
1512 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1513 {
1514 /*
1515 * Notes: some formats such as alpha, luminance, etc. were added
1516 * with GL_ARB_framebuffer_object.
1517 */
1518 switch (internalFormat) {
1519 case GL_ALPHA:
1520 case GL_ALPHA4:
1521 case GL_ALPHA8:
1522 case GL_ALPHA12:
1523 case GL_ALPHA16:
1524 return (ctx->API == API_OPENGL_COMPAT &&
1525 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
1526 case GL_LUMINANCE:
1527 case GL_LUMINANCE4:
1528 case GL_LUMINANCE8:
1529 case GL_LUMINANCE12:
1530 case GL_LUMINANCE16:
1531 return (ctx->API == API_OPENGL_COMPAT &&
1532 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
1533 case GL_LUMINANCE_ALPHA:
1534 case GL_LUMINANCE4_ALPHA4:
1535 case GL_LUMINANCE6_ALPHA2:
1536 case GL_LUMINANCE8_ALPHA8:
1537 case GL_LUMINANCE12_ALPHA4:
1538 case GL_LUMINANCE12_ALPHA12:
1539 case GL_LUMINANCE16_ALPHA16:
1540 return (ctx->API == API_OPENGL_COMPAT &&
1541 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
1542 case GL_INTENSITY:
1543 case GL_INTENSITY4:
1544 case GL_INTENSITY8:
1545 case GL_INTENSITY12:
1546 case GL_INTENSITY16:
1547 return (ctx->API == API_OPENGL_COMPAT &&
1548 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
1549 case GL_RGB8:
1550 return GL_RGB;
1551 case GL_RGB:
1552 case GL_R3_G3_B2:
1553 case GL_RGB4:
1554 case GL_RGB5:
1555 case GL_RGB10:
1556 case GL_RGB12:
1557 case GL_RGB16:
1558 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1559 case GL_SRGB8_EXT:
1560 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1561 case GL_RGBA4:
1562 case GL_RGB5_A1:
1563 case GL_RGBA8:
1564 return GL_RGBA;
1565 case GL_RGBA:
1566 case GL_RGBA2:
1567 case GL_RGBA12:
1568 case GL_RGBA16:
1569 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1570 case GL_RGB10_A2:
1571 case GL_SRGB8_ALPHA8_EXT:
1572 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1573 case GL_STENCIL_INDEX:
1574 case GL_STENCIL_INDEX1_EXT:
1575 case GL_STENCIL_INDEX4_EXT:
1576 case GL_STENCIL_INDEX16_EXT:
1577 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1578 * OpenGL ES, but Mesa does not currently support them.
1579 */
1580 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1581 case GL_STENCIL_INDEX8_EXT:
1582 return GL_STENCIL_INDEX;
1583 case GL_DEPTH_COMPONENT:
1584 case GL_DEPTH_COMPONENT32:
1585 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1586 case GL_DEPTH_COMPONENT16:
1587 case GL_DEPTH_COMPONENT24:
1588 return GL_DEPTH_COMPONENT;
1589 case GL_DEPTH_STENCIL:
1590 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1591 case GL_DEPTH24_STENCIL8:
1592 return GL_DEPTH_STENCIL;
1593 case GL_DEPTH_COMPONENT32F:
1594 return ctx->Version >= 30
1595 || (ctx->API == API_OPENGL_COMPAT &&
1596 ctx->Extensions.ARB_depth_buffer_float)
1597 ? GL_DEPTH_COMPONENT : 0;
1598 case GL_DEPTH32F_STENCIL8:
1599 return ctx->Version >= 30
1600 || (ctx->API == API_OPENGL_COMPAT &&
1601 ctx->Extensions.ARB_depth_buffer_float)
1602 ? GL_DEPTH_STENCIL : 0;
1603 case GL_RED:
1604 case GL_R16:
1605 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1606 ? GL_RED : 0;
1607 case GL_R8:
1608 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1609 ? GL_RED : 0;
1610 case GL_RG:
1611 case GL_RG16:
1612 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1613 ? GL_RG : 0;
1614 case GL_RG8:
1615 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1616 ? GL_RG : 0;
1617 /* signed normalized texture formats */
1618 case GL_RED_SNORM:
1619 case GL_R8_SNORM:
1620 case GL_R16_SNORM:
1621 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1622 ? GL_RED : 0;
1623 case GL_RG_SNORM:
1624 case GL_RG8_SNORM:
1625 case GL_RG16_SNORM:
1626 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1627 ? GL_RG : 0;
1628 case GL_RGB_SNORM:
1629 case GL_RGB8_SNORM:
1630 case GL_RGB16_SNORM:
1631 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1632 ? GL_RGB : 0;
1633 case GL_RGBA_SNORM:
1634 case GL_RGBA8_SNORM:
1635 case GL_RGBA16_SNORM:
1636 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1637 ? GL_RGBA : 0;
1638 case GL_ALPHA_SNORM:
1639 case GL_ALPHA8_SNORM:
1640 case GL_ALPHA16_SNORM:
1641 return ctx->API == API_OPENGL_COMPAT &&
1642 ctx->Extensions.EXT_texture_snorm &&
1643 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1644 case GL_LUMINANCE_SNORM:
1645 case GL_LUMINANCE8_SNORM:
1646 case GL_LUMINANCE16_SNORM:
1647 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1648 ? GL_LUMINANCE : 0;
1649 case GL_LUMINANCE_ALPHA_SNORM:
1650 case GL_LUMINANCE8_ALPHA8_SNORM:
1651 case GL_LUMINANCE16_ALPHA16_SNORM:
1652 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1653 ? GL_LUMINANCE_ALPHA : 0;
1654 case GL_INTENSITY_SNORM:
1655 case GL_INTENSITY8_SNORM:
1656 case GL_INTENSITY16_SNORM:
1657 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1658 ? GL_INTENSITY : 0;
1659
1660 case GL_R16F:
1661 case GL_R32F:
1662 return ((_mesa_is_desktop_gl(ctx) &&
1663 ctx->Extensions.ARB_texture_rg &&
1664 ctx->Extensions.ARB_texture_float) ||
1665 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1666 ? GL_RED : 0;
1667 case GL_RG16F:
1668 case GL_RG32F:
1669 return ((_mesa_is_desktop_gl(ctx) &&
1670 ctx->Extensions.ARB_texture_rg &&
1671 ctx->Extensions.ARB_texture_float) ||
1672 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1673 ? GL_RG : 0;
1674 case GL_RGB16F:
1675 case GL_RGB32F:
1676 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1677 ? GL_RGB : 0;
1678 case GL_RGBA16F:
1679 case GL_RGBA32F:
1680 return ((_mesa_is_desktop_gl(ctx) &&
1681 ctx->Extensions.ARB_texture_float) ||
1682 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1683 ? GL_RGBA : 0;
1684 case GL_ALPHA16F_ARB:
1685 case GL_ALPHA32F_ARB:
1686 return ctx->API == API_OPENGL_COMPAT &&
1687 ctx->Extensions.ARB_texture_float &&
1688 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1689 case GL_LUMINANCE16F_ARB:
1690 case GL_LUMINANCE32F_ARB:
1691 return ctx->API == API_OPENGL_COMPAT &&
1692 ctx->Extensions.ARB_texture_float &&
1693 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1694 case GL_LUMINANCE_ALPHA16F_ARB:
1695 case GL_LUMINANCE_ALPHA32F_ARB:
1696 return ctx->API == API_OPENGL_COMPAT &&
1697 ctx->Extensions.ARB_texture_float &&
1698 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1699 case GL_INTENSITY16F_ARB:
1700 case GL_INTENSITY32F_ARB:
1701 return ctx->API == API_OPENGL_COMPAT &&
1702 ctx->Extensions.ARB_texture_float &&
1703 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1704 case GL_R11F_G11F_B10F:
1705 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1706 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1707 ? GL_RGB : 0;
1708
1709 case GL_RGBA8UI_EXT:
1710 case GL_RGBA16UI_EXT:
1711 case GL_RGBA32UI_EXT:
1712 case GL_RGBA8I_EXT:
1713 case GL_RGBA16I_EXT:
1714 case GL_RGBA32I_EXT:
1715 return ctx->Version >= 30
1716 || (_mesa_is_desktop_gl(ctx) &&
1717 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1718
1719 case GL_RGB8UI_EXT:
1720 case GL_RGB16UI_EXT:
1721 case GL_RGB32UI_EXT:
1722 case GL_RGB8I_EXT:
1723 case GL_RGB16I_EXT:
1724 case GL_RGB32I_EXT:
1725 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1726 ? GL_RGB : 0;
1727 case GL_R8UI:
1728 case GL_R8I:
1729 case GL_R16UI:
1730 case GL_R16I:
1731 case GL_R32UI:
1732 case GL_R32I:
1733 return ctx->Version >= 30
1734 || (_mesa_is_desktop_gl(ctx) &&
1735 ctx->Extensions.ARB_texture_rg &&
1736 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1737
1738 case GL_RG8UI:
1739 case GL_RG8I:
1740 case GL_RG16UI:
1741 case GL_RG16I:
1742 case GL_RG32UI:
1743 case GL_RG32I:
1744 return ctx->Version >= 30
1745 || (_mesa_is_desktop_gl(ctx) &&
1746 ctx->Extensions.ARB_texture_rg &&
1747 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1748
1749 case GL_INTENSITY8I_EXT:
1750 case GL_INTENSITY8UI_EXT:
1751 case GL_INTENSITY16I_EXT:
1752 case GL_INTENSITY16UI_EXT:
1753 case GL_INTENSITY32I_EXT:
1754 case GL_INTENSITY32UI_EXT:
1755 return ctx->API == API_OPENGL_COMPAT &&
1756 ctx->Extensions.EXT_texture_integer &&
1757 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1758
1759 case GL_LUMINANCE8I_EXT:
1760 case GL_LUMINANCE8UI_EXT:
1761 case GL_LUMINANCE16I_EXT:
1762 case GL_LUMINANCE16UI_EXT:
1763 case GL_LUMINANCE32I_EXT:
1764 case GL_LUMINANCE32UI_EXT:
1765 return ctx->API == API_OPENGL_COMPAT &&
1766 ctx->Extensions.EXT_texture_integer &&
1767 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1768
1769 case GL_LUMINANCE_ALPHA8I_EXT:
1770 case GL_LUMINANCE_ALPHA8UI_EXT:
1771 case GL_LUMINANCE_ALPHA16I_EXT:
1772 case GL_LUMINANCE_ALPHA16UI_EXT:
1773 case GL_LUMINANCE_ALPHA32I_EXT:
1774 case GL_LUMINANCE_ALPHA32UI_EXT:
1775 return ctx->API == API_OPENGL_COMPAT &&
1776 ctx->Extensions.EXT_texture_integer &&
1777 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1778
1779 case GL_ALPHA8I_EXT:
1780 case GL_ALPHA8UI_EXT:
1781 case GL_ALPHA16I_EXT:
1782 case GL_ALPHA16UI_EXT:
1783 case GL_ALPHA32I_EXT:
1784 case GL_ALPHA32UI_EXT:
1785 return ctx->API == API_OPENGL_COMPAT &&
1786 ctx->Extensions.EXT_texture_integer &&
1787 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1788
1789 case GL_RGB10_A2UI:
1790 return (_mesa_is_desktop_gl(ctx) &&
1791 ctx->Extensions.ARB_texture_rgb10_a2ui)
1792 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1793
1794 case GL_RGB565:
1795 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1796 ? GL_RGB : 0;
1797 default:
1798 return 0;
1799 }
1800 }
1801
1802
1803 /**
1804 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1805 */
1806 static void
1807 invalidate_rb(GLuint key, void *data, void *userData)
1808 {
1809 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1810 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1811
1812 /* If this is a user-created FBO */
1813 if (_mesa_is_user_fbo(fb)) {
1814 GLuint i;
1815 for (i = 0; i < BUFFER_COUNT; i++) {
1816 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1817 if (att->Type == GL_RENDERBUFFER &&
1818 att->Renderbuffer == rb) {
1819 /* Mark fb status as indeterminate to force re-validation */
1820 fb->_Status = 0;
1821 return;
1822 }
1823 }
1824 }
1825 }
1826
1827
1828 /** sentinal value, see below */
1829 #define NO_SAMPLES 1000
1830
1831
1832 /**
1833 * Helper function used by renderbuffer_storage_direct() and
1834 * renderbuffer_storage_target().
1835 * samples will be NO_SAMPLES if called by a non-multisample function.
1836 */
1837 static void
1838 renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
1839 GLenum internalFormat, GLsizei width,
1840 GLsizei height, GLsizei samples, const char *func)
1841 {
1842 GLenum baseFormat;
1843 GLenum sample_count_error;
1844
1845 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1846 if (baseFormat == 0) {
1847 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1848 func, _mesa_lookup_enum_by_nr(internalFormat));
1849 return;
1850 }
1851
1852 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1853 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
1854 width);
1855 return;
1856 }
1857
1858 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1859 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
1860 height);
1861 return;
1862 }
1863
1864 if (samples == NO_SAMPLES) {
1865 /* NumSamples == 0 indicates non-multisampling */
1866 samples = 0;
1867 }
1868 else {
1869 /* check the sample count;
1870 * note: driver may choose to use more samples than what's requested
1871 */
1872 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
1873 internalFormat, samples);
1874 if (sample_count_error != GL_NO_ERROR) {
1875 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
1876 return;
1877 }
1878 }
1879
1880 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1881
1882 if (rb->InternalFormat == internalFormat &&
1883 rb->Width == (GLuint) width &&
1884 rb->Height == (GLuint) height &&
1885 rb->NumSamples == samples) {
1886 /* no change in allocation needed */
1887 return;
1888 }
1889
1890 /* These MUST get set by the AllocStorage func */
1891 rb->Format = MESA_FORMAT_NONE;
1892 rb->NumSamples = samples;
1893
1894 /* Now allocate the storage */
1895 assert(rb->AllocStorage);
1896 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1897 /* No error - check/set fields now */
1898 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1899 assert(rb->Width == (GLuint) width);
1900 assert(rb->Height == (GLuint) height);
1901 rb->InternalFormat = internalFormat;
1902 rb->_BaseFormat = baseFormat;
1903 assert(rb->_BaseFormat != 0);
1904 }
1905 else {
1906 /* Probably ran out of memory - clear the fields */
1907 rb->Width = 0;
1908 rb->Height = 0;
1909 rb->Format = MESA_FORMAT_NONE;
1910 rb->InternalFormat = GL_NONE;
1911 rb->_BaseFormat = GL_NONE;
1912 rb->NumSamples = 0;
1913 }
1914
1915 /* Invalidate the framebuffers the renderbuffer is attached in. */
1916 if (rb->AttachedAnytime) {
1917 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1918 }
1919 }
1920
1921 /**
1922 * Helper function used by _mesa_NamedRenderbufferStorage*().
1923 * samples will be NO_SAMPLES if called by a non-multisample function.
1924 */
1925 static void
1926 renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
1927 GLsizei width, GLsizei height, GLsizei samples,
1928 const char *func)
1929 {
1930 GET_CURRENT_CONTEXT(ctx);
1931
1932 if (MESA_VERBOSE & VERBOSE_API) {
1933 if (samples == NO_SAMPLES)
1934 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
1935 func, renderbuffer,
1936 _mesa_lookup_enum_by_nr(internalFormat),
1937 width, height);
1938 else
1939 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
1940 func, renderbuffer,
1941 _mesa_lookup_enum_by_nr(internalFormat),
1942 width, height, samples);
1943 }
1944
1945 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1946 if (!rb || rb == &DummyRenderbuffer) {
1947 /* ID was reserved, but no real renderbuffer object made yet */
1948 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
1949 func, renderbuffer);
1950 return;
1951 }
1952
1953 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
1954 }
1955
1956 /**
1957 * Helper function used by _mesa_RenderbufferStorage() and
1958 * _mesa_RenderbufferStorageMultisample().
1959 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1960 */
1961 static void
1962 renderbuffer_storage_target(GLenum target, GLenum internalFormat,
1963 GLsizei width, GLsizei height, GLsizei samples,
1964 const char *func)
1965 {
1966 GET_CURRENT_CONTEXT(ctx);
1967
1968 if (MESA_VERBOSE & VERBOSE_API) {
1969 if (samples == NO_SAMPLES)
1970 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1971 func,
1972 _mesa_lookup_enum_by_nr(target),
1973 _mesa_lookup_enum_by_nr(internalFormat),
1974 width, height);
1975 else
1976 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1977 func,
1978 _mesa_lookup_enum_by_nr(target),
1979 _mesa_lookup_enum_by_nr(internalFormat),
1980 width, height, samples);
1981 }
1982
1983 if (target != GL_RENDERBUFFER_EXT) {
1984 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1985 return;
1986 }
1987
1988 if (!ctx->CurrentRenderbuffer) {
1989 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
1990 func);
1991 return;
1992 }
1993
1994 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
1995 height, samples, func);
1996 }
1997
1998
1999 void GLAPIENTRY
2000 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
2001 {
2002 struct gl_renderbuffer *rb;
2003 GET_CURRENT_CONTEXT(ctx);
2004
2005 if (!ctx->Extensions.OES_EGL_image) {
2006 _mesa_error(ctx, GL_INVALID_OPERATION,
2007 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2008 return;
2009 }
2010
2011 if (target != GL_RENDERBUFFER) {
2012 _mesa_error(ctx, GL_INVALID_ENUM,
2013 "EGLImageTargetRenderbufferStorageOES");
2014 return;
2015 }
2016
2017 rb = ctx->CurrentRenderbuffer;
2018 if (!rb) {
2019 _mesa_error(ctx, GL_INVALID_OPERATION,
2020 "EGLImageTargetRenderbufferStorageOES");
2021 return;
2022 }
2023
2024 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2025
2026 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2027 }
2028
2029
2030 /**
2031 * Helper function for _mesa_GetRenderbufferParameteriv() and
2032 * _mesa_GetFramebufferAttachmentParameteriv()
2033 * We have to be careful to respect the base format. For example, if a
2034 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2035 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2036 * we need to return zero.
2037 */
2038 static GLint
2039 get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
2040 {
2041 if (_mesa_base_format_has_channel(baseFormat, pname))
2042 return _mesa_get_format_bits(format, pname);
2043 else
2044 return 0;
2045 }
2046
2047
2048
2049 void GLAPIENTRY
2050 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
2051 GLsizei width, GLsizei height)
2052 {
2053 /* GL_ARB_fbo says calling this function is equivalent to calling
2054 * glRenderbufferStorageMultisample() with samples=0. We pass in
2055 * a token value here just for error reporting purposes.
2056 */
2057 renderbuffer_storage_target(target, internalFormat, width, height,
2058 NO_SAMPLES, "glRenderbufferStorage");
2059 }
2060
2061
2062 void GLAPIENTRY
2063 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
2064 GLenum internalFormat,
2065 GLsizei width, GLsizei height)
2066 {
2067 renderbuffer_storage_target(target, internalFormat, width, height,
2068 samples, "glRenderbufferStorageMultisample");
2069 }
2070
2071
2072 /**
2073 * OpenGL ES version of glRenderBufferStorage.
2074 */
2075 void GLAPIENTRY
2076 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
2077 GLsizei width, GLsizei height)
2078 {
2079 switch (internalFormat) {
2080 case GL_RGB565:
2081 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2082 /* choose a closest format */
2083 internalFormat = GL_RGB5;
2084 break;
2085 default:
2086 break;
2087 }
2088
2089 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2090 "glRenderbufferStorageEXT");
2091 }
2092
2093 void GLAPIENTRY
2094 _mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2095 GLsizei width, GLsizei height)
2096 {
2097 /* GL_ARB_fbo says calling this function is equivalent to calling
2098 * glRenderbufferStorageMultisample() with samples=0. We pass in
2099 * a token value here just for error reporting purposes.
2100 */
2101 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2102 NO_SAMPLES, "glNamedRenderbufferStorage");
2103 }
2104
2105 void GLAPIENTRY
2106 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2107 GLenum internalformat,
2108 GLsizei width, GLsizei height)
2109 {
2110 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2111 samples,
2112 "glNamedRenderbufferStorageMultisample");
2113 }
2114
2115
2116 static void
2117 get_render_buffer_parameteriv(struct gl_context *ctx,
2118 struct gl_renderbuffer *rb, GLenum pname,
2119 GLint *params, const char *func)
2120 {
2121 /* No need to flush here since we're just quering state which is
2122 * not effected by rendering.
2123 */
2124
2125 switch (pname) {
2126 case GL_RENDERBUFFER_WIDTH_EXT:
2127 *params = rb->Width;
2128 return;
2129 case GL_RENDERBUFFER_HEIGHT_EXT:
2130 *params = rb->Height;
2131 return;
2132 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
2133 *params = rb->InternalFormat;
2134 return;
2135 case GL_RENDERBUFFER_RED_SIZE_EXT:
2136 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
2137 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
2138 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
2139 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
2140 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
2141 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
2142 break;
2143 case GL_RENDERBUFFER_SAMPLES:
2144 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2145 || _mesa_is_gles3(ctx)) {
2146 *params = rb->NumSamples;
2147 break;
2148 }
2149 /* fallthrough */
2150 default:
2151 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
2152 _mesa_lookup_enum_by_nr(pname));
2153 return;
2154 }
2155 }
2156
2157
2158 void GLAPIENTRY
2159 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2160 {
2161 GET_CURRENT_CONTEXT(ctx);
2162
2163 if (target != GL_RENDERBUFFER_EXT) {
2164 _mesa_error(ctx, GL_INVALID_ENUM,
2165 "glGetRenderbufferParameterivEXT(target)");
2166 return;
2167 }
2168
2169 if (!ctx->CurrentRenderbuffer) {
2170 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2171 "(no renderbuffer bound)");
2172 return;
2173 }
2174
2175 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2176 params, "glGetRenderbufferParameteriv");
2177 }
2178
2179
2180 void GLAPIENTRY
2181 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2182 GLint *params)
2183 {
2184 GET_CURRENT_CONTEXT(ctx);
2185
2186 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2187 if (!rb || rb == &DummyRenderbuffer) {
2188 /* ID was reserved, but no real renderbuffer object made yet */
2189 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2190 "(invalid renderbuffer %i)", renderbuffer);
2191 return;
2192 }
2193
2194 get_render_buffer_parameteriv(ctx, rb, pname, params,
2195 "glGetNamedRenderbufferParameteriv");
2196 }
2197
2198
2199 GLboolean GLAPIENTRY
2200 _mesa_IsFramebuffer(GLuint framebuffer)
2201 {
2202 GET_CURRENT_CONTEXT(ctx);
2203 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2204 if (framebuffer) {
2205 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
2206 if (rb != NULL && rb != &DummyFramebuffer)
2207 return GL_TRUE;
2208 }
2209 return GL_FALSE;
2210 }
2211
2212
2213 /**
2214 * Check if any of the attachments of the given framebuffer are textures
2215 * (render to texture). Call ctx->Driver.RenderTexture() for such
2216 * attachments.
2217 */
2218 static void
2219 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2220 {
2221 GLuint i;
2222 assert(ctx->Driver.RenderTexture);
2223
2224 if (_mesa_is_winsys_fbo(fb))
2225 return; /* can't render to texture with winsys framebuffers */
2226
2227 for (i = 0; i < BUFFER_COUNT; i++) {
2228 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2229 if (att->Texture && att->Renderbuffer->TexImage
2230 && driver_RenderTexture_is_safe(att)) {
2231 ctx->Driver.RenderTexture(ctx, fb, att);
2232 }
2233 }
2234 }
2235
2236
2237 /**
2238 * Examine all the framebuffer's attachments to see if any are textures.
2239 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2240 * notify the device driver that the texture image may have changed.
2241 */
2242 static void
2243 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2244 {
2245 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2246 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2247 return;
2248
2249 if (ctx->Driver.FinishRenderTexture) {
2250 GLuint i;
2251 for (i = 0; i < BUFFER_COUNT; i++) {
2252 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2253 struct gl_renderbuffer *rb = att->Renderbuffer;
2254 if (rb && rb->NeedsFinishRenderTexture) {
2255 ctx->Driver.FinishRenderTexture(ctx, rb);
2256 }
2257 }
2258 }
2259 }
2260
2261
2262 static void
2263 bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
2264 {
2265 struct gl_framebuffer *newDrawFb, *newReadFb;
2266 struct gl_framebuffer *oldDrawFb, *oldReadFb;
2267 GLboolean bindReadBuf, bindDrawBuf;
2268 GET_CURRENT_CONTEXT(ctx);
2269
2270 switch (target) {
2271 case GL_DRAW_FRAMEBUFFER_EXT:
2272 bindDrawBuf = GL_TRUE;
2273 bindReadBuf = GL_FALSE;
2274 break;
2275 case GL_READ_FRAMEBUFFER_EXT:
2276 bindDrawBuf = GL_FALSE;
2277 bindReadBuf = GL_TRUE;
2278 break;
2279 case GL_FRAMEBUFFER_EXT:
2280 bindDrawBuf = GL_TRUE;
2281 bindReadBuf = GL_TRUE;
2282 break;
2283 default:
2284 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
2285 return;
2286 }
2287
2288 if (framebuffer) {
2289 /* Binding a user-created framebuffer object */
2290 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2291 if (newDrawFb == &DummyFramebuffer) {
2292 /* ID was reserved, but no real framebuffer object made yet */
2293 newDrawFb = NULL;
2294 }
2295 else if (!newDrawFb && !allow_user_names) {
2296 /* All FBO IDs must be Gen'd */
2297 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2298 return;
2299 }
2300
2301 if (!newDrawFb) {
2302 /* create new framebuffer object */
2303 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2304 if (!newDrawFb) {
2305 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2306 return;
2307 }
2308 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
2309 }
2310 newReadFb = newDrawFb;
2311 }
2312 else {
2313 /* Binding the window system framebuffer (which was originally set
2314 * with MakeCurrent).
2315 */
2316 newDrawFb = ctx->WinSysDrawBuffer;
2317 newReadFb = ctx->WinSysReadBuffer;
2318 }
2319
2320 assert(newDrawFb);
2321 assert(newDrawFb != &DummyFramebuffer);
2322
2323 /* save pointers to current/old framebuffers */
2324 oldDrawFb = ctx->DrawBuffer;
2325 oldReadFb = ctx->ReadBuffer;
2326
2327 /* check if really changing bindings */
2328 if (oldDrawFb == newDrawFb)
2329 bindDrawBuf = GL_FALSE;
2330 if (oldReadFb == newReadFb)
2331 bindReadBuf = GL_FALSE;
2332
2333 /*
2334 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2335 *
2336 * We also check if we're beginning and/or ending render-to-texture.
2337 * When a framebuffer with texture attachments is unbound, call
2338 * ctx->Driver.FinishRenderTexture().
2339 * When a framebuffer with texture attachments is bound, call
2340 * ctx->Driver.RenderTexture().
2341 *
2342 * Note that if the ReadBuffer has texture attachments we don't consider
2343 * that a render-to-texture case.
2344 */
2345 if (bindReadBuf) {
2346 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2347
2348 /* check if old readbuffer was render-to-texture */
2349 check_end_texture_render(ctx, oldReadFb);
2350
2351 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2352 }
2353
2354 if (bindDrawBuf) {
2355 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2356
2357 /* check if old framebuffer had any texture attachments */
2358 if (oldDrawFb)
2359 check_end_texture_render(ctx, oldDrawFb);
2360
2361 /* check if newly bound framebuffer has any texture attachments */
2362 check_begin_texture_render(ctx, newDrawFb);
2363
2364 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2365 }
2366
2367 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2368 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
2369 }
2370 }
2371
2372 void GLAPIENTRY
2373 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
2374 {
2375 GET_CURRENT_CONTEXT(ctx);
2376
2377 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2378 * point, but they allow the use of user-generated names.
2379 */
2380 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
2381 }
2382
2383
2384 void GLAPIENTRY
2385 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2386 {
2387 /* This function should not be in the dispatch table for core profile /
2388 * OpenGL 3.1, so execution should never get here in those cases -- no
2389 * need for an explicit test.
2390 */
2391 bind_framebuffer(target, framebuffer, true);
2392 }
2393
2394
2395 void GLAPIENTRY
2396 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2397 {
2398 GLint i;
2399 GET_CURRENT_CONTEXT(ctx);
2400
2401 if (n < 0) {
2402 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2403 return;
2404 }
2405
2406 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2407
2408 for (i = 0; i < n; i++) {
2409 if (framebuffers[i] > 0) {
2410 struct gl_framebuffer *fb;
2411 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2412 if (fb) {
2413 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2414
2415 /* check if deleting currently bound framebuffer object */
2416 if (fb == ctx->DrawBuffer) {
2417 /* bind default */
2418 assert(fb->RefCount >= 2);
2419 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2420 }
2421 if (fb == ctx->ReadBuffer) {
2422 /* bind default */
2423 assert(fb->RefCount >= 2);
2424 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2425 }
2426
2427 /* remove from hash table immediately, to free the ID */
2428 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2429
2430 if (fb != &DummyFramebuffer) {
2431 /* But the object will not be freed until it's no longer
2432 * bound in any context.
2433 */
2434 _mesa_reference_framebuffer(&fb, NULL);
2435 }
2436 }
2437 }
2438 }
2439 }
2440
2441
2442 /**
2443 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2444 * It is not exposed to the rest of Mesa to encourage the use of
2445 * nameless buffers in driver internals.
2446 */
2447 static void
2448 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
2449 {
2450 GET_CURRENT_CONTEXT(ctx);
2451 GLuint first;
2452 GLint i;
2453 struct gl_framebuffer *fb;
2454
2455 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
2456
2457 if (n < 0) {
2458 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
2459 return;
2460 }
2461
2462 if (!framebuffers)
2463 return;
2464
2465 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2466
2467 for (i = 0; i < n; i++) {
2468 GLuint name = first + i;
2469 framebuffers[i] = name;
2470
2471 if (dsa) {
2472 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2473 if (!fb) {
2474 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2475 return;
2476 }
2477 }
2478 else
2479 fb = &DummyFramebuffer;
2480
2481 mtx_lock(&ctx->Shared->Mutex);
2482 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, fb);
2483 mtx_unlock(&ctx->Shared->Mutex);
2484 }
2485 }
2486
2487
2488 void GLAPIENTRY
2489 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2490 {
2491 create_framebuffers(n, framebuffers, false);
2492 }
2493
2494
2495 void GLAPIENTRY
2496 _mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2497 {
2498 create_framebuffers(n, framebuffers, true);
2499 }
2500
2501
2502 GLenum GLAPIENTRY
2503 _mesa_CheckFramebufferStatus(GLenum target)
2504 {
2505 struct gl_framebuffer *buffer;
2506 GET_CURRENT_CONTEXT(ctx);
2507
2508 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2509
2510 if (MESA_VERBOSE & VERBOSE_API)
2511 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2512 _mesa_lookup_enum_by_nr(target));
2513
2514 buffer = get_framebuffer_target(ctx, target);
2515 if (!buffer) {
2516 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
2517 return 0;
2518 }
2519
2520 if (_mesa_is_winsys_fbo(buffer)) {
2521 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2522 if (buffer != &IncompleteFramebuffer) {
2523 return GL_FRAMEBUFFER_COMPLETE_EXT;
2524 } else {
2525 return GL_FRAMEBUFFER_UNDEFINED;
2526 }
2527 }
2528
2529 /* No need to flush here */
2530
2531 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2532 _mesa_test_framebuffer_completeness(ctx, buffer);
2533 }
2534
2535 return buffer->_Status;
2536 }
2537
2538
2539 /**
2540 * Replicate the src attachment point. Used by framebuffer_texture() when
2541 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2542 * GL_STENCIL_ATTACHMENT.
2543 */
2544 static void
2545 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2546 gl_buffer_index dst,
2547 gl_buffer_index src)
2548 {
2549 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2550 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2551
2552 assert(src_att->Texture != NULL);
2553 assert(src_att->Renderbuffer != NULL);
2554
2555 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2556 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2557 dst_att->Type = src_att->Type;
2558 dst_att->Complete = src_att->Complete;
2559 dst_att->TextureLevel = src_att->TextureLevel;
2560 dst_att->Zoffset = src_att->Zoffset;
2561 }
2562
2563
2564 /**
2565 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2566 * texture object pointer and check for associated errors.
2567 *
2568 * \param textarget is the textarget that was passed to the
2569 * glFramebufferTexture...() function, or 0 if the corresponding function
2570 * doesn't have a textarget parameter.
2571 *
2572 * \param layered is true if this function was called from
2573 * gl*FramebufferTexture(), false otherwise.
2574 *
2575 * \param texObj where the pointer to the texture object is returned. Note
2576 * that a successful call may return texObj = NULL.
2577 *
2578 * \return true if no errors, false if errors
2579 */
2580 static bool
2581 get_texture_for_framebuffer(struct gl_context *ctx,
2582 GLuint texture, GLenum textarget,
2583 GLint level, GLuint zoffset, GLboolean *layered,
2584 const char *caller,
2585 struct gl_texture_object **texObj)
2586 {
2587 GLenum maxLevelsTarget;
2588 GLboolean err = GL_TRUE;
2589
2590 *texObj = NULL; /* This will get returned if texture = 0. */
2591
2592 /* The textarget, level, and zoffset parameters are only validated if
2593 * texture is non-zero.
2594 */
2595 if (!texture)
2596 return true;
2597
2598 *texObj = _mesa_lookup_texture(ctx, texture);
2599 if (*texObj == NULL || (*texObj)->Target == 0) {
2600 /* Can't render to a non-existent texture object.
2601 *
2602 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2603 * Managing Framebuffer Objects specifies a different error
2604 * depending upon the calling function (PDF pages 325-328).
2605 * *FramebufferTexture (where *layered = GL_TRUE) throws invalid
2606 * value, while the other commands throw invalid operation (where
2607 * *layered = GL_FALSE).
2608 */
2609 const GLenum error = *layered ? GL_INVALID_VALUE :
2610 GL_INVALID_OPERATION;
2611 _mesa_error(ctx, error,
2612 "%s(non-existent texture %u)", caller, texture);
2613 return false;
2614 }
2615
2616 if (textarget == 0) {
2617 if (*layered) {
2618 /* We're being called by gl*FramebufferTexture() and textarget
2619 * is not used.
2620 */
2621 switch ((*texObj)->Target) {
2622 case GL_TEXTURE_3D:
2623 case GL_TEXTURE_1D_ARRAY_EXT:
2624 case GL_TEXTURE_2D_ARRAY_EXT:
2625 case GL_TEXTURE_CUBE_MAP:
2626 case GL_TEXTURE_CUBE_MAP_ARRAY:
2627 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2628 err = false;
2629 break;
2630 case GL_TEXTURE_1D:
2631 case GL_TEXTURE_2D:
2632 case GL_TEXTURE_RECTANGLE:
2633 case GL_TEXTURE_2D_MULTISAMPLE:
2634 /* These texture types are valid to pass to
2635 * glFramebufferTexture(), but since they aren't layered, it
2636 * is equivalent to calling glFramebufferTexture{1D,2D}().
2637 */
2638 err = false;
2639 *layered = false;
2640 textarget = (*texObj)->Target;
2641 break;
2642 default:
2643 err = true;
2644 break;
2645 }
2646 } else {
2647 /* We're being called by glFramebufferTextureLayer() and
2648 * textarget is not used. The only legal texture types for
2649 * that function are 3D and 1D/2D arrays textures.
2650 */
2651 err = ((*texObj)->Target != GL_TEXTURE_3D) &&
2652 ((*texObj)->Target != GL_TEXTURE_1D_ARRAY) &&
2653 ((*texObj)->Target != GL_TEXTURE_2D_ARRAY) &&
2654 ((*texObj)->Target != GL_TEXTURE_CUBE_MAP_ARRAY) &&
2655 ((*texObj)->Target != GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
2656 }
2657 }
2658 else {
2659 /* Make sure textarget is consistent with the texture's type */
2660 err = ((*texObj)->Target == GL_TEXTURE_CUBE_MAP)
2661 ? !_mesa_is_cube_face(textarget)
2662 : ((*texObj)->Target != textarget);
2663 }
2664
2665 if (err) {
2666 _mesa_error(ctx, GL_INVALID_OPERATION,
2667 "%s(invalid or mismatched texture target)", caller);
2668 return false;
2669 }
2670
2671 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
2672 * spec says:
2673 *
2674 * "An INVALID_VALUE error is generated if texture is non-zero
2675 * and layer is negative."
2676 */
2677 if (zoffset < 0) {
2678 _mesa_error(ctx, GL_INVALID_VALUE,
2679 "%s(layer %u < 0)", caller, zoffset);
2680 return false;
2681 }
2682
2683 if ((*texObj)->Target == GL_TEXTURE_3D) {
2684 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2685 if (zoffset >= maxSize) {
2686 _mesa_error(ctx, GL_INVALID_VALUE,
2687 "%s(invalid zoffset %u)", caller, zoffset);
2688 return false;
2689 }
2690 }
2691 else if (((*texObj)->Target == GL_TEXTURE_1D_ARRAY) ||
2692 ((*texObj)->Target == GL_TEXTURE_2D_ARRAY) ||
2693 ((*texObj)->Target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
2694 ((*texObj)->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
2695 if (zoffset >= ctx->Const.MaxArrayTextureLayers) {
2696 _mesa_error(ctx, GL_INVALID_VALUE,
2697 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
2698 caller, zoffset);
2699 return false;
2700 }
2701 }
2702
2703 maxLevelsTarget = textarget ? textarget : (*texObj)->Target;
2704 if ((level < 0) ||
2705 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2706 _mesa_error(ctx, GL_INVALID_VALUE,
2707 "%s(invalid level %d)", caller, level);
2708 return false;
2709 }
2710
2711 return true;
2712 }
2713
2714
2715 void
2716 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
2717 GLenum attachment,
2718 struct gl_texture_object *texObj, GLenum textarget,
2719 GLint level, GLuint zoffset, GLboolean layered,
2720 const char *caller)
2721 {
2722 struct gl_renderbuffer_attachment *att;
2723
2724 /* The window-system framebuffer object is immutable */
2725 if (_mesa_is_winsys_fbo(fb)) {
2726 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
2727 caller);
2728 return;
2729 }
2730
2731 /* Not a hash lookup, so we can afford to get the attachment here. */
2732 att = get_attachment(ctx, fb, attachment);
2733 if (att == NULL) {
2734 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
2735 _mesa_lookup_enum_by_nr(attachment));
2736 return;
2737 }
2738
2739 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2740
2741 mtx_lock(&fb->Mutex);
2742 if (texObj) {
2743 if (attachment == GL_DEPTH_ATTACHMENT &&
2744 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2745 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2746 _mesa_tex_target_to_face(textarget) ==
2747 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2748 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2749 /* The texture object is already attached to the stencil attachment
2750 * point. Don't create a new renderbuffer; just reuse the stencil
2751 * attachment's. This is required to prevent a GL error in
2752 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2753 */
2754 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2755 BUFFER_STENCIL);
2756 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2757 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2758 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2759 _mesa_tex_target_to_face(textarget) ==
2760 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2761 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2762 /* As above, but with depth and stencil transposed. */
2763 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2764 BUFFER_DEPTH);
2765 } else {
2766 set_texture_attachment(ctx, fb, att, texObj, textarget,
2767 level, zoffset, layered);
2768
2769 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2770 /* Above we created a new renderbuffer and attached it to the
2771 * depth attachment point. Now attach it to the stencil attachment
2772 * point too.
2773 */
2774 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2775 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2776 BUFFER_DEPTH);
2777 }
2778 }
2779
2780 /* Set the render-to-texture flag. We'll check this flag in
2781 * glTexImage() and friends to determine if we need to revalidate
2782 * any FBOs that might be rendering into this texture.
2783 * This flag never gets cleared since it's non-trivial to determine
2784 * when all FBOs might be done rendering to this texture. That's OK
2785 * though since it's uncommon to render to a texture then repeatedly
2786 * call glTexImage() to change images in the texture.
2787 */
2788 texObj->_RenderToTexture = GL_TRUE;
2789 }
2790 else {
2791 remove_attachment(ctx, att);
2792 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2793 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2794 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2795 }
2796 }
2797
2798 invalidate_framebuffer(fb);
2799
2800 mtx_unlock(&fb->Mutex);
2801 }
2802
2803
2804 void GLAPIENTRY
2805 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2806 GLenum textarget, GLuint texture, GLint level)
2807 {
2808 GET_CURRENT_CONTEXT(ctx);
2809 struct gl_framebuffer *fb;
2810 struct gl_texture_object *texObj;
2811 GLboolean layered = GL_FALSE;
2812
2813 if (texture != 0) {
2814 GLboolean error;
2815
2816 switch (textarget) {
2817 case GL_TEXTURE_1D:
2818 error = GL_FALSE;
2819 break;
2820 case GL_TEXTURE_1D_ARRAY:
2821 error = !ctx->Extensions.EXT_texture_array;
2822 break;
2823 default:
2824 error = GL_TRUE;
2825 }
2826
2827 if (error) {
2828 _mesa_error(ctx, GL_INVALID_OPERATION,
2829 "glFramebufferTexture1D(invalid textarget %s)",
2830 _mesa_lookup_enum_by_nr(textarget));
2831 return;
2832 }
2833 }
2834
2835 /* Get the framebuffer object */
2836 fb = get_framebuffer_target(ctx, target);
2837 if (!fb) {
2838 _mesa_error(ctx, GL_INVALID_ENUM,
2839 "glFramebufferTexture1D(invalid target %s)",
2840 _mesa_lookup_enum_by_nr(target));
2841 return;
2842 }
2843
2844 /* Get the texture object */
2845 if (!get_texture_for_framebuffer(ctx, texture, textarget, level, 0,
2846 &layered, "glFramebufferTexture1D",
2847 &texObj)) {
2848 /* Error already recorded */
2849 return;
2850 }
2851
2852 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
2853 0, layered, "glFramebufferTexture1D");
2854 }
2855
2856
2857 void GLAPIENTRY
2858 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2859 GLenum textarget, GLuint texture, GLint level)
2860 {
2861 GET_CURRENT_CONTEXT(ctx);
2862 struct gl_framebuffer *fb;
2863 struct gl_texture_object *texObj;
2864 GLboolean layered = GL_FALSE;
2865
2866 if (texture != 0) {
2867 GLboolean error;
2868
2869 switch (textarget) {
2870 case GL_TEXTURE_2D:
2871 error = GL_FALSE;
2872 break;
2873 case GL_TEXTURE_RECTANGLE:
2874 error = _mesa_is_gles(ctx)
2875 || !ctx->Extensions.NV_texture_rectangle;
2876 break;
2877 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2878 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2879 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2880 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2881 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2883 error = !ctx->Extensions.ARB_texture_cube_map;
2884 break;
2885 case GL_TEXTURE_2D_ARRAY:
2886 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2887 || !ctx->Extensions.EXT_texture_array;
2888 break;
2889 case GL_TEXTURE_2D_MULTISAMPLE:
2890 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2891 error = _mesa_is_gles(ctx)
2892 || !ctx->Extensions.ARB_texture_multisample;
2893 break;
2894 default:
2895 error = GL_TRUE;
2896 }
2897
2898 if (error) {
2899 _mesa_error(ctx, GL_INVALID_OPERATION,
2900 "glFramebufferTexture2D(invalid textarget %s)",
2901 _mesa_lookup_enum_by_nr(textarget));
2902 return;
2903 }
2904 }
2905
2906 /* Get the framebuffer object */
2907 fb = get_framebuffer_target(ctx, target);
2908 if (!fb) {
2909 _mesa_error(ctx, GL_INVALID_ENUM,
2910 "glFramebufferTexture2D(invalid target %s)",
2911 _mesa_lookup_enum_by_nr(target));
2912 return;
2913 }
2914
2915 /* Get the texture object */
2916 if (!get_texture_for_framebuffer(ctx, texture, textarget, level, 0,
2917 &layered, "glFramebufferTexture2D",
2918 &texObj)) {
2919 /* Error already recorded */
2920 return;
2921 }
2922
2923 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
2924 0, layered, "glFramebufferTexture2D");
2925 }
2926
2927
2928 void GLAPIENTRY
2929 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2930 GLenum textarget, GLuint texture,
2931 GLint level, GLint zoffset)
2932 {
2933 GET_CURRENT_CONTEXT(ctx);
2934 struct gl_framebuffer *fb;
2935 struct gl_texture_object *texObj;
2936 GLboolean layered = GL_FALSE;
2937
2938 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2939 _mesa_error(ctx, GL_INVALID_OPERATION,
2940 "glFramebufferTexture3D(invalid textarget %s)",
2941 _mesa_lookup_enum_by_nr(textarget));
2942 return;
2943 }
2944
2945 /* Get the framebuffer object */
2946 fb = get_framebuffer_target(ctx, target);
2947 if (!fb) {
2948 _mesa_error(ctx, GL_INVALID_ENUM,
2949 "glFramebufferTexture3D(invalid target %s)",
2950 _mesa_lookup_enum_by_nr(target));
2951 return;
2952 }
2953
2954 /* Get the texture object */
2955 if (!get_texture_for_framebuffer(ctx, texture, textarget, level, zoffset,
2956 &layered, "glFramebufferTexture3D",
2957 &texObj)) {
2958 /* Error already recorded */
2959 return;
2960 }
2961
2962 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
2963 zoffset, layered, "glFramebufferTexture3D");
2964 }
2965
2966
2967 void GLAPIENTRY
2968 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2969 GLuint texture, GLint level, GLint layer)
2970 {
2971 GET_CURRENT_CONTEXT(ctx);
2972 struct gl_framebuffer *fb;
2973 struct gl_texture_object *texObj;
2974 GLboolean layered = GL_FALSE;
2975
2976 /* Get the framebuffer object */
2977 fb = get_framebuffer_target(ctx, target);
2978 if (!fb) {
2979 _mesa_error(ctx, GL_INVALID_ENUM,
2980 "glFramebufferTextureLayer(invalid target %s)",
2981 _mesa_lookup_enum_by_nr(target));
2982 return;
2983 }
2984
2985 /* Get the texture object */
2986 if (!get_texture_for_framebuffer(ctx, texture, 0, level, layer,
2987 &layered, "glFramebufferTextureLayer",
2988 &texObj)) {
2989 /* Error already recorded */
2990 return;
2991 }
2992
2993 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
2994 layer, layered, "glFramebufferTextureLayer");
2995 }
2996
2997
2998 void GLAPIENTRY
2999 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3000 GLuint texture, GLint level)
3001 {
3002 GET_CURRENT_CONTEXT(ctx);
3003 struct gl_framebuffer *fb;
3004 struct gl_texture_object *texObj;
3005 GLboolean layered = GL_TRUE;
3006
3007 if (!_mesa_has_geometry_shaders(ctx)) {
3008 _mesa_error(ctx, GL_INVALID_OPERATION,
3009 "unsupported function (glFramebufferTexture) called");
3010 return;
3011 }
3012
3013 /* Get the framebuffer object */
3014 fb = get_framebuffer_target(ctx, target);
3015 if (!fb) {
3016 _mesa_error(ctx, GL_INVALID_ENUM,
3017 "glFramebufferTexture(invalid target %s)",
3018 _mesa_lookup_enum_by_nr(target));
3019 return;
3020 }
3021
3022 /* Get the texture object */
3023 if (!get_texture_for_framebuffer(ctx, texture, 0, level, 0,
3024 &layered, "glFramebufferTexture",
3025 &texObj)) {
3026 /* Error already recorded */
3027 return;
3028 }
3029
3030 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3031 0, layered, "glFramebufferTexture");
3032 }
3033
3034
3035 void
3036 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3037 struct gl_framebuffer *fb,
3038 GLenum attachment,
3039 struct gl_renderbuffer *rb,
3040 const char *func)
3041 {
3042 struct gl_renderbuffer_attachment *att;
3043
3044 if (_mesa_is_winsys_fbo(fb)) {
3045 /* Can't attach new renderbuffers to a window system framebuffer */
3046 _mesa_error(ctx, GL_INVALID_OPERATION,
3047 "%s(window-system framebuffer)", func);
3048 return;
3049 }
3050
3051 att = get_attachment(ctx, fb, attachment);
3052 if (att == NULL) {
3053 _mesa_error(ctx, GL_INVALID_ENUM,
3054 "%s(invalid attachment %s)", func,
3055 _mesa_lookup_enum_by_nr(attachment));
3056 return;
3057 }
3058
3059 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3060 rb && rb->Format != MESA_FORMAT_NONE) {
3061 /* make sure the renderbuffer is a depth/stencil format */
3062 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3063 if (baseFormat != GL_DEPTH_STENCIL) {
3064 _mesa_error(ctx, GL_INVALID_OPERATION,
3065 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3066 return;
3067 }
3068 }
3069
3070 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3071
3072 assert(ctx->Driver.FramebufferRenderbuffer);
3073 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3074
3075 /* Some subsequent GL commands may depend on the framebuffer's visual
3076 * after the binding is updated. Update visual info now.
3077 */
3078 _mesa_update_framebuffer_visual(ctx, fb);
3079 }
3080
3081
3082 void GLAPIENTRY
3083 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3084 GLenum renderbuffertarget,
3085 GLuint renderbuffer)
3086 {
3087 struct gl_framebuffer *fb;
3088 struct gl_renderbuffer *rb;
3089 GET_CURRENT_CONTEXT(ctx);
3090
3091 fb = get_framebuffer_target(ctx, target);
3092 if (!fb) {
3093 _mesa_error(ctx, GL_INVALID_ENUM,
3094 "glFramebufferRenderbuffer(invalid target %s)",
3095 _mesa_lookup_enum_by_nr(target));
3096 return;
3097 }
3098
3099 if (renderbuffertarget != GL_RENDERBUFFER) {
3100 _mesa_error(ctx, GL_INVALID_ENUM,
3101 "glFramebufferRenderbuffer(renderbuffertarget is not "
3102 "GL_RENDERBUFFER)");
3103 return;
3104 }
3105
3106 if (renderbuffer) {
3107 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3108 "glFramebufferRenderbuffer");
3109 if (!rb)
3110 return;
3111 }
3112 else {
3113 /* remove renderbuffer attachment */
3114 rb = NULL;
3115 }
3116
3117 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb,
3118 "glFramebufferRenderbuffer");
3119 }
3120
3121
3122 void GLAPIENTRY
3123 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3124 GLenum renderbuffertarget,
3125 GLuint renderbuffer)
3126 {
3127 struct gl_framebuffer *fb;
3128 struct gl_renderbuffer *rb;
3129 GET_CURRENT_CONTEXT(ctx);
3130
3131 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3132 "glNamedFramebufferRenderbuffer");
3133
3134 if (renderbuffertarget != GL_RENDERBUFFER) {
3135 _mesa_error(ctx, GL_INVALID_ENUM,
3136 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3137 "GL_RENDERBUFFER)");
3138 return;
3139 }
3140
3141 if (renderbuffer) {
3142 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3143 "glNamedFramebufferRenderbuffer");
3144 if (!rb)
3145 return;
3146 }
3147 else {
3148 /* remove renderbuffer attachment */
3149 rb = NULL;
3150 }
3151
3152 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb,
3153 "glNamedFramebufferRenderbuffer");
3154 }
3155
3156
3157 void GLAPIENTRY
3158 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
3159 GLenum pname, GLint *params)
3160 {
3161 const struct gl_renderbuffer_attachment *att;
3162 struct gl_framebuffer *buffer;
3163 GLenum err;
3164 GET_CURRENT_CONTEXT(ctx);
3165
3166 /* The error differs in GL and GLES. */
3167 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
3168
3169 buffer = get_framebuffer_target(ctx, target);
3170 if (!buffer) {
3171 _mesa_error(ctx, GL_INVALID_ENUM,
3172 "glGetFramebufferAttachmentParameteriv(target)");
3173 return;
3174 }
3175
3176 if (_mesa_is_winsys_fbo(buffer)) {
3177 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3178 * says:
3179 *
3180 * "If the framebuffer currently bound to target is zero, then
3181 * INVALID_OPERATION is generated."
3182 *
3183 * The EXT_framebuffer_object spec has the same wording, and the
3184 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3185 * spec.
3186 */
3187 if ((!_mesa_is_desktop_gl(ctx) ||
3188 !ctx->Extensions.ARB_framebuffer_object)
3189 && !_mesa_is_gles3(ctx)) {
3190 _mesa_error(ctx, GL_INVALID_OPERATION,
3191 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
3192 return;
3193 }
3194
3195 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3196 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3197 _mesa_error(ctx, GL_INVALID_ENUM,
3198 "glGetFramebufferAttachmentParameteriv(attachment)");
3199 return;
3200 }
3201 /* the default / window-system FBO */
3202 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
3203 }
3204 else {
3205 /* user-created framebuffer FBO */
3206 att = get_attachment(ctx, buffer, attachment);
3207 }
3208
3209 if (att == NULL) {
3210 _mesa_error(ctx, GL_INVALID_ENUM,
3211 "glGetFramebufferAttachmentParameteriv(attachment)");
3212 return;
3213 }
3214
3215 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3216 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3217 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3218 /* This behavior is first specified in OpenGL 4.4 specification.
3219 *
3220 * From the OpenGL 4.4 spec page 275:
3221 * "This query cannot be performed for a combined depth+stencil
3222 * attachment, since it does not have a single format."
3223 */
3224 _mesa_error(ctx, GL_INVALID_OPERATION,
3225 "glGetFramebufferAttachmentParameteriv("
3226 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3227 " is invalid for depth+stencil attachment)");
3228 return;
3229 }
3230 /* the depth and stencil attachments must point to the same buffer */
3231 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
3232 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
3233 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3234 _mesa_error(ctx, GL_INVALID_OPERATION,
3235 "glGetFramebufferAttachmentParameteriv(DEPTH/STENCIL"
3236 " attachments differ)");
3237 return;
3238 }
3239 }
3240
3241 /* No need to flush here */
3242
3243 switch (pname) {
3244 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3245 *params = _mesa_is_winsys_fbo(buffer)
3246 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
3247 return;
3248 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3249 if (att->Type == GL_RENDERBUFFER_EXT) {
3250 *params = att->Renderbuffer->Name;
3251 }
3252 else if (att->Type == GL_TEXTURE) {
3253 *params = att->Texture->Name;
3254 }
3255 else {
3256 assert(att->Type == GL_NONE);
3257 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3258 *params = 0;
3259 } else {
3260 goto invalid_pname_enum;
3261 }
3262 }
3263 return;
3264 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3265 if (att->Type == GL_TEXTURE) {
3266 *params = att->TextureLevel;
3267 }
3268 else if (att->Type == GL_NONE) {
3269 _mesa_error(ctx, err,
3270 "glGetFramebufferAttachmentParameteriv(pname)");
3271 }
3272 else {
3273 goto invalid_pname_enum;
3274 }
3275 return;
3276 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3277 if (att->Type == GL_TEXTURE) {
3278 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3279 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3280 }
3281 else {
3282 *params = 0;
3283 }
3284 }
3285 else if (att->Type == GL_NONE) {
3286 _mesa_error(ctx, err,
3287 "glGetFramebufferAttachmentParameteriv(pname)");
3288 }
3289 else {
3290 goto invalid_pname_enum;
3291 }
3292 return;
3293 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3294 if (ctx->API == API_OPENGLES) {
3295 goto invalid_pname_enum;
3296 } else if (att->Type == GL_NONE) {
3297 _mesa_error(ctx, err,
3298 "glGetFramebufferAttachmentParameteriv(pname)");
3299 } else if (att->Type == GL_TEXTURE) {
3300 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3301 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
3302 *params = att->Zoffset;
3303 }
3304 else {
3305 *params = 0;
3306 }
3307 }
3308 else {
3309 goto invalid_pname_enum;
3310 }
3311 return;
3312 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3313 if ((!_mesa_is_desktop_gl(ctx) ||
3314 !ctx->Extensions.ARB_framebuffer_object)
3315 && !_mesa_is_gles3(ctx)) {
3316 goto invalid_pname_enum;
3317 }
3318 else if (att->Type == GL_NONE) {
3319 _mesa_error(ctx, err,
3320 "glGetFramebufferAttachmentParameteriv(pname)");
3321 }
3322 else {
3323 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3324 *params =
3325 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
3326 }
3327 else {
3328 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3329 * if the sRGB conversion is unsupported. */
3330 *params = GL_LINEAR;
3331 }
3332 }
3333 return;
3334 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3335 if ((ctx->API != API_OPENGL_COMPAT ||
3336 !ctx->Extensions.ARB_framebuffer_object)
3337 && ctx->API != API_OPENGL_CORE
3338 && !_mesa_is_gles3(ctx)) {
3339 goto invalid_pname_enum;
3340 }
3341 else if (att->Type == GL_NONE) {
3342 _mesa_error(ctx, err,
3343 "glGetFramebufferAttachmentParameteriv(pname)");
3344 }
3345 else {
3346 mesa_format format = att->Renderbuffer->Format;
3347
3348 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3349 * 3.0.1 spec says:
3350 *
3351 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3352 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3353 * generate an INVALID_OPERATION error.
3354 */
3355 if (_mesa_is_gles3(ctx) &&
3356 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3357 _mesa_error(ctx, GL_INVALID_OPERATION,
3358 "glGetFramebufferAttachmentParameteriv(cannot query "
3359 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3360 "GL_DEPTH_STENCIL_ATTACHMENT");
3361 return;
3362 }
3363
3364 if (format == MESA_FORMAT_S_UINT8) {
3365 /* special cases */
3366 *params = GL_INDEX;
3367 }
3368 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
3369 /* depends on the attachment parameter */
3370 if (attachment == GL_STENCIL_ATTACHMENT) {
3371 *params = GL_INDEX;
3372 }
3373 else {
3374 *params = GL_FLOAT;
3375 }
3376 }
3377 else {
3378 *params = _mesa_get_format_datatype(format);
3379 }
3380 }
3381 return;
3382 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3383 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3384 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3385 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3386 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3387 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3388 if ((!_mesa_is_desktop_gl(ctx) ||
3389 !ctx->Extensions.ARB_framebuffer_object)
3390 && !_mesa_is_gles3(ctx)) {
3391 goto invalid_pname_enum;
3392 }
3393 else if (att->Type == GL_NONE) {
3394 _mesa_error(ctx, err,
3395 "glGetFramebufferAttachmentParameteriv(pname)");
3396 }
3397 else if (att->Texture) {
3398 const struct gl_texture_image *texImage =
3399 _mesa_select_tex_image(att->Texture, att->Texture->Target,
3400 att->TextureLevel);
3401 if (texImage) {
3402 *params = get_component_bits(pname, texImage->_BaseFormat,
3403 texImage->TexFormat);
3404 }
3405 else {
3406 *params = 0;
3407 }
3408 }
3409 else if (att->Renderbuffer) {
3410 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
3411 att->Renderbuffer->Format);
3412 }
3413 else {
3414 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
3415 " invalid FBO attachment structure");
3416 }
3417 return;
3418 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
3419 if (!_mesa_has_geometry_shaders(ctx)) {
3420 goto invalid_pname_enum;
3421 } else if (att->Type == GL_TEXTURE) {
3422 *params = att->Layered;
3423 } else if (att->Type == GL_NONE) {
3424 _mesa_error(ctx, err,
3425 "glGetFramebufferAttachmentParameteriv(pname)");
3426 } else {
3427 goto invalid_pname_enum;
3428 }
3429 return;
3430 default:
3431 goto invalid_pname_enum;
3432 }
3433
3434 return;
3435
3436 invalid_pname_enum:
3437 _mesa_error(ctx, GL_INVALID_ENUM,
3438 "glGetFramebufferAttachmentParameteriv(pname)");
3439 return;
3440 }
3441
3442
3443 static void
3444 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3445 const GLenum *attachments, GLint x, GLint y,
3446 GLsizei width, GLsizei height, const char *name)
3447 {
3448 int i;
3449 struct gl_framebuffer *fb;
3450 GET_CURRENT_CONTEXT(ctx);
3451
3452 fb = get_framebuffer_target(ctx, target);
3453 if (!fb) {
3454 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3455 return;
3456 }
3457
3458 if (numAttachments < 0) {
3459 _mesa_error(ctx, GL_INVALID_VALUE,
3460 "%s(numAttachments < 0)", name);
3461 return;
3462 }
3463
3464 /* The GL_ARB_invalidate_subdata spec says:
3465 *
3466 * "If an attachment is specified that does not exist in the
3467 * framebuffer bound to <target>, it is ignored."
3468 *
3469 * It also says:
3470 *
3471 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3472 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3473 * INVALID_OPERATION is generated."
3474 *
3475 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3476 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3477 * set of retrictions).
3478 */
3479 for (i = 0; i < numAttachments; i++) {
3480 if (_mesa_is_winsys_fbo(fb)) {
3481 switch (attachments[i]) {
3482 case GL_ACCUM:
3483 case GL_AUX0:
3484 case GL_AUX1:
3485 case GL_AUX2:
3486 case GL_AUX3:
3487 /* Accumulation buffers and auxilary buffers were removed in
3488 * OpenGL 3.1, and they never existed in OpenGL ES.
3489 */
3490 if (ctx->API != API_OPENGL_COMPAT)
3491 goto invalid_enum;
3492 break;
3493 case GL_COLOR:
3494 case GL_DEPTH:
3495 case GL_STENCIL:
3496 break;
3497 case GL_BACK_LEFT:
3498 case GL_BACK_RIGHT:
3499 case GL_FRONT_LEFT:
3500 case GL_FRONT_RIGHT:
3501 if (!_mesa_is_desktop_gl(ctx))
3502 goto invalid_enum;
3503 break;
3504 default:
3505 goto invalid_enum;
3506 }
3507 } else {
3508 switch (attachments[i]) {
3509 case GL_DEPTH_ATTACHMENT:
3510 case GL_STENCIL_ATTACHMENT:
3511 break;
3512 case GL_DEPTH_STENCIL_ATTACHMENT:
3513 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
3514 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
3515 * extension does not make this attachment point valid on ES 2.0.
3516 */
3517 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
3518 break;
3519 /* fallthrough */
3520 case GL_COLOR_ATTACHMENT0:
3521 case GL_COLOR_ATTACHMENT1:
3522 case GL_COLOR_ATTACHMENT2:
3523 case GL_COLOR_ATTACHMENT3:
3524 case GL_COLOR_ATTACHMENT4:
3525 case GL_COLOR_ATTACHMENT5:
3526 case GL_COLOR_ATTACHMENT6:
3527 case GL_COLOR_ATTACHMENT7:
3528 case GL_COLOR_ATTACHMENT8:
3529 case GL_COLOR_ATTACHMENT9:
3530 case GL_COLOR_ATTACHMENT10:
3531 case GL_COLOR_ATTACHMENT11:
3532 case GL_COLOR_ATTACHMENT12:
3533 case GL_COLOR_ATTACHMENT13:
3534 case GL_COLOR_ATTACHMENT14:
3535 case GL_COLOR_ATTACHMENT15: {
3536 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3537 if (k >= ctx->Const.MaxColorAttachments) {
3538 _mesa_error(ctx, GL_INVALID_OPERATION,
3539 "%s(attachment >= max. color attachments)", name);
3540 return;
3541 }
3542 break;
3543 }
3544 default:
3545 goto invalid_enum;
3546 }
3547 }
3548 }
3549
3550 /* We don't actually do anything for this yet. Just return after
3551 * validating the parameters and generating the required errors.
3552 */
3553 return;
3554
3555 invalid_enum:
3556 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3557 return;
3558 }
3559
3560
3561 void GLAPIENTRY
3562 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3563 const GLenum *attachments, GLint x, GLint y,
3564 GLsizei width, GLsizei height)
3565 {
3566 invalidate_framebuffer_storage(target, numAttachments, attachments,
3567 x, y, width, height,
3568 "glInvalidateSubFramebuffer");
3569 }
3570
3571
3572 void GLAPIENTRY
3573 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3574 const GLenum *attachments)
3575 {
3576 /* The GL_ARB_invalidate_subdata spec says:
3577 *
3578 * "The command
3579 *
3580 * void InvalidateFramebuffer(enum target,
3581 * sizei numAttachments,
3582 * const enum *attachments);
3583 *
3584 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3585 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3586 * <MAX_VIEWPORT_DIMS[1]> respectively."
3587 */
3588 invalidate_framebuffer_storage(target, numAttachments, attachments,
3589 0, 0,
3590 MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3591 "glInvalidateFramebuffer");
3592 }
3593
3594
3595 void GLAPIENTRY
3596 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
3597 const GLenum *attachments)
3598 {
3599 struct gl_framebuffer *fb;
3600 GLint i;
3601
3602 GET_CURRENT_CONTEXT(ctx);
3603
3604 fb = get_framebuffer_target(ctx, target);
3605 if (!fb) {
3606 _mesa_error(ctx, GL_INVALID_ENUM,
3607 "glDiscardFramebufferEXT(target %s)",
3608 _mesa_lookup_enum_by_nr(target));
3609 return;
3610 }
3611
3612 if (numAttachments < 0) {
3613 _mesa_error(ctx, GL_INVALID_VALUE,
3614 "glDiscardFramebufferEXT(numAttachments < 0)");
3615 return;
3616 }
3617
3618 for (i = 0; i < numAttachments; i++) {
3619 switch (attachments[i]) {
3620 case GL_COLOR:
3621 case GL_DEPTH:
3622 case GL_STENCIL:
3623 if (_mesa_is_user_fbo(fb))
3624 goto invalid_enum;
3625 break;
3626 case GL_COLOR_ATTACHMENT0:
3627 case GL_DEPTH_ATTACHMENT:
3628 case GL_STENCIL_ATTACHMENT:
3629 if (_mesa_is_winsys_fbo(fb))
3630 goto invalid_enum;
3631 break;
3632 default:
3633 goto invalid_enum;
3634 }
3635 }
3636
3637 if (ctx->Driver.DiscardFramebuffer)
3638 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
3639
3640 return;
3641
3642 invalid_enum:
3643 _mesa_error(ctx, GL_INVALID_ENUM,
3644 "glDiscardFramebufferEXT(attachment %s)",
3645 _mesa_lookup_enum_by_nr(attachments[i]));
3646 }