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