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