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