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