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