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