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