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 **************************************************************************/
32 #include "framebuffer.h"
33 #include "renderbuffer.h"
35 #include "texformat.h"
36 #include "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
49 #define INTEL_RB_CLASS 0x12345678
52 /* XXX FBO: move this to intel_context.h (inlined) */
54 * Return a gl_renderbuffer ptr casted to intel_renderbuffer.
55 * NULL will be returned if the rb isn't really an intel_renderbuffer.
56 * This is determiend by checking the ClassID.
58 struct intel_renderbuffer
*
59 intel_renderbuffer(struct gl_renderbuffer
*rb
)
61 struct intel_renderbuffer
*irb
= (struct intel_renderbuffer
*) rb
;
62 if (irb
&& irb
->Base
.ClassID
== INTEL_RB_CLASS
) {
63 /*_mesa_warning(NULL, "Returning non-intel Rb\n");*/
71 struct intel_renderbuffer
*
72 intel_get_renderbuffer(struct gl_framebuffer
*fb
, int attIndex
)
75 return intel_renderbuffer(fb
->Attachment
[attIndex
].Renderbuffer
);
82 intel_flip_renderbuffers(struct intel_framebuffer
*intel_fb
)
84 int current_page
= intel_fb
->pf_current_page
;
85 int next_page
= (current_page
+ 1) % intel_fb
->pf_num_pages
;
86 struct gl_renderbuffer
*tmp_rb
;
88 /* Exchange renderbuffers if necessary but make sure their reference counts
91 if (intel_fb
->color_rb
[current_page
] &&
92 intel_fb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
!=
93 &intel_fb
->color_rb
[current_page
]->Base
) {
95 _mesa_reference_renderbuffer(&tmp_rb
,
96 intel_fb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
97 tmp_rb
= &intel_fb
->color_rb
[current_page
]->Base
;
98 _mesa_reference_renderbuffer(
99 &intel_fb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
, tmp_rb
);
100 _mesa_reference_renderbuffer(&tmp_rb
, NULL
);
103 if (intel_fb
->color_rb
[next_page
] &&
104 intel_fb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
!=
105 &intel_fb
->color_rb
[next_page
]->Base
) {
107 _mesa_reference_renderbuffer(&tmp_rb
,
108 intel_fb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
109 tmp_rb
= &intel_fb
->color_rb
[next_page
]->Base
;
110 _mesa_reference_renderbuffer(
111 &intel_fb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
, tmp_rb
);
112 _mesa_reference_renderbuffer(&tmp_rb
, NULL
);
117 struct intel_region
*
118 intel_get_rb_region(struct gl_framebuffer
*fb
, GLuint attIndex
)
120 struct intel_renderbuffer
*irb
= intel_get_renderbuffer(fb
, attIndex
);
131 * Create a new framebuffer object.
133 static struct gl_framebuffer
*
134 intel_new_framebuffer(GLcontext
* ctx
, GLuint name
)
136 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
139 return _mesa_new_framebuffer(ctx
, name
);
144 intel_delete_renderbuffer(struct gl_renderbuffer
*rb
)
146 GET_CURRENT_CONTEXT(ctx
);
147 struct intel_context
*intel
= intel_context(ctx
);
148 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
152 if (irb
->PairedStencil
|| irb
->PairedDepth
) {
153 intel_unpair_depth_stencil(ctx
, irb
);
156 if (intel
&& irb
->region
) {
157 intel_region_release(&irb
->region
);
166 * Return a pointer to a specific pixel in a renderbuffer.
169 intel_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
172 /* By returning NULL we force all software rendering to go through
181 * Called via glRenderbufferStorageEXT() to set the format and allocate
182 * storage for a user-created renderbuffer.
185 intel_alloc_renderbuffer_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
186 GLenum internalFormat
,
187 GLuint width
, GLuint height
)
189 struct intel_context
*intel
= intel_context(ctx
);
190 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
191 GLboolean softwareBuffer
= GL_FALSE
;
194 ASSERT(rb
->Name
!= 0);
196 switch (internalFormat
) {
200 rb
->_ActualFormat
= GL_RGB5
;
201 rb
->DataType
= GL_UNSIGNED_BYTE
;
220 rb
->_ActualFormat
= GL_RGBA8
;
221 rb
->DataType
= GL_UNSIGNED_BYTE
;
228 case GL_STENCIL_INDEX
:
229 case GL_STENCIL_INDEX1_EXT
:
230 case GL_STENCIL_INDEX4_EXT
:
231 case GL_STENCIL_INDEX8_EXT
:
232 case GL_STENCIL_INDEX16_EXT
:
233 /* alloc a depth+stencil buffer */
234 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
235 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
239 case GL_DEPTH_COMPONENT16
:
240 rb
->_ActualFormat
= GL_DEPTH_COMPONENT16
;
241 rb
->DataType
= GL_UNSIGNED_SHORT
;
245 case GL_DEPTH_COMPONENT
:
246 case GL_DEPTH_COMPONENT24
:
247 case GL_DEPTH_COMPONENT32
:
248 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
249 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
253 case GL_DEPTH_STENCIL_EXT
:
254 case GL_DEPTH24_STENCIL8_EXT
:
255 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
256 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
263 "Unexpected format in intel_alloc_renderbuffer_storage");
269 /* free old region */
271 intel_region_release(&irb
->region
);
274 /* allocate new memory region/renderbuffer */
275 if (softwareBuffer
) {
276 return _mesa_soft_renderbuffer_storage(ctx
, rb
, internalFormat
,
280 /* Choose a pitch to match hardware requirements:
282 GLuint pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
284 /* alloc hardware renderbuffer */
285 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width
,
288 irb
->region
= intel_region_alloc(intel
, cpp
, pitch
, height
);
290 return GL_FALSE
; /* out of memory? */
292 ASSERT(irb
->region
->buffer
);
297 /* This sets the Get/PutRow/Value functions */
298 intel_set_span_functions(&irb
->Base
);
307 * Called for each hardware renderbuffer when a _window_ is resized.
308 * Just update fields.
309 * Not used for user-created renderbuffers!
312 intel_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
313 GLenum internalFormat
, GLuint width
, GLuint height
)
315 ASSERT(rb
->Name
== 0);
318 rb
->_ActualFormat
= internalFormat
;
324 intel_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
325 GLuint width
, GLuint height
)
327 struct intel_framebuffer
*intel_fb
= (struct intel_framebuffer
*)fb
;
330 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
332 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
338 /* Make sure all window system renderbuffers are up to date */
339 for (i
= 0; i
< 3; i
++) {
340 struct gl_renderbuffer
*rb
= &intel_fb
->color_rb
[i
]->Base
;
342 /* only resize if size is changing */
343 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
344 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
350 intel_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
351 GLenum internalFormat
, GLuint width
, GLuint height
)
353 _mesa_problem(ctx
, "intel_op_alloc_storage should never be called.");
359 intel_renderbuffer_set_region(struct intel_renderbuffer
*rb
,
360 struct intel_region
*region
)
362 struct intel_region
*old
;
366 intel_region_reference(&rb
->region
, region
);
367 intel_region_release(&old
);
369 rb
->pfMap
= region
->map
;
370 rb
->pfPitch
= region
->pitch
;
374 * Create a new intel_renderbuffer which corresponds to an on-screen window,
375 * not a user-created renderbuffer.
377 struct intel_renderbuffer
*
378 intel_create_renderbuffer(GLenum intFormat
)
380 GET_CURRENT_CONTEXT(ctx
);
382 struct intel_renderbuffer
*irb
;
383 const GLuint name
= 0;
385 irb
= CALLOC_STRUCT(intel_renderbuffer
);
387 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
391 _mesa_init_renderbuffer(&irb
->Base
, name
);
392 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
396 irb
->Base
._ActualFormat
= GL_RGB5
;
397 irb
->Base
._BaseFormat
= GL_RGBA
;
398 irb
->Base
.RedBits
= 5;
399 irb
->Base
.GreenBits
= 6;
400 irb
->Base
.BlueBits
= 5;
401 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
404 irb
->Base
._ActualFormat
= GL_RGBA8
;
405 irb
->Base
._BaseFormat
= GL_RGBA
;
406 irb
->Base
.RedBits
= 8;
407 irb
->Base
.GreenBits
= 8;
408 irb
->Base
.BlueBits
= 8;
409 irb
->Base
.AlphaBits
= 8;
410 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
412 case GL_STENCIL_INDEX8_EXT
:
413 irb
->Base
._ActualFormat
= GL_STENCIL_INDEX8_EXT
;
414 irb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
415 irb
->Base
.StencilBits
= 8;
416 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
418 case GL_DEPTH_COMPONENT16
:
419 irb
->Base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
420 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
421 irb
->Base
.DepthBits
= 16;
422 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
424 case GL_DEPTH_COMPONENT24
:
425 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
426 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
427 irb
->Base
.DepthBits
= 24;
428 irb
->Base
.DataType
= GL_UNSIGNED_INT
;
430 case GL_DEPTH24_STENCIL8_EXT
:
431 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
432 irb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
433 irb
->Base
.DepthBits
= 24;
434 irb
->Base
.StencilBits
= 8;
435 irb
->Base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
439 "Unexpected intFormat in intel_create_renderbuffer");
443 irb
->Base
.InternalFormat
= intFormat
;
445 /* intel-specific methods */
446 irb
->Base
.Delete
= intel_delete_renderbuffer
;
447 irb
->Base
.AllocStorage
= intel_alloc_window_storage
;
448 irb
->Base
.GetPointer
= intel_get_pointer
;
449 /* This sets the Get/PutRow/Value functions */
450 intel_set_span_functions(&irb
->Base
);
457 * Create a new renderbuffer object.
458 * Typically called via glBindRenderbufferEXT().
460 static struct gl_renderbuffer
*
461 intel_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
463 /*struct intel_context *intel = intel_context(ctx); */
464 struct intel_renderbuffer
*irb
;
466 irb
= CALLOC_STRUCT(intel_renderbuffer
);
468 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
472 _mesa_init_renderbuffer(&irb
->Base
, name
);
473 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
475 /* intel-specific methods */
476 irb
->Base
.Delete
= intel_delete_renderbuffer
;
477 irb
->Base
.AllocStorage
= intel_alloc_renderbuffer_storage
;
478 irb
->Base
.GetPointer
= intel_get_pointer
;
479 /* span routines set in alloc_storage function */
486 * Called via glBindFramebufferEXT().
489 intel_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
490 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
492 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
493 intel_draw_buffer(ctx
, fb
);
494 /* Integer depth range depends on depth buffer bits */
495 if (ctx
->Driver
.DepthRange
!= NULL
)
496 ctx
->Driver
.DepthRange(ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
499 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
505 * Called via glFramebufferRenderbufferEXT().
508 intel_framebuffer_renderbuffer(GLcontext
* ctx
,
509 struct gl_framebuffer
*fb
,
510 GLenum attachment
, struct gl_renderbuffer
*rb
)
512 DBG("Intel FramebufferRenderbuffer %u %u\n", fb
->Name
, rb
? rb
->Name
: 0);
516 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
517 intel_draw_buffer(ctx
, fb
);
521 intel_update_wrapper(GLcontext
*ctx
, struct intel_renderbuffer
*irb
,
522 struct gl_texture_image
*texImage
)
524 if (texImage
->TexFormat
== &_mesa_texformat_argb8888
) {
525 irb
->Base
._ActualFormat
= GL_RGBA8
;
526 irb
->Base
._BaseFormat
= GL_RGBA
;
527 DBG("Render to RGBA8 texture OK\n");
529 else if (texImage
->TexFormat
== &_mesa_texformat_rgb565
) {
530 irb
->Base
._ActualFormat
= GL_RGB5
;
531 irb
->Base
._BaseFormat
= GL_RGB
;
532 DBG("Render to RGB5 texture OK\n");
534 else if (texImage
->TexFormat
== &_mesa_texformat_z16
) {
535 irb
->Base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
536 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
537 DBG("Render to DEPTH16 texture OK\n");
538 } else if (texImage
->TexFormat
== &_mesa_texformat_z24_s8
) {
539 irb
->Base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
540 irb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
541 DBG("Render to DEPTH_STENCIL texture OK\n");
544 DBG("Render to texture BAD FORMAT %d\n",
545 texImage
->TexFormat
->MesaFormat
);
549 irb
->Base
.InternalFormat
= irb
->Base
._ActualFormat
;
550 irb
->Base
.Width
= texImage
->Width
;
551 irb
->Base
.Height
= texImage
->Height
;
552 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
; /* FBO XXX fix */
553 irb
->Base
.RedBits
= texImage
->TexFormat
->RedBits
;
554 irb
->Base
.GreenBits
= texImage
->TexFormat
->GreenBits
;
555 irb
->Base
.BlueBits
= texImage
->TexFormat
->BlueBits
;
556 irb
->Base
.AlphaBits
= texImage
->TexFormat
->AlphaBits
;
557 irb
->Base
.DepthBits
= texImage
->TexFormat
->DepthBits
;
559 irb
->Base
.Delete
= intel_delete_renderbuffer
;
560 irb
->Base
.AllocStorage
= intel_nop_alloc_storage
;
561 intel_set_span_functions(&irb
->Base
);
563 irb
->RenderToTexture
= GL_TRUE
;
569 * When glFramebufferTexture[123]D is called this function sets up the
570 * gl_renderbuffer wrapper around the texture image.
571 * This will have the region info needed for hardware rendering.
573 static struct intel_renderbuffer
*
574 intel_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
576 const GLuint name
= ~0; /* not significant, but distinct for debugging */
577 struct intel_renderbuffer
*irb
;
579 /* make an intel_renderbuffer to wrap the texture image */
580 irb
= CALLOC_STRUCT(intel_renderbuffer
);
582 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
586 _mesa_init_renderbuffer(&irb
->Base
, name
);
587 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
589 if (!intel_update_wrapper(ctx
, irb
, texImage
)) {
599 * Called by glFramebufferTexture[123]DEXT() (and other places) to
600 * prepare for rendering into texture memory. This might be called
601 * many times to choose different texture levels, cube faces, etc
602 * before intel_finish_render_texture() is ever called.
605 intel_render_texture(GLcontext
* ctx
,
606 struct gl_framebuffer
*fb
,
607 struct gl_renderbuffer_attachment
*att
)
609 struct gl_texture_image
*newImage
610 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
611 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
612 struct intel_texture_image
*intel_image
;
620 irb
= intel_wrap_texture(ctx
, newImage
);
622 /* bind the wrapper to the attachment point */
623 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &irb
->Base
);
626 /* fallback to software rendering */
627 _mesa_render_texture(ctx
, fb
, att
);
630 } if (!intel_update_wrapper(ctx
, irb
, newImage
)) {
631 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
632 _mesa_render_texture(ctx
, fb
, att
);
636 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
638 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
641 /* point the renderbufer's region to the texture image region */
642 intel_image
= intel_texture_image(newImage
);
643 if (irb
->region
!= intel_image
->mt
->region
) {
645 intel_region_release(&irb
->region
);
646 intel_region_reference(&irb
->region
, intel_image
->mt
->region
);
649 /* compute offset of the particular 2D image within the texture region */
650 imageOffset
= intel_miptree_image_offset(intel_image
->mt
,
654 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
655 const GLuint
*offsets
= intel_miptree_depth_offsets(intel_image
->mt
,
657 imageOffset
+= offsets
[att
->Zoffset
];
660 /* store that offset in the region */
661 intel_image
->mt
->region
->draw_offset
= imageOffset
;
663 /* update drawing region, etc */
664 intel_draw_buffer(ctx
, fb
);
669 * Called by Mesa when rendering to a texture is done.
672 intel_finish_render_texture(GLcontext
* ctx
,
673 struct gl_renderbuffer_attachment
*att
)
675 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
677 DBG("End render texture (tid %x) tex %u\n", _glthread_GetID(), att
->Texture
->Name
);
680 /* just release the region */
681 intel_region_release(&irb
->region
);
683 else if (att
->Renderbuffer
) {
684 /* software fallback */
685 _mesa_finish_render_texture(ctx
, att
);
686 /* XXX FBO: Need to unmap the buffer (or in intelSpanRenderStart???) */
692 * Do one-time context initializations related to GL_EXT_framebuffer_object.
693 * Hook in device driver functions.
696 intel_fbo_init(struct intel_context
*intel
)
698 intel
->ctx
.Driver
.NewFramebuffer
= intel_new_framebuffer
;
699 intel
->ctx
.Driver
.NewRenderbuffer
= intel_new_renderbuffer
;
700 intel
->ctx
.Driver
.BindFramebuffer
= intel_bind_framebuffer
;
701 intel
->ctx
.Driver
.FramebufferRenderbuffer
= intel_framebuffer_renderbuffer
;
702 intel
->ctx
.Driver
.RenderTexture
= intel_render_texture
;
703 intel
->ctx
.Driver
.FinishRenderTexture
= intel_finish_render_texture
;
704 intel
->ctx
.Driver
.ResizeBuffers
= intel_resize_buffers
;