mesa: minor clean-up
[mesa.git] / src / mesa / main / fbobject.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * GL_EXT/ARB_framebuffer_object extensions
29 *
30 * Authors:
31 * Brian Paul
32 */
33
34
35 #include "buffers.h"
36 #include "context.h"
37 #include "fbobject.h"
38 #include "framebuffer.h"
39 #include "hash.h"
40 #include "macros.h"
41 #include "mipmap.h"
42 #include "renderbuffer.h"
43 #include "state.h"
44 #include "teximage.h"
45 #include "texobj.h"
46 #include "texstore.h"
47
48
49 /** Set this to 1 to help debug FBO incompleteness problems */
50 #define DEBUG_FBO 0
51
52
53 /**
54 * Notes:
55 *
56 * None of the GL_EXT_framebuffer_object functions are compiled into
57 * display lists.
58 */
59
60
61
62 /*
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
67 */
68 static struct gl_framebuffer DummyFramebuffer;
69 static struct gl_renderbuffer DummyRenderbuffer;
70
71
72 #define IS_CUBE_FACE(TARGET) \
73 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
74 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
75
76
77 static void
78 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
79 {
80 /* no op */
81 }
82
83 static void
84 delete_dummy_framebuffer(struct gl_framebuffer *fb)
85 {
86 /* no op */
87 }
88
89
90 void
91 _mesa_init_fbobjects(GLcontext *ctx)
92 {
93 DummyFramebuffer.Delete = delete_dummy_framebuffer;
94 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
95 }
96
97
98 /**
99 * Helper routine for getting a gl_renderbuffer.
100 */
101 struct gl_renderbuffer *
102 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
103 {
104 struct gl_renderbuffer *rb;
105
106 if (id == 0)
107 return NULL;
108
109 rb = (struct gl_renderbuffer *)
110 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
111 return rb;
112 }
113
114
115 /**
116 * Helper routine for getting a gl_framebuffer.
117 */
118 struct gl_framebuffer *
119 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
120 {
121 struct gl_framebuffer *fb;
122
123 if (id == 0)
124 return NULL;
125
126 fb = (struct gl_framebuffer *)
127 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
128 return fb;
129 }
130
131
132 /**
133 * Mark the given framebuffer as invalid. This will force the
134 * test for framebuffer completeness to be done before the framebuffer
135 * is used.
136 */
137 static void
138 invalidate_framebuffer(struct gl_framebuffer *fb)
139 {
140 fb->_Status = 0; /* "indeterminate" */
141 }
142
143
144 /**
145 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
146 * gl_renderbuffer_attachment object.
147 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
148 * the depth buffer attachment point.
149 */
150 struct gl_renderbuffer_attachment *
151 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
152 GLenum attachment)
153 {
154 GLuint i;
155
156 switch (attachment) {
157 case GL_COLOR_ATTACHMENT0_EXT:
158 case GL_COLOR_ATTACHMENT1_EXT:
159 case GL_COLOR_ATTACHMENT2_EXT:
160 case GL_COLOR_ATTACHMENT3_EXT:
161 case GL_COLOR_ATTACHMENT4_EXT:
162 case GL_COLOR_ATTACHMENT5_EXT:
163 case GL_COLOR_ATTACHMENT6_EXT:
164 case GL_COLOR_ATTACHMENT7_EXT:
165 case GL_COLOR_ATTACHMENT8_EXT:
166 case GL_COLOR_ATTACHMENT9_EXT:
167 case GL_COLOR_ATTACHMENT10_EXT:
168 case GL_COLOR_ATTACHMENT11_EXT:
169 case GL_COLOR_ATTACHMENT12_EXT:
170 case GL_COLOR_ATTACHMENT13_EXT:
171 case GL_COLOR_ATTACHMENT14_EXT:
172 case GL_COLOR_ATTACHMENT15_EXT:
173 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
174 if (i >= ctx->Const.MaxColorAttachments) {
175 return NULL;
176 }
177 return &fb->Attachment[BUFFER_COLOR0 + i];
178 case GL_DEPTH_STENCIL_ATTACHMENT:
179 /* fall-through */
180 case GL_DEPTH_ATTACHMENT_EXT:
181 return &fb->Attachment[BUFFER_DEPTH];
182 case GL_STENCIL_ATTACHMENT_EXT:
183 return &fb->Attachment[BUFFER_STENCIL];
184 default:
185 return NULL;
186 }
187 }
188
189
190 /**
191 * Remove any texture or renderbuffer attached to the given attachment
192 * point. Update reference counts, etc.
193 */
194 void
195 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
196 {
197 if (att->Type == GL_TEXTURE) {
198 ASSERT(att->Texture);
199 if (ctx->Driver.FinishRenderTexture) {
200 /* tell driver that we're done rendering to this texture. */
201 ctx->Driver.FinishRenderTexture(ctx, att);
202 }
203 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
204 ASSERT(!att->Texture);
205 }
206 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
207 ASSERT(!att->Texture);
208 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
209 ASSERT(!att->Renderbuffer);
210 }
211 att->Type = GL_NONE;
212 att->Complete = GL_TRUE;
213 }
214
215
216 /**
217 * Bind a texture object to an attachment point.
218 * The previous binding, if any, will be removed first.
219 */
220 void
221 _mesa_set_texture_attachment(GLcontext *ctx,
222 struct gl_framebuffer *fb,
223 struct gl_renderbuffer_attachment *att,
224 struct gl_texture_object *texObj,
225 GLenum texTarget, GLuint level, GLuint zoffset)
226 {
227 if (att->Texture == texObj) {
228 /* re-attaching same texture */
229 ASSERT(att->Type == GL_TEXTURE);
230 }
231 else {
232 /* new attachment */
233 _mesa_remove_attachment(ctx, att);
234 att->Type = GL_TEXTURE;
235 assert(!att->Texture);
236 _mesa_reference_texobj(&att->Texture, texObj);
237 }
238
239 /* always update these fields */
240 att->TextureLevel = level;
241 if (IS_CUBE_FACE(texTarget)) {
242 att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
243 }
244 else {
245 att->CubeMapFace = 0;
246 }
247 att->Zoffset = zoffset;
248 att->Complete = GL_FALSE;
249
250 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
251 ctx->Driver.RenderTexture(ctx, fb, att);
252 }
253
254 invalidate_framebuffer(fb);
255 }
256
257
258 /**
259 * Bind a renderbuffer to an attachment point.
260 * The previous binding, if any, will be removed first.
261 */
262 void
263 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
264 struct gl_renderbuffer_attachment *att,
265 struct gl_renderbuffer *rb)
266 {
267 /* XXX check if re-doing same attachment, exit early */
268 _mesa_remove_attachment(ctx, att);
269 att->Type = GL_RENDERBUFFER_EXT;
270 att->Texture = NULL; /* just to be safe */
271 att->Complete = GL_FALSE;
272 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
273 }
274
275
276 /**
277 * Fallback for ctx->Driver.FramebufferRenderbuffer()
278 * Attach a renderbuffer object to a framebuffer object.
279 */
280 void
281 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
282 GLenum attachment, struct gl_renderbuffer *rb)
283 {
284 struct gl_renderbuffer_attachment *att;
285
286 _glthread_LOCK_MUTEX(fb->Mutex);
287
288 att = _mesa_get_attachment(ctx, fb, attachment);
289 ASSERT(att);
290 if (rb) {
291 _mesa_set_renderbuffer_attachment(ctx, att, rb);
292 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
293 /* do stencil attachment here (depth already done above) */
294 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
295 assert(att);
296 _mesa_set_renderbuffer_attachment(ctx, att, rb);
297 }
298 }
299 else {
300 _mesa_remove_attachment(ctx, att);
301 }
302
303 invalidate_framebuffer(fb);
304
305 _glthread_UNLOCK_MUTEX(fb->Mutex);
306 }
307
308
309 /**
310 * For debug only.
311 */
312 static void
313 att_incomplete(const char *msg)
314 {
315 #if DEBUG_FBO
316 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
317 #else
318 (void) msg;
319 #endif
320 }
321
322
323 /**
324 * For debug only.
325 */
326 static void
327 fbo_incomplete(const char *msg, int index)
328 {
329 #if DEBUG_FBO
330 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
331 #else
332 (void) msg;
333 (void) index;
334 #endif
335 }
336
337
338
339
340 /**
341 * Test if an attachment point is complete and update its Complete field.
342 * \param format if GL_COLOR, this is a color attachment point,
343 * if GL_DEPTH, this is a depth component attachment point,
344 * if GL_STENCIL, this is a stencil component attachment point.
345 */
346 static void
347 test_attachment_completeness(const GLcontext *ctx, GLenum format,
348 struct gl_renderbuffer_attachment *att)
349 {
350 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
351
352 /* assume complete */
353 att->Complete = GL_TRUE;
354
355 /* Look for reasons why the attachment might be incomplete */
356 if (att->Type == GL_TEXTURE) {
357 const struct gl_texture_object *texObj = att->Texture;
358 struct gl_texture_image *texImage;
359
360 if (!texObj) {
361 att_incomplete("no texobj");
362 att->Complete = GL_FALSE;
363 return;
364 }
365
366 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
367 if (!texImage) {
368 att_incomplete("no teximage");
369 att->Complete = GL_FALSE;
370 return;
371 }
372 if (texImage->Width < 1 || texImage->Height < 1) {
373 att_incomplete("teximage width/height=0");
374 _mesa_printf("texobj = %u\n", texObj->Name);
375 _mesa_printf("level = %d\n", att->TextureLevel);
376 att->Complete = GL_FALSE;
377 return;
378 }
379 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
380 att_incomplete("bad z offset");
381 att->Complete = GL_FALSE;
382 return;
383 }
384
385 if (format == GL_COLOR) {
386 if (texImage->TexFormat->BaseFormat != GL_RGB &&
387 texImage->TexFormat->BaseFormat != GL_RGBA) {
388 att_incomplete("bad format");
389 att->Complete = GL_FALSE;
390 return;
391 }
392 if (texImage->TexFormat->TexelBytes == 0) {
393 att_incomplete("compressed internalformat");
394 att->Complete = GL_FALSE;
395 return;
396 }
397 }
398 else if (format == GL_DEPTH) {
399 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
400 /* OK */
401 }
402 else if (ctx->Extensions.EXT_packed_depth_stencil &&
403 ctx->Extensions.ARB_depth_texture &&
404 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
405 /* OK */
406 }
407 else {
408 att->Complete = GL_FALSE;
409 att_incomplete("bad depth format");
410 return;
411 }
412 }
413 else {
414 ASSERT(format == GL_STENCIL);
415 ASSERT(att->Renderbuffer->StencilBits);
416 if (ctx->Extensions.EXT_packed_depth_stencil &&
417 ctx->Extensions.ARB_depth_texture &&
418 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
419 /* OK */
420 }
421 else {
422 /* no such thing as stencil-only textures */
423 att_incomplete("illegal stencil texture");
424 att->Complete = GL_FALSE;
425 return;
426 }
427 }
428 }
429 else if (att->Type == GL_RENDERBUFFER_EXT) {
430 ASSERT(att->Renderbuffer);
431 if (!att->Renderbuffer->InternalFormat ||
432 att->Renderbuffer->Width < 1 ||
433 att->Renderbuffer->Height < 1) {
434 att_incomplete("0x0 renderbuffer");
435 att->Complete = GL_FALSE;
436 return;
437 }
438 if (format == GL_COLOR) {
439 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
440 att->Renderbuffer->_BaseFormat != GL_RGBA) {
441 att_incomplete("bad renderbuffer color format");
442 att->Complete = GL_FALSE;
443 return;
444 }
445 ASSERT(att->Renderbuffer->RedBits);
446 ASSERT(att->Renderbuffer->GreenBits);
447 ASSERT(att->Renderbuffer->BlueBits);
448 }
449 else if (format == GL_DEPTH) {
450 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
451 ASSERT(att->Renderbuffer->DepthBits);
452 /* OK */
453 }
454 else if (ctx->Extensions.EXT_packed_depth_stencil &&
455 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
456 ASSERT(att->Renderbuffer->DepthBits);
457 /* OK */
458 }
459 else {
460 att_incomplete("bad renderbuffer depth format");
461 att->Complete = GL_FALSE;
462 return;
463 }
464 }
465 else {
466 assert(format == GL_STENCIL);
467 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
468 ASSERT(att->Renderbuffer->StencilBits);
469 /* OK */
470 }
471 else if (ctx->Extensions.EXT_packed_depth_stencil &&
472 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
473 ASSERT(att->Renderbuffer->StencilBits);
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 #endif
655
656 if (numImages == 0) {
657 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
658 fbo_incomplete("no attachments", -1);
659 return;
660 }
661
662 /* Provisionally set status = COMPLETE ... */
663 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
664
665 /* ... but the driver may say the FB is incomplete.
666 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
667 * if anything.
668 */
669 if (ctx->Driver.ValidateFramebuffer) {
670 ctx->Driver.ValidateFramebuffer(ctx, fb);
671 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
672 fbo_incomplete("driver marked FBO as incomplete", -1);
673 }
674 }
675
676 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
677 /*
678 * Note that if ARB_framebuffer_object is supported and the attached
679 * renderbuffers/textures are different sizes, the framebuffer
680 * width/height will be set to the smallest width/height.
681 */
682 fb->Width = minWidth;
683 fb->Height = minHeight;
684
685 /* finally, update the visual info for the framebuffer */
686 _mesa_update_framebuffer_visual(fb);
687 }
688 }
689
690
691 GLboolean GLAPIENTRY
692 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
693 {
694 GET_CURRENT_CONTEXT(ctx);
695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
696 if (renderbuffer) {
697 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
698 if (rb != NULL && rb != &DummyRenderbuffer)
699 return GL_TRUE;
700 }
701 return GL_FALSE;
702 }
703
704
705 void GLAPIENTRY
706 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
707 {
708 struct gl_renderbuffer *newRb;
709 GET_CURRENT_CONTEXT(ctx);
710
711 ASSERT_OUTSIDE_BEGIN_END(ctx);
712
713 if (target != GL_RENDERBUFFER_EXT) {
714 _mesa_error(ctx, GL_INVALID_ENUM,
715 "glBindRenderbufferEXT(target)");
716 return;
717 }
718
719 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
720 /* The above doesn't fully flush the drivers in the way that a
721 * glFlush does, but that is required here:
722 */
723 if (ctx->Driver.Flush)
724 ctx->Driver.Flush(ctx);
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->_ActualFormat = 0;
980 rb->RedBits =
981 rb->GreenBits =
982 rb->BlueBits =
983 rb->AlphaBits =
984 rb->IndexBits =
985 rb->DepthBits =
986 rb->StencilBits = 0;
987 rb->NumSamples = samples;
988
989 /* Now allocate the storage */
990 ASSERT(rb->AllocStorage);
991 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
992 /* No error - check/set fields now */
993 assert(rb->_ActualFormat);
994 assert(rb->Width == (GLuint) width);
995 assert(rb->Height == (GLuint) height);
996 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
997 rb->DepthBits || rb->StencilBits || rb->IndexBits);
998 rb->InternalFormat = internalFormat;
999 rb->_BaseFormat = baseFormat;
1000 }
1001 else {
1002 /* Probably ran out of memory - clear the fields */
1003 rb->Width = 0;
1004 rb->Height = 0;
1005 rb->InternalFormat = GL_NONE;
1006 rb->_ActualFormat = GL_NONE;
1007 rb->_BaseFormat = GL_NONE;
1008 rb->RedBits =
1009 rb->GreenBits =
1010 rb->BlueBits =
1011 rb->AlphaBits =
1012 rb->IndexBits =
1013 rb->DepthBits =
1014 rb->StencilBits =
1015 rb->NumSamples = 0;
1016 }
1017
1018 /*
1019 test_framebuffer_completeness(ctx, fb);
1020 */
1021 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1022 * points???
1023 */
1024 }
1025
1026
1027 void GLAPIENTRY
1028 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1029 GLsizei width, GLsizei height)
1030 {
1031 /* GL_ARB_fbo says calling this function is equivalent to calling
1032 * glRenderbufferStorageMultisample() with samples=0. We pass in
1033 * a token value here just for error reporting purposes.
1034 */
1035 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1036 }
1037
1038
1039 void GLAPIENTRY
1040 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1041 GLenum internalFormat,
1042 GLsizei width, GLsizei height)
1043 {
1044 renderbuffer_storage(target, internalFormat, width, height, samples);
1045 }
1046
1047
1048
1049 void GLAPIENTRY
1050 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1051 {
1052 struct gl_renderbuffer *rb;
1053 GET_CURRENT_CONTEXT(ctx);
1054
1055 ASSERT_OUTSIDE_BEGIN_END(ctx);
1056
1057 if (target != GL_RENDERBUFFER_EXT) {
1058 _mesa_error(ctx, GL_INVALID_ENUM,
1059 "glGetRenderbufferParameterivEXT(target)");
1060 return;
1061 }
1062
1063 rb = ctx->CurrentRenderbuffer;
1064 if (!rb) {
1065 _mesa_error(ctx, GL_INVALID_OPERATION,
1066 "glGetRenderbufferParameterivEXT");
1067 return;
1068 }
1069
1070 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1071
1072 switch (pname) {
1073 case GL_RENDERBUFFER_WIDTH_EXT:
1074 *params = rb->Width;
1075 return;
1076 case GL_RENDERBUFFER_HEIGHT_EXT:
1077 *params = rb->Height;
1078 return;
1079 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1080 *params = rb->InternalFormat;
1081 return;
1082 case GL_RENDERBUFFER_RED_SIZE_EXT:
1083 *params = rb->RedBits;
1084 break;
1085 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1086 *params = rb->GreenBits;
1087 break;
1088 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1089 *params = rb->BlueBits;
1090 break;
1091 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1092 *params = rb->AlphaBits;
1093 break;
1094 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1095 *params = rb->DepthBits;
1096 break;
1097 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1098 *params = rb->StencilBits;
1099 break;
1100 case GL_RENDERBUFFER_SAMPLES:
1101 if (ctx->Extensions.ARB_framebuffer_object) {
1102 *params = rb->NumSamples;
1103 break;
1104 }
1105 /* fallthrough */
1106 default:
1107 _mesa_error(ctx, GL_INVALID_ENUM,
1108 "glGetRenderbufferParameterivEXT(target)");
1109 return;
1110 }
1111 }
1112
1113
1114 GLboolean GLAPIENTRY
1115 _mesa_IsFramebufferEXT(GLuint framebuffer)
1116 {
1117 GET_CURRENT_CONTEXT(ctx);
1118 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1119 if (framebuffer) {
1120 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1121 if (rb != NULL && rb != &DummyFramebuffer)
1122 return GL_TRUE;
1123 }
1124 return GL_FALSE;
1125 }
1126
1127
1128 static void
1129 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1130 {
1131 GLuint i;
1132 ASSERT(ctx->Driver.RenderTexture);
1133 for (i = 0; i < BUFFER_COUNT; i++) {
1134 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1135 struct gl_texture_object *texObj = att->Texture;
1136 if (texObj
1137 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
1138 ctx->Driver.RenderTexture(ctx, fb, att);
1139 }
1140 }
1141 }
1142
1143
1144 /**
1145 * Examine all the framebuffer's attachments to see if any are textures.
1146 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1147 * notify the device driver that the texture image may have changed.
1148 */
1149 static void
1150 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1151 {
1152 if (ctx->Driver.FinishRenderTexture) {
1153 GLuint i;
1154 for (i = 0; i < BUFFER_COUNT; i++) {
1155 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1156 if (att->Texture && att->Renderbuffer) {
1157 ctx->Driver.FinishRenderTexture(ctx, att);
1158 }
1159 }
1160 }
1161 }
1162
1163
1164 void GLAPIENTRY
1165 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1166 {
1167 struct gl_framebuffer *newFb, *newFbread;
1168 GLboolean bindReadBuf, bindDrawBuf;
1169 GET_CURRENT_CONTEXT(ctx);
1170
1171 #ifdef DEBUG
1172 if (ctx->Extensions.ARB_framebuffer_object) {
1173 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1174 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1175 }
1176 #endif
1177
1178 ASSERT_OUTSIDE_BEGIN_END(ctx);
1179
1180 if (!ctx->Extensions.EXT_framebuffer_object) {
1181 _mesa_error(ctx, GL_INVALID_OPERATION,
1182 "glBindFramebufferEXT(unsupported)");
1183 return;
1184 }
1185
1186 switch (target) {
1187 #if FEATURE_EXT_framebuffer_blit
1188 case GL_DRAW_FRAMEBUFFER_EXT:
1189 if (!ctx->Extensions.EXT_framebuffer_blit) {
1190 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1191 return;
1192 }
1193 bindDrawBuf = GL_TRUE;
1194 bindReadBuf = GL_FALSE;
1195 break;
1196 case GL_READ_FRAMEBUFFER_EXT:
1197 if (!ctx->Extensions.EXT_framebuffer_blit) {
1198 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1199 return;
1200 }
1201 bindDrawBuf = GL_FALSE;
1202 bindReadBuf = GL_TRUE;
1203 break;
1204 #endif
1205 case GL_FRAMEBUFFER_EXT:
1206 bindDrawBuf = GL_TRUE;
1207 bindReadBuf = GL_TRUE;
1208 break;
1209 default:
1210 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1211 return;
1212 }
1213
1214 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1215 if (ctx->Driver.Flush) {
1216 ctx->Driver.Flush(ctx);
1217 }
1218
1219 if (framebuffer) {
1220 /* Binding a user-created framebuffer object */
1221 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1222 if (newFb == &DummyFramebuffer) {
1223 /* ID was reserved, but no real framebuffer object made yet */
1224 newFb = NULL;
1225 }
1226 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1227 /* All FBO IDs must be Gen'd */
1228 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1229 return;
1230 }
1231
1232 if (!newFb) {
1233 /* create new framebuffer object */
1234 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1235 if (!newFb) {
1236 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1237 return;
1238 }
1239 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1240 }
1241 newFbread = newFb;
1242 }
1243 else {
1244 /* Binding the window system framebuffer (which was originally set
1245 * with MakeCurrent).
1246 */
1247 newFb = ctx->WinSysDrawBuffer;
1248 newFbread = ctx->WinSysReadBuffer;
1249 }
1250
1251 ASSERT(newFb);
1252 ASSERT(newFb != &DummyFramebuffer);
1253
1254 /*
1255 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1256 */
1257
1258 if (bindReadBuf) {
1259 if (ctx->ReadBuffer == newFbread)
1260 bindReadBuf = GL_FALSE; /* no change */
1261 else
1262 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1263 }
1264
1265 if (bindDrawBuf) {
1266 /* check if old FB had any texture attachments */
1267 if (ctx->DrawBuffer->Name != 0) {
1268 check_end_texture_render(ctx, ctx->DrawBuffer);
1269 }
1270
1271 if (ctx->DrawBuffer == newFb)
1272 bindDrawBuf = GL_FALSE; /* no change */
1273 else
1274 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1275
1276 if (newFb->Name != 0) {
1277 /* check if newly bound framebuffer has any texture attachments */
1278 check_begin_texture_render(ctx, newFb);
1279 }
1280 }
1281
1282 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1283 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1284 }
1285 }
1286
1287
1288 void GLAPIENTRY
1289 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1290 {
1291 GLint i;
1292 GET_CURRENT_CONTEXT(ctx);
1293
1294 ASSERT_OUTSIDE_BEGIN_END(ctx);
1295 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1296 /* The above doesn't fully flush the drivers in the way that a
1297 * glFlush does, but that is required here:
1298 */
1299 if (ctx->Driver.Flush)
1300 ctx->Driver.Flush(ctx);
1301
1302 for (i = 0; i < n; i++) {
1303 if (framebuffers[i] > 0) {
1304 struct gl_framebuffer *fb;
1305 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1306 if (fb) {
1307 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1308
1309 /* check if deleting currently bound framebuffer object */
1310 if (fb == ctx->DrawBuffer) {
1311 /* bind default */
1312 ASSERT(fb->RefCount >= 2);
1313 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1314 }
1315 if (fb == ctx->ReadBuffer) {
1316 /* bind default */
1317 ASSERT(fb->RefCount >= 2);
1318 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1319 }
1320
1321 /* remove from hash table immediately, to free the ID */
1322 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1323
1324 if (fb != &DummyFramebuffer) {
1325 /* But the object will not be freed until it's no longer
1326 * bound in any context.
1327 */
1328 _mesa_reference_framebuffer(&fb, NULL);
1329 }
1330 }
1331 }
1332 }
1333 }
1334
1335
1336 void GLAPIENTRY
1337 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1338 {
1339 GET_CURRENT_CONTEXT(ctx);
1340 GLuint first;
1341 GLint i;
1342
1343 ASSERT_OUTSIDE_BEGIN_END(ctx);
1344
1345 if (n < 0) {
1346 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1347 return;
1348 }
1349
1350 if (!framebuffers)
1351 return;
1352
1353 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1354
1355 for (i = 0; i < n; i++) {
1356 GLuint name = first + i;
1357 framebuffers[i] = name;
1358 /* insert dummy placeholder into hash table */
1359 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1360 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1361 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1362 }
1363 }
1364
1365
1366
1367 GLenum GLAPIENTRY
1368 _mesa_CheckFramebufferStatusEXT(GLenum target)
1369 {
1370 struct gl_framebuffer *buffer;
1371 GET_CURRENT_CONTEXT(ctx);
1372
1373 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1374
1375 switch (target) {
1376 #if FEATURE_EXT_framebuffer_blit
1377 case GL_DRAW_FRAMEBUFFER_EXT:
1378 if (!ctx->Extensions.EXT_framebuffer_blit) {
1379 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1380 return 0;
1381 }
1382 buffer = ctx->DrawBuffer;
1383 break;
1384 case GL_READ_FRAMEBUFFER_EXT:
1385 if (!ctx->Extensions.EXT_framebuffer_blit) {
1386 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1387 return 0;
1388 }
1389 buffer = ctx->ReadBuffer;
1390 break;
1391 #endif
1392 case GL_FRAMEBUFFER_EXT:
1393 buffer = ctx->DrawBuffer;
1394 break;
1395 default:
1396 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1397 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1398 }
1399
1400 if (buffer->Name == 0) {
1401 /* The window system / default framebuffer is always complete */
1402 return GL_FRAMEBUFFER_COMPLETE_EXT;
1403 }
1404
1405 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1406
1407 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1408 _mesa_test_framebuffer_completeness(ctx, buffer);
1409 }
1410
1411 return buffer->_Status;
1412 }
1413
1414
1415
1416 /**
1417 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1418 */
1419 static void
1420 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1421 GLenum attachment, GLenum textarget, GLuint texture,
1422 GLint level, GLint zoffset)
1423 {
1424 struct gl_renderbuffer_attachment *att;
1425 struct gl_texture_object *texObj = NULL;
1426 struct gl_framebuffer *fb;
1427 GLboolean error = GL_FALSE;
1428
1429 ASSERT_OUTSIDE_BEGIN_END(ctx);
1430
1431 switch (target) {
1432 case GL_READ_FRAMEBUFFER_EXT:
1433 error = !ctx->Extensions.EXT_framebuffer_blit;
1434 fb = ctx->ReadBuffer;
1435 break;
1436 case GL_DRAW_FRAMEBUFFER_EXT:
1437 error = !ctx->Extensions.EXT_framebuffer_blit;
1438 /* fall-through */
1439 case GL_FRAMEBUFFER_EXT:
1440 fb = ctx->DrawBuffer;
1441 break;
1442 default:
1443 error = GL_TRUE;
1444 }
1445
1446 if (error) {
1447 _mesa_error(ctx, GL_INVALID_ENUM,
1448 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1449 return;
1450 }
1451
1452 ASSERT(fb);
1453
1454 /* check framebuffer binding */
1455 if (fb->Name == 0) {
1456 _mesa_error(ctx, GL_INVALID_OPERATION,
1457 "glFramebufferTexture%sEXT", caller);
1458 return;
1459 }
1460
1461
1462 /* The textarget, level, and zoffset parameters are only validated if
1463 * texture is non-zero.
1464 */
1465 if (texture) {
1466 GLboolean err = GL_TRUE;
1467
1468 texObj = _mesa_lookup_texture(ctx, texture);
1469 if (texObj != NULL) {
1470 if (textarget == 0) {
1471 err = (texObj->Target != GL_TEXTURE_3D) &&
1472 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1473 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1474 }
1475 else {
1476 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1477 ? !IS_CUBE_FACE(textarget)
1478 : (texObj->Target != textarget);
1479 }
1480 }
1481
1482 if (err) {
1483 _mesa_error(ctx, GL_INVALID_OPERATION,
1484 "glFramebufferTexture%sEXT(texture target mismatch)",
1485 caller);
1486 return;
1487 }
1488
1489 if (texObj->Target == GL_TEXTURE_3D) {
1490 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1491 if (zoffset < 0 || zoffset >= maxSize) {
1492 _mesa_error(ctx, GL_INVALID_VALUE,
1493 "glFramebufferTexture%sEXT(zoffset)", caller);
1494 return;
1495 }
1496 }
1497 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1498 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1499 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1500 _mesa_error(ctx, GL_INVALID_VALUE,
1501 "glFramebufferTexture%sEXT(layer)", caller);
1502 return;
1503 }
1504 }
1505
1506 if ((level < 0) ||
1507 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1508 _mesa_error(ctx, GL_INVALID_VALUE,
1509 "glFramebufferTexture%sEXT(level)", caller);
1510 return;
1511 }
1512 }
1513
1514 att = _mesa_get_attachment(ctx, fb, attachment);
1515 if (att == NULL) {
1516 _mesa_error(ctx, GL_INVALID_ENUM,
1517 "glFramebufferTexture%sEXT(attachment)", caller);
1518 return;
1519 }
1520
1521 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1522 /* the texture format must be depth+stencil */
1523 const struct gl_texture_image *texImg;
1524 texImg = texObj->Image[0][texObj->BaseLevel];
1525 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1526 _mesa_error(ctx, GL_INVALID_OPERATION,
1527 "glFramebufferTexture%sEXT(texture is not"
1528 " DEPTH_STENCIL format)", caller);
1529 return;
1530 }
1531 }
1532
1533 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1534 /* The above doesn't fully flush the drivers in the way that a
1535 * glFlush does, but that is required here:
1536 */
1537 if (ctx->Driver.Flush)
1538 ctx->Driver.Flush(ctx);
1539
1540 _glthread_LOCK_MUTEX(fb->Mutex);
1541 if (texObj) {
1542 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1543 level, zoffset);
1544 /* Set the render-to-texture flag. We'll check this flag in
1545 * glTexImage() and friends to determine if we need to revalidate
1546 * any FBOs that might be rendering into this texture.
1547 * This flag never gets cleared since it's non-trivial to determine
1548 * when all FBOs might be done rendering to this texture. That's OK
1549 * though since it's uncommon to render to a texture then repeatedly
1550 * call glTexImage() to change images in the texture.
1551 */
1552 texObj->_RenderToTexture = GL_TRUE;
1553 }
1554 else {
1555 _mesa_remove_attachment(ctx, att);
1556 }
1557
1558 invalidate_framebuffer(fb);
1559
1560 _glthread_UNLOCK_MUTEX(fb->Mutex);
1561 }
1562
1563
1564
1565 void GLAPIENTRY
1566 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1567 GLenum textarget, GLuint texture, GLint level)
1568 {
1569 GET_CURRENT_CONTEXT(ctx);
1570
1571 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1572 _mesa_error(ctx, GL_INVALID_ENUM,
1573 "glFramebufferTexture1DEXT(textarget)");
1574 return;
1575 }
1576
1577 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1578 level, 0);
1579 }
1580
1581
1582 void GLAPIENTRY
1583 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1584 GLenum textarget, GLuint texture, GLint level)
1585 {
1586 GET_CURRENT_CONTEXT(ctx);
1587
1588 if ((texture != 0) &&
1589 (textarget != GL_TEXTURE_2D) &&
1590 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1591 (!IS_CUBE_FACE(textarget))) {
1592 _mesa_error(ctx, GL_INVALID_OPERATION,
1593 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1594 return;
1595 }
1596
1597 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1598 level, 0);
1599 }
1600
1601
1602 void GLAPIENTRY
1603 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1604 GLenum textarget, GLuint texture,
1605 GLint level, GLint zoffset)
1606 {
1607 GET_CURRENT_CONTEXT(ctx);
1608
1609 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1610 _mesa_error(ctx, GL_INVALID_ENUM,
1611 "glFramebufferTexture3DEXT(textarget)");
1612 return;
1613 }
1614
1615 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1616 level, zoffset);
1617 }
1618
1619
1620 void GLAPIENTRY
1621 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1622 GLuint texture, GLint level, GLint layer)
1623 {
1624 GET_CURRENT_CONTEXT(ctx);
1625
1626 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1627 level, layer);
1628 }
1629
1630
1631 void GLAPIENTRY
1632 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1633 GLenum renderbufferTarget,
1634 GLuint renderbuffer)
1635 {
1636 struct gl_renderbuffer_attachment *att;
1637 struct gl_framebuffer *fb;
1638 struct gl_renderbuffer *rb;
1639 GET_CURRENT_CONTEXT(ctx);
1640
1641 ASSERT_OUTSIDE_BEGIN_END(ctx);
1642
1643 switch (target) {
1644 #if FEATURE_EXT_framebuffer_blit
1645 case GL_DRAW_FRAMEBUFFER_EXT:
1646 if (!ctx->Extensions.EXT_framebuffer_blit) {
1647 _mesa_error(ctx, GL_INVALID_ENUM,
1648 "glFramebufferRenderbufferEXT(target)");
1649 return;
1650 }
1651 fb = ctx->DrawBuffer;
1652 break;
1653 case GL_READ_FRAMEBUFFER_EXT:
1654 if (!ctx->Extensions.EXT_framebuffer_blit) {
1655 _mesa_error(ctx, GL_INVALID_ENUM,
1656 "glFramebufferRenderbufferEXT(target)");
1657 return;
1658 }
1659 fb = ctx->ReadBuffer;
1660 break;
1661 #endif
1662 case GL_FRAMEBUFFER_EXT:
1663 fb = ctx->DrawBuffer;
1664 break;
1665 default:
1666 _mesa_error(ctx, GL_INVALID_ENUM,
1667 "glFramebufferRenderbufferEXT(target)");
1668 return;
1669 }
1670
1671 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1672 _mesa_error(ctx, GL_INVALID_ENUM,
1673 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1674 return;
1675 }
1676
1677 if (fb->Name == 0) {
1678 /* Can't attach new renderbuffers to a window system framebuffer */
1679 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1680 return;
1681 }
1682
1683 att = _mesa_get_attachment(ctx, fb, attachment);
1684 if (att == NULL) {
1685 _mesa_error(ctx, GL_INVALID_ENUM,
1686 "glFramebufferRenderbufferEXT(attachment)");
1687 return;
1688 }
1689
1690 if (renderbuffer) {
1691 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1692 if (!rb) {
1693 _mesa_error(ctx, GL_INVALID_OPERATION,
1694 "glFramebufferRenderbufferEXT(renderbuffer)");
1695 return;
1696 }
1697 }
1698 else {
1699 /* remove renderbuffer attachment */
1700 rb = NULL;
1701 }
1702
1703 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1704 /* make sure the renderbuffer is a depth/stencil format */
1705 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1706 _mesa_error(ctx, GL_INVALID_OPERATION,
1707 "glFramebufferRenderbufferEXT(renderbuffer"
1708 " is not DEPTH_STENCIL format)");
1709 return;
1710 }
1711 }
1712
1713
1714 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1715 /* The above doesn't fully flush the drivers in the way that a
1716 * glFlush does, but that is required here:
1717 */
1718 if (ctx->Driver.Flush)
1719 ctx->Driver.Flush(ctx);
1720
1721 assert(ctx->Driver.FramebufferRenderbuffer);
1722 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1723
1724 /* Some subsequent GL commands may depend on the framebuffer's visual
1725 * after the binding is updated. Update visual info now.
1726 */
1727 _mesa_update_framebuffer_visual(fb);
1728 }
1729
1730
1731 void GLAPIENTRY
1732 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1733 GLenum pname, GLint *params)
1734 {
1735 const struct gl_renderbuffer_attachment *att;
1736 struct gl_framebuffer *buffer;
1737 GET_CURRENT_CONTEXT(ctx);
1738
1739 ASSERT_OUTSIDE_BEGIN_END(ctx);
1740
1741 switch (target) {
1742 #if FEATURE_EXT_framebuffer_blit
1743 case GL_DRAW_FRAMEBUFFER_EXT:
1744 if (!ctx->Extensions.EXT_framebuffer_blit) {
1745 _mesa_error(ctx, GL_INVALID_ENUM,
1746 "glGetFramebufferAttachmentParameterivEXT(target)");
1747 return;
1748 }
1749 buffer = ctx->DrawBuffer;
1750 break;
1751 case GL_READ_FRAMEBUFFER_EXT:
1752 if (!ctx->Extensions.EXT_framebuffer_blit) {
1753 _mesa_error(ctx, GL_INVALID_ENUM,
1754 "glGetFramebufferAttachmentParameterivEXT(target)");
1755 return;
1756 }
1757 buffer = ctx->ReadBuffer;
1758 break;
1759 #endif
1760 case GL_FRAMEBUFFER_EXT:
1761 buffer = ctx->DrawBuffer;
1762 break;
1763 default:
1764 _mesa_error(ctx, GL_INVALID_ENUM,
1765 "glGetFramebufferAttachmentParameterivEXT(target)");
1766 return;
1767 }
1768
1769 if (buffer->Name == 0) {
1770 _mesa_error(ctx, GL_INVALID_OPERATION,
1771 "glGetFramebufferAttachmentParameterivEXT");
1772 return;
1773 }
1774
1775 att = _mesa_get_attachment(ctx, buffer, attachment);
1776 if (att == NULL) {
1777 _mesa_error(ctx, GL_INVALID_ENUM,
1778 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1779 return;
1780 }
1781
1782 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1783 /* the depth and stencil attachments must point to the same buffer */
1784 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1785 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1786 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1787 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1788 _mesa_error(ctx, GL_INVALID_OPERATION,
1789 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1790 " attachments differ)");
1791 return;
1792 }
1793 }
1794
1795 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1796 /* The above doesn't fully flush the drivers in the way that a
1797 * glFlush does, but that is required here:
1798 */
1799 if (ctx->Driver.Flush)
1800 ctx->Driver.Flush(ctx);
1801
1802 switch (pname) {
1803 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1804 *params = att->Type;
1805 return;
1806 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1807 if (att->Type == GL_RENDERBUFFER_EXT) {
1808 *params = att->Renderbuffer->Name;
1809 }
1810 else if (att->Type == GL_TEXTURE) {
1811 *params = att->Texture->Name;
1812 }
1813 else {
1814 _mesa_error(ctx, GL_INVALID_ENUM,
1815 "glGetFramebufferAttachmentParameterivEXT(pname)");
1816 }
1817 return;
1818 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1819 if (att->Type == GL_TEXTURE) {
1820 *params = att->TextureLevel;
1821 }
1822 else {
1823 _mesa_error(ctx, GL_INVALID_ENUM,
1824 "glGetFramebufferAttachmentParameterivEXT(pname)");
1825 }
1826 return;
1827 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1828 if (att->Type == GL_TEXTURE) {
1829 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1830 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1831 }
1832 else {
1833 *params = 0;
1834 }
1835 }
1836 else {
1837 _mesa_error(ctx, GL_INVALID_ENUM,
1838 "glGetFramebufferAttachmentParameterivEXT(pname)");
1839 }
1840 return;
1841 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1842 if (att->Type == GL_TEXTURE) {
1843 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1844 *params = att->Zoffset;
1845 }
1846 else {
1847 *params = 0;
1848 }
1849 }
1850 else {
1851 _mesa_error(ctx, GL_INVALID_ENUM,
1852 "glGetFramebufferAttachmentParameterivEXT(pname)");
1853 }
1854 return;
1855 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1856 if (!ctx->Extensions.ARB_framebuffer_object) {
1857 _mesa_error(ctx, GL_INVALID_ENUM,
1858 "glGetFramebufferAttachmentParameterivEXT(pname)");
1859 }
1860 else {
1861 *params = att->Renderbuffer->ColorEncoding;
1862 }
1863 return;
1864 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1865 if (!ctx->Extensions.ARB_framebuffer_object) {
1866 _mesa_error(ctx, GL_INVALID_ENUM,
1867 "glGetFramebufferAttachmentParameterivEXT(pname)");
1868 return;
1869 }
1870 else {
1871 *params = att->Renderbuffer->ComponentType;
1872 }
1873 return;
1874 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1875 if (!ctx->Extensions.ARB_framebuffer_object) {
1876 _mesa_error(ctx, GL_INVALID_ENUM,
1877 "glGetFramebufferAttachmentParameterivEXT(pname)");
1878 }
1879 else {
1880 *params = att->Renderbuffer->RedBits;
1881 }
1882 return;
1883 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1884 if (!ctx->Extensions.ARB_framebuffer_object) {
1885 _mesa_error(ctx, GL_INVALID_ENUM,
1886 "glGetFramebufferAttachmentParameterivEXT(pname)");
1887 }
1888 else {
1889 *params = att->Renderbuffer->GreenBits;
1890 }
1891 return;
1892 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1893 if (!ctx->Extensions.ARB_framebuffer_object) {
1894 _mesa_error(ctx, GL_INVALID_ENUM,
1895 "glGetFramebufferAttachmentParameterivEXT(pname)");
1896 }
1897 else {
1898 *params = att->Renderbuffer->BlueBits;
1899 }
1900 return;
1901 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1902 if (!ctx->Extensions.ARB_framebuffer_object) {
1903 _mesa_error(ctx, GL_INVALID_ENUM,
1904 "glGetFramebufferAttachmentParameterivEXT(pname)");
1905 }
1906 else {
1907 *params = att->Renderbuffer->AlphaBits;
1908 }
1909 return;
1910 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1911 if (!ctx->Extensions.ARB_framebuffer_object) {
1912 _mesa_error(ctx, GL_INVALID_ENUM,
1913 "glGetFramebufferAttachmentParameterivEXT(pname)");
1914 }
1915 else {
1916 *params = att->Renderbuffer->DepthBits;
1917 }
1918 return;
1919 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1920 if (!ctx->Extensions.ARB_framebuffer_object) {
1921 _mesa_error(ctx, GL_INVALID_ENUM,
1922 "glGetFramebufferAttachmentParameterivEXT(pname)");
1923 }
1924 else {
1925 *params = att->Renderbuffer->StencilBits;
1926 }
1927 return;
1928 default:
1929 _mesa_error(ctx, GL_INVALID_ENUM,
1930 "glGetFramebufferAttachmentParameterivEXT(pname)");
1931 return;
1932 }
1933 }
1934
1935
1936 void GLAPIENTRY
1937 _mesa_GenerateMipmapEXT(GLenum target)
1938 {
1939 struct gl_texture_unit *texUnit;
1940 struct gl_texture_object *texObj;
1941 GET_CURRENT_CONTEXT(ctx);
1942
1943 ASSERT_OUTSIDE_BEGIN_END(ctx);
1944 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1945
1946 switch (target) {
1947 case GL_TEXTURE_1D:
1948 case GL_TEXTURE_2D:
1949 case GL_TEXTURE_3D:
1950 case GL_TEXTURE_CUBE_MAP:
1951 /* OK, legal value */
1952 break;
1953 default:
1954 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1955 return;
1956 }
1957
1958 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1959 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1960
1961 _mesa_lock_texture(ctx, texObj);
1962 if (target == GL_TEXTURE_CUBE_MAP) {
1963 GLuint face;
1964 for (face = 0; face < 6; face++)
1965 ctx->Driver.GenerateMipmap(ctx,
1966 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1967 texObj);
1968 }
1969 else {
1970 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1971 }
1972 _mesa_unlock_texture(ctx, texObj);
1973 }
1974
1975
1976 #if FEATURE_EXT_framebuffer_blit
1977 /**
1978 * Blit rectangular region, optionally from one framebuffer to another.
1979 *
1980 * Note, if the src buffer is multisampled and the dest is not, this is
1981 * when the samples must be resolved to a single color.
1982 */
1983 void GLAPIENTRY
1984 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1985 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1986 GLbitfield mask, GLenum filter)
1987 {
1988 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1989 GL_DEPTH_BUFFER_BIT |
1990 GL_STENCIL_BUFFER_BIT);
1991 const struct gl_framebuffer *readFb, *drawFb;
1992 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1993 GET_CURRENT_CONTEXT(ctx);
1994
1995 ASSERT_OUTSIDE_BEGIN_END(ctx);
1996 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1997
1998 if (ctx->NewState) {
1999 _mesa_update_state(ctx);
2000 }
2001
2002 readFb = ctx->ReadBuffer;
2003 drawFb = ctx->DrawBuffer;
2004
2005 if (!readFb || !drawFb) {
2006 /* This will normally never happen but someday we may want to
2007 * support MakeCurrent() with no drawables.
2008 */
2009 return;
2010 }
2011
2012 /* check for complete framebuffers */
2013 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2014 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2015 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2016 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2017 return;
2018 }
2019
2020 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2021 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2022 return;
2023 }
2024
2025 if (mask & ~legalMaskBits) {
2026 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2027 return;
2028 }
2029
2030 /* depth/stencil must be blitted with nearest filtering */
2031 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2032 && filter != GL_NEAREST) {
2033 _mesa_error(ctx, GL_INVALID_OPERATION,
2034 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2035 return;
2036 }
2037
2038 /* get color read/draw renderbuffers */
2039 if (mask & GL_COLOR_BUFFER_BIT) {
2040 colorReadRb = readFb->_ColorReadBuffer;
2041 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2042 }
2043 else {
2044 colorReadRb = colorDrawRb = NULL;
2045 }
2046
2047 if (mask & GL_STENCIL_BUFFER_BIT) {
2048 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2049 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2050 if (!readRb ||
2051 !drawRb ||
2052 readRb->StencilBits != drawRb->StencilBits) {
2053 _mesa_error(ctx, GL_INVALID_OPERATION,
2054 "glBlitFramebufferEXT(stencil buffer size mismatch");
2055 return;
2056 }
2057 }
2058
2059 if (mask & GL_DEPTH_BUFFER_BIT) {
2060 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2061 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2062 if (!readRb ||
2063 !drawRb ||
2064 readRb->DepthBits != drawRb->DepthBits) {
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->_ActualFormat != colorDrawRb->_ActualFormat) {
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 ASSERT(ctx->Driver.BlitFramebuffer);
2105 ctx->Driver.BlitFramebuffer(ctx,
2106 srcX0, srcY0, srcX1, srcY1,
2107 dstX0, dstY0, dstX1, dstY1,
2108 mask, filter);
2109 }
2110 #endif /* FEATURE_EXT_framebuffer_blit */