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