Merge branch 'mesa_7_6_branch'
[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
35 #include "buffers.h"
36 #include "context.h"
37 #include "enums.h"
38 #include "fbobject.h"
39 #include "framebuffer.h"
40 #include "hash.h"
41 #include "macros.h"
42 #include "mipmap.h"
43 #include "renderbuffer.h"
44 #include "state.h"
45 #include "teximage.h"
46 #include "texobj.h"
47 #include "texstore.h"
48 #include "texstate.h"
49
50
51 /** Set this to 1 to help debug FBO incompleteness problems */
52 #define DEBUG_FBO 0
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
77 #define IS_CUBE_FACE(TARGET) \
78 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
79 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
80
81
82 static void
83 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
84 {
85 /* no op */
86 }
87
88 static void
89 delete_dummy_framebuffer(struct gl_framebuffer *fb)
90 {
91 /* no op */
92 }
93
94
95 void
96 _mesa_init_fbobjects(GLcontext *ctx)
97 {
98 DummyFramebuffer.Delete = delete_dummy_framebuffer;
99 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
100 }
101
102
103 /**
104 * Helper routine for getting a gl_renderbuffer.
105 */
106 struct gl_renderbuffer *
107 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
108 {
109 struct gl_renderbuffer *rb;
110
111 if (id == 0)
112 return NULL;
113
114 rb = (struct gl_renderbuffer *)
115 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
116 return rb;
117 }
118
119
120 /**
121 * Helper routine for getting a gl_framebuffer.
122 */
123 struct gl_framebuffer *
124 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
125 {
126 struct gl_framebuffer *fb;
127
128 if (id == 0)
129 return NULL;
130
131 fb = (struct gl_framebuffer *)
132 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
133 return fb;
134 }
135
136
137 /**
138 * Mark the given framebuffer as invalid. This will force the
139 * test for framebuffer completeness to be done before the framebuffer
140 * is used.
141 */
142 static void
143 invalidate_framebuffer(struct gl_framebuffer *fb)
144 {
145 fb->_Status = 0; /* "indeterminate" */
146 }
147
148
149 /**
150 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
151 * gl_renderbuffer_attachment object.
152 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
153 * the depth buffer attachment point.
154 */
155 struct gl_renderbuffer_attachment *
156 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
157 GLenum attachment)
158 {
159 GLuint i;
160
161 switch (attachment) {
162 case GL_COLOR_ATTACHMENT0_EXT:
163 case GL_COLOR_ATTACHMENT1_EXT:
164 case GL_COLOR_ATTACHMENT2_EXT:
165 case GL_COLOR_ATTACHMENT3_EXT:
166 case GL_COLOR_ATTACHMENT4_EXT:
167 case GL_COLOR_ATTACHMENT5_EXT:
168 case GL_COLOR_ATTACHMENT6_EXT:
169 case GL_COLOR_ATTACHMENT7_EXT:
170 case GL_COLOR_ATTACHMENT8_EXT:
171 case GL_COLOR_ATTACHMENT9_EXT:
172 case GL_COLOR_ATTACHMENT10_EXT:
173 case GL_COLOR_ATTACHMENT11_EXT:
174 case GL_COLOR_ATTACHMENT12_EXT:
175 case GL_COLOR_ATTACHMENT13_EXT:
176 case GL_COLOR_ATTACHMENT14_EXT:
177 case GL_COLOR_ATTACHMENT15_EXT:
178 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
179 if (i >= ctx->Const.MaxColorAttachments) {
180 return NULL;
181 }
182 return &fb->Attachment[BUFFER_COLOR0 + i];
183 case GL_DEPTH_STENCIL_ATTACHMENT:
184 /* fall-through */
185 case GL_DEPTH_ATTACHMENT_EXT:
186 return &fb->Attachment[BUFFER_DEPTH];
187 case GL_STENCIL_ATTACHMENT_EXT:
188 return &fb->Attachment[BUFFER_STENCIL];
189 default:
190 return NULL;
191 }
192 }
193
194
195 /**
196 * Remove any texture or renderbuffer attached to the given attachment
197 * point. Update reference counts, etc.
198 */
199 void
200 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
201 {
202 if (att->Type == GL_TEXTURE) {
203 ASSERT(att->Texture);
204 if (ctx->Driver.FinishRenderTexture) {
205 /* tell driver that we're done rendering to this texture. */
206 ctx->Driver.FinishRenderTexture(ctx, att);
207 }
208 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
209 ASSERT(!att->Texture);
210 }
211 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
212 ASSERT(!att->Texture);
213 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
214 ASSERT(!att->Renderbuffer);
215 }
216 att->Type = GL_NONE;
217 att->Complete = GL_TRUE;
218 }
219
220
221 /**
222 * Bind a texture object to an attachment point.
223 * The previous binding, if any, will be removed first.
224 */
225 void
226 _mesa_set_texture_attachment(GLcontext *ctx,
227 struct gl_framebuffer *fb,
228 struct gl_renderbuffer_attachment *att,
229 struct gl_texture_object *texObj,
230 GLenum texTarget, GLuint level, GLuint zoffset)
231 {
232 if (att->Texture == texObj) {
233 /* re-attaching same texture */
234 ASSERT(att->Type == GL_TEXTURE);
235 }
236 else {
237 /* new attachment */
238 _mesa_remove_attachment(ctx, att);
239 att->Type = GL_TEXTURE;
240 assert(!att->Texture);
241 _mesa_reference_texobj(&att->Texture, texObj);
242 }
243
244 /* always update these fields */
245 att->TextureLevel = level;
246 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
247 att->Zoffset = zoffset;
248 att->Complete = GL_FALSE;
249
250 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
251 ctx->Driver.RenderTexture(ctx, fb, att);
252 }
253
254 invalidate_framebuffer(fb);
255 }
256
257
258 /**
259 * Bind a renderbuffer to an attachment point.
260 * The previous binding, if any, will be removed first.
261 */
262 void
263 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
264 struct gl_renderbuffer_attachment *att,
265 struct gl_renderbuffer *rb)
266 {
267 /* XXX check if re-doing same attachment, exit early */
268 _mesa_remove_attachment(ctx, att);
269 att->Type = GL_RENDERBUFFER_EXT;
270 att->Texture = NULL; /* just to be safe */
271 att->Complete = GL_FALSE;
272 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
273 }
274
275
276 /**
277 * Fallback for ctx->Driver.FramebufferRenderbuffer()
278 * Attach a renderbuffer object to a framebuffer object.
279 */
280 void
281 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
282 GLenum attachment, struct gl_renderbuffer *rb)
283 {
284 struct gl_renderbuffer_attachment *att;
285
286 _glthread_LOCK_MUTEX(fb->Mutex);
287
288 att = _mesa_get_attachment(ctx, fb, attachment);
289 ASSERT(att);
290 if (rb) {
291 _mesa_set_renderbuffer_attachment(ctx, att, rb);
292 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
293 /* do stencil attachment here (depth already done above) */
294 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
295 assert(att);
296 _mesa_set_renderbuffer_attachment(ctx, att, rb);
297 }
298 }
299 else {
300 _mesa_remove_attachment(ctx, att);
301 }
302
303 invalidate_framebuffer(fb);
304
305 _glthread_UNLOCK_MUTEX(fb->Mutex);
306 }
307
308
309 /**
310 * For debug only.
311 */
312 static void
313 att_incomplete(const char *msg)
314 {
315 #if DEBUG_FBO
316 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
317 #else
318 (void) msg;
319 #endif
320 }
321
322
323 /**
324 * For debug only.
325 */
326 static void
327 fbo_incomplete(const char *msg, int index)
328 {
329 #if DEBUG_FBO
330 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
331 #else
332 (void) msg;
333 (void) index;
334 #endif
335 }
336
337
338
339
340 /**
341 * Test if an attachment point is complete and update its Complete field.
342 * \param format if GL_COLOR, this is a color attachment point,
343 * if GL_DEPTH, this is a depth component attachment point,
344 * if GL_STENCIL, this is a stencil component attachment point.
345 */
346 static void
347 test_attachment_completeness(const GLcontext *ctx, GLenum format,
348 struct gl_renderbuffer_attachment *att)
349 {
350 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
351
352 /* assume complete */
353 att->Complete = GL_TRUE;
354
355 /* Look for reasons why the attachment might be incomplete */
356 if (att->Type == GL_TEXTURE) {
357 const struct gl_texture_object *texObj = att->Texture;
358 struct gl_texture_image *texImage;
359
360 if (!texObj) {
361 att_incomplete("no texobj");
362 att->Complete = GL_FALSE;
363 return;
364 }
365
366 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
367 if (!texImage) {
368 att_incomplete("no teximage");
369 att->Complete = GL_FALSE;
370 return;
371 }
372 if (texImage->Width < 1 || texImage->Height < 1) {
373 att_incomplete("teximage width/height=0");
374 _mesa_printf("texobj = %u\n", texObj->Name);
375 _mesa_printf("level = %d\n", att->TextureLevel);
376 att->Complete = GL_FALSE;
377 return;
378 }
379 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
380 att_incomplete("bad z offset");
381 att->Complete = GL_FALSE;
382 return;
383 }
384
385 if (format == GL_COLOR) {
386 if (texImage->TexFormat->BaseFormat != GL_RGB &&
387 texImage->TexFormat->BaseFormat != GL_RGBA) {
388 att_incomplete("bad format");
389 att->Complete = GL_FALSE;
390 return;
391 }
392 if (texImage->TexFormat->TexelBytes == 0) {
393 att_incomplete("compressed internalformat");
394 att->Complete = GL_FALSE;
395 return;
396 }
397 }
398 else if (format == GL_DEPTH) {
399 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
400 /* OK */
401 }
402 else if (ctx->Extensions.EXT_packed_depth_stencil &&
403 ctx->Extensions.ARB_depth_texture &&
404 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
405 /* OK */
406 }
407 else {
408 att->Complete = GL_FALSE;
409 att_incomplete("bad depth format");
410 return;
411 }
412 }
413 else {
414 ASSERT(format == GL_STENCIL);
415 ASSERT(att->Renderbuffer->StencilBits);
416 if (ctx->Extensions.EXT_packed_depth_stencil &&
417 ctx->Extensions.ARB_depth_texture &&
418 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
419 /* OK */
420 }
421 else {
422 /* no such thing as stencil-only textures */
423 att_incomplete("illegal stencil texture");
424 att->Complete = GL_FALSE;
425 return;
426 }
427 }
428 }
429 else if (att->Type == GL_RENDERBUFFER_EXT) {
430 ASSERT(att->Renderbuffer);
431 if (!att->Renderbuffer->InternalFormat ||
432 att->Renderbuffer->Width < 1 ||
433 att->Renderbuffer->Height < 1) {
434 att_incomplete("0x0 renderbuffer");
435 att->Complete = GL_FALSE;
436 return;
437 }
438 if (format == GL_COLOR) {
439 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
440 att->Renderbuffer->_BaseFormat != GL_RGBA) {
441 att_incomplete("bad renderbuffer color format");
442 att->Complete = GL_FALSE;
443 return;
444 }
445 ASSERT(att->Renderbuffer->RedBits);
446 ASSERT(att->Renderbuffer->GreenBits);
447 ASSERT(att->Renderbuffer->BlueBits);
448 }
449 else if (format == GL_DEPTH) {
450 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
451 ASSERT(att->Renderbuffer->DepthBits);
452 /* OK */
453 }
454 else if (ctx->Extensions.EXT_packed_depth_stencil &&
455 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
456 ASSERT(att->Renderbuffer->DepthBits);
457 /* OK */
458 }
459 else {
460 att_incomplete("bad renderbuffer depth format");
461 att->Complete = GL_FALSE;
462 return;
463 }
464 }
465 else {
466 assert(format == GL_STENCIL);
467 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
468 ASSERT(att->Renderbuffer->StencilBits);
469 /* OK */
470 }
471 else if (ctx->Extensions.EXT_packed_depth_stencil &&
472 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
473 ASSERT(att->Renderbuffer->StencilBits);
474 /* OK */
475 }
476 else {
477 att->Complete = GL_FALSE;
478 att_incomplete("bad renderbuffer stencil format");
479 return;
480 }
481 }
482 }
483 else {
484 ASSERT(att->Type == GL_NONE);
485 /* complete */
486 return;
487 }
488 }
489
490
491 /**
492 * Test if the given framebuffer object is complete and update its
493 * Status field with the results.
494 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
495 * driver to make hardware-specific validation/completeness checks.
496 * Also update the framebuffer's Width and Height fields if the
497 * framebuffer is complete.
498 */
499 void
500 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
501 {
502 GLuint numImages;
503 GLenum intFormat = GL_NONE; /* color buffers' internal format */
504 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
505 GLint numSamples = -1;
506 GLint i;
507 GLuint j;
508
509 assert(fb->Name != 0);
510
511 numImages = 0;
512 fb->Width = 0;
513 fb->Height = 0;
514
515 /* Start at -2 to more easily loop over all attachment points.
516 * -2: depth buffer
517 * -1: stencil buffer
518 * >=0: color buffer
519 */
520 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
521 struct gl_renderbuffer_attachment *att;
522 GLenum f;
523
524 /*
525 * XXX for ARB_fbo, only check color buffers that are named by
526 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
527 */
528
529 /* check for attachment completeness
530 */
531 if (i == -2) {
532 att = &fb->Attachment[BUFFER_DEPTH];
533 test_attachment_completeness(ctx, GL_DEPTH, att);
534 if (!att->Complete) {
535 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
536 fbo_incomplete("depth attachment incomplete", -1);
537 return;
538 }
539 }
540 else if (i == -1) {
541 att = &fb->Attachment[BUFFER_STENCIL];
542 test_attachment_completeness(ctx, GL_STENCIL, att);
543 if (!att->Complete) {
544 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
545 fbo_incomplete("stencil attachment incomplete", -1);
546 return;
547 }
548 }
549 else {
550 att = &fb->Attachment[BUFFER_COLOR0 + i];
551 test_attachment_completeness(ctx, GL_COLOR, att);
552 if (!att->Complete) {
553 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
554 fbo_incomplete("color attachment incomplete", i);
555 return;
556 }
557 }
558
559 /* get width, height, format of the renderbuffer/texture
560 */
561 if (att->Type == GL_TEXTURE) {
562 const struct gl_texture_image *texImg
563 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
564 minWidth = MIN2(minWidth, texImg->Width);
565 maxWidth = MAX2(maxWidth, texImg->Width);
566 minHeight = MIN2(minHeight, texImg->Height);
567 maxHeight = MAX2(maxHeight, texImg->Height);
568 f = texImg->_BaseFormat;
569 numImages++;
570 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
571 && f != GL_DEPTH_STENCIL_EXT) {
572 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
573 fbo_incomplete("texture attachment incomplete", -1);
574 return;
575 }
576 }
577 else if (att->Type == GL_RENDERBUFFER_EXT) {
578 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
579 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
580 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
581 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
582 f = att->Renderbuffer->InternalFormat;
583 numImages++;
584 }
585 else {
586 assert(att->Type == GL_NONE);
587 continue;
588 }
589
590 if (numSamples < 0) {
591 /* first buffer */
592 numSamples = att->Renderbuffer->NumSamples;
593 }
594
595 /* Error-check width, height, format, samples
596 */
597 if (numImages == 1) {
598 /* save format, num samples */
599 if (i >= 0) {
600 intFormat = f;
601 }
602 }
603 else {
604 if (!ctx->Extensions.ARB_framebuffer_object) {
605 /* check that width, height, format are same */
606 if (minWidth != maxWidth || minHeight != maxHeight) {
607 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
608 fbo_incomplete("width or height mismatch", -1);
609 return;
610 }
611 /* check that all color buffer have same format */
612 if (intFormat != GL_NONE && f != intFormat) {
613 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
614 fbo_incomplete("format mismatch", -1);
615 return;
616 }
617 }
618 if (att->Renderbuffer &&
619 att->Renderbuffer->NumSamples != numSamples) {
620 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
621 fbo_incomplete("inconsistant number of samples", i);
622 return;
623 }
624
625 }
626 }
627
628 #ifndef FEATURE_OES_framebuffer_object
629 /* Check that all DrawBuffers are present */
630 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
631 if (fb->ColorDrawBuffer[j] != GL_NONE) {
632 const struct gl_renderbuffer_attachment *att
633 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
634 assert(att);
635 if (att->Type == GL_NONE) {
636 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
637 fbo_incomplete("missing drawbuffer", j);
638 return;
639 }
640 }
641 }
642
643 /* Check that the ReadBuffer is present */
644 if (fb->ColorReadBuffer != GL_NONE) {
645 const struct gl_renderbuffer_attachment *att
646 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
647 assert(att);
648 if (att->Type == GL_NONE) {
649 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
650 fbo_incomplete("missing readbuffer", -1);
651 return;
652 }
653 }
654 #endif
655
656 if (numImages == 0) {
657 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
658 fbo_incomplete("no attachments", -1);
659 return;
660 }
661
662 /* Provisionally set status = COMPLETE ... */
663 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
664
665 /* ... but the driver may say the FB is incomplete.
666 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
667 * if anything.
668 */
669 if (ctx->Driver.ValidateFramebuffer) {
670 ctx->Driver.ValidateFramebuffer(ctx, fb);
671 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
672 fbo_incomplete("driver marked FBO as incomplete", -1);
673 }
674 }
675
676 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
677 /*
678 * Note that if ARB_framebuffer_object is supported and the attached
679 * renderbuffers/textures are different sizes, the framebuffer
680 * width/height will be set to the smallest width/height.
681 */
682 fb->Width = minWidth;
683 fb->Height = minHeight;
684
685 /* finally, update the visual info for the framebuffer */
686 _mesa_update_framebuffer_visual(fb);
687 }
688 }
689
690
691 GLboolean GLAPIENTRY
692 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
693 {
694 GET_CURRENT_CONTEXT(ctx);
695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
696 if (renderbuffer) {
697 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
698 if (rb != NULL && rb != &DummyRenderbuffer)
699 return GL_TRUE;
700 }
701 return GL_FALSE;
702 }
703
704
705 void GLAPIENTRY
706 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
707 {
708 struct gl_renderbuffer *newRb;
709 GET_CURRENT_CONTEXT(ctx);
710
711 ASSERT_OUTSIDE_BEGIN_END(ctx);
712
713 if (target != GL_RENDERBUFFER_EXT) {
714 _mesa_error(ctx, GL_INVALID_ENUM,
715 "glBindRenderbufferEXT(target)");
716 return;
717 }
718
719 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
720
721 if (renderbuffer) {
722 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
723 if (newRb == &DummyRenderbuffer) {
724 /* ID was reserved, but no real renderbuffer object made yet */
725 newRb = NULL;
726 }
727 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
728 /* All RB IDs must be Gen'd */
729 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
730 return;
731 }
732
733 if (!newRb) {
734 /* create new renderbuffer object */
735 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
736 if (!newRb) {
737 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
738 return;
739 }
740 ASSERT(newRb->AllocStorage);
741 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
742 newRb->RefCount = 1; /* referenced by hash table */
743 }
744 }
745 else {
746 newRb = NULL;
747 }
748
749 ASSERT(newRb != &DummyRenderbuffer);
750
751 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
752 }
753
754
755 /**
756 * If the given renderbuffer is anywhere attached to the framebuffer, detach
757 * the renderbuffer.
758 * This is used when a renderbuffer object is deleted.
759 * The spec calls for unbinding.
760 */
761 static void
762 detach_renderbuffer(GLcontext *ctx,
763 struct gl_framebuffer *fb,
764 struct gl_renderbuffer *rb)
765 {
766 GLuint i;
767 for (i = 0; i < BUFFER_COUNT; i++) {
768 if (fb->Attachment[i].Renderbuffer == rb) {
769 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
770 }
771 }
772 invalidate_framebuffer(fb);
773 }
774
775
776 void GLAPIENTRY
777 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
778 {
779 GLint i;
780 GET_CURRENT_CONTEXT(ctx);
781
782 ASSERT_OUTSIDE_BEGIN_END(ctx);
783 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
784
785 for (i = 0; i < n; i++) {
786 if (renderbuffers[i] > 0) {
787 struct gl_renderbuffer *rb;
788 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
789 if (rb) {
790 /* check if deleting currently bound renderbuffer object */
791 if (rb == ctx->CurrentRenderbuffer) {
792 /* bind default */
793 ASSERT(rb->RefCount >= 2);
794 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
795 }
796
797 if (ctx->DrawBuffer->Name) {
798 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
799 }
800 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
801 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
802 }
803
804 /* Remove from hash table immediately, to free the ID.
805 * But the object will not be freed until it's no longer
806 * referenced anywhere else.
807 */
808 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
809
810 if (rb != &DummyRenderbuffer) {
811 /* no longer referenced by hash table */
812 _mesa_reference_renderbuffer(&rb, NULL);
813 }
814 }
815 }
816 }
817 }
818
819
820 void GLAPIENTRY
821 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
822 {
823 GET_CURRENT_CONTEXT(ctx);
824 GLuint first;
825 GLint i;
826
827 ASSERT_OUTSIDE_BEGIN_END(ctx);
828
829 if (n < 0) {
830 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
831 return;
832 }
833
834 if (!renderbuffers)
835 return;
836
837 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
838
839 for (i = 0; i < n; i++) {
840 GLuint name = first + i;
841 renderbuffers[i] = name;
842 /* insert dummy placeholder into hash table */
843 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
844 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
845 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
846 }
847 }
848
849
850 /**
851 * Given an internal format token for a render buffer, return the
852 * corresponding base format.
853 * This is very similar to _mesa_base_tex_format() but the set of valid
854 * internal formats is somewhat different.
855 *
856 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
857 * GL_DEPTH_STENCIL_EXT or zero if error.
858 */
859 GLenum
860 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
861 {
862 switch (internalFormat) {
863 case GL_RGB:
864 case GL_R3_G3_B2:
865 case GL_RGB4:
866 case GL_RGB5:
867 case GL_RGB8:
868 case GL_RGB10:
869 case GL_RGB12:
870 case GL_RGB16:
871 return GL_RGB;
872 case GL_RGBA:
873 case GL_RGBA2:
874 case GL_RGBA4:
875 case GL_RGB5_A1:
876 case GL_RGBA8:
877 case GL_RGB10_A2:
878 case GL_RGBA12:
879 case GL_RGBA16:
880 return GL_RGBA;
881 case GL_STENCIL_INDEX:
882 case GL_STENCIL_INDEX1_EXT:
883 case GL_STENCIL_INDEX4_EXT:
884 case GL_STENCIL_INDEX8_EXT:
885 case GL_STENCIL_INDEX16_EXT:
886 return GL_STENCIL_INDEX;
887 case GL_DEPTH_COMPONENT:
888 case GL_DEPTH_COMPONENT16:
889 case GL_DEPTH_COMPONENT24:
890 case GL_DEPTH_COMPONENT32:
891 return GL_DEPTH_COMPONENT;
892 case GL_DEPTH_STENCIL_EXT:
893 case GL_DEPTH24_STENCIL8_EXT:
894 if (ctx->Extensions.EXT_packed_depth_stencil)
895 return GL_DEPTH_STENCIL_EXT;
896 else
897 return 0;
898 /* XXX add floating point formats eventually */
899 default:
900 return 0;
901 }
902 }
903
904
905 /** sentinal value, see below */
906 #define NO_SAMPLES 1000
907
908
909 /**
910 * Helper function used by _mesa_RenderbufferStorageEXT() and
911 * _mesa_RenderbufferStorageMultisample().
912 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
913 */
914 static void
915 renderbuffer_storage(GLenum target, GLenum internalFormat,
916 GLsizei width, GLsizei height, GLsizei samples)
917 {
918 const char *func = samples == NO_SAMPLES ?
919 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
920 struct gl_renderbuffer *rb;
921 GLenum baseFormat;
922 GET_CURRENT_CONTEXT(ctx);
923
924 ASSERT_OUTSIDE_BEGIN_END(ctx);
925
926 if (target != GL_RENDERBUFFER_EXT) {
927 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
928 return;
929 }
930
931 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
932 if (baseFormat == 0) {
933 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
934 return;
935 }
936
937 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
938 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
939 return;
940 }
941
942 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
943 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
944 return;
945 }
946
947 if (samples == NO_SAMPLES) {
948 /* NumSamples == 0 indicates non-multisampling */
949 samples = 0;
950 }
951 else if (samples > ctx->Const.MaxSamples) {
952 /* note: driver may choose to use more samples than what's requested */
953 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
954 return;
955 }
956
957 rb = ctx->CurrentRenderbuffer;
958 if (!rb) {
959 _mesa_error(ctx, GL_INVALID_OPERATION, func);
960 return;
961 }
962
963 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
964
965 if (rb->InternalFormat == internalFormat &&
966 rb->Width == (GLuint) width &&
967 rb->Height == (GLuint) height) {
968 /* no change in allocation needed */
969 return;
970 }
971
972 /* These MUST get set by the AllocStorage func */
973 rb->_ActualFormat = 0;
974 rb->RedBits =
975 rb->GreenBits =
976 rb->BlueBits =
977 rb->AlphaBits =
978 rb->IndexBits =
979 rb->DepthBits =
980 rb->StencilBits = 0;
981 rb->NumSamples = samples;
982
983 /* Now allocate the storage */
984 ASSERT(rb->AllocStorage);
985 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
986 /* No error - check/set fields now */
987 assert(rb->_ActualFormat);
988 assert(rb->Width == (GLuint) width);
989 assert(rb->Height == (GLuint) height);
990 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
991 rb->DepthBits || rb->StencilBits || rb->IndexBits);
992 rb->InternalFormat = internalFormat;
993 rb->_BaseFormat = baseFormat;
994 }
995 else {
996 /* Probably ran out of memory - clear the fields */
997 rb->Width = 0;
998 rb->Height = 0;
999 rb->InternalFormat = GL_NONE;
1000 rb->_ActualFormat = GL_NONE;
1001 rb->_BaseFormat = GL_NONE;
1002 rb->RedBits =
1003 rb->GreenBits =
1004 rb->BlueBits =
1005 rb->AlphaBits =
1006 rb->IndexBits =
1007 rb->DepthBits =
1008 rb->StencilBits =
1009 rb->NumSamples = 0;
1010 }
1011
1012 /*
1013 test_framebuffer_completeness(ctx, fb);
1014 */
1015 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1016 * points???
1017 */
1018 }
1019
1020
1021 void GLAPIENTRY
1022 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1023 GLsizei width, GLsizei height)
1024 {
1025 /* GL_ARB_fbo says calling this function is equivalent to calling
1026 * glRenderbufferStorageMultisample() with samples=0. We pass in
1027 * a token value here just for error reporting purposes.
1028 */
1029 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1030 }
1031
1032
1033 void GLAPIENTRY
1034 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1035 GLenum internalFormat,
1036 GLsizei width, GLsizei height)
1037 {
1038 renderbuffer_storage(target, internalFormat, width, height, samples);
1039 }
1040
1041
1042
1043 void GLAPIENTRY
1044 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1045 {
1046 struct gl_renderbuffer *rb;
1047 GET_CURRENT_CONTEXT(ctx);
1048
1049 ASSERT_OUTSIDE_BEGIN_END(ctx);
1050
1051 if (target != GL_RENDERBUFFER_EXT) {
1052 _mesa_error(ctx, GL_INVALID_ENUM,
1053 "glGetRenderbufferParameterivEXT(target)");
1054 return;
1055 }
1056
1057 rb = ctx->CurrentRenderbuffer;
1058 if (!rb) {
1059 _mesa_error(ctx, GL_INVALID_OPERATION,
1060 "glGetRenderbufferParameterivEXT");
1061 return;
1062 }
1063
1064 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1065
1066 switch (pname) {
1067 case GL_RENDERBUFFER_WIDTH_EXT:
1068 *params = rb->Width;
1069 return;
1070 case GL_RENDERBUFFER_HEIGHT_EXT:
1071 *params = rb->Height;
1072 return;
1073 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1074 *params = rb->InternalFormat;
1075 return;
1076 case GL_RENDERBUFFER_RED_SIZE_EXT:
1077 *params = rb->RedBits;
1078 break;
1079 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1080 *params = rb->GreenBits;
1081 break;
1082 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1083 *params = rb->BlueBits;
1084 break;
1085 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1086 *params = rb->AlphaBits;
1087 break;
1088 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1089 *params = rb->DepthBits;
1090 break;
1091 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1092 *params = rb->StencilBits;
1093 break;
1094 case GL_RENDERBUFFER_SAMPLES:
1095 if (ctx->Extensions.ARB_framebuffer_object) {
1096 *params = rb->NumSamples;
1097 break;
1098 }
1099 /* fallthrough */
1100 default:
1101 _mesa_error(ctx, GL_INVALID_ENUM,
1102 "glGetRenderbufferParameterivEXT(target)");
1103 return;
1104 }
1105 }
1106
1107
1108 GLboolean GLAPIENTRY
1109 _mesa_IsFramebufferEXT(GLuint framebuffer)
1110 {
1111 GET_CURRENT_CONTEXT(ctx);
1112 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1113 if (framebuffer) {
1114 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1115 if (rb != NULL && rb != &DummyFramebuffer)
1116 return GL_TRUE;
1117 }
1118 return GL_FALSE;
1119 }
1120
1121
1122 static void
1123 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1124 {
1125 GLuint i;
1126 ASSERT(ctx->Driver.RenderTexture);
1127 for (i = 0; i < BUFFER_COUNT; i++) {
1128 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1129 struct gl_texture_object *texObj = att->Texture;
1130 if (texObj
1131 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1132 ctx->Driver.RenderTexture(ctx, fb, att);
1133 }
1134 }
1135 }
1136
1137
1138 /**
1139 * Examine all the framebuffer's attachments to see if any are textures.
1140 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1141 * notify the device driver that the texture image may have changed.
1142 */
1143 static void
1144 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1145 {
1146 if (ctx->Driver.FinishRenderTexture) {
1147 GLuint i;
1148 for (i = 0; i < BUFFER_COUNT; i++) {
1149 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1150 if (att->Texture && att->Renderbuffer) {
1151 ctx->Driver.FinishRenderTexture(ctx, att);
1152 }
1153 }
1154 }
1155 }
1156
1157
1158 void GLAPIENTRY
1159 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1160 {
1161 struct gl_framebuffer *newFb, *newFbread;
1162 GLboolean bindReadBuf, bindDrawBuf;
1163 GET_CURRENT_CONTEXT(ctx);
1164
1165 #ifdef DEBUG
1166 if (ctx->Extensions.ARB_framebuffer_object) {
1167 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1168 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1169 }
1170 #endif
1171
1172 ASSERT_OUTSIDE_BEGIN_END(ctx);
1173
1174 if (!ctx->Extensions.EXT_framebuffer_object) {
1175 _mesa_error(ctx, GL_INVALID_OPERATION,
1176 "glBindFramebufferEXT(unsupported)");
1177 return;
1178 }
1179
1180 switch (target) {
1181 #if FEATURE_EXT_framebuffer_blit
1182 case GL_DRAW_FRAMEBUFFER_EXT:
1183 if (!ctx->Extensions.EXT_framebuffer_blit) {
1184 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1185 return;
1186 }
1187 bindDrawBuf = GL_TRUE;
1188 bindReadBuf = GL_FALSE;
1189 break;
1190 case GL_READ_FRAMEBUFFER_EXT:
1191 if (!ctx->Extensions.EXT_framebuffer_blit) {
1192 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1193 return;
1194 }
1195 bindDrawBuf = GL_FALSE;
1196 bindReadBuf = GL_TRUE;
1197 break;
1198 #endif
1199 case GL_FRAMEBUFFER_EXT:
1200 bindDrawBuf = GL_TRUE;
1201 bindReadBuf = GL_TRUE;
1202 break;
1203 default:
1204 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1205 return;
1206 }
1207
1208 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1209
1210 if (framebuffer) {
1211 /* Binding a user-created framebuffer object */
1212 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1213 if (newFb == &DummyFramebuffer) {
1214 /* ID was reserved, but no real framebuffer object made yet */
1215 newFb = NULL;
1216 }
1217 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1218 /* All FBO IDs must be Gen'd */
1219 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1220 return;
1221 }
1222
1223 if (!newFb) {
1224 /* create new framebuffer object */
1225 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1226 if (!newFb) {
1227 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1228 return;
1229 }
1230 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1231 }
1232 newFbread = newFb;
1233 }
1234 else {
1235 /* Binding the window system framebuffer (which was originally set
1236 * with MakeCurrent).
1237 */
1238 newFb = ctx->WinSysDrawBuffer;
1239 newFbread = ctx->WinSysReadBuffer;
1240 }
1241
1242 ASSERT(newFb);
1243 ASSERT(newFb != &DummyFramebuffer);
1244
1245 /*
1246 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1247 */
1248
1249 if (bindReadBuf) {
1250 if (ctx->ReadBuffer == newFbread)
1251 bindReadBuf = GL_FALSE; /* no change */
1252 else
1253 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1254 }
1255
1256 if (bindDrawBuf) {
1257 /* check if old FB had any texture attachments */
1258 if (ctx->DrawBuffer->Name != 0) {
1259 check_end_texture_render(ctx, ctx->DrawBuffer);
1260 }
1261
1262 if (ctx->DrawBuffer == newFb)
1263 bindDrawBuf = GL_FALSE; /* no change */
1264 else
1265 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1266
1267 if (newFb->Name != 0) {
1268 /* check if newly bound framebuffer has any texture attachments */
1269 check_begin_texture_render(ctx, newFb);
1270 }
1271 }
1272
1273 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1274 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1275 }
1276 }
1277
1278
1279 void GLAPIENTRY
1280 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1281 {
1282 GLint i;
1283 GET_CURRENT_CONTEXT(ctx);
1284
1285 ASSERT_OUTSIDE_BEGIN_END(ctx);
1286 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1287
1288 for (i = 0; i < n; i++) {
1289 if (framebuffers[i] > 0) {
1290 struct gl_framebuffer *fb;
1291 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1292 if (fb) {
1293 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1294
1295 /* check if deleting currently bound framebuffer object */
1296 if (fb == ctx->DrawBuffer) {
1297 /* bind default */
1298 ASSERT(fb->RefCount >= 2);
1299 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1300 }
1301 if (fb == ctx->ReadBuffer) {
1302 /* bind default */
1303 ASSERT(fb->RefCount >= 2);
1304 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1305 }
1306
1307 /* remove from hash table immediately, to free the ID */
1308 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1309
1310 if (fb != &DummyFramebuffer) {
1311 /* But the object will not be freed until it's no longer
1312 * bound in any context.
1313 */
1314 _mesa_reference_framebuffer(&fb, NULL);
1315 }
1316 }
1317 }
1318 }
1319 }
1320
1321
1322 void GLAPIENTRY
1323 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1324 {
1325 GET_CURRENT_CONTEXT(ctx);
1326 GLuint first;
1327 GLint i;
1328
1329 ASSERT_OUTSIDE_BEGIN_END(ctx);
1330
1331 if (n < 0) {
1332 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1333 return;
1334 }
1335
1336 if (!framebuffers)
1337 return;
1338
1339 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1340
1341 for (i = 0; i < n; i++) {
1342 GLuint name = first + i;
1343 framebuffers[i] = name;
1344 /* insert dummy placeholder into hash table */
1345 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1346 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1347 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1348 }
1349 }
1350
1351
1352
1353 GLenum GLAPIENTRY
1354 _mesa_CheckFramebufferStatusEXT(GLenum target)
1355 {
1356 struct gl_framebuffer *buffer;
1357 GET_CURRENT_CONTEXT(ctx);
1358
1359 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1360
1361 switch (target) {
1362 #if FEATURE_EXT_framebuffer_blit
1363 case GL_DRAW_FRAMEBUFFER_EXT:
1364 if (!ctx->Extensions.EXT_framebuffer_blit) {
1365 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1366 return 0;
1367 }
1368 buffer = ctx->DrawBuffer;
1369 break;
1370 case GL_READ_FRAMEBUFFER_EXT:
1371 if (!ctx->Extensions.EXT_framebuffer_blit) {
1372 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1373 return 0;
1374 }
1375 buffer = ctx->ReadBuffer;
1376 break;
1377 #endif
1378 case GL_FRAMEBUFFER_EXT:
1379 buffer = ctx->DrawBuffer;
1380 break;
1381 default:
1382 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1383 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1384 }
1385
1386 if (buffer->Name == 0) {
1387 /* The window system / default framebuffer is always complete */
1388 return GL_FRAMEBUFFER_COMPLETE_EXT;
1389 }
1390
1391 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1392
1393 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1394 _mesa_test_framebuffer_completeness(ctx, buffer);
1395 }
1396
1397 return buffer->_Status;
1398 }
1399
1400
1401
1402 /**
1403 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1404 */
1405 static void
1406 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1407 GLenum attachment, GLenum textarget, GLuint texture,
1408 GLint level, GLint zoffset)
1409 {
1410 struct gl_renderbuffer_attachment *att;
1411 struct gl_texture_object *texObj = NULL;
1412 struct gl_framebuffer *fb;
1413 GLboolean error = GL_FALSE;
1414
1415 ASSERT_OUTSIDE_BEGIN_END(ctx);
1416
1417 switch (target) {
1418 case GL_READ_FRAMEBUFFER_EXT:
1419 error = !ctx->Extensions.EXT_framebuffer_blit;
1420 fb = ctx->ReadBuffer;
1421 break;
1422 case GL_DRAW_FRAMEBUFFER_EXT:
1423 error = !ctx->Extensions.EXT_framebuffer_blit;
1424 /* fall-through */
1425 case GL_FRAMEBUFFER_EXT:
1426 fb = ctx->DrawBuffer;
1427 break;
1428 default:
1429 error = GL_TRUE;
1430 }
1431
1432 if (error) {
1433 _mesa_error(ctx, GL_INVALID_ENUM,
1434 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1435 return;
1436 }
1437
1438 ASSERT(fb);
1439
1440 /* check framebuffer binding */
1441 if (fb->Name == 0) {
1442 _mesa_error(ctx, GL_INVALID_OPERATION,
1443 "glFramebufferTexture%sEXT", caller);
1444 return;
1445 }
1446
1447
1448 /* The textarget, level, and zoffset parameters are only validated if
1449 * texture is non-zero.
1450 */
1451 if (texture) {
1452 GLboolean err = GL_TRUE;
1453
1454 texObj = _mesa_lookup_texture(ctx, texture);
1455 if (texObj != NULL) {
1456 if (textarget == 0) {
1457 err = (texObj->Target != GL_TEXTURE_3D) &&
1458 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1459 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1460 }
1461 else {
1462 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1463 ? !IS_CUBE_FACE(textarget)
1464 : (texObj->Target != textarget);
1465 }
1466 }
1467
1468 if (err) {
1469 _mesa_error(ctx, GL_INVALID_OPERATION,
1470 "glFramebufferTexture%sEXT(texture target mismatch)",
1471 caller);
1472 return;
1473 }
1474
1475 if (texObj->Target == GL_TEXTURE_3D) {
1476 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1477 if (zoffset < 0 || zoffset >= maxSize) {
1478 _mesa_error(ctx, GL_INVALID_VALUE,
1479 "glFramebufferTexture%sEXT(zoffset)", caller);
1480 return;
1481 }
1482 }
1483 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1484 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1485 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1486 _mesa_error(ctx, GL_INVALID_VALUE,
1487 "glFramebufferTexture%sEXT(layer)", caller);
1488 return;
1489 }
1490 }
1491
1492 if ((level < 0) ||
1493 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1494 _mesa_error(ctx, GL_INVALID_VALUE,
1495 "glFramebufferTexture%sEXT(level)", caller);
1496 return;
1497 }
1498 }
1499
1500 att = _mesa_get_attachment(ctx, fb, attachment);
1501 if (att == NULL) {
1502 _mesa_error(ctx, GL_INVALID_ENUM,
1503 "glFramebufferTexture%sEXT(attachment)", caller);
1504 return;
1505 }
1506
1507 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1508
1509 _glthread_LOCK_MUTEX(fb->Mutex);
1510 if (texObj) {
1511 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1512 level, zoffset);
1513 /* Set the render-to-texture flag. We'll check this flag in
1514 * glTexImage() and friends to determine if we need to revalidate
1515 * any FBOs that might be rendering into this texture.
1516 * This flag never gets cleared since it's non-trivial to determine
1517 * when all FBOs might be done rendering to this texture. That's OK
1518 * though since it's uncommon to render to a texture then repeatedly
1519 * call glTexImage() to change images in the texture.
1520 */
1521 texObj->_RenderToTexture = GL_TRUE;
1522 }
1523 else {
1524 _mesa_remove_attachment(ctx, att);
1525 }
1526
1527 invalidate_framebuffer(fb);
1528
1529 _glthread_UNLOCK_MUTEX(fb->Mutex);
1530 }
1531
1532
1533
1534 void GLAPIENTRY
1535 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1536 GLenum textarget, GLuint texture, GLint level)
1537 {
1538 GET_CURRENT_CONTEXT(ctx);
1539
1540 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1541 _mesa_error(ctx, GL_INVALID_ENUM,
1542 "glFramebufferTexture1DEXT(textarget)");
1543 return;
1544 }
1545
1546 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1547 level, 0);
1548 }
1549
1550
1551 void GLAPIENTRY
1552 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1553 GLenum textarget, GLuint texture, GLint level)
1554 {
1555 GET_CURRENT_CONTEXT(ctx);
1556
1557 if ((texture != 0) &&
1558 (textarget != GL_TEXTURE_2D) &&
1559 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1560 (!IS_CUBE_FACE(textarget))) {
1561 _mesa_error(ctx, GL_INVALID_OPERATION,
1562 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1563 return;
1564 }
1565
1566 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1567 level, 0);
1568 }
1569
1570
1571 void GLAPIENTRY
1572 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1573 GLenum textarget, GLuint texture,
1574 GLint level, GLint zoffset)
1575 {
1576 GET_CURRENT_CONTEXT(ctx);
1577
1578 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1579 _mesa_error(ctx, GL_INVALID_ENUM,
1580 "glFramebufferTexture3DEXT(textarget)");
1581 return;
1582 }
1583
1584 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1585 level, zoffset);
1586 }
1587
1588
1589 void GLAPIENTRY
1590 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1591 GLuint texture, GLint level, GLint layer)
1592 {
1593 GET_CURRENT_CONTEXT(ctx);
1594
1595 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1596 level, layer);
1597 }
1598
1599
1600 void GLAPIENTRY
1601 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1602 GLenum renderbufferTarget,
1603 GLuint renderbuffer)
1604 {
1605 struct gl_renderbuffer_attachment *att;
1606 struct gl_framebuffer *fb;
1607 struct gl_renderbuffer *rb;
1608 GET_CURRENT_CONTEXT(ctx);
1609
1610 ASSERT_OUTSIDE_BEGIN_END(ctx);
1611
1612 switch (target) {
1613 #if FEATURE_EXT_framebuffer_blit
1614 case GL_DRAW_FRAMEBUFFER_EXT:
1615 if (!ctx->Extensions.EXT_framebuffer_blit) {
1616 _mesa_error(ctx, GL_INVALID_ENUM,
1617 "glFramebufferRenderbufferEXT(target)");
1618 return;
1619 }
1620 fb = ctx->DrawBuffer;
1621 break;
1622 case GL_READ_FRAMEBUFFER_EXT:
1623 if (!ctx->Extensions.EXT_framebuffer_blit) {
1624 _mesa_error(ctx, GL_INVALID_ENUM,
1625 "glFramebufferRenderbufferEXT(target)");
1626 return;
1627 }
1628 fb = ctx->ReadBuffer;
1629 break;
1630 #endif
1631 case GL_FRAMEBUFFER_EXT:
1632 fb = ctx->DrawBuffer;
1633 break;
1634 default:
1635 _mesa_error(ctx, GL_INVALID_ENUM,
1636 "glFramebufferRenderbufferEXT(target)");
1637 return;
1638 }
1639
1640 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1641 _mesa_error(ctx, GL_INVALID_ENUM,
1642 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1643 return;
1644 }
1645
1646 if (fb->Name == 0) {
1647 /* Can't attach new renderbuffers to a window system framebuffer */
1648 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1649 return;
1650 }
1651
1652 att = _mesa_get_attachment(ctx, fb, attachment);
1653 if (att == NULL) {
1654 _mesa_error(ctx, GL_INVALID_ENUM,
1655 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1656 _mesa_lookup_enum_by_nr(attachment));
1657 return;
1658 }
1659
1660 if (renderbuffer) {
1661 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1662 if (!rb) {
1663 _mesa_error(ctx, GL_INVALID_OPERATION,
1664 "glFramebufferRenderbufferEXT(non-existant"
1665 " renderbuffer %u)", renderbuffer);
1666 return;
1667 }
1668 }
1669 else {
1670 /* remove renderbuffer attachment */
1671 rb = NULL;
1672 }
1673
1674 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1675 /* make sure the renderbuffer is a depth/stencil format */
1676 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1677 _mesa_error(ctx, GL_INVALID_OPERATION,
1678 "glFramebufferRenderbufferEXT(renderbuffer"
1679 " is not DEPTH_STENCIL format)");
1680 return;
1681 }
1682 }
1683
1684
1685 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1686
1687 assert(ctx->Driver.FramebufferRenderbuffer);
1688 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1689
1690 /* Some subsequent GL commands may depend on the framebuffer's visual
1691 * after the binding is updated. Update visual info now.
1692 */
1693 _mesa_update_framebuffer_visual(fb);
1694 }
1695
1696
1697 void GLAPIENTRY
1698 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1699 GLenum pname, GLint *params)
1700 {
1701 const struct gl_renderbuffer_attachment *att;
1702 struct gl_framebuffer *buffer;
1703 GET_CURRENT_CONTEXT(ctx);
1704
1705 ASSERT_OUTSIDE_BEGIN_END(ctx);
1706
1707 switch (target) {
1708 #if FEATURE_EXT_framebuffer_blit
1709 case GL_DRAW_FRAMEBUFFER_EXT:
1710 if (!ctx->Extensions.EXT_framebuffer_blit) {
1711 _mesa_error(ctx, GL_INVALID_ENUM,
1712 "glGetFramebufferAttachmentParameterivEXT(target)");
1713 return;
1714 }
1715 buffer = ctx->DrawBuffer;
1716 break;
1717 case GL_READ_FRAMEBUFFER_EXT:
1718 if (!ctx->Extensions.EXT_framebuffer_blit) {
1719 _mesa_error(ctx, GL_INVALID_ENUM,
1720 "glGetFramebufferAttachmentParameterivEXT(target)");
1721 return;
1722 }
1723 buffer = ctx->ReadBuffer;
1724 break;
1725 #endif
1726 case GL_FRAMEBUFFER_EXT:
1727 buffer = ctx->DrawBuffer;
1728 break;
1729 default:
1730 _mesa_error(ctx, GL_INVALID_ENUM,
1731 "glGetFramebufferAttachmentParameterivEXT(target)");
1732 return;
1733 }
1734
1735 if (buffer->Name == 0) {
1736 _mesa_error(ctx, GL_INVALID_OPERATION,
1737 "glGetFramebufferAttachmentParameterivEXT");
1738 return;
1739 }
1740
1741 att = _mesa_get_attachment(ctx, buffer, attachment);
1742 if (att == NULL) {
1743 _mesa_error(ctx, GL_INVALID_ENUM,
1744 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1745 return;
1746 }
1747
1748 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1749 /* the depth and stencil attachments must point to the same buffer */
1750 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1751 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1752 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1753 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1754 _mesa_error(ctx, GL_INVALID_OPERATION,
1755 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1756 " attachments differ)");
1757 return;
1758 }
1759 }
1760
1761 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1762
1763 switch (pname) {
1764 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1765 *params = att->Type;
1766 return;
1767 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1768 if (att->Type == GL_RENDERBUFFER_EXT) {
1769 *params = att->Renderbuffer->Name;
1770 }
1771 else if (att->Type == GL_TEXTURE) {
1772 *params = att->Texture->Name;
1773 }
1774 else {
1775 _mesa_error(ctx, GL_INVALID_ENUM,
1776 "glGetFramebufferAttachmentParameterivEXT(pname)");
1777 }
1778 return;
1779 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1780 if (att->Type == GL_TEXTURE) {
1781 *params = att->TextureLevel;
1782 }
1783 else {
1784 _mesa_error(ctx, GL_INVALID_ENUM,
1785 "glGetFramebufferAttachmentParameterivEXT(pname)");
1786 }
1787 return;
1788 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1789 if (att->Type == GL_TEXTURE) {
1790 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1791 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1792 }
1793 else {
1794 *params = 0;
1795 }
1796 }
1797 else {
1798 _mesa_error(ctx, GL_INVALID_ENUM,
1799 "glGetFramebufferAttachmentParameterivEXT(pname)");
1800 }
1801 return;
1802 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1803 if (att->Type == GL_TEXTURE) {
1804 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1805 *params = att->Zoffset;
1806 }
1807 else {
1808 *params = 0;
1809 }
1810 }
1811 else {
1812 _mesa_error(ctx, GL_INVALID_ENUM,
1813 "glGetFramebufferAttachmentParameterivEXT(pname)");
1814 }
1815 return;
1816 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1817 if (!ctx->Extensions.ARB_framebuffer_object) {
1818 _mesa_error(ctx, GL_INVALID_ENUM,
1819 "glGetFramebufferAttachmentParameterivEXT(pname)");
1820 }
1821 else {
1822 *params = att->Renderbuffer->ColorEncoding;
1823 }
1824 return;
1825 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1826 if (!ctx->Extensions.ARB_framebuffer_object) {
1827 _mesa_error(ctx, GL_INVALID_ENUM,
1828 "glGetFramebufferAttachmentParameterivEXT(pname)");
1829 return;
1830 }
1831 else {
1832 *params = att->Renderbuffer->ComponentType;
1833 }
1834 return;
1835 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1836 if (!ctx->Extensions.ARB_framebuffer_object) {
1837 _mesa_error(ctx, GL_INVALID_ENUM,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1839 }
1840 else {
1841 *params = att->Renderbuffer->RedBits;
1842 }
1843 return;
1844 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1845 if (!ctx->Extensions.ARB_framebuffer_object) {
1846 _mesa_error(ctx, GL_INVALID_ENUM,
1847 "glGetFramebufferAttachmentParameterivEXT(pname)");
1848 }
1849 else {
1850 *params = att->Renderbuffer->GreenBits;
1851 }
1852 return;
1853 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1854 if (!ctx->Extensions.ARB_framebuffer_object) {
1855 _mesa_error(ctx, GL_INVALID_ENUM,
1856 "glGetFramebufferAttachmentParameterivEXT(pname)");
1857 }
1858 else {
1859 *params = att->Renderbuffer->BlueBits;
1860 }
1861 return;
1862 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1863 if (!ctx->Extensions.ARB_framebuffer_object) {
1864 _mesa_error(ctx, GL_INVALID_ENUM,
1865 "glGetFramebufferAttachmentParameterivEXT(pname)");
1866 }
1867 else {
1868 *params = att->Renderbuffer->AlphaBits;
1869 }
1870 return;
1871 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1872 if (!ctx->Extensions.ARB_framebuffer_object) {
1873 _mesa_error(ctx, GL_INVALID_ENUM,
1874 "glGetFramebufferAttachmentParameterivEXT(pname)");
1875 }
1876 else {
1877 *params = att->Renderbuffer->DepthBits;
1878 }
1879 return;
1880 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1881 if (!ctx->Extensions.ARB_framebuffer_object) {
1882 _mesa_error(ctx, GL_INVALID_ENUM,
1883 "glGetFramebufferAttachmentParameterivEXT(pname)");
1884 }
1885 else {
1886 *params = att->Renderbuffer->StencilBits;
1887 }
1888 return;
1889 default:
1890 _mesa_error(ctx, GL_INVALID_ENUM,
1891 "glGetFramebufferAttachmentParameterivEXT(pname)");
1892 return;
1893 }
1894 }
1895
1896
1897 void GLAPIENTRY
1898 _mesa_GenerateMipmapEXT(GLenum target)
1899 {
1900 struct gl_texture_unit *texUnit;
1901 struct gl_texture_object *texObj;
1902 GET_CURRENT_CONTEXT(ctx);
1903
1904 ASSERT_OUTSIDE_BEGIN_END(ctx);
1905 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1906
1907 switch (target) {
1908 case GL_TEXTURE_1D:
1909 case GL_TEXTURE_2D:
1910 case GL_TEXTURE_3D:
1911 case GL_TEXTURE_CUBE_MAP:
1912 /* OK, legal value */
1913 break;
1914 default:
1915 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1916 return;
1917 }
1918
1919 texUnit = _mesa_get_current_tex_unit(ctx);
1920 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1921
1922 _mesa_lock_texture(ctx, texObj);
1923 if (target == GL_TEXTURE_CUBE_MAP) {
1924 GLuint face;
1925 for (face = 0; face < 6; face++)
1926 ctx->Driver.GenerateMipmap(ctx,
1927 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1928 texObj);
1929 }
1930 else {
1931 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1932 }
1933 _mesa_unlock_texture(ctx, texObj);
1934 }
1935
1936
1937 #if FEATURE_EXT_framebuffer_blit
1938
1939 static const struct gl_renderbuffer_attachment *
1940 find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
1941 {
1942 GLuint i;
1943 for (i = 0; i < Elements(fb->Attachment); i++) {
1944 if (fb->Attachment[i].Renderbuffer == rb)
1945 return &fb->Attachment[i];
1946 }
1947 return NULL;
1948 }
1949
1950
1951
1952 /**
1953 * Blit rectangular region, optionally from one framebuffer to another.
1954 *
1955 * Note, if the src buffer is multisampled and the dest is not, this is
1956 * when the samples must be resolved to a single color.
1957 */
1958 void GLAPIENTRY
1959 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1960 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1961 GLbitfield mask, GLenum filter)
1962 {
1963 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1964 GL_DEPTH_BUFFER_BIT |
1965 GL_STENCIL_BUFFER_BIT);
1966 const struct gl_framebuffer *readFb, *drawFb;
1967 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1968 GET_CURRENT_CONTEXT(ctx);
1969
1970 ASSERT_OUTSIDE_BEGIN_END(ctx);
1971 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1972
1973 if (ctx->NewState) {
1974 _mesa_update_state(ctx);
1975 }
1976
1977 readFb = ctx->ReadBuffer;
1978 drawFb = ctx->DrawBuffer;
1979
1980 if (!readFb || !drawFb) {
1981 /* This will normally never happen but someday we may want to
1982 * support MakeCurrent() with no drawables.
1983 */
1984 return;
1985 }
1986
1987 /* check for complete framebuffers */
1988 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
1989 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1990 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1991 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1992 return;
1993 }
1994
1995 if (filter != GL_NEAREST && filter != GL_LINEAR) {
1996 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
1997 return;
1998 }
1999
2000 if (mask & ~legalMaskBits) {
2001 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2002 return;
2003 }
2004
2005 /* depth/stencil must be blitted with nearest filtering */
2006 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2007 && filter != GL_NEAREST) {
2008 _mesa_error(ctx, GL_INVALID_OPERATION,
2009 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2010 return;
2011 }
2012
2013 /* get color read/draw renderbuffers */
2014 if (mask & GL_COLOR_BUFFER_BIT) {
2015 colorReadRb = readFb->_ColorReadBuffer;
2016 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2017 }
2018 else {
2019 colorReadRb = colorDrawRb = NULL;
2020 }
2021
2022 if (mask & GL_STENCIL_BUFFER_BIT) {
2023 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2024 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2025 if (!readRb ||
2026 !drawRb ||
2027 readRb->StencilBits != drawRb->StencilBits) {
2028 _mesa_error(ctx, GL_INVALID_OPERATION,
2029 "glBlitFramebufferEXT(stencil buffer size mismatch");
2030 return;
2031 }
2032 }
2033
2034 if (mask & GL_DEPTH_BUFFER_BIT) {
2035 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2036 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2037 if (!readRb ||
2038 !drawRb ||
2039 readRb->DepthBits != drawRb->DepthBits) {
2040 _mesa_error(ctx, GL_INVALID_OPERATION,
2041 "glBlitFramebufferEXT(depth buffer size mismatch");
2042 return;
2043 }
2044 }
2045
2046 if (readFb->Visual.samples > 0 &&
2047 drawFb->Visual.samples > 0 &&
2048 readFb->Visual.samples != drawFb->Visual.samples) {
2049 _mesa_error(ctx, GL_INVALID_OPERATION,
2050 "glBlitFramebufferEXT(mismatched samples");
2051 return;
2052 }
2053
2054 /* extra checks for multisample copies... */
2055 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2056 /* src and dest region sizes must be the same */
2057 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2058 srcY1 - srcY0 != dstY1 - dstY0) {
2059 _mesa_error(ctx, GL_INVALID_OPERATION,
2060 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2061 return;
2062 }
2063
2064 /* color formats must match */
2065 if (colorReadRb &&
2066 colorDrawRb &&
2067 colorReadRb->_ActualFormat != colorDrawRb->_ActualFormat) {
2068 _mesa_error(ctx, GL_INVALID_OPERATION,
2069 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2070 return;
2071 }
2072 }
2073
2074 if (!ctx->Extensions.EXT_framebuffer_blit) {
2075 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2076 return;
2077 }
2078
2079 /* Debug code */
2080 if (DEBUG_BLIT) {
2081 _mesa_printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2082 " 0x%x, 0x%x)\n",
2083 srcX0, srcY0, srcX1, srcY1,
2084 dstX0, dstY0, dstX1, dstY1,
2085 mask, filter);
2086 if (colorReadRb) {
2087 const struct gl_renderbuffer_attachment *att;
2088
2089 att = find_attachment(readFb, colorReadRb);
2090 _mesa_printf(" Src FBO %u RB %u (%dx%d) ",
2091 readFb->Name, colorReadRb->Name,
2092 colorReadRb->Width, colorReadRb->Height);
2093 if (att && att->Texture) {
2094 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2095 att->Texture->Name,
2096 att->Texture->Target,
2097 att->TextureLevel,
2098 att->CubeMapFace);
2099 }
2100 _mesa_printf("\n");
2101
2102 att = find_attachment(drawFb, colorDrawRb);
2103 _mesa_printf(" Dst FBO %u RB %u (%dx%d) ",
2104 drawFb->Name, colorDrawRb->Name,
2105 colorDrawRb->Width, colorDrawRb->Height);
2106 if (att && att->Texture) {
2107 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2108 att->Texture->Name,
2109 att->Texture->Target,
2110 att->TextureLevel,
2111 att->CubeMapFace);
2112 }
2113 _mesa_printf("\n");
2114 }
2115 }
2116
2117 ASSERT(ctx->Driver.BlitFramebuffer);
2118 ctx->Driver.BlitFramebuffer(ctx,
2119 srcX0, srcY0, srcX1, srcY1,
2120 dstX0, dstY0, dstX1, dstY1,
2121 mask, filter);
2122 }
2123 #endif /* FEATURE_EXT_framebuffer_blit */