Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[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 && texObj->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
1211 if (framebuffer) {
1212 /* Binding a user-created framebuffer object */
1213 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1214 if (newFb == &DummyFramebuffer) {
1215 /* ID was reserved, but no real framebuffer object made yet */
1216 newFb = NULL;
1217 }
1218 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1219 /* All FBO IDs must be Gen'd */
1220 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1221 return;
1222 }
1223
1224 if (!newFb) {
1225 /* create new framebuffer object */
1226 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1227 if (!newFb) {
1228 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1229 return;
1230 }
1231 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1232 }
1233 newFbread = newFb;
1234 }
1235 else {
1236 /* Binding the window system framebuffer (which was originally set
1237 * with MakeCurrent).
1238 */
1239 newFb = ctx->WinSysDrawBuffer;
1240 newFbread = ctx->WinSysReadBuffer;
1241 }
1242
1243 ASSERT(newFb);
1244 ASSERT(newFb != &DummyFramebuffer);
1245
1246 /*
1247 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1248 */
1249
1250 if (bindReadBuf) {
1251 if (ctx->ReadBuffer == newFbread)
1252 bindReadBuf = GL_FALSE; /* no change */
1253 else
1254 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1255 }
1256
1257 if (bindDrawBuf) {
1258 /* check if old FB had any texture attachments */
1259 if (ctx->DrawBuffer->Name != 0) {
1260 check_end_texture_render(ctx, ctx->DrawBuffer);
1261 }
1262
1263 if (ctx->DrawBuffer == newFb)
1264 bindDrawBuf = GL_FALSE; /* no change */
1265 else
1266 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1267
1268 if (newFb->Name != 0) {
1269 /* check if newly bound framebuffer has any texture attachments */
1270 check_begin_texture_render(ctx, newFb);
1271 }
1272 }
1273
1274 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1275 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1276 }
1277 }
1278
1279
1280 void GLAPIENTRY
1281 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1282 {
1283 GLint i;
1284 GET_CURRENT_CONTEXT(ctx);
1285
1286 ASSERT_OUTSIDE_BEGIN_END(ctx);
1287 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1288
1289 for (i = 0; i < n; i++) {
1290 if (framebuffers[i] > 0) {
1291 struct gl_framebuffer *fb;
1292 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1293 if (fb) {
1294 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1295
1296 /* check if deleting currently bound framebuffer object */
1297 if (fb == ctx->DrawBuffer) {
1298 /* bind default */
1299 ASSERT(fb->RefCount >= 2);
1300 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1301 }
1302 if (fb == ctx->ReadBuffer) {
1303 /* bind default */
1304 ASSERT(fb->RefCount >= 2);
1305 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1306 }
1307
1308 /* remove from hash table immediately, to free the ID */
1309 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1310
1311 if (fb != &DummyFramebuffer) {
1312 /* But the object will not be freed until it's no longer
1313 * bound in any context.
1314 */
1315 _mesa_reference_framebuffer(&fb, NULL);
1316 }
1317 }
1318 }
1319 }
1320 }
1321
1322
1323 void GLAPIENTRY
1324 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1325 {
1326 GET_CURRENT_CONTEXT(ctx);
1327 GLuint first;
1328 GLint i;
1329
1330 ASSERT_OUTSIDE_BEGIN_END(ctx);
1331
1332 if (n < 0) {
1333 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1334 return;
1335 }
1336
1337 if (!framebuffers)
1338 return;
1339
1340 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1341
1342 for (i = 0; i < n; i++) {
1343 GLuint name = first + i;
1344 framebuffers[i] = name;
1345 /* insert dummy placeholder into hash table */
1346 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1347 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1348 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1349 }
1350 }
1351
1352
1353
1354 GLenum GLAPIENTRY
1355 _mesa_CheckFramebufferStatusEXT(GLenum target)
1356 {
1357 struct gl_framebuffer *buffer;
1358 GET_CURRENT_CONTEXT(ctx);
1359
1360 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1361
1362 switch (target) {
1363 #if FEATURE_EXT_framebuffer_blit
1364 case GL_DRAW_FRAMEBUFFER_EXT:
1365 if (!ctx->Extensions.EXT_framebuffer_blit) {
1366 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1367 return 0;
1368 }
1369 buffer = ctx->DrawBuffer;
1370 break;
1371 case GL_READ_FRAMEBUFFER_EXT:
1372 if (!ctx->Extensions.EXT_framebuffer_blit) {
1373 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1374 return 0;
1375 }
1376 buffer = ctx->ReadBuffer;
1377 break;
1378 #endif
1379 case GL_FRAMEBUFFER_EXT:
1380 buffer = ctx->DrawBuffer;
1381 break;
1382 default:
1383 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1384 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1385 }
1386
1387 if (buffer->Name == 0) {
1388 /* The window system / default framebuffer is always complete */
1389 return GL_FRAMEBUFFER_COMPLETE_EXT;
1390 }
1391
1392 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1393
1394 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1395 _mesa_test_framebuffer_completeness(ctx, buffer);
1396 }
1397
1398 return buffer->_Status;
1399 }
1400
1401
1402
1403 /**
1404 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1405 */
1406 static void
1407 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1408 GLenum attachment, GLenum textarget, GLuint texture,
1409 GLint level, GLint zoffset)
1410 {
1411 struct gl_renderbuffer_attachment *att;
1412 struct gl_texture_object *texObj = NULL;
1413 struct gl_framebuffer *fb;
1414 GLboolean error = GL_FALSE;
1415
1416 ASSERT_OUTSIDE_BEGIN_END(ctx);
1417
1418 switch (target) {
1419 case GL_READ_FRAMEBUFFER_EXT:
1420 error = !ctx->Extensions.EXT_framebuffer_blit;
1421 fb = ctx->ReadBuffer;
1422 break;
1423 case GL_DRAW_FRAMEBUFFER_EXT:
1424 error = !ctx->Extensions.EXT_framebuffer_blit;
1425 /* fall-through */
1426 case GL_FRAMEBUFFER_EXT:
1427 fb = ctx->DrawBuffer;
1428 break;
1429 default:
1430 error = GL_TRUE;
1431 }
1432
1433 if (error) {
1434 _mesa_error(ctx, GL_INVALID_ENUM,
1435 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1436 return;
1437 }
1438
1439 ASSERT(fb);
1440
1441 /* check framebuffer binding */
1442 if (fb->Name == 0) {
1443 _mesa_error(ctx, GL_INVALID_OPERATION,
1444 "glFramebufferTexture%sEXT", caller);
1445 return;
1446 }
1447
1448
1449 /* The textarget, level, and zoffset parameters are only validated if
1450 * texture is non-zero.
1451 */
1452 if (texture) {
1453 GLboolean err = GL_TRUE;
1454
1455 texObj = _mesa_lookup_texture(ctx, texture);
1456 if (texObj != NULL) {
1457 if (textarget == 0) {
1458 err = (texObj->Target != GL_TEXTURE_3D) &&
1459 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1460 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1461 }
1462 else {
1463 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1464 ? !IS_CUBE_FACE(textarget)
1465 : (texObj->Target != textarget);
1466 }
1467 }
1468
1469 if (err) {
1470 _mesa_error(ctx, GL_INVALID_OPERATION,
1471 "glFramebufferTexture%sEXT(texture target mismatch)",
1472 caller);
1473 return;
1474 }
1475
1476 if (texObj->Target == GL_TEXTURE_3D) {
1477 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1478 if (zoffset < 0 || zoffset >= maxSize) {
1479 _mesa_error(ctx, GL_INVALID_VALUE,
1480 "glFramebufferTexture%sEXT(zoffset)", caller);
1481 return;
1482 }
1483 }
1484 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1485 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1486 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1487 _mesa_error(ctx, GL_INVALID_VALUE,
1488 "glFramebufferTexture%sEXT(layer)", caller);
1489 return;
1490 }
1491 }
1492
1493 if ((level < 0) ||
1494 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1495 _mesa_error(ctx, GL_INVALID_VALUE,
1496 "glFramebufferTexture%sEXT(level)", caller);
1497 return;
1498 }
1499 }
1500
1501 att = _mesa_get_attachment(ctx, fb, attachment);
1502 if (att == NULL) {
1503 _mesa_error(ctx, GL_INVALID_ENUM,
1504 "glFramebufferTexture%sEXT(attachment)", caller);
1505 return;
1506 }
1507
1508 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1509 /* the texture format must be depth+stencil */
1510 const struct gl_texture_image *texImg;
1511 texImg = texObj->Image[0][texObj->BaseLevel];
1512 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1513 _mesa_error(ctx, GL_INVALID_OPERATION,
1514 "glFramebufferTexture%sEXT(texture is not"
1515 " DEPTH_STENCIL format)", caller);
1516 return;
1517 }
1518 }
1519
1520 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1521
1522 _glthread_LOCK_MUTEX(fb->Mutex);
1523 if (texObj) {
1524 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1525 level, zoffset);
1526 /* Set the render-to-texture flag. We'll check this flag in
1527 * glTexImage() and friends to determine if we need to revalidate
1528 * any FBOs that might be rendering into this texture.
1529 * This flag never gets cleared since it's non-trivial to determine
1530 * when all FBOs might be done rendering to this texture. That's OK
1531 * though since it's uncommon to render to a texture then repeatedly
1532 * call glTexImage() to change images in the texture.
1533 */
1534 texObj->_RenderToTexture = GL_TRUE;
1535 }
1536 else {
1537 _mesa_remove_attachment(ctx, att);
1538 }
1539
1540 invalidate_framebuffer(fb);
1541
1542 _glthread_UNLOCK_MUTEX(fb->Mutex);
1543 }
1544
1545
1546
1547 void GLAPIENTRY
1548 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1549 GLenum textarget, GLuint texture, GLint level)
1550 {
1551 GET_CURRENT_CONTEXT(ctx);
1552
1553 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1554 _mesa_error(ctx, GL_INVALID_ENUM,
1555 "glFramebufferTexture1DEXT(textarget)");
1556 return;
1557 }
1558
1559 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1560 level, 0);
1561 }
1562
1563
1564 void GLAPIENTRY
1565 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1566 GLenum textarget, GLuint texture, GLint level)
1567 {
1568 GET_CURRENT_CONTEXT(ctx);
1569
1570 if ((texture != 0) &&
1571 (textarget != GL_TEXTURE_2D) &&
1572 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1573 (!IS_CUBE_FACE(textarget))) {
1574 _mesa_error(ctx, GL_INVALID_OPERATION,
1575 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1576 return;
1577 }
1578
1579 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1580 level, 0);
1581 }
1582
1583
1584 void GLAPIENTRY
1585 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1586 GLenum textarget, GLuint texture,
1587 GLint level, GLint zoffset)
1588 {
1589 GET_CURRENT_CONTEXT(ctx);
1590
1591 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1592 _mesa_error(ctx, GL_INVALID_ENUM,
1593 "glFramebufferTexture3DEXT(textarget)");
1594 return;
1595 }
1596
1597 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1598 level, zoffset);
1599 }
1600
1601
1602 void GLAPIENTRY
1603 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1604 GLuint texture, GLint level, GLint layer)
1605 {
1606 GET_CURRENT_CONTEXT(ctx);
1607
1608 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1609 level, layer);
1610 }
1611
1612
1613 void GLAPIENTRY
1614 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1615 GLenum renderbufferTarget,
1616 GLuint renderbuffer)
1617 {
1618 struct gl_renderbuffer_attachment *att;
1619 struct gl_framebuffer *fb;
1620 struct gl_renderbuffer *rb;
1621 GET_CURRENT_CONTEXT(ctx);
1622
1623 ASSERT_OUTSIDE_BEGIN_END(ctx);
1624
1625 switch (target) {
1626 #if FEATURE_EXT_framebuffer_blit
1627 case GL_DRAW_FRAMEBUFFER_EXT:
1628 if (!ctx->Extensions.EXT_framebuffer_blit) {
1629 _mesa_error(ctx, GL_INVALID_ENUM,
1630 "glFramebufferRenderbufferEXT(target)");
1631 return;
1632 }
1633 fb = ctx->DrawBuffer;
1634 break;
1635 case GL_READ_FRAMEBUFFER_EXT:
1636 if (!ctx->Extensions.EXT_framebuffer_blit) {
1637 _mesa_error(ctx, GL_INVALID_ENUM,
1638 "glFramebufferRenderbufferEXT(target)");
1639 return;
1640 }
1641 fb = ctx->ReadBuffer;
1642 break;
1643 #endif
1644 case GL_FRAMEBUFFER_EXT:
1645 fb = ctx->DrawBuffer;
1646 break;
1647 default:
1648 _mesa_error(ctx, GL_INVALID_ENUM,
1649 "glFramebufferRenderbufferEXT(target)");
1650 return;
1651 }
1652
1653 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1654 _mesa_error(ctx, GL_INVALID_ENUM,
1655 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1656 return;
1657 }
1658
1659 if (fb->Name == 0) {
1660 /* Can't attach new renderbuffers to a window system framebuffer */
1661 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1662 return;
1663 }
1664
1665 att = _mesa_get_attachment(ctx, fb, attachment);
1666 if (att == NULL) {
1667 _mesa_error(ctx, GL_INVALID_ENUM,
1668 "glFramebufferRenderbufferEXT(attachment)");
1669 return;
1670 }
1671
1672 if (renderbuffer) {
1673 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1674 if (!rb) {
1675 _mesa_error(ctx, GL_INVALID_OPERATION,
1676 "glFramebufferRenderbufferEXT(renderbuffer)");
1677 return;
1678 }
1679 }
1680 else {
1681 /* remove renderbuffer attachment */
1682 rb = NULL;
1683 }
1684
1685 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1686 /* make sure the renderbuffer is a depth/stencil format */
1687 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1688 _mesa_error(ctx, GL_INVALID_OPERATION,
1689 "glFramebufferRenderbufferEXT(renderbuffer"
1690 " is not DEPTH_STENCIL format)");
1691 return;
1692 }
1693 }
1694
1695
1696 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1697
1698 assert(ctx->Driver.FramebufferRenderbuffer);
1699 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1700
1701 /* Some subsequent GL commands may depend on the framebuffer's visual
1702 * after the binding is updated. Update visual info now.
1703 */
1704 _mesa_update_framebuffer_visual(fb);
1705 }
1706
1707
1708 void GLAPIENTRY
1709 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1710 GLenum pname, GLint *params)
1711 {
1712 const struct gl_renderbuffer_attachment *att;
1713 struct gl_framebuffer *buffer;
1714 GET_CURRENT_CONTEXT(ctx);
1715
1716 ASSERT_OUTSIDE_BEGIN_END(ctx);
1717
1718 switch (target) {
1719 #if FEATURE_EXT_framebuffer_blit
1720 case GL_DRAW_FRAMEBUFFER_EXT:
1721 if (!ctx->Extensions.EXT_framebuffer_blit) {
1722 _mesa_error(ctx, GL_INVALID_ENUM,
1723 "glGetFramebufferAttachmentParameterivEXT(target)");
1724 return;
1725 }
1726 buffer = ctx->DrawBuffer;
1727 break;
1728 case GL_READ_FRAMEBUFFER_EXT:
1729 if (!ctx->Extensions.EXT_framebuffer_blit) {
1730 _mesa_error(ctx, GL_INVALID_ENUM,
1731 "glGetFramebufferAttachmentParameterivEXT(target)");
1732 return;
1733 }
1734 buffer = ctx->ReadBuffer;
1735 break;
1736 #endif
1737 case GL_FRAMEBUFFER_EXT:
1738 buffer = ctx->DrawBuffer;
1739 break;
1740 default:
1741 _mesa_error(ctx, GL_INVALID_ENUM,
1742 "glGetFramebufferAttachmentParameterivEXT(target)");
1743 return;
1744 }
1745
1746 if (buffer->Name == 0) {
1747 _mesa_error(ctx, GL_INVALID_OPERATION,
1748 "glGetFramebufferAttachmentParameterivEXT");
1749 return;
1750 }
1751
1752 att = _mesa_get_attachment(ctx, buffer, attachment);
1753 if (att == NULL) {
1754 _mesa_error(ctx, GL_INVALID_ENUM,
1755 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1756 return;
1757 }
1758
1759 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1760 /* the depth and stencil attachments must point to the same buffer */
1761 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1762 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1763 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1764 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1765 _mesa_error(ctx, GL_INVALID_OPERATION,
1766 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1767 " attachments differ)");
1768 return;
1769 }
1770 }
1771
1772 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1773
1774 switch (pname) {
1775 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1776 *params = att->Type;
1777 return;
1778 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1779 if (att->Type == GL_RENDERBUFFER_EXT) {
1780 *params = att->Renderbuffer->Name;
1781 }
1782 else if (att->Type == GL_TEXTURE) {
1783 *params = att->Texture->Name;
1784 }
1785 else {
1786 _mesa_error(ctx, GL_INVALID_ENUM,
1787 "glGetFramebufferAttachmentParameterivEXT(pname)");
1788 }
1789 return;
1790 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1791 if (att->Type == GL_TEXTURE) {
1792 *params = att->TextureLevel;
1793 }
1794 else {
1795 _mesa_error(ctx, GL_INVALID_ENUM,
1796 "glGetFramebufferAttachmentParameterivEXT(pname)");
1797 }
1798 return;
1799 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1800 if (att->Type == GL_TEXTURE) {
1801 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1802 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1803 }
1804 else {
1805 *params = 0;
1806 }
1807 }
1808 else {
1809 _mesa_error(ctx, GL_INVALID_ENUM,
1810 "glGetFramebufferAttachmentParameterivEXT(pname)");
1811 }
1812 return;
1813 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1814 if (att->Type == GL_TEXTURE) {
1815 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1816 *params = att->Zoffset;
1817 }
1818 else {
1819 *params = 0;
1820 }
1821 }
1822 else {
1823 _mesa_error(ctx, GL_INVALID_ENUM,
1824 "glGetFramebufferAttachmentParameterivEXT(pname)");
1825 }
1826 return;
1827 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1828 if (!ctx->Extensions.ARB_framebuffer_object) {
1829 _mesa_error(ctx, GL_INVALID_ENUM,
1830 "glGetFramebufferAttachmentParameterivEXT(pname)");
1831 }
1832 else {
1833 *params = att->Renderbuffer->ColorEncoding;
1834 }
1835 return;
1836 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1837 if (!ctx->Extensions.ARB_framebuffer_object) {
1838 _mesa_error(ctx, GL_INVALID_ENUM,
1839 "glGetFramebufferAttachmentParameterivEXT(pname)");
1840 return;
1841 }
1842 else {
1843 *params = att->Renderbuffer->ComponentType;
1844 }
1845 return;
1846 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1847 if (!ctx->Extensions.ARB_framebuffer_object) {
1848 _mesa_error(ctx, GL_INVALID_ENUM,
1849 "glGetFramebufferAttachmentParameterivEXT(pname)");
1850 }
1851 else {
1852 *params = att->Renderbuffer->RedBits;
1853 }
1854 return;
1855 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1856 if (!ctx->Extensions.ARB_framebuffer_object) {
1857 _mesa_error(ctx, GL_INVALID_ENUM,
1858 "glGetFramebufferAttachmentParameterivEXT(pname)");
1859 }
1860 else {
1861 *params = att->Renderbuffer->GreenBits;
1862 }
1863 return;
1864 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1865 if (!ctx->Extensions.ARB_framebuffer_object) {
1866 _mesa_error(ctx, GL_INVALID_ENUM,
1867 "glGetFramebufferAttachmentParameterivEXT(pname)");
1868 }
1869 else {
1870 *params = att->Renderbuffer->BlueBits;
1871 }
1872 return;
1873 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1874 if (!ctx->Extensions.ARB_framebuffer_object) {
1875 _mesa_error(ctx, GL_INVALID_ENUM,
1876 "glGetFramebufferAttachmentParameterivEXT(pname)");
1877 }
1878 else {
1879 *params = att->Renderbuffer->AlphaBits;
1880 }
1881 return;
1882 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1883 if (!ctx->Extensions.ARB_framebuffer_object) {
1884 _mesa_error(ctx, GL_INVALID_ENUM,
1885 "glGetFramebufferAttachmentParameterivEXT(pname)");
1886 }
1887 else {
1888 *params = att->Renderbuffer->DepthBits;
1889 }
1890 return;
1891 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1892 if (!ctx->Extensions.ARB_framebuffer_object) {
1893 _mesa_error(ctx, GL_INVALID_ENUM,
1894 "glGetFramebufferAttachmentParameterivEXT(pname)");
1895 }
1896 else {
1897 *params = att->Renderbuffer->StencilBits;
1898 }
1899 return;
1900 default:
1901 _mesa_error(ctx, GL_INVALID_ENUM,
1902 "glGetFramebufferAttachmentParameterivEXT(pname)");
1903 return;
1904 }
1905 }
1906
1907
1908 void GLAPIENTRY
1909 _mesa_GenerateMipmapEXT(GLenum target)
1910 {
1911 struct gl_texture_unit *texUnit;
1912 struct gl_texture_object *texObj;
1913 GET_CURRENT_CONTEXT(ctx);
1914
1915 ASSERT_OUTSIDE_BEGIN_END(ctx);
1916 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1917
1918 switch (target) {
1919 case GL_TEXTURE_1D:
1920 case GL_TEXTURE_2D:
1921 case GL_TEXTURE_3D:
1922 case GL_TEXTURE_CUBE_MAP:
1923 /* OK, legal value */
1924 break;
1925 default:
1926 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1927 return;
1928 }
1929
1930 texUnit = _mesa_get_current_tex_unit(ctx);
1931 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1932
1933 _mesa_lock_texture(ctx, texObj);
1934 if (target == GL_TEXTURE_CUBE_MAP) {
1935 GLuint face;
1936 for (face = 0; face < 6; face++)
1937 ctx->Driver.GenerateMipmap(ctx,
1938 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1939 texObj);
1940 }
1941 else {
1942 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1943 }
1944 _mesa_unlock_texture(ctx, texObj);
1945 }
1946
1947
1948 #if FEATURE_EXT_framebuffer_blit
1949 /**
1950 * Blit rectangular region, optionally from one framebuffer to another.
1951 *
1952 * Note, if the src buffer is multisampled and the dest is not, this is
1953 * when the samples must be resolved to a single color.
1954 */
1955 void GLAPIENTRY
1956 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1957 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1958 GLbitfield mask, GLenum filter)
1959 {
1960 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1961 GL_DEPTH_BUFFER_BIT |
1962 GL_STENCIL_BUFFER_BIT);
1963 const struct gl_framebuffer *readFb, *drawFb;
1964 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1965 GET_CURRENT_CONTEXT(ctx);
1966
1967 ASSERT_OUTSIDE_BEGIN_END(ctx);
1968 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1969
1970 if (ctx->NewState) {
1971 _mesa_update_state(ctx);
1972 }
1973
1974 readFb = ctx->ReadBuffer;
1975 drawFb = ctx->DrawBuffer;
1976
1977 if (!readFb || !drawFb) {
1978 /* This will normally never happen but someday we may want to
1979 * support MakeCurrent() with no drawables.
1980 */
1981 return;
1982 }
1983
1984 /* check for complete framebuffers */
1985 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
1986 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1987 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1988 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1989 return;
1990 }
1991
1992 if (filter != GL_NEAREST && filter != GL_LINEAR) {
1993 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
1994 return;
1995 }
1996
1997 if (mask & ~legalMaskBits) {
1998 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
1999 return;
2000 }
2001
2002 /* depth/stencil must be blitted with nearest filtering */
2003 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2004 && filter != GL_NEAREST) {
2005 _mesa_error(ctx, GL_INVALID_OPERATION,
2006 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2007 return;
2008 }
2009
2010 /* get color read/draw renderbuffers */
2011 if (mask & GL_COLOR_BUFFER_BIT) {
2012 colorReadRb = readFb->_ColorReadBuffer;
2013 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2014 }
2015 else {
2016 colorReadRb = colorDrawRb = NULL;
2017 }
2018
2019 if (mask & GL_STENCIL_BUFFER_BIT) {
2020 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2021 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2022 if (!readRb ||
2023 !drawRb ||
2024 readRb->StencilBits != drawRb->StencilBits) {
2025 _mesa_error(ctx, GL_INVALID_OPERATION,
2026 "glBlitFramebufferEXT(stencil buffer size mismatch");
2027 return;
2028 }
2029 }
2030
2031 if (mask & GL_DEPTH_BUFFER_BIT) {
2032 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2033 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2034 if (!readRb ||
2035 !drawRb ||
2036 readRb->DepthBits != drawRb->DepthBits) {
2037 _mesa_error(ctx, GL_INVALID_OPERATION,
2038 "glBlitFramebufferEXT(depth buffer size mismatch");
2039 return;
2040 }
2041 }
2042
2043 if (readFb->Visual.samples > 0 &&
2044 drawFb->Visual.samples > 0 &&
2045 readFb->Visual.samples != drawFb->Visual.samples) {
2046 _mesa_error(ctx, GL_INVALID_OPERATION,
2047 "glBlitFramebufferEXT(mismatched samples");
2048 return;
2049 }
2050
2051 /* extra checks for multisample copies... */
2052 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2053 /* src and dest region sizes must be the same */
2054 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2055 srcY1 - srcY0 != dstY1 - dstY0) {
2056 _mesa_error(ctx, GL_INVALID_OPERATION,
2057 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2058 return;
2059 }
2060
2061 /* color formats must match */
2062 if (colorReadRb &&
2063 colorDrawRb &&
2064 colorReadRb->_ActualFormat != colorDrawRb->_ActualFormat) {
2065 _mesa_error(ctx, GL_INVALID_OPERATION,
2066 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2067 return;
2068 }
2069 }
2070
2071 if (!ctx->Extensions.EXT_framebuffer_blit) {
2072 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2073 return;
2074 }
2075
2076 ASSERT(ctx->Driver.BlitFramebuffer);
2077 ctx->Driver.BlitFramebuffer(ctx,
2078 srcX0, srcY0, srcX1, srcY1,
2079 dstX0, dstY0, dstX1, dstY1,
2080 mask, filter);
2081 }
2082 #endif /* FEATURE_EXT_framebuffer_blit */