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