1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/fbobject.h"
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
34 #include "main/context.h"
35 #include "main/texformat.h"
36 #include "main/texrender.h"
38 #include "intel_context.h"
39 #include "intel_buffers.h"
40 #include "intel_depthstencil.h"
41 #include "intel_fbo.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_regions.h"
44 #include "intel_span.h"
47 #define FILE_DEBUG_FLAG DEBUG_FBO
51 * Create a new framebuffer object.
53 static struct gl_framebuffer *
54 intel_new_framebuffer(GLcontext * ctx, GLuint name)
56 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
59 return _mesa_new_framebuffer(ctx, name);
64 intel_delete_renderbuffer(struct gl_renderbuffer *rb)
66 GET_CURRENT_CONTEXT(ctx);
67 struct intel_context *intel = intel_context(ctx);
68 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
72 if (irb->PairedStencil || irb->PairedDepth) {
73 intel_unpair_depth_stencil(ctx, irb);
76 if (irb->span_cache != NULL)
77 _mesa_free(irb->span_cache);
79 if (intel && irb->region) {
80 intel_region_release(&irb->region);
89 * Return a pointer to a specific pixel in a renderbuffer.
92 intel_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
95 /* By returning NULL we force all software rendering to go through
104 * Called via glRenderbufferStorageEXT() to set the format and allocate
105 * storage for a user-created renderbuffer.
108 intel_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
109 GLenum internalFormat,
110 GLuint width, GLuint height)
112 struct intel_context *intel = intel_context(ctx);
113 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
114 GLboolean softwareBuffer = GL_FALSE;
117 ASSERT(rb->Name != 0);
119 switch (internalFormat) {
123 rb->_ActualFormat = GL_RGB5;
124 rb->DataType = GL_UNSIGNED_BYTE;
135 rb->_ActualFormat = GL_RGB8;
136 rb->DataType = GL_UNSIGNED_BYTE;
151 rb->_ActualFormat = GL_RGBA8;
152 rb->DataType = GL_UNSIGNED_BYTE;
159 case GL_STENCIL_INDEX:
160 case GL_STENCIL_INDEX1_EXT:
161 case GL_STENCIL_INDEX4_EXT:
162 case GL_STENCIL_INDEX8_EXT:
163 case GL_STENCIL_INDEX16_EXT:
164 /* alloc a depth+stencil buffer */
165 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
166 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
170 case GL_DEPTH_COMPONENT16:
172 rb->_ActualFormat = GL_DEPTH_COMPONENT16;
173 rb->DataType = GL_UNSIGNED_SHORT;
179 * 16bpp depth renderbuffer can't be paired with a stencil buffer so
180 * always used combined depth/stencil format.
183 case GL_DEPTH_COMPONENT:
184 case GL_DEPTH_COMPONENT24:
185 case GL_DEPTH_COMPONENT32:
186 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
187 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
191 case GL_DEPTH_STENCIL_EXT:
192 case GL_DEPTH24_STENCIL8_EXT:
193 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
194 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
201 "Unexpected format in intel_alloc_renderbuffer_storage");
207 /* free old region */
209 intel_region_release(&irb->region);
212 /* allocate new memory region/renderbuffer */
213 if (softwareBuffer) {
214 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
218 /* Choose a pitch to match hardware requirements:
220 GLuint pitch = ((cpp * width + 63) & ~63) / cpp;
222 /* alloc hardware renderbuffer */
223 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width,
226 irb->region = intel_region_alloc(intel, cpp, width, height, pitch);
228 return GL_FALSE; /* out of memory? */
230 ASSERT(irb->region->buffer);
242 * Called for each hardware renderbuffer when a _window_ is resized.
243 * Just update fields.
244 * Not used for user-created renderbuffers!
247 intel_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
248 GLenum internalFormat, GLuint width, GLuint height)
250 ASSERT(rb->Name == 0);
253 rb->_ActualFormat = internalFormat;
259 intel_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
260 GLuint width, GLuint height)
262 struct intel_framebuffer *intel_fb = (struct intel_framebuffer*)fb;
265 _mesa_resize_framebuffer(ctx, fb, width, height);
267 fb->Initialized = GL_TRUE; /* XXX remove someday */
273 /* Make sure all window system renderbuffers are up to date */
274 for (i = 0; i < 2; i++) {
275 struct gl_renderbuffer *rb = &intel_fb->color_rb[i]->Base;
277 /* only resize if size is changing */
278 if (rb && (rb->Width != width || rb->Height != height)) {
279 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
285 intel_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
286 GLenum internalFormat, GLuint width, GLuint height)
288 _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
294 intel_renderbuffer_set_region(struct intel_renderbuffer *rb,
295 struct intel_region *region)
297 struct intel_region *old;
301 intel_region_reference(&rb->region, region);
302 intel_region_release(&old);
304 rb->pfPitch = region->pitch;
308 * Create a new intel_renderbuffer which corresponds to an on-screen window,
309 * not a user-created renderbuffer.
311 struct intel_renderbuffer *
312 intel_create_renderbuffer(GLenum intFormat)
314 GET_CURRENT_CONTEXT(ctx);
316 struct intel_renderbuffer *irb;
317 const GLuint name = 0;
319 irb = CALLOC_STRUCT(intel_renderbuffer);
321 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
325 _mesa_init_renderbuffer(&irb->Base, name);
326 irb->Base.ClassID = INTEL_RB_CLASS;
330 irb->Base._ActualFormat = GL_RGB5;
331 irb->Base._BaseFormat = GL_RGBA;
332 irb->Base.RedBits = 5;
333 irb->Base.GreenBits = 6;
334 irb->Base.BlueBits = 5;
335 irb->Base.DataType = GL_UNSIGNED_BYTE;
338 irb->Base._ActualFormat = GL_RGBA8;
339 irb->Base._BaseFormat = GL_RGBA;
340 irb->Base.RedBits = 8;
341 irb->Base.GreenBits = 8;
342 irb->Base.BlueBits = 8;
343 irb->Base.AlphaBits = 8;
344 irb->Base.DataType = GL_UNSIGNED_BYTE;
346 case GL_STENCIL_INDEX8_EXT:
347 irb->Base._ActualFormat = GL_STENCIL_INDEX8_EXT;
348 irb->Base._BaseFormat = GL_STENCIL_INDEX;
349 irb->Base.StencilBits = 8;
350 irb->Base.DataType = GL_UNSIGNED_BYTE;
352 case GL_DEPTH_COMPONENT16:
353 irb->Base._ActualFormat = GL_DEPTH_COMPONENT16;
354 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
355 irb->Base.DepthBits = 16;
356 irb->Base.DataType = GL_UNSIGNED_SHORT;
358 case GL_DEPTH_COMPONENT24:
359 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
360 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
361 irb->Base.DepthBits = 24;
362 irb->Base.DataType = GL_UNSIGNED_INT;
364 case GL_DEPTH24_STENCIL8_EXT:
365 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
366 irb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
367 irb->Base.DepthBits = 24;
368 irb->Base.StencilBits = 8;
369 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
373 "Unexpected intFormat in intel_create_renderbuffer");
377 irb->Base.InternalFormat = intFormat;
379 /* intel-specific methods */
380 irb->Base.Delete = intel_delete_renderbuffer;
381 irb->Base.AllocStorage = intel_alloc_window_storage;
382 irb->Base.GetPointer = intel_get_pointer;
389 * Create a new renderbuffer object.
390 * Typically called via glBindRenderbufferEXT().
392 static struct gl_renderbuffer *
393 intel_new_renderbuffer(GLcontext * ctx, GLuint name)
395 /*struct intel_context *intel = intel_context(ctx); */
396 struct intel_renderbuffer *irb;
398 irb = CALLOC_STRUCT(intel_renderbuffer);
400 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
404 _mesa_init_renderbuffer(&irb->Base, name);
405 irb->Base.ClassID = INTEL_RB_CLASS;
407 /* intel-specific methods */
408 irb->Base.Delete = intel_delete_renderbuffer;
409 irb->Base.AllocStorage = intel_alloc_renderbuffer_storage;
410 irb->Base.GetPointer = intel_get_pointer;
411 /* span routines set in alloc_storage function */
418 * Called via glBindFramebufferEXT().
421 intel_bind_framebuffer(GLcontext * ctx, GLenum target,
422 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
424 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
425 intel_draw_buffer(ctx, fb);
426 /* Integer depth range depends on depth buffer bits */
427 if (ctx->Driver.DepthRange != NULL)
428 ctx->Driver.DepthRange(ctx, ctx->Viewport.Near, ctx->Viewport.Far);
431 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
437 * Called via glFramebufferRenderbufferEXT().
440 intel_framebuffer_renderbuffer(GLcontext * ctx,
441 struct gl_framebuffer *fb,
442 GLenum attachment, struct gl_renderbuffer *rb)
444 DBG("Intel FramebufferRenderbuffer %u %u\n", fb->Name, rb ? rb->Name : 0);
448 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
449 intel_draw_buffer(ctx, fb);
453 intel_update_wrapper(GLcontext *ctx, struct intel_renderbuffer *irb,
454 struct gl_texture_image *texImage)
456 if (texImage->TexFormat == &_mesa_texformat_argb8888) {
457 irb->Base._ActualFormat = GL_RGBA8;
458 irb->Base._BaseFormat = GL_RGBA;
459 irb->Base.DataType = GL_UNSIGNED_BYTE;
460 DBG("Render to RGBA8 texture OK\n");
462 else if (texImage->TexFormat == &_mesa_texformat_rgb565) {
463 irb->Base._ActualFormat = GL_RGB5;
464 irb->Base._BaseFormat = GL_RGB;
465 irb->Base.DataType = GL_UNSIGNED_SHORT;
466 DBG("Render to RGB5 texture OK\n");
468 else if (texImage->TexFormat == &_mesa_texformat_z16) {
469 irb->Base._ActualFormat = GL_DEPTH_COMPONENT16;
470 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
471 irb->Base.DataType = GL_UNSIGNED_SHORT;
472 DBG("Render to DEPTH16 texture OK\n");
474 else if (texImage->TexFormat == &_mesa_texformat_s8_z24) {
475 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
476 irb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
477 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
478 DBG("Render to DEPTH_STENCIL texture OK\n");
481 DBG("Render to texture BAD FORMAT %d\n",
482 texImage->TexFormat->MesaFormat);
486 irb->Base.InternalFormat = irb->Base._ActualFormat;
487 irb->Base.Width = texImage->Width;
488 irb->Base.Height = texImage->Height;
489 irb->Base.RedBits = texImage->TexFormat->RedBits;
490 irb->Base.GreenBits = texImage->TexFormat->GreenBits;
491 irb->Base.BlueBits = texImage->TexFormat->BlueBits;
492 irb->Base.AlphaBits = texImage->TexFormat->AlphaBits;
493 irb->Base.DepthBits = texImage->TexFormat->DepthBits;
495 irb->Base.Delete = intel_delete_renderbuffer;
496 irb->Base.AllocStorage = intel_nop_alloc_storage;
498 irb->RenderToTexture = GL_TRUE;
504 * When glFramebufferTexture[123]D is called this function sets up the
505 * gl_renderbuffer wrapper around the texture image.
506 * This will have the region info needed for hardware rendering.
508 static struct intel_renderbuffer *
509 intel_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
511 const GLuint name = ~0; /* not significant, but distinct for debugging */
512 struct intel_renderbuffer *irb;
514 /* make an intel_renderbuffer to wrap the texture image */
515 irb = CALLOC_STRUCT(intel_renderbuffer);
517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
521 _mesa_init_renderbuffer(&irb->Base, name);
522 irb->Base.ClassID = INTEL_RB_CLASS;
524 if (!intel_update_wrapper(ctx, irb, texImage)) {
534 * Called by glFramebufferTexture[123]DEXT() (and other places) to
535 * prepare for rendering into texture memory. This might be called
536 * many times to choose different texture levels, cube faces, etc
537 * before intel_finish_render_texture() is ever called.
540 intel_render_texture(GLcontext * ctx,
541 struct gl_framebuffer *fb,
542 struct gl_renderbuffer_attachment *att)
544 struct gl_texture_image *newImage
545 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
546 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
547 struct intel_texture_image *intel_image;
554 if (newImage->Border != 0) {
555 /* Fallback on drawing to a texture with a border, which won't have a
558 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
559 _mesa_render_texture(ctx, fb, att);
562 irb = intel_wrap_texture(ctx, newImage);
564 /* bind the wrapper to the attachment point */
565 _mesa_reference_renderbuffer(&att->Renderbuffer, &irb->Base);
568 /* fallback to software rendering */
569 _mesa_render_texture(ctx, fb, att);
572 } if (!intel_update_wrapper(ctx, irb, newImage)) {
573 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
574 _mesa_render_texture(ctx, fb, att);
578 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
580 att->Texture->Name, newImage->Width, newImage->Height,
583 /* point the renderbufer's region to the texture image region */
584 intel_image = intel_texture_image(newImage);
585 if (irb->region != intel_image->mt->region) {
587 intel_region_release(&irb->region);
588 intel_region_reference(&irb->region, intel_image->mt->region);
591 /* compute offset of the particular 2D image within the texture region */
592 imageOffset = intel_miptree_image_offset(intel_image->mt,
596 if (att->Texture->Target == GL_TEXTURE_3D) {
597 const GLuint *offsets = intel_miptree_depth_offsets(intel_image->mt,
599 imageOffset += offsets[att->Zoffset];
602 /* store that offset in the region */
603 intel_image->mt->region->draw_offset = imageOffset;
605 /* update drawing region, etc */
606 intel_draw_buffer(ctx, fb);
611 * Called by Mesa when rendering to a texture is done.
614 intel_finish_render_texture(GLcontext * ctx,
615 struct gl_renderbuffer_attachment *att)
617 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
619 DBG("End render texture (tid %x) tex %u\n", _glthread_GetID(), att->Texture->Name);
622 /* just release the region */
623 intel_region_release(&irb->region);
625 else if (att->Renderbuffer) {
626 /* software fallback */
627 _mesa_finish_render_texture(ctx, att);
628 /* XXX FBO: Need to unmap the buffer (or in intelSpanRenderStart???) */
634 * Do additional "completeness" testing of a framebuffer object.
637 intel_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
640 * Eventually: examine the renderbuffers/textures and make sure it's
641 * supported by the hw.
647 * Do one-time context initializations related to GL_EXT_framebuffer_object.
648 * Hook in device driver functions.
651 intel_fbo_init(struct intel_context *intel)
653 intel->ctx.Driver.NewFramebuffer = intel_new_framebuffer;
654 intel->ctx.Driver.NewRenderbuffer = intel_new_renderbuffer;
655 intel->ctx.Driver.BindFramebuffer = intel_bind_framebuffer;
656 intel->ctx.Driver.FramebufferRenderbuffer = intel_framebuffer_renderbuffer;
657 intel->ctx.Driver.RenderTexture = intel_render_texture;
658 intel->ctx.Driver.FinishRenderTexture = intel_finish_render_texture;
659 intel->ctx.Driver.ResizeBuffers = intel_resize_buffers;
660 intel->ctx.Driver.ValidateFramebuffer = intel_validate_framebuffer;