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