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