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/macros.h"
31 #include "main/mtypes.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/context.h"
36 #include "main/texformat.h"
37 #include "main/texrender.h"
39 #include "intel_context.h"
40 #include "intel_buffers.h"
41 #include "intel_fbo.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_regions.h"
46 #define FILE_DEBUG_FLAG DEBUG_FBO
50 * Create a new framebuffer object.
52 static struct gl_framebuffer
*
53 intel_new_framebuffer(GLcontext
* ctx
, GLuint name
)
55 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
58 return _mesa_new_framebuffer(ctx
, name
);
62 /** Called by gl_renderbuffer::Delete() */
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
->span_cache
!= NULL
)
73 _mesa_free(irb
->span_cache
);
75 if (intel
&& irb
->region
) {
76 intel_region_release(&irb
->region
);
84 * Return a pointer to a specific pixel in a renderbuffer.
87 intel_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
90 /* By returning NULL we force all software rendering to go through
98 * Called via glRenderbufferStorageEXT() to set the format and allocate
99 * storage for a user-created renderbuffer.
102 intel_alloc_renderbuffer_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
103 GLenum internalFormat
,
104 GLuint width
, GLuint height
)
106 struct intel_context
*intel
= intel_context(ctx
);
107 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
108 GLboolean softwareBuffer
= GL_FALSE
;
111 ASSERT(rb
->Name
!= 0);
113 switch (internalFormat
) {
117 rb
->_ActualFormat
= GL_RGB5
;
118 rb
->DataType
= GL_UNSIGNED_BYTE
;
129 rb
->_ActualFormat
= GL_RGB8
;
130 rb
->DataType
= GL_UNSIGNED_BYTE
;
145 rb
->_ActualFormat
= GL_RGBA8
;
146 rb
->DataType
= GL_UNSIGNED_BYTE
;
153 case GL_STENCIL_INDEX
:
154 case GL_STENCIL_INDEX1_EXT
:
155 case GL_STENCIL_INDEX4_EXT
:
156 case GL_STENCIL_INDEX8_EXT
:
157 case GL_STENCIL_INDEX16_EXT
:
158 /* alloc a depth+stencil buffer */
159 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
160 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
164 case GL_DEPTH_COMPONENT16
:
165 rb
->_ActualFormat
= GL_DEPTH_COMPONENT16
;
166 rb
->DataType
= GL_UNSIGNED_SHORT
;
170 case GL_DEPTH_COMPONENT
:
171 case GL_DEPTH_COMPONENT24
:
172 case GL_DEPTH_COMPONENT32
:
173 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
174 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
178 case GL_DEPTH_STENCIL_EXT
:
179 case GL_DEPTH24_STENCIL8_EXT
:
180 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
181 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
188 "Unexpected format in intel_alloc_renderbuffer_storage");
194 /* free old region */
196 intel_region_release(&irb
->region
);
199 /* allocate new memory region/renderbuffer */
200 if (softwareBuffer
) {
201 return _mesa_soft_renderbuffer_storage(ctx
, rb
, internalFormat
,
205 /* Choose a pitch to match hardware requirements:
207 GLuint pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
209 /* alloc hardware renderbuffer */
210 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width
,
213 irb
->region
= intel_region_alloc(intel
, cpp
, width
, height
, pitch
,
216 return GL_FALSE
; /* out of memory? */
218 ASSERT(irb
->region
->buffer
);
229 * Called for each hardware renderbuffer when a _window_ is resized.
230 * Just update fields.
231 * Not used for user-created renderbuffers!
234 intel_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
235 GLenum internalFormat
, GLuint width
, GLuint height
)
237 ASSERT(rb
->Name
== 0);
240 rb
->_ActualFormat
= internalFormat
;
247 intel_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
248 GLuint width
, GLuint height
)
250 struct intel_framebuffer
*intel_fb
= (struct intel_framebuffer
*)fb
;
253 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
255 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
261 /* Make sure all window system renderbuffers are up to date */
262 for (i
= 0; i
< 2; i
++) {
263 struct gl_renderbuffer
*rb
= &intel_fb
->color_rb
[i
]->Base
;
265 /* only resize if size is changing */
266 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
267 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
273 /** Dummy function for gl_renderbuffer::AllocStorage() */
275 intel_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
276 GLenum internalFormat
, GLuint width
, GLuint height
)
278 _mesa_problem(ctx
, "intel_op_alloc_storage should never be called.");
284 intel_renderbuffer_set_region(struct intel_renderbuffer
*rb
,
285 struct intel_region
*region
)
287 struct intel_region
*old
;
291 intel_region_reference(&rb
->region
, region
);
292 intel_region_release(&old
);
297 * Create a new intel_renderbuffer which corresponds to an on-screen window,
298 * not a user-created renderbuffer.
300 struct intel_renderbuffer
*
301 intel_create_renderbuffer(GLenum intFormat
)
303 GET_CURRENT_CONTEXT(ctx
);
305 struct intel_renderbuffer
*irb
;
306 const GLuint name
= 0;
308 irb
= CALLOC_STRUCT(intel_renderbuffer
);
310 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
314 _mesa_init_renderbuffer(&irb
->Base
, name
);
315 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
319 irb
->Base
._ActualFormat
= GL_RGB5
;
320 irb
->Base
._BaseFormat
= GL_RGBA
;
321 irb
->Base
.RedBits
= 5;
322 irb
->Base
.GreenBits
= 6;
323 irb
->Base
.BlueBits
= 5;
324 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
327 irb
->Base
._ActualFormat
= GL_RGBA8
;
328 irb
->Base
._BaseFormat
= GL_RGBA
;
329 irb
->Base
.RedBits
= 8;
330 irb
->Base
.GreenBits
= 8;
331 irb
->Base
.BlueBits
= 8;
332 irb
->Base
.AlphaBits
= 8;
333 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
335 case GL_STENCIL_INDEX8_EXT
:
336 irb
->Base
._ActualFormat
= GL_STENCIL_INDEX8_EXT
;
337 irb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
338 irb
->Base
.StencilBits
= 8;
339 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
341 case GL_DEPTH_COMPONENT16
:
342 irb
->Base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
343 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
344 irb
->Base
.DepthBits
= 16;
345 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
347 case GL_DEPTH_COMPONENT24
:
348 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
349 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
350 irb
->Base
.DepthBits
= 24;
351 irb
->Base
.DataType
= GL_UNSIGNED_INT
;
353 case GL_DEPTH24_STENCIL8_EXT
:
354 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
355 irb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
356 irb
->Base
.DepthBits
= 24;
357 irb
->Base
.StencilBits
= 8;
358 irb
->Base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
362 "Unexpected intFormat in intel_create_renderbuffer");
366 irb
->Base
.InternalFormat
= intFormat
;
368 /* intel-specific methods */
369 irb
->Base
.Delete
= intel_delete_renderbuffer
;
370 irb
->Base
.AllocStorage
= intel_alloc_window_storage
;
371 irb
->Base
.GetPointer
= intel_get_pointer
;
378 * Create a new renderbuffer object.
379 * Typically called via glBindRenderbufferEXT().
381 static struct gl_renderbuffer
*
382 intel_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
384 /*struct intel_context *intel = intel_context(ctx); */
385 struct intel_renderbuffer
*irb
;
387 irb
= CALLOC_STRUCT(intel_renderbuffer
);
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
393 _mesa_init_renderbuffer(&irb
->Base
, name
);
394 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
396 /* intel-specific methods */
397 irb
->Base
.Delete
= intel_delete_renderbuffer
;
398 irb
->Base
.AllocStorage
= intel_alloc_renderbuffer_storage
;
399 irb
->Base
.GetPointer
= intel_get_pointer
;
400 /* span routines set in alloc_storage function */
407 * Called via glBindFramebufferEXT().
410 intel_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
411 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
413 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
414 intel_draw_buffer(ctx
, fb
);
417 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
423 * Called via glFramebufferRenderbufferEXT().
426 intel_framebuffer_renderbuffer(GLcontext
* ctx
,
427 struct gl_framebuffer
*fb
,
428 GLenum attachment
, struct gl_renderbuffer
*rb
)
430 DBG("Intel FramebufferRenderbuffer %u %u\n", fb
->Name
, rb
? rb
->Name
: 0);
434 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
435 intel_draw_buffer(ctx
, fb
);
440 intel_update_wrapper(GLcontext
*ctx
, struct intel_renderbuffer
*irb
,
441 struct gl_texture_image
*texImage
)
443 if (texImage
->TexFormat
== &_mesa_texformat_argb8888
) {
444 irb
->Base
._ActualFormat
= GL_RGBA8
;
445 irb
->Base
._BaseFormat
= GL_RGBA
;
446 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
447 DBG("Render to RGBA8 texture OK\n");
449 else if (texImage
->TexFormat
== &_mesa_texformat_rgb565
) {
450 irb
->Base
._ActualFormat
= GL_RGB5
;
451 irb
->Base
._BaseFormat
= GL_RGB
;
452 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
453 DBG("Render to RGB5 texture OK\n");
455 else if (texImage
->TexFormat
== &_mesa_texformat_z16
) {
456 irb
->Base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
457 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
458 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
459 DBG("Render to DEPTH16 texture OK\n");
461 else if (texImage
->TexFormat
== &_mesa_texformat_s8_z24
) {
462 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
463 irb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
464 irb
->Base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
465 DBG("Render to DEPTH_STENCIL texture OK\n");
468 DBG("Render to texture BAD FORMAT %d\n",
469 texImage
->TexFormat
->MesaFormat
);
473 irb
->Base
.InternalFormat
= irb
->Base
._ActualFormat
;
474 irb
->Base
.Width
= texImage
->Width
;
475 irb
->Base
.Height
= texImage
->Height
;
476 irb
->Base
.RedBits
= texImage
->TexFormat
->RedBits
;
477 irb
->Base
.GreenBits
= texImage
->TexFormat
->GreenBits
;
478 irb
->Base
.BlueBits
= texImage
->TexFormat
->BlueBits
;
479 irb
->Base
.AlphaBits
= texImage
->TexFormat
->AlphaBits
;
480 irb
->Base
.DepthBits
= texImage
->TexFormat
->DepthBits
;
482 irb
->Base
.Delete
= intel_delete_renderbuffer
;
483 irb
->Base
.AllocStorage
= intel_nop_alloc_storage
;
490 * When glFramebufferTexture[123]D is called this function sets up the
491 * gl_renderbuffer wrapper around the texture image.
492 * This will have the region info needed for hardware rendering.
494 static struct intel_renderbuffer
*
495 intel_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
497 const GLuint name
= ~0; /* not significant, but distinct for debugging */
498 struct intel_renderbuffer
*irb
;
500 /* make an intel_renderbuffer to wrap the texture image */
501 irb
= CALLOC_STRUCT(intel_renderbuffer
);
503 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
507 _mesa_init_renderbuffer(&irb
->Base
, name
);
508 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
510 if (!intel_update_wrapper(ctx
, irb
, texImage
)) {
520 * Called by glFramebufferTexture[123]DEXT() (and other places) to
521 * prepare for rendering into texture memory. This might be called
522 * many times to choose different texture levels, cube faces, etc
523 * before intel_finish_render_texture() is ever called.
526 intel_render_texture(GLcontext
* ctx
,
527 struct gl_framebuffer
*fb
,
528 struct gl_renderbuffer_attachment
*att
)
530 struct gl_texture_image
*newImage
531 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
532 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
533 struct intel_texture_image
*intel_image
;
540 if (newImage
->Border
!= 0) {
541 /* Fallback on drawing to a texture with a border, which won't have a
544 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
545 _mesa_render_texture(ctx
, fb
, att
);
549 irb
= intel_wrap_texture(ctx
, newImage
);
551 /* bind the wrapper to the attachment point */
552 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &irb
->Base
);
555 /* fallback to software rendering */
556 _mesa_render_texture(ctx
, fb
, att
);
561 if (!intel_update_wrapper(ctx
, irb
, newImage
)) {
562 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
563 _mesa_render_texture(ctx
, fb
, att
);
567 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
569 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
572 /* point the renderbufer's region to the texture image region */
573 intel_image
= intel_texture_image(newImage
);
574 if (irb
->region
!= intel_image
->mt
->region
) {
576 intel_region_release(&irb
->region
);
577 intel_region_reference(&irb
->region
, intel_image
->mt
->region
);
580 /* compute offset of the particular 2D image within the texture region */
581 imageOffset
= intel_miptree_image_offset(intel_image
->mt
,
585 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
586 const GLuint
*offsets
= intel_miptree_depth_offsets(intel_image
->mt
,
588 imageOffset
+= offsets
[att
->Zoffset
];
591 /* store that offset in the region */
592 intel_image
->mt
->region
->draw_offset
= imageOffset
;
594 /* update drawing region, etc */
595 intel_draw_buffer(ctx
, fb
);
600 * Called by Mesa when rendering to a texture is done.
603 intel_finish_render_texture(GLcontext
* ctx
,
604 struct gl_renderbuffer_attachment
*att
)
606 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
608 DBG("End render texture (tid %x) tex %u\n", _glthread_GetID(), att
->Texture
->Name
);
611 /* just release the region */
612 intel_region_release(&irb
->region
);
614 else if (att
->Renderbuffer
) {
615 /* software fallback */
616 _mesa_finish_render_texture(ctx
, att
);
617 /* XXX FBO: Need to unmap the buffer (or in intelSpanRenderStart???) */
623 * Do additional "completeness" testing of a framebuffer object.
626 intel_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
628 const struct intel_renderbuffer
*depthRb
=
629 intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
630 const struct intel_renderbuffer
*stencilRb
=
631 intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
633 if (stencilRb
&& stencilRb
!= depthRb
) {
634 /* we only support combined depth/stencil buffers, not separate
637 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
643 * Called from glBlitFramebuffer().
644 * For now, we're doing an approximation with glCopyPixels().
645 * XXX we need to bypass all the per-fragment operations, except scissor.
648 intel_blit_framebuffer(GLcontext
*ctx
,
649 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
650 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
651 GLbitfield mask
, GLenum filter
)
653 const GLfloat xZoomSave
= ctx
->Pixel
.ZoomX
;
654 const GLfloat yZoomSave
= ctx
->Pixel
.ZoomY
;
655 GLsizei width
, height
;
656 GLfloat xFlip
= 1.0F
, yFlip
= 1.0F
;
672 width
= srcX1
- srcX0
;
673 height
= srcY1
- srcY0
;
675 ctx
->Pixel
.ZoomX
= xFlip
* (dstX1
- dstX0
) / (srcX1
- srcY0
);
676 ctx
->Pixel
.ZoomY
= yFlip
* (dstY1
- dstY0
) / (srcY1
- srcY0
);
678 if (ctx
->Pixel
.ZoomX
< 0.0F
) {
679 dstX0
= MAX2(dstX0
, dstX1
);
682 dstX0
= MIN2(dstX0
, dstX1
);
685 if (ctx
->Pixel
.ZoomY
< 0.0F
) {
686 dstY0
= MAX2(dstY0
, dstY1
);
689 dstY0
= MIN2(dstY0
, dstY1
);
692 if (mask
& GL_COLOR_BUFFER_BIT
) {
693 ctx
->Driver
.CopyPixels(ctx
, srcX0
, srcY0
, width
, height
,
694 dstX0
, dstY0
, GL_COLOR
);
696 if (mask
& GL_DEPTH_BUFFER_BIT
) {
697 ctx
->Driver
.CopyPixels(ctx
, srcX0
, srcY0
, width
, height
,
698 dstX0
, dstY0
, GL_DEPTH
);
700 if (mask
& GL_STENCIL_BUFFER_BIT
) {
701 ctx
->Driver
.CopyPixels(ctx
, srcX0
, srcY0
, width
, height
,
702 dstX0
, dstY0
, GL_STENCIL
);
705 ctx
->Pixel
.ZoomX
= xZoomSave
;
706 ctx
->Pixel
.ZoomY
= yZoomSave
;
711 * Do one-time context initializations related to GL_EXT_framebuffer_object.
712 * Hook in device driver functions.
715 intel_fbo_init(struct intel_context
*intel
)
717 intel
->ctx
.Driver
.NewFramebuffer
= intel_new_framebuffer
;
718 intel
->ctx
.Driver
.NewRenderbuffer
= intel_new_renderbuffer
;
719 intel
->ctx
.Driver
.BindFramebuffer
= intel_bind_framebuffer
;
720 intel
->ctx
.Driver
.FramebufferRenderbuffer
= intel_framebuffer_renderbuffer
;
721 intel
->ctx
.Driver
.RenderTexture
= intel_render_texture
;
722 intel
->ctx
.Driver
.FinishRenderTexture
= intel_finish_render_texture
;
723 intel
->ctx
.Driver
.ResizeBuffers
= intel_resize_buffers
;
724 intel
->ctx
.Driver
.ValidateFramebuffer
= intel_validate_framebuffer
;
725 intel
->ctx
.Driver
.BlitFramebuffer
= intel_blit_framebuffer
;