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