mesa: more detailed error messages in fbo code
[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
55 /**
56 * Notes:
57 *
58 * None of the GL_EXT_framebuffer_object functions are compiled into
59 * display lists.
60 */
61
62
63
64 /*
65 * When glGenRender/FramebuffersEXT() is called we insert pointers to
66 * these placeholder objects into the hash table.
67 * Later, when the object ID is first bound, we replace the placeholder
68 * with the real frame/renderbuffer.
69 */
70 static struct gl_framebuffer DummyFramebuffer;
71 static struct gl_renderbuffer DummyRenderbuffer;
72
73
74 #define IS_CUBE_FACE(TARGET) \
75 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
76 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
77
78
79 static void
80 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
81 {
82 /* no op */
83 }
84
85 static void
86 delete_dummy_framebuffer(struct gl_framebuffer *fb)
87 {
88 /* no op */
89 }
90
91
92 void
93 _mesa_init_fbobjects(GLcontext *ctx)
94 {
95 DummyFramebuffer.Delete = delete_dummy_framebuffer;
96 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
97 }
98
99
100 /**
101 * Helper routine for getting a gl_renderbuffer.
102 */
103 struct gl_renderbuffer *
104 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
105 {
106 struct gl_renderbuffer *rb;
107
108 if (id == 0)
109 return NULL;
110
111 rb = (struct gl_renderbuffer *)
112 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
113 return rb;
114 }
115
116
117 /**
118 * Helper routine for getting a gl_framebuffer.
119 */
120 struct gl_framebuffer *
121 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
122 {
123 struct gl_framebuffer *fb;
124
125 if (id == 0)
126 return NULL;
127
128 fb = (struct gl_framebuffer *)
129 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
130 return fb;
131 }
132
133
134 /**
135 * Mark the given framebuffer as invalid. This will force the
136 * test for framebuffer completeness to be done before the framebuffer
137 * is used.
138 */
139 static void
140 invalidate_framebuffer(struct gl_framebuffer *fb)
141 {
142 fb->_Status = 0; /* "indeterminate" */
143 }
144
145
146 /**
147 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
148 * gl_renderbuffer_attachment object.
149 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
150 * the depth buffer attachment point.
151 */
152 struct gl_renderbuffer_attachment *
153 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
154 GLenum attachment)
155 {
156 GLuint i;
157
158 switch (attachment) {
159 case GL_COLOR_ATTACHMENT0_EXT:
160 case GL_COLOR_ATTACHMENT1_EXT:
161 case GL_COLOR_ATTACHMENT2_EXT:
162 case GL_COLOR_ATTACHMENT3_EXT:
163 case GL_COLOR_ATTACHMENT4_EXT:
164 case GL_COLOR_ATTACHMENT5_EXT:
165 case GL_COLOR_ATTACHMENT6_EXT:
166 case GL_COLOR_ATTACHMENT7_EXT:
167 case GL_COLOR_ATTACHMENT8_EXT:
168 case GL_COLOR_ATTACHMENT9_EXT:
169 case GL_COLOR_ATTACHMENT10_EXT:
170 case GL_COLOR_ATTACHMENT11_EXT:
171 case GL_COLOR_ATTACHMENT12_EXT:
172 case GL_COLOR_ATTACHMENT13_EXT:
173 case GL_COLOR_ATTACHMENT14_EXT:
174 case GL_COLOR_ATTACHMENT15_EXT:
175 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
176 if (i >= ctx->Const.MaxColorAttachments) {
177 return NULL;
178 }
179 return &fb->Attachment[BUFFER_COLOR0 + i];
180 case GL_DEPTH_STENCIL_ATTACHMENT:
181 /* fall-through */
182 case GL_DEPTH_ATTACHMENT_EXT:
183 return &fb->Attachment[BUFFER_DEPTH];
184 case GL_STENCIL_ATTACHMENT_EXT:
185 return &fb->Attachment[BUFFER_STENCIL];
186 default:
187 return NULL;
188 }
189 }
190
191
192 /**
193 * Remove any texture or renderbuffer attached to the given attachment
194 * point. Update reference counts, etc.
195 */
196 void
197 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
198 {
199 if (att->Type == GL_TEXTURE) {
200 ASSERT(att->Texture);
201 if (ctx->Driver.FinishRenderTexture) {
202 /* tell driver that we're done rendering to this texture. */
203 ctx->Driver.FinishRenderTexture(ctx, att);
204 }
205 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
206 ASSERT(!att->Texture);
207 }
208 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
209 ASSERT(!att->Texture);
210 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
211 ASSERT(!att->Renderbuffer);
212 }
213 att->Type = GL_NONE;
214 att->Complete = GL_TRUE;
215 }
216
217
218 /**
219 * Bind a texture object to an attachment point.
220 * The previous binding, if any, will be removed first.
221 */
222 void
223 _mesa_set_texture_attachment(GLcontext *ctx,
224 struct gl_framebuffer *fb,
225 struct gl_renderbuffer_attachment *att,
226 struct gl_texture_object *texObj,
227 GLenum texTarget, GLuint level, GLuint zoffset)
228 {
229 if (att->Texture == texObj) {
230 /* re-attaching same texture */
231 ASSERT(att->Type == GL_TEXTURE);
232 }
233 else {
234 /* new attachment */
235 _mesa_remove_attachment(ctx, att);
236 att->Type = GL_TEXTURE;
237 assert(!att->Texture);
238 _mesa_reference_texobj(&att->Texture, texObj);
239 }
240
241 /* always update these fields */
242 att->TextureLevel = level;
243 if (IS_CUBE_FACE(texTarget)) {
244 att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
245 }
246 else {
247 att->CubeMapFace = 0;
248 }
249 att->Zoffset = zoffset;
250 att->Complete = GL_FALSE;
251
252 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
253 ctx->Driver.RenderTexture(ctx, fb, att);
254 }
255
256 invalidate_framebuffer(fb);
257 }
258
259
260 /**
261 * Bind a renderbuffer to an attachment point.
262 * The previous binding, if any, will be removed first.
263 */
264 void
265 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
266 struct gl_renderbuffer_attachment *att,
267 struct gl_renderbuffer *rb)
268 {
269 /* XXX check if re-doing same attachment, exit early */
270 _mesa_remove_attachment(ctx, att);
271 att->Type = GL_RENDERBUFFER_EXT;
272 att->Texture = NULL; /* just to be safe */
273 att->Complete = GL_FALSE;
274 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
275 }
276
277
278 /**
279 * Fallback for ctx->Driver.FramebufferRenderbuffer()
280 * Attach a renderbuffer object to a framebuffer object.
281 */
282 void
283 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
284 GLenum attachment, struct gl_renderbuffer *rb)
285 {
286 struct gl_renderbuffer_attachment *att;
287
288 _glthread_LOCK_MUTEX(fb->Mutex);
289
290 att = _mesa_get_attachment(ctx, fb, attachment);
291 ASSERT(att);
292 if (rb) {
293 _mesa_set_renderbuffer_attachment(ctx, att, rb);
294 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
295 /* do stencil attachment here (depth already done above) */
296 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
297 assert(att);
298 _mesa_set_renderbuffer_attachment(ctx, att, rb);
299 }
300 }
301 else {
302 _mesa_remove_attachment(ctx, att);
303 }
304
305 invalidate_framebuffer(fb);
306
307 _glthread_UNLOCK_MUTEX(fb->Mutex);
308 }
309
310
311 /**
312 * For debug only.
313 */
314 static void
315 att_incomplete(const char *msg)
316 {
317 #if DEBUG_FBO
318 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
319 #else
320 (void) msg;
321 #endif
322 }
323
324
325 /**
326 * For debug only.
327 */
328 static void
329 fbo_incomplete(const char *msg, int index)
330 {
331 #if DEBUG_FBO
332 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
333 #else
334 (void) msg;
335 (void) index;
336 #endif
337 }
338
339
340
341
342 /**
343 * Test if an attachment point is complete and update its Complete field.
344 * \param format if GL_COLOR, this is a color attachment point,
345 * if GL_DEPTH, this is a depth component attachment point,
346 * if GL_STENCIL, this is a stencil component attachment point.
347 */
348 static void
349 test_attachment_completeness(const GLcontext *ctx, GLenum format,
350 struct gl_renderbuffer_attachment *att)
351 {
352 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
353
354 /* assume complete */
355 att->Complete = GL_TRUE;
356
357 /* Look for reasons why the attachment might be incomplete */
358 if (att->Type == GL_TEXTURE) {
359 const struct gl_texture_object *texObj = att->Texture;
360 struct gl_texture_image *texImage;
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 if (format == GL_COLOR) {
388 if (texImage->TexFormat->BaseFormat != GL_RGB &&
389 texImage->TexFormat->BaseFormat != GL_RGBA) {
390 att_incomplete("bad format");
391 att->Complete = GL_FALSE;
392 return;
393 }
394 if (texImage->TexFormat->TexelBytes == 0) {
395 att_incomplete("compressed internalformat");
396 att->Complete = GL_FALSE;
397 return;
398 }
399 }
400 else if (format == GL_DEPTH) {
401 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
402 /* OK */
403 }
404 else if (ctx->Extensions.EXT_packed_depth_stencil &&
405 ctx->Extensions.ARB_depth_texture &&
406 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
407 /* OK */
408 }
409 else {
410 att->Complete = GL_FALSE;
411 att_incomplete("bad depth format");
412 return;
413 }
414 }
415 else {
416 ASSERT(format == GL_STENCIL);
417 ASSERT(att->Renderbuffer->StencilBits);
418 if (ctx->Extensions.EXT_packed_depth_stencil &&
419 ctx->Extensions.ARB_depth_texture &&
420 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
421 /* OK */
422 }
423 else {
424 /* no such thing as stencil-only textures */
425 att_incomplete("illegal stencil texture");
426 att->Complete = GL_FALSE;
427 return;
428 }
429 }
430 }
431 else if (att->Type == GL_RENDERBUFFER_EXT) {
432 ASSERT(att->Renderbuffer);
433 if (!att->Renderbuffer->InternalFormat ||
434 att->Renderbuffer->Width < 1 ||
435 att->Renderbuffer->Height < 1) {
436 att_incomplete("0x0 renderbuffer");
437 att->Complete = GL_FALSE;
438 return;
439 }
440 if (format == GL_COLOR) {
441 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
442 att->Renderbuffer->_BaseFormat != GL_RGBA) {
443 att_incomplete("bad renderbuffer color format");
444 att->Complete = GL_FALSE;
445 return;
446 }
447 ASSERT(att->Renderbuffer->RedBits);
448 ASSERT(att->Renderbuffer->GreenBits);
449 ASSERT(att->Renderbuffer->BlueBits);
450 }
451 else if (format == GL_DEPTH) {
452 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
453 ASSERT(att->Renderbuffer->DepthBits);
454 /* OK */
455 }
456 else if (ctx->Extensions.EXT_packed_depth_stencil &&
457 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
458 ASSERT(att->Renderbuffer->DepthBits);
459 /* OK */
460 }
461 else {
462 att_incomplete("bad renderbuffer depth format");
463 att->Complete = GL_FALSE;
464 return;
465 }
466 }
467 else {
468 assert(format == GL_STENCIL);
469 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
470 ASSERT(att->Renderbuffer->StencilBits);
471 /* OK */
472 }
473 else if (ctx->Extensions.EXT_packed_depth_stencil &&
474 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
475 ASSERT(att->Renderbuffer->StencilBits);
476 /* OK */
477 }
478 else {
479 att->Complete = GL_FALSE;
480 att_incomplete("bad renderbuffer stencil format");
481 return;
482 }
483 }
484 }
485 else {
486 ASSERT(att->Type == GL_NONE);
487 /* complete */
488 return;
489 }
490 }
491
492
493 /**
494 * Test if the given framebuffer object is complete and update its
495 * Status field with the results.
496 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
497 * driver to make hardware-specific validation/completeness checks.
498 * Also update the framebuffer's Width and Height fields if the
499 * framebuffer is complete.
500 */
501 void
502 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
503 {
504 GLuint numImages;
505 GLenum intFormat = GL_NONE; /* color buffers' internal format */
506 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
507 GLint numSamples = -1;
508 GLint i;
509 GLuint j;
510
511 assert(fb->Name != 0);
512
513 numImages = 0;
514 fb->Width = 0;
515 fb->Height = 0;
516
517 /* Start at -2 to more easily loop over all attachment points.
518 * -2: depth buffer
519 * -1: stencil buffer
520 * >=0: color buffer
521 */
522 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
523 struct gl_renderbuffer_attachment *att;
524 GLenum f;
525
526 /*
527 * XXX for ARB_fbo, only check color buffers that are named by
528 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
529 */
530
531 /* check for attachment completeness
532 */
533 if (i == -2) {
534 att = &fb->Attachment[BUFFER_DEPTH];
535 test_attachment_completeness(ctx, GL_DEPTH, att);
536 if (!att->Complete) {
537 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
538 fbo_incomplete("depth attachment incomplete", -1);
539 return;
540 }
541 }
542 else if (i == -1) {
543 att = &fb->Attachment[BUFFER_STENCIL];
544 test_attachment_completeness(ctx, GL_STENCIL, att);
545 if (!att->Complete) {
546 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
547 fbo_incomplete("stencil attachment incomplete", -1);
548 return;
549 }
550 }
551 else {
552 att = &fb->Attachment[BUFFER_COLOR0 + i];
553 test_attachment_completeness(ctx, GL_COLOR, att);
554 if (!att->Complete) {
555 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
556 fbo_incomplete("color attachment incomplete", i);
557 return;
558 }
559 }
560
561 /* get width, height, format of the renderbuffer/texture
562 */
563 if (att->Type == GL_TEXTURE) {
564 const struct gl_texture_image *texImg
565 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
566 minWidth = MIN2(minWidth, texImg->Width);
567 maxWidth = MAX2(maxWidth, texImg->Width);
568 minHeight = MIN2(minHeight, texImg->Height);
569 maxHeight = MAX2(maxHeight, texImg->Height);
570 f = texImg->_BaseFormat;
571 numImages++;
572 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
573 && f != GL_DEPTH_STENCIL_EXT) {
574 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
575 fbo_incomplete("texture attachment incomplete", -1);
576 return;
577 }
578 }
579 else if (att->Type == GL_RENDERBUFFER_EXT) {
580 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
581 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
582 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
583 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
584 f = att->Renderbuffer->InternalFormat;
585 numImages++;
586 }
587 else {
588 assert(att->Type == GL_NONE);
589 continue;
590 }
591
592 if (numSamples < 0) {
593 /* first buffer */
594 numSamples = att->Renderbuffer->NumSamples;
595 }
596
597 /* Error-check width, height, format, samples
598 */
599 if (numImages == 1) {
600 /* save format, num samples */
601 if (i >= 0) {
602 intFormat = f;
603 }
604 }
605 else {
606 if (!ctx->Extensions.ARB_framebuffer_object) {
607 /* check that width, height, format are same */
608 if (minWidth != maxWidth || minHeight != maxHeight) {
609 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
610 fbo_incomplete("width or height mismatch", -1);
611 return;
612 }
613 /* check that all color buffer have same format */
614 if (intFormat != GL_NONE && f != intFormat) {
615 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
616 fbo_incomplete("format mismatch", -1);
617 return;
618 }
619 }
620 if (att->Renderbuffer &&
621 att->Renderbuffer->NumSamples != numSamples) {
622 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
623 fbo_incomplete("inconsistant number of samples", i);
624 return;
625 }
626
627 }
628 }
629
630 #ifndef FEATURE_OES_framebuffer_object
631 /* Check that all DrawBuffers are present */
632 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
633 if (fb->ColorDrawBuffer[j] != GL_NONE) {
634 const struct gl_renderbuffer_attachment *att
635 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
636 assert(att);
637 if (att->Type == GL_NONE) {
638 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
639 fbo_incomplete("missing drawbuffer", j);
640 return;
641 }
642 }
643 }
644
645 /* Check that the ReadBuffer is present */
646 if (fb->ColorReadBuffer != GL_NONE) {
647 const struct gl_renderbuffer_attachment *att
648 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
649 assert(att);
650 if (att->Type == GL_NONE) {
651 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
652 fbo_incomplete("missing readbuffer", -1);
653 return;
654 }
655 }
656 #endif
657
658 if (numImages == 0) {
659 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
660 fbo_incomplete("no attachments", -1);
661 return;
662 }
663
664 /* Provisionally set status = COMPLETE ... */
665 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
666
667 /* ... but the driver may say the FB is incomplete.
668 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
669 * if anything.
670 */
671 if (ctx->Driver.ValidateFramebuffer) {
672 ctx->Driver.ValidateFramebuffer(ctx, fb);
673 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
674 fbo_incomplete("driver marked FBO as incomplete", -1);
675 }
676 }
677
678 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
679 /*
680 * Note that if ARB_framebuffer_object is supported and the attached
681 * renderbuffers/textures are different sizes, the framebuffer
682 * width/height will be set to the smallest width/height.
683 */
684 fb->Width = minWidth;
685 fb->Height = minHeight;
686
687 /* finally, update the visual info for the framebuffer */
688 _mesa_update_framebuffer_visual(fb);
689 }
690 }
691
692
693 GLboolean GLAPIENTRY
694 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
695 {
696 GET_CURRENT_CONTEXT(ctx);
697 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
698 if (renderbuffer) {
699 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
700 if (rb != NULL && rb != &DummyRenderbuffer)
701 return GL_TRUE;
702 }
703 return GL_FALSE;
704 }
705
706
707 void GLAPIENTRY
708 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
709 {
710 struct gl_renderbuffer *newRb;
711 GET_CURRENT_CONTEXT(ctx);
712
713 ASSERT_OUTSIDE_BEGIN_END(ctx);
714
715 if (target != GL_RENDERBUFFER_EXT) {
716 _mesa_error(ctx, GL_INVALID_ENUM,
717 "glBindRenderbufferEXT(target)");
718 return;
719 }
720
721 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
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->_ActualFormat = 0;
976 rb->RedBits =
977 rb->GreenBits =
978 rb->BlueBits =
979 rb->AlphaBits =
980 rb->IndexBits =
981 rb->DepthBits =
982 rb->StencilBits = 0;
983 rb->NumSamples = samples;
984
985 /* Now allocate the storage */
986 ASSERT(rb->AllocStorage);
987 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
988 /* No error - check/set fields now */
989 assert(rb->_ActualFormat);
990 assert(rb->Width == (GLuint) width);
991 assert(rb->Height == (GLuint) height);
992 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
993 rb->DepthBits || rb->StencilBits || rb->IndexBits);
994 rb->InternalFormat = internalFormat;
995 rb->_BaseFormat = baseFormat;
996 }
997 else {
998 /* Probably ran out of memory - clear the fields */
999 rb->Width = 0;
1000 rb->Height = 0;
1001 rb->InternalFormat = GL_NONE;
1002 rb->_ActualFormat = GL_NONE;
1003 rb->_BaseFormat = GL_NONE;
1004 rb->RedBits =
1005 rb->GreenBits =
1006 rb->BlueBits =
1007 rb->AlphaBits =
1008 rb->IndexBits =
1009 rb->DepthBits =
1010 rb->StencilBits =
1011 rb->NumSamples = 0;
1012 }
1013
1014 /*
1015 test_framebuffer_completeness(ctx, fb);
1016 */
1017 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1018 * points???
1019 */
1020 }
1021
1022
1023 void GLAPIENTRY
1024 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1025 GLsizei width, GLsizei height)
1026 {
1027 /* GL_ARB_fbo says calling this function is equivalent to calling
1028 * glRenderbufferStorageMultisample() with samples=0. We pass in
1029 * a token value here just for error reporting purposes.
1030 */
1031 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1032 }
1033
1034
1035 void GLAPIENTRY
1036 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1037 GLenum internalFormat,
1038 GLsizei width, GLsizei height)
1039 {
1040 renderbuffer_storage(target, internalFormat, width, height, samples);
1041 }
1042
1043
1044
1045 void GLAPIENTRY
1046 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1047 {
1048 struct gl_renderbuffer *rb;
1049 GET_CURRENT_CONTEXT(ctx);
1050
1051 ASSERT_OUTSIDE_BEGIN_END(ctx);
1052
1053 if (target != GL_RENDERBUFFER_EXT) {
1054 _mesa_error(ctx, GL_INVALID_ENUM,
1055 "glGetRenderbufferParameterivEXT(target)");
1056 return;
1057 }
1058
1059 rb = ctx->CurrentRenderbuffer;
1060 if (!rb) {
1061 _mesa_error(ctx, GL_INVALID_OPERATION,
1062 "glGetRenderbufferParameterivEXT");
1063 return;
1064 }
1065
1066 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1067
1068 switch (pname) {
1069 case GL_RENDERBUFFER_WIDTH_EXT:
1070 *params = rb->Width;
1071 return;
1072 case GL_RENDERBUFFER_HEIGHT_EXT:
1073 *params = rb->Height;
1074 return;
1075 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1076 *params = rb->InternalFormat;
1077 return;
1078 case GL_RENDERBUFFER_RED_SIZE_EXT:
1079 *params = rb->RedBits;
1080 break;
1081 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1082 *params = rb->GreenBits;
1083 break;
1084 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1085 *params = rb->BlueBits;
1086 break;
1087 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1088 *params = rb->AlphaBits;
1089 break;
1090 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1091 *params = rb->DepthBits;
1092 break;
1093 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1094 *params = rb->StencilBits;
1095 break;
1096 case GL_RENDERBUFFER_SAMPLES:
1097 if (ctx->Extensions.ARB_framebuffer_object) {
1098 *params = rb->NumSamples;
1099 break;
1100 }
1101 /* fallthrough */
1102 default:
1103 _mesa_error(ctx, GL_INVALID_ENUM,
1104 "glGetRenderbufferParameterivEXT(target)");
1105 return;
1106 }
1107 }
1108
1109
1110 GLboolean GLAPIENTRY
1111 _mesa_IsFramebufferEXT(GLuint framebuffer)
1112 {
1113 GET_CURRENT_CONTEXT(ctx);
1114 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1115 if (framebuffer) {
1116 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1117 if (rb != NULL && rb != &DummyFramebuffer)
1118 return GL_TRUE;
1119 }
1120 return GL_FALSE;
1121 }
1122
1123
1124 static void
1125 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1126 {
1127 GLuint i;
1128 ASSERT(ctx->Driver.RenderTexture);
1129 for (i = 0; i < BUFFER_COUNT; i++) {
1130 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1131 struct gl_texture_object *texObj = att->Texture;
1132 if (texObj
1133 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1134 ctx->Driver.RenderTexture(ctx, fb, att);
1135 }
1136 }
1137 }
1138
1139
1140 /**
1141 * Examine all the framebuffer's attachments to see if any are textures.
1142 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1143 * notify the device driver that the texture image may have changed.
1144 */
1145 static void
1146 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1147 {
1148 if (ctx->Driver.FinishRenderTexture) {
1149 GLuint i;
1150 for (i = 0; i < BUFFER_COUNT; i++) {
1151 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1152 if (att->Texture && att->Renderbuffer) {
1153 ctx->Driver.FinishRenderTexture(ctx, att);
1154 }
1155 }
1156 }
1157 }
1158
1159
1160 void GLAPIENTRY
1161 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1162 {
1163 struct gl_framebuffer *newFb, *newFbread;
1164 GLboolean bindReadBuf, bindDrawBuf;
1165 GET_CURRENT_CONTEXT(ctx);
1166
1167 #ifdef DEBUG
1168 if (ctx->Extensions.ARB_framebuffer_object) {
1169 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1170 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1171 }
1172 #endif
1173
1174 ASSERT_OUTSIDE_BEGIN_END(ctx);
1175
1176 if (!ctx->Extensions.EXT_framebuffer_object) {
1177 _mesa_error(ctx, GL_INVALID_OPERATION,
1178 "glBindFramebufferEXT(unsupported)");
1179 return;
1180 }
1181
1182 switch (target) {
1183 #if FEATURE_EXT_framebuffer_blit
1184 case GL_DRAW_FRAMEBUFFER_EXT:
1185 if (!ctx->Extensions.EXT_framebuffer_blit) {
1186 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1187 return;
1188 }
1189 bindDrawBuf = GL_TRUE;
1190 bindReadBuf = GL_FALSE;
1191 break;
1192 case GL_READ_FRAMEBUFFER_EXT:
1193 if (!ctx->Extensions.EXT_framebuffer_blit) {
1194 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1195 return;
1196 }
1197 bindDrawBuf = GL_FALSE;
1198 bindReadBuf = GL_TRUE;
1199 break;
1200 #endif
1201 case GL_FRAMEBUFFER_EXT:
1202 bindDrawBuf = GL_TRUE;
1203 bindReadBuf = GL_TRUE;
1204 break;
1205 default:
1206 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1207 return;
1208 }
1209
1210 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1211
1212 if (framebuffer) {
1213 /* Binding a user-created framebuffer object */
1214 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1215 if (newFb == &DummyFramebuffer) {
1216 /* ID was reserved, but no real framebuffer object made yet */
1217 newFb = NULL;
1218 }
1219 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1220 /* All FBO IDs must be Gen'd */
1221 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1222 return;
1223 }
1224
1225 if (!newFb) {
1226 /* create new framebuffer object */
1227 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1228 if (!newFb) {
1229 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1230 return;
1231 }
1232 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1233 }
1234 newFbread = newFb;
1235 }
1236 else {
1237 /* Binding the window system framebuffer (which was originally set
1238 * with MakeCurrent).
1239 */
1240 newFb = ctx->WinSysDrawBuffer;
1241 newFbread = ctx->WinSysReadBuffer;
1242 }
1243
1244 ASSERT(newFb);
1245 ASSERT(newFb != &DummyFramebuffer);
1246
1247 /*
1248 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1249 */
1250
1251 if (bindReadBuf) {
1252 if (ctx->ReadBuffer == newFbread)
1253 bindReadBuf = GL_FALSE; /* no change */
1254 else
1255 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1256 }
1257
1258 if (bindDrawBuf) {
1259 /* check if old FB had any texture attachments */
1260 if (ctx->DrawBuffer->Name != 0) {
1261 check_end_texture_render(ctx, ctx->DrawBuffer);
1262 }
1263
1264 if (ctx->DrawBuffer == newFb)
1265 bindDrawBuf = GL_FALSE; /* no change */
1266 else
1267 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1268
1269 if (newFb->Name != 0) {
1270 /* check if newly bound framebuffer has any texture attachments */
1271 check_begin_texture_render(ctx, newFb);
1272 }
1273 }
1274
1275 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1276 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1277 }
1278 }
1279
1280
1281 void GLAPIENTRY
1282 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1283 {
1284 GLint i;
1285 GET_CURRENT_CONTEXT(ctx);
1286
1287 ASSERT_OUTSIDE_BEGIN_END(ctx);
1288 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1289
1290 for (i = 0; i < n; i++) {
1291 if (framebuffers[i] > 0) {
1292 struct gl_framebuffer *fb;
1293 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1294 if (fb) {
1295 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1296
1297 /* check if deleting currently bound framebuffer object */
1298 if (fb == ctx->DrawBuffer) {
1299 /* bind default */
1300 ASSERT(fb->RefCount >= 2);
1301 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1302 }
1303 if (fb == ctx->ReadBuffer) {
1304 /* bind default */
1305 ASSERT(fb->RefCount >= 2);
1306 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1307 }
1308
1309 /* remove from hash table immediately, to free the ID */
1310 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1311
1312 if (fb != &DummyFramebuffer) {
1313 /* But the object will not be freed until it's no longer
1314 * bound in any context.
1315 */
1316 _mesa_reference_framebuffer(&fb, NULL);
1317 }
1318 }
1319 }
1320 }
1321 }
1322
1323
1324 void GLAPIENTRY
1325 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1326 {
1327 GET_CURRENT_CONTEXT(ctx);
1328 GLuint first;
1329 GLint i;
1330
1331 ASSERT_OUTSIDE_BEGIN_END(ctx);
1332
1333 if (n < 0) {
1334 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1335 return;
1336 }
1337
1338 if (!framebuffers)
1339 return;
1340
1341 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1342
1343 for (i = 0; i < n; i++) {
1344 GLuint name = first + i;
1345 framebuffers[i] = name;
1346 /* insert dummy placeholder into hash table */
1347 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1348 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1349 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1350 }
1351 }
1352
1353
1354
1355 GLenum GLAPIENTRY
1356 _mesa_CheckFramebufferStatusEXT(GLenum target)
1357 {
1358 struct gl_framebuffer *buffer;
1359 GET_CURRENT_CONTEXT(ctx);
1360
1361 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1362
1363 switch (target) {
1364 #if FEATURE_EXT_framebuffer_blit
1365 case GL_DRAW_FRAMEBUFFER_EXT:
1366 if (!ctx->Extensions.EXT_framebuffer_blit) {
1367 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1368 return 0;
1369 }
1370 buffer = ctx->DrawBuffer;
1371 break;
1372 case GL_READ_FRAMEBUFFER_EXT:
1373 if (!ctx->Extensions.EXT_framebuffer_blit) {
1374 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1375 return 0;
1376 }
1377 buffer = ctx->ReadBuffer;
1378 break;
1379 #endif
1380 case GL_FRAMEBUFFER_EXT:
1381 buffer = ctx->DrawBuffer;
1382 break;
1383 default:
1384 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1385 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1386 }
1387
1388 if (buffer->Name == 0) {
1389 /* The window system / default framebuffer is always complete */
1390 return GL_FRAMEBUFFER_COMPLETE_EXT;
1391 }
1392
1393 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1394
1395 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1396 _mesa_test_framebuffer_completeness(ctx, buffer);
1397 }
1398
1399 return buffer->_Status;
1400 }
1401
1402
1403
1404 /**
1405 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1406 */
1407 static void
1408 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1409 GLenum attachment, GLenum textarget, GLuint texture,
1410 GLint level, GLint zoffset)
1411 {
1412 struct gl_renderbuffer_attachment *att;
1413 struct gl_texture_object *texObj = NULL;
1414 struct gl_framebuffer *fb;
1415 GLboolean error = GL_FALSE;
1416
1417 ASSERT_OUTSIDE_BEGIN_END(ctx);
1418
1419 switch (target) {
1420 case GL_READ_FRAMEBUFFER_EXT:
1421 error = !ctx->Extensions.EXT_framebuffer_blit;
1422 fb = ctx->ReadBuffer;
1423 break;
1424 case GL_DRAW_FRAMEBUFFER_EXT:
1425 error = !ctx->Extensions.EXT_framebuffer_blit;
1426 /* fall-through */
1427 case GL_FRAMEBUFFER_EXT:
1428 fb = ctx->DrawBuffer;
1429 break;
1430 default:
1431 error = GL_TRUE;
1432 }
1433
1434 if (error) {
1435 _mesa_error(ctx, GL_INVALID_ENUM,
1436 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1437 return;
1438 }
1439
1440 ASSERT(fb);
1441
1442 /* check framebuffer binding */
1443 if (fb->Name == 0) {
1444 _mesa_error(ctx, GL_INVALID_OPERATION,
1445 "glFramebufferTexture%sEXT", caller);
1446 return;
1447 }
1448
1449
1450 /* The textarget, level, and zoffset parameters are only validated if
1451 * texture is non-zero.
1452 */
1453 if (texture) {
1454 GLboolean err = GL_TRUE;
1455
1456 texObj = _mesa_lookup_texture(ctx, texture);
1457 if (texObj != NULL) {
1458 if (textarget == 0) {
1459 err = (texObj->Target != GL_TEXTURE_3D) &&
1460 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1461 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1462 }
1463 else {
1464 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1465 ? !IS_CUBE_FACE(textarget)
1466 : (texObj->Target != textarget);
1467 }
1468 }
1469
1470 if (err) {
1471 _mesa_error(ctx, GL_INVALID_OPERATION,
1472 "glFramebufferTexture%sEXT(texture target mismatch)",
1473 caller);
1474 return;
1475 }
1476
1477 if (texObj->Target == GL_TEXTURE_3D) {
1478 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1479 if (zoffset < 0 || zoffset >= maxSize) {
1480 _mesa_error(ctx, GL_INVALID_VALUE,
1481 "glFramebufferTexture%sEXT(zoffset)", caller);
1482 return;
1483 }
1484 }
1485 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1486 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1487 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1488 _mesa_error(ctx, GL_INVALID_VALUE,
1489 "glFramebufferTexture%sEXT(layer)", caller);
1490 return;
1491 }
1492 }
1493
1494 if ((level < 0) ||
1495 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1496 _mesa_error(ctx, GL_INVALID_VALUE,
1497 "glFramebufferTexture%sEXT(level)", caller);
1498 return;
1499 }
1500 }
1501
1502 att = _mesa_get_attachment(ctx, fb, attachment);
1503 if (att == NULL) {
1504 _mesa_error(ctx, GL_INVALID_ENUM,
1505 "glFramebufferTexture%sEXT(attachment)", caller);
1506 return;
1507 }
1508
1509 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1510 /* the texture format must be depth+stencil */
1511 const struct gl_texture_image *texImg;
1512 texImg = texObj->Image[0][texObj->BaseLevel];
1513 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1514 _mesa_error(ctx, GL_INVALID_OPERATION,
1515 "glFramebufferTexture%sEXT(texture is not"
1516 " DEPTH_STENCIL format)", caller);
1517 return;
1518 }
1519 }
1520
1521 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1522
1523 _glthread_LOCK_MUTEX(fb->Mutex);
1524 if (texObj) {
1525 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1526 level, zoffset);
1527 /* Set the render-to-texture flag. We'll check this flag in
1528 * glTexImage() and friends to determine if we need to revalidate
1529 * any FBOs that might be rendering into this texture.
1530 * This flag never gets cleared since it's non-trivial to determine
1531 * when all FBOs might be done rendering to this texture. That's OK
1532 * though since it's uncommon to render to a texture then repeatedly
1533 * call glTexImage() to change images in the texture.
1534 */
1535 texObj->_RenderToTexture = GL_TRUE;
1536 }
1537 else {
1538 _mesa_remove_attachment(ctx, att);
1539 }
1540
1541 invalidate_framebuffer(fb);
1542
1543 _glthread_UNLOCK_MUTEX(fb->Mutex);
1544 }
1545
1546
1547
1548 void GLAPIENTRY
1549 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1550 GLenum textarget, GLuint texture, GLint level)
1551 {
1552 GET_CURRENT_CONTEXT(ctx);
1553
1554 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1555 _mesa_error(ctx, GL_INVALID_ENUM,
1556 "glFramebufferTexture1DEXT(textarget)");
1557 return;
1558 }
1559
1560 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1561 level, 0);
1562 }
1563
1564
1565 void GLAPIENTRY
1566 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1567 GLenum textarget, GLuint texture, GLint level)
1568 {
1569 GET_CURRENT_CONTEXT(ctx);
1570
1571 if ((texture != 0) &&
1572 (textarget != GL_TEXTURE_2D) &&
1573 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1574 (!IS_CUBE_FACE(textarget))) {
1575 _mesa_error(ctx, GL_INVALID_OPERATION,
1576 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1577 return;
1578 }
1579
1580 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1581 level, 0);
1582 }
1583
1584
1585 void GLAPIENTRY
1586 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1587 GLenum textarget, GLuint texture,
1588 GLint level, GLint zoffset)
1589 {
1590 GET_CURRENT_CONTEXT(ctx);
1591
1592 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1593 _mesa_error(ctx, GL_INVALID_ENUM,
1594 "glFramebufferTexture3DEXT(textarget)");
1595 return;
1596 }
1597
1598 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1599 level, zoffset);
1600 }
1601
1602
1603 void GLAPIENTRY
1604 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1605 GLuint texture, GLint level, GLint layer)
1606 {
1607 GET_CURRENT_CONTEXT(ctx);
1608
1609 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1610 level, layer);
1611 }
1612
1613
1614 void GLAPIENTRY
1615 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1616 GLenum renderbufferTarget,
1617 GLuint renderbuffer)
1618 {
1619 struct gl_renderbuffer_attachment *att;
1620 struct gl_framebuffer *fb;
1621 struct gl_renderbuffer *rb;
1622 GET_CURRENT_CONTEXT(ctx);
1623
1624 ASSERT_OUTSIDE_BEGIN_END(ctx);
1625
1626 switch (target) {
1627 #if FEATURE_EXT_framebuffer_blit
1628 case GL_DRAW_FRAMEBUFFER_EXT:
1629 if (!ctx->Extensions.EXT_framebuffer_blit) {
1630 _mesa_error(ctx, GL_INVALID_ENUM,
1631 "glFramebufferRenderbufferEXT(target)");
1632 return;
1633 }
1634 fb = ctx->DrawBuffer;
1635 break;
1636 case GL_READ_FRAMEBUFFER_EXT:
1637 if (!ctx->Extensions.EXT_framebuffer_blit) {
1638 _mesa_error(ctx, GL_INVALID_ENUM,
1639 "glFramebufferRenderbufferEXT(target)");
1640 return;
1641 }
1642 fb = ctx->ReadBuffer;
1643 break;
1644 #endif
1645 case GL_FRAMEBUFFER_EXT:
1646 fb = ctx->DrawBuffer;
1647 break;
1648 default:
1649 _mesa_error(ctx, GL_INVALID_ENUM,
1650 "glFramebufferRenderbufferEXT(target)");
1651 return;
1652 }
1653
1654 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1655 _mesa_error(ctx, GL_INVALID_ENUM,
1656 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1657 return;
1658 }
1659
1660 if (fb->Name == 0) {
1661 /* Can't attach new renderbuffers to a window system framebuffer */
1662 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1663 return;
1664 }
1665
1666 att = _mesa_get_attachment(ctx, fb, attachment);
1667 if (att == NULL) {
1668 _mesa_error(ctx, GL_INVALID_ENUM,
1669 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1670 _mesa_lookup_enum_by_nr(attachment));
1671 return;
1672 }
1673
1674 if (renderbuffer) {
1675 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1676 if (!rb) {
1677 _mesa_error(ctx, GL_INVALID_OPERATION,
1678 "glFramebufferRenderbufferEXT(non-existant"
1679 " renderbuffer %u)", renderbuffer);
1680 return;
1681 }
1682 }
1683 else {
1684 /* remove renderbuffer attachment */
1685 rb = NULL;
1686 }
1687
1688 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1689 /* make sure the renderbuffer is a depth/stencil format */
1690 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1691 _mesa_error(ctx, GL_INVALID_OPERATION,
1692 "glFramebufferRenderbufferEXT(renderbuffer"
1693 " is not DEPTH_STENCIL format)");
1694 return;
1695 }
1696 }
1697
1698
1699 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1700
1701 assert(ctx->Driver.FramebufferRenderbuffer);
1702 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1703
1704 /* Some subsequent GL commands may depend on the framebuffer's visual
1705 * after the binding is updated. Update visual info now.
1706 */
1707 _mesa_update_framebuffer_visual(fb);
1708 }
1709
1710
1711 void GLAPIENTRY
1712 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1713 GLenum pname, GLint *params)
1714 {
1715 const struct gl_renderbuffer_attachment *att;
1716 struct gl_framebuffer *buffer;
1717 GET_CURRENT_CONTEXT(ctx);
1718
1719 ASSERT_OUTSIDE_BEGIN_END(ctx);
1720
1721 switch (target) {
1722 #if FEATURE_EXT_framebuffer_blit
1723 case GL_DRAW_FRAMEBUFFER_EXT:
1724 if (!ctx->Extensions.EXT_framebuffer_blit) {
1725 _mesa_error(ctx, GL_INVALID_ENUM,
1726 "glGetFramebufferAttachmentParameterivEXT(target)");
1727 return;
1728 }
1729 buffer = ctx->DrawBuffer;
1730 break;
1731 case GL_READ_FRAMEBUFFER_EXT:
1732 if (!ctx->Extensions.EXT_framebuffer_blit) {
1733 _mesa_error(ctx, GL_INVALID_ENUM,
1734 "glGetFramebufferAttachmentParameterivEXT(target)");
1735 return;
1736 }
1737 buffer = ctx->ReadBuffer;
1738 break;
1739 #endif
1740 case GL_FRAMEBUFFER_EXT:
1741 buffer = ctx->DrawBuffer;
1742 break;
1743 default:
1744 _mesa_error(ctx, GL_INVALID_ENUM,
1745 "glGetFramebufferAttachmentParameterivEXT(target)");
1746 return;
1747 }
1748
1749 if (buffer->Name == 0) {
1750 _mesa_error(ctx, GL_INVALID_OPERATION,
1751 "glGetFramebufferAttachmentParameterivEXT");
1752 return;
1753 }
1754
1755 att = _mesa_get_attachment(ctx, buffer, attachment);
1756 if (att == NULL) {
1757 _mesa_error(ctx, GL_INVALID_ENUM,
1758 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1759 return;
1760 }
1761
1762 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1763 /* the depth and stencil attachments must point to the same buffer */
1764 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1765 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1766 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1767 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1768 _mesa_error(ctx, GL_INVALID_OPERATION,
1769 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1770 " attachments differ)");
1771 return;
1772 }
1773 }
1774
1775 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1776
1777 switch (pname) {
1778 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1779 *params = att->Type;
1780 return;
1781 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1782 if (att->Type == GL_RENDERBUFFER_EXT) {
1783 *params = att->Renderbuffer->Name;
1784 }
1785 else if (att->Type == GL_TEXTURE) {
1786 *params = att->Texture->Name;
1787 }
1788 else {
1789 _mesa_error(ctx, GL_INVALID_ENUM,
1790 "glGetFramebufferAttachmentParameterivEXT(pname)");
1791 }
1792 return;
1793 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1794 if (att->Type == GL_TEXTURE) {
1795 *params = att->TextureLevel;
1796 }
1797 else {
1798 _mesa_error(ctx, GL_INVALID_ENUM,
1799 "glGetFramebufferAttachmentParameterivEXT(pname)");
1800 }
1801 return;
1802 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1803 if (att->Type == GL_TEXTURE) {
1804 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1805 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
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_TEXTURE_3D_ZOFFSET_EXT:
1817 if (att->Type == GL_TEXTURE) {
1818 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1819 *params = att->Zoffset;
1820 }
1821 else {
1822 *params = 0;
1823 }
1824 }
1825 else {
1826 _mesa_error(ctx, GL_INVALID_ENUM,
1827 "glGetFramebufferAttachmentParameterivEXT(pname)");
1828 }
1829 return;
1830 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1831 if (!ctx->Extensions.ARB_framebuffer_object) {
1832 _mesa_error(ctx, GL_INVALID_ENUM,
1833 "glGetFramebufferAttachmentParameterivEXT(pname)");
1834 }
1835 else {
1836 *params = att->Renderbuffer->ColorEncoding;
1837 }
1838 return;
1839 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1840 if (!ctx->Extensions.ARB_framebuffer_object) {
1841 _mesa_error(ctx, GL_INVALID_ENUM,
1842 "glGetFramebufferAttachmentParameterivEXT(pname)");
1843 return;
1844 }
1845 else {
1846 *params = att->Renderbuffer->ComponentType;
1847 }
1848 return;
1849 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1850 if (!ctx->Extensions.ARB_framebuffer_object) {
1851 _mesa_error(ctx, GL_INVALID_ENUM,
1852 "glGetFramebufferAttachmentParameterivEXT(pname)");
1853 }
1854 else {
1855 *params = att->Renderbuffer->RedBits;
1856 }
1857 return;
1858 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1859 if (!ctx->Extensions.ARB_framebuffer_object) {
1860 _mesa_error(ctx, GL_INVALID_ENUM,
1861 "glGetFramebufferAttachmentParameterivEXT(pname)");
1862 }
1863 else {
1864 *params = att->Renderbuffer->GreenBits;
1865 }
1866 return;
1867 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1868 if (!ctx->Extensions.ARB_framebuffer_object) {
1869 _mesa_error(ctx, GL_INVALID_ENUM,
1870 "glGetFramebufferAttachmentParameterivEXT(pname)");
1871 }
1872 else {
1873 *params = att->Renderbuffer->BlueBits;
1874 }
1875 return;
1876 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1877 if (!ctx->Extensions.ARB_framebuffer_object) {
1878 _mesa_error(ctx, GL_INVALID_ENUM,
1879 "glGetFramebufferAttachmentParameterivEXT(pname)");
1880 }
1881 else {
1882 *params = att->Renderbuffer->AlphaBits;
1883 }
1884 return;
1885 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1886 if (!ctx->Extensions.ARB_framebuffer_object) {
1887 _mesa_error(ctx, GL_INVALID_ENUM,
1888 "glGetFramebufferAttachmentParameterivEXT(pname)");
1889 }
1890 else {
1891 *params = att->Renderbuffer->DepthBits;
1892 }
1893 return;
1894 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1895 if (!ctx->Extensions.ARB_framebuffer_object) {
1896 _mesa_error(ctx, GL_INVALID_ENUM,
1897 "glGetFramebufferAttachmentParameterivEXT(pname)");
1898 }
1899 else {
1900 *params = att->Renderbuffer->StencilBits;
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 * 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 ASSERT(ctx->Driver.BlitFramebuffer);
2080 ctx->Driver.BlitFramebuffer(ctx,
2081 srcX0, srcY0, srcX1, srcY1,
2082 dstX0, dstY0, dstX1, dstY1,
2083 mask, filter);
2084 }
2085 #endif /* FEATURE_EXT_framebuffer_blit */