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