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