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