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