mesa: added comment for check_begin_texture_render()
[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 if (ctx->Driver.FinishRenderTexture)
237 ctx->Driver.FinishRenderTexture(ctx, att);
238 }
239 else {
240 /* new attachment */
241 if (ctx->Driver.FinishRenderTexture && att->Texture)
242 ctx->Driver.FinishRenderTexture(ctx, att);
243 _mesa_remove_attachment(ctx, att);
244 att->Type = GL_TEXTURE;
245 assert(!att->Texture);
246 _mesa_reference_texobj(&att->Texture, texObj);
247 }
248
249 /* always update these fields */
250 att->TextureLevel = level;
251 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
252 att->Zoffset = zoffset;
253 att->Complete = GL_FALSE;
254
255 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
256 ctx->Driver.RenderTexture(ctx, fb, att);
257 }
258
259 invalidate_framebuffer(fb);
260 }
261
262
263 /**
264 * Bind a renderbuffer to an attachment point.
265 * The previous binding, if any, will be removed first.
266 */
267 void
268 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
269 struct gl_renderbuffer_attachment *att,
270 struct gl_renderbuffer *rb)
271 {
272 /* XXX check if re-doing same attachment, exit early */
273 _mesa_remove_attachment(ctx, att);
274 att->Type = GL_RENDERBUFFER_EXT;
275 att->Texture = NULL; /* just to be safe */
276 att->Complete = GL_FALSE;
277 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
278 }
279
280
281 /**
282 * Fallback for ctx->Driver.FramebufferRenderbuffer()
283 * Attach a renderbuffer object to a framebuffer object.
284 */
285 void
286 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
287 GLenum attachment, struct gl_renderbuffer *rb)
288 {
289 struct gl_renderbuffer_attachment *att;
290
291 _glthread_LOCK_MUTEX(fb->Mutex);
292
293 att = _mesa_get_attachment(ctx, fb, attachment);
294 ASSERT(att);
295 if (rb) {
296 _mesa_set_renderbuffer_attachment(ctx, att, rb);
297 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
298 /* do stencil attachment here (depth already done above) */
299 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
300 assert(att);
301 _mesa_set_renderbuffer_attachment(ctx, att, rb);
302 }
303 }
304 else {
305 _mesa_remove_attachment(ctx, att);
306 }
307
308 invalidate_framebuffer(fb);
309
310 _glthread_UNLOCK_MUTEX(fb->Mutex);
311 }
312
313
314 /**
315 * For debug only.
316 */
317 static void
318 att_incomplete(const char *msg)
319 {
320 #if DEBUG_FBO
321 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
322 #else
323 (void) msg;
324 #endif
325 }
326
327
328 /**
329 * For debug only.
330 */
331 static void
332 fbo_incomplete(const char *msg, int index)
333 {
334 #if DEBUG_FBO
335 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
336 #else
337 (void) msg;
338 (void) index;
339 #endif
340 }
341
342
343
344
345 /**
346 * Test if an attachment point is complete and update its Complete field.
347 * \param format if GL_COLOR, this is a color attachment point,
348 * if GL_DEPTH, this is a depth component attachment point,
349 * if GL_STENCIL, this is a stencil component attachment point.
350 */
351 static void
352 test_attachment_completeness(const GLcontext *ctx, GLenum format,
353 struct gl_renderbuffer_attachment *att)
354 {
355 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
356
357 /* assume complete */
358 att->Complete = GL_TRUE;
359
360 /* Look for reasons why the attachment might be incomplete */
361 if (att->Type == GL_TEXTURE) {
362 const struct gl_texture_object *texObj = att->Texture;
363 struct gl_texture_image *texImage;
364 GLenum baseFormat;
365
366 if (!texObj) {
367 att_incomplete("no texobj");
368 att->Complete = GL_FALSE;
369 return;
370 }
371
372 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
373 if (!texImage) {
374 att_incomplete("no teximage");
375 att->Complete = GL_FALSE;
376 return;
377 }
378 if (texImage->Width < 1 || texImage->Height < 1) {
379 att_incomplete("teximage width/height=0");
380 _mesa_printf("texobj = %u\n", texObj->Name);
381 _mesa_printf("level = %d\n", att->TextureLevel);
382 att->Complete = GL_FALSE;
383 return;
384 }
385 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
386 att_incomplete("bad z offset");
387 att->Complete = GL_FALSE;
388 return;
389 }
390
391 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
392
393 if (format == GL_COLOR) {
394 if (baseFormat != GL_RGB &&
395 baseFormat != GL_RGBA) {
396 att_incomplete("bad format");
397 att->Complete = GL_FALSE;
398 return;
399 }
400 if (_mesa_is_format_compressed(texImage->TexFormat)) {
401 att_incomplete("compressed internalformat");
402 att->Complete = GL_FALSE;
403 return;
404 }
405 }
406 else if (format == GL_DEPTH) {
407 if (baseFormat == GL_DEPTH_COMPONENT) {
408 /* OK */
409 }
410 else if (ctx->Extensions.EXT_packed_depth_stencil &&
411 ctx->Extensions.ARB_depth_texture &&
412 baseFormat == GL_DEPTH_STENCIL_EXT) {
413 /* OK */
414 }
415 else {
416 att->Complete = GL_FALSE;
417 att_incomplete("bad depth format");
418 return;
419 }
420 }
421 else {
422 ASSERT(format == GL_STENCIL);
423 if (ctx->Extensions.EXT_packed_depth_stencil &&
424 ctx->Extensions.ARB_depth_texture &&
425 baseFormat == GL_DEPTH_STENCIL_EXT) {
426 /* OK */
427 }
428 else {
429 /* no such thing as stencil-only textures */
430 att_incomplete("illegal stencil texture");
431 att->Complete = GL_FALSE;
432 return;
433 }
434 }
435 }
436 else if (att->Type == GL_RENDERBUFFER_EXT) {
437 const GLenum baseFormat =
438 _mesa_get_format_base_format(att->Renderbuffer->Format);
439
440 ASSERT(att->Renderbuffer);
441 if (!att->Renderbuffer->InternalFormat ||
442 att->Renderbuffer->Width < 1 ||
443 att->Renderbuffer->Height < 1) {
444 att_incomplete("0x0 renderbuffer");
445 att->Complete = GL_FALSE;
446 return;
447 }
448 if (format == GL_COLOR) {
449 if (baseFormat != GL_RGB &&
450 baseFormat != GL_RGBA) {
451 att_incomplete("bad renderbuffer color format");
452 att->Complete = GL_FALSE;
453 return;
454 }
455 }
456 else if (format == GL_DEPTH) {
457 if (baseFormat == GL_DEPTH_COMPONENT) {
458 /* OK */
459 }
460 else if (ctx->Extensions.EXT_packed_depth_stencil &&
461 baseFormat == GL_DEPTH_STENCIL_EXT) {
462 /* OK */
463 }
464 else {
465 att_incomplete("bad renderbuffer depth format");
466 att->Complete = GL_FALSE;
467 return;
468 }
469 }
470 else {
471 assert(format == GL_STENCIL);
472 if (baseFormat == GL_STENCIL_INDEX) {
473 /* OK */
474 }
475 else if (ctx->Extensions.EXT_packed_depth_stencil &&
476 baseFormat == GL_DEPTH_STENCIL_EXT) {
477 /* OK */
478 }
479 else {
480 att->Complete = GL_FALSE;
481 att_incomplete("bad renderbuffer stencil format");
482 return;
483 }
484 }
485 }
486 else {
487 ASSERT(att->Type == GL_NONE);
488 /* complete */
489 return;
490 }
491 }
492
493
494 /**
495 * Test if the given framebuffer object is complete and update its
496 * Status field with the results.
497 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
498 * driver to make hardware-specific validation/completeness checks.
499 * Also update the framebuffer's Width and Height fields if the
500 * framebuffer is complete.
501 */
502 void
503 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
504 {
505 GLuint numImages;
506 GLenum intFormat = GL_NONE; /* color buffers' internal format */
507 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
508 GLint numSamples = -1;
509 GLint i;
510 GLuint j;
511
512 assert(fb->Name != 0);
513
514 numImages = 0;
515 fb->Width = 0;
516 fb->Height = 0;
517
518 /* Start at -2 to more easily loop over all attachment points.
519 * -2: depth buffer
520 * -1: stencil buffer
521 * >=0: color buffer
522 */
523 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
524 struct gl_renderbuffer_attachment *att;
525 GLenum f;
526
527 /*
528 * XXX for ARB_fbo, only check color buffers that are named by
529 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
530 */
531
532 /* check for attachment completeness
533 */
534 if (i == -2) {
535 att = &fb->Attachment[BUFFER_DEPTH];
536 test_attachment_completeness(ctx, GL_DEPTH, att);
537 if (!att->Complete) {
538 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
539 fbo_incomplete("depth attachment incomplete", -1);
540 return;
541 }
542 }
543 else if (i == -1) {
544 att = &fb->Attachment[BUFFER_STENCIL];
545 test_attachment_completeness(ctx, GL_STENCIL, att);
546 if (!att->Complete) {
547 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
548 fbo_incomplete("stencil attachment incomplete", -1);
549 return;
550 }
551 }
552 else {
553 att = &fb->Attachment[BUFFER_COLOR0 + i];
554 test_attachment_completeness(ctx, GL_COLOR, att);
555 if (!att->Complete) {
556 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
557 fbo_incomplete("color attachment incomplete", i);
558 return;
559 }
560 }
561
562 /* get width, height, format of the renderbuffer/texture
563 */
564 if (att->Type == GL_TEXTURE) {
565 const struct gl_texture_image *texImg
566 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
567 minWidth = MIN2(minWidth, texImg->Width);
568 maxWidth = MAX2(maxWidth, texImg->Width);
569 minHeight = MIN2(minHeight, texImg->Height);
570 maxHeight = MAX2(maxHeight, texImg->Height);
571 f = texImg->_BaseFormat;
572 numImages++;
573 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
574 && f != GL_DEPTH_STENCIL_EXT) {
575 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
576 fbo_incomplete("texture attachment incomplete", -1);
577 return;
578 }
579 }
580 else if (att->Type == GL_RENDERBUFFER_EXT) {
581 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
582 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
583 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
584 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
585 f = att->Renderbuffer->InternalFormat;
586 numImages++;
587 }
588 else {
589 assert(att->Type == GL_NONE);
590 continue;
591 }
592
593 if (numSamples < 0) {
594 /* first buffer */
595 numSamples = att->Renderbuffer->NumSamples;
596 }
597
598 /* Error-check width, height, format, samples
599 */
600 if (numImages == 1) {
601 /* save format, num samples */
602 if (i >= 0) {
603 intFormat = f;
604 }
605 }
606 else {
607 if (!ctx->Extensions.ARB_framebuffer_object) {
608 /* check that width, height, format are same */
609 if (minWidth != maxWidth || minHeight != maxHeight) {
610 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
611 fbo_incomplete("width or height mismatch", -1);
612 return;
613 }
614 /* check that all color buffer have same format */
615 if (intFormat != GL_NONE && f != intFormat) {
616 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
617 fbo_incomplete("format mismatch", -1);
618 return;
619 }
620 }
621 if (att->Renderbuffer &&
622 att->Renderbuffer->NumSamples != numSamples) {
623 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
624 fbo_incomplete("inconsistant number of samples", i);
625 return;
626 }
627
628 }
629 }
630
631 #ifndef FEATURE_OES_framebuffer_object
632 /* Check that all DrawBuffers are present */
633 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
634 if (fb->ColorDrawBuffer[j] != GL_NONE) {
635 const struct gl_renderbuffer_attachment *att
636 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
637 assert(att);
638 if (att->Type == GL_NONE) {
639 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
640 fbo_incomplete("missing drawbuffer", j);
641 return;
642 }
643 }
644 }
645
646 /* Check that the ReadBuffer is present */
647 if (fb->ColorReadBuffer != GL_NONE) {
648 const struct gl_renderbuffer_attachment *att
649 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
650 assert(att);
651 if (att->Type == GL_NONE) {
652 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
653 fbo_incomplete("missing readbuffer", -1);
654 return;
655 }
656 }
657 #else
658 (void) j;
659 #endif
660
661 if (numImages == 0) {
662 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
663 fbo_incomplete("no attachments", -1);
664 return;
665 }
666
667 /* Provisionally set status = COMPLETE ... */
668 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
669
670 /* ... but the driver may say the FB is incomplete.
671 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
672 * if anything.
673 */
674 if (ctx->Driver.ValidateFramebuffer) {
675 ctx->Driver.ValidateFramebuffer(ctx, fb);
676 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
677 fbo_incomplete("driver marked FBO as incomplete", -1);
678 }
679 }
680
681 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
682 /*
683 * Note that if ARB_framebuffer_object is supported and the attached
684 * renderbuffers/textures are different sizes, the framebuffer
685 * width/height will be set to the smallest width/height.
686 */
687 fb->Width = minWidth;
688 fb->Height = minHeight;
689
690 /* finally, update the visual info for the framebuffer */
691 _mesa_update_framebuffer_visual(fb);
692 }
693 }
694
695
696 GLboolean GLAPIENTRY
697 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
698 {
699 GET_CURRENT_CONTEXT(ctx);
700 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
701 if (renderbuffer) {
702 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
703 if (rb != NULL && rb != &DummyRenderbuffer)
704 return GL_TRUE;
705 }
706 return GL_FALSE;
707 }
708
709
710 void GLAPIENTRY
711 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
712 {
713 struct gl_renderbuffer *newRb;
714 GET_CURRENT_CONTEXT(ctx);
715
716 ASSERT_OUTSIDE_BEGIN_END(ctx);
717
718 if (target != GL_RENDERBUFFER_EXT) {
719 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
720 return;
721 }
722
723 /* No need to flush here since the render buffer binding has no
724 * effect on rendering state.
725 */
726
727 if (renderbuffer) {
728 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
729 if (newRb == &DummyRenderbuffer) {
730 /* ID was reserved, but no real renderbuffer object made yet */
731 newRb = NULL;
732 }
733 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
734 /* All RB IDs must be Gen'd */
735 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
736 return;
737 }
738
739 if (!newRb) {
740 /* create new renderbuffer object */
741 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
742 if (!newRb) {
743 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
744 return;
745 }
746 ASSERT(newRb->AllocStorage);
747 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
748 newRb->RefCount = 1; /* referenced by hash table */
749 }
750 }
751 else {
752 newRb = NULL;
753 }
754
755 ASSERT(newRb != &DummyRenderbuffer);
756
757 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
758 }
759
760
761 /**
762 * If the given renderbuffer is anywhere attached to the framebuffer, detach
763 * the renderbuffer.
764 * This is used when a renderbuffer object is deleted.
765 * The spec calls for unbinding.
766 */
767 static void
768 detach_renderbuffer(GLcontext *ctx,
769 struct gl_framebuffer *fb,
770 struct gl_renderbuffer *rb)
771 {
772 GLuint i;
773 for (i = 0; i < BUFFER_COUNT; i++) {
774 if (fb->Attachment[i].Renderbuffer == rb) {
775 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
776 }
777 }
778 invalidate_framebuffer(fb);
779 }
780
781
782 void GLAPIENTRY
783 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
784 {
785 GLint i;
786 GET_CURRENT_CONTEXT(ctx);
787
788 ASSERT_OUTSIDE_BEGIN_END(ctx);
789 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
790
791 for (i = 0; i < n; i++) {
792 if (renderbuffers[i] > 0) {
793 struct gl_renderbuffer *rb;
794 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
795 if (rb) {
796 /* check if deleting currently bound renderbuffer object */
797 if (rb == ctx->CurrentRenderbuffer) {
798 /* bind default */
799 ASSERT(rb->RefCount >= 2);
800 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
801 }
802
803 if (ctx->DrawBuffer->Name) {
804 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
805 }
806 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
807 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
808 }
809
810 /* Remove from hash table immediately, to free the ID.
811 * But the object will not be freed until it's no longer
812 * referenced anywhere else.
813 */
814 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
815
816 if (rb != &DummyRenderbuffer) {
817 /* no longer referenced by hash table */
818 _mesa_reference_renderbuffer(&rb, NULL);
819 }
820 }
821 }
822 }
823 }
824
825
826 void GLAPIENTRY
827 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
828 {
829 GET_CURRENT_CONTEXT(ctx);
830 GLuint first;
831 GLint i;
832
833 ASSERT_OUTSIDE_BEGIN_END(ctx);
834
835 if (n < 0) {
836 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
837 return;
838 }
839
840 if (!renderbuffers)
841 return;
842
843 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
844
845 for (i = 0; i < n; i++) {
846 GLuint name = first + i;
847 renderbuffers[i] = name;
848 /* insert dummy placeholder into hash table */
849 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
850 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
851 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
852 }
853 }
854
855
856 /**
857 * Given an internal format token for a render buffer, return the
858 * corresponding base format.
859 * This is very similar to _mesa_base_tex_format() but the set of valid
860 * internal formats is somewhat different.
861 *
862 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
863 * GL_DEPTH_STENCIL_EXT or zero if error.
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 for (i = 0; i < BUFFER_COUNT; i++) {
1163 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1164 struct gl_texture_object *texObj = att->Texture;
1165 if (texObj
1166 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1167 ctx->Driver.RenderTexture(ctx, fb, att);
1168 }
1169 }
1170 }
1171
1172
1173 /**
1174 * Examine all the framebuffer's attachments to see if any are textures.
1175 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1176 * notify the device driver that the texture image may have changed.
1177 */
1178 static void
1179 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1180 {
1181 if (ctx->Driver.FinishRenderTexture) {
1182 GLuint i;
1183 for (i = 0; i < BUFFER_COUNT; i++) {
1184 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1185 if (att->Texture && att->Renderbuffer) {
1186 ctx->Driver.FinishRenderTexture(ctx, att);
1187 }
1188 }
1189 }
1190 }
1191
1192
1193 void GLAPIENTRY
1194 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1195 {
1196 struct gl_framebuffer *newFb, *newFbread;
1197 GLboolean bindReadBuf, bindDrawBuf;
1198 GET_CURRENT_CONTEXT(ctx);
1199
1200 #ifdef DEBUG
1201 if (ctx->Extensions.ARB_framebuffer_object) {
1202 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1203 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1204 }
1205 #endif
1206
1207 ASSERT_OUTSIDE_BEGIN_END(ctx);
1208
1209 if (!ctx->Extensions.EXT_framebuffer_object) {
1210 _mesa_error(ctx, GL_INVALID_OPERATION,
1211 "glBindFramebufferEXT(unsupported)");
1212 return;
1213 }
1214
1215 switch (target) {
1216 #if FEATURE_EXT_framebuffer_blit
1217 case GL_DRAW_FRAMEBUFFER_EXT:
1218 if (!ctx->Extensions.EXT_framebuffer_blit) {
1219 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1220 return;
1221 }
1222 bindDrawBuf = GL_TRUE;
1223 bindReadBuf = GL_FALSE;
1224 break;
1225 case GL_READ_FRAMEBUFFER_EXT:
1226 if (!ctx->Extensions.EXT_framebuffer_blit) {
1227 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1228 return;
1229 }
1230 bindDrawBuf = GL_FALSE;
1231 bindReadBuf = GL_TRUE;
1232 break;
1233 #endif
1234 case GL_FRAMEBUFFER_EXT:
1235 bindDrawBuf = GL_TRUE;
1236 bindReadBuf = GL_TRUE;
1237 break;
1238 default:
1239 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1240 return;
1241 }
1242
1243 if (framebuffer) {
1244 /* Binding a user-created framebuffer object */
1245 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1246 if (newFb == &DummyFramebuffer) {
1247 /* ID was reserved, but no real framebuffer object made yet */
1248 newFb = NULL;
1249 }
1250 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1251 /* All FBO IDs must be Gen'd */
1252 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1253 return;
1254 }
1255
1256 if (!newFb) {
1257 /* create new framebuffer object */
1258 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1259 if (!newFb) {
1260 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1261 return;
1262 }
1263 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1264 }
1265 newFbread = newFb;
1266 }
1267 else {
1268 /* Binding the window system framebuffer (which was originally set
1269 * with MakeCurrent).
1270 */
1271 newFb = ctx->WinSysDrawBuffer;
1272 newFbread = ctx->WinSysReadBuffer;
1273 }
1274
1275 ASSERT(newFb);
1276 ASSERT(newFb != &DummyFramebuffer);
1277
1278 /*
1279 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1280 */
1281 if (bindReadBuf) {
1282 if (ctx->ReadBuffer == newFbread) {
1283 bindReadBuf = GL_FALSE; /* no change */
1284 }
1285 else {
1286 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1287 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1288 }
1289 }
1290
1291 if (bindDrawBuf) {
1292 /* check if old FB had any texture attachments */
1293 if (ctx->DrawBuffer->Name != 0) {
1294 check_end_texture_render(ctx, ctx->DrawBuffer);
1295 }
1296
1297 if (ctx->DrawBuffer == newFb) {
1298 bindDrawBuf = GL_FALSE; /* no change */
1299 }
1300 else {
1301 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1302 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1303 }
1304
1305 if (newFb->Name != 0) {
1306 /* check if newly bound framebuffer has any texture attachments */
1307 check_begin_texture_render(ctx, newFb);
1308 }
1309 }
1310
1311 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1312 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1313 }
1314 }
1315
1316
1317 void GLAPIENTRY
1318 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1319 {
1320 GLint i;
1321 GET_CURRENT_CONTEXT(ctx);
1322
1323 ASSERT_OUTSIDE_BEGIN_END(ctx);
1324 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1325
1326 for (i = 0; i < n; i++) {
1327 if (framebuffers[i] > 0) {
1328 struct gl_framebuffer *fb;
1329 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1330 if (fb) {
1331 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1332
1333 /* check if deleting currently bound framebuffer object */
1334 if (fb == ctx->DrawBuffer) {
1335 /* bind default */
1336 ASSERT(fb->RefCount >= 2);
1337 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1338 }
1339 if (fb == ctx->ReadBuffer) {
1340 /* bind default */
1341 ASSERT(fb->RefCount >= 2);
1342 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1343 }
1344
1345 /* remove from hash table immediately, to free the ID */
1346 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1347
1348 if (fb != &DummyFramebuffer) {
1349 /* But the object will not be freed until it's no longer
1350 * bound in any context.
1351 */
1352 _mesa_reference_framebuffer(&fb, NULL);
1353 }
1354 }
1355 }
1356 }
1357 }
1358
1359
1360 void GLAPIENTRY
1361 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1362 {
1363 GET_CURRENT_CONTEXT(ctx);
1364 GLuint first;
1365 GLint i;
1366
1367 ASSERT_OUTSIDE_BEGIN_END(ctx);
1368
1369 if (n < 0) {
1370 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1371 return;
1372 }
1373
1374 if (!framebuffers)
1375 return;
1376
1377 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1378
1379 for (i = 0; i < n; i++) {
1380 GLuint name = first + i;
1381 framebuffers[i] = name;
1382 /* insert dummy placeholder into hash table */
1383 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1384 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1385 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1386 }
1387 }
1388
1389
1390
1391 GLenum GLAPIENTRY
1392 _mesa_CheckFramebufferStatusEXT(GLenum target)
1393 {
1394 struct gl_framebuffer *buffer;
1395 GET_CURRENT_CONTEXT(ctx);
1396
1397 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1398
1399 switch (target) {
1400 #if FEATURE_EXT_framebuffer_blit
1401 case GL_DRAW_FRAMEBUFFER_EXT:
1402 if (!ctx->Extensions.EXT_framebuffer_blit) {
1403 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1404 return 0;
1405 }
1406 buffer = ctx->DrawBuffer;
1407 break;
1408 case GL_READ_FRAMEBUFFER_EXT:
1409 if (!ctx->Extensions.EXT_framebuffer_blit) {
1410 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1411 return 0;
1412 }
1413 buffer = ctx->ReadBuffer;
1414 break;
1415 #endif
1416 case GL_FRAMEBUFFER_EXT:
1417 buffer = ctx->DrawBuffer;
1418 break;
1419 default:
1420 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1421 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1422 }
1423
1424 if (buffer->Name == 0) {
1425 /* The window system / default framebuffer is always complete */
1426 return GL_FRAMEBUFFER_COMPLETE_EXT;
1427 }
1428
1429 /* No need to flush here */
1430
1431 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1432 _mesa_test_framebuffer_completeness(ctx, buffer);
1433 }
1434
1435 return buffer->_Status;
1436 }
1437
1438
1439
1440 /**
1441 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1442 */
1443 static void
1444 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1445 GLenum attachment, GLenum textarget, GLuint texture,
1446 GLint level, GLint zoffset)
1447 {
1448 struct gl_renderbuffer_attachment *att;
1449 struct gl_texture_object *texObj = NULL;
1450 struct gl_framebuffer *fb;
1451 GLboolean error = GL_FALSE;
1452
1453 ASSERT_OUTSIDE_BEGIN_END(ctx);
1454
1455 switch (target) {
1456 case GL_READ_FRAMEBUFFER_EXT:
1457 error = !ctx->Extensions.EXT_framebuffer_blit;
1458 fb = ctx->ReadBuffer;
1459 break;
1460 case GL_DRAW_FRAMEBUFFER_EXT:
1461 error = !ctx->Extensions.EXT_framebuffer_blit;
1462 /* fall-through */
1463 case GL_FRAMEBUFFER_EXT:
1464 fb = ctx->DrawBuffer;
1465 break;
1466 default:
1467 error = GL_TRUE;
1468 }
1469
1470 if (error) {
1471 _mesa_error(ctx, GL_INVALID_ENUM,
1472 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1473 return;
1474 }
1475
1476 ASSERT(fb);
1477
1478 /* check framebuffer binding */
1479 if (fb->Name == 0) {
1480 _mesa_error(ctx, GL_INVALID_OPERATION,
1481 "glFramebufferTexture%sEXT", caller);
1482 return;
1483 }
1484
1485
1486 /* The textarget, level, and zoffset parameters are only validated if
1487 * texture is non-zero.
1488 */
1489 if (texture) {
1490 GLboolean err = GL_TRUE;
1491
1492 texObj = _mesa_lookup_texture(ctx, texture);
1493 if (texObj != NULL) {
1494 if (textarget == 0) {
1495 err = (texObj->Target != GL_TEXTURE_3D) &&
1496 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1497 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1498 }
1499 else {
1500 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1501 ? !IS_CUBE_FACE(textarget)
1502 : (texObj->Target != textarget);
1503 }
1504 }
1505
1506 if (err) {
1507 _mesa_error(ctx, GL_INVALID_OPERATION,
1508 "glFramebufferTexture%sEXT(texture target mismatch)",
1509 caller);
1510 return;
1511 }
1512
1513 if (texObj->Target == GL_TEXTURE_3D) {
1514 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1515 if (zoffset < 0 || zoffset >= maxSize) {
1516 _mesa_error(ctx, GL_INVALID_VALUE,
1517 "glFramebufferTexture%sEXT(zoffset)", caller);
1518 return;
1519 }
1520 }
1521 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1522 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1523 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1524 _mesa_error(ctx, GL_INVALID_VALUE,
1525 "glFramebufferTexture%sEXT(layer)", caller);
1526 return;
1527 }
1528 }
1529
1530 if ((level < 0) ||
1531 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1532 _mesa_error(ctx, GL_INVALID_VALUE,
1533 "glFramebufferTexture%sEXT(level)", caller);
1534 return;
1535 }
1536 }
1537
1538 att = _mesa_get_attachment(ctx, fb, attachment);
1539 if (att == NULL) {
1540 _mesa_error(ctx, GL_INVALID_ENUM,
1541 "glFramebufferTexture%sEXT(attachment)", caller);
1542 return;
1543 }
1544
1545 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1546
1547 _glthread_LOCK_MUTEX(fb->Mutex);
1548 if (texObj) {
1549 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1550 level, zoffset);
1551 /* Set the render-to-texture flag. We'll check this flag in
1552 * glTexImage() and friends to determine if we need to revalidate
1553 * any FBOs that might be rendering into this texture.
1554 * This flag never gets cleared since it's non-trivial to determine
1555 * when all FBOs might be done rendering to this texture. That's OK
1556 * though since it's uncommon to render to a texture then repeatedly
1557 * call glTexImage() to change images in the texture.
1558 */
1559 texObj->_RenderToTexture = GL_TRUE;
1560 }
1561 else {
1562 _mesa_remove_attachment(ctx, att);
1563 }
1564
1565 invalidate_framebuffer(fb);
1566
1567 _glthread_UNLOCK_MUTEX(fb->Mutex);
1568 }
1569
1570
1571
1572 void GLAPIENTRY
1573 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1574 GLenum textarget, GLuint texture, GLint level)
1575 {
1576 GET_CURRENT_CONTEXT(ctx);
1577
1578 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1579 _mesa_error(ctx, GL_INVALID_ENUM,
1580 "glFramebufferTexture1DEXT(textarget)");
1581 return;
1582 }
1583
1584 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1585 level, 0);
1586 }
1587
1588
1589 void GLAPIENTRY
1590 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1591 GLenum textarget, GLuint texture, GLint level)
1592 {
1593 GET_CURRENT_CONTEXT(ctx);
1594
1595 if ((texture != 0) &&
1596 (textarget != GL_TEXTURE_2D) &&
1597 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1598 (!IS_CUBE_FACE(textarget))) {
1599 _mesa_error(ctx, GL_INVALID_OPERATION,
1600 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1601 return;
1602 }
1603
1604 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1605 level, 0);
1606 }
1607
1608
1609 void GLAPIENTRY
1610 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1611 GLenum textarget, GLuint texture,
1612 GLint level, GLint zoffset)
1613 {
1614 GET_CURRENT_CONTEXT(ctx);
1615
1616 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1617 _mesa_error(ctx, GL_INVALID_ENUM,
1618 "glFramebufferTexture3DEXT(textarget)");
1619 return;
1620 }
1621
1622 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1623 level, zoffset);
1624 }
1625
1626
1627 void GLAPIENTRY
1628 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1629 GLuint texture, GLint level, GLint layer)
1630 {
1631 GET_CURRENT_CONTEXT(ctx);
1632
1633 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1634 level, layer);
1635 }
1636
1637
1638 void GLAPIENTRY
1639 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1640 GLenum renderbufferTarget,
1641 GLuint renderbuffer)
1642 {
1643 struct gl_renderbuffer_attachment *att;
1644 struct gl_framebuffer *fb;
1645 struct gl_renderbuffer *rb;
1646 GET_CURRENT_CONTEXT(ctx);
1647
1648 ASSERT_OUTSIDE_BEGIN_END(ctx);
1649
1650 switch (target) {
1651 #if FEATURE_EXT_framebuffer_blit
1652 case GL_DRAW_FRAMEBUFFER_EXT:
1653 if (!ctx->Extensions.EXT_framebuffer_blit) {
1654 _mesa_error(ctx, GL_INVALID_ENUM,
1655 "glFramebufferRenderbufferEXT(target)");
1656 return;
1657 }
1658 fb = ctx->DrawBuffer;
1659 break;
1660 case GL_READ_FRAMEBUFFER_EXT:
1661 if (!ctx->Extensions.EXT_framebuffer_blit) {
1662 _mesa_error(ctx, GL_INVALID_ENUM,
1663 "glFramebufferRenderbufferEXT(target)");
1664 return;
1665 }
1666 fb = ctx->ReadBuffer;
1667 break;
1668 #endif
1669 case GL_FRAMEBUFFER_EXT:
1670 fb = ctx->DrawBuffer;
1671 break;
1672 default:
1673 _mesa_error(ctx, GL_INVALID_ENUM,
1674 "glFramebufferRenderbufferEXT(target)");
1675 return;
1676 }
1677
1678 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1679 _mesa_error(ctx, GL_INVALID_ENUM,
1680 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1681 return;
1682 }
1683
1684 if (fb->Name == 0) {
1685 /* Can't attach new renderbuffers to a window system framebuffer */
1686 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1687 return;
1688 }
1689
1690 att = _mesa_get_attachment(ctx, fb, attachment);
1691 if (att == NULL) {
1692 _mesa_error(ctx, GL_INVALID_ENUM,
1693 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1694 _mesa_lookup_enum_by_nr(attachment));
1695 return;
1696 }
1697
1698 if (renderbuffer) {
1699 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1700 if (!rb) {
1701 _mesa_error(ctx, GL_INVALID_OPERATION,
1702 "glFramebufferRenderbufferEXT(non-existant"
1703 " renderbuffer %u)", renderbuffer);
1704 return;
1705 }
1706 }
1707 else {
1708 /* remove renderbuffer attachment */
1709 rb = NULL;
1710 }
1711
1712 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1713 /* make sure the renderbuffer is a depth/stencil format */
1714 const GLenum baseFormat =
1715 _mesa_get_format_base_format(att->Renderbuffer->Format);
1716 if (baseFormat != GL_DEPTH_STENCIL) {
1717 _mesa_error(ctx, GL_INVALID_OPERATION,
1718 "glFramebufferRenderbufferEXT(renderbuffer"
1719 " is not DEPTH_STENCIL format)");
1720 return;
1721 }
1722 }
1723
1724
1725 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1726
1727 assert(ctx->Driver.FramebufferRenderbuffer);
1728 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1729
1730 /* Some subsequent GL commands may depend on the framebuffer's visual
1731 * after the binding is updated. Update visual info now.
1732 */
1733 _mesa_update_framebuffer_visual(fb);
1734 }
1735
1736
1737 void GLAPIENTRY
1738 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1739 GLenum pname, GLint *params)
1740 {
1741 const struct gl_renderbuffer_attachment *att;
1742 struct gl_framebuffer *buffer;
1743 GET_CURRENT_CONTEXT(ctx);
1744
1745 ASSERT_OUTSIDE_BEGIN_END(ctx);
1746
1747 switch (target) {
1748 #if FEATURE_EXT_framebuffer_blit
1749 case GL_DRAW_FRAMEBUFFER_EXT:
1750 if (!ctx->Extensions.EXT_framebuffer_blit) {
1751 _mesa_error(ctx, GL_INVALID_ENUM,
1752 "glGetFramebufferAttachmentParameterivEXT(target)");
1753 return;
1754 }
1755 buffer = ctx->DrawBuffer;
1756 break;
1757 case GL_READ_FRAMEBUFFER_EXT:
1758 if (!ctx->Extensions.EXT_framebuffer_blit) {
1759 _mesa_error(ctx, GL_INVALID_ENUM,
1760 "glGetFramebufferAttachmentParameterivEXT(target)");
1761 return;
1762 }
1763 buffer = ctx->ReadBuffer;
1764 break;
1765 #endif
1766 case GL_FRAMEBUFFER_EXT:
1767 buffer = ctx->DrawBuffer;
1768 break;
1769 default:
1770 _mesa_error(ctx, GL_INVALID_ENUM,
1771 "glGetFramebufferAttachmentParameterivEXT(target)");
1772 return;
1773 }
1774
1775 if (buffer->Name == 0) {
1776 _mesa_error(ctx, GL_INVALID_OPERATION,
1777 "glGetFramebufferAttachmentParameterivEXT");
1778 return;
1779 }
1780
1781 att = _mesa_get_attachment(ctx, buffer, attachment);
1782 if (att == NULL) {
1783 _mesa_error(ctx, GL_INVALID_ENUM,
1784 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1785 return;
1786 }
1787
1788 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1789 /* the depth and stencil attachments must point to the same buffer */
1790 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1791 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1792 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1793 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1794 _mesa_error(ctx, GL_INVALID_OPERATION,
1795 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1796 " attachments differ)");
1797 return;
1798 }
1799 }
1800
1801 /* No need to flush here */
1802
1803 switch (pname) {
1804 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1805 *params = att->Type;
1806 return;
1807 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1808 if (att->Type == GL_RENDERBUFFER_EXT) {
1809 *params = att->Renderbuffer->Name;
1810 }
1811 else if (att->Type == GL_TEXTURE) {
1812 *params = att->Texture->Name;
1813 }
1814 else {
1815 _mesa_error(ctx, GL_INVALID_ENUM,
1816 "glGetFramebufferAttachmentParameterivEXT(pname)");
1817 }
1818 return;
1819 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1820 if (att->Type == GL_TEXTURE) {
1821 *params = att->TextureLevel;
1822 }
1823 else {
1824 _mesa_error(ctx, GL_INVALID_ENUM,
1825 "glGetFramebufferAttachmentParameterivEXT(pname)");
1826 }
1827 return;
1828 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1829 if (att->Type == GL_TEXTURE) {
1830 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1831 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1832 }
1833 else {
1834 *params = 0;
1835 }
1836 }
1837 else {
1838 _mesa_error(ctx, GL_INVALID_ENUM,
1839 "glGetFramebufferAttachmentParameterivEXT(pname)");
1840 }
1841 return;
1842 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1843 if (att->Type == GL_TEXTURE) {
1844 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1845 *params = att->Zoffset;
1846 }
1847 else {
1848 *params = 0;
1849 }
1850 }
1851 else {
1852 _mesa_error(ctx, GL_INVALID_ENUM,
1853 "glGetFramebufferAttachmentParameterivEXT(pname)");
1854 }
1855 return;
1856 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1857 if (!ctx->Extensions.ARB_framebuffer_object) {
1858 _mesa_error(ctx, GL_INVALID_ENUM,
1859 "glGetFramebufferAttachmentParameterivEXT(pname)");
1860 }
1861 else {
1862 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
1863 }
1864 return;
1865 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1866 if (!ctx->Extensions.ARB_framebuffer_object) {
1867 _mesa_error(ctx, GL_INVALID_ENUM,
1868 "glGetFramebufferAttachmentParameterivEXT(pname)");
1869 return;
1870 }
1871 else {
1872 gl_format format = att->Renderbuffer->Format;
1873 if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
1874 /* special cases */
1875 *params = GL_INDEX;
1876 }
1877 else {
1878 *params = _mesa_get_format_datatype(format);
1879 }
1880 }
1881 return;
1882 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1883 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1884 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1885 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1886 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1887 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1888 if (!ctx->Extensions.ARB_framebuffer_object) {
1889 _mesa_error(ctx, GL_INVALID_ENUM,
1890 "glGetFramebufferAttachmentParameterivEXT(pname)");
1891 }
1892 else if (att->Texture) {
1893 const struct gl_texture_image *texImage =
1894 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
1895 att->TextureLevel);
1896 if (texImage) {
1897 *params = get_component_bits(pname, texImage->_BaseFormat,
1898 texImage->TexFormat);
1899 }
1900 else {
1901 *params = 0;
1902 }
1903 }
1904 else if (att->Renderbuffer) {
1905 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
1906 att->Renderbuffer->Format);
1907 }
1908 else {
1909 *params = 0;
1910 }
1911 return;
1912 default:
1913 _mesa_error(ctx, GL_INVALID_ENUM,
1914 "glGetFramebufferAttachmentParameterivEXT(pname)");
1915 return;
1916 }
1917 }
1918
1919
1920 void GLAPIENTRY
1921 _mesa_GenerateMipmapEXT(GLenum target)
1922 {
1923 struct gl_texture_unit *texUnit;
1924 struct gl_texture_object *texObj;
1925 GET_CURRENT_CONTEXT(ctx);
1926
1927 ASSERT_OUTSIDE_BEGIN_END(ctx);
1928 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1929
1930 switch (target) {
1931 case GL_TEXTURE_1D:
1932 case GL_TEXTURE_2D:
1933 case GL_TEXTURE_3D:
1934 case GL_TEXTURE_CUBE_MAP:
1935 /* OK, legal value */
1936 break;
1937 default:
1938 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1939 return;
1940 }
1941
1942 texUnit = _mesa_get_current_tex_unit(ctx);
1943 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1944
1945 _mesa_lock_texture(ctx, texObj);
1946 if (target == GL_TEXTURE_CUBE_MAP) {
1947 GLuint face;
1948 for (face = 0; face < 6; face++)
1949 ctx->Driver.GenerateMipmap(ctx,
1950 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1951 texObj);
1952 }
1953 else {
1954 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1955 }
1956 _mesa_unlock_texture(ctx, texObj);
1957 }
1958
1959
1960 #if FEATURE_EXT_framebuffer_blit
1961
1962 static const struct gl_renderbuffer_attachment *
1963 find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
1964 {
1965 GLuint i;
1966 for (i = 0; i < Elements(fb->Attachment); i++) {
1967 if (fb->Attachment[i].Renderbuffer == rb)
1968 return &fb->Attachment[i];
1969 }
1970 return NULL;
1971 }
1972
1973
1974
1975 /**
1976 * Blit rectangular region, optionally from one framebuffer to another.
1977 *
1978 * Note, if the src buffer is multisampled and the dest is not, this is
1979 * when the samples must be resolved to a single color.
1980 */
1981 void GLAPIENTRY
1982 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1983 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1984 GLbitfield mask, GLenum filter)
1985 {
1986 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1987 GL_DEPTH_BUFFER_BIT |
1988 GL_STENCIL_BUFFER_BIT);
1989 const struct gl_framebuffer *readFb, *drawFb;
1990 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1991 GET_CURRENT_CONTEXT(ctx);
1992
1993 ASSERT_OUTSIDE_BEGIN_END(ctx);
1994 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1995
1996 if (ctx->NewState) {
1997 _mesa_update_state(ctx);
1998 }
1999
2000 readFb = ctx->ReadBuffer;
2001 drawFb = ctx->DrawBuffer;
2002
2003 if (!readFb || !drawFb) {
2004 /* This will normally never happen but someday we may want to
2005 * support MakeCurrent() with no drawables.
2006 */
2007 return;
2008 }
2009
2010 /* check for complete framebuffers */
2011 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2012 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2013 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2014 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2015 return;
2016 }
2017
2018 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2019 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2020 return;
2021 }
2022
2023 if (mask & ~legalMaskBits) {
2024 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2025 return;
2026 }
2027
2028 /* depth/stencil must be blitted with nearest filtering */
2029 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2030 && filter != GL_NEAREST) {
2031 _mesa_error(ctx, GL_INVALID_OPERATION,
2032 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2033 return;
2034 }
2035
2036 /* get color read/draw renderbuffers */
2037 if (mask & GL_COLOR_BUFFER_BIT) {
2038 colorReadRb = readFb->_ColorReadBuffer;
2039 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2040 }
2041 else {
2042 colorReadRb = colorDrawRb = NULL;
2043 }
2044
2045 if (mask & GL_STENCIL_BUFFER_BIT) {
2046 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2047 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2048 if (!readRb ||
2049 !drawRb ||
2050 _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2051 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2052 _mesa_error(ctx, GL_INVALID_OPERATION,
2053 "glBlitFramebufferEXT(stencil buffer size mismatch");
2054 return;
2055 }
2056 }
2057
2058 if (mask & GL_DEPTH_BUFFER_BIT) {
2059 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2060 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2061 if (!readRb ||
2062 !drawRb ||
2063 _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2064 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2065 _mesa_error(ctx, GL_INVALID_OPERATION,
2066 "glBlitFramebufferEXT(depth buffer size mismatch");
2067 return;
2068 }
2069 }
2070
2071 if (readFb->Visual.samples > 0 &&
2072 drawFb->Visual.samples > 0 &&
2073 readFb->Visual.samples != drawFb->Visual.samples) {
2074 _mesa_error(ctx, GL_INVALID_OPERATION,
2075 "glBlitFramebufferEXT(mismatched samples");
2076 return;
2077 }
2078
2079 /* extra checks for multisample copies... */
2080 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2081 /* src and dest region sizes must be the same */
2082 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2083 srcY1 - srcY0 != dstY1 - dstY0) {
2084 _mesa_error(ctx, GL_INVALID_OPERATION,
2085 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2086 return;
2087 }
2088
2089 /* color formats must match */
2090 if (colorReadRb &&
2091 colorDrawRb &&
2092 colorReadRb->Format != colorDrawRb->Format) {
2093 _mesa_error(ctx, GL_INVALID_OPERATION,
2094 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2095 return;
2096 }
2097 }
2098
2099 if (!ctx->Extensions.EXT_framebuffer_blit) {
2100 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2101 return;
2102 }
2103
2104 /* Debug code */
2105 if (DEBUG_BLIT) {
2106 _mesa_printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2107 " 0x%x, 0x%x)\n",
2108 srcX0, srcY0, srcX1, srcY1,
2109 dstX0, dstY0, dstX1, dstY1,
2110 mask, filter);
2111 if (colorReadRb) {
2112 const struct gl_renderbuffer_attachment *att;
2113
2114 att = find_attachment(readFb, colorReadRb);
2115 _mesa_printf(" Src FBO %u RB %u (%dx%d) ",
2116 readFb->Name, colorReadRb->Name,
2117 colorReadRb->Width, colorReadRb->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 att = find_attachment(drawFb, colorDrawRb);
2128 _mesa_printf(" Dst FBO %u RB %u (%dx%d) ",
2129 drawFb->Name, colorDrawRb->Name,
2130 colorDrawRb->Width, colorDrawRb->Height);
2131 if (att && att->Texture) {
2132 _mesa_printf("Tex %u tgt 0x%x level %u face %u",
2133 att->Texture->Name,
2134 att->Texture->Target,
2135 att->TextureLevel,
2136 att->CubeMapFace);
2137 }
2138 _mesa_printf("\n");
2139 }
2140 }
2141
2142 ASSERT(ctx->Driver.BlitFramebuffer);
2143 ctx->Driver.BlitFramebuffer(ctx,
2144 srcX0, srcY0, srcX1, srcY1,
2145 dstX0, dstY0, dstX1, dstY1,
2146 mask, filter);
2147 }
2148 #endif /* FEATURE_EXT_framebuffer_blit */