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