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