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