2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
29 #include "main/glformats.h"
31 #include "brw_blorp.h"
32 #include "brw_context.h"
33 #include "brw_defines.h"
34 #include "brw_meta_util.h"
35 #include "brw_state.h"
36 #include "intel_fbo.h"
37 #include "intel_debug.h"
39 #define FILE_DEBUG_FLAG DEBUG_BLORP
42 brw_blorp_lookup_shader(struct blorp_context
*blorp
,
43 const void *key
, uint32_t key_size
,
44 uint32_t *kernel_out
, void *prog_data_out
)
46 struct brw_context
*brw
= blorp
->driver_ctx
;
47 return brw_search_cache(&brw
->cache
, BRW_CACHE_BLORP_PROG
,
48 key
, key_size
, kernel_out
, prog_data_out
);
52 brw_blorp_upload_shader(struct blorp_context
*blorp
,
53 const void *key
, uint32_t key_size
,
54 const void *kernel
, uint32_t kernel_size
,
55 const struct brw_stage_prog_data
*prog_data
,
56 uint32_t prog_data_size
,
57 uint32_t *kernel_out
, void *prog_data_out
)
59 struct brw_context
*brw
= blorp
->driver_ctx
;
60 brw_upload_cache(&brw
->cache
, BRW_CACHE_BLORP_PROG
, key
, key_size
,
61 kernel
, kernel_size
, prog_data
, prog_data_size
,
62 kernel_out
, prog_data_out
);
66 brw_blorp_init(struct brw_context
*brw
)
68 blorp_init(&brw
->blorp
, brw
, &brw
->isl_dev
);
70 brw
->blorp
.compiler
= brw
->screen
->compiler
;
74 brw
->blorp
.mocs
.tex
= 0;
75 brw
->blorp
.mocs
.rb
= 0;
76 brw
->blorp
.mocs
.vb
= 0;
77 brw
->blorp
.exec
= gen6_blorp_exec
;
80 brw
->blorp
.mocs
.tex
= GEN7_MOCS_L3
;
81 brw
->blorp
.mocs
.rb
= GEN7_MOCS_L3
;
82 brw
->blorp
.mocs
.vb
= GEN7_MOCS_L3
;
83 if (brw
->is_haswell
) {
84 brw
->blorp
.exec
= gen75_blorp_exec
;
86 brw
->blorp
.exec
= gen7_blorp_exec
;
90 brw
->blorp
.mocs
.tex
= BDW_MOCS_WB
;
91 brw
->blorp
.mocs
.rb
= BDW_MOCS_PTE
;
92 brw
->blorp
.mocs
.vb
= BDW_MOCS_WB
;
93 brw
->blorp
.exec
= gen8_blorp_exec
;
96 brw
->blorp
.mocs
.tex
= SKL_MOCS_WB
;
97 brw
->blorp
.mocs
.rb
= SKL_MOCS_PTE
;
98 brw
->blorp
.mocs
.vb
= SKL_MOCS_WB
;
99 brw
->blorp
.exec
= gen9_blorp_exec
;
102 unreachable("Invalid gen");
105 brw
->blorp
.lookup_shader
= brw_blorp_lookup_shader
;
106 brw
->blorp
.upload_shader
= brw_blorp_upload_shader
;
110 apply_gen6_stencil_hiz_offset(struct isl_surf
*surf
,
111 struct intel_mipmap_tree
*mt
,
115 assert(mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
);
117 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
118 /* Note: we can't compute the stencil offset using
119 * intel_miptree_get_aligned_offset(), because the miptree
120 * claims that the region is untiled even though it's W tiled.
122 *offset
= mt
->level
[lod
].level_y
* mt
->pitch
+
123 mt
->level
[lod
].level_x
* 64;
125 *offset
= intel_miptree_get_aligned_offset(mt
,
126 mt
->level
[lod
].level_x
,
127 mt
->level
[lod
].level_y
);
130 surf
->logical_level0_px
.width
= minify(surf
->logical_level0_px
.width
, lod
);
131 surf
->logical_level0_px
.height
= minify(surf
->logical_level0_px
.height
, lod
);
132 surf
->phys_level0_sa
.width
= minify(surf
->phys_level0_sa
.width
, lod
);
133 surf
->phys_level0_sa
.height
= minify(surf
->phys_level0_sa
.height
, lod
);
135 surf
->array_pitch_el_rows
=
136 ALIGN(surf
->phys_level0_sa
.height
, surf
->image_alignment_el
.height
);
140 blorp_surf_for_miptree(struct brw_context
*brw
,
141 struct blorp_surf
*surf
,
142 struct intel_mipmap_tree
*mt
,
143 bool is_render_target
,
144 uint32_t safe_aux_usage
,
146 unsigned start_layer
, unsigned num_layers
,
147 struct isl_surf tmp_surfs
[2])
149 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_UMS
||
150 mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
151 const unsigned num_samples
= MAX2(1, mt
->num_samples
);
152 for (unsigned i
= 0; i
< num_layers
; i
++) {
153 for (unsigned s
= 0; s
< num_samples
; s
++) {
154 const unsigned phys_layer
= (start_layer
+ i
) * num_samples
+ s
;
155 intel_miptree_check_level_layer(mt
, *level
, phys_layer
);
159 for (unsigned i
= 0; i
< num_layers
; i
++)
160 intel_miptree_check_level_layer(mt
, *level
, start_layer
+ i
);
163 intel_miptree_get_isl_surf(brw
, mt
, &tmp_surfs
[0]);
164 surf
->surf
= &tmp_surfs
[0];
165 surf
->addr
= (struct blorp_address
) {
167 .offset
= mt
->offset
,
168 .read_domains
= is_render_target
? I915_GEM_DOMAIN_RENDER
:
169 I915_GEM_DOMAIN_SAMPLER
,
170 .write_domain
= is_render_target
? I915_GEM_DOMAIN_RENDER
: 0,
173 if (brw
->gen
== 6 && mt
->format
== MESA_FORMAT_S_UINT8
&&
174 mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
) {
175 /* Sandy bridge stencil and HiZ use this ALL_SLICES_AT_EACH_LOD hack in
176 * order to allow for layered rendering. The hack makes each LOD of the
177 * stencil or HiZ buffer a single tightly packed array surface at some
178 * offset into the surface. Since ISL doesn't know how to deal with the
179 * crazy ALL_SLICES_AT_EACH_LOD layout and since we have to do a manual
180 * offset of it anyway, we might as well do the offset here and keep the
181 * hacks inside the i965 driver.
183 * See also gen6_depth_stencil_state.c
186 apply_gen6_stencil_hiz_offset(&tmp_surfs
[0], mt
, *level
, &offset
);
187 surf
->addr
.offset
+= offset
;
191 struct isl_surf
*aux_surf
= &tmp_surfs
[1];
192 intel_miptree_get_aux_isl_surf(brw
, mt
, aux_surf
, &surf
->aux_usage
);
194 if (surf
->aux_usage
!= ISL_AUX_USAGE_NONE
) {
195 if (surf
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
196 /* If we're not going to use it as a depth buffer, resolve HiZ */
197 if (!(safe_aux_usage
& (1 << ISL_AUX_USAGE_HIZ
))) {
198 for (unsigned i
= 0; i
< num_layers
; i
++) {
199 intel_miptree_slice_resolve_depth(brw
, mt
, *level
,
202 /* If we're rendering to it then we'll need a HiZ resolve once
203 * we're done before we can use it with HiZ again.
205 if (is_render_target
)
206 intel_miptree_slice_set_needs_hiz_resolve(mt
, *level
,
209 surf
->aux_usage
= ISL_AUX_USAGE_NONE
;
211 } else if (!(safe_aux_usage
& (1 << surf
->aux_usage
))) {
213 if (safe_aux_usage
& (1 << ISL_AUX_USAGE_CCS_E
))
214 flags
|= INTEL_MIPTREE_IGNORE_CCS_E
;
216 intel_miptree_resolve_color(brw
, mt
,
217 *level
, start_layer
, num_layers
, flags
);
219 assert(!intel_miptree_has_color_unresolved(mt
, *level
, 1,
220 start_layer
, num_layers
));
221 surf
->aux_usage
= ISL_AUX_USAGE_NONE
;
225 if (is_render_target
)
226 intel_miptree_used_for_rendering(brw
, mt
, *level
,
227 start_layer
, num_layers
);
229 if (surf
->aux_usage
!= ISL_AUX_USAGE_NONE
) {
230 /* We only really need a clear color if we also have an auxiliary
231 * surface. Without one, it does nothing.
233 surf
->clear_color
= intel_miptree_get_isl_clear_color(brw
, mt
);
235 surf
->aux_surf
= aux_surf
;
236 surf
->aux_addr
= (struct blorp_address
) {
237 .read_domains
= is_render_target
? I915_GEM_DOMAIN_RENDER
:
238 I915_GEM_DOMAIN_SAMPLER
,
239 .write_domain
= is_render_target
? I915_GEM_DOMAIN_RENDER
: 0,
243 surf
->aux_addr
.buffer
= mt
->mcs_buf
->bo
;
244 surf
->aux_addr
.offset
= mt
->mcs_buf
->offset
;
246 assert(surf
->aux_usage
== ISL_AUX_USAGE_HIZ
);
248 surf
->aux_addr
.buffer
= mt
->hiz_buf
->aux_base
.bo
;
249 surf
->aux_addr
.offset
= mt
->hiz_buf
->aux_base
.offset
;
251 struct intel_mipmap_tree
*hiz_mt
= mt
->hiz_buf
->mt
;
253 assert(brw
->gen
== 6 &&
254 hiz_mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
);
256 /* gen6 requires the HiZ buffer to be manually offset to the
257 * right location. We could fixup the surf but it doesn't
258 * matter since most of those fields don't matter.
260 apply_gen6_stencil_hiz_offset(aux_surf
, hiz_mt
, *level
,
261 &surf
->aux_addr
.offset
);
262 assert(hiz_mt
->pitch
== aux_surf
->row_pitch
);
266 surf
->aux_addr
= (struct blorp_address
) {
269 memset(&surf
->clear_color
, 0, sizeof(surf
->clear_color
));
271 assert((surf
->aux_usage
== ISL_AUX_USAGE_NONE
) ==
272 (surf
->aux_addr
.buffer
== NULL
));
275 static enum isl_format
276 brw_blorp_to_isl_format(struct brw_context
*brw
, mesa_format format
,
277 bool is_render_target
)
280 case MESA_FORMAT_NONE
:
281 return ISL_FORMAT_UNSUPPORTED
;
282 case MESA_FORMAT_S_UINT8
:
283 return ISL_FORMAT_R8_UINT
;
284 case MESA_FORMAT_Z24_UNORM_X8_UINT
:
285 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
286 return ISL_FORMAT_R24_UNORM_X8_TYPELESS
;
287 case MESA_FORMAT_Z_FLOAT32
:
288 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
289 return ISL_FORMAT_R32_FLOAT
;
290 case MESA_FORMAT_Z_UNORM16
:
291 return ISL_FORMAT_R16_UNORM
;
293 if (is_render_target
) {
294 assert(brw
->format_supported_as_render_target
[format
]);
295 return brw
->render_target_format
[format
];
297 return brw_format_for_mesa_format(format
);
305 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
306 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
308 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
311 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
313 * which is simply adding 4 then modding by 8 (or anding with 7).
315 * We then may need to apply workarounds for textureGather hardware bugs.
317 static enum isl_channel_select
318 swizzle_to_scs(GLenum swizzle
)
320 return (enum isl_channel_select
)((swizzle
+ 4) & 7);
324 physical_to_logical_layer(struct intel_mipmap_tree
*mt
,
325 unsigned physical_layer
)
327 if (mt
->num_samples
> 1 &&
328 (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_UMS
||
329 mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
)) {
330 assert(physical_layer
% mt
->num_samples
== 0);
331 return physical_layer
/ mt
->num_samples
;
333 return physical_layer
;
338 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
339 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
340 * the physical layer holding sample 0. So, for example, if
341 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
345 brw_blorp_blit_miptrees(struct brw_context
*brw
,
346 struct intel_mipmap_tree
*src_mt
,
347 unsigned src_level
, unsigned src_layer
,
348 mesa_format src_format
, int src_swizzle
,
349 struct intel_mipmap_tree
*dst_mt
,
350 unsigned dst_level
, unsigned dst_layer
,
351 mesa_format dst_format
,
352 float src_x0
, float src_y0
,
353 float src_x1
, float src_y1
,
354 float dst_x0
, float dst_y0
,
355 float dst_x1
, float dst_y1
,
356 GLenum filter
, bool mirror_x
, bool mirror_y
,
357 bool decode_srgb
, bool encode_srgb
)
359 /* Blorp operates in logical layers */
360 src_layer
= physical_to_logical_layer(src_mt
, src_layer
);
361 dst_layer
= physical_to_logical_layer(dst_mt
, dst_layer
);
363 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
364 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
366 src_mt
->num_samples
, _mesa_get_format_name(src_mt
->format
), src_mt
,
367 src_level
, src_layer
, src_x0
, src_y0
, src_x1
, src_y1
,
368 dst_mt
->num_samples
, _mesa_get_format_name(dst_mt
->format
), dst_mt
,
369 dst_level
, dst_layer
, dst_x0
, dst_y0
, dst_x1
, dst_y1
,
372 if (!decode_srgb
&& _mesa_get_format_color_encoding(src_format
) == GL_SRGB
)
373 src_format
= _mesa_get_srgb_format_linear(src_format
);
375 if (!encode_srgb
&& _mesa_get_format_color_encoding(dst_format
) == GL_SRGB
)
376 dst_format
= _mesa_get_srgb_format_linear(dst_format
);
378 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
379 * texture, the above code configures the source format for L32_FLOAT or
380 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
381 * the SAMPLE message appears to handle multisampled L32_FLOAT and
382 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
383 * around the problem by using a source format of R32_FLOAT. This
384 * shouldn't affect rendering correctness, since the destination format is
385 * R32_FLOAT, so only the contents of the red channel matters.
388 src_mt
->num_samples
> 1 && dst_mt
->num_samples
<= 1 &&
389 src_mt
->format
== dst_mt
->format
&&
390 (dst_format
== MESA_FORMAT_L_FLOAT32
||
391 dst_format
== MESA_FORMAT_I_FLOAT32
)) {
392 src_format
= dst_format
= MESA_FORMAT_R_FLOAT32
;
395 uint32_t src_usage_flags
= (1 << ISL_AUX_USAGE_MCS
);
396 if (src_format
== src_mt
->format
)
397 src_usage_flags
|= (1 << ISL_AUX_USAGE_CCS_E
);
399 uint32_t dst_usage_flags
= (1 << ISL_AUX_USAGE_MCS
);
400 if (dst_format
== dst_mt
->format
) {
401 dst_usage_flags
|= (1 << ISL_AUX_USAGE_CCS_E
) |
402 (1 << ISL_AUX_USAGE_CCS_D
);
405 struct isl_surf tmp_surfs
[4];
406 struct blorp_surf src_surf
, dst_surf
;
407 blorp_surf_for_miptree(brw
, &src_surf
, src_mt
, false, src_usage_flags
,
408 &src_level
, src_layer
, 1, &tmp_surfs
[0]);
409 blorp_surf_for_miptree(brw
, &dst_surf
, dst_mt
, true, dst_usage_flags
,
410 &dst_level
, dst_layer
, 1, &tmp_surfs
[2]);
412 struct isl_swizzle src_isl_swizzle
= {
413 .r
= swizzle_to_scs(GET_SWZ(src_swizzle
, 0)),
414 .g
= swizzle_to_scs(GET_SWZ(src_swizzle
, 1)),
415 .b
= swizzle_to_scs(GET_SWZ(src_swizzle
, 2)),
416 .a
= swizzle_to_scs(GET_SWZ(src_swizzle
, 3)),
419 struct blorp_batch batch
;
420 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
421 blorp_blit(&batch
, &src_surf
, src_level
, src_layer
,
422 brw_blorp_to_isl_format(brw
, src_format
, false), src_isl_swizzle
,
423 &dst_surf
, dst_level
, dst_layer
,
424 brw_blorp_to_isl_format(brw
, dst_format
, true),
425 ISL_SWIZZLE_IDENTITY
,
426 src_x0
, src_y0
, src_x1
, src_y1
,
427 dst_x0
, dst_y0
, dst_x1
, dst_y1
,
428 filter
, mirror_x
, mirror_y
);
429 blorp_batch_finish(&batch
);
433 brw_blorp_copy_miptrees(struct brw_context
*brw
,
434 struct intel_mipmap_tree
*src_mt
,
435 unsigned src_level
, unsigned src_layer
,
436 struct intel_mipmap_tree
*dst_mt
,
437 unsigned dst_level
, unsigned dst_layer
,
438 unsigned src_x
, unsigned src_y
,
439 unsigned dst_x
, unsigned dst_y
,
440 unsigned src_width
, unsigned src_height
)
442 DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
443 "to %dx %s mt %p %d %d (%d,%d)\n",
445 src_mt
->num_samples
, _mesa_get_format_name(src_mt
->format
), src_mt
,
446 src_level
, src_layer
, src_x
, src_y
, src_width
, src_height
,
447 dst_mt
->num_samples
, _mesa_get_format_name(dst_mt
->format
), dst_mt
,
448 dst_level
, dst_layer
, dst_x
, dst_y
);
450 struct isl_surf tmp_surfs
[4];
451 struct blorp_surf src_surf
, dst_surf
;
452 blorp_surf_for_miptree(brw
, &src_surf
, src_mt
, false,
453 (1 << ISL_AUX_USAGE_MCS
) |
454 (1 << ISL_AUX_USAGE_CCS_E
),
455 &src_level
, src_layer
, 1, &tmp_surfs
[0]);
456 blorp_surf_for_miptree(brw
, &dst_surf
, dst_mt
, true,
457 (1 << ISL_AUX_USAGE_MCS
) |
458 (1 << ISL_AUX_USAGE_CCS_E
),
459 &dst_level
, dst_layer
, 1, &tmp_surfs
[2]);
461 struct blorp_batch batch
;
462 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
463 blorp_copy(&batch
, &src_surf
, src_level
, src_layer
,
464 &dst_surf
, dst_level
, dst_layer
,
465 src_x
, src_y
, dst_x
, dst_y
, src_width
, src_height
);
466 blorp_batch_finish(&batch
);
469 static struct intel_mipmap_tree
*
470 find_miptree(GLbitfield buffer_bit
, struct intel_renderbuffer
*irb
)
472 struct intel_mipmap_tree
*mt
= irb
->mt
;
473 if (buffer_bit
== GL_STENCIL_BUFFER_BIT
&& mt
->stencil_mt
)
479 blorp_get_texture_swizzle(const struct intel_renderbuffer
*irb
)
481 return irb
->Base
.Base
._BaseFormat
== GL_RGB
?
482 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
) :
487 do_blorp_blit(struct brw_context
*brw
, GLbitfield buffer_bit
,
488 struct intel_renderbuffer
*src_irb
, mesa_format src_format
,
489 struct intel_renderbuffer
*dst_irb
, mesa_format dst_format
,
490 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
491 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
492 GLenum filter
, bool mirror_x
, bool mirror_y
)
494 const struct gl_context
*ctx
= &brw
->ctx
;
496 /* Find source/dst miptrees */
497 struct intel_mipmap_tree
*src_mt
= find_miptree(buffer_bit
, src_irb
);
498 struct intel_mipmap_tree
*dst_mt
= find_miptree(buffer_bit
, dst_irb
);
500 const bool do_srgb
= ctx
->Color
.sRGBEnabled
;
503 brw_blorp_blit_miptrees(brw
,
504 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
505 src_format
, blorp_get_texture_swizzle(src_irb
),
506 dst_mt
, dst_irb
->mt_level
, dst_irb
->mt_layer
,
508 srcX0
, srcY0
, srcX1
, srcY1
,
509 dstX0
, dstY0
, dstX1
, dstY1
,
510 filter
, mirror_x
, mirror_y
,
513 dst_irb
->need_downsample
= true;
517 try_blorp_blit(struct brw_context
*brw
,
518 const struct gl_framebuffer
*read_fb
,
519 const struct gl_framebuffer
*draw_fb
,
520 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
521 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
522 GLenum filter
, GLbitfield buffer_bit
)
524 struct gl_context
*ctx
= &brw
->ctx
;
526 /* Sync up the state of window system buffers. We need to do this before
527 * we go looking for the buffers.
529 intel_prepare_render(brw
);
531 bool mirror_x
, mirror_y
;
532 if (brw_meta_mirror_clip_and_scissor(ctx
, read_fb
, draw_fb
,
533 &srcX0
, &srcY0
, &srcX1
, &srcY1
,
534 &dstX0
, &dstY0
, &dstX1
, &dstY1
,
535 &mirror_x
, &mirror_y
))
539 struct intel_renderbuffer
*src_irb
;
540 struct intel_renderbuffer
*dst_irb
;
541 struct intel_mipmap_tree
*src_mt
;
542 struct intel_mipmap_tree
*dst_mt
;
543 switch (buffer_bit
) {
544 case GL_COLOR_BUFFER_BIT
:
545 src_irb
= intel_renderbuffer(read_fb
->_ColorReadBuffer
);
546 for (unsigned i
= 0; i
< draw_fb
->_NumColorDrawBuffers
; ++i
) {
547 dst_irb
= intel_renderbuffer(draw_fb
->_ColorDrawBuffers
[i
]);
549 do_blorp_blit(brw
, buffer_bit
,
550 src_irb
, src_irb
->Base
.Base
.Format
,
551 dst_irb
, dst_irb
->Base
.Base
.Format
,
552 srcX0
, srcY0
, srcX1
, srcY1
,
553 dstX0
, dstY0
, dstX1
, dstY1
,
554 filter
, mirror_x
, mirror_y
);
557 case GL_DEPTH_BUFFER_BIT
:
559 intel_renderbuffer(read_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
561 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
562 src_mt
= find_miptree(buffer_bit
, src_irb
);
563 dst_mt
= find_miptree(buffer_bit
, dst_irb
);
565 /* We can't handle format conversions between Z24 and other formats
566 * since we have to lie about the surface format. See the comments in
567 * brw_blorp_surface_info::set().
569 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
570 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
))
573 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
574 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
575 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
576 filter
, mirror_x
, mirror_y
);
578 case GL_STENCIL_BUFFER_BIT
:
580 intel_renderbuffer(read_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
582 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
583 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
584 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
585 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
586 filter
, mirror_x
, mirror_y
);
589 unreachable("not reached");
596 brw_blorp_copytexsubimage(struct brw_context
*brw
,
597 struct gl_renderbuffer
*src_rb
,
598 struct gl_texture_image
*dst_image
,
600 int srcX0
, int srcY0
,
601 int dstX0
, int dstY0
,
602 int width
, int height
)
604 struct gl_context
*ctx
= &brw
->ctx
;
605 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
606 struct intel_texture_image
*intel_image
= intel_texture_image(dst_image
);
608 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
609 if (brw
->ctx
._ImageTransferState
)
612 /* Sync up the state of window system buffers. We need to do this before
613 * we go looking at the src renderbuffer's miptree.
615 intel_prepare_render(brw
);
617 struct intel_mipmap_tree
*src_mt
= src_irb
->mt
;
618 struct intel_mipmap_tree
*dst_mt
= intel_image
->mt
;
620 /* There is support for only up to eight samples. */
621 if (src_mt
->num_samples
> 8 || dst_mt
->num_samples
> 8)
624 /* BLORP is only supported from Gen6 onwards. */
628 if (_mesa_get_format_base_format(src_rb
->Format
) !=
629 _mesa_get_format_base_format(dst_image
->TexFormat
)) {
633 /* We can't handle format conversions between Z24 and other formats since
634 * we have to lie about the surface format. See the comments in
635 * brw_blorp_surface_info::set().
637 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
638 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
)) {
642 if (!brw
->format_supported_as_render_target
[dst_image
->TexFormat
])
645 /* Source clipping shouldn't be necessary, since copytexsubimage (in
646 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
649 * Destination clipping shouldn't be necessary since the restrictions on
650 * glCopyTexSubImage prevent the user from specifying a destination rectangle
651 * that falls outside the bounds of the destination texture.
652 * See error_check_subtexture_dimensions().
655 int srcY1
= srcY0
+ height
;
656 int srcX1
= srcX0
+ width
;
657 int dstX1
= dstX0
+ width
;
658 int dstY1
= dstY0
+ height
;
660 /* Account for the fact that in the system framebuffer, the origin is at
663 bool mirror_y
= false;
664 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
)) {
665 GLint tmp
= src_rb
->Height
- srcY0
;
666 srcY0
= src_rb
->Height
- srcY1
;
671 /* Account for face selection and texture view MinLayer */
672 int dst_slice
= slice
+ dst_image
->TexObject
->MinLayer
+ dst_image
->Face
;
673 int dst_level
= dst_image
->Level
+ dst_image
->TexObject
->MinLevel
;
675 brw_blorp_blit_miptrees(brw
,
676 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
677 src_rb
->Format
, blorp_get_texture_swizzle(src_irb
),
678 dst_mt
, dst_level
, dst_slice
,
679 dst_image
->TexFormat
,
680 srcX0
, srcY0
, srcX1
, srcY1
,
681 dstX0
, dstY0
, dstX1
, dstY1
,
682 GL_NEAREST
, false, mirror_y
,
685 /* If we're copying to a packed depth stencil texture and the source
686 * framebuffer has separate stencil, we need to also copy the stencil data
689 src_rb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
690 if (_mesa_get_format_bits(dst_image
->TexFormat
, GL_STENCIL_BITS
) > 0 &&
692 src_irb
= intel_renderbuffer(src_rb
);
693 src_mt
= src_irb
->mt
;
695 if (src_mt
->stencil_mt
)
696 src_mt
= src_mt
->stencil_mt
;
697 if (dst_mt
->stencil_mt
)
698 dst_mt
= dst_mt
->stencil_mt
;
700 if (src_mt
!= dst_mt
) {
701 brw_blorp_blit_miptrees(brw
,
702 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
704 blorp_get_texture_swizzle(src_irb
),
705 dst_mt
, dst_level
, dst_slice
,
707 srcX0
, srcY0
, srcX1
, srcY1
,
708 dstX0
, dstY0
, dstX1
, dstY1
,
709 GL_NEAREST
, false, mirror_y
,
719 brw_blorp_framebuffer(struct brw_context
*brw
,
720 struct gl_framebuffer
*readFb
,
721 struct gl_framebuffer
*drawFb
,
722 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
723 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
724 GLbitfield mask
, GLenum filter
)
726 /* BLORP is not supported before Gen6. */
730 static GLbitfield buffer_bits
[] = {
733 GL_STENCIL_BUFFER_BIT
,
736 for (unsigned int i
= 0; i
< ARRAY_SIZE(buffer_bits
); ++i
) {
737 if ((mask
& buffer_bits
[i
]) &&
738 try_blorp_blit(brw
, readFb
, drawFb
,
739 srcX0
, srcY0
, srcX1
, srcY1
,
740 dstX0
, dstY0
, dstX1
, dstY1
,
741 filter
, buffer_bits
[i
])) {
742 mask
&= ~buffer_bits
[i
];
750 set_write_disables(const struct intel_renderbuffer
*irb
,
751 const GLubyte
*color_mask
, bool *color_write_disable
)
753 /* Format information in the renderbuffer represents the requirements
754 * given by the client. There are cases where the backing miptree uses,
755 * for example, RGBA to represent RGBX. Since the client is only expecting
756 * RGB we can treat alpha as not used and write whatever we like into it.
758 const GLenum base_format
= irb
->Base
.Base
._BaseFormat
;
759 const int components
= _mesa_base_format_component_count(base_format
);
760 bool disables
= false;
762 assert(components
> 0);
764 for (int i
= 0; i
< components
; i
++) {
765 color_write_disable
[i
] = !color_mask
[i
];
766 disables
= disables
|| !color_mask
[i
];
773 irb_logical_mt_layer(struct intel_renderbuffer
*irb
)
775 return physical_to_logical_layer(irb
->mt
, irb
->mt_layer
);
779 do_single_blorp_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
780 struct gl_renderbuffer
*rb
, unsigned buf
,
781 bool partial_clear
, bool encode_srgb
)
783 struct gl_context
*ctx
= &brw
->ctx
;
784 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
785 mesa_format format
= irb
->mt
->format
;
786 uint32_t x0
, x1
, y0
, y1
;
788 if (!encode_srgb
&& _mesa_get_format_color_encoding(format
) == GL_SRGB
)
789 format
= _mesa_get_srgb_format_linear(format
);
797 y0
= rb
->Height
- fb
->_Ymax
;
798 y1
= rb
->Height
- fb
->_Ymin
;
801 /* If the clear region is empty, just return. */
802 if (x0
== x1
|| y0
== y1
)
805 bool can_fast_clear
= !partial_clear
;
807 bool color_write_disable
[4] = { false, false, false, false };
808 if (set_write_disables(irb
, ctx
->Color
.ColorMask
[buf
], color_write_disable
))
809 can_fast_clear
= false;
811 if (irb
->mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
||
812 !brw_is_color_fast_clear_compatible(brw
, irb
->mt
, &ctx
->Color
.ClearColor
))
813 can_fast_clear
= false;
815 const unsigned logical_layer
= irb_logical_mt_layer(irb
);
816 const enum intel_fast_clear_state fast_clear_state
=
817 intel_miptree_get_fast_clear_state(irb
->mt
, irb
->mt_level
,
820 /* Surface state can only record one fast clear color value. Therefore
821 * unless different levels/layers agree on the color it can be used to
822 * represent only single level/layer. Here it will be reserved for the
823 * first slice (level 0, layer 0).
825 if (irb
->layer_count
> 1 || irb
->mt_level
|| irb
->mt_layer
)
826 can_fast_clear
= false;
828 if (can_fast_clear
) {
829 union gl_color_union override_color
=
830 brw_meta_convert_fast_clear_color(brw
, irb
->mt
,
831 &ctx
->Color
.ClearColor
);
833 /* Record the clear color in the miptree so that it will be
834 * programmed in SURFACE_STATE by later rendering and resolve
837 const bool color_updated
= brw_meta_set_fast_clear_color(
838 brw
, &irb
->mt
->gen9_fast_clear_color
,
841 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
842 * is redundant and can be skipped.
844 if (!color_updated
&& fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
847 /* If the MCS buffer hasn't been allocated yet, we need to allocate
850 if (!irb
->mt
->mcs_buf
) {
851 assert(!intel_miptree_is_lossless_compressed(brw
, irb
->mt
));
852 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
, false)) {
853 /* MCS allocation failed--probably this will only happen in
854 * out-of-memory conditions. But in any case, try to recover
855 * by falling back to a non-blorp clear technique.
862 const unsigned num_layers
= fb
->MaxNumLayers
? irb
->layer_count
: 1;
864 /* We can't setup the blorp_surf until we've allocated the MCS above */
865 struct isl_surf isl_tmp
[2];
866 struct blorp_surf surf
;
867 unsigned level
= irb
->mt_level
;
868 blorp_surf_for_miptree(brw
, &surf
, irb
->mt
, true,
869 (1 << ISL_AUX_USAGE_MCS
) |
870 (1 << ISL_AUX_USAGE_CCS_E
) |
871 (1 << ISL_AUX_USAGE_CCS_D
),
872 &level
, logical_layer
, num_layers
, isl_tmp
);
874 if (can_fast_clear
) {
875 DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__
,
876 irb
->mt
, irb
->mt_level
, irb
->mt_layer
, num_layers
);
878 struct blorp_batch batch
;
879 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
880 blorp_fast_clear(&batch
, &surf
,
881 (enum isl_format
)brw
->render_target_format
[format
],
882 level
, logical_layer
, num_layers
,
884 blorp_batch_finish(&batch
);
886 /* Now that the fast clear has occurred, put the buffer in
887 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
890 intel_miptree_set_fast_clear_state(brw
, irb
->mt
, irb
->mt_level
,
891 logical_layer
, num_layers
,
892 INTEL_FAST_CLEAR_STATE_CLEAR
);
894 DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__
,
895 irb
->mt
, irb
->mt_level
, irb
->mt_layer
, num_layers
);
897 union isl_color_value clear_color
;
898 memcpy(clear_color
.f32
, ctx
->Color
.ClearColor
.f
, sizeof(float) * 4);
900 struct blorp_batch batch
;
901 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
902 blorp_clear(&batch
, &surf
,
903 (enum isl_format
)brw
->render_target_format
[format
],
904 ISL_SWIZZLE_IDENTITY
,
905 level
, irb_logical_mt_layer(irb
), num_layers
,
907 clear_color
, color_write_disable
);
908 blorp_batch_finish(&batch
);
912 * Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
914 * Any transition from any value in {Clear, Render, Resolve} to a
915 * different value in {Clear, Render, Resolve} requires end of pipe
918 brw_emit_pipe_control_flush(brw
,
919 PIPE_CONTROL_RENDER_TARGET_FLUSH
|
920 PIPE_CONTROL_CS_STALL
);
926 brw_blorp_clear_color(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
927 GLbitfield mask
, bool partial_clear
, bool encode_srgb
)
929 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
930 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
931 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
933 /* Only clear the buffers present in the provided mask */
934 if (((1 << fb
->_ColorDrawBufferIndexes
[buf
]) & mask
) == 0)
937 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
938 * the framebuffer can be complete with some attachments missing. In
939 * this case the _ColorDrawBuffers pointer will be NULL.
944 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
,
949 irb
->need_downsample
= true;
956 brw_blorp_resolve_color(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
,
957 unsigned level
, unsigned layer
)
959 DBG("%s to mt %p level %u layer %u\n", __FUNCTION__
, mt
, level
, layer
);
961 const mesa_format format
= _mesa_get_srgb_format_linear(mt
->format
);
963 struct isl_surf isl_tmp
[2];
964 struct blorp_surf surf
;
965 blorp_surf_for_miptree(brw
, &surf
, mt
, true,
966 (1 << ISL_AUX_USAGE_CCS_E
) |
967 (1 << ISL_AUX_USAGE_CCS_D
),
968 &level
, layer
, 1 /* num_layers */,
971 enum blorp_fast_clear_op resolve_op
;
973 if (surf
.aux_usage
== ISL_AUX_USAGE_CCS_E
)
974 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
976 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
;
978 assert(surf
.aux_usage
== ISL_AUX_USAGE_CCS_D
);
979 /* Broadwell and earlier do not have a partial resolve */
980 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
983 struct blorp_batch batch
;
984 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
985 blorp_ccs_resolve(&batch
, &surf
, level
, layer
,
986 brw_blorp_to_isl_format(brw
, format
, true),
988 blorp_batch_finish(&batch
);
991 * Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
993 * Any transition from any value in {Clear, Render, Resolve} to a
994 * different value in {Clear, Render, Resolve} requires end of pipe
997 brw_emit_pipe_control_flush(brw
,
998 PIPE_CONTROL_RENDER_TARGET_FLUSH
|
999 PIPE_CONTROL_CS_STALL
);
1003 gen6_blorp_hiz_exec(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
,
1004 unsigned int level
, unsigned int layer
, enum blorp_hiz_op op
)
1006 assert(intel_miptree_level_has_hiz(mt
, level
));
1008 struct isl_surf isl_tmp
[2];
1009 struct blorp_surf surf
;
1010 blorp_surf_for_miptree(brw
, &surf
, mt
, true, (1 << ISL_AUX_USAGE_HIZ
),
1011 &level
, layer
, 1, isl_tmp
);
1013 struct blorp_batch batch
;
1014 blorp_batch_init(&brw
->blorp
, &batch
, brw
, 0);
1015 blorp_gen6_hiz_op(&batch
, &surf
, level
, layer
, op
);
1016 blorp_batch_finish(&batch
);
1020 * Perform a HiZ or depth resolve operation.
1022 * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1023 * PRM, Volume 1, Part 2:
1024 * - 7.5.3.1 Depth Buffer Clear
1025 * - 7.5.3.2 Depth Buffer Resolve
1026 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1029 intel_hiz_exec(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
,
1030 unsigned int level
, unsigned int layer
, enum blorp_hiz_op op
)
1032 const char *opname
= NULL
;
1035 case BLORP_HIZ_OP_DEPTH_RESOLVE
:
1036 opname
= "depth resolve";
1038 case BLORP_HIZ_OP_HIZ_RESOLVE
:
1039 opname
= "hiz ambiguate";
1041 case BLORP_HIZ_OP_DEPTH_CLEAR
:
1042 opname
= "depth clear";
1044 case BLORP_HIZ_OP_NONE
:
1049 DBG("%s %s to mt %p level %d layer %d\n",
1050 __func__
, opname
, mt
, level
, layer
);
1052 if (brw
->gen
>= 8) {
1053 gen8_hiz_exec(brw
, mt
, level
, layer
, op
);
1055 gen6_blorp_hiz_exec(brw
, mt
, level
, layer
, op
);