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