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