1 /**************************************************************************
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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/enums.h"
30 #include "main/imports.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
33 #include "main/fbobject.h"
34 #include "main/framebuffer.h"
35 #include "main/renderbuffer.h"
36 #include "main/context.h"
37 #include "main/teximage.h"
38 #include "main/image.h"
39 #include "main/hash_table.h"
42 #include "swrast/swrast.h"
43 #include "drivers/common/meta.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_buffers.h"
47 #include "intel_blit.h"
48 #include "intel_fbo.h"
49 #include "intel_mipmap_tree.h"
50 #include "intel_regions.h"
51 #include "intel_screen.h"
52 #include "intel_tex.h"
53 #include "brw_context.h"
55 #define FILE_DEBUG_FLAG DEBUG_FBO
58 * Create a new framebuffer object.
60 static struct gl_framebuffer
*
61 intel_new_framebuffer(struct gl_context
* ctx
, GLuint name
)
63 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
66 return _mesa_new_framebuffer(ctx
, name
);
70 /** Called by gl_renderbuffer::Delete() */
72 intel_delete_renderbuffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
74 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
78 intel_miptree_release(&irb
->mt
);
79 intel_miptree_release(&irb
->singlesample_mt
);
81 _mesa_delete_renderbuffer(ctx
, rb
);
85 * \brief Downsample a winsys renderbuffer from mt to singlesample_mt.
87 * If the miptree needs no downsample, then skip.
90 intel_renderbuffer_downsample(struct brw_context
*brw
,
91 struct intel_renderbuffer
*irb
)
93 if (!irb
->need_downsample
)
95 intel_miptree_updownsample(brw
, irb
->mt
, irb
->singlesample_mt
);
96 irb
->need_downsample
= false;
100 * \brief Upsample a winsys renderbuffer from singlesample_mt to mt.
102 * The upsample is done unconditionally.
105 intel_renderbuffer_upsample(struct brw_context
*brw
,
106 struct intel_renderbuffer
*irb
)
108 assert(!irb
->need_downsample
);
110 intel_miptree_updownsample(brw
, irb
->singlesample_mt
, irb
->mt
);
114 * \see dd_function_table::MapRenderbuffer
117 intel_map_renderbuffer(struct gl_context
*ctx
,
118 struct gl_renderbuffer
*rb
,
119 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
124 struct brw_context
*brw
= brw_context(ctx
);
125 struct swrast_renderbuffer
*srb
= (struct swrast_renderbuffer
*)rb
;
126 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
127 struct intel_mipmap_tree
*mt
;
132 /* this is a malloc'd renderbuffer (accum buffer), not an irb */
133 GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
134 GLint rowStride
= srb
->RowStride
;
135 *out_map
= (GLubyte
*) srb
->Buffer
+ y
* rowStride
+ x
* bpp
;
136 *out_stride
= rowStride
;
140 intel_prepare_render(brw
);
142 /* The MapRenderbuffer API should always return a single-sampled mapping.
143 * The case we are asked to map multisampled RBs is in glReadPixels() (or
144 * swrast paths like glCopyTexImage()) from a window-system MSAA buffer,
145 * and GL expects an automatic resolve to happen.
147 * If it's a color miptree, there is a ->singlesample_mt which wraps the
148 * actual window system renderbuffer (which we may resolve to at any time),
149 * while the miptree itself is our driver-private allocation. If it's a
150 * depth or stencil miptree, we have a private MSAA buffer and no shared
151 * singlesample buffer, and since we don't expect anybody to ever actually
152 * resolve it, we just make a temporary singlesample buffer now when we
155 if (rb
->NumSamples
> 1) {
156 if (!irb
->singlesample_mt
) {
157 irb
->singlesample_mt
=
158 intel_miptree_create_for_renderbuffer(brw
, irb
->mt
->format
,
159 rb
->Width
, rb
->Height
,
161 if (!irb
->singlesample_mt
)
163 irb
->singlesample_mt_is_tmp
= true;
164 irb
->need_downsample
= true;
167 intel_renderbuffer_downsample(brw
, irb
);
168 mt
= irb
->singlesample_mt
;
170 irb
->need_map_upsample
= mode
& GL_MAP_WRITE_BIT
;
175 /* For a window-system renderbuffer, we need to flip the mapping we receive
176 * upside-down. So we need to ask for a rectangle on flipped vertically, and
177 * we then return a pointer to the bottom of it with a negative stride.
180 y
= rb
->Height
- y
- h
;
183 intel_miptree_map(brw
, mt
, irb
->mt_level
, irb
->mt_layer
,
184 x
, y
, w
, h
, mode
, &map
, &stride
);
187 map
+= (h
- 1) * stride
;
191 DBG("%s: rb %d (%s) mt mapped: (%d, %d) (%dx%d) -> %p/%d\n",
192 __FUNCTION__
, rb
->Name
, _mesa_get_format_name(rb
->Format
),
193 x
, y
, w
, h
, map
, stride
);
196 *out_stride
= stride
;
205 * \see dd_function_table::UnmapRenderbuffer
208 intel_unmap_renderbuffer(struct gl_context
*ctx
,
209 struct gl_renderbuffer
*rb
)
211 struct brw_context
*brw
= brw_context(ctx
);
212 struct swrast_renderbuffer
*srb
= (struct swrast_renderbuffer
*)rb
;
213 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
214 struct intel_mipmap_tree
*mt
;
216 DBG("%s: rb %d (%s)\n", __FUNCTION__
,
217 rb
->Name
, _mesa_get_format_name(rb
->Format
));
220 /* this is a malloc'd renderbuffer (accum buffer) */
225 if (rb
->NumSamples
> 1) {
226 mt
= irb
->singlesample_mt
;
231 intel_miptree_unmap(brw
, mt
, irb
->mt_level
, irb
->mt_layer
);
233 if (irb
->need_map_upsample
) {
234 intel_renderbuffer_upsample(brw
, irb
);
235 irb
->need_map_upsample
= false;
238 if (irb
->singlesample_mt_is_tmp
)
239 intel_miptree_release(&irb
->singlesample_mt
);
244 * Round up the requested multisample count to the next supported sample size.
247 intel_quantize_num_samples(struct intel_screen
*intel
, unsigned num_samples
)
249 const int *msaa_modes
= intel_supported_msaa_modes(intel
);
250 int quantized_samples
= 0;
252 for (int i
= 0; msaa_modes
[i
] != -1; ++i
) {
253 if (msaa_modes
[i
] >= num_samples
)
254 quantized_samples
= msaa_modes
[i
];
259 return quantized_samples
;
264 * Called via glRenderbufferStorageEXT() to set the format and allocate
265 * storage for a user-created renderbuffer.
268 intel_alloc_renderbuffer_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
269 GLenum internalFormat
,
270 GLuint width
, GLuint height
)
272 struct brw_context
*brw
= brw_context(ctx
);
273 struct intel_screen
*screen
= brw
->intelScreen
;
274 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
275 rb
->NumSamples
= intel_quantize_num_samples(screen
, rb
->NumSamples
);
277 switch (internalFormat
) {
279 /* Use the same format-choice logic as for textures.
280 * Renderbuffers aren't any different from textures for us,
281 * except they're less useful because you can't texture with
284 rb
->Format
= ctx
->Driver
.ChooseTextureFormat(ctx
, GL_TEXTURE_2D
,
288 case GL_STENCIL_INDEX
:
289 case GL_STENCIL_INDEX1_EXT
:
290 case GL_STENCIL_INDEX4_EXT
:
291 case GL_STENCIL_INDEX8_EXT
:
292 case GL_STENCIL_INDEX16_EXT
:
293 /* These aren't actual texture formats, so force them here. */
294 if (brw
->has_separate_stencil
) {
295 rb
->Format
= MESA_FORMAT_S_UINT8
;
297 assert(!brw
->must_use_separate_stencil
);
298 rb
->Format
= MESA_FORMAT_Z24_UNORM_S8_UINT
;
305 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
307 intel_miptree_release(&irb
->mt
);
309 DBG("%s: %s: %s (%dx%d)\n", __FUNCTION__
,
310 _mesa_lookup_enum_by_nr(internalFormat
),
311 _mesa_get_format_name(rb
->Format
), width
, height
);
313 if (width
== 0 || height
== 0)
316 irb
->mt
= intel_miptree_create_for_renderbuffer(brw
, rb
->Format
,
327 intel_image_target_renderbuffer_storage(struct gl_context
*ctx
,
328 struct gl_renderbuffer
*rb
,
331 struct brw_context
*brw
= brw_context(ctx
);
332 struct intel_renderbuffer
*irb
;
336 screen
= brw
->intelScreen
->driScrnPriv
;
337 image
= screen
->dri2
.image
->lookupEGLImage(screen
, image_handle
,
338 screen
->loaderPrivate
);
342 if (image
->planar_format
&& image
->planar_format
->nplanes
> 1) {
343 _mesa_error(ctx
, GL_INVALID_OPERATION
,
344 "glEGLImageTargetRenderbufferStorage(planar buffers are not "
345 "supported as render targets.");
349 /* Buffers originating from outside are for read-only. */
350 if (image
->dma_buf_imported
) {
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "glEGLImageTargetRenderbufferStorage(dma buffers are read-only)");
356 /* __DRIimage is opaque to the core so it has to be checked here */
357 switch (image
->format
) {
358 case MESA_FORMAT_R8G8B8A8_UNORM
:
359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
360 "glEGLImageTargetRenderbufferStorage(unsupported image format");
367 irb
= intel_renderbuffer(rb
);
368 intel_miptree_release(&irb
->mt
);
369 irb
->mt
= intel_miptree_create_for_bo(brw
,
373 image
->region
->width
,
374 image
->region
->height
,
375 image
->region
->pitch
,
376 image
->region
->tiling
);
380 rb
->InternalFormat
= image
->internal_format
;
381 rb
->Width
= image
->region
->width
;
382 rb
->Height
= image
->region
->height
;
383 rb
->Format
= image
->format
;
384 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, image
->internal_format
);
385 rb
->NeedsFinishRenderTexture
= true;
389 * Called by _mesa_resize_framebuffer() for each hardware renderbuffer when a
390 * window system framebuffer is resized.
392 * Any actual buffer reallocations for hardware renderbuffers (which would
393 * have triggered _mesa_resize_framebuffer()) were done by
394 * intel_process_dri2_buffer().
397 intel_alloc_window_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
398 GLenum internalFormat
, GLuint width
, GLuint height
)
400 ASSERT(rb
->Name
== 0);
403 rb
->InternalFormat
= internalFormat
;
408 /** Dummy function for gl_renderbuffer::AllocStorage() */
410 intel_nop_alloc_storage(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
411 GLenum internalFormat
, GLuint width
, GLuint height
)
413 _mesa_problem(ctx
, "intel_op_alloc_storage should never be called.");
418 * Create a new intel_renderbuffer which corresponds to an on-screen window,
419 * not a user-created renderbuffer.
421 * \param num_samples must be quantized.
423 struct intel_renderbuffer
*
424 intel_create_renderbuffer(mesa_format format
, unsigned num_samples
)
426 struct intel_renderbuffer
*irb
;
427 struct gl_renderbuffer
*rb
;
429 GET_CURRENT_CONTEXT(ctx
);
431 irb
= CALLOC_STRUCT(intel_renderbuffer
);
433 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
437 rb
= &irb
->Base
.Base
;
439 _mesa_init_renderbuffer(rb
, 0);
440 rb
->ClassID
= INTEL_RB_CLASS
;
441 rb
->_BaseFormat
= _mesa_get_format_base_format(format
);
443 rb
->InternalFormat
= rb
->_BaseFormat
;
444 rb
->NumSamples
= num_samples
;
446 /* intel-specific methods */
447 rb
->Delete
= intel_delete_renderbuffer
;
448 rb
->AllocStorage
= intel_alloc_window_storage
;
454 * Private window-system buffers (as opposed to ones shared with the display
455 * server created with intel_create_renderbuffer()) are most similar in their
456 * handling to user-created renderbuffers, but they have a resize handler that
457 * may be called at intel_update_renderbuffers() time.
459 * \param num_samples must be quantized.
461 struct intel_renderbuffer
*
462 intel_create_private_renderbuffer(mesa_format format
, unsigned num_samples
)
464 struct intel_renderbuffer
*irb
;
466 irb
= intel_create_renderbuffer(format
, num_samples
);
467 irb
->Base
.Base
.AllocStorage
= intel_alloc_renderbuffer_storage
;
473 * Create a new renderbuffer object.
474 * Typically called via glBindRenderbufferEXT().
476 static struct gl_renderbuffer
*
477 intel_new_renderbuffer(struct gl_context
* ctx
, GLuint name
)
479 struct intel_renderbuffer
*irb
;
480 struct gl_renderbuffer
*rb
;
482 irb
= CALLOC_STRUCT(intel_renderbuffer
);
484 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
488 rb
= &irb
->Base
.Base
;
490 _mesa_init_renderbuffer(rb
, name
);
491 rb
->ClassID
= INTEL_RB_CLASS
;
493 /* intel-specific methods */
494 rb
->Delete
= intel_delete_renderbuffer
;
495 rb
->AllocStorage
= intel_alloc_renderbuffer_storage
;
496 /* span routines set in alloc_storage function */
502 intel_renderbuffer_update_wrapper(struct brw_context
*brw
,
503 struct intel_renderbuffer
*irb
,
504 struct gl_texture_image
*image
,
507 struct gl_renderbuffer
*rb
= &irb
->Base
.Base
;
508 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
509 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
510 int level
= image
->Level
;
512 rb
->AllocStorage
= intel_nop_alloc_storage
;
514 /* adjust for texture view parameters */
515 layer
+= image
->TexObject
->MinLayer
;
516 level
+= image
->TexObject
->MinLevel
;
518 intel_miptree_check_level_layer(mt
, level
, layer
);
519 irb
->mt_level
= level
;
521 switch (mt
->msaa_layout
) {
522 case INTEL_MSAA_LAYOUT_UMS
:
523 case INTEL_MSAA_LAYOUT_CMS
:
524 irb
->mt_layer
= layer
* mt
->num_samples
;
528 irb
->mt_layer
= layer
;
531 intel_miptree_reference(&irb
->mt
, mt
);
533 intel_renderbuffer_set_draw_offset(irb
);
535 if (mt
->hiz_mt
== NULL
&& brw_is_hiz_depth_format(brw
, rb
->Format
)) {
536 intel_miptree_alloc_hiz(brw
, mt
);
545 intel_renderbuffer_set_draw_offset(struct intel_renderbuffer
*irb
)
547 unsigned int dst_x
, dst_y
;
549 /* compute offset of the particular 2D image within the texture region */
550 intel_miptree_get_image_offset(irb
->mt
,
560 * Called by glFramebufferTexture[123]DEXT() (and other places) to
561 * prepare for rendering into texture memory. This might be called
562 * many times to choose different texture levels, cube faces, etc
563 * before intel_finish_render_texture() is ever called.
566 intel_render_texture(struct gl_context
* ctx
,
567 struct gl_framebuffer
*fb
,
568 struct gl_renderbuffer_attachment
*att
)
570 struct brw_context
*brw
= brw_context(ctx
);
571 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
572 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
573 struct gl_texture_image
*image
= rb
->TexImage
;
574 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
575 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
580 if (att
->CubeMapFace
> 0) {
581 assert(att
->Zoffset
== 0);
582 layer
= att
->CubeMapFace
;
584 layer
= att
->Zoffset
;
587 if (!intel_image
->mt
) {
588 /* Fallback on drawing to a texture that doesn't have a miptree
589 * (has a border, width/height 0, etc.)
591 _swrast_render_texture(ctx
, fb
, att
);
595 intel_miptree_check_level_layer(mt
, att
->TextureLevel
, layer
);
597 if (!intel_renderbuffer_update_wrapper(brw
, irb
, image
, layer
)) {
598 _swrast_render_texture(ctx
, fb
, att
);
602 DBG("Begin render %s texture tex=%u w=%d h=%d d=%d refcount=%d\n",
603 _mesa_get_format_name(image
->TexFormat
),
604 att
->Texture
->Name
, image
->Width
, image
->Height
, image
->Depth
,
609 #define fbo_incomplete(fb, ...) do { \
610 static GLuint msg_id = 0; \
611 if (unlikely(ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT)) { \
612 _mesa_gl_debug(ctx, &msg_id, \
613 MESA_DEBUG_TYPE_OTHER, \
614 MESA_DEBUG_SEVERITY_MEDIUM, \
618 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED; \
622 * Do additional "completeness" testing of a framebuffer object.
625 intel_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
627 struct brw_context
*brw
= brw_context(ctx
);
628 struct intel_renderbuffer
*depthRb
=
629 intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
630 struct intel_renderbuffer
*stencilRb
=
631 intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
632 struct intel_mipmap_tree
*depth_mt
= NULL
, *stencil_mt
= NULL
;
635 DBG("%s() on fb %p (%s)\n", __FUNCTION__
,
636 fb
, (fb
== ctx
->DrawBuffer
? "drawbuffer" :
637 (fb
== ctx
->ReadBuffer
? "readbuffer" : "other buffer")));
640 depth_mt
= depthRb
->mt
;
642 stencil_mt
= stencilRb
->mt
;
643 if (stencil_mt
->stencil_mt
)
644 stencil_mt
= stencil_mt
->stencil_mt
;
647 if (depth_mt
&& stencil_mt
) {
649 /* For gen >= 7, we are using the lod/minimum-array-element fields
650 * and supportting layered rendering. This means that we must restrict
651 * the depth & stencil attachments to match in various more retrictive
652 * ways. (width, height, depth, LOD and layer)
654 if (depth_mt
->physical_width0
!= stencil_mt
->physical_width0
||
655 depth_mt
->physical_height0
!= stencil_mt
->physical_height0
||
656 depth_mt
->physical_depth0
!= stencil_mt
->physical_depth0
||
657 depthRb
->mt_level
!= stencilRb
->mt_level
||
658 depthRb
->mt_layer
!= stencilRb
->mt_layer
) {
660 "FBO incomplete: depth and stencil must match in"
661 "width, height, depth, LOD and layer\n");
664 if (depth_mt
== stencil_mt
) {
665 /* For true packed depth/stencil (not faked on prefers-separate-stencil
666 * hardware) we need to be sure they're the same level/layer, since
667 * we'll be emitting a single packet describing the packed setup.
669 if (depthRb
->mt_level
!= stencilRb
->mt_level
||
670 depthRb
->mt_layer
!= stencilRb
->mt_layer
) {
672 "FBO incomplete: depth image level/layer %d/%d != "
673 "stencil image %d/%d\n",
677 stencilRb
->mt_layer
);
680 if (!brw
->has_separate_stencil
) {
681 fbo_incomplete(fb
, "FBO incomplete: separate stencil "
684 if (stencil_mt
->format
!= MESA_FORMAT_S_UINT8
) {
685 fbo_incomplete(fb
, "FBO incomplete: separate stencil is %s "
687 _mesa_get_format_name(stencil_mt
->format
));
689 if (brw
->gen
< 7 && !intel_renderbuffer_has_hiz(depthRb
)) {
690 /* Before Gen7, separate depth and stencil buffers can be used
691 * only if HiZ is enabled. From the Sandybridge PRM, Volume 2,
692 * Part 1, Bit 3DSTATE_DEPTH_BUFFER.SeparateStencilBufferEnable:
693 * [DevSNB]: This field must be set to the same value (enabled
694 * or disabled) as Hierarchical Depth Buffer Enable.
696 fbo_incomplete(fb
, "FBO incomplete: separate stencil "
702 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
703 struct gl_renderbuffer
*rb
;
704 struct intel_renderbuffer
*irb
;
706 if (fb
->Attachment
[i
].Type
== GL_NONE
)
709 /* A supported attachment will have a Renderbuffer set either
710 * from being a Renderbuffer or being a texture that got the
711 * intel_wrap_texture() treatment.
713 rb
= fb
->Attachment
[i
].Renderbuffer
;
715 fbo_incomplete(fb
, "FBO incomplete: attachment without "
720 if (fb
->Attachment
[i
].Type
== GL_TEXTURE
) {
721 if (rb
->TexImage
->Border
) {
722 fbo_incomplete(fb
, "FBO incomplete: texture with border\n");
727 irb
= intel_renderbuffer(rb
);
729 fbo_incomplete(fb
, "FBO incomplete: software rendering "
734 if (!brw_render_target_supported(brw
, rb
)) {
735 fbo_incomplete(fb
, "FBO incomplete: Unsupported HW "
736 "texture/renderbuffer format attached: %s\n",
737 _mesa_get_format_name(intel_rb_format(irb
)));
743 * Try to do a glBlitFramebuffer using glCopyTexSubImage2D
744 * We can do this when the dst renderbuffer is actually a texture and
745 * there is no scaling, mirroring or scissoring.
747 * \return new buffer mask indicating the buffers left to blit using the
751 intel_blit_framebuffer_with_blitter(struct gl_context
*ctx
,
752 GLint srcX0
, GLint srcY0
,
753 GLint srcX1
, GLint srcY1
,
754 GLint dstX0
, GLint dstY0
,
755 GLint dstX1
, GLint dstY1
,
756 GLbitfield mask
, GLenum filter
)
758 struct brw_context
*brw
= brw_context(ctx
);
760 /* Sync up the state of window system buffers. We need to do this before
761 * we go looking for the buffers.
763 intel_prepare_render(brw
);
765 if (mask
& GL_COLOR_BUFFER_BIT
) {
767 const struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
768 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
769 struct gl_renderbuffer
*src_rb
= readFb
->_ColorReadBuffer
;
770 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
773 perf_debug("glBlitFramebuffer(): missing src renderbuffer. "
774 "Falling back to software rendering.\n");
778 /* If the source and destination are the same size with no mirroring,
779 * the rectangles are within the size of the texture and there is no
780 * scissor, then we can probably use the blit engine.
782 if (!(srcX0
- srcX1
== dstX0
- dstX1
&&
783 srcY0
- srcY1
== dstY0
- dstY1
&&
786 srcX0
>= 0 && srcX1
<= readFb
->Width
&&
787 srcY0
>= 0 && srcY1
<= readFb
->Height
&&
788 dstX0
>= 0 && dstX1
<= drawFb
->Width
&&
789 dstY0
>= 0 && dstY1
<= drawFb
->Height
&&
790 !(ctx
->Scissor
.EnableFlags
))) {
791 perf_debug("glBlitFramebuffer(): non-1:1 blit. "
792 "Falling back to software rendering.\n");
796 /* Blit to all active draw buffers. We don't do any pre-checking,
797 * because we assume that copying to MRTs is rare, and failure midway
798 * through copying is even more rare. Even if it was to occur, it's
799 * safe to let meta start the copy over from scratch, because
800 * glBlitFramebuffer completely overwrites the destination pixels, and
801 * results are undefined if any destination pixels have a dependency on
804 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
805 struct gl_renderbuffer
*dst_rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[i
];
806 struct intel_renderbuffer
*dst_irb
= intel_renderbuffer(dst_rb
);
809 perf_debug("glBlitFramebuffer(): missing dst renderbuffer. "
810 "Falling back to software rendering.\n");
814 if (!intel_miptree_blit(brw
,
816 src_irb
->mt_level
, src_irb
->mt_layer
,
817 srcX0
, srcY0
, src_rb
->Name
== 0,
819 dst_irb
->mt_level
, dst_irb
->mt_layer
,
820 dstX0
, dstY0
, dst_rb
->Name
== 0,
821 dstX1
- dstX0
, dstY1
- dstY0
, GL_COPY
)) {
822 perf_debug("glBlitFramebuffer(): unknown blit failure. "
823 "Falling back to software rendering.\n");
828 mask
&= ~GL_COLOR_BUFFER_BIT
;
835 intel_blit_framebuffer(struct gl_context
*ctx
,
836 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
837 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
838 GLbitfield mask
, GLenum filter
)
840 mask
= brw_blorp_framebuffer(brw_context(ctx
),
841 srcX0
, srcY0
, srcX1
, srcY1
,
842 dstX0
, dstY0
, dstX1
, dstY1
,
847 /* Try using the BLT engine. */
848 mask
= intel_blit_framebuffer_with_blitter(ctx
,
849 srcX0
, srcY0
, srcX1
, srcY1
,
850 dstX0
, dstY0
, dstX1
, dstY1
,
856 _mesa_meta_BlitFramebuffer(ctx
,
857 srcX0
, srcY0
, srcX1
, srcY1
,
858 dstX0
, dstY0
, dstX1
, dstY1
,
863 * Does the renderbuffer have hiz enabled?
866 intel_renderbuffer_has_hiz(struct intel_renderbuffer
*irb
)
868 return intel_miptree_slice_has_hiz(irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
872 intel_renderbuffer_resolve_hiz(struct brw_context
*brw
,
873 struct intel_renderbuffer
*irb
)
876 return intel_miptree_slice_resolve_hiz(brw
,
885 intel_renderbuffer_att_set_needs_depth_resolve(struct gl_renderbuffer_attachment
*att
)
887 struct intel_renderbuffer
*irb
= intel_renderbuffer(att
->Renderbuffer
);
890 intel_miptree_set_all_slices_need_depth_resolve(irb
->mt
, irb
->mt_level
);
892 intel_miptree_slice_set_needs_depth_resolve(irb
->mt
,
900 intel_renderbuffer_resolve_depth(struct brw_context
*brw
,
901 struct intel_renderbuffer
*irb
)
904 return intel_miptree_slice_resolve_depth(brw
,
913 intel_renderbuffer_move_to_temp(struct brw_context
*brw
,
914 struct intel_renderbuffer
*irb
,
917 struct gl_renderbuffer
*rb
=&irb
->Base
.Base
;
918 struct intel_texture_image
*intel_image
= intel_texture_image(rb
->TexImage
);
919 struct intel_mipmap_tree
*new_mt
;
920 int width
, height
, depth
;
922 intel_miptree_get_dimensions_for_image(rb
->TexImage
, &width
, &height
, &depth
);
924 new_mt
= intel_miptree_create(brw
, rb
->TexImage
->TexObject
->Target
,
925 intel_image
->base
.Base
.TexFormat
,
926 intel_image
->base
.Base
.Level
,
927 intel_image
->base
.Base
.Level
,
928 width
, height
, depth
,
930 irb
->mt
->num_samples
,
931 INTEL_MIPTREE_TILING_ANY
);
933 if (brw_is_hiz_depth_format(brw
, new_mt
->format
)) {
934 intel_miptree_alloc_hiz(brw
, new_mt
);
937 intel_miptree_copy_teximage(brw
, intel_image
, new_mt
, invalidate
);
939 intel_miptree_reference(&irb
->mt
, intel_image
->mt
);
940 intel_renderbuffer_set_draw_offset(irb
);
941 intel_miptree_release(&new_mt
);
945 brw_render_cache_set_clear(struct brw_context
*brw
)
947 struct set_entry
*entry
;
949 set_foreach(brw
->render_cache
, entry
) {
950 _mesa_set_remove(brw
->render_cache
, entry
);
955 brw_render_cache_set_add_bo(struct brw_context
*brw
, drm_intel_bo
*bo
)
957 _mesa_set_add(brw
->render_cache
, _mesa_hash_pointer(bo
), bo
);
961 * Emits an appropriate flush for a BO if it has been rendered to within the
962 * same batchbuffer as a read that's about to be emitted.
964 * The GPU has separate, incoherent caches for the render cache and the
965 * sampler cache, along with other caches. Usually data in the different
966 * caches don't interact (e.g. we don't render to our driver-generated
967 * immediate constant data), but for render-to-texture in FBOs we definitely
968 * do. When a batchbuffer is flushed, the kernel will ensure that everything
969 * necessary is flushed before another use of that BO, but for reuse from
970 * different caches within a batchbuffer, it's all our responsibility.
973 brw_render_cache_set_check_flush(struct brw_context
*brw
, drm_intel_bo
*bo
)
975 if (!_mesa_set_search(brw
->render_cache
, _mesa_hash_pointer(bo
), bo
))
978 intel_batchbuffer_emit_mi_flush(brw
);
982 * Do one-time context initializations related to GL_EXT_framebuffer_object.
983 * Hook in device driver functions.
986 intel_fbo_init(struct brw_context
*brw
)
988 struct dd_function_table
*dd
= &brw
->ctx
.Driver
;
989 dd
->NewFramebuffer
= intel_new_framebuffer
;
990 dd
->NewRenderbuffer
= intel_new_renderbuffer
;
991 dd
->MapRenderbuffer
= intel_map_renderbuffer
;
992 dd
->UnmapRenderbuffer
= intel_unmap_renderbuffer
;
993 dd
->RenderTexture
= intel_render_texture
;
994 dd
->ValidateFramebuffer
= intel_validate_framebuffer
;
995 dd
->BlitFramebuffer
= intel_blit_framebuffer
;
996 dd
->EGLImageTargetRenderbufferStorage
=
997 intel_image_target_renderbuffer_storage
;
999 brw
->render_cache
= _mesa_set_create(brw
, _mesa_key_pointer_equal
);