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