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