680fd22ba8f14deca7a24f3368b69fe6d5a81cdd
[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 /* The above doesn't fully flush the drivers in the way that a
722 * glFlush does, but that is required here:
723 */
724 if (ctx->Driver.Flush)
725 ctx->Driver.Flush(ctx);
726
727
728 if (renderbuffer) {
729 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
730 if (newRb == &DummyRenderbuffer) {
731 /* ID was reserved, but no real renderbuffer object made yet */
732 newRb = NULL;
733 }
734 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
735 /* All RB IDs must be Gen'd */
736 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
737 return;
738 }
739
740 if (!newRb) {
741 /* create new renderbuffer object */
742 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
743 if (!newRb) {
744 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
745 return;
746 }
747 ASSERT(newRb->AllocStorage);
748 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
749 newRb->RefCount = 1; /* referenced by hash table */
750 }
751 }
752 else {
753 newRb = NULL;
754 }
755
756 ASSERT(newRb != &DummyRenderbuffer);
757
758 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
759 }
760
761
762 /**
763 * If the given renderbuffer is anywhere attached to the framebuffer, detach
764 * the renderbuffer.
765 * This is used when a renderbuffer object is deleted.
766 * The spec calls for unbinding.
767 */
768 static void
769 detach_renderbuffer(GLcontext *ctx,
770 struct gl_framebuffer *fb,
771 struct gl_renderbuffer *rb)
772 {
773 GLuint i;
774 for (i = 0; i < BUFFER_COUNT; i++) {
775 if (fb->Attachment[i].Renderbuffer == rb) {
776 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
777 }
778 }
779 invalidate_framebuffer(fb);
780 }
781
782
783 void GLAPIENTRY
784 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
785 {
786 GLint i;
787 GET_CURRENT_CONTEXT(ctx);
788
789 ASSERT_OUTSIDE_BEGIN_END(ctx);
790 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
791
792 for (i = 0; i < n; i++) {
793 if (renderbuffers[i] > 0) {
794 struct gl_renderbuffer *rb;
795 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
796 if (rb) {
797 /* check if deleting currently bound renderbuffer object */
798 if (rb == ctx->CurrentRenderbuffer) {
799 /* bind default */
800 ASSERT(rb->RefCount >= 2);
801 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
802 }
803
804 if (ctx->DrawBuffer->Name) {
805 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
806 }
807 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
808 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
809 }
810
811 /* Remove from hash table immediately, to free the ID.
812 * But the object will not be freed until it's no longer
813 * referenced anywhere else.
814 */
815 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
816
817 if (rb != &DummyRenderbuffer) {
818 /* no longer referenced by hash table */
819 _mesa_reference_renderbuffer(&rb, NULL);
820 }
821 }
822 }
823 }
824 }
825
826
827 void GLAPIENTRY
828 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
829 {
830 GET_CURRENT_CONTEXT(ctx);
831 GLuint first;
832 GLint i;
833
834 ASSERT_OUTSIDE_BEGIN_END(ctx);
835
836 if (n < 0) {
837 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
838 return;
839 }
840
841 if (!renderbuffers)
842 return;
843
844 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
845
846 for (i = 0; i < n; i++) {
847 GLuint name = first + i;
848 renderbuffers[i] = name;
849 /* insert dummy placeholder into hash table */
850 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
851 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
852 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
853 }
854 }
855
856
857 /**
858 * Given an internal format token for a render buffer, return the
859 * corresponding base format.
860 * This is very similar to _mesa_base_tex_format() but the set of valid
861 * internal formats is somewhat different.
862 *
863 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
864 * GL_DEPTH_STENCIL_EXT or zero if error.
865 */
866 GLenum
867 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
868 {
869 switch (internalFormat) {
870 case GL_RGB:
871 case GL_R3_G3_B2:
872 case GL_RGB4:
873 case GL_RGB5:
874 case GL_RGB8:
875 case GL_RGB10:
876 case GL_RGB12:
877 case GL_RGB16:
878 return GL_RGB;
879 case GL_RGBA:
880 case GL_RGBA2:
881 case GL_RGBA4:
882 case GL_RGB5_A1:
883 case GL_RGBA8:
884 case GL_RGB10_A2:
885 case GL_RGBA12:
886 case GL_RGBA16:
887 return GL_RGBA;
888 case GL_STENCIL_INDEX:
889 case GL_STENCIL_INDEX1_EXT:
890 case GL_STENCIL_INDEX4_EXT:
891 case GL_STENCIL_INDEX8_EXT:
892 case GL_STENCIL_INDEX16_EXT:
893 return GL_STENCIL_INDEX;
894 case GL_DEPTH_COMPONENT:
895 case GL_DEPTH_COMPONENT16:
896 case GL_DEPTH_COMPONENT24:
897 case GL_DEPTH_COMPONENT32:
898 return GL_DEPTH_COMPONENT;
899 case GL_DEPTH_STENCIL_EXT:
900 case GL_DEPTH24_STENCIL8_EXT:
901 if (ctx->Extensions.EXT_packed_depth_stencil)
902 return GL_DEPTH_STENCIL_EXT;
903 else
904 return 0;
905 /* XXX add floating point formats eventually */
906 default:
907 return 0;
908 }
909 }
910
911
912 /** sentinal value, see below */
913 #define NO_SAMPLES 1000
914
915
916 /**
917 * Helper function used by _mesa_RenderbufferStorageEXT() and
918 * _mesa_RenderbufferStorageMultisample().
919 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
920 */
921 static void
922 renderbuffer_storage(GLenum target, GLenum internalFormat,
923 GLsizei width, GLsizei height, GLsizei samples)
924 {
925 const char *func = samples == NO_SAMPLES ?
926 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
927 struct gl_renderbuffer *rb;
928 GLenum baseFormat;
929 GET_CURRENT_CONTEXT(ctx);
930
931 ASSERT_OUTSIDE_BEGIN_END(ctx);
932
933 if (target != GL_RENDERBUFFER_EXT) {
934 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
935 return;
936 }
937
938 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
939 if (baseFormat == 0) {
940 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
941 return;
942 }
943
944 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
945 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
946 return;
947 }
948
949 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
950 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
951 return;
952 }
953
954 if (samples == NO_SAMPLES) {
955 /* NumSamples == 0 indicates non-multisampling */
956 samples = 0;
957 }
958 else if (samples > ctx->Const.MaxSamples) {
959 /* note: driver may choose to use more samples than what's requested */
960 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
961 return;
962 }
963
964 rb = ctx->CurrentRenderbuffer;
965 if (!rb) {
966 _mesa_error(ctx, GL_INVALID_OPERATION, func);
967 return;
968 }
969
970 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
971
972 if (rb->InternalFormat == internalFormat &&
973 rb->Width == (GLuint) width &&
974 rb->Height == (GLuint) height) {
975 /* no change in allocation needed */
976 return;
977 }
978
979 /* These MUST get set by the AllocStorage func */
980 rb->_ActualFormat = 0;
981 rb->RedBits =
982 rb->GreenBits =
983 rb->BlueBits =
984 rb->AlphaBits =
985 rb->IndexBits =
986 rb->DepthBits =
987 rb->StencilBits = 0;
988 rb->NumSamples = samples;
989
990 /* Now allocate the storage */
991 ASSERT(rb->AllocStorage);
992 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
993 /* No error - check/set fields now */
994 assert(rb->_ActualFormat);
995 assert(rb->Width == (GLuint) width);
996 assert(rb->Height == (GLuint) height);
997 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
998 rb->DepthBits || rb->StencilBits || rb->IndexBits);
999 rb->InternalFormat = internalFormat;
1000 rb->_BaseFormat = baseFormat;
1001 }
1002 else {
1003 /* Probably ran out of memory - clear the fields */
1004 rb->Width = 0;
1005 rb->Height = 0;
1006 rb->InternalFormat = GL_NONE;
1007 rb->_ActualFormat = GL_NONE;
1008 rb->_BaseFormat = GL_NONE;
1009 rb->RedBits =
1010 rb->GreenBits =
1011 rb->BlueBits =
1012 rb->AlphaBits =
1013 rb->IndexBits =
1014 rb->DepthBits =
1015 rb->StencilBits =
1016 rb->NumSamples = 0;
1017 }
1018
1019 /*
1020 test_framebuffer_completeness(ctx, fb);
1021 */
1022 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1023 * points???
1024 */
1025 }
1026
1027
1028 void GLAPIENTRY
1029 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1030 GLsizei width, GLsizei height)
1031 {
1032 /* GL_ARB_fbo says calling this function is equivalent to calling
1033 * glRenderbufferStorageMultisample() with samples=0. We pass in
1034 * a token value here just for error reporting purposes.
1035 */
1036 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1037 }
1038
1039
1040 void GLAPIENTRY
1041 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1042 GLenum internalFormat,
1043 GLsizei width, GLsizei height)
1044 {
1045 renderbuffer_storage(target, internalFormat, width, height, samples);
1046 }
1047
1048
1049
1050 void GLAPIENTRY
1051 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1052 {
1053 struct gl_renderbuffer *rb;
1054 GET_CURRENT_CONTEXT(ctx);
1055
1056 ASSERT_OUTSIDE_BEGIN_END(ctx);
1057
1058 if (target != GL_RENDERBUFFER_EXT) {
1059 _mesa_error(ctx, GL_INVALID_ENUM,
1060 "glGetRenderbufferParameterivEXT(target)");
1061 return;
1062 }
1063
1064 rb = ctx->CurrentRenderbuffer;
1065 if (!rb) {
1066 _mesa_error(ctx, GL_INVALID_OPERATION,
1067 "glGetRenderbufferParameterivEXT");
1068 return;
1069 }
1070
1071 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1072
1073 switch (pname) {
1074 case GL_RENDERBUFFER_WIDTH_EXT:
1075 *params = rb->Width;
1076 return;
1077 case GL_RENDERBUFFER_HEIGHT_EXT:
1078 *params = rb->Height;
1079 return;
1080 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1081 *params = rb->InternalFormat;
1082 return;
1083 case GL_RENDERBUFFER_RED_SIZE_EXT:
1084 *params = rb->RedBits;
1085 break;
1086 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1087 *params = rb->GreenBits;
1088 break;
1089 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1090 *params = rb->BlueBits;
1091 break;
1092 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1093 *params = rb->AlphaBits;
1094 break;
1095 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1096 *params = rb->DepthBits;
1097 break;
1098 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1099 *params = rb->StencilBits;
1100 break;
1101 case GL_RENDERBUFFER_SAMPLES:
1102 if (ctx->Extensions.ARB_framebuffer_object) {
1103 *params = rb->NumSamples;
1104 break;
1105 }
1106 /* fallthrough */
1107 default:
1108 _mesa_error(ctx, GL_INVALID_ENUM,
1109 "glGetRenderbufferParameterivEXT(target)");
1110 return;
1111 }
1112 }
1113
1114
1115 GLboolean GLAPIENTRY
1116 _mesa_IsFramebufferEXT(GLuint framebuffer)
1117 {
1118 GET_CURRENT_CONTEXT(ctx);
1119 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1120 if (framebuffer) {
1121 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1122 if (rb != NULL && rb != &DummyFramebuffer)
1123 return GL_TRUE;
1124 }
1125 return GL_FALSE;
1126 }
1127
1128
1129 static void
1130 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1131 {
1132 GLuint i;
1133 ASSERT(ctx->Driver.RenderTexture);
1134 for (i = 0; i < BUFFER_COUNT; i++) {
1135 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1136 struct gl_texture_object *texObj = att->Texture;
1137 if (texObj
1138 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
1139 ctx->Driver.RenderTexture(ctx, fb, att);
1140 }
1141 }
1142 }
1143
1144
1145 /**
1146 * Examine all the framebuffer's attachments to see if any are textures.
1147 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1148 * notify the device driver that the texture image may have changed.
1149 */
1150 static void
1151 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1152 {
1153 if (ctx->Driver.FinishRenderTexture) {
1154 GLuint i;
1155 for (i = 0; i < BUFFER_COUNT; i++) {
1156 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1157 if (att->Texture && att->Renderbuffer) {
1158 ctx->Driver.FinishRenderTexture(ctx, att);
1159 }
1160 }
1161 }
1162 }
1163
1164
1165 void GLAPIENTRY
1166 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1167 {
1168 struct gl_framebuffer *newFb, *newFbread;
1169 GLboolean bindReadBuf, bindDrawBuf;
1170 GET_CURRENT_CONTEXT(ctx);
1171
1172 #ifdef DEBUG
1173 if (ctx->Extensions.ARB_framebuffer_object) {
1174 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1175 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1176 }
1177 #endif
1178
1179 ASSERT_OUTSIDE_BEGIN_END(ctx);
1180
1181 if (!ctx->Extensions.EXT_framebuffer_object) {
1182 _mesa_error(ctx, GL_INVALID_OPERATION,
1183 "glBindFramebufferEXT(unsupported)");
1184 return;
1185 }
1186
1187 switch (target) {
1188 #if FEATURE_EXT_framebuffer_blit
1189 case GL_DRAW_FRAMEBUFFER_EXT:
1190 if (!ctx->Extensions.EXT_framebuffer_blit) {
1191 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1192 return;
1193 }
1194 bindDrawBuf = GL_TRUE;
1195 bindReadBuf = GL_FALSE;
1196 break;
1197 case GL_READ_FRAMEBUFFER_EXT:
1198 if (!ctx->Extensions.EXT_framebuffer_blit) {
1199 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1200 return;
1201 }
1202 bindDrawBuf = GL_FALSE;
1203 bindReadBuf = GL_TRUE;
1204 break;
1205 #endif
1206 case GL_FRAMEBUFFER_EXT:
1207 bindDrawBuf = GL_TRUE;
1208 bindReadBuf = GL_TRUE;
1209 break;
1210 default:
1211 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1212 return;
1213 }
1214
1215 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1216 if (ctx->Driver.Flush) {
1217 ctx->Driver.Flush(ctx);
1218 }
1219
1220 if (framebuffer) {
1221 /* Binding a user-created framebuffer object */
1222 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1223 if (newFb == &DummyFramebuffer) {
1224 /* ID was reserved, but no real framebuffer object made yet */
1225 newFb = NULL;
1226 }
1227 else if (!newFb && ctx->Extensions.ARB_framebuffer_object) {
1228 /* All FBO IDs must be Gen'd */
1229 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1230 return;
1231 }
1232
1233 if (!newFb) {
1234 /* create new framebuffer object */
1235 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1236 if (!newFb) {
1237 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1238 return;
1239 }
1240 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1241 }
1242 newFbread = newFb;
1243 }
1244 else {
1245 /* Binding the window system framebuffer (which was originally set
1246 * with MakeCurrent).
1247 */
1248 newFb = ctx->WinSysDrawBuffer;
1249 newFbread = ctx->WinSysReadBuffer;
1250 }
1251
1252 ASSERT(newFb);
1253 ASSERT(newFb != &DummyFramebuffer);
1254
1255 /*
1256 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1257 */
1258
1259 if (bindReadBuf) {
1260 if (ctx->ReadBuffer == newFbread)
1261 bindReadBuf = GL_FALSE; /* no change */
1262 else
1263 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1264 }
1265
1266 if (bindDrawBuf) {
1267 /* check if old FB had any texture attachments */
1268 if (ctx->DrawBuffer->Name != 0) {
1269 check_end_texture_render(ctx, ctx->DrawBuffer);
1270 }
1271
1272 if (ctx->DrawBuffer == newFb)
1273 bindDrawBuf = GL_FALSE; /* no change */
1274 else
1275 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1276
1277 if (newFb->Name != 0) {
1278 /* check if newly bound framebuffer has any texture attachments */
1279 check_begin_texture_render(ctx, newFb);
1280 }
1281 }
1282
1283 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1284 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1285 }
1286 }
1287
1288
1289 void GLAPIENTRY
1290 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1291 {
1292 GLint i;
1293 GET_CURRENT_CONTEXT(ctx);
1294
1295 ASSERT_OUTSIDE_BEGIN_END(ctx);
1296 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1297 /* The above doesn't fully flush the drivers in the way that a
1298 * glFlush does, but that is required here:
1299 */
1300 if (ctx->Driver.Flush)
1301 ctx->Driver.Flush(ctx);
1302
1303 for (i = 0; i < n; i++) {
1304 if (framebuffers[i] > 0) {
1305 struct gl_framebuffer *fb;
1306 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1307 if (fb) {
1308 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1309
1310 /* check if deleting currently bound framebuffer object */
1311 if (fb == ctx->DrawBuffer) {
1312 /* bind default */
1313 ASSERT(fb->RefCount >= 2);
1314 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1315 }
1316 if (fb == ctx->ReadBuffer) {
1317 /* bind default */
1318 ASSERT(fb->RefCount >= 2);
1319 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1320 }
1321
1322 /* remove from hash table immediately, to free the ID */
1323 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1324
1325 if (fb != &DummyFramebuffer) {
1326 /* But the object will not be freed until it's no longer
1327 * bound in any context.
1328 */
1329 _mesa_reference_framebuffer(&fb, NULL);
1330 }
1331 }
1332 }
1333 }
1334 }
1335
1336
1337 void GLAPIENTRY
1338 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1339 {
1340 GET_CURRENT_CONTEXT(ctx);
1341 GLuint first;
1342 GLint i;
1343
1344 ASSERT_OUTSIDE_BEGIN_END(ctx);
1345
1346 if (n < 0) {
1347 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1348 return;
1349 }
1350
1351 if (!framebuffers)
1352 return;
1353
1354 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1355
1356 for (i = 0; i < n; i++) {
1357 GLuint name = first + i;
1358 framebuffers[i] = name;
1359 /* insert dummy placeholder into hash table */
1360 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1361 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1362 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1363 }
1364 }
1365
1366
1367
1368 GLenum GLAPIENTRY
1369 _mesa_CheckFramebufferStatusEXT(GLenum target)
1370 {
1371 struct gl_framebuffer *buffer;
1372 GET_CURRENT_CONTEXT(ctx);
1373
1374 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1375
1376 switch (target) {
1377 #if FEATURE_EXT_framebuffer_blit
1378 case GL_DRAW_FRAMEBUFFER_EXT:
1379 if (!ctx->Extensions.EXT_framebuffer_blit) {
1380 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1381 return 0;
1382 }
1383 buffer = ctx->DrawBuffer;
1384 break;
1385 case GL_READ_FRAMEBUFFER_EXT:
1386 if (!ctx->Extensions.EXT_framebuffer_blit) {
1387 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1388 return 0;
1389 }
1390 buffer = ctx->ReadBuffer;
1391 break;
1392 #endif
1393 case GL_FRAMEBUFFER_EXT:
1394 buffer = ctx->DrawBuffer;
1395 break;
1396 default:
1397 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1398 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1399 }
1400
1401 if (buffer->Name == 0) {
1402 /* The window system / default framebuffer is always complete */
1403 return GL_FRAMEBUFFER_COMPLETE_EXT;
1404 }
1405
1406 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1407
1408 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1409 _mesa_test_framebuffer_completeness(ctx, buffer);
1410 }
1411
1412 return buffer->_Status;
1413 }
1414
1415
1416
1417 /**
1418 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1419 */
1420 static void
1421 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1422 GLenum attachment, GLenum textarget, GLuint texture,
1423 GLint level, GLint zoffset)
1424 {
1425 struct gl_renderbuffer_attachment *att;
1426 struct gl_texture_object *texObj = NULL;
1427 struct gl_framebuffer *fb;
1428 GLboolean error = GL_FALSE;
1429
1430 ASSERT_OUTSIDE_BEGIN_END(ctx);
1431
1432 switch (target) {
1433 case GL_READ_FRAMEBUFFER_EXT:
1434 error = !ctx->Extensions.EXT_framebuffer_blit;
1435 fb = ctx->ReadBuffer;
1436 break;
1437 case GL_DRAW_FRAMEBUFFER_EXT:
1438 error = !ctx->Extensions.EXT_framebuffer_blit;
1439 /* fall-through */
1440 case GL_FRAMEBUFFER_EXT:
1441 fb = ctx->DrawBuffer;
1442 break;
1443 default:
1444 error = GL_TRUE;
1445 }
1446
1447 if (error) {
1448 _mesa_error(ctx, GL_INVALID_ENUM,
1449 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1450 return;
1451 }
1452
1453 ASSERT(fb);
1454
1455 /* check framebuffer binding */
1456 if (fb->Name == 0) {
1457 _mesa_error(ctx, GL_INVALID_OPERATION,
1458 "glFramebufferTexture%sEXT", caller);
1459 return;
1460 }
1461
1462
1463 /* The textarget, level, and zoffset parameters are only validated if
1464 * texture is non-zero.
1465 */
1466 if (texture) {
1467 GLboolean err = GL_TRUE;
1468
1469 texObj = _mesa_lookup_texture(ctx, texture);
1470 if (texObj != NULL) {
1471 if (textarget == 0) {
1472 err = (texObj->Target != GL_TEXTURE_3D) &&
1473 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1474 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1475 }
1476 else {
1477 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1478 ? !IS_CUBE_FACE(textarget)
1479 : (texObj->Target != textarget);
1480 }
1481 }
1482
1483 if (err) {
1484 _mesa_error(ctx, GL_INVALID_OPERATION,
1485 "glFramebufferTexture%sEXT(texture target mismatch)",
1486 caller);
1487 return;
1488 }
1489
1490 if (texObj->Target == GL_TEXTURE_3D) {
1491 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1492 if (zoffset < 0 || zoffset >= maxSize) {
1493 _mesa_error(ctx, GL_INVALID_VALUE,
1494 "glFramebufferTexture%sEXT(zoffset)", caller);
1495 return;
1496 }
1497 }
1498 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1499 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1500 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1501 _mesa_error(ctx, GL_INVALID_VALUE,
1502 "glFramebufferTexture%sEXT(layer)", caller);
1503 return;
1504 }
1505 }
1506
1507 if ((level < 0) ||
1508 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1509 _mesa_error(ctx, GL_INVALID_VALUE,
1510 "glFramebufferTexture%sEXT(level)", caller);
1511 return;
1512 }
1513 }
1514
1515 att = _mesa_get_attachment(ctx, fb, attachment);
1516 if (att == NULL) {
1517 _mesa_error(ctx, GL_INVALID_ENUM,
1518 "glFramebufferTexture%sEXT(attachment)", caller);
1519 return;
1520 }
1521
1522 if (texObj && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1523 /* the texture format must be depth+stencil */
1524 const struct gl_texture_image *texImg;
1525 texImg = texObj->Image[0][texObj->BaseLevel];
1526 if (!texImg || texImg->_BaseFormat != GL_DEPTH_STENCIL) {
1527 _mesa_error(ctx, GL_INVALID_OPERATION,
1528 "glFramebufferTexture%sEXT(texture is not"
1529 " DEPTH_STENCIL format)", caller);
1530 return;
1531 }
1532 }
1533
1534 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1535 /* The above doesn't fully flush the drivers in the way that a
1536 * glFlush does, but that is required here:
1537 */
1538 if (ctx->Driver.Flush)
1539 ctx->Driver.Flush(ctx);
1540
1541 _glthread_LOCK_MUTEX(fb->Mutex);
1542 if (texObj) {
1543 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1544 level, zoffset);
1545 /* Set the render-to-texture flag. We'll check this flag in
1546 * glTexImage() and friends to determine if we need to revalidate
1547 * any FBOs that might be rendering into this texture.
1548 * This flag never gets cleared since it's non-trivial to determine
1549 * when all FBOs might be done rendering to this texture. That's OK
1550 * though since it's uncommon to render to a texture then repeatedly
1551 * call glTexImage() to change images in the texture.
1552 */
1553 texObj->_RenderToTexture = GL_TRUE;
1554 }
1555 else {
1556 _mesa_remove_attachment(ctx, att);
1557 }
1558
1559 invalidate_framebuffer(fb);
1560
1561 _glthread_UNLOCK_MUTEX(fb->Mutex);
1562 }
1563
1564
1565
1566 void GLAPIENTRY
1567 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1568 GLenum textarget, GLuint texture, GLint level)
1569 {
1570 GET_CURRENT_CONTEXT(ctx);
1571
1572 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1573 _mesa_error(ctx, GL_INVALID_ENUM,
1574 "glFramebufferTexture1DEXT(textarget)");
1575 return;
1576 }
1577
1578 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1579 level, 0);
1580 }
1581
1582
1583 void GLAPIENTRY
1584 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1585 GLenum textarget, GLuint texture, GLint level)
1586 {
1587 GET_CURRENT_CONTEXT(ctx);
1588
1589 if ((texture != 0) &&
1590 (textarget != GL_TEXTURE_2D) &&
1591 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1592 (!IS_CUBE_FACE(textarget))) {
1593 _mesa_error(ctx, GL_INVALID_OPERATION,
1594 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1595 return;
1596 }
1597
1598 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1599 level, 0);
1600 }
1601
1602
1603 void GLAPIENTRY
1604 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1605 GLenum textarget, GLuint texture,
1606 GLint level, GLint zoffset)
1607 {
1608 GET_CURRENT_CONTEXT(ctx);
1609
1610 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1611 _mesa_error(ctx, GL_INVALID_ENUM,
1612 "glFramebufferTexture3DEXT(textarget)");
1613 return;
1614 }
1615
1616 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1617 level, zoffset);
1618 }
1619
1620
1621 void GLAPIENTRY
1622 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1623 GLuint texture, GLint level, GLint layer)
1624 {
1625 GET_CURRENT_CONTEXT(ctx);
1626
1627 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1628 level, layer);
1629 }
1630
1631
1632 void GLAPIENTRY
1633 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1634 GLenum renderbufferTarget,
1635 GLuint renderbuffer)
1636 {
1637 struct gl_renderbuffer_attachment *att;
1638 struct gl_framebuffer *fb;
1639 struct gl_renderbuffer *rb;
1640 GET_CURRENT_CONTEXT(ctx);
1641
1642 ASSERT_OUTSIDE_BEGIN_END(ctx);
1643
1644 switch (target) {
1645 #if FEATURE_EXT_framebuffer_blit
1646 case GL_DRAW_FRAMEBUFFER_EXT:
1647 if (!ctx->Extensions.EXT_framebuffer_blit) {
1648 _mesa_error(ctx, GL_INVALID_ENUM,
1649 "glFramebufferRenderbufferEXT(target)");
1650 return;
1651 }
1652 fb = ctx->DrawBuffer;
1653 break;
1654 case GL_READ_FRAMEBUFFER_EXT:
1655 if (!ctx->Extensions.EXT_framebuffer_blit) {
1656 _mesa_error(ctx, GL_INVALID_ENUM,
1657 "glFramebufferRenderbufferEXT(target)");
1658 return;
1659 }
1660 fb = ctx->ReadBuffer;
1661 break;
1662 #endif
1663 case GL_FRAMEBUFFER_EXT:
1664 fb = ctx->DrawBuffer;
1665 break;
1666 default:
1667 _mesa_error(ctx, GL_INVALID_ENUM,
1668 "glFramebufferRenderbufferEXT(target)");
1669 return;
1670 }
1671
1672 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1673 _mesa_error(ctx, GL_INVALID_ENUM,
1674 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1675 return;
1676 }
1677
1678 if (fb->Name == 0) {
1679 /* Can't attach new renderbuffers to a window system framebuffer */
1680 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1681 return;
1682 }
1683
1684 att = _mesa_get_attachment(ctx, fb, attachment);
1685 if (att == NULL) {
1686 _mesa_error(ctx, GL_INVALID_ENUM,
1687 "glFramebufferRenderbufferEXT(attachment)");
1688 return;
1689 }
1690
1691 if (renderbuffer) {
1692 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1693 if (!rb) {
1694 _mesa_error(ctx, GL_INVALID_OPERATION,
1695 "glFramebufferRenderbufferEXT(renderbuffer)");
1696 return;
1697 }
1698 }
1699 else {
1700 /* remove renderbuffer attachment */
1701 rb = NULL;
1702 }
1703
1704 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1705 /* make sure the renderbuffer is a depth/stencil format */
1706 if (rb->_BaseFormat != GL_DEPTH_STENCIL) {
1707 _mesa_error(ctx, GL_INVALID_OPERATION,
1708 "glFramebufferRenderbufferEXT(renderbuffer"
1709 " is not DEPTH_STENCIL format)");
1710 return;
1711 }
1712 }
1713
1714
1715 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1716 /* The above doesn't fully flush the drivers in the way that a
1717 * glFlush does, but that is required here:
1718 */
1719 if (ctx->Driver.Flush)
1720 ctx->Driver.Flush(ctx);
1721
1722 assert(ctx->Driver.FramebufferRenderbuffer);
1723 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1724
1725 /* Some subsequent GL commands may depend on the framebuffer's visual
1726 * after the binding is updated. Update visual info now.
1727 */
1728 _mesa_update_framebuffer_visual(fb);
1729 }
1730
1731
1732 void GLAPIENTRY
1733 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1734 GLenum pname, GLint *params)
1735 {
1736 const struct gl_renderbuffer_attachment *att;
1737 struct gl_framebuffer *buffer;
1738 GET_CURRENT_CONTEXT(ctx);
1739
1740 ASSERT_OUTSIDE_BEGIN_END(ctx);
1741
1742 switch (target) {
1743 #if FEATURE_EXT_framebuffer_blit
1744 case GL_DRAW_FRAMEBUFFER_EXT:
1745 if (!ctx->Extensions.EXT_framebuffer_blit) {
1746 _mesa_error(ctx, GL_INVALID_ENUM,
1747 "glGetFramebufferAttachmentParameterivEXT(target)");
1748 return;
1749 }
1750 buffer = ctx->DrawBuffer;
1751 break;
1752 case GL_READ_FRAMEBUFFER_EXT:
1753 if (!ctx->Extensions.EXT_framebuffer_blit) {
1754 _mesa_error(ctx, GL_INVALID_ENUM,
1755 "glGetFramebufferAttachmentParameterivEXT(target)");
1756 return;
1757 }
1758 buffer = ctx->ReadBuffer;
1759 break;
1760 #endif
1761 case GL_FRAMEBUFFER_EXT:
1762 buffer = ctx->DrawBuffer;
1763 break;
1764 default:
1765 _mesa_error(ctx, GL_INVALID_ENUM,
1766 "glGetFramebufferAttachmentParameterivEXT(target)");
1767 return;
1768 }
1769
1770 if (buffer->Name == 0) {
1771 _mesa_error(ctx, GL_INVALID_OPERATION,
1772 "glGetFramebufferAttachmentParameterivEXT");
1773 return;
1774 }
1775
1776 att = _mesa_get_attachment(ctx, buffer, attachment);
1777 if (att == NULL) {
1778 _mesa_error(ctx, GL_INVALID_ENUM,
1779 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1780 return;
1781 }
1782
1783 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1784 /* the depth and stencil attachments must point to the same buffer */
1785 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1786 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1787 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1788 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1789 _mesa_error(ctx, GL_INVALID_OPERATION,
1790 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1791 " attachments differ)");
1792 return;
1793 }
1794 }
1795
1796 FLUSH_CURRENT(ctx, _NEW_BUFFERS);
1797 /* The above doesn't fully flush the drivers in the way that a
1798 * glFlush does, but that is required here:
1799 */
1800 if (ctx->Driver.Flush)
1801 ctx->Driver.Flush(ctx);
1802
1803 switch (pname) {
1804 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1805 *params = att->Type;
1806 return;
1807 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1808 if (att->Type == GL_RENDERBUFFER_EXT) {
1809 *params = att->Renderbuffer->Name;
1810 }
1811 else if (att->Type == GL_TEXTURE) {
1812 *params = att->Texture->Name;
1813 }
1814 else {
1815 _mesa_error(ctx, GL_INVALID_ENUM,
1816 "glGetFramebufferAttachmentParameterivEXT(pname)");
1817 }
1818 return;
1819 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1820 if (att->Type == GL_TEXTURE) {
1821 *params = att->TextureLevel;
1822 }
1823 else {
1824 _mesa_error(ctx, GL_INVALID_ENUM,
1825 "glGetFramebufferAttachmentParameterivEXT(pname)");
1826 }
1827 return;
1828 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1829 if (att->Type == GL_TEXTURE) {
1830 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1831 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1832 }
1833 else {
1834 *params = 0;
1835 }
1836 }
1837 else {
1838 _mesa_error(ctx, GL_INVALID_ENUM,
1839 "glGetFramebufferAttachmentParameterivEXT(pname)");
1840 }
1841 return;
1842 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1843 if (att->Type == GL_TEXTURE) {
1844 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1845 *params = att->Zoffset;
1846 }
1847 else {
1848 *params = 0;
1849 }
1850 }
1851 else {
1852 _mesa_error(ctx, GL_INVALID_ENUM,
1853 "glGetFramebufferAttachmentParameterivEXT(pname)");
1854 }
1855 return;
1856 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1857 if (!ctx->Extensions.ARB_framebuffer_object) {
1858 _mesa_error(ctx, GL_INVALID_ENUM,
1859 "glGetFramebufferAttachmentParameterivEXT(pname)");
1860 }
1861 else {
1862 *params = att->Renderbuffer->ColorEncoding;
1863 }
1864 return;
1865 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1866 if (!ctx->Extensions.ARB_framebuffer_object) {
1867 _mesa_error(ctx, GL_INVALID_ENUM,
1868 "glGetFramebufferAttachmentParameterivEXT(pname)");
1869 return;
1870 }
1871 else {
1872 *params = att->Renderbuffer->ComponentType;
1873 }
1874 return;
1875 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1876 if (!ctx->Extensions.ARB_framebuffer_object) {
1877 _mesa_error(ctx, GL_INVALID_ENUM,
1878 "glGetFramebufferAttachmentParameterivEXT(pname)");
1879 }
1880 else {
1881 *params = att->Renderbuffer->RedBits;
1882 }
1883 return;
1884 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1885 if (!ctx->Extensions.ARB_framebuffer_object) {
1886 _mesa_error(ctx, GL_INVALID_ENUM,
1887 "glGetFramebufferAttachmentParameterivEXT(pname)");
1888 }
1889 else {
1890 *params = att->Renderbuffer->GreenBits;
1891 }
1892 return;
1893 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1894 if (!ctx->Extensions.ARB_framebuffer_object) {
1895 _mesa_error(ctx, GL_INVALID_ENUM,
1896 "glGetFramebufferAttachmentParameterivEXT(pname)");
1897 }
1898 else {
1899 *params = att->Renderbuffer->BlueBits;
1900 }
1901 return;
1902 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1903 if (!ctx->Extensions.ARB_framebuffer_object) {
1904 _mesa_error(ctx, GL_INVALID_ENUM,
1905 "glGetFramebufferAttachmentParameterivEXT(pname)");
1906 }
1907 else {
1908 *params = att->Renderbuffer->AlphaBits;
1909 }
1910 return;
1911 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1912 if (!ctx->Extensions.ARB_framebuffer_object) {
1913 _mesa_error(ctx, GL_INVALID_ENUM,
1914 "glGetFramebufferAttachmentParameterivEXT(pname)");
1915 }
1916 else {
1917 *params = att->Renderbuffer->DepthBits;
1918 }
1919 return;
1920 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1921 if (!ctx->Extensions.ARB_framebuffer_object) {
1922 _mesa_error(ctx, GL_INVALID_ENUM,
1923 "glGetFramebufferAttachmentParameterivEXT(pname)");
1924 }
1925 else {
1926 *params = att->Renderbuffer->StencilBits;
1927 }
1928 return;
1929 default:
1930 _mesa_error(ctx, GL_INVALID_ENUM,
1931 "glGetFramebufferAttachmentParameterivEXT(pname)");
1932 return;
1933 }
1934 }
1935
1936
1937 void GLAPIENTRY
1938 _mesa_GenerateMipmapEXT(GLenum target)
1939 {
1940 struct gl_texture_unit *texUnit;
1941 struct gl_texture_object *texObj;
1942 GET_CURRENT_CONTEXT(ctx);
1943
1944 ASSERT_OUTSIDE_BEGIN_END(ctx);
1945 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1946
1947 switch (target) {
1948 case GL_TEXTURE_1D:
1949 case GL_TEXTURE_2D:
1950 case GL_TEXTURE_3D:
1951 case GL_TEXTURE_CUBE_MAP:
1952 /* OK, legal value */
1953 break;
1954 default:
1955 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1956 return;
1957 }
1958
1959 texUnit = _mesa_get_current_tex_unit(ctx);
1960 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1961
1962 _mesa_lock_texture(ctx, texObj);
1963 if (target == GL_TEXTURE_CUBE_MAP) {
1964 GLuint face;
1965 for (face = 0; face < 6; face++)
1966 ctx->Driver.GenerateMipmap(ctx,
1967 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
1968 texObj);
1969 }
1970 else {
1971 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1972 }
1973 _mesa_unlock_texture(ctx, texObj);
1974 }
1975
1976
1977 #if FEATURE_EXT_framebuffer_blit
1978 /**
1979 * Blit rectangular region, optionally from one framebuffer to another.
1980 *
1981 * Note, if the src buffer is multisampled and the dest is not, this is
1982 * when the samples must be resolved to a single color.
1983 */
1984 void GLAPIENTRY
1985 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1986 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1987 GLbitfield mask, GLenum filter)
1988 {
1989 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
1990 GL_DEPTH_BUFFER_BIT |
1991 GL_STENCIL_BUFFER_BIT);
1992 const struct gl_framebuffer *readFb, *drawFb;
1993 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
1994 GET_CURRENT_CONTEXT(ctx);
1995
1996 ASSERT_OUTSIDE_BEGIN_END(ctx);
1997 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1998
1999 if (ctx->NewState) {
2000 _mesa_update_state(ctx);
2001 }
2002
2003 readFb = ctx->ReadBuffer;
2004 drawFb = ctx->DrawBuffer;
2005
2006 if (!readFb || !drawFb) {
2007 /* This will normally never happen but someday we may want to
2008 * support MakeCurrent() with no drawables.
2009 */
2010 return;
2011 }
2012
2013 /* check for complete framebuffers */
2014 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2015 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2016 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2017 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2018 return;
2019 }
2020
2021 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2022 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2023 return;
2024 }
2025
2026 if (mask & ~legalMaskBits) {
2027 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2028 return;
2029 }
2030
2031 /* depth/stencil must be blitted with nearest filtering */
2032 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2033 && filter != GL_NEAREST) {
2034 _mesa_error(ctx, GL_INVALID_OPERATION,
2035 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2036 return;
2037 }
2038
2039 /* get color read/draw renderbuffers */
2040 if (mask & GL_COLOR_BUFFER_BIT) {
2041 colorReadRb = readFb->_ColorReadBuffer;
2042 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2043 }
2044 else {
2045 colorReadRb = colorDrawRb = NULL;
2046 }
2047
2048 if (mask & GL_STENCIL_BUFFER_BIT) {
2049 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2050 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2051 if (!readRb ||
2052 !drawRb ||
2053 readRb->StencilBits != drawRb->StencilBits) {
2054 _mesa_error(ctx, GL_INVALID_OPERATION,
2055 "glBlitFramebufferEXT(stencil buffer size mismatch");
2056 return;
2057 }
2058 }
2059
2060 if (mask & GL_DEPTH_BUFFER_BIT) {
2061 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2062 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2063 if (!readRb ||
2064 !drawRb ||
2065 readRb->DepthBits != drawRb->DepthBits) {
2066 _mesa_error(ctx, GL_INVALID_OPERATION,
2067 "glBlitFramebufferEXT(depth buffer size mismatch");
2068 return;
2069 }
2070 }
2071
2072 if (readFb->Visual.samples > 0 &&
2073 drawFb->Visual.samples > 0 &&
2074 readFb->Visual.samples != drawFb->Visual.samples) {
2075 _mesa_error(ctx, GL_INVALID_OPERATION,
2076 "glBlitFramebufferEXT(mismatched samples");
2077 return;
2078 }
2079
2080 /* extra checks for multisample copies... */
2081 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2082 /* src and dest region sizes must be the same */
2083 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2084 srcY1 - srcY0 != dstY1 - dstY0) {
2085 _mesa_error(ctx, GL_INVALID_OPERATION,
2086 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2087 return;
2088 }
2089
2090 /* color formats must match */
2091 if (colorReadRb &&
2092 colorDrawRb &&
2093 colorReadRb->_ActualFormat != colorDrawRb->_ActualFormat) {
2094 _mesa_error(ctx, GL_INVALID_OPERATION,
2095 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2096 return;
2097 }
2098 }
2099
2100 if (!ctx->Extensions.EXT_framebuffer_blit) {
2101 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2102 return;
2103 }
2104
2105 ASSERT(ctx->Driver.BlitFramebuffer);
2106 ctx->Driver.BlitFramebuffer(ctx,
2107 srcX0, srcY0, srcX1, srcY1,
2108 dstX0, dstY0, dstX1, dstY1,
2109 mask, filter);
2110 }
2111 #endif /* FEATURE_EXT_framebuffer_blit */