OSDN Git Service

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