Merge remote-tracking branch 'public/master' into vulkan
[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(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 mtx_lock(&ctx->Shared->Mutex);
1303 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1304 newRb->RefCount = 1; /* referenced by hash table */
1305 mtx_unlock(&ctx->Shared->Mutex);
1306
1307 return newRb;
1308 }
1309
1310
1311 static void
1312 bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
1313 {
1314 struct gl_renderbuffer *newRb;
1315 GET_CURRENT_CONTEXT(ctx);
1316
1317 if (target != GL_RENDERBUFFER_EXT) {
1318 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
1319 return;
1320 }
1321
1322 /* No need to flush here since the render buffer binding has no
1323 * effect on rendering state.
1324 */
1325
1326 if (renderbuffer) {
1327 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1328 if (newRb == &DummyRenderbuffer) {
1329 /* ID was reserved, but no real renderbuffer object made yet */
1330 newRb = NULL;
1331 }
1332 else if (!newRb && !allow_user_names) {
1333 /* All RB IDs must be Gen'd */
1334 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1335 return;
1336 }
1337
1338 if (!newRb) {
1339 newRb = allocate_renderbuffer(ctx, renderbuffer, "glBindRenderbufferEXT");
1340 }
1341 }
1342 else {
1343 newRb = NULL;
1344 }
1345
1346 assert(newRb != &DummyRenderbuffer);
1347
1348 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1349 }
1350
1351 void GLAPIENTRY
1352 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1353 {
1354 GET_CURRENT_CONTEXT(ctx);
1355
1356 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1357 * entry point, but they allow the use of user-generated names.
1358 */
1359 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1360 }
1361
1362 void GLAPIENTRY
1363 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1364 {
1365 /* This function should not be in the dispatch table for core profile /
1366 * OpenGL 3.1, so execution should never get here in those cases -- no
1367 * need for an explicit test.
1368 */
1369 bind_renderbuffer(target, renderbuffer, true);
1370 }
1371
1372 /**
1373 * ARB_framebuffer_no_attachment - Application passes requested param's
1374 * here. NOTE: NumSamples requested need not be _NumSamples which is
1375 * what the hw supports.
1376 */
1377 static void
1378 framebuffer_parameteri(struct gl_context *ctx, struct gl_framebuffer *fb,
1379 GLenum pname, GLint param, const char *func)
1380 {
1381 switch (pname) {
1382 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1383 if (param < 0 || param > ctx->Const.MaxFramebufferWidth)
1384 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1385 else
1386 fb->DefaultGeometry.Width = param;
1387 break;
1388 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1389 if (param < 0 || param > ctx->Const.MaxFramebufferHeight)
1390 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1391 else
1392 fb->DefaultGeometry.Height = param;
1393 break;
1394 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1395 /*
1396 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1397 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1398 */
1399 if (_mesa_is_gles31(ctx)) {
1400 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1401 break;
1402 }
1403 if (param < 0 || param > ctx->Const.MaxFramebufferLayers)
1404 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1405 else
1406 fb->DefaultGeometry.Layers = param;
1407 break;
1408 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1409 if (param < 0 || param > ctx->Const.MaxFramebufferSamples)
1410 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1411 else
1412 fb->DefaultGeometry.NumSamples = param;
1413 break;
1414 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1415 fb->DefaultGeometry.FixedSampleLocations = param;
1416 break;
1417 default:
1418 _mesa_error(ctx, GL_INVALID_ENUM,
1419 "%s(pname=0x%x)", func, pname);
1420 }
1421
1422 invalidate_framebuffer(fb);
1423 ctx->NewState |= _NEW_BUFFERS;
1424 }
1425
1426 void GLAPIENTRY
1427 _mesa_FramebufferParameteri(GLenum target, GLenum pname, GLint param)
1428 {
1429 GET_CURRENT_CONTEXT(ctx);
1430 struct gl_framebuffer *fb;
1431
1432 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1433 _mesa_error(ctx, GL_INVALID_OPERATION,
1434 "glFramebufferParameteriv not supported "
1435 "(ARB_framebuffer_no_attachments not implemented)");
1436 return;
1437 }
1438
1439 fb = get_framebuffer_target(ctx, target);
1440 if (!fb) {
1441 _mesa_error(ctx, GL_INVALID_ENUM,
1442 "glFramebufferParameteri(target=0x%x)", target);
1443 return;
1444 }
1445
1446 /* check framebuffer binding */
1447 if (_mesa_is_winsys_fbo(fb)) {
1448 _mesa_error(ctx, GL_INVALID_OPERATION,
1449 "glFramebufferParameteri");
1450 return;
1451 }
1452
1453 framebuffer_parameteri(ctx, fb, pname, param, "glFramebufferParameteri");
1454 }
1455
1456 static void
1457 get_framebuffer_parameteriv(struct gl_context *ctx, struct gl_framebuffer *fb,
1458 GLenum pname, GLint *params, const char *func)
1459 {
1460 switch (pname) {
1461 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1462 *params = fb->DefaultGeometry.Width;
1463 break;
1464 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1465 *params = fb->DefaultGeometry.Height;
1466 break;
1467 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1468 /*
1469 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1470 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1471 */
1472 if (_mesa_is_gles31(ctx)) {
1473 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1474 break;
1475 }
1476 *params = fb->DefaultGeometry.Layers;
1477 break;
1478 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1479 *params = fb->DefaultGeometry.NumSamples;
1480 break;
1481 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1482 *params = fb->DefaultGeometry.FixedSampleLocations;
1483 break;
1484 default:
1485 _mesa_error(ctx, GL_INVALID_ENUM,
1486 "%s(pname=0x%x)", func, pname);
1487 }
1488 }
1489
1490 void GLAPIENTRY
1491 _mesa_GetFramebufferParameteriv(GLenum target, GLenum pname, GLint *params)
1492 {
1493 GET_CURRENT_CONTEXT(ctx);
1494 struct gl_framebuffer *fb;
1495
1496 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1497 _mesa_error(ctx, GL_INVALID_OPERATION,
1498 "glGetFramebufferParameteriv not supported "
1499 "(ARB_framebuffer_no_attachments not implemented)");
1500 return;
1501 }
1502
1503 fb = get_framebuffer_target(ctx, target);
1504 if (!fb) {
1505 _mesa_error(ctx, GL_INVALID_ENUM,
1506 "glGetFramebufferParameteriv(target=0x%x)", target);
1507 return;
1508 }
1509
1510 /* check framebuffer binding */
1511 if (_mesa_is_winsys_fbo(fb)) {
1512 _mesa_error(ctx, GL_INVALID_OPERATION,
1513 "glGetFramebufferParameteriv");
1514 return;
1515 }
1516
1517 get_framebuffer_parameteriv(ctx, fb, pname, params,
1518 "glGetFramebufferParameteriv");
1519 }
1520
1521
1522 /**
1523 * Remove the specified renderbuffer or texture from any attachment point in
1524 * the framebuffer.
1525 *
1526 * \returns
1527 * \c true if the renderbuffer was detached from an attachment point. \c
1528 * false otherwise.
1529 */
1530 bool
1531 _mesa_detach_renderbuffer(struct gl_context *ctx,
1532 struct gl_framebuffer *fb,
1533 const void *att)
1534 {
1535 unsigned i;
1536 bool progress = false;
1537
1538 for (i = 0; i < BUFFER_COUNT; i++) {
1539 if (fb->Attachment[i].Texture == att
1540 || fb->Attachment[i].Renderbuffer == att) {
1541 remove_attachment(ctx, &fb->Attachment[i]);
1542 progress = true;
1543 }
1544 }
1545
1546 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1547 * Completeness," of the OpenGL 3.1 spec says:
1548 *
1549 * "Performing any of the following actions may change whether the
1550 * framebuffer is considered complete or incomplete:
1551 *
1552 * ...
1553 *
1554 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1555 * containing an image that is attached to a framebuffer object
1556 * that is bound to the framebuffer."
1557 */
1558 if (progress)
1559 invalidate_framebuffer(fb);
1560
1561 return progress;
1562 }
1563
1564
1565 void GLAPIENTRY
1566 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1567 {
1568 GLint i;
1569 GET_CURRENT_CONTEXT(ctx);
1570
1571 if (n < 0) {
1572 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1573 return;
1574 }
1575
1576 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1577
1578 for (i = 0; i < n; i++) {
1579 if (renderbuffers[i] > 0) {
1580 struct gl_renderbuffer *rb;
1581 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1582 if (rb) {
1583 /* check if deleting currently bound renderbuffer object */
1584 if (rb == ctx->CurrentRenderbuffer) {
1585 /* bind default */
1586 assert(rb->RefCount >= 2);
1587 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1588 }
1589
1590 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1591 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1592 * of the OpenGL 3.1 spec says:
1593 *
1594 * "If a renderbuffer object is deleted while its image is
1595 * attached to one or more attachment points in the currently
1596 * bound framebuffer, then it is as if FramebufferRenderbuffer
1597 * had been called, with a renderbuffer of 0, for each
1598 * attachment point to which this image was attached in the
1599 * currently bound framebuffer. In other words, this
1600 * renderbuffer image is first detached from all attachment
1601 * points in the currently bound framebuffer. Note that the
1602 * renderbuffer image is specifically not detached from any
1603 * non-bound framebuffers. Detaching the image from any
1604 * non-bound framebuffers is the responsibility of the
1605 * application.
1606 */
1607 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1608 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1609 }
1610 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1611 && ctx->ReadBuffer != ctx->DrawBuffer) {
1612 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1613 }
1614
1615 /* Remove from hash table immediately, to free the ID.
1616 * But the object will not be freed until it's no longer
1617 * referenced anywhere else.
1618 */
1619 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1620
1621 if (rb != &DummyRenderbuffer) {
1622 /* no longer referenced by hash table */
1623 _mesa_reference_renderbuffer(&rb, NULL);
1624 }
1625 }
1626 }
1627 }
1628 }
1629
1630 static void
1631 create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1632 bool dsa)
1633 {
1634 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
1635 GLuint first;
1636 GLint i;
1637
1638 if (n < 0) {
1639 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
1640 return;
1641 }
1642
1643 if (!renderbuffers)
1644 return;
1645
1646 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1647
1648 for (i = 0; i < n; i++) {
1649 GLuint name = first + i;
1650 renderbuffers[i] = name;
1651
1652 if (dsa) {
1653 allocate_renderbuffer(ctx, name, func);
1654 } else {
1655 /* insert a dummy renderbuffer into the hash table */
1656 mtx_lock(&ctx->Shared->Mutex);
1657 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1658 mtx_unlock(&ctx->Shared->Mutex);
1659 }
1660 }
1661 }
1662
1663
1664 void GLAPIENTRY
1665 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1666 {
1667 GET_CURRENT_CONTEXT(ctx);
1668 create_render_buffers(ctx, n, renderbuffers, false);
1669 }
1670
1671
1672 void GLAPIENTRY
1673 _mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1674 {
1675 GET_CURRENT_CONTEXT(ctx);
1676 create_render_buffers(ctx, n, renderbuffers, true);
1677 }
1678
1679
1680 /**
1681 * Given an internal format token for a render buffer, return the
1682 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1683 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1684 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1685 *
1686 * This is similar to _mesa_base_tex_format() but the set of valid
1687 * internal formats is different.
1688 *
1689 * Note that even if a format is determined to be legal here, validation
1690 * of the FBO may fail if the format is not supported by the driver/GPU.
1691 *
1692 * \param internalFormat as passed to glRenderbufferStorage()
1693 * \return the base internal format, or 0 if internalFormat is illegal
1694 */
1695 GLenum
1696 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1697 {
1698 /*
1699 * Notes: some formats such as alpha, luminance, etc. were added
1700 * with GL_ARB_framebuffer_object.
1701 */
1702 switch (internalFormat) {
1703 case GL_ALPHA:
1704 case GL_ALPHA4:
1705 case GL_ALPHA8:
1706 case GL_ALPHA12:
1707 case GL_ALPHA16:
1708 return (ctx->API == API_OPENGL_COMPAT &&
1709 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
1710 case GL_LUMINANCE:
1711 case GL_LUMINANCE4:
1712 case GL_LUMINANCE8:
1713 case GL_LUMINANCE12:
1714 case GL_LUMINANCE16:
1715 return (ctx->API == API_OPENGL_COMPAT &&
1716 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
1717 case GL_LUMINANCE_ALPHA:
1718 case GL_LUMINANCE4_ALPHA4:
1719 case GL_LUMINANCE6_ALPHA2:
1720 case GL_LUMINANCE8_ALPHA8:
1721 case GL_LUMINANCE12_ALPHA4:
1722 case GL_LUMINANCE12_ALPHA12:
1723 case GL_LUMINANCE16_ALPHA16:
1724 return (ctx->API == API_OPENGL_COMPAT &&
1725 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
1726 case GL_INTENSITY:
1727 case GL_INTENSITY4:
1728 case GL_INTENSITY8:
1729 case GL_INTENSITY12:
1730 case GL_INTENSITY16:
1731 return (ctx->API == API_OPENGL_COMPAT &&
1732 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
1733 case GL_RGB8:
1734 return GL_RGB;
1735 case GL_RGB:
1736 case GL_R3_G3_B2:
1737 case GL_RGB4:
1738 case GL_RGB5:
1739 case GL_RGB10:
1740 case GL_RGB12:
1741 case GL_RGB16:
1742 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1743 case GL_SRGB8_EXT:
1744 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1745 case GL_RGBA4:
1746 case GL_RGB5_A1:
1747 case GL_RGBA8:
1748 return GL_RGBA;
1749 case GL_RGBA:
1750 case GL_RGBA2:
1751 case GL_RGBA12:
1752 case GL_RGBA16:
1753 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1754 case GL_RGB10_A2:
1755 case GL_SRGB8_ALPHA8_EXT:
1756 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1757 case GL_STENCIL_INDEX:
1758 case GL_STENCIL_INDEX1_EXT:
1759 case GL_STENCIL_INDEX4_EXT:
1760 case GL_STENCIL_INDEX16_EXT:
1761 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1762 * OpenGL ES, but Mesa does not currently support them.
1763 */
1764 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1765 case GL_STENCIL_INDEX8_EXT:
1766 return GL_STENCIL_INDEX;
1767 case GL_DEPTH_COMPONENT:
1768 case GL_DEPTH_COMPONENT32:
1769 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1770 case GL_DEPTH_COMPONENT16:
1771 case GL_DEPTH_COMPONENT24:
1772 return GL_DEPTH_COMPONENT;
1773 case GL_DEPTH_STENCIL:
1774 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1775 case GL_DEPTH24_STENCIL8:
1776 return GL_DEPTH_STENCIL;
1777 case GL_DEPTH_COMPONENT32F:
1778 return ctx->Version >= 30
1779 || (ctx->API == API_OPENGL_COMPAT &&
1780 ctx->Extensions.ARB_depth_buffer_float)
1781 ? GL_DEPTH_COMPONENT : 0;
1782 case GL_DEPTH32F_STENCIL8:
1783 return ctx->Version >= 30
1784 || (ctx->API == API_OPENGL_COMPAT &&
1785 ctx->Extensions.ARB_depth_buffer_float)
1786 ? GL_DEPTH_STENCIL : 0;
1787 case GL_RED:
1788 case GL_R16:
1789 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1790 ? GL_RED : 0;
1791 case GL_R8:
1792 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1793 ? GL_RED : 0;
1794 case GL_RG:
1795 case GL_RG16:
1796 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1797 ? GL_RG : 0;
1798 case GL_RG8:
1799 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1800 ? GL_RG : 0;
1801 /* signed normalized texture formats */
1802 case GL_RED_SNORM:
1803 case GL_R8_SNORM:
1804 case GL_R16_SNORM:
1805 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1806 ? GL_RED : 0;
1807 case GL_RG_SNORM:
1808 case GL_RG8_SNORM:
1809 case GL_RG16_SNORM:
1810 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1811 ? GL_RG : 0;
1812 case GL_RGB_SNORM:
1813 case GL_RGB8_SNORM:
1814 case GL_RGB16_SNORM:
1815 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1816 ? GL_RGB : 0;
1817 case GL_RGBA_SNORM:
1818 case GL_RGBA8_SNORM:
1819 case GL_RGBA16_SNORM:
1820 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1821 ? GL_RGBA : 0;
1822 case GL_ALPHA_SNORM:
1823 case GL_ALPHA8_SNORM:
1824 case GL_ALPHA16_SNORM:
1825 return ctx->API == API_OPENGL_COMPAT &&
1826 ctx->Extensions.EXT_texture_snorm &&
1827 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1828 case GL_LUMINANCE_SNORM:
1829 case GL_LUMINANCE8_SNORM:
1830 case GL_LUMINANCE16_SNORM:
1831 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1832 ? GL_LUMINANCE : 0;
1833 case GL_LUMINANCE_ALPHA_SNORM:
1834 case GL_LUMINANCE8_ALPHA8_SNORM:
1835 case GL_LUMINANCE16_ALPHA16_SNORM:
1836 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1837 ? GL_LUMINANCE_ALPHA : 0;
1838 case GL_INTENSITY_SNORM:
1839 case GL_INTENSITY8_SNORM:
1840 case GL_INTENSITY16_SNORM:
1841 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1842 ? GL_INTENSITY : 0;
1843
1844 case GL_R16F:
1845 case GL_R32F:
1846 return ((_mesa_is_desktop_gl(ctx) &&
1847 ctx->Extensions.ARB_texture_rg &&
1848 ctx->Extensions.ARB_texture_float) ||
1849 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1850 ? GL_RED : 0;
1851 case GL_RG16F:
1852 case GL_RG32F:
1853 return ((_mesa_is_desktop_gl(ctx) &&
1854 ctx->Extensions.ARB_texture_rg &&
1855 ctx->Extensions.ARB_texture_float) ||
1856 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1857 ? GL_RG : 0;
1858 case GL_RGB16F:
1859 case GL_RGB32F:
1860 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1861 ? GL_RGB : 0;
1862 case GL_RGBA16F:
1863 case GL_RGBA32F:
1864 return ((_mesa_is_desktop_gl(ctx) &&
1865 ctx->Extensions.ARB_texture_float) ||
1866 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1867 ? GL_RGBA : 0;
1868 case GL_ALPHA16F_ARB:
1869 case GL_ALPHA32F_ARB:
1870 return ctx->API == API_OPENGL_COMPAT &&
1871 ctx->Extensions.ARB_texture_float &&
1872 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1873 case GL_LUMINANCE16F_ARB:
1874 case GL_LUMINANCE32F_ARB:
1875 return ctx->API == API_OPENGL_COMPAT &&
1876 ctx->Extensions.ARB_texture_float &&
1877 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1878 case GL_LUMINANCE_ALPHA16F_ARB:
1879 case GL_LUMINANCE_ALPHA32F_ARB:
1880 return ctx->API == API_OPENGL_COMPAT &&
1881 ctx->Extensions.ARB_texture_float &&
1882 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1883 case GL_INTENSITY16F_ARB:
1884 case GL_INTENSITY32F_ARB:
1885 return ctx->API == API_OPENGL_COMPAT &&
1886 ctx->Extensions.ARB_texture_float &&
1887 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1888 case GL_R11F_G11F_B10F:
1889 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1890 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1891 ? GL_RGB : 0;
1892
1893 case GL_RGBA8UI_EXT:
1894 case GL_RGBA16UI_EXT:
1895 case GL_RGBA32UI_EXT:
1896 case GL_RGBA8I_EXT:
1897 case GL_RGBA16I_EXT:
1898 case GL_RGBA32I_EXT:
1899 return ctx->Version >= 30
1900 || (_mesa_is_desktop_gl(ctx) &&
1901 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1902
1903 case GL_RGB8UI_EXT:
1904 case GL_RGB16UI_EXT:
1905 case GL_RGB32UI_EXT:
1906 case GL_RGB8I_EXT:
1907 case GL_RGB16I_EXT:
1908 case GL_RGB32I_EXT:
1909 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1910 ? GL_RGB : 0;
1911 case GL_R8UI:
1912 case GL_R8I:
1913 case GL_R16UI:
1914 case GL_R16I:
1915 case GL_R32UI:
1916 case GL_R32I:
1917 return ctx->Version >= 30
1918 || (_mesa_is_desktop_gl(ctx) &&
1919 ctx->Extensions.ARB_texture_rg &&
1920 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1921
1922 case GL_RG8UI:
1923 case GL_RG8I:
1924 case GL_RG16UI:
1925 case GL_RG16I:
1926 case GL_RG32UI:
1927 case GL_RG32I:
1928 return ctx->Version >= 30
1929 || (_mesa_is_desktop_gl(ctx) &&
1930 ctx->Extensions.ARB_texture_rg &&
1931 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1932
1933 case GL_INTENSITY8I_EXT:
1934 case GL_INTENSITY8UI_EXT:
1935 case GL_INTENSITY16I_EXT:
1936 case GL_INTENSITY16UI_EXT:
1937 case GL_INTENSITY32I_EXT:
1938 case GL_INTENSITY32UI_EXT:
1939 return ctx->API == API_OPENGL_COMPAT &&
1940 ctx->Extensions.EXT_texture_integer &&
1941 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1942
1943 case GL_LUMINANCE8I_EXT:
1944 case GL_LUMINANCE8UI_EXT:
1945 case GL_LUMINANCE16I_EXT:
1946 case GL_LUMINANCE16UI_EXT:
1947 case GL_LUMINANCE32I_EXT:
1948 case GL_LUMINANCE32UI_EXT:
1949 return ctx->API == API_OPENGL_COMPAT &&
1950 ctx->Extensions.EXT_texture_integer &&
1951 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1952
1953 case GL_LUMINANCE_ALPHA8I_EXT:
1954 case GL_LUMINANCE_ALPHA8UI_EXT:
1955 case GL_LUMINANCE_ALPHA16I_EXT:
1956 case GL_LUMINANCE_ALPHA16UI_EXT:
1957 case GL_LUMINANCE_ALPHA32I_EXT:
1958 case GL_LUMINANCE_ALPHA32UI_EXT:
1959 return ctx->API == API_OPENGL_COMPAT &&
1960 ctx->Extensions.EXT_texture_integer &&
1961 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1962
1963 case GL_ALPHA8I_EXT:
1964 case GL_ALPHA8UI_EXT:
1965 case GL_ALPHA16I_EXT:
1966 case GL_ALPHA16UI_EXT:
1967 case GL_ALPHA32I_EXT:
1968 case GL_ALPHA32UI_EXT:
1969 return ctx->API == API_OPENGL_COMPAT &&
1970 ctx->Extensions.EXT_texture_integer &&
1971 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1972
1973 case GL_RGB10_A2UI:
1974 return (_mesa_is_desktop_gl(ctx) &&
1975 ctx->Extensions.ARB_texture_rgb10_a2ui)
1976 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1977
1978 case GL_RGB565:
1979 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1980 ? GL_RGB : 0;
1981 default:
1982 return 0;
1983 }
1984 }
1985
1986
1987 /**
1988 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1989 */
1990 static void
1991 invalidate_rb(GLuint key, void *data, void *userData)
1992 {
1993 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1994 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1995
1996 /* If this is a user-created FBO */
1997 if (_mesa_is_user_fbo(fb)) {
1998 GLuint i;
1999 for (i = 0; i < BUFFER_COUNT; i++) {
2000 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2001 if (att->Type == GL_RENDERBUFFER &&
2002 att->Renderbuffer == rb) {
2003 /* Mark fb status as indeterminate to force re-validation */
2004 fb->_Status = 0;
2005 return;
2006 }
2007 }
2008 }
2009 }
2010
2011
2012 /** sentinal value, see below */
2013 #define NO_SAMPLES 1000
2014
2015 void
2016 _mesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2017 GLenum internalFormat, GLsizei width,
2018 GLsizei height, GLsizei samples)
2019 {
2020 const GLenum baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2021
2022 assert(baseFormat != 0);
2023 assert(width >= 0 && width <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2024 assert(height >= 0 && height <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2025 assert(samples != NO_SAMPLES);
2026 if (samples != 0) {
2027 assert(samples > 0);
2028 assert(_mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2029 internalFormat, samples) == GL_NO_ERROR);
2030 }
2031
2032 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2033
2034 if (rb->InternalFormat == internalFormat &&
2035 rb->Width == (GLuint) width &&
2036 rb->Height == (GLuint) height &&
2037 rb->NumSamples == samples) {
2038 /* no change in allocation needed */
2039 return;
2040 }
2041
2042 /* These MUST get set by the AllocStorage func */
2043 rb->Format = MESA_FORMAT_NONE;
2044 rb->NumSamples = samples;
2045
2046 /* Now allocate the storage */
2047 assert(rb->AllocStorage);
2048 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
2049 /* No error - check/set fields now */
2050 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2051 assert(rb->Width == (GLuint) width);
2052 assert(rb->Height == (GLuint) height);
2053 rb->InternalFormat = internalFormat;
2054 rb->_BaseFormat = baseFormat;
2055 assert(rb->_BaseFormat != 0);
2056 }
2057 else {
2058 /* Probably ran out of memory - clear the fields */
2059 rb->Width = 0;
2060 rb->Height = 0;
2061 rb->Format = MESA_FORMAT_NONE;
2062 rb->InternalFormat = GL_NONE;
2063 rb->_BaseFormat = GL_NONE;
2064 rb->NumSamples = 0;
2065 }
2066
2067 /* Invalidate the framebuffers the renderbuffer is attached in. */
2068 if (rb->AttachedAnytime) {
2069 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
2070 }
2071 }
2072
2073 /**
2074 * Helper function used by renderbuffer_storage_direct() and
2075 * renderbuffer_storage_target().
2076 * samples will be NO_SAMPLES if called by a non-multisample function.
2077 */
2078 static void
2079 renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2080 GLenum internalFormat, GLsizei width,
2081 GLsizei height, GLsizei samples, const char *func)
2082 {
2083 GLenum baseFormat;
2084 GLenum sample_count_error;
2085
2086 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2087 if (baseFormat == 0) {
2088 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
2089 func, _mesa_enum_to_string(internalFormat));
2090 return;
2091 }
2092
2093 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2094 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
2095 width);
2096 return;
2097 }
2098
2099 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2100 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
2101 height);
2102 return;
2103 }
2104
2105 if (samples == NO_SAMPLES) {
2106 /* NumSamples == 0 indicates non-multisampling */
2107 samples = 0;
2108 }
2109 else {
2110 /* check the sample count;
2111 * note: driver may choose to use more samples than what's requested
2112 */
2113 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2114 internalFormat, samples);
2115
2116 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2117 *
2118 * "If a negative number is provided where an argument of type sizei or
2119 * sizeiptr is specified, the error INVALID VALUE is generated."
2120 */
2121 if (samples < 0) {
2122 sample_count_error = GL_INVALID_VALUE;
2123 }
2124
2125 if (sample_count_error != GL_NO_ERROR) {
2126 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
2127 return;
2128 }
2129 }
2130
2131 _mesa_renderbuffer_storage(ctx, rb, internalFormat, width, height, samples);
2132 }
2133
2134 /**
2135 * Helper function used by _mesa_NamedRenderbufferStorage*().
2136 * samples will be NO_SAMPLES if called by a non-multisample function.
2137 */
2138 static void
2139 renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
2140 GLsizei width, GLsizei height, GLsizei samples,
2141 const char *func)
2142 {
2143 GET_CURRENT_CONTEXT(ctx);
2144
2145 if (MESA_VERBOSE & VERBOSE_API) {
2146 if (samples == NO_SAMPLES)
2147 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
2148 func, renderbuffer,
2149 _mesa_enum_to_string(internalFormat),
2150 width, height);
2151 else
2152 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
2153 func, renderbuffer,
2154 _mesa_enum_to_string(internalFormat),
2155 width, height, samples);
2156 }
2157
2158 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2159 if (!rb || rb == &DummyRenderbuffer) {
2160 /* ID was reserved, but no real renderbuffer object made yet */
2161 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
2162 func, renderbuffer);
2163 return;
2164 }
2165
2166 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
2167 }
2168
2169 /**
2170 * Helper function used by _mesa_RenderbufferStorage() and
2171 * _mesa_RenderbufferStorageMultisample().
2172 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2173 */
2174 static void
2175 renderbuffer_storage_target(GLenum target, GLenum internalFormat,
2176 GLsizei width, GLsizei height, GLsizei samples,
2177 const char *func)
2178 {
2179 GET_CURRENT_CONTEXT(ctx);
2180
2181 if (MESA_VERBOSE & VERBOSE_API) {
2182 if (samples == NO_SAMPLES)
2183 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
2184 func,
2185 _mesa_enum_to_string(target),
2186 _mesa_enum_to_string(internalFormat),
2187 width, height);
2188 else
2189 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
2190 func,
2191 _mesa_enum_to_string(target),
2192 _mesa_enum_to_string(internalFormat),
2193 width, height, samples);
2194 }
2195
2196 if (target != GL_RENDERBUFFER_EXT) {
2197 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
2198 return;
2199 }
2200
2201 if (!ctx->CurrentRenderbuffer) {
2202 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
2203 func);
2204 return;
2205 }
2206
2207 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
2208 height, samples, func);
2209 }
2210
2211
2212 void GLAPIENTRY
2213 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
2214 {
2215 struct gl_renderbuffer *rb;
2216 GET_CURRENT_CONTEXT(ctx);
2217
2218 if (!ctx->Extensions.OES_EGL_image) {
2219 _mesa_error(ctx, GL_INVALID_OPERATION,
2220 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2221 return;
2222 }
2223
2224 if (target != GL_RENDERBUFFER) {
2225 _mesa_error(ctx, GL_INVALID_ENUM,
2226 "EGLImageTargetRenderbufferStorageOES");
2227 return;
2228 }
2229
2230 rb = ctx->CurrentRenderbuffer;
2231 if (!rb) {
2232 _mesa_error(ctx, GL_INVALID_OPERATION,
2233 "EGLImageTargetRenderbufferStorageOES");
2234 return;
2235 }
2236
2237 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2238
2239 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2240 }
2241
2242
2243 /**
2244 * Helper function for _mesa_GetRenderbufferParameteriv() and
2245 * _mesa_GetFramebufferAttachmentParameteriv()
2246 * We have to be careful to respect the base format. For example, if a
2247 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2248 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2249 * we need to return zero.
2250 */
2251 static GLint
2252 get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
2253 {
2254 if (_mesa_base_format_has_channel(baseFormat, pname))
2255 return _mesa_get_format_bits(format, pname);
2256 else
2257 return 0;
2258 }
2259
2260
2261
2262 void GLAPIENTRY
2263 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
2264 GLsizei width, GLsizei height)
2265 {
2266 /* GL_ARB_fbo says calling this function is equivalent to calling
2267 * glRenderbufferStorageMultisample() with samples=0. We pass in
2268 * a token value here just for error reporting purposes.
2269 */
2270 renderbuffer_storage_target(target, internalFormat, width, height,
2271 NO_SAMPLES, "glRenderbufferStorage");
2272 }
2273
2274
2275 void GLAPIENTRY
2276 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
2277 GLenum internalFormat,
2278 GLsizei width, GLsizei height)
2279 {
2280 renderbuffer_storage_target(target, internalFormat, width, height,
2281 samples, "glRenderbufferStorageMultisample");
2282 }
2283
2284
2285 /**
2286 * OpenGL ES version of glRenderBufferStorage.
2287 */
2288 void GLAPIENTRY
2289 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
2290 GLsizei width, GLsizei height)
2291 {
2292 switch (internalFormat) {
2293 case GL_RGB565:
2294 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2295 /* choose a closest format */
2296 internalFormat = GL_RGB5;
2297 break;
2298 default:
2299 break;
2300 }
2301
2302 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2303 "glRenderbufferStorageEXT");
2304 }
2305
2306 void GLAPIENTRY
2307 _mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2308 GLsizei width, GLsizei height)
2309 {
2310 /* GL_ARB_fbo says calling this function is equivalent to calling
2311 * glRenderbufferStorageMultisample() with samples=0. We pass in
2312 * a token value here just for error reporting purposes.
2313 */
2314 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2315 NO_SAMPLES, "glNamedRenderbufferStorage");
2316 }
2317
2318 void GLAPIENTRY
2319 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2320 GLenum internalformat,
2321 GLsizei width, GLsizei height)
2322 {
2323 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2324 samples,
2325 "glNamedRenderbufferStorageMultisample");
2326 }
2327
2328
2329 static void
2330 get_render_buffer_parameteriv(struct gl_context *ctx,
2331 struct gl_renderbuffer *rb, GLenum pname,
2332 GLint *params, const char *func)
2333 {
2334 /* No need to flush here since we're just quering state which is
2335 * not effected by rendering.
2336 */
2337
2338 switch (pname) {
2339 case GL_RENDERBUFFER_WIDTH_EXT:
2340 *params = rb->Width;
2341 return;
2342 case GL_RENDERBUFFER_HEIGHT_EXT:
2343 *params = rb->Height;
2344 return;
2345 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
2346 *params = rb->InternalFormat;
2347 return;
2348 case GL_RENDERBUFFER_RED_SIZE_EXT:
2349 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
2350 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
2351 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
2352 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
2353 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
2354 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
2355 break;
2356 case GL_RENDERBUFFER_SAMPLES:
2357 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2358 || _mesa_is_gles3(ctx)) {
2359 *params = rb->NumSamples;
2360 break;
2361 }
2362 /* fallthrough */
2363 default:
2364 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
2365 _mesa_enum_to_string(pname));
2366 return;
2367 }
2368 }
2369
2370
2371 void GLAPIENTRY
2372 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2373 {
2374 GET_CURRENT_CONTEXT(ctx);
2375
2376 if (target != GL_RENDERBUFFER_EXT) {
2377 _mesa_error(ctx, GL_INVALID_ENUM,
2378 "glGetRenderbufferParameterivEXT(target)");
2379 return;
2380 }
2381
2382 if (!ctx->CurrentRenderbuffer) {
2383 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2384 "(no renderbuffer bound)");
2385 return;
2386 }
2387
2388 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2389 params, "glGetRenderbufferParameteriv");
2390 }
2391
2392
2393 void GLAPIENTRY
2394 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2395 GLint *params)
2396 {
2397 GET_CURRENT_CONTEXT(ctx);
2398
2399 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2400 if (!rb || rb == &DummyRenderbuffer) {
2401 /* ID was reserved, but no real renderbuffer object made yet */
2402 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2403 "(invalid renderbuffer %i)", renderbuffer);
2404 return;
2405 }
2406
2407 get_render_buffer_parameteriv(ctx, rb, pname, params,
2408 "glGetNamedRenderbufferParameteriv");
2409 }
2410
2411
2412 GLboolean GLAPIENTRY
2413 _mesa_IsFramebuffer(GLuint framebuffer)
2414 {
2415 GET_CURRENT_CONTEXT(ctx);
2416 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2417 if (framebuffer) {
2418 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
2419 if (rb != NULL && rb != &DummyFramebuffer)
2420 return GL_TRUE;
2421 }
2422 return GL_FALSE;
2423 }
2424
2425
2426 /**
2427 * Check if any of the attachments of the given framebuffer are textures
2428 * (render to texture). Call ctx->Driver.RenderTexture() for such
2429 * attachments.
2430 */
2431 static void
2432 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2433 {
2434 GLuint i;
2435 assert(ctx->Driver.RenderTexture);
2436
2437 if (_mesa_is_winsys_fbo(fb))
2438 return; /* can't render to texture with winsys framebuffers */
2439
2440 for (i = 0; i < BUFFER_COUNT; i++) {
2441 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2442 if (att->Texture && att->Renderbuffer->TexImage
2443 && driver_RenderTexture_is_safe(att)) {
2444 ctx->Driver.RenderTexture(ctx, fb, att);
2445 }
2446 }
2447 }
2448
2449
2450 /**
2451 * Examine all the framebuffer's attachments to see if any are textures.
2452 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2453 * notify the device driver that the texture image may have changed.
2454 */
2455 static void
2456 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2457 {
2458 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2459 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2460 return;
2461
2462 if (ctx->Driver.FinishRenderTexture) {
2463 GLuint i;
2464 for (i = 0; i < BUFFER_COUNT; i++) {
2465 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2466 struct gl_renderbuffer *rb = att->Renderbuffer;
2467 if (rb && rb->NeedsFinishRenderTexture) {
2468 ctx->Driver.FinishRenderTexture(ctx, rb);
2469 }
2470 }
2471 }
2472 }
2473
2474
2475 static void
2476 bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
2477 {
2478 struct gl_framebuffer *newDrawFb, *newReadFb;
2479 GLboolean bindReadBuf, bindDrawBuf;
2480 GET_CURRENT_CONTEXT(ctx);
2481
2482 switch (target) {
2483 case GL_DRAW_FRAMEBUFFER_EXT:
2484 bindDrawBuf = GL_TRUE;
2485 bindReadBuf = GL_FALSE;
2486 break;
2487 case GL_READ_FRAMEBUFFER_EXT:
2488 bindDrawBuf = GL_FALSE;
2489 bindReadBuf = GL_TRUE;
2490 break;
2491 case GL_FRAMEBUFFER_EXT:
2492 bindDrawBuf = GL_TRUE;
2493 bindReadBuf = GL_TRUE;
2494 break;
2495 default:
2496 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
2497 return;
2498 }
2499
2500 if (framebuffer) {
2501 /* Binding a user-created framebuffer object */
2502 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2503 if (newDrawFb == &DummyFramebuffer) {
2504 /* ID was reserved, but no real framebuffer object made yet */
2505 newDrawFb = NULL;
2506 }
2507 else if (!newDrawFb && !allow_user_names) {
2508 /* All FBO IDs must be Gen'd */
2509 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2510 return;
2511 }
2512
2513 if (!newDrawFb) {
2514 /* create new framebuffer object */
2515 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2516 if (!newDrawFb) {
2517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2518 return;
2519 }
2520 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
2521 }
2522 newReadFb = newDrawFb;
2523 }
2524 else {
2525 /* Binding the window system framebuffer (which was originally set
2526 * with MakeCurrent).
2527 */
2528 newDrawFb = ctx->WinSysDrawBuffer;
2529 newReadFb = ctx->WinSysReadBuffer;
2530 }
2531
2532 _mesa_bind_framebuffers(ctx,
2533 bindDrawBuf ? newDrawFb : ctx->DrawBuffer,
2534 bindReadBuf ? newReadFb : ctx->ReadBuffer);
2535 }
2536
2537 void
2538 _mesa_bind_framebuffers(struct gl_context *ctx,
2539 struct gl_framebuffer *newDrawFb,
2540 struct gl_framebuffer *newReadFb)
2541 {
2542 struct gl_framebuffer *const oldDrawFb = ctx->DrawBuffer;
2543 struct gl_framebuffer *const oldReadFb = ctx->ReadBuffer;
2544 const bool bindDrawBuf = oldDrawFb != newDrawFb;
2545 const bool bindReadBuf = oldReadFb != newReadFb;
2546
2547 assert(newDrawFb);
2548 assert(newDrawFb != &DummyFramebuffer);
2549
2550 /*
2551 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2552 *
2553 * We also check if we're beginning and/or ending render-to-texture.
2554 * When a framebuffer with texture attachments is unbound, call
2555 * ctx->Driver.FinishRenderTexture().
2556 * When a framebuffer with texture attachments is bound, call
2557 * ctx->Driver.RenderTexture().
2558 *
2559 * Note that if the ReadBuffer has texture attachments we don't consider
2560 * that a render-to-texture case.
2561 */
2562 if (bindReadBuf) {
2563 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2564
2565 /* check if old readbuffer was render-to-texture */
2566 check_end_texture_render(ctx, oldReadFb);
2567
2568 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2569 }
2570
2571 if (bindDrawBuf) {
2572 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2573
2574 /* check if old framebuffer had any texture attachments */
2575 if (oldDrawFb)
2576 check_end_texture_render(ctx, oldDrawFb);
2577
2578 /* check if newly bound framebuffer has any texture attachments */
2579 check_begin_texture_render(ctx, newDrawFb);
2580
2581 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2582 }
2583
2584 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2585 /* The few classic drivers that actually hook this function really only
2586 * want to know if the draw framebuffer changed.
2587 */
2588 ctx->Driver.BindFramebuffer(ctx,
2589 bindDrawBuf ? GL_FRAMEBUFFER : GL_READ_FRAMEBUFFER,
2590 newDrawFb, newReadFb);
2591 }
2592 }
2593
2594 void GLAPIENTRY
2595 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
2596 {
2597 GET_CURRENT_CONTEXT(ctx);
2598
2599 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2600 * point, but they allow the use of user-generated names.
2601 */
2602 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
2603 }
2604
2605
2606 void GLAPIENTRY
2607 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2608 {
2609 /* This function should not be in the dispatch table for core profile /
2610 * OpenGL 3.1, so execution should never get here in those cases -- no
2611 * need for an explicit test.
2612 */
2613 bind_framebuffer(target, framebuffer, true);
2614 }
2615
2616
2617 void GLAPIENTRY
2618 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2619 {
2620 GLint i;
2621 GET_CURRENT_CONTEXT(ctx);
2622
2623 if (n < 0) {
2624 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2625 return;
2626 }
2627
2628 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2629
2630 for (i = 0; i < n; i++) {
2631 if (framebuffers[i] > 0) {
2632 struct gl_framebuffer *fb;
2633 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2634 if (fb) {
2635 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2636
2637 /* check if deleting currently bound framebuffer object */
2638 if (fb == ctx->DrawBuffer) {
2639 /* bind default */
2640 assert(fb->RefCount >= 2);
2641 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2642 }
2643 if (fb == ctx->ReadBuffer) {
2644 /* bind default */
2645 assert(fb->RefCount >= 2);
2646 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2647 }
2648
2649 /* remove from hash table immediately, to free the ID */
2650 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2651
2652 if (fb != &DummyFramebuffer) {
2653 /* But the object will not be freed until it's no longer
2654 * bound in any context.
2655 */
2656 _mesa_reference_framebuffer(&fb, NULL);
2657 }
2658 }
2659 }
2660 }
2661 }
2662
2663
2664 /**
2665 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2666 * It is not exposed to the rest of Mesa to encourage the use of
2667 * nameless buffers in driver internals.
2668 */
2669 static void
2670 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
2671 {
2672 GET_CURRENT_CONTEXT(ctx);
2673 GLuint first;
2674 GLint i;
2675 struct gl_framebuffer *fb;
2676
2677 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
2678
2679 if (n < 0) {
2680 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
2681 return;
2682 }
2683
2684 if (!framebuffers)
2685 return;
2686
2687 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2688
2689 for (i = 0; i < n; i++) {
2690 GLuint name = first + i;
2691 framebuffers[i] = name;
2692
2693 if (dsa) {
2694 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2695 if (!fb) {
2696 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2697 return;
2698 }
2699 }
2700 else
2701 fb = &DummyFramebuffer;
2702
2703 mtx_lock(&ctx->Shared->Mutex);
2704 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, fb);
2705 mtx_unlock(&ctx->Shared->Mutex);
2706 }
2707 }
2708
2709
2710 void GLAPIENTRY
2711 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2712 {
2713 create_framebuffers(n, framebuffers, false);
2714 }
2715
2716
2717 void GLAPIENTRY
2718 _mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2719 {
2720 create_framebuffers(n, framebuffers, true);
2721 }
2722
2723
2724 GLenum
2725 _mesa_check_framebuffer_status(struct gl_context *ctx,
2726 struct gl_framebuffer *buffer)
2727 {
2728 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2729
2730 if (_mesa_is_winsys_fbo(buffer)) {
2731 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2732 if (buffer != &IncompleteFramebuffer) {
2733 return GL_FRAMEBUFFER_COMPLETE_EXT;
2734 } else {
2735 return GL_FRAMEBUFFER_UNDEFINED;
2736 }
2737 }
2738
2739 /* No need to flush here */
2740
2741 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2742 _mesa_test_framebuffer_completeness(ctx, buffer);
2743 }
2744
2745 return buffer->_Status;
2746 }
2747
2748
2749 GLenum GLAPIENTRY
2750 _mesa_CheckFramebufferStatus(GLenum target)
2751 {
2752 struct gl_framebuffer *fb;
2753 GET_CURRENT_CONTEXT(ctx);
2754
2755 if (MESA_VERBOSE & VERBOSE_API)
2756 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2757 _mesa_enum_to_string(target));
2758
2759 fb = get_framebuffer_target(ctx, target);
2760 if (!fb) {
2761 _mesa_error(ctx, GL_INVALID_ENUM,
2762 "glCheckFramebufferStatus(invalid target %s)",
2763 _mesa_enum_to_string(target));
2764 return 0;
2765 }
2766
2767 return _mesa_check_framebuffer_status(ctx, fb);
2768 }
2769
2770
2771 GLenum GLAPIENTRY
2772 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer, GLenum target)
2773 {
2774 struct gl_framebuffer *fb;
2775 GET_CURRENT_CONTEXT(ctx);
2776
2777 /* Validate the target (for conformance's sake) and grab a reference to the
2778 * default framebuffer in case framebuffer = 0.
2779 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2780 * (30.10.2014, PDF page 336) says:
2781 * "If framebuffer is zero, then the status of the default read or
2782 * draw framebuffer (as determined by target) is returned."
2783 */
2784 switch (target) {
2785 case GL_DRAW_FRAMEBUFFER:
2786 case GL_FRAMEBUFFER:
2787 fb = ctx->WinSysDrawBuffer;
2788 break;
2789 case GL_READ_FRAMEBUFFER:
2790 fb = ctx->WinSysReadBuffer;
2791 break;
2792 default:
2793 _mesa_error(ctx, GL_INVALID_ENUM,
2794 "glCheckNamedFramebufferStatus(invalid target %s)",
2795 _mesa_enum_to_string(target));
2796 return 0;
2797 }
2798
2799 if (framebuffer) {
2800 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
2801 "glCheckNamedFramebufferStatus");
2802 if (!fb)
2803 return 0;
2804 }
2805
2806 return _mesa_check_framebuffer_status(ctx, fb);
2807 }
2808
2809
2810 /**
2811 * Replicate the src attachment point. Used by framebuffer_texture() when
2812 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2813 * GL_STENCIL_ATTACHMENT.
2814 */
2815 static void
2816 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2817 gl_buffer_index dst,
2818 gl_buffer_index src)
2819 {
2820 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2821 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2822
2823 assert(src_att->Texture != NULL);
2824 assert(src_att->Renderbuffer != NULL);
2825
2826 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2827 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2828 dst_att->Type = src_att->Type;
2829 dst_att->Complete = src_att->Complete;
2830 dst_att->TextureLevel = src_att->TextureLevel;
2831 dst_att->Zoffset = src_att->Zoffset;
2832 dst_att->Layered = src_att->Layered;
2833 }
2834
2835
2836 /**
2837 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2838 * texture object pointer.
2839 *
2840 * \param texObj where the pointer to the texture object is returned. Note
2841 * that a successful call may return texObj = NULL.
2842 *
2843 * \return true if no errors, false if errors
2844 */
2845 static bool
2846 get_texture_for_framebuffer(struct gl_context *ctx, GLuint texture,
2847 bool layered, const char *caller,
2848 struct gl_texture_object **texObj)
2849 {
2850 *texObj = NULL; /* This will get returned if texture = 0. */
2851
2852 if (!texture)
2853 return true;
2854
2855 *texObj = _mesa_lookup_texture(ctx, texture);
2856 if (*texObj == NULL || (*texObj)->Target == 0) {
2857 /* Can't render to a non-existent texture object.
2858 *
2859 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2860 * Managing Framebuffer Objects specifies a different error
2861 * depending upon the calling function (PDF pages 325-328).
2862 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
2863 * value, while the other commands throw invalid operation (where
2864 * layered = GL_FALSE).
2865 */
2866 const GLenum error = layered ? GL_INVALID_VALUE :
2867 GL_INVALID_OPERATION;
2868 _mesa_error(ctx, error,
2869 "%s(non-existent texture %u)", caller, texture);
2870 return false;
2871 }
2872
2873 return true;
2874 }
2875
2876
2877 /**
2878 * Common code called by gl*FramebufferTexture() to verify the texture target
2879 * and decide whether or not the attachment should truly be considered
2880 * layered.
2881 *
2882 * \param layered true if attachment should be considered layered, false if
2883 * not
2884 *
2885 * \return true if no errors, false if errors
2886 */
2887 static bool
2888 check_layered_texture_target(struct gl_context *ctx, GLenum target,
2889 const char *caller, GLboolean *layered)
2890 {
2891 *layered = GL_TRUE;
2892
2893 switch (target) {
2894 case GL_TEXTURE_3D:
2895 case GL_TEXTURE_1D_ARRAY_EXT:
2896 case GL_TEXTURE_2D_ARRAY_EXT:
2897 case GL_TEXTURE_CUBE_MAP:
2898 case GL_TEXTURE_CUBE_MAP_ARRAY:
2899 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2900 return true;
2901 case GL_TEXTURE_1D:
2902 case GL_TEXTURE_2D:
2903 case GL_TEXTURE_RECTANGLE:
2904 case GL_TEXTURE_2D_MULTISAMPLE:
2905 /* These texture types are valid to pass to
2906 * glFramebufferTexture(), but since they aren't layered, it
2907 * is equivalent to calling glFramebufferTexture{1D,2D}().
2908 */
2909 *layered = GL_FALSE;
2910 return true;
2911 }
2912
2913 _mesa_error(ctx, GL_INVALID_OPERATION,
2914 "%s(invalid texture target %s)", caller,
2915 _mesa_enum_to_string(target));
2916 return false;
2917 }
2918
2919
2920 /**
2921 * Common code called by gl*FramebufferTextureLayer() to verify the texture
2922 * target.
2923 *
2924 * \return true if no errors, false if errors
2925 */
2926 static bool
2927 check_texture_target(struct gl_context *ctx, GLenum target,
2928 const char *caller)
2929 {
2930 /* We're being called by glFramebufferTextureLayer().
2931 * The only legal texture types for that function are 3D,
2932 * cube-map, and 1D/2D/cube-map array textures.
2933 *
2934 * We don't need to check for GL_ARB_texture_cube_map_array because the
2935 * application wouldn't have been able to create a texture with a
2936 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
2937 */
2938 switch (target) {
2939 case GL_TEXTURE_3D:
2940 case GL_TEXTURE_1D_ARRAY:
2941 case GL_TEXTURE_2D_ARRAY:
2942 case GL_TEXTURE_CUBE_MAP_ARRAY:
2943 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2944 return true;
2945 case GL_TEXTURE_CUBE_MAP:
2946 /* We don't need to check the extension (GL_ARB_direct_state_access) or
2947 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
2948 * enabled in core profile. This can be called from
2949 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
2950 * so we do have to check the profile.
2951 */
2952 return ctx->API == API_OPENGL_CORE;
2953 }
2954
2955 _mesa_error(ctx, GL_INVALID_OPERATION,
2956 "%s(invalid texture target %s)", caller,
2957 _mesa_enum_to_string(target));
2958 return false;
2959 }
2960
2961
2962 /**
2963 * Common code called by glFramebufferTexture*D() to verify the texture
2964 * target.
2965 *
2966 * \return true if no errors, false if errors
2967 */
2968 static bool
2969 check_textarget(struct gl_context *ctx, int dims, GLenum target,
2970 GLenum textarget, const char *caller)
2971 {
2972 bool err = false;
2973
2974 switch (dims) {
2975 case 1:
2976 switch (textarget) {
2977 case GL_TEXTURE_1D:
2978 break;
2979 case GL_TEXTURE_1D_ARRAY:
2980 err = !ctx->Extensions.EXT_texture_array;
2981 break;
2982 default:
2983 err = true;
2984 }
2985 break;
2986 case 2:
2987 switch (textarget) {
2988 case GL_TEXTURE_2D:
2989 break;
2990 case GL_TEXTURE_RECTANGLE:
2991 err = _mesa_is_gles(ctx)
2992 || !ctx->Extensions.NV_texture_rectangle;
2993 break;
2994 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2995 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2996 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2997 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2998 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2999 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3000 err = !ctx->Extensions.ARB_texture_cube_map;
3001 break;
3002 case GL_TEXTURE_2D_ARRAY:
3003 err = (_mesa_is_gles(ctx) && ctx->Version < 30)
3004 || !ctx->Extensions.EXT_texture_array;
3005 break;
3006 case GL_TEXTURE_2D_MULTISAMPLE:
3007 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3008 err = (_mesa_is_gles(ctx) ||
3009 !ctx->Extensions.ARB_texture_multisample) &&
3010 !_mesa_is_gles31(ctx);
3011 break;
3012 default:
3013 err = true;
3014 }
3015 break;
3016 case 3:
3017 if (textarget != GL_TEXTURE_3D)
3018 err = true;
3019 break;
3020 default:
3021 err = true;
3022 }
3023
3024 if (err) {
3025 _mesa_error(ctx, GL_INVALID_OPERATION,
3026 "%s(invalid textarget %s)",
3027 caller, _mesa_enum_to_string(textarget));
3028 return false;
3029 }
3030
3031 /* Make sure textarget is consistent with the texture's type */
3032 err = (target == GL_TEXTURE_CUBE_MAP) ?
3033 !_mesa_is_cube_face(textarget): (target != textarget);
3034
3035 if (err) {
3036 _mesa_error(ctx, GL_INVALID_OPERATION,
3037 "%s(mismatched texture target)", caller);
3038 return false;
3039 }
3040
3041 return true;
3042 }
3043
3044
3045 /**
3046 * Common code called by gl*FramebufferTextureLayer() and
3047 * glFramebufferTexture3D() to validate the layer.
3048 *
3049 * \return true if no errors, false if errors
3050 */
3051 static bool
3052 check_layer(struct gl_context *ctx, GLenum target, GLint layer,
3053 const char *caller)
3054 {
3055 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3056 * spec says:
3057 *
3058 * "An INVALID_VALUE error is generated if texture is non-zero
3059 * and layer is negative."
3060 */
3061 if (layer < 0) {
3062 _mesa_error(ctx, GL_INVALID_VALUE,
3063 "%s(layer %u < 0)", caller, layer);
3064 return false;
3065 }
3066
3067 if (target == GL_TEXTURE_3D) {
3068 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
3069 if (layer >= maxSize) {
3070 _mesa_error(ctx, GL_INVALID_VALUE,
3071 "%s(invalid layer %u)", caller, layer);
3072 return false;
3073 }
3074 }
3075 else if ((target == GL_TEXTURE_1D_ARRAY) ||
3076 (target == GL_TEXTURE_2D_ARRAY) ||
3077 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
3078 (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
3079 if (layer >= ctx->Const.MaxArrayTextureLayers) {
3080 _mesa_error(ctx, GL_INVALID_VALUE,
3081 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3082 caller, layer);
3083 return false;
3084 }
3085 }
3086 else if (target == GL_TEXTURE_CUBE_MAP) {
3087 if (layer >= 6) {
3088 _mesa_error(ctx, GL_INVALID_VALUE,
3089 "%s(layer %u >= 6)", caller, layer);
3090 return false;
3091 }
3092 }
3093
3094 return true;
3095 }
3096
3097
3098 /**
3099 * Common code called by all gl*FramebufferTexture*() entry points to verify
3100 * the level.
3101 *
3102 * \return true if no errors, false if errors
3103 */
3104 static bool
3105 check_level(struct gl_context *ctx, GLenum target, GLint level,
3106 const char *caller)
3107 {
3108 if ((level < 0) ||
3109 (level >= _mesa_max_texture_levels(ctx, target))) {
3110 _mesa_error(ctx, GL_INVALID_VALUE,
3111 "%s(invalid level %d)", caller, level);
3112 return false;
3113 }
3114
3115 return true;
3116 }
3117
3118
3119 void
3120 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3121 GLenum attachment,
3122 struct gl_texture_object *texObj, GLenum textarget,
3123 GLint level, GLuint layer, GLboolean layered,
3124 const char *caller)
3125 {
3126 struct gl_renderbuffer_attachment *att;
3127
3128 /* The window-system framebuffer object is immutable */
3129 if (_mesa_is_winsys_fbo(fb)) {
3130 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3131 caller);
3132 return;
3133 }
3134
3135 /* Not a hash lookup, so we can afford to get the attachment here. */
3136 att = get_attachment(ctx, fb, attachment);
3137 if (att == NULL) {
3138 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3139 _mesa_enum_to_string(attachment));
3140 return;
3141 }
3142
3143 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3144
3145 mtx_lock(&fb->Mutex);
3146 if (texObj) {
3147 if (attachment == GL_DEPTH_ATTACHMENT &&
3148 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3149 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3150 _mesa_tex_target_to_face(textarget) ==
3151 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
3152 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
3153 /* The texture object is already attached to the stencil attachment
3154 * point. Don't create a new renderbuffer; just reuse the stencil
3155 * attachment's. This is required to prevent a GL error in
3156 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3157 */
3158 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3159 BUFFER_STENCIL);
3160 } else if (attachment == GL_STENCIL_ATTACHMENT &&
3161 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
3162 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3163 _mesa_tex_target_to_face(textarget) ==
3164 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
3165 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
3166 /* As above, but with depth and stencil transposed. */
3167 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3168 BUFFER_DEPTH);
3169 } else {
3170 set_texture_attachment(ctx, fb, att, texObj, textarget,
3171 level, layer, layered);
3172
3173 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3174 /* Above we created a new renderbuffer and attached it to the
3175 * depth attachment point. Now attach it to the stencil attachment
3176 * point too.
3177 */
3178 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3179 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3180 BUFFER_DEPTH);
3181 }
3182 }
3183
3184 /* Set the render-to-texture flag. We'll check this flag in
3185 * glTexImage() and friends to determine if we need to revalidate
3186 * any FBOs that might be rendering into this texture.
3187 * This flag never gets cleared since it's non-trivial to determine
3188 * when all FBOs might be done rendering to this texture. That's OK
3189 * though since it's uncommon to render to a texture then repeatedly
3190 * call glTexImage() to change images in the texture.
3191 */
3192 texObj->_RenderToTexture = GL_TRUE;
3193 }
3194 else {
3195 remove_attachment(ctx, att);
3196 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3197 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3198 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
3199 }
3200 }
3201
3202 invalidate_framebuffer(fb);
3203
3204 mtx_unlock(&fb->Mutex);
3205 }
3206
3207
3208 static void
3209 framebuffer_texture_with_dims(int dims, GLenum target,
3210 GLenum attachment, GLenum textarget,
3211 GLuint texture, GLint level, GLint layer,
3212 const char *caller)
3213 {
3214 GET_CURRENT_CONTEXT(ctx);
3215 struct gl_framebuffer *fb;
3216 struct gl_texture_object *texObj;
3217
3218 /* Get the framebuffer object */
3219 fb = get_framebuffer_target(ctx, target);
3220 if (!fb) {
3221 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
3222 _mesa_enum_to_string(target));
3223 return;
3224 }
3225
3226 /* Get the texture object */
3227 if (!get_texture_for_framebuffer(ctx, texture, false, caller, &texObj))
3228 return;
3229
3230 if (texObj) {
3231 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3232 return;
3233
3234 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3235 return;
3236 }
3237
3238 if (!check_level(ctx, textarget, level, caller))
3239 return;
3240
3241 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3242 layer, GL_FALSE, caller);
3243 }
3244
3245
3246 void GLAPIENTRY
3247 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3248 GLenum textarget, GLuint texture, GLint level)
3249 {
3250 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3251 level, 0, "glFramebufferTexture1D");
3252 }
3253
3254
3255 void GLAPIENTRY
3256 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
3257 GLenum textarget, GLuint texture, GLint level)
3258 {
3259 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3260 level, 0, "glFramebufferTexture2D");
3261 }
3262
3263
3264 void GLAPIENTRY
3265 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
3266 GLenum textarget, GLuint texture,
3267 GLint level, GLint layer)
3268 {
3269 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3270 level, layer, "glFramebufferTexture3D");
3271 }
3272
3273
3274 void GLAPIENTRY
3275 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3276 GLuint texture, GLint level, GLint layer)
3277 {
3278 GET_CURRENT_CONTEXT(ctx);
3279 struct gl_framebuffer *fb;
3280 struct gl_texture_object *texObj;
3281 GLenum textarget = 0;
3282
3283 const char *func = "glFramebufferTextureLayer";
3284
3285 /* Get the framebuffer object */
3286 fb = get_framebuffer_target(ctx, target);
3287 if (!fb) {
3288 _mesa_error(ctx, GL_INVALID_ENUM,
3289 "glFramebufferTextureLayer(invalid target %s)",
3290 _mesa_enum_to_string(target));
3291 return;
3292 }
3293
3294 /* Get the texture object */
3295 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3296 return;
3297
3298 if (texObj) {
3299 if (!check_texture_target(ctx, texObj->Target, func))
3300 return;
3301
3302 if (!check_layer(ctx, texObj->Target, layer, func))
3303 return;
3304
3305 if (!check_level(ctx, texObj->Target, level, func))
3306 return;
3307
3308 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3309 assert(layer >= 0 && layer < 6);
3310 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3311 layer = 0;
3312 }
3313 }
3314
3315 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3316 layer, GL_FALSE, func);
3317 }
3318
3319
3320 void GLAPIENTRY
3321 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3322 GLuint texture, GLint level, GLint layer)
3323 {
3324 GET_CURRENT_CONTEXT(ctx);
3325 struct gl_framebuffer *fb;
3326 struct gl_texture_object *texObj;
3327 GLenum textarget = 0;
3328
3329 const char *func = "glNamedFramebufferTextureLayer";
3330
3331 /* Get the framebuffer object */
3332 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3333 if (!fb)
3334 return;
3335
3336 /* Get the texture object */
3337 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3338 return;
3339
3340 if (texObj) {
3341 if (!check_texture_target(ctx, texObj->Target, func))
3342 return;
3343
3344 if (!check_layer(ctx, texObj->Target, layer, func))
3345 return;
3346
3347 if (!check_level(ctx, texObj->Target, level, func))
3348 return;
3349
3350 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3351 assert(layer >= 0 && layer < 6);
3352 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3353 layer = 0;
3354 }
3355 }
3356
3357 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3358 layer, GL_FALSE, func);
3359 }
3360
3361
3362 void GLAPIENTRY
3363 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3364 GLuint texture, GLint level)
3365 {
3366 GET_CURRENT_CONTEXT(ctx);
3367 struct gl_framebuffer *fb;
3368 struct gl_texture_object *texObj;
3369 GLboolean layered = GL_FALSE;
3370
3371 const char *func = "FramebufferTexture";
3372
3373 if (!_mesa_has_geometry_shaders(ctx)) {
3374 _mesa_error(ctx, GL_INVALID_OPERATION,
3375 "unsupported function (glFramebufferTexture) called");
3376 return;
3377 }
3378
3379 /* Get the framebuffer object */
3380 fb = get_framebuffer_target(ctx, target);
3381 if (!fb) {
3382 _mesa_error(ctx, GL_INVALID_ENUM,
3383 "glFramebufferTexture(invalid target %s)",
3384 _mesa_enum_to_string(target));
3385 return;
3386 }
3387
3388 /* Get the texture object */
3389 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3390 return;
3391
3392 if (texObj) {
3393 if (!check_layered_texture_target(ctx, texObj->Target, func, &layered))
3394 return;
3395
3396 if (!check_level(ctx, texObj->Target, level, func))
3397 return;
3398 }
3399
3400 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3401 0, layered, func);
3402 }
3403
3404
3405 void GLAPIENTRY
3406 _mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3407 GLuint texture, GLint level)
3408 {
3409 GET_CURRENT_CONTEXT(ctx);
3410 struct gl_framebuffer *fb;
3411 struct gl_texture_object *texObj;
3412 GLboolean layered = GL_FALSE;
3413
3414 const char *func = "glNamedFramebufferTexture";
3415
3416 if (!_mesa_has_geometry_shaders(ctx)) {
3417 _mesa_error(ctx, GL_INVALID_OPERATION,
3418 "unsupported function (glNamedFramebufferTexture) called");
3419 return;
3420 }
3421
3422 /* Get the framebuffer object */
3423 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3424 if (!fb)
3425 return;
3426
3427 /* Get the texture object */
3428 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3429 return;
3430
3431 if (texObj) {
3432 if (!check_layered_texture_target(ctx, texObj->Target, func,
3433 &layered))
3434 return;
3435
3436 if (!check_level(ctx, texObj->Target, level, func))
3437 return;
3438 }
3439
3440 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3441 0, layered, func);
3442 }
3443
3444
3445 void
3446 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3447 struct gl_framebuffer *fb,
3448 GLenum attachment,
3449 struct gl_renderbuffer *rb)
3450 {
3451 assert(!_mesa_is_winsys_fbo(fb));
3452
3453 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3454
3455 assert(ctx->Driver.FramebufferRenderbuffer);
3456 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3457
3458 /* Some subsequent GL commands may depend on the framebuffer's visual
3459 * after the binding is updated. Update visual info now.
3460 */
3461 _mesa_update_framebuffer_visual(ctx, fb);
3462 }
3463
3464 static void
3465 framebuffer_renderbuffer(struct gl_context *ctx,
3466 struct gl_framebuffer *fb,
3467 GLenum attachment,
3468 struct gl_renderbuffer *rb,
3469 const char *func)
3470 {
3471 struct gl_renderbuffer_attachment *att;
3472
3473 if (_mesa_is_winsys_fbo(fb)) {
3474 /* Can't attach new renderbuffers to a window system framebuffer */
3475 _mesa_error(ctx, GL_INVALID_OPERATION,
3476 "%s(window-system framebuffer)", func);
3477 return;
3478 }
3479
3480 att = get_attachment(ctx, fb, attachment);
3481 if (att == NULL) {
3482 _mesa_error(ctx, GL_INVALID_ENUM,
3483 "%s(invalid attachment %s)", func,
3484 _mesa_enum_to_string(attachment));
3485 return;
3486 }
3487
3488 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3489 rb && rb->Format != MESA_FORMAT_NONE) {
3490 /* make sure the renderbuffer is a depth/stencil format */
3491 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3492 if (baseFormat != GL_DEPTH_STENCIL) {
3493 _mesa_error(ctx, GL_INVALID_OPERATION,
3494 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3495 return;
3496 }
3497 }
3498
3499 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
3500 }
3501
3502 void GLAPIENTRY
3503 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3504 GLenum renderbuffertarget,
3505 GLuint renderbuffer)
3506 {
3507 struct gl_framebuffer *fb;
3508 struct gl_renderbuffer *rb;
3509 GET_CURRENT_CONTEXT(ctx);
3510
3511 fb = get_framebuffer_target(ctx, target);
3512 if (!fb) {
3513 _mesa_error(ctx, GL_INVALID_ENUM,
3514 "glFramebufferRenderbuffer(invalid target %s)",
3515 _mesa_enum_to_string(target));
3516 return;
3517 }
3518
3519 if (renderbuffertarget != GL_RENDERBUFFER) {
3520 _mesa_error(ctx, GL_INVALID_ENUM,
3521 "glFramebufferRenderbuffer(renderbuffertarget is not "
3522 "GL_RENDERBUFFER)");
3523 return;
3524 }
3525
3526 if (renderbuffer) {
3527 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3528 "glFramebufferRenderbuffer");
3529 if (!rb)
3530 return;
3531 }
3532 else {
3533 /* remove renderbuffer attachment */
3534 rb = NULL;
3535 }
3536
3537 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3538 "glFramebufferRenderbuffer");
3539 }
3540
3541
3542 void GLAPIENTRY
3543 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3544 GLenum renderbuffertarget,
3545 GLuint renderbuffer)
3546 {
3547 struct gl_framebuffer *fb;
3548 struct gl_renderbuffer *rb;
3549 GET_CURRENT_CONTEXT(ctx);
3550
3551 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3552 "glNamedFramebufferRenderbuffer");
3553 if (!fb)
3554 return;
3555
3556 if (renderbuffertarget != GL_RENDERBUFFER) {
3557 _mesa_error(ctx, GL_INVALID_ENUM,
3558 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3559 "GL_RENDERBUFFER)");
3560 return;
3561 }
3562
3563 if (renderbuffer) {
3564 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3565 "glNamedFramebufferRenderbuffer");
3566 if (!rb)
3567 return;
3568 }
3569 else {
3570 /* remove renderbuffer attachment */
3571 rb = NULL;
3572 }
3573
3574 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3575 "glNamedFramebufferRenderbuffer");
3576 }
3577
3578
3579 void
3580 _mesa_get_framebuffer_attachment_parameter(struct gl_context *ctx,
3581 struct gl_framebuffer *buffer,
3582 GLenum attachment, GLenum pname,
3583 GLint *params, const char *caller)
3584 {
3585 const struct gl_renderbuffer_attachment *att;
3586 GLenum err;
3587
3588 /* The error code for an attachment type of GL_NONE differs between APIs.
3589 *
3590 * From the ES 2.0.25 specification, page 127:
3591 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3592 * querying any other pname will generate INVALID_ENUM."
3593 *
3594 * From the OpenGL 3.0 specification, page 337, or identically,
3595 * the OpenGL ES 3.0.4 specification, page 240:
3596 *
3597 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3598 * framebuffer is bound to target. In this case querying pname
3599 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3600 * queries will generate an INVALID_OPERATION error."
3601 */
3602 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3603 GL_INVALID_ENUM : GL_INVALID_OPERATION;
3604
3605 if (_mesa_is_winsys_fbo(buffer)) {
3606 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3607 * says:
3608 *
3609 * "If the framebuffer currently bound to target is zero, then
3610 * INVALID_OPERATION is generated."
3611 *
3612 * The EXT_framebuffer_object spec has the same wording, and the
3613 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3614 * spec.
3615 */
3616 if ((!_mesa_is_desktop_gl(ctx) ||
3617 !ctx->Extensions.ARB_framebuffer_object)
3618 && !_mesa_is_gles3(ctx)) {
3619 _mesa_error(ctx, GL_INVALID_OPERATION,
3620 "%s(window-system framebuffer)", caller);
3621 return;
3622 }
3623
3624 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3625 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3626 _mesa_error(ctx, GL_INVALID_ENUM,
3627 "%s(invalid attachment %s)", caller,
3628 _mesa_enum_to_string(attachment));
3629 return;
3630 }
3631
3632 /* The specs are not clear about how to handle
3633 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3634 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3635 * discussed in:
3636 *
3637 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3638 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3639 */
3640 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3641 _mesa_error(ctx, GL_INVALID_ENUM,
3642 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3643 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3644 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3645 return;
3646 }
3647
3648 /* the default / window-system FBO */
3649 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
3650 }
3651 else {
3652 /* user-created framebuffer FBO */
3653 att = get_attachment(ctx, buffer, attachment);
3654 }
3655
3656 if (att == NULL) {
3657 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3658 _mesa_enum_to_string(attachment));
3659 return;
3660 }
3661
3662 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3663 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3664 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3665 /* This behavior is first specified in OpenGL 4.4 specification.
3666 *
3667 * From the OpenGL 4.4 spec page 275:
3668 * "This query cannot be performed for a combined depth+stencil
3669 * attachment, since it does not have a single format."
3670 */
3671 _mesa_error(ctx, GL_INVALID_OPERATION,
3672 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3673 " is invalid for depth+stencil attachment)", caller);
3674 return;
3675 }
3676 /* the depth and stencil attachments must point to the same buffer */
3677 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
3678 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
3679 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3680 _mesa_error(ctx, GL_INVALID_OPERATION,
3681 "%s(DEPTH/STENCIL attachments differ)", caller);
3682 return;
3683 }
3684 }
3685
3686 /* No need to flush here */
3687
3688 switch (pname) {
3689 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3690 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3691 *
3692 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3693 * either no framebuffer is bound to target; or the default framebuffer
3694 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3695 * stencil bits, respectively, is zero."
3696 */
3697 *params = (_mesa_is_winsys_fbo(buffer) &&
3698 ((attachment != GL_DEPTH && attachment != GL_STENCIL) ||
3699 (att->Type != GL_NONE)))
3700 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
3701 return;
3702 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3703 if (att->Type == GL_RENDERBUFFER_EXT) {
3704 *params = att->Renderbuffer->Name;
3705 }
3706 else if (att->Type == GL_TEXTURE) {
3707 *params = att->Texture->Name;
3708 }
3709 else {
3710 assert(att->Type == GL_NONE);
3711 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3712 *params = 0;
3713 } else {
3714 goto invalid_pname_enum;
3715 }
3716 }
3717 return;
3718 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3719 if (att->Type == GL_TEXTURE) {
3720 *params = att->TextureLevel;
3721 }
3722 else if (att->Type == GL_NONE) {
3723 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3724 _mesa_enum_to_string(pname));
3725 }
3726 else {
3727 goto invalid_pname_enum;
3728 }
3729 return;
3730 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3731 if (att->Type == GL_TEXTURE) {
3732 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3733 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3734 }
3735 else {
3736 *params = 0;
3737 }
3738 }
3739 else if (att->Type == GL_NONE) {
3740 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3741 _mesa_enum_to_string(pname));
3742 }
3743 else {
3744 goto invalid_pname_enum;
3745 }
3746 return;
3747 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3748 if (ctx->API == API_OPENGLES) {
3749 goto invalid_pname_enum;
3750 } else if (att->Type == GL_NONE) {
3751 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3752 _mesa_enum_to_string(pname));
3753 } else if (att->Type == GL_TEXTURE) {
3754 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3755 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
3756 *params = att->Zoffset;
3757 }
3758 else {
3759 *params = 0;
3760 }
3761 }
3762 else {
3763 goto invalid_pname_enum;
3764 }
3765 return;
3766 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3767 if ((!_mesa_is_desktop_gl(ctx) ||
3768 !ctx->Extensions.ARB_framebuffer_object)
3769 && !_mesa_is_gles3(ctx)) {
3770 goto invalid_pname_enum;
3771 }
3772 else if (att->Type == GL_NONE) {
3773 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3774 _mesa_enum_to_string(pname));
3775 }
3776 else {
3777 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3778 *params =
3779 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
3780 }
3781 else {
3782 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3783 * if the sRGB conversion is unsupported. */
3784 *params = GL_LINEAR;
3785 }
3786 }
3787 return;
3788 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3789 if ((ctx->API != API_OPENGL_COMPAT ||
3790 !ctx->Extensions.ARB_framebuffer_object)
3791 && ctx->API != API_OPENGL_CORE
3792 && !_mesa_is_gles3(ctx)) {
3793 goto invalid_pname_enum;
3794 }
3795 else if (att->Type == GL_NONE) {
3796 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3797 _mesa_enum_to_string(pname));
3798 }
3799 else {
3800 mesa_format format = att->Renderbuffer->Format;
3801
3802 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3803 * 3.0.1 spec says:
3804 *
3805 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3806 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3807 * generate an INVALID_OPERATION error.
3808 */
3809 if (_mesa_is_gles3(ctx) &&
3810 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3811 _mesa_error(ctx, GL_INVALID_OPERATION,
3812 "%s(cannot query "
3813 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3814 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
3815 return;
3816 }
3817
3818 if (format == MESA_FORMAT_S_UINT8) {
3819 /* special cases */
3820 *params = GL_INDEX;
3821 }
3822 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
3823 /* depends on the attachment parameter */
3824 if (attachment == GL_STENCIL_ATTACHMENT) {
3825 *params = GL_INDEX;
3826 }
3827 else {
3828 *params = GL_FLOAT;
3829 }
3830 }
3831 else {
3832 *params = _mesa_get_format_datatype(format);
3833 }
3834 }
3835 return;
3836 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3837 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3838 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3839 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3840 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3841 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3842 if ((!_mesa_is_desktop_gl(ctx) ||
3843 !ctx->Extensions.ARB_framebuffer_object)
3844 && !_mesa_is_gles3(ctx)) {
3845 goto invalid_pname_enum;
3846 }
3847 else if (att->Type == GL_NONE) {
3848 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3849 _mesa_enum_to_string(pname));
3850 }
3851 else if (att->Texture) {
3852 const struct gl_texture_image *texImage =
3853 _mesa_select_tex_image(att->Texture, att->Texture->Target,
3854 att->TextureLevel);
3855 if (texImage) {
3856 *params = get_component_bits(pname, texImage->_BaseFormat,
3857 texImage->TexFormat);
3858 }
3859 else {
3860 *params = 0;
3861 }
3862 }
3863 else if (att->Renderbuffer) {
3864 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
3865 att->Renderbuffer->Format);
3866 }
3867 else {
3868 _mesa_problem(ctx, "%s: invalid FBO attachment structure", caller);
3869 }
3870 return;
3871 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
3872 if (!_mesa_has_geometry_shaders(ctx)) {
3873 goto invalid_pname_enum;
3874 } else if (att->Type == GL_TEXTURE) {
3875 *params = att->Layered;
3876 } else if (att->Type == GL_NONE) {
3877 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3878 _mesa_enum_to_string(pname));
3879 } else {
3880 goto invalid_pname_enum;
3881 }
3882 return;
3883 default:
3884 goto invalid_pname_enum;
3885 }
3886
3887 return;
3888
3889 invalid_pname_enum:
3890 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
3891 _mesa_enum_to_string(pname));
3892 return;
3893 }
3894
3895
3896 void GLAPIENTRY
3897 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
3898 GLenum pname, GLint *params)
3899 {
3900 GET_CURRENT_CONTEXT(ctx);
3901 struct gl_framebuffer *buffer;
3902
3903 buffer = get_framebuffer_target(ctx, target);
3904 if (!buffer) {
3905 _mesa_error(ctx, GL_INVALID_ENUM,
3906 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
3907 _mesa_enum_to_string(target));
3908 return;
3909 }
3910
3911 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3912 params,
3913 "glGetFramebufferAttachmentParameteriv");
3914 }
3915
3916
3917 void GLAPIENTRY
3918 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
3919 GLenum attachment,
3920 GLenum pname, GLint *params)
3921 {
3922 GET_CURRENT_CONTEXT(ctx);
3923 struct gl_framebuffer *buffer;
3924
3925 if (framebuffer) {
3926 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3927 "glGetNamedFramebufferAttachmentParameteriv");
3928 if (!buffer)
3929 return;
3930 }
3931 else {
3932 /*
3933 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
3934 * 4.5 core spec (30.10.2014, PDF page 314):
3935 * "If framebuffer is zero, then the default draw framebuffer is
3936 * queried."
3937 */
3938 buffer = ctx->WinSysDrawBuffer;
3939 }
3940
3941 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3942 params,
3943 "glGetNamedFramebufferAttachmentParameteriv");
3944 }
3945
3946
3947 void GLAPIENTRY
3948 _mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
3949 GLint param)
3950 {
3951 GET_CURRENT_CONTEXT(ctx);
3952 struct gl_framebuffer *fb = NULL;
3953
3954 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3955 _mesa_error(ctx, GL_INVALID_OPERATION,
3956 "glNamedFramebufferParameteri("
3957 "ARB_framebuffer_no_attachments not implemented)");
3958 return;
3959 }
3960
3961 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3962 "glNamedFramebufferParameteri");
3963
3964 if (fb) {
3965 framebuffer_parameteri(ctx, fb, pname, param,
3966 "glNamedFramebufferParameteriv");
3967 }
3968 }
3969
3970
3971 void GLAPIENTRY
3972 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
3973 GLint *param)
3974 {
3975 GET_CURRENT_CONTEXT(ctx);
3976 struct gl_framebuffer *fb;
3977
3978 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3979 _mesa_error(ctx, GL_INVALID_OPERATION,
3980 "glNamedFramebufferParameteriv("
3981 "ARB_framebuffer_no_attachments not implemented)");
3982 return;
3983 }
3984
3985 if (framebuffer) {
3986 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3987 "glGetNamedFramebufferParameteriv");
3988 } else {
3989 fb = ctx->WinSysDrawBuffer;
3990 }
3991
3992 if (fb) {
3993 get_framebuffer_parameteriv(ctx, fb, pname, param,
3994 "glGetNamedFramebufferParameteriv");
3995 }
3996 }
3997
3998
3999 static void
4000 invalidate_framebuffer_storage(struct gl_context *ctx,
4001 struct gl_framebuffer *fb,
4002 GLsizei numAttachments,
4003 const GLenum *attachments, GLint x, GLint y,
4004 GLsizei width, GLsizei height, const char *name)
4005 {
4006 int i;
4007
4008 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4009 * Spec (2.2.2015, PDF page 522) says:
4010 * "An INVALID_VALUE error is generated if numAttachments, width, or
4011 * height is negative."
4012 */
4013 if (numAttachments < 0) {
4014 _mesa_error(ctx, GL_INVALID_VALUE,
4015 "%s(numAttachments < 0)", name);
4016 return;
4017 }
4018
4019 if (width < 0) {
4020 _mesa_error(ctx, GL_INVALID_VALUE,
4021 "%s(width < 0)", name);
4022 return;
4023 }
4024
4025 if (height < 0) {
4026 _mesa_error(ctx, GL_INVALID_VALUE,
4027 "%s(height < 0)", name);
4028 return;
4029 }
4030
4031 /* The GL_ARB_invalidate_subdata spec says:
4032 *
4033 * "If an attachment is specified that does not exist in the
4034 * framebuffer bound to <target>, it is ignored."
4035 *
4036 * It also says:
4037 *
4038 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4039 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4040 * INVALID_OPERATION is generated."
4041 *
4042 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4043 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4044 * set of retrictions).
4045 */
4046 for (i = 0; i < numAttachments; i++) {
4047 if (_mesa_is_winsys_fbo(fb)) {
4048 switch (attachments[i]) {
4049 case GL_ACCUM:
4050 case GL_AUX0:
4051 case GL_AUX1:
4052 case GL_AUX2:
4053 case GL_AUX3:
4054 /* Accumulation buffers and auxilary buffers were removed in
4055 * OpenGL 3.1, and they never existed in OpenGL ES.
4056 */
4057 if (ctx->API != API_OPENGL_COMPAT)
4058 goto invalid_enum;
4059 break;
4060 case GL_COLOR:
4061 case GL_DEPTH:
4062 case GL_STENCIL:
4063 break;
4064 case GL_BACK_LEFT:
4065 case GL_BACK_RIGHT:
4066 case GL_FRONT_LEFT:
4067 case GL_FRONT_RIGHT:
4068 if (!_mesa_is_desktop_gl(ctx))
4069 goto invalid_enum;
4070 break;
4071 default:
4072 goto invalid_enum;
4073 }
4074 } else {
4075 switch (attachments[i]) {
4076 case GL_DEPTH_ATTACHMENT:
4077 case GL_STENCIL_ATTACHMENT:
4078 break;
4079 case GL_DEPTH_STENCIL_ATTACHMENT:
4080 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4081 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4082 * extension does not make this attachment point valid on ES 2.0.
4083 */
4084 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4085 break;
4086 /* fallthrough */
4087 case GL_COLOR_ATTACHMENT0:
4088 case GL_COLOR_ATTACHMENT1:
4089 case GL_COLOR_ATTACHMENT2:
4090 case GL_COLOR_ATTACHMENT3:
4091 case GL_COLOR_ATTACHMENT4:
4092 case GL_COLOR_ATTACHMENT5:
4093 case GL_COLOR_ATTACHMENT6:
4094 case GL_COLOR_ATTACHMENT7:
4095 case GL_COLOR_ATTACHMENT8:
4096 case GL_COLOR_ATTACHMENT9:
4097 case GL_COLOR_ATTACHMENT10:
4098 case GL_COLOR_ATTACHMENT11:
4099 case GL_COLOR_ATTACHMENT12:
4100 case GL_COLOR_ATTACHMENT13:
4101 case GL_COLOR_ATTACHMENT14:
4102 case GL_COLOR_ATTACHMENT15: {
4103 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
4104 if (k >= ctx->Const.MaxColorAttachments) {
4105 _mesa_error(ctx, GL_INVALID_OPERATION,
4106 "%s(attachment >= max. color attachments)", name);
4107 return;
4108 }
4109 break;
4110 }
4111 default:
4112 goto invalid_enum;
4113 }
4114 }
4115 }
4116
4117 /* We don't actually do anything for this yet. Just return after
4118 * validating the parameters and generating the required errors.
4119 */
4120 return;
4121
4122 invalid_enum:
4123 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
4124 _mesa_enum_to_string(attachments[i]));
4125 return;
4126 }
4127
4128
4129 void GLAPIENTRY
4130 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4131 const GLenum *attachments, GLint x, GLint y,
4132 GLsizei width, GLsizei height)
4133 {
4134 struct gl_framebuffer *fb;
4135 GET_CURRENT_CONTEXT(ctx);
4136
4137 fb = get_framebuffer_target(ctx, target);
4138 if (!fb) {
4139 _mesa_error(ctx, GL_INVALID_ENUM,
4140 "glInvalidateSubFramebuffer(invalid target %s)",
4141 _mesa_enum_to_string(target));
4142 return;
4143 }
4144
4145 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4146 x, y, width, height,
4147 "glInvalidateSubFramebuffer");
4148 }
4149
4150
4151 void GLAPIENTRY
4152 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4153 GLsizei numAttachments,
4154 const GLenum *attachments,
4155 GLint x, GLint y,
4156 GLsizei width, GLsizei height)
4157 {
4158 struct gl_framebuffer *fb;
4159 GET_CURRENT_CONTEXT(ctx);
4160
4161 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4162 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4163 * default draw framebuffer is affected."
4164 */
4165 if (framebuffer) {
4166 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4167 "glInvalidateNamedFramebufferSubData");
4168 if (!fb)
4169 return;
4170 }
4171 else
4172 fb = ctx->WinSysDrawBuffer;
4173
4174 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4175 x, y, width, height,
4176 "glInvalidateNamedFramebufferSubData");
4177 }
4178
4179
4180 void GLAPIENTRY
4181 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4182 const GLenum *attachments)
4183 {
4184 struct gl_framebuffer *fb;
4185 GET_CURRENT_CONTEXT(ctx);
4186
4187 fb = get_framebuffer_target(ctx, target);
4188 if (!fb) {
4189 _mesa_error(ctx, GL_INVALID_ENUM,
4190 "glInvalidateFramebuffer(invalid target %s)",
4191 _mesa_enum_to_string(target));
4192 return;
4193 }
4194
4195 /* The GL_ARB_invalidate_subdata spec says:
4196 *
4197 * "The command
4198 *
4199 * void InvalidateFramebuffer(enum target,
4200 * sizei numAttachments,
4201 * const enum *attachments);
4202 *
4203 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4204 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4205 * <MAX_VIEWPORT_DIMS[1]> respectively."
4206 */
4207 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4208 0, 0,
4209 ctx->Const.MaxViewportWidth,
4210 ctx->Const.MaxViewportHeight,
4211 "glInvalidateFramebuffer");
4212 }
4213
4214
4215 void GLAPIENTRY
4216 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4217 GLsizei numAttachments,
4218 const GLenum *attachments)
4219 {
4220 struct gl_framebuffer *fb;
4221 GET_CURRENT_CONTEXT(ctx);
4222
4223 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4224 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4225 * default draw framebuffer is affected."
4226 */
4227 if (framebuffer) {
4228 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4229 "glInvalidateNamedFramebufferData");
4230 if (!fb)
4231 return;
4232 }
4233 else
4234 fb = ctx->WinSysDrawBuffer;
4235
4236 /* The GL_ARB_invalidate_subdata spec says:
4237 *
4238 * "The command
4239 *
4240 * void InvalidateFramebuffer(enum target,
4241 * sizei numAttachments,
4242 * const enum *attachments);
4243 *
4244 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4245 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4246 * <MAX_VIEWPORT_DIMS[1]> respectively."
4247 */
4248 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4249 0, 0,
4250 ctx->Const.MaxViewportWidth,
4251 ctx->Const.MaxViewportHeight,
4252 "glInvalidateNamedFramebufferData");
4253 }
4254
4255
4256 void GLAPIENTRY
4257 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4258 const GLenum *attachments)
4259 {
4260 struct gl_framebuffer *fb;
4261 GLint i;
4262
4263 GET_CURRENT_CONTEXT(ctx);
4264
4265 fb = get_framebuffer_target(ctx, target);
4266 if (!fb) {
4267 _mesa_error(ctx, GL_INVALID_ENUM,
4268 "glDiscardFramebufferEXT(target %s)",
4269 _mesa_enum_to_string(target));
4270 return;
4271 }
4272
4273 if (numAttachments < 0) {
4274 _mesa_error(ctx, GL_INVALID_VALUE,
4275 "glDiscardFramebufferEXT(numAttachments < 0)");
4276 return;
4277 }
4278
4279 for (i = 0; i < numAttachments; i++) {
4280 switch (attachments[i]) {
4281 case GL_COLOR:
4282 case GL_DEPTH:
4283 case GL_STENCIL:
4284 if (_mesa_is_user_fbo(fb))
4285 goto invalid_enum;
4286 break;
4287 case GL_COLOR_ATTACHMENT0:
4288 case GL_DEPTH_ATTACHMENT:
4289 case GL_STENCIL_ATTACHMENT:
4290 if (_mesa_is_winsys_fbo(fb))
4291 goto invalid_enum;
4292 break;
4293 default:
4294 goto invalid_enum;
4295 }
4296 }
4297
4298 if (ctx->Driver.DiscardFramebuffer)
4299 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4300
4301 return;
4302
4303 invalid_enum:
4304 _mesa_error(ctx, GL_INVALID_ENUM,
4305 "glDiscardFramebufferEXT(attachment %s)",
4306 _mesa_enum_to_string(attachments[i]));
4307 }