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