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