OSDN Git Service

extensions: enable EXT_color_buffer_float for ES3
[android-x86/external-mesa.git] / src / mesa / main / fbobject.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.1
4  *
5  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6  * Copyright (C) 1999-2009  VMware, Inc.  All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26
27 /*
28  * GL_EXT/ARB_framebuffer_object extensions
29  *
30  * Authors:
31  *   Brian Paul
32  */
33
34 #include <stdbool.h>
35
36 #include "buffers.h"
37 #include "context.h"
38 #include "enums.h"
39 #include "fbobject.h"
40 #include "formats.h"
41 #include "framebuffer.h"
42 #include "glformats.h"
43 #include "hash.h"
44 #include "macros.h"
45 #include "mfeatures.h"
46 #include "mtypes.h"
47 #include "renderbuffer.h"
48 #include "state.h"
49 #include "teximage.h"
50 #include "texobj.h"
51
52
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
54 #define DEBUG_BLIT 0
55
56
57 /**
58  * Notes:
59  *
60  * None of the GL_EXT_framebuffer_object functions are compiled into
61  * display lists.
62  */
63
64
65
66 /*
67  * When glGenRender/FramebuffersEXT() is called we insert pointers to
68  * these placeholder objects into the hash table.
69  * Later, when the object ID is first bound, we replace the placeholder
70  * with the real frame/renderbuffer.
71  */
72 static struct gl_framebuffer DummyFramebuffer;
73 static struct gl_renderbuffer DummyRenderbuffer;
74
75 /* We bind this framebuffer when applications pass a NULL
76  * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer;
78
79
80 static void
81 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
82 {
83    /* no op */
84 }
85
86 static void
87 delete_dummy_framebuffer(struct gl_framebuffer *fb)
88 {
89    /* no op */
90 }
91
92
93 void
94 _mesa_init_fbobjects(struct gl_context *ctx)
95 {
96    _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
97    _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
98    _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
99    DummyFramebuffer.Delete = delete_dummy_framebuffer;
100    DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
101    IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
102 }
103
104 struct gl_framebuffer *
105 _mesa_get_incomplete_framebuffer(void)
106 {
107    return &IncompleteFramebuffer;
108 }
109
110 /**
111  * Helper routine for getting a gl_renderbuffer.
112  */
113 struct gl_renderbuffer *
114 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
115 {
116    struct gl_renderbuffer *rb;
117
118    if (id == 0)
119       return NULL;
120
121    rb = (struct gl_renderbuffer *)
122       _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
123    return rb;
124 }
125
126
127 /**
128  * Helper routine for getting a gl_framebuffer.
129  */
130 struct gl_framebuffer *
131 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
132 {
133    struct gl_framebuffer *fb;
134
135    if (id == 0)
136       return NULL;
137
138    fb = (struct gl_framebuffer *)
139       _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
140    return fb;
141 }
142
143
144 /**
145  * Mark the given framebuffer as invalid.  This will force the
146  * test for framebuffer completeness to be done before the framebuffer
147  * is used.
148  */
149 static void
150 invalidate_framebuffer(struct gl_framebuffer *fb)
151 {
152    fb->_Status = 0; /* "indeterminate" */
153 }
154
155
156 /**
157  * Return the gl_framebuffer object which corresponds to the given
158  * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159  * Check support for GL_EXT_framebuffer_blit to determine if certain
160  * targets are legal.
161  * \return gl_framebuffer pointer or NULL if target is illegal
162  */
163 static struct gl_framebuffer *
164 get_framebuffer_target(struct gl_context *ctx, GLenum target)
165 {
166    bool have_fb_blit = _mesa_is_gles3(ctx) ||
167       (ctx->Extensions.EXT_framebuffer_blit && _mesa_is_desktop_gl(ctx));
168    switch (target) {
169    case GL_DRAW_FRAMEBUFFER:
170       return have_fb_blit ? ctx->DrawBuffer : NULL;
171    case GL_READ_FRAMEBUFFER:
172       return have_fb_blit ? ctx->ReadBuffer : NULL;
173    case GL_FRAMEBUFFER_EXT:
174       return ctx->DrawBuffer;
175    default:
176       return NULL;
177    }
178 }
179
180
181 /**
182  * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183  * gl_renderbuffer_attachment object.
184  * This function is only used for user-created FB objects, not the
185  * default / window-system FB object.
186  * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187  * the depth buffer attachment point.
188  */
189 struct gl_renderbuffer_attachment *
190 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
191                      GLenum attachment)
192 {
193    GLuint i;
194
195    assert(_mesa_is_user_fbo(fb));
196
197    switch (attachment) {
198    case GL_COLOR_ATTACHMENT0_EXT:
199    case GL_COLOR_ATTACHMENT1_EXT:
200    case GL_COLOR_ATTACHMENT2_EXT:
201    case GL_COLOR_ATTACHMENT3_EXT:
202    case GL_COLOR_ATTACHMENT4_EXT:
203    case GL_COLOR_ATTACHMENT5_EXT:
204    case GL_COLOR_ATTACHMENT6_EXT:
205    case GL_COLOR_ATTACHMENT7_EXT:
206    case GL_COLOR_ATTACHMENT8_EXT:
207    case GL_COLOR_ATTACHMENT9_EXT:
208    case GL_COLOR_ATTACHMENT10_EXT:
209    case GL_COLOR_ATTACHMENT11_EXT:
210    case GL_COLOR_ATTACHMENT12_EXT:
211    case GL_COLOR_ATTACHMENT13_EXT:
212    case GL_COLOR_ATTACHMENT14_EXT:
213    case GL_COLOR_ATTACHMENT15_EXT:
214       /* Only OpenGL ES 1.x forbids color attachments other than
215        * GL_COLOR_ATTACHMENT0.  For all other APIs the limit set by the
216        * hardware is used.
217        */
218       i = attachment - GL_COLOR_ATTACHMENT0_EXT;
219       if (i >= ctx->Const.MaxColorAttachments
220           || (i > 0 && ctx->API == API_OPENGLES)) {
221          return NULL;
222       }
223       return &fb->Attachment[BUFFER_COLOR0 + i];
224    case GL_DEPTH_STENCIL_ATTACHMENT:
225       if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
226          return NULL;
227       /* fall-through */
228    case GL_DEPTH_ATTACHMENT_EXT:
229       return &fb->Attachment[BUFFER_DEPTH];
230    case GL_STENCIL_ATTACHMENT_EXT:
231       return &fb->Attachment[BUFFER_STENCIL];
232    default:
233       return NULL;
234    }
235 }
236
237
238 /**
239  * As above, but only used for getting attachments of the default /
240  * window-system framebuffer (not user-created framebuffer objects).
241  */
242 static struct gl_renderbuffer_attachment *
243 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
244                          GLenum attachment)
245 {
246    assert(_mesa_is_winsys_fbo(fb));
247
248    if (_mesa_is_gles3(ctx)) {
249       assert(attachment == GL_BACK ||
250              attachment == GL_DEPTH ||
251              attachment == GL_STENCIL);
252       switch (attachment) {
253       case GL_BACK:
254          /* Since there is no stereo rendering in ES 3.0, only return the
255           * LEFT bits.
256           */
257          if (ctx->DrawBuffer->Visual.doubleBufferMode)
258             return &fb->Attachment[BUFFER_BACK_LEFT];
259          return &fb->Attachment[BUFFER_FRONT_LEFT];
260       case GL_DEPTH:
261       return &fb->Attachment[BUFFER_DEPTH];
262       case GL_STENCIL:
263          return &fb->Attachment[BUFFER_STENCIL];
264       }
265    }
266
267    switch (attachment) {
268    case GL_FRONT_LEFT:
269       return &fb->Attachment[BUFFER_FRONT_LEFT];
270    case GL_FRONT_RIGHT:
271       return &fb->Attachment[BUFFER_FRONT_RIGHT];
272    case GL_BACK_LEFT:
273       return &fb->Attachment[BUFFER_BACK_LEFT];
274    case GL_BACK_RIGHT:
275       return &fb->Attachment[BUFFER_BACK_RIGHT];
276    case GL_AUX0:
277       if (fb->Visual.numAuxBuffers == 1) {
278          return &fb->Attachment[BUFFER_AUX0];
279       }
280       return NULL;
281
282    /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
283     *
284     *     "If the default framebuffer is bound to target, then attachment must
285     *     be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
286     *     identifying a color buffer; DEPTH, identifying the depth buffer; or
287     *     STENCIL, identifying the stencil buffer."
288     *
289     * Revision #34 of the ARB_framebuffer_object spec has essentially the same
290     * language.  However, revision #33 of the ARB_framebuffer_object spec
291     * says:
292     *
293     *     "If the default framebuffer is bound to <target>, then <attachment>
294     *     must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
295     *     DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
296     *     depth buffer, or the stencil buffer, and <pname> may be
297     *     FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
298     *     FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
299     *
300     * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
301     * from glext.h, so shipping apps should not use those values.
302     *
303     * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
304     * support queries of the window system FBO.
305     */
306    case GL_DEPTH:
307       return &fb->Attachment[BUFFER_DEPTH];
308    case GL_STENCIL:
309       return &fb->Attachment[BUFFER_STENCIL];
310    default:
311       return NULL;
312    }
313 }
314
315
316
317 /**
318  * Remove any texture or renderbuffer attached to the given attachment
319  * point.  Update reference counts, etc.
320  */
321 void
322 _mesa_remove_attachment(struct gl_context *ctx,
323                         struct gl_renderbuffer_attachment *att)
324 {
325    if (att->Type == GL_TEXTURE) {
326       ASSERT(att->Texture);
327       if (ctx->Driver.FinishRenderTexture) {
328          /* tell driver that we're done rendering to this texture. */
329          ctx->Driver.FinishRenderTexture(ctx, att);
330       }
331       _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
332       ASSERT(!att->Texture);
333    }
334    if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
335       ASSERT(!att->Texture);
336       _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
337       ASSERT(!att->Renderbuffer);
338    }
339    att->Type = GL_NONE;
340    att->Complete = GL_TRUE;
341 }
342
343
344 /**
345  * Bind a texture object to an attachment point.
346  * The previous binding, if any, will be removed first.
347  */
348 void
349 _mesa_set_texture_attachment(struct gl_context *ctx,
350                              struct gl_framebuffer *fb,
351                              struct gl_renderbuffer_attachment *att,
352                              struct gl_texture_object *texObj,
353                              GLenum texTarget, GLuint level, GLuint zoffset)
354 {
355    if (att->Texture == texObj) {
356       /* re-attaching same texture */
357       ASSERT(att->Type == GL_TEXTURE);
358       if (ctx->Driver.FinishRenderTexture)
359          ctx->Driver.FinishRenderTexture(ctx, att);
360    }
361    else {
362       /* new attachment */
363       if (ctx->Driver.FinishRenderTexture && att->Texture)
364          ctx->Driver.FinishRenderTexture(ctx, att);
365       _mesa_remove_attachment(ctx, att);
366       att->Type = GL_TEXTURE;
367       assert(!att->Texture);
368       _mesa_reference_texobj(&att->Texture, texObj);
369    }
370
371    /* always update these fields */
372    att->TextureLevel = level;
373    att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
374    att->Zoffset = zoffset;
375    att->Complete = GL_FALSE;
376
377    if (_mesa_get_attachment_teximage(att)) {
378       ctx->Driver.RenderTexture(ctx, fb, att);
379    }
380
381    invalidate_framebuffer(fb);
382 }
383
384
385 /**
386  * Bind a renderbuffer to an attachment point.
387  * The previous binding, if any, will be removed first.
388  */
389 void
390 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
391                                   struct gl_renderbuffer_attachment *att,
392                                   struct gl_renderbuffer *rb)
393 {
394    /* XXX check if re-doing same attachment, exit early */
395    _mesa_remove_attachment(ctx, att);
396    att->Type = GL_RENDERBUFFER_EXT;
397    att->Texture = NULL; /* just to be safe */
398    att->Complete = GL_FALSE;
399    _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
400 }
401
402
403 /**
404  * Fallback for ctx->Driver.FramebufferRenderbuffer()
405  * Attach a renderbuffer object to a framebuffer object.
406  */
407 void
408 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
409                                struct gl_framebuffer *fb,
410                                GLenum attachment, struct gl_renderbuffer *rb)
411 {
412    struct gl_renderbuffer_attachment *att;
413
414    _glthread_LOCK_MUTEX(fb->Mutex);
415
416    att = _mesa_get_attachment(ctx, fb, attachment);
417    ASSERT(att);
418    if (rb) {
419       _mesa_set_renderbuffer_attachment(ctx, att, rb);
420       if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
421          /* do stencil attachment here (depth already done above) */
422          att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
423          assert(att);
424          _mesa_set_renderbuffer_attachment(ctx, att, rb);
425       }
426       rb->AttachedAnytime = GL_TRUE;
427    }
428    else {
429       _mesa_remove_attachment(ctx, att);
430    }
431
432    invalidate_framebuffer(fb);
433
434    _glthread_UNLOCK_MUTEX(fb->Mutex);
435 }
436
437
438 /**
439  * Fallback for ctx->Driver.ValidateFramebuffer()
440  * Check if the renderbuffer's formats are supported by the software
441  * renderer.
442  * Drivers should probably override this.
443  */
444 void
445 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
446 {
447    gl_buffer_index buf;
448    for (buf = 0; buf < BUFFER_COUNT; buf++) {
449       const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
450       if (rb) {
451          switch (rb->_BaseFormat) {
452          case GL_ALPHA:
453          case GL_LUMINANCE_ALPHA:
454          case GL_LUMINANCE:
455          case GL_INTENSITY:
456          case GL_RED:
457          case GL_RG:
458             fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
459             return;
460
461          default:
462             switch (rb->Format) {
463             /* XXX This list is likely incomplete. */
464             case MESA_FORMAT_RGB9_E5_FLOAT:
465                fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
466                return;
467             default:;
468                /* render buffer format is supported by software rendering */
469             }
470          }
471       }
472    }
473 }
474
475
476 /**
477  * For debug only.
478  */
479 static void
480 att_incomplete(const char *msg)
481 {
482    if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
483       _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
484    }
485 }
486
487
488 /**
489  * For debug only.
490  */
491 static void
492 fbo_incomplete(const char *msg, int index)
493 {
494    if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
495       _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
496    }
497 }
498
499
500 /**
501  * Is the given base format a legal format for a color renderbuffer?
502  */
503 GLboolean
504 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
505 {
506    switch (baseFormat) {
507    case GL_RGB:
508    case GL_RGBA:
509       return GL_TRUE;
510    case GL_LUMINANCE:
511    case GL_LUMINANCE_ALPHA:
512    case GL_INTENSITY:
513    case GL_ALPHA:
514       return ctx->API == API_OPENGL_COMPAT &&
515              ctx->Extensions.ARB_framebuffer_object;
516    case GL_RED:
517    case GL_RG:
518       return ctx->Extensions.ARB_texture_rg;
519    default:
520       return GL_FALSE;
521    }
522 }
523
524
525 /**
526  * Is the given base format a legal format for a depth/stencil renderbuffer?
527  */
528 static GLboolean
529 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
530 {
531    switch (baseFormat) {
532    case GL_DEPTH_COMPONENT:
533    case GL_DEPTH_STENCIL_EXT:
534       return GL_TRUE;
535    default:
536       return GL_FALSE;
537    }
538 }
539
540
541 /**
542  * Test if an attachment point is complete and update its Complete field.
543  * \param format if GL_COLOR, this is a color attachment point,
544  *               if GL_DEPTH, this is a depth component attachment point,
545  *               if GL_STENCIL, this is a stencil component attachment point.
546  */
547 static void
548 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
549                              struct gl_renderbuffer_attachment *att)
550 {
551    assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
552
553    /* assume complete */
554    att->Complete = GL_TRUE;
555
556    /* Look for reasons why the attachment might be incomplete */
557    if (att->Type == GL_TEXTURE) {
558       const struct gl_texture_object *texObj = att->Texture;
559       struct gl_texture_image *texImage;
560       GLenum baseFormat;
561
562       if (!texObj) {
563          att_incomplete("no texobj");
564          att->Complete = GL_FALSE;
565          return;
566       }
567
568       texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
569       if (!texImage) {
570          att_incomplete("no teximage");
571          att->Complete = GL_FALSE;
572          return;
573       }
574       if (texImage->Width < 1 || texImage->Height < 1) {
575          att_incomplete("teximage width/height=0");
576          printf("texobj = %u\n", texObj->Name);
577          printf("level = %d\n", att->TextureLevel);
578          att->Complete = GL_FALSE;
579          return;
580       }
581       if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
582          att_incomplete("bad z offset");
583          att->Complete = GL_FALSE;
584          return;
585       }
586
587       baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
588
589       if (format == GL_COLOR) {
590          if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
591             att_incomplete("bad format");
592             att->Complete = GL_FALSE;
593             return;
594          }
595          if (_mesa_is_format_compressed(texImage->TexFormat)) {
596             att_incomplete("compressed internalformat");
597             att->Complete = GL_FALSE;
598             return;
599          }
600       }
601       else if (format == GL_DEPTH) {
602          if (baseFormat == GL_DEPTH_COMPONENT) {
603             /* OK */
604          }
605          else if (ctx->Extensions.EXT_packed_depth_stencil &&
606                   ctx->Extensions.ARB_depth_texture &&
607                   baseFormat == GL_DEPTH_STENCIL_EXT) {
608             /* OK */
609          }
610          else {
611             att->Complete = GL_FALSE;
612             att_incomplete("bad depth format");
613             return;
614          }
615       }
616       else {
617          ASSERT(format == GL_STENCIL);
618          if (ctx->Extensions.EXT_packed_depth_stencil &&
619              ctx->Extensions.ARB_depth_texture &&
620              baseFormat == GL_DEPTH_STENCIL_EXT) {
621             /* OK */
622          }
623          else {
624             /* no such thing as stencil-only textures */
625             att_incomplete("illegal stencil texture");
626             att->Complete = GL_FALSE;
627             return;
628          }
629       }
630    }
631    else if (att->Type == GL_RENDERBUFFER_EXT) {
632       const GLenum baseFormat =
633          _mesa_get_format_base_format(att->Renderbuffer->Format);
634
635       ASSERT(att->Renderbuffer);
636       if (!att->Renderbuffer->InternalFormat ||
637           att->Renderbuffer->Width < 1 ||
638           att->Renderbuffer->Height < 1) {
639          att_incomplete("0x0 renderbuffer");
640          att->Complete = GL_FALSE;
641          return;
642       }
643       if (format == GL_COLOR) {
644          if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
645             att_incomplete("bad renderbuffer color format");
646             att->Complete = GL_FALSE;
647             return;
648          }
649       }
650       else if (format == GL_DEPTH) {
651          if (baseFormat == GL_DEPTH_COMPONENT) {
652             /* OK */
653          }
654          else if (ctx->Extensions.EXT_packed_depth_stencil &&
655                   baseFormat == GL_DEPTH_STENCIL_EXT) {
656             /* OK */
657          }
658          else {
659             att_incomplete("bad renderbuffer depth format");
660             att->Complete = GL_FALSE;
661             return;
662          }
663       }
664       else {
665          assert(format == GL_STENCIL);
666          if (baseFormat == GL_STENCIL_INDEX) {
667             /* OK */
668          }
669          else if (ctx->Extensions.EXT_packed_depth_stencil &&
670                   baseFormat == GL_DEPTH_STENCIL_EXT) {
671             /* OK */
672          }
673          else {
674             att->Complete = GL_FALSE;
675             att_incomplete("bad renderbuffer stencil format");
676             return;
677          }
678       }
679    }
680    else {
681       ASSERT(att->Type == GL_NONE);
682       /* complete */
683       return;
684    }
685 }
686
687
688 /**
689  * Test if the given framebuffer object is complete and update its
690  * Status field with the results.
691  * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
692  * driver to make hardware-specific validation/completeness checks.
693  * Also update the framebuffer's Width and Height fields if the
694  * framebuffer is complete.
695  */
696 void
697 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
698                                     struct gl_framebuffer *fb)
699 {
700    GLuint numImages;
701    GLenum intFormat = GL_NONE; /* color buffers' internal format */
702    GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
703    GLint numSamples = -1;
704    GLint i;
705    GLuint j;
706
707    assert(_mesa_is_user_fbo(fb));
708
709    /* we're changing framebuffer fields here */
710    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
711
712    numImages = 0;
713    fb->Width = 0;
714    fb->Height = 0;
715
716    /* Start at -2 to more easily loop over all attachment points.
717     *  -2: depth buffer
718     *  -1: stencil buffer
719     * >=0: color buffer
720     */
721    for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
722       struct gl_renderbuffer_attachment *att;
723       GLenum f;
724       gl_format attFormat;
725
726       /*
727        * XXX for ARB_fbo, only check color buffers that are named by
728        * GL_READ_BUFFER and GL_DRAW_BUFFERi.
729        */
730
731       /* check for attachment completeness
732        */
733       if (i == -2) {
734          att = &fb->Attachment[BUFFER_DEPTH];
735          test_attachment_completeness(ctx, GL_DEPTH, att);
736          if (!att->Complete) {
737             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
738             fbo_incomplete("depth attachment incomplete", -1);
739             return;
740          }
741       }
742       else if (i == -1) {
743          att = &fb->Attachment[BUFFER_STENCIL];
744          test_attachment_completeness(ctx, GL_STENCIL, att);
745          if (!att->Complete) {
746             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
747             fbo_incomplete("stencil attachment incomplete", -1);
748             return;
749          }
750       }
751       else {
752          att = &fb->Attachment[BUFFER_COLOR0 + i];
753          test_attachment_completeness(ctx, GL_COLOR, att);
754          if (!att->Complete) {
755             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
756             fbo_incomplete("color attachment incomplete", i);
757             return;
758          }
759       }
760
761       /* get width, height, format of the renderbuffer/texture
762        */
763       if (att->Type == GL_TEXTURE) {
764          const struct gl_texture_image *texImg =
765             _mesa_get_attachment_teximage(att);
766          minWidth = MIN2(minWidth, texImg->Width);
767          maxWidth = MAX2(maxWidth, texImg->Width);
768          minHeight = MIN2(minHeight, texImg->Height);
769          maxHeight = MAX2(maxHeight, texImg->Height);
770          f = texImg->_BaseFormat;
771          attFormat = texImg->TexFormat;
772          numImages++;
773          if (!_mesa_is_legal_color_format(ctx, f) &&
774              !is_legal_depth_format(ctx, f)) {
775             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
776             fbo_incomplete("texture attachment incomplete", -1);
777             return;
778          }
779       }
780       else if (att->Type == GL_RENDERBUFFER_EXT) {
781          minWidth = MIN2(minWidth, att->Renderbuffer->Width);
782          maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
783          minHeight = MIN2(minHeight, att->Renderbuffer->Height);
784          maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
785          f = att->Renderbuffer->InternalFormat;
786          attFormat = att->Renderbuffer->Format;
787          numImages++;
788       }
789       else {
790          assert(att->Type == GL_NONE);
791          continue;
792       }
793
794       if (att->Renderbuffer && numSamples < 0) {
795          /* first buffer */
796          numSamples = att->Renderbuffer->NumSamples;
797       }
798
799       /* check if integer color */
800       fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
801
802       /* Error-check width, height, format, samples
803        */
804       if (numImages == 1) {
805          /* save format, num samples */
806          if (i >= 0) {
807             intFormat = f;
808          }
809       }
810       else {
811          if (!ctx->Extensions.ARB_framebuffer_object) {
812             /* check that width, height, format are same */
813             if (minWidth != maxWidth || minHeight != maxHeight) {
814                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
815                fbo_incomplete("width or height mismatch", -1);
816                return;
817             }
818             /* check that all color buffers are the same format */
819             if (intFormat != GL_NONE && f != intFormat) {
820                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
821                fbo_incomplete("format mismatch", -1);
822                return;
823             }
824          }
825          if (att->Renderbuffer &&
826              att->Renderbuffer->NumSamples != numSamples) {
827             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
828             fbo_incomplete("inconsistant number of samples", i);
829             return;
830          }
831       }
832
833       /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
834        */
835       if (att->Type == GL_RENDERBUFFER &&
836           att->Renderbuffer->Format == MESA_FORMAT_NONE) {
837          fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
838          fbo_incomplete("unsupported renderbuffer format", i);
839          return;
840       }
841    }
842
843    if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
844       /* Check that all DrawBuffers are present */
845       for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
846          if (fb->ColorDrawBuffer[j] != GL_NONE) {
847             const struct gl_renderbuffer_attachment *att
848                = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
849             assert(att);
850             if (att->Type == GL_NONE) {
851                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
852                fbo_incomplete("missing drawbuffer", j);
853                return;
854             }
855          }
856       }
857
858       /* Check that the ReadBuffer is present */
859       if (fb->ColorReadBuffer != GL_NONE) {
860          const struct gl_renderbuffer_attachment *att
861             = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
862          assert(att);
863          if (att->Type == GL_NONE) {
864             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
865             fbo_incomplete("missing readbuffer", -1);
866             return;
867          }
868       }
869    }
870
871    if (numImages == 0) {
872       fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
873       fbo_incomplete("no attachments", -1);
874       return;
875    }
876
877    /* Provisionally set status = COMPLETE ... */
878    fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
879
880    /* ... but the driver may say the FB is incomplete.
881     * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
882     * if anything.
883     */
884    if (ctx->Driver.ValidateFramebuffer) {
885       ctx->Driver.ValidateFramebuffer(ctx, fb);
886       if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
887          fbo_incomplete("driver marked FBO as incomplete", -1);
888       }
889    }
890
891    if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
892       /*
893        * Note that if ARB_framebuffer_object is supported and the attached
894        * renderbuffers/textures are different sizes, the framebuffer
895        * width/height will be set to the smallest width/height.
896        */
897       fb->Width = minWidth;
898       fb->Height = minHeight;
899
900       /* finally, update the visual info for the framebuffer */
901       _mesa_update_framebuffer_visual(ctx, fb);
902    }
903 }
904
905
906 GLboolean GLAPIENTRY
907 _mesa_IsRenderbuffer(GLuint renderbuffer)
908 {
909    GET_CURRENT_CONTEXT(ctx);
910    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
911    if (renderbuffer) {
912       struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
913       if (rb != NULL && rb != &DummyRenderbuffer)
914          return GL_TRUE;
915    }
916    return GL_FALSE;
917 }
918
919
920 void GLAPIENTRY
921 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
922 {
923    struct gl_renderbuffer *newRb;
924    GET_CURRENT_CONTEXT(ctx);
925
926    ASSERT_OUTSIDE_BEGIN_END(ctx);
927
928    if (target != GL_RENDERBUFFER_EXT) {
929       _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
930       return;
931    }
932
933    /* No need to flush here since the render buffer binding has no
934     * effect on rendering state.
935     */
936
937    if (renderbuffer) {
938       newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
939       if (newRb == &DummyRenderbuffer) {
940          /* ID was reserved, but no real renderbuffer object made yet */
941          newRb = NULL;
942       }
943       else if (!newRb
944                && _mesa_is_desktop_gl(ctx)
945                && ctx->Extensions.ARB_framebuffer_object) {
946          /* All RB IDs must be Gen'd */
947          _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
948          return;
949       }
950
951       if (!newRb) {
952          /* create new renderbuffer object */
953          newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
954          if (!newRb) {
955             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
956             return;
957          }
958          ASSERT(newRb->AllocStorage);
959          _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
960          newRb->RefCount = 1; /* referenced by hash table */
961       }
962    }
963    else {
964       newRb = NULL;
965    }
966
967    ASSERT(newRb != &DummyRenderbuffer);
968
969    _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
970 }
971
972
973 /**
974  * If the given renderbuffer is anywhere attached to the framebuffer, detach
975  * the renderbuffer.
976  * This is used when a renderbuffer object is deleted.
977  * The spec calls for unbinding.
978  */
979 static void
980 detach_renderbuffer(struct gl_context *ctx,
981                     struct gl_framebuffer *fb,
982                     struct gl_renderbuffer *rb)
983 {
984    GLuint i;
985    for (i = 0; i < BUFFER_COUNT; i++) {
986       if (fb->Attachment[i].Renderbuffer == rb) {
987          _mesa_remove_attachment(ctx, &fb->Attachment[i]);
988       }
989    }
990    invalidate_framebuffer(fb);
991 }
992
993
994 void GLAPIENTRY
995 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
996 {
997    GLint i;
998    GET_CURRENT_CONTEXT(ctx);
999
1000    ASSERT_OUTSIDE_BEGIN_END(ctx);
1001    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1002
1003    for (i = 0; i < n; i++) {
1004       if (renderbuffers[i] > 0) {
1005          struct gl_renderbuffer *rb;
1006          rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1007          if (rb) {
1008             /* check if deleting currently bound renderbuffer object */
1009             if (rb == ctx->CurrentRenderbuffer) {
1010                /* bind default */
1011                ASSERT(rb->RefCount >= 2);
1012                _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1013             }
1014
1015             if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1016                detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1017             }
1018             if (_mesa_is_user_fbo(ctx->ReadBuffer)
1019                 && ctx->ReadBuffer != ctx->DrawBuffer) {
1020                detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1021             }
1022
1023             /* Remove from hash table immediately, to free the ID.
1024              * But the object will not be freed until it's no longer
1025              * referenced anywhere else.
1026              */
1027             _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1028
1029             if (rb != &DummyRenderbuffer) {
1030                /* no longer referenced by hash table */
1031                _mesa_reference_renderbuffer(&rb, NULL);
1032             }
1033          }
1034       }
1035    }
1036 }
1037
1038
1039 void GLAPIENTRY
1040 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1041 {
1042    GET_CURRENT_CONTEXT(ctx);
1043    GLuint first;
1044    GLint i;
1045
1046    ASSERT_OUTSIDE_BEGIN_END(ctx);
1047
1048    if (n < 0) {
1049       _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1050       return;
1051    }
1052
1053    if (!renderbuffers)
1054       return;
1055
1056    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1057
1058    for (i = 0; i < n; i++) {
1059       GLuint name = first + i;
1060       renderbuffers[i] = name;
1061       /* insert dummy placeholder into hash table */
1062       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1063       _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1064       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1065    }
1066 }
1067
1068
1069 /**
1070  * Given an internal format token for a render buffer, return the
1071  * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1072  * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1073  * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1074  *
1075  * This is similar to _mesa_base_tex_format() but the set of valid
1076  * internal formats is different.
1077  *
1078  * Note that even if a format is determined to be legal here, validation
1079  * of the FBO may fail if the format is not supported by the driver/GPU.
1080  *
1081  * \param internalFormat  as passed to glRenderbufferStorage()
1082  * \return the base internal format, or 0 if internalFormat is illegal
1083  */
1084 GLenum
1085 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1086 {
1087    /*
1088     * Notes: some formats such as alpha, luminance, etc. were added
1089     * with GL_ARB_framebuffer_object.
1090     */
1091    switch (internalFormat) {
1092    case GL_ALPHA:
1093    case GL_ALPHA4:
1094    case GL_ALPHA8:
1095    case GL_ALPHA12:
1096    case GL_ALPHA16:
1097       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1098          ? GL_ALPHA : 0;
1099    case GL_LUMINANCE:
1100    case GL_LUMINANCE4:
1101    case GL_LUMINANCE8:
1102    case GL_LUMINANCE12:
1103    case GL_LUMINANCE16:
1104       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1105          ? GL_LUMINANCE : 0;
1106    case GL_LUMINANCE_ALPHA:
1107    case GL_LUMINANCE4_ALPHA4:
1108    case GL_LUMINANCE6_ALPHA2:
1109    case GL_LUMINANCE8_ALPHA8:
1110    case GL_LUMINANCE12_ALPHA4:
1111    case GL_LUMINANCE12_ALPHA12:
1112    case GL_LUMINANCE16_ALPHA16:
1113       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1114          ? GL_LUMINANCE_ALPHA : 0;
1115    case GL_INTENSITY:
1116    case GL_INTENSITY4:
1117    case GL_INTENSITY8:
1118    case GL_INTENSITY12:
1119    case GL_INTENSITY16:
1120       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1121          ? GL_INTENSITY : 0;
1122    case GL_RGB8:
1123       return GL_RGB;
1124    case GL_RGB:
1125    case GL_R3_G3_B2:
1126    case GL_RGB4:
1127    case GL_RGB5:
1128    case GL_RGB10:
1129    case GL_RGB12:
1130    case GL_RGB16:
1131       return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1132    case GL_SRGB8_EXT:
1133       return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1134    case GL_RGBA4:
1135    case GL_RGB5_A1:
1136    case GL_RGBA8:
1137       return GL_RGBA;
1138    case GL_RGBA:
1139    case GL_RGBA2:
1140    case GL_RGBA12:
1141    case GL_RGBA16:
1142       return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1143    case GL_RGB10_A2:
1144    case GL_SRGB8_ALPHA8_EXT:
1145       return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1146    case GL_STENCIL_INDEX:
1147    case GL_STENCIL_INDEX1_EXT:
1148    case GL_STENCIL_INDEX4_EXT:
1149    case GL_STENCIL_INDEX16_EXT:
1150       /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1151        * OpenGL ES, but Mesa does not currently support them.
1152        */
1153       return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1154    case GL_STENCIL_INDEX8_EXT:
1155       return GL_STENCIL_INDEX;
1156    case GL_DEPTH_COMPONENT:
1157    case GL_DEPTH_COMPONENT32:
1158       return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1159    case GL_DEPTH_COMPONENT16:
1160    case GL_DEPTH_COMPONENT24:
1161       return GL_DEPTH_COMPONENT;
1162    case GL_DEPTH_STENCIL_EXT:
1163       return _mesa_is_desktop_gl(ctx)
1164          && ctx->Extensions.EXT_packed_depth_stencil
1165          ? GL_DEPTH_STENCIL_EXT : 0;
1166    case GL_DEPTH24_STENCIL8_EXT:
1167       return ctx->Extensions.EXT_packed_depth_stencil
1168          ? GL_DEPTH_STENCIL_EXT : 0;
1169    case GL_DEPTH_COMPONENT32F:
1170       return ctx->Version >= 30
1171          || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1172          ? GL_DEPTH_COMPONENT : 0;
1173    case GL_DEPTH32F_STENCIL8:
1174       return ctx->Version >= 30
1175          || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1176          ? GL_DEPTH_STENCIL : 0;
1177    case GL_RED:
1178    case GL_R16:
1179       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1180          ? GL_RED : 0;
1181    case GL_R8:
1182       return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1183          ? GL_RED : 0;
1184    case GL_RG:
1185    case GL_RG16:
1186       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1187          ? GL_RG : 0;
1188    case GL_RG8:
1189       return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1190          ? GL_RG : 0;
1191    /* signed normalized texture formats */
1192    case GL_RED_SNORM:
1193    case GL_R8_SNORM:
1194    case GL_R16_SNORM:
1195       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1196          ? GL_RED : 0;
1197    case GL_RG_SNORM:
1198    case GL_RG8_SNORM:
1199    case GL_RG16_SNORM:
1200       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1201          ? GL_RG : 0;
1202    case GL_RGB_SNORM:
1203    case GL_RGB8_SNORM:
1204    case GL_RGB16_SNORM:
1205       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1206          ? GL_RGB : 0;
1207    case GL_RGBA_SNORM:
1208    case GL_RGBA8_SNORM:
1209    case GL_RGBA16_SNORM:
1210       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1211          ? GL_RGBA : 0;
1212    case GL_ALPHA_SNORM:
1213    case GL_ALPHA8_SNORM:
1214    case GL_ALPHA16_SNORM:
1215       return ctx->API == API_OPENGL_COMPAT &&
1216              ctx->Extensions.EXT_texture_snorm &&
1217              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1218    case GL_R16F:
1219    case GL_R32F:
1220       return ((_mesa_is_desktop_gl(ctx) &&
1221                ctx->Extensions.ARB_texture_rg &&
1222                ctx->Extensions.ARB_texture_float) ||
1223               _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1224          ? GL_RED : 0;
1225    case GL_RG16F:
1226    case GL_RG32F:
1227       return ((_mesa_is_desktop_gl(ctx) &&
1228                ctx->Extensions.ARB_texture_rg &&
1229                ctx->Extensions.ARB_texture_float) ||
1230               _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1231          ? GL_RG : 0;
1232    case GL_RGB16F:
1233    case GL_RGB32F:
1234       return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1235          ? GL_RGB : 0;
1236    case GL_RGBA16F:
1237    case GL_RGBA32F:
1238       return ((_mesa_is_desktop_gl(ctx) &&
1239                ctx->Extensions.ARB_texture_float) ||
1240               _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1241          ? GL_RGBA : 0;
1242    case GL_ALPHA16F_ARB:
1243    case GL_ALPHA32F_ARB:
1244       return ctx->API == API_OPENGL_COMPAT &&
1245              ctx->Extensions.ARB_texture_float &&
1246              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1247    case GL_LUMINANCE16F_ARB:
1248    case GL_LUMINANCE32F_ARB:
1249       return ctx->API == API_OPENGL_COMPAT &&
1250              ctx->Extensions.ARB_texture_float &&
1251              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1252    case GL_LUMINANCE_ALPHA16F_ARB:
1253    case GL_LUMINANCE_ALPHA32F_ARB:
1254       return ctx->API == API_OPENGL_COMPAT &&
1255              ctx->Extensions.ARB_texture_float &&
1256              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1257    case GL_INTENSITY16F_ARB:
1258    case GL_INTENSITY32F_ARB:
1259       return ctx->API == API_OPENGL_COMPAT &&
1260              ctx->Extensions.ARB_texture_float &&
1261              ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1262    case GL_RGB9_E5:
1263       return (_mesa_is_desktop_gl(ctx)
1264               && ctx->Extensions.EXT_texture_shared_exponent)
1265          ? GL_RGB : 0;
1266    case GL_R11F_G11F_B10F:
1267       return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1268               _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1269          ? GL_RGB : 0;
1270
1271    case GL_RGBA8UI_EXT:
1272    case GL_RGBA16UI_EXT:
1273    case GL_RGBA32UI_EXT:
1274    case GL_RGBA8I_EXT:
1275    case GL_RGBA16I_EXT:
1276    case GL_RGBA32I_EXT:
1277       return ctx->Version >= 30
1278          || (_mesa_is_desktop_gl(ctx) &&
1279              ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1280
1281    case GL_RGB8UI_EXT:
1282    case GL_RGB16UI_EXT:
1283    case GL_RGB32UI_EXT:
1284    case GL_RGB8I_EXT:
1285    case GL_RGB16I_EXT:
1286    case GL_RGB32I_EXT:
1287       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1288          ? GL_RGB : 0;
1289    case GL_R8UI:
1290    case GL_R8I:
1291    case GL_R16UI:
1292    case GL_R16I:
1293    case GL_R32UI:
1294    case GL_R32I:
1295       return ctx->Version >= 30
1296          || (_mesa_is_desktop_gl(ctx) &&
1297              ctx->Extensions.ARB_texture_rg &&
1298              ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1299
1300    case GL_RG8UI:
1301    case GL_RG8I:
1302    case GL_RG16UI:
1303    case GL_RG16I:
1304    case GL_RG32UI:
1305    case GL_RG32I:
1306       return ctx->Version >= 30
1307          || (_mesa_is_desktop_gl(ctx) &&
1308              ctx->Extensions.ARB_texture_rg &&
1309              ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1310
1311    case GL_INTENSITY8I_EXT:
1312    case GL_INTENSITY8UI_EXT:
1313    case GL_INTENSITY16I_EXT:
1314    case GL_INTENSITY16UI_EXT:
1315    case GL_INTENSITY32I_EXT:
1316    case GL_INTENSITY32UI_EXT:
1317       return ctx->API == API_OPENGL_COMPAT &&
1318              ctx->Extensions.EXT_texture_integer &&
1319              ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1320
1321    case GL_LUMINANCE8I_EXT:
1322    case GL_LUMINANCE8UI_EXT:
1323    case GL_LUMINANCE16I_EXT:
1324    case GL_LUMINANCE16UI_EXT:
1325    case GL_LUMINANCE32I_EXT:
1326    case GL_LUMINANCE32UI_EXT:
1327       return ctx->API == API_OPENGL_COMPAT &&
1328              ctx->Extensions.EXT_texture_integer &&
1329              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1330
1331    case GL_LUMINANCE_ALPHA8I_EXT:
1332    case GL_LUMINANCE_ALPHA8UI_EXT:
1333    case GL_LUMINANCE_ALPHA16I_EXT:
1334    case GL_LUMINANCE_ALPHA16UI_EXT:
1335    case GL_LUMINANCE_ALPHA32I_EXT:
1336    case GL_LUMINANCE_ALPHA32UI_EXT:
1337       return ctx->API == API_OPENGL_COMPAT &&
1338              ctx->Extensions.EXT_texture_integer &&
1339              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1340
1341    case GL_ALPHA8I_EXT:
1342    case GL_ALPHA8UI_EXT:
1343    case GL_ALPHA16I_EXT:
1344    case GL_ALPHA16UI_EXT:
1345    case GL_ALPHA32I_EXT:
1346    case GL_ALPHA32UI_EXT:
1347       return ctx->API == API_OPENGL_COMPAT &&
1348              ctx->Extensions.EXT_texture_integer &&
1349              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1350
1351    case GL_RGB10_A2UI:
1352       return (_mesa_is_desktop_gl(ctx) &&
1353               ctx->Extensions.ARB_texture_rgb10_a2ui)
1354          || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1355
1356    case GL_RGB565:
1357       return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1358          ? GL_RGB : 0;
1359    default:
1360       return 0;
1361    }
1362 }
1363
1364
1365 /**
1366  * Invalidate a renderbuffer attachment.  Called from _mesa_HashWalk().
1367  */
1368 static void
1369 invalidate_rb(GLuint key, void *data, void *userData)
1370 {
1371    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1372    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1373
1374    /* If this is a user-created FBO */
1375    if (_mesa_is_user_fbo(fb)) {
1376       GLuint i;
1377       for (i = 0; i < BUFFER_COUNT; i++) {
1378          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1379          if (att->Type == GL_RENDERBUFFER &&
1380              att->Renderbuffer == rb) {
1381             /* Mark fb status as indeterminate to force re-validation */
1382             fb->_Status = 0;
1383             return;
1384          }
1385       }
1386    }
1387 }
1388
1389
1390 /** sentinal value, see below */
1391 #define NO_SAMPLES 1000
1392
1393
1394 /**
1395  * Helper function used by _mesa_RenderbufferStorage() and 
1396  * _mesa_RenderbufferStorageMultisample().
1397  * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1398  */
1399 static void
1400 renderbuffer_storage(GLenum target, GLenum internalFormat,
1401                      GLsizei width, GLsizei height, GLsizei samples)
1402 {
1403    const char *func = samples == NO_SAMPLES ?
1404       "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1405    struct gl_renderbuffer *rb;
1406    GLenum baseFormat;
1407    GET_CURRENT_CONTEXT(ctx);
1408
1409    ASSERT_OUTSIDE_BEGIN_END(ctx);
1410
1411    if (MESA_VERBOSE & VERBOSE_API) {
1412       if (samples == NO_SAMPLES)
1413          _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1414                      func,
1415                      _mesa_lookup_enum_by_nr(target),
1416                      _mesa_lookup_enum_by_nr(internalFormat),
1417                      width, height);
1418       else
1419          _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1420                      func,
1421                      _mesa_lookup_enum_by_nr(target),
1422                      _mesa_lookup_enum_by_nr(internalFormat),
1423                      width, height, samples);
1424    }
1425
1426    if (target != GL_RENDERBUFFER_EXT) {
1427       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1428       return;
1429    }
1430
1431    baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1432    if (baseFormat == 0) {
1433       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
1434                   func, _mesa_lookup_enum_by_nr(internalFormat));
1435       return;
1436    }
1437
1438    if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1439       _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1440       return;
1441    }
1442
1443    if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1444       _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1445       return;
1446    }
1447
1448    if (samples == NO_SAMPLES) {
1449       /* NumSamples == 0 indicates non-multisampling */
1450       samples = 0;
1451    }
1452    else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1453       /* note: driver may choose to use more samples than what's requested */
1454       _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1455       return;
1456    }
1457
1458    rb = ctx->CurrentRenderbuffer;
1459    if (!rb) {
1460       _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1461       return;
1462    }
1463
1464    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1465
1466    if (rb->InternalFormat == internalFormat &&
1467        rb->Width == (GLuint) width &&
1468        rb->Height == (GLuint) height &&
1469        rb->NumSamples == samples) {
1470       /* no change in allocation needed */
1471       return;
1472    }
1473
1474    /* These MUST get set by the AllocStorage func */
1475    rb->Format = MESA_FORMAT_NONE;
1476    rb->NumSamples = samples;
1477
1478    /* Now allocate the storage */
1479    ASSERT(rb->AllocStorage);
1480    if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1481       /* No error - check/set fields now */
1482       /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1483       assert(rb->Width == (GLuint) width);
1484       assert(rb->Height == (GLuint) height);
1485       rb->InternalFormat = internalFormat;
1486       rb->_BaseFormat = baseFormat;
1487       assert(rb->_BaseFormat != 0);
1488    }
1489    else {
1490       /* Probably ran out of memory - clear the fields */
1491       rb->Width = 0;
1492       rb->Height = 0;
1493       rb->Format = MESA_FORMAT_NONE;
1494       rb->InternalFormat = GL_NONE;
1495       rb->_BaseFormat = GL_NONE;
1496       rb->NumSamples = 0;
1497    }
1498
1499    /* Invalidate the framebuffers the renderbuffer is attached in. */
1500    if (rb->AttachedAnytime) {
1501       _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1502    }
1503 }
1504
1505
1506 void GLAPIENTRY
1507 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1508 {
1509    struct gl_renderbuffer *rb;
1510    GET_CURRENT_CONTEXT(ctx);
1511    ASSERT_OUTSIDE_BEGIN_END(ctx);
1512
1513    if (!ctx->Extensions.OES_EGL_image) {
1514       _mesa_error(ctx, GL_INVALID_OPERATION,
1515                   "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1516       return;
1517    }
1518
1519    if (target != GL_RENDERBUFFER) {
1520       _mesa_error(ctx, GL_INVALID_ENUM,
1521                   "EGLImageTargetRenderbufferStorageOES");
1522       return;
1523    }
1524
1525    rb = ctx->CurrentRenderbuffer;
1526    if (!rb) {
1527       _mesa_error(ctx, GL_INVALID_OPERATION,
1528                   "EGLImageTargetRenderbufferStorageOES");
1529       return;
1530    }
1531
1532    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1533
1534    ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1535 }
1536
1537
1538 /**
1539  * Helper function for _mesa_GetRenderbufferParameteriv() and
1540  * _mesa_GetFramebufferAttachmentParameteriv()
1541  * We have to be careful to respect the base format.  For example, if a
1542  * renderbuffer/texture was created with internalFormat=GL_RGB but the
1543  * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1544  * we need to return zero.
1545  */
1546 static GLint
1547 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1548 {
1549    if (_mesa_base_format_has_channel(baseFormat, pname))
1550       return _mesa_get_format_bits(format, pname);
1551    else
1552       return 0;
1553 }
1554
1555
1556
1557 void GLAPIENTRY
1558 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1559                              GLsizei width, GLsizei height)
1560 {
1561    /* GL_ARB_fbo says calling this function is equivalent to calling
1562     * glRenderbufferStorageMultisample() with samples=0.  We pass in
1563     * a token value here just for error reporting purposes.
1564     */
1565    renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1566 }
1567
1568
1569 void GLAPIENTRY
1570 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1571                                      GLenum internalFormat,
1572                                      GLsizei width, GLsizei height)
1573 {
1574    renderbuffer_storage(target, internalFormat, width, height, samples);
1575 }
1576
1577
1578 /**
1579  * OpenGL ES version of glRenderBufferStorage.
1580  */
1581 void GLAPIENTRY
1582 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1583                            GLsizei width, GLsizei height)
1584 {
1585    switch (internalFormat) {
1586    case GL_RGB565:
1587       /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1588       /* choose a closest format */
1589       internalFormat = GL_RGB5;
1590       break;
1591    default:
1592       break;
1593    }
1594
1595    renderbuffer_storage(target, internalFormat, width, height, 0);
1596 }
1597
1598
1599 void GLAPIENTRY
1600 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1601 {
1602    struct gl_renderbuffer *rb;
1603    GET_CURRENT_CONTEXT(ctx);
1604
1605    ASSERT_OUTSIDE_BEGIN_END(ctx);
1606
1607    if (target != GL_RENDERBUFFER_EXT) {
1608       _mesa_error(ctx, GL_INVALID_ENUM,
1609                   "glGetRenderbufferParameterivEXT(target)");
1610       return;
1611    }
1612
1613    rb = ctx->CurrentRenderbuffer;
1614    if (!rb) {
1615       _mesa_error(ctx, GL_INVALID_OPERATION,
1616                   "glGetRenderbufferParameterivEXT");
1617       return;
1618    }
1619
1620    /* No need to flush here since we're just quering state which is
1621     * not effected by rendering.
1622     */
1623
1624    switch (pname) {
1625    case GL_RENDERBUFFER_WIDTH_EXT:
1626       *params = rb->Width;
1627       return;
1628    case GL_RENDERBUFFER_HEIGHT_EXT:
1629       *params = rb->Height;
1630       return;
1631    case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1632       *params = rb->InternalFormat;
1633       return;
1634    case GL_RENDERBUFFER_RED_SIZE_EXT:
1635    case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1636    case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1637    case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1638    case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1639    case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1640       *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1641       break;
1642    case GL_RENDERBUFFER_SAMPLES:
1643       if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1644           || _mesa_is_gles3(ctx)) {
1645          *params = rb->NumSamples;
1646          break;
1647       }
1648       /* fallthrough */
1649    default:
1650       _mesa_error(ctx, GL_INVALID_ENUM,
1651                   "glGetRenderbufferParameterivEXT(target)");
1652       return;
1653    }
1654 }
1655
1656
1657 GLboolean GLAPIENTRY
1658 _mesa_IsFramebuffer(GLuint framebuffer)
1659 {
1660    GET_CURRENT_CONTEXT(ctx);
1661    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1662    if (framebuffer) {
1663       struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1664       if (rb != NULL && rb != &DummyFramebuffer)
1665          return GL_TRUE;
1666    }
1667    return GL_FALSE;
1668 }
1669
1670
1671 /**
1672  * Check if any of the attachments of the given framebuffer are textures
1673  * (render to texture).  Call ctx->Driver.RenderTexture() for such
1674  * attachments.
1675  */
1676 static void
1677 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1678 {
1679    GLuint i;
1680    ASSERT(ctx->Driver.RenderTexture);
1681
1682    if (_mesa_is_winsys_fbo(fb))
1683       return; /* can't render to texture with winsys framebuffers */
1684
1685    for (i = 0; i < BUFFER_COUNT; i++) {
1686       struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1687       if (att->Texture && _mesa_get_attachment_teximage(att)) {
1688          ctx->Driver.RenderTexture(ctx, fb, att);
1689       }
1690    }
1691 }
1692
1693
1694 /**
1695  * Examine all the framebuffer's attachments to see if any are textures.
1696  * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1697  * notify the device driver that the texture image may have changed.
1698  */
1699 static void
1700 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1701 {
1702    if (_mesa_is_winsys_fbo(fb))
1703       return; /* can't render to texture with winsys framebuffers */
1704
1705    if (ctx->Driver.FinishRenderTexture) {
1706       GLuint i;
1707       for (i = 0; i < BUFFER_COUNT; i++) {
1708          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1709          if (att->Texture && att->Renderbuffer) {
1710             ctx->Driver.FinishRenderTexture(ctx, att);
1711          }
1712       }
1713    }
1714 }
1715
1716
1717 void GLAPIENTRY
1718 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1719 {
1720    struct gl_framebuffer *newDrawFb, *newReadFb;
1721    struct gl_framebuffer *oldDrawFb, *oldReadFb;
1722    GLboolean bindReadBuf, bindDrawBuf;
1723    GET_CURRENT_CONTEXT(ctx);
1724
1725 #ifdef DEBUG
1726    if (ctx->Extensions.ARB_framebuffer_object) {
1727       ASSERT(ctx->Extensions.EXT_framebuffer_object);
1728       ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1729    }
1730 #endif
1731
1732    ASSERT_OUTSIDE_BEGIN_END(ctx);
1733
1734    if (!ctx->Extensions.EXT_framebuffer_object) {
1735       _mesa_error(ctx, GL_INVALID_OPERATION,
1736                   "glBindFramebufferEXT(unsupported)");
1737       return;
1738    }
1739
1740    switch (target) {
1741    case GL_DRAW_FRAMEBUFFER_EXT:
1742       if (!ctx->Extensions.EXT_framebuffer_blit) {
1743          _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1744          return;
1745       }
1746       bindDrawBuf = GL_TRUE;
1747       bindReadBuf = GL_FALSE;
1748       break;
1749    case GL_READ_FRAMEBUFFER_EXT:
1750       if (!ctx->Extensions.EXT_framebuffer_blit) {
1751          _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1752          return;
1753       }
1754       bindDrawBuf = GL_FALSE;
1755       bindReadBuf = GL_TRUE;
1756       break;
1757    case GL_FRAMEBUFFER_EXT:
1758       bindDrawBuf = GL_TRUE;
1759       bindReadBuf = GL_TRUE;
1760       break;
1761    default:
1762       _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1763       return;
1764    }
1765
1766    if (framebuffer) {
1767       /* Binding a user-created framebuffer object */
1768       newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1769       if (newDrawFb == &DummyFramebuffer) {
1770          /* ID was reserved, but no real framebuffer object made yet */
1771          newDrawFb = NULL;
1772       }
1773       else if (!newDrawFb
1774                && _mesa_is_desktop_gl(ctx)
1775                && ctx->Extensions.ARB_framebuffer_object) {
1776          /* All FBO IDs must be Gen'd */
1777          _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1778          return;
1779       }
1780
1781       if (!newDrawFb) {
1782          /* create new framebuffer object */
1783          newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1784          if (!newDrawFb) {
1785             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1786             return;
1787          }
1788          _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1789       }
1790       newReadFb = newDrawFb;
1791    }
1792    else {
1793       /* Binding the window system framebuffer (which was originally set
1794        * with MakeCurrent).
1795        */
1796       newDrawFb = ctx->WinSysDrawBuffer;
1797       newReadFb = ctx->WinSysReadBuffer;
1798    }
1799
1800    ASSERT(newDrawFb);
1801    ASSERT(newDrawFb != &DummyFramebuffer);
1802
1803    /* save pointers to current/old framebuffers */
1804    oldDrawFb = ctx->DrawBuffer;
1805    oldReadFb = ctx->ReadBuffer;
1806
1807    /* check if really changing bindings */
1808    if (oldDrawFb == newDrawFb)
1809       bindDrawBuf = GL_FALSE;
1810    if (oldReadFb == newReadFb)
1811       bindReadBuf = GL_FALSE;
1812
1813    /*
1814     * OK, now bind the new Draw/Read framebuffers, if they're changing.
1815     *
1816     * We also check if we're beginning and/or ending render-to-texture.
1817     * When a framebuffer with texture attachments is unbound, call
1818     * ctx->Driver.FinishRenderTexture().
1819     * When a framebuffer with texture attachments is bound, call
1820     * ctx->Driver.RenderTexture().
1821     *
1822     * Note that if the ReadBuffer has texture attachments we don't consider
1823     * that a render-to-texture case.
1824     */
1825    if (bindReadBuf) {
1826       FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1827
1828       /* check if old readbuffer was render-to-texture */
1829       check_end_texture_render(ctx, oldReadFb);
1830
1831       _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1832    }
1833
1834    if (bindDrawBuf) {
1835       FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1836
1837       /* check if old framebuffer had any texture attachments */
1838       if (oldDrawFb)
1839          check_end_texture_render(ctx, oldDrawFb);
1840
1841       /* check if newly bound framebuffer has any texture attachments */
1842       check_begin_texture_render(ctx, newDrawFb);
1843
1844       _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1845    }
1846
1847    if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1848       ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1849    }
1850 }
1851
1852
1853 void GLAPIENTRY
1854 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1855 {
1856    GLint i;
1857    GET_CURRENT_CONTEXT(ctx);
1858
1859    ASSERT_OUTSIDE_BEGIN_END(ctx);
1860    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1861
1862    for (i = 0; i < n; i++) {
1863       if (framebuffers[i] > 0) {
1864          struct gl_framebuffer *fb;
1865          fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1866          if (fb) {
1867             ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1868
1869             /* check if deleting currently bound framebuffer object */
1870             if (ctx->Extensions.EXT_framebuffer_blit) {
1871                /* separate draw/read binding points */
1872                if (fb == ctx->DrawBuffer) {
1873                   /* bind default */
1874                   ASSERT(fb->RefCount >= 2);
1875                   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1876                }
1877                if (fb == ctx->ReadBuffer) {
1878                   /* bind default */
1879                   ASSERT(fb->RefCount >= 2);
1880                   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1881                }
1882             }
1883             else {
1884                /* only one binding point for read/draw buffers */
1885                if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1886                   /* bind default */
1887                   ASSERT(fb->RefCount >= 2);
1888                   _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1889                }
1890             }
1891
1892             /* remove from hash table immediately, to free the ID */
1893             _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1894
1895             if (fb != &DummyFramebuffer) {
1896                /* But the object will not be freed until it's no longer
1897                 * bound in any context.
1898                 */
1899                _mesa_reference_framebuffer(&fb, NULL);
1900             }
1901          }
1902       }
1903    }
1904 }
1905
1906
1907 void GLAPIENTRY
1908 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1909 {
1910    GET_CURRENT_CONTEXT(ctx);
1911    GLuint first;
1912    GLint i;
1913
1914    ASSERT_OUTSIDE_BEGIN_END(ctx);
1915
1916    if (n < 0) {
1917       _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1918       return;
1919    }
1920
1921    if (!framebuffers)
1922       return;
1923
1924    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1925
1926    for (i = 0; i < n; i++) {
1927       GLuint name = first + i;
1928       framebuffers[i] = name;
1929       /* insert dummy placeholder into hash table */
1930       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1931       _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1932       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1933    }
1934 }
1935
1936
1937
1938 GLenum GLAPIENTRY
1939 _mesa_CheckFramebufferStatus(GLenum target)
1940 {
1941    struct gl_framebuffer *buffer;
1942    GET_CURRENT_CONTEXT(ctx);
1943
1944    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1945
1946    if (MESA_VERBOSE & VERBOSE_API)
1947       _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1948                   _mesa_lookup_enum_by_nr(target));
1949
1950    buffer = get_framebuffer_target(ctx, target);
1951    if (!buffer) {
1952       _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1953       return 0;
1954    }
1955
1956    if (_mesa_is_winsys_fbo(buffer)) {
1957       /* The window system / default framebuffer is always complete */
1958       return GL_FRAMEBUFFER_COMPLETE_EXT;
1959    }
1960
1961    /* No need to flush here */
1962
1963    if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1964       _mesa_test_framebuffer_completeness(ctx, buffer);
1965    }
1966
1967    return buffer->_Status;
1968 }
1969
1970
1971 /**
1972  * Replicate the src attachment point. Used by framebuffer_texture() when
1973  * the same texture is attached at GL_DEPTH_ATTACHMENT and
1974  * GL_STENCIL_ATTACHMENT.
1975  */
1976 static void
1977 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
1978                                      gl_buffer_index dst,
1979                                      gl_buffer_index src)
1980 {
1981    struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
1982    struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
1983
1984    assert(src_att->Texture != NULL);
1985    assert(src_att->Renderbuffer != NULL);
1986
1987    _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
1988    _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
1989    dst_att->Type = src_att->Type;
1990    dst_att->Complete = src_att->Complete;
1991    dst_att->TextureLevel = src_att->TextureLevel;
1992    dst_att->Zoffset = src_att->Zoffset;
1993 }
1994
1995
1996 /**
1997  * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1998  * glFramebufferTextureLayerEXT().
1999  * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
2000  * get textarget=0 in that case.
2001  */
2002 static void
2003 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target, 
2004                     GLenum attachment, GLenum textarget, GLuint texture,
2005                     GLint level, GLint zoffset)
2006 {
2007    struct gl_renderbuffer_attachment *att;
2008    struct gl_texture_object *texObj = NULL;
2009    struct gl_framebuffer *fb;
2010    GLenum maxLevelsTarget;
2011
2012    ASSERT_OUTSIDE_BEGIN_END(ctx);
2013
2014    fb = get_framebuffer_target(ctx, target);
2015    if (!fb) {
2016       _mesa_error(ctx, GL_INVALID_ENUM,
2017                   "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
2018       return;
2019    }
2020
2021    /* check framebuffer binding */
2022    if (_mesa_is_winsys_fbo(fb)) {
2023       _mesa_error(ctx, GL_INVALID_OPERATION,
2024                   "glFramebufferTexture%sEXT", caller);
2025       return;
2026    }
2027
2028    /* The textarget, level, and zoffset parameters are only validated if
2029     * texture is non-zero.
2030     */
2031    if (texture) {
2032       GLboolean err = GL_TRUE;
2033
2034       texObj = _mesa_lookup_texture(ctx, texture);
2035       if (texObj != NULL) {
2036          if (textarget == 0) {
2037             /* If textarget == 0 it means we're being called by
2038              * glFramebufferTextureLayer() and textarget is not used.
2039              * The only legal texture types for that function are 3D and
2040              * 1D/2D arrays textures.
2041              */
2042             err = (texObj->Target != GL_TEXTURE_3D) &&
2043                 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2044                 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2045                 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2046          }
2047          else {
2048             /* Make sure textarget is consistent with the texture's type */
2049             err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2050                 ? !_mesa_is_cube_face(textarget)
2051                 : (texObj->Target != textarget);
2052          }
2053       }
2054       else {
2055          /* can't render to a non-existant texture */
2056          _mesa_error(ctx, GL_INVALID_OPERATION,
2057                      "glFramebufferTexture%sEXT(non existant texture)",
2058                      caller);
2059          return;
2060       }
2061
2062       if (err) {
2063          _mesa_error(ctx, GL_INVALID_OPERATION,
2064                      "glFramebufferTexture%sEXT(texture target mismatch)",
2065                      caller);
2066          return;
2067       }
2068
2069       if (texObj->Target == GL_TEXTURE_3D) {
2070          const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2071          if (zoffset < 0 || zoffset >= maxSize) {
2072             _mesa_error(ctx, GL_INVALID_VALUE,
2073                         "glFramebufferTexture%sEXT(zoffset)", caller);
2074             return;
2075          }
2076       }
2077       else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2078                (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2079                (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2080          if (zoffset < 0 ||
2081              zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2082             _mesa_error(ctx, GL_INVALID_VALUE,
2083                         "glFramebufferTexture%sEXT(layer)", caller);
2084             return;
2085          }
2086       }
2087
2088       maxLevelsTarget = textarget ? textarget : texObj->Target;
2089       if ((level < 0) ||
2090           (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2091          _mesa_error(ctx, GL_INVALID_VALUE,
2092                      "glFramebufferTexture%sEXT(level)", caller);
2093          return;
2094       }
2095    }
2096
2097    att = _mesa_get_attachment(ctx, fb, attachment);
2098    if (att == NULL) {
2099       _mesa_error(ctx, GL_INVALID_ENUM,
2100                   "glFramebufferTexture%sEXT(attachment)", caller);
2101       return;
2102    }
2103
2104    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2105
2106    _glthread_LOCK_MUTEX(fb->Mutex);
2107    if (texObj) {
2108       if (attachment == GL_DEPTH_ATTACHMENT &&
2109           texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2110           level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2111           _mesa_tex_target_to_face(textarget) ==
2112           fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2113           zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2114          /* The texture object is already attached to the stencil attachment
2115           * point. Don't create a new renderbuffer; just reuse the stencil
2116           * attachment's. This is required to prevent a GL error in
2117           * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2118           */
2119          reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2120                                               BUFFER_STENCIL);
2121       } else if (attachment == GL_STENCIL_ATTACHMENT &&
2122                  texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2123                  level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2124                  _mesa_tex_target_to_face(textarget) ==
2125                  fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2126                  zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2127          /* As above, but with depth and stencil transposed. */
2128          reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2129                                               BUFFER_DEPTH);
2130       } else {
2131          _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2132                                       level, zoffset);
2133          if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2134             /* Above we created a new renderbuffer and attached it to the
2135              * depth attachment point. Now attach it to the stencil attachment
2136              * point too.
2137              */
2138             assert(att == &fb->Attachment[BUFFER_DEPTH]);
2139             reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2140                                                  BUFFER_DEPTH);
2141          }
2142       }
2143
2144       /* Set the render-to-texture flag.  We'll check this flag in
2145        * glTexImage() and friends to determine if we need to revalidate
2146        * any FBOs that might be rendering into this texture.
2147        * This flag never gets cleared since it's non-trivial to determine
2148        * when all FBOs might be done rendering to this texture.  That's OK
2149        * though since it's uncommon to render to a texture then repeatedly
2150        * call glTexImage() to change images in the texture.
2151        */
2152       texObj->_RenderToTexture = GL_TRUE;
2153    }
2154    else {
2155       _mesa_remove_attachment(ctx, att);
2156       if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2157          assert(att == &fb->Attachment[BUFFER_DEPTH]);
2158          _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2159       }
2160    }
2161
2162    invalidate_framebuffer(fb);
2163
2164    _glthread_UNLOCK_MUTEX(fb->Mutex);
2165 }
2166
2167
2168
2169 void GLAPIENTRY
2170 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2171                               GLenum textarget, GLuint texture, GLint level)
2172 {
2173    GET_CURRENT_CONTEXT(ctx);
2174
2175    if (texture != 0) {
2176       GLboolean error;
2177
2178       switch (textarget) {
2179       case GL_TEXTURE_1D:
2180          error = GL_FALSE;
2181          break;
2182       case GL_TEXTURE_1D_ARRAY:
2183          error = !ctx->Extensions.EXT_texture_array;
2184          break;
2185       default:
2186          error = GL_TRUE;
2187       }
2188
2189       if (error) {
2190          _mesa_error(ctx, GL_INVALID_OPERATION,
2191                      "glFramebufferTexture1DEXT(textarget=%s)",
2192                      _mesa_lookup_enum_by_nr(textarget));
2193          return;
2194       }
2195    }
2196
2197    framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2198                        level, 0);
2199 }
2200
2201
2202 void GLAPIENTRY
2203 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2204                               GLenum textarget, GLuint texture, GLint level)
2205 {
2206    GET_CURRENT_CONTEXT(ctx);
2207
2208    if (texture != 0) {
2209       GLboolean error;
2210
2211       switch (textarget) {
2212       case GL_TEXTURE_2D:
2213          error = GL_FALSE;
2214          break;
2215       case GL_TEXTURE_RECTANGLE:
2216          error = _mesa_is_gles(ctx)
2217             || !ctx->Extensions.NV_texture_rectangle;
2218          break;
2219       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2220       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2221       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2222       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2223       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2224       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2225          error = !ctx->Extensions.ARB_texture_cube_map;
2226          break;
2227       case GL_TEXTURE_2D_ARRAY:
2228          error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2229             || !ctx->Extensions.EXT_texture_array;
2230          break;
2231       default:
2232          error = GL_TRUE;
2233       }
2234
2235       if (error) {
2236          _mesa_error(ctx, GL_INVALID_OPERATION,
2237                      "glFramebufferTexture2DEXT(textarget=%s)",
2238                      _mesa_lookup_enum_by_nr(textarget));
2239          return;
2240       }
2241    }
2242
2243    framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2244                        level, 0);
2245 }
2246
2247
2248 void GLAPIENTRY
2249 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2250                               GLenum textarget, GLuint texture,
2251                               GLint level, GLint zoffset)
2252 {
2253    GET_CURRENT_CONTEXT(ctx);
2254
2255    if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2256       _mesa_error(ctx, GL_INVALID_OPERATION,
2257                   "glFramebufferTexture3DEXT(textarget)");
2258       return;
2259    }
2260
2261    framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2262                        level, zoffset);
2263 }
2264
2265
2266 void GLAPIENTRY
2267 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2268                                  GLuint texture, GLint level, GLint layer)
2269 {
2270    GET_CURRENT_CONTEXT(ctx);
2271
2272    framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2273                        level, layer);
2274 }
2275
2276
2277 void GLAPIENTRY
2278 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2279                                  GLenum renderbufferTarget,
2280                                  GLuint renderbuffer)
2281 {
2282    struct gl_renderbuffer_attachment *att;
2283    struct gl_framebuffer *fb;
2284    struct gl_renderbuffer *rb;
2285    GET_CURRENT_CONTEXT(ctx);
2286
2287    ASSERT_OUTSIDE_BEGIN_END(ctx);
2288
2289    fb = get_framebuffer_target(ctx, target);
2290    if (!fb) {
2291       _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2292       return;
2293    }
2294
2295    if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2296       _mesa_error(ctx, GL_INVALID_ENUM,
2297                   "glFramebufferRenderbufferEXT(renderbufferTarget)");
2298       return;
2299    }
2300
2301    if (_mesa_is_winsys_fbo(fb)) {
2302       /* Can't attach new renderbuffers to a window system framebuffer */
2303       _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2304       return;
2305    }
2306
2307    att = _mesa_get_attachment(ctx, fb, attachment);
2308    if (att == NULL) {
2309       _mesa_error(ctx, GL_INVALID_ENUM,
2310                   "glFramebufferRenderbufferEXT(invalid attachment %s)",
2311                   _mesa_lookup_enum_by_nr(attachment));
2312       return;
2313    }
2314
2315    if (renderbuffer) {
2316       rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2317       if (!rb) {
2318          _mesa_error(ctx, GL_INVALID_OPERATION,
2319                      "glFramebufferRenderbufferEXT(non-existant"
2320                      " renderbuffer %u)", renderbuffer);
2321          return;
2322       }
2323       else if (rb == &DummyRenderbuffer) {
2324          /* This is what NVIDIA does */
2325          _mesa_error(ctx, GL_INVALID_VALUE,
2326                      "glFramebufferRenderbufferEXT(renderbuffer %u)",
2327                      renderbuffer);
2328          return;
2329       }
2330    }
2331    else {
2332       /* remove renderbuffer attachment */
2333       rb = NULL;
2334    }
2335
2336    if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2337        rb && rb->Format != MESA_FORMAT_NONE) {
2338       /* make sure the renderbuffer is a depth/stencil format */
2339       const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2340       if (baseFormat != GL_DEPTH_STENCIL) {
2341          _mesa_error(ctx, GL_INVALID_OPERATION,
2342                      "glFramebufferRenderbufferEXT(renderbuffer"
2343                      " is not DEPTH_STENCIL format)");
2344          return;
2345       }
2346    }
2347
2348
2349    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2350
2351    assert(ctx->Driver.FramebufferRenderbuffer);
2352    ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2353
2354    /* Some subsequent GL commands may depend on the framebuffer's visual
2355     * after the binding is updated.  Update visual info now.
2356     */
2357    _mesa_update_framebuffer_visual(ctx, fb);
2358 }
2359
2360
2361 void GLAPIENTRY
2362 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2363                                              GLenum pname, GLint *params)
2364 {
2365    const struct gl_renderbuffer_attachment *att;
2366    struct gl_framebuffer *buffer;
2367    GLenum err;
2368    GET_CURRENT_CONTEXT(ctx);
2369
2370    ASSERT_OUTSIDE_BEGIN_END(ctx);
2371
2372    /* The error differs in GL and GLES. */
2373    err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2374
2375    buffer = get_framebuffer_target(ctx, target);
2376    if (!buffer) {
2377       _mesa_error(ctx, GL_INVALID_ENUM,
2378                   "glGetFramebufferAttachmentParameterivEXT(target)");
2379       return;
2380    }
2381
2382    if (_mesa_is_winsys_fbo(buffer)) {
2383       /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2384        * says:
2385        *
2386        *     "If the framebuffer currently bound to target is zero, then
2387        *     INVALID_OPERATION is generated."
2388        *
2389        * The EXT_framebuffer_object spec has the same wording, and the
2390        * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2391        * spec.
2392        */
2393       if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2394           && !_mesa_is_gles3(ctx)) {
2395          _mesa_error(ctx, GL_INVALID_OPERATION,
2396                      "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2397          return;
2398       }
2399
2400       if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
2401           attachment != GL_DEPTH && attachment != GL_STENCIL) {
2402          _mesa_error(ctx, GL_INVALID_OPERATION,
2403                      "glGetFramebufferAttachmentParameteriv(attachment)");
2404          return;
2405       }
2406       /* the default / window-system FBO */
2407       att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2408    }
2409    else {
2410       /* user-created framebuffer FBO */
2411       att = _mesa_get_attachment(ctx, buffer, attachment);
2412    }
2413
2414    if (att == NULL) {
2415       _mesa_error(ctx, GL_INVALID_ENUM,
2416                   "glGetFramebufferAttachmentParameterivEXT(attachment)");
2417       return;
2418    }
2419
2420    if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2421       /* the depth and stencil attachments must point to the same buffer */
2422       const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2423       depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2424       stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2425       if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2426          _mesa_error(ctx, GL_INVALID_OPERATION,
2427                      "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2428                      " attachments differ)");
2429          return;
2430       }
2431    }
2432
2433    /* No need to flush here */
2434
2435    switch (pname) {
2436    case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2437       *params = _mesa_is_winsys_fbo(buffer)
2438          ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2439       return;
2440    case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2441       if (att->Type == GL_RENDERBUFFER_EXT) {
2442          *params = att->Renderbuffer->Name;
2443       }
2444       else if (att->Type == GL_TEXTURE) {
2445          *params = att->Texture->Name;
2446       }
2447       else {
2448          assert(att->Type == GL_NONE);
2449          if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2450             *params = 0;
2451          } else {
2452             goto invalid_pname_enum;
2453          }
2454       }
2455       return;
2456    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2457       if (att->Type == GL_TEXTURE) {
2458          *params = att->TextureLevel;
2459       }
2460       else if (att->Type == GL_NONE) {
2461          _mesa_error(ctx, err,
2462                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2463       }
2464       else {
2465          goto invalid_pname_enum;
2466       }
2467       return;
2468    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2469       if (att->Type == GL_TEXTURE) {
2470          if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2471             *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2472          }
2473          else {
2474             *params = 0;
2475          }
2476       }
2477       else if (att->Type == GL_NONE) {
2478          _mesa_error(ctx, err,
2479                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2480       }
2481       else {
2482          goto invalid_pname_enum;
2483       }
2484       return;
2485    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2486       if (ctx->API == API_OPENGLES) {
2487          goto invalid_pname_enum;
2488       } else if (att->Type == GL_NONE) {
2489          _mesa_error(ctx, err,
2490                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2491       } else if (att->Type == GL_TEXTURE) {
2492          if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2493             *params = att->Zoffset;
2494          }
2495          else {
2496             *params = 0;
2497          }
2498       }
2499       else {
2500          goto invalid_pname_enum;
2501       }
2502       return;
2503    case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2504       if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2505           && !_mesa_is_gles3(ctx)) {
2506          goto invalid_pname_enum;
2507       }
2508       else if (att->Type == GL_NONE) {
2509          _mesa_error(ctx, err,
2510                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2511       }
2512       else {
2513          if (ctx->Extensions.EXT_framebuffer_sRGB) {
2514             *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2515          }
2516          else {
2517             /* According to ARB_framebuffer_sRGB, we should return LINEAR
2518              * if the sRGB conversion is unsupported. */
2519             *params = GL_LINEAR;
2520          }
2521       }
2522       return;
2523    case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2524       if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2525           && ctx->API != API_OPENGL_CORE
2526           && !_mesa_is_gles3(ctx)) {
2527          goto invalid_pname_enum;
2528       }
2529       else if (att->Type == GL_NONE) {
2530          _mesa_error(ctx, err,
2531                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2532       }
2533       else {
2534          gl_format format = att->Renderbuffer->Format;
2535          if (format == MESA_FORMAT_S8) {
2536             /* special cases */
2537             *params = GL_INDEX;
2538          }
2539          else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2540             /* depends on the attachment parameter */
2541             if (attachment == GL_STENCIL_ATTACHMENT) {
2542                *params = GL_INDEX;
2543             }
2544             else {
2545                *params = GL_FLOAT;
2546             }
2547          }
2548          else {
2549             *params = _mesa_get_format_datatype(format);
2550          }
2551       }
2552       return;
2553    case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2554    case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2555    case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2556    case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2557    case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2558    case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2559       if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2560           && !_mesa_is_gles3(ctx)) {
2561          goto invalid_pname_enum;
2562       }
2563       else if (att->Type == GL_NONE) {
2564          _mesa_error(ctx, err,
2565                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2566       }
2567       else if (att->Texture) {
2568          const struct gl_texture_image *texImage =
2569             _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2570                                    att->TextureLevel);
2571          if (texImage) {
2572             *params = get_component_bits(pname, texImage->_BaseFormat,
2573                                          texImage->TexFormat);
2574          }
2575          else {
2576             *params = 0;
2577          }
2578       }
2579       else if (att->Renderbuffer) {
2580          *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2581                                       att->Renderbuffer->Format);
2582       }
2583       else {
2584          _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2585                        " invalid FBO attachment structure");
2586       }
2587       return;
2588    default:
2589       goto invalid_pname_enum;
2590    }
2591
2592    return;
2593
2594 invalid_pname_enum:
2595    _mesa_error(ctx, GL_INVALID_ENUM,
2596                "glGetFramebufferAttachmentParameteriv(pname)");
2597    return;
2598 }
2599
2600
2601 void GLAPIENTRY
2602 _mesa_GenerateMipmap(GLenum target)
2603 {
2604    struct gl_texture_image *srcImage;
2605    struct gl_texture_object *texObj;
2606    GLboolean error;
2607
2608    GET_CURRENT_CONTEXT(ctx);
2609
2610    ASSERT_OUTSIDE_BEGIN_END(ctx);
2611    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2612
2613    switch (target) {
2614    case GL_TEXTURE_1D:
2615       error = _mesa_is_gles(ctx);
2616       break;
2617    case GL_TEXTURE_2D:
2618       error = GL_FALSE;
2619       break;
2620    case GL_TEXTURE_3D:
2621       error = ctx->API == API_OPENGLES;
2622       break;
2623    case GL_TEXTURE_CUBE_MAP:
2624       error = !ctx->Extensions.ARB_texture_cube_map;
2625       break;
2626    case GL_TEXTURE_1D_ARRAY:
2627       error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2628       break;
2629    case GL_TEXTURE_2D_ARRAY:
2630       error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2631          || !ctx->Extensions.EXT_texture_array;
2632       break;
2633    default:
2634       error = GL_TRUE;
2635    }
2636
2637    if (error) {
2638       _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2639                   _mesa_lookup_enum_by_nr(target));
2640       return;
2641    }
2642
2643    texObj = _mesa_get_current_tex_object(ctx, target);
2644
2645    if (texObj->BaseLevel >= texObj->MaxLevel) {
2646       /* nothing to do */
2647       return;
2648    }
2649
2650    if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2651        !_mesa_cube_complete(texObj)) {
2652       _mesa_error(ctx, GL_INVALID_OPERATION,
2653                   "glGenerateMipmap(incomplete cube map)");
2654       return;
2655    }
2656
2657    _mesa_lock_texture(ctx, texObj);
2658
2659    srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2660    if (!srcImage) {
2661       _mesa_unlock_texture(ctx, texObj);
2662       _mesa_error(ctx, GL_INVALID_OPERATION,
2663                   "glGenerateMipmap(zero size base image)");
2664       return;
2665    }
2666
2667    if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2668        _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2669        _mesa_is_stencil_format(srcImage->InternalFormat)) {
2670       _mesa_unlock_texture(ctx, texObj);
2671       _mesa_error(ctx, GL_INVALID_OPERATION,
2672                   "glGenerateMipmap(invalid internal format)");
2673       return;
2674    }
2675
2676    if (target == GL_TEXTURE_CUBE_MAP) {
2677       GLuint face;
2678       for (face = 0; face < 6; face++)
2679          ctx->Driver.GenerateMipmap(ctx,
2680                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2681                                     texObj);
2682    }
2683    else {
2684       ctx->Driver.GenerateMipmap(ctx, target, texObj);
2685    }
2686    _mesa_unlock_texture(ctx, texObj);
2687 }
2688
2689
2690 static const struct gl_renderbuffer_attachment *
2691 find_attachment(const struct gl_framebuffer *fb,
2692                 const struct gl_renderbuffer *rb)
2693 {
2694    GLuint i;
2695    for (i = 0; i < Elements(fb->Attachment); i++) {
2696       if (fb->Attachment[i].Renderbuffer == rb)
2697          return &fb->Attachment[i];
2698    }
2699    return NULL;
2700 }
2701
2702
2703 /**
2704  * Helper function for checking if the datatypes of color buffers are
2705  * compatible for glBlitFramebuffer.  From the 3.1 spec, page 198:
2706  *
2707  * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2708  *  and any of the following conditions hold:
2709  *   - The read buffer contains fixed-point or floating-point values and any
2710  *     draw buffer contains neither fixed-point nor floating-point values.
2711  *   - The read buffer contains unsigned integer values and any draw buffer
2712  *     does not contain unsigned integer values.
2713  *   - The read buffer contains signed integer values and any draw buffer
2714  *     does not contain signed integer values."
2715  */
2716 static GLboolean
2717 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2718 {
2719    GLenum srcType = _mesa_get_format_datatype(srcFormat);
2720    GLenum dstType = _mesa_get_format_datatype(dstFormat);
2721
2722    if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2723       assert(srcType == GL_UNSIGNED_NORMALIZED ||
2724              srcType == GL_SIGNED_NORMALIZED ||
2725              srcType == GL_FLOAT);
2726       /* Boil any of those types down to GL_FLOAT */
2727       srcType = GL_FLOAT;
2728    }
2729
2730    if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2731       assert(dstType == GL_UNSIGNED_NORMALIZED ||
2732              dstType == GL_SIGNED_NORMALIZED ||
2733              dstType == GL_FLOAT);
2734       /* Boil any of those types down to GL_FLOAT */
2735       dstType = GL_FLOAT;
2736    }
2737
2738    return srcType == dstType;
2739 }
2740
2741
2742 static GLboolean
2743 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2744                            const struct gl_renderbuffer *drawRb)
2745 {
2746    GLenum readFormat, drawFormat;
2747
2748    /* The simple case where we know the backing Mesa formats are the same.
2749     */
2750    if (_mesa_get_srgb_format_linear(readRb->Format) ==
2751        _mesa_get_srgb_format_linear(drawRb->Format)) {
2752       return GL_TRUE;
2753    }
2754
2755    /* The Mesa formats are different, so we must check whether the internal
2756     * formats are compatible.
2757     *
2758     * Under some circumstances, the user may request e.g. two GL_RGBA8
2759     * textures and get two entirely different Mesa formats like RGBA8888 and
2760     * ARGB8888. Drivers behaving like that should be able to cope with
2761     * non-matching formats by themselves, because it's not the user's fault.
2762     *
2763     * Blits between linear and sRGB formats are also allowed.
2764     */
2765    readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2766    drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2767    readFormat = _mesa_get_linear_internalformat(readFormat);
2768    drawFormat = _mesa_get_linear_internalformat(drawFormat);
2769
2770    if (readFormat == drawFormat) {
2771       return GL_TRUE;
2772    }
2773
2774    return GL_FALSE;
2775 }
2776
2777
2778 /**
2779  * Blit rectangular region, optionally from one framebuffer to another.
2780  *
2781  * Note, if the src buffer is multisampled and the dest is not, this is
2782  * when the samples must be resolved to a single color.
2783  */
2784 void GLAPIENTRY
2785 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2786                          GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2787                          GLbitfield mask, GLenum filter)
2788 {
2789    const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2790                                      GL_DEPTH_BUFFER_BIT |
2791                                      GL_STENCIL_BUFFER_BIT);
2792    const struct gl_framebuffer *readFb, *drawFb;
2793    GET_CURRENT_CONTEXT(ctx);
2794
2795    ASSERT_OUTSIDE_BEGIN_END(ctx);
2796    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2797
2798    if (MESA_VERBOSE & VERBOSE_API)
2799       _mesa_debug(ctx,
2800                   "glBlitFramebuffer(%d, %d, %d, %d,  %d, %d, %d, %d, 0x%x, %s)\n",
2801                   srcX0, srcY0, srcX1, srcY1,
2802                   dstX0, dstY0, dstX1, dstY1,
2803                   mask, _mesa_lookup_enum_by_nr(filter));
2804
2805    if (ctx->NewState) {
2806       _mesa_update_state(ctx);
2807    }
2808
2809    readFb = ctx->ReadBuffer;
2810    drawFb = ctx->DrawBuffer;
2811
2812    if (!readFb || !drawFb) {
2813       /* This will normally never happen but someday we may want to
2814        * support MakeCurrent() with no drawables.
2815        */
2816       return;
2817    }
2818
2819    /* check for complete framebuffers */
2820    if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2821        readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2822       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2823                   "glBlitFramebufferEXT(incomplete draw/read buffers)");
2824       return;
2825    }
2826
2827    if (filter != GL_NEAREST && filter != GL_LINEAR) {
2828       _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2829       return;
2830    }
2831
2832    if (mask & ~legalMaskBits) {
2833       _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2834       return;
2835    }
2836
2837    /* depth/stencil must be blitted with nearest filtering */
2838    if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2839         && filter != GL_NEAREST) {
2840       _mesa_error(ctx, GL_INVALID_OPERATION,
2841              "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2842       return;
2843    }
2844
2845    /* get color read/draw renderbuffers */
2846    if (mask & GL_COLOR_BUFFER_BIT) {
2847       const GLuint numColorDrawBuffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2848       const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
2849       const struct gl_renderbuffer *colorDrawRb = NULL;
2850       GLuint i;
2851
2852       /* From the EXT_framebuffer_object spec:
2853        *
2854        *     "If a buffer is specified in <mask> and does not exist in both
2855        *     the read and draw framebuffers, the corresponding bit is silently
2856        *     ignored."
2857        */
2858       if (!colorReadRb || numColorDrawBuffers == 0) {
2859          mask &= ~GL_COLOR_BUFFER_BIT;
2860       }
2861       else {
2862          for (i = 0; i < numColorDrawBuffers; i++) {
2863             colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2864             if (!colorDrawRb)
2865                continue;
2866
2867             if (!compatible_color_datatypes(colorReadRb->Format,
2868                                             colorDrawRb->Format)) {
2869                _mesa_error(ctx, GL_INVALID_OPERATION,
2870                            "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2871                return;
2872             }
2873             /* extra checks for multisample copies... */
2874             if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2875                /* color formats must match */
2876                if (!compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2877                   _mesa_error(ctx, GL_INVALID_OPERATION,
2878                          "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2879                   return;
2880                }
2881             }
2882          }
2883          if (filter == GL_LINEAR) {
2884             /* 3.1 spec, page 199:
2885              * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2886              * if filter is LINEAR and read buffer contains integer data."
2887              */
2888             GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2889             if (type == GL_INT || type == GL_UNSIGNED_INT) {
2890                _mesa_error(ctx, GL_INVALID_OPERATION,
2891                            "glBlitFramebufferEXT(integer color type)");
2892                return;
2893             }
2894          }
2895       }
2896    }
2897
2898    if (mask & GL_STENCIL_BUFFER_BIT) {
2899       struct gl_renderbuffer *readRb =
2900          readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2901       struct gl_renderbuffer *drawRb =
2902          drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2903
2904       /* From the EXT_framebuffer_object spec:
2905        *
2906        *     "If a buffer is specified in <mask> and does not exist in both
2907        *     the read and draw framebuffers, the corresponding bit is silently
2908        *     ignored."
2909        */
2910       if ((readRb == NULL) || (drawRb == NULL)) {
2911          mask &= ~GL_STENCIL_BUFFER_BIT;
2912       }
2913       else {
2914          int read_z_bits, draw_z_bits;
2915
2916          if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2917              _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2918             /* There is no need to check the stencil datatype here, because
2919              * there is only one: GL_UNSIGNED_INT.
2920              */
2921             _mesa_error(ctx, GL_INVALID_OPERATION,
2922                         "glBlitFramebuffer(stencil attachment format mismatch)");
2923             return;
2924          }
2925
2926          read_z_bits = _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS);
2927          draw_z_bits = _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS);
2928
2929          /* If both buffers also have depth data, the depth formats must match
2930           * as well.  If one doesn't have depth, it's not blitted, so we should
2931           * ignore the depth format check.
2932           */
2933          if (read_z_bits > 0 && draw_z_bits > 0 &&
2934              (read_z_bits != draw_z_bits ||
2935               _mesa_get_format_datatype(readRb->Format) !=
2936               _mesa_get_format_datatype(drawRb->Format))) {
2937
2938             _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
2939                         "(stencil attachment depth format mismatch)");
2940             return;
2941          }
2942       }
2943    }
2944
2945    if (mask & GL_DEPTH_BUFFER_BIT) {
2946       struct gl_renderbuffer *readRb =
2947          readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2948       struct gl_renderbuffer *drawRb =
2949          drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2950
2951       /* From the EXT_framebuffer_object spec:
2952        *
2953        *     "If a buffer is specified in <mask> and does not exist in both
2954        *     the read and draw framebuffers, the corresponding bit is silently
2955        *     ignored."
2956        */
2957       if ((readRb == NULL) || (drawRb == NULL)) {
2958          mask &= ~GL_DEPTH_BUFFER_BIT;
2959       }
2960       else {
2961          int read_s_bit, draw_s_bit;
2962
2963          if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2964               _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
2965              (_mesa_get_format_datatype(readRb->Format) !=
2966               _mesa_get_format_datatype(drawRb->Format))) {
2967             _mesa_error(ctx, GL_INVALID_OPERATION,
2968                         "glBlitFramebuffer(depth attachment format mismatch)");
2969             return;
2970          }
2971
2972          read_s_bit = _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS);
2973          draw_s_bit = _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS);
2974
2975          /* If both buffers also have stencil data, the stencil formats must
2976           * match as well.  If one doesn't have stencil, it's not blitted, so
2977           * we should ignore the stencil format check.
2978           */
2979          if (read_s_bit > 0 && draw_s_bit > 0 && read_s_bit != draw_s_bit) {
2980             _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebuffer"
2981                         "(depth attachment stencil bits mismatch)");
2982             return;
2983          }
2984       }
2985    }
2986
2987    if (readFb->Visual.samples > 0 &&
2988        drawFb->Visual.samples > 0 &&
2989        readFb->Visual.samples != drawFb->Visual.samples) {
2990       _mesa_error(ctx, GL_INVALID_OPERATION,
2991                   "glBlitFramebufferEXT(mismatched samples)");
2992       return;
2993    }
2994
2995    /* extra checks for multisample copies... */
2996    if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2997       /* src and dest region sizes must be the same */
2998       if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
2999           abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
3000          _mesa_error(ctx, GL_INVALID_OPERATION,
3001                 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
3002          return;
3003       }
3004    }
3005
3006    if (!ctx->Extensions.EXT_framebuffer_blit) {
3007       _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
3008       return;
3009    }
3010
3011    /* Debug code */
3012    if (DEBUG_BLIT) {
3013       const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
3014       const struct gl_renderbuffer *colorDrawRb = NULL;
3015       GLuint i = 0;
3016
3017       printf("glBlitFramebuffer(%d, %d, %d, %d,  %d, %d, %d, %d,"
3018              " 0x%x, 0x%x)\n",
3019              srcX0, srcY0, srcX1, srcY1,
3020              dstX0, dstY0, dstX1, dstY1,
3021              mask, filter);
3022       if (colorReadRb) {
3023          const struct gl_renderbuffer_attachment *att;
3024
3025          att = find_attachment(readFb, colorReadRb);
3026          printf("  Src FBO %u  RB %u (%dx%d)  ",
3027                 readFb->Name, colorReadRb->Name,
3028                 colorReadRb->Width, colorReadRb->Height);
3029          if (att && att->Texture) {
3030             printf("Tex %u  tgt 0x%x  level %u  face %u",
3031                    att->Texture->Name,
3032                    att->Texture->Target,
3033                    att->TextureLevel,
3034                    att->CubeMapFace);
3035          }
3036          printf("\n");
3037
3038          /* Print all active color render buffers */
3039          for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
3040             colorDrawRb = ctx->DrawBuffer->_ColorDrawBuffers[i];
3041             if (!colorDrawRb)
3042                continue;
3043
3044             att = find_attachment(drawFb, colorDrawRb);
3045             printf("  Dst FBO %u  RB %u (%dx%d)  ",
3046                    drawFb->Name, colorDrawRb->Name,
3047                    colorDrawRb->Width, colorDrawRb->Height);
3048             if (att && att->Texture) {
3049                printf("Tex %u  tgt 0x%x  level %u  face %u",
3050                       att->Texture->Name,
3051                       att->Texture->Target,
3052                       att->TextureLevel,
3053                       att->CubeMapFace);
3054             }
3055             printf("\n");
3056          }
3057       }
3058    }
3059
3060    if (!mask) {
3061       return;
3062    }
3063
3064    ASSERT(ctx->Driver.BlitFramebuffer);
3065    ctx->Driver.BlitFramebuffer(ctx,
3066                                srcX0, srcY0, srcX1, srcY1,
3067                                dstX0, dstY0, dstX1, dstY1,
3068                                mask, filter);
3069 }
3070
3071
3072 static void
3073 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
3074                                const GLenum *attachments, GLint x, GLint y,
3075                                GLsizei width, GLsizei height, const char *name)
3076 {
3077    int i;
3078    struct gl_framebuffer *fb;
3079    GET_CURRENT_CONTEXT(ctx);
3080
3081    ASSERT_OUTSIDE_BEGIN_END(ctx);
3082
3083    fb = get_framebuffer_target(ctx, target);
3084    if (!fb) {
3085       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
3086       return;
3087    }
3088
3089    if (numAttachments < 0) {
3090       _mesa_error(ctx, GL_INVALID_VALUE,
3091                   "%s(numAttachments < 0)", name);
3092       return;
3093    }
3094
3095    /* The GL_ARB_invalidate_subdata spec says:
3096     *
3097     *     "If an attachment is specified that does not exist in the
3098     *     framebuffer bound to <target>, it is ignored."
3099     *
3100     * It also says:
3101     *
3102     *     "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3103     *     or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3104     *     INVALID_OPERATION is generated."
3105     *
3106     * No mention is made of GL_AUXi being out of range.  Therefore, we allow
3107     * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3108     * set of retrictions).
3109     */
3110    for (i = 0; i < numAttachments; i++) {
3111       if (_mesa_is_winsys_fbo(fb)) {
3112          switch (attachments[i]) {
3113          case GL_ACCUM:
3114          case GL_AUX0:
3115          case GL_AUX1:
3116          case GL_AUX2:
3117          case GL_AUX3:
3118             /* Accumulation buffers and auxilary buffers were removed in
3119              * OpenGL 3.1, and they never existed in OpenGL ES.
3120              */
3121             if (ctx->API != API_OPENGL_COMPAT)
3122                goto invalid_enum;
3123             break;
3124          case GL_COLOR:
3125          case GL_DEPTH:
3126          case GL_STENCIL:
3127             break;
3128          case GL_BACK_LEFT:
3129          case GL_BACK_RIGHT:
3130          case GL_FRONT_LEFT:
3131          case GL_FRONT_RIGHT:
3132             if (!_mesa_is_desktop_gl(ctx))
3133                goto invalid_enum;
3134             break;
3135          default:
3136             goto invalid_enum;
3137          }
3138       } else {
3139          switch (attachments[i]) {
3140          case GL_DEPTH_ATTACHMENT:
3141          case GL_STENCIL_ATTACHMENT:
3142             break;
3143          case GL_COLOR_ATTACHMENT0:
3144          case GL_COLOR_ATTACHMENT1:
3145          case GL_COLOR_ATTACHMENT2:
3146          case GL_COLOR_ATTACHMENT3:
3147          case GL_COLOR_ATTACHMENT4:
3148          case GL_COLOR_ATTACHMENT5:
3149          case GL_COLOR_ATTACHMENT6:
3150          case GL_COLOR_ATTACHMENT7:
3151          case GL_COLOR_ATTACHMENT8:
3152          case GL_COLOR_ATTACHMENT9:
3153          case GL_COLOR_ATTACHMENT10:
3154          case GL_COLOR_ATTACHMENT11:
3155          case GL_COLOR_ATTACHMENT12:
3156          case GL_COLOR_ATTACHMENT13:
3157          case GL_COLOR_ATTACHMENT14:
3158          case GL_COLOR_ATTACHMENT15: {
3159             unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3160             if (k >= ctx->Const.MaxColorAttachments) {
3161                _mesa_error(ctx, GL_INVALID_OPERATION,
3162                            "%s(attachment >= max. color attachments)", name);
3163                return;
3164             }
3165          }
3166          default:
3167             goto invalid_enum;
3168          }
3169       }
3170    }
3171
3172    /* We don't actually do anything for this yet.  Just return after
3173     * validating the parameters and generating the required errors.
3174     */
3175    return;
3176
3177 invalid_enum:
3178    _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3179    return;
3180 }
3181
3182 void GLAPIENTRY
3183 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3184                                const GLenum *attachments, GLint x, GLint y,
3185                                GLsizei width, GLsizei height)
3186 {
3187    invalidate_framebuffer_storage(target, numAttachments, attachments,
3188                                   x, y, width, height,
3189                                   "glInvalidateSubFramebuffer");
3190 }
3191
3192 void GLAPIENTRY
3193 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3194                             const GLenum *attachments)
3195 {
3196    /* The GL_ARB_invalidate_subdata spec says:
3197     *
3198     *     "The command
3199     *
3200     *        void InvalidateFramebuffer(enum target,
3201     *                                   sizei numAttachments,
3202     *                                   const enum *attachments);
3203     *
3204     *     is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3205     *     <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3206     *     <MAX_VIEWPORT_DIMS[1]> respectively."
3207     */
3208    invalidate_framebuffer_storage(target, numAttachments, attachments,
3209                                   0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3210                                   "glInvalidateFramebuffer");
3211 }