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