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