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