2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * GL_EXT/ARB_framebuffer_object extensions
38 #include "debug_output.h"
42 #include "framebuffer.h"
43 #include "glformats.h"
46 #include "multisample.h"
48 #include "renderbuffer.h"
57 * None of the GL_EXT_framebuffer_object functions are compiled into
64 * When glGenRender/FramebuffersEXT() is called we insert pointers to
65 * these placeholder objects into the hash table.
66 * Later, when the object ID is first bound, we replace the placeholder
67 * with the real frame/renderbuffer.
69 static struct gl_framebuffer DummyFramebuffer
;
70 static struct gl_renderbuffer DummyRenderbuffer
;
72 /* We bind this framebuffer when applications pass a NULL
73 * drawable/surface in make current. */
74 static struct gl_framebuffer IncompleteFramebuffer
;
78 delete_dummy_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
84 delete_dummy_framebuffer(struct gl_framebuffer
*fb
)
91 _mesa_init_fbobjects(struct gl_context
*ctx
)
93 simple_mtx_init(&DummyFramebuffer
.Mutex
, mtx_plain
);
94 simple_mtx_init(&DummyRenderbuffer
.Mutex
, mtx_plain
);
95 simple_mtx_init(&IncompleteFramebuffer
.Mutex
, mtx_plain
);
96 DummyFramebuffer
.Delete
= delete_dummy_framebuffer
;
97 DummyRenderbuffer
.Delete
= delete_dummy_renderbuffer
;
98 IncompleteFramebuffer
.Delete
= delete_dummy_framebuffer
;
101 struct gl_framebuffer
*
102 _mesa_get_incomplete_framebuffer(void)
104 return &IncompleteFramebuffer
;
108 * Helper routine for getting a gl_renderbuffer.
110 struct gl_renderbuffer
*
111 _mesa_lookup_renderbuffer(struct gl_context
*ctx
, GLuint id
)
113 struct gl_renderbuffer
*rb
;
118 rb
= (struct gl_renderbuffer
*)
119 _mesa_HashLookup(ctx
->Shared
->RenderBuffers
, id
);
125 * A convenience function for direct state access that throws
126 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
128 struct gl_renderbuffer
*
129 _mesa_lookup_renderbuffer_err(struct gl_context
*ctx
, GLuint id
,
132 struct gl_renderbuffer
*rb
;
134 rb
= _mesa_lookup_renderbuffer(ctx
, id
);
135 if (!rb
|| rb
== &DummyRenderbuffer
) {
136 _mesa_error(ctx
, GL_INVALID_OPERATION
,
137 "%s(non-existent renderbuffer %u)", func
, id
);
146 * Helper routine for getting a gl_framebuffer.
148 struct gl_framebuffer
*
149 _mesa_lookup_framebuffer(struct gl_context
*ctx
, GLuint id
)
151 struct gl_framebuffer
*fb
;
156 fb
= (struct gl_framebuffer
*)
157 _mesa_HashLookup(ctx
->Shared
->FrameBuffers
, id
);
163 struct gl_framebuffer
*
164 _mesa_lookup_framebuffer_dsa(struct gl_context
*ctx
, GLuint id
,
167 struct gl_framebuffer
*fb
;
172 fb
= _mesa_lookup_framebuffer(ctx
, id
);
174 /* Name exists but buffer is not initialized */
175 if (fb
== &DummyFramebuffer
) {
176 fb
= ctx
->Driver
.NewFramebuffer(ctx
, id
);
177 _mesa_HashLockMutex(ctx
->Shared
->FrameBuffers
);
178 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, id
, fb
);
179 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
181 /* Name doesn't exist */
183 _mesa_HashLockMutex(ctx
->Shared
->FrameBuffers
);
184 fb
= ctx
->Driver
.NewFramebuffer(ctx
, id
);
186 _mesa_HashUnlockMutex(ctx
->Shared
->FrameBuffers
);
187 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
190 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, id
, fb
);
191 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
198 * A convenience function for direct state access that throws
199 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
201 struct gl_framebuffer
*
202 _mesa_lookup_framebuffer_err(struct gl_context
*ctx
, GLuint id
,
205 struct gl_framebuffer
*fb
;
207 fb
= _mesa_lookup_framebuffer(ctx
, id
);
208 if (!fb
|| fb
== &DummyFramebuffer
) {
209 _mesa_error(ctx
, GL_INVALID_OPERATION
,
210 "%s(non-existent framebuffer %u)", func
, id
);
219 * Mark the given framebuffer as invalid. This will force the
220 * test for framebuffer completeness to be done before the framebuffer
224 invalidate_framebuffer(struct gl_framebuffer
*fb
)
226 fb
->_Status
= 0; /* "indeterminate" */
231 * Return the gl_framebuffer object which corresponds to the given
232 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
233 * Check support for GL_EXT_framebuffer_blit to determine if certain
235 * \return gl_framebuffer pointer or NULL if target is illegal
237 static struct gl_framebuffer
*
238 get_framebuffer_target(struct gl_context
*ctx
, GLenum target
)
240 bool have_fb_blit
= _mesa_is_gles3(ctx
) || _mesa_is_desktop_gl(ctx
);
242 case GL_DRAW_FRAMEBUFFER
:
243 return have_fb_blit
? ctx
->DrawBuffer
: NULL
;
244 case GL_READ_FRAMEBUFFER
:
245 return have_fb_blit
? ctx
->ReadBuffer
: NULL
;
246 case GL_FRAMEBUFFER_EXT
:
247 return ctx
->DrawBuffer
;
255 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
256 * gl_renderbuffer_attachment object.
257 * This function is only used for user-created FB objects, not the
258 * default / window-system FB object.
259 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
260 * the depth buffer attachment point.
261 * Returns if the attachment is a GL_COLOR_ATTACHMENTm_EXT on
262 * is_color_attachment, because several callers would return different errors
263 * if they don't find the attachment.
265 static struct gl_renderbuffer_attachment
*
266 get_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
267 GLenum attachment
, bool *is_color_attachment
)
271 assert(_mesa_is_user_fbo(fb
));
273 if (is_color_attachment
)
274 *is_color_attachment
= false;
276 switch (attachment
) {
277 case GL_COLOR_ATTACHMENT0_EXT
:
278 case GL_COLOR_ATTACHMENT1_EXT
:
279 case GL_COLOR_ATTACHMENT2_EXT
:
280 case GL_COLOR_ATTACHMENT3_EXT
:
281 case GL_COLOR_ATTACHMENT4_EXT
:
282 case GL_COLOR_ATTACHMENT5_EXT
:
283 case GL_COLOR_ATTACHMENT6_EXT
:
284 case GL_COLOR_ATTACHMENT7_EXT
:
285 case GL_COLOR_ATTACHMENT8_EXT
:
286 case GL_COLOR_ATTACHMENT9_EXT
:
287 case GL_COLOR_ATTACHMENT10_EXT
:
288 case GL_COLOR_ATTACHMENT11_EXT
:
289 case GL_COLOR_ATTACHMENT12_EXT
:
290 case GL_COLOR_ATTACHMENT13_EXT
:
291 case GL_COLOR_ATTACHMENT14_EXT
:
292 case GL_COLOR_ATTACHMENT15_EXT
:
293 if (is_color_attachment
)
294 *is_color_attachment
= true;
295 /* Only OpenGL ES 1.x forbids color attachments other than
296 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
299 i
= attachment
- GL_COLOR_ATTACHMENT0_EXT
;
300 if (i
>= ctx
->Const
.MaxColorAttachments
301 || (i
> 0 && ctx
->API
== API_OPENGLES
)) {
304 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
305 case GL_DEPTH_STENCIL_ATTACHMENT
:
306 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
))
309 case GL_DEPTH_ATTACHMENT_EXT
:
310 return &fb
->Attachment
[BUFFER_DEPTH
];
311 case GL_STENCIL_ATTACHMENT_EXT
:
312 return &fb
->Attachment
[BUFFER_STENCIL
];
320 * As above, but only used for getting attachments of the default /
321 * window-system framebuffer (not user-created framebuffer objects).
323 static struct gl_renderbuffer_attachment
*
324 get_fb0_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
327 assert(_mesa_is_winsys_fbo(fb
));
329 if (_mesa_is_gles3(ctx
)) {
330 assert(attachment
== GL_BACK
||
331 attachment
== GL_DEPTH
||
332 attachment
== GL_STENCIL
);
333 switch (attachment
) {
335 /* Since there is no stereo rendering in ES 3.0, only return the
338 if (ctx
->DrawBuffer
->Visual
.doubleBufferMode
)
339 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
340 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
342 return &fb
->Attachment
[BUFFER_DEPTH
];
344 return &fb
->Attachment
[BUFFER_STENCIL
];
348 switch (attachment
) {
350 /* Front buffers can be allocated on the first use, but
351 * glGetFramebufferAttachmentParameteriv must work even if that
352 * allocation hasn't happened yet. In such case, use the back buffer,
353 * which should be the same.
355 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Type
== GL_NONE
)
356 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
358 return &fb
->Attachment
[BUFFER_FRONT_LEFT
];
361 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Type
== GL_NONE
)
362 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
364 return &fb
->Attachment
[BUFFER_FRONT_RIGHT
];
366 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
368 return &fb
->Attachment
[BUFFER_BACK_RIGHT
];
370 /* The ARB_ES3_1_compatibility spec says:
372 * "Since this command can only query a single framebuffer
373 * attachment, BACK is equivalent to BACK_LEFT."
375 if (ctx
->Extensions
.ARB_ES3_1_compatibility
)
376 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
379 if (fb
->Visual
.numAuxBuffers
== 1) {
380 return &fb
->Attachment
[BUFFER_AUX0
];
384 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
386 * "If the default framebuffer is bound to target, then attachment must
387 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
388 * identifying a color buffer; DEPTH, identifying the depth buffer; or
389 * STENCIL, identifying the stencil buffer."
391 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
392 * language. However, revision #33 of the ARB_framebuffer_object spec
395 * "If the default framebuffer is bound to <target>, then <attachment>
396 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
397 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
398 * depth buffer, or the stencil buffer, and <pname> may be
399 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
400 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
402 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
403 * from glext.h, so shipping apps should not use those values.
405 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
406 * support queries of the window system FBO.
409 return &fb
->Attachment
[BUFFER_DEPTH
];
411 return &fb
->Attachment
[BUFFER_STENCIL
];
420 * Remove any texture or renderbuffer attached to the given attachment
421 * point. Update reference counts, etc.
424 remove_attachment(struct gl_context
*ctx
,
425 struct gl_renderbuffer_attachment
*att
)
427 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
429 /* tell driver that we're done rendering to this texture. */
430 if (rb
&& rb
->NeedsFinishRenderTexture
)
431 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
433 if (att
->Type
== GL_TEXTURE
) {
434 assert(att
->Texture
);
435 _mesa_reference_texobj(&att
->Texture
, NULL
); /* unbind */
436 assert(!att
->Texture
);
438 if (att
->Type
== GL_TEXTURE
|| att
->Type
== GL_RENDERBUFFER_EXT
) {
439 assert(!att
->Texture
);
440 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
); /* unbind */
441 assert(!att
->Renderbuffer
);
444 att
->Complete
= GL_TRUE
;
448 * Verify a couple error conditions that will lead to an incomplete FBO and
449 * may cause problems for the driver's RenderTexture path.
452 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment
*att
)
454 const struct gl_texture_image
*const texImage
=
455 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
458 texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
461 if ((texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
462 && att
->Zoffset
>= texImage
->Height
)
463 || (texImage
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
464 && att
->Zoffset
>= texImage
->Depth
))
471 * Create a renderbuffer which will be set up by the driver to wrap the
472 * texture image slice.
474 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
475 * to share most of their framebuffer rendering code between winsys,
476 * renderbuffer, and texture attachments.
478 * The allocated renderbuffer uses a non-zero Name so that drivers can check
479 * it for determining vertical orientation, but we use ~0 to make it fairly
480 * unambiguous with actual user (non-texture) renderbuffers.
483 _mesa_update_texture_renderbuffer(struct gl_context
*ctx
,
484 struct gl_framebuffer
*fb
,
485 struct gl_renderbuffer_attachment
*att
)
487 struct gl_texture_image
*texImage
;
488 struct gl_renderbuffer
*rb
;
490 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
492 rb
= att
->Renderbuffer
;
494 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, ~0);
496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
499 att
->Renderbuffer
= rb
;
501 /* This can't get called on a texture renderbuffer, so set it to NULL
502 * for clarity compared to user renderbuffers.
504 rb
->AllocStorage
= NULL
;
506 rb
->NeedsFinishRenderTexture
= ctx
->Driver
.FinishRenderTexture
!= NULL
;
512 rb
->_BaseFormat
= texImage
->_BaseFormat
;
513 rb
->Format
= texImage
->TexFormat
;
514 rb
->InternalFormat
= texImage
->InternalFormat
;
515 rb
->Width
= texImage
->Width2
;
516 rb
->Height
= texImage
->Height2
;
517 rb
->Depth
= texImage
->Depth2
;
518 rb
->NumSamples
= texImage
->NumSamples
;
519 rb
->NumStorageSamples
= texImage
->NumSamples
;
520 rb
->TexImage
= texImage
;
522 if (driver_RenderTexture_is_safe(att
))
523 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
527 * Bind a texture object to an attachment point.
528 * The previous binding, if any, will be removed first.
531 set_texture_attachment(struct gl_context
*ctx
,
532 struct gl_framebuffer
*fb
,
533 struct gl_renderbuffer_attachment
*att
,
534 struct gl_texture_object
*texObj
,
535 GLenum texTarget
, GLuint level
, GLsizei samples
,
536 GLuint layer
, GLboolean layered
)
538 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
540 if (rb
&& rb
->NeedsFinishRenderTexture
)
541 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
543 if (att
->Texture
== texObj
) {
544 /* re-attaching same texture */
545 assert(att
->Type
== GL_TEXTURE
);
549 remove_attachment(ctx
, att
);
550 att
->Type
= GL_TEXTURE
;
551 assert(!att
->Texture
);
552 _mesa_reference_texobj(&att
->Texture
, texObj
);
554 invalidate_framebuffer(fb
);
556 /* always update these fields */
557 att
->TextureLevel
= level
;
558 att
->NumSamples
= samples
;
559 att
->CubeMapFace
= _mesa_tex_target_to_face(texTarget
);
560 att
->Zoffset
= layer
;
561 att
->Layered
= layered
;
562 att
->Complete
= GL_FALSE
;
564 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
569 * Bind a renderbuffer to an attachment point.
570 * The previous binding, if any, will be removed first.
573 set_renderbuffer_attachment(struct gl_context
*ctx
,
574 struct gl_renderbuffer_attachment
*att
,
575 struct gl_renderbuffer
*rb
)
577 /* XXX check if re-doing same attachment, exit early */
578 remove_attachment(ctx
, att
);
579 att
->Type
= GL_RENDERBUFFER_EXT
;
580 att
->Texture
= NULL
; /* just to be safe */
581 att
->Layered
= GL_FALSE
;
582 att
->Complete
= GL_FALSE
;
583 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
588 * Fallback for ctx->Driver.FramebufferRenderbuffer()
589 * Attach a renderbuffer object to a framebuffer object.
592 _mesa_FramebufferRenderbuffer_sw(struct gl_context
*ctx
,
593 struct gl_framebuffer
*fb
,
595 struct gl_renderbuffer
*rb
)
597 struct gl_renderbuffer_attachment
*att
;
599 simple_mtx_lock(&fb
->Mutex
);
601 att
= get_attachment(ctx
, fb
, attachment
, NULL
);
604 set_renderbuffer_attachment(ctx
, att
, rb
);
605 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
606 /* do stencil attachment here (depth already done above) */
607 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
, NULL
);
609 set_renderbuffer_attachment(ctx
, att
, rb
);
611 rb
->AttachedAnytime
= GL_TRUE
;
614 remove_attachment(ctx
, att
);
615 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
616 /* detach stencil (depth was detached above) */
617 att
= get_attachment(ctx
, fb
, GL_STENCIL_ATTACHMENT_EXT
, NULL
);
619 remove_attachment(ctx
, att
);
623 invalidate_framebuffer(fb
);
625 simple_mtx_unlock(&fb
->Mutex
);
630 * Fallback for ctx->Driver.ValidateFramebuffer()
631 * Check if the renderbuffer's formats are supported by the software
633 * Drivers should probably override this.
636 _mesa_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
639 for (buf
= 0; buf
< BUFFER_COUNT
; buf
++) {
640 const struct gl_renderbuffer
*rb
= fb
->Attachment
[buf
].Renderbuffer
;
642 switch (rb
->_BaseFormat
) {
644 case GL_LUMINANCE_ALPHA
:
649 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
653 switch (rb
->Format
) {
654 /* XXX This list is likely incomplete. */
655 case MESA_FORMAT_R9G9B9E5_FLOAT
:
656 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
659 /* render buffer format is supported by software rendering */
668 * Return true if the framebuffer has a combined depth/stencil
669 * renderbuffer attached.
672 _mesa_has_depthstencil_combined(const struct gl_framebuffer
*fb
)
674 const struct gl_renderbuffer_attachment
*depth
=
675 &fb
->Attachment
[BUFFER_DEPTH
];
676 const struct gl_renderbuffer_attachment
*stencil
=
677 &fb
->Attachment
[BUFFER_STENCIL
];
679 if (depth
->Type
== stencil
->Type
) {
680 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
681 depth
->Renderbuffer
== stencil
->Renderbuffer
)
684 if (depth
->Type
== GL_TEXTURE
&&
685 depth
->Texture
== stencil
->Texture
)
697 att_incomplete(const char *msg
)
699 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
700 _mesa_debug(NULL
, "attachment incomplete: %s\n", msg
);
709 fbo_incomplete(struct gl_context
*ctx
, const char *msg
, int index
)
711 static GLuint msg_id
;
713 _mesa_gl_debugf(ctx
, &msg_id
,
714 MESA_DEBUG_SOURCE_API
,
715 MESA_DEBUG_TYPE_OTHER
,
716 MESA_DEBUG_SEVERITY_MEDIUM
,
717 "FBO incomplete: %s [%d]\n", msg
, index
);
719 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_FBO
) {
720 _mesa_debug(NULL
, "FBO Incomplete: %s [%d]\n", msg
, index
);
726 * Is the given base format a legal format for a color renderbuffer?
729 _mesa_is_legal_color_format(const struct gl_context
*ctx
, GLenum baseFormat
)
731 switch (baseFormat
) {
736 case GL_LUMINANCE_ALPHA
:
739 return ctx
->API
== API_OPENGL_COMPAT
&&
740 ctx
->Extensions
.ARB_framebuffer_object
;
743 return ctx
->Extensions
.ARB_texture_rg
;
751 * Is the given base format a legal format for a color renderbuffer?
754 is_format_color_renderable(const struct gl_context
*ctx
, mesa_format format
,
755 GLenum internalFormat
)
757 const GLenum baseFormat
=
758 _mesa_get_format_base_format(format
);
761 valid
= _mesa_is_legal_color_format(ctx
, baseFormat
);
762 if (!valid
|| _mesa_is_desktop_gl(ctx
)) {
766 /* Reject additional cases for GLES */
767 switch (internalFormat
) {
771 return _mesa_has_EXT_render_snorm(ctx
);
774 case GL_RGBA16_SNORM
:
775 return _mesa_has_EXT_texture_norm16(ctx
) &&
776 _mesa_has_EXT_render_snorm(ctx
);
795 if (internalFormat
!= GL_RGB10_A2
&&
796 (format
== MESA_FORMAT_B10G10R10A2_UNORM
||
797 format
== MESA_FORMAT_B10G10R10X2_UNORM
||
798 format
== MESA_FORMAT_R10G10B10A2_UNORM
||
799 format
== MESA_FORMAT_R10G10B10X2_UNORM
)) {
808 * Is the given base format a legal format for a depth/stencil renderbuffer?
811 is_legal_depth_format(const struct gl_context
*ctx
, GLenum baseFormat
)
813 switch (baseFormat
) {
814 case GL_DEPTH_COMPONENT
:
815 case GL_DEPTH_STENCIL_EXT
:
824 * Test if an attachment point is complete and update its Complete field.
825 * \param format if GL_COLOR, this is a color attachment point,
826 * if GL_DEPTH, this is a depth component attachment point,
827 * if GL_STENCIL, this is a stencil component attachment point.
830 test_attachment_completeness(const struct gl_context
*ctx
, GLenum format
,
831 struct gl_renderbuffer_attachment
*att
)
833 assert(format
== GL_COLOR
|| format
== GL_DEPTH
|| format
== GL_STENCIL
);
835 /* assume complete */
836 att
->Complete
= GL_TRUE
;
838 /* Look for reasons why the attachment might be incomplete */
839 if (att
->Type
== GL_TEXTURE
) {
840 const struct gl_texture_object
*texObj
= att
->Texture
;
841 const struct gl_texture_image
*texImage
;
845 att_incomplete("no texobj");
846 att
->Complete
= GL_FALSE
;
850 texImage
= texObj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
852 att_incomplete("no teximage");
853 att
->Complete
= GL_FALSE
;
856 if (texImage
->Width
< 1 || texImage
->Height
< 1) {
857 att_incomplete("teximage width/height=0");
858 att
->Complete
= GL_FALSE
;
862 switch (texObj
->Target
) {
864 if (att
->Zoffset
>= texImage
->Depth
) {
865 att_incomplete("bad z offset");
866 att
->Complete
= GL_FALSE
;
870 case GL_TEXTURE_1D_ARRAY
:
871 if (att
->Zoffset
>= texImage
->Height
) {
872 att_incomplete("bad 1D-array layer");
873 att
->Complete
= GL_FALSE
;
877 case GL_TEXTURE_2D_ARRAY
:
878 if (att
->Zoffset
>= texImage
->Depth
) {
879 att_incomplete("bad 2D-array layer");
880 att
->Complete
= GL_FALSE
;
884 case GL_TEXTURE_CUBE_MAP_ARRAY
:
885 if (att
->Zoffset
>= texImage
->Depth
) {
886 att_incomplete("bad cube-array layer");
887 att
->Complete
= GL_FALSE
;
893 baseFormat
= texImage
->_BaseFormat
;
895 if (format
== GL_COLOR
) {
896 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
897 att_incomplete("bad format");
898 att
->Complete
= GL_FALSE
;
901 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
902 att_incomplete("compressed internalformat");
903 att
->Complete
= GL_FALSE
;
907 /* OES_texture_float allows creation and use of floating point
908 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
909 * these textures to be used as a render target, this is done via
910 * GL_EXT_color_buffer(_half)_float with set of new sized types.
912 if (_mesa_is_gles(ctx
) && (texObj
->_IsFloat
|| texObj
->_IsHalfFloat
)) {
913 att_incomplete("bad internal format");
914 att
->Complete
= GL_FALSE
;
918 else if (format
== GL_DEPTH
) {
919 if (baseFormat
== GL_DEPTH_COMPONENT
) {
922 else if (ctx
->Extensions
.ARB_depth_texture
&&
923 baseFormat
== GL_DEPTH_STENCIL
) {
927 att
->Complete
= GL_FALSE
;
928 att_incomplete("bad depth format");
933 assert(format
== GL_STENCIL
);
934 if (ctx
->Extensions
.ARB_depth_texture
&&
935 baseFormat
== GL_DEPTH_STENCIL
) {
937 } else if (ctx
->Extensions
.ARB_texture_stencil8
&&
938 baseFormat
== GL_STENCIL_INDEX
) {
941 /* no such thing as stencil-only textures */
942 att_incomplete("illegal stencil texture");
943 att
->Complete
= GL_FALSE
;
948 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
949 const GLenum baseFormat
= att
->Renderbuffer
->_BaseFormat
;
951 assert(att
->Renderbuffer
);
952 if (!att
->Renderbuffer
->InternalFormat
||
953 att
->Renderbuffer
->Width
< 1 ||
954 att
->Renderbuffer
->Height
< 1) {
955 att_incomplete("0x0 renderbuffer");
956 att
->Complete
= GL_FALSE
;
959 if (format
== GL_COLOR
) {
960 if (!_mesa_is_legal_color_format(ctx
, baseFormat
)) {
961 att_incomplete("bad renderbuffer color format");
962 att
->Complete
= GL_FALSE
;
966 else if (format
== GL_DEPTH
) {
967 if (baseFormat
== GL_DEPTH_COMPONENT
) {
970 else if (baseFormat
== GL_DEPTH_STENCIL
) {
974 att_incomplete("bad renderbuffer depth format");
975 att
->Complete
= GL_FALSE
;
980 assert(format
== GL_STENCIL
);
981 if (baseFormat
== GL_STENCIL_INDEX
||
982 baseFormat
== GL_DEPTH_STENCIL
) {
986 att
->Complete
= GL_FALSE
;
987 att_incomplete("bad renderbuffer stencil format");
993 assert(att
->Type
== GL_NONE
);
1001 * Test if the given framebuffer object is complete and update its
1002 * Status field with the results.
1003 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
1004 * driver to make hardware-specific validation/completeness checks.
1005 * Also update the framebuffer's Width and Height fields if the
1006 * framebuffer is complete.
1009 _mesa_test_framebuffer_completeness(struct gl_context
*ctx
,
1010 struct gl_framebuffer
*fb
)
1013 GLenum intFormat
= GL_NONE
; /* color buffers' internal format */
1014 GLuint minWidth
= ~0, minHeight
= ~0, maxWidth
= 0, maxHeight
= 0;
1015 GLint numColorSamples
= -1;
1016 GLint numColorStorageSamples
= -1;
1017 GLint numDepthSamples
= -1;
1018 GLint fixedSampleLocations
= -1;
1021 /* Covers max_layer_count, is_layered, and layer_tex_target */
1022 bool layer_info_valid
= false;
1023 GLuint max_layer_count
= 0, att_layer_count
;
1024 bool is_layered
= false;
1025 GLenum layer_tex_target
= 0;
1026 bool has_depth_attachment
= false;
1027 bool has_stencil_attachment
= false;
1029 assert(_mesa_is_user_fbo(fb
));
1031 /* we're changing framebuffer fields here */
1032 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1037 fb
->_AllColorBuffersFixedPoint
= GL_TRUE
;
1038 fb
->_HasSNormOrFloatColorBuffer
= GL_FALSE
;
1039 fb
->_HasAttachments
= true;
1040 fb
->_IntegerBuffers
= 0;
1041 fb
->_RGBBuffers
= 0;
1042 fb
->_FP32Buffers
= 0;
1044 /* Start at -2 to more easily loop over all attachment points.
1046 * -1: stencil buffer
1049 for (i
= -2; i
< (GLint
) ctx
->Const
.MaxColorAttachments
; i
++) {
1050 struct gl_renderbuffer_attachment
*att
;
1053 mesa_format attFormat
;
1054 GLenum att_tex_target
= GL_NONE
;
1057 * XXX for ARB_fbo, only check color buffers that are named by
1058 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
1061 /* check for attachment completeness
1064 att
= &fb
->Attachment
[BUFFER_DEPTH
];
1065 test_attachment_completeness(ctx
, GL_DEPTH
, att
);
1066 if (!att
->Complete
) {
1067 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1068 fbo_incomplete(ctx
, "depth attachment incomplete", -1);
1070 } else if (att
->Type
!= GL_NONE
) {
1071 has_depth_attachment
= true;
1075 att
= &fb
->Attachment
[BUFFER_STENCIL
];
1076 test_attachment_completeness(ctx
, GL_STENCIL
, att
);
1077 if (!att
->Complete
) {
1078 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1079 fbo_incomplete(ctx
, "stencil attachment incomplete", -1);
1081 } else if (att
->Type
!= GL_NONE
) {
1082 has_stencil_attachment
= true;
1086 att
= &fb
->Attachment
[BUFFER_COLOR0
+ i
];
1087 test_attachment_completeness(ctx
, GL_COLOR
, att
);
1088 if (!att
->Complete
) {
1089 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
;
1090 fbo_incomplete(ctx
, "color attachment incomplete", i
);
1095 /* get width, height, format of the renderbuffer/texture
1097 unsigned attNumSamples
, attNumStorageSamples
;
1099 if (att
->Type
== GL_TEXTURE
) {
1100 const struct gl_texture_image
*texImg
= att
->Renderbuffer
->TexImage
;
1101 att_tex_target
= att
->Texture
->Target
;
1102 minWidth
= MIN2(minWidth
, texImg
->Width
);
1103 maxWidth
= MAX2(maxWidth
, texImg
->Width
);
1104 minHeight
= MIN2(minHeight
, texImg
->Height
);
1105 maxHeight
= MAX2(maxHeight
, texImg
->Height
);
1106 f
= texImg
->_BaseFormat
;
1108 attFormat
= texImg
->TexFormat
;
1111 if (!is_format_color_renderable(ctx
, attFormat
,
1112 texImg
->InternalFormat
) &&
1113 !is_legal_depth_format(ctx
, f
) &&
1114 f
!= GL_STENCIL_INDEX
) {
1115 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
;
1116 fbo_incomplete(ctx
, "texture attachment incomplete", -1);
1120 if (fixedSampleLocations
< 0)
1121 fixedSampleLocations
= texImg
->FixedSampleLocations
;
1122 else if (fixedSampleLocations
!= texImg
->FixedSampleLocations
) {
1123 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1124 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1128 if (att
->NumSamples
> 0)
1129 attNumSamples
= att
->NumSamples
;
1131 attNumSamples
= texImg
->NumSamples
;
1132 attNumStorageSamples
= attNumSamples
;
1134 else if (att
->Type
== GL_RENDERBUFFER_EXT
) {
1135 minWidth
= MIN2(minWidth
, att
->Renderbuffer
->Width
);
1136 maxWidth
= MAX2(minWidth
, att
->Renderbuffer
->Width
);
1137 minHeight
= MIN2(minHeight
, att
->Renderbuffer
->Height
);
1138 maxHeight
= MAX2(minHeight
, att
->Renderbuffer
->Height
);
1139 f
= att
->Renderbuffer
->InternalFormat
;
1140 baseFormat
= att
->Renderbuffer
->_BaseFormat
;
1141 attFormat
= att
->Renderbuffer
->Format
;
1144 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1145 if (fixedSampleLocations
< 0)
1146 fixedSampleLocations
= GL_TRUE
;
1147 else if (fixedSampleLocations
!= GL_TRUE
) {
1148 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1149 fbo_incomplete(ctx
, "inconsistent fixed sample locations", -1);
1153 attNumSamples
= att
->Renderbuffer
->NumSamples
;
1154 attNumStorageSamples
= att
->Renderbuffer
->NumStorageSamples
;
1157 assert(att
->Type
== GL_NONE
);
1162 /* Color buffers. */
1163 if (numColorSamples
< 0) {
1164 assert(numColorStorageSamples
< 0);
1165 numColorSamples
= attNumSamples
;
1166 numColorStorageSamples
= attNumStorageSamples
;
1167 } else if (numColorSamples
!= attNumSamples
||
1168 numColorStorageSamples
!= attNumStorageSamples
) {
1169 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1170 fbo_incomplete(ctx
, "inconsistent sample counts", -1);
1174 /* Depth/stencil buffers. */
1175 if (numDepthSamples
< 0) {
1176 numDepthSamples
= attNumSamples
;
1177 } else if (numDepthSamples
!= attNumSamples
) {
1178 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1179 fbo_incomplete(ctx
, "inconsistent sample counts", -1);
1184 /* Update flags describing color buffer datatypes */
1186 GLenum type
= _mesa_get_format_datatype(attFormat
);
1188 /* check if integer color */
1189 if (_mesa_is_format_integer_color(attFormat
))
1190 fb
->_IntegerBuffers
|= (1 << i
);
1192 if (baseFormat
== GL_RGB
)
1193 fb
->_RGBBuffers
|= (1 << i
);
1195 if (type
== GL_FLOAT
&& _mesa_get_format_max_bits(attFormat
) > 16)
1196 fb
->_FP32Buffers
|= (1 << i
);
1198 fb
->_AllColorBuffersFixedPoint
=
1199 fb
->_AllColorBuffersFixedPoint
&&
1200 (type
== GL_UNSIGNED_NORMALIZED
|| type
== GL_SIGNED_NORMALIZED
);
1202 fb
->_HasSNormOrFloatColorBuffer
=
1203 fb
->_HasSNormOrFloatColorBuffer
||
1204 type
== GL_SIGNED_NORMALIZED
|| type
== GL_FLOAT
;
1207 /* Error-check width, height, format */
1208 if (numImages
== 1) {
1215 if (!ctx
->Extensions
.ARB_framebuffer_object
) {
1216 /* check that width, height, format are same */
1217 if (minWidth
!= maxWidth
|| minHeight
!= maxHeight
) {
1218 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
;
1219 fbo_incomplete(ctx
, "width or height mismatch", -1);
1222 /* check that all color buffers are the same format */
1223 if (intFormat
!= GL_NONE
&& f
!= intFormat
) {
1224 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
;
1225 fbo_incomplete(ctx
, "format mismatch", -1);
1231 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1233 if (att
->Type
== GL_RENDERBUFFER
&&
1234 att
->Renderbuffer
->Format
== MESA_FORMAT_NONE
) {
1235 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1236 fbo_incomplete(ctx
, "unsupported renderbuffer format", i
);
1240 /* Check that layered rendering is consistent. */
1242 if (att_tex_target
== GL_TEXTURE_CUBE_MAP
)
1243 att_layer_count
= 6;
1244 else if (att_tex_target
== GL_TEXTURE_1D_ARRAY
)
1245 att_layer_count
= att
->Renderbuffer
->Height
;
1247 att_layer_count
= att
->Renderbuffer
->Depth
;
1249 att_layer_count
= 0;
1251 if (!layer_info_valid
) {
1252 is_layered
= att
->Layered
;
1253 max_layer_count
= att_layer_count
;
1254 layer_tex_target
= att_tex_target
;
1255 layer_info_valid
= true;
1256 } else if (max_layer_count
> 0 && layer_tex_target
!= att_tex_target
) {
1257 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1258 fbo_incomplete(ctx
, "layered framebuffer has mismatched targets", i
);
1260 } else if (is_layered
!= att
->Layered
) {
1261 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
;
1263 "framebuffer attachment layer mode is inconsistent",
1266 } else if (att_layer_count
> max_layer_count
) {
1267 max_layer_count
= att_layer_count
;
1271 * The extension GL_ARB_framebuffer_no_attachments places additional
1272 * requirement on each attachment. Those additional requirements are
1273 * tighter that those of previous versions of GL. In interest of better
1274 * compatibility, we will not enforce these restrictions. For the record
1275 * those additional restrictions are quoted below:
1277 * "The width and height of image are greater than zero and less than or
1278 * equal to the values of the implementation-dependent limits
1279 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1281 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1282 * array texture and the attachment is layered, the depth or layer count
1283 * of the texture is less than or equal to the implementation-dependent
1284 * limit MAX_FRAMEBUFFER_LAYERS."
1286 * "If image has multiple samples, its sample count is less than or equal
1287 * to the value of the implementation-dependent limit
1288 * MAX_FRAMEBUFFER_SAMPLES."
1290 * The same requirements are also in place for GL 4.5,
1291 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1295 if (ctx
->Extensions
.AMD_framebuffer_multisample_advanced
) {
1296 /* See if non-matching sample counts are supported. */
1297 if (numColorSamples
>= 0 && numDepthSamples
>= 0) {
1300 assert(numColorStorageSamples
!= -1);
1302 numColorSamples
= MAX2(numColorSamples
, 1);
1303 numColorStorageSamples
= MAX2(numColorStorageSamples
, 1);
1304 numDepthSamples
= MAX2(numDepthSamples
, 1);
1306 if (numColorSamples
== 1 && numColorStorageSamples
== 1 &&
1307 numDepthSamples
== 1) {
1310 for (i
= 0; i
< ctx
->Const
.NumSupportedMultisampleModes
; i
++) {
1312 &ctx
->Const
.SupportedMultisampleModes
[i
].NumColorSamples
;
1314 if (counts
[0] == numColorSamples
&&
1315 counts
[1] == numColorStorageSamples
&&
1316 counts
[2] == numDepthSamples
) {
1324 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1325 fbo_incomplete(ctx
, "unsupported sample counts", -1);
1330 /* If the extension is unsupported, all sample counts must be equal. */
1331 if (numColorSamples
>= 0 &&
1332 (numColorSamples
!= numColorStorageSamples
||
1333 (numDepthSamples
>= 0 && numColorSamples
!= numDepthSamples
))) {
1334 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
;
1335 fbo_incomplete(ctx
, "inconsistent sample counts", -1);
1340 fb
->MaxNumLayers
= max_layer_count
;
1342 if (numImages
== 0) {
1343 fb
->_HasAttachments
= false;
1345 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
1346 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1347 fbo_incomplete(ctx
, "no attachments", -1);
1351 if (fb
->DefaultGeometry
.Width
== 0 || fb
->DefaultGeometry
.Height
== 0) {
1352 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
;
1353 fbo_incomplete(ctx
, "no attachments and default width or height is 0", -1);
1358 if (_mesa_is_desktop_gl(ctx
) && !ctx
->Extensions
.ARB_ES2_compatibility
) {
1359 /* Check that all DrawBuffers are present */
1360 for (j
= 0; j
< ctx
->Const
.MaxDrawBuffers
; j
++) {
1361 if (fb
->ColorDrawBuffer
[j
] != GL_NONE
) {
1362 const struct gl_renderbuffer_attachment
*att
1363 = get_attachment(ctx
, fb
, fb
->ColorDrawBuffer
[j
], NULL
);
1365 if (att
->Type
== GL_NONE
) {
1366 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
;
1367 fbo_incomplete(ctx
, "missing drawbuffer", j
);
1373 /* Check that the ReadBuffer is present */
1374 if (fb
->ColorReadBuffer
!= GL_NONE
) {
1375 const struct gl_renderbuffer_attachment
*att
1376 = get_attachment(ctx
, fb
, fb
->ColorReadBuffer
, NULL
);
1378 if (att
->Type
== GL_NONE
) {
1379 fb
->_Status
= GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
;
1380 fbo_incomplete(ctx
, "missing readbuffer", -1);
1386 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1388 * "Depth and stencil attachments, if present, are the same image."
1390 * This restriction is not present in the OpenGL ES2 spec.
1392 if (_mesa_is_gles3(ctx
) &&
1393 has_stencil_attachment
&& has_depth_attachment
&&
1394 !_mesa_has_depthstencil_combined(fb
)) {
1395 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED
;
1396 fbo_incomplete(ctx
, "Depth and stencil attachments must be the same image", -1);
1400 /* Provisionally set status = COMPLETE ... */
1401 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
1403 /* ... but the driver may say the FB is incomplete.
1404 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1407 if (ctx
->Driver
.ValidateFramebuffer
) {
1408 ctx
->Driver
.ValidateFramebuffer(ctx
, fb
);
1409 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1410 fbo_incomplete(ctx
, "driver marked FBO as incomplete", -1);
1416 * Note that if ARB_framebuffer_object is supported and the attached
1417 * renderbuffers/textures are different sizes, the framebuffer
1418 * width/height will be set to the smallest width/height.
1420 if (numImages
!= 0) {
1421 fb
->Width
= minWidth
;
1422 fb
->Height
= minHeight
;
1425 /* finally, update the visual info for the framebuffer */
1426 _mesa_update_framebuffer_visual(ctx
, fb
);
1430 GLboolean GLAPIENTRY
1431 _mesa_IsRenderbuffer(GLuint renderbuffer
)
1433 struct gl_renderbuffer
*rb
;
1435 GET_CURRENT_CONTEXT(ctx
);
1437 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1439 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1440 return rb
!= NULL
&& rb
!= &DummyRenderbuffer
;
1444 static struct gl_renderbuffer
*
1445 allocate_renderbuffer_locked(struct gl_context
*ctx
, GLuint renderbuffer
,
1448 struct gl_renderbuffer
*newRb
;
1450 /* create new renderbuffer object */
1451 newRb
= ctx
->Driver
.NewRenderbuffer(ctx
, renderbuffer
);
1453 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1456 assert(newRb
->AllocStorage
);
1457 _mesa_HashInsertLocked(ctx
->Shared
->RenderBuffers
, renderbuffer
, newRb
);
1464 bind_renderbuffer(GLenum target
, GLuint renderbuffer
)
1466 struct gl_renderbuffer
*newRb
;
1467 GET_CURRENT_CONTEXT(ctx
);
1469 if (target
!= GL_RENDERBUFFER_EXT
) {
1470 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindRenderbufferEXT(target)");
1474 /* No need to flush here since the render buffer binding has no
1475 * effect on rendering state.
1479 newRb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
1480 if (newRb
== &DummyRenderbuffer
) {
1481 /* ID was reserved, but no real renderbuffer object made yet */
1484 else if (!newRb
&& ctx
->API
== API_OPENGL_CORE
) {
1485 /* All RB IDs must be Gen'd */
1486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1487 "glBindRenderbuffer(non-gen name)");
1492 _mesa_HashLockMutex(ctx
->Shared
->RenderBuffers
);
1493 newRb
= allocate_renderbuffer_locked(ctx
, renderbuffer
,
1494 "glBindRenderbufferEXT");
1495 _mesa_HashUnlockMutex(ctx
->Shared
->RenderBuffers
);
1502 assert(newRb
!= &DummyRenderbuffer
);
1504 _mesa_reference_renderbuffer(&ctx
->CurrentRenderbuffer
, newRb
);
1508 _mesa_BindRenderbuffer(GLenum target
, GLuint renderbuffer
)
1510 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1511 * entry point, but they allow the use of user-generated names.
1513 bind_renderbuffer(target
, renderbuffer
);
1517 _mesa_BindRenderbufferEXT(GLenum target
, GLuint renderbuffer
)
1519 bind_renderbuffer(target
, renderbuffer
);
1523 * ARB_framebuffer_no_attachment and ARB_sample_locations - Application passes
1524 * requested param's here. NOTE: NumSamples requested need not be _NumSamples
1525 * which is what the hw supports.
1528 framebuffer_parameteri(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1529 GLenum pname
, GLint param
, const char *func
)
1531 bool cannot_be_winsys_fbo
= false;
1534 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1535 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1536 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1537 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1538 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1539 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
)
1540 goto invalid_pname_enum
;
1541 cannot_be_winsys_fbo
= true;
1543 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1544 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1545 if (!ctx
->Extensions
.ARB_sample_locations
)
1546 goto invalid_pname_enum
;
1548 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1549 if (!ctx
->Extensions
.MESA_framebuffer_flip_y
)
1550 goto invalid_pname_enum
;
1551 cannot_be_winsys_fbo
= true;
1554 goto invalid_pname_enum
;
1557 if (cannot_be_winsys_fbo
&& _mesa_is_winsys_fbo(fb
)) {
1558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1559 "%s(invalid pname=0x%x for default framebuffer)", func
, pname
);
1564 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1565 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferWidth
)
1566 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1568 fb
->DefaultGeometry
.Width
= param
;
1570 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1571 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferHeight
)
1572 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1574 fb
->DefaultGeometry
.Height
= param
;
1576 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1578 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1579 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1581 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
) {
1582 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1585 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferLayers
)
1586 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1588 fb
->DefaultGeometry
.Layers
= param
;
1590 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1591 if (param
< 0 || param
> ctx
->Const
.MaxFramebufferSamples
)
1592 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", func
);
1594 fb
->DefaultGeometry
.NumSamples
= param
;
1596 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1597 fb
->DefaultGeometry
.FixedSampleLocations
= param
;
1599 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1600 fb
->ProgrammableSampleLocations
= !!param
;
1602 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1603 fb
->SampleLocationPixelGrid
= !!param
;
1605 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1611 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1612 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1613 if (fb
== ctx
->DrawBuffer
)
1614 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
1617 invalidate_framebuffer(fb
);
1618 ctx
->NewState
|= _NEW_BUFFERS
;
1625 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1629 _mesa_FramebufferParameteri(GLenum target
, GLenum pname
, GLint param
)
1631 GET_CURRENT_CONTEXT(ctx
);
1632 struct gl_framebuffer
*fb
;
1634 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
1635 !ctx
->Extensions
.ARB_sample_locations
) {
1636 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1637 "glFramebufferParameteriv not supported "
1638 "(neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
1643 fb
= get_framebuffer_target(ctx
, target
);
1645 _mesa_error(ctx
, GL_INVALID_ENUM
,
1646 "glFramebufferParameteri(target=0x%x)", target
);
1650 framebuffer_parameteri(ctx
, fb
, pname
, param
, "glFramebufferParameteri");
1654 validate_get_framebuffer_parameteriv_pname(struct gl_context
*ctx
,
1655 struct gl_framebuffer
*fb
,
1656 GLuint pname
, const char *func
)
1658 bool cannot_be_winsys_fbo
= true;
1661 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1663 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1664 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1666 if (_mesa_is_gles31(ctx
) && !ctx
->Extensions
.OES_geometry_shader
) {
1667 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1671 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1672 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1673 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1674 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1676 case GL_DOUBLEBUFFER
:
1677 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
1678 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
1680 case GL_SAMPLE_BUFFERS
:
1682 /* From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries:
1684 * "An INVALID_OPERATION error is generated by GetFramebufferParameteriv
1685 * if the default framebuffer is bound to target and pname is not one
1686 * of the accepted values from table 23.73, other than
1689 * For OpenGL ES, using default framebuffer raises INVALID_OPERATION
1692 cannot_be_winsys_fbo
= !_mesa_is_desktop_gl(ctx
);
1694 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1695 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1696 if (!ctx
->Extensions
.ARB_sample_locations
)
1697 goto invalid_pname_enum
;
1698 cannot_be_winsys_fbo
= false;
1700 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1701 if (!ctx
->Extensions
.MESA_framebuffer_flip_y
) {
1702 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1707 goto invalid_pname_enum
;
1710 if (cannot_be_winsys_fbo
&& _mesa_is_winsys_fbo(fb
)) {
1711 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1712 "%s(invalid pname=0x%x for default framebuffer)", func
, pname
);
1719 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(pname=0x%x)", func
, pname
);
1724 get_framebuffer_parameteriv(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
1725 GLenum pname
, GLint
*params
, const char *func
)
1727 if (!validate_get_framebuffer_parameteriv_pname(ctx
, fb
, pname
, func
))
1731 case GL_FRAMEBUFFER_DEFAULT_WIDTH
:
1732 *params
= fb
->DefaultGeometry
.Width
;
1734 case GL_FRAMEBUFFER_DEFAULT_HEIGHT
:
1735 *params
= fb
->DefaultGeometry
.Height
;
1737 case GL_FRAMEBUFFER_DEFAULT_LAYERS
:
1738 *params
= fb
->DefaultGeometry
.Layers
;
1740 case GL_FRAMEBUFFER_DEFAULT_SAMPLES
:
1741 *params
= fb
->DefaultGeometry
.NumSamples
;
1743 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS
:
1744 *params
= fb
->DefaultGeometry
.FixedSampleLocations
;
1746 case GL_DOUBLEBUFFER
:
1747 *params
= fb
->Visual
.doubleBufferMode
;
1749 case GL_IMPLEMENTATION_COLOR_READ_FORMAT
:
1750 *params
= _mesa_get_color_read_format(ctx
, fb
, func
);
1752 case GL_IMPLEMENTATION_COLOR_READ_TYPE
:
1753 *params
= _mesa_get_color_read_type(ctx
, fb
, func
);
1756 *params
= _mesa_geometric_samples(fb
);
1758 case GL_SAMPLE_BUFFERS
:
1759 *params
= _mesa_geometric_samples(fb
) > 0;
1762 *params
= fb
->Visual
.stereoMode
;
1764 case GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS_ARB
:
1765 *params
= fb
->ProgrammableSampleLocations
;
1767 case GL_FRAMEBUFFER_SAMPLE_LOCATION_PIXEL_GRID_ARB
:
1768 *params
= fb
->SampleLocationPixelGrid
;
1770 case GL_FRAMEBUFFER_FLIP_Y_MESA
:
1771 *params
= fb
->FlipY
;
1777 _mesa_GetFramebufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1779 GET_CURRENT_CONTEXT(ctx
);
1780 struct gl_framebuffer
*fb
;
1782 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
1783 !ctx
->Extensions
.ARB_sample_locations
) {
1784 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1785 "glGetFramebufferParameteriv not supported "
1786 "(neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
1791 fb
= get_framebuffer_target(ctx
, target
);
1793 _mesa_error(ctx
, GL_INVALID_ENUM
,
1794 "glGetFramebufferParameteriv(target=0x%x)", target
);
1798 get_framebuffer_parameteriv(ctx
, fb
, pname
, params
,
1799 "glGetFramebufferParameteriv");
1804 * Remove the specified renderbuffer or texture from any attachment point in
1808 * \c true if the renderbuffer was detached from an attachment point. \c
1812 _mesa_detach_renderbuffer(struct gl_context
*ctx
,
1813 struct gl_framebuffer
*fb
,
1817 bool progress
= false;
1819 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
1820 if (fb
->Attachment
[i
].Texture
== att
1821 || fb
->Attachment
[i
].Renderbuffer
== att
) {
1822 remove_attachment(ctx
, &fb
->Attachment
[i
]);
1827 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1828 * Completeness," of the OpenGL 3.1 spec says:
1830 * "Performing any of the following actions may change whether the
1831 * framebuffer is considered complete or incomplete:
1835 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1836 * containing an image that is attached to a framebuffer object
1837 * that is bound to the framebuffer."
1840 invalidate_framebuffer(fb
);
1847 _mesa_DeleteRenderbuffers(GLsizei n
, const GLuint
*renderbuffers
)
1850 GET_CURRENT_CONTEXT(ctx
);
1853 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteRenderbuffers(n < 0)");
1857 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
1859 for (i
= 0; i
< n
; i
++) {
1860 if (renderbuffers
[i
] > 0) {
1861 struct gl_renderbuffer
*rb
;
1862 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffers
[i
]);
1864 /* check if deleting currently bound renderbuffer object */
1865 if (rb
== ctx
->CurrentRenderbuffer
) {
1867 assert(rb
->RefCount
>= 2);
1868 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT
, 0);
1871 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1872 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1873 * of the OpenGL 3.1 spec says:
1875 * "If a renderbuffer object is deleted while its image is
1876 * attached to one or more attachment points in the currently
1877 * bound framebuffer, then it is as if FramebufferRenderbuffer
1878 * had been called, with a renderbuffer of 0, for each
1879 * attachment point to which this image was attached in the
1880 * currently bound framebuffer. In other words, this
1881 * renderbuffer image is first detached from all attachment
1882 * points in the currently bound framebuffer. Note that the
1883 * renderbuffer image is specifically not detached from any
1884 * non-bound framebuffers. Detaching the image from any
1885 * non-bound framebuffers is the responsibility of the
1888 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1889 _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, rb
);
1891 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1892 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1893 _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, rb
);
1896 /* Remove from hash table immediately, to free the ID.
1897 * But the object will not be freed until it's no longer
1898 * referenced anywhere else.
1900 _mesa_HashRemove(ctx
->Shared
->RenderBuffers
, renderbuffers
[i
]);
1902 if (rb
!= &DummyRenderbuffer
) {
1903 /* no longer referenced by hash table */
1904 _mesa_reference_renderbuffer(&rb
, NULL
);
1912 create_render_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*renderbuffers
,
1915 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1922 _mesa_HashLockMutex(ctx
->Shared
->RenderBuffers
);
1924 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->RenderBuffers
, n
);
1926 for (i
= 0; i
< n
; i
++) {
1927 GLuint name
= first
+ i
;
1928 renderbuffers
[i
] = name
;
1931 allocate_renderbuffer_locked(ctx
, name
, func
);
1933 /* insert a dummy renderbuffer into the hash table */
1934 _mesa_HashInsertLocked(ctx
->Shared
->RenderBuffers
, name
,
1935 &DummyRenderbuffer
);
1939 _mesa_HashUnlockMutex(ctx
->Shared
->RenderBuffers
);
1944 create_render_buffers_err(struct gl_context
*ctx
, GLsizei n
,
1945 GLuint
*renderbuffers
, bool dsa
)
1947 const char *func
= dsa
? "glCreateRenderbuffers" : "glGenRenderbuffers";
1950 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n<0)", func
);
1954 create_render_buffers(ctx
, n
, renderbuffers
, dsa
);
1959 _mesa_GenRenderbuffers_no_error(GLsizei n
, GLuint
*renderbuffers
)
1961 GET_CURRENT_CONTEXT(ctx
);
1962 create_render_buffers(ctx
, n
, renderbuffers
, false);
1967 _mesa_GenRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1969 GET_CURRENT_CONTEXT(ctx
);
1970 create_render_buffers_err(ctx
, n
, renderbuffers
, false);
1975 _mesa_CreateRenderbuffers_no_error(GLsizei n
, GLuint
*renderbuffers
)
1977 GET_CURRENT_CONTEXT(ctx
);
1978 create_render_buffers(ctx
, n
, renderbuffers
, true);
1983 _mesa_CreateRenderbuffers(GLsizei n
, GLuint
*renderbuffers
)
1985 GET_CURRENT_CONTEXT(ctx
);
1986 create_render_buffers_err(ctx
, n
, renderbuffers
, true);
1991 * Given an internal format token for a render buffer, return the
1992 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1993 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1994 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1996 * This is similar to _mesa_base_tex_format() but the set of valid
1997 * internal formats is different.
1999 * Note that even if a format is determined to be legal here, validation
2000 * of the FBO may fail if the format is not supported by the driver/GPU.
2002 * \param internalFormat as passed to glRenderbufferStorage()
2003 * \return the base internal format, or 0 if internalFormat is illegal
2006 _mesa_base_fbo_format(const struct gl_context
*ctx
, GLenum internalFormat
)
2009 * Notes: some formats such as alpha, luminance, etc. were added
2010 * with GL_ARB_framebuffer_object.
2012 switch (internalFormat
) {
2018 return (ctx
->API
== API_OPENGL_COMPAT
&&
2019 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_ALPHA
: 0;
2023 case GL_LUMINANCE12
:
2024 case GL_LUMINANCE16
:
2025 return (ctx
->API
== API_OPENGL_COMPAT
&&
2026 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE
: 0;
2027 case GL_LUMINANCE_ALPHA
:
2028 case GL_LUMINANCE4_ALPHA4
:
2029 case GL_LUMINANCE6_ALPHA2
:
2030 case GL_LUMINANCE8_ALPHA8
:
2031 case GL_LUMINANCE12_ALPHA4
:
2032 case GL_LUMINANCE12_ALPHA12
:
2033 case GL_LUMINANCE16_ALPHA16
:
2034 return (ctx
->API
== API_OPENGL_COMPAT
&&
2035 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_LUMINANCE_ALPHA
: 0;
2039 case GL_INTENSITY12
:
2040 case GL_INTENSITY16
:
2041 return (ctx
->API
== API_OPENGL_COMPAT
&&
2042 ctx
->Extensions
.ARB_framebuffer_object
) ? GL_INTENSITY
: 0;
2052 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
2054 return _mesa_is_desktop_gl(ctx
) ? GL_RGB
: 0;
2062 return _mesa_is_desktop_gl(ctx
) ? GL_RGBA
: 0;
2064 return _mesa_is_desktop_gl(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
2067 case GL_SRGB8_ALPHA8_EXT
:
2068 return _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
2069 case GL_STENCIL_INDEX
:
2070 case GL_STENCIL_INDEX1_EXT
:
2071 case GL_STENCIL_INDEX4_EXT
:
2072 case GL_STENCIL_INDEX16_EXT
:
2073 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
2074 * OpenGL ES, but Mesa does not currently support them.
2076 return _mesa_is_desktop_gl(ctx
) ? GL_STENCIL_INDEX
: 0;
2077 case GL_STENCIL_INDEX8_EXT
:
2078 return GL_STENCIL_INDEX
;
2079 case GL_DEPTH_COMPONENT
:
2080 case GL_DEPTH_COMPONENT32
:
2081 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_COMPONENT
: 0;
2082 case GL_DEPTH_COMPONENT16
:
2083 case GL_DEPTH_COMPONENT24
:
2084 return GL_DEPTH_COMPONENT
;
2085 case GL_DEPTH_STENCIL
:
2086 return _mesa_is_desktop_gl(ctx
) ? GL_DEPTH_STENCIL
: 0;
2087 case GL_DEPTH24_STENCIL8
:
2088 return GL_DEPTH_STENCIL
;
2089 case GL_DEPTH_COMPONENT32F
:
2090 return ctx
->Version
>= 30
2091 || (ctx
->API
== API_OPENGL_COMPAT
&&
2092 ctx
->Extensions
.ARB_depth_buffer_float
)
2093 ? GL_DEPTH_COMPONENT
: 0;
2094 case GL_DEPTH32F_STENCIL8
:
2095 return ctx
->Version
>= 30
2096 || (ctx
->API
== API_OPENGL_COMPAT
&&
2097 ctx
->Extensions
.ARB_depth_buffer_float
)
2098 ? GL_DEPTH_STENCIL
: 0;
2100 return _mesa_has_ARB_texture_rg(ctx
) ? GL_RED
: 0;
2102 return _mesa_has_ARB_texture_rg(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
2105 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
2108 return _mesa_has_ARB_texture_rg(ctx
) ? GL_RG
: 0;
2110 return _mesa_has_ARB_texture_rg(ctx
) || _mesa_has_EXT_texture_norm16(ctx
)
2113 return ctx
->API
!= API_OPENGLES
&& ctx
->Extensions
.ARB_texture_rg
2115 /* signed normalized texture formats */
2117 return _mesa_has_EXT_texture_snorm(ctx
) || _mesa_has_EXT_render_snorm(ctx
)
2120 return _mesa_has_EXT_texture_snorm(ctx
) ? GL_RED
: 0;
2122 return _mesa_has_EXT_texture_snorm(ctx
) ||
2123 (_mesa_has_EXT_render_snorm(ctx
) &&
2124 _mesa_has_EXT_texture_norm16(ctx
))
2127 return _mesa_has_EXT_texture_snorm(ctx
) || _mesa_has_EXT_render_snorm(ctx
)
2130 return _mesa_has_EXT_texture_snorm(ctx
) ? GL_RG
: 0;
2132 return _mesa_has_EXT_texture_snorm(ctx
) ||
2133 (_mesa_has_EXT_render_snorm(ctx
) &&
2134 _mesa_has_EXT_texture_norm16(ctx
))
2138 case GL_RGB16_SNORM
:
2139 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2141 case GL_RGBA8_SNORM
:
2142 return _mesa_has_EXT_texture_snorm(ctx
) || _mesa_has_EXT_render_snorm(ctx
)
2145 return _mesa_has_EXT_texture_snorm(ctx
) ? GL_RGBA
: 0;
2146 case GL_RGBA16_SNORM
:
2147 return _mesa_has_EXT_texture_snorm(ctx
) ||
2148 (_mesa_has_EXT_render_snorm(ctx
) &&
2149 _mesa_has_EXT_texture_norm16(ctx
))
2151 case GL_ALPHA_SNORM
:
2152 case GL_ALPHA8_SNORM
:
2153 case GL_ALPHA16_SNORM
:
2154 return ctx
->API
== API_OPENGL_COMPAT
&&
2155 ctx
->Extensions
.EXT_texture_snorm
&&
2156 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2157 case GL_LUMINANCE_SNORM
:
2158 case GL_LUMINANCE8_SNORM
:
2159 case GL_LUMINANCE16_SNORM
:
2160 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2162 case GL_LUMINANCE_ALPHA_SNORM
:
2163 case GL_LUMINANCE8_ALPHA8_SNORM
:
2164 case GL_LUMINANCE16_ALPHA16_SNORM
:
2165 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2166 ? GL_LUMINANCE_ALPHA
: 0;
2167 case GL_INTENSITY_SNORM
:
2168 case GL_INTENSITY8_SNORM
:
2169 case GL_INTENSITY16_SNORM
:
2170 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_snorm
2175 return ((_mesa_is_desktop_gl(ctx
) &&
2176 ctx
->Extensions
.ARB_texture_rg
&&
2177 ctx
->Extensions
.ARB_texture_float
) ||
2178 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2182 return ((_mesa_is_desktop_gl(ctx
) &&
2183 ctx
->Extensions
.ARB_texture_rg
&&
2184 ctx
->Extensions
.ARB_texture_float
) ||
2185 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2189 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_float
)
2193 return ((_mesa_is_desktop_gl(ctx
) &&
2194 ctx
->Extensions
.ARB_texture_float
) ||
2195 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2198 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_shared_exponent
)
2200 case GL_ALPHA16F_ARB
:
2201 case GL_ALPHA32F_ARB
:
2202 return ctx
->API
== API_OPENGL_COMPAT
&&
2203 ctx
->Extensions
.ARB_texture_float
&&
2204 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2205 case GL_LUMINANCE16F_ARB
:
2206 case GL_LUMINANCE32F_ARB
:
2207 return ctx
->API
== API_OPENGL_COMPAT
&&
2208 ctx
->Extensions
.ARB_texture_float
&&
2209 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
2210 case GL_LUMINANCE_ALPHA16F_ARB
:
2211 case GL_LUMINANCE_ALPHA32F_ARB
:
2212 return ctx
->API
== API_OPENGL_COMPAT
&&
2213 ctx
->Extensions
.ARB_texture_float
&&
2214 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
2215 case GL_INTENSITY16F_ARB
:
2216 case GL_INTENSITY32F_ARB
:
2217 return ctx
->API
== API_OPENGL_COMPAT
&&
2218 ctx
->Extensions
.ARB_texture_float
&&
2219 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
2220 case GL_R11F_G11F_B10F
:
2221 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_packed_float
) ||
2222 _mesa_is_gles3(ctx
) /* EXT_color_buffer_float */ )
2225 case GL_RGBA8UI_EXT
:
2226 case GL_RGBA16UI_EXT
:
2227 case GL_RGBA32UI_EXT
:
2229 case GL_RGBA16I_EXT
:
2230 case GL_RGBA32I_EXT
:
2231 return ctx
->Version
>= 30
2232 || (_mesa_is_desktop_gl(ctx
) &&
2233 ctx
->Extensions
.EXT_texture_integer
) ? GL_RGBA
: 0;
2236 case GL_RGB16UI_EXT
:
2237 case GL_RGB32UI_EXT
:
2241 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_integer
2249 return ctx
->Version
>= 30
2250 || (_mesa_is_desktop_gl(ctx
) &&
2251 ctx
->Extensions
.ARB_texture_rg
&&
2252 ctx
->Extensions
.EXT_texture_integer
) ? GL_RED
: 0;
2260 return ctx
->Version
>= 30
2261 || (_mesa_is_desktop_gl(ctx
) &&
2262 ctx
->Extensions
.ARB_texture_rg
&&
2263 ctx
->Extensions
.EXT_texture_integer
) ? GL_RG
: 0;
2265 case GL_INTENSITY8I_EXT
:
2266 case GL_INTENSITY8UI_EXT
:
2267 case GL_INTENSITY16I_EXT
:
2268 case GL_INTENSITY16UI_EXT
:
2269 case GL_INTENSITY32I_EXT
:
2270 case GL_INTENSITY32UI_EXT
:
2271 return ctx
->API
== API_OPENGL_COMPAT
&&
2272 ctx
->Extensions
.EXT_texture_integer
&&
2273 ctx
->Extensions
.ARB_framebuffer_object
? GL_INTENSITY
: 0;
2275 case GL_LUMINANCE8I_EXT
:
2276 case GL_LUMINANCE8UI_EXT
:
2277 case GL_LUMINANCE16I_EXT
:
2278 case GL_LUMINANCE16UI_EXT
:
2279 case GL_LUMINANCE32I_EXT
:
2280 case GL_LUMINANCE32UI_EXT
:
2281 return ctx
->API
== API_OPENGL_COMPAT
&&
2282 ctx
->Extensions
.EXT_texture_integer
&&
2283 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE
: 0;
2285 case GL_LUMINANCE_ALPHA8I_EXT
:
2286 case GL_LUMINANCE_ALPHA8UI_EXT
:
2287 case GL_LUMINANCE_ALPHA16I_EXT
:
2288 case GL_LUMINANCE_ALPHA16UI_EXT
:
2289 case GL_LUMINANCE_ALPHA32I_EXT
:
2290 case GL_LUMINANCE_ALPHA32UI_EXT
:
2291 return ctx
->API
== API_OPENGL_COMPAT
&&
2292 ctx
->Extensions
.EXT_texture_integer
&&
2293 ctx
->Extensions
.ARB_framebuffer_object
? GL_LUMINANCE_ALPHA
: 0;
2295 case GL_ALPHA8I_EXT
:
2296 case GL_ALPHA8UI_EXT
:
2297 case GL_ALPHA16I_EXT
:
2298 case GL_ALPHA16UI_EXT
:
2299 case GL_ALPHA32I_EXT
:
2300 case GL_ALPHA32UI_EXT
:
2301 return ctx
->API
== API_OPENGL_COMPAT
&&
2302 ctx
->Extensions
.EXT_texture_integer
&&
2303 ctx
->Extensions
.ARB_framebuffer_object
? GL_ALPHA
: 0;
2306 return (_mesa_is_desktop_gl(ctx
) &&
2307 ctx
->Extensions
.ARB_texture_rgb10_a2ui
)
2308 || _mesa_is_gles3(ctx
) ? GL_RGBA
: 0;
2311 return _mesa_is_gles(ctx
) || ctx
->Extensions
.ARB_ES2_compatibility
2320 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
2323 invalidate_rb(GLuint key
, void *data
, void *userData
)
2325 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2326 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) userData
;
2328 /* If this is a user-created FBO */
2329 if (_mesa_is_user_fbo(fb
)) {
2331 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2332 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2333 if (att
->Type
== GL_RENDERBUFFER
&&
2334 att
->Renderbuffer
== rb
) {
2335 /* Mark fb status as indeterminate to force re-validation */
2344 /** sentinal value, see below */
2345 #define NO_SAMPLES 1000
2348 _mesa_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
2349 GLenum internalFormat
, GLsizei width
,
2350 GLsizei height
, GLsizei samples
,
2351 GLsizei storageSamples
)
2353 const GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
2355 assert(baseFormat
!= 0);
2356 assert(width
>= 0 && width
<= (GLsizei
) ctx
->Const
.MaxRenderbufferSize
);
2357 assert(height
>= 0 && height
<= (GLsizei
) ctx
->Const
.MaxRenderbufferSize
);
2358 assert(samples
!= NO_SAMPLES
);
2360 assert(samples
> 0);
2361 assert(_mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
2362 internalFormat
, samples
,
2363 storageSamples
) == GL_NO_ERROR
);
2366 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2368 if (rb
->InternalFormat
== internalFormat
&&
2369 rb
->Width
== (GLuint
) width
&&
2370 rb
->Height
== (GLuint
) height
&&
2371 rb
->NumSamples
== samples
&&
2372 rb
->NumStorageSamples
== storageSamples
) {
2373 /* no change in allocation needed */
2377 /* These MUST get set by the AllocStorage func */
2378 rb
->Format
= MESA_FORMAT_NONE
;
2379 rb
->NumSamples
= samples
;
2380 rb
->NumStorageSamples
= storageSamples
;
2382 /* Now allocate the storage */
2383 assert(rb
->AllocStorage
);
2384 if (rb
->AllocStorage(ctx
, rb
, internalFormat
, width
, height
)) {
2385 /* No error - check/set fields now */
2386 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2387 assert(rb
->Width
== (GLuint
) width
);
2388 assert(rb
->Height
== (GLuint
) height
);
2389 rb
->InternalFormat
= internalFormat
;
2390 rb
->_BaseFormat
= baseFormat
;
2391 assert(rb
->_BaseFormat
!= 0);
2394 /* Probably ran out of memory - clear the fields */
2397 rb
->Format
= MESA_FORMAT_NONE
;
2398 rb
->InternalFormat
= GL_NONE
;
2399 rb
->_BaseFormat
= GL_NONE
;
2401 rb
->NumStorageSamples
= 0;
2404 /* Invalidate the framebuffers the renderbuffer is attached in. */
2405 if (rb
->AttachedAnytime
) {
2406 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, invalidate_rb
, rb
);
2411 * Helper function used by renderbuffer_storage_direct() and
2412 * renderbuffer_storage_target().
2413 * samples will be NO_SAMPLES if called by a non-multisample function.
2416 renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
2417 GLenum internalFormat
, GLsizei width
,
2418 GLsizei height
, GLsizei samples
, GLsizei storageSamples
,
2422 GLenum sample_count_error
;
2424 baseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
2425 if (baseFormat
== 0) {
2426 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat=%s)",
2427 func
, _mesa_enum_to_string(internalFormat
));
2431 if (width
< 0 || width
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2432 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid width %d)", func
,
2437 if (height
< 0 || height
> (GLsizei
) ctx
->Const
.MaxRenderbufferSize
) {
2438 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid height %d)", func
,
2443 if (samples
== NO_SAMPLES
) {
2444 /* NumSamples == 0 indicates non-multisampling */
2449 /* check the sample count;
2450 * note: driver may choose to use more samples than what's requested
2452 sample_count_error
= _mesa_check_sample_count(ctx
, GL_RENDERBUFFER
,
2453 internalFormat
, samples
, storageSamples
);
2455 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2457 * "If a negative number is provided where an argument of type sizei or
2458 * sizeiptr is specified, the error INVALID VALUE is generated."
2460 if (samples
< 0 || storageSamples
< 0) {
2461 sample_count_error
= GL_INVALID_VALUE
;
2464 if (sample_count_error
!= GL_NO_ERROR
) {
2465 _mesa_error(ctx
, sample_count_error
,
2466 "%s(samples=%d, storageSamples=%d)", func
, samples
,
2472 _mesa_renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
,
2477 * Helper function used by _mesa_NamedRenderbufferStorage*().
2478 * samples will be NO_SAMPLES if called by a non-multisample function.
2481 renderbuffer_storage_named(GLuint renderbuffer
, GLenum internalFormat
,
2482 GLsizei width
, GLsizei height
, GLsizei samples
,
2483 GLsizei storageSamples
, const char *func
)
2485 GET_CURRENT_CONTEXT(ctx
);
2487 if (MESA_VERBOSE
& VERBOSE_API
) {
2488 if (samples
== NO_SAMPLES
)
2489 _mesa_debug(ctx
, "%s(%u, %s, %d, %d)\n",
2491 _mesa_enum_to_string(internalFormat
),
2494 _mesa_debug(ctx
, "%s(%u, %s, %d, %d, %d)\n",
2496 _mesa_enum_to_string(internalFormat
),
2497 width
, height
, samples
);
2500 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2501 if (!rb
|| rb
== &DummyRenderbuffer
) {
2502 /* ID was reserved, but no real renderbuffer object made yet */
2503 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid renderbuffer %u)",
2504 func
, renderbuffer
);
2508 renderbuffer_storage(ctx
, rb
, internalFormat
, width
, height
, samples
,
2509 storageSamples
, func
);
2513 * Helper function used by _mesa_RenderbufferStorage() and
2514 * _mesa_RenderbufferStorageMultisample().
2515 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2518 renderbuffer_storage_target(GLenum target
, GLenum internalFormat
,
2519 GLsizei width
, GLsizei height
, GLsizei samples
,
2520 GLsizei storageSamples
, const char *func
)
2522 GET_CURRENT_CONTEXT(ctx
);
2524 if (MESA_VERBOSE
& VERBOSE_API
) {
2525 if (samples
== NO_SAMPLES
)
2526 _mesa_debug(ctx
, "%s(%s, %s, %d, %d)\n",
2528 _mesa_enum_to_string(target
),
2529 _mesa_enum_to_string(internalFormat
),
2532 _mesa_debug(ctx
, "%s(%s, %s, %d, %d, %d)\n",
2534 _mesa_enum_to_string(target
),
2535 _mesa_enum_to_string(internalFormat
),
2536 width
, height
, samples
);
2539 if (target
!= GL_RENDERBUFFER_EXT
) {
2540 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
2544 if (!ctx
->CurrentRenderbuffer
) {
2545 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no renderbuffer bound)",
2550 renderbuffer_storage(ctx
, ctx
->CurrentRenderbuffer
, internalFormat
, width
,
2551 height
, samples
, storageSamples
, func
);
2556 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target
, GLeglImageOES image
)
2558 struct gl_renderbuffer
*rb
;
2559 GET_CURRENT_CONTEXT(ctx
);
2561 if (!ctx
->Extensions
.OES_EGL_image
) {
2562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2563 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2567 if (target
!= GL_RENDERBUFFER
) {
2568 _mesa_error(ctx
, GL_INVALID_ENUM
,
2569 "EGLImageTargetRenderbufferStorageOES");
2573 rb
= ctx
->CurrentRenderbuffer
;
2575 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2576 "EGLImageTargetRenderbufferStorageOES");
2580 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2582 ctx
->Driver
.EGLImageTargetRenderbufferStorage(ctx
, rb
, image
);
2587 * Helper function for _mesa_GetRenderbufferParameteriv() and
2588 * _mesa_GetFramebufferAttachmentParameteriv()
2589 * We have to be careful to respect the base format. For example, if a
2590 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2591 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2592 * we need to return zero.
2595 get_component_bits(GLenum pname
, GLenum baseFormat
, mesa_format format
)
2597 if (_mesa_base_format_has_channel(baseFormat
, pname
))
2598 return _mesa_get_format_bits(format
, pname
);
2606 _mesa_RenderbufferStorage(GLenum target
, GLenum internalFormat
,
2607 GLsizei width
, GLsizei height
)
2609 /* GL_ARB_fbo says calling this function is equivalent to calling
2610 * glRenderbufferStorageMultisample() with samples=0. We pass in
2611 * a token value here just for error reporting purposes.
2613 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2614 NO_SAMPLES
, 0, "glRenderbufferStorage");
2619 _mesa_RenderbufferStorageMultisample(GLenum target
, GLsizei samples
,
2620 GLenum internalFormat
,
2621 GLsizei width
, GLsizei height
)
2623 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2625 "glRenderbufferStorageMultisample");
2630 _mesa_RenderbufferStorageMultisampleAdvancedAMD(
2631 GLenum target
, GLsizei samples
, GLsizei storageSamples
,
2632 GLenum internalFormat
, GLsizei width
, GLsizei height
)
2634 renderbuffer_storage_target(target
, internalFormat
, width
, height
,
2635 samples
, storageSamples
,
2636 "glRenderbufferStorageMultisampleAdvancedAMD");
2641 * OpenGL ES version of glRenderBufferStorage.
2644 _es_RenderbufferStorageEXT(GLenum target
, GLenum internalFormat
,
2645 GLsizei width
, GLsizei height
)
2647 switch (internalFormat
) {
2649 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2650 /* choose a closest format */
2651 internalFormat
= GL_RGB5
;
2657 renderbuffer_storage_target(target
, internalFormat
, width
, height
, 0, 0,
2658 "glRenderbufferStorageEXT");
2662 _mesa_NamedRenderbufferStorage(GLuint renderbuffer
, GLenum internalformat
,
2663 GLsizei width
, GLsizei height
)
2665 /* GL_ARB_fbo says calling this function is equivalent to calling
2666 * glRenderbufferStorageMultisample() with samples=0. We pass in
2667 * a token value here just for error reporting purposes.
2669 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2670 NO_SAMPLES
, 0, "glNamedRenderbufferStorage");
2674 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer
, GLsizei samples
,
2675 GLenum internalformat
,
2676 GLsizei width
, GLsizei height
)
2678 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2680 "glNamedRenderbufferStorageMultisample");
2685 _mesa_NamedRenderbufferStorageMultisampleAdvancedAMD(
2686 GLuint renderbuffer
, GLsizei samples
, GLsizei storageSamples
,
2687 GLenum internalformat
, GLsizei width
, GLsizei height
)
2689 renderbuffer_storage_named(renderbuffer
, internalformat
, width
, height
,
2690 samples
, storageSamples
,
2691 "glNamedRenderbufferStorageMultisampleAdvancedAMD");
2696 get_render_buffer_parameteriv(struct gl_context
*ctx
,
2697 struct gl_renderbuffer
*rb
, GLenum pname
,
2698 GLint
*params
, const char *func
)
2700 /* No need to flush here since we're just quering state which is
2701 * not effected by rendering.
2705 case GL_RENDERBUFFER_WIDTH_EXT
:
2706 *params
= rb
->Width
;
2708 case GL_RENDERBUFFER_HEIGHT_EXT
:
2709 *params
= rb
->Height
;
2711 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT
:
2712 *params
= rb
->InternalFormat
;
2714 case GL_RENDERBUFFER_RED_SIZE_EXT
:
2715 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
2716 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
2717 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
2718 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
2719 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
2720 *params
= get_component_bits(pname
, rb
->_BaseFormat
, rb
->Format
);
2722 case GL_RENDERBUFFER_SAMPLES
:
2723 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_framebuffer_object
)
2724 || _mesa_is_gles3(ctx
)) {
2725 *params
= rb
->NumSamples
;
2729 case GL_RENDERBUFFER_STORAGE_SAMPLES_AMD
:
2730 if (ctx
->Extensions
.AMD_framebuffer_multisample_advanced
) {
2731 *params
= rb
->NumStorageSamples
;
2737 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname=%s)", func
,
2738 _mesa_enum_to_string(pname
));
2743 _mesa_GetRenderbufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2745 GET_CURRENT_CONTEXT(ctx
);
2747 if (target
!= GL_RENDERBUFFER_EXT
) {
2748 _mesa_error(ctx
, GL_INVALID_ENUM
,
2749 "glGetRenderbufferParameterivEXT(target)");
2753 if (!ctx
->CurrentRenderbuffer
) {
2754 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetRenderbufferParameterivEXT"
2755 "(no renderbuffer bound)");
2759 get_render_buffer_parameteriv(ctx
, ctx
->CurrentRenderbuffer
, pname
,
2760 params
, "glGetRenderbufferParameteriv");
2765 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer
, GLenum pname
,
2768 GET_CURRENT_CONTEXT(ctx
);
2770 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
2771 if (!rb
|| rb
== &DummyRenderbuffer
) {
2772 /* ID was reserved, but no real renderbuffer object made yet */
2773 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetNamedRenderbufferParameteriv"
2774 "(invalid renderbuffer %i)", renderbuffer
);
2778 get_render_buffer_parameteriv(ctx
, rb
, pname
, params
,
2779 "glGetNamedRenderbufferParameteriv");
2783 GLboolean GLAPIENTRY
2784 _mesa_IsFramebuffer(GLuint framebuffer
)
2786 GET_CURRENT_CONTEXT(ctx
);
2787 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2789 struct gl_framebuffer
*rb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2790 if (rb
!= NULL
&& rb
!= &DummyFramebuffer
)
2798 * Check if any of the attachments of the given framebuffer are textures
2799 * (render to texture). Call ctx->Driver.RenderTexture() for such
2803 check_begin_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2806 assert(ctx
->Driver
.RenderTexture
);
2808 if (_mesa_is_winsys_fbo(fb
))
2809 return; /* can't render to texture with winsys framebuffers */
2811 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2812 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2813 if (att
->Texture
&& att
->Renderbuffer
->TexImage
2814 && driver_RenderTexture_is_safe(att
)) {
2815 ctx
->Driver
.RenderTexture(ctx
, fb
, att
);
2822 * Examine all the framebuffer's attachments to see if any are textures.
2823 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2824 * notify the device driver that the texture image may have changed.
2827 check_end_texture_render(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
2829 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2830 if (_mesa_is_winsys_fbo(fb
) && !ctx
->Driver
.BindRenderbufferTexImage
)
2833 if (ctx
->Driver
.FinishRenderTexture
) {
2835 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2836 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2837 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
2838 if (rb
&& rb
->NeedsFinishRenderTexture
) {
2839 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
2847 bind_framebuffer(GLenum target
, GLuint framebuffer
)
2849 struct gl_framebuffer
*newDrawFb
, *newReadFb
;
2850 GLboolean bindReadBuf
, bindDrawBuf
;
2851 GET_CURRENT_CONTEXT(ctx
);
2854 case GL_DRAW_FRAMEBUFFER_EXT
:
2855 bindDrawBuf
= GL_TRUE
;
2856 bindReadBuf
= GL_FALSE
;
2858 case GL_READ_FRAMEBUFFER_EXT
:
2859 bindDrawBuf
= GL_FALSE
;
2860 bindReadBuf
= GL_TRUE
;
2862 case GL_FRAMEBUFFER_EXT
:
2863 bindDrawBuf
= GL_TRUE
;
2864 bindReadBuf
= GL_TRUE
;
2867 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindFramebufferEXT(target)");
2872 /* Binding a user-created framebuffer object */
2873 newDrawFb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
2874 if (newDrawFb
== &DummyFramebuffer
) {
2875 /* ID was reserved, but no real framebuffer object made yet */
2878 else if (!newDrawFb
&& ctx
->API
== API_OPENGL_CORE
) {
2879 /* All FBO IDs must be Gen'd */
2880 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2881 "glBindFramebuffer(non-gen name)");
2886 /* create new framebuffer object */
2887 newDrawFb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffer
);
2889 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindFramebufferEXT");
2892 _mesa_HashInsert(ctx
->Shared
->FrameBuffers
, framebuffer
, newDrawFb
);
2894 newReadFb
= newDrawFb
;
2897 /* Binding the window system framebuffer (which was originally set
2898 * with MakeCurrent).
2900 newDrawFb
= ctx
->WinSysDrawBuffer
;
2901 newReadFb
= ctx
->WinSysReadBuffer
;
2904 _mesa_bind_framebuffers(ctx
,
2905 bindDrawBuf
? newDrawFb
: ctx
->DrawBuffer
,
2906 bindReadBuf
? newReadFb
: ctx
->ReadBuffer
);
2910 _mesa_bind_framebuffers(struct gl_context
*ctx
,
2911 struct gl_framebuffer
*newDrawFb
,
2912 struct gl_framebuffer
*newReadFb
)
2914 struct gl_framebuffer
*const oldDrawFb
= ctx
->DrawBuffer
;
2915 struct gl_framebuffer
*const oldReadFb
= ctx
->ReadBuffer
;
2916 const bool bindDrawBuf
= oldDrawFb
!= newDrawFb
;
2917 const bool bindReadBuf
= oldReadFb
!= newReadFb
;
2920 assert(newDrawFb
!= &DummyFramebuffer
);
2923 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2925 * We also check if we're beginning and/or ending render-to-texture.
2926 * When a framebuffer with texture attachments is unbound, call
2927 * ctx->Driver.FinishRenderTexture().
2928 * When a framebuffer with texture attachments is bound, call
2929 * ctx->Driver.RenderTexture().
2931 * Note that if the ReadBuffer has texture attachments we don't consider
2932 * that a render-to-texture case.
2935 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2937 /* check if old readbuffer was render-to-texture */
2938 check_end_texture_render(ctx
, oldReadFb
);
2940 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, newReadFb
);
2944 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2945 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
2947 /* check if old framebuffer had any texture attachments */
2949 check_end_texture_render(ctx
, oldDrawFb
);
2951 /* check if newly bound framebuffer has any texture attachments */
2952 check_begin_texture_render(ctx
, newDrawFb
);
2954 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, newDrawFb
);
2957 if ((bindDrawBuf
|| bindReadBuf
) && ctx
->Driver
.BindFramebuffer
) {
2958 /* The few classic drivers that actually hook this function really only
2959 * want to know if the draw framebuffer changed.
2961 ctx
->Driver
.BindFramebuffer(ctx
,
2962 bindDrawBuf
? GL_FRAMEBUFFER
: GL_READ_FRAMEBUFFER
,
2963 newDrawFb
, newReadFb
);
2968 _mesa_BindFramebuffer(GLenum target
, GLuint framebuffer
)
2970 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2971 * point, but they allow the use of user-generated names.
2973 bind_framebuffer(target
, framebuffer
);
2978 _mesa_BindFramebufferEXT(GLenum target
, GLuint framebuffer
)
2980 bind_framebuffer(target
, framebuffer
);
2985 _mesa_DeleteFramebuffers(GLsizei n
, const GLuint
*framebuffers
)
2988 GET_CURRENT_CONTEXT(ctx
);
2991 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteFramebuffers(n < 0)");
2995 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
2997 for (i
= 0; i
< n
; i
++) {
2998 if (framebuffers
[i
] > 0) {
2999 struct gl_framebuffer
*fb
;
3000 fb
= _mesa_lookup_framebuffer(ctx
, framebuffers
[i
]);
3002 assert(fb
== &DummyFramebuffer
|| fb
->Name
== framebuffers
[i
]);
3004 /* check if deleting currently bound framebuffer object */
3005 if (fb
== ctx
->DrawBuffer
) {
3007 assert(fb
->RefCount
>= 2);
3008 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, 0);
3010 if (fb
== ctx
->ReadBuffer
) {
3012 assert(fb
->RefCount
>= 2);
3013 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, 0);
3016 /* remove from hash table immediately, to free the ID */
3017 _mesa_HashRemove(ctx
->Shared
->FrameBuffers
, framebuffers
[i
]);
3019 if (fb
!= &DummyFramebuffer
) {
3020 /* But the object will not be freed until it's no longer
3021 * bound in any context.
3023 _mesa_reference_framebuffer(&fb
, NULL
);
3032 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
3033 * It is not exposed to the rest of Mesa to encourage the use of
3034 * nameless buffers in driver internals.
3037 create_framebuffers(GLsizei n
, GLuint
*framebuffers
, bool dsa
)
3039 GET_CURRENT_CONTEXT(ctx
);
3042 struct gl_framebuffer
*fb
;
3044 const char *func
= dsa
? "glCreateFramebuffers" : "glGenFramebuffers";
3047 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
3054 _mesa_HashLockMutex(ctx
->Shared
->FrameBuffers
);
3056 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->FrameBuffers
, n
);
3058 for (i
= 0; i
< n
; i
++) {
3059 GLuint name
= first
+ i
;
3060 framebuffers
[i
] = name
;
3063 fb
= ctx
->Driver
.NewFramebuffer(ctx
, framebuffers
[i
]);
3065 _mesa_HashUnlockMutex(ctx
->Shared
->FrameBuffers
);
3066 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
3071 fb
= &DummyFramebuffer
;
3073 _mesa_HashInsertLocked(ctx
->Shared
->FrameBuffers
, name
, fb
);
3076 _mesa_HashUnlockMutex(ctx
->Shared
->FrameBuffers
);
3081 _mesa_GenFramebuffers(GLsizei n
, GLuint
*framebuffers
)
3083 create_framebuffers(n
, framebuffers
, false);
3088 _mesa_CreateFramebuffers(GLsizei n
, GLuint
*framebuffers
)
3090 create_framebuffers(n
, framebuffers
, true);
3095 _mesa_check_framebuffer_status(struct gl_context
*ctx
,
3096 struct gl_framebuffer
*buffer
)
3098 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
3100 if (_mesa_is_winsys_fbo(buffer
)) {
3101 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
3102 if (buffer
!= &IncompleteFramebuffer
) {
3103 return GL_FRAMEBUFFER_COMPLETE_EXT
;
3105 return GL_FRAMEBUFFER_UNDEFINED
;
3109 /* No need to flush here */
3111 if (buffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE
) {
3112 _mesa_test_framebuffer_completeness(ctx
, buffer
);
3115 return buffer
->_Status
;
3120 _mesa_CheckFramebufferStatus_no_error(GLenum target
)
3122 GET_CURRENT_CONTEXT(ctx
);
3124 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
3125 return _mesa_check_framebuffer_status(ctx
, fb
);
3130 _mesa_CheckFramebufferStatus(GLenum target
)
3132 struct gl_framebuffer
*fb
;
3133 GET_CURRENT_CONTEXT(ctx
);
3135 if (MESA_VERBOSE
& VERBOSE_API
)
3136 _mesa_debug(ctx
, "glCheckFramebufferStatus(%s)\n",
3137 _mesa_enum_to_string(target
));
3139 fb
= get_framebuffer_target(ctx
, target
);
3141 _mesa_error(ctx
, GL_INVALID_ENUM
,
3142 "glCheckFramebufferStatus(invalid target %s)",
3143 _mesa_enum_to_string(target
));
3147 return _mesa_check_framebuffer_status(ctx
, fb
);
3152 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer
, GLenum target
)
3154 struct gl_framebuffer
*fb
;
3155 GET_CURRENT_CONTEXT(ctx
);
3157 /* Validate the target (for conformance's sake) and grab a reference to the
3158 * default framebuffer in case framebuffer = 0.
3159 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
3160 * (30.10.2014, PDF page 336) says:
3161 * "If framebuffer is zero, then the status of the default read or
3162 * draw framebuffer (as determined by target) is returned."
3165 case GL_DRAW_FRAMEBUFFER
:
3166 case GL_FRAMEBUFFER
:
3167 fb
= ctx
->WinSysDrawBuffer
;
3169 case GL_READ_FRAMEBUFFER
:
3170 fb
= ctx
->WinSysReadBuffer
;
3173 _mesa_error(ctx
, GL_INVALID_ENUM
,
3174 "glCheckNamedFramebufferStatus(invalid target %s)",
3175 _mesa_enum_to_string(target
));
3180 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
3181 "glCheckNamedFramebufferStatus");
3186 return _mesa_check_framebuffer_status(ctx
, fb
);
3191 _mesa_CheckNamedFramebufferStatusEXT(GLuint framebuffer
, GLenum target
)
3193 struct gl_framebuffer
*fb
;
3194 GET_CURRENT_CONTEXT(ctx
);
3197 case GL_DRAW_FRAMEBUFFER
:
3198 case GL_FRAMEBUFFER
:
3199 case GL_READ_FRAMEBUFFER
:
3202 _mesa_error(ctx
, GL_INVALID_ENUM
,
3203 "glCheckNamedFramebufferStatusEXT(invalid target %s)",
3204 _mesa_enum_to_string(target
));
3208 if (framebuffer
== 0) {
3209 return _mesa_CheckNamedFramebufferStatus(0, target
);
3212 fb
= _mesa_lookup_framebuffer_dsa(ctx
, framebuffer
,
3213 "glCheckNamedFramebufferStatusEXT");
3217 return _mesa_check_framebuffer_status(ctx
, fb
);
3222 * Replicate the src attachment point. Used by framebuffer_texture() when
3223 * the same texture is attached at GL_DEPTH_ATTACHMENT and
3224 * GL_STENCIL_ATTACHMENT.
3227 reuse_framebuffer_texture_attachment(struct gl_framebuffer
*fb
,
3228 gl_buffer_index dst
,
3229 gl_buffer_index src
)
3231 struct gl_renderbuffer_attachment
*dst_att
= &fb
->Attachment
[dst
];
3232 struct gl_renderbuffer_attachment
*src_att
= &fb
->Attachment
[src
];
3234 assert(src_att
->Texture
!= NULL
);
3235 assert(src_att
->Renderbuffer
!= NULL
);
3237 _mesa_reference_texobj(&dst_att
->Texture
, src_att
->Texture
);
3238 _mesa_reference_renderbuffer(&dst_att
->Renderbuffer
, src_att
->Renderbuffer
);
3239 dst_att
->Type
= src_att
->Type
;
3240 dst_att
->Complete
= src_att
->Complete
;
3241 dst_att
->TextureLevel
= src_att
->TextureLevel
;
3242 dst_att
->CubeMapFace
= src_att
->CubeMapFace
;
3243 dst_att
->Zoffset
= src_att
->Zoffset
;
3244 dst_att
->Layered
= src_att
->Layered
;
3248 static struct gl_texture_object
*
3249 get_texture_for_framebuffer(struct gl_context
*ctx
, GLuint texture
)
3254 return _mesa_lookup_texture(ctx
, texture
);
3259 * Common code called by gl*FramebufferTexture*() to retrieve the correct
3260 * texture object pointer.
3262 * \param texObj where the pointer to the texture object is returned. Note
3263 * that a successful call may return texObj = NULL.
3265 * \return true if no errors, false if errors
3268 get_texture_for_framebuffer_err(struct gl_context
*ctx
, GLuint texture
,
3269 bool layered
, const char *caller
,
3270 struct gl_texture_object
**texObj
)
3272 *texObj
= NULL
; /* This will get returned if texture = 0. */
3277 *texObj
= _mesa_lookup_texture(ctx
, texture
);
3278 if (*texObj
== NULL
|| (*texObj
)->Target
== 0) {
3279 /* Can't render to a non-existent texture object.
3281 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
3282 * Managing Framebuffer Objects specifies a different error
3283 * depending upon the calling function (PDF pages 325-328).
3284 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
3285 * value, while the other commands throw invalid operation (where
3286 * layered = GL_FALSE).
3288 const GLenum error
= layered
? GL_INVALID_VALUE
:
3289 GL_INVALID_OPERATION
;
3290 _mesa_error(ctx
, error
,
3291 "%s(non-existent texture %u)", caller
, texture
);
3300 * Common code called by gl*FramebufferTexture() to verify the texture target
3301 * and decide whether or not the attachment should truly be considered
3304 * \param layered true if attachment should be considered layered, false if
3307 * \return true if no errors, false if errors
3310 check_layered_texture_target(struct gl_context
*ctx
, GLenum target
,
3311 const char *caller
, GLboolean
*layered
)
3317 case GL_TEXTURE_1D_ARRAY_EXT
:
3318 case GL_TEXTURE_2D_ARRAY_EXT
:
3319 case GL_TEXTURE_CUBE_MAP
:
3320 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3321 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3325 case GL_TEXTURE_RECTANGLE
:
3326 case GL_TEXTURE_2D_MULTISAMPLE
:
3327 /* These texture types are valid to pass to
3328 * glFramebufferTexture(), but since they aren't layered, it
3329 * is equivalent to calling glFramebufferTexture{1D,2D}().
3331 *layered
= GL_FALSE
;
3335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3336 "%s(invalid texture target %s)", caller
,
3337 _mesa_enum_to_string(target
));
3343 * Common code called by gl*FramebufferTextureLayer() to verify the texture
3346 * \return true if no errors, false if errors
3349 check_texture_target(struct gl_context
*ctx
, GLenum target
,
3352 /* We're being called by glFramebufferTextureLayer().
3353 * The only legal texture types for that function are 3D,
3354 * cube-map, and 1D/2D/cube-map array textures.
3356 * We don't need to check for GL_ARB_texture_cube_map_array because the
3357 * application wouldn't have been able to create a texture with a
3358 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
3362 case GL_TEXTURE_1D_ARRAY
:
3363 case GL_TEXTURE_2D_ARRAY
:
3364 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3365 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3367 case GL_TEXTURE_CUBE_MAP
:
3368 /* GL_TEXTURE_CUBE_MAP is only allowed by OpenGL 4.5 here, which
3369 * includes the DSA API.
3371 * Because DSA is only enabled for GL 3.1+ and this can be called
3372 * from _mesa_FramebufferTextureLayer in compatibility profile,
3373 * we need to check the version.
3375 return _mesa_is_desktop_gl(ctx
) && ctx
->Version
>= 31;
3378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3379 "%s(invalid texture target %s)", caller
,
3380 _mesa_enum_to_string(target
));
3386 * Common code called by glFramebufferTexture*D() to verify the texture
3389 * \return true if no errors, false if errors
3392 check_textarget(struct gl_context
*ctx
, int dims
, GLenum target
,
3393 GLenum textarget
, const char *caller
)
3397 switch (textarget
) {
3401 case GL_TEXTURE_1D_ARRAY
:
3402 err
= dims
!= 1 || !ctx
->Extensions
.EXT_texture_array
;
3407 case GL_TEXTURE_2D_ARRAY
:
3408 err
= dims
!= 2 || !ctx
->Extensions
.EXT_texture_array
||
3409 (_mesa_is_gles(ctx
) && ctx
->Version
< 30);
3411 case GL_TEXTURE_2D_MULTISAMPLE
:
3412 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3414 !ctx
->Extensions
.ARB_texture_multisample
||
3415 (_mesa_is_gles(ctx
) && ctx
->Version
< 31);
3417 case GL_TEXTURE_RECTANGLE
:
3418 err
= dims
!= 2 || _mesa_is_gles(ctx
) ||
3419 !ctx
->Extensions
.NV_texture_rectangle
;
3421 case GL_TEXTURE_CUBE_MAP
:
3422 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3425 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3426 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3427 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3428 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3429 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3430 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3431 err
= dims
!= 2 || !ctx
->Extensions
.ARB_texture_cube_map
;
3437 _mesa_error(ctx
, GL_INVALID_ENUM
,
3438 "%s(unknown textarget 0x%x)", caller
, textarget
);
3443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3444 "%s(invalid textarget %s)",
3445 caller
, _mesa_enum_to_string(textarget
));
3449 /* Make sure textarget is consistent with the texture's type */
3450 err
= (target
== GL_TEXTURE_CUBE_MAP
) ?
3451 !_mesa_is_cube_face(textarget
): (target
!= textarget
);
3454 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3455 "%s(mismatched texture target)", caller
);
3464 * Common code called by gl*FramebufferTextureLayer() and
3465 * glFramebufferTexture3D() to validate the layer.
3467 * \return true if no errors, false if errors
3470 check_layer(struct gl_context
*ctx
, GLenum target
, GLint layer
,
3473 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3476 * "An INVALID_VALUE error is generated if texture is non-zero
3477 * and layer is negative."
3480 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(layer %d < 0)", caller
, layer
);
3484 if (target
== GL_TEXTURE_3D
) {
3485 const GLuint maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
3486 if (layer
>= maxSize
) {
3487 _mesa_error(ctx
, GL_INVALID_VALUE
,
3488 "%s(invalid layer %u)", caller
, layer
);
3492 else if ((target
== GL_TEXTURE_1D_ARRAY
) ||
3493 (target
== GL_TEXTURE_2D_ARRAY
) ||
3494 (target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ||
3495 (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
3496 if (layer
>= ctx
->Const
.MaxArrayTextureLayers
) {
3497 _mesa_error(ctx
, GL_INVALID_VALUE
,
3498 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3503 else if (target
== GL_TEXTURE_CUBE_MAP
) {
3505 _mesa_error(ctx
, GL_INVALID_VALUE
,
3506 "%s(layer %u >= 6)", caller
, layer
);
3516 * Common code called by all gl*FramebufferTexture*() entry points to verify
3519 * \return true if no errors, false if errors
3522 check_level(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3523 GLenum target
, GLint level
, const char *caller
)
3525 /* Section 9.2.8 of the OpenGL 4.6 specification says:
3527 * "If texture refers to an immutable-format texture, level must be
3528 * greater than or equal to zero and smaller than the value of
3529 * TEXTURE_VIEW_NUM_LEVELS for texture."
3531 const int max_levels
= texObj
->Immutable
? texObj
->ImmutableLevels
:
3532 _mesa_max_texture_levels(ctx
, target
);
3534 if (level
< 0 || level
>= max_levels
) {
3535 _mesa_error(ctx
, GL_INVALID_VALUE
,
3536 "%s(invalid level %d)", caller
, level
);
3544 struct gl_renderbuffer_attachment
*
3545 _mesa_get_and_validate_attachment(struct gl_context
*ctx
,
3546 struct gl_framebuffer
*fb
,
3547 GLenum attachment
, const char *caller
)
3549 /* The window-system framebuffer object is immutable */
3550 if (_mesa_is_winsys_fbo(fb
)) {
3551 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(window-system framebuffer)",
3556 /* Not a hash lookup, so we can afford to get the attachment here. */
3557 bool is_color_attachment
;
3558 struct gl_renderbuffer_attachment
*att
=
3559 get_attachment(ctx
, fb
, attachment
, &is_color_attachment
);
3561 if (is_color_attachment
) {
3562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3563 "%s(invalid color attachment %s)", caller
,
3564 _mesa_enum_to_string(attachment
));
3566 _mesa_error(ctx
, GL_INVALID_ENUM
,
3567 "%s(invalid attachment %s)", caller
,
3568 _mesa_enum_to_string(attachment
));
3578 _mesa_framebuffer_texture(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
3580 struct gl_renderbuffer_attachment
*att
,
3581 struct gl_texture_object
*texObj
, GLenum textarget
,
3582 GLint level
, GLsizei samples
,
3583 GLuint layer
, GLboolean layered
)
3585 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3587 simple_mtx_lock(&fb
->Mutex
);
3589 if (attachment
== GL_DEPTH_ATTACHMENT
&&
3590 texObj
== fb
->Attachment
[BUFFER_STENCIL
].Texture
&&
3591 level
== fb
->Attachment
[BUFFER_STENCIL
].TextureLevel
&&
3592 _mesa_tex_target_to_face(textarget
) ==
3593 fb
->Attachment
[BUFFER_STENCIL
].CubeMapFace
&&
3594 samples
== fb
->Attachment
[BUFFER_STENCIL
].NumSamples
&&
3595 layer
== fb
->Attachment
[BUFFER_STENCIL
].Zoffset
) {
3596 /* The texture object is already attached to the stencil attachment
3597 * point. Don't create a new renderbuffer; just reuse the stencil
3598 * attachment's. This is required to prevent a GL error in
3599 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3601 reuse_framebuffer_texture_attachment(fb
, BUFFER_DEPTH
,
3603 } else if (attachment
== GL_STENCIL_ATTACHMENT
&&
3604 texObj
== fb
->Attachment
[BUFFER_DEPTH
].Texture
&&
3605 level
== fb
->Attachment
[BUFFER_DEPTH
].TextureLevel
&&
3606 _mesa_tex_target_to_face(textarget
) ==
3607 fb
->Attachment
[BUFFER_DEPTH
].CubeMapFace
&&
3608 samples
== fb
->Attachment
[BUFFER_DEPTH
].NumSamples
&&
3609 layer
== fb
->Attachment
[BUFFER_DEPTH
].Zoffset
) {
3610 /* As above, but with depth and stencil transposed. */
3611 reuse_framebuffer_texture_attachment(fb
, BUFFER_STENCIL
,
3614 set_texture_attachment(ctx
, fb
, att
, texObj
, textarget
,
3615 level
, samples
, layer
, layered
);
3617 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3618 /* Above we created a new renderbuffer and attached it to the
3619 * depth attachment point. Now attach it to the stencil attachment
3622 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3623 reuse_framebuffer_texture_attachment(fb
,BUFFER_STENCIL
,
3628 /* Set the render-to-texture flag. We'll check this flag in
3629 * glTexImage() and friends to determine if we need to revalidate
3630 * any FBOs that might be rendering into this texture.
3631 * This flag never gets cleared since it's non-trivial to determine
3632 * when all FBOs might be done rendering to this texture. That's OK
3633 * though since it's uncommon to render to a texture then repeatedly
3634 * call glTexImage() to change images in the texture.
3636 texObj
->_RenderToTexture
= GL_TRUE
;
3639 remove_attachment(ctx
, att
);
3640 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
3641 assert(att
== &fb
->Attachment
[BUFFER_DEPTH
]);
3642 remove_attachment(ctx
, &fb
->Attachment
[BUFFER_STENCIL
]);
3646 invalidate_framebuffer(fb
);
3648 simple_mtx_unlock(&fb
->Mutex
);
3653 framebuffer_texture_with_dims_no_error(GLenum target
, GLenum attachment
,
3654 GLenum textarget
, GLuint texture
,
3655 GLint level
, GLint layer
)
3657 GET_CURRENT_CONTEXT(ctx
);
3659 /* Get the framebuffer object */
3660 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
3662 /* Get the texture object */
3663 struct gl_texture_object
*texObj
=
3664 get_texture_for_framebuffer(ctx
, texture
);
3666 struct gl_renderbuffer_attachment
*att
=
3667 get_attachment(ctx
, fb
, attachment
, NULL
);
3669 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3670 level
, 0, layer
, GL_FALSE
);
3675 framebuffer_texture_with_dims(int dims
, GLenum target
, GLuint framebuffer
,
3676 GLenum attachment
, GLenum textarget
,
3677 GLuint texture
, GLint level
, GLsizei samples
,
3678 GLint layer
, const char *caller
, bool dsa
)
3680 GET_CURRENT_CONTEXT(ctx
);
3681 struct gl_framebuffer
*fb
;
3682 struct gl_texture_object
*texObj
;
3684 /* Get the framebuffer object */
3686 fb
= _mesa_lookup_framebuffer_dsa(ctx
, framebuffer
, caller
);
3688 fb
= get_framebuffer_target(ctx
, target
);
3691 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
3692 _mesa_enum_to_string(target
));
3696 /* Get the texture object */
3697 if (!get_texture_for_framebuffer_err(ctx
, texture
, false, caller
, &texObj
))
3701 if (!check_textarget(ctx
, dims
, texObj
->Target
, textarget
, caller
))
3704 if ((dims
== 3) && !check_layer(ctx
, texObj
->Target
, layer
, caller
))
3707 if (!check_level(ctx
, texObj
, textarget
, level
, caller
))
3711 struct gl_renderbuffer_attachment
*att
=
3712 _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, caller
);
3716 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3717 level
, samples
, layer
, GL_FALSE
);
3722 _mesa_FramebufferTexture1D_no_error(GLenum target
, GLenum attachment
,
3723 GLenum textarget
, GLuint texture
,
3726 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3732 _mesa_FramebufferTexture1D(GLenum target
, GLenum attachment
,
3733 GLenum textarget
, GLuint texture
, GLint level
)
3735 framebuffer_texture_with_dims(1, target
, 0, attachment
, textarget
, texture
,
3736 level
, 0, 0, "glFramebufferTexture1D", false);
3741 _mesa_FramebufferTexture2D_no_error(GLenum target
, GLenum attachment
,
3742 GLenum textarget
, GLuint texture
,
3745 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3751 _mesa_FramebufferTexture2D(GLenum target
, GLenum attachment
,
3752 GLenum textarget
, GLuint texture
, GLint level
)
3754 framebuffer_texture_with_dims(2, target
, 0, attachment
, textarget
, texture
,
3755 level
, 0, 0, "glFramebufferTexture2D", false);
3760 _mesa_FramebufferTexture2DMultisampleEXT(GLenum target
, GLenum attachment
,
3761 GLenum textarget
, GLuint texture
,
3762 GLint level
, GLsizei samples
)
3764 framebuffer_texture_with_dims(2, target
, 0, attachment
, textarget
, texture
,
3766 "glFramebufferTexture2DMultisampleEXT",
3772 _mesa_FramebufferTexture3D_no_error(GLenum target
, GLenum attachment
,
3773 GLenum textarget
, GLuint texture
,
3774 GLint level
, GLint layer
)
3776 framebuffer_texture_with_dims_no_error(target
, attachment
, textarget
,
3777 texture
, level
, layer
);
3782 _mesa_FramebufferTexture3D(GLenum target
, GLenum attachment
,
3783 GLenum textarget
, GLuint texture
,
3784 GLint level
, GLint layer
)
3786 framebuffer_texture_with_dims(3, target
, 0, attachment
, textarget
, texture
,
3787 level
, 0, layer
, "glFramebufferTexture3D", false);
3791 static ALWAYS_INLINE
void
3792 frame_buffer_texture(GLuint framebuffer
, GLenum target
,
3793 GLenum attachment
, GLuint texture
,
3794 GLint level
, GLint layer
, const char *func
,
3795 bool dsa
, bool no_error
, bool check_layered
)
3797 GET_CURRENT_CONTEXT(ctx
);
3798 GLboolean layered
= GL_FALSE
;
3800 if (!no_error
&& check_layered
) {
3801 if (!_mesa_has_geometry_shaders(ctx
)) {
3802 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3803 "unsupported function (%s) called", func
);
3808 /* Get the framebuffer object */
3809 struct gl_framebuffer
*fb
;
3812 fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
3814 fb
= get_framebuffer_target(ctx
, target
);
3818 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
, func
);
3822 fb
= get_framebuffer_target(ctx
, target
);
3824 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)",
3825 func
, _mesa_enum_to_string(target
));
3831 /* Get the texture object and framebuffer attachment*/
3832 struct gl_renderbuffer_attachment
*att
;
3833 struct gl_texture_object
*texObj
;
3835 texObj
= get_texture_for_framebuffer(ctx
, texture
);
3836 att
= get_attachment(ctx
, fb
, attachment
, NULL
);
3838 if (!get_texture_for_framebuffer_err(ctx
, texture
, check_layered
, func
,
3842 att
= _mesa_get_and_validate_attachment(ctx
, fb
, attachment
, func
);
3847 GLenum textarget
= 0;
3849 if (check_layered
) {
3850 /* We do this regardless of no_error because this sets layered */
3851 if (!check_layered_texture_target(ctx
, texObj
->Target
, func
,
3857 if (!check_layered
) {
3858 if (!check_texture_target(ctx
, texObj
->Target
, func
))
3861 if (!check_layer(ctx
, texObj
->Target
, layer
, func
))
3865 if (!check_level(ctx
, texObj
, texObj
->Target
, level
, func
))
3869 if (!check_layered
&& texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3870 assert(layer
>= 0 && layer
< 6);
3871 textarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ layer
;
3876 _mesa_framebuffer_texture(ctx
, fb
, attachment
, att
, texObj
, textarget
,
3877 level
, 0, layer
, layered
);
3881 _mesa_FramebufferTextureLayer_no_error(GLenum target
, GLenum attachment
,
3882 GLuint texture
, GLint level
,
3885 frame_buffer_texture(0, target
, attachment
, texture
, level
, layer
,
3886 "glFramebufferTextureLayer", false, true, false);
3891 _mesa_FramebufferTextureLayer(GLenum target
, GLenum attachment
,
3892 GLuint texture
, GLint level
, GLint layer
)
3894 frame_buffer_texture(0, target
, attachment
, texture
, level
, layer
,
3895 "glFramebufferTextureLayer", false, false, false);
3900 _mesa_NamedFramebufferTextureLayer_no_error(GLuint framebuffer
,
3902 GLuint texture
, GLint level
,
3905 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, layer
,
3906 "glNamedFramebufferTextureLayer", true, true, false);
3911 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer
, GLenum attachment
,
3912 GLuint texture
, GLint level
, GLint layer
)
3914 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, layer
,
3915 "glNamedFramebufferTextureLayer", true, false, false);
3920 _mesa_FramebufferTexture_no_error(GLenum target
, GLenum attachment
,
3921 GLuint texture
, GLint level
)
3923 frame_buffer_texture(0, target
, attachment
, texture
, level
, 0,
3924 "glFramebufferTexture", false, true, true);
3929 _mesa_FramebufferTexture(GLenum target
, GLenum attachment
,
3930 GLuint texture
, GLint level
)
3932 frame_buffer_texture(0, target
, attachment
, texture
, level
, 0,
3933 "glFramebufferTexture", false, false, true);
3937 _mesa_NamedFramebufferTexture_no_error(GLuint framebuffer
, GLenum attachment
,
3938 GLuint texture
, GLint level
)
3940 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, 0,
3941 "glNamedFramebufferTexture", true, true, true);
3946 _mesa_NamedFramebufferTexture(GLuint framebuffer
, GLenum attachment
,
3947 GLuint texture
, GLint level
)
3949 frame_buffer_texture(framebuffer
, 0, attachment
, texture
, level
, 0,
3950 "glNamedFramebufferTexture", true, false, true);
3955 _mesa_NamedFramebufferTexture1DEXT(GLuint framebuffer
, GLenum attachment
,
3956 GLenum textarget
, GLuint texture
, GLint level
)
3958 framebuffer_texture_with_dims(1, GL_FRAMEBUFFER
, framebuffer
, attachment
,
3959 textarget
, texture
, level
, 0, 0,
3960 "glNamedFramebufferTexture1DEXT", true);
3965 _mesa_NamedFramebufferTexture2DEXT(GLuint framebuffer
, GLenum attachment
,
3966 GLenum textarget
, GLuint texture
, GLint level
)
3968 framebuffer_texture_with_dims(2, GL_FRAMEBUFFER
, framebuffer
, attachment
,
3969 textarget
, texture
, level
, 0, 0,
3970 "glNamedFramebufferTexture2DEXT", true);
3975 _mesa_NamedFramebufferTexture3DEXT(GLuint framebuffer
, GLenum attachment
,
3976 GLenum textarget
, GLuint texture
,
3977 GLint level
, GLint zoffset
)
3979 framebuffer_texture_with_dims(3, GL_FRAMEBUFFER
, framebuffer
, attachment
,
3980 textarget
, texture
, level
, 0, zoffset
,
3981 "glNamedFramebufferTexture3DEXT", true);
3986 _mesa_framebuffer_renderbuffer(struct gl_context
*ctx
,
3987 struct gl_framebuffer
*fb
,
3989 struct gl_renderbuffer
*rb
)
3991 assert(!_mesa_is_winsys_fbo(fb
));
3993 FLUSH_VERTICES(ctx
, _NEW_BUFFERS
);
3995 assert(ctx
->Driver
.FramebufferRenderbuffer
);
3996 ctx
->Driver
.FramebufferRenderbuffer(ctx
, fb
, attachment
, rb
);
3998 /* Some subsequent GL commands may depend on the framebuffer's visual
3999 * after the binding is updated. Update visual info now.
4001 _mesa_update_framebuffer_visual(ctx
, fb
);
4004 static ALWAYS_INLINE
void
4005 framebuffer_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
4006 GLenum attachment
, GLenum renderbuffertarget
,
4007 GLuint renderbuffer
, const char *func
, bool no_error
)
4009 struct gl_renderbuffer_attachment
*att
;
4010 struct gl_renderbuffer
*rb
;
4011 bool is_color_attachment
;
4013 if (!no_error
&& renderbuffertarget
!= GL_RENDERBUFFER
) {
4014 _mesa_error(ctx
, GL_INVALID_ENUM
,
4015 "%s(renderbuffertarget is not GL_RENDERBUFFER)", func
);
4021 rb
= _mesa_lookup_renderbuffer_err(ctx
, renderbuffer
, func
);
4025 rb
= _mesa_lookup_renderbuffer(ctx
, renderbuffer
);
4028 /* remove renderbuffer attachment */
4033 if (_mesa_is_winsys_fbo(fb
)) {
4034 /* Can't attach new renderbuffers to a window system framebuffer */
4035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4036 "%s(window-system framebuffer)", func
);
4040 att
= get_attachment(ctx
, fb
, attachment
, &is_color_attachment
);
4043 * From OpenGL 4.5 spec, section 9.2.7 "Attaching Renderbuffer Images
4044 * to a Framebuffer":
4046 * "An INVALID_OPERATION error is generated if attachment is
4047 * COLOR_- ATTACHMENTm where m is greater than or equal to the
4048 * value of MAX_COLOR_- ATTACHMENTS ."
4050 * If we are at this point, is because the attachment is not valid, so
4051 * if is_color_attachment is true, is because of the previous reason.
4053 if (is_color_attachment
) {
4054 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4055 "%s(invalid color attachment %s)", func
,
4056 _mesa_enum_to_string(attachment
));
4058 _mesa_error(ctx
, GL_INVALID_ENUM
,
4059 "%s(invalid attachment %s)", func
,
4060 _mesa_enum_to_string(attachment
));
4066 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
&&
4067 rb
&& rb
->Format
!= MESA_FORMAT_NONE
) {
4068 /* make sure the renderbuffer is a depth/stencil format */
4069 const GLenum baseFormat
= _mesa_get_format_base_format(rb
->Format
);
4070 if (baseFormat
!= GL_DEPTH_STENCIL
) {
4071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4072 "%s(renderbuffer is not DEPTH_STENCIL format)", func
);
4078 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
4082 framebuffer_renderbuffer_error(struct gl_context
*ctx
,
4083 struct gl_framebuffer
*fb
, GLenum attachment
,
4084 GLenum renderbuffertarget
,
4085 GLuint renderbuffer
, const char *func
)
4087 framebuffer_renderbuffer(ctx
, fb
, attachment
, renderbuffertarget
,
4088 renderbuffer
, func
, false);
4092 framebuffer_renderbuffer_no_error(struct gl_context
*ctx
,
4093 struct gl_framebuffer
*fb
, GLenum attachment
,
4094 GLenum renderbuffertarget
,
4095 GLuint renderbuffer
, const char *func
)
4097 framebuffer_renderbuffer(ctx
, fb
, attachment
, renderbuffertarget
,
4098 renderbuffer
, func
, true);
4102 _mesa_FramebufferRenderbuffer_no_error(GLenum target
, GLenum attachment
,
4103 GLenum renderbuffertarget
,
4104 GLuint renderbuffer
)
4106 GET_CURRENT_CONTEXT(ctx
);
4108 struct gl_framebuffer
*fb
= get_framebuffer_target(ctx
, target
);
4109 framebuffer_renderbuffer_no_error(ctx
, fb
, attachment
, renderbuffertarget
,
4110 renderbuffer
, "glFramebufferRenderbuffer");
4114 _mesa_FramebufferRenderbuffer(GLenum target
, GLenum attachment
,
4115 GLenum renderbuffertarget
,
4116 GLuint renderbuffer
)
4118 struct gl_framebuffer
*fb
;
4119 GET_CURRENT_CONTEXT(ctx
);
4121 fb
= get_framebuffer_target(ctx
, target
);
4123 _mesa_error(ctx
, GL_INVALID_ENUM
,
4124 "glFramebufferRenderbuffer(invalid target %s)",
4125 _mesa_enum_to_string(target
));
4129 framebuffer_renderbuffer_error(ctx
, fb
, attachment
, renderbuffertarget
,
4130 renderbuffer
, "glFramebufferRenderbuffer");
4134 _mesa_NamedFramebufferRenderbuffer_no_error(GLuint framebuffer
,
4136 GLenum renderbuffertarget
,
4137 GLuint renderbuffer
)
4139 GET_CURRENT_CONTEXT(ctx
);
4141 struct gl_framebuffer
*fb
= _mesa_lookup_framebuffer(ctx
, framebuffer
);
4142 framebuffer_renderbuffer_no_error(ctx
, fb
, attachment
, renderbuffertarget
,
4144 "glNamedFramebufferRenderbuffer");
4148 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer
, GLenum attachment
,
4149 GLenum renderbuffertarget
,
4150 GLuint renderbuffer
)
4152 struct gl_framebuffer
*fb
;
4153 GET_CURRENT_CONTEXT(ctx
);
4155 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4156 "glNamedFramebufferRenderbuffer");
4160 framebuffer_renderbuffer_error(ctx
, fb
, attachment
, renderbuffertarget
,
4162 "glNamedFramebufferRenderbuffer");
4167 _mesa_NamedFramebufferRenderbufferEXT(GLuint framebuffer
, GLenum attachment
,
4168 GLenum renderbuffertarget
,
4169 GLuint renderbuffer
)
4171 struct gl_framebuffer
*fb
;
4172 GET_CURRENT_CONTEXT(ctx
);
4174 fb
= _mesa_lookup_framebuffer_dsa(ctx
, framebuffer
,
4175 "glNamedFramebufferRenderbufferEXT");
4179 framebuffer_renderbuffer_error(ctx
, fb
, attachment
, renderbuffertarget
,
4181 "glNamedFramebufferRenderbuffer");
4186 get_framebuffer_attachment_parameter(struct gl_context
*ctx
,
4187 struct gl_framebuffer
*buffer
,
4188 GLenum attachment
, GLenum pname
,
4189 GLint
*params
, const char *caller
)
4191 const struct gl_renderbuffer_attachment
*att
;
4192 bool is_color_attachment
= false;
4195 /* The error code for an attachment type of GL_NONE differs between APIs.
4197 * From the ES 2.0.25 specification, page 127:
4198 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
4199 * querying any other pname will generate INVALID_ENUM."
4201 * From the OpenGL 3.0 specification, page 337, or identically,
4202 * the OpenGL ES 3.0.4 specification, page 240:
4204 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
4205 * framebuffer is bound to target. In this case querying pname
4206 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
4207 * queries will generate an INVALID_OPERATION error."
4209 err
= ctx
->API
== API_OPENGLES2
&& ctx
->Version
< 30 ?
4210 GL_INVALID_ENUM
: GL_INVALID_OPERATION
;
4212 if (_mesa_is_winsys_fbo(buffer
)) {
4213 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
4216 * "If the framebuffer currently bound to target is zero, then
4217 * INVALID_OPERATION is generated."
4219 * The EXT_framebuffer_object spec has the same wording, and the
4220 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
4223 if ((!_mesa_is_desktop_gl(ctx
) ||
4224 !ctx
->Extensions
.ARB_framebuffer_object
)
4225 && !_mesa_is_gles3(ctx
)) {
4226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4227 "%s(window-system framebuffer)", caller
);
4231 if (_mesa_is_gles3(ctx
) && attachment
!= GL_BACK
&&
4232 attachment
!= GL_DEPTH
&& attachment
!= GL_STENCIL
) {
4233 _mesa_error(ctx
, GL_INVALID_ENUM
,
4234 "%s(invalid attachment %s)", caller
,
4235 _mesa_enum_to_string(attachment
));
4239 /* The specs are not clear about how to handle
4240 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
4241 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
4244 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
4245 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
4247 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
) {
4248 _mesa_error(ctx
, GL_INVALID_ENUM
,
4249 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
4250 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
4251 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller
);
4255 /* the default / window-system FBO */
4256 att
= get_fb0_attachment(ctx
, buffer
, attachment
);
4259 /* user-created framebuffer FBO */
4260 att
= get_attachment(ctx
, buffer
, attachment
, &is_color_attachment
);
4265 * From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries":
4267 * "An INVALID_OPERATION error is generated if a framebuffer object
4268 * is bound to target and attachment is COLOR_ATTACHMENTm where m is
4269 * greater than or equal to the value of MAX_COLOR_ATTACHMENTS."
4271 * If we are at this point, is because the attachment is not valid, so
4272 * if is_color_attachment is true, is because of the previous reason.
4274 if (is_color_attachment
) {
4275 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid color attachment %s)",
4276 caller
, _mesa_enum_to_string(attachment
));
4278 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", caller
,
4279 _mesa_enum_to_string(attachment
));
4284 if (attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
4285 const struct gl_renderbuffer_attachment
*depthAtt
, *stencilAtt
;
4286 if (pname
== GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
) {
4287 /* This behavior is first specified in OpenGL 4.4 specification.
4289 * From the OpenGL 4.4 spec page 275:
4290 * "This query cannot be performed for a combined depth+stencil
4291 * attachment, since it does not have a single format."
4293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4294 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
4295 " is invalid for depth+stencil attachment)", caller
);
4298 /* the depth and stencil attachments must point to the same buffer */
4299 depthAtt
= get_attachment(ctx
, buffer
, GL_DEPTH_ATTACHMENT
, NULL
);
4300 stencilAtt
= get_attachment(ctx
, buffer
, GL_STENCIL_ATTACHMENT
, NULL
);
4301 if (depthAtt
->Renderbuffer
!= stencilAtt
->Renderbuffer
) {
4302 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4303 "%s(DEPTH/STENCIL attachments differ)", caller
);
4308 /* No need to flush here */
4311 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT
:
4312 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
4314 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
4315 * either no framebuffer is bound to target; or the default framebuffer
4316 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
4317 * stencil bits, respectively, is zero."
4319 * Note that we don't need explicit checks on DEPTH and STENCIL, because
4320 * on the case the spec is pointing, att->Type is already NONE, so we
4321 * just need to check att->Type.
4323 *params
= (_mesa_is_winsys_fbo(buffer
) && att
->Type
!= GL_NONE
) ?
4324 GL_FRAMEBUFFER_DEFAULT
: att
->Type
;
4326 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT
:
4327 if (att
->Type
== GL_RENDERBUFFER_EXT
) {
4328 *params
= att
->Renderbuffer
->Name
;
4330 else if (att
->Type
== GL_TEXTURE
) {
4331 *params
= att
->Texture
->Name
;
4334 assert(att
->Type
== GL_NONE
);
4335 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
4338 goto invalid_pname_enum
;
4342 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT
:
4343 if (att
->Type
== GL_TEXTURE
) {
4344 *params
= att
->TextureLevel
;
4346 else if (att
->Type
== GL_NONE
) {
4347 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4348 _mesa_enum_to_string(pname
));
4351 goto invalid_pname_enum
;
4354 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT
:
4355 if (att
->Type
== GL_TEXTURE
) {
4356 if (att
->Texture
&& att
->Texture
->Target
== GL_TEXTURE_CUBE_MAP
) {
4357 *params
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ att
->CubeMapFace
;
4363 else if (att
->Type
== GL_NONE
) {
4364 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4365 _mesa_enum_to_string(pname
));
4368 goto invalid_pname_enum
;
4371 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT
:
4372 if (ctx
->API
== API_OPENGLES
) {
4373 goto invalid_pname_enum
;
4374 } else if (att
->Type
== GL_NONE
) {
4375 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4376 _mesa_enum_to_string(pname
));
4377 } else if (att
->Type
== GL_TEXTURE
) {
4378 if (att
->Texture
&& (att
->Texture
->Target
== GL_TEXTURE_3D
||
4379 att
->Texture
->Target
== GL_TEXTURE_2D_ARRAY
)) {
4380 *params
= att
->Zoffset
;
4387 goto invalid_pname_enum
;
4390 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
:
4391 if ((!_mesa_is_desktop_gl(ctx
) ||
4392 !ctx
->Extensions
.ARB_framebuffer_object
)
4393 && !_mesa_is_gles3(ctx
)) {
4394 goto invalid_pname_enum
;
4396 else if (att
->Type
== GL_NONE
) {
4397 if (_mesa_is_winsys_fbo(buffer
) &&
4398 (attachment
== GL_DEPTH
|| attachment
== GL_STENCIL
)) {
4399 *params
= GL_LINEAR
;
4401 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4402 _mesa_enum_to_string(pname
));
4406 if (ctx
->Extensions
.EXT_sRGB
) {
4407 *params
= (_mesa_is_format_srgb(att
->Renderbuffer
->Format
) ?
4408 GL_SRGB
: GL_LINEAR
);
4411 /* According to ARB_framebuffer_sRGB, we should return LINEAR
4412 * if the sRGB conversion is unsupported. */
4413 *params
= GL_LINEAR
;
4417 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE
:
4418 if ((ctx
->API
!= API_OPENGL_COMPAT
||
4419 !ctx
->Extensions
.ARB_framebuffer_object
)
4420 && ctx
->API
!= API_OPENGL_CORE
4421 && !_mesa_is_gles3(ctx
)) {
4422 goto invalid_pname_enum
;
4424 else if (att
->Type
== GL_NONE
) {
4425 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4426 _mesa_enum_to_string(pname
));
4429 mesa_format format
= att
->Renderbuffer
->Format
;
4431 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
4434 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
4435 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
4436 * generate an INVALID_OPERATION error.
4438 if (_mesa_is_gles3(ctx
) &&
4439 attachment
== GL_DEPTH_STENCIL_ATTACHMENT
) {
4440 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4442 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
4443 "GL_DEPTH_STENCIL_ATTACHMENT)", caller
);
4447 if (format
== MESA_FORMAT_S_UINT8
) {
4451 else if (format
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
) {
4452 /* depends on the attachment parameter */
4453 if (attachment
== GL_STENCIL_ATTACHMENT
) {
4461 *params
= _mesa_get_format_datatype(format
);
4465 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
4466 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
4467 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
4468 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
4469 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
4470 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
4471 if ((!_mesa_is_desktop_gl(ctx
) ||
4472 !ctx
->Extensions
.ARB_framebuffer_object
)
4473 && !_mesa_is_gles3(ctx
)) {
4474 goto invalid_pname_enum
;
4476 else if (att
->Texture
) {
4477 const struct gl_texture_image
*texImage
=
4478 _mesa_select_tex_image(att
->Texture
, att
->Texture
->Target
,
4481 *params
= get_component_bits(pname
, texImage
->_BaseFormat
,
4482 texImage
->TexFormat
);
4488 else if (att
->Renderbuffer
) {
4489 *params
= get_component_bits(pname
, att
->Renderbuffer
->_BaseFormat
,
4490 att
->Renderbuffer
->Format
);
4493 assert(att
->Type
== GL_NONE
);
4494 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4495 _mesa_enum_to_string(pname
));
4498 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED
:
4499 if (!_mesa_has_geometry_shaders(ctx
)) {
4500 goto invalid_pname_enum
;
4501 } else if (att
->Type
== GL_TEXTURE
) {
4502 *params
= att
->Layered
;
4503 } else if (att
->Type
== GL_NONE
) {
4504 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4505 _mesa_enum_to_string(pname
));
4507 goto invalid_pname_enum
;
4510 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT
:
4511 if (!ctx
->Extensions
.EXT_multisampled_render_to_texture
) {
4512 goto invalid_pname_enum
;
4513 } else if (att
->Type
== GL_TEXTURE
) {
4514 *params
= att
->NumSamples
;
4515 } else if (att
->Type
== GL_NONE
) {
4516 _mesa_error(ctx
, err
, "%s(invalid pname %s)", caller
,
4517 _mesa_enum_to_string(pname
));
4519 goto invalid_pname_enum
;
4523 goto invalid_pname_enum
;
4529 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname %s)", caller
,
4530 _mesa_enum_to_string(pname
));
4536 _mesa_GetFramebufferAttachmentParameteriv(GLenum target
, GLenum attachment
,
4537 GLenum pname
, GLint
*params
)
4539 GET_CURRENT_CONTEXT(ctx
);
4540 struct gl_framebuffer
*buffer
;
4542 buffer
= get_framebuffer_target(ctx
, target
);
4544 _mesa_error(ctx
, GL_INVALID_ENUM
,
4545 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
4546 _mesa_enum_to_string(target
));
4550 get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
4552 "glGetFramebufferAttachmentParameteriv");
4557 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer
,
4559 GLenum pname
, GLint
*params
)
4561 GET_CURRENT_CONTEXT(ctx
);
4562 struct gl_framebuffer
*buffer
;
4565 buffer
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4566 "glGetNamedFramebufferAttachmentParameteriv");
4572 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4573 * 4.5 core spec (30.10.2014, PDF page 314):
4574 * "If framebuffer is zero, then the default draw framebuffer is
4577 buffer
= ctx
->WinSysDrawBuffer
;
4580 get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
4582 "glGetNamedFramebufferAttachmentParameteriv");
4587 _mesa_GetNamedFramebufferAttachmentParameterivEXT(GLuint framebuffer
,
4589 GLenum pname
, GLint
*params
)
4591 GET_CURRENT_CONTEXT(ctx
);
4592 struct gl_framebuffer
*buffer
;
4595 buffer
= _mesa_lookup_framebuffer_dsa(ctx
, framebuffer
,
4596 "glGetNamedFramebufferAttachmentParameterivEXT");
4602 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4603 * 4.5 core spec (30.10.2014, PDF page 314):
4604 * "If framebuffer is zero, then the default draw framebuffer is
4607 buffer
= ctx
->WinSysDrawBuffer
;
4610 get_framebuffer_attachment_parameter(ctx
, buffer
, attachment
, pname
,
4612 "glGetNamedFramebufferAttachmentParameterivEXT");
4617 _mesa_NamedFramebufferParameteri(GLuint framebuffer
, GLenum pname
,
4620 GET_CURRENT_CONTEXT(ctx
);
4621 struct gl_framebuffer
*fb
= NULL
;
4623 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
&&
4624 !ctx
->Extensions
.ARB_sample_locations
) {
4625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4626 "glNamedFramebufferParameteri("
4627 "neither ARB_framebuffer_no_attachments nor "
4628 "ARB_sample_locations is available)");
4633 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4634 "glNamedFramebufferParameteri");
4636 fb
= ctx
->WinSysDrawBuffer
;
4640 framebuffer_parameteri(ctx
, fb
, pname
, param
,
4641 "glNamedFramebufferParameteriv");
4647 _mesa_GetFramebufferParameterivEXT(GLuint framebuffer
, GLenum pname
,
4650 GET_CURRENT_CONTEXT(ctx
);
4651 struct gl_framebuffer
*fb
;
4654 fb
= _mesa_lookup_framebuffer_dsa(ctx
, framebuffer
,
4655 "glGetFramebufferParameterivEXT");
4657 fb
= ctx
->WinSysDrawBuffer
;
4660 /* The GL_EXT_direct_state_access says:
4662 * The pname parameter must be one of framebuffer dependent values
4663 * listed in either table 4.nnn (namely DRAW_BUFFER, READ_BUFFER,
4664 * or DRAW_BUFFER0 through DRAW_BUFFER15).
4666 if (pname
== GL_DRAW_BUFFER
) {
4667 *param
= fb
->ColorDrawBuffer
[0];
4670 else if (pname
== GL_READ_BUFFER
) {
4671 *param
= fb
->ColorReadBuffer
;
4673 else if (GL_DRAW_BUFFER0
<= pname
&& pname
<= GL_DRAW_BUFFER15
) {
4674 *param
= fb
->ColorDrawBuffer
[pname
- GL_DRAW_BUFFER0
];
4677 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetFramebufferParameterivEXT(pname)");
4684 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer
, GLenum pname
,
4687 GET_CURRENT_CONTEXT(ctx
);
4688 struct gl_framebuffer
*fb
;
4690 if (!ctx
->Extensions
.ARB_framebuffer_no_attachments
) {
4691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4692 "glNamedFramebufferParameteriv("
4693 "neither ARB_framebuffer_no_attachments nor ARB_sample_locations"
4699 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4700 "glGetNamedFramebufferParameteriv");
4702 fb
= ctx
->WinSysDrawBuffer
;
4705 get_framebuffer_parameteriv(ctx
, fb
, pname
, param
,
4706 "glGetNamedFramebufferParameteriv");
4712 invalidate_framebuffer_storage(struct gl_context
*ctx
,
4713 struct gl_framebuffer
*fb
,
4714 GLsizei numAttachments
,
4715 const GLenum
*attachments
, GLint x
, GLint y
,
4716 GLsizei width
, GLsizei height
, const char *name
)
4720 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4721 * Spec (2.2.2015, PDF page 522) says:
4722 * "An INVALID_VALUE error is generated if numAttachments, width, or
4723 * height is negative."
4725 if (numAttachments
< 0) {
4726 _mesa_error(ctx
, GL_INVALID_VALUE
,
4727 "%s(numAttachments < 0)", name
);
4732 _mesa_error(ctx
, GL_INVALID_VALUE
,
4733 "%s(width < 0)", name
);
4738 _mesa_error(ctx
, GL_INVALID_VALUE
,
4739 "%s(height < 0)", name
);
4743 /* The GL_ARB_invalidate_subdata spec says:
4745 * "If an attachment is specified that does not exist in the
4746 * framebuffer bound to <target>, it is ignored."
4750 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4751 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4752 * INVALID_OPERATION is generated."
4754 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4755 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4756 * set of retrictions).
4758 for (i
= 0; i
< numAttachments
; i
++) {
4759 if (_mesa_is_winsys_fbo(fb
)) {
4760 switch (attachments
[i
]) {
4766 /* Accumulation buffers and auxilary buffers were removed in
4767 * OpenGL 3.1, and they never existed in OpenGL ES.
4769 if (ctx
->API
!= API_OPENGL_COMPAT
)
4779 case GL_FRONT_RIGHT
:
4780 if (!_mesa_is_desktop_gl(ctx
))
4787 switch (attachments
[i
]) {
4788 case GL_DEPTH_ATTACHMENT
:
4789 case GL_STENCIL_ATTACHMENT
:
4791 case GL_DEPTH_STENCIL_ATTACHMENT
:
4792 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4793 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4794 * extension does not make this attachment point valid on ES 2.0.
4796 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
))
4799 case GL_COLOR_ATTACHMENT0
:
4800 case GL_COLOR_ATTACHMENT1
:
4801 case GL_COLOR_ATTACHMENT2
:
4802 case GL_COLOR_ATTACHMENT3
:
4803 case GL_COLOR_ATTACHMENT4
:
4804 case GL_COLOR_ATTACHMENT5
:
4805 case GL_COLOR_ATTACHMENT6
:
4806 case GL_COLOR_ATTACHMENT7
:
4807 case GL_COLOR_ATTACHMENT8
:
4808 case GL_COLOR_ATTACHMENT9
:
4809 case GL_COLOR_ATTACHMENT10
:
4810 case GL_COLOR_ATTACHMENT11
:
4811 case GL_COLOR_ATTACHMENT12
:
4812 case GL_COLOR_ATTACHMENT13
:
4813 case GL_COLOR_ATTACHMENT14
:
4814 case GL_COLOR_ATTACHMENT15
: {
4815 unsigned k
= attachments
[i
] - GL_COLOR_ATTACHMENT0
;
4816 if (k
>= ctx
->Const
.MaxColorAttachments
) {
4817 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4818 "%s(attachment >= max. color attachments)", name
);
4829 /* We don't actually do anything for this yet. Just return after
4830 * validating the parameters and generating the required errors.
4835 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid attachment %s)", name
,
4836 _mesa_enum_to_string(attachments
[i
]));
4840 static struct gl_renderbuffer_attachment
*
4841 get_fb_attachment(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
4842 const GLenum attachment
)
4844 switch (attachment
) {
4846 return &fb
->Attachment
[BUFFER_BACK_LEFT
];
4847 case GL_COLOR_ATTACHMENT0
:
4848 case GL_COLOR_ATTACHMENT1
:
4849 case GL_COLOR_ATTACHMENT2
:
4850 case GL_COLOR_ATTACHMENT3
:
4851 case GL_COLOR_ATTACHMENT4
:
4852 case GL_COLOR_ATTACHMENT5
:
4853 case GL_COLOR_ATTACHMENT6
:
4854 case GL_COLOR_ATTACHMENT7
:
4855 case GL_COLOR_ATTACHMENT8
:
4856 case GL_COLOR_ATTACHMENT9
:
4857 case GL_COLOR_ATTACHMENT10
:
4858 case GL_COLOR_ATTACHMENT11
:
4859 case GL_COLOR_ATTACHMENT12
:
4860 case GL_COLOR_ATTACHMENT13
:
4861 case GL_COLOR_ATTACHMENT14
:
4862 case GL_COLOR_ATTACHMENT15
: {
4863 const unsigned i
= attachment
- GL_COLOR_ATTACHMENT0
;
4864 if (i
>= ctx
->Const
.MaxColorAttachments
)
4866 return &fb
->Attachment
[BUFFER_COLOR0
+ i
];
4869 case GL_DEPTH_ATTACHMENT
:
4870 case GL_DEPTH_STENCIL_ATTACHMENT
:
4871 return &fb
->Attachment
[BUFFER_DEPTH
];
4873 case GL_STENCIL_ATTACHMENT
:
4874 return &fb
->Attachment
[BUFFER_STENCIL
];
4881 discard_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
4882 GLsizei numAttachments
, const GLenum
*attachments
)
4884 if (!ctx
->Driver
.DiscardFramebuffer
)
4887 for (int i
= 0; i
< numAttachments
; i
++) {
4888 struct gl_renderbuffer_attachment
*att
=
4889 get_fb_attachment(ctx
, fb
, attachments
[i
]);
4894 /* If we're asked to invalidate just depth or just stencil, but the
4895 * attachment is packed depth/stencil, then we can only use
4896 * Driver.DiscardFramebuffer if the attachments list includes both depth
4897 * and stencil and they both point at the same renderbuffer.
4899 if ((attachments
[i
] == GL_DEPTH_ATTACHMENT
||
4900 attachments
[i
] == GL_STENCIL_ATTACHMENT
) &&
4901 (!att
->Renderbuffer
||
4902 att
->Renderbuffer
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
4903 GLenum other_format
= (attachments
[i
] == GL_DEPTH_ATTACHMENT
?
4904 GL_STENCIL_ATTACHMENT
: GL_DEPTH_ATTACHMENT
);
4905 bool has_both
= false;
4906 for (int j
= 0; j
< numAttachments
; j
++) {
4907 if (attachments
[j
] == other_format
)
4912 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
!=
4913 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
|| !has_both
)
4917 ctx
->Driver
.DiscardFramebuffer(ctx
, fb
, att
);
4922 _mesa_InvalidateSubFramebuffer_no_error(GLenum target
, GLsizei numAttachments
,
4923 const GLenum
*attachments
, GLint x
,
4924 GLint y
, GLsizei width
, GLsizei height
)
4931 _mesa_InvalidateSubFramebuffer(GLenum target
, GLsizei numAttachments
,
4932 const GLenum
*attachments
, GLint x
, GLint y
,
4933 GLsizei width
, GLsizei height
)
4935 struct gl_framebuffer
*fb
;
4936 GET_CURRENT_CONTEXT(ctx
);
4938 fb
= get_framebuffer_target(ctx
, target
);
4940 _mesa_error(ctx
, GL_INVALID_ENUM
,
4941 "glInvalidateSubFramebuffer(invalid target %s)",
4942 _mesa_enum_to_string(target
));
4946 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4947 x
, y
, width
, height
,
4948 "glInvalidateSubFramebuffer");
4953 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer
,
4954 GLsizei numAttachments
,
4955 const GLenum
*attachments
,
4957 GLsizei width
, GLsizei height
)
4959 struct gl_framebuffer
*fb
;
4960 GET_CURRENT_CONTEXT(ctx
);
4962 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4963 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4964 * default draw framebuffer is affected."
4967 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
4968 "glInvalidateNamedFramebufferSubData");
4973 fb
= ctx
->WinSysDrawBuffer
;
4975 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
4976 x
, y
, width
, height
,
4977 "glInvalidateNamedFramebufferSubData");
4981 _mesa_InvalidateFramebuffer_no_error(GLenum target
, GLsizei numAttachments
,
4982 const GLenum
*attachments
)
4984 struct gl_framebuffer
*fb
;
4985 GET_CURRENT_CONTEXT(ctx
);
4987 fb
= get_framebuffer_target(ctx
, target
);
4991 discard_framebuffer(ctx
, fb
, numAttachments
, attachments
);
4996 _mesa_InvalidateFramebuffer(GLenum target
, GLsizei numAttachments
,
4997 const GLenum
*attachments
)
4999 struct gl_framebuffer
*fb
;
5000 GET_CURRENT_CONTEXT(ctx
);
5002 fb
= get_framebuffer_target(ctx
, target
);
5004 _mesa_error(ctx
, GL_INVALID_ENUM
,
5005 "glInvalidateFramebuffer(invalid target %s)",
5006 _mesa_enum_to_string(target
));
5010 /* The GL_ARB_invalidate_subdata spec says:
5014 * void InvalidateFramebuffer(enum target,
5015 * sizei numAttachments,
5016 * const enum *attachments);
5018 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
5019 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
5020 * <MAX_VIEWPORT_DIMS[1]> respectively."
5022 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
5024 ctx
->Const
.MaxViewportWidth
,
5025 ctx
->Const
.MaxViewportHeight
,
5026 "glInvalidateFramebuffer");
5028 discard_framebuffer(ctx
, fb
, numAttachments
, attachments
);
5033 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer
,
5034 GLsizei numAttachments
,
5035 const GLenum
*attachments
)
5037 struct gl_framebuffer
*fb
;
5038 GET_CURRENT_CONTEXT(ctx
);
5040 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
5041 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
5042 * default draw framebuffer is affected."
5045 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
5046 "glInvalidateNamedFramebufferData");
5051 fb
= ctx
->WinSysDrawBuffer
;
5053 /* The GL_ARB_invalidate_subdata spec says:
5057 * void InvalidateFramebuffer(enum target,
5058 * sizei numAttachments,
5059 * const enum *attachments);
5061 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
5062 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
5063 * <MAX_VIEWPORT_DIMS[1]> respectively."
5065 invalidate_framebuffer_storage(ctx
, fb
, numAttachments
, attachments
,
5067 ctx
->Const
.MaxViewportWidth
,
5068 ctx
->Const
.MaxViewportHeight
,
5069 "glInvalidateNamedFramebufferData");
5074 _mesa_DiscardFramebufferEXT(GLenum target
, GLsizei numAttachments
,
5075 const GLenum
*attachments
)
5077 struct gl_framebuffer
*fb
;
5080 GET_CURRENT_CONTEXT(ctx
);
5082 fb
= get_framebuffer_target(ctx
, target
);
5084 _mesa_error(ctx
, GL_INVALID_ENUM
,
5085 "glDiscardFramebufferEXT(target %s)",
5086 _mesa_enum_to_string(target
));
5090 if (numAttachments
< 0) {
5091 _mesa_error(ctx
, GL_INVALID_VALUE
,
5092 "glDiscardFramebufferEXT(numAttachments < 0)");
5096 for (i
= 0; i
< numAttachments
; i
++) {
5097 switch (attachments
[i
]) {
5101 if (_mesa_is_user_fbo(fb
))
5104 case GL_COLOR_ATTACHMENT0
:
5105 case GL_DEPTH_ATTACHMENT
:
5106 case GL_STENCIL_ATTACHMENT
:
5107 if (_mesa_is_winsys_fbo(fb
))
5115 discard_framebuffer(ctx
, fb
, numAttachments
, attachments
);
5120 _mesa_error(ctx
, GL_INVALID_ENUM
,
5121 "glDiscardFramebufferEXT(attachment %s)",
5122 _mesa_enum_to_string(attachments
[i
]));
5126 sample_locations(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
5127 GLuint start
, GLsizei count
, const GLfloat
*v
, bool no_error
,
5133 if (!ctx
->Extensions
.ARB_sample_locations
) {
5134 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5136 "(ARB_sample_locations not available)", name
);
5140 if (start
+ count
> MAX_SAMPLE_LOCATION_TABLE_SIZE
) {
5141 _mesa_error(ctx
, GL_INVALID_VALUE
,
5142 "%s(start+size > sample location table size)", name
);
5147 if (!fb
->SampleLocationTable
) {
5148 size_t size
= MAX_SAMPLE_LOCATION_TABLE_SIZE
* 2 * sizeof(GLfloat
);
5149 fb
->SampleLocationTable
= malloc(size
);
5150 if (!fb
->SampleLocationTable
) {
5151 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
5152 "Cannot allocate sample location table");
5155 for (i
= 0; i
< MAX_SAMPLE_LOCATION_TABLE_SIZE
* 2; i
++)
5156 fb
->SampleLocationTable
[i
] = 0.5f
;
5159 for (i
= 0; i
< count
* 2; i
++) {
5160 /* The ARB_sample_locations spec says:
5162 * Sample locations outside of [0,1] result in undefined
5165 * To simplify driver implementations, we choose to clamp to
5166 * [0,1] and change NaN into 0.5.
5168 if (isnan(v
[i
]) || v
[i
] < 0.0f
|| v
[i
] > 1.0f
) {
5169 static GLuint msg_id
= 0;
5170 static const char* msg
= "Invalid sample location specified";
5171 _mesa_debug_get_id(&msg_id
);
5173 _mesa_log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_UNDEFINED
,
5174 msg_id
, MESA_DEBUG_SEVERITY_HIGH
, strlen(msg
), msg
);
5178 fb
->SampleLocationTable
[start
* 2 + i
] = 0.5f
;
5180 fb
->SampleLocationTable
[start
* 2 + i
] = CLAMP(v
[i
], 0.0f
, 1.0f
);
5183 if (fb
== ctx
->DrawBuffer
)
5184 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleLocations
;
5188 _mesa_FramebufferSampleLocationsfvARB(GLenum target
, GLuint start
,
5189 GLsizei count
, const GLfloat
*v
)
5191 struct gl_framebuffer
*fb
;
5193 GET_CURRENT_CONTEXT(ctx
);
5195 fb
= get_framebuffer_target(ctx
, target
);
5197 _mesa_error(ctx
, GL_INVALID_ENUM
,
5198 "glFramebufferSampleLocationsfvARB(target %s)",
5199 _mesa_enum_to_string(target
));
5203 sample_locations(ctx
, fb
, start
, count
, v
, false,
5204 "glFramebufferSampleLocationsfvARB");
5208 _mesa_NamedFramebufferSampleLocationsfvARB(GLuint framebuffer
, GLuint start
,
5209 GLsizei count
, const GLfloat
*v
)
5211 struct gl_framebuffer
*fb
;
5213 GET_CURRENT_CONTEXT(ctx
);
5216 fb
= _mesa_lookup_framebuffer_err(ctx
, framebuffer
,
5217 "glNamedFramebufferSampleLocationsfvARB");
5222 fb
= ctx
->WinSysDrawBuffer
;
5224 sample_locations(ctx
, fb
, start
, count
, v
, false,
5225 "glNamedFramebufferSampleLocationsfvARB");
5229 _mesa_FramebufferSampleLocationsfvARB_no_error(GLenum target
, GLuint start
,
5230 GLsizei count
, const GLfloat
*v
)
5232 GET_CURRENT_CONTEXT(ctx
);
5233 sample_locations(ctx
, get_framebuffer_target(ctx
, target
), start
,
5234 count
, v
, true, "glFramebufferSampleLocationsfvARB");
5238 _mesa_NamedFramebufferSampleLocationsfvARB_no_error(GLuint framebuffer
,
5239 GLuint start
, GLsizei count
,
5242 GET_CURRENT_CONTEXT(ctx
);
5243 sample_locations(ctx
, _mesa_lookup_framebuffer(ctx
, framebuffer
), start
,
5244 count
, v
, true, "glNamedFramebufferSampleLocationsfvARB");
5248 _mesa_EvaluateDepthValuesARB(void)
5250 GET_CURRENT_CONTEXT(ctx
);
5252 if (!ctx
->Extensions
.ARB_sample_locations
) {
5253 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5254 "EvaluateDepthValuesARB not supported (neither "
5255 "ARB_sample_locations nor NV_sample_locations is available)");
5259 if (ctx
->Driver
.EvaluateDepthValues
)
5260 ctx
->Driver
.EvaluateDepthValues(ctx
);