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