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