blob: 2c6c599731b91403cf26f7464d7b3706ad7c4033 [file] [log] [blame]
Brian Paulddc82ee2005-02-05 19:56:45 +00001/*
2 * Mesa 3-D graphics library
Brian Paulddc82ee2005-02-05 19:56:45 +00003 *
Brian Paul3dc65912008-07-03 15:40:38 -06004 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian Paul989edea2009-01-22 15:05:13 -07005 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
Brian Paulddc82ee2005-02-05 19:56:45 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Kenneth Graunke3d8d5b22013-04-21 13:46:48 -070020 * 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.
Brian Paulddc82ee2005-02-05 19:56:45 +000024 */
25
26
Brian Paul463642c2005-02-08 02:06:00 +000027/*
Brian Paul989edea2009-01-22 15:05:13 -070028 * GL_EXT/ARB_framebuffer_object extensions
29 *
Brian Paul463642c2005-02-08 02:06:00 +000030 * Authors:
31 * Brian Paul
32 */
33
Kenneth Graunkec6ed42a2012-11-17 23:23:06 -080034#include <stdbool.h>
Brian Paul463642c2005-02-08 02:06:00 +000035
Roland Scheideggera1bc0d02007-07-18 20:17:14 +020036#include "buffers.h"
Brian Paulddc82ee2005-02-05 19:56:45 +000037#include "context.h"
Brian Paul9b50cea2009-10-23 11:34:14 -060038#include "enums.h"
Brian Paulddc82ee2005-02-05 19:56:45 +000039#include "fbobject.h"
Brian Paul5cf5d4b2009-09-27 20:51:18 -060040#include "formats.h"
Brian Paule4b23562005-05-04 20:11:35 +000041#include "framebuffer.h"
Brian Paula1287f52012-07-22 11:20:00 -060042#include "glformats.h"
Brian Paulddc82ee2005-02-05 19:56:45 +000043#include "hash.h"
Brian Paul989edea2009-01-22 15:05:13 -070044#include "macros.h"
Chris Forbes90b5a242013-02-06 20:42:53 +130045#include "multisample.h"
Vinson Lee0117da42011-01-05 23:11:54 -080046#include "mtypes.h"
Brian Paule4b23562005-05-04 20:11:35 +000047#include "renderbuffer.h"
Brian Paul99745402006-03-01 02:02:43 +000048#include "state.h"
Brian Paul463642c2005-02-08 02:06:00 +000049#include "teximage.h"
Brian Paulea4fe662006-03-26 05:22:17 +000050#include "texobj.h"
Brian Paulddc82ee2005-02-05 19:56:45 +000051
52
Brian Pauld9468c92005-02-10 16:08:07 +000053/**
54 * Notes:
55 *
56 * None of the GL_EXT_framebuffer_object functions are compiled into
57 * display lists.
58 */
59
60
61
Brian Paul923b6fc2005-02-08 04:08:56 +000062/*
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
67 */
Brian Paul2c6f9112005-02-24 05:47:06 +000068static struct gl_framebuffer DummyFramebuffer;
69static struct gl_renderbuffer DummyRenderbuffer;
Brian Paul1864c7d2005-02-08 03:46:37 +000070
Kristian Høgsberg144356f2010-09-09 17:08:12 -040071/* We bind this framebuffer when applications pass a NULL
72 * drawable/surface in make current. */
73static struct gl_framebuffer IncompleteFramebuffer;
74
Brian Paul1864c7d2005-02-08 03:46:37 +000075
Brian Paul3dc65912008-07-03 15:40:38 -060076static void
Brian Paulc7324582012-11-30 10:04:48 -070077delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
Brian Paul3dc65912008-07-03 15:40:38 -060078{
79 /* no op */
80}
81
82static void
83delete_dummy_framebuffer(struct gl_framebuffer *fb)
84{
85 /* no op */
86}
87
88
89void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040090_mesa_init_fbobjects(struct gl_context *ctx)
Brian Paul3dc65912008-07-03 15:40:38 -060091{
Brian Pauld129ea72014-03-01 10:21:07 -070092 mtx_init(&DummyFramebuffer.Mutex, mtx_plain);
93 mtx_init(&DummyRenderbuffer.Mutex, mtx_plain);
94 mtx_init(&IncompleteFramebuffer.Mutex, mtx_plain);
Brian Paul3dc65912008-07-03 15:40:38 -060095 DummyFramebuffer.Delete = delete_dummy_framebuffer;
96 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
Kristian Høgsberg144356f2010-09-09 17:08:12 -040097 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
Brian Paul3dc65912008-07-03 15:40:38 -060098}
99
Kristian Høgsberg9456e222010-06-04 14:28:59 -0400100struct gl_framebuffer *
101_mesa_get_incomplete_framebuffer(void)
102{
Kristian Høgsberg144356f2010-09-09 17:08:12 -0400103 return &IncompleteFramebuffer;
Kristian Høgsberg9456e222010-06-04 14:28:59 -0400104}
Brian Paul3dc65912008-07-03 15:40:38 -0600105
Brian Paulddc82ee2005-02-05 19:56:45 +0000106/**
Brian Paul2c6f9112005-02-24 05:47:06 +0000107 * Helper routine for getting a gl_renderbuffer.
Brian Paulddc82ee2005-02-05 19:56:45 +0000108 */
Brian Paulea4fe662006-03-26 05:22:17 +0000109struct gl_renderbuffer *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400110_mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
Brian Paulddc82ee2005-02-05 19:56:45 +0000111{
Brian Paul2c6f9112005-02-24 05:47:06 +0000112 struct gl_renderbuffer *rb;
Brian Paulddc82ee2005-02-05 19:56:45 +0000113
Brian Paul1864c7d2005-02-08 03:46:37 +0000114 if (id == 0)
Brian Paulddc82ee2005-02-05 19:56:45 +0000115 return NULL;
116
Brian Paul2c6f9112005-02-24 05:47:06 +0000117 rb = (struct gl_renderbuffer *)
Brian Paulddc82ee2005-02-05 19:56:45 +0000118 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
119 return rb;
120}
121
122
123/**
Laura Ekstrand2bb138e2015-01-23 16:38:36 -0800124 * A convenience function for direct state access that throws
125 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
126 */
127struct gl_renderbuffer *
128_mesa_lookup_renderbuffer_err(struct gl_context *ctx, GLuint id,
129 const char *func)
130{
131 struct gl_renderbuffer *rb;
132
133 rb = _mesa_lookup_renderbuffer(ctx, id);
134 if (!rb || rb == &DummyRenderbuffer) {
135 _mesa_error(ctx, GL_INVALID_OPERATION,
136 "%s(non-existent renderbuffer %u)", func, id);
137 return NULL;
138 }
139
140 return rb;
141}
142
143
144/**
Brian Paul2c6f9112005-02-24 05:47:06 +0000145 * Helper routine for getting a gl_framebuffer.
Brian Paulddc82ee2005-02-05 19:56:45 +0000146 */
Brian Paulea4fe662006-03-26 05:22:17 +0000147struct gl_framebuffer *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400148_mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
Brian Paulddc82ee2005-02-05 19:56:45 +0000149{
Brian Paul2c6f9112005-02-24 05:47:06 +0000150 struct gl_framebuffer *fb;
Brian Paulddc82ee2005-02-05 19:56:45 +0000151
Brian Paul1864c7d2005-02-08 03:46:37 +0000152 if (id == 0)
Brian Paulddc82ee2005-02-05 19:56:45 +0000153 return NULL;
154
Brian Paul2c6f9112005-02-24 05:47:06 +0000155 fb = (struct gl_framebuffer *)
Brian Paulddc82ee2005-02-05 19:56:45 +0000156 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
Brian Paul463642c2005-02-08 02:06:00 +0000157 return fb;
Brian Paulddc82ee2005-02-05 19:56:45 +0000158}
159
160
161/**
Laura Ekstrand6d8eff42015-01-22 10:23:35 -0800162 * A convenience function for direct state access that throws
163 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
164 */
165struct gl_framebuffer *
166_mesa_lookup_framebuffer_err(struct gl_context *ctx, GLuint id,
167 const char *func)
168{
169 struct gl_framebuffer *fb;
170
171 fb = _mesa_lookup_framebuffer(ctx, id);
172 if (!fb || fb == &DummyFramebuffer) {
173 _mesa_error(ctx, GL_INVALID_OPERATION,
174 "%s(non-existent framebuffer %u)", func, id);
175 return NULL;
176 }
177
178 return fb;
179}
180
181
182/**
Brian Paul72966362009-01-21 16:28:38 -0700183 * Mark the given framebuffer as invalid. This will force the
184 * test for framebuffer completeness to be done before the framebuffer
185 * is used.
186 */
187static void
188invalidate_framebuffer(struct gl_framebuffer *fb)
189{
190 fb->_Status = 0; /* "indeterminate" */
191}
192
193
194/**
Brian Paulc6991432011-02-28 18:24:25 -0700195 * Return the gl_framebuffer object which corresponds to the given
196 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
197 * Check support for GL_EXT_framebuffer_blit to determine if certain
198 * targets are legal.
199 * \return gl_framebuffer pointer or NULL if target is illegal
200 */
201static struct gl_framebuffer *
202get_framebuffer_target(struct gl_context *ctx, GLenum target)
203{
Ian Romanicka6729732013-11-13 13:30:37 -0800204 bool have_fb_blit = _mesa_is_gles3(ctx) || _mesa_is_desktop_gl(ctx);
Brian Paulc6991432011-02-28 18:24:25 -0700205 switch (target) {
206 case GL_DRAW_FRAMEBUFFER:
Kenneth Graunkec6ed42a2012-11-17 23:23:06 -0800207 return have_fb_blit ? ctx->DrawBuffer : NULL;
Brian Paulc6991432011-02-28 18:24:25 -0700208 case GL_READ_FRAMEBUFFER:
Kenneth Graunkec6ed42a2012-11-17 23:23:06 -0800209 return have_fb_blit ? ctx->ReadBuffer : NULL;
Brian Paulc6991432011-02-28 18:24:25 -0700210 case GL_FRAMEBUFFER_EXT:
211 return ctx->DrawBuffer;
212 default:
213 return NULL;
214 }
215}
216
217
218/**
Brian Pauld9468c92005-02-10 16:08:07 +0000219 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
Brian Paul2c6f9112005-02-24 05:47:06 +0000220 * gl_renderbuffer_attachment object.
Brian Paul61ec2052010-06-22 08:37:44 -0600221 * This function is only used for user-created FB objects, not the
222 * default / window-system FB object.
Brian Paul30590072009-01-21 11:06:11 -0700223 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
224 * the depth buffer attachment point.
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200225 * Returns if the attachment is a GL_COLOR_ATTACHMENTm_EXT on
226 * is_color_attachment, because several callers would return different errors
227 * if they don't find the attachment.
Brian Pauld9468c92005-02-10 16:08:07 +0000228 */
Brian Paul94512812014-02-01 08:58:43 -0700229static struct gl_renderbuffer_attachment *
230get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200231 GLenum attachment, bool *is_color_attachment)
Brian Paul3deaa012005-02-07 05:08:24 +0000232{
233 GLuint i;
234
Brian Paul36ede892012-01-12 09:17:23 -0700235 assert(_mesa_is_user_fbo(fb));
Brian Paul61ec2052010-06-22 08:37:44 -0600236
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200237 if (is_color_attachment)
238 *is_color_attachment = false;
239
Brian Paul3deaa012005-02-07 05:08:24 +0000240 switch (attachment) {
241 case GL_COLOR_ATTACHMENT0_EXT:
242 case GL_COLOR_ATTACHMENT1_EXT:
243 case GL_COLOR_ATTACHMENT2_EXT:
244 case GL_COLOR_ATTACHMENT3_EXT:
245 case GL_COLOR_ATTACHMENT4_EXT:
246 case GL_COLOR_ATTACHMENT5_EXT:
247 case GL_COLOR_ATTACHMENT6_EXT:
248 case GL_COLOR_ATTACHMENT7_EXT:
249 case GL_COLOR_ATTACHMENT8_EXT:
250 case GL_COLOR_ATTACHMENT9_EXT:
251 case GL_COLOR_ATTACHMENT10_EXT:
252 case GL_COLOR_ATTACHMENT11_EXT:
253 case GL_COLOR_ATTACHMENT12_EXT:
254 case GL_COLOR_ATTACHMENT13_EXT:
255 case GL_COLOR_ATTACHMENT14_EXT:
256 case GL_COLOR_ATTACHMENT15_EXT:
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200257 if (is_color_attachment)
258 *is_color_attachment = true;
Ian Romanick2e3a4ab2011-10-02 15:03:07 -0700259 /* Only OpenGL ES 1.x forbids color attachments other than
260 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
261 * hardware is used.
262 */
Brian Paul3deaa012005-02-07 05:08:24 +0000263 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
Ian Romanick7e4cb322011-10-02 14:50:21 -0700264 if (i >= ctx->Const.MaxColorAttachments
Martin Peres7bd8b482015-02-12 17:54:43 +0200265 || (i > 0 && ctx->API == API_OPENGLES)) {
266 return NULL;
Brian Paul3deaa012005-02-07 05:08:24 +0000267 }
Brian Paule4b23562005-05-04 20:11:35 +0000268 return &fb->Attachment[BUFFER_COLOR0 + i];
Brian Paul30590072009-01-21 11:06:11 -0700269 case GL_DEPTH_STENCIL_ATTACHMENT:
Matt Turnerec8ee912012-11-15 22:13:48 -0800270 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
Martin Peres7bd8b482015-02-12 17:54:43 +0200271 return NULL;
Brian Paul30590072009-01-21 11:06:11 -0700272 /* fall-through */
Brian Paul3deaa012005-02-07 05:08:24 +0000273 case GL_DEPTH_ATTACHMENT_EXT:
Brian Paule4b23562005-05-04 20:11:35 +0000274 return &fb->Attachment[BUFFER_DEPTH];
Brian Paul3deaa012005-02-07 05:08:24 +0000275 case GL_STENCIL_ATTACHMENT_EXT:
Brian Paule4b23562005-05-04 20:11:35 +0000276 return &fb->Attachment[BUFFER_STENCIL];
Brian Paul61ec2052010-06-22 08:37:44 -0600277 default:
278 return NULL;
279 }
280}
281
282
283/**
284 * As above, but only used for getting attachments of the default /
285 * window-system framebuffer (not user-created framebuffer objects).
286 */
287static struct gl_renderbuffer_attachment *
Timothy Arcerie6187612017-05-05 15:02:20 +1000288get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
289 GLenum attachment)
Brian Paul61ec2052010-06-22 08:37:44 -0600290{
Brian Paul36ede892012-01-12 09:17:23 -0700291 assert(_mesa_is_winsys_fbo(fb));
Brian Paul61ec2052010-06-22 08:37:44 -0600292
Anuj Phogat2f2801f2012-12-11 20:08:13 -0800293 if (_mesa_is_gles3(ctx)) {
294 assert(attachment == GL_BACK ||
295 attachment == GL_DEPTH ||
296 attachment == GL_STENCIL);
297 switch (attachment) {
298 case GL_BACK:
299 /* Since there is no stereo rendering in ES 3.0, only return the
300 * LEFT bits.
301 */
302 if (ctx->DrawBuffer->Visual.doubleBufferMode)
303 return &fb->Attachment[BUFFER_BACK_LEFT];
304 return &fb->Attachment[BUFFER_FRONT_LEFT];
305 case GL_DEPTH:
Timothy Arceria754e4c2017-05-05 15:09:37 +1000306 return &fb->Attachment[BUFFER_DEPTH];
Anuj Phogat2f2801f2012-12-11 20:08:13 -0800307 case GL_STENCIL:
308 return &fb->Attachment[BUFFER_STENCIL];
309 }
310 }
311
Brian Paul61ec2052010-06-22 08:37:44 -0600312 switch (attachment) {
Kristian Høgsberg80dfec32010-06-15 13:07:01 -0400313 case GL_FRONT_LEFT:
Marek Olšákd58a3902016-09-08 21:02:29 +0200314 /* Front buffers can be allocated on the first use, but
315 * glGetFramebufferAttachmentParameteriv must work even if that
316 * allocation hasn't happened yet. In such case, use the back buffer,
317 * which should be the same.
318 */
319 if (fb->Attachment[BUFFER_FRONT_LEFT].Type == GL_NONE)
320 return &fb->Attachment[BUFFER_BACK_LEFT];
321 else
322 return &fb->Attachment[BUFFER_FRONT_LEFT];
Kristian Høgsberg80dfec32010-06-15 13:07:01 -0400323 case GL_FRONT_RIGHT:
Marek Olšákd58a3902016-09-08 21:02:29 +0200324 /* Same as above. */
325 if (fb->Attachment[BUFFER_FRONT_RIGHT].Type == GL_NONE)
326 return &fb->Attachment[BUFFER_BACK_RIGHT];
327 else
328 return &fb->Attachment[BUFFER_FRONT_RIGHT];
Kristian Høgsberg80dfec32010-06-15 13:07:01 -0400329 case GL_BACK_LEFT:
330 return &fb->Attachment[BUFFER_BACK_LEFT];
331 case GL_BACK_RIGHT:
332 return &fb->Attachment[BUFFER_BACK_RIGHT];
Brian Paul61ec2052010-06-22 08:37:44 -0600333 case GL_AUX0:
334 if (fb->Visual.numAuxBuffers == 1) {
335 return &fb->Attachment[BUFFER_AUX0];
336 }
337 return NULL;
Ian Romanicka8328cc2011-10-03 12:02:18 -0700338
339 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
340 *
341 * "If the default framebuffer is bound to target, then attachment must
342 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
343 * identifying a color buffer; DEPTH, identifying the depth buffer; or
344 * STENCIL, identifying the stencil buffer."
345 *
346 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
347 * language. However, revision #33 of the ARB_framebuffer_object spec
348 * says:
349 *
350 * "If the default framebuffer is bound to <target>, then <attachment>
351 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
352 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
353 * depth buffer, or the stencil buffer, and <pname> may be
354 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
355 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
356 *
357 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
358 * from glext.h, so shipping apps should not use those values.
359 *
360 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
361 * support queries of the window system FBO.
362 */
363 case GL_DEPTH:
Brian Paul61ec2052010-06-22 08:37:44 -0600364 return &fb->Attachment[BUFFER_DEPTH];
Ian Romanicka8328cc2011-10-03 12:02:18 -0700365 case GL_STENCIL:
Brian Paul61ec2052010-06-22 08:37:44 -0600366 return &fb->Attachment[BUFFER_STENCIL];
Brian Paul3deaa012005-02-07 05:08:24 +0000367 default:
368 return NULL;
369 }
370}
371
372
Brian Paul61ec2052010-06-22 08:37:44 -0600373
Brian Pauld9468c92005-02-10 16:08:07 +0000374/**
375 * Remove any texture or renderbuffer attached to the given attachment
376 * point. Update reference counts, etc.
377 */
Brian Paul94512812014-02-01 08:58:43 -0700378static void
379remove_attachment(struct gl_context *ctx,
380 struct gl_renderbuffer_attachment *att)
Brian Paul3deaa012005-02-07 05:08:24 +0000381{
Eric Anholtc810e672013-05-10 12:36:43 -0700382 struct gl_renderbuffer *rb = att->Renderbuffer;
383
384 /* tell driver that we're done rendering to this texture. */
385 if (rb && rb->NeedsFinishRenderTexture)
Eric Anholta5b04522013-05-10 12:17:52 -0700386 ctx->Driver.FinishRenderTexture(ctx, rb);
Eric Anholtc810e672013-05-10 12:36:43 -0700387
Brian Paul3deaa012005-02-07 05:08:24 +0000388 if (att->Type == GL_TEXTURE) {
Matt Turnerbfcdb842015-02-20 20:18:47 -0800389 assert(att->Texture);
Brian9e01b912007-08-13 11:29:46 +0100390 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
Matt Turnerbfcdb842015-02-20 20:18:47 -0800391 assert(!att->Texture);
Brian Paul3deaa012005-02-07 05:08:24 +0000392 }
Brian Paul0e31e022005-12-01 00:25:00 +0000393 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
Matt Turnerbfcdb842015-02-20 20:18:47 -0800394 assert(!att->Texture);
Brian9e01b912007-08-13 11:29:46 +0100395 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
Matt Turnerbfcdb842015-02-20 20:18:47 -0800396 assert(!att->Renderbuffer);
Brian Paul3deaa012005-02-07 05:08:24 +0000397 }
398 att->Type = GL_NONE;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000399 att->Complete = GL_TRUE;
Brian Paul3deaa012005-02-07 05:08:24 +0000400}
401
Eric Anholt749a9272013-04-22 10:38:41 -0700402/**
Ian Romanickfb497132013-07-27 12:04:20 -0700403 * Verify a couple error conditions that will lead to an incomplete FBO and
404 * may cause problems for the driver's RenderTexture path.
405 */
406static bool
407driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment *att)
408{
409 const struct gl_texture_image *const texImage =
410 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
411
Marek Olšák8a101922016-05-30 16:29:18 +0200412 if (!texImage ||
413 texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
Ian Romanickfb497132013-07-27 12:04:20 -0700414 return false;
415
Ian Romanick41485fe2013-07-27 12:16:56 -0700416 if ((texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY
417 && att->Zoffset >= texImage->Height)
418 || (texImage->TexObject->Target != GL_TEXTURE_1D_ARRAY
419 && att->Zoffset >= texImage->Depth))
420 return false;
421
Ian Romanickfb497132013-07-27 12:04:20 -0700422 return true;
423}
424
425/**
Eric Anholt749a9272013-04-22 10:38:41 -0700426 * Create a renderbuffer which will be set up by the driver to wrap the
427 * texture image slice.
428 *
429 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
430 * to share most of their framebuffer rendering code between winsys,
431 * renderbuffer, and texture attachments.
432 *
433 * The allocated renderbuffer uses a non-zero Name so that drivers can check
434 * it for determining vertical orientation, but we use ~0 to make it fairly
435 * unambiguous with actual user (non-texture) renderbuffers.
436 */
437void
438_mesa_update_texture_renderbuffer(struct gl_context *ctx,
439 struct gl_framebuffer *fb,
440 struct gl_renderbuffer_attachment *att)
441{
442 struct gl_texture_image *texImage;
443 struct gl_renderbuffer *rb;
444
Eric Anholte98c39c2013-05-10 11:51:01 -0700445 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
Eric Anholt749a9272013-04-22 10:38:41 -0700446
447 rb = att->Renderbuffer;
448 if (!rb) {
449 rb = ctx->Driver.NewRenderbuffer(ctx, ~0);
450 if (!rb) {
451 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
452 return;
453 }
Timothy Arceri8046a942017-04-07 07:55:17 +1000454 att->Renderbuffer = rb;
Eric Anholt749a9272013-04-22 10:38:41 -0700455
456 /* This can't get called on a texture renderbuffer, so set it to NULL
457 * for clarity compared to user renderbuffers.
458 */
459 rb->AllocStorage = NULL;
Eric Anholtc810e672013-05-10 12:36:43 -0700460
461 rb->NeedsFinishRenderTexture = ctx->Driver.FinishRenderTexture != NULL;
Eric Anholt749a9272013-04-22 10:38:41 -0700462 }
463
Ian Romanick2f9fe2d2013-07-28 13:08:27 -0700464 if (!texImage)
465 return;
466
Eric Anholt77a405d2013-04-22 11:04:21 -0700467 rb->_BaseFormat = texImage->_BaseFormat;
468 rb->Format = texImage->TexFormat;
469 rb->InternalFormat = texImage->InternalFormat;
470 rb->Width = texImage->Width2;
471 rb->Height = texImage->Height2;
Marek Olšáka3969aa2013-11-20 01:47:36 +0100472 rb->Depth = texImage->Depth2;
Eric Anholt77a405d2013-04-22 11:04:21 -0700473 rb->NumSamples = texImage->NumSamples;
Eric Anholte98c39c2013-05-10 11:51:01 -0700474 rb->TexImage = texImage;
Eric Anholt77a405d2013-04-22 11:04:21 -0700475
Ian Romanickfb497132013-07-27 12:04:20 -0700476 if (driver_RenderTexture_is_safe(att))
477 ctx->Driver.RenderTexture(ctx, fb, att);
Eric Anholt749a9272013-04-22 10:38:41 -0700478}
Brian Paul3deaa012005-02-07 05:08:24 +0000479
Brian Pauld9468c92005-02-10 16:08:07 +0000480/**
481 * Bind a texture object to an attachment point.
482 * The previous binding, if any, will be removed first.
483 */
Brian Paul94512812014-02-01 08:58:43 -0700484static void
485set_texture_attachment(struct gl_context *ctx,
486 struct gl_framebuffer *fb,
487 struct gl_renderbuffer_attachment *att,
488 struct gl_texture_object *texObj,
Laura Ekstrand085c67d2015-03-02 16:48:59 -0800489 GLenum texTarget, GLuint level, GLuint layer,
Brian Paul94512812014-02-01 08:58:43 -0700490 GLboolean layered)
Brian Paul3deaa012005-02-07 05:08:24 +0000491{
Eric Anholtc810e672013-05-10 12:36:43 -0700492 struct gl_renderbuffer *rb = att->Renderbuffer;
493
494 if (rb && rb->NeedsFinishRenderTexture)
Eric Anholta5b04522013-05-10 12:17:52 -0700495 ctx->Driver.FinishRenderTexture(ctx, rb);
Eric Anholtc810e672013-05-10 12:36:43 -0700496
Brian Paul0e31e022005-12-01 00:25:00 +0000497 if (att->Texture == texObj) {
498 /* re-attaching same texture */
Matt Turnerbfcdb842015-02-20 20:18:47 -0800499 assert(att->Type == GL_TEXTURE);
Brian Paul0e31e022005-12-01 00:25:00 +0000500 }
501 else {
502 /* new attachment */
Brian Paul94512812014-02-01 08:58:43 -0700503 remove_attachment(ctx, att);
Brian Paul0e31e022005-12-01 00:25:00 +0000504 att->Type = GL_TEXTURE;
Brian9e01b912007-08-13 11:29:46 +0100505 assert(!att->Texture);
506 _mesa_reference_texobj(&att->Texture, texObj);
Brian Paul0e31e022005-12-01 00:25:00 +0000507 }
Eric Anholt749a9272013-04-22 10:38:41 -0700508 invalidate_framebuffer(fb);
Brian Paul0e31e022005-12-01 00:25:00 +0000509
510 /* always update these fields */
Brian Paul3deaa012005-02-07 05:08:24 +0000511 att->TextureLevel = level;
Brian Paul26f1ad62009-10-23 18:15:55 -0600512 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
Laura Ekstrand085c67d2015-03-02 16:48:59 -0800513 att->Zoffset = layer;
Jordan Justena6280802013-04-18 10:08:50 -0700514 att->Layered = layered;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000515 att->Complete = GL_FALSE;
Brian Paul519b23b2006-03-20 18:51:57 +0000516
Eric Anholt749a9272013-04-22 10:38:41 -0700517 _mesa_update_texture_renderbuffer(ctx, fb, att);
Brian Paul3deaa012005-02-07 05:08:24 +0000518}
519
520
Brian Pauld9468c92005-02-10 16:08:07 +0000521/**
522 * Bind a renderbuffer to an attachment point.
523 * The previous binding, if any, will be removed first.
524 */
Brian Paul94512812014-02-01 08:58:43 -0700525static void
526set_renderbuffer_attachment(struct gl_context *ctx,
527 struct gl_renderbuffer_attachment *att,
528 struct gl_renderbuffer *rb)
Brian Paul3deaa012005-02-07 05:08:24 +0000529{
Brian Paulea4fe662006-03-26 05:22:17 +0000530 /* XXX check if re-doing same attachment, exit early */
Brian Paul94512812014-02-01 08:58:43 -0700531 remove_attachment(ctx, att);
Brian Paul3deaa012005-02-07 05:08:24 +0000532 att->Type = GL_RENDERBUFFER_EXT;
Brian Paul2c6f9112005-02-24 05:47:06 +0000533 att->Texture = NULL; /* just to be safe */
James Legg1581e122015-02-07 23:33:15 +0000534 att->Layered = GL_FALSE;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000535 att->Complete = GL_FALSE;
Briandccd9c42007-04-02 09:56:28 -0600536 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
Brian Paul3deaa012005-02-07 05:08:24 +0000537}
538
Brian Paulddc82ee2005-02-05 19:56:45 +0000539
Brian Paulf0bbbf62005-02-09 03:50:30 +0000540/**
Brian Paule4b23562005-05-04 20:11:35 +0000541 * Fallback for ctx->Driver.FramebufferRenderbuffer()
Brian Paul84716042005-11-16 04:05:54 +0000542 * Attach a renderbuffer object to a framebuffer object.
Brian Paule4b23562005-05-04 20:11:35 +0000543 */
544void
Laura Ekstrand3d100372015-02-27 17:23:59 -0800545_mesa_FramebufferRenderbuffer_sw(struct gl_context *ctx,
546 struct gl_framebuffer *fb,
547 GLenum attachment,
548 struct gl_renderbuffer *rb)
Brian Paule4b23562005-05-04 20:11:35 +0000549{
Brian Paul84716042005-11-16 04:05:54 +0000550 struct gl_renderbuffer_attachment *att;
551
Brian Pauld129ea72014-03-01 10:21:07 -0700552 mtx_lock(&fb->Mutex);
Brian Paulea4fe662006-03-26 05:22:17 +0000553
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200554 att = get_attachment(ctx, fb, attachment, NULL);
Matt Turnerbfcdb842015-02-20 20:18:47 -0800555 assert(att);
Brian Paule4b23562005-05-04 20:11:35 +0000556 if (rb) {
Brian Paul94512812014-02-01 08:58:43 -0700557 set_renderbuffer_attachment(ctx, att, rb);
Brian Paul30590072009-01-21 11:06:11 -0700558 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
559 /* do stencil attachment here (depth already done above) */
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200560 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT, NULL);
Brian Paul30590072009-01-21 11:06:11 -0700561 assert(att);
Brian Paul94512812014-02-01 08:58:43 -0700562 set_renderbuffer_attachment(ctx, att, rb);
Brian Paul30590072009-01-21 11:06:11 -0700563 }
Marek Olšákdf818d52011-03-06 05:26:12 +0100564 rb->AttachedAnytime = GL_TRUE;
Brian Paule4b23562005-05-04 20:11:35 +0000565 }
566 else {
Brian Paul94512812014-02-01 08:58:43 -0700567 remove_attachment(ctx, att);
James Legg846c7152014-05-23 12:25:37 +0100568 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
569 /* detach stencil (depth was detached above) */
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -0200570 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT, NULL);
James Legg846c7152014-05-23 12:25:37 +0100571 assert(att);
572 remove_attachment(ctx, att);
573 }
Brian Paule4b23562005-05-04 20:11:35 +0000574 }
Brian Paulea4fe662006-03-26 05:22:17 +0000575
Brian Paul72966362009-01-21 16:28:38 -0700576 invalidate_framebuffer(fb);
577
Brian Pauld129ea72014-03-01 10:21:07 -0700578 mtx_unlock(&fb->Mutex);
Brian Paule4b23562005-05-04 20:11:35 +0000579}
580
581
582/**
Brian Paul62c66b32011-01-24 19:38:52 -0700583 * Fallback for ctx->Driver.ValidateFramebuffer()
584 * Check if the renderbuffer's formats are supported by the software
585 * renderer.
586 * Drivers should probably override this.
587 */
588void
589_mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
590{
591 gl_buffer_index buf;
592 for (buf = 0; buf < BUFFER_COUNT; buf++) {
593 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
594 if (rb) {
595 switch (rb->_BaseFormat) {
596 case GL_ALPHA:
597 case GL_LUMINANCE_ALPHA:
598 case GL_LUMINANCE:
599 case GL_INTENSITY:
Brian Pauld3015652011-01-24 19:38:52 -0700600 case GL_RED:
601 case GL_RG:
Brian Paul62c66b32011-01-24 19:38:52 -0700602 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
603 return;
Marek Olšáka3ac28a2011-05-14 04:42:29 +0200604
605 default:
Marek Olšák9d7698c2011-04-26 02:18:24 +0200606 switch (rb->Format) {
Marek Olšáka3ac28a2011-05-14 04:42:29 +0200607 /* XXX This list is likely incomplete. */
Mark Muellereeed49f2014-01-26 15:12:56 -0800608 case MESA_FORMAT_R9G9B9E5_FLOAT:
Marek Olšák9d7698c2011-04-26 02:18:24 +0200609 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
610 return;
611 default:;
Marek Olšáka3ac28a2011-05-14 04:42:29 +0200612 /* render buffer format is supported by software rendering */
Marek Olšák9d7698c2011-04-26 02:18:24 +0200613 }
Brian Paul62c66b32011-01-24 19:38:52 -0700614 }
615 }
616 }
617}
618
619
620/**
Marek Olšákf8855a42013-03-14 14:22:56 +0100621 * Return true if the framebuffer has a combined depth/stencil
622 * renderbuffer attached.
623 */
624GLboolean
625_mesa_has_depthstencil_combined(const struct gl_framebuffer *fb)
626{
627 const struct gl_renderbuffer_attachment *depth =
628 &fb->Attachment[BUFFER_DEPTH];
629 const struct gl_renderbuffer_attachment *stencil =
630 &fb->Attachment[BUFFER_STENCIL];
631
632 if (depth->Type == stencil->Type) {
633 if (depth->Type == GL_RENDERBUFFER_EXT &&
634 depth->Renderbuffer == stencil->Renderbuffer)
635 return GL_TRUE;
636
637 if (depth->Type == GL_TEXTURE &&
638 depth->Texture == stencil->Texture)
639 return GL_TRUE;
640 }
641
642 return GL_FALSE;
643}
644
645
646/**
Brian Paul9f731c82009-02-17 16:47:54 -0700647 * For debug only.
648 */
649static void
650att_incomplete(const char *msg)
651{
Brian Paul93bcf782012-05-09 12:09:21 -0600652 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
653 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
654 }
Brian Paul9f731c82009-02-17 16:47:54 -0700655}
656
657
658/**
Brian Paulc26c2002009-09-15 17:20:32 -0600659 * For debug only.
660 */
661static void
Eric Anholt3c21a7d2013-06-06 11:13:02 -0700662fbo_incomplete(struct gl_context *ctx, const char *msg, int index)
Brian Paulc26c2002009-09-15 17:20:32 -0600663{
Eric Anholt3c21a7d2013-06-06 11:13:02 -0700664 static GLuint msg_id;
665
666 _mesa_gl_debug(ctx, &msg_id,
Matt Turner5b1e51b2014-11-13 17:35:58 -0800667 MESA_DEBUG_SOURCE_API,
Eric Anholt3c21a7d2013-06-06 11:13:02 -0700668 MESA_DEBUG_TYPE_OTHER,
669 MESA_DEBUG_SEVERITY_MEDIUM,
670 "FBO incomplete: %s [%d]\n", msg, index);
671
Brian Paul93bcf782012-05-09 12:09:21 -0600672 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
673 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
674 }
Brian Paulc26c2002009-09-15 17:20:32 -0600675}
676
677
Brian Paule67f6ee2010-10-22 11:38:23 -0600678/**
679 * Is the given base format a legal format for a color renderbuffer?
680 */
Eric Anholt059cca92011-01-02 17:58:07 -0800681GLboolean
682_mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
Brian Paule67f6ee2010-10-22 11:38:23 -0600683{
684 switch (baseFormat) {
685 case GL_RGB:
686 case GL_RGBA:
687 return GL_TRUE;
Marek Olšák6e618532010-10-02 21:53:03 +0200688 case GL_LUMINANCE:
689 case GL_LUMINANCE_ALPHA:
690 case GL_INTENSITY:
Brian Paule67f6ee2010-10-22 11:38:23 -0600691 case GL_ALPHA:
Jordan Justencf300ea2012-12-27 12:41:10 -0800692 return ctx->API == API_OPENGL_COMPAT &&
693 ctx->Extensions.ARB_framebuffer_object;
Brian Paule67f6ee2010-10-22 11:38:23 -0600694 case GL_RED:
695 case GL_RG:
696 return ctx->Extensions.ARB_texture_rg;
697 default:
698 return GL_FALSE;
699 }
700}
701
702
703/**
Jordan Justen6c7fa722012-12-27 13:34:44 -0800704 * Is the given base format a legal format for a color renderbuffer?
705 */
706static GLboolean
Brian Paulc1377ed2014-03-22 10:31:58 -0600707is_format_color_renderable(const struct gl_context *ctx, mesa_format format,
708 GLenum internalFormat)
Jordan Justen6c7fa722012-12-27 13:34:44 -0800709{
710 const GLenum baseFormat =
711 _mesa_get_format_base_format(format);
712 GLboolean valid;
713
714 valid = _mesa_is_legal_color_format(ctx, baseFormat);
715 if (!valid || _mesa_is_desktop_gl(ctx)) {
716 return valid;
717 }
718
719 /* Reject additional cases for GLES */
720 switch (internalFormat) {
721 case GL_RGBA8_SNORM:
722 case GL_RGB32F:
723 case GL_RGB32I:
724 case GL_RGB32UI:
725 case GL_RGB16F:
726 case GL_RGB16I:
727 case GL_RGB16UI:
728 case GL_RGB8_SNORM:
729 case GL_RGB8I:
730 case GL_RGB8UI:
731 case GL_SRGB8:
732 case GL_RGB9_E5:
733 case GL_RG8_SNORM:
734 case GL_R8_SNORM:
735 return GL_FALSE;
736 default:
737 break;
738 }
739
Brian Paulc1377ed2014-03-22 10:31:58 -0600740 if (format == MESA_FORMAT_B10G10R10A2_UNORM &&
741 internalFormat != GL_RGB10_A2) {
Jordan Justen6c7fa722012-12-27 13:34:44 -0800742 return GL_FALSE;
743 }
744
745 return GL_TRUE;
746}
747
748
749/**
Brian Paule67f6ee2010-10-22 11:38:23 -0600750 * Is the given base format a legal format for a depth/stencil renderbuffer?
751 */
752static GLboolean
753is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
754{
755 switch (baseFormat) {
756 case GL_DEPTH_COMPONENT:
757 case GL_DEPTH_STENCIL_EXT:
758 return GL_TRUE;
759 default:
760 return GL_FALSE;
761 }
762}
Brian Paulc26c2002009-09-15 17:20:32 -0600763
764
765/**
Brian Paulf0bbbf62005-02-09 03:50:30 +0000766 * Test if an attachment point is complete and update its Complete field.
767 * \param format if GL_COLOR, this is a color attachment point,
768 * if GL_DEPTH, this is a depth component attachment point,
769 * if GL_STENCIL, this is a stencil component attachment point.
770 */
771static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400772test_attachment_completeness(const struct gl_context *ctx, GLenum format,
Brian Paul2c6f9112005-02-24 05:47:06 +0000773 struct gl_renderbuffer_attachment *att)
Brian Paulf0bbbf62005-02-09 03:50:30 +0000774{
775 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
776
777 /* assume complete */
778 att->Complete = GL_TRUE;
779
Brian Paulf0bbbf62005-02-09 03:50:30 +0000780 /* Look for reasons why the attachment might be incomplete */
781 if (att->Type == GL_TEXTURE) {
Brian Paule4b23562005-05-04 20:11:35 +0000782 const struct gl_texture_object *texObj = att->Texture;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000783 struct gl_texture_image *texImage;
Brian Paul5cf5d4b2009-09-27 20:51:18 -0600784 GLenum baseFormat;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000785
Brian Paule4b23562005-05-04 20:11:35 +0000786 if (!texObj) {
Brian Paul9f731c82009-02-17 16:47:54 -0700787 att_incomplete("no texobj");
Brian Paule4b23562005-05-04 20:11:35 +0000788 att->Complete = GL_FALSE;
789 return;
790 }
Brian Paulf0bbbf62005-02-09 03:50:30 +0000791
792 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
793 if (!texImage) {
Brian Paul9f731c82009-02-17 16:47:54 -0700794 att_incomplete("no teximage");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000795 att->Complete = GL_FALSE;
796 return;
797 }
798 if (texImage->Width < 1 || texImage->Height < 1) {
Brian Paul9f731c82009-02-17 16:47:54 -0700799 att_incomplete("teximage width/height=0");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000800 att->Complete = GL_FALSE;
801 return;
802 }
Ian Romanick25281fe2013-07-27 12:27:45 -0700803
804 switch (texObj->Target) {
805 case GL_TEXTURE_3D:
806 if (att->Zoffset >= texImage->Depth) {
807 att_incomplete("bad z offset");
808 att->Complete = GL_FALSE;
809 return;
810 }
811 break;
812 case GL_TEXTURE_1D_ARRAY:
813 if (att->Zoffset >= texImage->Height) {
814 att_incomplete("bad 1D-array layer");
815 att->Complete = GL_FALSE;
816 return;
817 }
818 break;
819 case GL_TEXTURE_2D_ARRAY:
820 if (att->Zoffset >= texImage->Depth) {
821 att_incomplete("bad 2D-array layer");
822 att->Complete = GL_FALSE;
823 return;
824 }
825 break;
826 case GL_TEXTURE_CUBE_MAP_ARRAY:
827 if (att->Zoffset >= texImage->Depth) {
828 att_incomplete("bad cube-array layer");
829 att->Complete = GL_FALSE;
830 return;
831 }
832 break;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000833 }
834
Ilia Mirkin68c4af12016-02-17 20:31:38 -0500835 baseFormat = texImage->_BaseFormat;
Brian Paul5cf5d4b2009-09-27 20:51:18 -0600836
Brian Paulf0bbbf62005-02-09 03:50:30 +0000837 if (format == GL_COLOR) {
Eric Anholt059cca92011-01-02 17:58:07 -0800838 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
Brian Paul9f731c82009-02-17 16:47:54 -0700839 att_incomplete("bad format");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000840 att->Complete = GL_FALSE;
841 return;
842 }
Brian Paul1f7c9142009-09-30 20:28:45 -0600843 if (_mesa_is_format_compressed(texImage->TexFormat)) {
Eric Anholt957f3c82009-05-15 16:24:59 -0700844 att_incomplete("compressed internalformat");
845 att->Complete = GL_FALSE;
846 return;
847 }
Tapani Pällie3330352015-02-12 14:33:53 +0200848
849 /* OES_texture_float allows creation and use of floating point
850 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
851 * these textures to be used as a render target, this is done via
852 * GL_EXT_color_buffer(_half)_float with set of new sized types.
853 */
854 if (_mesa_is_gles(ctx) && (texImage->TexObject->_IsFloat ||
855 texImage->TexObject->_IsHalfFloat)) {
856 att_incomplete("bad internal format");
857 att->Complete = GL_FALSE;
858 return;
859 }
Brian Paulf0bbbf62005-02-09 03:50:30 +0000860 }
861 else if (format == GL_DEPTH) {
Brian Paul5cf5d4b2009-09-27 20:51:18 -0600862 if (baseFormat == GL_DEPTH_COMPONENT) {
Brian Paul1ad7b992005-09-28 02:29:50 +0000863 /* OK */
864 }
Ian Romanicka92b9e62013-11-13 14:10:34 -0800865 else if (ctx->Extensions.ARB_depth_texture &&
Ian Romanick49493222013-11-13 14:15:11 -0800866 baseFormat == GL_DEPTH_STENCIL) {
Brian Paul1ad7b992005-09-28 02:29:50 +0000867 /* OK */
868 }
869 else {
Brian Paulf0bbbf62005-02-09 03:50:30 +0000870 att->Complete = GL_FALSE;
Brian Paul9f731c82009-02-17 16:47:54 -0700871 att_incomplete("bad depth format");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000872 return;
873 }
874 }
875 else {
Matt Turnerbfcdb842015-02-20 20:18:47 -0800876 assert(format == GL_STENCIL);
Ian Romanicka92b9e62013-11-13 14:10:34 -0800877 if (ctx->Extensions.ARB_depth_texture &&
Ian Romanick49493222013-11-13 14:15:11 -0800878 baseFormat == GL_DEPTH_STENCIL) {
Mathias Fröhlich042d9a52009-05-19 09:59:01 -0600879 /* OK */
Dave Airlie782e71c2015-04-05 13:19:18 +1000880 } else if (ctx->Extensions.ARB_texture_stencil8 &&
881 baseFormat == GL_STENCIL_INDEX) {
882 /* OK */
883 } else {
Mathias Fröhlich042d9a52009-05-19 09:59:01 -0600884 /* no such thing as stencil-only textures */
885 att_incomplete("illegal stencil texture");
886 att->Complete = GL_FALSE;
887 return;
888 }
Brian Paulf0bbbf62005-02-09 03:50:30 +0000889 }
890 }
Brian Paule4b23562005-05-04 20:11:35 +0000891 else if (att->Type == GL_RENDERBUFFER_EXT) {
Ilia Mirkin68c4af12016-02-17 20:31:38 -0500892 const GLenum baseFormat = att->Renderbuffer->_BaseFormat;
Brian Paul45e76d22009-10-08 20:27:27 -0600893
Matt Turnerbfcdb842015-02-20 20:18:47 -0800894 assert(att->Renderbuffer);
Brian Paul49918882006-03-20 15:27:55 +0000895 if (!att->Renderbuffer->InternalFormat ||
896 att->Renderbuffer->Width < 1 ||
897 att->Renderbuffer->Height < 1) {
Brian Paul9f731c82009-02-17 16:47:54 -0700898 att_incomplete("0x0 renderbuffer");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000899 att->Complete = GL_FALSE;
900 return;
901 }
902 if (format == GL_COLOR) {
Eric Anholt059cca92011-01-02 17:58:07 -0800903 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
Brian Paul9f731c82009-02-17 16:47:54 -0700904 att_incomplete("bad renderbuffer color format");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000905 att->Complete = GL_FALSE;
906 return;
907 }
908 }
909 else if (format == GL_DEPTH) {
Brian Paul45e76d22009-10-08 20:27:27 -0600910 if (baseFormat == GL_DEPTH_COMPONENT) {
Brian Paul1ad7b992005-09-28 02:29:50 +0000911 /* OK */
912 }
Ian Romanick49493222013-11-13 14:15:11 -0800913 else if (baseFormat == GL_DEPTH_STENCIL) {
Brian Paul1ad7b992005-09-28 02:29:50 +0000914 /* OK */
915 }
916 else {
Brian Paul9f731c82009-02-17 16:47:54 -0700917 att_incomplete("bad renderbuffer depth format");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000918 att->Complete = GL_FALSE;
919 return;
920 }
921 }
922 else {
923 assert(format == GL_STENCIL);
Ian Romanick49493222013-11-13 14:15:11 -0800924 if (baseFormat == GL_STENCIL_INDEX ||
925 baseFormat == GL_DEPTH_STENCIL) {
Brian Paul1ad7b992005-09-28 02:29:50 +0000926 /* OK */
927 }
928 else {
Brian Paulf0bbbf62005-02-09 03:50:30 +0000929 att->Complete = GL_FALSE;
Brian Paul9f731c82009-02-17 16:47:54 -0700930 att_incomplete("bad renderbuffer stencil format");
Brian Paulf0bbbf62005-02-09 03:50:30 +0000931 return;
932 }
933 }
934 }
Brian Paule4b23562005-05-04 20:11:35 +0000935 else {
Matt Turnerbfcdb842015-02-20 20:18:47 -0800936 assert(att->Type == GL_NONE);
Brian Paule4b23562005-05-04 20:11:35 +0000937 /* complete */
938 return;
939 }
Brian Paulf0bbbf62005-02-09 03:50:30 +0000940}
941
942
943/**
944 * Test if the given framebuffer object is complete and update its
945 * Status field with the results.
Brian Paul3528f692009-01-22 15:13:18 -0700946 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
947 * driver to make hardware-specific validation/completeness checks.
Brian Paule4b23562005-05-04 20:11:35 +0000948 * Also update the framebuffer's Width and Height fields if the
949 * framebuffer is complete.
Brian Paulf0bbbf62005-02-09 03:50:30 +0000950 */
Brian Paule4b23562005-05-04 20:11:35 +0000951void
Brian Paulf9288542010-10-22 11:25:14 -0600952_mesa_test_framebuffer_completeness(struct gl_context *ctx,
953 struct gl_framebuffer *fb)
Brian Paulf0bbbf62005-02-09 03:50:30 +0000954{
Brian Paul989edea2009-01-22 15:05:13 -0700955 GLuint numImages;
956 GLenum intFormat = GL_NONE; /* color buffers' internal format */
957 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
Brian Paul722d9762009-01-20 16:58:49 -0700958 GLint numSamples = -1;
Chris Forbes61d42ff2012-12-16 20:58:00 +1300959 GLint fixedSampleLocations = -1;
Brian Paule4b23562005-05-04 20:11:35 +0000960 GLint i;
Brian Paul28b014e2006-04-05 03:05:17 +0000961 GLuint j;
Paul Berry532b1fe2014-01-07 06:29:47 -0800962 /* Covers max_layer_count, is_layered, and layer_tex_target */
963 bool layer_info_valid = false;
964 GLuint max_layer_count = 0, att_layer_count;
Brian Paul5306ee72014-01-22 10:02:28 -0800965 bool is_layered = false;
Paul Berry28af1dc2013-11-19 19:01:37 -0800966 GLenum layer_tex_target = 0;
Iago Toral Quirogad42e0902014-12-12 15:14:32 +0100967 bool has_depth_attachment = false;
968 bool has_stencil_attachment = false;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000969
Brian Paul36ede892012-01-12 09:17:23 -0700970 assert(_mesa_is_user_fbo(fb));
Brian Paulc7264412005-06-01 00:50:23 +0000971
Marek Olšáke06d6162012-08-04 13:37:03 +0200972 /* we're changing framebuffer fields here */
973 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
974
Brian Paulf0bbbf62005-02-09 03:50:30 +0000975 numImages = 0;
Brian Paule4b23562005-05-04 20:11:35 +0000976 fb->Width = 0;
977 fb->Height = 0;
Marek Olšák21d407c2013-03-28 01:50:21 +0100978 fb->_AllColorBuffersFixedPoint = GL_TRUE;
Marek Olšák755648c2013-03-28 01:56:01 +0100979 fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
Kevin Rogovinda819992015-06-17 13:29:50 +0300980 fb->_HasAttachments = true;
Brian Paulff00ab72016-10-10 11:29:14 -0600981 fb->_IntegerBuffers = 0;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000982
Brian Paul989edea2009-01-22 15:05:13 -0700983 /* Start at -2 to more easily loop over all attachment points.
984 * -2: depth buffer
985 * -1: stencil buffer
986 * >=0: color buffer
987 */
Brian Paule4b23562005-05-04 20:11:35 +0000988 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
Brian Paul2c6f9112005-02-24 05:47:06 +0000989 struct gl_renderbuffer_attachment *att;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000990 GLenum f;
Mark Mueller71fe9432014-01-04 14:11:43 -0800991 mesa_format attFormat;
Paul Berry95140742013-11-19 15:55:51 -0800992 GLenum att_tex_target = GL_NONE;
Brian Paulf0bbbf62005-02-09 03:50:30 +0000993
Brian Paul1bc59bf2009-01-22 15:07:34 -0700994 /*
995 * XXX for ARB_fbo, only check color buffers that are named by
996 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
997 */
998
Brian Paul989edea2009-01-22 15:05:13 -0700999 /* check for attachment completeness
1000 */
Brian Paulf0bbbf62005-02-09 03:50:30 +00001001 if (i == -2) {
Brian Paule4b23562005-05-04 20:11:35 +00001002 att = &fb->Attachment[BUFFER_DEPTH];
Brian Paulf0bbbf62005-02-09 03:50:30 +00001003 test_attachment_completeness(ctx, GL_DEPTH, att);
1004 if (!att->Complete) {
Brian Paule4b23562005-05-04 20:11:35 +00001005 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001006 fbo_incomplete(ctx, "depth attachment incomplete", -1);
Brian Paulf0bbbf62005-02-09 03:50:30 +00001007 return;
Iago Toral Quirogad42e0902014-12-12 15:14:32 +01001008 } else if (att->Type != GL_NONE) {
1009 has_depth_attachment = true;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001010 }
1011 }
1012 else if (i == -1) {
Brian Paule4b23562005-05-04 20:11:35 +00001013 att = &fb->Attachment[BUFFER_STENCIL];
Brian Paulf0bbbf62005-02-09 03:50:30 +00001014 test_attachment_completeness(ctx, GL_STENCIL, att);
1015 if (!att->Complete) {
Brian Paule4b23562005-05-04 20:11:35 +00001016 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001017 fbo_incomplete(ctx, "stencil attachment incomplete", -1);
Brian Paulf0bbbf62005-02-09 03:50:30 +00001018 return;
Iago Toral Quirogad42e0902014-12-12 15:14:32 +01001019 } else if (att->Type != GL_NONE) {
1020 has_stencil_attachment = true;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001021 }
1022 }
1023 else {
Brian Paule4b23562005-05-04 20:11:35 +00001024 att = &fb->Attachment[BUFFER_COLOR0 + i];
Brian Paulf0bbbf62005-02-09 03:50:30 +00001025 test_attachment_completeness(ctx, GL_COLOR, att);
1026 if (!att->Complete) {
Brian Paule4b23562005-05-04 20:11:35 +00001027 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001028 fbo_incomplete(ctx, "color attachment incomplete", i);
Brian Paulf0bbbf62005-02-09 03:50:30 +00001029 return;
1030 }
1031 }
1032
Brian Paul989edea2009-01-22 15:05:13 -07001033 /* get width, height, format of the renderbuffer/texture
1034 */
Brian Paulf0bbbf62005-02-09 03:50:30 +00001035 if (att->Type == GL_TEXTURE) {
Eric Anholte98c39c2013-05-10 11:51:01 -07001036 const struct gl_texture_image *texImg = att->Renderbuffer->TexImage;
Paul Berry95140742013-11-19 15:55:51 -08001037 att_tex_target = att->Texture->Target;
Brian Paul989edea2009-01-22 15:05:13 -07001038 minWidth = MIN2(minWidth, texImg->Width);
1039 maxWidth = MAX2(maxWidth, texImg->Width);
1040 minHeight = MIN2(minHeight, texImg->Height);
1041 maxHeight = MAX2(maxHeight, texImg->Height);
Brian Paula9fc8ba2005-10-05 01:48:07 +00001042 f = texImg->_BaseFormat;
Brian Paulca1b5512011-02-28 18:23:23 -07001043 attFormat = texImg->TexFormat;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001044 numImages++;
Chris Forbes61d42ff2012-12-16 20:58:00 +13001045
Brian Paulc1377ed2014-03-22 10:31:58 -06001046 if (!is_format_color_renderable(ctx, attFormat,
1047 texImg->InternalFormat) &&
Dave Airlie782e71c2015-04-05 13:19:18 +10001048 !is_legal_depth_format(ctx, f) &&
1049 f != GL_STENCIL_INDEX) {
Iago Toral Quirogab6819cd2014-12-15 09:29:55 +01001050 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001051 fbo_incomplete(ctx, "texture attachment incomplete", -1);
Brian Pauled7f3ae2005-06-07 15:03:40 +00001052 return;
1053 }
Chris Forbes61d42ff2012-12-16 20:58:00 +13001054
1055 if (numSamples < 0)
1056 numSamples = texImg->NumSamples;
1057 else if (numSamples != texImg->NumSamples) {
1058 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001059 fbo_incomplete(ctx, "inconsistent sample count", -1);
Chris Forbes61d42ff2012-12-16 20:58:00 +13001060 return;
1061 }
1062
1063 if (fixedSampleLocations < 0)
1064 fixedSampleLocations = texImg->FixedSampleLocations;
1065 else if (fixedSampleLocations != texImg->FixedSampleLocations) {
1066 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001067 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
Chris Forbes61d42ff2012-12-16 20:58:00 +13001068 return;
1069 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001070 }
1071 else if (att->Type == GL_RENDERBUFFER_EXT) {
Brian Paul989edea2009-01-22 15:05:13 -07001072 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
1073 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
1074 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
1075 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
Brian Paulf0bbbf62005-02-09 03:50:30 +00001076 f = att->Renderbuffer->InternalFormat;
Brian Paulca1b5512011-02-28 18:23:23 -07001077 attFormat = att->Renderbuffer->Format;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001078 numImages++;
Chris Forbes61d42ff2012-12-16 20:58:00 +13001079
1080 if (numSamples < 0)
1081 numSamples = att->Renderbuffer->NumSamples;
1082 else if (numSamples != att->Renderbuffer->NumSamples) {
1083 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001084 fbo_incomplete(ctx, "inconsistent sample count", -1);
Chris Forbes61d42ff2012-12-16 20:58:00 +13001085 return;
1086 }
1087
1088 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1089 if (fixedSampleLocations < 0)
1090 fixedSampleLocations = GL_TRUE;
1091 else if (fixedSampleLocations != GL_TRUE) {
1092 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001093 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
Chris Forbes61d42ff2012-12-16 20:58:00 +13001094 return;
1095 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001096 }
1097 else {
1098 assert(att->Type == GL_NONE);
1099 continue;
1100 }
1101
Brian Paulff00ab72016-10-10 11:29:14 -06001102 /* Update flags describing color buffer datatypes */
Marek Olšák21d407c2013-03-28 01:50:21 +01001103 if (i >= 0) {
1104 GLenum type = _mesa_get_format_datatype(attFormat);
1105
Brian Paulff00ab72016-10-10 11:29:14 -06001106 /* check if integer color */
1107 if (_mesa_is_format_integer_color(attFormat))
1108 fb->_IntegerBuffers |= (1 << i);
1109
Marek Olšák21d407c2013-03-28 01:50:21 +01001110 fb->_AllColorBuffersFixedPoint =
1111 fb->_AllColorBuffersFixedPoint &&
1112 (type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED);
Marek Olšák755648c2013-03-28 01:56:01 +01001113
1114 fb->_HasSNormOrFloatColorBuffer =
1115 fb->_HasSNormOrFloatColorBuffer ||
1116 type == GL_SIGNED_NORMALIZED || type == GL_FLOAT;
Marek Olšák21d407c2013-03-28 01:50:21 +01001117 }
1118
Chris Forbes61d42ff2012-12-16 20:58:00 +13001119 /* Error-check width, height, format */
Brian Paulf0bbbf62005-02-09 03:50:30 +00001120 if (numImages == 1) {
Chris Forbes61d42ff2012-12-16 20:58:00 +13001121 /* save format */
Brian Paul722d9762009-01-20 16:58:49 -07001122 if (i >= 0) {
Brian Paulf0bbbf62005-02-09 03:50:30 +00001123 intFormat = f;
Brian Paul722d9762009-01-20 16:58:49 -07001124 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001125 }
1126 else {
Brian Paul989edea2009-01-22 15:05:13 -07001127 if (!ctx->Extensions.ARB_framebuffer_object) {
1128 /* check that width, height, format are same */
1129 if (minWidth != maxWidth || minHeight != maxHeight) {
1130 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001131 fbo_incomplete(ctx, "width or height mismatch", -1);
Brian Paul989edea2009-01-22 15:05:13 -07001132 return;
1133 }
Brian Paul45bd5c42011-12-16 08:44:43 -07001134 /* check that all color buffers are the same format */
Brian Paul989edea2009-01-22 15:05:13 -07001135 if (intFormat != GL_NONE && f != intFormat) {
1136 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001137 fbo_incomplete(ctx, "format mismatch", -1);
Brian Paul989edea2009-01-22 15:05:13 -07001138 return;
1139 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001140 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001141 }
Marek Olšáka82227c2012-06-15 17:21:05 +02001142
1143 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1144 */
1145 if (att->Type == GL_RENDERBUFFER &&
1146 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
1147 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001148 fbo_incomplete(ctx, "unsupported renderbuffer format", i);
Marek Olšáka82227c2012-06-15 17:21:05 +02001149 return;
1150 }
Jordan Justen5da82882013-04-18 10:20:05 -07001151
1152 /* Check that layered rendering is consistent. */
Paul Berry95140742013-11-19 15:55:51 -08001153 if (att->Layered) {
1154 if (att_tex_target == GL_TEXTURE_CUBE_MAP)
1155 att_layer_count = 6;
Kenneth Graunke5c399ca2014-05-07 14:35:42 -07001156 else if (att_tex_target == GL_TEXTURE_1D_ARRAY)
1157 att_layer_count = att->Renderbuffer->Height;
Paul Berry95140742013-11-19 15:55:51 -08001158 else
1159 att_layer_count = att->Renderbuffer->Depth;
1160 } else {
1161 att_layer_count = 0;
1162 }
Paul Berry28af1dc2013-11-19 19:01:37 -08001163 if (!layer_info_valid) {
Paul Berry532b1fe2014-01-07 06:29:47 -08001164 is_layered = att->Layered;
1165 max_layer_count = att_layer_count;
Paul Berry28af1dc2013-11-19 19:01:37 -08001166 layer_tex_target = att_tex_target;
1167 layer_info_valid = true;
Paul Berry532b1fe2014-01-07 06:29:47 -08001168 } else if (max_layer_count > 0 && layer_tex_target != att_tex_target) {
Paul Berry28af1dc2013-11-19 19:01:37 -08001169 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1170 fbo_incomplete(ctx, "layered framebuffer has mismatched targets", i);
1171 return;
Paul Berry532b1fe2014-01-07 06:29:47 -08001172 } else if (is_layered != att->Layered) {
1173 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
Brian Paulc1377ed2014-03-22 10:31:58 -06001174 fbo_incomplete(ctx,
1175 "framebuffer attachment layer mode is inconsistent",
1176 i);
Jordan Justen5da82882013-04-18 10:20:05 -07001177 return;
Paul Berry532b1fe2014-01-07 06:29:47 -08001178 } else if (att_layer_count > max_layer_count) {
1179 max_layer_count = att_layer_count;
Jordan Justen5da82882013-04-18 10:20:05 -07001180 }
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001181
1182 /*
1183 * The extension GL_ARB_framebuffer_no_attachments places additional
1184 * requirement on each attachment. Those additional requirements are
1185 * tighter that those of previous versions of GL. In interest of better
1186 * compatibility, we will not enforce these restrictions. For the record
1187 * those additional restrictions are quoted below:
1188 *
1189 * "The width and height of image are greater than zero and less than or
1190 * equal to the values of the implementation-dependent limits
1191 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1192 *
1193 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1194 * array texture and the attachment is layered, the depth or layer count
1195 * of the texture is less than or equal to the implementation-dependent
1196 * limit MAX_FRAMEBUFFER_LAYERS."
1197 *
1198 * "If image has multiple samples, its sample count is less than or equal
1199 * to the value of the implementation-dependent limit
1200 * MAX_FRAMEBUFFER_SAMPLES."
1201 *
1202 * The same requirements are also in place for GL 4.5,
1203 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1204 */
Brian Paulf0bbbf62005-02-09 03:50:30 +00001205 }
1206
Paul Berry532b1fe2014-01-07 06:29:47 -08001207 fb->MaxNumLayers = max_layer_count;
Jordan Justen5da82882013-04-18 10:20:05 -07001208
Chris Forbesa419a1c2014-03-23 22:41:28 +13001209 if (numImages == 0) {
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001210 fb->_HasAttachments = false;
1211
1212 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1213 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1214 fbo_incomplete(ctx, "no attachments", -1);
1215 return;
1216 }
1217
1218 if (fb->DefaultGeometry.Width == 0 || fb->DefaultGeometry.Height == 0) {
1219 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1220 fbo_incomplete(ctx, "no attachments and default width or height is 0", -1);
1221 return;
1222 }
Chris Forbesa419a1c2014-03-23 22:41:28 +13001223 }
1224
Jordan Justen09714c02012-07-19 11:27:16 -07001225 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
Kristian Høgsberge88cef32010-05-24 16:56:12 -04001226 /* Check that all DrawBuffers are present */
1227 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
Martin Peres7bd8b482015-02-12 17:54:43 +02001228 if (fb->ColorDrawBuffer[j] != GL_NONE) {
1229 const struct gl_renderbuffer_attachment *att
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -02001230 = get_attachment(ctx, fb, fb->ColorDrawBuffer[j], NULL);
Martin Peres7bd8b482015-02-12 17:54:43 +02001231 assert(att);
1232 if (att->Type == GL_NONE) {
1233 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
1234 fbo_incomplete(ctx, "missing drawbuffer", j);
1235 return;
1236 }
1237 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001238 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001239
Kristian Høgsberge88cef32010-05-24 16:56:12 -04001240 /* Check that the ReadBuffer is present */
1241 if (fb->ColorReadBuffer != GL_NONE) {
Martin Peres7bd8b482015-02-12 17:54:43 +02001242 const struct gl_renderbuffer_attachment *att
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -02001243 = get_attachment(ctx, fb, fb->ColorReadBuffer, NULL);
Martin Peres7bd8b482015-02-12 17:54:43 +02001244 assert(att);
1245 if (att->Type == GL_NONE) {
1246 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001247 fbo_incomplete(ctx, "missing readbuffer", -1);
Martin Peres7bd8b482015-02-12 17:54:43 +02001248 return;
1249 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00001250 }
1251 }
1252
Iago Toral Quirogad42e0902014-12-12 15:14:32 +01001253 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1254 *
1255 * "Depth and stencil attachments, if present, are the same image."
1256 *
1257 * This restriction is not present in the OpenGL ES2 spec.
1258 */
1259 if (_mesa_is_gles3(ctx) &&
1260 has_stencil_attachment && has_depth_attachment &&
1261 !_mesa_has_depthstencil_combined(fb)) {
1262 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1263 fbo_incomplete(ctx, "Depth and stencil attachments must be the same image", -1);
1264 return;
1265 }
1266
Brian Paul3528f692009-01-22 15:13:18 -07001267 /* Provisionally set status = COMPLETE ... */
Brian Paule4b23562005-05-04 20:11:35 +00001268 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
Brian Paul3528f692009-01-22 15:13:18 -07001269
Brian Paul777a2ef2009-01-22 15:17:42 -07001270 /* ... but the driver may say the FB is incomplete.
1271 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1272 * if anything.
1273 */
Brian Paul3528f692009-01-22 15:13:18 -07001274 if (ctx->Driver.ValidateFramebuffer) {
1275 ctx->Driver.ValidateFramebuffer(ctx, fb);
Brian Paul1f32c412009-01-19 17:34:19 -07001276 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
Eric Anholt3c21a7d2013-06-06 11:13:02 -07001277 fbo_incomplete(ctx, "driver marked FBO as incomplete", -1);
Erik Faye-Lund395b53d2015-12-16 17:09:50 +01001278 return;
Brian Paul1f32c412009-01-19 17:34:19 -07001279 }
Brian Paul3528f692009-01-22 15:13:18 -07001280 }
1281
Erik Faye-Lund395b53d2015-12-16 17:09:50 +01001282 /*
1283 * Note that if ARB_framebuffer_object is supported and the attached
1284 * renderbuffers/textures are different sizes, the framebuffer
1285 * width/height will be set to the smallest width/height.
1286 */
1287 if (numImages != 0) {
1288 fb->Width = minWidth;
1289 fb->Height = minHeight;
Brian Paul3528f692009-01-22 15:13:18 -07001290 }
Erik Faye-Lund395b53d2015-12-16 17:09:50 +01001291
1292 /* finally, update the visual info for the framebuffer */
1293 _mesa_update_framebuffer_visual(ctx, fb);
Brian Paule4b23562005-05-04 20:11:35 +00001294}
Brian Paulf0bbbf62005-02-09 03:50:30 +00001295
1296
Brian Paul1864c7d2005-02-08 03:46:37 +00001297GLboolean GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08001298_mesa_IsRenderbuffer(GLuint renderbuffer)
Brian Paulddc82ee2005-02-05 19:56:45 +00001299{
Samuel Pitoiset78392432017-04-06 18:05:36 +02001300 struct gl_renderbuffer *rb;
1301
Brian Paulddc82ee2005-02-05 19:56:45 +00001302 GET_CURRENT_CONTEXT(ctx);
Samuel Pitoiset78392432017-04-06 18:05:36 +02001303
Brian Paulddc82ee2005-02-05 19:56:45 +00001304 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Samuel Pitoiset78392432017-04-06 18:05:36 +02001305
1306 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1307 return rb != NULL && rb != &DummyRenderbuffer;
Brian Paulddc82ee2005-02-05 19:56:45 +00001308}
1309
1310
Martin Peresa34669b2015-02-12 18:52:10 +02001311static struct gl_renderbuffer *
Matt Turner015f2202015-07-30 14:31:04 -07001312allocate_renderbuffer_locked(struct gl_context *ctx, GLuint renderbuffer,
1313 const char *func)
Martin Peresa34669b2015-02-12 18:52:10 +02001314{
1315 struct gl_renderbuffer *newRb;
1316
1317 /* create new renderbuffer object */
1318 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
1319 if (!newRb) {
1320 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1321 return NULL;
1322 }
1323 assert(newRb->AllocStorage);
Matt Turner015f2202015-07-30 14:31:04 -07001324 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, renderbuffer, newRb);
Martin Peresa34669b2015-02-12 18:52:10 +02001325
1326 return newRb;
1327}
1328
1329
Ian Romanick97965e82013-07-18 17:38:16 -07001330static void
1331bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
Brian Paulddc82ee2005-02-05 19:56:45 +00001332{
Brian42aaa542007-03-25 10:39:36 -06001333 struct gl_renderbuffer *newRb;
Brian Paulddc82ee2005-02-05 19:56:45 +00001334 GET_CURRENT_CONTEXT(ctx);
1335
Brian Paul3deaa012005-02-07 05:08:24 +00001336 if (target != GL_RENDERBUFFER_EXT) {
Brian Paul4de18fb2009-11-02 15:30:51 -07001337 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
Brian Paulddc82ee2005-02-05 19:56:45 +00001338 return;
1339 }
1340
Brian Paul800e5532009-11-02 15:39:39 -07001341 /* No need to flush here since the render buffer binding has no
1342 * effect on rendering state.
1343 */
Brian Paul474f28e2005-10-08 14:41:17 +00001344
Brian Paul3deaa012005-02-07 05:08:24 +00001345 if (renderbuffer) {
Brian Paulea4fe662006-03-26 05:22:17 +00001346 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
Brian Paul1864c7d2005-02-08 03:46:37 +00001347 if (newRb == &DummyRenderbuffer) {
1348 /* ID was reserved, but no real renderbuffer object made yet */
1349 newRb = NULL;
1350 }
Ian Romanick97965e82013-07-18 17:38:16 -07001351 else if (!newRb && !allow_user_names) {
Brian Paul1bc59bf2009-01-22 15:07:34 -07001352 /* All RB IDs must be Gen'd */
1353 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1354 return;
1355 }
1356
Brian Paul3deaa012005-02-07 05:08:24 +00001357 if (!newRb) {
Matt Turner015f2202015-07-30 14:31:04 -07001358 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1359 newRb = allocate_renderbuffer_locked(ctx, renderbuffer,
1360 "glBindRenderbufferEXT");
1361 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
Brian Paul3deaa012005-02-07 05:08:24 +00001362 }
Brian Paulddc82ee2005-02-05 19:56:45 +00001363 }
Brian Paul463642c2005-02-08 02:06:00 +00001364 else {
1365 newRb = NULL;
1366 }
Brian Paulddc82ee2005-02-05 19:56:45 +00001367
Matt Turnerbfcdb842015-02-20 20:18:47 -08001368 assert(newRb != &DummyRenderbuffer);
Brian Paul1864c7d2005-02-08 03:46:37 +00001369
Brian42aaa542007-03-25 10:39:36 -06001370 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
Brian Paulddc82ee2005-02-05 19:56:45 +00001371}
1372
Ian Romanick97965e82013-07-18 17:38:16 -07001373void GLAPIENTRY
1374_mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1375{
1376 GET_CURRENT_CONTEXT(ctx);
1377
1378 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1379 * entry point, but they allow the use of user-generated names.
1380 */
1381 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1382}
Brian Paulddc82ee2005-02-05 19:56:45 +00001383
Tomasz Lis9f07ca12013-07-16 20:57:26 +02001384void GLAPIENTRY
1385_mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1386{
Ian Romanick97965e82013-07-18 17:38:16 -07001387 /* This function should not be in the dispatch table for core profile /
1388 * OpenGL 3.1, so execution should never get here in those cases -- no
1389 * need for an explicit test.
1390 */
1391 bind_renderbuffer(target, renderbuffer, true);
Tomasz Lis9f07ca12013-07-16 20:57:26 +02001392}
1393
Edward O'Callaghanb40375a2016-02-17 19:15:49 +11001394/**
1395 * ARB_framebuffer_no_attachment - Application passes requested param's
1396 * here. NOTE: NumSamples requested need not be _NumSamples which is
1397 * what the hw supports.
1398 */
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001399static void
1400framebuffer_parameteri(struct gl_context *ctx, struct gl_framebuffer *fb,
1401 GLenum pname, GLint param, const char *func)
1402{
1403 switch (pname) {
1404 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1405 if (param < 0 || param > ctx->Const.MaxFramebufferWidth)
1406 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1407 else
1408 fb->DefaultGeometry.Width = param;
1409 break;
1410 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1411 if (param < 0 || param > ctx->Const.MaxFramebufferHeight)
1412 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1413 else
1414 fb->DefaultGeometry.Height = param;
1415 break;
1416 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
Marta Lofstedtf8a93882015-08-24 13:01:53 +02001417 /*
1418 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1419 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1420 */
Ilia Mirkin82d756f2016-05-21 20:26:47 -04001421 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
Marta Lofstedtf8a93882015-08-24 13:01:53 +02001422 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1423 break;
1424 }
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001425 if (param < 0 || param > ctx->Const.MaxFramebufferLayers)
Marta Lofstedtf8a93882015-08-24 13:01:53 +02001426 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001427 else
1428 fb->DefaultGeometry.Layers = param;
1429 break;
1430 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1431 if (param < 0 || param > ctx->Const.MaxFramebufferSamples)
1432 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1433 else
1434 fb->DefaultGeometry.NumSamples = param;
1435 break;
1436 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1437 fb->DefaultGeometry.FixedSampleLocations = param;
1438 break;
1439 default:
1440 _mesa_error(ctx, GL_INVALID_ENUM,
1441 "%s(pname=0x%x)", func, pname);
1442 }
Ilia Mirkin095da3b2016-01-23 09:27:22 -05001443
1444 invalidate_framebuffer(fb);
1445 ctx->NewState |= _NEW_BUFFERS;
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001446}
1447
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001448void GLAPIENTRY
1449_mesa_FramebufferParameteri(GLenum target, GLenum pname, GLint param)
1450{
1451 GET_CURRENT_CONTEXT(ctx);
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001452 struct gl_framebuffer *fb;
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001453
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001454 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1455 _mesa_error(ctx, GL_INVALID_OPERATION,
1456 "glFramebufferParameteriv not supported "
1457 "(ARB_framebuffer_no_attachments not implemented)");
1458 return;
1459 }
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001460
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001461 fb = get_framebuffer_target(ctx, target);
1462 if (!fb) {
1463 _mesa_error(ctx, GL_INVALID_ENUM,
1464 "glFramebufferParameteri(target=0x%x)", target);
1465 return;
1466 }
1467
1468 /* check framebuffer binding */
1469 if (_mesa_is_winsys_fbo(fb)) {
1470 _mesa_error(ctx, GL_INVALID_OPERATION,
1471 "glFramebufferParameteri");
1472 return;
1473 }
1474
1475 framebuffer_parameteri(ctx, fb, pname, param, "glFramebufferParameteri");
1476}
1477
Alejandro Piñeirodfb1b542017-01-13 15:53:13 -02001478static bool
1479_pname_valid_for_default_framebuffer(struct gl_context *ctx,
1480 GLenum pname)
1481{
1482 if (!_mesa_is_desktop_gl(ctx))
1483 return false;
1484
1485 switch (pname) {
1486 case GL_DOUBLEBUFFER:
1487 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1488 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1489 case GL_SAMPLES:
1490 case GL_SAMPLE_BUFFERS:
1491 case GL_STEREO:
1492 return true;
1493 default:
1494 return false;
1495 }
1496}
1497
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001498static void
1499get_framebuffer_parameteriv(struct gl_context *ctx, struct gl_framebuffer *fb,
1500 GLenum pname, GLint *params, const char *func)
1501{
Alejandro Piñeirodfb1b542017-01-13 15:53:13 -02001502 /* From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries:
1503 *
1504 * "An INVALID_OPERATION error is generated by GetFramebufferParameteriv
1505 * if the default framebuffer is bound to target and pname is not one
1506 * of the accepted values from table 23.73, other than
1507 * SAMPLE_POSITION."
1508 *
1509 * For OpenGL ES, using default framebuffer still raises INVALID_OPERATION
1510 * for any pname.
1511 */
1512 if (_mesa_is_winsys_fbo(fb) &&
1513 !_pname_valid_for_default_framebuffer(ctx, pname)) {
1514 _mesa_error(ctx, GL_INVALID_OPERATION,
1515 "%s(invalid pname=0x%x for default framebuffer)", func, pname);
1516 return;
1517 }
1518
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001519 switch (pname) {
1520 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1521 *params = fb->DefaultGeometry.Width;
1522 break;
1523 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1524 *params = fb->DefaultGeometry.Height;
1525 break;
1526 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
Marta Lofstedtf8a93882015-08-24 13:01:53 +02001527 /*
1528 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1529 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1530 */
Ilia Mirkin82d756f2016-05-21 20:26:47 -04001531 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
Marta Lofstedtf8a93882015-08-24 13:01:53 +02001532 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1533 break;
1534 }
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001535 *params = fb->DefaultGeometry.Layers;
1536 break;
1537 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1538 *params = fb->DefaultGeometry.NumSamples;
1539 break;
1540 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1541 *params = fb->DefaultGeometry.FixedSampleLocations;
1542 break;
Alejandro Piñeiro0fb0c572017-01-13 16:23:05 -02001543 case GL_DOUBLEBUFFER:
1544 *params = fb->Visual.doubleBufferMode;
1545 break;
1546 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1547 *params = _mesa_get_color_read_format(ctx, fb, func);
1548 break;
1549 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1550 *params = _mesa_get_color_read_type(ctx, fb, func);
1551 break;
1552 case GL_SAMPLES:
1553 *params = _mesa_geometric_samples(fb);
1554 break;
1555 case GL_SAMPLE_BUFFERS:
1556 *params = _mesa_geometric_samples(fb) > 0;
1557 break;
1558 case GL_STEREO:
1559 *params = fb->Visual.stereoMode;
1560 break;
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001561 default:
1562 _mesa_error(ctx, GL_INVALID_ENUM,
1563 "%s(pname=0x%x)", func, pname);
1564 }
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001565}
1566
1567void GLAPIENTRY
1568_mesa_GetFramebufferParameteriv(GLenum target, GLenum pname, GLint *params)
1569{
1570 GET_CURRENT_CONTEXT(ctx);
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001571 struct gl_framebuffer *fb;
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001572
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001573 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1574 _mesa_error(ctx, GL_INVALID_OPERATION,
1575 "glGetFramebufferParameteriv not supported "
1576 "(ARB_framebuffer_no_attachments not implemented)");
1577 return;
1578 }
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001579
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001580 fb = get_framebuffer_target(ctx, target);
1581 if (!fb) {
1582 _mesa_error(ctx, GL_INVALID_ENUM,
1583 "glGetFramebufferParameteriv(target=0x%x)", target);
1584 return;
1585 }
1586
Kevin Rogovin6aa12992015-06-17 13:29:52 +03001587 get_framebuffer_parameteriv(ctx, fb, pname, params,
1588 "glGetFramebufferParameteriv");
Kevin Rogovinc9d26f22015-06-17 13:29:51 +03001589}
1590
Tomasz Lis9f07ca12013-07-16 20:57:26 +02001591
Brian Pauld0f13fa2009-01-21 11:17:45 -07001592/**
Ian Romanickef83bd22013-08-08 15:41:36 -07001593 * Remove the specified renderbuffer or texture from any attachment point in
1594 * the framebuffer.
Ian Romanick438cc6b2013-08-08 15:26:36 -07001595 *
1596 * \returns
1597 * \c true if the renderbuffer was detached from an attachment point. \c
1598 * false otherwise.
Brian Pauld0f13fa2009-01-21 11:17:45 -07001599 */
Ian Romanick438cc6b2013-08-08 15:26:36 -07001600bool
1601_mesa_detach_renderbuffer(struct gl_context *ctx,
1602 struct gl_framebuffer *fb,
1603 const void *att)
Brian Pauld0f13fa2009-01-21 11:17:45 -07001604{
Ian Romanick438cc6b2013-08-08 15:26:36 -07001605 unsigned i;
1606 bool progress = false;
1607
Brian Pauld0f13fa2009-01-21 11:17:45 -07001608 for (i = 0; i < BUFFER_COUNT; i++) {
Ian Romanickef83bd22013-08-08 15:41:36 -07001609 if (fb->Attachment[i].Texture == att
1610 || fb->Attachment[i].Renderbuffer == att) {
Brian Paul94512812014-02-01 08:58:43 -07001611 remove_attachment(ctx, &fb->Attachment[i]);
Ian Romanick438cc6b2013-08-08 15:26:36 -07001612 progress = true;
Brian Pauld0f13fa2009-01-21 11:17:45 -07001613 }
1614 }
Ian Romanick438cc6b2013-08-08 15:26:36 -07001615
1616 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1617 * Completeness," of the OpenGL 3.1 spec says:
1618 *
1619 * "Performing any of the following actions may change whether the
1620 * framebuffer is considered complete or incomplete:
1621 *
1622 * ...
1623 *
1624 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1625 * containing an image that is attached to a framebuffer object
1626 * that is bound to the framebuffer."
1627 */
1628 if (progress)
1629 invalidate_framebuffer(fb);
1630
1631 return progress;
Brian Pauld0f13fa2009-01-21 11:17:45 -07001632}
1633
1634
Brian Paul1864c7d2005-02-08 03:46:37 +00001635void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08001636_mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
Brian Paulddc82ee2005-02-05 19:56:45 +00001637{
1638 GLint i;
1639 GET_CURRENT_CONTEXT(ctx);
1640
Eduardo Lima Mitev2012f622014-12-11 23:34:18 +01001641 if (n < 0) {
1642 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1643 return;
1644 }
1645
Brian Paul474f28e2005-10-08 14:41:17 +00001646 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
Brian Paulddc82ee2005-02-05 19:56:45 +00001647
1648 for (i = 0; i < n; i++) {
Brian Paul2c6f9112005-02-24 05:47:06 +00001649 if (renderbuffers[i] > 0) {
Martin Peres7bd8b482015-02-12 17:54:43 +02001650 struct gl_renderbuffer *rb;
1651 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1652 if (rb) {
Brian Paul91802fd2005-10-04 16:01:02 +00001653 /* check if deleting currently bound renderbuffer object */
1654 if (rb == ctx->CurrentRenderbuffer) {
1655 /* bind default */
Matt Turnerbfcdb842015-02-20 20:18:47 -08001656 assert(rb->RefCount >= 2);
Paul Berry1a1db172012-11-06 08:57:59 -08001657 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
Brian Paul91802fd2005-10-04 16:01:02 +00001658 }
1659
Ian Romanickef83bd22013-08-08 15:41:36 -07001660 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
Brian Paulc1377ed2014-03-22 10:31:58 -06001661 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1662 * of the OpenGL 3.1 spec says:
Ian Romanickef83bd22013-08-08 15:41:36 -07001663 *
1664 * "If a renderbuffer object is deleted while its image is
1665 * attached to one or more attachment points in the currently
1666 * bound framebuffer, then it is as if FramebufferRenderbuffer
1667 * had been called, with a renderbuffer of 0, for each
1668 * attachment point to which this image was attached in the
1669 * currently bound framebuffer. In other words, this
1670 * renderbuffer image is first detached from all attachment
1671 * points in the currently bound framebuffer. Note that the
1672 * renderbuffer image is specifically not detached from any
1673 * non-bound framebuffers. Detaching the image from any
1674 * non-bound framebuffers is the responsibility of the
1675 * application.
1676 */
Brian Paul36ede892012-01-12 09:17:23 -07001677 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
Ian Romanick438cc6b2013-08-08 15:26:36 -07001678 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
Brian Pauld0f13fa2009-01-21 11:17:45 -07001679 }
Brian Paul36ede892012-01-12 09:17:23 -07001680 if (_mesa_is_user_fbo(ctx->ReadBuffer)
Brian Paulfc8c4a32011-06-16 07:31:58 -06001681 && ctx->ReadBuffer != ctx->DrawBuffer) {
Ian Romanick438cc6b2013-08-08 15:26:36 -07001682 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
Brian Pauld0f13fa2009-01-21 11:17:45 -07001683 }
1684
Martin Peres7bd8b482015-02-12 17:54:43 +02001685 /* Remove from hash table immediately, to free the ID.
Brian42aaa542007-03-25 10:39:36 -06001686 * But the object will not be freed until it's no longer
1687 * referenced anywhere else.
1688 */
Martin Peres7bd8b482015-02-12 17:54:43 +02001689 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
Brian Paulddc82ee2005-02-05 19:56:45 +00001690
Brian Paul1864c7d2005-02-08 03:46:37 +00001691 if (rb != &DummyRenderbuffer) {
Brian42aaa542007-03-25 10:39:36 -06001692 /* no longer referenced by hash table */
1693 _mesa_reference_renderbuffer(&rb, NULL);
Martin Peres7bd8b482015-02-12 17:54:43 +02001694 }
1695 }
Brian Paulddc82ee2005-02-05 19:56:45 +00001696 }
1697 }
1698}
1699
Martin Peresa34669b2015-02-12 18:52:10 +02001700static void
1701create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1702 bool dsa)
Brian Paulddc82ee2005-02-05 19:56:45 +00001703{
Martin Peresa34669b2015-02-12 18:52:10 +02001704 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
Brian Paulddc82ee2005-02-05 19:56:45 +00001705 GLuint first;
1706 GLint i;
1707
Brian Paulddc82ee2005-02-05 19:56:45 +00001708 if (n < 0) {
Martin Peresa34669b2015-02-12 18:52:10 +02001709 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
Brian Paulddc82ee2005-02-05 19:56:45 +00001710 return;
1711 }
1712
1713 if (!renderbuffers)
1714 return;
1715
Matt Turner015f2202015-07-30 14:31:04 -07001716 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1717
Brian Paulddc82ee2005-02-05 19:56:45 +00001718 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1719
1720 for (i = 0; i < n; i++) {
Brian Paulddc82ee2005-02-05 19:56:45 +00001721 GLuint name = first + i;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001722 renderbuffers[i] = name;
Martin Peresa34669b2015-02-12 18:52:10 +02001723
1724 if (dsa) {
Matt Turner015f2202015-07-30 14:31:04 -07001725 allocate_renderbuffer_locked(ctx, name, func);
Martin Peresa34669b2015-02-12 18:52:10 +02001726 } else {
Martin Peresfa383212015-03-25 16:28:03 +02001727 /* insert a dummy renderbuffer into the hash table */
Matt Turner015f2202015-07-30 14:31:04 -07001728 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, name,
1729 &DummyRenderbuffer);
Martin Peresa34669b2015-02-12 18:52:10 +02001730 }
Brian Paulddc82ee2005-02-05 19:56:45 +00001731 }
Matt Turner015f2202015-07-30 14:31:04 -07001732
1733 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
Brian Paulddc82ee2005-02-05 19:56:45 +00001734}
1735
1736
Martin Peresa34669b2015-02-12 18:52:10 +02001737void GLAPIENTRY
1738_mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1739{
1740 GET_CURRENT_CONTEXT(ctx);
1741 create_render_buffers(ctx, n, renderbuffers, false);
1742}
1743
1744
1745void GLAPIENTRY
1746_mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1747{
1748 GET_CURRENT_CONTEXT(ctx);
1749 create_render_buffers(ctx, n, renderbuffers, true);
1750}
1751
1752
Brian Pauld9468c92005-02-10 16:08:07 +00001753/**
Brian Paulf41bbc72011-01-24 19:38:52 -07001754 * Given an internal format token for a render buffer, return the
Brian Paul976ea9d2011-01-24 19:38:52 -07001755 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1756 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1757 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
Brian Paulf41bbc72011-01-24 19:38:52 -07001758 *
Brian Paul976ea9d2011-01-24 19:38:52 -07001759 * This is similar to _mesa_base_tex_format() but the set of valid
1760 * internal formats is different.
Brian Paulf41bbc72011-01-24 19:38:52 -07001761 *
Brian Paul976ea9d2011-01-24 19:38:52 -07001762 * Note that even if a format is determined to be legal here, validation
Brian Paulb3cfcdf2011-01-28 20:25:26 -07001763 * of the FBO may fail if the format is not supported by the driver/GPU.
Brian Paul976ea9d2011-01-24 19:38:52 -07001764 *
1765 * \param internalFormat as passed to glRenderbufferStorage()
1766 * \return the base internal format, or 0 if internalFormat is illegal
Brian Pauld9468c92005-02-10 16:08:07 +00001767 */
Brian Paul59e0faa2006-03-15 17:48:00 +00001768GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001769_mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
Brian Paul463642c2005-02-08 02:06:00 +00001770{
Brian Paul976ea9d2011-01-24 19:38:52 -07001771 /*
1772 * Notes: some formats such as alpha, luminance, etc. were added
1773 * with GL_ARB_framebuffer_object.
1774 */
Brian Paul463642c2005-02-08 02:06:00 +00001775 switch (internalFormat) {
Brian Paulf41bbc72011-01-24 19:38:52 -07001776 case GL_ALPHA:
1777 case GL_ALPHA4:
1778 case GL_ALPHA8:
1779 case GL_ALPHA12:
1780 case GL_ALPHA16:
Brian Paulc1377ed2014-03-22 10:31:58 -06001781 return (ctx->API == API_OPENGL_COMPAT &&
1782 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
Brian Paul976ea9d2011-01-24 19:38:52 -07001783 case GL_LUMINANCE:
1784 case GL_LUMINANCE4:
1785 case GL_LUMINANCE8:
1786 case GL_LUMINANCE12:
1787 case GL_LUMINANCE16:
Brian Paulc1377ed2014-03-22 10:31:58 -06001788 return (ctx->API == API_OPENGL_COMPAT &&
1789 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
Brian Paul976ea9d2011-01-24 19:38:52 -07001790 case GL_LUMINANCE_ALPHA:
1791 case GL_LUMINANCE4_ALPHA4:
1792 case GL_LUMINANCE6_ALPHA2:
1793 case GL_LUMINANCE8_ALPHA8:
1794 case GL_LUMINANCE12_ALPHA4:
1795 case GL_LUMINANCE12_ALPHA12:
1796 case GL_LUMINANCE16_ALPHA16:
Brian Paulc1377ed2014-03-22 10:31:58 -06001797 return (ctx->API == API_OPENGL_COMPAT &&
1798 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
Brian Paul976ea9d2011-01-24 19:38:52 -07001799 case GL_INTENSITY:
1800 case GL_INTENSITY4:
1801 case GL_INTENSITY8:
1802 case GL_INTENSITY12:
1803 case GL_INTENSITY16:
Brian Paulc1377ed2014-03-22 10:31:58 -06001804 return (ctx->API == API_OPENGL_COMPAT &&
1805 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
Ian Romanickf0c99d02012-07-27 08:31:12 -07001806 case GL_RGB8:
1807 return GL_RGB;
Brian Paulf41bbc72011-01-24 19:38:52 -07001808 case GL_RGB:
1809 case GL_R3_G3_B2:
1810 case GL_RGB4:
1811 case GL_RGB5:
Brian Paulf41bbc72011-01-24 19:38:52 -07001812 case GL_RGB10:
1813 case GL_RGB12:
1814 case GL_RGB16:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001815 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
Brian Paulf41bbc72011-01-24 19:38:52 -07001816 case GL_SRGB8_EXT:
Matt Turnercbef5372012-11-20 13:45:03 -08001817 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
Brian Paulf41bbc72011-01-24 19:38:52 -07001818 case GL_RGBA4:
1819 case GL_RGB5_A1:
1820 case GL_RGBA8:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001821 return GL_RGBA;
1822 case GL_RGBA:
1823 case GL_RGBA2:
Brian Paulf41bbc72011-01-24 19:38:52 -07001824 case GL_RGBA12:
1825 case GL_RGBA16:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001826 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1827 case GL_RGB10_A2:
Brian Paulf41bbc72011-01-24 19:38:52 -07001828 case GL_SRGB8_ALPHA8_EXT:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001829 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
Brian Paulf0bbbf62005-02-09 03:50:30 +00001830 case GL_STENCIL_INDEX:
1831 case GL_STENCIL_INDEX1_EXT:
1832 case GL_STENCIL_INDEX4_EXT:
Brian Paulf0bbbf62005-02-09 03:50:30 +00001833 case GL_STENCIL_INDEX16_EXT:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001834 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1835 * OpenGL ES, but Mesa does not currently support them.
1836 */
1837 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1838 case GL_STENCIL_INDEX8_EXT:
Brian Paulf0bbbf62005-02-09 03:50:30 +00001839 return GL_STENCIL_INDEX;
1840 case GL_DEPTH_COMPONENT:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001841 case GL_DEPTH_COMPONENT32:
1842 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
Brian Paul2c6f9112005-02-24 05:47:06 +00001843 case GL_DEPTH_COMPONENT16:
1844 case GL_DEPTH_COMPONENT24:
Brian Paulf0bbbf62005-02-09 03:50:30 +00001845 return GL_DEPTH_COMPONENT;
Ian Romanick49493222013-11-13 14:15:11 -08001846 case GL_DEPTH_STENCIL:
1847 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1848 case GL_DEPTH24_STENCIL8:
1849 return GL_DEPTH_STENCIL;
Marek Olšák11652802011-06-01 15:48:51 +02001850 case GL_DEPTH_COMPONENT32F:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001851 return ctx->Version >= 30
Brian Paulc1377ed2014-03-22 10:31:58 -06001852 || (ctx->API == API_OPENGL_COMPAT &&
1853 ctx->Extensions.ARB_depth_buffer_float)
Ian Romanickf0c99d02012-07-27 08:31:12 -07001854 ? GL_DEPTH_COMPONENT : 0;
Marek Olšák11652802011-06-01 15:48:51 +02001855 case GL_DEPTH32F_STENCIL8:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001856 return ctx->Version >= 30
Brian Paulc1377ed2014-03-22 10:31:58 -06001857 || (ctx->API == API_OPENGL_COMPAT &&
1858 ctx->Extensions.ARB_depth_buffer_float)
Ian Romanickf0c99d02012-07-27 08:31:12 -07001859 ? GL_DEPTH_STENCIL : 0;
Brian Pauld3015652011-01-24 19:38:52 -07001860 case GL_RED:
Brian Pauld3015652011-01-24 19:38:52 -07001861 case GL_R16:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001862 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1863 ? GL_RED : 0;
1864 case GL_R8:
1865 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1866 ? GL_RED : 0;
Brian Pauld3015652011-01-24 19:38:52 -07001867 case GL_RG:
Brian Pauld3015652011-01-24 19:38:52 -07001868 case GL_RG16:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001869 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1870 ? GL_RG : 0;
1871 case GL_RG8:
1872 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1873 ? GL_RG : 0;
Marek Olšák0be36992011-03-18 13:44:51 +01001874 /* signed normalized texture formats */
Ian Romanickf0c99d02012-07-27 08:31:12 -07001875 case GL_RED_SNORM:
Ian Romanick2aae3ab2012-12-04 10:36:10 -08001876 case GL_R8_SNORM:
Marek Olšák0be36992011-03-18 13:44:51 +01001877 case GL_R16_SNORM:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001878 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1879 ? GL_RED : 0;
Ian Romanickf0c99d02012-07-27 08:31:12 -07001880 case GL_RG_SNORM:
Ian Romanick2aae3ab2012-12-04 10:36:10 -08001881 case GL_RG8_SNORM:
Marek Olšák0be36992011-03-18 13:44:51 +01001882 case GL_RG16_SNORM:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001883 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1884 ? GL_RG : 0;
Ian Romanickf0c99d02012-07-27 08:31:12 -07001885 case GL_RGB_SNORM:
Ian Romanick2aae3ab2012-12-04 10:36:10 -08001886 case GL_RGB8_SNORM:
Marek Olšák0be36992011-03-18 13:44:51 +01001887 case GL_RGB16_SNORM:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001888 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1889 ? GL_RGB : 0;
Ian Romanickf0c99d02012-07-27 08:31:12 -07001890 case GL_RGBA_SNORM:
Ian Romanick2aae3ab2012-12-04 10:36:10 -08001891 case GL_RGBA8_SNORM:
Marek Olšák0be36992011-03-18 13:44:51 +01001892 case GL_RGBA16_SNORM:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001893 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1894 ? GL_RGBA : 0;
Marek Olšák0be36992011-03-18 13:44:51 +01001895 case GL_ALPHA_SNORM:
1896 case GL_ALPHA8_SNORM:
1897 case GL_ALPHA16_SNORM:
Paul Berrydbd61352012-11-27 12:26:51 -08001898 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07001899 ctx->Extensions.EXT_texture_snorm &&
Marek Olšák0be36992011-03-18 13:44:51 +01001900 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
Brian Paul513a3242014-01-08 09:05:29 -07001901 case GL_LUMINANCE_SNORM:
1902 case GL_LUMINANCE8_SNORM:
1903 case GL_LUMINANCE16_SNORM:
1904 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1905 ? GL_LUMINANCE : 0;
1906 case GL_LUMINANCE_ALPHA_SNORM:
1907 case GL_LUMINANCE8_ALPHA8_SNORM:
1908 case GL_LUMINANCE16_ALPHA16_SNORM:
1909 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1910 ? GL_LUMINANCE_ALPHA : 0;
1911 case GL_INTENSITY_SNORM:
1912 case GL_INTENSITY8_SNORM:
1913 case GL_INTENSITY16_SNORM:
1914 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1915 ? GL_INTENSITY : 0;
1916
Marek Olšák15f99d12011-02-16 00:35:44 +01001917 case GL_R16F:
1918 case GL_R32F:
Jordan Justen119002a2013-01-10 17:29:27 -08001919 return ((_mesa_is_desktop_gl(ctx) &&
1920 ctx->Extensions.ARB_texture_rg &&
1921 ctx->Extensions.ARB_texture_float) ||
1922 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1923 ? GL_RED : 0;
Marek Olšák15f99d12011-02-16 00:35:44 +01001924 case GL_RG16F:
1925 case GL_RG32F:
Jordan Justen119002a2013-01-10 17:29:27 -08001926 return ((_mesa_is_desktop_gl(ctx) &&
1927 ctx->Extensions.ARB_texture_rg &&
1928 ctx->Extensions.ARB_texture_float) ||
1929 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1930 ? GL_RG : 0;
Marek Olšák15f99d12011-02-16 00:35:44 +01001931 case GL_RGB16F:
1932 case GL_RGB32F:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001933 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
Ian Romanickf0c99d02012-07-27 08:31:12 -07001934 ? GL_RGB : 0;
Marek Olšák15f99d12011-02-16 00:35:44 +01001935 case GL_RGBA16F:
1936 case GL_RGBA32F:
Jordan Justen119002a2013-01-10 17:29:27 -08001937 return ((_mesa_is_desktop_gl(ctx) &&
1938 ctx->Extensions.ARB_texture_float) ||
1939 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
Ian Romanickf0c99d02012-07-27 08:31:12 -07001940 ? GL_RGBA : 0;
Marek Olšák15f99d12011-02-16 00:35:44 +01001941 case GL_ALPHA16F_ARB:
1942 case GL_ALPHA32F_ARB:
Paul Berrydbd61352012-11-27 12:26:51 -08001943 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07001944 ctx->Extensions.ARB_texture_float &&
Marek Olšák15f99d12011-02-16 00:35:44 +01001945 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1946 case GL_LUMINANCE16F_ARB:
1947 case GL_LUMINANCE32F_ARB:
Paul Berrydbd61352012-11-27 12:26:51 -08001948 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07001949 ctx->Extensions.ARB_texture_float &&
Marek Olšák15f99d12011-02-16 00:35:44 +01001950 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1951 case GL_LUMINANCE_ALPHA16F_ARB:
1952 case GL_LUMINANCE_ALPHA32F_ARB:
Paul Berrydbd61352012-11-27 12:26:51 -08001953 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07001954 ctx->Extensions.ARB_texture_float &&
Marek Olšák15f99d12011-02-16 00:35:44 +01001955 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1956 case GL_INTENSITY16F_ARB:
1957 case GL_INTENSITY32F_ARB:
Paul Berrydbd61352012-11-27 12:26:51 -08001958 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07001959 ctx->Extensions.ARB_texture_float &&
Marek Olšák15f99d12011-02-16 00:35:44 +01001960 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
Marek Olšák631d23d2011-04-26 02:27:25 +02001961 case GL_R11F_G11F_B10F:
Jordan Justen119002a2013-01-10 17:29:27 -08001962 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1963 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
Matt Turnercbef5372012-11-20 13:45:03 -08001964 ? GL_RGB : 0;
Dave Airlie9c697a92011-10-04 20:59:40 +01001965
1966 case GL_RGBA8UI_EXT:
1967 case GL_RGBA16UI_EXT:
1968 case GL_RGBA32UI_EXT:
1969 case GL_RGBA8I_EXT:
1970 case GL_RGBA16I_EXT:
1971 case GL_RGBA32I_EXT:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001972 return ctx->Version >= 30
1973 || (_mesa_is_desktop_gl(ctx) &&
1974 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
Dave Airlie9c697a92011-10-04 20:59:40 +01001975
1976 case GL_RGB8UI_EXT:
1977 case GL_RGB16UI_EXT:
1978 case GL_RGB32UI_EXT:
1979 case GL_RGB8I_EXT:
1980 case GL_RGB16I_EXT:
1981 case GL_RGB32I_EXT:
Ian Romanicka86d6292012-12-03 11:55:12 -08001982 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1983 ? GL_RGB : 0;
Dave Airlie9c697a92011-10-04 20:59:40 +01001984 case GL_R8UI:
1985 case GL_R8I:
1986 case GL_R16UI:
1987 case GL_R16I:
1988 case GL_R32UI:
1989 case GL_R32I:
Ian Romanickf0c99d02012-07-27 08:31:12 -07001990 return ctx->Version >= 30
1991 || (_mesa_is_desktop_gl(ctx) &&
1992 ctx->Extensions.ARB_texture_rg &&
1993 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
Dave Airlie9c697a92011-10-04 20:59:40 +01001994
1995 case GL_RG8UI:
1996 case GL_RG8I:
1997 case GL_RG16UI:
1998 case GL_RG16I:
1999 case GL_RG32UI:
2000 case GL_RG32I:
Ian Romanickf0c99d02012-07-27 08:31:12 -07002001 return ctx->Version >= 30
2002 || (_mesa_is_desktop_gl(ctx) &&
2003 ctx->Extensions.ARB_texture_rg &&
2004 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
Brian Paul45bd5c42011-12-16 08:44:43 -07002005
Dave Airlie9c697a92011-10-04 20:59:40 +01002006 case GL_INTENSITY8I_EXT:
2007 case GL_INTENSITY8UI_EXT:
2008 case GL_INTENSITY16I_EXT:
2009 case GL_INTENSITY16UI_EXT:
2010 case GL_INTENSITY32I_EXT:
2011 case GL_INTENSITY32UI_EXT:
Paul Berrydbd61352012-11-27 12:26:51 -08002012 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07002013 ctx->Extensions.EXT_texture_integer &&
Dave Airlie9c697a92011-10-04 20:59:40 +01002014 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
2015
2016 case GL_LUMINANCE8I_EXT:
2017 case GL_LUMINANCE8UI_EXT:
2018 case GL_LUMINANCE16I_EXT:
2019 case GL_LUMINANCE16UI_EXT:
2020 case GL_LUMINANCE32I_EXT:
2021 case GL_LUMINANCE32UI_EXT:
Paul Berrydbd61352012-11-27 12:26:51 -08002022 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07002023 ctx->Extensions.EXT_texture_integer &&
Dave Airlie9c697a92011-10-04 20:59:40 +01002024 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
2025
2026 case GL_LUMINANCE_ALPHA8I_EXT:
2027 case GL_LUMINANCE_ALPHA8UI_EXT:
2028 case GL_LUMINANCE_ALPHA16I_EXT:
2029 case GL_LUMINANCE_ALPHA16UI_EXT:
2030 case GL_LUMINANCE_ALPHA32I_EXT:
2031 case GL_LUMINANCE_ALPHA32UI_EXT:
Paul Berrydbd61352012-11-27 12:26:51 -08002032 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07002033 ctx->Extensions.EXT_texture_integer &&
Dave Airlie9c697a92011-10-04 20:59:40 +01002034 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
Dave Airlief449be62011-11-27 16:21:02 +00002035
Marek Olšák636802f2012-01-22 20:21:36 +01002036 case GL_ALPHA8I_EXT:
2037 case GL_ALPHA8UI_EXT:
2038 case GL_ALPHA16I_EXT:
2039 case GL_ALPHA16UI_EXT:
2040 case GL_ALPHA32I_EXT:
2041 case GL_ALPHA32UI_EXT:
Paul Berrydbd61352012-11-27 12:26:51 -08002042 return ctx->API == API_OPENGL_COMPAT &&
Ian Romanickf0c99d02012-07-27 08:31:12 -07002043 ctx->Extensions.EXT_texture_integer &&
Marek Olšák636802f2012-01-22 20:21:36 +01002044 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
2045
Dave Airlief449be62011-11-27 16:21:02 +00002046 case GL_RGB10_A2UI:
Ian Romanickf0c99d02012-07-27 08:31:12 -07002047 return (_mesa_is_desktop_gl(ctx) &&
2048 ctx->Extensions.ARB_texture_rgb10_a2ui)
2049 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
Marek Olšák1a06e842012-07-12 14:07:41 +02002050
2051 case GL_RGB565:
Ian Romanickf0c99d02012-07-27 08:31:12 -07002052 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
2053 ? GL_RGB : 0;
Brian Paulf41bbc72011-01-24 19:38:52 -07002054 default:
Eric Anholt65c41d52011-01-13 10:05:50 -08002055 return 0;
Brian Paulf41bbc72011-01-24 19:38:52 -07002056 }
Brian Paul463642c2005-02-08 02:06:00 +00002057}
2058
2059
Marek Olšákdf818d52011-03-06 05:26:12 +01002060/**
2061 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
2062 */
2063static void
2064invalidate_rb(GLuint key, void *data, void *userData)
2065{
2066 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2067 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
2068
2069 /* If this is a user-created FBO */
Brian Paul36ede892012-01-12 09:17:23 -07002070 if (_mesa_is_user_fbo(fb)) {
Marek Olšákdf818d52011-03-06 05:26:12 +01002071 GLuint i;
2072 for (i = 0; i < BUFFER_COUNT; i++) {
2073 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2074 if (att->Type == GL_RENDERBUFFER &&
2075 att->Renderbuffer == rb) {
2076 /* Mark fb status as indeterminate to force re-validation */
2077 fb->_Status = 0;
Marek Olšáka674ef72011-03-07 23:33:36 +01002078 return;
Marek Olšákdf818d52011-03-06 05:26:12 +01002079 }
2080 }
2081 }
2082}
2083
2084
Brian Paul4f3514e2009-01-22 15:19:56 -07002085/** sentinal value, see below */
2086#define NO_SAMPLES 1000
2087
Ian Romanickeb5bc622015-11-12 09:11:20 -08002088void
2089_mesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2090 GLenum internalFormat, GLsizei width,
2091 GLsizei height, GLsizei samples)
2092{
2093 const GLenum baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2094
2095 assert(baseFormat != 0);
2096 assert(width >= 0 && width <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2097 assert(height >= 0 && height <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2098 assert(samples != NO_SAMPLES);
2099 if (samples != 0) {
2100 assert(samples > 0);
2101 assert(_mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2102 internalFormat, samples) == GL_NO_ERROR);
2103 }
2104
2105 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2106
2107 if (rb->InternalFormat == internalFormat &&
2108 rb->Width == (GLuint) width &&
2109 rb->Height == (GLuint) height &&
2110 rb->NumSamples == samples) {
2111 /* no change in allocation needed */
2112 return;
2113 }
2114
2115 /* These MUST get set by the AllocStorage func */
2116 rb->Format = MESA_FORMAT_NONE;
2117 rb->NumSamples = samples;
2118
2119 /* Now allocate the storage */
2120 assert(rb->AllocStorage);
2121 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
2122 /* No error - check/set fields now */
2123 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2124 assert(rb->Width == (GLuint) width);
2125 assert(rb->Height == (GLuint) height);
2126 rb->InternalFormat = internalFormat;
2127 rb->_BaseFormat = baseFormat;
2128 assert(rb->_BaseFormat != 0);
2129 }
2130 else {
2131 /* Probably ran out of memory - clear the fields */
2132 rb->Width = 0;
2133 rb->Height = 0;
2134 rb->Format = MESA_FORMAT_NONE;
2135 rb->InternalFormat = GL_NONE;
2136 rb->_BaseFormat = GL_NONE;
2137 rb->NumSamples = 0;
2138 }
2139
2140 /* Invalidate the framebuffers the renderbuffer is attached in. */
2141 if (rb->AttachedAnytime) {
2142 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
2143 }
2144}
Brian Paul4f3514e2009-01-22 15:19:56 -07002145
2146/**
Martin Peresbf11c192015-02-13 15:35:48 +02002147 * Helper function used by renderbuffer_storage_direct() and
2148 * renderbuffer_storage_target().
2149 * samples will be NO_SAMPLES if called by a non-multisample function.
Brian Paul4f3514e2009-01-22 15:19:56 -07002150 */
2151static void
Martin Peresbf11c192015-02-13 15:35:48 +02002152renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2153 GLenum internalFormat, GLsizei width,
2154 GLsizei height, GLsizei samples, const char *func)
Brian Paulddc82ee2005-02-05 19:56:45 +00002155{
Brian Paul463642c2005-02-08 02:06:00 +00002156 GLenum baseFormat;
Chris Forbes90b5a242013-02-06 20:42:53 +13002157 GLenum sample_count_error;
Brian Paulddc82ee2005-02-05 19:56:45 +00002158
Brian Paul59e0faa2006-03-15 17:48:00 +00002159 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
Brian Paul463642c2005-02-08 02:06:00 +00002160 if (baseFormat == 0) {
Jordan Justen119002a2013-01-10 17:29:27 -08002161 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002162 func, _mesa_enum_to_string(internalFormat));
Brian Paulddc82ee2005-02-05 19:56:45 +00002163 return;
2164 }
2165
Yuanhan Liu49f84472011-10-25 15:36:59 +08002166 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
Martin Peresbf11c192015-02-13 15:35:48 +02002167 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
2168 width);
Brian Paulddc82ee2005-02-05 19:56:45 +00002169 return;
2170 }
2171
Yuanhan Liu49f84472011-10-25 15:36:59 +08002172 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
Martin Peresbf11c192015-02-13 15:35:48 +02002173 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
2174 height);
Brian Paul4f3514e2009-01-22 15:19:56 -07002175 return;
2176 }
2177
2178 if (samples == NO_SAMPLES) {
2179 /* NumSamples == 0 indicates non-multisampling */
2180 samples = 0;
2181 }
Chris Forbes21a2dfa2013-03-25 23:19:07 +13002182 else {
2183 /* check the sample count;
2184 * note: driver may choose to use more samples than what's requested
2185 */
Martin Peresbf11c192015-02-13 15:35:48 +02002186 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
Chris Forbes21a2dfa2013-03-25 23:19:07 +13002187 internalFormat, samples);
Timothy Arceri46684d32015-08-13 18:44:51 +10002188
2189 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2190 *
2191 * "If a negative number is provided where an argument of type sizei or
2192 * sizeiptr is specified, the error INVALID VALUE is generated."
2193 */
2194 if (samples < 0) {
2195 sample_count_error = GL_INVALID_VALUE;
2196 }
2197
Chris Forbes21a2dfa2013-03-25 23:19:07 +13002198 if (sample_count_error != GL_NO_ERROR) {
Brian Paul0e23f372016-07-06 17:10:52 -06002199 _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
Chris Forbes21a2dfa2013-03-25 23:19:07 +13002200 return;
2201 }
Brian Paulddc82ee2005-02-05 19:56:45 +00002202 }
2203
Ian Romanickeb5bc622015-11-12 09:11:20 -08002204 _mesa_renderbuffer_storage(ctx, rb, internalFormat, width, height, samples);
Brian Paulddc82ee2005-02-05 19:56:45 +00002205}
2206
Martin Peresbf11c192015-02-13 15:35:48 +02002207/**
2208 * Helper function used by _mesa_NamedRenderbufferStorage*().
2209 * samples will be NO_SAMPLES if called by a non-multisample function.
2210 */
2211static void
2212renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
2213 GLsizei width, GLsizei height, GLsizei samples,
2214 const char *func)
2215{
2216 GET_CURRENT_CONTEXT(ctx);
2217
2218 if (MESA_VERBOSE & VERBOSE_API) {
2219 if (samples == NO_SAMPLES)
2220 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
2221 func, renderbuffer,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002222 _mesa_enum_to_string(internalFormat),
Martin Peresbf11c192015-02-13 15:35:48 +02002223 width, height);
2224 else
2225 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
2226 func, renderbuffer,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002227 _mesa_enum_to_string(internalFormat),
Martin Peresbf11c192015-02-13 15:35:48 +02002228 width, height, samples);
2229 }
2230
2231 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2232 if (!rb || rb == &DummyRenderbuffer) {
2233 /* ID was reserved, but no real renderbuffer object made yet */
2234 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
2235 func, renderbuffer);
2236 return;
2237 }
2238
2239 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
2240}
2241
2242/**
2243 * Helper function used by _mesa_RenderbufferStorage() and
2244 * _mesa_RenderbufferStorageMultisample().
2245 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2246 */
2247static void
2248renderbuffer_storage_target(GLenum target, GLenum internalFormat,
2249 GLsizei width, GLsizei height, GLsizei samples,
2250 const char *func)
2251{
2252 GET_CURRENT_CONTEXT(ctx);
2253
2254 if (MESA_VERBOSE & VERBOSE_API) {
2255 if (samples == NO_SAMPLES)
2256 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
2257 func,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002258 _mesa_enum_to_string(target),
2259 _mesa_enum_to_string(internalFormat),
Martin Peresbf11c192015-02-13 15:35:48 +02002260 width, height);
2261 else
2262 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
2263 func,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002264 _mesa_enum_to_string(target),
2265 _mesa_enum_to_string(internalFormat),
Martin Peresbf11c192015-02-13 15:35:48 +02002266 width, height, samples);
2267 }
2268
2269 if (target != GL_RENDERBUFFER_EXT) {
2270 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
2271 return;
2272 }
2273
2274 if (!ctx->CurrentRenderbuffer) {
2275 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
2276 func);
2277 return;
2278 }
2279
2280 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
2281 height, samples, func);
2282}
2283
Brian Paul23c5b212010-05-28 13:33:03 -06002284
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002285void GLAPIENTRY
Brian Paul23c5b212010-05-28 13:33:03 -06002286_mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002287{
Brian Paul51b79922010-02-24 11:57:26 -07002288 struct gl_renderbuffer *rb;
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002289 GET_CURRENT_CONTEXT(ctx);
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002290
Chia-I Wu2002e4d02010-04-06 17:46:17 +08002291 if (!ctx->Extensions.OES_EGL_image) {
2292 _mesa_error(ctx, GL_INVALID_OPERATION,
2293 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2294 return;
2295 }
2296
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002297 if (target != GL_RENDERBUFFER) {
Brian Paul23c5b212010-05-28 13:33:03 -06002298 _mesa_error(ctx, GL_INVALID_ENUM,
2299 "EGLImageTargetRenderbufferStorageOES");
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002300 return;
2301 }
2302
2303 rb = ctx->CurrentRenderbuffer;
2304 if (!rb) {
Brian Paul23c5b212010-05-28 13:33:03 -06002305 _mesa_error(ctx, GL_INVALID_OPERATION,
2306 "EGLImageTargetRenderbufferStorageOES");
Kristian Høgsbergd1dc5b12010-02-11 17:42:30 -05002307 return;
2308 }
2309
2310 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2311
2312 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2313}
Brian Paulddc82ee2005-02-05 19:56:45 +00002314
Brian Paul23c5b212010-05-28 13:33:03 -06002315
Brian Paul45e76d22009-10-08 20:27:27 -06002316/**
Paul Berry1a1db172012-11-06 08:57:59 -08002317 * Helper function for _mesa_GetRenderbufferParameteriv() and
2318 * _mesa_GetFramebufferAttachmentParameteriv()
Brian Paul45e76d22009-10-08 20:27:27 -06002319 * We have to be careful to respect the base format. For example, if a
2320 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2321 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2322 * we need to return zero.
2323 */
2324static GLint
Mark Mueller71fe9432014-01-04 14:11:43 -08002325get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
Brian Paul45e76d22009-10-08 20:27:27 -06002326{
Brian Paulf0b6e9a2011-11-23 15:33:45 -07002327 if (_mesa_base_format_has_channel(baseFormat, pname))
2328 return _mesa_get_format_bits(format, pname);
2329 else
Brian Paul45e76d22009-10-08 20:27:27 -06002330 return 0;
Brian Paul45e76d22009-10-08 20:27:27 -06002331}
2332
2333
2334
Brian Paul1864c7d2005-02-08 03:46:37 +00002335void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08002336_mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
Brian Paul4f3514e2009-01-22 15:19:56 -07002337 GLsizei width, GLsizei height)
2338{
Brian Paul722d9762009-01-20 16:58:49 -07002339 /* GL_ARB_fbo says calling this function is equivalent to calling
2340 * glRenderbufferStorageMultisample() with samples=0. We pass in
2341 * a token value here just for error reporting purposes.
2342 */
Martin Peresbf11c192015-02-13 15:35:48 +02002343 renderbuffer_storage_target(target, internalFormat, width, height,
2344 NO_SAMPLES, "glRenderbufferStorage");
Brian Paul4f3514e2009-01-22 15:19:56 -07002345}
2346
2347
2348void GLAPIENTRY
Brian Paul777a2ef2009-01-22 15:17:42 -07002349_mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
Brian Paul4f3514e2009-01-22 15:19:56 -07002350 GLenum internalFormat,
Brian Paul777a2ef2009-01-22 15:17:42 -07002351 GLsizei width, GLsizei height)
2352{
Martin Peresbf11c192015-02-13 15:35:48 +02002353 renderbuffer_storage_target(target, internalFormat, width, height,
2354 samples, "glRenderbufferStorageMultisample");
Brian Paul777a2ef2009-01-22 15:17:42 -07002355}
2356
Brian Paul23c5b212010-05-28 13:33:03 -06002357
2358/**
2359 * OpenGL ES version of glRenderBufferStorage.
2360 */
Kristian Høgsberg61d94dd2010-04-22 21:11:56 -04002361void GLAPIENTRY
2362_es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
Martin Peres7bd8b482015-02-12 17:54:43 +02002363 GLsizei width, GLsizei height)
Kristian Høgsberg61d94dd2010-04-22 21:11:56 -04002364{
2365 switch (internalFormat) {
2366 case GL_RGB565:
2367 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2368 /* choose a closest format */
2369 internalFormat = GL_RGB5;
2370 break;
2371 default:
2372 break;
2373 }
Brian Paul777a2ef2009-01-22 15:17:42 -07002374
Martin Peresbf11c192015-02-13 15:35:48 +02002375 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2376 "glRenderbufferStorageEXT");
2377}
2378
2379void GLAPIENTRY
2380_mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2381 GLsizei width, GLsizei height)
2382{
2383 /* GL_ARB_fbo says calling this function is equivalent to calling
2384 * glRenderbufferStorageMultisample() with samples=0. We pass in
2385 * a token value here just for error reporting purposes.
2386 */
2387 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2388 NO_SAMPLES, "glNamedRenderbufferStorage");
2389}
2390
2391void GLAPIENTRY
2392_mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2393 GLenum internalformat,
2394 GLsizei width, GLsizei height)
2395{
2396 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2397 samples,
2398 "glNamedRenderbufferStorageMultisample");
Kristian Høgsberg61d94dd2010-04-22 21:11:56 -04002399}
Brian Paul777a2ef2009-01-22 15:17:42 -07002400
Brian Paul23c5b212010-05-28 13:33:03 -06002401
Martin Peres245e5c42015-02-13 15:46:55 +02002402static void
2403get_render_buffer_parameteriv(struct gl_context *ctx,
2404 struct gl_renderbuffer *rb, GLenum pname,
2405 GLint *params, const char *func)
Brian Paulddc82ee2005-02-05 19:56:45 +00002406{
Brian Paul800e5532009-11-02 15:39:39 -07002407 /* No need to flush here since we're just quering state which is
2408 * not effected by rendering.
2409 */
Brian Paul474f28e2005-10-08 14:41:17 +00002410
Brian Paul463642c2005-02-08 02:06:00 +00002411 switch (pname) {
2412 case GL_RENDERBUFFER_WIDTH_EXT:
Brian Paul722d9762009-01-20 16:58:49 -07002413 *params = rb->Width;
Brian Paul463642c2005-02-08 02:06:00 +00002414 return;
2415 case GL_RENDERBUFFER_HEIGHT_EXT:
Brian Paul722d9762009-01-20 16:58:49 -07002416 *params = rb->Height;
Brian Paul463642c2005-02-08 02:06:00 +00002417 return;
2418 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
Brian Paul722d9762009-01-20 16:58:49 -07002419 *params = rb->InternalFormat;
Brian Paul463642c2005-02-08 02:06:00 +00002420 return;
Brian Paul1b939532005-05-31 23:55:21 +00002421 case GL_RENDERBUFFER_RED_SIZE_EXT:
Brian Paul1b939532005-05-31 23:55:21 +00002422 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
Brian Paul1b939532005-05-31 23:55:21 +00002423 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
Brian Paul1b939532005-05-31 23:55:21 +00002424 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
Brian Paul1b939532005-05-31 23:55:21 +00002425 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
Brian Paul1b939532005-05-31 23:55:21 +00002426 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
Brian Paul45e76d22009-10-08 20:27:27 -06002427 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
Brian Paul1b939532005-05-31 23:55:21 +00002428 break;
Brian Paul722d9762009-01-20 16:58:49 -07002429 case GL_RENDERBUFFER_SAMPLES:
Ian Romanickae86ebf2012-07-27 07:49:49 -07002430 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2431 || _mesa_is_gles3(ctx)) {
Brian Paul722d9762009-01-20 16:58:49 -07002432 *params = rb->NumSamples;
2433 break;
2434 }
2435 /* fallthrough */
Brian Paul463642c2005-02-08 02:06:00 +00002436 default:
Martin Peres245e5c42015-02-13 15:46:55 +02002437 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002438 _mesa_enum_to_string(pname));
Martin Peres245e5c42015-02-13 15:46:55 +02002439 return;
2440 }
2441}
2442
2443
2444void GLAPIENTRY
2445_mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2446{
2447 GET_CURRENT_CONTEXT(ctx);
2448
2449 if (target != GL_RENDERBUFFER_EXT) {
Brian Paul463642c2005-02-08 02:06:00 +00002450 _mesa_error(ctx, GL_INVALID_ENUM,
2451 "glGetRenderbufferParameterivEXT(target)");
2452 return;
2453 }
Martin Peres245e5c42015-02-13 15:46:55 +02002454
2455 if (!ctx->CurrentRenderbuffer) {
2456 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2457 "(no renderbuffer bound)");
2458 return;
2459 }
2460
2461 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2462 params, "glGetRenderbufferParameteriv");
2463}
2464
2465
2466void GLAPIENTRY
2467_mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2468 GLint *params)
2469{
2470 GET_CURRENT_CONTEXT(ctx);
2471
2472 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2473 if (!rb || rb == &DummyRenderbuffer) {
2474 /* ID was reserved, but no real renderbuffer object made yet */
2475 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2476 "(invalid renderbuffer %i)", renderbuffer);
2477 return;
2478 }
2479
2480 get_render_buffer_parameteriv(ctx, rb, pname, params,
2481 "glGetNamedRenderbufferParameteriv");
Brian Paulddc82ee2005-02-05 19:56:45 +00002482}
2483
2484
Brian Paul1864c7d2005-02-08 03:46:37 +00002485GLboolean GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08002486_mesa_IsFramebuffer(GLuint framebuffer)
Brian Paulddc82ee2005-02-05 19:56:45 +00002487{
Brian Paulddc82ee2005-02-05 19:56:45 +00002488 GET_CURRENT_CONTEXT(ctx);
Brian Paulddc82ee2005-02-05 19:56:45 +00002489 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
Brian Paulbc6cced2005-10-04 15:01:27 +00002490 if (framebuffer) {
Brian Paulea4fe662006-03-26 05:22:17 +00002491 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
Brian Paulbc6cced2005-10-04 15:01:27 +00002492 if (rb != NULL && rb != &DummyFramebuffer)
2493 return GL_TRUE;
2494 }
2495 return GL_FALSE;
Brian Paulddc82ee2005-02-05 19:56:45 +00002496}
2497
2498
briana492ab72009-11-10 15:33:31 -07002499/**
2500 * Check if any of the attachments of the given framebuffer are textures
2501 * (render to texture). Call ctx->Driver.RenderTexture() for such
2502 * attachments.
2503 */
Brian Paulea4fe662006-03-26 05:22:17 +00002504static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002505check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
Brian Paulea4fe662006-03-26 05:22:17 +00002506{
2507 GLuint i;
Matt Turnerbfcdb842015-02-20 20:18:47 -08002508 assert(ctx->Driver.RenderTexture);
briana65b84d2009-11-10 18:02:03 -07002509
Brian Paul36ede892012-01-12 09:17:23 -07002510 if (_mesa_is_winsys_fbo(fb))
briana65b84d2009-11-10 18:02:03 -07002511 return; /* can't render to texture with winsys framebuffers */
2512
Brian Paulea4fe662006-03-26 05:22:17 +00002513 for (i = 0; i < BUFFER_COUNT; i++) {
2514 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
Ian Romanickfb497132013-07-27 12:04:20 -07002515 if (att->Texture && att->Renderbuffer->TexImage
2516 && driver_RenderTexture_is_safe(att)) {
Brian Paulea4fe662006-03-26 05:22:17 +00002517 ctx->Driver.RenderTexture(ctx, fb, att);
2518 }
2519 }
2520}
2521
2522
Brian Paul0e31e022005-12-01 00:25:00 +00002523/**
2524 * Examine all the framebuffer's attachments to see if any are textures.
2525 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2526 * notify the device driver that the texture image may have changed.
2527 */
2528static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002529check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
Brian Paul0e31e022005-12-01 00:25:00 +00002530{
Eric Anholt7ccb26f2014-03-03 09:16:48 -08002531 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2532 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2533 return;
briana65b84d2009-11-10 18:02:03 -07002534
Brian Paul0e31e022005-12-01 00:25:00 +00002535 if (ctx->Driver.FinishRenderTexture) {
2536 GLuint i;
2537 for (i = 0; i < BUFFER_COUNT; i++) {
2538 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
Eric Anholtc810e672013-05-10 12:36:43 -07002539 struct gl_renderbuffer *rb = att->Renderbuffer;
2540 if (rb && rb->NeedsFinishRenderTexture) {
Eric Anholta5b04522013-05-10 12:17:52 -07002541 ctx->Driver.FinishRenderTexture(ctx, rb);
Brian Paul0e31e022005-12-01 00:25:00 +00002542 }
2543 }
2544 }
2545}
2546
2547
Ian Romanick4a9522a2013-07-18 17:39:22 -07002548static void
2549bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
Brian Paulddc82ee2005-02-05 19:56:45 +00002550{
briane6f60d32009-11-10 15:47:34 -07002551 struct gl_framebuffer *newDrawFb, *newReadFb;
Brian Paul0bffb112005-11-08 14:45:48 +00002552 GLboolean bindReadBuf, bindDrawBuf;
Brian Paulddc82ee2005-02-05 19:56:45 +00002553 GET_CURRENT_CONTEXT(ctx);
2554
Brian Paul0bffb112005-11-08 14:45:48 +00002555 switch (target) {
Brian Paul0bffb112005-11-08 14:45:48 +00002556 case GL_DRAW_FRAMEBUFFER_EXT:
Brian Paul0bffb112005-11-08 14:45:48 +00002557 bindDrawBuf = GL_TRUE;
2558 bindReadBuf = GL_FALSE;
2559 break;
2560 case GL_READ_FRAMEBUFFER_EXT:
Brian Paul0bffb112005-11-08 14:45:48 +00002561 bindDrawBuf = GL_FALSE;
2562 bindReadBuf = GL_TRUE;
2563 break;
Brian Paul0bffb112005-11-08 14:45:48 +00002564 case GL_FRAMEBUFFER_EXT:
2565 bindDrawBuf = GL_TRUE;
2566 bindReadBuf = GL_TRUE;
2567 break;
2568 default:
Brian Pauleba4ff62005-09-06 21:22:16 +00002569 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
Brian Paulddc82ee2005-02-05 19:56:45 +00002570 return;
2571 }
2572
Brian Paul3deaa012005-02-07 05:08:24 +00002573 if (framebuffer) {
Brian Paule4b23562005-05-04 20:11:35 +00002574 /* Binding a user-created framebuffer object */
briane6f60d32009-11-10 15:47:34 -07002575 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2576 if (newDrawFb == &DummyFramebuffer) {
Brian Paul923b6fc2005-02-08 04:08:56 +00002577 /* ID was reserved, but no real framebuffer object made yet */
briane6f60d32009-11-10 15:47:34 -07002578 newDrawFb = NULL;
Brian Paul1864c7d2005-02-08 03:46:37 +00002579 }
Ian Romanick4a9522a2013-07-18 17:39:22 -07002580 else if (!newDrawFb && !allow_user_names) {
Brian Paul1bc59bf2009-01-22 15:07:34 -07002581 /* All FBO IDs must be Gen'd */
2582 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2583 return;
2584 }
2585
briane6f60d32009-11-10 15:47:34 -07002586 if (!newDrawFb) {
Martin Peres7bd8b482015-02-12 17:54:43 +02002587 /* create new framebuffer object */
2588 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2589 if (!newDrawFb) {
2590 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2591 return;
2592 }
briane6f60d32009-11-10 15:47:34 -07002593 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
Brian Paul3deaa012005-02-07 05:08:24 +00002594 }
briane6f60d32009-11-10 15:47:34 -07002595 newReadFb = newDrawFb;
Brian Paul3deaa012005-02-07 05:08:24 +00002596 }
Brian Paul463642c2005-02-08 02:06:00 +00002597 else {
Brian Paule4b23562005-05-04 20:11:35 +00002598 /* Binding the window system framebuffer (which was originally set
2599 * with MakeCurrent).
2600 */
briane6f60d32009-11-10 15:47:34 -07002601 newDrawFb = ctx->WinSysDrawBuffer;
2602 newReadFb = ctx->WinSysReadBuffer;
Brian Paul3deaa012005-02-07 05:08:24 +00002603 }
2604
Ian Romanickfed9b0e2015-11-13 10:46:40 -08002605 _mesa_bind_framebuffers(ctx,
2606 bindDrawBuf ? newDrawFb : ctx->DrawBuffer,
2607 bindReadBuf ? newReadFb : ctx->ReadBuffer);
2608}
2609
2610void
2611_mesa_bind_framebuffers(struct gl_context *ctx,
2612 struct gl_framebuffer *newDrawFb,
2613 struct gl_framebuffer *newReadFb)
2614{
2615 struct gl_framebuffer *const oldDrawFb = ctx->DrawBuffer;
2616 struct gl_framebuffer *const oldReadFb = ctx->ReadBuffer;
2617 const bool bindDrawBuf = oldDrawFb != newDrawFb;
2618 const bool bindReadBuf = oldReadFb != newReadFb;
2619
Matt Turnerbfcdb842015-02-20 20:18:47 -08002620 assert(newDrawFb);
2621 assert(newDrawFb != &DummyFramebuffer);
Brian Paul1864c7d2005-02-08 03:46:37 +00002622
Brian Paulea4fe662006-03-26 05:22:17 +00002623 /*
Brian Paul16144632009-02-26 14:49:24 -07002624 * OK, now bind the new Draw/Read framebuffers, if they're changing.
briana65b84d2009-11-10 18:02:03 -07002625 *
2626 * We also check if we're beginning and/or ending render-to-texture.
2627 * When a framebuffer with texture attachments is unbound, call
2628 * ctx->Driver.FinishRenderTexture().
2629 * When a framebuffer with texture attachments is bound, call
2630 * ctx->Driver.RenderTexture().
2631 *
2632 * Note that if the ReadBuffer has texture attachments we don't consider
2633 * that a render-to-texture case.
Brian Paulea4fe662006-03-26 05:22:17 +00002634 */
Brian Paul0bffb112005-11-08 14:45:48 +00002635 if (bindReadBuf) {
briana65b84d2009-11-10 18:02:03 -07002636 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
brianbc569cd2009-11-10 16:00:35 -07002637
briana65b84d2009-11-10 18:02:03 -07002638 /* check if old readbuffer was render-to-texture */
2639 check_end_texture_render(ctx, oldReadFb);
2640
2641 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
Brian Paul0bffb112005-11-08 14:45:48 +00002642 }
2643
2644 if (bindDrawBuf) {
briana65b84d2009-11-10 18:02:03 -07002645 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
Brian32d86eb2007-08-16 18:52:48 +01002646
Eric Anholta1fd13f2012-02-10 12:05:16 -08002647 /* check if old framebuffer had any texture attachments */
2648 if (oldDrawFb)
briana65b84d2009-11-10 18:02:03 -07002649 check_end_texture_render(ctx, oldDrawFb);
brianbc569cd2009-11-10 16:00:35 -07002650
briana65b84d2009-11-10 18:02:03 -07002651 /* check if newly bound framebuffer has any texture attachments */
2652 check_begin_texture_render(ctx, newDrawFb);
2653
2654 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
Brian Paul0bffb112005-11-08 14:45:48 +00002655 }
Brian Paul59e0faa2006-03-15 17:48:00 +00002656
Brian Paul16144632009-02-26 14:49:24 -07002657 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
Ian Romanickfed9b0e2015-11-13 10:46:40 -08002658 /* The few classic drivers that actually hook this function really only
2659 * want to know if the draw framebuffer changed.
2660 */
2661 ctx->Driver.BindFramebuffer(ctx,
2662 bindDrawBuf ? GL_FRAMEBUFFER : GL_READ_FRAMEBUFFER,
2663 newDrawFb, newReadFb);
Brian Paul59e0faa2006-03-15 17:48:00 +00002664 }
Brian Paulddc82ee2005-02-05 19:56:45 +00002665}
2666
Tomasz Lis9f07ca12013-07-16 20:57:26 +02002667void GLAPIENTRY
Ian Romanick4a9522a2013-07-18 17:39:22 -07002668_mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
Tomasz Lis9f07ca12013-07-16 20:57:26 +02002669{
Ian Romanick4a9522a2013-07-18 17:39:22 -07002670 GET_CURRENT_CONTEXT(ctx);
2671
2672 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2673 * point, but they allow the use of user-generated names.
2674 */
2675 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
Tomasz Lis9f07ca12013-07-16 20:57:26 +02002676}
2677
Brian Paulc1377ed2014-03-22 10:31:58 -06002678
Ian Romanick4a9522a2013-07-18 17:39:22 -07002679void GLAPIENTRY
2680_mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2681{
2682 /* This function should not be in the dispatch table for core profile /
2683 * OpenGL 3.1, so execution should never get here in those cases -- no
2684 * need for an explicit test.
2685 */
2686 bind_framebuffer(target, framebuffer, true);
2687}
Brian Paulddc82ee2005-02-05 19:56:45 +00002688
Brian Paulc1377ed2014-03-22 10:31:58 -06002689
Brian Paul1864c7d2005-02-08 03:46:37 +00002690void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08002691_mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
Brian Paulddc82ee2005-02-05 19:56:45 +00002692{
2693 GLint i;
2694 GET_CURRENT_CONTEXT(ctx);
2695
Eduardo Lima Mitevf77a4732014-12-11 23:34:17 +01002696 if (n < 0) {
2697 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2698 return;
2699 }
2700
Brian Paul800e5532009-11-02 15:39:39 -07002701 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
Brian Paulddc82ee2005-02-05 19:56:45 +00002702
2703 for (i = 0; i < n; i++) {
Brian Paul2c6f9112005-02-24 05:47:06 +00002704 if (framebuffers[i] > 0) {
Martin Peres7bd8b482015-02-12 17:54:43 +02002705 struct gl_framebuffer *fb;
2706 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2707 if (fb) {
Matt Turnerbfcdb842015-02-20 20:18:47 -08002708 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
Brian Paul91802fd2005-10-04 16:01:02 +00002709
2710 /* check if deleting currently bound framebuffer object */
Ian Romanick764be9f2013-11-13 14:00:06 -08002711 if (fb == ctx->DrawBuffer) {
2712 /* bind default */
Matt Turnerbfcdb842015-02-20 20:18:47 -08002713 assert(fb->RefCount >= 2);
Ian Romanick764be9f2013-11-13 14:00:06 -08002714 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2715 }
2716 if (fb == ctx->ReadBuffer) {
2717 /* bind default */
Matt Turnerbfcdb842015-02-20 20:18:47 -08002718 assert(fb->RefCount >= 2);
Ian Romanick764be9f2013-11-13 14:00:06 -08002719 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2720 }
Brian Paul91802fd2005-10-04 16:01:02 +00002721
Martin Peres7bd8b482015-02-12 17:54:43 +02002722 /* remove from hash table immediately, to free the ID */
2723 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
Brian Paulddc82ee2005-02-05 19:56:45 +00002724
Brian Paul1864c7d2005-02-08 03:46:37 +00002725 if (fb != &DummyFramebuffer) {
2726 /* But the object will not be freed until it's no longer
2727 * bound in any context.
2728 */
Brian Pauld5229442009-02-09 08:30:55 -07002729 _mesa_reference_framebuffer(&fb, NULL);
Martin Peres7bd8b482015-02-12 17:54:43 +02002730 }
2731 }
Brian Paulddc82ee2005-02-05 19:56:45 +00002732 }
2733 }
2734}
2735
2736
Laura Ekstrandf868de72015-01-23 14:54:48 -08002737/**
2738 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2739 * It is not exposed to the rest of Mesa to encourage the use of
2740 * nameless buffers in driver internals.
2741 */
2742static void
2743create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
Brian Paulddc82ee2005-02-05 19:56:45 +00002744{
2745 GET_CURRENT_CONTEXT(ctx);
2746 GLuint first;
2747 GLint i;
Laura Ekstrandf868de72015-01-23 14:54:48 -08002748 struct gl_framebuffer *fb;
2749
2750 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
Brian Paulddc82ee2005-02-05 19:56:45 +00002751
Brian Paulddc82ee2005-02-05 19:56:45 +00002752 if (n < 0) {
Laura Ekstrandf868de72015-01-23 14:54:48 -08002753 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
Brian Paulddc82ee2005-02-05 19:56:45 +00002754 return;
2755 }
2756
2757 if (!framebuffers)
2758 return;
2759
Matt Turner015f2202015-07-30 14:31:04 -07002760 _mesa_HashLockMutex(ctx->Shared->FrameBuffers);
2761
Brian Paulddc82ee2005-02-05 19:56:45 +00002762 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2763
2764 for (i = 0; i < n; i++) {
Brian Paulddc82ee2005-02-05 19:56:45 +00002765 GLuint name = first + i;
Brian Paulf0bbbf62005-02-09 03:50:30 +00002766 framebuffers[i] = name;
Laura Ekstrandf868de72015-01-23 14:54:48 -08002767
2768 if (dsa) {
2769 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2770 if (!fb) {
Matt Turnerbdc9c202016-05-22 07:00:34 -07002771 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
Laura Ekstrandf868de72015-01-23 14:54:48 -08002772 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2773 return;
2774 }
2775 }
2776 else
2777 fb = &DummyFramebuffer;
2778
Matt Turner015f2202015-07-30 14:31:04 -07002779 _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, name, fb);
Brian Paulddc82ee2005-02-05 19:56:45 +00002780 }
Matt Turner015f2202015-07-30 14:31:04 -07002781
2782 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
Brian Paulddc82ee2005-02-05 19:56:45 +00002783}
2784
2785
Laura Ekstrandf868de72015-01-23 14:54:48 -08002786void GLAPIENTRY
2787_mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2788{
2789 create_framebuffers(n, framebuffers, false);
2790}
2791
2792
2793void GLAPIENTRY
2794_mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2795{
2796 create_framebuffers(n, framebuffers, true);
2797}
2798
2799
Laura Ekstrandf93f9592015-01-29 13:15:37 -08002800GLenum
2801_mesa_check_framebuffer_status(struct gl_context *ctx,
2802 struct gl_framebuffer *buffer)
Brian Paulddc82ee2005-02-05 19:56:45 +00002803{
Brian Paule4b23562005-05-04 20:11:35 +00002804 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
Brian Paulddc82ee2005-02-05 19:56:45 +00002805
Brian Paul36ede892012-01-12 09:17:23 -07002806 if (_mesa_is_winsys_fbo(buffer)) {
Matt Turner8dd15e62013-07-26 16:36:19 -07002807 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2808 if (buffer != &IncompleteFramebuffer) {
2809 return GL_FRAMEBUFFER_COMPLETE_EXT;
2810 } else {
2811 return GL_FRAMEBUFFER_UNDEFINED;
2812 }
Brian Paulf0bbbf62005-02-09 03:50:30 +00002813 }
2814
Brian Paul800e5532009-11-02 15:39:39 -07002815 /* No need to flush here */
Brian Paul474f28e2005-10-08 14:41:17 +00002816
Brian Paul72966362009-01-21 16:28:38 -07002817 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2818 _mesa_test_framebuffer_completeness(ctx, buffer);
2819 }
2820
Brian Paul0bffb112005-11-08 14:45:48 +00002821 return buffer->_Status;
Brian Paulddc82ee2005-02-05 19:56:45 +00002822}
2823
Brian Paul45bd5c42011-12-16 08:44:43 -07002824
Laura Ekstrandf93f9592015-01-29 13:15:37 -08002825GLenum GLAPIENTRY
2826_mesa_CheckFramebufferStatus(GLenum target)
2827{
2828 struct gl_framebuffer *fb;
2829 GET_CURRENT_CONTEXT(ctx);
2830
2831 if (MESA_VERBOSE & VERBOSE_API)
2832 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002833 _mesa_enum_to_string(target));
Laura Ekstrandf93f9592015-01-29 13:15:37 -08002834
2835 fb = get_framebuffer_target(ctx, target);
2836 if (!fb) {
2837 _mesa_error(ctx, GL_INVALID_ENUM,
2838 "glCheckFramebufferStatus(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002839 _mesa_enum_to_string(target));
Laura Ekstrandf93f9592015-01-29 13:15:37 -08002840 return 0;
2841 }
2842
2843 return _mesa_check_framebuffer_status(ctx, fb);
2844}
2845
2846
2847GLenum GLAPIENTRY
2848_mesa_CheckNamedFramebufferStatus(GLuint framebuffer, GLenum target)
2849{
2850 struct gl_framebuffer *fb;
2851 GET_CURRENT_CONTEXT(ctx);
2852
2853 /* Validate the target (for conformance's sake) and grab a reference to the
2854 * default framebuffer in case framebuffer = 0.
2855 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2856 * (30.10.2014, PDF page 336) says:
2857 * "If framebuffer is zero, then the status of the default read or
2858 * draw framebuffer (as determined by target) is returned."
2859 */
2860 switch (target) {
2861 case GL_DRAW_FRAMEBUFFER:
2862 case GL_FRAMEBUFFER:
2863 fb = ctx->WinSysDrawBuffer;
2864 break;
2865 case GL_READ_FRAMEBUFFER:
2866 fb = ctx->WinSysReadBuffer;
2867 break;
2868 default:
2869 _mesa_error(ctx, GL_INVALID_ENUM,
2870 "glCheckNamedFramebufferStatus(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07002871 _mesa_enum_to_string(target));
Laura Ekstrandf93f9592015-01-29 13:15:37 -08002872 return 0;
2873 }
2874
2875 if (framebuffer) {
2876 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
2877 "glCheckNamedFramebufferStatus");
2878 if (!fb)
2879 return 0;
2880 }
2881
2882 return _mesa_check_framebuffer_status(ctx, fb);
2883}
2884
2885
Chad Versacebf8ad172011-11-10 10:19:20 -08002886/**
2887 * Replicate the src attachment point. Used by framebuffer_texture() when
2888 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2889 * GL_STENCIL_ATTACHMENT.
2890 */
2891static void
2892reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2893 gl_buffer_index dst,
2894 gl_buffer_index src)
2895{
2896 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2897 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
Brian Paulddc82ee2005-02-05 19:56:45 +00002898
Chad Versacebf8ad172011-11-10 10:19:20 -08002899 assert(src_att->Texture != NULL);
Brian Paul45bd5c42011-12-16 08:44:43 -07002900 assert(src_att->Renderbuffer != NULL);
Chad Versacebf8ad172011-11-10 10:19:20 -08002901
2902 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2903 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2904 dst_att->Type = src_att->Type;
2905 dst_att->Complete = src_att->Complete;
2906 dst_att->TextureLevel = src_att->TextureLevel;
Nanley Chery63318d32016-11-15 16:42:23 -08002907 dst_att->CubeMapFace = src_att->CubeMapFace;
Chad Versacebf8ad172011-11-10 10:19:20 -08002908 dst_att->Zoffset = src_att->Zoffset;
Dave Airlie35859d52016-02-29 17:16:10 +10002909 dst_att->Layered = src_att->Layered;
Chad Versacebf8ad172011-11-10 10:19:20 -08002910}
Brian Paulddc82ee2005-02-05 19:56:45 +00002911
Brian Paul45bd5c42011-12-16 08:44:43 -07002912
Timothy Arceri304058a2017-05-05 16:25:11 +10002913static struct gl_texture_object *
2914get_texture_for_framebuffer(struct gl_context *ctx, GLuint texture)
2915{
2916 if (!texture)
2917 return NULL;
2918
2919 return _mesa_lookup_texture(ctx, texture);
2920}
2921
2922
Brian Paulddc82ee2005-02-05 19:56:45 +00002923/**
Laura Ekstranda245e3b2015-01-27 16:11:52 -08002924 * Common code called by gl*FramebufferTexture*() to retrieve the correct
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002925 * texture object pointer.
Laura Ekstranda245e3b2015-01-27 16:11:52 -08002926 *
2927 * \param texObj where the pointer to the texture object is returned. Note
2928 * that a successful call may return texObj = NULL.
2929 *
2930 * \return true if no errors, false if errors
Brian Paulddc82ee2005-02-05 19:56:45 +00002931 */
Laura Ekstranda245e3b2015-01-27 16:11:52 -08002932static bool
Timothy Arceri304058a2017-05-05 16:25:11 +10002933get_texture_for_framebuffer_err(struct gl_context *ctx, GLuint texture,
2934 bool layered, const char *caller,
2935 struct gl_texture_object **texObj)
Brian Paulddc82ee2005-02-05 19:56:45 +00002936{
Laura Ekstranda245e3b2015-01-27 16:11:52 -08002937 *texObj = NULL; /* This will get returned if texture = 0. */
Brian Paulea4fe662006-03-26 05:22:17 +00002938
Laura Ekstranda9f73f72015-03-02 13:41:13 -08002939 if (!texture)
2940 return true;
Ian Romanickb0fe0d82007-05-15 13:42:25 -07002941
Laura Ekstranda9f73f72015-03-02 13:41:13 -08002942 *texObj = _mesa_lookup_texture(ctx, texture);
2943 if (*texObj == NULL || (*texObj)->Target == 0) {
2944 /* Can't render to a non-existent texture object.
2945 *
2946 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2947 * Managing Framebuffer Objects specifies a different error
2948 * depending upon the calling function (PDF pages 325-328).
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002949 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
Laura Ekstranda9f73f72015-03-02 13:41:13 -08002950 * value, while the other commands throw invalid operation (where
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002951 * layered = GL_FALSE).
Laura Ekstranda9f73f72015-03-02 13:41:13 -08002952 */
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002953 const GLenum error = layered ? GL_INVALID_VALUE :
Laura Ekstranda9f73f72015-03-02 13:41:13 -08002954 GL_INVALID_OPERATION;
2955 _mesa_error(ctx, error,
2956 "%s(non-existent texture %u)", caller, texture);
2957 return false;
2958 }
2959
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002960 return true;
2961}
2962
2963
2964/**
2965 * Common code called by gl*FramebufferTexture() to verify the texture target
2966 * and decide whether or not the attachment should truly be considered
2967 * layered.
2968 *
2969 * \param layered true if attachment should be considered layered, false if
2970 * not
2971 *
2972 * \return true if no errors, false if errors
2973 */
2974static bool
2975check_layered_texture_target(struct gl_context *ctx, GLenum target,
2976 const char *caller, GLboolean *layered)
2977{
2978 *layered = GL_TRUE;
2979
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08002980 switch (target) {
2981 case GL_TEXTURE_3D:
2982 case GL_TEXTURE_1D_ARRAY_EXT:
2983 case GL_TEXTURE_2D_ARRAY_EXT:
2984 case GL_TEXTURE_CUBE_MAP:
2985 case GL_TEXTURE_CUBE_MAP_ARRAY:
2986 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2987 return true;
2988 case GL_TEXTURE_1D:
2989 case GL_TEXTURE_2D:
2990 case GL_TEXTURE_RECTANGLE:
2991 case GL_TEXTURE_2D_MULTISAMPLE:
2992 /* These texture types are valid to pass to
2993 * glFramebufferTexture(), but since they aren't layered, it
2994 * is equivalent to calling glFramebufferTexture{1D,2D}().
2995 */
2996 *layered = GL_FALSE;
2997 return true;
2998 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08002999
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003000 _mesa_error(ctx, GL_INVALID_OPERATION,
3001 "%s(invalid texture target %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003002 _mesa_enum_to_string(target));
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003003 return false;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003004}
3005
3006
3007/**
3008 * Common code called by gl*FramebufferTextureLayer() to verify the texture
3009 * target.
3010 *
3011 * \return true if no errors, false if errors
3012 */
3013static bool
3014check_texture_target(struct gl_context *ctx, GLenum target,
3015 const char *caller)
3016{
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003017 /* We're being called by glFramebufferTextureLayer().
3018 * The only legal texture types for that function are 3D,
3019 * cube-map, and 1D/2D/cube-map array textures.
Ian Romanick832ea232015-05-20 17:19:29 -07003020 *
3021 * We don't need to check for GL_ARB_texture_cube_map_array because the
3022 * application wouldn't have been able to create a texture with a
3023 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003024 */
3025 switch (target) {
3026 case GL_TEXTURE_3D:
3027 case GL_TEXTURE_1D_ARRAY:
3028 case GL_TEXTURE_2D_ARRAY:
3029 case GL_TEXTURE_CUBE_MAP_ARRAY:
3030 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3031 return true;
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003032 case GL_TEXTURE_CUBE_MAP:
Ian Romanick832ea232015-05-20 17:19:29 -07003033 /* We don't need to check the extension (GL_ARB_direct_state_access) or
3034 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
3035 * enabled in core profile. This can be called from
3036 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
3037 * so we do have to check the profile.
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003038 */
Ian Romanick832ea232015-05-20 17:19:29 -07003039 return ctx->API == API_OPENGL_CORE;
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003040 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003041
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003042 _mesa_error(ctx, GL_INVALID_OPERATION,
3043 "%s(invalid texture target %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003044 _mesa_enum_to_string(target));
Laura Ekstrand80e9bf22015-03-02 16:52:55 -08003045 return false;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003046}
3047
3048
3049/**
3050 * Common code called by glFramebufferTexture*D() to verify the texture
3051 * target.
3052 *
3053 * \return true if no errors, false if errors
3054 */
3055static bool
3056check_textarget(struct gl_context *ctx, int dims, GLenum target,
3057 GLenum textarget, const char *caller)
3058{
3059 bool err = false;
3060
Kenneth Graunkeaecdb212016-10-03 16:37:25 -07003061 switch (textarget) {
3062 case GL_TEXTURE_1D:
3063 err = dims != 1;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003064 break;
Kenneth Graunkeaecdb212016-10-03 16:37:25 -07003065 case GL_TEXTURE_1D_ARRAY:
3066 err = dims != 1 || !ctx->Extensions.EXT_texture_array;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003067 break;
Kenneth Graunkeaecdb212016-10-03 16:37:25 -07003068 case GL_TEXTURE_2D:
3069 err = dims != 2;
3070 break;
3071 case GL_TEXTURE_2D_ARRAY:
3072 err = dims != 2 || !ctx->Extensions.EXT_texture_array ||
3073 (_mesa_is_gles(ctx) && ctx->Version < 30);
3074 break;
3075 case GL_TEXTURE_2D_MULTISAMPLE:
3076 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3077 err = dims != 2 ||
3078 !ctx->Extensions.ARB_texture_multisample ||
3079 (_mesa_is_gles(ctx) && ctx->Version < 31);
3080 break;
3081 case GL_TEXTURE_RECTANGLE:
3082 err = dims != 2 || _mesa_is_gles(ctx) ||
3083 !ctx->Extensions.NV_texture_rectangle;
3084 break;
3085 case GL_TEXTURE_CUBE_MAP:
3086 case GL_TEXTURE_CUBE_MAP_ARRAY:
3087 err = true;
3088 break;
3089 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3090 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3091 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3092 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3093 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3094 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3095 err = dims != 2 || !ctx->Extensions.ARB_texture_cube_map;
3096 break;
3097 case GL_TEXTURE_3D:
3098 err = dims != 3;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003099 break;
3100 default:
Kenneth Graunkea40640f2016-10-03 16:37:26 -07003101 _mesa_error(ctx, GL_INVALID_ENUM,
3102 "%s(unknown textarget 0x%x)", caller, textarget);
3103 return false;
Laura Ekstranda602b212015-03-02 13:43:09 -08003104 }
Ian Romanickb0fe0d82007-05-15 13:42:25 -07003105
Laura Ekstranda602b212015-03-02 13:43:09 -08003106 if (err) {
3107 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003108 "%s(invalid textarget %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003109 caller, _mesa_enum_to_string(textarget));
Laura Ekstranda602b212015-03-02 13:43:09 -08003110 return false;
3111 }
3112
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003113 /* Make sure textarget is consistent with the texture's type */
3114 err = (target == GL_TEXTURE_CUBE_MAP) ?
3115 !_mesa_is_cube_face(textarget): (target != textarget);
3116
3117 if (err) {
3118 _mesa_error(ctx, GL_INVALID_OPERATION,
3119 "%s(mismatched texture target)", caller);
3120 return false;
3121 }
3122
3123 return true;
3124}
3125
3126
3127/**
3128 * Common code called by gl*FramebufferTextureLayer() and
3129 * glFramebufferTexture3D() to validate the layer.
3130 *
3131 * \return true if no errors, false if errors
3132 */
3133static bool
3134check_layer(struct gl_context *ctx, GLenum target, GLint layer,
3135 const char *caller)
3136{
Laura Ekstranda602b212015-03-02 13:43:09 -08003137 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3138 * spec says:
3139 *
3140 * "An INVALID_VALUE error is generated if texture is non-zero
3141 * and layer is negative."
3142 */
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003143 if (layer < 0) {
Laura Ekstranda602b212015-03-02 13:43:09 -08003144 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003145 "%s(layer %u < 0)", caller, layer);
Laura Ekstranda602b212015-03-02 13:43:09 -08003146 return false;
3147 }
3148
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003149 if (target == GL_TEXTURE_3D) {
Laura Ekstranda602b212015-03-02 13:43:09 -08003150 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003151 if (layer >= maxSize) {
Fredrik Höglund8ba7ad82015-05-09 15:31:45 +02003152 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003153 "%s(invalid layer %u)", caller, layer);
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003154 return false;
Fredrik Höglund8ba7ad82015-05-09 15:31:45 +02003155 }
Laura Ekstranda602b212015-03-02 13:43:09 -08003156 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003157 else if ((target == GL_TEXTURE_1D_ARRAY) ||
3158 (target == GL_TEXTURE_2D_ARRAY) ||
3159 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
3160 (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
3161 if (layer >= ctx->Const.MaxArrayTextureLayers) {
Ian Romanickb0fe0d82007-05-15 13:42:25 -07003162 _mesa_error(ctx, GL_INVALID_VALUE,
Laura Ekstranda602b212015-03-02 13:43:09 -08003163 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003164 caller, layer);
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003165 return false;
Ian Romanickb0fe0d82007-05-15 13:42:25 -07003166 }
Laura Ekstranda602b212015-03-02 13:43:09 -08003167 }
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003168 else if (target == GL_TEXTURE_CUBE_MAP) {
3169 if (layer >= 6) {
3170 _mesa_error(ctx, GL_INVALID_VALUE,
3171 "%s(layer %u >= 6)", caller, layer);
3172 return false;
3173 }
3174 }
Laura Ekstranda602b212015-03-02 13:43:09 -08003175
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003176 return true;
3177}
3178
3179
3180/**
3181 * Common code called by all gl*FramebufferTexture*() entry points to verify
3182 * the level.
3183 *
3184 * \return true if no errors, false if errors
3185 */
3186static bool
3187check_level(struct gl_context *ctx, GLenum target, GLint level,
3188 const char *caller)
3189{
Laura Ekstranda602b212015-03-02 13:43:09 -08003190 if ((level < 0) ||
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003191 (level >= _mesa_max_texture_levels(ctx, target))) {
Laura Ekstranda602b212015-03-02 13:43:09 -08003192 _mesa_error(ctx, GL_INVALID_VALUE,
3193 "%s(invalid level %d)", caller, level);
3194 return false;
3195 }
Brian Paulea4fe662006-03-26 05:22:17 +00003196
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003197 return true;
3198}
3199
3200
Timothy Arcerid90ced42017-05-05 16:46:03 +10003201struct gl_renderbuffer_attachment *
3202_mesa_get_and_validate_attachment(struct gl_context *ctx,
3203 struct gl_framebuffer *fb,
3204 GLenum attachment, const char *caller)
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003205{
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003206 /* The window-system framebuffer object is immutable */
3207 if (_mesa_is_winsys_fbo(fb)) {
3208 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3209 caller);
Timothy Arcerid90ced42017-05-05 16:46:03 +10003210 return NULL;
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003211 }
3212
3213 /* Not a hash lookup, so we can afford to get the attachment here. */
Timothy Arcerid90ced42017-05-05 16:46:03 +10003214 bool is_color_attachment;
3215 struct gl_renderbuffer_attachment *att =
3216 get_attachment(ctx, fb, attachment, &is_color_attachment);
Brian Paul3deaa012005-02-07 05:08:24 +00003217 if (att == NULL) {
Ilia Mirkin62b8f492017-01-24 00:26:29 -05003218 if (is_color_attachment) {
3219 _mesa_error(ctx, GL_INVALID_OPERATION,
3220 "%s(invalid color attachment %s)", caller,
3221 _mesa_enum_to_string(attachment));
3222 } else {
3223 _mesa_error(ctx, GL_INVALID_ENUM,
3224 "%s(invalid attachment %s)", caller,
3225 _mesa_enum_to_string(attachment));
3226 }
Timothy Arcerid90ced42017-05-05 16:46:03 +10003227 return NULL;
Brian Paul3deaa012005-02-07 05:08:24 +00003228 }
3229
Timothy Arcerid90ced42017-05-05 16:46:03 +10003230 return att;
3231}
3232
3233
3234void
3235_mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3236 GLenum attachment,
Timothy Arceri69ca1ef2017-05-05 17:00:34 +10003237 struct gl_renderbuffer_attachment *att,
Timothy Arcerid90ced42017-05-05 16:46:03 +10003238 struct gl_texture_object *texObj, GLenum textarget,
Timothy Arceri69ca1ef2017-05-05 17:00:34 +10003239 GLint level, GLuint layer, GLboolean layered)
Timothy Arcerid90ced42017-05-05 16:46:03 +10003240{
Brian Paul800e5532009-11-02 15:39:39 -07003241 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
Brian Paul474f28e2005-10-08 14:41:17 +00003242
Brian Pauld129ea72014-03-01 10:21:07 -07003243 mtx_lock(&fb->Mutex);
Brian Paulea4fe662006-03-26 05:22:17 +00003244 if (texObj) {
Chad Versacebf8ad172011-11-10 10:19:20 -08003245 if (attachment == GL_DEPTH_ATTACHMENT &&
Paul Berryb9819a02012-04-13 21:50:08 -07003246 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3247 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3248 _mesa_tex_target_to_face(textarget) ==
3249 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003250 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003251 /* The texture object is already attached to the stencil attachment
3252 * point. Don't create a new renderbuffer; just reuse the stencil
3253 * attachment's. This is required to prevent a GL error in
3254 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3255 */
3256 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3257 BUFFER_STENCIL);
Chad Versacebf8ad172011-11-10 10:19:20 -08003258 } else if (attachment == GL_STENCIL_ATTACHMENT &&
Martin Peres7bd8b482015-02-12 17:54:43 +02003259 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
Paul Berryb9819a02012-04-13 21:50:08 -07003260 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3261 _mesa_tex_target_to_face(textarget) ==
3262 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003263 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003264 /* As above, but with depth and stencil transposed. */
3265 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3266 BUFFER_DEPTH);
Chad Versacebf8ad172011-11-10 10:19:20 -08003267 } else {
Martin Peres7bd8b482015-02-12 17:54:43 +02003268 set_texture_attachment(ctx, fb, att, texObj, textarget,
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003269 level, layer, layered);
Laura Ekstrand8f78c682015-01-27 14:11:13 -08003270
Martin Peres7bd8b482015-02-12 17:54:43 +02003271 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3272 /* Above we created a new renderbuffer and attached it to the
3273 * depth attachment point. Now attach it to the stencil attachment
3274 * point too.
3275 */
3276 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3277 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3278 BUFFER_DEPTH);
3279 }
Chad Versacebf8ad172011-11-10 10:19:20 -08003280 }
3281
Brian Paul2897cee2009-01-29 09:20:18 -07003282 /* Set the render-to-texture flag. We'll check this flag in
3283 * glTexImage() and friends to determine if we need to revalidate
3284 * any FBOs that might be rendering into this texture.
3285 * This flag never gets cleared since it's non-trivial to determine
3286 * when all FBOs might be done rendering to this texture. That's OK
3287 * though since it's uncommon to render to a texture then repeatedly
3288 * call glTexImage() to change images in the texture.
3289 */
3290 texObj->_RenderToTexture = GL_TRUE;
Brian Paul3deaa012005-02-07 05:08:24 +00003291 }
3292 else {
Brian Paul94512812014-02-01 08:58:43 -07003293 remove_attachment(ctx, att);
Chad Versacebf8ad172011-11-10 10:19:20 -08003294 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003295 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3296 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
Chad Versacebf8ad172011-11-10 10:19:20 -08003297 }
Brian Paul3deaa012005-02-07 05:08:24 +00003298 }
Brian Paul72966362009-01-21 16:28:38 -07003299
3300 invalidate_framebuffer(fb);
3301
Brian Pauld129ea72014-03-01 10:21:07 -07003302 mtx_unlock(&fb->Mutex);
Brian Paulea4fe662006-03-26 05:22:17 +00003303}
3304
3305
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003306static void
Timothy Arceri20cabc22017-05-08 10:59:15 +10003307framebuffer_texture_with_dims_no_error(GLenum target, GLenum attachment,
3308 GLenum textarget, GLuint texture,
3309 GLint level, GLint layer)
3310{
3311 GET_CURRENT_CONTEXT(ctx);
3312
3313 /* Get the framebuffer object */
3314 struct gl_framebuffer *fb = get_framebuffer_target(ctx, target);
3315
3316 /* Get the texture object */
3317 struct gl_texture_object *texObj =
3318 get_texture_for_framebuffer(ctx, texture);
3319
3320 struct gl_renderbuffer_attachment *att =
3321 get_attachment(ctx, fb, attachment, NULL);
3322
3323 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
3324 level, layer, GL_FALSE);
3325}
3326
3327
3328static void
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003329framebuffer_texture_with_dims(int dims, GLenum target,
3330 GLenum attachment, GLenum textarget,
3331 GLuint texture, GLint level, GLint layer,
3332 const char *caller)
Brian Paulea4fe662006-03-26 05:22:17 +00003333{
Ian Romanickb0fe0d82007-05-15 13:42:25 -07003334 GET_CURRENT_CONTEXT(ctx);
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003335 struct gl_framebuffer *fb;
3336 struct gl_texture_object *texObj;
Ian Romanickb0fe0d82007-05-15 13:42:25 -07003337
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003338 /* Get the framebuffer object */
3339 fb = get_framebuffer_target(ctx, target);
3340 if (!fb) {
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003341 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003342 _mesa_enum_to_string(target));
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003343 return;
3344 }
3345
3346 /* Get the texture object */
Timothy Arceri304058a2017-05-05 16:25:11 +10003347 if (!get_texture_for_framebuffer_err(ctx, texture, false, caller, &texObj))
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003348 return;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003349
3350 if (texObj) {
3351 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3352 return;
3353
3354 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3355 return;
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003356
Marek Olšák04a78062016-05-27 21:40:19 +02003357 if (!check_level(ctx, textarget, level, caller))
3358 return;
3359 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003360
Timothy Arceri69ca1ef2017-05-05 17:00:34 +10003361 struct gl_renderbuffer_attachment *att =
3362 _mesa_get_and_validate_attachment(ctx, fb, attachment, caller);
3363 if (!att)
3364 return;
3365
3366 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
3367 level, layer, GL_FALSE);
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003368}
3369
3370
3371void GLAPIENTRY
Timothy Arceri276166c2017-05-08 11:10:58 +10003372_mesa_FramebufferTexture1D_no_error(GLenum target, GLenum attachment,
3373 GLenum textarget, GLuint texture,
3374 GLint level)
3375{
3376 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3377 texture, level, 0);
3378}
3379
3380
3381void GLAPIENTRY
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003382_mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3383 GLenum textarget, GLuint texture, GLint level)
3384{
3385 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3386 level, 0, "glFramebufferTexture1D");
Brian Paulddc82ee2005-02-05 19:56:45 +00003387}
3388
3389
Brian Paul1864c7d2005-02-08 03:46:37 +00003390void GLAPIENTRY
Timothy Arceri276166c2017-05-08 11:10:58 +10003391_mesa_FramebufferTexture2D_no_error(GLenum target, GLenum attachment,
3392 GLenum textarget, GLuint texture,
3393 GLint level)
3394{
3395 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3396 texture, level, 0);
3397}
3398
3399
3400void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08003401_mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
Brian Paulc1377ed2014-03-22 10:31:58 -06003402 GLenum textarget, GLuint texture, GLint level)
Brian Paulddc82ee2005-02-05 19:56:45 +00003403{
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003404 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3405 level, 0, "glFramebufferTexture2D");
Brian Paulddc82ee2005-02-05 19:56:45 +00003406}
3407
3408
Brian Paul1864c7d2005-02-08 03:46:37 +00003409void GLAPIENTRY
Timothy Arceri276166c2017-05-08 11:10:58 +10003410_mesa_FramebufferTexture3D_no_error(GLenum target, GLenum attachment,
3411 GLenum textarget, GLuint texture,
3412 GLint level, GLint layer)
3413{
3414 framebuffer_texture_with_dims_no_error(target, attachment, textarget,
3415 texture, level, layer);
3416}
3417
3418
3419void GLAPIENTRY
Paul Berry1a1db172012-11-06 08:57:59 -08003420_mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
Brian Paulc1377ed2014-03-22 10:31:58 -06003421 GLenum textarget, GLuint texture,
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003422 GLint level, GLint layer)
Brian Paulddc82ee2005-02-05 19:56:45 +00003423{
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003424 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3425 level, layer, "glFramebufferTexture3D");
Brian Paulddc82ee2005-02-05 19:56:45 +00003426}
3427
3428
Timothy Arceri01081c62017-05-10 11:22:47 +10003429static ALWAYS_INLINE void
Timothy Arcerif6229282017-05-10 11:44:54 +10003430frame_buffer_texture(GLuint framebuffer, GLenum target,
3431 GLenum attachment, GLuint texture,
3432 GLint level, GLint layer, const char *func,
3433 bool dsa, bool no_error, bool check_layered)
Ian Romanickbb372f12007-05-16 15:34:22 -07003434{
3435 GET_CURRENT_CONTEXT(ctx);
Timothy Arcerif6229282017-05-10 11:44:54 +10003436 GLboolean layered = GL_FALSE;
3437
3438 if (!no_error && check_layered) {
3439 if (!_mesa_has_geometry_shaders(ctx)) {
3440 _mesa_error(ctx, GL_INVALID_OPERATION,
3441 "unsupported function (%s) called", func);
3442 return;
3443 }
3444 }
Ian Romanickbb372f12007-05-16 15:34:22 -07003445
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003446 /* Get the framebuffer object */
Timothy Arceri01081c62017-05-10 11:22:47 +10003447 struct gl_framebuffer *fb;
Timothy Arcerif6198e92017-05-11 15:43:44 +10003448 if (no_error) {
3449 if (dsa) {
3450 fb = _mesa_lookup_framebuffer(ctx, framebuffer);
3451 } else {
3452 fb = get_framebuffer_target(ctx, target);
3453 }
Timothy Arceri01081c62017-05-10 11:22:47 +10003454 } else {
Timothy Arcerif6198e92017-05-11 15:43:44 +10003455 if (dsa) {
3456 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3457 if (!fb)
3458 return;
3459 } else {
3460 fb = get_framebuffer_target(ctx, target);
3461 if (!fb) {
3462 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)",
3463 func, _mesa_enum_to_string(target));
3464 return;
3465 }
Timothy Arceri01081c62017-05-10 11:22:47 +10003466 }
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003467 }
3468
Timothy Arcerif6198e92017-05-11 15:43:44 +10003469 /* Get the texture object and framebuffer attachment*/
3470 struct gl_renderbuffer_attachment *att;
Timothy Arceri01081c62017-05-10 11:22:47 +10003471 struct gl_texture_object *texObj;
Timothy Arcerif6198e92017-05-11 15:43:44 +10003472 if (no_error) {
3473 texObj = get_texture_for_framebuffer(ctx, texture);
3474 att = get_attachment(ctx, fb, attachment, NULL);
3475 } else {
Timothy Arcerif6229282017-05-10 11:44:54 +10003476 if (!get_texture_for_framebuffer_err(ctx, texture, check_layered, func,
Timothy Arcerif6198e92017-05-11 15:43:44 +10003477 &texObj))
3478 return;
3479
3480 att = _mesa_get_and_validate_attachment(ctx, fb, attachment, func);
3481 if (!att)
3482 return;
3483 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003484
Timothy Arceri01081c62017-05-10 11:22:47 +10003485 GLenum textarget = 0;
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003486 if (texObj) {
Timothy Arcerif6229282017-05-10 11:44:54 +10003487 if (check_layered) {
3488 /* We do this regardless of no_error because this sets layered */
3489 if (!check_layered_texture_target(ctx, texObj->Target, func,
3490 &layered))
Timothy Arcerif6198e92017-05-11 15:43:44 +10003491 return;
Timothy Arcerif6229282017-05-10 11:44:54 +10003492 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003493
Timothy Arcerif6229282017-05-10 11:44:54 +10003494 if (!no_error) {
3495 if (!check_layered) {
3496 if (!check_texture_target(ctx, texObj->Target, func))
3497 return;
3498
3499 if (!check_layer(ctx, texObj->Target, layer, func))
3500 return;
3501 }
Laura Ekstrand085c67d2015-03-02 16:48:59 -08003502
Timothy Arcerif6198e92017-05-11 15:43:44 +10003503 if (!check_level(ctx, texObj->Target, level, func))
3504 return;
3505 }
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003506
Timothy Arcerif6229282017-05-10 11:44:54 +10003507 if (!check_layered && texObj->Target == GL_TEXTURE_CUBE_MAP) {
Fredrik Höglund03420ea2015-04-29 19:44:06 +02003508 assert(layer >= 0 && layer < 6);
3509 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3510 layer = 0;
3511 }
Laura Ekstranda245e3b2015-01-27 16:11:52 -08003512 }
3513
Timothy Arceri69ca1ef2017-05-05 17:00:34 +10003514 _mesa_framebuffer_texture(ctx, fb, attachment, att, texObj, textarget,
Timothy Arcerif6229282017-05-10 11:44:54 +10003515 level, layer, layered);
Ian Romanickbb372f12007-05-16 15:34:22 -07003516}
3517
Timothy Arcerie75e8d62017-05-08 11:24:07 +10003518void GLAPIENTRY
3519_mesa_FramebufferTextureLayer_no_error(GLenum target, GLenum attachment,
3520 GLuint texture, GLint level,
3521 GLint layer)
3522{
Timothy Arcerif6229282017-05-10 11:44:54 +10003523 frame_buffer_texture(0, target, attachment, texture, level, layer,
3524 "glFramebufferTextureLayer", false, true, false);
Timothy Arcerie75e8d62017-05-08 11:24:07 +10003525}
3526
Ian Romanickbb372f12007-05-16 15:34:22 -07003527
3528void GLAPIENTRY
Timothy Arceri01081c62017-05-10 11:22:47 +10003529_mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3530 GLuint texture, GLint level, GLint layer)
3531{
Timothy Arcerif6229282017-05-10 11:44:54 +10003532 frame_buffer_texture(0, target, attachment, texture, level, layer,
3533 "glFramebufferTextureLayer", false, false, false);
Timothy Arceri01081c62017-05-10 11:22:47 +10003534}
3535
3536
3537void GLAPIENTRY
Timothy Arceri4e125c42017-05-08 11:37:33 +10003538_mesa_NamedFramebufferTextureLayer_no_error(GLuint framebuffer,
3539 GLenum attachment,
3540 GLuint texture, GLint level,
3541 GLint layer)
3542{
Timothy Arcerif6229282017-05-10 11:44:54 +10003543 frame_buffer_texture(framebuffer, 0, attachment, texture, level, layer,
3544 "glNamedFramebufferTextureLayer", true, true, false);
Timothy Arceri4e125c42017-05-08 11:37:33 +10003545}
3546
3547
3548void GLAPIENTRY
Laura Ekstrandd78c8312015-01-28 13:19:57 -08003549_mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3550 GLuint texture, GLint level, GLint layer)
3551{
Timothy Arcerif6229282017-05-10 11:44:54 +10003552 frame_buffer_texture(framebuffer, 0, attachment, texture, level, layer,
3553 "glNamedFramebufferTextureLayer", true, false, false);
Laura Ekstrandd78c8312015-01-28 13:19:57 -08003554}
3555
3556
3557void GLAPIENTRY
Jordan Justen02f2bce2013-04-18 10:46:12 -07003558_mesa_FramebufferTexture(GLenum target, GLenum attachment,
3559 GLuint texture, GLint level)
3560{
Timothy Arcerif6229282017-05-10 11:44:54 +10003561 frame_buffer_texture(0, target, attachment, texture, level, 0,
3562 "glFramebufferTexture", false, false, true);
Jordan Justen02f2bce2013-04-18 10:46:12 -07003563}
3564
3565
Laura Ekstrandd78c8312015-01-28 13:19:57 -08003566void GLAPIENTRY
3567_mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3568 GLuint texture, GLint level)
3569{
Timothy Arcerif6229282017-05-10 11:44:54 +10003570 frame_buffer_texture(framebuffer, 0, attachment, texture, level, 0,
3571 "glNamedFramebufferTexture", true, false, true);
Laura Ekstrandd78c8312015-01-28 13:19:57 -08003572}
3573
3574
Laura Ekstranda29318b2015-02-27 17:27:30 -08003575void
3576_mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3577 struct gl_framebuffer *fb,
3578 GLenum attachment,
Ian Romanick9ae42ab2015-11-11 16:30:41 -08003579 struct gl_renderbuffer *rb)
3580{
3581 assert(!_mesa_is_winsys_fbo(fb));
3582
3583 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3584
3585 assert(ctx->Driver.FramebufferRenderbuffer);
3586 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3587
3588 /* Some subsequent GL commands may depend on the framebuffer's visual
3589 * after the binding is updated. Update visual info now.
3590 */
3591 _mesa_update_framebuffer_visual(ctx, fb);
3592}
3593
3594static void
3595framebuffer_renderbuffer(struct gl_context *ctx,
3596 struct gl_framebuffer *fb,
3597 GLenum attachment,
3598 struct gl_renderbuffer *rb,
3599 const char *func)
Brian Paulddc82ee2005-02-05 19:56:45 +00003600{
Brian Paul2c6f9112005-02-24 05:47:06 +00003601 struct gl_renderbuffer_attachment *att;
Alejandro Piñeiro84e3e122017-01-12 16:09:17 -02003602 bool is_color_attachment;
Brian Paul3deaa012005-02-07 05:08:24 +00003603
Brian Paul36ede892012-01-12 09:17:23 -07003604 if (_mesa_is_winsys_fbo(fb)) {
Brian Paulab8ef282005-09-07 23:21:59 +00003605 /* Can't attach new renderbuffers to a window system framebuffer */
Laura Ekstranda29318b2015-02-27 17:27:30 -08003606 _mesa_error(ctx, GL_INVALID_OPERATION,
3607 "%s(window-system framebuffer)", func);
Brian Paul3deaa012005-02-07 05:08:24 +00003608 return;
3609 }
3610
Alejandro Piñeiro84e3e122017-01-12 16:09:17 -02003611 att = get_attachment(ctx, fb, attachment, &is_color_attachment);
Brian Paul3deaa012005-02-07 05:08:24 +00003612 if (att == NULL) {
Alejandro Piñeiro84e3e122017-01-12 16:09:17 -02003613 /*
3614 * From OpenGL 4.5 spec, section 9.2.7 "Attaching Renderbuffer Images to
3615 * a Framebuffer":
3616 *
3617 * "An INVALID_OPERATION error is generated if attachment is COLOR_-
3618 * ATTACHMENTm where m is greater than or equal to the value of
3619 * MAX_COLOR_- ATTACHMENTS ."
3620 *
3621 * If we are at this point, is because the attachment is not valid, so
3622 * if is_color_attachment is true, is because of the previous reason.
3623 */
3624 if (is_color_attachment) {
3625 _mesa_error(ctx, GL_INVALID_OPERATION,
3626 "%s(invalid color attachment %s)", func,
3627 _mesa_enum_to_string(attachment));
3628 } else {
3629 _mesa_error(ctx, GL_INVALID_ENUM,
3630 "%s(invalid attachment %s)", func,
3631 _mesa_enum_to_string(attachment));
3632 }
3633
Brian Paulddc82ee2005-02-05 19:56:45 +00003634 return;
3635 }
3636
Brian Paula504f232010-05-27 13:05:23 -06003637 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3638 rb && rb->Format != MESA_FORMAT_NONE) {
Brian Paul30590072009-01-21 11:06:11 -07003639 /* make sure the renderbuffer is a depth/stencil format */
Brian Paula504f232010-05-27 13:05:23 -06003640 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
Brian Paul45e76d22009-10-08 20:27:27 -06003641 if (baseFormat != GL_DEPTH_STENCIL) {
Brian Paul30590072009-01-21 11:06:11 -07003642 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstranda29318b2015-02-27 17:27:30 -08003643 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
Brian Paul30590072009-01-21 11:06:11 -07003644 return;
3645 }
3646 }
3647
Ian Romanick9ae42ab2015-11-11 16:30:41 -08003648 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
Brian Paulddc82ee2005-02-05 19:56:45 +00003649}
3650
Brian Paul1864c7d2005-02-08 03:46:37 +00003651void GLAPIENTRY
Laura Ekstranda29318b2015-02-27 17:27:30 -08003652_mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3653 GLenum renderbuffertarget,
3654 GLuint renderbuffer)
3655{
3656 struct gl_framebuffer *fb;
3657 struct gl_renderbuffer *rb;
3658 GET_CURRENT_CONTEXT(ctx);
3659
3660 fb = get_framebuffer_target(ctx, target);
3661 if (!fb) {
3662 _mesa_error(ctx, GL_INVALID_ENUM,
3663 "glFramebufferRenderbuffer(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003664 _mesa_enum_to_string(target));
Laura Ekstranda29318b2015-02-27 17:27:30 -08003665 return;
3666 }
3667
3668 if (renderbuffertarget != GL_RENDERBUFFER) {
3669 _mesa_error(ctx, GL_INVALID_ENUM,
3670 "glFramebufferRenderbuffer(renderbuffertarget is not "
3671 "GL_RENDERBUFFER)");
3672 return;
3673 }
3674
3675 if (renderbuffer) {
3676 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3677 "glFramebufferRenderbuffer");
3678 if (!rb)
3679 return;
3680 }
3681 else {
3682 /* remove renderbuffer attachment */
3683 rb = NULL;
3684 }
3685
Ian Romanick9ae42ab2015-11-11 16:30:41 -08003686 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3687 "glFramebufferRenderbuffer");
Laura Ekstranda29318b2015-02-27 17:27:30 -08003688}
3689
3690
3691void GLAPIENTRY
3692_mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3693 GLenum renderbuffertarget,
3694 GLuint renderbuffer)
3695{
3696 struct gl_framebuffer *fb;
3697 struct gl_renderbuffer *rb;
3698 GET_CURRENT_CONTEXT(ctx);
3699
3700 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3701 "glNamedFramebufferRenderbuffer");
Fredrik Höglund5a55f682015-05-16 19:43:39 +02003702 if (!fb)
3703 return;
Laura Ekstranda29318b2015-02-27 17:27:30 -08003704
3705 if (renderbuffertarget != GL_RENDERBUFFER) {
3706 _mesa_error(ctx, GL_INVALID_ENUM,
3707 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3708 "GL_RENDERBUFFER)");
3709 return;
3710 }
3711
3712 if (renderbuffer) {
3713 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3714 "glNamedFramebufferRenderbuffer");
3715 if (!rb)
3716 return;
3717 }
3718 else {
3719 /* remove renderbuffer attachment */
3720 rb = NULL;
3721 }
3722
Ian Romanick9ae42ab2015-11-11 16:30:41 -08003723 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3724 "glNamedFramebufferRenderbuffer");
Laura Ekstranda29318b2015-02-27 17:27:30 -08003725}
3726
3727
Timothy Arceri8b006302017-05-05 15:21:22 +10003728static void
3729get_framebuffer_attachment_parameter(struct gl_context *ctx,
3730 struct gl_framebuffer *buffer,
3731 GLenum attachment, GLenum pname,
3732 GLint *params, const char *caller)
Brian Paulddc82ee2005-02-05 19:56:45 +00003733{
Brian Paul2c6f9112005-02-24 05:47:06 +00003734 const struct gl_renderbuffer_attachment *att;
Robert Foss88becf72017-03-01 19:14:39 -05003735 bool is_color_attachment = false;
Marek Olšák000896c2011-07-19 03:05:07 +02003736 GLenum err;
Brian Paulddc82ee2005-02-05 19:56:45 +00003737
Kenneth Graunke19f13b22016-03-07 16:43:35 -08003738 /* The error code for an attachment type of GL_NONE differs between APIs.
3739 *
3740 * From the ES 2.0.25 specification, page 127:
3741 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3742 * querying any other pname will generate INVALID_ENUM."
3743 *
3744 * From the OpenGL 3.0 specification, page 337, or identically,
3745 * the OpenGL ES 3.0.4 specification, page 240:
3746 *
3747 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3748 * framebuffer is bound to target. In this case querying pname
3749 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3750 * queries will generate an INVALID_OPERATION error."
3751 */
3752 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3753 GL_INVALID_ENUM : GL_INVALID_OPERATION;
Marek Olšák000896c2011-07-19 03:05:07 +02003754
Brian Paul36ede892012-01-12 09:17:23 -07003755 if (_mesa_is_winsys_fbo(buffer)) {
Ian Romanickda2e41c2011-10-03 12:04:09 -07003756 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3757 * says:
3758 *
3759 * "If the framebuffer currently bound to target is zero, then
3760 * INVALID_OPERATION is generated."
3761 *
3762 * The EXT_framebuffer_object spec has the same wording, and the
3763 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3764 * spec.
3765 */
Brian Paulc1377ed2014-03-22 10:31:58 -06003766 if ((!_mesa_is_desktop_gl(ctx) ||
3767 !ctx->Extensions.ARB_framebuffer_object)
Anuj Phogat2f2801f2012-12-11 20:08:13 -08003768 && !_mesa_is_gles3(ctx)) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003769 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003770 "%s(window-system framebuffer)", caller);
Martin Peres7bd8b482015-02-12 17:54:43 +02003771 return;
Ian Romanickda2e41c2011-10-03 12:04:09 -07003772 }
Anuj Phogat2f2801f2012-12-11 20:08:13 -08003773
3774 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3775 attachment != GL_DEPTH && attachment != GL_STENCIL) {
Samuel Iglesias Gonsalvez284bd1e2015-01-16 16:00:13 +01003776 _mesa_error(ctx, GL_INVALID_ENUM,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003777 "%s(invalid attachment %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003778 _mesa_enum_to_string(attachment));
Anuj Phogat2f2801f2012-12-11 20:08:13 -08003779 return;
3780 }
Kenneth Graunke789e0962016-03-08 23:59:37 -08003781
3782 /* The specs are not clear about how to handle
3783 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3784 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3785 * discussed in:
3786 *
3787 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3788 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3789 */
3790 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3791 _mesa_error(ctx, GL_INVALID_ENUM,
3792 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3793 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3794 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3795 return;
3796 }
3797
Brian Paul61ec2052010-06-22 08:37:44 -06003798 /* the default / window-system FBO */
Timothy Arcerie6187612017-05-05 15:02:20 +10003799 att = get_fb0_attachment(ctx, buffer, attachment);
Brian Paul61ec2052010-06-22 08:37:44 -06003800 }
3801 else {
3802 /* user-created framebuffer FBO */
Alejandro Piñeiro84e3e122017-01-12 16:09:17 -02003803 att = get_attachment(ctx, buffer, attachment, &is_color_attachment);
Brian Paul61ec2052010-06-22 08:37:44 -06003804 }
3805
Brian Paul3deaa012005-02-07 05:08:24 +00003806 if (att == NULL) {
Alejandro Piñeiro84e3e122017-01-12 16:09:17 -02003807 /*
3808 * From OpenGL 4.5 spec, section 9.2.3 "Framebuffer Object Queries":
3809 *
3810 * "An INVALID_OPERATION error is generated if a framebuffer object
3811 * is bound to target and attachment is COLOR_ATTACHMENTm where m is
3812 * greater than or equal to the value of MAX_COLOR_ATTACHMENTS."
3813 *
3814 * If we are at this point, is because the attachment is not valid, so
3815 * if is_color_attachment is true, is because of the previous reason.
3816 */
3817 if (is_color_attachment) {
3818 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid color attachment %s)",
3819 caller, _mesa_enum_to_string(attachment));
3820 } else {
3821 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3822 _mesa_enum_to_string(attachment));
3823 }
Brian Paulddc82ee2005-02-05 19:56:45 +00003824 return;
3825 }
3826
Brian Paul30590072009-01-21 11:06:11 -07003827 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
Brian Paule9592742014-04-21 13:24:25 -06003828 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
Anuj Phogatbd1880d2014-03-11 17:04:11 -07003829 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3830 /* This behavior is first specified in OpenGL 4.4 specification.
3831 *
3832 * From the OpenGL 4.4 spec page 275:
3833 * "This query cannot be performed for a combined depth+stencil
3834 * attachment, since it does not have a single format."
3835 */
3836 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003837 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3838 " is invalid for depth+stencil attachment)", caller);
Anuj Phogatbd1880d2014-03-11 17:04:11 -07003839 return;
3840 }
Brian Paul30590072009-01-21 11:06:11 -07003841 /* the depth and stencil attachments must point to the same buffer */
Alejandro Piñeiroc6eb3ae2017-01-12 16:03:00 -02003842 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT, NULL);
3843 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT, NULL);
Brian Paul30590072009-01-21 11:06:11 -07003844 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3845 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003846 "%s(DEPTH/STENCIL attachments differ)", caller);
Brian Paul30590072009-01-21 11:06:11 -07003847 return;
3848 }
3849 }
3850
Brian Paul800e5532009-11-02 15:39:39 -07003851 /* No need to flush here */
Brian Paul474f28e2005-10-08 14:41:17 +00003852
Brian Paulddc82ee2005-02-05 19:56:45 +00003853 switch (pname) {
3854 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
Iago Toral Quirogacf439952015-02-24 19:02:56 +01003855 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3856 *
3857 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3858 * either no framebuffer is bound to target; or the default framebuffer
3859 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3860 * stencil bits, respectively, is zero."
Alejandro Piñeiro90596142017-01-13 11:39:24 -02003861 *
3862 * Note that we don't need explicit checks on DEPTH and STENCIL, because
3863 * on the case the spec is pointing, att->Type is already NONE, so we
3864 * just need to check att->Type.
Iago Toral Quirogacf439952015-02-24 19:02:56 +01003865 */
Alejandro Piñeiro90596142017-01-13 11:39:24 -02003866 *params = (_mesa_is_winsys_fbo(buffer) && att->Type != GL_NONE) ?
3867 GL_FRAMEBUFFER_DEFAULT : att->Type;
Brian Paul3deaa012005-02-07 05:08:24 +00003868 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00003869 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
Brian Paul3deaa012005-02-07 05:08:24 +00003870 if (att->Type == GL_RENDERBUFFER_EXT) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003871 *params = att->Renderbuffer->Name;
Brian Paul3deaa012005-02-07 05:08:24 +00003872 }
3873 else if (att->Type == GL_TEXTURE) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003874 *params = att->Texture->Name;
Brian Paul3deaa012005-02-07 05:08:24 +00003875 }
3876 else {
Brian Paul20cf1852010-12-03 08:23:31 -07003877 assert(att->Type == GL_NONE);
Ian Romanick0cdaa472012-07-27 07:47:28 -07003878 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
Marek Olšák000896c2011-07-19 03:05:07 +02003879 *params = 0;
3880 } else {
Ian Romanick0cdaa472012-07-27 07:47:28 -07003881 goto invalid_pname_enum;
Marek Olšák000896c2011-07-19 03:05:07 +02003882 }
Brian Paul3deaa012005-02-07 05:08:24 +00003883 }
3884 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00003885 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
Brian Paul3deaa012005-02-07 05:08:24 +00003886 if (att->Type == GL_TEXTURE) {
Martin Peres7bd8b482015-02-12 17:54:43 +02003887 *params = att->TextureLevel;
Brian Paul3deaa012005-02-07 05:08:24 +00003888 }
Marek Olšákb9e9df72011-05-31 20:36:07 +02003889 else if (att->Type == GL_NONE) {
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003890 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003891 _mesa_enum_to_string(pname));
Marek Olšákb9e9df72011-05-31 20:36:07 +02003892 }
Brian Paul3deaa012005-02-07 05:08:24 +00003893 else {
Ian Romanick0cdaa472012-07-27 07:47:28 -07003894 goto invalid_pname_enum;
Brian Paul3deaa012005-02-07 05:08:24 +00003895 }
3896 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00003897 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
Brian Paul3deaa012005-02-07 05:08:24 +00003898 if (att->Type == GL_TEXTURE) {
Brian Paulf1e4ca72008-08-06 08:39:54 -06003899 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3900 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3901 }
3902 else {
3903 *params = 0;
3904 }
Brian Paul3deaa012005-02-07 05:08:24 +00003905 }
Marek Olšákb9e9df72011-05-31 20:36:07 +02003906 else if (att->Type == GL_NONE) {
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003907 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003908 _mesa_enum_to_string(pname));
Marek Olšákb9e9df72011-05-31 20:36:07 +02003909 }
Brian Paul3deaa012005-02-07 05:08:24 +00003910 else {
Ian Romanick0cdaa472012-07-27 07:47:28 -07003911 goto invalid_pname_enum;
Brian Paul3deaa012005-02-07 05:08:24 +00003912 }
3913 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00003914 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
Ian Romanick0cdaa472012-07-27 07:47:28 -07003915 if (ctx->API == API_OPENGLES) {
3916 goto invalid_pname_enum;
3917 } else if (att->Type == GL_NONE) {
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003918 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003919 _mesa_enum_to_string(pname));
Ian Romanick0cdaa472012-07-27 07:47:28 -07003920 } else if (att->Type == GL_TEXTURE) {
Samuel Iglesias Gonsalvez8e49a3e2014-12-11 23:34:13 +01003921 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3922 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
Brian Paulf1e4ca72008-08-06 08:39:54 -06003923 *params = att->Zoffset;
3924 }
3925 else {
3926 *params = 0;
3927 }
Brian Paul3deaa012005-02-07 05:08:24 +00003928 }
3929 else {
Ian Romanick0cdaa472012-07-27 07:47:28 -07003930 goto invalid_pname_enum;
Brian Paul3deaa012005-02-07 05:08:24 +00003931 }
3932 return;
Brian Paul1bc59bf2009-01-22 15:07:34 -07003933 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
Brian Paulc1377ed2014-03-22 10:31:58 -06003934 if ((!_mesa_is_desktop_gl(ctx) ||
3935 !ctx->Extensions.ARB_framebuffer_object)
Ian Romanick0cdaa472012-07-27 07:47:28 -07003936 && !_mesa_is_gles3(ctx)) {
3937 goto invalid_pname_enum;
Brian Paul1bc59bf2009-01-22 15:07:34 -07003938 }
Marek Olšákb9e9df72011-05-31 20:36:07 +02003939 else if (att->Type == GL_NONE) {
Kenneth Graunke713cd232016-12-09 16:16:53 -08003940 if (_mesa_is_winsys_fbo(buffer) &&
3941 (attachment == GL_DEPTH || attachment == GL_STENCIL)) {
3942 *params = GL_LINEAR;
3943 } else {
3944 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3945 _mesa_enum_to_string(pname));
3946 }
Marek Olšákb9e9df72011-05-31 20:36:07 +02003947 }
Brian Paul1bc59bf2009-01-22 15:07:34 -07003948 else {
Marek Olšák69c8f462012-01-24 23:39:31 +01003949 if (ctx->Extensions.EXT_framebuffer_sRGB) {
Brian Paulc1377ed2014-03-22 10:31:58 -06003950 *params =
3951 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
Marek Olšák81ae8c62011-01-23 13:26:43 +01003952 }
3953 else {
3954 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3955 * if the sRGB conversion is unsupported. */
3956 *params = GL_LINEAR;
3957 }
Brian Paul1bc59bf2009-01-22 15:07:34 -07003958 }
3959 return;
3960 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
Brian Paulc1377ed2014-03-22 10:31:58 -06003961 if ((ctx->API != API_OPENGL_COMPAT ||
3962 !ctx->Extensions.ARB_framebuffer_object)
Ian Romanick0cdaa472012-07-27 07:47:28 -07003963 && ctx->API != API_OPENGL_CORE
3964 && !_mesa_is_gles3(ctx)) {
3965 goto invalid_pname_enum;
Brian Paul1bc59bf2009-01-22 15:07:34 -07003966 }
Marek Olšákb9e9df72011-05-31 20:36:07 +02003967 else if (att->Type == GL_NONE) {
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003968 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07003969 _mesa_enum_to_string(pname));
Marek Olšákb9e9df72011-05-31 20:36:07 +02003970 }
Brian Paul1bc59bf2009-01-22 15:07:34 -07003971 else {
Mark Mueller71fe9432014-01-04 14:11:43 -08003972 mesa_format format = att->Renderbuffer->Format;
Ian Romanickd7475c72013-01-18 17:25:57 -08003973
3974 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3975 * 3.0.1 spec says:
3976 *
3977 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3978 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3979 * generate an INVALID_OPERATION error.
3980 */
Brian Paulc1377ed2014-03-22 10:31:58 -06003981 if (_mesa_is_gles3(ctx) &&
3982 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
Ian Romanickd7475c72013-01-18 17:25:57 -08003983 _mesa_error(ctx, GL_INVALID_OPERATION,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003984 "%s(cannot query "
Ian Romanickd7475c72013-01-18 17:25:57 -08003985 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
Laura Ekstrandf22fa302015-01-29 17:11:37 -08003986 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
Ian Romanickd7475c72013-01-18 17:25:57 -08003987 return;
3988 }
3989
Mark Mueller50a01d22014-01-20 19:08:54 -08003990 if (format == MESA_FORMAT_S_UINT8) {
Brian Paul45e76d22009-10-08 20:27:27 -06003991 /* special cases */
3992 *params = GL_INDEX;
3993 }
Mark Muellereeed49f2014-01-26 15:12:56 -08003994 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
Marek Olšák11652802011-06-01 15:48:51 +02003995 /* depends on the attachment parameter */
3996 if (attachment == GL_STENCIL_ATTACHMENT) {
3997 *params = GL_INDEX;
3998 }
3999 else {
4000 *params = GL_FLOAT;
4001 }
4002 }
Brian Paul45e76d22009-10-08 20:27:27 -06004003 else {
4004 *params = _mesa_get_format_datatype(format);
4005 }
Brian Paul1bc59bf2009-01-22 15:07:34 -07004006 }
4007 return;
4008 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
Brian Paul1bc59bf2009-01-22 15:07:34 -07004009 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
Brian Paul1bc59bf2009-01-22 15:07:34 -07004010 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
Brian Paul1bc59bf2009-01-22 15:07:34 -07004011 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
Brian Paul1bc59bf2009-01-22 15:07:34 -07004012 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
Brian Paul1bc59bf2009-01-22 15:07:34 -07004013 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
Brian Paulc1377ed2014-03-22 10:31:58 -06004014 if ((!_mesa_is_desktop_gl(ctx) ||
4015 !ctx->Extensions.ARB_framebuffer_object)
Ian Romanick0cdaa472012-07-27 07:47:28 -07004016 && !_mesa_is_gles3(ctx)) {
4017 goto invalid_pname_enum;
Brian Paul1bc59bf2009-01-22 15:07:34 -07004018 }
Brian Paul45e76d22009-10-08 20:27:27 -06004019 else if (att->Texture) {
4020 const struct gl_texture_image *texImage =
Brian Paulf262ed62015-01-02 16:56:12 -07004021 _mesa_select_tex_image(att->Texture, att->Texture->Target,
Brian Paul45e76d22009-10-08 20:27:27 -06004022 att->TextureLevel);
4023 if (texImage) {
4024 *params = get_component_bits(pname, texImage->_BaseFormat,
4025 texImage->TexFormat);
4026 }
4027 else {
4028 *params = 0;
4029 }
4030 }
4031 else if (att->Renderbuffer) {
4032 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
4033 att->Renderbuffer->Format);
4034 }
Brian Paul1bc59bf2009-01-22 15:07:34 -07004035 else {
Timothy Arceri786b9ad2017-05-05 15:39:15 +10004036 assert(att->Type == GL_NONE);
4037 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
4038 _mesa_enum_to_string(pname));
Brian Paul1bc59bf2009-01-22 15:07:34 -07004039 }
4040 return;
Paul Berryec79c052013-11-19 21:17:19 -08004041 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
4042 if (!_mesa_has_geometry_shaders(ctx)) {
4043 goto invalid_pname_enum;
4044 } else if (att->Type == GL_TEXTURE) {
4045 *params = att->Layered;
4046 } else if (att->Type == GL_NONE) {
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004047 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004048 _mesa_enum_to_string(pname));
Paul Berryec79c052013-11-19 21:17:19 -08004049 } else {
4050 goto invalid_pname_enum;
4051 }
4052 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00004053 default:
Ian Romanick0cdaa472012-07-27 07:47:28 -07004054 goto invalid_pname_enum;
Brian Paulddc82ee2005-02-05 19:56:45 +00004055 }
Ian Romanick0cdaa472012-07-27 07:47:28 -07004056
4057 return;
4058
4059invalid_pname_enum:
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004060 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004061 _mesa_enum_to_string(pname));
Ian Romanick0cdaa472012-07-27 07:47:28 -07004062 return;
Brian Paulddc82ee2005-02-05 19:56:45 +00004063}
4064
4065
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004066void GLAPIENTRY
4067_mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
4068 GLenum pname, GLint *params)
4069{
4070 GET_CURRENT_CONTEXT(ctx);
4071 struct gl_framebuffer *buffer;
4072
4073 buffer = get_framebuffer_target(ctx, target);
4074 if (!buffer) {
4075 _mesa_error(ctx, GL_INVALID_ENUM,
4076 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004077 _mesa_enum_to_string(target));
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004078 return;
4079 }
4080
Timothy Arceri8b006302017-05-05 15:21:22 +10004081 get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4082 params,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004083 "glGetFramebufferAttachmentParameteriv");
4084}
4085
4086
4087void GLAPIENTRY
4088_mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
4089 GLenum attachment,
4090 GLenum pname, GLint *params)
4091{
4092 GET_CURRENT_CONTEXT(ctx);
4093 struct gl_framebuffer *buffer;
4094
4095 if (framebuffer) {
4096 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4097 "glGetNamedFramebufferAttachmentParameteriv");
4098 if (!buffer)
4099 return;
4100 }
4101 else {
4102 /*
4103 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
4104 * 4.5 core spec (30.10.2014, PDF page 314):
4105 * "If framebuffer is zero, then the default draw framebuffer is
4106 * queried."
4107 */
4108 buffer = ctx->WinSysDrawBuffer;
4109 }
4110
Timothy Arceri8b006302017-05-05 15:21:22 +10004111 get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
4112 params,
Laura Ekstrandf22fa302015-01-29 17:11:37 -08004113 "glGetNamedFramebufferAttachmentParameteriv");
4114}
4115
4116
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004117void GLAPIENTRY
4118_mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
4119 GLint param)
4120{
4121 GET_CURRENT_CONTEXT(ctx);
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004122 struct gl_framebuffer *fb = NULL;
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004123
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004124 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4125 _mesa_error(ctx, GL_INVALID_OPERATION,
4126 "glNamedFramebufferParameteri("
4127 "ARB_framebuffer_no_attachments not implemented)");
4128 return;
4129 }
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004130
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004131 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4132 "glNamedFramebufferParameteri");
4133
4134 if (fb) {
4135 framebuffer_parameteri(ctx, fb, pname, param,
4136 "glNamedFramebufferParameteriv");
4137 }
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004138}
4139
4140
4141void GLAPIENTRY
4142_mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
4143 GLint *param)
4144{
4145 GET_CURRENT_CONTEXT(ctx);
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004146 struct gl_framebuffer *fb;
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004147
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004148 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
4149 _mesa_error(ctx, GL_INVALID_OPERATION,
4150 "glNamedFramebufferParameteriv("
4151 "ARB_framebuffer_no_attachments not implemented)");
4152 return;
4153 }
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004154
Kevin Rogovin6aa12992015-06-17 13:29:52 +03004155 if (framebuffer) {
4156 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4157 "glGetNamedFramebufferParameteriv");
4158 } else {
4159 fb = ctx->WinSysDrawBuffer;
4160 }
4161
4162 if (fb) {
4163 get_framebuffer_parameteriv(ctx, fb, pname, param,
4164 "glGetNamedFramebufferParameteriv");
4165 }
Laura Ekstrand9f1db782015-02-05 16:38:11 -08004166}
4167
4168
Ian Romanick342be8a2012-08-13 09:27:00 -07004169static void
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004170invalidate_framebuffer_storage(struct gl_context *ctx,
4171 struct gl_framebuffer *fb,
4172 GLsizei numAttachments,
Ian Romanick342be8a2012-08-13 09:27:00 -07004173 const GLenum *attachments, GLint x, GLint y,
4174 GLsizei width, GLsizei height, const char *name)
4175{
4176 int i;
Ian Romanick342be8a2012-08-13 09:27:00 -07004177
Laura Ekstrandb4368ac2015-02-04 14:21:17 -08004178 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4179 * Spec (2.2.2015, PDF page 522) says:
4180 * "An INVALID_VALUE error is generated if numAttachments, width, or
4181 * height is negative."
4182 */
Ian Romanick342be8a2012-08-13 09:27:00 -07004183 if (numAttachments < 0) {
4184 _mesa_error(ctx, GL_INVALID_VALUE,
4185 "%s(numAttachments < 0)", name);
4186 return;
4187 }
4188
Laura Ekstrandb4368ac2015-02-04 14:21:17 -08004189 if (width < 0) {
4190 _mesa_error(ctx, GL_INVALID_VALUE,
4191 "%s(width < 0)", name);
4192 return;
4193 }
4194
4195 if (height < 0) {
4196 _mesa_error(ctx, GL_INVALID_VALUE,
4197 "%s(height < 0)", name);
4198 return;
4199 }
4200
Ian Romanick342be8a2012-08-13 09:27:00 -07004201 /* The GL_ARB_invalidate_subdata spec says:
4202 *
4203 * "If an attachment is specified that does not exist in the
4204 * framebuffer bound to <target>, it is ignored."
4205 *
4206 * It also says:
4207 *
4208 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4209 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4210 * INVALID_OPERATION is generated."
4211 *
4212 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4213 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4214 * set of retrictions).
4215 */
4216 for (i = 0; i < numAttachments; i++) {
4217 if (_mesa_is_winsys_fbo(fb)) {
4218 switch (attachments[i]) {
4219 case GL_ACCUM:
4220 case GL_AUX0:
4221 case GL_AUX1:
4222 case GL_AUX2:
4223 case GL_AUX3:
4224 /* Accumulation buffers and auxilary buffers were removed in
4225 * OpenGL 3.1, and they never existed in OpenGL ES.
4226 */
Paul Berrydbd61352012-11-27 12:26:51 -08004227 if (ctx->API != API_OPENGL_COMPAT)
Ian Romanick342be8a2012-08-13 09:27:00 -07004228 goto invalid_enum;
4229 break;
4230 case GL_COLOR:
4231 case GL_DEPTH:
4232 case GL_STENCIL:
4233 break;
4234 case GL_BACK_LEFT:
4235 case GL_BACK_RIGHT:
4236 case GL_FRONT_LEFT:
4237 case GL_FRONT_RIGHT:
4238 if (!_mesa_is_desktop_gl(ctx))
4239 goto invalid_enum;
4240 break;
4241 default:
4242 goto invalid_enum;
4243 }
4244 } else {
4245 switch (attachments[i]) {
4246 case GL_DEPTH_ATTACHMENT:
4247 case GL_STENCIL_ATTACHMENT:
4248 break;
Eduardo Lima Mitev242ad322014-11-18 16:28:18 +01004249 case GL_DEPTH_STENCIL_ATTACHMENT:
4250 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4251 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4252 * extension does not make this attachment point valid on ES 2.0.
4253 */
4254 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4255 break;
4256 /* fallthrough */
Ian Romanick342be8a2012-08-13 09:27:00 -07004257 case GL_COLOR_ATTACHMENT0:
4258 case GL_COLOR_ATTACHMENT1:
4259 case GL_COLOR_ATTACHMENT2:
4260 case GL_COLOR_ATTACHMENT3:
4261 case GL_COLOR_ATTACHMENT4:
4262 case GL_COLOR_ATTACHMENT5:
4263 case GL_COLOR_ATTACHMENT6:
4264 case GL_COLOR_ATTACHMENT7:
4265 case GL_COLOR_ATTACHMENT8:
4266 case GL_COLOR_ATTACHMENT9:
4267 case GL_COLOR_ATTACHMENT10:
4268 case GL_COLOR_ATTACHMENT11:
4269 case GL_COLOR_ATTACHMENT12:
4270 case GL_COLOR_ATTACHMENT13:
4271 case GL_COLOR_ATTACHMENT14:
4272 case GL_COLOR_ATTACHMENT15: {
Brian Paul859c3872012-11-04 16:43:44 -07004273 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
Ian Romanick342be8a2012-08-13 09:27:00 -07004274 if (k >= ctx->Const.MaxColorAttachments) {
4275 _mesa_error(ctx, GL_INVALID_OPERATION,
4276 "%s(attachment >= max. color attachments)", name);
4277 return;
4278 }
Constantin Baranov53904c62013-10-13 01:17:15 +03004279 break;
Ian Romanick342be8a2012-08-13 09:27:00 -07004280 }
4281 default:
4282 goto invalid_enum;
4283 }
4284 }
4285 }
4286
4287 /* We don't actually do anything for this yet. Just return after
4288 * validating the parameters and generating the required errors.
4289 */
4290 return;
4291
4292invalid_enum:
Laura Ekstrandb4368ac2015-02-04 14:21:17 -08004293 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004294 _mesa_enum_to_string(attachments[i]));
Ian Romanick342be8a2012-08-13 09:27:00 -07004295 return;
4296}
4297
Brian Paulc1377ed2014-03-22 10:31:58 -06004298
Ian Romanick342be8a2012-08-13 09:27:00 -07004299void GLAPIENTRY
4300_mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4301 const GLenum *attachments, GLint x, GLint y,
4302 GLsizei width, GLsizei height)
4303{
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004304 struct gl_framebuffer *fb;
4305 GET_CURRENT_CONTEXT(ctx);
4306
4307 fb = get_framebuffer_target(ctx, target);
4308 if (!fb) {
4309 _mesa_error(ctx, GL_INVALID_ENUM,
4310 "glInvalidateSubFramebuffer(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004311 _mesa_enum_to_string(target));
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004312 return;
4313 }
4314
4315 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
Ian Romanick342be8a2012-08-13 09:27:00 -07004316 x, y, width, height,
4317 "glInvalidateSubFramebuffer");
4318}
4319
Brian Paulc1377ed2014-03-22 10:31:58 -06004320
Ian Romanick342be8a2012-08-13 09:27:00 -07004321void GLAPIENTRY
Laura Ekstrandd890fc72015-02-04 14:21:48 -08004322_mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4323 GLsizei numAttachments,
4324 const GLenum *attachments,
4325 GLint x, GLint y,
4326 GLsizei width, GLsizei height)
4327{
4328 struct gl_framebuffer *fb;
4329 GET_CURRENT_CONTEXT(ctx);
4330
4331 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4332 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4333 * default draw framebuffer is affected."
4334 */
4335 if (framebuffer) {
4336 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4337 "glInvalidateNamedFramebufferSubData");
4338 if (!fb)
4339 return;
4340 }
4341 else
4342 fb = ctx->WinSysDrawBuffer;
4343
4344 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4345 x, y, width, height,
4346 "glInvalidateNamedFramebufferSubData");
4347}
4348
4349
4350void GLAPIENTRY
Ian Romanick342be8a2012-08-13 09:27:00 -07004351_mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4352 const GLenum *attachments)
4353{
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004354 struct gl_framebuffer *fb;
4355 GET_CURRENT_CONTEXT(ctx);
4356
4357 fb = get_framebuffer_target(ctx, target);
4358 if (!fb) {
4359 _mesa_error(ctx, GL_INVALID_ENUM,
4360 "glInvalidateFramebuffer(invalid target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004361 _mesa_enum_to_string(target));
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004362 return;
4363 }
4364
Ian Romanick342be8a2012-08-13 09:27:00 -07004365 /* The GL_ARB_invalidate_subdata spec says:
4366 *
4367 * "The command
4368 *
4369 * void InvalidateFramebuffer(enum target,
4370 * sizei numAttachments,
4371 * const enum *attachments);
4372 *
4373 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4374 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4375 * <MAX_VIEWPORT_DIMS[1]> respectively."
4376 */
Laura Ekstrand65d4a202015-02-04 09:49:58 -08004377 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
Brian Paulc1377ed2014-03-22 10:31:58 -06004378 0, 0,
Samuel Iglesias Gonsálvezaa849d92016-03-01 12:02:27 +01004379 ctx->Const.MaxViewportWidth,
4380 ctx->Const.MaxViewportHeight,
Ian Romanick342be8a2012-08-13 09:27:00 -07004381 "glInvalidateFramebuffer");
4382}
Tapani Pälli413941e2013-02-18 09:12:27 +02004383
Brian Paulc1377ed2014-03-22 10:31:58 -06004384
Tapani Pälli413941e2013-02-18 09:12:27 +02004385void GLAPIENTRY
Laura Ekstrandd890fc72015-02-04 14:21:48 -08004386_mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4387 GLsizei numAttachments,
4388 const GLenum *attachments)
4389{
4390 struct gl_framebuffer *fb;
4391 GET_CURRENT_CONTEXT(ctx);
4392
4393 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4394 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4395 * default draw framebuffer is affected."
4396 */
4397 if (framebuffer) {
4398 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4399 "glInvalidateNamedFramebufferData");
4400 if (!fb)
4401 return;
4402 }
4403 else
4404 fb = ctx->WinSysDrawBuffer;
4405
4406 /* The GL_ARB_invalidate_subdata spec says:
4407 *
4408 * "The command
4409 *
4410 * void InvalidateFramebuffer(enum target,
4411 * sizei numAttachments,
4412 * const enum *attachments);
4413 *
4414 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4415 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4416 * <MAX_VIEWPORT_DIMS[1]> respectively."
4417 */
4418 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4419 0, 0,
Samuel Iglesias Gonsálvezaa849d92016-03-01 12:02:27 +01004420 ctx->Const.MaxViewportWidth,
4421 ctx->Const.MaxViewportHeight,
Laura Ekstrandd890fc72015-02-04 14:21:48 -08004422 "glInvalidateNamedFramebufferData");
4423}
4424
4425
4426void GLAPIENTRY
Tapani Pälli413941e2013-02-18 09:12:27 +02004427_mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4428 const GLenum *attachments)
4429{
4430 struct gl_framebuffer *fb;
4431 GLint i;
4432
4433 GET_CURRENT_CONTEXT(ctx);
4434
4435 fb = get_framebuffer_target(ctx, target);
4436 if (!fb) {
4437 _mesa_error(ctx, GL_INVALID_ENUM,
4438 "glDiscardFramebufferEXT(target %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004439 _mesa_enum_to_string(target));
Tapani Pälli413941e2013-02-18 09:12:27 +02004440 return;
4441 }
4442
4443 if (numAttachments < 0) {
4444 _mesa_error(ctx, GL_INVALID_VALUE,
4445 "glDiscardFramebufferEXT(numAttachments < 0)");
4446 return;
4447 }
4448
4449 for (i = 0; i < numAttachments; i++) {
4450 switch (attachments[i]) {
4451 case GL_COLOR:
4452 case GL_DEPTH:
4453 case GL_STENCIL:
4454 if (_mesa_is_user_fbo(fb))
4455 goto invalid_enum;
4456 break;
4457 case GL_COLOR_ATTACHMENT0:
4458 case GL_DEPTH_ATTACHMENT:
4459 case GL_STENCIL_ATTACHMENT:
4460 if (_mesa_is_winsys_fbo(fb))
4461 goto invalid_enum;
4462 break;
4463 default:
4464 goto invalid_enum;
4465 }
4466 }
4467
4468 if (ctx->Driver.DiscardFramebuffer)
4469 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4470
4471 return;
4472
4473invalid_enum:
4474 _mesa_error(ctx, GL_INVALID_ENUM,
4475 "glDiscardFramebufferEXT(attachment %s)",
Kenneth Graunke2f11e922015-07-18 01:22:00 -07004476 _mesa_enum_to_string(attachments[i]));
Tapani Pälli413941e2013-02-18 09:12:27 +02004477}