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