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"
45 #include "intel_tex.h"
46 #include "intel_span.h"
48 #define FILE_DEBUG_FLAG DEBUG_FBO
52 * Create a new framebuffer object.
54 static struct gl_framebuffer
*
55 intel_new_framebuffer(struct gl_context
* ctx
, GLuint name
)
57 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
60 return _mesa_new_framebuffer(ctx
, name
);
64 /** Called by gl_renderbuffer::Delete() */
66 intel_delete_renderbuffer(struct gl_renderbuffer
*rb
)
68 GET_CURRENT_CONTEXT(ctx
);
69 struct intel_context
*intel
= intel_context(ctx
);
70 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
74 if (intel
&& irb
->region
) {
75 intel_region_release(&irb
->region
);
83 * Return a pointer to a specific pixel in a renderbuffer.
86 intel_get_pointer(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
89 /* By returning NULL we force all software rendering to go through
97 * Called via glRenderbufferStorageEXT() to set the format and allocate
98 * storage for a user-created renderbuffer.
101 intel_alloc_renderbuffer_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
102 GLenum internalFormat
,
103 GLuint width
, GLuint height
)
105 struct intel_context
*intel
= intel_context(ctx
);
106 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
109 ASSERT(rb
->Name
!= 0);
111 switch (internalFormat
) {
113 /* Use the same format-choice logic as for textures.
114 * Renderbuffers aren't any different from textures for us,
115 * except they're less useful because you can't texture with
118 rb
->Format
= intel
->ctx
.Driver
.ChooseTextureFormat(ctx
, internalFormat
,
121 case GL_STENCIL_INDEX
:
122 case GL_STENCIL_INDEX1_EXT
:
123 case GL_STENCIL_INDEX4_EXT
:
124 case GL_STENCIL_INDEX8_EXT
:
125 case GL_STENCIL_INDEX16_EXT
:
126 /* These aren't actual texture formats, so force them here. */
127 rb
->Format
= MESA_FORMAT_S8_Z24
;
131 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
132 rb
->DataType
= intel_mesa_format_to_rb_datatype(rb
->Format
);
133 cpp
= _mesa_get_format_bytes(rb
->Format
);
137 /* free old region */
139 intel_region_release(&irb
->region
);
142 /* allocate new memory region/renderbuffer */
144 /* alloc hardware renderbuffer */
145 DBG("Allocating %d x %d Intel RBO\n", width
, height
);
147 tiling
= I915_TILING_NONE
;
148 if (intel
->use_texture_tiling
) {
149 GLenum base_format
= _mesa_get_format_base_format(rb
->Format
);
151 if (intel
->gen
>= 4 && (base_format
== GL_DEPTH_COMPONENT
||
152 base_format
== GL_DEPTH_STENCIL
))
153 tiling
= I915_TILING_Y
;
155 tiling
= I915_TILING_X
;
158 irb
->region
= intel_region_alloc(intel
->intelScreen
, tiling
, cpp
,
159 width
, height
, GL_TRUE
);
161 return GL_FALSE
; /* out of memory? */
163 ASSERT(irb
->region
->buffer
);
172 #if FEATURE_OES_EGL_image
174 intel_image_target_renderbuffer_storage(struct gl_context
*ctx
,
175 struct gl_renderbuffer
*rb
,
178 struct intel_context
*intel
= intel_context(ctx
);
179 struct intel_renderbuffer
*irb
;
183 screen
= intel
->intelScreen
->driScrnPriv
;
184 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
185 screen
->loaderPrivate
);
189 irb
= intel_renderbuffer(rb
);
191 intel_region_release(&irb
->region
);
192 intel_region_reference(&irb
->region
, image
->region
);
194 rb
->InternalFormat
= image
->internal_format
;
195 rb
->Width
= image
->region
->width
;
196 rb
->Height
= image
->region
->height
;
197 rb
->Format
= image
->format
;
198 rb
->DataType
= image
->data_type
;
199 rb
->_BaseFormat
= _mesa_base_fbo_format(&intel
->ctx
,
200 image
->internal_format
);
205 * Called for each hardware renderbuffer when a _window_ is resized.
206 * Just update fields.
207 * Not used for user-created renderbuffers!
210 intel_alloc_window_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
211 GLenum internalFormat
, GLuint width
, GLuint height
)
213 ASSERT(rb
->Name
== 0);
216 rb
->InternalFormat
= internalFormat
;
223 intel_resize_buffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
224 GLuint width
, GLuint height
)
228 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
230 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
237 /* Make sure all window system renderbuffers are up to date */
238 for (i
= BUFFER_FRONT_LEFT
; i
<= BUFFER_BACK_RIGHT
; i
++) {
239 struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
241 /* only resize if size is changing */
242 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
243 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
249 /** Dummy function for gl_renderbuffer::AllocStorage() */
251 intel_nop_alloc_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
252 GLenum internalFormat
, GLuint width
, GLuint height
)
254 _mesa_problem(ctx
, "intel_op_alloc_storage should never be called.");
260 intel_renderbuffer_set_region(struct intel_context
*intel
,
261 struct intel_renderbuffer
*rb
,
262 struct intel_region
*region
)
264 struct intel_region
*old
;
268 intel_region_reference(&rb
->region
, region
);
269 intel_region_release(&old
);
274 * Create a new intel_renderbuffer which corresponds to an on-screen window,
275 * not a user-created renderbuffer.
277 struct intel_renderbuffer
*
278 intel_create_renderbuffer(gl_format format
)
280 GET_CURRENT_CONTEXT(ctx
);
282 struct intel_renderbuffer
*irb
;
284 irb
= CALLOC_STRUCT(intel_renderbuffer
);
286 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
290 _mesa_init_renderbuffer(&irb
->Base
, 0);
291 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
292 irb
->Base
._BaseFormat
= _mesa_get_format_base_format(format
);
293 irb
->Base
.Format
= format
;
294 irb
->Base
.InternalFormat
= irb
->Base
._BaseFormat
;
295 irb
->Base
.DataType
= intel_mesa_format_to_rb_datatype(format
);
297 /* intel-specific methods */
298 irb
->Base
.Delete
= intel_delete_renderbuffer
;
299 irb
->Base
.AllocStorage
= intel_alloc_window_storage
;
300 irb
->Base
.GetPointer
= intel_get_pointer
;
307 * Create a new renderbuffer object.
308 * Typically called via glBindRenderbufferEXT().
310 static struct gl_renderbuffer
*
311 intel_new_renderbuffer(struct gl_context
* ctx
, GLuint name
)
313 /*struct intel_context *intel = intel_context(ctx); */
314 struct intel_renderbuffer
*irb
;
316 irb
= CALLOC_STRUCT(intel_renderbuffer
);
318 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
322 _mesa_init_renderbuffer(&irb
->Base
, name
);
323 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
325 /* intel-specific methods */
326 irb
->Base
.Delete
= intel_delete_renderbuffer
;
327 irb
->Base
.AllocStorage
= intel_alloc_renderbuffer_storage
;
328 irb
->Base
.GetPointer
= intel_get_pointer
;
329 /* span routines set in alloc_storage function */
336 * Called via glBindFramebufferEXT().
339 intel_bind_framebuffer(struct gl_context
* ctx
, GLenum target
,
340 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
342 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
343 intel_draw_buffer(ctx
, fb
);
346 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
352 * Called via glFramebufferRenderbufferEXT().
355 intel_framebuffer_renderbuffer(struct gl_context
* ctx
,
356 struct gl_framebuffer
*fb
,
357 GLenum attachment
, struct gl_renderbuffer
*rb
)
359 DBG("Intel FramebufferRenderbuffer %u %u\n", fb
->Name
, rb
? rb
->Name
: 0);
363 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
364 intel_draw_buffer(ctx
, fb
);
369 intel_update_wrapper(struct gl_context
*ctx
, struct intel_renderbuffer
*irb
,
370 struct gl_texture_image
*texImage
)
372 if (!intel_span_supports_format(texImage
->TexFormat
)) {
373 DBG("Render to texture BAD FORMAT %s\n",
374 _mesa_get_format_name(texImage
->TexFormat
));
377 DBG("Render to texture %s\n", _mesa_get_format_name(texImage
->TexFormat
));
380 irb
->Base
.Format
= texImage
->TexFormat
;
381 irb
->Base
.DataType
= intel_mesa_format_to_rb_datatype(texImage
->TexFormat
);
382 irb
->Base
.InternalFormat
= texImage
->InternalFormat
;
383 irb
->Base
._BaseFormat
= _mesa_base_fbo_format(ctx
, irb
->Base
.InternalFormat
);
384 irb
->Base
.Width
= texImage
->Width
;
385 irb
->Base
.Height
= texImage
->Height
;
387 irb
->Base
.Delete
= intel_delete_renderbuffer
;
388 irb
->Base
.AllocStorage
= intel_nop_alloc_storage
;
395 * When glFramebufferTexture[123]D is called this function sets up the
396 * gl_renderbuffer wrapper around the texture image.
397 * This will have the region info needed for hardware rendering.
399 static struct intel_renderbuffer
*
400 intel_wrap_texture(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
402 const GLuint name
= ~0; /* not significant, but distinct for debugging */
403 struct intel_renderbuffer
*irb
;
405 /* make an intel_renderbuffer to wrap the texture image */
406 irb
= CALLOC_STRUCT(intel_renderbuffer
);
408 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
412 _mesa_init_renderbuffer(&irb
->Base
, name
);
413 irb
->Base
.ClassID
= INTEL_RB_CLASS
;
415 if (!intel_update_wrapper(ctx
, irb
, texImage
)) {
425 * Called by glFramebufferTexture[123]DEXT() (and other places) to
426 * prepare for rendering into texture memory. This might be called
427 * many times to choose different texture levels, cube faces, etc
428 * before intel_finish_render_texture() is ever called.
431 intel_render_texture(struct gl_context
* ctx
,
432 struct gl_framebuffer
*fb
,
433 struct gl_renderbuffer_attachment
*att
)
435 struct gl_texture_image
*newImage
436 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
437 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
438 struct intel_texture_image
*intel_image
;
445 intel_image
= intel_texture_image(newImage
);
446 if (!intel_image
->mt
) {
447 /* Fallback on drawing to a texture that doesn't have a miptree
448 * (has a border, width/height 0, etc.)
450 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
451 _mesa_render_texture(ctx
, fb
, att
);
455 irb
= intel_wrap_texture(ctx
, newImage
);
457 /* bind the wrapper to the attachment point */
458 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &irb
->Base
);
461 /* fallback to software rendering */
462 _mesa_render_texture(ctx
, fb
, att
);
467 if (!intel_update_wrapper(ctx
, irb
, newImage
)) {
468 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
469 _mesa_render_texture(ctx
, fb
, att
);
473 DBG("Begin render texture tid %lx tex=%u w=%d h=%d refcount=%d\n",
475 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
478 /* point the renderbufer's region to the texture image region */
479 if (irb
->region
!= intel_image
->mt
->region
) {
481 intel_region_release(&irb
->region
);
482 intel_region_reference(&irb
->region
, intel_image
->mt
->region
);
485 /* compute offset of the particular 2D image within the texture region */
486 intel_miptree_get_image_offset(intel_image
->mt
,
492 intel_image
->mt
->region
->draw_offset
= (dst_y
* intel_image
->mt
->region
->pitch
+
493 dst_x
) * intel_image
->mt
->cpp
;
494 intel_image
->mt
->region
->draw_x
= dst_x
;
495 intel_image
->mt
->region
->draw_y
= dst_y
;
496 intel_image
->used_as_render_target
= GL_TRUE
;
498 /* update drawing region, etc */
499 intel_draw_buffer(ctx
, fb
);
504 * Called by Mesa when rendering to a texture is done.
507 intel_finish_render_texture(struct gl_context
* ctx
,
508 struct gl_renderbuffer_attachment
*att
)
510 struct intel_context
*intel
= intel_context(ctx
);
511 struct gl_texture_object
*tex_obj
= att
->Texture
;
512 struct gl_texture_image
*image
=
513 tex_obj
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
514 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
516 DBG("Finish render texture tid %lx tex=%u\n",
517 _glthread_GetID(), att
->Texture
->Name
);
519 /* Flag that this image may now be validated into the object's miptree. */
521 intel_image
->used_as_render_target
= GL_FALSE
;
523 /* Since we've (probably) rendered to the texture and will (likely) use
524 * it in the texture domain later on in this batchbuffer, flush the
525 * batch. Once again, we wish for a domain tracker in libdrm to cover
526 * usage inside of a batchbuffer like GEM does in the kernel.
528 intel_batchbuffer_emit_mi_flush(intel
->batch
);
532 * Do additional "completeness" testing of a framebuffer object.
535 intel_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
537 struct intel_context
*intel
= intel_context(ctx
);
538 const struct intel_renderbuffer
*depthRb
=
539 intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
540 const struct intel_renderbuffer
*stencilRb
=
541 intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
544 if (depthRb
&& stencilRb
&& stencilRb
!= depthRb
) {
545 if (fb
->Attachment
[BUFFER_DEPTH
].Type
== GL_TEXTURE
&&
546 fb
->Attachment
[BUFFER_STENCIL
].Type
== GL_TEXTURE
&&
547 (fb
->Attachment
[BUFFER_DEPTH
].Texture
->Name
==
548 fb
->Attachment
[BUFFER_STENCIL
].Texture
->Name
)) {
551 /* we only support combined depth/stencil buffers, not separate
554 DBG("Only supports combined depth/stencil (found %s, %s)\n",
555 depthRb
? _mesa_get_format_name(depthRb
->Base
.Format
): "NULL",
556 stencilRb
? _mesa_get_format_name(stencilRb
->Base
.Format
): "NULL");
557 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
561 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
562 struct gl_renderbuffer
*rb
;
563 struct intel_renderbuffer
*irb
;
565 if (fb
->Attachment
[i
].Type
== GL_NONE
)
568 /* A supported attachment will have a Renderbuffer set either
569 * from being a Renderbuffer or being a texture that got the
570 * intel_wrap_texture() treatment.
572 rb
= fb
->Attachment
[i
].Renderbuffer
;
574 DBG("attachment without renderbuffer\n");
575 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
579 irb
= intel_renderbuffer(rb
);
581 DBG("software rendering renderbuffer\n");
582 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
586 if (!intel_span_supports_format(irb
->Base
.Format
) ||
587 !intel
->vtbl
.render_target_supported(irb
->Base
.Format
)) {
588 DBG("Unsupported texture/renderbuffer format attached: %s\n",
589 _mesa_get_format_name(irb
->Base
.Format
));
590 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
597 * Do one-time context initializations related to GL_EXT_framebuffer_object.
598 * Hook in device driver functions.
601 intel_fbo_init(struct intel_context
*intel
)
603 intel
->ctx
.Driver
.NewFramebuffer
= intel_new_framebuffer
;
604 intel
->ctx
.Driver
.NewRenderbuffer
= intel_new_renderbuffer
;
605 intel
->ctx
.Driver
.BindFramebuffer
= intel_bind_framebuffer
;
606 intel
->ctx
.Driver
.FramebufferRenderbuffer
= intel_framebuffer_renderbuffer
;
607 intel
->ctx
.Driver
.RenderTexture
= intel_render_texture
;
608 intel
->ctx
.Driver
.FinishRenderTexture
= intel_finish_render_texture
;
609 intel
->ctx
.Driver
.ResizeBuffers
= intel_resize_buffers
;
610 intel
->ctx
.Driver
.ValidateFramebuffer
= intel_validate_framebuffer
;
611 intel
->ctx
.Driver
.BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
613 #if FEATURE_OES_EGL_image
614 intel
->ctx
.Driver
.EGLImageTargetRenderbufferStorage
=
615 intel_image_target_renderbuffer_storage
;