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