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 "blorp_nir_builder.h"
25 #include "compiler/nir/nir_format_convert.h"
27 #include "blorp_priv.h"
29 #include "util/format_rgb9e5.h"
30 /* header-only include needed for _mesa_unorm_to_float and friends. */
31 #include "mesa/main/format_utils.h"
33 #define FILE_DEBUG_FLAG DEBUG_BLORP
35 static const bool split_blorp_blit_debug
= false;
38 * Enum to specify the order of arguments in a sampler message
40 enum sampler_message_arg
42 SAMPLER_MESSAGE_ARG_U_FLOAT
,
43 SAMPLER_MESSAGE_ARG_V_FLOAT
,
44 SAMPLER_MESSAGE_ARG_U_INT
,
45 SAMPLER_MESSAGE_ARG_V_INT
,
46 SAMPLER_MESSAGE_ARG_R_INT
,
47 SAMPLER_MESSAGE_ARG_SI_INT
,
48 SAMPLER_MESSAGE_ARG_MCS_INT
,
49 SAMPLER_MESSAGE_ARG_ZERO_INT
,
52 struct brw_blorp_blit_vars
{
53 /* Input values from brw_blorp_wm_inputs */
54 nir_variable
*v_discard_rect
;
55 nir_variable
*v_rect_grid
;
56 nir_variable
*v_coord_transform
;
57 nir_variable
*v_src_z
;
58 nir_variable
*v_src_offset
;
59 nir_variable
*v_dst_offset
;
60 nir_variable
*v_src_inv_size
;
63 nir_variable
*frag_coord
;
66 nir_variable
*color_out
;
70 brw_blorp_blit_vars_init(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
71 const struct brw_blorp_blit_prog_key
*key
)
73 /* Blended and scaled blits never use pixel discard. */
74 assert(!key
->use_kill
|| !(key
->blend
&& key
->blit_scaled
));
76 #define LOAD_INPUT(name, type)\
77 v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
79 LOAD_INPUT(discard_rect
, glsl_vec4_type())
80 LOAD_INPUT(rect_grid
, glsl_vec4_type())
81 LOAD_INPUT(coord_transform
, glsl_vec4_type())
82 LOAD_INPUT(src_z
, glsl_uint_type())
83 LOAD_INPUT(src_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
84 LOAD_INPUT(dst_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
85 LOAD_INPUT(src_inv_size
, glsl_vector_type(GLSL_TYPE_FLOAT
, 2))
89 v
->frag_coord
= nir_variable_create(b
->shader
, nir_var_shader_in
,
90 glsl_vec4_type(), "gl_FragCoord");
91 v
->frag_coord
->data
.location
= VARYING_SLOT_POS
;
92 v
->frag_coord
->data
.origin_upper_left
= true;
94 v
->color_out
= nir_variable_create(b
->shader
, nir_var_shader_out
,
95 glsl_vec4_type(), "gl_FragColor");
96 v
->color_out
->data
.location
= FRAG_RESULT_COLOR
;
100 blorp_blit_get_frag_coords(nir_builder
*b
,
101 const struct brw_blorp_blit_prog_key
*key
,
102 struct brw_blorp_blit_vars
*v
)
104 nir_ssa_def
*coord
= nir_f2i32(b
, nir_load_var(b
, v
->frag_coord
));
106 /* Account for destination surface intratile offset
108 * Transformation parameters giving translation from destination to source
109 * coordinates don't take into account possible intra-tile destination
110 * offset. Therefore it has to be first subtracted from the incoming
111 * coordinates. Vertices are set up based on coordinates containing the
114 if (key
->need_dst_offset
)
115 coord
= nir_isub(b
, coord
, nir_load_var(b
, v
->v_dst_offset
));
117 if (key
->persample_msaa_dispatch
) {
118 return nir_vec3(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1),
119 nir_load_sample_id(b
));
121 return nir_vec2(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1));
126 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
130 blorp_blit_apply_transform(nir_builder
*b
, nir_ssa_def
*src_pos
,
131 struct brw_blorp_blit_vars
*v
)
133 nir_ssa_def
*coord_transform
= nir_load_var(b
, v
->v_coord_transform
);
135 nir_ssa_def
*offset
= nir_vec2(b
, nir_channel(b
, coord_transform
, 1),
136 nir_channel(b
, coord_transform
, 3));
137 nir_ssa_def
*mul
= nir_vec2(b
, nir_channel(b
, coord_transform
, 0),
138 nir_channel(b
, coord_transform
, 2));
140 return nir_fadd(b
, nir_fmul(b
, src_pos
, mul
), offset
);
144 blorp_nir_discard_if_outside_rect(nir_builder
*b
, nir_ssa_def
*pos
,
145 struct brw_blorp_blit_vars
*v
)
147 nir_ssa_def
*c0
, *c1
, *c2
, *c3
;
148 nir_ssa_def
*discard_rect
= nir_load_var(b
, v
->v_discard_rect
);
149 nir_ssa_def
*dst_x0
= nir_channel(b
, discard_rect
, 0);
150 nir_ssa_def
*dst_x1
= nir_channel(b
, discard_rect
, 1);
151 nir_ssa_def
*dst_y0
= nir_channel(b
, discard_rect
, 2);
152 nir_ssa_def
*dst_y1
= nir_channel(b
, discard_rect
, 3);
154 c0
= nir_ult(b
, nir_channel(b
, pos
, 0), dst_x0
);
155 c1
= nir_uge(b
, nir_channel(b
, pos
, 0), dst_x1
);
156 c2
= nir_ult(b
, nir_channel(b
, pos
, 1), dst_y0
);
157 c3
= nir_uge(b
, nir_channel(b
, pos
, 1), dst_y1
);
159 nir_ssa_def
*oob
= nir_ior(b
, nir_ior(b
, c0
, c1
), nir_ior(b
, c2
, c3
));
161 nir_intrinsic_instr
*discard
=
162 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard_if
);
163 discard
->src
[0] = nir_src_for_ssa(oob
);
164 nir_builder_instr_insert(b
, &discard
->instr
);
167 static nir_tex_instr
*
168 blorp_create_nir_tex_instr(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
169 nir_texop op
, nir_ssa_def
*pos
, unsigned num_srcs
,
170 nir_alu_type dst_type
)
172 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, num_srcs
);
176 tex
->dest_type
= dst_type
;
177 tex
->is_array
= false;
178 tex
->is_shadow
= false;
180 /* Blorp only has one texture and it's bound at unit 0 */
183 tex
->texture_index
= 0;
184 tex
->sampler_index
= 0;
186 /* To properly handle 3-D and 2-D array textures, we pull the Z component
187 * from an input. TODO: This is a bit magic; we should probably make this
188 * more explicit in the future.
190 assert(pos
->num_components
>= 2);
191 pos
= nir_vec3(b
, nir_channel(b
, pos
, 0), nir_channel(b
, pos
, 1),
192 nir_load_var(b
, v
->v_src_z
));
194 tex
->src
[0].src_type
= nir_tex_src_coord
;
195 tex
->src
[0].src
= nir_src_for_ssa(pos
);
196 tex
->coord_components
= 3;
198 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
204 blorp_nir_tex(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
205 const struct brw_blorp_blit_prog_key
*key
, nir_ssa_def
*pos
)
207 if (key
->need_src_offset
)
208 pos
= nir_fadd(b
, pos
, nir_i2f32(b
, nir_load_var(b
, v
->v_src_offset
)));
210 /* If the sampler requires normalized coordinates, we need to compensate. */
211 if (key
->src_coords_normalized
)
212 pos
= nir_fmul(b
, pos
, nir_load_var(b
, v
->v_src_inv_size
));
215 blorp_create_nir_tex_instr(b
, v
, nir_texop_tex
, pos
, 2,
216 key
->texture_data_type
);
218 assert(pos
->num_components
== 2);
219 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
220 tex
->src
[1].src_type
= nir_tex_src_lod
;
221 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
223 nir_builder_instr_insert(b
, &tex
->instr
);
225 return &tex
->dest
.ssa
;
229 blorp_nir_txf(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
230 nir_ssa_def
*pos
, nir_alu_type dst_type
)
233 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf
, pos
, 2, dst_type
);
235 tex
->sampler_dim
= GLSL_SAMPLER_DIM_3D
;
236 tex
->src
[1].src_type
= nir_tex_src_lod
;
237 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
239 nir_builder_instr_insert(b
, &tex
->instr
);
241 return &tex
->dest
.ssa
;
245 blorp_nir_txf_ms(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
246 nir_ssa_def
*pos
, nir_ssa_def
*mcs
, nir_alu_type dst_type
)
249 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms
, pos
,
250 mcs
!= NULL
? 3 : 2, dst_type
);
252 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
254 tex
->src
[1].src_type
= nir_tex_src_ms_index
;
255 if (pos
->num_components
== 2) {
256 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
258 assert(pos
->num_components
== 3);
259 tex
->src
[1].src
= nir_src_for_ssa(nir_channel(b
, pos
, 2));
263 tex
->src
[2].src_type
= nir_tex_src_ms_mcs
;
264 tex
->src
[2].src
= nir_src_for_ssa(mcs
);
267 nir_builder_instr_insert(b
, &tex
->instr
);
269 return &tex
->dest
.ssa
;
273 blorp_blit_txf_ms_mcs(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
277 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms_mcs
,
278 pos
, 1, nir_type_int
);
280 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
282 nir_builder_instr_insert(b
, &tex
->instr
);
284 return &tex
->dest
.ssa
;
288 * Emit code to compensate for the difference between Y and W tiling.
290 * This code modifies the X and Y coordinates according to the formula:
292 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
294 * (See brw_blorp_build_nir_shader).
296 static inline nir_ssa_def
*
297 blorp_nir_retile_y_to_w(nir_builder
*b
, nir_ssa_def
*pos
)
299 assert(pos
->num_components
== 2);
300 nir_ssa_def
*x_Y
= nir_channel(b
, pos
, 0);
301 nir_ssa_def
*y_Y
= nir_channel(b
, pos
, 1);
303 /* Given X and Y coordinates that describe an address using Y tiling,
304 * translate to the X and Y coordinates that describe the same address
307 * If we break down the low order bits of X and Y, using a
308 * single letter to represent each low-order bit:
310 * X = A << 7 | 0bBCDEFGH
311 * Y = J << 5 | 0bKLMNP (1)
313 * Then we can apply the Y tiling formula to see the memory offset being
316 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
318 * If we apply the W detiling formula to this memory location, that the
319 * corresponding X' and Y' coordinates are:
321 * X' = A << 6 | 0bBCDPFH (3)
322 * Y' = J << 6 | 0bKLMNEG
324 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
325 * we need to make the following computation:
327 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
328 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
330 nir_ssa_def
*x_W
= nir_imm_int(b
, 0);
331 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0xfffffff4, -1);
332 x_W
= nir_mask_shift_or(b
, x_W
, y_Y
, 0x1, 2);
333 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0x1, 0);
335 nir_ssa_def
*y_W
= nir_imm_int(b
, 0);
336 y_W
= nir_mask_shift_or(b
, y_W
, y_Y
, 0xfffffffe, 1);
337 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x8, -2);
338 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x2, -1);
340 return nir_vec2(b
, x_W
, y_W
);
344 * Emit code to compensate for the difference between Y and W tiling.
346 * This code modifies the X and Y coordinates according to the formula:
348 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
350 * (See brw_blorp_build_nir_shader).
352 static inline nir_ssa_def
*
353 blorp_nir_retile_w_to_y(nir_builder
*b
, nir_ssa_def
*pos
)
355 assert(pos
->num_components
== 2);
356 nir_ssa_def
*x_W
= nir_channel(b
, pos
, 0);
357 nir_ssa_def
*y_W
= nir_channel(b
, pos
, 1);
359 /* Applying the same logic as above, but in reverse, we obtain the
362 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
363 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
365 nir_ssa_def
*x_Y
= nir_imm_int(b
, 0);
366 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0xfffffffa, 1);
367 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x2, 2);
368 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x1, 1);
369 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0x1, 0);
371 nir_ssa_def
*y_Y
= nir_imm_int(b
, 0);
372 y_Y
= nir_mask_shift_or(b
, y_Y
, y_W
, 0xfffffffc, -1);
373 y_Y
= nir_mask_shift_or(b
, y_Y
, x_W
, 0x4, -2);
375 return nir_vec2(b
, x_Y
, y_Y
);
379 * Emit code to compensate for the difference between MSAA and non-MSAA
382 * This code modifies the X and Y coordinates according to the formula:
384 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
386 * (See brw_blorp_blit_program).
388 static inline nir_ssa_def
*
389 blorp_nir_encode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
390 unsigned num_samples
, enum isl_msaa_layout layout
)
392 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
395 case ISL_MSAA_LAYOUT_NONE
:
396 assert(pos
->num_components
== 2);
398 case ISL_MSAA_LAYOUT_ARRAY
:
399 /* No translation needed */
401 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
402 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
403 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
404 nir_ssa_def
*s_in
= pos
->num_components
== 2 ? nir_imm_int(b
, 0) :
405 nir_channel(b
, pos
, 2);
407 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
408 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
409 switch (num_samples
) {
412 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
413 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
416 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
417 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
418 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
420 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 1);
421 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
422 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
423 if (num_samples
== 2) {
426 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
427 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
428 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
433 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
434 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
436 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
438 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
439 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
440 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
441 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
442 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
443 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
444 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
448 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
449 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
451 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
454 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
455 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
456 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
457 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
458 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 2);
459 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x8, -1);
460 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
461 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
465 unreachable("Invalid number of samples for IMS layout");
468 return nir_vec2(b
, x_out
, y_out
);
472 unreachable("Invalid MSAA layout");
477 * Emit code to compensate for the difference between MSAA and non-MSAA
480 * This code modifies the X and Y coordinates according to the formula:
482 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
484 * (See brw_blorp_blit_program).
486 static inline nir_ssa_def
*
487 blorp_nir_decode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
488 unsigned num_samples
, enum isl_msaa_layout layout
)
490 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
493 case ISL_MSAA_LAYOUT_NONE
:
494 /* No translation necessary, and S should already be zero. */
495 assert(pos
->num_components
== 2);
497 case ISL_MSAA_LAYOUT_ARRAY
:
498 /* No translation necessary. */
500 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
501 assert(pos
->num_components
== 2);
503 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
504 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
506 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
507 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
508 nir_ssa_def
*s_out
= nir_imm_int(b
, 0);
509 switch (num_samples
) {
512 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
513 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
514 * S = (X & 0b10) >> 1
516 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
517 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
518 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
519 * S = (Y & 0b10) | (X & 0b10) >> 1
521 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffc, -1);
522 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
523 if (num_samples
== 2) {
525 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
527 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
528 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
529 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
530 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
535 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
536 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
537 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
538 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
540 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
541 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
542 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
543 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
544 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
545 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
546 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
550 /* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
551 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
552 * Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
553 * S = (Y & 0b100) << 1 | (X & 0b100) |
554 * (Y & 0b10) | (X & 0b10) >> 1
556 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
557 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
558 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffff8, -2);
559 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
560 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x4, 1);
561 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
562 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
563 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
567 unreachable("Invalid number of samples for IMS layout");
570 return nir_vec3(b
, x_out
, y_out
, s_out
);
574 unreachable("Invalid MSAA layout");
579 * Count the number of trailing 1 bits in the given value. For example:
581 * count_trailing_one_bits(0) == 0
582 * count_trailing_one_bits(7) == 3
583 * count_trailing_one_bits(11) == 2
585 static inline int count_trailing_one_bits(unsigned value
)
587 #ifdef HAVE___BUILTIN_CTZ
588 return __builtin_ctz(~value
);
590 return _mesa_bitcount(value
& ~(value
+ 1));
595 blorp_nir_manual_blend_average(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
596 nir_ssa_def
*pos
, unsigned tex_samples
,
597 enum isl_aux_usage tex_aux_usage
,
598 nir_alu_type dst_type
)
600 /* If non-null, this is the outer-most if statement */
601 nir_if
*outer_if
= NULL
;
603 nir_variable
*color
=
604 nir_local_variable_create(b
->impl
, glsl_vec4_type(), "color");
606 nir_ssa_def
*mcs
= NULL
;
607 if (tex_aux_usage
== ISL_AUX_USAGE_MCS
)
608 mcs
= blorp_blit_txf_ms_mcs(b
, v
, pos
);
610 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
612 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
614 * This ensures that when all samples have the same value, no numerical
615 * precision is lost, since each addition operation always adds two equal
616 * values, and summing two equal floating point values does not lose
619 * We perform this computation by treating the texture_data array as a
620 * stack and performing the following operations:
622 * - push sample 0 onto stack
623 * - push sample 1 onto stack
624 * - add top two stack entries
625 * - push sample 2 onto stack
626 * - push sample 3 onto stack
627 * - add top two stack entries
628 * - add top two stack entries
629 * - divide top stack entry by 4
631 * Note that after pushing sample i onto the stack, the number of add
632 * operations we do is equal to the number of trailing 1 bits in i. This
633 * works provided the total number of samples is a power of two, which it
634 * always is for i965.
636 * For integer formats, we replace the add operations with average
637 * operations and skip the final division.
639 nir_ssa_def
*texture_data
[5];
640 unsigned stack_depth
= 0;
641 for (unsigned i
= 0; i
< tex_samples
; ++i
) {
642 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
644 /* Push sample i onto the stack */
645 assert(stack_depth
< ARRAY_SIZE(texture_data
));
647 nir_ssa_def
*ms_pos
= nir_vec3(b
, nir_channel(b
, pos
, 0),
648 nir_channel(b
, pos
, 1),
650 texture_data
[stack_depth
++] = blorp_nir_txf_ms(b
, v
, ms_pos
, mcs
, dst_type
);
652 if (i
== 0 && tex_aux_usage
== ISL_AUX_USAGE_MCS
) {
653 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
654 * suggests an optimization:
656 * "A simple optimization with probable large return in
657 * performance is to compare the MCS value to zero (indicating
658 * all samples are on sample slice 0), and sample only from
659 * sample slice 0 using ld2dss if MCS is zero."
661 * Note that in the case where the MCS value is zero, sampling from
662 * sample slice 0 using ld2dss and sampling from sample 0 using
663 * ld2dms are equivalent (since all samples are on sample slice 0).
664 * Since we have already sampled from sample 0, all we need to do is
665 * skip the remaining fetches and averaging if MCS is zero.
667 * It's also trivial to detect when the MCS has the magic clear color
668 * value. In this case, the txf we did on sample 0 will return the
669 * clear color and we can skip the remaining fetches just like we do
672 nir_ssa_def
*mcs_zero
=
673 nir_ieq(b
, nir_channel(b
, mcs
, 0), nir_imm_int(b
, 0));
674 if (tex_samples
== 16) {
675 mcs_zero
= nir_iand(b
, mcs_zero
,
676 nir_ieq(b
, nir_channel(b
, mcs
, 1), nir_imm_int(b
, 0)));
678 nir_ssa_def
*mcs_clear
=
679 blorp_nir_mcs_is_clear_color(b
, mcs
, tex_samples
);
681 nir_if
*if_stmt
= nir_if_create(b
->shader
);
682 if_stmt
->condition
= nir_src_for_ssa(nir_ior(b
, mcs_zero
, mcs_clear
));
683 nir_cf_node_insert(b
->cursor
, &if_stmt
->cf_node
);
685 b
->cursor
= nir_after_cf_list(&if_stmt
->then_list
);
686 nir_store_var(b
, color
, texture_data
[0], 0xf);
688 b
->cursor
= nir_after_cf_list(&if_stmt
->else_list
);
692 for (int j
= 0; j
< count_trailing_one_bits(i
); j
++) {
693 assert(stack_depth
>= 2);
696 assert(dst_type
== nir_type_float
);
697 texture_data
[stack_depth
- 1] =
698 nir_fadd(b
, texture_data
[stack_depth
- 1],
699 texture_data
[stack_depth
]);
703 /* We should have just 1 sample on the stack now. */
704 assert(stack_depth
== 1);
706 texture_data
[0] = nir_fmul(b
, texture_data
[0],
707 nir_imm_float(b
, 1.0 / tex_samples
));
709 nir_store_var(b
, color
, texture_data
[0], 0xf);
712 b
->cursor
= nir_after_cf_node(&outer_if
->cf_node
);
714 return nir_load_var(b
, color
);
717 static inline nir_ssa_def
*
718 nir_imm_vec2(nir_builder
*build
, float x
, float y
)
722 memset(&v
, 0, sizeof(v
));
726 return nir_build_imm(build
, 4, 32, v
);
730 blorp_nir_manual_blend_bilinear(nir_builder
*b
, nir_ssa_def
*pos
,
731 unsigned tex_samples
,
732 const struct brw_blorp_blit_prog_key
*key
,
733 struct brw_blorp_blit_vars
*v
)
735 nir_ssa_def
*pos_xy
= nir_channels(b
, pos
, 0x3);
736 nir_ssa_def
*rect_grid
= nir_load_var(b
, v
->v_rect_grid
);
737 nir_ssa_def
*scale
= nir_imm_vec2(b
, key
->x_scale
, key
->y_scale
);
739 /* Translate coordinates to lay out the samples in a rectangular grid
740 * roughly corresponding to sample locations.
742 pos_xy
= nir_fmul(b
, pos_xy
, scale
);
743 /* Adjust coordinates so that integers represent pixel centers rather
746 pos_xy
= nir_fadd(b
, pos_xy
, nir_imm_float(b
, -0.5));
747 /* Clamp the X, Y texture coordinates to properly handle the sampling of
748 * texels on texture edges.
750 pos_xy
= nir_fmin(b
, nir_fmax(b
, pos_xy
, nir_imm_float(b
, 0.0)),
751 nir_vec2(b
, nir_channel(b
, rect_grid
, 0),
752 nir_channel(b
, rect_grid
, 1)));
754 /* Store the fractional parts to be used as bilinear interpolation
757 nir_ssa_def
*frac_xy
= nir_ffract(b
, pos_xy
);
758 /* Round the float coordinates down to nearest integer */
759 pos_xy
= nir_fdiv(b
, nir_ftrunc(b
, pos_xy
), scale
);
761 nir_ssa_def
*tex_data
[4];
762 for (unsigned i
= 0; i
< 4; ++i
) {
763 float sample_off_x
= (float)(i
& 0x1) / key
->x_scale
;
764 float sample_off_y
= (float)((i
>> 1) & 0x1) / key
->y_scale
;
765 nir_ssa_def
*sample_off
= nir_imm_vec2(b
, sample_off_x
, sample_off_y
);
767 nir_ssa_def
*sample_coords
= nir_fadd(b
, pos_xy
, sample_off
);
768 nir_ssa_def
*sample_coords_int
= nir_f2i32(b
, sample_coords
);
770 /* The MCS value we fetch has to match up with the pixel that we're
771 * sampling from. Since we sample from different pixels in each
772 * iteration of this "for" loop, the call to mcs_fetch() should be
773 * here inside the loop after computing the pixel coordinates.
775 nir_ssa_def
*mcs
= NULL
;
776 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
777 mcs
= blorp_blit_txf_ms_mcs(b
, v
, sample_coords_int
);
779 /* Compute sample index and map the sample index to a sample number.
780 * Sample index layout shows the numbering of slots in a rectangular
781 * grid of samples with in a pixel. Sample number layout shows the
782 * rectangular grid of samples roughly corresponding to the real sample
783 * locations with in a pixel.
784 * In case of 4x MSAA, layout of sample indices matches the layout of
792 * In case of 8x MSAA the two layouts don't match.
793 * sample index layout : --------- sample number layout : ---------
794 * | 0 | 1 | | 3 | 7 |
795 * --------- ---------
796 * | 2 | 3 | | 5 | 0 |
797 * --------- ---------
798 * | 4 | 5 | | 1 | 2 |
799 * --------- ---------
800 * | 6 | 7 | | 4 | 6 |
801 * --------- ---------
803 * Fortunately, this can be done fairly easily as:
804 * S' = (0x17306425 >> (S * 4)) & 0xf
806 * In the case of 16x MSAA the two layouts don't match.
807 * Sample index layout: Sample number layout:
808 * --------------------- ---------------------
809 * | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
810 * --------------------- ---------------------
811 * | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
812 * --------------------- ---------------------
813 * | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
814 * --------------------- ---------------------
815 * | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
816 * --------------------- ---------------------
818 * This is equivalent to
819 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
821 nir_ssa_def
*frac
= nir_ffract(b
, sample_coords
);
822 nir_ssa_def
*sample
=
823 nir_fdot2(b
, frac
, nir_imm_vec2(b
, key
->x_scale
,
824 key
->x_scale
* key
->y_scale
));
825 sample
= nir_f2i32(b
, sample
);
827 if (tex_samples
== 8) {
828 sample
= nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0x64210573),
829 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
830 nir_imm_int(b
, 0xf));
831 } else if (tex_samples
== 16) {
832 nir_ssa_def
*sample_low
=
833 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xd31479af),
834 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
835 nir_imm_int(b
, 0xf));
836 nir_ssa_def
*sample_high
=
837 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xe58b602c),
838 nir_ishl(b
, nir_iadd(b
, sample
,
841 nir_imm_int(b
, 0xf));
843 sample
= nir_bcsel(b
, nir_ilt(b
, sample
, nir_imm_int(b
, 8)),
844 sample_low
, sample_high
);
846 nir_ssa_def
*pos_ms
= nir_vec3(b
, nir_channel(b
, sample_coords_int
, 0),
847 nir_channel(b
, sample_coords_int
, 1),
849 tex_data
[i
] = blorp_nir_txf_ms(b
, v
, pos_ms
, mcs
, key
->texture_data_type
);
852 nir_ssa_def
*frac_x
= nir_channel(b
, frac_xy
, 0);
853 nir_ssa_def
*frac_y
= nir_channel(b
, frac_xy
, 1);
854 return nir_flrp(b
, nir_flrp(b
, tex_data
[0], tex_data
[1], frac_x
),
855 nir_flrp(b
, tex_data
[2], tex_data
[3], frac_x
),
859 /** Perform a color bit-cast operation
861 * For copy operations involving CCS, we may need to use different formats for
862 * the source and destination surfaces. The two formats must both be UINT
863 * formats and must have the same size but may have different bit layouts.
864 * For instance, we may be copying from R8G8B8A8_UINT to R32_UINT or R32_UINT
865 * to R16G16_UINT. This function generates code to shuffle bits around to get
866 * us from one to the other.
869 bit_cast_color(struct nir_builder
*b
, nir_ssa_def
*color
,
870 const struct brw_blorp_blit_prog_key
*key
)
872 assert(key
->texture_data_type
== nir_type_uint
);
874 /* We don't actually know how many source channels we have and NIR will
875 * assert if the number of destination channels ends up being more than 4.
876 * Choose the largest number of source channels that won't over-fill a
879 const unsigned src_channels
=
880 MIN2(4, (4 * key
->dst_bpc
) / key
->src_bpc
);
881 color
= nir_channels(b
, color
, (1 << src_channels
) - 1);
883 color
= nir_format_bitcast_uint_vec_unmasked(b
, color
, key
->src_bpc
,
886 /* Blorp likes to assume that colors are vec4s */
887 nir_ssa_def
*u
= nir_ssa_undef(b
, 1, 32);
888 nir_ssa_def
*chans
[4] = { u
, u
, u
, u
};
889 for (unsigned i
= 0; i
< color
->num_components
; i
++)
890 chans
[i
] = nir_channel(b
, color
, i
);
891 return nir_vec4(b
, chans
[0], chans
[1], chans
[2], chans
[3]);
895 select_color_channel(struct nir_builder
*b
, nir_ssa_def
*color
,
896 nir_alu_type data_type
,
897 enum isl_channel_select chan
)
899 if (chan
== ISL_CHANNEL_SELECT_ZERO
) {
900 return nir_imm_int(b
, 0);
901 } else if (chan
== ISL_CHANNEL_SELECT_ONE
) {
905 return nir_imm_int(b
, 1);
907 return nir_imm_float(b
, 1);
909 unreachable("Invalid data type");
912 assert((unsigned)(chan
- ISL_CHANNEL_SELECT_RED
) < 4);
913 return nir_channel(b
, color
, chan
- ISL_CHANNEL_SELECT_RED
);
918 swizzle_color(struct nir_builder
*b
, nir_ssa_def
*color
,
919 struct isl_swizzle swizzle
, nir_alu_type data_type
)
922 select_color_channel(b
, color
, data_type
, swizzle
.r
),
923 select_color_channel(b
, color
, data_type
, swizzle
.g
),
924 select_color_channel(b
, color
, data_type
, swizzle
.b
),
925 select_color_channel(b
, color
, data_type
, swizzle
.a
));
929 convert_color(struct nir_builder
*b
, nir_ssa_def
*color
,
930 const struct brw_blorp_blit_prog_key
*key
)
932 /* All of our color conversions end up generating a single-channel color
933 * value that we need to write out.
937 if (key
->dst_format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
) {
938 /* The destination image is bound as R32_UNORM but the data needs to be
939 * in R24_UNORM_X8_TYPELESS. The bottom 24 are the actual data and the
940 * top 8 need to be zero. We can accomplish this by simply multiplying
941 * by a factor to scale things down.
943 float factor
= (float)((1 << 24) - 1) / (float)UINT32_MAX
;
944 value
= nir_fmul(b
, nir_fsat(b
, nir_channel(b
, color
, 0)),
945 nir_imm_float(b
, factor
));
946 } else if (key
->dst_format
== ISL_FORMAT_L8_UNORM_SRGB
) {
947 value
= nir_format_linear_to_srgb(b
, color
);
948 } else if (key
->dst_format
== ISL_FORMAT_R9G9B9E5_SHAREDEXP
) {
949 value
= nir_format_pack_r9g9b9e5(b
, color
);
951 unreachable("Unsupported format conversion");
954 nir_ssa_def
*u
= nir_ssa_undef(b
, 1, 32);
955 return nir_vec4(b
, value
, u
, u
, u
);
959 * Generator for WM programs used in BLORP blits.
961 * The bulk of the work done by the WM program is to wrap and unwrap the
962 * coordinate transformations used by the hardware to store surfaces in
963 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
964 * sample index for a multisampled surface) to a memory offset by the
965 * following formulas:
967 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
968 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
970 * For a single-sampled surface, or for a multisampled surface using
971 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
974 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
975 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
976 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
977 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
979 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
980 * embeds the sample number into bit 1 of the X and Y coordinates:
982 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
983 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
984 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
985 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
986 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
987 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
988 * S = (Y & 0b10) | (X & 0b10) >> 1
990 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
991 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
994 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
995 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
996 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
997 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
998 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
999 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1000 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1002 * For X tiling, tile() combines together the low-order bits of the X and Y
1003 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
1004 * bytes wide and 8 rows high:
1006 * tile(x_tiled, X, Y, S) = A
1007 * where A = tile_num << 12 | offset
1008 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
1009 * offset = (Y' & 0b111) << 9
1010 * | (X & 0b111111111)
1012 * Y' = Y + S * qpitch
1013 * detile(x_tiled, A) = (X, Y, S)
1014 * where X = X' / cpp
1017 * Y' = (tile_num / tile_pitch) << 3
1018 * | (A & 0b111000000000) >> 9
1019 * X' = (tile_num % tile_pitch) << 9
1020 * | (A & 0b111111111)
1022 * (In all tiling formulas, cpp is the number of bytes occupied by a single
1023 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
1024 * to fill the width of the surface, and qpitch is the spacing (in rows)
1025 * between array slices).
1027 * For Y tiling, tile() combines together the low-order bits of the X and Y
1028 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
1029 * bytes wide and 32 rows high:
1031 * tile(y_tiled, X, Y, S) = A
1032 * where A = tile_num << 12 | offset
1033 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
1034 * offset = (X' & 0b1110000) << 5
1035 * | (Y' & 0b11111) << 4
1038 * Y' = Y + S * qpitch
1039 * detile(y_tiled, A) = (X, Y, S)
1040 * where X = X' / cpp
1043 * Y' = (tile_num / tile_pitch) << 5
1044 * | (A & 0b111110000) >> 4
1045 * X' = (tile_num % tile_pitch) << 7
1046 * | (A & 0b111000000000) >> 5
1049 * For W tiling, tile() combines together the low-order bits of the X and Y
1050 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
1051 * bytes wide and 64 rows high (note that W tiling is only used for stencil
1052 * buffers, which always have cpp = 1 and S=0):
1054 * tile(w_tiled, X, Y, S) = A
1055 * where A = tile_num << 12 | offset
1056 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
1057 * offset = (X' & 0b111000) << 6
1058 * | (Y' & 0b111100) << 3
1059 * | (X' & 0b100) << 2
1060 * | (Y' & 0b10) << 2
1061 * | (X' & 0b10) << 1
1065 * Y' = Y + S * qpitch
1066 * detile(w_tiled, A) = (X, Y, S)
1067 * where X = X' / cpp = X'
1068 * Y = Y' % qpitch = Y'
1069 * S = Y / qpitch = 0
1070 * Y' = (tile_num / tile_pitch) << 6
1071 * | (A & 0b111100000) >> 3
1072 * | (A & 0b1000) >> 2
1074 * X' = (tile_num % tile_pitch) << 6
1075 * | (A & 0b111000000000) >> 6
1076 * | (A & 0b10000) >> 2
1077 * | (A & 0b100) >> 1
1080 * Finally, for a non-tiled surface, tile() simply combines together the X and
1081 * Y coordinates in the natural way:
1083 * tile(untiled, X, Y, S) = A
1084 * where A = Y * pitch + X'
1086 * Y' = Y + S * qpitch
1087 * detile(untiled, A) = (X, Y, S)
1088 * where X = X' / cpp
1094 * (In these formulas, pitch is the number of bytes occupied by a single row
1098 brw_blorp_build_nir_shader(struct blorp_context
*blorp
, void *mem_ctx
,
1099 const struct brw_blorp_blit_prog_key
*key
)
1101 const struct gen_device_info
*devinfo
= blorp
->isl_dev
->info
;
1102 nir_ssa_def
*src_pos
, *dst_pos
, *color
;
1105 if (key
->dst_tiled_w
&& key
->rt_samples
> 1) {
1106 /* If the destination image is W tiled and multisampled, then the thread
1107 * must be dispatched once per sample, not once per pixel. This is
1108 * necessary because after conversion between W and Y tiling, there's no
1109 * guarantee that all samples corresponding to a single pixel will still
1112 assert(key
->persample_msaa_dispatch
);
1116 /* We are blending, which means we won't have an opportunity to
1117 * translate the tiling and sample count for the texture surface. So
1118 * the surface state for the texture must be configured with the correct
1119 * tiling and sample count.
1121 assert(!key
->src_tiled_w
);
1122 assert(key
->tex_samples
== key
->src_samples
);
1123 assert(key
->tex_layout
== key
->src_layout
);
1124 assert(key
->tex_samples
> 0);
1127 if (key
->persample_msaa_dispatch
) {
1128 /* It only makes sense to do persample dispatch if the render target is
1129 * configured as multisampled.
1131 assert(key
->rt_samples
> 0);
1134 /* Make sure layout is consistent with sample count */
1135 assert((key
->tex_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1136 (key
->tex_samples
<= 1));
1137 assert((key
->rt_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1138 (key
->rt_samples
<= 1));
1139 assert((key
->src_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1140 (key
->src_samples
<= 1));
1141 assert((key
->dst_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1142 (key
->dst_samples
<= 1));
1145 nir_builder_init_simple_shader(&b
, mem_ctx
, MESA_SHADER_FRAGMENT
, NULL
);
1147 struct brw_blorp_blit_vars v
;
1148 brw_blorp_blit_vars_init(&b
, &v
, key
);
1150 dst_pos
= blorp_blit_get_frag_coords(&b
, key
, &v
);
1152 /* Render target and texture hardware don't support W tiling until Gen8. */
1153 const bool rt_tiled_w
= false;
1154 const bool tex_tiled_w
= devinfo
->gen
>= 8 && key
->src_tiled_w
;
1156 /* The address that data will be written to is determined by the
1157 * coordinates supplied to the WM thread and the tiling and sample count of
1158 * the render target, according to the formula:
1160 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1162 * If the actual tiling and sample count of the destination surface are not
1163 * the same as the configuration of the render target, then these
1164 * coordinates are wrong and we have to adjust them to compensate for the
1167 if (rt_tiled_w
!= key
->dst_tiled_w
||
1168 key
->rt_samples
!= key
->dst_samples
||
1169 key
->rt_layout
!= key
->dst_layout
) {
1170 dst_pos
= blorp_nir_encode_msaa(&b
, dst_pos
, key
->rt_samples
,
1172 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1173 if (rt_tiled_w
!= key
->dst_tiled_w
)
1174 dst_pos
= blorp_nir_retile_y_to_w(&b
, dst_pos
);
1175 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1176 dst_pos
= blorp_nir_decode_msaa(&b
, dst_pos
, key
->dst_samples
,
1180 nir_ssa_def
*comp
= NULL
;
1182 /* The destination image is bound as a red texture three times as wide
1183 * as the actual image. Our shader is effectively running one color
1184 * component at a time. We need to save off the component and adjust
1185 * the destination position.
1187 assert(dst_pos
->num_components
== 2);
1188 nir_ssa_def
*dst_x
= nir_channel(&b
, dst_pos
, 0);
1189 comp
= nir_umod(&b
, dst_x
, nir_imm_int(&b
, 3));
1190 dst_pos
= nir_vec2(&b
, nir_idiv(&b
, dst_x
, nir_imm_int(&b
, 3)),
1191 nir_channel(&b
, dst_pos
, 1));
1194 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1196 * That is: X, Y and S now contain the true coordinates and sample index of
1197 * the data that the WM thread should output.
1199 * If we need to kill pixels that are outside the destination rectangle,
1200 * now is the time to do it.
1202 if (key
->use_kill
) {
1203 assert(!(key
->blend
&& key
->blit_scaled
));
1204 blorp_nir_discard_if_outside_rect(&b
, dst_pos
, &v
);
1207 src_pos
= blorp_blit_apply_transform(&b
, nir_i2f32(&b
, dst_pos
), &v
);
1208 if (dst_pos
->num_components
== 3) {
1209 /* The sample coordinate is an integer that we want left alone but
1210 * blorp_blit_apply_transform() blindly applies the transform to all
1211 * three coordinates. Grab the original sample index.
1213 src_pos
= nir_vec3(&b
, nir_channel(&b
, src_pos
, 0),
1214 nir_channel(&b
, src_pos
, 1),
1215 nir_channel(&b
, dst_pos
, 2));
1218 /* If the source image is not multisampled, then we want to fetch sample
1219 * number 0, because that's the only sample there is.
1221 if (key
->src_samples
== 1)
1222 src_pos
= nir_channels(&b
, src_pos
, 0x3);
1224 /* X, Y, and S are now the coordinates of the pixel in the source image
1225 * that we want to texture from. Exception: if we are blending, then S is
1226 * irrelevant, because we are going to fetch all samples.
1228 if (key
->blend
&& !key
->blit_scaled
) {
1229 /* Resolves (effecively) use texelFetch, so we need integers and we
1230 * don't care about the sample index if we got one.
1232 src_pos
= nir_f2i32(&b
, nir_channels(&b
, src_pos
, 0x3));
1234 if (devinfo
->gen
== 6) {
1235 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1236 * blending we need with a single linear-interpolated texture lookup
1237 * at the center of the sample. The texture coordinates to be odd
1238 * integers so that they correspond to the center of a 2x2 block
1239 * representing the four samples that maxe up a pixel. So we need
1240 * to multiply our X and Y coordinates each by 2 and then add 1.
1242 assert(key
->src_coords_normalized
);
1243 src_pos
= nir_fadd(&b
,
1244 nir_i2f32(&b
, src_pos
),
1245 nir_imm_float(&b
, 0.5f
));
1246 color
= blorp_nir_tex(&b
, &v
, key
, src_pos
);
1248 /* Gen7+ hardware doesn't automaticaly blend. */
1249 color
= blorp_nir_manual_blend_average(&b
, &v
, src_pos
, key
->src_samples
,
1251 key
->texture_data_type
);
1253 } else if (key
->blend
&& key
->blit_scaled
) {
1254 assert(!key
->use_kill
);
1255 color
= blorp_nir_manual_blend_bilinear(&b
, src_pos
, key
->src_samples
, key
, &v
);
1257 if (key
->bilinear_filter
) {
1258 color
= blorp_nir_tex(&b
, &v
, key
, src_pos
);
1260 /* We're going to use texelFetch, so we need integers */
1261 if (src_pos
->num_components
== 2) {
1262 src_pos
= nir_f2i32(&b
, src_pos
);
1264 assert(src_pos
->num_components
== 3);
1265 src_pos
= nir_vec3(&b
, nir_channel(&b
, nir_f2i32(&b
, src_pos
), 0),
1266 nir_channel(&b
, nir_f2i32(&b
, src_pos
), 1),
1267 nir_channel(&b
, src_pos
, 2));
1270 /* We aren't blending, which means we just want to fetch a single
1271 * sample from the source surface. The address that we want to fetch
1272 * from is related to the X, Y and S values according to the formula:
1274 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1276 * If the actual tiling and sample count of the source surface are
1277 * not the same as the configuration of the texture, then we need to
1278 * adjust the coordinates to compensate for the difference.
1280 if (tex_tiled_w
!= key
->src_tiled_w
||
1281 key
->tex_samples
!= key
->src_samples
||
1282 key
->tex_layout
!= key
->src_layout
) {
1283 src_pos
= blorp_nir_encode_msaa(&b
, src_pos
, key
->src_samples
,
1285 /* Now (X, Y, S) = detile(src_tiling, offset) */
1286 if (tex_tiled_w
!= key
->src_tiled_w
)
1287 src_pos
= blorp_nir_retile_w_to_y(&b
, src_pos
);
1288 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1289 src_pos
= blorp_nir_decode_msaa(&b
, src_pos
, key
->tex_samples
,
1293 if (key
->need_src_offset
)
1294 src_pos
= nir_iadd(&b
, src_pos
, nir_load_var(&b
, v
.v_src_offset
));
1296 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1298 * In other words: X, Y, and S now contain values which, when passed to
1299 * the texturing unit, will cause data to be read from the correct
1300 * memory location. So we can fetch the texel now.
1302 if (key
->src_samples
== 1) {
1303 color
= blorp_nir_txf(&b
, &v
, src_pos
, key
->texture_data_type
);
1305 nir_ssa_def
*mcs
= NULL
;
1306 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
1307 mcs
= blorp_blit_txf_ms_mcs(&b
, &v
, src_pos
);
1309 color
= blorp_nir_txf_ms(&b
, &v
, src_pos
, mcs
, key
->texture_data_type
);
1314 if (!isl_swizzle_is_identity(key
->src_swizzle
)) {
1315 color
= swizzle_color(&b
, color
, key
->src_swizzle
,
1316 key
->texture_data_type
);
1319 if (!isl_swizzle_is_identity(key
->dst_swizzle
)) {
1320 color
= swizzle_color(&b
, color
, isl_swizzle_invert(key
->dst_swizzle
),
1324 if (key
->dst_bpc
!= key
->src_bpc
) {
1325 assert(isl_swizzle_is_identity(key
->src_swizzle
));
1326 assert(isl_swizzle_is_identity(key
->dst_swizzle
));
1327 color
= bit_cast_color(&b
, color
, key
);
1330 if (key
->dst_format
)
1331 color
= convert_color(&b
, color
, key
);
1334 /* The destination image is bound as a red texture three times as wide
1335 * as the actual image. Our shader is effectively running one color
1336 * component at a time. We need to pick off the appropriate component
1337 * from the source color and write that to destination red.
1339 assert(dst_pos
->num_components
== 2);
1341 nir_ssa_def
*color_component
=
1342 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 0)),
1343 nir_channel(&b
, color
, 0),
1344 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 1)),
1345 nir_channel(&b
, color
, 1),
1346 nir_channel(&b
, color
, 2)));
1348 nir_ssa_def
*u
= nir_ssa_undef(&b
, 1, 32);
1349 color
= nir_vec4(&b
, color_component
, u
, u
, u
);
1352 nir_store_var(&b
, v
.color_out
, color
, 0xf);
1358 brw_blorp_get_blit_kernel(struct blorp_context
*blorp
,
1359 struct blorp_params
*params
,
1360 const struct brw_blorp_blit_prog_key
*prog_key
)
1362 if (blorp
->lookup_shader(blorp
, prog_key
, sizeof(*prog_key
),
1363 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
))
1366 void *mem_ctx
= ralloc_context(NULL
);
1368 const unsigned *program
;
1369 struct brw_wm_prog_data prog_data
;
1371 nir_shader
*nir
= brw_blorp_build_nir_shader(blorp
, mem_ctx
, prog_key
);
1372 nir
->info
.name
= ralloc_strdup(nir
, "BLORP-blit");
1374 struct brw_wm_prog_key wm_key
;
1375 brw_blorp_init_wm_prog_key(&wm_key
);
1376 wm_key
.tex
.compressed_multisample_layout_mask
=
1377 prog_key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
;
1378 wm_key
.tex
.msaa_16
= prog_key
->tex_samples
== 16;
1379 wm_key
.multisample_fbo
= prog_key
->rt_samples
> 1;
1381 program
= blorp_compile_fs(blorp
, mem_ctx
, nir
, &wm_key
, false,
1385 blorp
->upload_shader(blorp
, prog_key
, sizeof(*prog_key
),
1386 program
, prog_data
.base
.program_size
,
1387 &prog_data
.base
, sizeof(prog_data
),
1388 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
);
1390 ralloc_free(mem_ctx
);
1395 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform
*xform
,
1396 GLfloat src0
, GLfloat src1
,
1397 GLfloat dst0
, GLfloat dst1
,
1400 double scale
= (double)(src1
- src0
) / (double)(dst1
- dst0
);
1402 /* When not mirroring a coordinate (say, X), we need:
1403 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1405 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1407 * blorp program uses "round toward zero" to convert the
1408 * transformed floating point coordinates to integer coordinates,
1409 * whereas the behaviour we actually want is "round to nearest",
1410 * so 0.5 provides the necessary correction.
1412 xform
->multiplier
= scale
;
1413 xform
->offset
= src0
+ (-(double)dst0
+ 0.5) * scale
;
1415 /* When mirroring X we need:
1416 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1418 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1420 xform
->multiplier
= -scale
;
1421 xform
->offset
= src0
+ ((double)dst1
- 0.5) * scale
;
1426 surf_get_intratile_offset_px(struct brw_blorp_surface_info
*info
,
1427 uint32_t *tile_x_px
, uint32_t *tile_y_px
)
1429 if (info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1430 struct isl_extent2d px_size_sa
=
1431 isl_get_interleaved_msaa_px_size_sa(info
->surf
.samples
);
1432 assert(info
->tile_x_sa
% px_size_sa
.width
== 0);
1433 assert(info
->tile_y_sa
% px_size_sa
.height
== 0);
1434 *tile_x_px
= info
->tile_x_sa
/ px_size_sa
.width
;
1435 *tile_y_px
= info
->tile_y_sa
/ px_size_sa
.height
;
1437 *tile_x_px
= info
->tile_x_sa
;
1438 *tile_y_px
= info
->tile_y_sa
;
1443 blorp_surf_convert_to_single_slice(const struct isl_device
*isl_dev
,
1444 struct brw_blorp_surface_info
*info
)
1448 /* Just bail if we have nothing to do. */
1449 if (info
->surf
.dim
== ISL_SURF_DIM_2D
&&
1450 info
->view
.base_level
== 0 && info
->view
.base_array_layer
== 0 &&
1451 info
->surf
.levels
== 1 && info
->surf
.logical_level0_px
.array_len
== 1)
1454 /* If this gets triggered then we've gotten here twice which. This
1455 * shouldn't happen thanks to the above early return.
1457 assert(info
->tile_x_sa
== 0 && info
->tile_y_sa
== 0);
1459 uint32_t layer
= 0, z
= 0;
1460 if (info
->surf
.dim
== ISL_SURF_DIM_3D
)
1461 z
= info
->view
.base_array_layer
+ info
->z_offset
;
1463 layer
= info
->view
.base_array_layer
;
1465 uint32_t byte_offset
;
1466 isl_surf_get_image_surf(isl_dev
, &info
->surf
,
1467 info
->view
.base_level
, layer
, z
,
1469 &byte_offset
, &info
->tile_x_sa
, &info
->tile_y_sa
);
1470 info
->addr
.offset
+= byte_offset
;
1472 uint32_t tile_x_px
, tile_y_px
;
1473 surf_get_intratile_offset_px(info
, &tile_x_px
, &tile_y_px
);
1475 /* Instead of using the X/Y Offset fields in RENDER_SURFACE_STATE, we place
1476 * the image at the tile boundary and offset our sampling or rendering.
1477 * For this reason, we need to grow the image by the offset to ensure that
1478 * the hardware doesn't think we've gone past the edge.
1480 info
->surf
.logical_level0_px
.w
+= tile_x_px
;
1481 info
->surf
.logical_level0_px
.h
+= tile_y_px
;
1482 info
->surf
.phys_level0_sa
.w
+= info
->tile_x_sa
;
1483 info
->surf
.phys_level0_sa
.h
+= info
->tile_y_sa
;
1485 /* The view is also different now. */
1486 info
->view
.base_level
= 0;
1487 info
->view
.levels
= 1;
1488 info
->view
.base_array_layer
= 0;
1489 info
->view
.array_len
= 1;
1494 surf_fake_interleaved_msaa(const struct isl_device
*isl_dev
,
1495 struct brw_blorp_surface_info
*info
)
1497 assert(info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
);
1499 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1500 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1502 info
->surf
.logical_level0_px
= info
->surf
.phys_level0_sa
;
1503 info
->surf
.samples
= 1;
1504 info
->surf
.msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
1508 surf_retile_w_to_y(const struct isl_device
*isl_dev
,
1509 struct brw_blorp_surface_info
*info
)
1511 assert(info
->surf
.tiling
== ISL_TILING_W
);
1513 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1514 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1516 /* On gen7+, we don't have interleaved multisampling for color render
1517 * targets so we have to fake it.
1519 * TODO: Are we sure we don't also need to fake it on gen6?
1521 if (isl_dev
->info
->gen
> 6 &&
1522 info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1523 surf_fake_interleaved_msaa(isl_dev
, info
);
1526 if (isl_dev
->info
->gen
== 6) {
1527 /* Gen6 stencil buffers have a very large alignment coming in from the
1528 * miptree. It's out-of-bounds for what the surface state can handle.
1529 * Since we have a single layer and level, it doesn't really matter as
1530 * long as we don't pass a bogus value into isl_surf_fill_state().
1532 info
->surf
.image_alignment_el
= isl_extent3d(4, 2, 1);
1535 /* Now that we've converted everything to a simple 2-D surface with only
1536 * one miplevel, we can go about retiling it.
1538 const unsigned x_align
= 8, y_align
= info
->surf
.samples
!= 0 ? 8 : 4;
1539 info
->surf
.tiling
= ISL_TILING_Y0
;
1540 info
->surf
.logical_level0_px
.width
=
1541 ALIGN(info
->surf
.logical_level0_px
.width
, x_align
) * 2;
1542 info
->surf
.logical_level0_px
.height
=
1543 ALIGN(info
->surf
.logical_level0_px
.height
, y_align
) / 2;
1544 info
->tile_x_sa
*= 2;
1545 info
->tile_y_sa
/= 2;
1549 can_shrink_surface(const struct brw_blorp_surface_info
*surf
)
1551 /* The current code doesn't support offsets into the aux buffers. This
1552 * should be possible, but we need to make sure the offset is page
1553 * aligned for both the surface and the aux buffer surface. Generally
1554 * this mean using the page aligned offset for the aux buffer.
1556 * Currently the cases where we must split the blit are limited to cases
1557 * where we don't have a aux buffer.
1559 if (surf
->aux_addr
.buffer
!= NULL
)
1562 /* We can't support splitting the blit for gen <= 7, because the qpitch
1563 * size is calculated by the hardware based on the surface height for
1564 * gen <= 7. In gen >= 8, the qpitch is controlled by the driver.
1566 if (surf
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_ARRAY
)
1573 can_shrink_surfaces(const struct blorp_params
*params
)
1576 can_shrink_surface(¶ms
->src
) &&
1577 can_shrink_surface(¶ms
->dst
);
1581 get_max_surface_size(const struct gen_device_info
*devinfo
,
1582 const struct blorp_params
*params
)
1584 const unsigned max
= devinfo
->gen
>= 7 ? 16384 : 8192;
1585 if (split_blorp_blit_debug
&& can_shrink_surfaces(params
))
1586 return max
>> 4; /* A smaller restriction when debug is enabled */
1592 double src0
, src1
, dst0
, dst1
;
1597 struct blt_axis x
, y
;
1600 static enum isl_format
1601 get_red_format_for_rgb_format(enum isl_format format
)
1603 const struct isl_format_layout
*fmtl
= isl_format_get_layout(format
);
1605 switch (fmtl
->channels
.r
.bits
) {
1607 switch (fmtl
->channels
.r
.type
) {
1609 return ISL_FORMAT_R8_UNORM
;
1611 return ISL_FORMAT_R8_SNORM
;
1613 return ISL_FORMAT_R8_UINT
;
1615 return ISL_FORMAT_R8_SINT
;
1617 unreachable("Invalid 8-bit RGB channel type");
1620 switch (fmtl
->channels
.r
.type
) {
1622 return ISL_FORMAT_R16_UNORM
;
1624 return ISL_FORMAT_R16_SNORM
;
1626 return ISL_FORMAT_R16_FLOAT
;
1628 return ISL_FORMAT_R16_UINT
;
1630 return ISL_FORMAT_R16_SINT
;
1632 unreachable("Invalid 8-bit RGB channel type");
1635 switch (fmtl
->channels
.r
.type
) {
1637 return ISL_FORMAT_R32_FLOAT
;
1639 return ISL_FORMAT_R32_UINT
;
1641 return ISL_FORMAT_R32_SINT
;
1643 unreachable("Invalid 8-bit RGB channel type");
1646 unreachable("Invalid number of red channel bits");
1651 surf_fake_rgb_with_red(const struct isl_device
*isl_dev
,
1652 struct brw_blorp_surface_info
*info
)
1654 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1656 info
->surf
.logical_level0_px
.width
*= 3;
1657 info
->surf
.phys_level0_sa
.width
*= 3;
1658 info
->tile_x_sa
*= 3;
1660 enum isl_format red_format
=
1661 get_red_format_for_rgb_format(info
->view
.format
);
1663 assert(isl_format_get_layout(red_format
)->channels
.r
.type
==
1664 isl_format_get_layout(info
->view
.format
)->channels
.r
.type
);
1665 assert(isl_format_get_layout(red_format
)->channels
.r
.bits
==
1666 isl_format_get_layout(info
->view
.format
)->channels
.r
.bits
);
1668 info
->surf
.format
= info
->view
.format
= red_format
;
1671 enum blit_shrink_status
{
1673 BLIT_WIDTH_SHRINK
= 1,
1674 BLIT_HEIGHT_SHRINK
= 2,
1677 /* Try to blit. If the surface parameters exceed the size allowed by hardware,
1678 * then enum blit_shrink_status will be returned. If BLIT_NO_SHRINK is
1679 * returned, then the blit was successful.
1681 static enum blit_shrink_status
1682 try_blorp_blit(struct blorp_batch
*batch
,
1683 struct blorp_params
*params
,
1684 struct brw_blorp_blit_prog_key
*wm_prog_key
,
1685 struct blt_coords
*coords
)
1687 const struct gen_device_info
*devinfo
= batch
->blorp
->isl_dev
->info
;
1689 if (isl_format_has_sint_channel(params
->src
.view
.format
)) {
1690 wm_prog_key
->texture_data_type
= nir_type_int
;
1691 } else if (isl_format_has_uint_channel(params
->src
.view
.format
)) {
1692 wm_prog_key
->texture_data_type
= nir_type_uint
;
1694 wm_prog_key
->texture_data_type
= nir_type_float
;
1697 /* src_samples and dst_samples are the true sample counts */
1698 wm_prog_key
->src_samples
= params
->src
.surf
.samples
;
1699 wm_prog_key
->dst_samples
= params
->dst
.surf
.samples
;
1701 wm_prog_key
->tex_aux_usage
= params
->src
.aux_usage
;
1703 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1706 wm_prog_key
->src_layout
= params
->src
.surf
.msaa_layout
;
1707 wm_prog_key
->dst_layout
= params
->dst
.surf
.msaa_layout
;
1709 /* Round floating point values to nearest integer to avoid "off by one texel"
1710 * kind of errors when blitting.
1712 params
->x0
= params
->wm_inputs
.discard_rect
.x0
= round(coords
->x
.dst0
);
1713 params
->y0
= params
->wm_inputs
.discard_rect
.y0
= round(coords
->y
.dst0
);
1714 params
->x1
= params
->wm_inputs
.discard_rect
.x1
= round(coords
->x
.dst1
);
1715 params
->y1
= params
->wm_inputs
.discard_rect
.y1
= round(coords
->y
.dst1
);
1717 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[0],
1718 coords
->x
.src0
, coords
->x
.src1
,
1719 coords
->x
.dst0
, coords
->x
.dst1
,
1721 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[1],
1722 coords
->y
.src0
, coords
->y
.src1
,
1723 coords
->y
.dst0
, coords
->y
.dst1
,
1727 if (devinfo
->gen
== 4) {
1728 /* The MinLOD and MinimumArrayElement don't work properly for cube maps.
1729 * Convert them to a single slice on gen4.
1731 if (params
->dst
.surf
.usage
& ISL_SURF_USAGE_CUBE_BIT
) {
1732 blorp_surf_convert_to_single_slice(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1733 wm_prog_key
->need_dst_offset
= true;
1736 if (params
->src
.surf
.usage
& ISL_SURF_USAGE_CUBE_BIT
) {
1737 blorp_surf_convert_to_single_slice(batch
->blorp
->isl_dev
, ¶ms
->src
);
1738 wm_prog_key
->need_src_offset
= true;
1742 if (devinfo
->gen
> 6 &&
1743 params
->dst
.surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1744 assert(params
->dst
.surf
.samples
> 1);
1746 /* We must expand the rectangle we send through the rendering pipeline,
1747 * to account for the fact that we are mapping the destination region as
1748 * single-sampled when it is in fact multisampled. We must also align
1749 * it to a multiple of the multisampling pattern, because the
1750 * differences between multisampled and single-sampled surface formats
1751 * will mean that pixels are scrambled within the multisampling pattern.
1752 * TODO: what if this makes the coordinates too large?
1754 * Note: this only works if the destination surface uses the IMS layout.
1755 * If it's UMS, then we have no choice but to set up the rendering
1756 * pipeline as multisampled.
1758 struct isl_extent2d px_size_sa
=
1759 isl_get_interleaved_msaa_px_size_sa(params
->dst
.surf
.samples
);
1760 params
->x0
= ROUND_DOWN_TO(params
->x0
, 2) * px_size_sa
.width
;
1761 params
->y0
= ROUND_DOWN_TO(params
->y0
, 2) * px_size_sa
.height
;
1762 params
->x1
= ALIGN(params
->x1
, 2) * px_size_sa
.width
;
1763 params
->y1
= ALIGN(params
->y1
, 2) * px_size_sa
.height
;
1765 surf_fake_interleaved_msaa(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1767 wm_prog_key
->use_kill
= true;
1768 wm_prog_key
->need_dst_offset
= true;
1771 if (params
->dst
.surf
.tiling
== ISL_TILING_W
) {
1772 /* We must modify the rectangle we send through the rendering pipeline
1773 * (and the size and x/y offset of the destination surface), to account
1774 * for the fact that we are mapping it as Y-tiled when it is in fact
1777 * Both Y tiling and W tiling can be understood as organizations of
1778 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1779 * is different, but the layout of the 32-byte sub-tiles within the 4k
1780 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1781 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1782 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1784 * Therefore, to account for the layout differences within the 32-byte
1785 * sub-tiles, we must expand the rectangle so the X coordinates of its
1786 * edges are multiples of 8 (the W sub-tile width), and its Y
1787 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1788 * Then we need to scale the X and Y coordinates of the rectangle to
1789 * account for the differences in aspect ratio between the Y and W
1790 * sub-tiles. We need to modify the layer width and height similarly.
1792 * A correction needs to be applied when MSAA is in use: since
1793 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1794 * we need to align the Y coordinates to multiples of 8, so that when
1795 * they are divided by two they are still multiples of 4.
1797 * Note: Since the x/y offset of the surface will be applied using the
1798 * SURFACE_STATE command packet, it will be invisible to the swizzling
1799 * code in the shader; therefore it needs to be in a multiple of the
1800 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1801 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1802 * buffer), and the miplevel alignment used for stencil buffers is 8
1803 * pixels horizontally and either 4 or 8 pixels vertically (see
1804 * intel_horizontal_texture_alignment_unit() and
1805 * intel_vertical_texture_alignment_unit()).
1807 * Note: Also, since the SURFACE_STATE command packet can only apply
1808 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1809 * vertically, it is important that the offsets will be multiples of
1810 * these sizes after they are converted into Y-tiled coordinates.
1811 * Fortunately they will be, since we know from above that the offsets
1812 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1813 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1815 * TODO: what if this makes the coordinates (or the texture size) too
1818 const unsigned x_align
= 8;
1819 const unsigned y_align
= params
->dst
.surf
.samples
!= 0 ? 8 : 4;
1820 params
->x0
= ROUND_DOWN_TO(params
->x0
, x_align
) * 2;
1821 params
->y0
= ROUND_DOWN_TO(params
->y0
, y_align
) / 2;
1822 params
->x1
= ALIGN(params
->x1
, x_align
) * 2;
1823 params
->y1
= ALIGN(params
->y1
, y_align
) / 2;
1825 /* Retile the surface to Y-tiled */
1826 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1828 wm_prog_key
->dst_tiled_w
= true;
1829 wm_prog_key
->use_kill
= true;
1830 wm_prog_key
->need_dst_offset
= true;
1832 if (params
->dst
.surf
.samples
> 1) {
1833 /* If the destination surface is a W-tiled multisampled stencil
1834 * buffer that we're mapping as Y tiled, then we need to arrange for
1835 * the WM program to run once per sample rather than once per pixel,
1836 * because the memory layout of related samples doesn't match between
1839 wm_prog_key
->persample_msaa_dispatch
= true;
1843 if (devinfo
->gen
< 8 && params
->src
.surf
.tiling
== ISL_TILING_W
) {
1844 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1845 * Broadwell adds support for sampling from stencil.
1847 * See the comments above concerning x/y offset alignment for the
1848 * destination surface.
1850 * TODO: what if this makes the texture size too large?
1852 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->src
);
1854 wm_prog_key
->src_tiled_w
= true;
1855 wm_prog_key
->need_src_offset
= true;
1858 /* tex_samples and rt_samples are the sample counts that are set up in
1861 wm_prog_key
->tex_samples
= params
->src
.surf
.samples
;
1862 wm_prog_key
->rt_samples
= params
->dst
.surf
.samples
;
1864 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1865 * use to access the source and destination surfaces.
1867 wm_prog_key
->tex_layout
= params
->src
.surf
.msaa_layout
;
1868 wm_prog_key
->rt_layout
= params
->dst
.surf
.msaa_layout
;
1870 if (params
->src
.surf
.samples
> 0 && params
->dst
.surf
.samples
> 1) {
1871 /* We are blitting from a multisample buffer to a multisample buffer, so
1872 * we must preserve samples within a pixel. This means we have to
1873 * arrange for the WM program to run once per sample rather than once
1876 wm_prog_key
->persample_msaa_dispatch
= true;
1879 params
->num_samples
= params
->dst
.surf
.samples
;
1881 if ((wm_prog_key
->bilinear_filter
||
1882 (wm_prog_key
->blend
&& !wm_prog_key
->blit_scaled
)) &&
1883 batch
->blorp
->isl_dev
->info
->gen
<= 6) {
1884 /* Gen4-5 don't support non-normalized texture coordinates */
1885 wm_prog_key
->src_coords_normalized
= true;
1886 params
->wm_inputs
.src_inv_size
[0] =
1887 1.0f
/ minify(params
->src
.surf
.logical_level0_px
.width
,
1888 params
->src
.view
.base_level
);
1889 params
->wm_inputs
.src_inv_size
[1] =
1890 1.0f
/ minify(params
->src
.surf
.logical_level0_px
.height
,
1891 params
->src
.view
.base_level
);
1894 if (isl_format_get_layout(params
->dst
.view
.format
)->bpb
% 3 == 0) {
1895 /* We can't render to RGB formats natively because they aren't a
1896 * power-of-two size. Instead, we fake them by using a red format
1897 * with the same channel type and size and emitting shader code to
1898 * only write one channel at a time.
1903 surf_fake_rgb_with_red(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1905 wm_prog_key
->dst_rgb
= true;
1906 wm_prog_key
->need_dst_offset
= true;
1907 } else if (isl_format_is_rgbx(params
->dst
.view
.format
)) {
1908 /* We can handle RGBX formats easily enough by treating them as RGBA */
1909 params
->dst
.view
.format
=
1910 isl_format_rgbx_to_rgba(params
->dst
.view
.format
);
1911 } else if (params
->dst
.view
.format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
) {
1912 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
1913 params
->dst
.view
.format
= ISL_FORMAT_R32_UNORM
;
1914 } else if (params
->dst
.view
.format
== ISL_FORMAT_A4B4G4R4_UNORM
) {
1915 params
->dst
.view
.swizzle
=
1916 isl_swizzle_compose(params
->dst
.view
.swizzle
,
1917 ISL_SWIZZLE(ALPHA
, RED
, GREEN
, BLUE
));
1918 params
->dst
.view
.format
= ISL_FORMAT_B4G4R4A4_UNORM
;
1919 } else if (params
->dst
.view
.format
== ISL_FORMAT_L8_UNORM_SRGB
) {
1920 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
1921 params
->dst
.view
.format
= ISL_FORMAT_R8_UNORM
;
1922 } else if (params
->dst
.view
.format
== ISL_FORMAT_R9G9B9E5_SHAREDEXP
) {
1923 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
1924 params
->dst
.view
.format
= ISL_FORMAT_R32_UINT
;
1927 if (devinfo
->gen
<= 7 && !devinfo
->is_haswell
&&
1928 !isl_swizzle_is_identity(params
->src
.view
.swizzle
)) {
1929 wm_prog_key
->src_swizzle
= params
->src
.view
.swizzle
;
1930 params
->src
.view
.swizzle
= ISL_SWIZZLE_IDENTITY
;
1932 wm_prog_key
->src_swizzle
= ISL_SWIZZLE_IDENTITY
;
1935 if (!isl_swizzle_supports_rendering(devinfo
, params
->dst
.view
.swizzle
)) {
1936 wm_prog_key
->dst_swizzle
= params
->dst
.view
.swizzle
;
1937 params
->dst
.view
.swizzle
= ISL_SWIZZLE_IDENTITY
;
1939 wm_prog_key
->dst_swizzle
= ISL_SWIZZLE_IDENTITY
;
1942 if (params
->src
.tile_x_sa
|| params
->src
.tile_y_sa
) {
1943 assert(wm_prog_key
->need_src_offset
);
1944 surf_get_intratile_offset_px(¶ms
->src
,
1945 ¶ms
->wm_inputs
.src_offset
.x
,
1946 ¶ms
->wm_inputs
.src_offset
.y
);
1949 if (params
->dst
.tile_x_sa
|| params
->dst
.tile_y_sa
) {
1950 assert(wm_prog_key
->need_dst_offset
);
1951 surf_get_intratile_offset_px(¶ms
->dst
,
1952 ¶ms
->wm_inputs
.dst_offset
.x
,
1953 ¶ms
->wm_inputs
.dst_offset
.y
);
1954 params
->x0
+= params
->wm_inputs
.dst_offset
.x
;
1955 params
->y0
+= params
->wm_inputs
.dst_offset
.y
;
1956 params
->x1
+= params
->wm_inputs
.dst_offset
.x
;
1957 params
->y1
+= params
->wm_inputs
.dst_offset
.y
;
1960 /* For some texture types, we need to pass the layer through the sampler. */
1961 params
->wm_inputs
.src_z
= params
->src
.z_offset
;
1963 if (!brw_blorp_get_blit_kernel(batch
->blorp
, params
, wm_prog_key
))
1966 if (!blorp_ensure_sf_program(batch
->blorp
, params
))
1969 unsigned result
= 0;
1970 unsigned max_surface_size
= get_max_surface_size(devinfo
, params
);
1971 if (params
->src
.surf
.logical_level0_px
.width
> max_surface_size
||
1972 params
->dst
.surf
.logical_level0_px
.width
> max_surface_size
)
1973 result
|= BLIT_WIDTH_SHRINK
;
1974 if (params
->src
.surf
.logical_level0_px
.height
> max_surface_size
||
1975 params
->dst
.surf
.logical_level0_px
.height
> max_surface_size
)
1976 result
|= BLIT_HEIGHT_SHRINK
;
1979 batch
->blorp
->exec(batch
, params
);
1985 /* Adjust split blit source coordinates for the current destination
1989 adjust_split_source_coords(const struct blt_axis
*orig
,
1990 struct blt_axis
*split_coords
,
1993 /* When scale is greater than 0, then we are growing from the start, so
1994 * src0 uses delta0, and src1 uses delta1. When scale is less than 0, the
1995 * source range shrinks from the end. In that case src0 is adjusted by
1996 * delta1, and src1 is adjusted by delta0.
1998 double delta0
= scale
* (split_coords
->dst0
- orig
->dst0
);
1999 double delta1
= scale
* (split_coords
->dst1
- orig
->dst1
);
2000 split_coords
->src0
= orig
->src0
+ (scale
>= 0.0 ? delta0
: delta1
);
2001 split_coords
->src1
= orig
->src1
+ (scale
>= 0.0 ? delta1
: delta0
);
2004 static struct isl_extent2d
2005 get_px_size_sa(const struct isl_surf
*surf
)
2007 static const struct isl_extent2d one_to_one
= { .w
= 1, .h
= 1 };
2009 if (surf
->msaa_layout
!= ISL_MSAA_LAYOUT_INTERLEAVED
)
2012 return isl_get_interleaved_msaa_px_size_sa(surf
->samples
);
2016 shrink_surface_params(const struct isl_device
*dev
,
2017 struct brw_blorp_surface_info
*info
,
2018 double *x0
, double *x1
, double *y0
, double *y1
)
2020 uint32_t byte_offset
, x_offset_sa
, y_offset_sa
, size
;
2021 struct isl_extent2d px_size_sa
;
2024 blorp_surf_convert_to_single_slice(dev
, info
);
2026 px_size_sa
= get_px_size_sa(&info
->surf
);
2028 /* Because this gets called after we lower compressed images, the tile
2029 * offsets may be non-zero and we need to incorporate them in our
2032 x_offset_sa
= (uint32_t)*x0
* px_size_sa
.w
+ info
->tile_x_sa
;
2033 y_offset_sa
= (uint32_t)*y0
* px_size_sa
.h
+ info
->tile_y_sa
;
2034 isl_tiling_get_intratile_offset_sa(info
->surf
.tiling
,
2035 info
->surf
.format
, info
->surf
.row_pitch
,
2036 x_offset_sa
, y_offset_sa
,
2038 &info
->tile_x_sa
, &info
->tile_y_sa
);
2040 info
->addr
.offset
+= byte_offset
;
2042 adjust
= (int)info
->tile_x_sa
/ px_size_sa
.w
- (int)*x0
;
2045 info
->tile_x_sa
= 0;
2047 adjust
= (int)info
->tile_y_sa
/ px_size_sa
.h
- (int)*y0
;
2050 info
->tile_y_sa
= 0;
2052 size
= MIN2((uint32_t)ceil(*x1
), info
->surf
.logical_level0_px
.width
);
2053 info
->surf
.logical_level0_px
.width
= size
;
2054 info
->surf
.phys_level0_sa
.width
= size
* px_size_sa
.w
;
2056 size
= MIN2((uint32_t)ceil(*y1
), info
->surf
.logical_level0_px
.height
);
2057 info
->surf
.logical_level0_px
.height
= size
;
2058 info
->surf
.phys_level0_sa
.height
= size
* px_size_sa
.h
;
2062 shrink_surfaces(const struct isl_device
*dev
,
2063 struct blorp_params
*params
,
2064 struct brw_blorp_blit_prog_key
*wm_prog_key
,
2065 struct blt_coords
*coords
)
2067 /* Shrink source surface */
2068 shrink_surface_params(dev
, ¶ms
->src
, &coords
->x
.src0
, &coords
->x
.src1
,
2069 &coords
->y
.src0
, &coords
->y
.src1
);
2070 wm_prog_key
->need_src_offset
= false;
2072 /* Shrink destination surface */
2073 shrink_surface_params(dev
, ¶ms
->dst
, &coords
->x
.dst0
, &coords
->x
.dst1
,
2074 &coords
->y
.dst0
, &coords
->y
.dst1
);
2075 wm_prog_key
->need_dst_offset
= false;
2079 do_blorp_blit(struct blorp_batch
*batch
,
2080 const struct blorp_params
*orig_params
,
2081 struct brw_blorp_blit_prog_key
*wm_prog_key
,
2082 const struct blt_coords
*orig
)
2084 struct blorp_params params
;
2085 struct blt_coords blit_coords
;
2086 struct blt_coords split_coords
= *orig
;
2087 double w
= orig
->x
.dst1
- orig
->x
.dst0
;
2088 double h
= orig
->y
.dst1
- orig
->y
.dst0
;
2089 double x_scale
= (orig
->x
.src1
- orig
->x
.src0
) / w
;
2090 double y_scale
= (orig
->y
.src1
- orig
->y
.src0
) / h
;
2096 bool x_done
, y_done
;
2097 bool shrink
= split_blorp_blit_debug
&& can_shrink_surfaces(orig_params
);
2099 params
= *orig_params
;
2100 blit_coords
= split_coords
;
2102 shrink_surfaces(batch
->blorp
->isl_dev
, ¶ms
, wm_prog_key
,
2104 enum blit_shrink_status result
=
2105 try_blorp_blit(batch
, ¶ms
, wm_prog_key
, &blit_coords
);
2107 if (result
& BLIT_WIDTH_SHRINK
) {
2110 split_coords
.x
.dst1
= MIN2(split_coords
.x
.dst0
+ w
, orig
->x
.dst1
);
2111 adjust_split_source_coords(&orig
->x
, &split_coords
.x
, x_scale
);
2113 if (result
& BLIT_HEIGHT_SHRINK
) {
2116 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2117 adjust_split_source_coords(&orig
->y
, &split_coords
.y
, y_scale
);
2121 assert(can_shrink_surfaces(orig_params
));
2126 y_done
= (orig
->y
.dst1
- split_coords
.y
.dst1
< 0.5);
2127 x_done
= y_done
&& (orig
->x
.dst1
- split_coords
.x
.dst1
< 0.5);
2130 } else if (y_done
) {
2131 split_coords
.x
.dst0
+= w
;
2132 split_coords
.x
.dst1
= MIN2(split_coords
.x
.dst0
+ w
, orig
->x
.dst1
);
2133 split_coords
.y
.dst0
= orig
->y
.dst0
;
2134 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2135 adjust_split_source_coords(&orig
->x
, &split_coords
.x
, x_scale
);
2137 split_coords
.y
.dst0
+= h
;
2138 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2139 adjust_split_source_coords(&orig
->y
, &split_coords
.y
, y_scale
);
2145 blorp_blit(struct blorp_batch
*batch
,
2146 const struct blorp_surf
*src_surf
,
2147 unsigned src_level
, unsigned src_layer
,
2148 enum isl_format src_format
, struct isl_swizzle src_swizzle
,
2149 const struct blorp_surf
*dst_surf
,
2150 unsigned dst_level
, unsigned dst_layer
,
2151 enum isl_format dst_format
, struct isl_swizzle dst_swizzle
,
2152 float src_x0
, float src_y0
,
2153 float src_x1
, float src_y1
,
2154 float dst_x0
, float dst_y0
,
2155 float dst_x1
, float dst_y1
,
2156 GLenum filter
, bool mirror_x
, bool mirror_y
)
2158 struct blorp_params params
;
2159 blorp_params_init(¶ms
);
2161 /* We cannot handle combined depth and stencil. */
2162 if (src_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
)
2163 assert(src_surf
->surf
->format
== ISL_FORMAT_R8_UINT
);
2164 if (dst_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
)
2165 assert(dst_surf
->surf
->format
== ISL_FORMAT_R8_UINT
);
2167 if (dst_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
) {
2168 assert(src_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
);
2169 /* Prior to Broadwell, we can't render to R8_UINT */
2170 if (batch
->blorp
->isl_dev
->info
->gen
< 8) {
2171 src_format
= ISL_FORMAT_R8_UNORM
;
2172 dst_format
= ISL_FORMAT_R8_UNORM
;
2176 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
2177 src_layer
, src_format
, false);
2178 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
2179 dst_layer
, dst_format
, true);
2181 params
.src
.view
.swizzle
= src_swizzle
;
2182 params
.dst
.view
.swizzle
= dst_swizzle
;
2184 struct brw_blorp_blit_prog_key wm_prog_key
= {
2185 .shader_type
= BLORP_SHADER_TYPE_BLIT
2188 /* Scaled blitting or not. */
2189 wm_prog_key
.blit_scaled
=
2190 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
2191 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
2193 /* Scaling factors used for bilinear filtering in multisample scaled
2196 if (params
.src
.surf
.samples
== 16)
2197 wm_prog_key
.x_scale
= 4.0f
;
2199 wm_prog_key
.x_scale
= 2.0f
;
2200 wm_prog_key
.y_scale
= params
.src
.surf
.samples
/ wm_prog_key
.x_scale
;
2202 if (filter
== GL_LINEAR
&&
2203 params
.src
.surf
.samples
<= 1 && params
.dst
.surf
.samples
<= 1) {
2204 wm_prog_key
.bilinear_filter
= true;
2207 if ((params
.src
.surf
.usage
& ISL_SURF_USAGE_DEPTH_BIT
) == 0 &&
2208 (params
.src
.surf
.usage
& ISL_SURF_USAGE_STENCIL_BIT
) == 0 &&
2209 !isl_format_has_int_channel(params
.src
.surf
.format
) &&
2210 params
.src
.surf
.samples
> 1 && params
.dst
.surf
.samples
<= 1) {
2211 /* We are downsampling a non-integer color buffer, so blend.
2213 * Regarding integer color buffers, the OpenGL ES 3.2 spec says:
2215 * "If the source formats are integer types or stencil values, a
2216 * single sample's value is selected for each pixel."
2218 * This implies we should not blend in that case.
2220 wm_prog_key
.blend
= true;
2223 params
.wm_inputs
.rect_grid
.x1
=
2224 minify(params
.src
.surf
.logical_level0_px
.width
, src_level
) *
2225 wm_prog_key
.x_scale
- 1.0f
;
2226 params
.wm_inputs
.rect_grid
.y1
=
2227 minify(params
.src
.surf
.logical_level0_px
.height
, src_level
) *
2228 wm_prog_key
.y_scale
- 1.0f
;
2230 struct blt_coords coords
= {
2247 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
, &coords
);
2250 static enum isl_format
2251 get_copy_format_for_bpb(const struct isl_device
*isl_dev
, unsigned bpb
)
2253 /* The choice of UNORM and UINT formats is very intentional here. Most
2254 * of the time, we want to use a UINT format to avoid any rounding error
2255 * in the blit. For stencil blits, R8_UINT is required by the hardware.
2256 * (It's the only format allowed in conjunction with W-tiling.) Also we
2257 * intentionally use the 4-channel formats whenever we can. This is so
2258 * that, when we do a RGB <-> RGBX copy, the two formats will line up
2259 * even though one of them is 3/4 the size of the other. The choice of
2260 * UNORM vs. UINT is also very intentional because we don't have 8 or
2261 * 16-bit RGB UINT formats until Sky Lake so we have to use UNORM there.
2262 * Fortunately, the only time we should ever use two different formats in
2263 * the table below is for RGB -> RGBA blits and so we will never have any
2264 * UNORM/UINT mismatch.
2266 if (ISL_DEV_GEN(isl_dev
) >= 9) {
2268 case 8: return ISL_FORMAT_R8_UINT
;
2269 case 16: return ISL_FORMAT_R8G8_UINT
;
2270 case 24: return ISL_FORMAT_R8G8B8_UINT
;
2271 case 32: return ISL_FORMAT_R8G8B8A8_UINT
;
2272 case 48: return ISL_FORMAT_R16G16B16_UINT
;
2273 case 64: return ISL_FORMAT_R16G16B16A16_UINT
;
2274 case 96: return ISL_FORMAT_R32G32B32_UINT
;
2275 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
2277 unreachable("Unknown format bpb");
2281 case 8: return ISL_FORMAT_R8_UINT
;
2282 case 16: return ISL_FORMAT_R8G8_UINT
;
2283 case 24: return ISL_FORMAT_R8G8B8_UNORM
;
2284 case 32: return ISL_FORMAT_R8G8B8A8_UNORM
;
2285 case 48: return ISL_FORMAT_R16G16B16_UNORM
;
2286 case 64: return ISL_FORMAT_R16G16B16A16_UNORM
;
2287 case 96: return ISL_FORMAT_R32G32B32_UINT
;
2288 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
2290 unreachable("Unknown format bpb");
2295 /** Returns a UINT format that is CCS-compatible with the given format
2297 * The PRM's say absolutely nothing about how render compression works. The
2298 * only thing they provide is a list of formats on which it is and is not
2299 * supported. Empirical testing indicates that the compression is only based
2300 * on the bit-layout of the format and the channel encoding doesn't matter.
2301 * So, while texture views don't work in general, you can create a view as
2302 * long as the bit-layout of the formats are the same.
2304 * Fortunately, for every render compression capable format, the UINT format
2305 * with the same bit layout also supports render compression. This means that
2306 * we only need to handle UINT formats for copy operations. In order to do
2307 * copies between formats with different bit layouts, we attach both with a
2308 * UINT format and use bit_cast_color() to generate code to do the bit-cast
2309 * operation between the two bit layouts.
2311 static enum isl_format
2312 get_ccs_compatible_uint_format(const struct isl_format_layout
*fmtl
)
2314 switch (fmtl
->format
) {
2315 case ISL_FORMAT_R32G32B32A32_FLOAT
:
2316 case ISL_FORMAT_R32G32B32A32_SINT
:
2317 case ISL_FORMAT_R32G32B32A32_UINT
:
2318 case ISL_FORMAT_R32G32B32A32_UNORM
:
2319 case ISL_FORMAT_R32G32B32A32_SNORM
:
2320 case ISL_FORMAT_R32G32B32X32_FLOAT
:
2321 return ISL_FORMAT_R32G32B32A32_UINT
;
2323 case ISL_FORMAT_R16G16B16A16_UNORM
:
2324 case ISL_FORMAT_R16G16B16A16_SNORM
:
2325 case ISL_FORMAT_R16G16B16A16_SINT
:
2326 case ISL_FORMAT_R16G16B16A16_UINT
:
2327 case ISL_FORMAT_R16G16B16A16_FLOAT
:
2328 case ISL_FORMAT_R16G16B16X16_UNORM
:
2329 case ISL_FORMAT_R16G16B16X16_FLOAT
:
2330 return ISL_FORMAT_R16G16B16A16_UINT
;
2332 case ISL_FORMAT_R32G32_FLOAT
:
2333 case ISL_FORMAT_R32G32_SINT
:
2334 case ISL_FORMAT_R32G32_UINT
:
2335 case ISL_FORMAT_R32G32_UNORM
:
2336 case ISL_FORMAT_R32G32_SNORM
:
2337 return ISL_FORMAT_R32G32_UINT
;
2339 case ISL_FORMAT_B8G8R8A8_UNORM
:
2340 case ISL_FORMAT_B8G8R8A8_UNORM_SRGB
:
2341 case ISL_FORMAT_R8G8B8A8_UNORM
:
2342 case ISL_FORMAT_R8G8B8A8_UNORM_SRGB
:
2343 case ISL_FORMAT_R8G8B8A8_SNORM
:
2344 case ISL_FORMAT_R8G8B8A8_SINT
:
2345 case ISL_FORMAT_R8G8B8A8_UINT
:
2346 case ISL_FORMAT_B8G8R8X8_UNORM
:
2347 case ISL_FORMAT_B8G8R8X8_UNORM_SRGB
:
2348 case ISL_FORMAT_R8G8B8X8_UNORM
:
2349 case ISL_FORMAT_R8G8B8X8_UNORM_SRGB
:
2350 return ISL_FORMAT_R8G8B8A8_UINT
;
2352 case ISL_FORMAT_R16G16_UNORM
:
2353 case ISL_FORMAT_R16G16_SNORM
:
2354 case ISL_FORMAT_R16G16_SINT
:
2355 case ISL_FORMAT_R16G16_UINT
:
2356 case ISL_FORMAT_R16G16_FLOAT
:
2357 return ISL_FORMAT_R16G16_UINT
;
2359 case ISL_FORMAT_R32_SINT
:
2360 case ISL_FORMAT_R32_UINT
:
2361 case ISL_FORMAT_R32_FLOAT
:
2362 case ISL_FORMAT_R32_UNORM
:
2363 case ISL_FORMAT_R32_SNORM
:
2364 return ISL_FORMAT_R32_UINT
;
2367 unreachable("Not a compressible format");
2372 blorp_surf_convert_to_uncompressed(const struct isl_device
*isl_dev
,
2373 struct brw_blorp_surface_info
*info
,
2374 uint32_t *x
, uint32_t *y
,
2375 uint32_t *width
, uint32_t *height
)
2377 const struct isl_format_layout
*fmtl
=
2378 isl_format_get_layout(info
->surf
.format
);
2380 assert(fmtl
->bw
> 1 || fmtl
->bh
> 1);
2382 /* This is a compressed surface. We need to convert it to a single
2383 * slice (because compressed layouts don't perfectly match uncompressed
2384 * ones with the same bpb) and divide x, y, width, and height by the
2387 blorp_surf_convert_to_single_slice(isl_dev
, info
);
2389 if (width
&& height
) {
2391 uint32_t right_edge_px
= info
->tile_x_sa
+ *x
+ *width
;
2392 uint32_t bottom_edge_px
= info
->tile_y_sa
+ *y
+ *height
;
2393 assert(*width
% fmtl
->bw
== 0 ||
2394 right_edge_px
== info
->surf
.logical_level0_px
.width
);
2395 assert(*height
% fmtl
->bh
== 0 ||
2396 bottom_edge_px
== info
->surf
.logical_level0_px
.height
);
2398 *width
= DIV_ROUND_UP(*width
, fmtl
->bw
);
2399 *height
= DIV_ROUND_UP(*height
, fmtl
->bh
);
2403 assert(*x
% fmtl
->bw
== 0);
2404 assert(*y
% fmtl
->bh
== 0);
2409 info
->surf
.logical_level0_px
.width
=
2410 DIV_ROUND_UP(info
->surf
.logical_level0_px
.width
, fmtl
->bw
);
2411 info
->surf
.logical_level0_px
.height
=
2412 DIV_ROUND_UP(info
->surf
.logical_level0_px
.height
, fmtl
->bh
);
2414 assert(info
->surf
.phys_level0_sa
.width
% fmtl
->bw
== 0);
2415 assert(info
->surf
.phys_level0_sa
.height
% fmtl
->bh
== 0);
2416 info
->surf
.phys_level0_sa
.width
/= fmtl
->bw
;
2417 info
->surf
.phys_level0_sa
.height
/= fmtl
->bh
;
2419 assert(info
->tile_x_sa
% fmtl
->bw
== 0);
2420 assert(info
->tile_y_sa
% fmtl
->bh
== 0);
2421 info
->tile_x_sa
/= fmtl
->bw
;
2422 info
->tile_y_sa
/= fmtl
->bh
;
2424 /* It's now an uncompressed surface so we need an uncompressed format */
2425 info
->surf
.format
= get_copy_format_for_bpb(isl_dev
, fmtl
->bpb
);
2429 blorp_copy(struct blorp_batch
*batch
,
2430 const struct blorp_surf
*src_surf
,
2431 unsigned src_level
, unsigned src_layer
,
2432 const struct blorp_surf
*dst_surf
,
2433 unsigned dst_level
, unsigned dst_layer
,
2434 uint32_t src_x
, uint32_t src_y
,
2435 uint32_t dst_x
, uint32_t dst_y
,
2436 uint32_t src_width
, uint32_t src_height
)
2438 const struct isl_device
*isl_dev
= batch
->blorp
->isl_dev
;
2439 struct blorp_params params
;
2441 if (src_width
== 0 || src_height
== 0)
2444 blorp_params_init(¶ms
);
2445 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
2446 src_layer
, ISL_FORMAT_UNSUPPORTED
, false);
2447 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
2448 dst_layer
, ISL_FORMAT_UNSUPPORTED
, true);
2450 struct brw_blorp_blit_prog_key wm_prog_key
= {
2451 .shader_type
= BLORP_SHADER_TYPE_BLIT
2454 const struct isl_format_layout
*src_fmtl
=
2455 isl_format_get_layout(params
.src
.surf
.format
);
2456 const struct isl_format_layout
*dst_fmtl
=
2457 isl_format_get_layout(params
.dst
.surf
.format
);
2459 assert(params
.src
.aux_usage
== ISL_AUX_USAGE_NONE
||
2460 params
.src
.aux_usage
== ISL_AUX_USAGE_MCS
||
2461 params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
);
2462 assert(params
.dst
.aux_usage
== ISL_AUX_USAGE_NONE
||
2463 params
.dst
.aux_usage
== ISL_AUX_USAGE_MCS
||
2464 params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
);
2466 if (params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2467 params
.dst
.view
.format
= get_ccs_compatible_uint_format(dst_fmtl
);
2468 if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2469 params
.src
.view
.format
= get_ccs_compatible_uint_format(src_fmtl
);
2470 } else if (src_fmtl
->bpb
== dst_fmtl
->bpb
) {
2471 params
.src
.view
.format
= params
.dst
.view
.format
;
2473 params
.src
.view
.format
=
2474 get_copy_format_for_bpb(isl_dev
, src_fmtl
->bpb
);
2476 } else if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2477 params
.src
.view
.format
= get_ccs_compatible_uint_format(src_fmtl
);
2478 if (src_fmtl
->bpb
== dst_fmtl
->bpb
) {
2479 params
.dst
.view
.format
= params
.src
.view
.format
;
2481 params
.dst
.view
.format
=
2482 get_copy_format_for_bpb(isl_dev
, dst_fmtl
->bpb
);
2485 params
.dst
.view
.format
= get_copy_format_for_bpb(isl_dev
, dst_fmtl
->bpb
);
2486 params
.src
.view
.format
= get_copy_format_for_bpb(isl_dev
, src_fmtl
->bpb
);
2489 if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2490 /* It's safe to do a blorp_copy between things which are sRGB with CCS_E
2491 * enabled even though CCS_E doesn't technically do sRGB on SKL because
2492 * we stomp everything to UINT anyway. The one thing we have to be
2493 * careful of is clear colors. Because fast clear colors for sRGB on
2494 * gen9 are encoded as the float values between format conversion and
2495 * sRGB curve application, a given clear color float will convert to the
2496 * same bits regardless of whether the format is UNORM or sRGB.
2497 * Therefore, we can handle sRGB without any special cases.
2499 UNUSED
enum isl_format linear_src_format
=
2500 isl_format_srgb_to_linear(src_surf
->surf
->format
);
2501 assert(isl_formats_are_ccs_e_compatible(batch
->blorp
->isl_dev
->info
,
2503 params
.src
.view
.format
));
2505 isl_color_value_pack(¶ms
.src
.clear_color
,
2506 params
.src
.surf
.format
, packed
);
2507 isl_color_value_unpack(¶ms
.src
.clear_color
,
2508 params
.src
.view
.format
, packed
);
2511 if (params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2512 /* See above where we handle linear_src_format */
2513 UNUSED
enum isl_format linear_dst_format
=
2514 isl_format_srgb_to_linear(dst_surf
->surf
->format
);
2515 assert(isl_formats_are_ccs_e_compatible(batch
->blorp
->isl_dev
->info
,
2517 params
.dst
.view
.format
));
2519 isl_color_value_pack(¶ms
.dst
.clear_color
,
2520 params
.dst
.surf
.format
, packed
);
2521 isl_color_value_unpack(¶ms
.dst
.clear_color
,
2522 params
.dst
.view
.format
, packed
);
2525 wm_prog_key
.src_bpc
=
2526 isl_format_get_layout(params
.src
.view
.format
)->channels
.r
.bits
;
2527 wm_prog_key
.dst_bpc
=
2528 isl_format_get_layout(params
.dst
.view
.format
)->channels
.r
.bits
;
2530 if (src_fmtl
->bw
> 1 || src_fmtl
->bh
> 1) {
2531 blorp_surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.src
,
2533 &src_width
, &src_height
);
2534 wm_prog_key
.need_src_offset
= true;
2537 if (dst_fmtl
->bw
> 1 || dst_fmtl
->bh
> 1) {
2538 blorp_surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.dst
,
2539 &dst_x
, &dst_y
, NULL
, NULL
);
2540 wm_prog_key
.need_dst_offset
= true;
2543 /* Once both surfaces are stompped to uncompressed as needed, the
2544 * destination size is the same as the source size.
2546 uint32_t dst_width
= src_width
;
2547 uint32_t dst_height
= src_height
;
2549 struct blt_coords coords
= {
2552 .src1
= src_x
+ src_width
,
2554 .dst1
= dst_x
+ dst_width
,
2559 .src1
= src_y
+ src_height
,
2561 .dst1
= dst_y
+ dst_height
,
2566 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
, &coords
);
2569 static enum isl_format
2570 isl_format_for_size(unsigned size_B
)
2573 case 1: return ISL_FORMAT_R8_UINT
;
2574 case 2: return ISL_FORMAT_R8G8_UINT
;
2575 case 4: return ISL_FORMAT_R8G8B8A8_UINT
;
2576 case 8: return ISL_FORMAT_R16G16B16A16_UINT
;
2577 case 16: return ISL_FORMAT_R32G32B32A32_UINT
;
2579 unreachable("Not a power-of-two format size");
2584 * Returns the greatest common divisor of a and b that is a power of two.
2587 gcd_pow2_u64(uint64_t a
, uint64_t b
)
2589 assert(a
> 0 || b
> 0);
2591 unsigned a_log2
= ffsll(a
) - 1;
2592 unsigned b_log2
= ffsll(b
) - 1;
2594 /* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
2595 * case, the MIN2() will take the other one. If both are 0 then we will
2596 * hit the assert above.
2598 return 1 << MIN2(a_log2
, b_log2
);
2602 do_buffer_copy(struct blorp_batch
*batch
,
2603 struct blorp_address
*src
,
2604 struct blorp_address
*dst
,
2605 int width
, int height
, int block_size
)
2607 /* The actual format we pick doesn't matter as blorp will throw it away.
2608 * The only thing that actually matters is the size.
2610 enum isl_format format
= isl_format_for_size(block_size
);
2613 struct isl_surf surf
;
2614 ok
= isl_surf_init(batch
->blorp
->isl_dev
, &surf
,
2615 .dim
= ISL_SURF_DIM_2D
,
2623 .row_pitch
= width
* block_size
,
2624 .usage
= ISL_SURF_USAGE_TEXTURE_BIT
|
2625 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
2626 .tiling_flags
= ISL_TILING_LINEAR_BIT
);
2629 struct blorp_surf src_blorp_surf
= {
2634 struct blorp_surf dst_blorp_surf
= {
2639 blorp_copy(batch
, &src_blorp_surf
, 0, 0, &dst_blorp_surf
, 0, 0,
2640 0, 0, 0, 0, width
, height
);
2644 blorp_buffer_copy(struct blorp_batch
*batch
,
2645 struct blorp_address src
,
2646 struct blorp_address dst
,
2649 const struct gen_device_info
*devinfo
= batch
->blorp
->isl_dev
->info
;
2650 uint64_t copy_size
= size
;
2652 /* This is maximum possible width/height our HW can handle */
2653 uint64_t max_surface_dim
= 1 << (devinfo
->gen
>= 7 ? 14 : 13);
2655 /* First, we compute the biggest format that can be used with the
2656 * given offsets and size.
2659 bs
= gcd_pow2_u64(bs
, src
.offset
);
2660 bs
= gcd_pow2_u64(bs
, dst
.offset
);
2661 bs
= gcd_pow2_u64(bs
, size
);
2663 /* First, we make a bunch of max-sized copies */
2664 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
2665 while (copy_size
>= max_copy_size
) {
2666 do_buffer_copy(batch
, &src
, &dst
, max_surface_dim
, max_surface_dim
, bs
);
2667 copy_size
-= max_copy_size
;
2668 src
.offset
+= max_copy_size
;
2669 dst
.offset
+= max_copy_size
;
2672 /* Now make a max-width copy */
2673 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
2674 assert(height
< max_surface_dim
);
2676 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
2677 do_buffer_copy(batch
, &src
, &dst
, max_surface_dim
, height
, bs
);
2678 copy_size
-= rect_copy_size
;
2679 src
.offset
+= rect_copy_size
;
2680 dst
.offset
+= rect_copy_size
;
2683 /* Finally, make a small copy to finish it off */
2684 if (copy_size
!= 0) {
2685 do_buffer_copy(batch
, &src
, &dst
, copy_size
/ bs
, 1, bs
);