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