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