mesa: fix problem freeing framebuffer/renderbuffer objects
[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 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * Authors:
28 * Brian Paul
29 */
30
31
32 #include "buffers.h"
33 #include "context.h"
34 #include "fbobject.h"
35 #include "framebuffer.h"
36 #include "hash.h"
37 #include "mipmap.h"
38 #include "renderbuffer.h"
39 #include "state.h"
40 #include "teximage.h"
41 #include "texobj.h"
42 #include "texstore.h"
43
44
45 /**
46 * Notes:
47 *
48 * None of the GL_EXT_framebuffer_object functions are compiled into
49 * display lists.
50 */
51
52
53
54 /*
55 * When glGenRender/FramebuffersEXT() is called we insert pointers to
56 * these placeholder objects into the hash table.
57 * Later, when the object ID is first bound, we replace the placeholder
58 * with the real frame/renderbuffer.
59 */
60 static struct gl_framebuffer DummyFramebuffer;
61 static struct gl_renderbuffer DummyRenderbuffer;
62
63
64 #define IS_CUBE_FACE(TARGET) \
65 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
66 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
67
68
69 static void
70 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
71 {
72 /* no op */
73 }
74
75 static void
76 delete_dummy_framebuffer(struct gl_framebuffer *fb)
77 {
78 /* no op */
79 }
80
81
82 void
83 _mesa_init_fbobjects(GLcontext *ctx)
84 {
85 DummyFramebuffer.Delete = delete_dummy_framebuffer;
86 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
87 }
88
89
90 /**
91 * Helper routine for getting a gl_renderbuffer.
92 */
93 struct gl_renderbuffer *
94 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
95 {
96 struct gl_renderbuffer *rb;
97
98 if (id == 0)
99 return NULL;
100
101 rb = (struct gl_renderbuffer *)
102 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
103 return rb;
104 }
105
106
107 /**
108 * Helper routine for getting a gl_framebuffer.
109 */
110 struct gl_framebuffer *
111 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
112 {
113 struct gl_framebuffer *fb;
114
115 if (id == 0)
116 return NULL;
117
118 fb = (struct gl_framebuffer *)
119 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
120 return fb;
121 }
122
123
124 /**
125 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
126 * gl_renderbuffer_attachment object.
127 */
128 struct gl_renderbuffer_attachment *
129 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
130 GLenum attachment)
131 {
132 GLuint i;
133
134 switch (attachment) {
135 case GL_COLOR_ATTACHMENT0_EXT:
136 case GL_COLOR_ATTACHMENT1_EXT:
137 case GL_COLOR_ATTACHMENT2_EXT:
138 case GL_COLOR_ATTACHMENT3_EXT:
139 case GL_COLOR_ATTACHMENT4_EXT:
140 case GL_COLOR_ATTACHMENT5_EXT:
141 case GL_COLOR_ATTACHMENT6_EXT:
142 case GL_COLOR_ATTACHMENT7_EXT:
143 case GL_COLOR_ATTACHMENT8_EXT:
144 case GL_COLOR_ATTACHMENT9_EXT:
145 case GL_COLOR_ATTACHMENT10_EXT:
146 case GL_COLOR_ATTACHMENT11_EXT:
147 case GL_COLOR_ATTACHMENT12_EXT:
148 case GL_COLOR_ATTACHMENT13_EXT:
149 case GL_COLOR_ATTACHMENT14_EXT:
150 case GL_COLOR_ATTACHMENT15_EXT:
151 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
152 if (i >= ctx->Const.MaxColorAttachments) {
153 return NULL;
154 }
155 return &fb->Attachment[BUFFER_COLOR0 + i];
156 case GL_DEPTH_ATTACHMENT_EXT:
157 return &fb->Attachment[BUFFER_DEPTH];
158 case GL_STENCIL_ATTACHMENT_EXT:
159 return &fb->Attachment[BUFFER_STENCIL];
160 default:
161 return NULL;
162 }
163 }
164
165
166 /**
167 * Remove any texture or renderbuffer attached to the given attachment
168 * point. Update reference counts, etc.
169 */
170 void
171 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
172 {
173 if (att->Type == GL_TEXTURE) {
174 ASSERT(att->Texture);
175 att->Texture->RefCount--;
176 if (att->Texture->RefCount == 0) {
177 ctx->Driver.DeleteTexture(ctx, att->Texture);
178 }
179 else {
180 /* tell driver that we're done rendering to this texture. */
181 if (ctx->Driver.FinishRenderTexture) {
182 ctx->Driver.FinishRenderTexture(ctx, att);
183 }
184 }
185 att->Texture = NULL;
186 }
187 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
188 ASSERT(att->Renderbuffer);
189 ASSERT(!att->Texture);
190 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
191 }
192 att->Type = GL_NONE;
193 att->Complete = GL_TRUE;
194 }
195
196
197 /**
198 * Bind a texture object to an attachment point.
199 * The previous binding, if any, will be removed first.
200 */
201 void
202 _mesa_set_texture_attachment(GLcontext *ctx,
203 struct gl_framebuffer *fb,
204 struct gl_renderbuffer_attachment *att,
205 struct gl_texture_object *texObj,
206 GLenum texTarget, GLuint level, GLuint zoffset)
207 {
208 if (att->Texture == texObj) {
209 /* re-attaching same texture */
210 ASSERT(att->Type == GL_TEXTURE);
211 }
212 else {
213 /* new attachment */
214 _mesa_remove_attachment(ctx, att);
215 att->Type = GL_TEXTURE;
216 att->Texture = texObj;
217 texObj->RefCount++;
218 }
219
220 /* always update these fields */
221 att->TextureLevel = level;
222 if (IS_CUBE_FACE(texTarget)) {
223 att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
224 }
225 else {
226 att->CubeMapFace = 0;
227 }
228 att->Zoffset = zoffset;
229 att->Complete = GL_FALSE;
230
231 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
232 ctx->Driver.RenderTexture(ctx, fb, att);
233 }
234 }
235
236
237 /**
238 * Bind a renderbuffer to an attachment point.
239 * The previous binding, if any, will be removed first.
240 */
241 void
242 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
243 struct gl_renderbuffer_attachment *att,
244 struct gl_renderbuffer *rb)
245 {
246 /* XXX check if re-doing same attachment, exit early */
247 _mesa_remove_attachment(ctx, att);
248 att->Type = GL_RENDERBUFFER_EXT;
249 att->Texture = NULL; /* just to be safe */
250 att->Complete = GL_FALSE;
251 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
252 }
253
254
255 /**
256 * Fallback for ctx->Driver.FramebufferRenderbuffer()
257 * Attach a renderbuffer object to a framebuffer object.
258 */
259 void
260 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
261 GLenum attachment, struct gl_renderbuffer *rb)
262 {
263 struct gl_renderbuffer_attachment *att;
264
265 _glthread_LOCK_MUTEX(fb->Mutex);
266
267 att = _mesa_get_attachment(ctx, fb, attachment);
268 ASSERT(att);
269 if (rb) {
270 _mesa_set_renderbuffer_attachment(ctx, att, rb);
271 }
272 else {
273 _mesa_remove_attachment(ctx, att);
274 }
275
276 _glthread_UNLOCK_MUTEX(fb->Mutex);
277 }
278
279
280 /**
281 * Test if an attachment point is complete and update its Complete field.
282 * \param format if GL_COLOR, this is a color attachment point,
283 * if GL_DEPTH, this is a depth component attachment point,
284 * if GL_STENCIL, this is a stencil component attachment point.
285 */
286 static void
287 test_attachment_completeness(const GLcontext *ctx, GLenum format,
288 struct gl_renderbuffer_attachment *att)
289 {
290 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
291
292 /* assume complete */
293 att->Complete = GL_TRUE;
294
295 /* Look for reasons why the attachment might be incomplete */
296 if (att->Type == GL_TEXTURE) {
297 const struct gl_texture_object *texObj = att->Texture;
298 struct gl_texture_image *texImage;
299
300 if (!texObj) {
301 att->Complete = GL_FALSE;
302 return;
303 }
304
305 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
306 if (!texImage) {
307 att->Complete = GL_FALSE;
308 return;
309 }
310 if (texImage->Width < 1 || texImage->Height < 1) {
311 att->Complete = GL_FALSE;
312 return;
313 }
314 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
315 att->Complete = GL_FALSE;
316 return;
317 }
318
319 if (format == GL_COLOR) {
320 if (texImage->TexFormat->BaseFormat != GL_RGB &&
321 texImage->TexFormat->BaseFormat != GL_RGBA) {
322 att->Complete = GL_FALSE;
323 return;
324 }
325 }
326 else if (format == GL_DEPTH) {
327 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
328 /* OK */
329 }
330 else if (ctx->Extensions.EXT_packed_depth_stencil &&
331 texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
332 /* OK */
333 }
334 else {
335 att->Complete = GL_FALSE;
336 return;
337 }
338 }
339 else {
340 /* no such thing as stencil textures */
341 att->Complete = GL_FALSE;
342 return;
343 }
344 }
345 else if (att->Type == GL_RENDERBUFFER_EXT) {
346 ASSERT(att->Renderbuffer);
347 if (!att->Renderbuffer->InternalFormat ||
348 att->Renderbuffer->Width < 1 ||
349 att->Renderbuffer->Height < 1) {
350 att->Complete = GL_FALSE;
351 return;
352 }
353 if (format == GL_COLOR) {
354 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
355 att->Renderbuffer->_BaseFormat != GL_RGBA) {
356 ASSERT(att->Renderbuffer->RedBits);
357 ASSERT(att->Renderbuffer->GreenBits);
358 ASSERT(att->Renderbuffer->BlueBits);
359 att->Complete = GL_FALSE;
360 return;
361 }
362 }
363 else if (format == GL_DEPTH) {
364 ASSERT(att->Renderbuffer->DepthBits);
365 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
366 /* OK */
367 }
368 else if (ctx->Extensions.EXT_packed_depth_stencil &&
369 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
370 /* OK */
371 }
372 else {
373 att->Complete = GL_FALSE;
374 return;
375 }
376 }
377 else {
378 assert(format == GL_STENCIL);
379 ASSERT(att->Renderbuffer->StencilBits);
380 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
381 /* OK */
382 }
383 else if (ctx->Extensions.EXT_packed_depth_stencil &&
384 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
385 /* OK */
386 }
387 else {
388 att->Complete = GL_FALSE;
389 return;
390 }
391 }
392 }
393 else {
394 ASSERT(att->Type == GL_NONE);
395 /* complete */
396 return;
397 }
398 }
399
400
401 /**
402 * Helpful for debugging
403 */
404 static void
405 fbo_incomplete(const char *msg, int index)
406 {
407 (void) msg;
408 (void) index;
409 /*
410 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
411 */
412 }
413
414
415 /**
416 * Test if the given framebuffer object is complete and update its
417 * Status field with the results.
418 * Also update the framebuffer's Width and Height fields if the
419 * framebuffer is complete.
420 */
421 void
422 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
423 {
424 GLuint numImages, width = 0, height = 0;
425 GLenum intFormat = GL_NONE;
426 GLuint w = 0, h = 0;
427 GLint i;
428 GLuint j;
429
430 assert(fb->Name != 0);
431
432 numImages = 0;
433 fb->Width = 0;
434 fb->Height = 0;
435
436 /* Start at -2 to more easily loop over all attachment points */
437 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
438 struct gl_renderbuffer_attachment *att;
439 GLenum f;
440
441 if (i == -2) {
442 att = &fb->Attachment[BUFFER_DEPTH];
443 test_attachment_completeness(ctx, GL_DEPTH, att);
444 if (!att->Complete) {
445 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
446 fbo_incomplete("depth attachment incomplete", -1);
447 return;
448 }
449 }
450 else if (i == -1) {
451 att = &fb->Attachment[BUFFER_STENCIL];
452 test_attachment_completeness(ctx, GL_STENCIL, att);
453 if (!att->Complete) {
454 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
455 fbo_incomplete("stencil attachment incomplete", -1);
456 return;
457 }
458 }
459 else {
460 att = &fb->Attachment[BUFFER_COLOR0 + i];
461 test_attachment_completeness(ctx, GL_COLOR, att);
462 if (!att->Complete) {
463 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
464 fbo_incomplete("color attachment incomplete", i);
465 return;
466 }
467 }
468
469 if (att->Type == GL_TEXTURE) {
470 const struct gl_texture_image *texImg
471 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
472 w = texImg->Width;
473 h = texImg->Height;
474 f = texImg->_BaseFormat;
475 numImages++;
476 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
477 && f != GL_DEPTH_STENCIL_EXT) {
478 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
479 fbo_incomplete("texture attachment incomplete", -1);
480 return;
481 }
482 }
483 else if (att->Type == GL_RENDERBUFFER_EXT) {
484 w = att->Renderbuffer->Width;
485 h = att->Renderbuffer->Height;
486 f = att->Renderbuffer->InternalFormat;
487 numImages++;
488 }
489 else {
490 assert(att->Type == GL_NONE);
491 continue;
492 }
493
494 if (numImages == 1) {
495 /* set required width, height and format */
496 width = w;
497 height = h;
498 if (i >= 0)
499 intFormat = f;
500 }
501 else {
502 /* check that width, height, format are same */
503 if (w != width || h != height) {
504 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
505 fbo_incomplete("width or height mismatch", -1);
506 return;
507 }
508 if (intFormat != GL_NONE && f != intFormat) {
509 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
510 fbo_incomplete("format mismatch", -1);
511 return;
512 }
513 }
514 }
515
516 /* Check that all DrawBuffers are present */
517 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
518 if (fb->ColorDrawBuffer[j] != GL_NONE) {
519 const struct gl_renderbuffer_attachment *att
520 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
521 assert(att);
522 if (att->Type == GL_NONE) {
523 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
524 fbo_incomplete("missing drawbuffer", j);
525 return;
526 }
527 }
528 }
529
530 /* Check that the ReadBuffer is present */
531 if (fb->ColorReadBuffer != GL_NONE) {
532 const struct gl_renderbuffer_attachment *att
533 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
534 assert(att);
535 if (att->Type == GL_NONE) {
536 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
537 fbo_incomplete("missing readbuffer", -1);
538 return;
539 }
540 }
541
542 if (numImages == 0) {
543 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
544 fbo_incomplete("no attachments", -1);
545 return;
546 }
547
548 /*
549 * If we get here, the framebuffer is complete!
550 */
551 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
552 fb->Width = w;
553 fb->Height = h;
554 }
555
556
557 GLboolean GLAPIENTRY
558 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
559 {
560 GET_CURRENT_CONTEXT(ctx);
561 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
562 if (renderbuffer) {
563 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
564 if (rb != NULL && rb != &DummyRenderbuffer)
565 return GL_TRUE;
566 }
567 return GL_FALSE;
568 }
569
570
571 void GLAPIENTRY
572 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
573 {
574 struct gl_renderbuffer *newRb;
575 GET_CURRENT_CONTEXT(ctx);
576
577 ASSERT_OUTSIDE_BEGIN_END(ctx);
578
579 if (target != GL_RENDERBUFFER_EXT) {
580 _mesa_error(ctx, GL_INVALID_ENUM,
581 "glBindRenderbufferEXT(target)");
582 return;
583 }
584
585 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
586 /* The above doesn't fully flush the drivers in the way that a
587 * glFlush does, but that is required here:
588 */
589 if (ctx->Driver.Flush)
590 ctx->Driver.Flush(ctx);
591
592
593 if (renderbuffer) {
594 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
595 if (newRb == &DummyRenderbuffer) {
596 /* ID was reserved, but no real renderbuffer object made yet */
597 newRb = NULL;
598 }
599 if (!newRb) {
600 /* create new renderbuffer object */
601 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
602 if (!newRb) {
603 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
604 return;
605 }
606 ASSERT(newRb->AllocStorage);
607 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
608 newRb->RefCount = 1; /* referenced by hash table */
609 }
610 }
611 else {
612 newRb = NULL;
613 }
614
615 ASSERT(newRb != &DummyRenderbuffer);
616
617 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
618 }
619
620
621 void GLAPIENTRY
622 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
623 {
624 GLint i;
625 GET_CURRENT_CONTEXT(ctx);
626
627 ASSERT_OUTSIDE_BEGIN_END(ctx);
628 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
629
630 for (i = 0; i < n; i++) {
631 if (renderbuffers[i] > 0) {
632 struct gl_renderbuffer *rb;
633 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
634 if (rb) {
635 /* check if deleting currently bound renderbuffer object */
636 if (rb == ctx->CurrentRenderbuffer) {
637 /* bind default */
638 ASSERT(rb->RefCount >= 2);
639 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
640 }
641
642 /* Remove from hash table immediately, to free the ID.
643 * But the object will not be freed until it's no longer
644 * referenced anywhere else.
645 */
646 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
647
648 if (rb != &DummyRenderbuffer) {
649 /* no longer referenced by hash table */
650 _mesa_reference_renderbuffer(&rb, NULL);
651 }
652 }
653 }
654 }
655 }
656
657
658 void GLAPIENTRY
659 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
660 {
661 GET_CURRENT_CONTEXT(ctx);
662 GLuint first;
663 GLint i;
664
665 ASSERT_OUTSIDE_BEGIN_END(ctx);
666
667 if (n < 0) {
668 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
669 return;
670 }
671
672 if (!renderbuffers)
673 return;
674
675 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
676
677 for (i = 0; i < n; i++) {
678 GLuint name = first + i;
679 renderbuffers[i] = name;
680 /* insert dummy placeholder into hash table */
681 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
682 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
683 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
684 }
685 }
686
687
688 /**
689 * Given an internal format token for a render buffer, return the
690 * corresponding base format.
691 * This is very similar to _mesa_base_tex_format() but the set of valid
692 * internal formats is somewhat different.
693 *
694 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
695 * GL_DEPTH_STENCIL_EXT or zero if error.
696 */
697 GLenum
698 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
699 {
700 switch (internalFormat) {
701 case GL_RGB:
702 case GL_R3_G3_B2:
703 case GL_RGB4:
704 case GL_RGB5:
705 case GL_RGB8:
706 case GL_RGB10:
707 case GL_RGB12:
708 case GL_RGB16:
709 return GL_RGB;
710 case GL_RGBA:
711 case GL_RGBA2:
712 case GL_RGBA4:
713 case GL_RGB5_A1:
714 case GL_RGBA8:
715 case GL_RGB10_A2:
716 case GL_RGBA12:
717 case GL_RGBA16:
718 return GL_RGBA;
719 case GL_STENCIL_INDEX:
720 case GL_STENCIL_INDEX1_EXT:
721 case GL_STENCIL_INDEX4_EXT:
722 case GL_STENCIL_INDEX8_EXT:
723 case GL_STENCIL_INDEX16_EXT:
724 return GL_STENCIL_INDEX;
725 case GL_DEPTH_COMPONENT:
726 case GL_DEPTH_COMPONENT16:
727 case GL_DEPTH_COMPONENT24:
728 case GL_DEPTH_COMPONENT32:
729 return GL_DEPTH_COMPONENT;
730 case GL_DEPTH_STENCIL_EXT:
731 case GL_DEPTH24_STENCIL8_EXT:
732 if (ctx->Extensions.EXT_packed_depth_stencil)
733 return GL_DEPTH_STENCIL_EXT;
734 else
735 return 0;
736 /* XXX add floating point formats eventually */
737 default:
738 return 0;
739 }
740 }
741
742
743 void GLAPIENTRY
744 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
745 GLsizei width, GLsizei height)
746 {
747 struct gl_renderbuffer *rb;
748 GLenum baseFormat;
749 GET_CURRENT_CONTEXT(ctx);
750
751 ASSERT_OUTSIDE_BEGIN_END(ctx);
752
753 if (target != GL_RENDERBUFFER_EXT) {
754 _mesa_error(ctx, GL_INVALID_ENUM, "glRenderbufferStorageEXT(target)");
755 return;
756 }
757
758 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
759 if (baseFormat == 0) {
760 _mesa_error(ctx, GL_INVALID_ENUM,
761 "glRenderbufferStorageEXT(internalFormat)");
762 return;
763 }
764
765 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
766 _mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(width)");
767 return;
768 }
769
770 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
771 _mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(height)");
772 return;
773 }
774
775 rb = ctx->CurrentRenderbuffer;
776
777 if (!rb) {
778 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferStorageEXT");
779 return;
780 }
781
782 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
783
784 if (rb->InternalFormat == internalFormat &&
785 rb->Width == (GLuint) width &&
786 rb->Height == (GLuint) height) {
787 /* no change in allocation needed */
788 return;
789 }
790
791 /* These MUST get set by the AllocStorage func */
792 rb->_ActualFormat = 0;
793 rb->RedBits =
794 rb->GreenBits =
795 rb->BlueBits =
796 rb->AlphaBits =
797 rb->IndexBits =
798 rb->DepthBits =
799 rb->StencilBits = 0;
800
801 /* Now allocate the storage */
802 ASSERT(rb->AllocStorage);
803 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
804 /* No error - check/set fields now */
805 assert(rb->_ActualFormat);
806 assert(rb->Width == (GLuint) width);
807 assert(rb->Height == (GLuint) height);
808 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
809 rb->DepthBits || rb->StencilBits || rb->IndexBits);
810 rb->InternalFormat = internalFormat;
811 rb->_BaseFormat = baseFormat;
812 }
813 else {
814 /* Probably ran out of memory - clear the fields */
815 rb->Width = 0;
816 rb->Height = 0;
817 rb->InternalFormat = GL_NONE;
818 rb->_ActualFormat = GL_NONE;
819 rb->_BaseFormat = GL_NONE;
820 rb->RedBits =
821 rb->GreenBits =
822 rb->BlueBits =
823 rb->AlphaBits =
824 rb->IndexBits =
825 rb->DepthBits =
826 rb->StencilBits = 0;
827 }
828
829 /*
830 test_framebuffer_completeness(ctx, fb);
831 */
832 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
833 * points???
834 */
835 }
836
837
838 void GLAPIENTRY
839 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
840 {
841 GET_CURRENT_CONTEXT(ctx);
842
843 ASSERT_OUTSIDE_BEGIN_END(ctx);
844
845 if (target != GL_RENDERBUFFER_EXT) {
846 _mesa_error(ctx, GL_INVALID_ENUM,
847 "glGetRenderbufferParameterivEXT(target)");
848 return;
849 }
850
851 if (!ctx->CurrentRenderbuffer) {
852 _mesa_error(ctx, GL_INVALID_OPERATION,
853 "glGetRenderbufferParameterivEXT");
854 return;
855 }
856
857 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
858
859 switch (pname) {
860 case GL_RENDERBUFFER_WIDTH_EXT:
861 *params = ctx->CurrentRenderbuffer->Width;
862 return;
863 case GL_RENDERBUFFER_HEIGHT_EXT:
864 *params = ctx->CurrentRenderbuffer->Height;
865 return;
866 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
867 *params = ctx->CurrentRenderbuffer->InternalFormat;
868 return;
869 case GL_RENDERBUFFER_RED_SIZE_EXT:
870 *params = ctx->CurrentRenderbuffer->RedBits;
871 break;
872 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
873 *params = ctx->CurrentRenderbuffer->GreenBits;
874 break;
875 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
876 *params = ctx->CurrentRenderbuffer->BlueBits;
877 break;
878 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
879 *params = ctx->CurrentRenderbuffer->AlphaBits;
880 break;
881 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
882 *params = ctx->CurrentRenderbuffer->DepthBits;
883 break;
884 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
885 *params = ctx->CurrentRenderbuffer->StencilBits;
886 break;
887 default:
888 _mesa_error(ctx, GL_INVALID_ENUM,
889 "glGetRenderbufferParameterivEXT(target)");
890 return;
891 }
892 }
893
894
895 GLboolean GLAPIENTRY
896 _mesa_IsFramebufferEXT(GLuint framebuffer)
897 {
898 GET_CURRENT_CONTEXT(ctx);
899 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
900 if (framebuffer) {
901 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
902 if (rb != NULL && rb != &DummyFramebuffer)
903 return GL_TRUE;
904 }
905 return GL_FALSE;
906 }
907
908
909 static void
910 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
911 {
912 GLuint i;
913 ASSERT(ctx->Driver.RenderTexture);
914 for (i = 0; i < BUFFER_COUNT; i++) {
915 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
916 struct gl_texture_object *texObj = att->Texture;
917 if (texObj
918 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
919 ctx->Driver.RenderTexture(ctx, fb, att);
920 }
921 }
922 }
923
924
925 /**
926 * Examine all the framebuffer's attachments to see if any are textures.
927 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
928 * notify the device driver that the texture image may have changed.
929 */
930 static void
931 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
932 {
933 if (ctx->Driver.FinishRenderTexture) {
934 GLuint i;
935 for (i = 0; i < BUFFER_COUNT; i++) {
936 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
937 if (att->Texture && att->Renderbuffer) {
938 ctx->Driver.FinishRenderTexture(ctx, att);
939 }
940 }
941 }
942 }
943
944
945 void GLAPIENTRY
946 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
947 {
948 struct gl_framebuffer *newFb, *newFbread;
949 GLboolean bindReadBuf, bindDrawBuf;
950 GET_CURRENT_CONTEXT(ctx);
951
952 ASSERT_OUTSIDE_BEGIN_END(ctx);
953
954 if (!ctx->Extensions.EXT_framebuffer_object) {
955 _mesa_error(ctx, GL_INVALID_OPERATION,
956 "glBindFramebufferEXT(unsupported)");
957 return;
958 }
959
960 switch (target) {
961 #if FEATURE_EXT_framebuffer_blit
962 case GL_DRAW_FRAMEBUFFER_EXT:
963 if (!ctx->Extensions.EXT_framebuffer_blit) {
964 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
965 return;
966 }
967 bindDrawBuf = GL_TRUE;
968 bindReadBuf = GL_FALSE;
969 break;
970 case GL_READ_FRAMEBUFFER_EXT:
971 if (!ctx->Extensions.EXT_framebuffer_blit) {
972 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
973 return;
974 }
975 bindDrawBuf = GL_FALSE;
976 bindReadBuf = GL_TRUE;
977 break;
978 #endif
979 case GL_FRAMEBUFFER_EXT:
980 bindDrawBuf = GL_TRUE;
981 bindReadBuf = GL_TRUE;
982 break;
983 default:
984 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
985 return;
986 }
987
988 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
989 if (ctx->Driver.Flush) {
990 ctx->Driver.Flush(ctx);
991 }
992 if (framebuffer) {
993 /* Binding a user-created framebuffer object */
994 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
995 if (newFb == &DummyFramebuffer) {
996 /* ID was reserved, but no real framebuffer object made yet */
997 newFb = NULL;
998 }
999 if (!newFb) {
1000 /* create new framebuffer object */
1001 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1002 if (!newFb) {
1003 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1004 return;
1005 }
1006 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
1007 }
1008 newFbread = newFb;
1009 }
1010 else {
1011 /* Binding the window system framebuffer (which was originally set
1012 * with MakeCurrent).
1013 */
1014 newFb = ctx->WinSysDrawBuffer;
1015 newFbread = ctx->WinSysReadBuffer;
1016 }
1017
1018 ASSERT(newFb);
1019 ASSERT(newFb != &DummyFramebuffer);
1020
1021 /*
1022 * XXX check if re-binding same buffer and skip some of this code.
1023 */
1024
1025 /* for window-framebuffers, re-initialize the fbo values, as they
1026 could be wrong (makecurrent with a new drawable while still a fbo
1027 was bound will lead to default init fbo values).
1028 note that therefore the context ReadBuffer/DrawBuffer values are not
1029 valid while fbo's are bound!!! */
1030 if (bindReadBuf) {
1031 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1032 if (!newFbread->Name) {
1033 _mesa_readbuffer_update_fields(ctx, ctx->Pixel.ReadBuffer);
1034 }
1035 }
1036
1037 if (bindDrawBuf) {
1038 /* check if old FB had any texture attachments */
1039 check_end_texture_render(ctx, ctx->DrawBuffer);
1040 /* check if time to delete this framebuffer */
1041 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1042 if (!newFb->Name) {
1043 GLuint i;
1044 GLenum buffers[MAX_DRAW_BUFFERS];
1045 for(i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
1046 buffers[i] = ctx->Color.DrawBuffer[i];
1047 }
1048 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
1049 }
1050 else {
1051 /* check if newly bound framebuffer has any texture attachments */
1052 check_begin_texture_render(ctx, newFb);
1053 }
1054 }
1055
1056 if (ctx->Driver.BindFramebuffer) {
1057 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1058 }
1059 }
1060
1061
1062 void GLAPIENTRY
1063 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1064 {
1065 GLint i;
1066 GET_CURRENT_CONTEXT(ctx);
1067
1068 ASSERT_OUTSIDE_BEGIN_END(ctx);
1069 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1070 /* The above doesn't fully flush the drivers in the way that a
1071 * glFlush does, but that is required here:
1072 */
1073 if (ctx->Driver.Flush)
1074 ctx->Driver.Flush(ctx);
1075
1076 for (i = 0; i < n; i++) {
1077 if (framebuffers[i] > 0) {
1078 struct gl_framebuffer *fb;
1079 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1080 if (fb) {
1081 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1082
1083 /* check if deleting currently bound framebuffer object */
1084 if (fb == ctx->DrawBuffer) {
1085 /* bind default */
1086 ASSERT(fb->RefCount >= 2);
1087 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1088 }
1089
1090 /* remove from hash table immediately, to free the ID */
1091 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1092
1093 if (fb != &DummyFramebuffer) {
1094 /* But the object will not be freed until it's no longer
1095 * bound in any context.
1096 */
1097 _mesa_unreference_framebuffer(&fb);
1098 }
1099 }
1100 }
1101 }
1102 }
1103
1104
1105 void GLAPIENTRY
1106 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1107 {
1108 GET_CURRENT_CONTEXT(ctx);
1109 GLuint first;
1110 GLint i;
1111
1112 ASSERT_OUTSIDE_BEGIN_END(ctx);
1113
1114 if (n < 0) {
1115 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1116 return;
1117 }
1118
1119 if (!framebuffers)
1120 return;
1121
1122 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1123
1124 for (i = 0; i < n; i++) {
1125 GLuint name = first + i;
1126 framebuffers[i] = name;
1127 /* insert dummy placeholder into hash table */
1128 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1129 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1130 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1131 }
1132 }
1133
1134
1135
1136 GLenum GLAPIENTRY
1137 _mesa_CheckFramebufferStatusEXT(GLenum target)
1138 {
1139 struct gl_framebuffer *buffer;
1140 GET_CURRENT_CONTEXT(ctx);
1141
1142 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1143
1144 switch (target) {
1145 #if FEATURE_EXT_framebuffer_blit
1146 case GL_DRAW_FRAMEBUFFER_EXT:
1147 if (!ctx->Extensions.EXT_framebuffer_blit) {
1148 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1149 return 0;
1150 }
1151 buffer = ctx->DrawBuffer;
1152 break;
1153 case GL_READ_FRAMEBUFFER_EXT:
1154 if (!ctx->Extensions.EXT_framebuffer_blit) {
1155 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1156 return 0;
1157 }
1158 buffer = ctx->ReadBuffer;
1159 break;
1160 #endif
1161 case GL_FRAMEBUFFER_EXT:
1162 buffer = ctx->DrawBuffer;
1163 break;
1164 default:
1165 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1166 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1167 }
1168
1169 if (buffer->Name == 0) {
1170 /* The window system / default framebuffer is always complete */
1171 return GL_FRAMEBUFFER_COMPLETE_EXT;
1172 }
1173
1174 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1175
1176 _mesa_test_framebuffer_completeness(ctx, buffer);
1177 return buffer->_Status;
1178 }
1179
1180
1181
1182 /**
1183 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1184 */
1185 static void
1186 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1187 GLenum attachment, GLenum textarget, GLuint texture,
1188 GLint level, GLint zoffset)
1189 {
1190 struct gl_renderbuffer_attachment *att;
1191 struct gl_texture_object *texObj = NULL;
1192 struct gl_framebuffer *fb;
1193
1194 ASSERT_OUTSIDE_BEGIN_END(ctx);
1195
1196 if (target != GL_FRAMEBUFFER_EXT) {
1197 _mesa_error(ctx, GL_INVALID_ENUM,
1198 "glFramebufferTexture%sEXT(target)", caller);
1199 return;
1200 }
1201
1202 fb = ctx->DrawBuffer;
1203 ASSERT(fb);
1204
1205 /* check framebuffer binding */
1206 if (fb->Name == 0) {
1207 _mesa_error(ctx, GL_INVALID_OPERATION,
1208 "glFramebufferTexture%sEXT", caller);
1209 return;
1210 }
1211
1212
1213 /* The textarget, level, and zoffset parameters are only validated if
1214 * texture is non-zero.
1215 */
1216 if (texture) {
1217 GLboolean err = GL_TRUE;
1218
1219 texObj = _mesa_lookup_texture(ctx, texture);
1220 if (texObj != NULL) {
1221 if (textarget == 0) {
1222 err = (texObj->Target != GL_TEXTURE_3D) &&
1223 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1224 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1225 }
1226 else {
1227 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1228 ? !IS_CUBE_FACE(textarget)
1229 : (texObj->Target != textarget);
1230 }
1231 }
1232
1233 if (err) {
1234 _mesa_error(ctx, GL_INVALID_OPERATION,
1235 "glFramebufferTexture%sEXT(texture target mismatch)",
1236 caller);
1237 return;
1238 }
1239
1240 if (texObj->Target == GL_TEXTURE_3D) {
1241 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1242 if (zoffset < 0 || zoffset >= maxSize) {
1243 _mesa_error(ctx, GL_INVALID_VALUE,
1244 "glFramebufferTexture%sEXT(zoffset)", caller);
1245 return;
1246 }
1247 }
1248 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1249 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1250 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1251 _mesa_error(ctx, GL_INVALID_VALUE,
1252 "glFramebufferTexture%sEXT(layer)", caller);
1253 return;
1254 }
1255 }
1256
1257
1258 if ((level < 0) ||
1259 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1260 _mesa_error(ctx, GL_INVALID_VALUE,
1261 "glFramebufferTexture%sEXT(level)", caller);
1262 return;
1263 }
1264 }
1265
1266 att = _mesa_get_attachment(ctx, fb, attachment);
1267 if (att == NULL) {
1268 _mesa_error(ctx, GL_INVALID_ENUM,
1269 "glFramebufferTexture%sEXT(attachment)", caller);
1270 return;
1271 }
1272
1273 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1274 /* The above doesn't fully flush the drivers in the way that a
1275 * glFlush does, but that is required here:
1276 */
1277 if (ctx->Driver.Flush)
1278 ctx->Driver.Flush(ctx);
1279
1280 _glthread_LOCK_MUTEX(fb->Mutex);
1281 if (texObj) {
1282 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1283 level, zoffset);
1284 }
1285 else {
1286 _mesa_remove_attachment(ctx, att);
1287 }
1288 _glthread_UNLOCK_MUTEX(fb->Mutex);
1289 }
1290
1291
1292
1293 void GLAPIENTRY
1294 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1295 GLenum textarget, GLuint texture, GLint level)
1296 {
1297 GET_CURRENT_CONTEXT(ctx);
1298
1299 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1300 _mesa_error(ctx, GL_INVALID_ENUM,
1301 "glFramebufferTexture1DEXT(textarget)");
1302 return;
1303 }
1304
1305 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1306 level, 0);
1307 }
1308
1309
1310 void GLAPIENTRY
1311 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1312 GLenum textarget, GLuint texture, GLint level)
1313 {
1314 GET_CURRENT_CONTEXT(ctx);
1315
1316 if ((texture != 0) &&
1317 (textarget != GL_TEXTURE_2D) &&
1318 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1319 (!IS_CUBE_FACE(textarget))) {
1320 _mesa_error(ctx, GL_INVALID_OPERATION,
1321 "glFramebufferTexture2DEXT(textarget)");
1322 return;
1323 }
1324
1325 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1326 level, 0);
1327 }
1328
1329
1330 void GLAPIENTRY
1331 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1332 GLenum textarget, GLuint texture,
1333 GLint level, GLint zoffset)
1334 {
1335 GET_CURRENT_CONTEXT(ctx);
1336
1337 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1338 _mesa_error(ctx, GL_INVALID_ENUM,
1339 "glFramebufferTexture3DEXT(textarget)");
1340 return;
1341 }
1342
1343 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1344 level, zoffset);
1345 }
1346
1347
1348 void GLAPIENTRY
1349 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1350 GLuint texture, GLint level, GLint layer)
1351 {
1352 GET_CURRENT_CONTEXT(ctx);
1353
1354 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1355 level, layer);
1356 }
1357
1358
1359 void GLAPIENTRY
1360 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1361 GLenum renderbufferTarget,
1362 GLuint renderbuffer)
1363 {
1364 struct gl_renderbuffer_attachment *att;
1365 struct gl_framebuffer *fb;
1366 struct gl_renderbuffer *rb;
1367 GET_CURRENT_CONTEXT(ctx);
1368
1369 ASSERT_OUTSIDE_BEGIN_END(ctx);
1370
1371 switch (target) {
1372 #if FEATURE_EXT_framebuffer_blit
1373 case GL_DRAW_FRAMEBUFFER_EXT:
1374 if (!ctx->Extensions.EXT_framebuffer_blit) {
1375 _mesa_error(ctx, GL_INVALID_ENUM,
1376 "glFramebufferRenderbufferEXT(target)");
1377 return;
1378 }
1379 fb = ctx->DrawBuffer;
1380 break;
1381 case GL_READ_FRAMEBUFFER_EXT:
1382 if (!ctx->Extensions.EXT_framebuffer_blit) {
1383 _mesa_error(ctx, GL_INVALID_ENUM,
1384 "glFramebufferRenderbufferEXT(target)");
1385 return;
1386 }
1387 fb = ctx->ReadBuffer;
1388 break;
1389 #endif
1390 case GL_FRAMEBUFFER_EXT:
1391 fb = ctx->DrawBuffer;
1392 break;
1393 default:
1394 _mesa_error(ctx, GL_INVALID_ENUM,
1395 "glFramebufferRenderbufferEXT(target)");
1396 return;
1397 }
1398
1399 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1400 _mesa_error(ctx, GL_INVALID_ENUM,
1401 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1402 return;
1403 }
1404
1405 if (fb->Name == 0) {
1406 /* Can't attach new renderbuffers to a window system framebuffer */
1407 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1408 return;
1409 }
1410
1411 att = _mesa_get_attachment(ctx, fb, attachment);
1412 if (att == NULL) {
1413 _mesa_error(ctx, GL_INVALID_ENUM,
1414 "glFramebufferRenderbufferEXT(attachment)");
1415 return;
1416 }
1417
1418 if (renderbuffer) {
1419 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1420 if (!rb) {
1421 _mesa_error(ctx, GL_INVALID_OPERATION,
1422 "glFramebufferRenderbufferEXT(renderbuffer)");
1423 return;
1424 }
1425 }
1426 else {
1427 /* remove renderbuffer attachment */
1428 rb = NULL;
1429 }
1430
1431 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1432 /* The above doesn't fully flush the drivers in the way that a
1433 * glFlush does, but that is required here:
1434 */
1435 if (ctx->Driver.Flush)
1436 ctx->Driver.Flush(ctx);
1437
1438 assert(ctx->Driver.FramebufferRenderbuffer);
1439 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1440
1441 /* Some subsequent GL commands may depend on the framebuffer's visual
1442 * after the binding is updated. Update visual info now.
1443 */
1444 _mesa_update_framebuffer_visual(fb);
1445 }
1446
1447
1448 void GLAPIENTRY
1449 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1450 GLenum pname, GLint *params)
1451 {
1452 const struct gl_renderbuffer_attachment *att;
1453 struct gl_framebuffer *buffer;
1454 GET_CURRENT_CONTEXT(ctx);
1455
1456 ASSERT_OUTSIDE_BEGIN_END(ctx);
1457
1458 switch (target) {
1459 #if FEATURE_EXT_framebuffer_blit
1460 case GL_DRAW_FRAMEBUFFER_EXT:
1461 if (!ctx->Extensions.EXT_framebuffer_blit) {
1462 _mesa_error(ctx, GL_INVALID_ENUM,
1463 "glGetFramebufferAttachmentParameterivEXT(target)");
1464 return;
1465 }
1466 buffer = ctx->DrawBuffer;
1467 break;
1468 case GL_READ_FRAMEBUFFER_EXT:
1469 if (!ctx->Extensions.EXT_framebuffer_blit) {
1470 _mesa_error(ctx, GL_INVALID_ENUM,
1471 "glGetFramebufferAttachmentParameterivEXT(target)");
1472 return;
1473 }
1474 buffer = ctx->ReadBuffer;
1475 break;
1476 #endif
1477 case GL_FRAMEBUFFER_EXT:
1478 buffer = ctx->DrawBuffer;
1479 break;
1480 default:
1481 _mesa_error(ctx, GL_INVALID_ENUM,
1482 "glGetFramebufferAttachmentParameterivEXT(target)");
1483 return;
1484 }
1485
1486 if (buffer->Name == 0) {
1487 _mesa_error(ctx, GL_INVALID_OPERATION,
1488 "glGetFramebufferAttachmentParameterivEXT");
1489 return;
1490 }
1491
1492 att = _mesa_get_attachment(ctx, buffer, attachment);
1493 if (att == NULL) {
1494 _mesa_error(ctx, GL_INVALID_ENUM,
1495 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1496 return;
1497 }
1498
1499 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1500 /* The above doesn't fully flush the drivers in the way that a
1501 * glFlush does, but that is required here:
1502 */
1503 if (ctx->Driver.Flush)
1504 ctx->Driver.Flush(ctx);
1505
1506 switch (pname) {
1507 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1508 *params = att->Type;
1509 return;
1510 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1511 if (att->Type == GL_RENDERBUFFER_EXT) {
1512 *params = att->Renderbuffer->Name;
1513 }
1514 else if (att->Type == GL_TEXTURE) {
1515 *params = att->Texture->Name;
1516 }
1517 else {
1518 _mesa_error(ctx, GL_INVALID_ENUM,
1519 "glGetFramebufferAttachmentParameterivEXT(pname)");
1520 }
1521 return;
1522 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1523 if (att->Type == GL_TEXTURE) {
1524 *params = att->TextureLevel;
1525 }
1526 else {
1527 _mesa_error(ctx, GL_INVALID_ENUM,
1528 "glGetFramebufferAttachmentParameterivEXT(pname)");
1529 }
1530 return;
1531 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1532 if (att->Type == GL_TEXTURE) {
1533 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1534 }
1535 else {
1536 _mesa_error(ctx, GL_INVALID_ENUM,
1537 "glGetFramebufferAttachmentParameterivEXT(pname)");
1538 }
1539 return;
1540 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1541 if (att->Type == GL_TEXTURE) {
1542 *params = att->Zoffset;
1543 }
1544 else {
1545 _mesa_error(ctx, GL_INVALID_ENUM,
1546 "glGetFramebufferAttachmentParameterivEXT(pname)");
1547 }
1548 return;
1549 default:
1550 _mesa_error(ctx, GL_INVALID_ENUM,
1551 "glGetFramebufferAttachmentParameterivEXT(pname)");
1552 return;
1553 }
1554 }
1555
1556
1557 void GLAPIENTRY
1558 _mesa_GenerateMipmapEXT(GLenum target)
1559 {
1560 struct gl_texture_unit *texUnit;
1561 struct gl_texture_object *texObj;
1562 GET_CURRENT_CONTEXT(ctx);
1563
1564 ASSERT_OUTSIDE_BEGIN_END(ctx);
1565 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1566
1567 switch (target) {
1568 case GL_TEXTURE_1D:
1569 case GL_TEXTURE_2D:
1570 case GL_TEXTURE_3D:
1571 case GL_TEXTURE_CUBE_MAP:
1572 /* OK, legal value */
1573 break;
1574 default:
1575 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1576 return;
1577 }
1578
1579 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1580 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1581
1582 /* XXX this might not handle cube maps correctly */
1583 _mesa_lock_texture(ctx, texObj);
1584 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1585 _mesa_unlock_texture(ctx, texObj);
1586 }
1587
1588
1589 #if FEATURE_EXT_framebuffer_blit
1590 void GLAPIENTRY
1591 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1592 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1593 GLbitfield mask, GLenum filter)
1594 {
1595 GET_CURRENT_CONTEXT(ctx);
1596
1597 ASSERT_OUTSIDE_BEGIN_END(ctx);
1598 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1599
1600 if (ctx->NewState) {
1601 _mesa_update_state(ctx);
1602 }
1603
1604 if (!ctx->ReadBuffer) {
1605 /* XXX */
1606 }
1607
1608 /* check for complete framebuffers */
1609 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
1610 ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1611 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1612 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1613 return;
1614 }
1615
1616 if (filter != GL_NEAREST && filter != GL_LINEAR) {
1617 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
1618 return;
1619 }
1620
1621 if (mask & ~(GL_COLOR_BUFFER_BIT |
1622 GL_DEPTH_BUFFER_BIT |
1623 GL_STENCIL_BUFFER_BIT)) {
1624 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
1625 return;
1626 }
1627
1628 /* depth/stencil must be blitted with nearest filtering */
1629 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
1630 && filter != GL_NEAREST) {
1631 _mesa_error(ctx, GL_INVALID_OPERATION,
1632 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
1633 return;
1634 }
1635
1636 if (mask & GL_STENCIL_BUFFER_BIT) {
1637 struct gl_renderbuffer *readRb = ctx->ReadBuffer->_StencilBuffer;
1638 struct gl_renderbuffer *drawRb = ctx->DrawBuffer->_StencilBuffer;
1639 if (readRb->StencilBits != drawRb->StencilBits) {
1640 _mesa_error(ctx, GL_INVALID_OPERATION,
1641 "glBlitFramebufferEXT(stencil buffer size mismatch");
1642 return;
1643 }
1644 }
1645
1646 if (mask & GL_DEPTH_BUFFER_BIT) {
1647 struct gl_renderbuffer *readRb = ctx->ReadBuffer->_DepthBuffer;
1648 struct gl_renderbuffer *drawRb = ctx->DrawBuffer->_DepthBuffer;
1649 if (readRb->DepthBits != drawRb->DepthBits) {
1650 _mesa_error(ctx, GL_INVALID_OPERATION,
1651 "glBlitFramebufferEXT(depth buffer size mismatch");
1652 return;
1653 }
1654 }
1655
1656 if (!ctx->Extensions.EXT_framebuffer_blit) {
1657 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
1658 return;
1659 }
1660
1661 ASSERT(ctx->Driver.BlitFramebuffer);
1662 ctx->Driver.BlitFramebuffer(ctx,
1663 srcX0, srcY0, srcX1, srcY1,
1664 dstX0, dstY0, dstX1, dstY1,
1665 mask, filter);
1666 }
1667 #endif /* FEATURE_EXT_framebuffer_blit */