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"
32 #include "util/u_math.h"
34 #define FILE_DEBUG_FLAG DEBUG_BLORP
36 static const bool split_blorp_blit_debug
= false;
39 * Enum to specify the order of arguments in a sampler message
41 enum sampler_message_arg
43 SAMPLER_MESSAGE_ARG_U_FLOAT
,
44 SAMPLER_MESSAGE_ARG_V_FLOAT
,
45 SAMPLER_MESSAGE_ARG_U_INT
,
46 SAMPLER_MESSAGE_ARG_V_INT
,
47 SAMPLER_MESSAGE_ARG_R_INT
,
48 SAMPLER_MESSAGE_ARG_SI_INT
,
49 SAMPLER_MESSAGE_ARG_MCS_INT
,
50 SAMPLER_MESSAGE_ARG_ZERO_INT
,
53 struct brw_blorp_blit_vars
{
54 /* Input values from brw_blorp_wm_inputs */
55 nir_variable
*v_discard_rect
;
56 nir_variable
*v_rect_grid
;
57 nir_variable
*v_coord_transform
;
58 nir_variable
*v_src_z
;
59 nir_variable
*v_src_offset
;
60 nir_variable
*v_dst_offset
;
61 nir_variable
*v_src_inv_size
;
64 nir_variable
*frag_coord
;
67 nir_variable
*color_out
;
71 brw_blorp_blit_vars_init(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
72 const struct brw_blorp_blit_prog_key
*key
)
74 #define LOAD_INPUT(name, type)\
75 v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
77 LOAD_INPUT(discard_rect
, glsl_vec4_type())
78 LOAD_INPUT(rect_grid
, glsl_vec4_type())
79 LOAD_INPUT(coord_transform
, glsl_vec4_type())
80 LOAD_INPUT(src_z
, glsl_uint_type())
81 LOAD_INPUT(src_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
82 LOAD_INPUT(dst_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
83 LOAD_INPUT(src_inv_size
, glsl_vector_type(GLSL_TYPE_FLOAT
, 2))
87 v
->frag_coord
= nir_variable_create(b
->shader
, nir_var_shader_in
,
88 glsl_vec4_type(), "gl_FragCoord");
89 v
->frag_coord
->data
.location
= VARYING_SLOT_POS
;
91 v
->color_out
= nir_variable_create(b
->shader
, nir_var_shader_out
,
92 glsl_vec4_type(), "gl_FragColor");
93 v
->color_out
->data
.location
= FRAG_RESULT_COLOR
;
97 blorp_blit_get_frag_coords(nir_builder
*b
,
98 const struct brw_blorp_blit_prog_key
*key
,
99 struct brw_blorp_blit_vars
*v
)
101 nir_ssa_def
*coord
= nir_f2i32(b
, nir_load_var(b
, v
->frag_coord
));
103 /* Account for destination surface intratile offset
105 * Transformation parameters giving translation from destination to source
106 * coordinates don't take into account possible intra-tile destination
107 * offset. Therefore it has to be first subtracted from the incoming
108 * coordinates. Vertices are set up based on coordinates containing the
111 if (key
->need_dst_offset
)
112 coord
= nir_isub(b
, coord
, nir_load_var(b
, v
->v_dst_offset
));
114 if (key
->persample_msaa_dispatch
) {
115 return nir_vec3(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1),
116 nir_load_sample_id(b
));
118 return nir_vec2(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1));
123 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
127 blorp_blit_apply_transform(nir_builder
*b
, nir_ssa_def
*src_pos
,
128 struct brw_blorp_blit_vars
*v
)
130 nir_ssa_def
*coord_transform
= nir_load_var(b
, v
->v_coord_transform
);
132 nir_ssa_def
*offset
= nir_vec2(b
, nir_channel(b
, coord_transform
, 1),
133 nir_channel(b
, coord_transform
, 3));
134 nir_ssa_def
*mul
= nir_vec2(b
, nir_channel(b
, coord_transform
, 0),
135 nir_channel(b
, coord_transform
, 2));
137 return nir_fadd(b
, nir_fmul(b
, src_pos
, mul
), offset
);
141 blorp_nir_discard_if_outside_rect(nir_builder
*b
, nir_ssa_def
*pos
,
142 struct brw_blorp_blit_vars
*v
)
144 nir_ssa_def
*c0
, *c1
, *c2
, *c3
;
145 nir_ssa_def
*discard_rect
= nir_load_var(b
, v
->v_discard_rect
);
146 nir_ssa_def
*dst_x0
= nir_channel(b
, discard_rect
, 0);
147 nir_ssa_def
*dst_x1
= nir_channel(b
, discard_rect
, 1);
148 nir_ssa_def
*dst_y0
= nir_channel(b
, discard_rect
, 2);
149 nir_ssa_def
*dst_y1
= nir_channel(b
, discard_rect
, 3);
151 c0
= nir_ult(b
, nir_channel(b
, pos
, 0), dst_x0
);
152 c1
= nir_uge(b
, nir_channel(b
, pos
, 0), dst_x1
);
153 c2
= nir_ult(b
, nir_channel(b
, pos
, 1), dst_y0
);
154 c3
= nir_uge(b
, nir_channel(b
, pos
, 1), dst_y1
);
156 nir_ssa_def
*oob
= nir_ior(b
, nir_ior(b
, c0
, c1
), nir_ior(b
, c2
, c3
));
158 nir_intrinsic_instr
*discard
=
159 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard_if
);
160 discard
->src
[0] = nir_src_for_ssa(oob
);
161 nir_builder_instr_insert(b
, &discard
->instr
);
164 static nir_tex_instr
*
165 blorp_create_nir_tex_instr(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
166 nir_texop op
, nir_ssa_def
*pos
, unsigned num_srcs
,
167 nir_alu_type dst_type
)
169 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, num_srcs
);
173 tex
->dest_type
= dst_type
;
174 tex
->is_array
= false;
175 tex
->is_shadow
= false;
177 /* Blorp only has one texture and it's bound at unit 0 */
178 tex
->texture_index
= 0;
179 tex
->sampler_index
= 0;
181 /* To properly handle 3-D and 2-D array textures, we pull the Z component
182 * from an input. TODO: This is a bit magic; we should probably make this
183 * more explicit in the future.
185 assert(pos
->num_components
>= 2);
186 pos
= nir_vec3(b
, nir_channel(b
, pos
, 0), nir_channel(b
, pos
, 1),
187 nir_load_var(b
, v
->v_src_z
));
189 tex
->src
[0].src_type
= nir_tex_src_coord
;
190 tex
->src
[0].src
= nir_src_for_ssa(pos
);
191 tex
->coord_components
= 3;
193 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
199 blorp_nir_tex(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
200 const struct brw_blorp_blit_prog_key
*key
, nir_ssa_def
*pos
)
202 if (key
->need_src_offset
)
203 pos
= nir_fadd(b
, pos
, nir_i2f32(b
, nir_load_var(b
, v
->v_src_offset
)));
205 /* If the sampler requires normalized coordinates, we need to compensate. */
206 if (key
->src_coords_normalized
)
207 pos
= nir_fmul(b
, pos
, nir_load_var(b
, v
->v_src_inv_size
));
210 blorp_create_nir_tex_instr(b
, v
, nir_texop_tex
, pos
, 2,
211 key
->texture_data_type
);
213 assert(pos
->num_components
== 2);
214 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
215 tex
->src
[1].src_type
= nir_tex_src_lod
;
216 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
218 nir_builder_instr_insert(b
, &tex
->instr
);
220 return &tex
->dest
.ssa
;
224 blorp_nir_txf(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
225 nir_ssa_def
*pos
, nir_alu_type dst_type
)
228 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf
, pos
, 2, dst_type
);
230 tex
->sampler_dim
= GLSL_SAMPLER_DIM_3D
;
231 tex
->src
[1].src_type
= nir_tex_src_lod
;
232 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
234 nir_builder_instr_insert(b
, &tex
->instr
);
236 return &tex
->dest
.ssa
;
240 blorp_nir_txf_ms(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
241 nir_ssa_def
*pos
, nir_ssa_def
*mcs
, nir_alu_type dst_type
)
244 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms
, pos
,
245 mcs
!= NULL
? 3 : 2, dst_type
);
247 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
249 tex
->src
[1].src_type
= nir_tex_src_ms_index
;
250 if (pos
->num_components
== 2) {
251 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
253 assert(pos
->num_components
== 3);
254 tex
->src
[1].src
= nir_src_for_ssa(nir_channel(b
, pos
, 2));
258 tex
->src
[2].src_type
= nir_tex_src_ms_mcs
;
259 tex
->src
[2].src
= nir_src_for_ssa(mcs
);
262 nir_builder_instr_insert(b
, &tex
->instr
);
264 return &tex
->dest
.ssa
;
268 blorp_blit_txf_ms_mcs(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
272 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms_mcs
,
273 pos
, 1, nir_type_int
);
275 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
277 nir_builder_instr_insert(b
, &tex
->instr
);
279 return &tex
->dest
.ssa
;
283 * Emit code to compensate for the difference between Y and W tiling.
285 * This code modifies the X and Y coordinates according to the formula:
287 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
289 * (See brw_blorp_build_nir_shader).
291 static inline nir_ssa_def
*
292 blorp_nir_retile_y_to_w(nir_builder
*b
, nir_ssa_def
*pos
)
294 assert(pos
->num_components
== 2);
295 nir_ssa_def
*x_Y
= nir_channel(b
, pos
, 0);
296 nir_ssa_def
*y_Y
= nir_channel(b
, pos
, 1);
298 /* Given X and Y coordinates that describe an address using Y tiling,
299 * translate to the X and Y coordinates that describe the same address
302 * If we break down the low order bits of X and Y, using a
303 * single letter to represent each low-order bit:
305 * X = A << 7 | 0bBCDEFGH
306 * Y = J << 5 | 0bKLMNP (1)
308 * Then we can apply the Y tiling formula to see the memory offset being
311 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
313 * If we apply the W detiling formula to this memory location, that the
314 * corresponding X' and Y' coordinates are:
316 * X' = A << 6 | 0bBCDPFH (3)
317 * Y' = J << 6 | 0bKLMNEG
319 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
320 * we need to make the following computation:
322 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
323 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
325 nir_ssa_def
*x_W
= nir_imm_int(b
, 0);
326 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0xfffffff4, -1);
327 x_W
= nir_mask_shift_or(b
, x_W
, y_Y
, 0x1, 2);
328 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0x1, 0);
330 nir_ssa_def
*y_W
= nir_imm_int(b
, 0);
331 y_W
= nir_mask_shift_or(b
, y_W
, y_Y
, 0xfffffffe, 1);
332 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x8, -2);
333 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x2, -1);
335 return nir_vec2(b
, x_W
, y_W
);
339 * Emit code to compensate for the difference between Y and W tiling.
341 * This code modifies the X and Y coordinates according to the formula:
343 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
345 * (See brw_blorp_build_nir_shader).
347 static inline nir_ssa_def
*
348 blorp_nir_retile_w_to_y(nir_builder
*b
, nir_ssa_def
*pos
)
350 assert(pos
->num_components
== 2);
351 nir_ssa_def
*x_W
= nir_channel(b
, pos
, 0);
352 nir_ssa_def
*y_W
= nir_channel(b
, pos
, 1);
354 /* Applying the same logic as above, but in reverse, we obtain the
357 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
358 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
360 nir_ssa_def
*x_Y
= nir_imm_int(b
, 0);
361 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0xfffffffa, 1);
362 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x2, 2);
363 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x1, 1);
364 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0x1, 0);
366 nir_ssa_def
*y_Y
= nir_imm_int(b
, 0);
367 y_Y
= nir_mask_shift_or(b
, y_Y
, y_W
, 0xfffffffc, -1);
368 y_Y
= nir_mask_shift_or(b
, y_Y
, x_W
, 0x4, -2);
370 return nir_vec2(b
, x_Y
, y_Y
);
374 * Emit code to compensate for the difference between MSAA and non-MSAA
377 * This code modifies the X and Y coordinates according to the formula:
379 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
381 * (See brw_blorp_blit_program).
383 static inline nir_ssa_def
*
384 blorp_nir_encode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
385 unsigned num_samples
, enum isl_msaa_layout layout
)
387 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
390 case ISL_MSAA_LAYOUT_NONE
:
391 assert(pos
->num_components
== 2);
393 case ISL_MSAA_LAYOUT_ARRAY
:
394 /* No translation needed */
396 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
397 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
398 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
399 nir_ssa_def
*s_in
= pos
->num_components
== 2 ? nir_imm_int(b
, 0) :
400 nir_channel(b
, pos
, 2);
402 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
403 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
404 switch (num_samples
) {
407 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
408 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
411 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
412 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
413 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
415 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 1);
416 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
417 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
418 if (num_samples
== 2) {
421 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
422 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
423 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
428 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
429 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
431 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
433 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
434 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
435 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
436 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
437 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
438 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
439 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
443 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
444 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
446 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
449 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
450 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
451 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
452 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
453 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 2);
454 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x8, -1);
455 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
456 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
460 unreachable("Invalid number of samples for IMS layout");
463 return nir_vec2(b
, x_out
, y_out
);
467 unreachable("Invalid MSAA layout");
472 * Emit code to compensate for the difference between MSAA and non-MSAA
475 * This code modifies the X and Y coordinates according to the formula:
477 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
479 * (See brw_blorp_blit_program).
481 static inline nir_ssa_def
*
482 blorp_nir_decode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
483 unsigned num_samples
, enum isl_msaa_layout layout
)
485 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
488 case ISL_MSAA_LAYOUT_NONE
:
489 /* No translation necessary, and S should already be zero. */
490 assert(pos
->num_components
== 2);
492 case ISL_MSAA_LAYOUT_ARRAY
:
493 /* No translation necessary. */
495 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
496 assert(pos
->num_components
== 2);
498 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
499 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
501 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
502 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
503 nir_ssa_def
*s_out
= nir_imm_int(b
, 0);
504 switch (num_samples
) {
507 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
508 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
509 * S = (X & 0b10) >> 1
511 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
512 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
513 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
514 * S = (Y & 0b10) | (X & 0b10) >> 1
516 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffc, -1);
517 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
518 if (num_samples
== 2) {
520 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
522 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
523 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
524 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
525 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
530 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
531 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
532 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
533 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
535 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
536 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
537 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
538 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
539 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
540 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
541 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
545 /* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
546 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
547 * Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
548 * S = (Y & 0b100) << 1 | (X & 0b100) |
549 * (Y & 0b10) | (X & 0b10) >> 1
551 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
552 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
553 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffff8, -2);
554 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
555 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x4, 1);
556 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
557 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
558 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
562 unreachable("Invalid number of samples for IMS layout");
565 return nir_vec3(b
, x_out
, y_out
, s_out
);
569 unreachable("Invalid MSAA layout");
574 * Count the number of trailing 1 bits in the given value. For example:
576 * count_trailing_one_bits(0) == 0
577 * count_trailing_one_bits(7) == 3
578 * count_trailing_one_bits(11) == 2
580 static inline int count_trailing_one_bits(unsigned value
)
582 #ifdef HAVE___BUILTIN_CTZ
583 return __builtin_ctz(~value
);
585 return util_bitcount(value
& ~(value
+ 1));
590 blorp_nir_combine_samples(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
591 nir_ssa_def
*pos
, unsigned tex_samples
,
592 enum isl_aux_usage tex_aux_usage
,
593 nir_alu_type dst_type
,
594 enum blorp_filter filter
)
596 /* If non-null, this is the outer-most if statement */
597 nir_if
*outer_if
= NULL
;
599 nir_variable
*color
=
600 nir_local_variable_create(b
->impl
, glsl_vec4_type(), "color");
602 nir_ssa_def
*mcs
= NULL
;
603 if (tex_aux_usage
== ISL_AUX_USAGE_MCS
)
604 mcs
= blorp_blit_txf_ms_mcs(b
, v
, pos
);
608 case BLORP_FILTER_AVERAGE
:
609 assert(dst_type
== nir_type_float
);
610 combine_op
= nir_op_fadd
;
613 case BLORP_FILTER_MIN_SAMPLE
:
615 case nir_type_int
: combine_op
= nir_op_imin
; break;
616 case nir_type_uint
: combine_op
= nir_op_umin
; break;
617 case nir_type_float
: combine_op
= nir_op_fmin
; break;
618 default: unreachable("Invalid dst_type");
622 case BLORP_FILTER_MAX_SAMPLE
:
624 case nir_type_int
: combine_op
= nir_op_imax
; break;
625 case nir_type_uint
: combine_op
= nir_op_umax
; break;
626 case nir_type_float
: combine_op
= nir_op_fmax
; break;
627 default: unreachable("Invalid dst_type");
632 unreachable("Invalid filter");
635 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
637 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
639 * This ensures that when all samples have the same value, no numerical
640 * precision is lost, since each addition operation always adds two equal
641 * values, and summing two equal floating point values does not lose
644 * We perform this computation by treating the texture_data array as a
645 * stack and performing the following operations:
647 * - push sample 0 onto stack
648 * - push sample 1 onto stack
649 * - add top two stack entries
650 * - push sample 2 onto stack
651 * - push sample 3 onto stack
652 * - add top two stack entries
653 * - add top two stack entries
654 * - divide top stack entry by 4
656 * Note that after pushing sample i onto the stack, the number of add
657 * operations we do is equal to the number of trailing 1 bits in i. This
658 * works provided the total number of samples is a power of two, which it
659 * always is for i965.
661 * For integer formats, we replace the add operations with average
662 * operations and skip the final division.
664 nir_ssa_def
*texture_data
[5];
665 unsigned stack_depth
= 0;
666 for (unsigned i
= 0; i
< tex_samples
; ++i
) {
667 assert(stack_depth
== util_bitcount(i
)); /* Loop invariant */
669 /* Push sample i onto the stack */
670 assert(stack_depth
< ARRAY_SIZE(texture_data
));
672 nir_ssa_def
*ms_pos
= nir_vec3(b
, nir_channel(b
, pos
, 0),
673 nir_channel(b
, pos
, 1),
675 texture_data
[stack_depth
++] = blorp_nir_txf_ms(b
, v
, ms_pos
, mcs
, dst_type
);
677 if (i
== 0 && tex_aux_usage
== ISL_AUX_USAGE_MCS
) {
678 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
679 * suggests an optimization:
681 * "A simple optimization with probable large return in
682 * performance is to compare the MCS value to zero (indicating
683 * all samples are on sample slice 0), and sample only from
684 * sample slice 0 using ld2dss if MCS is zero."
686 * Note that in the case where the MCS value is zero, sampling from
687 * sample slice 0 using ld2dss and sampling from sample 0 using
688 * ld2dms are equivalent (since all samples are on sample slice 0).
689 * Since we have already sampled from sample 0, all we need to do is
690 * skip the remaining fetches and averaging if MCS is zero.
692 * It's also trivial to detect when the MCS has the magic clear color
693 * value. In this case, the txf we did on sample 0 will return the
694 * clear color and we can skip the remaining fetches just like we do
697 nir_ssa_def
*mcs_zero
=
698 nir_ieq(b
, nir_channel(b
, mcs
, 0), nir_imm_int(b
, 0));
699 if (tex_samples
== 16) {
700 mcs_zero
= nir_iand(b
, mcs_zero
,
701 nir_ieq(b
, nir_channel(b
, mcs
, 1), nir_imm_int(b
, 0)));
703 nir_ssa_def
*mcs_clear
=
704 blorp_nir_mcs_is_clear_color(b
, mcs
, tex_samples
);
706 nir_if
*if_stmt
= nir_if_create(b
->shader
);
707 if_stmt
->condition
= nir_src_for_ssa(nir_ior(b
, mcs_zero
, mcs_clear
));
708 nir_cf_node_insert(b
->cursor
, &if_stmt
->cf_node
);
710 b
->cursor
= nir_after_cf_list(&if_stmt
->then_list
);
711 nir_store_var(b
, color
, texture_data
[0], 0xf);
713 b
->cursor
= nir_after_cf_list(&if_stmt
->else_list
);
717 for (int j
= 0; j
< count_trailing_one_bits(i
); j
++) {
718 assert(stack_depth
>= 2);
721 texture_data
[stack_depth
- 1] =
722 nir_build_alu(b
, combine_op
,
723 texture_data
[stack_depth
- 1],
724 texture_data
[stack_depth
],
729 /* We should have just 1 sample on the stack now. */
730 assert(stack_depth
== 1);
732 if (filter
== BLORP_FILTER_AVERAGE
) {
733 assert(dst_type
== nir_type_float
);
734 texture_data
[0] = nir_fmul(b
, texture_data
[0],
735 nir_imm_float(b
, 1.0 / tex_samples
));
738 nir_store_var(b
, color
, texture_data
[0], 0xf);
741 b
->cursor
= nir_after_cf_node(&outer_if
->cf_node
);
743 return nir_load_var(b
, color
);
747 blorp_nir_manual_blend_bilinear(nir_builder
*b
, nir_ssa_def
*pos
,
748 unsigned tex_samples
,
749 const struct brw_blorp_blit_prog_key
*key
,
750 struct brw_blorp_blit_vars
*v
)
752 nir_ssa_def
*pos_xy
= nir_channels(b
, pos
, 0x3);
753 nir_ssa_def
*rect_grid
= nir_load_var(b
, v
->v_rect_grid
);
754 nir_ssa_def
*scale
= nir_imm_vec2(b
, key
->x_scale
, key
->y_scale
);
756 /* Translate coordinates to lay out the samples in a rectangular grid
757 * roughly corresponding to sample locations.
759 pos_xy
= nir_fmul(b
, pos_xy
, scale
);
760 /* Adjust coordinates so that integers represent pixel centers rather
763 pos_xy
= nir_fadd(b
, pos_xy
, nir_imm_float(b
, -0.5));
764 /* Clamp the X, Y texture coordinates to properly handle the sampling of
765 * texels on texture edges.
767 pos_xy
= nir_fmin(b
, nir_fmax(b
, pos_xy
, nir_imm_float(b
, 0.0)),
768 nir_vec2(b
, nir_channel(b
, rect_grid
, 0),
769 nir_channel(b
, rect_grid
, 1)));
771 /* Store the fractional parts to be used as bilinear interpolation
774 nir_ssa_def
*frac_xy
= nir_ffract(b
, pos_xy
);
775 /* Round the float coordinates down to nearest integer */
776 pos_xy
= nir_fdiv(b
, nir_ftrunc(b
, pos_xy
), scale
);
778 nir_ssa_def
*tex_data
[4];
779 for (unsigned i
= 0; i
< 4; ++i
) {
780 float sample_off_x
= (float)(i
& 0x1) / key
->x_scale
;
781 float sample_off_y
= (float)((i
>> 1) & 0x1) / key
->y_scale
;
782 nir_ssa_def
*sample_off
= nir_imm_vec2(b
, sample_off_x
, sample_off_y
);
784 nir_ssa_def
*sample_coords
= nir_fadd(b
, pos_xy
, sample_off
);
785 nir_ssa_def
*sample_coords_int
= nir_f2i32(b
, sample_coords
);
787 /* The MCS value we fetch has to match up with the pixel that we're
788 * sampling from. Since we sample from different pixels in each
789 * iteration of this "for" loop, the call to mcs_fetch() should be
790 * here inside the loop after computing the pixel coordinates.
792 nir_ssa_def
*mcs
= NULL
;
793 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
794 mcs
= blorp_blit_txf_ms_mcs(b
, v
, sample_coords_int
);
796 /* Compute sample index and map the sample index to a sample number.
797 * Sample index layout shows the numbering of slots in a rectangular
798 * grid of samples with in a pixel. Sample number layout shows the
799 * rectangular grid of samples roughly corresponding to the real sample
800 * locations with in a pixel.
802 * In the case of 2x MSAA, the layout of sample indices is reversed from
803 * the layout of sample numbers:
805 * sample index layout : --------- sample number layout : ---------
806 * | 0 | 1 | | 1 | 0 |
807 * --------- ---------
809 * In case of 4x MSAA, layout of sample indices matches the layout of
817 * In case of 8x MSAA the two layouts don't match.
818 * sample index layout : --------- sample number layout : ---------
819 * | 0 | 1 | | 3 | 7 |
820 * --------- ---------
821 * | 2 | 3 | | 5 | 0 |
822 * --------- ---------
823 * | 4 | 5 | | 1 | 2 |
824 * --------- ---------
825 * | 6 | 7 | | 4 | 6 |
826 * --------- ---------
828 * Fortunately, this can be done fairly easily as:
829 * S' = (0x17306425 >> (S * 4)) & 0xf
831 * In the case of 16x MSAA the two layouts don't match.
832 * Sample index layout: Sample number layout:
833 * --------------------- ---------------------
834 * | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
835 * --------------------- ---------------------
836 * | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
837 * --------------------- ---------------------
838 * | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
839 * --------------------- ---------------------
840 * | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
841 * --------------------- ---------------------
843 * This is equivalent to
844 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
846 nir_ssa_def
*frac
= nir_ffract(b
, sample_coords
);
847 nir_ssa_def
*sample
=
848 nir_fdot2(b
, frac
, nir_imm_vec2(b
, key
->x_scale
,
849 key
->x_scale
* key
->y_scale
));
850 sample
= nir_f2i32(b
, sample
);
852 if (tex_samples
== 2) {
853 sample
= nir_isub(b
, nir_imm_int(b
, 1), sample
);
854 } else if (tex_samples
== 8) {
855 sample
= nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0x64210573),
856 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
857 nir_imm_int(b
, 0xf));
858 } else if (tex_samples
== 16) {
859 nir_ssa_def
*sample_low
=
860 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xd31479af),
861 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
862 nir_imm_int(b
, 0xf));
863 nir_ssa_def
*sample_high
=
864 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xe58b602c),
865 nir_ishl(b
, nir_iadd(b
, sample
,
868 nir_imm_int(b
, 0xf));
870 sample
= nir_bcsel(b
, nir_ilt(b
, sample
, nir_imm_int(b
, 8)),
871 sample_low
, sample_high
);
873 nir_ssa_def
*pos_ms
= nir_vec3(b
, nir_channel(b
, sample_coords_int
, 0),
874 nir_channel(b
, sample_coords_int
, 1),
876 tex_data
[i
] = blorp_nir_txf_ms(b
, v
, pos_ms
, mcs
, key
->texture_data_type
);
879 nir_ssa_def
*frac_x
= nir_channel(b
, frac_xy
, 0);
880 nir_ssa_def
*frac_y
= nir_channel(b
, frac_xy
, 1);
881 return nir_flrp(b
, nir_flrp(b
, tex_data
[0], tex_data
[1], frac_x
),
882 nir_flrp(b
, tex_data
[2], tex_data
[3], frac_x
),
886 /** Perform a color bit-cast operation
888 * For copy operations involving CCS, we may need to use different formats for
889 * the source and destination surfaces. The two formats must both be UINT
890 * formats and must have the same size but may have different bit layouts.
891 * For instance, we may be copying from R8G8B8A8_UINT to R32_UINT or R32_UINT
892 * to R16G16_UINT. This function generates code to shuffle bits around to get
893 * us from one to the other.
896 bit_cast_color(struct nir_builder
*b
, nir_ssa_def
*color
,
897 const struct brw_blorp_blit_prog_key
*key
)
899 assert(key
->texture_data_type
== nir_type_uint
);
901 if (key
->src_format
== key
->dst_format
)
904 const struct isl_format_layout
*src_fmtl
=
905 isl_format_get_layout(key
->src_format
);
906 const struct isl_format_layout
*dst_fmtl
=
907 isl_format_get_layout(key
->dst_format
);
909 /* They must be uint formats with the same bit size */
910 assert(src_fmtl
->bpb
== dst_fmtl
->bpb
);
911 assert(src_fmtl
->channels
.r
.type
== ISL_UINT
);
912 assert(dst_fmtl
->channels
.r
.type
== ISL_UINT
);
914 /* They must be in regular color formats (no luminance or alpha) */
915 assert(src_fmtl
->channels
.r
.bits
> 0);
916 assert(dst_fmtl
->channels
.r
.bits
> 0);
918 /* They must be in RGBA order (possibly with channels missing) */
919 assert(src_fmtl
->channels
.r
.start_bit
== 0);
920 assert(dst_fmtl
->channels
.r
.start_bit
== 0);
922 if (src_fmtl
->bpb
<= 32) {
923 const unsigned src_channels
=
924 isl_format_get_num_channels(key
->src_format
);
925 const unsigned src_bits
[4] = {
926 src_fmtl
->channels
.r
.bits
,
927 src_fmtl
->channels
.g
.bits
,
928 src_fmtl
->channels
.b
.bits
,
929 src_fmtl
->channels
.a
.bits
,
931 const unsigned dst_channels
=
932 isl_format_get_num_channels(key
->dst_format
);
933 const unsigned dst_bits
[4] = {
934 dst_fmtl
->channels
.r
.bits
,
935 dst_fmtl
->channels
.g
.bits
,
936 dst_fmtl
->channels
.b
.bits
,
937 dst_fmtl
->channels
.a
.bits
,
939 nir_ssa_def
*packed
=
940 nir_format_pack_uint_unmasked(b
, color
, src_bits
, src_channels
);
941 color
= nir_format_unpack_uint(b
, packed
, dst_bits
, dst_channels
);
943 const unsigned src_bpc
= src_fmtl
->channels
.r
.bits
;
944 const unsigned dst_bpc
= dst_fmtl
->channels
.r
.bits
;
946 assert(src_fmtl
->channels
.g
.bits
== 0 ||
947 src_fmtl
->channels
.g
.bits
== src_fmtl
->channels
.r
.bits
);
948 assert(src_fmtl
->channels
.b
.bits
== 0 ||
949 src_fmtl
->channels
.b
.bits
== src_fmtl
->channels
.r
.bits
);
950 assert(src_fmtl
->channels
.a
.bits
== 0 ||
951 src_fmtl
->channels
.a
.bits
== src_fmtl
->channels
.r
.bits
);
952 assert(dst_fmtl
->channels
.g
.bits
== 0 ||
953 dst_fmtl
->channels
.g
.bits
== dst_fmtl
->channels
.r
.bits
);
954 assert(dst_fmtl
->channels
.b
.bits
== 0 ||
955 dst_fmtl
->channels
.b
.bits
== dst_fmtl
->channels
.r
.bits
);
956 assert(dst_fmtl
->channels
.a
.bits
== 0 ||
957 dst_fmtl
->channels
.a
.bits
== dst_fmtl
->channels
.r
.bits
);
959 /* Restrict to only the channels we actually have */
960 const unsigned src_channels
=
961 isl_format_get_num_channels(key
->src_format
);
962 color
= nir_channels(b
, color
, (1 << src_channels
) - 1);
964 color
= nir_format_bitcast_uvec_unmasked(b
, color
, src_bpc
, dst_bpc
);
967 /* Blorp likes to assume that colors are vec4s */
968 nir_ssa_def
*u
= nir_ssa_undef(b
, 1, 32);
969 nir_ssa_def
*chans
[4] = { u
, u
, u
, u
};
970 for (unsigned i
= 0; i
< color
->num_components
; i
++)
971 chans
[i
] = nir_channel(b
, color
, i
);
972 return nir_vec4(b
, chans
[0], chans
[1], chans
[2], chans
[3]);
976 select_color_channel(struct nir_builder
*b
, nir_ssa_def
*color
,
977 nir_alu_type data_type
,
978 enum isl_channel_select chan
)
980 if (chan
== ISL_CHANNEL_SELECT_ZERO
) {
981 return nir_imm_int(b
, 0);
982 } else if (chan
== ISL_CHANNEL_SELECT_ONE
) {
986 return nir_imm_int(b
, 1);
988 return nir_imm_float(b
, 1);
990 unreachable("Invalid data type");
993 assert((unsigned)(chan
- ISL_CHANNEL_SELECT_RED
) < 4);
994 return nir_channel(b
, color
, chan
- ISL_CHANNEL_SELECT_RED
);
999 swizzle_color(struct nir_builder
*b
, nir_ssa_def
*color
,
1000 struct isl_swizzle swizzle
, nir_alu_type data_type
)
1003 select_color_channel(b
, color
, data_type
, swizzle
.r
),
1004 select_color_channel(b
, color
, data_type
, swizzle
.g
),
1005 select_color_channel(b
, color
, data_type
, swizzle
.b
),
1006 select_color_channel(b
, color
, data_type
, swizzle
.a
));
1009 static nir_ssa_def
*
1010 convert_color(struct nir_builder
*b
, nir_ssa_def
*color
,
1011 const struct brw_blorp_blit_prog_key
*key
)
1013 /* All of our color conversions end up generating a single-channel color
1014 * value that we need to write out.
1018 if (key
->dst_format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
) {
1019 /* The destination image is bound as R32_UINT but the data needs to be
1020 * in R24_UNORM_X8_TYPELESS. The bottom 24 are the actual data and the
1021 * top 8 need to be zero. We can accomplish this by simply multiplying
1022 * by a factor to scale things down.
1024 unsigned factor
= (1 << 24) - 1;
1025 value
= nir_fsat(b
, nir_channel(b
, color
, 0));
1026 value
= nir_f2i32(b
, nir_fmul(b
, value
, nir_imm_float(b
, factor
)));
1027 } else if (key
->dst_format
== ISL_FORMAT_L8_UNORM_SRGB
) {
1028 value
= nir_format_linear_to_srgb(b
, nir_channel(b
, color
, 0));
1029 } else if (key
->dst_format
== ISL_FORMAT_R8G8B8_UNORM_SRGB
) {
1030 value
= nir_format_linear_to_srgb(b
, color
);
1031 } else if (key
->dst_format
== ISL_FORMAT_R9G9B9E5_SHAREDEXP
) {
1032 value
= nir_format_pack_r9g9b9e5(b
, color
);
1034 unreachable("Unsupported format conversion");
1037 nir_ssa_def
*out_comps
[4];
1038 for (unsigned i
= 0; i
< 4; i
++) {
1039 if (i
< value
->num_components
)
1040 out_comps
[i
] = nir_channel(b
, value
, i
);
1042 out_comps
[i
] = nir_ssa_undef(b
, 1, 32);
1044 return nir_vec(b
, out_comps
, 4);
1048 * Generator for WM programs used in BLORP blits.
1050 * The bulk of the work done by the WM program is to wrap and unwrap the
1051 * coordinate transformations used by the hardware to store surfaces in
1052 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
1053 * sample index for a multisampled surface) to a memory offset by the
1054 * following formulas:
1056 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
1057 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
1059 * For a single-sampled surface, or for a multisampled surface using
1060 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
1063 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1064 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1065 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1066 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1068 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1069 * embeds the sample number into bit 1 of the X and Y coordinates:
1071 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1072 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1073 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
1074 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1075 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1076 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1077 * S = (Y & 0b10) | (X & 0b10) >> 1
1079 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1080 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
1083 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1084 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
1085 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1086 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1087 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1088 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1089 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1091 * For X tiling, tile() combines together the low-order bits of the X and Y
1092 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
1093 * bytes wide and 8 rows high:
1095 * tile(x_tiled, X, Y, S) = A
1096 * where A = tile_num << 12 | offset
1097 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
1098 * offset = (Y' & 0b111) << 9
1099 * | (X & 0b111111111)
1101 * Y' = Y + S * qpitch
1102 * detile(x_tiled, A) = (X, Y, S)
1103 * where X = X' / cpp
1106 * Y' = (tile_num / tile_pitch) << 3
1107 * | (A & 0b111000000000) >> 9
1108 * X' = (tile_num % tile_pitch) << 9
1109 * | (A & 0b111111111)
1111 * (In all tiling formulas, cpp is the number of bytes occupied by a single
1112 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
1113 * to fill the width of the surface, and qpitch is the spacing (in rows)
1114 * between array slices).
1116 * For Y tiling, tile() combines together the low-order bits of the X and Y
1117 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
1118 * bytes wide and 32 rows high:
1120 * tile(y_tiled, X, Y, S) = A
1121 * where A = tile_num << 12 | offset
1122 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
1123 * offset = (X' & 0b1110000) << 5
1124 * | (Y' & 0b11111) << 4
1127 * Y' = Y + S * qpitch
1128 * detile(y_tiled, A) = (X, Y, S)
1129 * where X = X' / cpp
1132 * Y' = (tile_num / tile_pitch) << 5
1133 * | (A & 0b111110000) >> 4
1134 * X' = (tile_num % tile_pitch) << 7
1135 * | (A & 0b111000000000) >> 5
1138 * For W tiling, tile() combines together the low-order bits of the X and Y
1139 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
1140 * bytes wide and 64 rows high (note that W tiling is only used for stencil
1141 * buffers, which always have cpp = 1 and S=0):
1143 * tile(w_tiled, X, Y, S) = A
1144 * where A = tile_num << 12 | offset
1145 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
1146 * offset = (X' & 0b111000) << 6
1147 * | (Y' & 0b111100) << 3
1148 * | (X' & 0b100) << 2
1149 * | (Y' & 0b10) << 2
1150 * | (X' & 0b10) << 1
1154 * Y' = Y + S * qpitch
1155 * detile(w_tiled, A) = (X, Y, S)
1156 * where X = X' / cpp = X'
1157 * Y = Y' % qpitch = Y'
1158 * S = Y / qpitch = 0
1159 * Y' = (tile_num / tile_pitch) << 6
1160 * | (A & 0b111100000) >> 3
1161 * | (A & 0b1000) >> 2
1163 * X' = (tile_num % tile_pitch) << 6
1164 * | (A & 0b111000000000) >> 6
1165 * | (A & 0b10000) >> 2
1166 * | (A & 0b100) >> 1
1169 * Finally, for a non-tiled surface, tile() simply combines together the X and
1170 * Y coordinates in the natural way:
1172 * tile(untiled, X, Y, S) = A
1173 * where A = Y * pitch + X'
1175 * Y' = Y + S * qpitch
1176 * detile(untiled, A) = (X, Y, S)
1177 * where X = X' / cpp
1183 * (In these formulas, pitch is the number of bytes occupied by a single row
1187 brw_blorp_build_nir_shader(struct blorp_context
*blorp
, void *mem_ctx
,
1188 const struct brw_blorp_blit_prog_key
*key
)
1190 const struct gen_device_info
*devinfo
= blorp
->isl_dev
->info
;
1191 nir_ssa_def
*src_pos
, *dst_pos
, *color
;
1194 if (key
->dst_tiled_w
&& key
->rt_samples
> 1) {
1195 /* If the destination image is W tiled and multisampled, then the thread
1196 * must be dispatched once per sample, not once per pixel. This is
1197 * necessary because after conversion between W and Y tiling, there's no
1198 * guarantee that all samples corresponding to a single pixel will still
1201 assert(key
->persample_msaa_dispatch
);
1204 if (key
->persample_msaa_dispatch
) {
1205 /* It only makes sense to do persample dispatch if the render target is
1206 * configured as multisampled.
1208 assert(key
->rt_samples
> 0);
1211 /* Make sure layout is consistent with sample count */
1212 assert((key
->tex_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1213 (key
->tex_samples
<= 1));
1214 assert((key
->rt_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1215 (key
->rt_samples
<= 1));
1216 assert((key
->src_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1217 (key
->src_samples
<= 1));
1218 assert((key
->dst_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1219 (key
->dst_samples
<= 1));
1222 blorp_nir_init_shader(&b
, mem_ctx
, MESA_SHADER_FRAGMENT
, NULL
);
1224 struct brw_blorp_blit_vars v
;
1225 brw_blorp_blit_vars_init(&b
, &v
, key
);
1227 dst_pos
= blorp_blit_get_frag_coords(&b
, key
, &v
);
1229 /* Render target and texture hardware don't support W tiling until Gen8. */
1230 const bool rt_tiled_w
= false;
1231 const bool tex_tiled_w
= devinfo
->gen
>= 8 && key
->src_tiled_w
;
1233 /* The address that data will be written to is determined by the
1234 * coordinates supplied to the WM thread and the tiling and sample count of
1235 * the render target, according to the formula:
1237 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1239 * If the actual tiling and sample count of the destination surface are not
1240 * the same as the configuration of the render target, then these
1241 * coordinates are wrong and we have to adjust them to compensate for the
1244 if (rt_tiled_w
!= key
->dst_tiled_w
||
1245 key
->rt_samples
!= key
->dst_samples
||
1246 key
->rt_layout
!= key
->dst_layout
) {
1247 dst_pos
= blorp_nir_encode_msaa(&b
, dst_pos
, key
->rt_samples
,
1249 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1250 if (rt_tiled_w
!= key
->dst_tiled_w
)
1251 dst_pos
= blorp_nir_retile_y_to_w(&b
, dst_pos
);
1252 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1253 dst_pos
= blorp_nir_decode_msaa(&b
, dst_pos
, key
->dst_samples
,
1257 nir_ssa_def
*comp
= NULL
;
1259 /* The destination image is bound as a red texture three times as wide
1260 * as the actual image. Our shader is effectively running one color
1261 * component at a time. We need to save off the component and adjust
1262 * the destination position.
1264 assert(dst_pos
->num_components
== 2);
1265 nir_ssa_def
*dst_x
= nir_channel(&b
, dst_pos
, 0);
1266 comp
= nir_umod(&b
, dst_x
, nir_imm_int(&b
, 3));
1267 dst_pos
= nir_vec2(&b
, nir_idiv(&b
, dst_x
, nir_imm_int(&b
, 3)),
1268 nir_channel(&b
, dst_pos
, 1));
1271 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1273 * That is: X, Y and S now contain the true coordinates and sample index of
1274 * the data that the WM thread should output.
1276 * If we need to kill pixels that are outside the destination rectangle,
1277 * now is the time to do it.
1280 blorp_nir_discard_if_outside_rect(&b
, dst_pos
, &v
);
1282 src_pos
= blorp_blit_apply_transform(&b
, nir_i2f32(&b
, dst_pos
), &v
);
1283 if (dst_pos
->num_components
== 3) {
1284 /* The sample coordinate is an integer that we want left alone but
1285 * blorp_blit_apply_transform() blindly applies the transform to all
1286 * three coordinates. Grab the original sample index.
1288 src_pos
= nir_vec3(&b
, nir_channel(&b
, src_pos
, 0),
1289 nir_channel(&b
, src_pos
, 1),
1290 nir_channel(&b
, dst_pos
, 2));
1293 /* If the source image is not multisampled, then we want to fetch sample
1294 * number 0, because that's the only sample there is.
1296 if (key
->src_samples
== 1)
1297 src_pos
= nir_channels(&b
, src_pos
, 0x3);
1299 /* X, Y, and S are now the coordinates of the pixel in the source image
1300 * that we want to texture from. Exception: if we are blending, then S is
1301 * irrelevant, because we are going to fetch all samples.
1303 switch (key
->filter
) {
1304 case BLORP_FILTER_NONE
:
1305 case BLORP_FILTER_NEAREST
:
1306 case BLORP_FILTER_SAMPLE_0
:
1307 /* We're going to use texelFetch, so we need integers */
1308 if (src_pos
->num_components
== 2) {
1309 src_pos
= nir_f2i32(&b
, src_pos
);
1311 assert(src_pos
->num_components
== 3);
1312 src_pos
= nir_vec3(&b
, nir_channel(&b
, nir_f2i32(&b
, src_pos
), 0),
1313 nir_channel(&b
, nir_f2i32(&b
, src_pos
), 1),
1314 nir_channel(&b
, src_pos
, 2));
1317 /* We aren't blending, which means we just want to fetch a single
1318 * sample from the source surface. The address that we want to fetch
1319 * from is related to the X, Y and S values according to the formula:
1321 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1323 * If the actual tiling and sample count of the source surface are
1324 * not the same as the configuration of the texture, then we need to
1325 * adjust the coordinates to compensate for the difference.
1327 if (tex_tiled_w
!= key
->src_tiled_w
||
1328 key
->tex_samples
!= key
->src_samples
||
1329 key
->tex_layout
!= key
->src_layout
) {
1330 src_pos
= blorp_nir_encode_msaa(&b
, src_pos
, key
->src_samples
,
1332 /* Now (X, Y, S) = detile(src_tiling, offset) */
1333 if (tex_tiled_w
!= key
->src_tiled_w
)
1334 src_pos
= blorp_nir_retile_w_to_y(&b
, src_pos
);
1335 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1336 src_pos
= blorp_nir_decode_msaa(&b
, src_pos
, key
->tex_samples
,
1340 if (key
->need_src_offset
)
1341 src_pos
= nir_iadd(&b
, src_pos
, nir_load_var(&b
, v
.v_src_offset
));
1343 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1345 * In other words: X, Y, and S now contain values which, when passed to
1346 * the texturing unit, will cause data to be read from the correct
1347 * memory location. So we can fetch the texel now.
1349 if (key
->src_samples
== 1) {
1350 color
= blorp_nir_txf(&b
, &v
, src_pos
, key
->texture_data_type
);
1352 nir_ssa_def
*mcs
= NULL
;
1353 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
1354 mcs
= blorp_blit_txf_ms_mcs(&b
, &v
, src_pos
);
1356 color
= blorp_nir_txf_ms(&b
, &v
, src_pos
, mcs
, key
->texture_data_type
);
1360 case BLORP_FILTER_BILINEAR
:
1361 assert(!key
->src_tiled_w
);
1362 assert(key
->tex_samples
== key
->src_samples
);
1363 assert(key
->tex_layout
== key
->src_layout
);
1365 if (key
->src_samples
== 1) {
1366 color
= blorp_nir_tex(&b
, &v
, key
, src_pos
);
1368 assert(!key
->use_kill
);
1369 color
= blorp_nir_manual_blend_bilinear(&b
, src_pos
, key
->src_samples
,
1374 case BLORP_FILTER_AVERAGE
:
1375 case BLORP_FILTER_MIN_SAMPLE
:
1376 case BLORP_FILTER_MAX_SAMPLE
:
1377 assert(!key
->src_tiled_w
);
1378 assert(key
->tex_samples
== key
->src_samples
);
1379 assert(key
->tex_layout
== key
->src_layout
);
1381 /* Resolves (effecively) use texelFetch, so we need integers and we
1382 * don't care about the sample index if we got one.
1384 src_pos
= nir_f2i32(&b
, nir_channels(&b
, src_pos
, 0x3));
1386 if (devinfo
->gen
== 6) {
1387 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1388 * blending we need with a single linear-interpolated texture lookup
1389 * at the center of the sample. The texture coordinates to be odd
1390 * integers so that they correspond to the center of a 2x2 block
1391 * representing the four samples that maxe up a pixel. So we need
1392 * to multiply our X and Y coordinates each by 2 and then add 1.
1394 assert(key
->src_coords_normalized
);
1395 assert(key
->filter
== BLORP_FILTER_AVERAGE
);
1396 src_pos
= nir_fadd(&b
,
1397 nir_i2f32(&b
, src_pos
),
1398 nir_imm_float(&b
, 0.5f
));
1399 color
= blorp_nir_tex(&b
, &v
, key
, src_pos
);
1401 /* Gen7+ hardware doesn't automaticaly blend. */
1402 color
= blorp_nir_combine_samples(&b
, &v
, src_pos
, key
->src_samples
,
1404 key
->texture_data_type
,
1410 unreachable("Invalid blorp filter");
1413 if (!isl_swizzle_is_identity(key
->src_swizzle
)) {
1414 color
= swizzle_color(&b
, color
, key
->src_swizzle
,
1415 key
->texture_data_type
);
1418 if (!isl_swizzle_is_identity(key
->dst_swizzle
)) {
1419 color
= swizzle_color(&b
, color
, isl_swizzle_invert(key
->dst_swizzle
),
1423 if (key
->format_bit_cast
) {
1424 assert(isl_swizzle_is_identity(key
->src_swizzle
));
1425 assert(isl_swizzle_is_identity(key
->dst_swizzle
));
1426 color
= bit_cast_color(&b
, color
, key
);
1427 } else if (key
->dst_format
) {
1428 color
= convert_color(&b
, color
, key
);
1429 } else if (key
->uint32_to_sint
) {
1430 /* Normally the hardware will take care of converting values from/to
1431 * the source and destination formats. But a few cases need help.
1433 * The Skylake PRM, volume 07, page 658 has a programming note:
1435 * "When using SINT or UINT rendertarget surface formats, Blending
1436 * must be DISABLED. The Pre-Blend Color Clamp Enable and Color
1437 * Clamp Range fields are ignored, and an implied clamp to the
1438 * rendertarget surface format is performed."
1440 * For UINT to SINT blits, our sample operation gives us a uint32_t,
1441 * but our render target write expects a signed int32_t number. If we
1442 * simply passed the value along, the hardware would interpret a value
1443 * with bit 31 set as a negative value, clamping it to the largest
1444 * negative number the destination format could represent. But the
1445 * actual source value is a positive number, so we want to clamp it
1446 * to INT_MAX. To fix this, we explicitly take min(color, INT_MAX).
1448 color
= nir_umin(&b
, color
, nir_imm_int(&b
, INT32_MAX
));
1449 } else if (key
->sint32_to_uint
) {
1450 /* Similar to above, but clamping negative numbers to zero. */
1451 color
= nir_imax(&b
, color
, nir_imm_int(&b
, 0));
1455 /* The destination image is bound as a red texture three times as wide
1456 * as the actual image. Our shader is effectively running one color
1457 * component at a time. We need to pick off the appropriate component
1458 * from the source color and write that to destination red.
1460 assert(dst_pos
->num_components
== 2);
1462 nir_ssa_def
*color_component
=
1463 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 0)),
1464 nir_channel(&b
, color
, 0),
1465 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 1)),
1466 nir_channel(&b
, color
, 1),
1467 nir_channel(&b
, color
, 2)));
1469 nir_ssa_def
*u
= nir_ssa_undef(&b
, 1, 32);
1470 color
= nir_vec4(&b
, color_component
, u
, u
, u
);
1473 nir_store_var(&b
, v
.color_out
, color
, 0xf);
1479 brw_blorp_get_blit_kernel(struct blorp_batch
*batch
,
1480 struct blorp_params
*params
,
1481 const struct brw_blorp_blit_prog_key
*prog_key
)
1483 struct blorp_context
*blorp
= batch
->blorp
;
1485 if (blorp
->lookup_shader(batch
, prog_key
, sizeof(*prog_key
),
1486 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
))
1489 void *mem_ctx
= ralloc_context(NULL
);
1491 const unsigned *program
;
1492 struct brw_wm_prog_data prog_data
;
1494 nir_shader
*nir
= brw_blorp_build_nir_shader(blorp
, mem_ctx
, prog_key
);
1495 nir
->info
.name
= ralloc_strdup(nir
, "BLORP-blit");
1497 struct brw_wm_prog_key wm_key
;
1498 brw_blorp_init_wm_prog_key(&wm_key
);
1499 wm_key
.base
.tex
.compressed_multisample_layout_mask
=
1500 prog_key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
;
1501 wm_key
.base
.tex
.msaa_16
= prog_key
->tex_samples
== 16;
1502 wm_key
.multisample_fbo
= prog_key
->rt_samples
> 1;
1504 program
= blorp_compile_fs(blorp
, mem_ctx
, nir
, &wm_key
, false,
1508 blorp
->upload_shader(batch
, prog_key
, sizeof(*prog_key
),
1509 program
, prog_data
.base
.program_size
,
1510 &prog_data
.base
, sizeof(prog_data
),
1511 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
);
1513 ralloc_free(mem_ctx
);
1518 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform
*xform
,
1519 GLfloat src0
, GLfloat src1
,
1520 GLfloat dst0
, GLfloat dst1
,
1523 double scale
= (double)(src1
- src0
) / (double)(dst1
- dst0
);
1525 /* When not mirroring a coordinate (say, X), we need:
1526 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1528 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1530 * blorp program uses "round toward zero" to convert the
1531 * transformed floating point coordinates to integer coordinates,
1532 * whereas the behaviour we actually want is "round to nearest",
1533 * so 0.5 provides the necessary correction.
1535 xform
->multiplier
= scale
;
1536 xform
->offset
= src0
+ (-(double)dst0
+ 0.5) * scale
;
1538 /* When mirroring X we need:
1539 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1541 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1543 xform
->multiplier
= -scale
;
1544 xform
->offset
= src0
+ ((double)dst1
- 0.5) * scale
;
1549 surf_get_intratile_offset_px(struct brw_blorp_surface_info
*info
,
1550 uint32_t *tile_x_px
, uint32_t *tile_y_px
)
1552 if (info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1553 struct isl_extent2d px_size_sa
=
1554 isl_get_interleaved_msaa_px_size_sa(info
->surf
.samples
);
1555 assert(info
->tile_x_sa
% px_size_sa
.width
== 0);
1556 assert(info
->tile_y_sa
% px_size_sa
.height
== 0);
1557 *tile_x_px
= info
->tile_x_sa
/ px_size_sa
.width
;
1558 *tile_y_px
= info
->tile_y_sa
/ px_size_sa
.height
;
1560 *tile_x_px
= info
->tile_x_sa
;
1561 *tile_y_px
= info
->tile_y_sa
;
1566 blorp_surf_convert_to_single_slice(const struct isl_device
*isl_dev
,
1567 struct brw_blorp_surface_info
*info
)
1571 /* It would be insane to try and do this on a compressed surface */
1572 assert(info
->aux_usage
== ISL_AUX_USAGE_NONE
);
1574 /* Just bail if we have nothing to do. */
1575 if (info
->surf
.dim
== ISL_SURF_DIM_2D
&&
1576 info
->view
.base_level
== 0 && info
->view
.base_array_layer
== 0 &&
1577 info
->surf
.levels
== 1 && info
->surf
.logical_level0_px
.array_len
== 1)
1580 /* If this gets triggered then we've gotten here twice which. This
1581 * shouldn't happen thanks to the above early return.
1583 assert(info
->tile_x_sa
== 0 && info
->tile_y_sa
== 0);
1585 uint32_t layer
= 0, z
= 0;
1586 if (info
->surf
.dim
== ISL_SURF_DIM_3D
)
1587 z
= info
->view
.base_array_layer
+ info
->z_offset
;
1589 layer
= info
->view
.base_array_layer
;
1591 uint32_t byte_offset
;
1592 isl_surf_get_image_surf(isl_dev
, &info
->surf
,
1593 info
->view
.base_level
, layer
, z
,
1595 &byte_offset
, &info
->tile_x_sa
, &info
->tile_y_sa
);
1596 info
->addr
.offset
+= byte_offset
;
1598 uint32_t tile_x_px
, tile_y_px
;
1599 surf_get_intratile_offset_px(info
, &tile_x_px
, &tile_y_px
);
1601 /* Instead of using the X/Y Offset fields in RENDER_SURFACE_STATE, we place
1602 * the image at the tile boundary and offset our sampling or rendering.
1603 * For this reason, we need to grow the image by the offset to ensure that
1604 * the hardware doesn't think we've gone past the edge.
1606 info
->surf
.logical_level0_px
.w
+= tile_x_px
;
1607 info
->surf
.logical_level0_px
.h
+= tile_y_px
;
1608 info
->surf
.phys_level0_sa
.w
+= info
->tile_x_sa
;
1609 info
->surf
.phys_level0_sa
.h
+= info
->tile_y_sa
;
1611 /* The view is also different now. */
1612 info
->view
.base_level
= 0;
1613 info
->view
.levels
= 1;
1614 info
->view
.base_array_layer
= 0;
1615 info
->view
.array_len
= 1;
1620 surf_fake_interleaved_msaa(const struct isl_device
*isl_dev
,
1621 struct brw_blorp_surface_info
*info
)
1623 assert(info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
);
1625 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1626 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1628 info
->surf
.logical_level0_px
= info
->surf
.phys_level0_sa
;
1629 info
->surf
.samples
= 1;
1630 info
->surf
.msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
1634 surf_retile_w_to_y(const struct isl_device
*isl_dev
,
1635 struct brw_blorp_surface_info
*info
)
1637 assert(info
->surf
.tiling
== ISL_TILING_W
);
1639 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1640 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1642 /* On gen7+, we don't have interleaved multisampling for color render
1643 * targets so we have to fake it.
1645 * TODO: Are we sure we don't also need to fake it on gen6?
1647 if (isl_dev
->info
->gen
> 6 &&
1648 info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1649 surf_fake_interleaved_msaa(isl_dev
, info
);
1652 if (isl_dev
->info
->gen
== 6) {
1653 /* Gen6 stencil buffers have a very large alignment coming in from the
1654 * miptree. It's out-of-bounds for what the surface state can handle.
1655 * Since we have a single layer and level, it doesn't really matter as
1656 * long as we don't pass a bogus value into isl_surf_fill_state().
1658 info
->surf
.image_alignment_el
= isl_extent3d(4, 2, 1);
1661 /* Now that we've converted everything to a simple 2-D surface with only
1662 * one miplevel, we can go about retiling it.
1664 const unsigned x_align
= 8, y_align
= info
->surf
.samples
!= 0 ? 8 : 4;
1665 info
->surf
.tiling
= ISL_TILING_Y0
;
1666 info
->surf
.logical_level0_px
.width
=
1667 ALIGN(info
->surf
.logical_level0_px
.width
, x_align
) * 2;
1668 info
->surf
.logical_level0_px
.height
=
1669 ALIGN(info
->surf
.logical_level0_px
.height
, y_align
) / 2;
1670 info
->tile_x_sa
*= 2;
1671 info
->tile_y_sa
/= 2;
1675 can_shrink_surface(const struct brw_blorp_surface_info
*surf
)
1677 /* The current code doesn't support offsets into the aux buffers. This
1678 * should be possible, but we need to make sure the offset is page
1679 * aligned for both the surface and the aux buffer surface. Generally
1680 * this mean using the page aligned offset for the aux buffer.
1682 * Currently the cases where we must split the blit are limited to cases
1683 * where we don't have a aux buffer.
1685 if (surf
->aux_addr
.buffer
!= NULL
)
1688 /* We can't support splitting the blit for gen <= 7, because the qpitch
1689 * size is calculated by the hardware based on the surface height for
1690 * gen <= 7. In gen >= 8, the qpitch is controlled by the driver.
1692 if (surf
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_ARRAY
)
1699 can_shrink_surfaces(const struct blorp_params
*params
)
1702 can_shrink_surface(¶ms
->src
) &&
1703 can_shrink_surface(¶ms
->dst
);
1707 get_max_surface_size(const struct gen_device_info
*devinfo
,
1708 const struct blorp_params
*params
)
1710 const unsigned max
= devinfo
->gen
>= 7 ? 16384 : 8192;
1711 if (split_blorp_blit_debug
&& can_shrink_surfaces(params
))
1712 return max
>> 4; /* A smaller restriction when debug is enabled */
1718 double src0
, src1
, dst0
, dst1
;
1723 struct blt_axis x
, y
;
1726 static enum isl_format
1727 get_red_format_for_rgb_format(enum isl_format format
)
1729 const struct isl_format_layout
*fmtl
= isl_format_get_layout(format
);
1731 switch (fmtl
->channels
.r
.bits
) {
1733 switch (fmtl
->channels
.r
.type
) {
1735 return ISL_FORMAT_R8_UNORM
;
1737 return ISL_FORMAT_R8_SNORM
;
1739 return ISL_FORMAT_R8_UINT
;
1741 return ISL_FORMAT_R8_SINT
;
1743 unreachable("Invalid 8-bit RGB channel type");
1746 switch (fmtl
->channels
.r
.type
) {
1748 return ISL_FORMAT_R16_UNORM
;
1750 return ISL_FORMAT_R16_SNORM
;
1752 return ISL_FORMAT_R16_FLOAT
;
1754 return ISL_FORMAT_R16_UINT
;
1756 return ISL_FORMAT_R16_SINT
;
1758 unreachable("Invalid 8-bit RGB channel type");
1761 switch (fmtl
->channels
.r
.type
) {
1763 return ISL_FORMAT_R32_FLOAT
;
1765 return ISL_FORMAT_R32_UINT
;
1767 return ISL_FORMAT_R32_SINT
;
1769 unreachable("Invalid 8-bit RGB channel type");
1772 unreachable("Invalid number of red channel bits");
1777 surf_fake_rgb_with_red(const struct isl_device
*isl_dev
,
1778 struct brw_blorp_surface_info
*info
)
1780 blorp_surf_convert_to_single_slice(isl_dev
, info
);
1782 info
->surf
.logical_level0_px
.width
*= 3;
1783 info
->surf
.phys_level0_sa
.width
*= 3;
1784 info
->tile_x_sa
*= 3;
1786 enum isl_format red_format
=
1787 get_red_format_for_rgb_format(info
->view
.format
);
1789 assert(isl_format_get_layout(red_format
)->channels
.r
.type
==
1790 isl_format_get_layout(info
->view
.format
)->channels
.r
.type
);
1791 assert(isl_format_get_layout(red_format
)->channels
.r
.bits
==
1792 isl_format_get_layout(info
->view
.format
)->channels
.r
.bits
);
1794 info
->surf
.format
= info
->view
.format
= red_format
;
1797 enum blit_shrink_status
{
1799 BLIT_WIDTH_SHRINK
= 1,
1800 BLIT_HEIGHT_SHRINK
= 2,
1803 /* Try to blit. If the surface parameters exceed the size allowed by hardware,
1804 * then enum blit_shrink_status will be returned. If BLIT_NO_SHRINK is
1805 * returned, then the blit was successful.
1807 static enum blit_shrink_status
1808 try_blorp_blit(struct blorp_batch
*batch
,
1809 struct blorp_params
*params
,
1810 struct brw_blorp_blit_prog_key
*wm_prog_key
,
1811 struct blt_coords
*coords
)
1813 const struct gen_device_info
*devinfo
= batch
->blorp
->isl_dev
->info
;
1815 if (isl_format_has_sint_channel(params
->src
.view
.format
)) {
1816 wm_prog_key
->texture_data_type
= nir_type_int
;
1817 } else if (isl_format_has_uint_channel(params
->src
.view
.format
)) {
1818 wm_prog_key
->texture_data_type
= nir_type_uint
;
1820 wm_prog_key
->texture_data_type
= nir_type_float
;
1823 /* src_samples and dst_samples are the true sample counts */
1824 wm_prog_key
->src_samples
= params
->src
.surf
.samples
;
1825 wm_prog_key
->dst_samples
= params
->dst
.surf
.samples
;
1827 wm_prog_key
->tex_aux_usage
= params
->src
.aux_usage
;
1829 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1832 wm_prog_key
->src_layout
= params
->src
.surf
.msaa_layout
;
1833 wm_prog_key
->dst_layout
= params
->dst
.surf
.msaa_layout
;
1835 /* Round floating point values to nearest integer to avoid "off by one texel"
1836 * kind of errors when blitting.
1838 params
->x0
= params
->wm_inputs
.discard_rect
.x0
= round(coords
->x
.dst0
);
1839 params
->y0
= params
->wm_inputs
.discard_rect
.y0
= round(coords
->y
.dst0
);
1840 params
->x1
= params
->wm_inputs
.discard_rect
.x1
= round(coords
->x
.dst1
);
1841 params
->y1
= params
->wm_inputs
.discard_rect
.y1
= round(coords
->y
.dst1
);
1843 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[0],
1844 coords
->x
.src0
, coords
->x
.src1
,
1845 coords
->x
.dst0
, coords
->x
.dst1
,
1847 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[1],
1848 coords
->y
.src0
, coords
->y
.src1
,
1849 coords
->y
.dst0
, coords
->y
.dst1
,
1853 if (devinfo
->gen
== 4) {
1854 /* The MinLOD and MinimumArrayElement don't work properly for cube maps.
1855 * Convert them to a single slice on gen4.
1857 if (params
->dst
.surf
.usage
& ISL_SURF_USAGE_CUBE_BIT
) {
1858 blorp_surf_convert_to_single_slice(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1859 wm_prog_key
->need_dst_offset
= true;
1862 if (params
->src
.surf
.usage
& ISL_SURF_USAGE_CUBE_BIT
) {
1863 blorp_surf_convert_to_single_slice(batch
->blorp
->isl_dev
, ¶ms
->src
);
1864 wm_prog_key
->need_src_offset
= true;
1868 if (devinfo
->gen
> 6 &&
1869 params
->dst
.surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1870 assert(params
->dst
.surf
.samples
> 1);
1872 /* We must expand the rectangle we send through the rendering pipeline,
1873 * to account for the fact that we are mapping the destination region as
1874 * single-sampled when it is in fact multisampled. We must also align
1875 * it to a multiple of the multisampling pattern, because the
1876 * differences between multisampled and single-sampled surface formats
1877 * will mean that pixels are scrambled within the multisampling pattern.
1878 * TODO: what if this makes the coordinates too large?
1880 * Note: this only works if the destination surface uses the IMS layout.
1881 * If it's UMS, then we have no choice but to set up the rendering
1882 * pipeline as multisampled.
1884 struct isl_extent2d px_size_sa
=
1885 isl_get_interleaved_msaa_px_size_sa(params
->dst
.surf
.samples
);
1886 params
->x0
= ROUND_DOWN_TO(params
->x0
, 2) * px_size_sa
.width
;
1887 params
->y0
= ROUND_DOWN_TO(params
->y0
, 2) * px_size_sa
.height
;
1888 params
->x1
= ALIGN(params
->x1
, 2) * px_size_sa
.width
;
1889 params
->y1
= ALIGN(params
->y1
, 2) * px_size_sa
.height
;
1891 surf_fake_interleaved_msaa(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1893 wm_prog_key
->use_kill
= true;
1894 wm_prog_key
->need_dst_offset
= true;
1897 if (params
->dst
.surf
.tiling
== ISL_TILING_W
) {
1898 /* We must modify the rectangle we send through the rendering pipeline
1899 * (and the size and x/y offset of the destination surface), to account
1900 * for the fact that we are mapping it as Y-tiled when it is in fact
1903 * Both Y tiling and W tiling can be understood as organizations of
1904 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1905 * is different, but the layout of the 32-byte sub-tiles within the 4k
1906 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1907 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1908 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1910 * Therefore, to account for the layout differences within the 32-byte
1911 * sub-tiles, we must expand the rectangle so the X coordinates of its
1912 * edges are multiples of 8 (the W sub-tile width), and its Y
1913 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1914 * Then we need to scale the X and Y coordinates of the rectangle to
1915 * account for the differences in aspect ratio between the Y and W
1916 * sub-tiles. We need to modify the layer width and height similarly.
1918 * A correction needs to be applied when MSAA is in use: since
1919 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1920 * we need to align the Y coordinates to multiples of 8, so that when
1921 * they are divided by two they are still multiples of 4.
1923 * Note: Since the x/y offset of the surface will be applied using the
1924 * SURFACE_STATE command packet, it will be invisible to the swizzling
1925 * code in the shader; therefore it needs to be in a multiple of the
1926 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1927 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1928 * buffer), and the miplevel alignment used for stencil buffers is 8
1929 * pixels horizontally and either 4 or 8 pixels vertically (see
1930 * intel_horizontal_texture_alignment_unit() and
1931 * intel_vertical_texture_alignment_unit()).
1933 * Note: Also, since the SURFACE_STATE command packet can only apply
1934 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1935 * vertically, it is important that the offsets will be multiples of
1936 * these sizes after they are converted into Y-tiled coordinates.
1937 * Fortunately they will be, since we know from above that the offsets
1938 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1939 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1941 * TODO: what if this makes the coordinates (or the texture size) too
1944 const unsigned x_align
= 8;
1945 const unsigned y_align
= params
->dst
.surf
.samples
!= 0 ? 8 : 4;
1946 params
->x0
= ROUND_DOWN_TO(params
->x0
, x_align
) * 2;
1947 params
->y0
= ROUND_DOWN_TO(params
->y0
, y_align
) / 2;
1948 params
->x1
= ALIGN(params
->x1
, x_align
) * 2;
1949 params
->y1
= ALIGN(params
->y1
, y_align
) / 2;
1951 /* Retile the surface to Y-tiled */
1952 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1954 wm_prog_key
->dst_tiled_w
= true;
1955 wm_prog_key
->use_kill
= true;
1956 wm_prog_key
->need_dst_offset
= true;
1958 if (params
->dst
.surf
.samples
> 1) {
1959 /* If the destination surface is a W-tiled multisampled stencil
1960 * buffer that we're mapping as Y tiled, then we need to arrange for
1961 * the WM program to run once per sample rather than once per pixel,
1962 * because the memory layout of related samples doesn't match between
1965 wm_prog_key
->persample_msaa_dispatch
= true;
1969 if (devinfo
->gen
< 8 && params
->src
.surf
.tiling
== ISL_TILING_W
) {
1970 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1971 * Broadwell adds support for sampling from stencil.
1973 * See the comments above concerning x/y offset alignment for the
1974 * destination surface.
1976 * TODO: what if this makes the texture size too large?
1978 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->src
);
1980 wm_prog_key
->src_tiled_w
= true;
1981 wm_prog_key
->need_src_offset
= true;
1984 /* tex_samples and rt_samples are the sample counts that are set up in
1987 wm_prog_key
->tex_samples
= params
->src
.surf
.samples
;
1988 wm_prog_key
->rt_samples
= params
->dst
.surf
.samples
;
1990 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1991 * use to access the source and destination surfaces.
1993 wm_prog_key
->tex_layout
= params
->src
.surf
.msaa_layout
;
1994 wm_prog_key
->rt_layout
= params
->dst
.surf
.msaa_layout
;
1996 if (params
->src
.surf
.samples
> 0 && params
->dst
.surf
.samples
> 1) {
1997 /* We are blitting from a multisample buffer to a multisample buffer, so
1998 * we must preserve samples within a pixel. This means we have to
1999 * arrange for the WM program to run once per sample rather than once
2002 wm_prog_key
->persample_msaa_dispatch
= true;
2005 params
->num_samples
= params
->dst
.surf
.samples
;
2007 if ((wm_prog_key
->filter
== BLORP_FILTER_AVERAGE
||
2008 wm_prog_key
->filter
== BLORP_FILTER_BILINEAR
) &&
2009 batch
->blorp
->isl_dev
->info
->gen
<= 6) {
2010 /* Gen4-5 don't support non-normalized texture coordinates */
2011 wm_prog_key
->src_coords_normalized
= true;
2012 params
->wm_inputs
.src_inv_size
[0] =
2013 1.0f
/ minify(params
->src
.surf
.logical_level0_px
.width
,
2014 params
->src
.view
.base_level
);
2015 params
->wm_inputs
.src_inv_size
[1] =
2016 1.0f
/ minify(params
->src
.surf
.logical_level0_px
.height
,
2017 params
->src
.view
.base_level
);
2020 if (isl_format_get_layout(params
->dst
.view
.format
)->bpb
% 3 == 0) {
2021 /* We can't render to RGB formats natively because they aren't a
2022 * power-of-two size. Instead, we fake them by using a red format
2023 * with the same channel type and size and emitting shader code to
2024 * only write one channel at a time.
2029 /* If it happens to be sRGB, we need to force a conversion */
2030 if (params
->dst
.view
.format
== ISL_FORMAT_R8G8B8_UNORM_SRGB
)
2031 wm_prog_key
->dst_format
= ISL_FORMAT_R8G8B8_UNORM_SRGB
;
2033 surf_fake_rgb_with_red(batch
->blorp
->isl_dev
, ¶ms
->dst
);
2035 wm_prog_key
->dst_rgb
= true;
2036 wm_prog_key
->need_dst_offset
= true;
2037 } else if (isl_format_is_rgbx(params
->dst
.view
.format
)) {
2038 /* We can handle RGBX formats easily enough by treating them as RGBA */
2039 params
->dst
.view
.format
=
2040 isl_format_rgbx_to_rgba(params
->dst
.view
.format
);
2041 } else if (params
->dst
.view
.format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
) {
2042 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
2043 params
->dst
.view
.format
= ISL_FORMAT_R32_UINT
;
2044 } else if (params
->dst
.view
.format
== ISL_FORMAT_A4B4G4R4_UNORM
) {
2045 params
->dst
.view
.swizzle
=
2046 isl_swizzle_compose(params
->dst
.view
.swizzle
,
2047 ISL_SWIZZLE(ALPHA
, RED
, GREEN
, BLUE
));
2048 params
->dst
.view
.format
= ISL_FORMAT_B4G4R4A4_UNORM
;
2049 } else if (params
->dst
.view
.format
== ISL_FORMAT_L8_UNORM_SRGB
) {
2050 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
2051 params
->dst
.view
.format
= ISL_FORMAT_R8_UNORM
;
2052 } else if (params
->dst
.view
.format
== ISL_FORMAT_R9G9B9E5_SHAREDEXP
) {
2053 wm_prog_key
->dst_format
= params
->dst
.view
.format
;
2054 params
->dst
.view
.format
= ISL_FORMAT_R32_UINT
;
2057 if (devinfo
->gen
<= 7 && !devinfo
->is_haswell
&&
2058 !isl_swizzle_is_identity(params
->src
.view
.swizzle
)) {
2059 wm_prog_key
->src_swizzle
= params
->src
.view
.swizzle
;
2060 params
->src
.view
.swizzle
= ISL_SWIZZLE_IDENTITY
;
2062 wm_prog_key
->src_swizzle
= ISL_SWIZZLE_IDENTITY
;
2065 if (!isl_swizzle_supports_rendering(devinfo
, params
->dst
.view
.swizzle
)) {
2066 wm_prog_key
->dst_swizzle
= params
->dst
.view
.swizzle
;
2067 params
->dst
.view
.swizzle
= ISL_SWIZZLE_IDENTITY
;
2069 wm_prog_key
->dst_swizzle
= ISL_SWIZZLE_IDENTITY
;
2072 if (params
->src
.tile_x_sa
|| params
->src
.tile_y_sa
) {
2073 assert(wm_prog_key
->need_src_offset
);
2074 surf_get_intratile_offset_px(¶ms
->src
,
2075 ¶ms
->wm_inputs
.src_offset
.x
,
2076 ¶ms
->wm_inputs
.src_offset
.y
);
2079 if (params
->dst
.tile_x_sa
|| params
->dst
.tile_y_sa
) {
2080 assert(wm_prog_key
->need_dst_offset
);
2081 surf_get_intratile_offset_px(¶ms
->dst
,
2082 ¶ms
->wm_inputs
.dst_offset
.x
,
2083 ¶ms
->wm_inputs
.dst_offset
.y
);
2084 params
->x0
+= params
->wm_inputs
.dst_offset
.x
;
2085 params
->y0
+= params
->wm_inputs
.dst_offset
.y
;
2086 params
->x1
+= params
->wm_inputs
.dst_offset
.x
;
2087 params
->y1
+= params
->wm_inputs
.dst_offset
.y
;
2090 /* For some texture types, we need to pass the layer through the sampler. */
2091 params
->wm_inputs
.src_z
= params
->src
.z_offset
;
2093 if (!brw_blorp_get_blit_kernel(batch
, params
, wm_prog_key
))
2096 if (!blorp_ensure_sf_program(batch
, params
))
2099 unsigned result
= 0;
2100 unsigned max_surface_size
= get_max_surface_size(devinfo
, params
);
2101 if (params
->src
.surf
.logical_level0_px
.width
> max_surface_size
||
2102 params
->dst
.surf
.logical_level0_px
.width
> max_surface_size
)
2103 result
|= BLIT_WIDTH_SHRINK
;
2104 if (params
->src
.surf
.logical_level0_px
.height
> max_surface_size
||
2105 params
->dst
.surf
.logical_level0_px
.height
> max_surface_size
)
2106 result
|= BLIT_HEIGHT_SHRINK
;
2109 batch
->blorp
->exec(batch
, params
);
2115 /* Adjust split blit source coordinates for the current destination
2119 adjust_split_source_coords(const struct blt_axis
*orig
,
2120 struct blt_axis
*split_coords
,
2123 /* When scale is greater than 0, then we are growing from the start, so
2124 * src0 uses delta0, and src1 uses delta1. When scale is less than 0, the
2125 * source range shrinks from the end. In that case src0 is adjusted by
2126 * delta1, and src1 is adjusted by delta0.
2128 double delta0
= scale
* (split_coords
->dst0
- orig
->dst0
);
2129 double delta1
= scale
* (split_coords
->dst1
- orig
->dst1
);
2130 split_coords
->src0
= orig
->src0
+ (scale
>= 0.0 ? delta0
: delta1
);
2131 split_coords
->src1
= orig
->src1
+ (scale
>= 0.0 ? delta1
: delta0
);
2134 static struct isl_extent2d
2135 get_px_size_sa(const struct isl_surf
*surf
)
2137 static const struct isl_extent2d one_to_one
= { .w
= 1, .h
= 1 };
2139 if (surf
->msaa_layout
!= ISL_MSAA_LAYOUT_INTERLEAVED
)
2142 return isl_get_interleaved_msaa_px_size_sa(surf
->samples
);
2146 shrink_surface_params(const struct isl_device
*dev
,
2147 struct brw_blorp_surface_info
*info
,
2148 double *x0
, double *x1
, double *y0
, double *y1
)
2150 uint32_t byte_offset
, x_offset_sa
, y_offset_sa
, size
;
2151 struct isl_extent2d px_size_sa
;
2154 blorp_surf_convert_to_single_slice(dev
, info
);
2156 px_size_sa
= get_px_size_sa(&info
->surf
);
2158 /* Because this gets called after we lower compressed images, the tile
2159 * offsets may be non-zero and we need to incorporate them in our
2162 x_offset_sa
= (uint32_t)*x0
* px_size_sa
.w
+ info
->tile_x_sa
;
2163 y_offset_sa
= (uint32_t)*y0
* px_size_sa
.h
+ info
->tile_y_sa
;
2164 isl_tiling_get_intratile_offset_sa(info
->surf
.tiling
,
2165 info
->surf
.format
, info
->surf
.row_pitch_B
,
2166 x_offset_sa
, y_offset_sa
,
2168 &info
->tile_x_sa
, &info
->tile_y_sa
);
2170 info
->addr
.offset
+= byte_offset
;
2172 adjust
= (int)info
->tile_x_sa
/ px_size_sa
.w
- (int)*x0
;
2175 info
->tile_x_sa
= 0;
2177 adjust
= (int)info
->tile_y_sa
/ px_size_sa
.h
- (int)*y0
;
2180 info
->tile_y_sa
= 0;
2182 size
= MIN2((uint32_t)ceil(*x1
), info
->surf
.logical_level0_px
.width
);
2183 info
->surf
.logical_level0_px
.width
= size
;
2184 info
->surf
.phys_level0_sa
.width
= size
* px_size_sa
.w
;
2186 size
= MIN2((uint32_t)ceil(*y1
), info
->surf
.logical_level0_px
.height
);
2187 info
->surf
.logical_level0_px
.height
= size
;
2188 info
->surf
.phys_level0_sa
.height
= size
* px_size_sa
.h
;
2192 shrink_surfaces(const struct isl_device
*dev
,
2193 struct blorp_params
*params
,
2194 struct brw_blorp_blit_prog_key
*wm_prog_key
,
2195 struct blt_coords
*coords
)
2197 /* Shrink source surface */
2198 shrink_surface_params(dev
, ¶ms
->src
, &coords
->x
.src0
, &coords
->x
.src1
,
2199 &coords
->y
.src0
, &coords
->y
.src1
);
2200 wm_prog_key
->need_src_offset
= false;
2202 /* Shrink destination surface */
2203 shrink_surface_params(dev
, ¶ms
->dst
, &coords
->x
.dst0
, &coords
->x
.dst1
,
2204 &coords
->y
.dst0
, &coords
->y
.dst1
);
2205 wm_prog_key
->need_dst_offset
= false;
2209 do_blorp_blit(struct blorp_batch
*batch
,
2210 const struct blorp_params
*orig_params
,
2211 struct brw_blorp_blit_prog_key
*wm_prog_key
,
2212 const struct blt_coords
*orig
)
2214 struct blorp_params params
;
2215 struct blt_coords blit_coords
;
2216 struct blt_coords split_coords
= *orig
;
2217 double w
= orig
->x
.dst1
- orig
->x
.dst0
;
2218 double h
= orig
->y
.dst1
- orig
->y
.dst0
;
2219 double x_scale
= (orig
->x
.src1
- orig
->x
.src0
) / w
;
2220 double y_scale
= (orig
->y
.src1
- orig
->y
.src0
) / h
;
2226 bool x_done
, y_done
;
2227 bool shrink
= split_blorp_blit_debug
&& can_shrink_surfaces(orig_params
);
2229 params
= *orig_params
;
2230 blit_coords
= split_coords
;
2232 shrink_surfaces(batch
->blorp
->isl_dev
, ¶ms
, wm_prog_key
,
2234 enum blit_shrink_status result
=
2235 try_blorp_blit(batch
, ¶ms
, wm_prog_key
, &blit_coords
);
2237 if (result
& BLIT_WIDTH_SHRINK
) {
2240 split_coords
.x
.dst1
= MIN2(split_coords
.x
.dst0
+ w
, orig
->x
.dst1
);
2241 adjust_split_source_coords(&orig
->x
, &split_coords
.x
, x_scale
);
2243 if (result
& BLIT_HEIGHT_SHRINK
) {
2246 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2247 adjust_split_source_coords(&orig
->y
, &split_coords
.y
, y_scale
);
2251 assert(can_shrink_surfaces(orig_params
));
2256 y_done
= (orig
->y
.dst1
- split_coords
.y
.dst1
< 0.5);
2257 x_done
= y_done
&& (orig
->x
.dst1
- split_coords
.x
.dst1
< 0.5);
2260 } else if (y_done
) {
2261 split_coords
.x
.dst0
+= w
;
2262 split_coords
.x
.dst1
= MIN2(split_coords
.x
.dst0
+ w
, orig
->x
.dst1
);
2263 split_coords
.y
.dst0
= orig
->y
.dst0
;
2264 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2265 adjust_split_source_coords(&orig
->x
, &split_coords
.x
, x_scale
);
2267 split_coords
.y
.dst0
+= h
;
2268 split_coords
.y
.dst1
= MIN2(split_coords
.y
.dst0
+ h
, orig
->y
.dst1
);
2269 adjust_split_source_coords(&orig
->y
, &split_coords
.y
, y_scale
);
2275 blorp_blit(struct blorp_batch
*batch
,
2276 const struct blorp_surf
*src_surf
,
2277 unsigned src_level
, unsigned src_layer
,
2278 enum isl_format src_format
, struct isl_swizzle src_swizzle
,
2279 const struct blorp_surf
*dst_surf
,
2280 unsigned dst_level
, unsigned dst_layer
,
2281 enum isl_format dst_format
, struct isl_swizzle dst_swizzle
,
2282 float src_x0
, float src_y0
,
2283 float src_x1
, float src_y1
,
2284 float dst_x0
, float dst_y0
,
2285 float dst_x1
, float dst_y1
,
2286 enum blorp_filter filter
,
2287 bool mirror_x
, bool mirror_y
)
2289 struct blorp_params params
;
2290 blorp_params_init(¶ms
);
2292 /* We cannot handle combined depth and stencil. */
2293 if (src_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
)
2294 assert(src_surf
->surf
->format
== ISL_FORMAT_R8_UINT
);
2295 if (dst_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
)
2296 assert(dst_surf
->surf
->format
== ISL_FORMAT_R8_UINT
);
2298 if (dst_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
) {
2299 assert(src_surf
->surf
->usage
& ISL_SURF_USAGE_STENCIL_BIT
);
2300 /* Prior to Broadwell, we can't render to R8_UINT */
2301 if (batch
->blorp
->isl_dev
->info
->gen
< 8) {
2302 src_format
= ISL_FORMAT_R8_UNORM
;
2303 dst_format
= ISL_FORMAT_R8_UNORM
;
2307 /* ISL_FORMAT_R24_UNORM_X8_TYPELESS it isn't supported as a render target,
2308 * which requires shader math to render to it. Blitting Z24X8 to Z24X8
2309 * is fairly common though, so we'd like to avoid it. Since we don't need
2310 * to blend depth values, we can simply pick a renderable format with the
2311 * right number of bits-per-pixel, like 8-bit BGRA.
2313 if (dst_surf
->surf
->format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
&&
2314 src_surf
->surf
->format
== ISL_FORMAT_R24_UNORM_X8_TYPELESS
) {
2315 src_format
= dst_format
= ISL_FORMAT_B8G8R8A8_UNORM
;
2318 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
2319 src_layer
, src_format
, false);
2320 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
2321 dst_layer
, dst_format
, true);
2323 params
.src
.view
.swizzle
= src_swizzle
;
2324 params
.dst
.view
.swizzle
= dst_swizzle
;
2326 const struct isl_format_layout
*src_fmtl
=
2327 isl_format_get_layout(params
.src
.view
.format
);
2329 struct brw_blorp_blit_prog_key wm_prog_key
= {
2330 .shader_type
= BLORP_SHADER_TYPE_BLIT
,
2332 .sint32_to_uint
= src_fmtl
->channels
.r
.bits
== 32 &&
2333 isl_format_has_sint_channel(params
.src
.view
.format
) &&
2334 isl_format_has_uint_channel(params
.dst
.view
.format
),
2335 .uint32_to_sint
= src_fmtl
->channels
.r
.bits
== 32 &&
2336 isl_format_has_uint_channel(params
.src
.view
.format
) &&
2337 isl_format_has_sint_channel(params
.dst
.view
.format
),
2340 /* Scaling factors used for bilinear filtering in multisample scaled
2343 if (params
.src
.surf
.samples
== 16)
2344 wm_prog_key
.x_scale
= 4.0f
;
2346 wm_prog_key
.x_scale
= 2.0f
;
2347 wm_prog_key
.y_scale
= params
.src
.surf
.samples
/ wm_prog_key
.x_scale
;
2349 params
.wm_inputs
.rect_grid
.x1
=
2350 minify(params
.src
.surf
.logical_level0_px
.width
, src_level
) *
2351 wm_prog_key
.x_scale
- 1.0f
;
2352 params
.wm_inputs
.rect_grid
.y1
=
2353 minify(params
.src
.surf
.logical_level0_px
.height
, src_level
) *
2354 wm_prog_key
.y_scale
- 1.0f
;
2356 struct blt_coords coords
= {
2373 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
, &coords
);
2376 static enum isl_format
2377 get_copy_format_for_bpb(const struct isl_device
*isl_dev
, unsigned bpb
)
2379 /* The choice of UNORM and UINT formats is very intentional here. Most
2380 * of the time, we want to use a UINT format to avoid any rounding error
2381 * in the blit. For stencil blits, R8_UINT is required by the hardware.
2382 * (It's the only format allowed in conjunction with W-tiling.) Also we
2383 * intentionally use the 4-channel formats whenever we can. This is so
2384 * that, when we do a RGB <-> RGBX copy, the two formats will line up
2385 * even though one of them is 3/4 the size of the other. The choice of
2386 * UNORM vs. UINT is also very intentional because we don't have 8 or
2387 * 16-bit RGB UINT formats until Sky Lake so we have to use UNORM there.
2388 * Fortunately, the only time we should ever use two different formats in
2389 * the table below is for RGB -> RGBA blits and so we will never have any
2390 * UNORM/UINT mismatch.
2392 if (ISL_DEV_GEN(isl_dev
) >= 9) {
2394 case 8: return ISL_FORMAT_R8_UINT
;
2395 case 16: return ISL_FORMAT_R8G8_UINT
;
2396 case 24: return ISL_FORMAT_R8G8B8_UINT
;
2397 case 32: return ISL_FORMAT_R8G8B8A8_UINT
;
2398 case 48: return ISL_FORMAT_R16G16B16_UINT
;
2399 case 64: return ISL_FORMAT_R16G16B16A16_UINT
;
2400 case 96: return ISL_FORMAT_R32G32B32_UINT
;
2401 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
2403 unreachable("Unknown format bpb");
2407 case 8: return ISL_FORMAT_R8_UINT
;
2408 case 16: return ISL_FORMAT_R8G8_UINT
;
2409 case 24: return ISL_FORMAT_R8G8B8_UNORM
;
2410 case 32: return ISL_FORMAT_R8G8B8A8_UNORM
;
2411 case 48: return ISL_FORMAT_R16G16B16_UNORM
;
2412 case 64: return ISL_FORMAT_R16G16B16A16_UNORM
;
2413 case 96: return ISL_FORMAT_R32G32B32_UINT
;
2414 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
2416 unreachable("Unknown format bpb");
2421 /** Returns a UINT format that is CCS-compatible with the given format
2423 * The PRM's say absolutely nothing about how render compression works. The
2424 * only thing they provide is a list of formats on which it is and is not
2425 * supported. Empirical testing indicates that the compression is only based
2426 * on the bit-layout of the format and the channel encoding doesn't matter.
2427 * So, while texture views don't work in general, you can create a view as
2428 * long as the bit-layout of the formats are the same.
2430 * Fortunately, for every render compression capable format, the UINT format
2431 * with the same bit layout also supports render compression. This means that
2432 * we only need to handle UINT formats for copy operations. In order to do
2433 * copies between formats with different bit layouts, we attach both with a
2434 * UINT format and use bit_cast_color() to generate code to do the bit-cast
2435 * operation between the two bit layouts.
2437 static enum isl_format
2438 get_ccs_compatible_uint_format(const struct isl_format_layout
*fmtl
)
2440 switch (fmtl
->format
) {
2441 case ISL_FORMAT_R32G32B32A32_FLOAT
:
2442 case ISL_FORMAT_R32G32B32A32_SINT
:
2443 case ISL_FORMAT_R32G32B32A32_UINT
:
2444 case ISL_FORMAT_R32G32B32A32_UNORM
:
2445 case ISL_FORMAT_R32G32B32A32_SNORM
:
2446 case ISL_FORMAT_R32G32B32X32_FLOAT
:
2447 return ISL_FORMAT_R32G32B32A32_UINT
;
2449 case ISL_FORMAT_R16G16B16A16_UNORM
:
2450 case ISL_FORMAT_R16G16B16A16_SNORM
:
2451 case ISL_FORMAT_R16G16B16A16_SINT
:
2452 case ISL_FORMAT_R16G16B16A16_UINT
:
2453 case ISL_FORMAT_R16G16B16A16_FLOAT
:
2454 case ISL_FORMAT_R16G16B16X16_UNORM
:
2455 case ISL_FORMAT_R16G16B16X16_FLOAT
:
2456 return ISL_FORMAT_R16G16B16A16_UINT
;
2458 case ISL_FORMAT_R32G32_FLOAT
:
2459 case ISL_FORMAT_R32G32_SINT
:
2460 case ISL_FORMAT_R32G32_UINT
:
2461 case ISL_FORMAT_R32G32_UNORM
:
2462 case ISL_FORMAT_R32G32_SNORM
:
2463 return ISL_FORMAT_R32G32_UINT
;
2465 case ISL_FORMAT_B8G8R8A8_UNORM
:
2466 case ISL_FORMAT_B8G8R8A8_UNORM_SRGB
:
2467 case ISL_FORMAT_R8G8B8A8_UNORM
:
2468 case ISL_FORMAT_R8G8B8A8_UNORM_SRGB
:
2469 case ISL_FORMAT_R8G8B8A8_SNORM
:
2470 case ISL_FORMAT_R8G8B8A8_SINT
:
2471 case ISL_FORMAT_R8G8B8A8_UINT
:
2472 case ISL_FORMAT_B8G8R8X8_UNORM
:
2473 case ISL_FORMAT_B8G8R8X8_UNORM_SRGB
:
2474 case ISL_FORMAT_R8G8B8X8_UNORM
:
2475 case ISL_FORMAT_R8G8B8X8_UNORM_SRGB
:
2476 return ISL_FORMAT_R8G8B8A8_UINT
;
2478 case ISL_FORMAT_R16G16_UNORM
:
2479 case ISL_FORMAT_R16G16_SNORM
:
2480 case ISL_FORMAT_R16G16_SINT
:
2481 case ISL_FORMAT_R16G16_UINT
:
2482 case ISL_FORMAT_R16G16_FLOAT
:
2483 return ISL_FORMAT_R16G16_UINT
;
2485 case ISL_FORMAT_R32_SINT
:
2486 case ISL_FORMAT_R32_UINT
:
2487 case ISL_FORMAT_R32_FLOAT
:
2488 case ISL_FORMAT_R32_UNORM
:
2489 case ISL_FORMAT_R32_SNORM
:
2490 return ISL_FORMAT_R32_UINT
;
2492 case ISL_FORMAT_B10G10R10A2_UNORM
:
2493 case ISL_FORMAT_B10G10R10A2_UNORM_SRGB
:
2494 case ISL_FORMAT_R10G10B10A2_UNORM
:
2495 case ISL_FORMAT_R10G10B10A2_UINT
:
2496 return ISL_FORMAT_R10G10B10A2_UINT
;
2499 unreachable("Not a compressible format");
2504 blorp_surf_convert_to_uncompressed(const struct isl_device
*isl_dev
,
2505 struct brw_blorp_surface_info
*info
,
2506 uint32_t *x
, uint32_t *y
,
2507 uint32_t *width
, uint32_t *height
)
2509 const struct isl_format_layout
*fmtl
=
2510 isl_format_get_layout(info
->surf
.format
);
2512 assert(fmtl
->bw
> 1 || fmtl
->bh
> 1);
2514 /* This is a compressed surface. We need to convert it to a single
2515 * slice (because compressed layouts don't perfectly match uncompressed
2516 * ones with the same bpb) and divide x, y, width, and height by the
2519 blorp_surf_convert_to_single_slice(isl_dev
, info
);
2521 if (width
&& height
) {
2523 uint32_t right_edge_px
= info
->tile_x_sa
+ *x
+ *width
;
2524 uint32_t bottom_edge_px
= info
->tile_y_sa
+ *y
+ *height
;
2525 assert(*width
% fmtl
->bw
== 0 ||
2526 right_edge_px
== info
->surf
.logical_level0_px
.width
);
2527 assert(*height
% fmtl
->bh
== 0 ||
2528 bottom_edge_px
== info
->surf
.logical_level0_px
.height
);
2530 *width
= DIV_ROUND_UP(*width
, fmtl
->bw
);
2531 *height
= DIV_ROUND_UP(*height
, fmtl
->bh
);
2535 assert(*x
% fmtl
->bw
== 0);
2536 assert(*y
% fmtl
->bh
== 0);
2541 info
->surf
.logical_level0_px
= isl_surf_get_logical_level0_el(&info
->surf
);
2542 info
->surf
.phys_level0_sa
= isl_surf_get_phys_level0_el(&info
->surf
);
2544 assert(info
->tile_x_sa
% fmtl
->bw
== 0);
2545 assert(info
->tile_y_sa
% fmtl
->bh
== 0);
2546 info
->tile_x_sa
/= fmtl
->bw
;
2547 info
->tile_y_sa
/= fmtl
->bh
;
2549 /* It's now an uncompressed surface so we need an uncompressed format */
2550 info
->surf
.format
= get_copy_format_for_bpb(isl_dev
, fmtl
->bpb
);
2554 blorp_copy(struct blorp_batch
*batch
,
2555 const struct blorp_surf
*src_surf
,
2556 unsigned src_level
, unsigned src_layer
,
2557 const struct blorp_surf
*dst_surf
,
2558 unsigned dst_level
, unsigned dst_layer
,
2559 uint32_t src_x
, uint32_t src_y
,
2560 uint32_t dst_x
, uint32_t dst_y
,
2561 uint32_t src_width
, uint32_t src_height
)
2563 const struct isl_device
*isl_dev
= batch
->blorp
->isl_dev
;
2564 struct blorp_params params
;
2566 if (src_width
== 0 || src_height
== 0)
2569 blorp_params_init(¶ms
);
2570 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
2571 src_layer
, ISL_FORMAT_UNSUPPORTED
, false);
2572 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
2573 dst_layer
, ISL_FORMAT_UNSUPPORTED
, true);
2575 struct brw_blorp_blit_prog_key wm_prog_key
= {
2576 .shader_type
= BLORP_SHADER_TYPE_BLIT
,
2577 .filter
= BLORP_FILTER_NONE
,
2578 .need_src_offset
= src_surf
->tile_x_sa
|| src_surf
->tile_y_sa
,
2579 .need_dst_offset
= dst_surf
->tile_x_sa
|| dst_surf
->tile_y_sa
,
2582 const struct isl_format_layout
*src_fmtl
=
2583 isl_format_get_layout(params
.src
.surf
.format
);
2584 const struct isl_format_layout
*dst_fmtl
=
2585 isl_format_get_layout(params
.dst
.surf
.format
);
2587 assert(params
.src
.aux_usage
== ISL_AUX_USAGE_NONE
||
2588 params
.src
.aux_usage
== ISL_AUX_USAGE_MCS
||
2589 params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
);
2590 assert(params
.dst
.aux_usage
== ISL_AUX_USAGE_NONE
||
2591 params
.dst
.aux_usage
== ISL_AUX_USAGE_MCS
||
2592 params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
);
2594 if (params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2595 params
.dst
.view
.format
= get_ccs_compatible_uint_format(dst_fmtl
);
2596 if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2597 params
.src
.view
.format
= get_ccs_compatible_uint_format(src_fmtl
);
2598 } else if (src_fmtl
->bpb
== dst_fmtl
->bpb
) {
2599 params
.src
.view
.format
= params
.dst
.view
.format
;
2601 params
.src
.view
.format
=
2602 get_copy_format_for_bpb(isl_dev
, src_fmtl
->bpb
);
2604 } else if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2605 params
.src
.view
.format
= get_ccs_compatible_uint_format(src_fmtl
);
2606 if (src_fmtl
->bpb
== dst_fmtl
->bpb
) {
2607 params
.dst
.view
.format
= params
.src
.view
.format
;
2609 params
.dst
.view
.format
=
2610 get_copy_format_for_bpb(isl_dev
, dst_fmtl
->bpb
);
2613 params
.dst
.view
.format
= get_copy_format_for_bpb(isl_dev
, dst_fmtl
->bpb
);
2614 params
.src
.view
.format
= get_copy_format_for_bpb(isl_dev
, src_fmtl
->bpb
);
2617 if (params
.src
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2618 /* It's safe to do a blorp_copy between things which are sRGB with CCS_E
2619 * enabled even though CCS_E doesn't technically do sRGB on SKL because
2620 * we stomp everything to UINT anyway. The one thing we have to be
2621 * careful of is clear colors. Because fast clear colors for sRGB on
2622 * gen9 are encoded as the float values between format conversion and
2623 * sRGB curve application, a given clear color float will convert to the
2624 * same bits regardless of whether the format is UNORM or sRGB.
2625 * Therefore, we can handle sRGB without any special cases.
2627 UNUSED
enum isl_format linear_src_format
=
2628 isl_format_srgb_to_linear(src_surf
->surf
->format
);
2629 assert(isl_formats_are_ccs_e_compatible(batch
->blorp
->isl_dev
->info
,
2631 params
.src
.view
.format
));
2633 isl_color_value_pack(¶ms
.src
.clear_color
,
2634 linear_src_format
, packed
);
2635 isl_color_value_unpack(¶ms
.src
.clear_color
,
2636 params
.src
.view
.format
, packed
);
2639 if (params
.dst
.aux_usage
== ISL_AUX_USAGE_CCS_E
) {
2640 /* See above where we handle linear_src_format */
2641 UNUSED
enum isl_format linear_dst_format
=
2642 isl_format_srgb_to_linear(dst_surf
->surf
->format
);
2643 assert(isl_formats_are_ccs_e_compatible(batch
->blorp
->isl_dev
->info
,
2645 params
.dst
.view
.format
));
2647 isl_color_value_pack(¶ms
.dst
.clear_color
,
2648 linear_dst_format
, packed
);
2649 isl_color_value_unpack(¶ms
.dst
.clear_color
,
2650 params
.dst
.view
.format
, packed
);
2653 if (params
.src
.view
.format
!= params
.dst
.view
.format
) {
2654 enum isl_format src_cast_format
= params
.src
.view
.format
;
2655 enum isl_format dst_cast_format
= params
.dst
.view
.format
;
2657 /* The BLORP bitcast code gets confused by RGB formats. Just treat them
2658 * as RGBA and then everything will be happy. This is perfectly safe
2659 * because BLORP likes to treat things as if they have vec4 colors all
2662 if (isl_format_is_rgb(src_cast_format
))
2663 src_cast_format
= isl_format_rgb_to_rgba(src_cast_format
);
2664 if (isl_format_is_rgb(dst_cast_format
))
2665 dst_cast_format
= isl_format_rgb_to_rgba(dst_cast_format
);
2667 if (src_cast_format
!= dst_cast_format
) {
2668 wm_prog_key
.format_bit_cast
= true;
2669 wm_prog_key
.src_format
= src_cast_format
;
2670 wm_prog_key
.dst_format
= dst_cast_format
;
2674 if (src_fmtl
->bw
> 1 || src_fmtl
->bh
> 1) {
2675 blorp_surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.src
,
2677 &src_width
, &src_height
);
2678 wm_prog_key
.need_src_offset
= true;
2681 if (dst_fmtl
->bw
> 1 || dst_fmtl
->bh
> 1) {
2682 blorp_surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.dst
,
2683 &dst_x
, &dst_y
, NULL
, NULL
);
2684 wm_prog_key
.need_dst_offset
= true;
2687 /* Once both surfaces are stompped to uncompressed as needed, the
2688 * destination size is the same as the source size.
2690 uint32_t dst_width
= src_width
;
2691 uint32_t dst_height
= src_height
;
2693 struct blt_coords coords
= {
2696 .src1
= src_x
+ src_width
,
2698 .dst1
= dst_x
+ dst_width
,
2703 .src1
= src_y
+ src_height
,
2705 .dst1
= dst_y
+ dst_height
,
2710 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
, &coords
);
2713 static enum isl_format
2714 isl_format_for_size(unsigned size_B
)
2717 case 1: return ISL_FORMAT_R8_UINT
;
2718 case 2: return ISL_FORMAT_R8G8_UINT
;
2719 case 4: return ISL_FORMAT_R8G8B8A8_UINT
;
2720 case 8: return ISL_FORMAT_R16G16B16A16_UINT
;
2721 case 16: return ISL_FORMAT_R32G32B32A32_UINT
;
2723 unreachable("Not a power-of-two format size");
2728 * Returns the greatest common divisor of a and b that is a power of two.
2731 gcd_pow2_u64(uint64_t a
, uint64_t b
)
2733 assert(a
> 0 || b
> 0);
2735 unsigned a_log2
= ffsll(a
) - 1;
2736 unsigned b_log2
= ffsll(b
) - 1;
2738 /* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
2739 * case, the MIN2() will take the other one. If both are 0 then we will
2740 * hit the assert above.
2742 return 1 << MIN2(a_log2
, b_log2
);
2746 do_buffer_copy(struct blorp_batch
*batch
,
2747 struct blorp_address
*src
,
2748 struct blorp_address
*dst
,
2749 int width
, int height
, int block_size
)
2751 /* The actual format we pick doesn't matter as blorp will throw it away.
2752 * The only thing that actually matters is the size.
2754 enum isl_format format
= isl_format_for_size(block_size
);
2757 struct isl_surf surf
;
2758 ok
= isl_surf_init(batch
->blorp
->isl_dev
, &surf
,
2759 .dim
= ISL_SURF_DIM_2D
,
2767 .row_pitch_B
= width
* block_size
,
2768 .usage
= ISL_SURF_USAGE_TEXTURE_BIT
|
2769 ISL_SURF_USAGE_RENDER_TARGET_BIT
,
2770 .tiling_flags
= ISL_TILING_LINEAR_BIT
);
2773 struct blorp_surf src_blorp_surf
= {
2778 struct blorp_surf dst_blorp_surf
= {
2783 blorp_copy(batch
, &src_blorp_surf
, 0, 0, &dst_blorp_surf
, 0, 0,
2784 0, 0, 0, 0, width
, height
);
2788 blorp_buffer_copy(struct blorp_batch
*batch
,
2789 struct blorp_address src
,
2790 struct blorp_address dst
,
2793 const struct gen_device_info
*devinfo
= batch
->blorp
->isl_dev
->info
;
2794 uint64_t copy_size
= size
;
2796 /* This is maximum possible width/height our HW can handle */
2797 uint64_t max_surface_dim
= 1 << (devinfo
->gen
>= 7 ? 14 : 13);
2799 /* First, we compute the biggest format that can be used with the
2800 * given offsets and size.
2803 bs
= gcd_pow2_u64(bs
, src
.offset
);
2804 bs
= gcd_pow2_u64(bs
, dst
.offset
);
2805 bs
= gcd_pow2_u64(bs
, size
);
2807 /* First, we make a bunch of max-sized copies */
2808 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
2809 while (copy_size
>= max_copy_size
) {
2810 do_buffer_copy(batch
, &src
, &dst
, max_surface_dim
, max_surface_dim
, bs
);
2811 copy_size
-= max_copy_size
;
2812 src
.offset
+= max_copy_size
;
2813 dst
.offset
+= max_copy_size
;
2816 /* Now make a max-width copy */
2817 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
2818 assert(height
< max_surface_dim
);
2820 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
2821 do_buffer_copy(batch
, &src
, &dst
, max_surface_dim
, height
, bs
);
2822 copy_size
-= rect_copy_size
;
2823 src
.offset
+= rect_copy_size
;
2824 dst
.offset
+= rect_copy_size
;
2827 /* Finally, make a small copy to finish it off */
2828 if (copy_size
!= 0) {
2829 do_buffer_copy(batch
, &src
, &dst
, copy_size
/ bs
, 1, bs
);