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