c0f617d6971a3d6e46673159a06f054beb046e26
[mesa.git] / src / mesa / main / fbobject.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 #include <stdbool.h>
35
36 #include "buffers.h"
37 #include "context.h"
38 #include "enums.h"
39 #include "fbobject.h"
40 #include "formats.h"
41 #include "framebuffer.h"
42 #include "glformats.h"
43 #include "hash.h"
44 #include "macros.h"
45 #include "multisample.h"
46 #include "mtypes.h"
47 #include "renderbuffer.h"
48 #include "state.h"
49 #include "teximage.h"
50 #include "texobj.h"
51
52
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
54 #define DEBUG_BLIT 0
55
56
57 /**
58 * Notes:
59 *
60 * None of the GL_EXT_framebuffer_object functions are compiled into
61 * display lists.
62 */
63
64
65
66 /*
67 * When glGenRender/FramebuffersEXT() is called we insert pointers to
68 * these placeholder objects into the hash table.
69 * Later, when the object ID is first bound, we replace the placeholder
70 * with the real frame/renderbuffer.
71 */
72 static struct gl_framebuffer DummyFramebuffer;
73 static struct gl_renderbuffer DummyRenderbuffer;
74
75 /* We bind this framebuffer when applications pass a NULL
76 * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer;
78
79
80 static void
81 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
82 {
83 /* no op */
84 }
85
86 static void
87 delete_dummy_framebuffer(struct gl_framebuffer *fb)
88 {
89 /* no op */
90 }
91
92
93 void
94 _mesa_init_fbobjects(struct gl_context *ctx)
95 {
96 _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
97 _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
98 _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
99 DummyFramebuffer.Delete = delete_dummy_framebuffer;
100 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
101 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
102 }
103
104 struct gl_framebuffer *
105 _mesa_get_incomplete_framebuffer(void)
106 {
107 return &IncompleteFramebuffer;
108 }
109
110 /**
111 * Helper routine for getting a gl_renderbuffer.
112 */
113 struct gl_renderbuffer *
114 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
115 {
116 struct gl_renderbuffer *rb;
117
118 if (id == 0)
119 return NULL;
120
121 rb = (struct gl_renderbuffer *)
122 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
123 return rb;
124 }
125
126
127 /**
128 * Helper routine for getting a gl_framebuffer.
129 */
130 struct gl_framebuffer *
131 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
132 {
133 struct gl_framebuffer *fb;
134
135 if (id == 0)
136 return NULL;
137
138 fb = (struct gl_framebuffer *)
139 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
140 return fb;
141 }
142
143
144 /**
145 * Mark the given framebuffer as invalid. This will force the
146 * test for framebuffer completeness to be done before the framebuffer
147 * is used.
148 */
149 static void
150 invalidate_framebuffer(struct gl_framebuffer *fb)
151 {
152 fb->_Status = 0; /* "indeterminate" */
153 }
154
155
156 /**
157 * Return the gl_framebuffer object which corresponds to the given
158 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159 * Check support for GL_EXT_framebuffer_blit to determine if certain
160 * targets are legal.
161 * \return gl_framebuffer pointer or NULL if target is illegal
162 */
163 static struct gl_framebuffer *
164 get_framebuffer_target(struct gl_context *ctx, GLenum target)
165 {
166 bool have_fb_blit = _mesa_is_gles3(ctx) ||
167 (ctx->Extensions.EXT_framebuffer_blit && _mesa_is_desktop_gl(ctx));
168 switch (target) {
169 case GL_DRAW_FRAMEBUFFER:
170 return have_fb_blit ? ctx->DrawBuffer : NULL;
171 case GL_READ_FRAMEBUFFER:
172 return have_fb_blit ? ctx->ReadBuffer : NULL;
173 case GL_FRAMEBUFFER_EXT:
174 return ctx->DrawBuffer;
175 default:
176 return NULL;
177 }
178 }
179
180
181 /**
182 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183 * gl_renderbuffer_attachment object.
184 * This function is only used for user-created FB objects, not the
185 * default / window-system FB object.
186 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187 * the depth buffer attachment point.
188 */
189 struct gl_renderbuffer_attachment *
190 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
191 GLenum attachment)
192 {
193 GLuint i;
194
195 assert(_mesa_is_user_fbo(fb));
196
197 switch (attachment) {
198 case GL_COLOR_ATTACHMENT0_EXT:
199 case GL_COLOR_ATTACHMENT1_EXT:
200 case GL_COLOR_ATTACHMENT2_EXT:
201 case GL_COLOR_ATTACHMENT3_EXT:
202 case GL_COLOR_ATTACHMENT4_EXT:
203 case GL_COLOR_ATTACHMENT5_EXT:
204 case GL_COLOR_ATTACHMENT6_EXT:
205 case GL_COLOR_ATTACHMENT7_EXT:
206 case GL_COLOR_ATTACHMENT8_EXT:
207 case GL_COLOR_ATTACHMENT9_EXT:
208 case GL_COLOR_ATTACHMENT10_EXT:
209 case GL_COLOR_ATTACHMENT11_EXT:
210 case GL_COLOR_ATTACHMENT12_EXT:
211 case GL_COLOR_ATTACHMENT13_EXT:
212 case GL_COLOR_ATTACHMENT14_EXT:
213 case GL_COLOR_ATTACHMENT15_EXT:
214 /* Only OpenGL ES 1.x forbids color attachments other than
215 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
216 * hardware is used.
217 */
218 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
219 if (i >= ctx->Const.MaxColorAttachments
220 || (i > 0 && ctx->API == API_OPENGLES)) {
221 return NULL;
222 }
223 return &fb->Attachment[BUFFER_COLOR0 + i];
224 case GL_DEPTH_STENCIL_ATTACHMENT:
225 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
226 return NULL;
227 /* fall-through */
228 case GL_DEPTH_ATTACHMENT_EXT:
229 return &fb->Attachment[BUFFER_DEPTH];
230 case GL_STENCIL_ATTACHMENT_EXT:
231 return &fb->Attachment[BUFFER_STENCIL];
232 default:
233 return NULL;
234 }
235 }
236
237
238 /**
239 * As above, but only used for getting attachments of the default /
240 * window-system framebuffer (not user-created framebuffer objects).
241 */
242 static struct gl_renderbuffer_attachment *
243 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
244 GLenum attachment)
245 {
246 assert(_mesa_is_winsys_fbo(fb));
247
248 if (_mesa_is_gles3(ctx)) {
249 assert(attachment == GL_BACK ||
250 attachment == GL_DEPTH ||
251 attachment == GL_STENCIL);
252 switch (attachment) {
253 case GL_BACK:
254 /* Since there is no stereo rendering in ES 3.0, only return the
255 * LEFT bits.
256 */
257 if (ctx->DrawBuffer->Visual.doubleBufferMode)
258 return &fb->Attachment[BUFFER_BACK_LEFT];
259 return &fb->Attachment[BUFFER_FRONT_LEFT];
260 case GL_DEPTH:
261 return &fb->Attachment[BUFFER_DEPTH];
262 case GL_STENCIL:
263 return &fb->Attachment[BUFFER_STENCIL];
264 }
265 }
266
267 switch (attachment) {
268 case GL_FRONT_LEFT:
269 return &fb->Attachment[BUFFER_FRONT_LEFT];
270 case GL_FRONT_RIGHT:
271 return &fb->Attachment[BUFFER_FRONT_RIGHT];
272 case GL_BACK_LEFT:
273 return &fb->Attachment[BUFFER_BACK_LEFT];
274 case GL_BACK_RIGHT:
275 return &fb->Attachment[BUFFER_BACK_RIGHT];
276 case GL_AUX0:
277 if (fb->Visual.numAuxBuffers == 1) {
278 return &fb->Attachment[BUFFER_AUX0];
279 }
280 return NULL;
281
282 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
283 *
284 * "If the default framebuffer is bound to target, then attachment must
285 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
286 * identifying a color buffer; DEPTH, identifying the depth buffer; or
287 * STENCIL, identifying the stencil buffer."
288 *
289 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
290 * language. However, revision #33 of the ARB_framebuffer_object spec
291 * says:
292 *
293 * "If the default framebuffer is bound to <target>, then <attachment>
294 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
295 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
296 * depth buffer, or the stencil buffer, and <pname> may be
297 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
298 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
299 *
300 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
301 * from glext.h, so shipping apps should not use those values.
302 *
303 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
304 * support queries of the window system FBO.
305 */
306 case GL_DEPTH:
307 return &fb->Attachment[BUFFER_DEPTH];
308 case GL_STENCIL:
309 return &fb->Attachment[BUFFER_STENCIL];
310 default:
311 return NULL;
312 }
313 }
314
315
316
317 /**
318 * Remove any texture or renderbuffer attached to the given attachment
319 * point. Update reference counts, etc.
320 */
321 void
322 _mesa_remove_attachment(struct gl_context *ctx,
323 struct gl_renderbuffer_attachment *att)
324 {
325 struct gl_renderbuffer *rb = att->Renderbuffer;
326
327 /* tell driver that we're done rendering to this texture. */
328 if (rb && rb->NeedsFinishRenderTexture)
329 ctx->Driver.FinishRenderTexture(ctx, rb);
330
331 if (att->Type == GL_TEXTURE) {
332 ASSERT(att->Texture);
333 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
334 ASSERT(!att->Texture);
335 }
336 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
337 ASSERT(!att->Texture);
338 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
339 ASSERT(!att->Renderbuffer);
340 }
341 att->Type = GL_NONE;
342 att->Complete = GL_TRUE;
343 }
344
345 /**
346 * Create a renderbuffer which will be set up by the driver to wrap the
347 * texture image slice.
348 *
349 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
350 * to share most of their framebuffer rendering code between winsys,
351 * renderbuffer, and texture attachments.
352 *
353 * The allocated renderbuffer uses a non-zero Name so that drivers can check
354 * it for determining vertical orientation, but we use ~0 to make it fairly
355 * unambiguous with actual user (non-texture) renderbuffers.
356 */
357 void
358 _mesa_update_texture_renderbuffer(struct gl_context *ctx,
359 struct gl_framebuffer *fb,
360 struct gl_renderbuffer_attachment *att)
361 {
362 struct gl_texture_image *texImage;
363 struct gl_renderbuffer *rb;
364
365 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
366 if (!texImage)
367 return;
368
369 rb = att->Renderbuffer;
370 if (!rb) {
371 rb = ctx->Driver.NewRenderbuffer(ctx, ~0);
372 if (!rb) {
373 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
374 return;
375 }
376 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
377
378 /* This can't get called on a texture renderbuffer, so set it to NULL
379 * for clarity compared to user renderbuffers.
380 */
381 rb->AllocStorage = NULL;
382
383 rb->NeedsFinishRenderTexture = ctx->Driver.FinishRenderTexture != NULL;
384 }
385
386 rb->_BaseFormat = texImage->_BaseFormat;
387 rb->Format = texImage->TexFormat;
388 rb->InternalFormat = texImage->InternalFormat;
389 rb->Width = texImage->Width2;
390 rb->Height = texImage->Height2;
391 rb->NumSamples = texImage->NumSamples;
392 rb->TexImage = texImage;
393
394 ctx->Driver.RenderTexture(ctx, fb, att);
395 }
396
397 /**
398 * Bind a texture object to an attachment point.
399 * The previous binding, if any, will be removed first.
400 */
401 void
402 _mesa_set_texture_attachment(struct gl_context *ctx,
403 struct gl_framebuffer *fb,
404 struct gl_renderbuffer_attachment *att,
405 struct gl_texture_object *texObj,
406 GLenum texTarget, GLuint level, GLuint zoffset,
407 GLboolean layered)
408 {
409 struct gl_renderbuffer *rb = att->Renderbuffer;
410
411 if (rb && rb->NeedsFinishRenderTexture)
412 ctx->Driver.FinishRenderTexture(ctx, rb);
413
414 if (att->Texture == texObj) {
415 /* re-attaching same texture */
416 ASSERT(att->Type == GL_TEXTURE);
417 }
418 else {
419 /* new attachment */
420 _mesa_remove_attachment(ctx, att);
421 att->Type = GL_TEXTURE;
422 assert(!att->Texture);
423 _mesa_reference_texobj(&att->Texture, texObj);
424 }
425 invalidate_framebuffer(fb);
426
427 /* always update these fields */
428 att->TextureLevel = level;
429 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
430 att->Zoffset = zoffset;
431 att->Layered = layered;
432 att->Complete = GL_FALSE;
433
434 _mesa_update_texture_renderbuffer(ctx, fb, att);
435 }
436
437
438 /**
439 * Bind a renderbuffer to an attachment point.
440 * The previous binding, if any, will be removed first.
441 */
442 void
443 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
444 struct gl_renderbuffer_attachment *att,
445 struct gl_renderbuffer *rb)
446 {
447 /* XXX check if re-doing same attachment, exit early */
448 _mesa_remove_attachment(ctx, att);
449 att->Type = GL_RENDERBUFFER_EXT;
450 att->Texture = NULL; /* just to be safe */
451 att->Complete = GL_FALSE;
452 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
453 }
454
455
456 /**
457 * Fallback for ctx->Driver.FramebufferRenderbuffer()
458 * Attach a renderbuffer object to a framebuffer object.
459 */
460 void
461 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
462 struct gl_framebuffer *fb,
463 GLenum attachment, struct gl_renderbuffer *rb)
464 {
465 struct gl_renderbuffer_attachment *att;
466
467 _glthread_LOCK_MUTEX(fb->Mutex);
468
469 att = _mesa_get_attachment(ctx, fb, attachment);
470 ASSERT(att);
471 if (rb) {
472 _mesa_set_renderbuffer_attachment(ctx, att, rb);
473 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
474 /* do stencil attachment here (depth already done above) */
475 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
476 assert(att);
477 _mesa_set_renderbuffer_attachment(ctx, att, rb);
478 }
479 rb->AttachedAnytime = GL_TRUE;
480 }
481 else {
482 _mesa_remove_attachment(ctx, att);
483 }
484
485 invalidate_framebuffer(fb);
486
487 _glthread_UNLOCK_MUTEX(fb->Mutex);
488 }
489
490
491 /**
492 * Fallback for ctx->Driver.ValidateFramebuffer()
493 * Check if the renderbuffer's formats are supported by the software
494 * renderer.
495 * Drivers should probably override this.
496 */
497 void
498 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
499 {
500 gl_buffer_index buf;
501 for (buf = 0; buf < BUFFER_COUNT; buf++) {
502 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
503 if (rb) {
504 switch (rb->_BaseFormat) {
505 case GL_ALPHA:
506 case GL_LUMINANCE_ALPHA:
507 case GL_LUMINANCE:
508 case GL_INTENSITY:
509 case GL_RED:
510 case GL_RG:
511 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
512 return;
513
514 default:
515 switch (rb->Format) {
516 /* XXX This list is likely incomplete. */
517 case MESA_FORMAT_RGB9_E5_FLOAT:
518 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
519 return;
520 default:;
521 /* render buffer format is supported by software rendering */
522 }
523 }
524 }
525 }
526 }
527
528
529 /**
530 * Return true if the framebuffer has a combined depth/stencil
531 * renderbuffer attached.
532 */
533 GLboolean
534 _mesa_has_depthstencil_combined(const struct gl_framebuffer *fb)
535 {
536 const struct gl_renderbuffer_attachment *depth =
537 &fb->Attachment[BUFFER_DEPTH];
538 const struct gl_renderbuffer_attachment *stencil =
539 &fb->Attachment[BUFFER_STENCIL];
540
541 if (depth->Type == stencil->Type) {
542 if (depth->Type == GL_RENDERBUFFER_EXT &&
543 depth->Renderbuffer == stencil->Renderbuffer)
544 return GL_TRUE;
545
546 if (depth->Type == GL_TEXTURE &&
547 depth->Texture == stencil->Texture)
548 return GL_TRUE;
549 }
550
551 return GL_FALSE;
552 }
553
554
555 /**
556 * For debug only.
557 */
558 static void
559 att_incomplete(const char *msg)
560 {
561 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
562 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
563 }
564 }
565
566
567 /**
568 * For debug only.
569 */
570 static void
571 fbo_incomplete(struct gl_context *ctx, const char *msg, int index)
572 {
573 static GLuint msg_id;
574
575 _mesa_gl_debug(ctx, &msg_id,
576 MESA_DEBUG_TYPE_OTHER,
577 MESA_DEBUG_SEVERITY_MEDIUM,
578 "FBO incomplete: %s [%d]\n", msg, index);
579
580 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
581 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
582 }
583 }
584
585
586 /**
587 * Is the given base format a legal format for a color renderbuffer?
588 */
589 GLboolean
590 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
591 {
592 switch (baseFormat) {
593 case GL_RGB:
594 case GL_RGBA:
595 return GL_TRUE;
596 case GL_LUMINANCE:
597 case GL_LUMINANCE_ALPHA:
598 case GL_INTENSITY:
599 case GL_ALPHA:
600 return ctx->API == API_OPENGL_COMPAT &&
601 ctx->Extensions.ARB_framebuffer_object;
602 case GL_RED:
603 case GL_RG:
604 return ctx->Extensions.ARB_texture_rg;
605 default:
606 return GL_FALSE;
607 }
608 }
609
610
611 /**
612 * Is the given base format a legal format for a color renderbuffer?
613 */
614 static GLboolean
615 is_format_color_renderable(const struct gl_context *ctx, gl_format format, GLenum internalFormat)
616 {
617 const GLenum baseFormat =
618 _mesa_get_format_base_format(format);
619 GLboolean valid;
620
621 valid = _mesa_is_legal_color_format(ctx, baseFormat);
622 if (!valid || _mesa_is_desktop_gl(ctx)) {
623 return valid;
624 }
625
626 /* Reject additional cases for GLES */
627 switch (internalFormat) {
628 case GL_RGBA8_SNORM:
629 case GL_RGB32F:
630 case GL_RGB32I:
631 case GL_RGB32UI:
632 case GL_RGB16F:
633 case GL_RGB16I:
634 case GL_RGB16UI:
635 case GL_RGB8_SNORM:
636 case GL_RGB8I:
637 case GL_RGB8UI:
638 case GL_SRGB8:
639 case GL_RGB9_E5:
640 case GL_RG8_SNORM:
641 case GL_R8_SNORM:
642 return GL_FALSE;
643 default:
644 break;
645 }
646
647 if (format == MESA_FORMAT_ARGB2101010 && internalFormat != GL_RGB10_A2) {
648 return GL_FALSE;
649 }
650
651 return GL_TRUE;
652 }
653
654
655 /**
656 * Is the given base format a legal format for a depth/stencil renderbuffer?
657 */
658 static GLboolean
659 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
660 {
661 switch (baseFormat) {
662 case GL_DEPTH_COMPONENT:
663 case GL_DEPTH_STENCIL_EXT:
664 return GL_TRUE;
665 default:
666 return GL_FALSE;
667 }
668 }
669
670
671 /**
672 * Test if an attachment point is complete and update its Complete field.
673 * \param format if GL_COLOR, this is a color attachment point,
674 * if GL_DEPTH, this is a depth component attachment point,
675 * if GL_STENCIL, this is a stencil component attachment point.
676 */
677 static void
678 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
679 struct gl_renderbuffer_attachment *att)
680 {
681 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
682
683 /* assume complete */
684 att->Complete = GL_TRUE;
685
686 /* Look for reasons why the attachment might be incomplete */
687 if (att->Type == GL_TEXTURE) {
688 const struct gl_texture_object *texObj = att->Texture;
689 struct gl_texture_image *texImage;
690 GLenum baseFormat;
691
692 if (!texObj) {
693 att_incomplete("no texobj");
694 att->Complete = GL_FALSE;
695 return;
696 }
697
698 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
699 if (!texImage) {
700 att_incomplete("no teximage");
701 att->Complete = GL_FALSE;
702 return;
703 }
704 if (texImage->Width < 1 || texImage->Height < 1) {
705 att_incomplete("teximage width/height=0");
706 printf("texobj = %u\n", texObj->Name);
707 printf("level = %d\n", att->TextureLevel);
708 att->Complete = GL_FALSE;
709 return;
710 }
711 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
712 att_incomplete("bad z offset");
713 att->Complete = GL_FALSE;
714 return;
715 }
716
717 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
718
719 if (format == GL_COLOR) {
720 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
721 att_incomplete("bad format");
722 att->Complete = GL_FALSE;
723 return;
724 }
725 if (_mesa_is_format_compressed(texImage->TexFormat)) {
726 att_incomplete("compressed internalformat");
727 att->Complete = GL_FALSE;
728 return;
729 }
730 }
731 else if (format == GL_DEPTH) {
732 if (baseFormat == GL_DEPTH_COMPONENT) {
733 /* OK */
734 }
735 else if (ctx->Extensions.EXT_packed_depth_stencil &&
736 ctx->Extensions.ARB_depth_texture &&
737 baseFormat == GL_DEPTH_STENCIL_EXT) {
738 /* OK */
739 }
740 else {
741 att->Complete = GL_FALSE;
742 att_incomplete("bad depth format");
743 return;
744 }
745 }
746 else {
747 ASSERT(format == GL_STENCIL);
748 if (ctx->Extensions.EXT_packed_depth_stencil &&
749 ctx->Extensions.ARB_depth_texture &&
750 baseFormat == GL_DEPTH_STENCIL_EXT) {
751 /* OK */
752 }
753 else {
754 /* no such thing as stencil-only textures */
755 att_incomplete("illegal stencil texture");
756 att->Complete = GL_FALSE;
757 return;
758 }
759 }
760 }
761 else if (att->Type == GL_RENDERBUFFER_EXT) {
762 const GLenum baseFormat =
763 _mesa_get_format_base_format(att->Renderbuffer->Format);
764
765 ASSERT(att->Renderbuffer);
766 if (!att->Renderbuffer->InternalFormat ||
767 att->Renderbuffer->Width < 1 ||
768 att->Renderbuffer->Height < 1) {
769 att_incomplete("0x0 renderbuffer");
770 att->Complete = GL_FALSE;
771 return;
772 }
773 if (format == GL_COLOR) {
774 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
775 att_incomplete("bad renderbuffer color format");
776 att->Complete = GL_FALSE;
777 return;
778 }
779 }
780 else if (format == GL_DEPTH) {
781 if (baseFormat == GL_DEPTH_COMPONENT) {
782 /* OK */
783 }
784 else if (ctx->Extensions.EXT_packed_depth_stencil &&
785 baseFormat == GL_DEPTH_STENCIL_EXT) {
786 /* OK */
787 }
788 else {
789 att_incomplete("bad renderbuffer depth format");
790 att->Complete = GL_FALSE;
791 return;
792 }
793 }
794 else {
795 assert(format == GL_STENCIL);
796 if (baseFormat == GL_STENCIL_INDEX) {
797 /* OK */
798 }
799 else if (ctx->Extensions.EXT_packed_depth_stencil &&
800 baseFormat == GL_DEPTH_STENCIL_EXT) {
801 /* OK */
802 }
803 else {
804 att->Complete = GL_FALSE;
805 att_incomplete("bad renderbuffer stencil format");
806 return;
807 }
808 }
809 }
810 else {
811 ASSERT(att->Type == GL_NONE);
812 /* complete */
813 return;
814 }
815 }
816
817
818 /**
819 * Test if the given framebuffer object is complete and update its
820 * Status field with the results.
821 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
822 * driver to make hardware-specific validation/completeness checks.
823 * Also update the framebuffer's Width and Height fields if the
824 * framebuffer is complete.
825 */
826 void
827 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
828 struct gl_framebuffer *fb)
829 {
830 GLuint numImages;
831 GLenum intFormat = GL_NONE; /* color buffers' internal format */
832 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
833 GLint numSamples = -1;
834 GLint fixedSampleLocations = -1;
835 GLint i;
836 GLuint j;
837 bool layer_count_valid = false;
838 GLuint layer_count = 0, att_layer_count;
839
840 assert(_mesa_is_user_fbo(fb));
841
842 /* we're changing framebuffer fields here */
843 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
844
845 numImages = 0;
846 fb->Width = 0;
847 fb->Height = 0;
848 fb->_AllColorBuffersFixedPoint = GL_TRUE;
849 fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
850
851 /* Start at -2 to more easily loop over all attachment points.
852 * -2: depth buffer
853 * -1: stencil buffer
854 * >=0: color buffer
855 */
856 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
857 struct gl_renderbuffer_attachment *att;
858 GLenum f;
859 gl_format attFormat;
860
861 /*
862 * XXX for ARB_fbo, only check color buffers that are named by
863 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
864 */
865
866 /* check for attachment completeness
867 */
868 if (i == -2) {
869 att = &fb->Attachment[BUFFER_DEPTH];
870 test_attachment_completeness(ctx, GL_DEPTH, att);
871 if (!att->Complete) {
872 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
873 fbo_incomplete(ctx, "depth attachment incomplete", -1);
874 return;
875 }
876 }
877 else if (i == -1) {
878 att = &fb->Attachment[BUFFER_STENCIL];
879 test_attachment_completeness(ctx, GL_STENCIL, att);
880 if (!att->Complete) {
881 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
882 fbo_incomplete(ctx, "stencil attachment incomplete", -1);
883 return;
884 }
885 }
886 else {
887 att = &fb->Attachment[BUFFER_COLOR0 + i];
888 test_attachment_completeness(ctx, GL_COLOR, att);
889 if (!att->Complete) {
890 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
891 fbo_incomplete(ctx, "color attachment incomplete", i);
892 return;
893 }
894 }
895
896 /* get width, height, format of the renderbuffer/texture
897 */
898 if (att->Type == GL_TEXTURE) {
899 const struct gl_texture_image *texImg = att->Renderbuffer->TexImage;
900 minWidth = MIN2(minWidth, texImg->Width);
901 maxWidth = MAX2(maxWidth, texImg->Width);
902 minHeight = MIN2(minHeight, texImg->Height);
903 maxHeight = MAX2(maxHeight, texImg->Height);
904 f = texImg->_BaseFormat;
905 attFormat = texImg->TexFormat;
906 numImages++;
907
908 if (!is_format_color_renderable(ctx, attFormat, texImg->InternalFormat) &&
909 !is_legal_depth_format(ctx, f)) {
910 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
911 fbo_incomplete(ctx, "texture attachment incomplete", -1);
912 return;
913 }
914
915 if (numSamples < 0)
916 numSamples = texImg->NumSamples;
917 else if (numSamples != texImg->NumSamples) {
918 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
919 fbo_incomplete(ctx, "inconsistent sample count", -1);
920 return;
921 }
922
923 if (fixedSampleLocations < 0)
924 fixedSampleLocations = texImg->FixedSampleLocations;
925 else if (fixedSampleLocations != texImg->FixedSampleLocations) {
926 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
927 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
928 return;
929 }
930 }
931 else if (att->Type == GL_RENDERBUFFER_EXT) {
932 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
933 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
934 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
935 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
936 f = att->Renderbuffer->InternalFormat;
937 attFormat = att->Renderbuffer->Format;
938 numImages++;
939
940 if (numSamples < 0)
941 numSamples = att->Renderbuffer->NumSamples;
942 else if (numSamples != att->Renderbuffer->NumSamples) {
943 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
944 fbo_incomplete(ctx, "inconsistent sample count", -1);
945 return;
946 }
947
948 /* RENDERBUFFER has fixedSampleLocations implicitly true */
949 if (fixedSampleLocations < 0)
950 fixedSampleLocations = GL_TRUE;
951 else if (fixedSampleLocations != GL_TRUE) {
952 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
953 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
954 return;
955 }
956 }
957 else {
958 assert(att->Type == GL_NONE);
959 continue;
960 }
961
962 /* check if integer color */
963 fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
964
965 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
966 if (i >= 0) {
967 GLenum type = _mesa_get_format_datatype(attFormat);
968
969 fb->_AllColorBuffersFixedPoint =
970 fb->_AllColorBuffersFixedPoint &&
971 (type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED);
972
973 fb->_HasSNormOrFloatColorBuffer =
974 fb->_HasSNormOrFloatColorBuffer ||
975 type == GL_SIGNED_NORMALIZED || type == GL_FLOAT;
976 }
977
978 /* Error-check width, height, format */
979 if (numImages == 1) {
980 /* save format */
981 if (i >= 0) {
982 intFormat = f;
983 }
984 }
985 else {
986 if (!ctx->Extensions.ARB_framebuffer_object) {
987 /* check that width, height, format are same */
988 if (minWidth != maxWidth || minHeight != maxHeight) {
989 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
990 fbo_incomplete(ctx, "width or height mismatch", -1);
991 return;
992 }
993 /* check that all color buffers are the same format */
994 if (intFormat != GL_NONE && f != intFormat) {
995 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
996 fbo_incomplete(ctx, "format mismatch", -1);
997 return;
998 }
999 }
1000 }
1001
1002 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1003 */
1004 if (att->Type == GL_RENDERBUFFER &&
1005 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
1006 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1007 fbo_incomplete(ctx, "unsupported renderbuffer format", i);
1008 return;
1009 }
1010
1011 /* Check that layered rendering is consistent. */
1012 att_layer_count = att->Layered ? att->Renderbuffer->Depth : 0;
1013 if (!layer_count_valid) {
1014 layer_count = att_layer_count;
1015 layer_count_valid = true;
1016 } else if (layer_count != att_layer_count) {
1017 if (layer_count == 0 || att_layer_count == 0) {
1018 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1019 fbo_incomplete(ctx, "framebuffer attachment layer mode is inconsistent", i);
1020 } else {
1021 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB;
1022 fbo_incomplete(ctx, "framebuffer attachment layer count is inconsistent", i);
1023 }
1024 return;
1025 }
1026 }
1027
1028 fb->Layered = layer_count > 0;
1029
1030 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
1031 /* Check that all DrawBuffers are present */
1032 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
1033 if (fb->ColorDrawBuffer[j] != GL_NONE) {
1034 const struct gl_renderbuffer_attachment *att
1035 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
1036 assert(att);
1037 if (att->Type == GL_NONE) {
1038 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
1039 fbo_incomplete(ctx, "missing drawbuffer", j);
1040 return;
1041 }
1042 }
1043 }
1044
1045 /* Check that the ReadBuffer is present */
1046 if (fb->ColorReadBuffer != GL_NONE) {
1047 const struct gl_renderbuffer_attachment *att
1048 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
1049 assert(att);
1050 if (att->Type == GL_NONE) {
1051 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
1052 fbo_incomplete(ctx, "missing readbuffer", -1);
1053 return;
1054 }
1055 }
1056 }
1057
1058 if (numImages == 0) {
1059 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1060 fbo_incomplete(ctx, "no attachments", -1);
1061 return;
1062 }
1063
1064 /* Provisionally set status = COMPLETE ... */
1065 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
1066
1067 /* ... but the driver may say the FB is incomplete.
1068 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1069 * if anything.
1070 */
1071 if (ctx->Driver.ValidateFramebuffer) {
1072 ctx->Driver.ValidateFramebuffer(ctx, fb);
1073 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1074 fbo_incomplete(ctx, "driver marked FBO as incomplete", -1);
1075 }
1076 }
1077
1078 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
1079 /*
1080 * Note that if ARB_framebuffer_object is supported and the attached
1081 * renderbuffers/textures are different sizes, the framebuffer
1082 * width/height will be set to the smallest width/height.
1083 */
1084 fb->Width = minWidth;
1085 fb->Height = minHeight;
1086
1087 /* finally, update the visual info for the framebuffer */
1088 _mesa_update_framebuffer_visual(ctx, fb);
1089 }
1090 }
1091
1092
1093 GLboolean GLAPIENTRY
1094 _mesa_IsRenderbuffer(GLuint renderbuffer)
1095 {
1096 GET_CURRENT_CONTEXT(ctx);
1097 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1098 if (renderbuffer) {
1099 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1100 if (rb != NULL && rb != &DummyRenderbuffer)
1101 return GL_TRUE;
1102 }
1103 return GL_FALSE;
1104 }
1105
1106
1107 static void
1108 bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
1109 {
1110 struct gl_renderbuffer *newRb;
1111 GET_CURRENT_CONTEXT(ctx);
1112
1113 if (target != GL_RENDERBUFFER_EXT) {
1114 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
1115 return;
1116 }
1117
1118 /* No need to flush here since the render buffer binding has no
1119 * effect on rendering state.
1120 */
1121
1122 if (renderbuffer) {
1123 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1124 if (newRb == &DummyRenderbuffer) {
1125 /* ID was reserved, but no real renderbuffer object made yet */
1126 newRb = NULL;
1127 }
1128 else if (!newRb && !allow_user_names) {
1129 /* All RB IDs must be Gen'd */
1130 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1131 return;
1132 }
1133
1134 if (!newRb) {
1135 /* create new renderbuffer object */
1136 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
1137 if (!newRb) {
1138 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
1139 return;
1140 }
1141 ASSERT(newRb->AllocStorage);
1142 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1143 newRb->RefCount = 1; /* referenced by hash table */
1144 }
1145 }
1146 else {
1147 newRb = NULL;
1148 }
1149
1150 ASSERT(newRb != &DummyRenderbuffer);
1151
1152 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1153 }
1154
1155 void GLAPIENTRY
1156 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1157 {
1158 GET_CURRENT_CONTEXT(ctx);
1159
1160 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1161 * entry point, but they allow the use of user-generated names.
1162 */
1163 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1164 }
1165
1166 void GLAPIENTRY
1167 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1168 {
1169 /* This function should not be in the dispatch table for core profile /
1170 * OpenGL 3.1, so execution should never get here in those cases -- no
1171 * need for an explicit test.
1172 */
1173 bind_renderbuffer(target, renderbuffer, true);
1174 }
1175
1176
1177 /**
1178 * If the given renderbuffer is anywhere attached to the framebuffer, detach
1179 * the renderbuffer.
1180 * This is used when a renderbuffer object is deleted.
1181 * The spec calls for unbinding.
1182 */
1183 static void
1184 detach_renderbuffer(struct gl_context *ctx,
1185 struct gl_framebuffer *fb,
1186 struct gl_renderbuffer *rb)
1187 {
1188 GLuint i;
1189 for (i = 0; i < BUFFER_COUNT; i++) {
1190 if (fb->Attachment[i].Renderbuffer == rb) {
1191 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
1192 }
1193 }
1194 invalidate_framebuffer(fb);
1195 }
1196
1197
1198 void GLAPIENTRY
1199 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1200 {
1201 GLint i;
1202 GET_CURRENT_CONTEXT(ctx);
1203
1204 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1205
1206 for (i = 0; i < n; i++) {
1207 if (renderbuffers[i] > 0) {
1208 struct gl_renderbuffer *rb;
1209 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1210 if (rb) {
1211 /* check if deleting currently bound renderbuffer object */
1212 if (rb == ctx->CurrentRenderbuffer) {
1213 /* bind default */
1214 ASSERT(rb->RefCount >= 2);
1215 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1216 }
1217
1218 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1219 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1220 }
1221 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1222 && ctx->ReadBuffer != ctx->DrawBuffer) {
1223 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1224 }
1225
1226 /* Remove from hash table immediately, to free the ID.
1227 * But the object will not be freed until it's no longer
1228 * referenced anywhere else.
1229 */
1230 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1231
1232 if (rb != &DummyRenderbuffer) {
1233 /* no longer referenced by hash table */
1234 _mesa_reference_renderbuffer(&rb, NULL);
1235 }
1236 }
1237 }
1238 }
1239 }
1240
1241
1242 void GLAPIENTRY
1243 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1244 {
1245 GET_CURRENT_CONTEXT(ctx);
1246 GLuint first;
1247 GLint i;
1248
1249 if (n < 0) {
1250 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1251 return;
1252 }
1253
1254 if (!renderbuffers)
1255 return;
1256
1257 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1258
1259 for (i = 0; i < n; i++) {
1260 GLuint name = first + i;
1261 renderbuffers[i] = name;
1262 /* insert dummy placeholder into hash table */
1263 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1264 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1265 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1266 }
1267 }
1268
1269
1270 /**
1271 * Given an internal format token for a render buffer, return the
1272 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1273 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1274 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1275 *
1276 * This is similar to _mesa_base_tex_format() but the set of valid
1277 * internal formats is different.
1278 *
1279 * Note that even if a format is determined to be legal here, validation
1280 * of the FBO may fail if the format is not supported by the driver/GPU.
1281 *
1282 * \param internalFormat as passed to glRenderbufferStorage()
1283 * \return the base internal format, or 0 if internalFormat is illegal
1284 */
1285 GLenum
1286 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1287 {
1288 /*
1289 * Notes: some formats such as alpha, luminance, etc. were added
1290 * with GL_ARB_framebuffer_object.
1291 */
1292 switch (internalFormat) {
1293 case GL_ALPHA:
1294 case GL_ALPHA4:
1295 case GL_ALPHA8:
1296 case GL_ALPHA12:
1297 case GL_ALPHA16:
1298 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1299 ? GL_ALPHA : 0;
1300 case GL_LUMINANCE:
1301 case GL_LUMINANCE4:
1302 case GL_LUMINANCE8:
1303 case GL_LUMINANCE12:
1304 case GL_LUMINANCE16:
1305 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1306 ? GL_LUMINANCE : 0;
1307 case GL_LUMINANCE_ALPHA:
1308 case GL_LUMINANCE4_ALPHA4:
1309 case GL_LUMINANCE6_ALPHA2:
1310 case GL_LUMINANCE8_ALPHA8:
1311 case GL_LUMINANCE12_ALPHA4:
1312 case GL_LUMINANCE12_ALPHA12:
1313 case GL_LUMINANCE16_ALPHA16:
1314 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1315 ? GL_LUMINANCE_ALPHA : 0;
1316 case GL_INTENSITY:
1317 case GL_INTENSITY4:
1318 case GL_INTENSITY8:
1319 case GL_INTENSITY12:
1320 case GL_INTENSITY16:
1321 return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1322 ? GL_INTENSITY : 0;
1323 case GL_RGB8:
1324 return GL_RGB;
1325 case GL_RGB:
1326 case GL_R3_G3_B2:
1327 case GL_RGB4:
1328 case GL_RGB5:
1329 case GL_RGB10:
1330 case GL_RGB12:
1331 case GL_RGB16:
1332 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1333 case GL_SRGB8_EXT:
1334 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1335 case GL_RGBA4:
1336 case GL_RGB5_A1:
1337 case GL_RGBA8:
1338 return GL_RGBA;
1339 case GL_RGBA:
1340 case GL_RGBA2:
1341 case GL_RGBA12:
1342 case GL_RGBA16:
1343 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1344 case GL_RGB10_A2:
1345 case GL_SRGB8_ALPHA8_EXT:
1346 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1347 case GL_STENCIL_INDEX:
1348 case GL_STENCIL_INDEX1_EXT:
1349 case GL_STENCIL_INDEX4_EXT:
1350 case GL_STENCIL_INDEX16_EXT:
1351 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1352 * OpenGL ES, but Mesa does not currently support them.
1353 */
1354 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1355 case GL_STENCIL_INDEX8_EXT:
1356 return GL_STENCIL_INDEX;
1357 case GL_DEPTH_COMPONENT:
1358 case GL_DEPTH_COMPONENT32:
1359 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1360 case GL_DEPTH_COMPONENT16:
1361 case GL_DEPTH_COMPONENT24:
1362 return GL_DEPTH_COMPONENT;
1363 case GL_DEPTH_STENCIL_EXT:
1364 return _mesa_is_desktop_gl(ctx)
1365 && ctx->Extensions.EXT_packed_depth_stencil
1366 ? GL_DEPTH_STENCIL_EXT : 0;
1367 case GL_DEPTH24_STENCIL8_EXT:
1368 return ctx->Extensions.EXT_packed_depth_stencil
1369 ? GL_DEPTH_STENCIL_EXT : 0;
1370 case GL_DEPTH_COMPONENT32F:
1371 return ctx->Version >= 30
1372 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1373 ? GL_DEPTH_COMPONENT : 0;
1374 case GL_DEPTH32F_STENCIL8:
1375 return ctx->Version >= 30
1376 || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1377 ? GL_DEPTH_STENCIL : 0;
1378 case GL_RED:
1379 case GL_R16:
1380 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1381 ? GL_RED : 0;
1382 case GL_R8:
1383 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1384 ? GL_RED : 0;
1385 case GL_RG:
1386 case GL_RG16:
1387 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1388 ? GL_RG : 0;
1389 case GL_RG8:
1390 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1391 ? GL_RG : 0;
1392 /* signed normalized texture formats */
1393 case GL_RED_SNORM:
1394 case GL_R8_SNORM:
1395 case GL_R16_SNORM:
1396 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1397 ? GL_RED : 0;
1398 case GL_RG_SNORM:
1399 case GL_RG8_SNORM:
1400 case GL_RG16_SNORM:
1401 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1402 ? GL_RG : 0;
1403 case GL_RGB_SNORM:
1404 case GL_RGB8_SNORM:
1405 case GL_RGB16_SNORM:
1406 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1407 ? GL_RGB : 0;
1408 case GL_RGBA_SNORM:
1409 case GL_RGBA8_SNORM:
1410 case GL_RGBA16_SNORM:
1411 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1412 ? GL_RGBA : 0;
1413 case GL_ALPHA_SNORM:
1414 case GL_ALPHA8_SNORM:
1415 case GL_ALPHA16_SNORM:
1416 return ctx->API == API_OPENGL_COMPAT &&
1417 ctx->Extensions.EXT_texture_snorm &&
1418 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1419 case GL_R16F:
1420 case GL_R32F:
1421 return ((_mesa_is_desktop_gl(ctx) &&
1422 ctx->Extensions.ARB_texture_rg &&
1423 ctx->Extensions.ARB_texture_float) ||
1424 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1425 ? GL_RED : 0;
1426 case GL_RG16F:
1427 case GL_RG32F:
1428 return ((_mesa_is_desktop_gl(ctx) &&
1429 ctx->Extensions.ARB_texture_rg &&
1430 ctx->Extensions.ARB_texture_float) ||
1431 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1432 ? GL_RG : 0;
1433 case GL_RGB16F:
1434 case GL_RGB32F:
1435 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1436 ? GL_RGB : 0;
1437 case GL_RGBA16F:
1438 case GL_RGBA32F:
1439 return ((_mesa_is_desktop_gl(ctx) &&
1440 ctx->Extensions.ARB_texture_float) ||
1441 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1442 ? GL_RGBA : 0;
1443 case GL_ALPHA16F_ARB:
1444 case GL_ALPHA32F_ARB:
1445 return ctx->API == API_OPENGL_COMPAT &&
1446 ctx->Extensions.ARB_texture_float &&
1447 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1448 case GL_LUMINANCE16F_ARB:
1449 case GL_LUMINANCE32F_ARB:
1450 return ctx->API == API_OPENGL_COMPAT &&
1451 ctx->Extensions.ARB_texture_float &&
1452 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1453 case GL_LUMINANCE_ALPHA16F_ARB:
1454 case GL_LUMINANCE_ALPHA32F_ARB:
1455 return ctx->API == API_OPENGL_COMPAT &&
1456 ctx->Extensions.ARB_texture_float &&
1457 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1458 case GL_INTENSITY16F_ARB:
1459 case GL_INTENSITY32F_ARB:
1460 return ctx->API == API_OPENGL_COMPAT &&
1461 ctx->Extensions.ARB_texture_float &&
1462 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1463 case GL_RGB9_E5:
1464 return (_mesa_is_desktop_gl(ctx)
1465 && ctx->Extensions.EXT_texture_shared_exponent)
1466 ? GL_RGB : 0;
1467 case GL_R11F_G11F_B10F:
1468 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1469 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1470 ? GL_RGB : 0;
1471
1472 case GL_RGBA8UI_EXT:
1473 case GL_RGBA16UI_EXT:
1474 case GL_RGBA32UI_EXT:
1475 case GL_RGBA8I_EXT:
1476 case GL_RGBA16I_EXT:
1477 case GL_RGBA32I_EXT:
1478 return ctx->Version >= 30
1479 || (_mesa_is_desktop_gl(ctx) &&
1480 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1481
1482 case GL_RGB8UI_EXT:
1483 case GL_RGB16UI_EXT:
1484 case GL_RGB32UI_EXT:
1485 case GL_RGB8I_EXT:
1486 case GL_RGB16I_EXT:
1487 case GL_RGB32I_EXT:
1488 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1489 ? GL_RGB : 0;
1490 case GL_R8UI:
1491 case GL_R8I:
1492 case GL_R16UI:
1493 case GL_R16I:
1494 case GL_R32UI:
1495 case GL_R32I:
1496 return ctx->Version >= 30
1497 || (_mesa_is_desktop_gl(ctx) &&
1498 ctx->Extensions.ARB_texture_rg &&
1499 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1500
1501 case GL_RG8UI:
1502 case GL_RG8I:
1503 case GL_RG16UI:
1504 case GL_RG16I:
1505 case GL_RG32UI:
1506 case GL_RG32I:
1507 return ctx->Version >= 30
1508 || (_mesa_is_desktop_gl(ctx) &&
1509 ctx->Extensions.ARB_texture_rg &&
1510 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1511
1512 case GL_INTENSITY8I_EXT:
1513 case GL_INTENSITY8UI_EXT:
1514 case GL_INTENSITY16I_EXT:
1515 case GL_INTENSITY16UI_EXT:
1516 case GL_INTENSITY32I_EXT:
1517 case GL_INTENSITY32UI_EXT:
1518 return ctx->API == API_OPENGL_COMPAT &&
1519 ctx->Extensions.EXT_texture_integer &&
1520 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1521
1522 case GL_LUMINANCE8I_EXT:
1523 case GL_LUMINANCE8UI_EXT:
1524 case GL_LUMINANCE16I_EXT:
1525 case GL_LUMINANCE16UI_EXT:
1526 case GL_LUMINANCE32I_EXT:
1527 case GL_LUMINANCE32UI_EXT:
1528 return ctx->API == API_OPENGL_COMPAT &&
1529 ctx->Extensions.EXT_texture_integer &&
1530 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1531
1532 case GL_LUMINANCE_ALPHA8I_EXT:
1533 case GL_LUMINANCE_ALPHA8UI_EXT:
1534 case GL_LUMINANCE_ALPHA16I_EXT:
1535 case GL_LUMINANCE_ALPHA16UI_EXT:
1536 case GL_LUMINANCE_ALPHA32I_EXT:
1537 case GL_LUMINANCE_ALPHA32UI_EXT:
1538 return ctx->API == API_OPENGL_COMPAT &&
1539 ctx->Extensions.EXT_texture_integer &&
1540 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1541
1542 case GL_ALPHA8I_EXT:
1543 case GL_ALPHA8UI_EXT:
1544 case GL_ALPHA16I_EXT:
1545 case GL_ALPHA16UI_EXT:
1546 case GL_ALPHA32I_EXT:
1547 case GL_ALPHA32UI_EXT:
1548 return ctx->API == API_OPENGL_COMPAT &&
1549 ctx->Extensions.EXT_texture_integer &&
1550 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1551
1552 case GL_RGB10_A2UI:
1553 return (_mesa_is_desktop_gl(ctx) &&
1554 ctx->Extensions.ARB_texture_rgb10_a2ui)
1555 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1556
1557 case GL_RGB565:
1558 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1559 ? GL_RGB : 0;
1560 default:
1561 return 0;
1562 }
1563 }
1564
1565
1566 /**
1567 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1568 */
1569 static void
1570 invalidate_rb(GLuint key, void *data, void *userData)
1571 {
1572 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1573 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1574
1575 /* If this is a user-created FBO */
1576 if (_mesa_is_user_fbo(fb)) {
1577 GLuint i;
1578 for (i = 0; i < BUFFER_COUNT; i++) {
1579 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1580 if (att->Type == GL_RENDERBUFFER &&
1581 att->Renderbuffer == rb) {
1582 /* Mark fb status as indeterminate to force re-validation */
1583 fb->_Status = 0;
1584 return;
1585 }
1586 }
1587 }
1588 }
1589
1590
1591 /** sentinal value, see below */
1592 #define NO_SAMPLES 1000
1593
1594
1595 /**
1596 * Helper function used by _mesa_RenderbufferStorage() and
1597 * _mesa_RenderbufferStorageMultisample().
1598 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1599 */
1600 static void
1601 renderbuffer_storage(GLenum target, GLenum internalFormat,
1602 GLsizei width, GLsizei height, GLsizei samples)
1603 {
1604 const char *func = samples == NO_SAMPLES ?
1605 "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1606 struct gl_renderbuffer *rb;
1607 GLenum baseFormat;
1608 GLenum sample_count_error;
1609 GET_CURRENT_CONTEXT(ctx);
1610
1611 if (MESA_VERBOSE & VERBOSE_API) {
1612 if (samples == NO_SAMPLES)
1613 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1614 func,
1615 _mesa_lookup_enum_by_nr(target),
1616 _mesa_lookup_enum_by_nr(internalFormat),
1617 width, height);
1618 else
1619 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1620 func,
1621 _mesa_lookup_enum_by_nr(target),
1622 _mesa_lookup_enum_by_nr(internalFormat),
1623 width, height, samples);
1624 }
1625
1626 if (target != GL_RENDERBUFFER_EXT) {
1627 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1628 return;
1629 }
1630
1631 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1632 if (baseFormat == 0) {
1633 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1634 func, _mesa_lookup_enum_by_nr(internalFormat));
1635 return;
1636 }
1637
1638 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1639 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1640 return;
1641 }
1642
1643 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1644 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1645 return;
1646 }
1647
1648 if (samples == NO_SAMPLES) {
1649 /* NumSamples == 0 indicates non-multisampling */
1650 samples = 0;
1651 }
1652 else {
1653 /* check the sample count;
1654 * note: driver may choose to use more samples than what's requested
1655 */
1656 sample_count_error = _mesa_check_sample_count(ctx, target,
1657 internalFormat, samples);
1658 if (sample_count_error != GL_NO_ERROR) {
1659 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
1660 return;
1661 }
1662 }
1663
1664 rb = ctx->CurrentRenderbuffer;
1665 if (!rb) {
1666 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1667 return;
1668 }
1669
1670 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1671
1672 if (rb->InternalFormat == internalFormat &&
1673 rb->Width == (GLuint) width &&
1674 rb->Height == (GLuint) height &&
1675 rb->NumSamples == samples) {
1676 /* no change in allocation needed */
1677 return;
1678 }
1679
1680 /* These MUST get set by the AllocStorage func */
1681 rb->Format = MESA_FORMAT_NONE;
1682 rb->NumSamples = samples;
1683
1684 /* Now allocate the storage */
1685 ASSERT(rb->AllocStorage);
1686 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1687 /* No error - check/set fields now */
1688 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1689 assert(rb->Width == (GLuint) width);
1690 assert(rb->Height == (GLuint) height);
1691 rb->InternalFormat = internalFormat;
1692 rb->_BaseFormat = baseFormat;
1693 assert(rb->_BaseFormat != 0);
1694 }
1695 else {
1696 /* Probably ran out of memory - clear the fields */
1697 rb->Width = 0;
1698 rb->Height = 0;
1699 rb->Format = MESA_FORMAT_NONE;
1700 rb->InternalFormat = GL_NONE;
1701 rb->_BaseFormat = GL_NONE;
1702 rb->NumSamples = 0;
1703 }
1704
1705 /* Invalidate the framebuffers the renderbuffer is attached in. */
1706 if (rb->AttachedAnytime) {
1707 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1708 }
1709 }
1710
1711
1712 void GLAPIENTRY
1713 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1714 {
1715 struct gl_renderbuffer *rb;
1716 GET_CURRENT_CONTEXT(ctx);
1717
1718 if (!ctx->Extensions.OES_EGL_image) {
1719 _mesa_error(ctx, GL_INVALID_OPERATION,
1720 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1721 return;
1722 }
1723
1724 if (target != GL_RENDERBUFFER) {
1725 _mesa_error(ctx, GL_INVALID_ENUM,
1726 "EGLImageTargetRenderbufferStorageOES");
1727 return;
1728 }
1729
1730 rb = ctx->CurrentRenderbuffer;
1731 if (!rb) {
1732 _mesa_error(ctx, GL_INVALID_OPERATION,
1733 "EGLImageTargetRenderbufferStorageOES");
1734 return;
1735 }
1736
1737 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1738
1739 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1740 }
1741
1742
1743 /**
1744 * Helper function for _mesa_GetRenderbufferParameteriv() and
1745 * _mesa_GetFramebufferAttachmentParameteriv()
1746 * We have to be careful to respect the base format. For example, if a
1747 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1748 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1749 * we need to return zero.
1750 */
1751 static GLint
1752 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1753 {
1754 if (_mesa_base_format_has_channel(baseFormat, pname))
1755 return _mesa_get_format_bits(format, pname);
1756 else
1757 return 0;
1758 }
1759
1760
1761
1762 void GLAPIENTRY
1763 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1764 GLsizei width, GLsizei height)
1765 {
1766 /* GL_ARB_fbo says calling this function is equivalent to calling
1767 * glRenderbufferStorageMultisample() with samples=0. We pass in
1768 * a token value here just for error reporting purposes.
1769 */
1770 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1771 }
1772
1773
1774 void GLAPIENTRY
1775 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1776 GLenum internalFormat,
1777 GLsizei width, GLsizei height)
1778 {
1779 renderbuffer_storage(target, internalFormat, width, height, samples);
1780 }
1781
1782
1783 /**
1784 * OpenGL ES version of glRenderBufferStorage.
1785 */
1786 void GLAPIENTRY
1787 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1788 GLsizei width, GLsizei height)
1789 {
1790 switch (internalFormat) {
1791 case GL_RGB565:
1792 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1793 /* choose a closest format */
1794 internalFormat = GL_RGB5;
1795 break;
1796 default:
1797 break;
1798 }
1799
1800 renderbuffer_storage(target, internalFormat, width, height, 0);
1801 }
1802
1803
1804 void GLAPIENTRY
1805 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1806 {
1807 struct gl_renderbuffer *rb;
1808 GET_CURRENT_CONTEXT(ctx);
1809
1810 if (target != GL_RENDERBUFFER_EXT) {
1811 _mesa_error(ctx, GL_INVALID_ENUM,
1812 "glGetRenderbufferParameterivEXT(target)");
1813 return;
1814 }
1815
1816 rb = ctx->CurrentRenderbuffer;
1817 if (!rb) {
1818 _mesa_error(ctx, GL_INVALID_OPERATION,
1819 "glGetRenderbufferParameterivEXT");
1820 return;
1821 }
1822
1823 /* No need to flush here since we're just quering state which is
1824 * not effected by rendering.
1825 */
1826
1827 switch (pname) {
1828 case GL_RENDERBUFFER_WIDTH_EXT:
1829 *params = rb->Width;
1830 return;
1831 case GL_RENDERBUFFER_HEIGHT_EXT:
1832 *params = rb->Height;
1833 return;
1834 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1835 *params = rb->InternalFormat;
1836 return;
1837 case GL_RENDERBUFFER_RED_SIZE_EXT:
1838 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1839 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1840 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1841 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1842 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1843 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1844 break;
1845 case GL_RENDERBUFFER_SAMPLES:
1846 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1847 || _mesa_is_gles3(ctx)) {
1848 *params = rb->NumSamples;
1849 break;
1850 }
1851 /* fallthrough */
1852 default:
1853 _mesa_error(ctx, GL_INVALID_ENUM,
1854 "glGetRenderbufferParameterivEXT(target)");
1855 return;
1856 }
1857 }
1858
1859
1860 GLboolean GLAPIENTRY
1861 _mesa_IsFramebuffer(GLuint framebuffer)
1862 {
1863 GET_CURRENT_CONTEXT(ctx);
1864 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1865 if (framebuffer) {
1866 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1867 if (rb != NULL && rb != &DummyFramebuffer)
1868 return GL_TRUE;
1869 }
1870 return GL_FALSE;
1871 }
1872
1873
1874 /**
1875 * Check if any of the attachments of the given framebuffer are textures
1876 * (render to texture). Call ctx->Driver.RenderTexture() for such
1877 * attachments.
1878 */
1879 static void
1880 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1881 {
1882 GLuint i;
1883 ASSERT(ctx->Driver.RenderTexture);
1884
1885 if (_mesa_is_winsys_fbo(fb))
1886 return; /* can't render to texture with winsys framebuffers */
1887
1888 for (i = 0; i < BUFFER_COUNT; i++) {
1889 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1890 if (att->Texture && att->Renderbuffer->TexImage) {
1891 ctx->Driver.RenderTexture(ctx, fb, att);
1892 }
1893 }
1894 }
1895
1896
1897 /**
1898 * Examine all the framebuffer's attachments to see if any are textures.
1899 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1900 * notify the device driver that the texture image may have changed.
1901 */
1902 static void
1903 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1904 {
1905 if (_mesa_is_winsys_fbo(fb))
1906 return; /* can't render to texture with winsys framebuffers */
1907
1908 if (ctx->Driver.FinishRenderTexture) {
1909 GLuint i;
1910 for (i = 0; i < BUFFER_COUNT; i++) {
1911 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1912 struct gl_renderbuffer *rb = att->Renderbuffer;
1913 if (rb && rb->NeedsFinishRenderTexture) {
1914 ctx->Driver.FinishRenderTexture(ctx, rb);
1915 }
1916 }
1917 }
1918 }
1919
1920
1921 void GLAPIENTRY
1922 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1923 {
1924 struct gl_framebuffer *newDrawFb, *newReadFb;
1925 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1926 GLboolean bindReadBuf, bindDrawBuf;
1927 GET_CURRENT_CONTEXT(ctx);
1928
1929 #ifdef DEBUG
1930 if (ctx->Extensions.ARB_framebuffer_object) {
1931 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1932 }
1933 #endif
1934
1935 switch (target) {
1936 case GL_DRAW_FRAMEBUFFER_EXT:
1937 if (!ctx->Extensions.EXT_framebuffer_blit) {
1938 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1939 return;
1940 }
1941 bindDrawBuf = GL_TRUE;
1942 bindReadBuf = GL_FALSE;
1943 break;
1944 case GL_READ_FRAMEBUFFER_EXT:
1945 if (!ctx->Extensions.EXT_framebuffer_blit) {
1946 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1947 return;
1948 }
1949 bindDrawBuf = GL_FALSE;
1950 bindReadBuf = GL_TRUE;
1951 break;
1952 case GL_FRAMEBUFFER_EXT:
1953 bindDrawBuf = GL_TRUE;
1954 bindReadBuf = GL_TRUE;
1955 break;
1956 default:
1957 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1958 return;
1959 }
1960
1961 if (framebuffer) {
1962 /* Binding a user-created framebuffer object */
1963 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1964 if (newDrawFb == &DummyFramebuffer) {
1965 /* ID was reserved, but no real framebuffer object made yet */
1966 newDrawFb = NULL;
1967 }
1968 else if (!newDrawFb
1969 && _mesa_is_desktop_gl(ctx)
1970 && ctx->Extensions.ARB_framebuffer_object) {
1971 /* All FBO IDs must be Gen'd */
1972 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1973 return;
1974 }
1975
1976 if (!newDrawFb) {
1977 /* create new framebuffer object */
1978 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1979 if (!newDrawFb) {
1980 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1981 return;
1982 }
1983 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1984 }
1985 newReadFb = newDrawFb;
1986 }
1987 else {
1988 /* Binding the window system framebuffer (which was originally set
1989 * with MakeCurrent).
1990 */
1991 newDrawFb = ctx->WinSysDrawBuffer;
1992 newReadFb = ctx->WinSysReadBuffer;
1993 }
1994
1995 ASSERT(newDrawFb);
1996 ASSERT(newDrawFb != &DummyFramebuffer);
1997
1998 /* save pointers to current/old framebuffers */
1999 oldDrawFb = ctx->DrawBuffer;
2000 oldReadFb = ctx->ReadBuffer;
2001
2002 /* check if really changing bindings */
2003 if (oldDrawFb == newDrawFb)
2004 bindDrawBuf = GL_FALSE;
2005 if (oldReadFb == newReadFb)
2006 bindReadBuf = GL_FALSE;
2007
2008 /*
2009 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2010 *
2011 * We also check if we're beginning and/or ending render-to-texture.
2012 * When a framebuffer with texture attachments is unbound, call
2013 * ctx->Driver.FinishRenderTexture().
2014 * When a framebuffer with texture attachments is bound, call
2015 * ctx->Driver.RenderTexture().
2016 *
2017 * Note that if the ReadBuffer has texture attachments we don't consider
2018 * that a render-to-texture case.
2019 */
2020 if (bindReadBuf) {
2021 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2022
2023 /* check if old readbuffer was render-to-texture */
2024 check_end_texture_render(ctx, oldReadFb);
2025
2026 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2027 }
2028
2029 if (bindDrawBuf) {
2030 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2031
2032 /* check if old framebuffer had any texture attachments */
2033 if (oldDrawFb)
2034 check_end_texture_render(ctx, oldDrawFb);
2035
2036 /* check if newly bound framebuffer has any texture attachments */
2037 check_begin_texture_render(ctx, newDrawFb);
2038
2039 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2040 }
2041
2042 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2043 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
2044 }
2045 }
2046
2047 void GLAPIENTRY
2048 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2049 {
2050 _mesa_BindFramebuffer(target, framebuffer);
2051 }
2052
2053
2054
2055 void GLAPIENTRY
2056 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2057 {
2058 GLint i;
2059 GET_CURRENT_CONTEXT(ctx);
2060
2061 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2062
2063 for (i = 0; i < n; i++) {
2064 if (framebuffers[i] > 0) {
2065 struct gl_framebuffer *fb;
2066 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2067 if (fb) {
2068 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2069
2070 /* check if deleting currently bound framebuffer object */
2071 if (ctx->Extensions.EXT_framebuffer_blit) {
2072 /* separate draw/read binding points */
2073 if (fb == ctx->DrawBuffer) {
2074 /* bind default */
2075 ASSERT(fb->RefCount >= 2);
2076 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
2077 }
2078 if (fb == ctx->ReadBuffer) {
2079 /* bind default */
2080 ASSERT(fb->RefCount >= 2);
2081 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
2082 }
2083 }
2084 else {
2085 /* only one binding point for read/draw buffers */
2086 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
2087 /* bind default */
2088 ASSERT(fb->RefCount >= 2);
2089 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
2090 }
2091 }
2092
2093 /* remove from hash table immediately, to free the ID */
2094 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2095
2096 if (fb != &DummyFramebuffer) {
2097 /* But the object will not be freed until it's no longer
2098 * bound in any context.
2099 */
2100 _mesa_reference_framebuffer(&fb, NULL);
2101 }
2102 }
2103 }
2104 }
2105 }
2106
2107
2108 void GLAPIENTRY
2109 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2110 {
2111 GET_CURRENT_CONTEXT(ctx);
2112 GLuint first;
2113 GLint i;
2114
2115 if (n < 0) {
2116 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
2117 return;
2118 }
2119
2120 if (!framebuffers)
2121 return;
2122
2123 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2124
2125 for (i = 0; i < n; i++) {
2126 GLuint name = first + i;
2127 framebuffers[i] = name;
2128 /* insert dummy placeholder into hash table */
2129 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
2130 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
2131 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
2132 }
2133 }
2134
2135
2136
2137 GLenum GLAPIENTRY
2138 _mesa_CheckFramebufferStatus(GLenum target)
2139 {
2140 struct gl_framebuffer *buffer;
2141 GET_CURRENT_CONTEXT(ctx);
2142
2143 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2144
2145 if (MESA_VERBOSE & VERBOSE_API)
2146 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2147 _mesa_lookup_enum_by_nr(target));
2148
2149 buffer = get_framebuffer_target(ctx, target);
2150 if (!buffer) {
2151 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
2152 return 0;
2153 }
2154
2155 if (_mesa_is_winsys_fbo(buffer)) {
2156 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2157 if (buffer != &IncompleteFramebuffer) {
2158 return GL_FRAMEBUFFER_COMPLETE_EXT;
2159 } else {
2160 return GL_FRAMEBUFFER_UNDEFINED;
2161 }
2162 }
2163
2164 /* No need to flush here */
2165
2166 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2167 _mesa_test_framebuffer_completeness(ctx, buffer);
2168 }
2169
2170 return buffer->_Status;
2171 }
2172
2173
2174 /**
2175 * Replicate the src attachment point. Used by framebuffer_texture() when
2176 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2177 * GL_STENCIL_ATTACHMENT.
2178 */
2179 static void
2180 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2181 gl_buffer_index dst,
2182 gl_buffer_index src)
2183 {
2184 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2185 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2186
2187 assert(src_att->Texture != NULL);
2188 assert(src_att->Renderbuffer != NULL);
2189
2190 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2191 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2192 dst_att->Type = src_att->Type;
2193 dst_att->Complete = src_att->Complete;
2194 dst_att->TextureLevel = src_att->TextureLevel;
2195 dst_att->Zoffset = src_att->Zoffset;
2196 }
2197
2198
2199 /**
2200 * Common code called by glFramebufferTexture1D/2D/3DEXT() and
2201 * glFramebufferTextureLayerEXT().
2202 * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2203 * get textarget=0 in that case.
2204 */
2205 static void
2206 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target,
2207 GLenum attachment, GLenum textarget, GLuint texture,
2208 GLint level, GLint zoffset, GLboolean layered)
2209 {
2210 struct gl_renderbuffer_attachment *att;
2211 struct gl_texture_object *texObj = NULL;
2212 struct gl_framebuffer *fb;
2213 GLenum maxLevelsTarget;
2214
2215 fb = get_framebuffer_target(ctx, target);
2216 if (!fb) {
2217 _mesa_error(ctx, GL_INVALID_ENUM,
2218 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2219 return;
2220 }
2221
2222 /* check framebuffer binding */
2223 if (_mesa_is_winsys_fbo(fb)) {
2224 _mesa_error(ctx, GL_INVALID_OPERATION,
2225 "glFramebufferTexture%sEXT", caller);
2226 return;
2227 }
2228
2229 /* The textarget, level, and zoffset parameters are only validated if
2230 * texture is non-zero.
2231 */
2232 if (texture) {
2233 GLboolean err = GL_TRUE;
2234
2235 texObj = _mesa_lookup_texture(ctx, texture);
2236 if (texObj != NULL) {
2237 if (textarget == 0) {
2238 /* If textarget == 0 it means we're being called by
2239 * glFramebufferTextureLayer() and textarget is not used.
2240 * The only legal texture types for that function are 3D and
2241 * 1D/2D arrays textures.
2242 */
2243 err = (texObj->Target != GL_TEXTURE_3D) &&
2244 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2245 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2246 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY) &&
2247 (texObj->Target != GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
2248 }
2249 else {
2250 /* Make sure textarget is consistent with the texture's type */
2251 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2252 ? !_mesa_is_cube_face(textarget)
2253 : (texObj->Target != textarget);
2254 }
2255 }
2256 else {
2257 /* can't render to a non-existant texture */
2258 _mesa_error(ctx, GL_INVALID_OPERATION,
2259 "glFramebufferTexture%sEXT(non existant texture)",
2260 caller);
2261 return;
2262 }
2263
2264 if (err) {
2265 _mesa_error(ctx, GL_INVALID_OPERATION,
2266 "glFramebufferTexture%sEXT(texture target mismatch)",
2267 caller);
2268 return;
2269 }
2270
2271 if (texObj->Target == GL_TEXTURE_3D) {
2272 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2273 if (zoffset < 0 || zoffset >= maxSize) {
2274 _mesa_error(ctx, GL_INVALID_VALUE,
2275 "glFramebufferTexture%sEXT(zoffset)", caller);
2276 return;
2277 }
2278 }
2279 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2280 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2281 (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
2282 (texObj->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
2283 if (zoffset < 0 ||
2284 zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2285 _mesa_error(ctx, GL_INVALID_VALUE,
2286 "glFramebufferTexture%sEXT(layer)", caller);
2287 return;
2288 }
2289 }
2290
2291 maxLevelsTarget = textarget ? textarget : texObj->Target;
2292 if ((level < 0) ||
2293 (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2294 _mesa_error(ctx, GL_INVALID_VALUE,
2295 "glFramebufferTexture%sEXT(level)", caller);
2296 return;
2297 }
2298 }
2299
2300 att = _mesa_get_attachment(ctx, fb, attachment);
2301 if (att == NULL) {
2302 _mesa_error(ctx, GL_INVALID_ENUM,
2303 "glFramebufferTexture%sEXT(attachment)", caller);
2304 return;
2305 }
2306
2307 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2308
2309 _glthread_LOCK_MUTEX(fb->Mutex);
2310 if (texObj) {
2311 if (attachment == GL_DEPTH_ATTACHMENT &&
2312 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2313 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2314 _mesa_tex_target_to_face(textarget) ==
2315 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2316 zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2317 /* The texture object is already attached to the stencil attachment
2318 * point. Don't create a new renderbuffer; just reuse the stencil
2319 * attachment's. This is required to prevent a GL error in
2320 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2321 */
2322 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2323 BUFFER_STENCIL);
2324 } else if (attachment == GL_STENCIL_ATTACHMENT &&
2325 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2326 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2327 _mesa_tex_target_to_face(textarget) ==
2328 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2329 zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2330 /* As above, but with depth and stencil transposed. */
2331 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2332 BUFFER_DEPTH);
2333 } else {
2334 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2335 level, zoffset, layered);
2336 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2337 /* Above we created a new renderbuffer and attached it to the
2338 * depth attachment point. Now attach it to the stencil attachment
2339 * point too.
2340 */
2341 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2342 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2343 BUFFER_DEPTH);
2344 }
2345 }
2346
2347 /* Set the render-to-texture flag. We'll check this flag in
2348 * glTexImage() and friends to determine if we need to revalidate
2349 * any FBOs that might be rendering into this texture.
2350 * This flag never gets cleared since it's non-trivial to determine
2351 * when all FBOs might be done rendering to this texture. That's OK
2352 * though since it's uncommon to render to a texture then repeatedly
2353 * call glTexImage() to change images in the texture.
2354 */
2355 texObj->_RenderToTexture = GL_TRUE;
2356 }
2357 else {
2358 _mesa_remove_attachment(ctx, att);
2359 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2360 assert(att == &fb->Attachment[BUFFER_DEPTH]);
2361 _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2362 }
2363 }
2364
2365 invalidate_framebuffer(fb);
2366
2367 _glthread_UNLOCK_MUTEX(fb->Mutex);
2368 }
2369
2370
2371
2372 void GLAPIENTRY
2373 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2374 GLenum textarget, GLuint texture, GLint level)
2375 {
2376 GET_CURRENT_CONTEXT(ctx);
2377
2378 if (texture != 0) {
2379 GLboolean error;
2380
2381 switch (textarget) {
2382 case GL_TEXTURE_1D:
2383 error = GL_FALSE;
2384 break;
2385 case GL_TEXTURE_1D_ARRAY:
2386 error = !ctx->Extensions.EXT_texture_array;
2387 break;
2388 default:
2389 error = GL_TRUE;
2390 }
2391
2392 if (error) {
2393 _mesa_error(ctx, GL_INVALID_OPERATION,
2394 "glFramebufferTexture1DEXT(textarget=%s)",
2395 _mesa_lookup_enum_by_nr(textarget));
2396 return;
2397 }
2398 }
2399
2400 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2401 level, 0, GL_FALSE);
2402 }
2403
2404
2405 void GLAPIENTRY
2406 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2407 GLenum textarget, GLuint texture, GLint level)
2408 {
2409 GET_CURRENT_CONTEXT(ctx);
2410
2411 if (texture != 0) {
2412 GLboolean error;
2413
2414 switch (textarget) {
2415 case GL_TEXTURE_2D:
2416 error = GL_FALSE;
2417 break;
2418 case GL_TEXTURE_RECTANGLE:
2419 error = _mesa_is_gles(ctx)
2420 || !ctx->Extensions.NV_texture_rectangle;
2421 break;
2422 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2423 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2424 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2425 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2426 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2427 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2428 error = !ctx->Extensions.ARB_texture_cube_map;
2429 break;
2430 case GL_TEXTURE_2D_ARRAY:
2431 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2432 || !ctx->Extensions.EXT_texture_array;
2433 break;
2434 case GL_TEXTURE_2D_MULTISAMPLE:
2435 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2436 error = _mesa_is_gles(ctx)
2437 || !ctx->Extensions.ARB_texture_multisample;
2438 break;
2439 default:
2440 error = GL_TRUE;
2441 }
2442
2443 if (error) {
2444 _mesa_error(ctx, GL_INVALID_OPERATION,
2445 "glFramebufferTexture2DEXT(textarget=%s)",
2446 _mesa_lookup_enum_by_nr(textarget));
2447 return;
2448 }
2449 }
2450
2451 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2452 level, 0, GL_FALSE);
2453 }
2454
2455
2456 void GLAPIENTRY
2457 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2458 GLenum textarget, GLuint texture,
2459 GLint level, GLint zoffset)
2460 {
2461 GET_CURRENT_CONTEXT(ctx);
2462
2463 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2464 _mesa_error(ctx, GL_INVALID_OPERATION,
2465 "glFramebufferTexture3DEXT(textarget)");
2466 return;
2467 }
2468
2469 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2470 level, zoffset, GL_FALSE);
2471 }
2472
2473
2474 void GLAPIENTRY
2475 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2476 GLuint texture, GLint level, GLint layer)
2477 {
2478 GET_CURRENT_CONTEXT(ctx);
2479
2480 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2481 level, layer, GL_FALSE);
2482 }
2483
2484
2485 void GLAPIENTRY
2486 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
2487 GLuint texture, GLint level)
2488 {
2489 GET_CURRENT_CONTEXT(ctx);
2490
2491 if (_mesa_has_geometry_shaders(ctx)) {
2492 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2493 level, 0, GL_TRUE);
2494 } else {
2495 _mesa_error(ctx, GL_INVALID_OPERATION,
2496 "unsupported function (glFramebufferTexture) called");
2497 }
2498 }
2499
2500
2501 void GLAPIENTRY
2502 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2503 GLenum renderbufferTarget,
2504 GLuint renderbuffer)
2505 {
2506 struct gl_renderbuffer_attachment *att;
2507 struct gl_framebuffer *fb;
2508 struct gl_renderbuffer *rb;
2509 GET_CURRENT_CONTEXT(ctx);
2510
2511 fb = get_framebuffer_target(ctx, target);
2512 if (!fb) {
2513 _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2514 return;
2515 }
2516
2517 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2518 _mesa_error(ctx, GL_INVALID_ENUM,
2519 "glFramebufferRenderbufferEXT(renderbufferTarget)");
2520 return;
2521 }
2522
2523 if (_mesa_is_winsys_fbo(fb)) {
2524 /* Can't attach new renderbuffers to a window system framebuffer */
2525 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2526 return;
2527 }
2528
2529 att = _mesa_get_attachment(ctx, fb, attachment);
2530 if (att == NULL) {
2531 _mesa_error(ctx, GL_INVALID_ENUM,
2532 "glFramebufferRenderbufferEXT(invalid attachment %s)",
2533 _mesa_lookup_enum_by_nr(attachment));
2534 return;
2535 }
2536
2537 if (renderbuffer) {
2538 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2539 if (!rb) {
2540 _mesa_error(ctx, GL_INVALID_OPERATION,
2541 "glFramebufferRenderbufferEXT(non-existant"
2542 " renderbuffer %u)", renderbuffer);
2543 return;
2544 }
2545 else if (rb == &DummyRenderbuffer) {
2546 /* This is what NVIDIA does */
2547 _mesa_error(ctx, GL_INVALID_VALUE,
2548 "glFramebufferRenderbufferEXT(renderbuffer %u)",
2549 renderbuffer);
2550 return;
2551 }
2552 }
2553 else {
2554 /* remove renderbuffer attachment */
2555 rb = NULL;
2556 }
2557
2558 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2559 rb && rb->Format != MESA_FORMAT_NONE) {
2560 /* make sure the renderbuffer is a depth/stencil format */
2561 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2562 if (baseFormat != GL_DEPTH_STENCIL) {
2563 _mesa_error(ctx, GL_INVALID_OPERATION,
2564 "glFramebufferRenderbufferEXT(renderbuffer"
2565 " is not DEPTH_STENCIL format)");
2566 return;
2567 }
2568 }
2569
2570
2571 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2572
2573 assert(ctx->Driver.FramebufferRenderbuffer);
2574 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2575
2576 /* Some subsequent GL commands may depend on the framebuffer's visual
2577 * after the binding is updated. Update visual info now.
2578 */
2579 _mesa_update_framebuffer_visual(ctx, fb);
2580 }
2581
2582
2583 void GLAPIENTRY
2584 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2585 GLenum pname, GLint *params)
2586 {
2587 const struct gl_renderbuffer_attachment *att;
2588 struct gl_framebuffer *buffer;
2589 GLenum err;
2590 GET_CURRENT_CONTEXT(ctx);
2591
2592 /* The error differs in GL and GLES. */
2593 err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2594
2595 buffer = get_framebuffer_target(ctx, target);
2596 if (!buffer) {
2597 _mesa_error(ctx, GL_INVALID_ENUM,
2598 "glGetFramebufferAttachmentParameterivEXT(target)");
2599 return;
2600 }
2601
2602 if (_mesa_is_winsys_fbo(buffer)) {
2603 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2604 * says:
2605 *
2606 * "If the framebuffer currently bound to target is zero, then
2607 * INVALID_OPERATION is generated."
2608 *
2609 * The EXT_framebuffer_object spec has the same wording, and the
2610 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2611 * spec.
2612 */
2613 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2614 && !_mesa_is_gles3(ctx)) {
2615 _mesa_error(ctx, GL_INVALID_OPERATION,
2616 "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2617 return;
2618 }
2619
2620 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2621 attachment != GL_DEPTH && attachment != GL_STENCIL) {
2622 _mesa_error(ctx, GL_INVALID_OPERATION,
2623 "glGetFramebufferAttachmentParameteriv(attachment)");
2624 return;
2625 }
2626 /* the default / window-system FBO */
2627 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2628 }
2629 else {
2630 /* user-created framebuffer FBO */
2631 att = _mesa_get_attachment(ctx, buffer, attachment);
2632 }
2633
2634 if (att == NULL) {
2635 _mesa_error(ctx, GL_INVALID_ENUM,
2636 "glGetFramebufferAttachmentParameterivEXT(attachment)");
2637 return;
2638 }
2639
2640 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2641 /* the depth and stencil attachments must point to the same buffer */
2642 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2643 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2644 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2645 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2646 _mesa_error(ctx, GL_INVALID_OPERATION,
2647 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2648 " attachments differ)");
2649 return;
2650 }
2651 }
2652
2653 /* No need to flush here */
2654
2655 switch (pname) {
2656 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2657 *params = _mesa_is_winsys_fbo(buffer)
2658 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2659 return;
2660 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2661 if (att->Type == GL_RENDERBUFFER_EXT) {
2662 *params = att->Renderbuffer->Name;
2663 }
2664 else if (att->Type == GL_TEXTURE) {
2665 *params = att->Texture->Name;
2666 }
2667 else {
2668 assert(att->Type == GL_NONE);
2669 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2670 *params = 0;
2671 } else {
2672 goto invalid_pname_enum;
2673 }
2674 }
2675 return;
2676 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2677 if (att->Type == GL_TEXTURE) {
2678 *params = att->TextureLevel;
2679 }
2680 else if (att->Type == GL_NONE) {
2681 _mesa_error(ctx, err,
2682 "glGetFramebufferAttachmentParameterivEXT(pname)");
2683 }
2684 else {
2685 goto invalid_pname_enum;
2686 }
2687 return;
2688 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2689 if (att->Type == GL_TEXTURE) {
2690 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2691 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2692 }
2693 else {
2694 *params = 0;
2695 }
2696 }
2697 else if (att->Type == GL_NONE) {
2698 _mesa_error(ctx, err,
2699 "glGetFramebufferAttachmentParameterivEXT(pname)");
2700 }
2701 else {
2702 goto invalid_pname_enum;
2703 }
2704 return;
2705 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2706 if (ctx->API == API_OPENGLES) {
2707 goto invalid_pname_enum;
2708 } else if (att->Type == GL_NONE) {
2709 _mesa_error(ctx, err,
2710 "glGetFramebufferAttachmentParameterivEXT(pname)");
2711 } else if (att->Type == GL_TEXTURE) {
2712 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2713 *params = att->Zoffset;
2714 }
2715 else {
2716 *params = 0;
2717 }
2718 }
2719 else {
2720 goto invalid_pname_enum;
2721 }
2722 return;
2723 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2724 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2725 && !_mesa_is_gles3(ctx)) {
2726 goto invalid_pname_enum;
2727 }
2728 else if (att->Type == GL_NONE) {
2729 _mesa_error(ctx, err,
2730 "glGetFramebufferAttachmentParameterivEXT(pname)");
2731 }
2732 else {
2733 if (ctx->Extensions.EXT_framebuffer_sRGB) {
2734 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2735 }
2736 else {
2737 /* According to ARB_framebuffer_sRGB, we should return LINEAR
2738 * if the sRGB conversion is unsupported. */
2739 *params = GL_LINEAR;
2740 }
2741 }
2742 return;
2743 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2744 if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2745 && ctx->API != API_OPENGL_CORE
2746 && !_mesa_is_gles3(ctx)) {
2747 goto invalid_pname_enum;
2748 }
2749 else if (att->Type == GL_NONE) {
2750 _mesa_error(ctx, err,
2751 "glGetFramebufferAttachmentParameterivEXT(pname)");
2752 }
2753 else {
2754 gl_format format = att->Renderbuffer->Format;
2755
2756 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
2757 * 3.0.1 spec says:
2758 *
2759 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
2760 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
2761 * generate an INVALID_OPERATION error.
2762 */
2763 if (_mesa_is_gles3(ctx) && attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2764 _mesa_error(ctx, GL_INVALID_OPERATION,
2765 "glGetFramebufferAttachmentParameteriv(cannot query "
2766 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
2767 "GL_DEPTH_STENCIL_ATTACHMENT");
2768 return;
2769 }
2770
2771 if (format == MESA_FORMAT_S8) {
2772 /* special cases */
2773 *params = GL_INDEX;
2774 }
2775 else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2776 /* depends on the attachment parameter */
2777 if (attachment == GL_STENCIL_ATTACHMENT) {
2778 *params = GL_INDEX;
2779 }
2780 else {
2781 *params = GL_FLOAT;
2782 }
2783 }
2784 else {
2785 *params = _mesa_get_format_datatype(format);
2786 }
2787 }
2788 return;
2789 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2790 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2791 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2792 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2793 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2794 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2795 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2796 && !_mesa_is_gles3(ctx)) {
2797 goto invalid_pname_enum;
2798 }
2799 else if (att->Type == GL_NONE) {
2800 _mesa_error(ctx, err,
2801 "glGetFramebufferAttachmentParameterivEXT(pname)");
2802 }
2803 else if (att->Texture) {
2804 const struct gl_texture_image *texImage =
2805 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2806 att->TextureLevel);
2807 if (texImage) {
2808 *params = get_component_bits(pname, texImage->_BaseFormat,
2809 texImage->TexFormat);
2810 }
2811 else {
2812 *params = 0;
2813 }
2814 }
2815 else if (att->Renderbuffer) {
2816 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2817 att->Renderbuffer->Format);
2818 }
2819 else {
2820 _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2821 " invalid FBO attachment structure");
2822 }
2823 return;
2824 default:
2825 goto invalid_pname_enum;
2826 }
2827
2828 return;
2829
2830 invalid_pname_enum:
2831 _mesa_error(ctx, GL_INVALID_ENUM,
2832 "glGetFramebufferAttachmentParameteriv(pname)");
2833 return;
2834 }
2835
2836
2837 void GLAPIENTRY
2838 _mesa_GenerateMipmap(GLenum target)
2839 {
2840 struct gl_texture_image *srcImage;
2841 struct gl_texture_object *texObj;
2842 GLboolean error;
2843
2844 GET_CURRENT_CONTEXT(ctx);
2845
2846 FLUSH_VERTICES(ctx, 0);
2847
2848 switch (target) {
2849 case GL_TEXTURE_1D:
2850 error = _mesa_is_gles(ctx);
2851 break;
2852 case GL_TEXTURE_2D:
2853 error = GL_FALSE;
2854 break;
2855 case GL_TEXTURE_3D:
2856 error = ctx->API == API_OPENGLES;
2857 break;
2858 case GL_TEXTURE_CUBE_MAP:
2859 error = !ctx->Extensions.ARB_texture_cube_map;
2860 break;
2861 case GL_TEXTURE_1D_ARRAY:
2862 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2863 break;
2864 case GL_TEXTURE_2D_ARRAY:
2865 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2866 || !ctx->Extensions.EXT_texture_array;
2867 break;
2868 default:
2869 error = GL_TRUE;
2870 }
2871
2872 if (error) {
2873 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2874 _mesa_lookup_enum_by_nr(target));
2875 return;
2876 }
2877
2878 texObj = _mesa_get_current_tex_object(ctx, target);
2879
2880 if (texObj->BaseLevel >= texObj->MaxLevel) {
2881 /* nothing to do */
2882 return;
2883 }
2884
2885 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2886 !_mesa_cube_complete(texObj)) {
2887 _mesa_error(ctx, GL_INVALID_OPERATION,
2888 "glGenerateMipmap(incomplete cube map)");
2889 return;
2890 }
2891
2892 _mesa_lock_texture(ctx, texObj);
2893
2894 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2895 if (!srcImage) {
2896 _mesa_unlock_texture(ctx, texObj);
2897 _mesa_error(ctx, GL_INVALID_OPERATION,
2898 "glGenerateMipmap(zero size base image)");
2899 return;
2900 }
2901
2902 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2903 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2904 _mesa_is_stencil_format(srcImage->InternalFormat)) {
2905 _mesa_unlock_texture(ctx, texObj);
2906 _mesa_error(ctx, GL_INVALID_OPERATION,
2907 "glGenerateMipmap(invalid internal format)");
2908 return;
2909 }
2910
2911 if (target == GL_TEXTURE_CUBE_MAP) {
2912 GLuint face;
2913 for (face = 0; face < 6; face++)
2914 ctx->Driver.GenerateMipmap(ctx,
2915 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2916 texObj);
2917 }
2918 else {
2919 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2920 }
2921 _mesa_unlock_texture(ctx, texObj);
2922 }
2923
2924
2925 static const struct gl_renderbuffer_attachment *
2926 find_attachment(const struct gl_framebuffer *fb,
2927 const struct gl_renderbuffer *rb)
2928 {
2929 GLuint i;
2930 for (i = 0; i < Elements(fb->Attachment); i++) {
2931 if (fb->Attachment[i].Renderbuffer == rb)
2932 return &fb->Attachment[i];
2933 }
2934 return NULL;
2935 }
2936
2937
2938 /**
2939 * Helper function for checking if the datatypes of color buffers are
2940 * compatible for glBlitFramebuffer. From the 3.1 spec, page 198:
2941 *
2942 * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2943 * and any of the following conditions hold:
2944 * - The read buffer contains fixed-point or floating-point values and any
2945 * draw buffer contains neither fixed-point nor floating-point values.
2946 * - The read buffer contains unsigned integer values and any draw buffer
2947 * does not contain unsigned integer values.
2948 * - The read buffer contains signed integer values and any draw buffer
2949 * does not contain signed integer values."
2950 */
2951 static GLboolean
2952 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2953 {
2954 GLenum srcType = _mesa_get_format_datatype(srcFormat);
2955 GLenum dstType = _mesa_get_format_datatype(dstFormat);
2956
2957 if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2958 assert(srcType == GL_UNSIGNED_NORMALIZED ||
2959 srcType == GL_SIGNED_NORMALIZED ||
2960 srcType == GL_FLOAT);
2961 /* Boil any of those types down to GL_FLOAT */
2962 srcType = GL_FLOAT;
2963 }
2964
2965 if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2966 assert(dstType == GL_UNSIGNED_NORMALIZED ||
2967 dstType == GL_SIGNED_NORMALIZED ||
2968 dstType == GL_FLOAT);
2969 /* Boil any of those types down to GL_FLOAT */
2970 dstType = GL_FLOAT;
2971 }
2972
2973 return srcType == dstType;
2974 }
2975
2976
2977 static GLboolean
2978 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2979 const struct gl_renderbuffer *drawRb)
2980 {
2981 GLenum readFormat, drawFormat;
2982
2983 /* The simple case where we know the backing Mesa formats are the same.
2984 */
2985 if (_mesa_get_srgb_format_linear(readRb->Format) ==
2986 _mesa_get_srgb_format_linear(drawRb->Format)) {
2987 return GL_TRUE;
2988 }
2989
2990 /* The Mesa formats are different, so we must check whether the internal
2991 * formats are compatible.
2992 *
2993 * Under some circumstances, the user may request e.g. two GL_RGBA8
2994 * textures and get two entirely different Mesa formats like RGBA8888 and
2995 * ARGB8888. Drivers behaving like that should be able to cope with
2996 * non-matching formats by themselves, because it's not the user's fault.
2997 *
2998 * Blits between linear and sRGB formats are also allowed.
2999 */
3000 readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
3001 drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
3002 readFormat = _mesa_get_linear_internalformat(readFormat);
3003 drawFormat = _mesa_get_linear_internalformat(drawFormat);
3004
3005 if (readFormat == drawFormat) {
3006 return GL_TRUE;
3007 }
3008
3009 return GL_FALSE;
3010 }
3011
3012 static GLboolean
3013 is_valid_blit_filter(const struct gl_context *ctx, GLenum filter)
3014 {
3015 switch (filter) {
3016 case GL_NEAREST:
3017 case GL_LINEAR:
3018 return true;
3019 case GL_SCALED_RESOLVE_FASTEST_EXT:
3020 case GL_SCALED_RESOLVE_NICEST_EXT:
3021 return ctx->Extensions.EXT_framebuffer_multisample_blit_scaled;
3022 default:
3023 return false;
3024 }
3025 }
3026
3027 /**
3028 * Blit rectangular region, optionally from one framebuffer to another.
3029 *
3030 * Note, if the src buffer is multisampled and the dest is not, this is
3031 * when the samples must be resolved to a single color.
3032 */
3033 void GLAPIENTRY
3034 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3035 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3036 GLbitfield mask, GLenum filter)
3037 {
3038 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
3039 GL_DEPTH_BUFFER_BIT |
3040 GL_STENCIL_BUFFER_BIT);
3041 const struct gl_framebuffer *readFb, *drawFb;
3042 GET_CURRENT_CONTEXT(ctx);
3043
3044 FLUSH_VERTICES(ctx, 0);
3045
3046 if (MESA_VERBOSE & VERBOSE_API)
3047 _mesa_debug(ctx,
3048 "glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d, 0x%x, %s)\n",
3049 srcX0, srcY0, srcX1, srcY1,
3050 dstX0, dstY0, dstX1, dstY1,
3051 mask, _mesa_lookup_enum_by_nr(filter));
3052
3053 if (ctx->NewState) {
3054 _mesa_update_state(ctx);
3055 }
3056
3057 readFb = ctx->ReadBuffer;
3058 drawFb = ctx->DrawBuffer;
3059
3060 if (!readFb || !drawFb) {
3061 /* This will normally never happen but someday we may want to
3062 * support MakeCurrent() with no drawables.
3063 */
3064 return;
3065 }
3066
3067 /* check for complete framebuffers */
3068 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
3069 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
3070 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
3071 "glBlitFramebufferEXT(incomplete draw/read buffers)");
3072 return;
3073 }
3074
3075 if (!is_valid_blit_filter(ctx, filter)) {
3076 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(%s)",
3077 _mesa_lookup_enum_by_nr(filter));
3078 return;
3079 }
3080
3081 if ((filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
3082 filter == GL_SCALED_RESOLVE_NICEST_EXT) &&
3083 (readFb->Visual.samples == 0 || drawFb->Visual.samples > 0)) {
3084 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT(%s)",
3085 _mesa_lookup_enum_by_nr(filter));
3086 return;
3087 }
3088
3089 if (mask & ~legalMaskBits) {
3090 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
3091 return;
3092 }
3093
3094 /* depth/stencil must be blitted with nearest filtering */
3095 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3096 && filter != GL_NEAREST) {
3097 _mesa_error(ctx, GL_INVALID_OPERATION,
3098 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
3099 return;
3100 }
3101
3102 /* get color read/draw renderbuffers */
3103 if (mask & GL_COLOR_BUFFER_BIT) {
3104 const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
3105 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3106 const struct gl_renderbuffer *colorDrawRb = NULL;
3107 GLuint i;
3108
3109 /* From the EXT_framebuffer_object spec:
3110 *
3111 * "If a buffer is specified in <mask> and does not exist in both
3112 * the read and draw framebuffers, the corresponding bit is silently
3113 * ignored."
3114 */
3115 if (!colorReadRb || numColorDrawBuffers == 0) {
3116 mask &= ~GL_COLOR_BUFFER_BIT;
3117 }
3118 else {
3119 for (i = 0; i < numColorDrawBuffers; i++) {
3120 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3121 if (!colorDrawRb)
3122 continue;
3123
3124 /* Page 193 (page 205 of the PDF) in section 4.3.2 of the OpenGL
3125 * ES 3.0.1 spec says:
3126 *
3127 * "If the source and destination buffers are identical, an
3128 * INVALID_OPERATION error is generated. Different mipmap
3129 * levels of a texture, different layers of a three-
3130 * dimensional texture or two-dimensional array texture, and
3131 * different faces of a cube map texture do not constitute
3132 * identical buffers."
3133 */
3134 if (_mesa_is_gles3(ctx) && (colorDrawRb == colorReadRb)) {
3135 _mesa_error(ctx, GL_INVALID_OPERATION,
3136 "glBlitFramebuffer(source and destination color "
3137 "buffer cannot be the same)");
3138 return;
3139 }
3140
3141 if (!compatible_color_datatypes(colorReadRb->Format,
3142 colorDrawRb->Format)) {
3143 _mesa_error(ctx, GL_INVALID_OPERATION,
3144 "glBlitFramebufferEXT(color buffer datatypes mismatch)");
3145 return;
3146 }
3147 /* extra checks for multisample copies... */
3148 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
3149 /* color formats must match */
3150 if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
3151 _mesa_error(ctx, GL_INVALID_OPERATION,
3152 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
3153 return;
3154 }
3155 }
3156 }
3157 if (filter != GL_NEAREST) {
3158 /* From EXT_framebuffer_multisample_blit_scaled specification:
3159 * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
3160 * if filter is not NEAREST and read buffer contains integer data."
3161 */
3162 GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
3163 if (type == GL_INT || type == GL_UNSIGNED_INT) {
3164 _mesa_error(ctx, GL_INVALID_OPERATION,
3165 "glBlitFramebufferEXT(integer color type)");
3166 return;
3167 }
3168 }
3169 }
3170 }
3171
3172 if (mask & GL_STENCIL_BUFFER_BIT) {
3173 struct gl_renderbuffer *readRb =
3174 readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
3175 struct gl_renderbuffer *drawRb =
3176 drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
3177
3178 /* From the EXT_framebuffer_object spec:
3179 *
3180 * "If a buffer is specified in <mask> and does not exist in both
3181 * the read and draw framebuffers, the corresponding bit is silently
3182 * ignored."
3183 */
3184 if ((readRb == NULL) || (drawRb == NULL)) {
3185 mask &= ~GL_STENCIL_BUFFER_BIT;
3186 }
3187 else {
3188 int read_z_bits, draw_z_bits;
3189
3190 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
3191 _mesa_error(ctx, GL_INVALID_OPERATION,
3192 "glBlitFramebuffer(source and destination stencil "
3193 "buffer cannot be the same)");
3194 return;
3195 }
3196
3197 if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
3198 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
3199 /* There is no need to check the stencil datatype here, because
3200 * there is only one: GL_UNSIGNED_INT.
3201 */
3202 _mesa_error(ctx, GL_INVALID_OPERATION,
3203 "glBlitFramebuffer(stencil attachment format mismatch)");
3204 return;
3205 }
3206
3207 read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
3208 draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
3209
3210 /* If both buffers also have depth data, the depth formats must match
3211 * as well. If one doesn't have depth, it's not blitted, so we should
3212 * ignore the depth format check.
3213 */
3214 if (read_z_bits > 0 && draw_z_bits > 0 &&
3215 (read_z_bits != draw_z_bits ||
3216 _mesa_get_format_datatype(readRb->Format) !=
3217 _mesa_get_format_datatype(drawRb->Format))) {
3218
3219 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3220 "(stencil attachment depth format mismatch)");
3221 return;
3222 }
3223 }
3224 }
3225
3226 if (mask & GL_DEPTH_BUFFER_BIT) {
3227 struct gl_renderbuffer *readRb =
3228 readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3229 struct gl_renderbuffer *drawRb =
3230 drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
3231
3232 /* From the EXT_framebuffer_object spec:
3233 *
3234 * "If a buffer is specified in <mask> and does not exist in both
3235 * the read and draw framebuffers, the corresponding bit is silently
3236 * ignored."
3237 */
3238 if ((readRb == NULL) || (drawRb == NULL)) {
3239 mask &= ~GL_DEPTH_BUFFER_BIT;
3240 }
3241 else {
3242 int read_s_bit, draw_s_bit;
3243
3244 if (_mesa_is_gles3(ctx) && (drawRb == readRb)) {
3245 _mesa_error(ctx, GL_INVALID_OPERATION,
3246 "glBlitFramebuffer(source and destination depth "
3247 "buffer cannot be the same)");
3248 return;
3249 }
3250
3251 if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
3252 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
3253 (_mesa_get_format_datatype(readRb->Format) !=
3254 _mesa_get_format_datatype(drawRb->Format))) {
3255 _mesa_error(ctx, GL_INVALID_OPERATION,
3256 "glBlitFramebuffer(depth attachment format mismatch)");
3257 return;
3258 }
3259
3260 read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
3261 draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
3262
3263 /* If both buffers also have stencil data, the stencil formats must
3264 * match as well. If one doesn't have stencil, it's not blitted, so
3265 * we should ignore the stencil format check.
3266 */
3267 if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
3268 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
3269 "(depth attachment stencil bits mismatch)");
3270 return;
3271 }
3272 }
3273 }
3274
3275
3276 if (_mesa_is_gles3(ctx)) {
3277 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3278 * 3.0.1 spec says:
3279 *
3280 * "If SAMPLE_BUFFERS for the draw framebuffer is greater than zero,
3281 * an INVALID_OPERATION error is generated."
3282 */
3283 if (drawFb->Visual.samples > 0) {
3284 _mesa_error(ctx, GL_INVALID_OPERATION,
3285 "glBlitFramebuffer(destination samples must be 0)");
3286 return;
3287 }
3288
3289 /* Page 194 (page 206 of the PDF) in section 4.3.2 of the OpenGL ES
3290 * 3.0.1 spec says:
3291 *
3292 * "If SAMPLE_BUFFERS for the read framebuffer is greater than zero,
3293 * no copy is performed and an INVALID_OPERATION error is generated
3294 * if the formats of the read and draw framebuffers are not
3295 * identical or if the source and destination rectangles are not
3296 * defined with the same (X0, Y0) and (X1, Y1) bounds."
3297 *
3298 * The format check was made above because desktop OpenGL has the same
3299 * requirement.
3300 */
3301 if (readFb->Visual.samples > 0
3302 && (srcX0 != dstX0 || srcY0 != dstY0
3303 || srcX1 != dstX1 || srcY1 != dstY1)) {
3304 _mesa_error(ctx, GL_INVALID_OPERATION,
3305 "glBlitFramebuffer(bad src/dst multisample region)");
3306 return;
3307 }
3308 } else {
3309 if (readFb->Visual.samples > 0 &&
3310 drawFb->Visual.samples > 0 &&
3311 readFb->Visual.samples != drawFb->Visual.samples) {
3312 _mesa_error(ctx, GL_INVALID_OPERATION,
3313 "glBlitFramebufferEXT(mismatched samples)");
3314 return;
3315 }
3316
3317 /* extra checks for multisample copies... */
3318 if ((readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) &&
3319 (filter == GL_NEAREST || filter == GL_LINEAR)) {
3320 /* src and dest region sizes must be the same */
3321 if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
3322 abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3323 _mesa_error(ctx, GL_INVALID_OPERATION,
3324 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3325 return;
3326 }
3327 }
3328 }
3329
3330 if (!ctx->Extensions.EXT_framebuffer_blit) {
3331 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3332 return;
3333 }
3334
3335 /* Debug code */
3336 if (DEBUG_BLIT) {
3337 const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3338 const struct gl_renderbuffer *colorDrawRb = NULL;
3339 GLuint i = 0;
3340
3341 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
3342 " 0x%x, 0x%x)\n",
3343 srcX0, srcY0, srcX1, srcY1,
3344 dstX0, dstY0, dstX1, dstY1,
3345 mask, filter);
3346 if (colorReadRb) {
3347 const struct gl_renderbuffer_attachment *att;
3348
3349 att = find_attachment(readFb, colorReadRb);
3350 printf(" Src FBO %u RB %u (%dx%d) ",
3351 readFb->Name, colorReadRb->Name,
3352 colorReadRb->Width, colorReadRb->Height);
3353 if (att && att->Texture) {
3354 printf("Tex %u tgt 0x%x level %u face %u",
3355 att->Texture->Name,
3356 att->Texture->Target,
3357 att->TextureLevel,
3358 att->CubeMapFace);
3359 }
3360 printf("\n");
3361
3362 /* Print all active color render buffers */
3363 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3364 colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3365 if (!colorDrawRb)
3366 continue;
3367
3368 att = find_attachment(drawFb, colorDrawRb);
3369 printf(" Dst FBO %u RB %u (%dx%d) ",
3370 drawFb->Name, colorDrawRb->Name,
3371 colorDrawRb->Width, colorDrawRb->Height);
3372 if (att && att->Texture) {
3373 printf("Tex %u tgt 0x%x level %u face %u",
3374 att->Texture->Name,
3375 att->Texture->Target,
3376 att->TextureLevel,
3377 att->CubeMapFace);
3378 }
3379 printf("\n");
3380 }
3381 }
3382 }
3383
3384 if (!mask ||
3385 (srcX1 - srcX0) == 0 || (srcY1 - srcY0) == 0 ||
3386 (dstX1 - dstX0) == 0 || (dstY1 - dstY0) == 0) {
3387 return;
3388 }
3389
3390 ASSERT(ctx->Driver.BlitFramebuffer);
3391 ctx->Driver.BlitFramebuffer(ctx,
3392 srcX0, srcY0, srcX1, srcY1,
3393 dstX0, dstY0, dstX1, dstY1,
3394 mask, filter);
3395 }
3396
3397
3398 static void
3399 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3400 const GLenum *attachments, GLint x, GLint y,
3401 GLsizei width, GLsizei height, const char *name)
3402 {
3403 int i;
3404 struct gl_framebuffer *fb;
3405 GET_CURRENT_CONTEXT(ctx);
3406
3407 fb = get_framebuffer_target(ctx, target);
3408 if (!fb) {
3409 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3410 return;
3411 }
3412
3413 if (numAttachments < 0) {
3414 _mesa_error(ctx, GL_INVALID_VALUE,
3415 "%s(numAttachments < 0)", name);
3416 return;
3417 }
3418
3419 /* The GL_ARB_invalidate_subdata spec says:
3420 *
3421 * "If an attachment is specified that does not exist in the
3422 * framebuffer bound to <target>, it is ignored."
3423 *
3424 * It also says:
3425 *
3426 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3427 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3428 * INVALID_OPERATION is generated."
3429 *
3430 * No mention is made of GL_AUXi being out of range. Therefore, we allow
3431 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3432 * set of retrictions).
3433 */
3434 for (i = 0; i < numAttachments; i++) {
3435 if (_mesa_is_winsys_fbo(fb)) {
3436 switch (attachments[i]) {
3437 case GL_ACCUM:
3438 case GL_AUX0:
3439 case GL_AUX1:
3440 case GL_AUX2:
3441 case GL_AUX3:
3442 /* Accumulation buffers and auxilary buffers were removed in
3443 * OpenGL 3.1, and they never existed in OpenGL ES.
3444 */
3445 if (ctx->API != API_OPENGL_COMPAT)
3446 goto invalid_enum;
3447 break;
3448 case GL_COLOR:
3449 case GL_DEPTH:
3450 case GL_STENCIL:
3451 break;
3452 case GL_BACK_LEFT:
3453 case GL_BACK_RIGHT:
3454 case GL_FRONT_LEFT:
3455 case GL_FRONT_RIGHT:
3456 if (!_mesa_is_desktop_gl(ctx))
3457 goto invalid_enum;
3458 break;
3459 default:
3460 goto invalid_enum;
3461 }
3462 } else {
3463 switch (attachments[i]) {
3464 case GL_DEPTH_ATTACHMENT:
3465 case GL_STENCIL_ATTACHMENT:
3466 break;
3467 case GL_COLOR_ATTACHMENT0:
3468 case GL_COLOR_ATTACHMENT1:
3469 case GL_COLOR_ATTACHMENT2:
3470 case GL_COLOR_ATTACHMENT3:
3471 case GL_COLOR_ATTACHMENT4:
3472 case GL_COLOR_ATTACHMENT5:
3473 case GL_COLOR_ATTACHMENT6:
3474 case GL_COLOR_ATTACHMENT7:
3475 case GL_COLOR_ATTACHMENT8:
3476 case GL_COLOR_ATTACHMENT9:
3477 case GL_COLOR_ATTACHMENT10:
3478 case GL_COLOR_ATTACHMENT11:
3479 case GL_COLOR_ATTACHMENT12:
3480 case GL_COLOR_ATTACHMENT13:
3481 case GL_COLOR_ATTACHMENT14:
3482 case GL_COLOR_ATTACHMENT15: {
3483 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3484 if (k >= ctx->Const.MaxColorAttachments) {
3485 _mesa_error(ctx, GL_INVALID_OPERATION,
3486 "%s(attachment >= max. color attachments)", name);
3487 return;
3488 }
3489 }
3490 default:
3491 goto invalid_enum;
3492 }
3493 }
3494 }
3495
3496 /* We don't actually do anything for this yet. Just return after
3497 * validating the parameters and generating the required errors.
3498 */
3499 return;
3500
3501 invalid_enum:
3502 _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3503 return;
3504 }
3505
3506 void GLAPIENTRY
3507 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3508 const GLenum *attachments, GLint x, GLint y,
3509 GLsizei width, GLsizei height)
3510 {
3511 invalidate_framebuffer_storage(target, numAttachments, attachments,
3512 x, y, width, height,
3513 "glInvalidateSubFramebuffer");
3514 }
3515
3516 void GLAPIENTRY
3517 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3518 const GLenum *attachments)
3519 {
3520 /* The GL_ARB_invalidate_subdata spec says:
3521 *
3522 * "The command
3523 *
3524 * void InvalidateFramebuffer(enum target,
3525 * sizei numAttachments,
3526 * const enum *attachments);
3527 *
3528 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3529 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3530 * <MAX_VIEWPORT_DIMS[1]> respectively."
3531 */
3532 invalidate_framebuffer_storage(target, numAttachments, attachments,
3533 0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3534 "glInvalidateFramebuffer");
3535 }
3536
3537 void GLAPIENTRY
3538 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
3539 const GLenum *attachments)
3540 {
3541 struct gl_framebuffer *fb;
3542 GLint i;
3543
3544 GET_CURRENT_CONTEXT(ctx);
3545
3546 fb = get_framebuffer_target(ctx, target);
3547 if (!fb) {
3548 _mesa_error(ctx, GL_INVALID_ENUM,
3549 "glDiscardFramebufferEXT(target %s)",
3550 _mesa_lookup_enum_by_nr(target));
3551 return;
3552 }
3553
3554 if (numAttachments < 0) {
3555 _mesa_error(ctx, GL_INVALID_VALUE,
3556 "glDiscardFramebufferEXT(numAttachments < 0)");
3557 return;
3558 }
3559
3560 for (i = 0; i < numAttachments; i++) {
3561 switch (attachments[i]) {
3562 case GL_COLOR:
3563 case GL_DEPTH:
3564 case GL_STENCIL:
3565 if (_mesa_is_user_fbo(fb))
3566 goto invalid_enum;
3567 break;
3568 case GL_COLOR_ATTACHMENT0:
3569 case GL_DEPTH_ATTACHMENT:
3570 case GL_STENCIL_ATTACHMENT:
3571 if (_mesa_is_winsys_fbo(fb))
3572 goto invalid_enum;
3573 break;
3574 default:
3575 goto invalid_enum;
3576 }
3577 }
3578
3579 if (ctx->Driver.DiscardFramebuffer)
3580 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
3581
3582 return;
3583
3584 invalid_enum:
3585 _mesa_error(ctx, GL_INVALID_ENUM,
3586 "glDiscardFramebufferEXT(attachment %s)",
3587 _mesa_lookup_enum_by_nr(attachments[i]));
3588 }