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