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