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