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/texrender.h"
37 #include "drivers/common/meta.h"
39 #include "intel_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_buffers.h"
42 #include "intel_fbo.h"
43 #include "intel_mipmap_tree.h"
44 #include "intel_regions.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
);
63 /** Called by gl_renderbuffer::Delete() */
65 intel_delete_renderbuffer(struct gl_renderbuffer
*rb
)
67 GET_CURRENT_CONTEXT(ctx
);
68 struct intel_context
*intel
= intel_context(ctx
);
69 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
73 if (irb
->span_cache
!= NULL
)
74 _mesa_free(irb
->span_cache
);
76 if (intel
&& irb
->region
) {
77 intel_region_release(&irb
->region
);
85 * Return a pointer to a specific pixel in a renderbuffer.
88 intel_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
91 /* By returning NULL we force all software rendering to go through
99 * Called via glRenderbufferStorageEXT() to set the format and allocate
100 * storage for a user-created renderbuffer.
103 intel_alloc_renderbuffer_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
104 GLenum internalFormat
,
105 GLuint width
, GLuint height
)
107 struct intel_context
*intel
= intel_context(ctx
);
108 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
112 ASSERT(rb
->Name
!= 0);
114 switch (internalFormat
) {
118 rb
->Format
= MESA_FORMAT_RGB565
;
119 rb
->DataType
= GL_UNSIGNED_BYTE
;
126 rb
->Format
= MESA_FORMAT_XRGB8888
;
127 rb
->DataType
= GL_UNSIGNED_BYTE
;
137 rb
->Format
= MESA_FORMAT_ARGB8888
;
138 rb
->DataType
= GL_UNSIGNED_BYTE
;
140 case GL_STENCIL_INDEX
:
141 case GL_STENCIL_INDEX1_EXT
:
142 case GL_STENCIL_INDEX4_EXT
:
143 case GL_STENCIL_INDEX8_EXT
:
144 case GL_STENCIL_INDEX16_EXT
:
145 /* alloc a depth+stencil buffer */
146 rb
->Format
= MESA_FORMAT_S8_Z24
;
147 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
149 case GL_DEPTH_COMPONENT16
:
150 rb
->Format
= MESA_FORMAT_Z16
;
151 rb
->DataType
= GL_UNSIGNED_SHORT
;
153 case GL_DEPTH_COMPONENT
:
154 case GL_DEPTH_COMPONENT24
:
155 case GL_DEPTH_COMPONENT32
:
156 rb
->Format
= MESA_FORMAT_S8_Z24
;
157 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
159 case GL_DEPTH_STENCIL_EXT
:
160 case GL_DEPTH24_STENCIL8_EXT
:
161 rb
->Format
= MESA_FORMAT_S8_Z24
;
162 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
166 "Unexpected format in intel_alloc_renderbuffer_storage");
170 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
171 cpp
= _mesa_get_format_bytes(rb
->Format
);
175 /* free old region */
177 intel_region_release(&irb
->region
);
180 /* allocate new memory region/renderbuffer */
182 /* Choose a pitch to match hardware requirements:
184 pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
186 /* alloc hardware renderbuffer */
187 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width
, height
, pitch
);
189 irb
->region
= intel_region_alloc(intel
, I915_TILING_NONE
, cpp
,
190 width
, height
, pitch
, GL_TRUE
);
192 return GL_FALSE
; /* out of memory? */
194 ASSERT(irb
->region
->buffer
);
204 * Called for each hardware renderbuffer when a _window_ is resized.
205 * Just update fields.
206 * Not used for user-created renderbuffers!
209 intel_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
210 GLenum internalFormat
, GLuint width
, GLuint height
)
212 ASSERT(rb
->Name
== 0);
215 rb
->InternalFormat
= internalFormat
;
222 intel_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
223 GLuint width
, GLuint height
)
227 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
229 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
236 /* Make sure all window system renderbuffers are up to date */
237 for (i
= BUFFER_FRONT_LEFT
; i
<= BUFFER_BACK_RIGHT
; i
++) {
238 struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
240 /* only resize if size is changing */
241 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
242 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
248 /** Dummy function for gl_renderbuffer::AllocStorage() */
250 intel_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
251 GLenum internalFormat
, GLuint width
, GLuint height
)
253 _mesa_problem(ctx
, "intel_op_alloc_storage should never be called.");
259 intel_renderbuffer_set_region(struct intel_renderbuffer
*rb
,
260 struct intel_region
*region
)
262 struct intel_region
*old
;
266 intel_region_reference(&rb
->region
, region
);
267 intel_region_release(&old
);
272 * Create a new intel_renderbuffer which corresponds to an on-screen window,
273 * not a user-created renderbuffer.
275 struct intel_renderbuffer
*
276 intel_create_renderbuffer(gl_format format
)
278 GET_CURRENT_CONTEXT(ctx
);
280 struct intel_renderbuffer
*irb
;
282 irb
= CALLOC_STRUCT(intel_renderbuffer
);
284 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
288 _mesa_init_renderbuffer(&irb
->Base
, 0);
289 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
292 case MESA_FORMAT_RGB565
:
293 irb
->Base
._BaseFormat
= GL_RGB
;
294 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
296 case MESA_FORMAT_XRGB8888
:
297 irb
->Base
._BaseFormat
= GL_RGB
;
298 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
300 case MESA_FORMAT_ARGB8888
:
301 irb
->Base
._BaseFormat
= GL_RGBA
;
302 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
304 case MESA_FORMAT_Z16
:
305 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
306 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
308 case MESA_FORMAT_X8_Z24
:
309 irb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
310 irb
->Base
.DataType
= GL_UNSIGNED_INT
;
312 case MESA_FORMAT_S8_Z24
:
313 irb
->Base
._BaseFormat
= GL_DEPTH_STENCIL
;
314 irb
->Base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
318 "Unexpected intFormat in intel_create_renderbuffer");
323 irb
->Base
.Format
= format
;
324 irb
->Base
.InternalFormat
= irb
->Base
._BaseFormat
;
326 /* intel-specific methods */
327 irb
->Base
.Delete
= intel_delete_renderbuffer
;
328 irb
->Base
.AllocStorage
= intel_alloc_window_storage
;
329 irb
->Base
.GetPointer
= intel_get_pointer
;
336 * Create a new renderbuffer object.
337 * Typically called via glBindRenderbufferEXT().
339 static struct gl_renderbuffer
*
340 intel_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
342 /*struct intel_context *intel = intel_context(ctx); */
343 struct intel_renderbuffer
*irb
;
345 irb
= CALLOC_STRUCT(intel_renderbuffer
);
347 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
351 _mesa_init_renderbuffer(&irb
->Base
, name
);
352 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
354 /* intel-specific methods */
355 irb
->Base
.Delete
= intel_delete_renderbuffer
;
356 irb
->Base
.AllocStorage
= intel_alloc_renderbuffer_storage
;
357 irb
->Base
.GetPointer
= intel_get_pointer
;
358 /* span routines set in alloc_storage function */
365 * Called via glBindFramebufferEXT().
368 intel_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
369 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
371 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
372 intel_draw_buffer(ctx
, fb
);
375 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
381 * Called via glFramebufferRenderbufferEXT().
384 intel_framebuffer_renderbuffer(GLcontext
* ctx
,
385 struct gl_framebuffer
*fb
,
386 GLenum attachment
, struct gl_renderbuffer
*rb
)
388 DBG("Intel FramebufferRenderbuffer %u %u\n", fb
->Name
, rb
? rb
->Name
: 0);
392 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
393 intel_draw_buffer(ctx
, fb
);
398 intel_update_wrapper(GLcontext
*ctx
, struct intel_renderbuffer
*irb
,
399 struct gl_texture_image
*texImage
)
401 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) {
402 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
403 DBG("Render to RGBA8 texture OK\n");
405 else if (texImage
->TexFormat
== MESA_FORMAT_XRGB8888
) {
406 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
407 DBG("Render to XGBA8 texture OK\n");
409 else if (texImage
->TexFormat
== MESA_FORMAT_RGB565
) {
410 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
411 DBG("Render to RGB5 texture OK\n");
413 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB1555
) {
414 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
415 DBG("Render to ARGB1555 texture OK\n");
417 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB4444
) {
418 irb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
419 DBG("Render to ARGB4444 texture OK\n");
421 else if (texImage
->TexFormat
== MESA_FORMAT_Z16
) {
422 irb
->Base
.DataType
= GL_UNSIGNED_SHORT
;
423 DBG("Render to DEPTH16 texture OK\n");
425 else if (texImage
->TexFormat
== MESA_FORMAT_S8_Z24
) {
426 irb
->Base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
427 DBG("Render to DEPTH_STENCIL texture OK\n");
430 DBG("Render to texture BAD FORMAT %s\n",
431 _mesa_get_format_name(texImage
->TexFormat
));
435 irb
->Base
.Format
= texImage
->TexFormat
;
437 irb
->Base
.InternalFormat
= texImage
->InternalFormat
;
438 irb
->Base
._BaseFormat
= _mesa_base_fbo_format(ctx
, irb
->Base
.InternalFormat
);
439 irb
->Base
.Width
= texImage
->Width
;
440 irb
->Base
.Height
= texImage
->Height
;
442 irb
->Base
.Delete
= intel_delete_renderbuffer
;
443 irb
->Base
.AllocStorage
= intel_nop_alloc_storage
;
450 * When glFramebufferTexture[123]D is called this function sets up the
451 * gl_renderbuffer wrapper around the texture image.
452 * This will have the region info needed for hardware rendering.
454 static struct intel_renderbuffer
*
455 intel_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
457 const GLuint name
= ~0; /* not significant, but distinct for debugging */
458 struct intel_renderbuffer
*irb
;
460 /* make an intel_renderbuffer to wrap the texture image */
461 irb
= CALLOC_STRUCT(intel_renderbuffer
);
463 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
467 _mesa_init_renderbuffer(&irb
->Base
, name
);
468 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
470 if (!intel_update_wrapper(ctx
, irb
, texImage
)) {
480 * Called by glFramebufferTexture[123]DEXT() (and other places) to
481 * prepare for rendering into texture memory. This might be called
482 * many times to choose different texture levels, cube faces, etc
483 * before intel_finish_render_texture() is ever called.
486 intel_render_texture(GLcontext
* ctx
,
487 struct gl_framebuffer
*fb
,
488 struct gl_renderbuffer_attachment
*att
)
490 struct gl_texture_image
*newImage
491 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
492 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
493 struct intel_texture_image
*intel_image
;
500 intel_image
= intel_texture_image(newImage
);
501 if (!intel_image
->mt
) {
502 /* Fallback on drawing to a texture that doesn't have a miptree
503 * (has a border, width/height 0, etc.)
505 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
506 _mesa_render_texture(ctx
, fb
, att
);
510 irb
= intel_wrap_texture(ctx
, newImage
);
512 /* bind the wrapper to the attachment point */
513 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &irb
->Base
);
516 /* fallback to software rendering */
517 _mesa_render_texture(ctx
, fb
, att
);
522 if (!intel_update_wrapper(ctx
, irb
, newImage
)) {
523 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
524 _mesa_render_texture(ctx
, fb
, att
);
528 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
530 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
533 /* point the renderbufer's region to the texture image region */
534 if (irb
->region
!= intel_image
->mt
->region
) {
536 intel_region_release(&irb
->region
);
537 intel_region_reference(&irb
->region
, intel_image
->mt
->region
);
540 /* compute offset of the particular 2D image within the texture region */
541 intel_miptree_get_image_offset(intel_image
->mt
,
547 intel_image
->mt
->region
->draw_offset
= (dst_y
* intel_image
->mt
->pitch
+
548 dst_x
) * intel_image
->mt
->cpp
;
549 intel_image
->mt
->region
->draw_x
= dst_x
;
550 intel_image
->mt
->region
->draw_y
= dst_y
;
551 intel_image
->used_as_render_target
= GL_TRUE
;
553 /* update drawing region, etc */
554 intel_draw_buffer(ctx
, fb
);
559 * Called by Mesa when rendering to a texture is done.
562 intel_finish_render_texture(GLcontext
* ctx
,
563 struct gl_renderbuffer_attachment
*att
)
565 struct intel_context
*intel
= intel_context(ctx
);
566 struct gl_texture_object
*tex_obj
= att
->Texture
;
567 struct gl_texture_image
*image
=
568 tex_obj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
569 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
571 /* Flag that this image may now be validated into the object's miptree. */
572 intel_image
->used_as_render_target
= GL_FALSE
;
574 /* Since we've (probably) rendered to the texture and will (likely) use
575 * it in the texture domain later on in this batchbuffer, flush the
576 * batch. Once again, we wish for a domain tracker in libdrm to cover
577 * usage inside of a batchbuffer like GEM does in the kernel.
579 intel_batchbuffer_emit_mi_flush(intel
->batch
);
583 * Do additional "completeness" testing of a framebuffer object.
586 intel_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
588 const struct intel_renderbuffer
*depthRb
=
589 intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
590 const struct intel_renderbuffer
*stencilRb
=
591 intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
594 if (depthRb
&& stencilRb
&& stencilRb
!= depthRb
) {
595 if (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Type
== GL_TEXTURE
&&
596 ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Type
== GL_TEXTURE
&&
597 (ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Texture
->Name
==
598 ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Texture
->Name
)) {
601 /* we only support combined depth/stencil buffers, not separate
604 DBG("Only supports combined depth/stencil (found %s, %s)\n",
605 depthRb
? _mesa_get_format_name(depthRb
->Base
.Format
): "NULL",
606 stencilRb
? _mesa_get_format_name(stencilRb
->Base
.Format
): "NULL");
607 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
611 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
612 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
613 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
619 DBG("software rendering renderbuffer\n");
620 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
624 switch (irb
->Base
.Format
) {
625 case MESA_FORMAT_ARGB8888
:
626 case MESA_FORMAT_XRGB8888
:
627 case MESA_FORMAT_RGB565
:
628 case MESA_FORMAT_ARGB1555
:
629 case MESA_FORMAT_ARGB4444
:
632 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
639 * Do one-time context initializations related to GL_EXT_framebuffer_object.
640 * Hook in device driver functions.
643 intel_fbo_init(struct intel_context
*intel
)
645 intel
->ctx
.Driver
.NewFramebuffer
= intel_new_framebuffer
;
646 intel
->ctx
.Driver
.NewRenderbuffer
= intel_new_renderbuffer
;
647 intel
->ctx
.Driver
.BindFramebuffer
= intel_bind_framebuffer
;
648 intel
->ctx
.Driver
.FramebufferRenderbuffer
= intel_framebuffer_renderbuffer
;
649 intel
->ctx
.Driver
.RenderTexture
= intel_render_texture
;
650 intel
->ctx
.Driver
.FinishRenderTexture
= intel_finish_render_texture
;
651 intel
->ctx
.Driver
.ResizeBuffers
= intel_resize_buffers
;
652 intel
->ctx
.Driver
.ValidateFramebuffer
= intel_validate_framebuffer
;
653 intel
->ctx
.Driver
.BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;