st/mesa: use the wrapped renderbuffer in CopyPixels()
[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 "renderbuffer.h"
44 #include "state.h"
45 #include "teximage.h"
46 #include "texobj.h"
47
48
49 /** Set this to 1 to help debug FBO incompleteness problems */
50 #define DEBUG_FBO 0
51
52 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
53 #define DEBUG_BLIT 0
54
55
56 /**
57 * Notes:
58 *
59 * None of the GL_EXT_framebuffer_object functions are compiled into
60 * display lists.
61 */
62
63
64
65 /*
66 * When glGenRender/FramebuffersEXT() is called we insert pointers to
67 * these placeholder objects into the hash table.
68 * Later, when the object ID is first bound, we replace the placeholder
69 * with the real frame/renderbuffer.
70 */
71 static struct gl_framebuffer DummyFramebuffer;
72 static struct gl_renderbuffer DummyRenderbuffer;
73
74 /* We bind this framebuffer when applications pass a NULL
75 * drawable/surface in make current. */
76 static struct gl_framebuffer IncompleteFramebuffer;
77
78
79 #define IS_CUBE_FACE(TARGET) \
80 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
81 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
82
83
84 static void
85 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
86 {
87 /* no op */
88 }
89
90 static void
91 delete_dummy_framebuffer(struct gl_framebuffer *fb)
92 {
93 /* no op */
94 }
95
96
97 void
98 _mesa_init_fbobjects(GLcontext *ctx)
99 {
100 _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
101 _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
102 _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
103 DummyFramebuffer.Delete = delete_dummy_framebuffer;
104 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
105 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
106 }
107
108 struct gl_framebuffer *
109 _mesa_get_incomplete_framebuffer(void)
110 {
111 return &IncompleteFramebuffer;
112 }
113
114 /**
115 * Helper routine for getting a gl_renderbuffer.
116 */
117 struct gl_renderbuffer *
118 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
119 {
120 struct gl_renderbuffer *rb;
121
122 if (id == 0)
123 return NULL;
124
125 rb = (struct gl_renderbuffer *)
126 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
127 return rb;
128 }
129
130
131 /**
132 * Helper routine for getting a gl_framebuffer.
133 */
134 struct gl_framebuffer *
135 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
136 {
137 struct gl_framebuffer *fb;
138
139 if (id == 0)
140 return NULL;
141
142 fb = (struct gl_framebuffer *)
143 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
144 return fb;
145 }
146
147
148 /**
149 * Mark the given framebuffer as invalid. This will force the
150 * test for framebuffer completeness to be done before the framebuffer
151 * is used.
152 */
153 static void
154 invalidate_framebuffer(struct gl_framebuffer *fb)
155 {
156 fb->_Status = 0; /* "indeterminate" */
157 }
158
159
160 /**
161 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
162 * gl_renderbuffer_attachment object.
163 * This function is only used for user-created FB objects, not the
164 * default / window-system FB object.
165 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
166 * the depth buffer attachment point.
167 */
168 struct gl_renderbuffer_attachment *
169 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
170 GLenum attachment)
171 {
172 GLuint i;
173
174 assert(fb->Name > 0);
175
176 switch (attachment) {
177 case GL_COLOR_ATTACHMENT0_EXT:
178 case GL_COLOR_ATTACHMENT1_EXT:
179 case GL_COLOR_ATTACHMENT2_EXT:
180 case GL_COLOR_ATTACHMENT3_EXT:
181 case GL_COLOR_ATTACHMENT4_EXT:
182 case GL_COLOR_ATTACHMENT5_EXT:
183 case GL_COLOR_ATTACHMENT6_EXT:
184 case GL_COLOR_ATTACHMENT7_EXT:
185 case GL_COLOR_ATTACHMENT8_EXT:
186 case GL_COLOR_ATTACHMENT9_EXT:
187 case GL_COLOR_ATTACHMENT10_EXT:
188 case GL_COLOR_ATTACHMENT11_EXT:
189 case GL_COLOR_ATTACHMENT12_EXT:
190 case GL_COLOR_ATTACHMENT13_EXT:
191 case GL_COLOR_ATTACHMENT14_EXT:
192 case GL_COLOR_ATTACHMENT15_EXT:
193 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
194 if (i >= ctx->Const.MaxColorAttachments) {
195 return NULL;
196 }
197 return &fb->Attachment[BUFFER_COLOR0 + i];
198 case GL_DEPTH_STENCIL_ATTACHMENT:
199 /* fall-through */
200 case GL_DEPTH_BUFFER:
201 /* fall-through / new in GL 3.0 */
202 case GL_DEPTH_ATTACHMENT_EXT:
203 return &fb->Attachment[BUFFER_DEPTH];
204 case GL_STENCIL_BUFFER:
205 /* fall-through / new in GL 3.0 */
206 case GL_STENCIL_ATTACHMENT_EXT:
207 return &fb->Attachment[BUFFER_STENCIL];
208 default:
209 return NULL;
210 }
211 }
212
213
214 /**
215 * As above, but only used for getting attachments of the default /
216 * window-system framebuffer (not user-created framebuffer objects).
217 */
218 static struct gl_renderbuffer_attachment *
219 _mesa_get_fb0_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
220 GLenum attachment)
221 {
222 assert(fb->Name == 0);
223
224 switch (attachment) {
225 case GL_FRONT_LEFT:
226 return &fb->Attachment[BUFFER_FRONT_LEFT];
227 case GL_FRONT_RIGHT:
228 return &fb->Attachment[BUFFER_FRONT_RIGHT];
229 case GL_BACK_LEFT:
230 return &fb->Attachment[BUFFER_BACK_LEFT];
231 case GL_BACK_RIGHT:
232 return &fb->Attachment[BUFFER_BACK_RIGHT];
233 case GL_AUX0:
234 if (fb->Visual.numAuxBuffers == 1) {
235 return &fb->Attachment[BUFFER_AUX0];
236 }
237 return NULL;
238 case GL_DEPTH_BUFFER:
239 /* fall-through / new in GL 3.0 */
240 case GL_DEPTH_ATTACHMENT_EXT:
241 return &fb->Attachment[BUFFER_DEPTH];
242 case GL_STENCIL_BUFFER:
243 /* fall-through / new in GL 3.0 */
244 case GL_STENCIL_ATTACHMENT_EXT:
245 return &fb->Attachment[BUFFER_STENCIL];
246 default:
247 return NULL;
248 }
249 }
250
251
252
253 /**
254 * Remove any texture or renderbuffer attached to the given attachment
255 * point. Update reference counts, etc.
256 */
257 void
258 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
259 {
260 if (att->Type == GL_TEXTURE) {
261 ASSERT(att->Texture);
262 if (ctx->Driver.FinishRenderTexture) {
263 /* tell driver that we're done rendering to this texture. */
264 ctx->Driver.FinishRenderTexture(ctx, att);
265 }
266 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
267 ASSERT(!att->Texture);
268 }
269 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
270 ASSERT(!att->Texture);
271 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
272 ASSERT(!att->Renderbuffer);
273 }
274 att->Type = GL_NONE;
275 att->Complete = GL_TRUE;
276 }
277
278
279 /**
280 * Bind a texture object to an attachment point.
281 * The previous binding, if any, will be removed first.
282 */
283 void
284 _mesa_set_texture_attachment(GLcontext *ctx,
285 struct gl_framebuffer *fb,
286 struct gl_renderbuffer_attachment *att,
287 struct gl_texture_object *texObj,
288 GLenum texTarget, GLuint level, GLuint zoffset)
289 {
290 if (att->Texture == texObj) {
291 /* re-attaching same texture */
292 ASSERT(att->Type == GL_TEXTURE);
293 if (ctx->Driver.FinishRenderTexture)
294 ctx->Driver.FinishRenderTexture(ctx, att);
295 }
296 else {
297 /* new attachment */
298 if (ctx->Driver.FinishRenderTexture && att->Texture)
299 ctx->Driver.FinishRenderTexture(ctx, att);
300 _mesa_remove_attachment(ctx, att);
301 att->Type = GL_TEXTURE;
302 assert(!att->Texture);
303 _mesa_reference_texobj(&att->Texture, texObj);
304 }
305
306 /* always update these fields */
307 att->TextureLevel = level;
308 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
309 att->Zoffset = zoffset;
310 att->Complete = GL_FALSE;
311
312 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
313 ctx->Driver.RenderTexture(ctx, fb, att);
314 }
315
316 invalidate_framebuffer(fb);
317 }
318
319
320 /**
321 * Bind a renderbuffer to an attachment point.
322 * The previous binding, if any, will be removed first.
323 */
324 void
325 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
326 struct gl_renderbuffer_attachment *att,
327 struct gl_renderbuffer *rb)
328 {
329 /* XXX check if re-doing same attachment, exit early */
330 _mesa_remove_attachment(ctx, att);
331 att->Type = GL_RENDERBUFFER_EXT;
332 att->Texture = NULL; /* just to be safe */
333 att->Complete = GL_FALSE;
334 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
335 }
336
337
338 /**
339 * Fallback for ctx->Driver.FramebufferRenderbuffer()
340 * Attach a renderbuffer object to a framebuffer object.
341 */
342 void
343 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
344 GLenum attachment, struct gl_renderbuffer *rb)
345 {
346 struct gl_renderbuffer_attachment *att;
347
348 _glthread_LOCK_MUTEX(fb->Mutex);
349
350 att = _mesa_get_attachment(ctx, fb, attachment);
351 ASSERT(att);
352 if (rb) {
353 _mesa_set_renderbuffer_attachment(ctx, att, rb);
354 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
355 /* do stencil attachment here (depth already done above) */
356 att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
357 assert(att);
358 _mesa_set_renderbuffer_attachment(ctx, att, rb);
359 }
360 }
361 else {
362 _mesa_remove_attachment(ctx, att);
363 }
364
365 invalidate_framebuffer(fb);
366
367 _glthread_UNLOCK_MUTEX(fb->Mutex);
368 }
369
370
371 /**
372 * For debug only.
373 */
374 static void
375 att_incomplete(const char *msg)
376 {
377 #if DEBUG_FBO
378 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
379 #else
380 (void) msg;
381 #endif
382 }
383
384
385 /**
386 * For debug only.
387 */
388 static void
389 fbo_incomplete(const char *msg, int index)
390 {
391 #if DEBUG_FBO
392 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
393 #else
394 (void) msg;
395 (void) index;
396 #endif
397 }
398
399
400
401
402 /**
403 * Test if an attachment point is complete and update its Complete field.
404 * \param format if GL_COLOR, this is a color attachment point,
405 * if GL_DEPTH, this is a depth component attachment point,
406 * if GL_STENCIL, this is a stencil component attachment point.
407 */
408 static void
409 test_attachment_completeness(const GLcontext *ctx, GLenum format,
410 struct gl_renderbuffer_attachment *att)
411 {
412 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
413
414 /* assume complete */
415 att->Complete = GL_TRUE;
416
417 /* Look for reasons why the attachment might be incomplete */
418 if (att->Type == GL_TEXTURE) {
419 const struct gl_texture_object *texObj = att->Texture;
420 struct gl_texture_image *texImage;
421 GLenum baseFormat;
422
423 if (!texObj) {
424 att_incomplete("no texobj");
425 att->Complete = GL_FALSE;
426 return;
427 }
428
429 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
430 if (!texImage) {
431 att_incomplete("no teximage");
432 att->Complete = GL_FALSE;
433 return;
434 }
435 if (texImage->Width < 1 || texImage->Height < 1) {
436 att_incomplete("teximage width/height=0");
437 printf("texobj = %u\n", texObj->Name);
438 printf("level = %d\n", att->TextureLevel);
439 att->Complete = GL_FALSE;
440 return;
441 }
442 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
443 att_incomplete("bad z offset");
444 att->Complete = GL_FALSE;
445 return;
446 }
447
448 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
449
450 if (format == GL_COLOR) {
451 if (baseFormat != GL_RGB &&
452 baseFormat != GL_RGBA &&
453 (!ctx->Extensions.ARB_framebuffer_object ||
454 baseFormat != GL_ALPHA)) {
455 att_incomplete("bad format");
456 att->Complete = GL_FALSE;
457 return;
458 }
459 if (_mesa_is_format_compressed(texImage->TexFormat)) {
460 att_incomplete("compressed internalformat");
461 att->Complete = GL_FALSE;
462 return;
463 }
464 }
465 else if (format == GL_DEPTH) {
466 if (baseFormat == GL_DEPTH_COMPONENT) {
467 /* OK */
468 }
469 else if (ctx->Extensions.EXT_packed_depth_stencil &&
470 ctx->Extensions.ARB_depth_texture &&
471 baseFormat == GL_DEPTH_STENCIL_EXT) {
472 /* OK */
473 }
474 else {
475 att->Complete = GL_FALSE;
476 att_incomplete("bad depth format");
477 return;
478 }
479 }
480 else {
481 ASSERT(format == GL_STENCIL);
482 if (ctx->Extensions.EXT_packed_depth_stencil &&
483 ctx->Extensions.ARB_depth_texture &&
484 baseFormat == GL_DEPTH_STENCIL_EXT) {
485 /* OK */
486 }
487 else {
488 /* no such thing as stencil-only textures */
489 att_incomplete("illegal stencil texture");
490 att->Complete = GL_FALSE;
491 return;
492 }
493 }
494 }
495 else if (att->Type == GL_RENDERBUFFER_EXT) {
496 const GLenum baseFormat =
497 _mesa_get_format_base_format(att->Renderbuffer->Format);
498
499 ASSERT(att->Renderbuffer);
500 if (!att->Renderbuffer->InternalFormat ||
501 att->Renderbuffer->Width < 1 ||
502 att->Renderbuffer->Height < 1) {
503 att_incomplete("0x0 renderbuffer");
504 att->Complete = GL_FALSE;
505 return;
506 }
507 if (format == GL_COLOR) {
508 if (baseFormat != GL_RGB &&
509 baseFormat != GL_RGBA) {
510 att_incomplete("bad renderbuffer color format");
511 att->Complete = GL_FALSE;
512 return;
513 }
514 }
515 else if (format == GL_DEPTH) {
516 if (baseFormat == GL_DEPTH_COMPONENT) {
517 /* OK */
518 }
519 else if (ctx->Extensions.EXT_packed_depth_stencil &&
520 baseFormat == GL_DEPTH_STENCIL_EXT) {
521 /* OK */
522 }
523 else {
524 att_incomplete("bad renderbuffer depth format");
525 att->Complete = GL_FALSE;
526 return;
527 }
528 }
529 else {
530 assert(format == GL_STENCIL);
531 if (baseFormat == GL_STENCIL_INDEX) {
532 /* OK */
533 }
534 else if (ctx->Extensions.EXT_packed_depth_stencil &&
535 baseFormat == GL_DEPTH_STENCIL_EXT) {
536 /* OK */
537 }
538 else {
539 att->Complete = GL_FALSE;
540 att_incomplete("bad renderbuffer stencil format");
541 return;
542 }
543 }
544 }
545 else {
546 ASSERT(att->Type == GL_NONE);
547 /* complete */
548 return;
549 }
550 }
551
552
553 /**
554 * Test if the given framebuffer object is complete and update its
555 * Status field with the results.
556 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
557 * driver to make hardware-specific validation/completeness checks.
558 * Also update the framebuffer's Width and Height fields if the
559 * framebuffer is complete.
560 */
561 void
562 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
563 {
564 GLuint numImages;
565 GLenum intFormat = GL_NONE; /* color buffers' internal format */
566 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
567 GLint numSamples = -1;
568 GLint i;
569 GLuint j;
570
571 assert(fb->Name != 0);
572
573 numImages = 0;
574 fb->Width = 0;
575 fb->Height = 0;
576
577 /* Start at -2 to more easily loop over all attachment points.
578 * -2: depth buffer
579 * -1: stencil buffer
580 * >=0: color buffer
581 */
582 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
583 struct gl_renderbuffer_attachment *att;
584 GLenum f;
585
586 /*
587 * XXX for ARB_fbo, only check color buffers that are named by
588 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
589 */
590
591 /* check for attachment completeness
592 */
593 if (i == -2) {
594 att = &fb->Attachment[BUFFER_DEPTH];
595 test_attachment_completeness(ctx, GL_DEPTH, att);
596 if (!att->Complete) {
597 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
598 fbo_incomplete("depth attachment incomplete", -1);
599 return;
600 }
601 }
602 else if (i == -1) {
603 att = &fb->Attachment[BUFFER_STENCIL];
604 test_attachment_completeness(ctx, GL_STENCIL, att);
605 if (!att->Complete) {
606 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
607 fbo_incomplete("stencil attachment incomplete", -1);
608 return;
609 }
610 }
611 else {
612 att = &fb->Attachment[BUFFER_COLOR0 + i];
613 test_attachment_completeness(ctx, GL_COLOR, att);
614 if (!att->Complete) {
615 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
616 fbo_incomplete("color attachment incomplete", i);
617 return;
618 }
619 }
620
621 /* get width, height, format of the renderbuffer/texture
622 */
623 if (att->Type == GL_TEXTURE) {
624 const struct gl_texture_image *texImg
625 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
626 minWidth = MIN2(minWidth, texImg->Width);
627 maxWidth = MAX2(maxWidth, texImg->Width);
628 minHeight = MIN2(minHeight, texImg->Height);
629 maxHeight = MAX2(maxHeight, texImg->Height);
630 f = texImg->_BaseFormat;
631 numImages++;
632 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
633 && f != GL_DEPTH_STENCIL_EXT
634 && (!ctx->Extensions.ARB_framebuffer_object || f != GL_ALPHA)) {
635 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
636 fbo_incomplete("texture attachment incomplete", -1);
637 return;
638 }
639 }
640 else if (att->Type == GL_RENDERBUFFER_EXT) {
641 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
642 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
643 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
644 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
645 f = att->Renderbuffer->InternalFormat;
646 numImages++;
647 }
648 else {
649 assert(att->Type == GL_NONE);
650 continue;
651 }
652
653 if (numSamples < 0) {
654 /* first buffer */
655 numSamples = att->Renderbuffer->NumSamples;
656 }
657
658 /* Error-check width, height, format, samples
659 */
660 if (numImages == 1) {
661 /* save format, num samples */
662 if (i >= 0) {
663 intFormat = f;
664 }
665 }
666 else {
667 if (!ctx->Extensions.ARB_framebuffer_object) {
668 /* check that width, height, format are same */
669 if (minWidth != maxWidth || minHeight != maxHeight) {
670 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
671 fbo_incomplete("width or height mismatch", -1);
672 return;
673 }
674 /* check that all color buffer have same format */
675 if (intFormat != GL_NONE && f != intFormat) {
676 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
677 fbo_incomplete("format mismatch", -1);
678 return;
679 }
680 }
681 if (att->Renderbuffer &&
682 att->Renderbuffer->NumSamples != numSamples) {
683 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
684 fbo_incomplete("inconsistant number of samples", i);
685 return;
686 }
687
688 }
689 }
690
691 #if FEATURE_GL
692 if (ctx->API == API_OPENGL) {
693 /* Check that all DrawBuffers are present */
694 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
695 if (fb->ColorDrawBuffer[j] != GL_NONE) {
696 const struct gl_renderbuffer_attachment *att
697 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
698 assert(att);
699 if (att->Type == GL_NONE) {
700 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
701 fbo_incomplete("missing drawbuffer", j);
702 return;
703 }
704 }
705 }
706
707 /* Check that the ReadBuffer is present */
708 if (fb->ColorReadBuffer != GL_NONE) {
709 const struct gl_renderbuffer_attachment *att
710 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
711 assert(att);
712 if (att->Type == GL_NONE) {
713 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
714 fbo_incomplete("missing readbuffer", -1);
715 return;
716 }
717 }
718 }
719 #else
720 (void) j;
721 #endif
722
723 if (numImages == 0) {
724 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
725 fbo_incomplete("no attachments", -1);
726 return;
727 }
728
729 /* Provisionally set status = COMPLETE ... */
730 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
731
732 /* ... but the driver may say the FB is incomplete.
733 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
734 * if anything.
735 */
736 if (ctx->Driver.ValidateFramebuffer) {
737 ctx->Driver.ValidateFramebuffer(ctx, fb);
738 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
739 fbo_incomplete("driver marked FBO as incomplete", -1);
740 }
741 }
742
743 if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
744 /*
745 * Note that if ARB_framebuffer_object is supported and the attached
746 * renderbuffers/textures are different sizes, the framebuffer
747 * width/height will be set to the smallest width/height.
748 */
749 fb->Width = minWidth;
750 fb->Height = minHeight;
751
752 /* finally, update the visual info for the framebuffer */
753 _mesa_update_framebuffer_visual(fb);
754 }
755 }
756
757
758 GLboolean GLAPIENTRY
759 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
760 {
761 GET_CURRENT_CONTEXT(ctx);
762 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
763 if (renderbuffer) {
764 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
765 if (rb != NULL && rb != &DummyRenderbuffer)
766 return GL_TRUE;
767 }
768 return GL_FALSE;
769 }
770
771
772 void GLAPIENTRY
773 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
774 {
775 struct gl_renderbuffer *newRb;
776 GET_CURRENT_CONTEXT(ctx);
777
778 ASSERT_OUTSIDE_BEGIN_END(ctx);
779
780 if (target != GL_RENDERBUFFER_EXT) {
781 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
782 return;
783 }
784
785 /* No need to flush here since the render buffer binding has no
786 * effect on rendering state.
787 */
788
789 if (renderbuffer) {
790 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
791 if (newRb == &DummyRenderbuffer) {
792 /* ID was reserved, but no real renderbuffer object made yet */
793 newRb = NULL;
794 }
795 else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
796 /* All RB IDs must be Gen'd */
797 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
798 return;
799 }
800
801 if (!newRb) {
802 /* create new renderbuffer object */
803 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
804 if (!newRb) {
805 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
806 return;
807 }
808 ASSERT(newRb->AllocStorage);
809 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
810 newRb->RefCount = 1; /* referenced by hash table */
811 }
812 }
813 else {
814 newRb = NULL;
815 }
816
817 ASSERT(newRb != &DummyRenderbuffer);
818
819 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
820 }
821
822
823 /**
824 * If the given renderbuffer is anywhere attached to the framebuffer, detach
825 * the renderbuffer.
826 * This is used when a renderbuffer object is deleted.
827 * The spec calls for unbinding.
828 */
829 static void
830 detach_renderbuffer(GLcontext *ctx,
831 struct gl_framebuffer *fb,
832 struct gl_renderbuffer *rb)
833 {
834 GLuint i;
835 for (i = 0; i < BUFFER_COUNT; i++) {
836 if (fb->Attachment[i].Renderbuffer == rb) {
837 _mesa_remove_attachment(ctx, &fb->Attachment[i]);
838 }
839 }
840 invalidate_framebuffer(fb);
841 }
842
843
844 void GLAPIENTRY
845 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
846 {
847 GLint i;
848 GET_CURRENT_CONTEXT(ctx);
849
850 ASSERT_OUTSIDE_BEGIN_END(ctx);
851 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
852
853 for (i = 0; i < n; i++) {
854 if (renderbuffers[i] > 0) {
855 struct gl_renderbuffer *rb;
856 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
857 if (rb) {
858 /* check if deleting currently bound renderbuffer object */
859 if (rb == ctx->CurrentRenderbuffer) {
860 /* bind default */
861 ASSERT(rb->RefCount >= 2);
862 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
863 }
864
865 if (ctx->DrawBuffer->Name) {
866 detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
867 }
868 if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
869 detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
870 }
871
872 /* Remove from hash table immediately, to free the ID.
873 * But the object will not be freed until it's no longer
874 * referenced anywhere else.
875 */
876 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
877
878 if (rb != &DummyRenderbuffer) {
879 /* no longer referenced by hash table */
880 _mesa_reference_renderbuffer(&rb, NULL);
881 }
882 }
883 }
884 }
885 }
886
887
888 void GLAPIENTRY
889 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
890 {
891 GET_CURRENT_CONTEXT(ctx);
892 GLuint first;
893 GLint i;
894
895 ASSERT_OUTSIDE_BEGIN_END(ctx);
896
897 if (n < 0) {
898 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
899 return;
900 }
901
902 if (!renderbuffers)
903 return;
904
905 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
906
907 for (i = 0; i < n; i++) {
908 GLuint name = first + i;
909 renderbuffers[i] = name;
910 /* insert dummy placeholder into hash table */
911 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
912 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
913 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
914 }
915 }
916
917
918 /**
919 * Given an internal format token for a render buffer, return the
920 * corresponding base format.
921 * This is very similar to _mesa_base_tex_format() but the set of valid
922 * internal formats is somewhat different.
923 *
924 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
925 * GL_DEPTH_STENCIL_EXT or zero if error.
926 *
927 * XXX in the future when we support red-only and red-green formats
928 * we'll also return GL_RED and GL_RG.
929 */
930 GLenum
931 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
932 {
933 switch (internalFormat) {
934 case GL_ALPHA:
935 case GL_ALPHA4:
936 case GL_ALPHA8:
937 case GL_ALPHA12:
938 case GL_ALPHA16:
939 return GL_ALPHA;
940 case GL_RGB:
941 case GL_R3_G3_B2:
942 case GL_RGB4:
943 case GL_RGB5:
944 case GL_RGB8:
945 case GL_RGB10:
946 case GL_RGB12:
947 case GL_RGB16:
948 return GL_RGB;
949 case GL_RGBA:
950 case GL_RGBA2:
951 case GL_RGBA4:
952 case GL_RGB5_A1:
953 case GL_RGBA8:
954 case GL_RGB10_A2:
955 case GL_RGBA12:
956 case GL_RGBA16:
957 case GL_RGBA16_SNORM:
958 return GL_RGBA;
959 case GL_STENCIL_INDEX:
960 case GL_STENCIL_INDEX1_EXT:
961 case GL_STENCIL_INDEX4_EXT:
962 case GL_STENCIL_INDEX8_EXT:
963 case GL_STENCIL_INDEX16_EXT:
964 return GL_STENCIL_INDEX;
965 case GL_DEPTH_COMPONENT:
966 case GL_DEPTH_COMPONENT16:
967 case GL_DEPTH_COMPONENT24:
968 case GL_DEPTH_COMPONENT32:
969 return GL_DEPTH_COMPONENT;
970 case GL_DEPTH_STENCIL_EXT:
971 case GL_DEPTH24_STENCIL8_EXT:
972 if (ctx->Extensions.EXT_packed_depth_stencil)
973 return GL_DEPTH_STENCIL_EXT;
974 else
975 return 0;
976 /* XXX add floating point formats eventually */
977 default:
978 return 0;
979 }
980 }
981
982
983 /** sentinal value, see below */
984 #define NO_SAMPLES 1000
985
986
987 /**
988 * Helper function used by _mesa_RenderbufferStorageEXT() and
989 * _mesa_RenderbufferStorageMultisample().
990 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
991 */
992 static void
993 renderbuffer_storage(GLenum target, GLenum internalFormat,
994 GLsizei width, GLsizei height, GLsizei samples)
995 {
996 const char *func = samples == NO_SAMPLES ?
997 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
998 struct gl_renderbuffer *rb;
999 GLenum baseFormat;
1000 GET_CURRENT_CONTEXT(ctx);
1001
1002 ASSERT_OUTSIDE_BEGIN_END(ctx);
1003
1004 if (target != GL_RENDERBUFFER_EXT) {
1005 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1006 return;
1007 }
1008
1009 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1010 if (baseFormat == 0) {
1011 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
1012 return;
1013 }
1014
1015 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1016 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1017 return;
1018 }
1019
1020 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1021 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1022 return;
1023 }
1024
1025 if (samples == NO_SAMPLES) {
1026 /* NumSamples == 0 indicates non-multisampling */
1027 samples = 0;
1028 }
1029 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1030 /* note: driver may choose to use more samples than what's requested */
1031 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1032 return;
1033 }
1034
1035 rb = ctx->CurrentRenderbuffer;
1036 if (!rb) {
1037 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1038 return;
1039 }
1040
1041 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1042
1043 if (rb->InternalFormat == internalFormat &&
1044 rb->Width == (GLuint) width &&
1045 rb->Height == (GLuint) height) {
1046 /* no change in allocation needed */
1047 return;
1048 }
1049
1050 /* These MUST get set by the AllocStorage func */
1051 rb->Format = MESA_FORMAT_NONE;
1052 rb->NumSamples = samples;
1053
1054 /* Now allocate the storage */
1055 ASSERT(rb->AllocStorage);
1056 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1057 /* No error - check/set fields now */
1058 assert(rb->Format != MESA_FORMAT_NONE);
1059 assert(rb->Width == (GLuint) width);
1060 assert(rb->Height == (GLuint) height);
1061 rb->InternalFormat = internalFormat;
1062 rb->_BaseFormat = baseFormat;
1063 assert(rb->_BaseFormat != 0);
1064 }
1065 else {
1066 /* Probably ran out of memory - clear the fields */
1067 rb->Width = 0;
1068 rb->Height = 0;
1069 rb->Format = MESA_FORMAT_NONE;
1070 rb->InternalFormat = GL_NONE;
1071 rb->_BaseFormat = GL_NONE;
1072 rb->NumSamples = 0;
1073 }
1074
1075 /*
1076 test_framebuffer_completeness(ctx, fb);
1077 */
1078 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1079 * points???
1080 */
1081 }
1082
1083
1084 #if FEATURE_OES_EGL_image
1085 void GLAPIENTRY
1086 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1087 {
1088 struct gl_renderbuffer *rb;
1089 GET_CURRENT_CONTEXT(ctx);
1090 ASSERT_OUTSIDE_BEGIN_END(ctx);
1091
1092 if (!ctx->Extensions.OES_EGL_image) {
1093 _mesa_error(ctx, GL_INVALID_OPERATION,
1094 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1095 return;
1096 }
1097
1098 if (target != GL_RENDERBUFFER) {
1099 _mesa_error(ctx, GL_INVALID_ENUM,
1100 "EGLImageTargetRenderbufferStorageOES");
1101 return;
1102 }
1103
1104 rb = ctx->CurrentRenderbuffer;
1105 if (!rb) {
1106 _mesa_error(ctx, GL_INVALID_OPERATION,
1107 "EGLImageTargetRenderbufferStorageOES");
1108 return;
1109 }
1110
1111 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1112
1113 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1114 }
1115 #endif
1116
1117
1118 /**
1119 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1120 * _mesa_GetFramebufferAttachmentParameterivEXT()
1121 * We have to be careful to respect the base format. For example, if a
1122 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1123 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1124 * we need to return zero.
1125 */
1126 static GLint
1127 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1128 {
1129 switch (pname) {
1130 case GL_RENDERBUFFER_RED_SIZE_EXT:
1131 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1132 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1133 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1134 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1135 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1136 if (baseFormat == GL_RGB || baseFormat == GL_RGBA)
1137 return _mesa_get_format_bits(format, pname);
1138 else
1139 return 0;
1140 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1141 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1142 if (baseFormat == GL_RGBA || baseFormat == GL_ALPHA)
1143 return _mesa_get_format_bits(format, pname);
1144 else
1145 return 0;
1146 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1147 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1148 if (baseFormat == GL_DEPTH_COMPONENT || baseFormat == GL_DEPTH_STENCIL)
1149 return _mesa_get_format_bits(format, pname);
1150 else
1151 return 0;
1152 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1153 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1154 if (baseFormat == GL_STENCIL_INDEX || baseFormat == GL_DEPTH_STENCIL)
1155 return _mesa_get_format_bits(format, pname);
1156 else
1157 return 0;
1158 default:
1159 return 0;
1160 }
1161 }
1162
1163
1164
1165 void GLAPIENTRY
1166 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1167 GLsizei width, GLsizei height)
1168 {
1169 /* GL_ARB_fbo says calling this function is equivalent to calling
1170 * glRenderbufferStorageMultisample() with samples=0. We pass in
1171 * a token value here just for error reporting purposes.
1172 */
1173 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1174 }
1175
1176
1177 void GLAPIENTRY
1178 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1179 GLenum internalFormat,
1180 GLsizei width, GLsizei height)
1181 {
1182 renderbuffer_storage(target, internalFormat, width, height, samples);
1183 }
1184
1185
1186 /**
1187 * OpenGL ES version of glRenderBufferStorage.
1188 */
1189 void GLAPIENTRY
1190 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1191 GLsizei width, GLsizei height)
1192 {
1193 switch (internalFormat) {
1194 case GL_RGB565:
1195 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1196 /* choose a closest format */
1197 internalFormat = GL_RGB5;
1198 break;
1199 default:
1200 break;
1201 }
1202
1203 renderbuffer_storage(target, internalFormat, width, height, 0);
1204 }
1205
1206
1207 void GLAPIENTRY
1208 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1209 {
1210 struct gl_renderbuffer *rb;
1211 GET_CURRENT_CONTEXT(ctx);
1212
1213 ASSERT_OUTSIDE_BEGIN_END(ctx);
1214
1215 if (target != GL_RENDERBUFFER_EXT) {
1216 _mesa_error(ctx, GL_INVALID_ENUM,
1217 "glGetRenderbufferParameterivEXT(target)");
1218 return;
1219 }
1220
1221 rb = ctx->CurrentRenderbuffer;
1222 if (!rb) {
1223 _mesa_error(ctx, GL_INVALID_OPERATION,
1224 "glGetRenderbufferParameterivEXT");
1225 return;
1226 }
1227
1228 /* No need to flush here since we're just quering state which is
1229 * not effected by rendering.
1230 */
1231
1232 switch (pname) {
1233 case GL_RENDERBUFFER_WIDTH_EXT:
1234 *params = rb->Width;
1235 return;
1236 case GL_RENDERBUFFER_HEIGHT_EXT:
1237 *params = rb->Height;
1238 return;
1239 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1240 *params = rb->InternalFormat;
1241 return;
1242 case GL_RENDERBUFFER_RED_SIZE_EXT:
1243 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1244 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1245 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1246 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1247 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1248 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1249 break;
1250 case GL_RENDERBUFFER_SAMPLES:
1251 if (ctx->Extensions.ARB_framebuffer_object) {
1252 *params = rb->NumSamples;
1253 break;
1254 }
1255 /* fallthrough */
1256 default:
1257 _mesa_error(ctx, GL_INVALID_ENUM,
1258 "glGetRenderbufferParameterivEXT(target)");
1259 return;
1260 }
1261 }
1262
1263
1264 GLboolean GLAPIENTRY
1265 _mesa_IsFramebufferEXT(GLuint framebuffer)
1266 {
1267 GET_CURRENT_CONTEXT(ctx);
1268 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1269 if (framebuffer) {
1270 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1271 if (rb != NULL && rb != &DummyFramebuffer)
1272 return GL_TRUE;
1273 }
1274 return GL_FALSE;
1275 }
1276
1277
1278 /**
1279 * Check if any of the attachments of the given framebuffer are textures
1280 * (render to texture). Call ctx->Driver.RenderTexture() for such
1281 * attachments.
1282 */
1283 static void
1284 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1285 {
1286 GLuint i;
1287 ASSERT(ctx->Driver.RenderTexture);
1288
1289 if (fb->Name == 0)
1290 return; /* can't render to texture with winsys framebuffers */
1291
1292 for (i = 0; i < BUFFER_COUNT; i++) {
1293 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1294 struct gl_texture_object *texObj = att->Texture;
1295 if (texObj
1296 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1297 ctx->Driver.RenderTexture(ctx, fb, att);
1298 }
1299 }
1300 }
1301
1302
1303 /**
1304 * Examine all the framebuffer's attachments to see if any are textures.
1305 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1306 * notify the device driver that the texture image may have changed.
1307 */
1308 static void
1309 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1310 {
1311 if (fb->Name == 0)
1312 return; /* can't render to texture with winsys framebuffers */
1313
1314 if (ctx->Driver.FinishRenderTexture) {
1315 GLuint i;
1316 for (i = 0; i < BUFFER_COUNT; i++) {
1317 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1318 if (att->Texture && att->Renderbuffer) {
1319 ctx->Driver.FinishRenderTexture(ctx, att);
1320 }
1321 }
1322 }
1323 }
1324
1325
1326 void GLAPIENTRY
1327 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1328 {
1329 struct gl_framebuffer *newDrawFb, *newReadFb;
1330 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1331 GLboolean bindReadBuf, bindDrawBuf;
1332 GET_CURRENT_CONTEXT(ctx);
1333
1334 #ifdef DEBUG
1335 if (ctx->Extensions.ARB_framebuffer_object) {
1336 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1337 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1338 }
1339 #endif
1340
1341 ASSERT_OUTSIDE_BEGIN_END(ctx);
1342
1343 if (!ctx->Extensions.EXT_framebuffer_object) {
1344 _mesa_error(ctx, GL_INVALID_OPERATION,
1345 "glBindFramebufferEXT(unsupported)");
1346 return;
1347 }
1348
1349 switch (target) {
1350 #if FEATURE_EXT_framebuffer_blit
1351 case GL_DRAW_FRAMEBUFFER_EXT:
1352 if (!ctx->Extensions.EXT_framebuffer_blit) {
1353 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1354 return;
1355 }
1356 bindDrawBuf = GL_TRUE;
1357 bindReadBuf = GL_FALSE;
1358 break;
1359 case GL_READ_FRAMEBUFFER_EXT:
1360 if (!ctx->Extensions.EXT_framebuffer_blit) {
1361 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1362 return;
1363 }
1364 bindDrawBuf = GL_FALSE;
1365 bindReadBuf = GL_TRUE;
1366 break;
1367 #endif
1368 case GL_FRAMEBUFFER_EXT:
1369 bindDrawBuf = GL_TRUE;
1370 bindReadBuf = GL_TRUE;
1371 break;
1372 default:
1373 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1374 return;
1375 }
1376
1377 if (framebuffer) {
1378 /* Binding a user-created framebuffer object */
1379 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1380 if (newDrawFb == &DummyFramebuffer) {
1381 /* ID was reserved, but no real framebuffer object made yet */
1382 newDrawFb = NULL;
1383 }
1384 else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1385 /* All FBO IDs must be Gen'd */
1386 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1387 return;
1388 }
1389
1390 if (!newDrawFb) {
1391 /* create new framebuffer object */
1392 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1393 if (!newDrawFb) {
1394 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1395 return;
1396 }
1397 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1398 }
1399 newReadFb = newDrawFb;
1400 }
1401 else {
1402 /* Binding the window system framebuffer (which was originally set
1403 * with MakeCurrent).
1404 */
1405 newDrawFb = ctx->WinSysDrawBuffer;
1406 newReadFb = ctx->WinSysReadBuffer;
1407 }
1408
1409 ASSERT(newDrawFb);
1410 ASSERT(newDrawFb != &DummyFramebuffer);
1411
1412 /* save pointers to current/old framebuffers */
1413 oldDrawFb = ctx->DrawBuffer;
1414 oldReadFb = ctx->ReadBuffer;
1415
1416 /* check if really changing bindings */
1417 if (oldDrawFb == newDrawFb)
1418 bindDrawBuf = GL_FALSE;
1419 if (oldReadFb == newReadFb)
1420 bindReadBuf = GL_FALSE;
1421
1422 /*
1423 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1424 *
1425 * We also check if we're beginning and/or ending render-to-texture.
1426 * When a framebuffer with texture attachments is unbound, call
1427 * ctx->Driver.FinishRenderTexture().
1428 * When a framebuffer with texture attachments is bound, call
1429 * ctx->Driver.RenderTexture().
1430 *
1431 * Note that if the ReadBuffer has texture attachments we don't consider
1432 * that a render-to-texture case.
1433 */
1434 if (bindReadBuf) {
1435 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1436
1437 /* check if old readbuffer was render-to-texture */
1438 check_end_texture_render(ctx, oldReadFb);
1439
1440 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1441 }
1442
1443 if (bindDrawBuf) {
1444 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1445
1446 /* check if old read/draw buffers were render-to-texture */
1447 if (!bindReadBuf)
1448 check_end_texture_render(ctx, oldReadFb);
1449
1450 if (oldDrawFb != oldReadFb)
1451 check_end_texture_render(ctx, oldDrawFb);
1452
1453 /* check if newly bound framebuffer has any texture attachments */
1454 check_begin_texture_render(ctx, newDrawFb);
1455
1456 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1457 }
1458
1459 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1460 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1461 }
1462 }
1463
1464
1465 void GLAPIENTRY
1466 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1467 {
1468 GLint i;
1469 GET_CURRENT_CONTEXT(ctx);
1470
1471 ASSERT_OUTSIDE_BEGIN_END(ctx);
1472 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1473
1474 for (i = 0; i < n; i++) {
1475 if (framebuffers[i] > 0) {
1476 struct gl_framebuffer *fb;
1477 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1478 if (fb) {
1479 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1480
1481 /* check if deleting currently bound framebuffer object */
1482 if (ctx->Extensions.EXT_framebuffer_blit) {
1483 /* separate draw/read binding points */
1484 if (fb == ctx->DrawBuffer) {
1485 /* bind default */
1486 ASSERT(fb->RefCount >= 2);
1487 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1488 }
1489 if (fb == ctx->ReadBuffer) {
1490 /* bind default */
1491 ASSERT(fb->RefCount >= 2);
1492 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1493 }
1494 }
1495 else {
1496 /* only one binding point for read/draw buffers */
1497 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1498 /* bind default */
1499 ASSERT(fb->RefCount >= 2);
1500 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1501 }
1502 }
1503
1504 /* remove from hash table immediately, to free the ID */
1505 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1506
1507 if (fb != &DummyFramebuffer) {
1508 /* But the object will not be freed until it's no longer
1509 * bound in any context.
1510 */
1511 _mesa_reference_framebuffer(&fb, NULL);
1512 }
1513 }
1514 }
1515 }
1516 }
1517
1518
1519 void GLAPIENTRY
1520 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1521 {
1522 GET_CURRENT_CONTEXT(ctx);
1523 GLuint first;
1524 GLint i;
1525
1526 ASSERT_OUTSIDE_BEGIN_END(ctx);
1527
1528 if (n < 0) {
1529 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1530 return;
1531 }
1532
1533 if (!framebuffers)
1534 return;
1535
1536 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1537
1538 for (i = 0; i < n; i++) {
1539 GLuint name = first + i;
1540 framebuffers[i] = name;
1541 /* insert dummy placeholder into hash table */
1542 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1543 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1544 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1545 }
1546 }
1547
1548
1549
1550 GLenum GLAPIENTRY
1551 _mesa_CheckFramebufferStatusEXT(GLenum target)
1552 {
1553 struct gl_framebuffer *buffer;
1554 GET_CURRENT_CONTEXT(ctx);
1555
1556 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1557
1558 switch (target) {
1559 #if FEATURE_EXT_framebuffer_blit
1560 case GL_DRAW_FRAMEBUFFER_EXT:
1561 if (!ctx->Extensions.EXT_framebuffer_blit) {
1562 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1563 return 0;
1564 }
1565 buffer = ctx->DrawBuffer;
1566 break;
1567 case GL_READ_FRAMEBUFFER_EXT:
1568 if (!ctx->Extensions.EXT_framebuffer_blit) {
1569 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1570 return 0;
1571 }
1572 buffer = ctx->ReadBuffer;
1573 break;
1574 #endif
1575 case GL_FRAMEBUFFER_EXT:
1576 buffer = ctx->DrawBuffer;
1577 break;
1578 default:
1579 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1580 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1581 }
1582
1583 if (buffer->Name == 0) {
1584 /* The window system / default framebuffer is always complete */
1585 return GL_FRAMEBUFFER_COMPLETE_EXT;
1586 }
1587
1588 /* No need to flush here */
1589
1590 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1591 _mesa_test_framebuffer_completeness(ctx, buffer);
1592 }
1593
1594 return buffer->_Status;
1595 }
1596
1597
1598
1599 /**
1600 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1601 */
1602 static void
1603 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1604 GLenum attachment, GLenum textarget, GLuint texture,
1605 GLint level, GLint zoffset)
1606 {
1607 struct gl_renderbuffer_attachment *att;
1608 struct gl_texture_object *texObj = NULL;
1609 struct gl_framebuffer *fb;
1610 GLboolean error = GL_FALSE;
1611
1612 ASSERT_OUTSIDE_BEGIN_END(ctx);
1613
1614 switch (target) {
1615 case GL_READ_FRAMEBUFFER_EXT:
1616 error = !ctx->Extensions.EXT_framebuffer_blit;
1617 fb = ctx->ReadBuffer;
1618 break;
1619 case GL_DRAW_FRAMEBUFFER_EXT:
1620 error = !ctx->Extensions.EXT_framebuffer_blit;
1621 /* fall-through */
1622 case GL_FRAMEBUFFER_EXT:
1623 fb = ctx->DrawBuffer;
1624 break;
1625 default:
1626 error = GL_TRUE;
1627 }
1628
1629 if (error) {
1630 _mesa_error(ctx, GL_INVALID_ENUM,
1631 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1632 return;
1633 }
1634
1635 ASSERT(fb);
1636
1637 /* check framebuffer binding */
1638 if (fb->Name == 0) {
1639 _mesa_error(ctx, GL_INVALID_OPERATION,
1640 "glFramebufferTexture%sEXT", caller);
1641 return;
1642 }
1643
1644
1645 /* The textarget, level, and zoffset parameters are only validated if
1646 * texture is non-zero.
1647 */
1648 if (texture) {
1649 GLboolean err = GL_TRUE;
1650
1651 texObj = _mesa_lookup_texture(ctx, texture);
1652 if (texObj != NULL) {
1653 if (textarget == 0) {
1654 /* XXX what's the purpose of this? */
1655 err = (texObj->Target != GL_TEXTURE_3D) &&
1656 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1657 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1658 }
1659 else {
1660 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1661 ? !IS_CUBE_FACE(textarget)
1662 : (texObj->Target != textarget);
1663 }
1664 }
1665 else {
1666 /* can't render to a non-existant texture */
1667 _mesa_error(ctx, GL_INVALID_OPERATION,
1668 "glFramebufferTexture%sEXT(non existant texture)",
1669 caller);
1670 return;
1671 }
1672
1673 if (err) {
1674 _mesa_error(ctx, GL_INVALID_OPERATION,
1675 "glFramebufferTexture%sEXT(texture target mismatch)",
1676 caller);
1677 return;
1678 }
1679
1680 if (texObj->Target == GL_TEXTURE_3D) {
1681 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1682 if (zoffset < 0 || zoffset >= maxSize) {
1683 _mesa_error(ctx, GL_INVALID_VALUE,
1684 "glFramebufferTexture%sEXT(zoffset)", caller);
1685 return;
1686 }
1687 }
1688 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1689 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1690 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1691 _mesa_error(ctx, GL_INVALID_VALUE,
1692 "glFramebufferTexture%sEXT(layer)", caller);
1693 return;
1694 }
1695 }
1696
1697 if ((level < 0) ||
1698 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1699 _mesa_error(ctx, GL_INVALID_VALUE,
1700 "glFramebufferTexture%sEXT(level)", caller);
1701 return;
1702 }
1703 }
1704
1705 att = _mesa_get_attachment(ctx, fb, attachment);
1706 if (att == NULL) {
1707 _mesa_error(ctx, GL_INVALID_ENUM,
1708 "glFramebufferTexture%sEXT(attachment)", caller);
1709 return;
1710 }
1711
1712 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1713
1714 _glthread_LOCK_MUTEX(fb->Mutex);
1715 if (texObj) {
1716 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1717 level, zoffset);
1718 /* Set the render-to-texture flag. We'll check this flag in
1719 * glTexImage() and friends to determine if we need to revalidate
1720 * any FBOs that might be rendering into this texture.
1721 * This flag never gets cleared since it's non-trivial to determine
1722 * when all FBOs might be done rendering to this texture. That's OK
1723 * though since it's uncommon to render to a texture then repeatedly
1724 * call glTexImage() to change images in the texture.
1725 */
1726 texObj->_RenderToTexture = GL_TRUE;
1727 }
1728 else {
1729 _mesa_remove_attachment(ctx, att);
1730 }
1731
1732 invalidate_framebuffer(fb);
1733
1734 _glthread_UNLOCK_MUTEX(fb->Mutex);
1735 }
1736
1737
1738
1739 void GLAPIENTRY
1740 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1741 GLenum textarget, GLuint texture, GLint level)
1742 {
1743 GET_CURRENT_CONTEXT(ctx);
1744
1745 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1746 _mesa_error(ctx, GL_INVALID_ENUM,
1747 "glFramebufferTexture1DEXT(textarget)");
1748 return;
1749 }
1750
1751 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1752 level, 0);
1753 }
1754
1755
1756 void GLAPIENTRY
1757 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1758 GLenum textarget, GLuint texture, GLint level)
1759 {
1760 GET_CURRENT_CONTEXT(ctx);
1761
1762 if ((texture != 0) &&
1763 (textarget != GL_TEXTURE_2D) &&
1764 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1765 (!IS_CUBE_FACE(textarget))) {
1766 _mesa_error(ctx, GL_INVALID_OPERATION,
1767 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1768 return;
1769 }
1770
1771 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1772 level, 0);
1773 }
1774
1775
1776 void GLAPIENTRY
1777 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1778 GLenum textarget, GLuint texture,
1779 GLint level, GLint zoffset)
1780 {
1781 GET_CURRENT_CONTEXT(ctx);
1782
1783 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1784 _mesa_error(ctx, GL_INVALID_ENUM,
1785 "glFramebufferTexture3DEXT(textarget)");
1786 return;
1787 }
1788
1789 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1790 level, zoffset);
1791 }
1792
1793
1794 void GLAPIENTRY
1795 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1796 GLuint texture, GLint level, GLint layer)
1797 {
1798 GET_CURRENT_CONTEXT(ctx);
1799
1800 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1801 level, layer);
1802 }
1803
1804
1805 void GLAPIENTRY
1806 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1807 GLenum renderbufferTarget,
1808 GLuint renderbuffer)
1809 {
1810 struct gl_renderbuffer_attachment *att;
1811 struct gl_framebuffer *fb;
1812 struct gl_renderbuffer *rb;
1813 GET_CURRENT_CONTEXT(ctx);
1814
1815 ASSERT_OUTSIDE_BEGIN_END(ctx);
1816
1817 switch (target) {
1818 #if FEATURE_EXT_framebuffer_blit
1819 case GL_DRAW_FRAMEBUFFER_EXT:
1820 if (!ctx->Extensions.EXT_framebuffer_blit) {
1821 _mesa_error(ctx, GL_INVALID_ENUM,
1822 "glFramebufferRenderbufferEXT(target)");
1823 return;
1824 }
1825 fb = ctx->DrawBuffer;
1826 break;
1827 case GL_READ_FRAMEBUFFER_EXT:
1828 if (!ctx->Extensions.EXT_framebuffer_blit) {
1829 _mesa_error(ctx, GL_INVALID_ENUM,
1830 "glFramebufferRenderbufferEXT(target)");
1831 return;
1832 }
1833 fb = ctx->ReadBuffer;
1834 break;
1835 #endif
1836 case GL_FRAMEBUFFER_EXT:
1837 fb = ctx->DrawBuffer;
1838 break;
1839 default:
1840 _mesa_error(ctx, GL_INVALID_ENUM,
1841 "glFramebufferRenderbufferEXT(target)");
1842 return;
1843 }
1844
1845 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1846 _mesa_error(ctx, GL_INVALID_ENUM,
1847 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1848 return;
1849 }
1850
1851 if (fb->Name == 0) {
1852 /* Can't attach new renderbuffers to a window system framebuffer */
1853 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1854 return;
1855 }
1856
1857 att = _mesa_get_attachment(ctx, fb, attachment);
1858 if (att == NULL) {
1859 _mesa_error(ctx, GL_INVALID_ENUM,
1860 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1861 _mesa_lookup_enum_by_nr(attachment));
1862 return;
1863 }
1864
1865 if (renderbuffer) {
1866 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1867 if (!rb) {
1868 _mesa_error(ctx, GL_INVALID_OPERATION,
1869 "glFramebufferRenderbufferEXT(non-existant"
1870 " renderbuffer %u)", renderbuffer);
1871 return;
1872 }
1873 }
1874 else {
1875 /* remove renderbuffer attachment */
1876 rb = NULL;
1877 }
1878
1879 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
1880 rb && rb->Format != MESA_FORMAT_NONE) {
1881 /* make sure the renderbuffer is a depth/stencil format */
1882 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
1883 if (baseFormat != GL_DEPTH_STENCIL) {
1884 _mesa_error(ctx, GL_INVALID_OPERATION,
1885 "glFramebufferRenderbufferEXT(renderbuffer"
1886 " is not DEPTH_STENCIL format)");
1887 return;
1888 }
1889 }
1890
1891
1892 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1893
1894 assert(ctx->Driver.FramebufferRenderbuffer);
1895 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1896
1897 /* Some subsequent GL commands may depend on the framebuffer's visual
1898 * after the binding is updated. Update visual info now.
1899 */
1900 _mesa_update_framebuffer_visual(fb);
1901 }
1902
1903
1904 void GLAPIENTRY
1905 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1906 GLenum pname, GLint *params)
1907 {
1908 const struct gl_renderbuffer_attachment *att;
1909 struct gl_framebuffer *buffer;
1910 GET_CURRENT_CONTEXT(ctx);
1911
1912 ASSERT_OUTSIDE_BEGIN_END(ctx);
1913
1914 switch (target) {
1915 #if FEATURE_EXT_framebuffer_blit
1916 case GL_DRAW_FRAMEBUFFER_EXT:
1917 if (!ctx->Extensions.EXT_framebuffer_blit) {
1918 _mesa_error(ctx, GL_INVALID_ENUM,
1919 "glGetFramebufferAttachmentParameterivEXT(target)");
1920 return;
1921 }
1922 buffer = ctx->DrawBuffer;
1923 break;
1924 case GL_READ_FRAMEBUFFER_EXT:
1925 if (!ctx->Extensions.EXT_framebuffer_blit) {
1926 _mesa_error(ctx, GL_INVALID_ENUM,
1927 "glGetFramebufferAttachmentParameterivEXT(target)");
1928 return;
1929 }
1930 buffer = ctx->ReadBuffer;
1931 break;
1932 #endif
1933 case GL_FRAMEBUFFER_EXT:
1934 buffer = ctx->DrawBuffer;
1935 break;
1936 default:
1937 _mesa_error(ctx, GL_INVALID_ENUM,
1938 "glGetFramebufferAttachmentParameterivEXT(target)");
1939 return;
1940 }
1941
1942 if (buffer->Name == 0) {
1943 /* the default / window-system FBO */
1944 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
1945 }
1946 else {
1947 /* user-created framebuffer FBO */
1948 att = _mesa_get_attachment(ctx, buffer, attachment);
1949 }
1950
1951 if (att == NULL) {
1952 _mesa_error(ctx, GL_INVALID_ENUM,
1953 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1954 return;
1955 }
1956
1957 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1958 /* the depth and stencil attachments must point to the same buffer */
1959 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1960 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1961 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1962 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1963 _mesa_error(ctx, GL_INVALID_OPERATION,
1964 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1965 " attachments differ)");
1966 return;
1967 }
1968 }
1969
1970 /* No need to flush here */
1971
1972 switch (pname) {
1973 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1974 *params = att->Type;
1975 return;
1976 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1977 if (att->Type == GL_RENDERBUFFER_EXT) {
1978 *params = att->Renderbuffer->Name;
1979 }
1980 else if (att->Type == GL_TEXTURE) {
1981 *params = att->Texture->Name;
1982 }
1983 else {
1984 _mesa_error(ctx, GL_INVALID_ENUM,
1985 "glGetFramebufferAttachmentParameterivEXT(pname)");
1986 }
1987 return;
1988 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1989 if (att->Type == GL_TEXTURE) {
1990 *params = att->TextureLevel;
1991 }
1992 else {
1993 _mesa_error(ctx, GL_INVALID_ENUM,
1994 "glGetFramebufferAttachmentParameterivEXT(pname)");
1995 }
1996 return;
1997 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1998 if (att->Type == GL_TEXTURE) {
1999 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2000 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2001 }
2002 else {
2003 *params = 0;
2004 }
2005 }
2006 else {
2007 _mesa_error(ctx, GL_INVALID_ENUM,
2008 "glGetFramebufferAttachmentParameterivEXT(pname)");
2009 }
2010 return;
2011 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2012 if (att->Type == GL_TEXTURE) {
2013 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2014 *params = att->Zoffset;
2015 }
2016 else {
2017 *params = 0;
2018 }
2019 }
2020 else {
2021 _mesa_error(ctx, GL_INVALID_ENUM,
2022 "glGetFramebufferAttachmentParameterivEXT(pname)");
2023 }
2024 return;
2025 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2026 if (!ctx->Extensions.ARB_framebuffer_object) {
2027 _mesa_error(ctx, GL_INVALID_ENUM,
2028 "glGetFramebufferAttachmentParameterivEXT(pname)");
2029 }
2030 else {
2031 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2032 }
2033 return;
2034 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2035 if (!ctx->Extensions.ARB_framebuffer_object) {
2036 _mesa_error(ctx, GL_INVALID_ENUM,
2037 "glGetFramebufferAttachmentParameterivEXT(pname)");
2038 return;
2039 }
2040 else {
2041 gl_format format = att->Renderbuffer->Format;
2042 if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
2043 /* special cases */
2044 *params = GL_INDEX;
2045 }
2046 else {
2047 *params = _mesa_get_format_datatype(format);
2048 }
2049 }
2050 return;
2051 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2052 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2053 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2054 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2055 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2056 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2057 if (!ctx->Extensions.ARB_framebuffer_object) {
2058 _mesa_error(ctx, GL_INVALID_ENUM,
2059 "glGetFramebufferAttachmentParameterivEXT(pname)");
2060 }
2061 else if (att->Texture) {
2062 const struct gl_texture_image *texImage =
2063 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2064 att->TextureLevel);
2065 if (texImage) {
2066 *params = get_component_bits(pname, texImage->_BaseFormat,
2067 texImage->TexFormat);
2068 }
2069 else {
2070 *params = 0;
2071 }
2072 }
2073 else if (att->Renderbuffer) {
2074 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2075 att->Renderbuffer->Format);
2076 }
2077 else {
2078 *params = 0;
2079 }
2080 return;
2081 default:
2082 _mesa_error(ctx, GL_INVALID_ENUM,
2083 "glGetFramebufferAttachmentParameterivEXT(pname)");
2084 return;
2085 }
2086 }
2087
2088
2089 void GLAPIENTRY
2090 _mesa_GenerateMipmapEXT(GLenum target)
2091 {
2092 struct gl_texture_object *texObj;
2093 GET_CURRENT_CONTEXT(ctx);
2094
2095 ASSERT_OUTSIDE_BEGIN_END(ctx);
2096 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2097
2098 switch (target) {
2099 case GL_TEXTURE_1D:
2100 case GL_TEXTURE_2D:
2101 case GL_TEXTURE_3D:
2102 case GL_TEXTURE_CUBE_MAP:
2103 /* OK, legal value */
2104 break;
2105 default:
2106 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
2107 return;
2108 }
2109
2110 texObj = _mesa_get_current_tex_object(ctx, target);
2111
2112 if (texObj->BaseLevel >= texObj->MaxLevel) {
2113 /* nothing to do */
2114 return;
2115 }
2116
2117 _mesa_lock_texture(ctx, texObj);
2118 if (target == GL_TEXTURE_CUBE_MAP) {
2119 GLuint face;
2120 for (face = 0; face < 6; face++)
2121 ctx->Driver.GenerateMipmap(ctx,
2122 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2123 texObj);
2124 }
2125 else {
2126 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2127 }
2128 _mesa_unlock_texture(ctx, texObj);
2129 }
2130
2131
2132 #if FEATURE_EXT_framebuffer_blit
2133
2134 static const struct gl_renderbuffer_attachment *
2135 find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
2136 {
2137 GLuint i;
2138 for (i = 0; i < Elements(fb->Attachment); i++) {
2139 if (fb->Attachment[i].Renderbuffer == rb)
2140 return &fb->Attachment[i];
2141 }
2142 return NULL;
2143 }
2144
2145
2146
2147 /**
2148 * Blit rectangular region, optionally from one framebuffer to another.
2149 *
2150 * Note, if the src buffer is multisampled and the dest is not, this is
2151 * when the samples must be resolved to a single color.
2152 */
2153 void GLAPIENTRY
2154 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2155 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2156 GLbitfield mask, GLenum filter)
2157 {
2158 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2159 GL_DEPTH_BUFFER_BIT |
2160 GL_STENCIL_BUFFER_BIT);
2161 const struct gl_framebuffer *readFb, *drawFb;
2162 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2163 GET_CURRENT_CONTEXT(ctx);
2164
2165 ASSERT_OUTSIDE_BEGIN_END(ctx);
2166 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2167
2168 if (ctx->NewState) {
2169 _mesa_update_state(ctx);
2170 }
2171
2172 readFb = ctx->ReadBuffer;
2173 drawFb = ctx->DrawBuffer;
2174
2175 if (!readFb || !drawFb) {
2176 /* This will normally never happen but someday we may want to
2177 * support MakeCurrent() with no drawables.
2178 */
2179 return;
2180 }
2181
2182 /* check for complete framebuffers */
2183 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2184 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2185 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2186 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2187 return;
2188 }
2189
2190 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2191 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2192 return;
2193 }
2194
2195 if (mask & ~legalMaskBits) {
2196 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2197 return;
2198 }
2199
2200 /* depth/stencil must be blitted with nearest filtering */
2201 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2202 && filter != GL_NEAREST) {
2203 _mesa_error(ctx, GL_INVALID_OPERATION,
2204 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2205 return;
2206 }
2207
2208 /* get color read/draw renderbuffers */
2209 if (mask & GL_COLOR_BUFFER_BIT) {
2210 colorReadRb = readFb->_ColorReadBuffer;
2211 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2212 }
2213 else {
2214 colorReadRb = colorDrawRb = NULL;
2215 }
2216
2217 if (mask & GL_STENCIL_BUFFER_BIT) {
2218 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2219 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2220 if (!readRb ||
2221 !drawRb ||
2222 _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2223 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2224 _mesa_error(ctx, GL_INVALID_OPERATION,
2225 "glBlitFramebufferEXT(stencil buffer size mismatch");
2226 return;
2227 }
2228 }
2229
2230 if (mask & GL_DEPTH_BUFFER_BIT) {
2231 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2232 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2233 if (!readRb ||
2234 !drawRb ||
2235 _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2236 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2237 _mesa_error(ctx, GL_INVALID_OPERATION,
2238 "glBlitFramebufferEXT(depth buffer size mismatch");
2239 return;
2240 }
2241 }
2242
2243 if (readFb->Visual.samples > 0 &&
2244 drawFb->Visual.samples > 0 &&
2245 readFb->Visual.samples != drawFb->Visual.samples) {
2246 _mesa_error(ctx, GL_INVALID_OPERATION,
2247 "glBlitFramebufferEXT(mismatched samples");
2248 return;
2249 }
2250
2251 /* extra checks for multisample copies... */
2252 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2253 /* src and dest region sizes must be the same */
2254 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2255 srcY1 - srcY0 != dstY1 - dstY0) {
2256 _mesa_error(ctx, GL_INVALID_OPERATION,
2257 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2258 return;
2259 }
2260
2261 /* color formats must match */
2262 if (colorReadRb &&
2263 colorDrawRb &&
2264 colorReadRb->Format != colorDrawRb->Format) {
2265 _mesa_error(ctx, GL_INVALID_OPERATION,
2266 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2267 return;
2268 }
2269 }
2270
2271 if (!ctx->Extensions.EXT_framebuffer_blit) {
2272 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2273 return;
2274 }
2275
2276 /* Debug code */
2277 if (DEBUG_BLIT) {
2278 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2279 " 0x%x, 0x%x)\n",
2280 srcX0, srcY0, srcX1, srcY1,
2281 dstX0, dstY0, dstX1, dstY1,
2282 mask, filter);
2283 if (colorReadRb) {
2284 const struct gl_renderbuffer_attachment *att;
2285
2286 att = find_attachment(readFb, colorReadRb);
2287 printf(" Src FBO %u RB %u (%dx%d) ",
2288 readFb->Name, colorReadRb->Name,
2289 colorReadRb->Width, colorReadRb->Height);
2290 if (att && att->Texture) {
2291 printf("Tex %u tgt 0x%x level %u face %u",
2292 att->Texture->Name,
2293 att->Texture->Target,
2294 att->TextureLevel,
2295 att->CubeMapFace);
2296 }
2297 printf("\n");
2298
2299 att = find_attachment(drawFb, colorDrawRb);
2300 printf(" Dst FBO %u RB %u (%dx%d) ",
2301 drawFb->Name, colorDrawRb->Name,
2302 colorDrawRb->Width, colorDrawRb->Height);
2303 if (att && att->Texture) {
2304 printf("Tex %u tgt 0x%x level %u face %u",
2305 att->Texture->Name,
2306 att->Texture->Target,
2307 att->TextureLevel,
2308 att->CubeMapFace);
2309 }
2310 printf("\n");
2311 }
2312 }
2313
2314 ASSERT(ctx->Driver.BlitFramebuffer);
2315 ctx->Driver.BlitFramebuffer(ctx,
2316 srcX0, srcY0, srcX1, srcY1,
2317 dstX0, dstY0, dstX1, dstY1,
2318 mask, filter);
2319 }
2320 #endif /* FEATURE_EXT_framebuffer_blit */
2321
2322 #if FEATURE_ARB_geometry_shader4
2323 void GLAPIENTRY
2324 _mesa_FramebufferTextureARB(GLenum target, GLenum attachment,
2325 GLuint texture, GLint level)
2326 {
2327 GET_CURRENT_CONTEXT(ctx);
2328 _mesa_error(ctx, GL_INVALID_OPERATION,
2329 "glFramebufferTextureARB "
2330 "not implemented!");
2331 }
2332
2333 void GLAPIENTRY
2334 _mesa_FramebufferTextureFaceARB(GLenum target, GLenum attachment,
2335 GLuint texture, GLint level, GLenum face)
2336 {
2337 GET_CURRENT_CONTEXT(ctx);
2338 _mesa_error(ctx, GL_INVALID_OPERATION,
2339 "glFramebufferTextureFaceARB "
2340 "not implemented!");
2341 }
2342 #endif /* FEATURE_ARB_geometry_shader4 */