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