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 "compiler/nir/nir_builder.h"
26 #include "blorp_priv.h"
27 #include "brw_meta_util.h"
29 #define FILE_DEBUG_FLAG DEBUG_BLORP
32 * Enum to specify the order of arguments in a sampler message
34 enum sampler_message_arg
36 SAMPLER_MESSAGE_ARG_U_FLOAT
,
37 SAMPLER_MESSAGE_ARG_V_FLOAT
,
38 SAMPLER_MESSAGE_ARG_U_INT
,
39 SAMPLER_MESSAGE_ARG_V_INT
,
40 SAMPLER_MESSAGE_ARG_R_INT
,
41 SAMPLER_MESSAGE_ARG_SI_INT
,
42 SAMPLER_MESSAGE_ARG_MCS_INT
,
43 SAMPLER_MESSAGE_ARG_ZERO_INT
,
46 struct brw_blorp_blit_vars
{
47 /* Input values from brw_blorp_wm_inputs */
48 nir_variable
*v_discard_rect
;
49 nir_variable
*v_rect_grid
;
50 nir_variable
*v_coord_transform
;
51 nir_variable
*v_src_z
;
52 nir_variable
*v_src_offset
;
53 nir_variable
*v_dst_offset
;
56 nir_variable
*frag_coord
;
59 nir_variable
*color_out
;
63 brw_blorp_blit_vars_init(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
64 const struct brw_blorp_blit_prog_key
*key
)
66 /* Blended and scaled blits never use pixel discard. */
67 assert(!key
->use_kill
|| !(key
->blend
&& key
->blit_scaled
));
69 #define LOAD_INPUT(name, type)\
70 v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
72 LOAD_INPUT(discard_rect
, glsl_vec4_type())
73 LOAD_INPUT(rect_grid
, glsl_vec4_type())
74 LOAD_INPUT(coord_transform
, glsl_vec4_type())
75 LOAD_INPUT(src_z
, glsl_uint_type())
76 LOAD_INPUT(src_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
77 LOAD_INPUT(dst_offset
, glsl_vector_type(GLSL_TYPE_UINT
, 2))
81 v
->frag_coord
= nir_variable_create(b
->shader
, nir_var_shader_in
,
82 glsl_vec4_type(), "gl_FragCoord");
83 v
->frag_coord
->data
.location
= VARYING_SLOT_POS
;
84 v
->frag_coord
->data
.origin_upper_left
= true;
86 v
->color_out
= nir_variable_create(b
->shader
, nir_var_shader_out
,
87 glsl_vec4_type(), "gl_FragColor");
88 v
->color_out
->data
.location
= FRAG_RESULT_COLOR
;
92 blorp_blit_get_frag_coords(nir_builder
*b
,
93 const struct brw_blorp_blit_prog_key
*key
,
94 struct brw_blorp_blit_vars
*v
)
96 nir_ssa_def
*coord
= nir_f2i(b
, nir_load_var(b
, v
->frag_coord
));
98 /* Account for destination surface intratile offset
100 * Transformation parameters giving translation from destination to source
101 * coordinates don't take into account possible intra-tile destination
102 * offset. Therefore it has to be first subtracted from the incoming
103 * coordinates. Vertices are set up based on coordinates containing the
106 if (key
->need_dst_offset
)
107 coord
= nir_isub(b
, coord
, nir_load_var(b
, v
->v_dst_offset
));
109 if (key
->persample_msaa_dispatch
) {
110 return nir_vec3(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1),
111 nir_load_sample_id(b
));
113 return nir_vec2(b
, nir_channel(b
, coord
, 0), nir_channel(b
, coord
, 1));
118 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
122 blorp_blit_apply_transform(nir_builder
*b
, nir_ssa_def
*src_pos
,
123 struct brw_blorp_blit_vars
*v
)
125 nir_ssa_def
*coord_transform
= nir_load_var(b
, v
->v_coord_transform
);
127 nir_ssa_def
*offset
= nir_vec2(b
, nir_channel(b
, coord_transform
, 1),
128 nir_channel(b
, coord_transform
, 3));
129 nir_ssa_def
*mul
= nir_vec2(b
, nir_channel(b
, coord_transform
, 0),
130 nir_channel(b
, coord_transform
, 2));
132 return nir_ffma(b
, src_pos
, mul
, offset
);
136 blorp_nir_discard_if_outside_rect(nir_builder
*b
, nir_ssa_def
*pos
,
137 struct brw_blorp_blit_vars
*v
)
139 nir_ssa_def
*c0
, *c1
, *c2
, *c3
;
140 nir_ssa_def
*discard_rect
= nir_load_var(b
, v
->v_discard_rect
);
141 nir_ssa_def
*dst_x0
= nir_channel(b
, discard_rect
, 0);
142 nir_ssa_def
*dst_x1
= nir_channel(b
, discard_rect
, 1);
143 nir_ssa_def
*dst_y0
= nir_channel(b
, discard_rect
, 2);
144 nir_ssa_def
*dst_y1
= nir_channel(b
, discard_rect
, 3);
146 c0
= nir_ult(b
, nir_channel(b
, pos
, 0), dst_x0
);
147 c1
= nir_uge(b
, nir_channel(b
, pos
, 0), dst_x1
);
148 c2
= nir_ult(b
, nir_channel(b
, pos
, 1), dst_y0
);
149 c3
= nir_uge(b
, nir_channel(b
, pos
, 1), dst_y1
);
151 nir_ssa_def
*oob
= nir_ior(b
, nir_ior(b
, c0
, c1
), nir_ior(b
, c2
, c3
));
153 nir_intrinsic_instr
*discard
=
154 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard_if
);
155 discard
->src
[0] = nir_src_for_ssa(oob
);
156 nir_builder_instr_insert(b
, &discard
->instr
);
159 static nir_tex_instr
*
160 blorp_create_nir_tex_instr(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
161 nir_texop op
, nir_ssa_def
*pos
, unsigned num_srcs
,
162 nir_alu_type dst_type
)
164 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, num_srcs
);
168 tex
->dest_type
= dst_type
;
169 tex
->is_array
= false;
170 tex
->is_shadow
= false;
172 /* Blorp only has one texture and it's bound at unit 0 */
175 tex
->texture_index
= 0;
176 tex
->sampler_index
= 0;
178 /* To properly handle 3-D and 2-D array textures, we pull the Z component
179 * from an input. TODO: This is a bit magic; we should probably make this
180 * more explicit in the future.
182 assert(pos
->num_components
>= 2);
183 pos
= nir_vec3(b
, nir_channel(b
, pos
, 0), nir_channel(b
, pos
, 1),
184 nir_load_var(b
, v
->v_src_z
));
186 tex
->src
[0].src_type
= nir_tex_src_coord
;
187 tex
->src
[0].src
= nir_src_for_ssa(pos
);
188 tex
->coord_components
= 3;
190 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, NULL
);
196 blorp_nir_tex(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
197 nir_ssa_def
*pos
, nir_alu_type dst_type
)
200 blorp_create_nir_tex_instr(b
, v
, nir_texop_tex
, pos
, 2, dst_type
);
202 assert(pos
->num_components
== 2);
203 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
204 tex
->src
[1].src_type
= nir_tex_src_lod
;
205 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
207 nir_builder_instr_insert(b
, &tex
->instr
);
209 return &tex
->dest
.ssa
;
213 blorp_nir_txf(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
214 nir_ssa_def
*pos
, nir_alu_type dst_type
)
217 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf
, pos
, 2, dst_type
);
219 tex
->sampler_dim
= GLSL_SAMPLER_DIM_3D
;
220 tex
->src
[1].src_type
= nir_tex_src_lod
;
221 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
223 nir_builder_instr_insert(b
, &tex
->instr
);
225 return &tex
->dest
.ssa
;
229 blorp_nir_txf_ms(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
230 nir_ssa_def
*pos
, nir_ssa_def
*mcs
, nir_alu_type dst_type
)
233 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms
, pos
,
234 mcs
!= NULL
? 3 : 2, dst_type
);
236 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
238 tex
->src
[1].src_type
= nir_tex_src_ms_index
;
239 if (pos
->num_components
== 2) {
240 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
242 assert(pos
->num_components
== 3);
243 tex
->src
[1].src
= nir_src_for_ssa(nir_channel(b
, pos
, 2));
247 tex
->src
[2].src_type
= nir_tex_src_ms_mcs
;
248 tex
->src
[2].src
= nir_src_for_ssa(mcs
);
251 nir_builder_instr_insert(b
, &tex
->instr
);
253 return &tex
->dest
.ssa
;
257 blorp_nir_txf_ms_mcs(nir_builder
*b
, struct brw_blorp_blit_vars
*v
, nir_ssa_def
*pos
)
260 blorp_create_nir_tex_instr(b
, v
, nir_texop_txf_ms_mcs
,
261 pos
, 1, nir_type_int
);
263 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
265 nir_builder_instr_insert(b
, &tex
->instr
);
267 return &tex
->dest
.ssa
;
271 nir_mask_shift_or(struct nir_builder
*b
, nir_ssa_def
*dst
, nir_ssa_def
*src
,
272 uint32_t src_mask
, int src_left_shift
)
274 nir_ssa_def
*masked
= nir_iand(b
, src
, nir_imm_int(b
, src_mask
));
276 nir_ssa_def
*shifted
;
277 if (src_left_shift
> 0) {
278 shifted
= nir_ishl(b
, masked
, nir_imm_int(b
, src_left_shift
));
279 } else if (src_left_shift
< 0) {
280 shifted
= nir_ushr(b
, masked
, nir_imm_int(b
, -src_left_shift
));
282 assert(src_left_shift
== 0);
286 return nir_ior(b
, dst
, shifted
);
290 * Emit code to compensate for the difference between Y and W tiling.
292 * This code modifies the X and Y coordinates according to the formula:
294 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
296 * (See brw_blorp_build_nir_shader).
298 static inline nir_ssa_def
*
299 blorp_nir_retile_y_to_w(nir_builder
*b
, nir_ssa_def
*pos
)
301 assert(pos
->num_components
== 2);
302 nir_ssa_def
*x_Y
= nir_channel(b
, pos
, 0);
303 nir_ssa_def
*y_Y
= nir_channel(b
, pos
, 1);
305 /* Given X and Y coordinates that describe an address using Y tiling,
306 * translate to the X and Y coordinates that describe the same address
309 * If we break down the low order bits of X and Y, using a
310 * single letter to represent each low-order bit:
312 * X = A << 7 | 0bBCDEFGH
313 * Y = J << 5 | 0bKLMNP (1)
315 * Then we can apply the Y tiling formula to see the memory offset being
318 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
320 * If we apply the W detiling formula to this memory location, that the
321 * corresponding X' and Y' coordinates are:
323 * X' = A << 6 | 0bBCDPFH (3)
324 * Y' = J << 6 | 0bKLMNEG
326 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
327 * we need to make the following computation:
329 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
330 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
332 nir_ssa_def
*x_W
= nir_imm_int(b
, 0);
333 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0xfffffff4, -1);
334 x_W
= nir_mask_shift_or(b
, x_W
, y_Y
, 0x1, 2);
335 x_W
= nir_mask_shift_or(b
, x_W
, x_Y
, 0x1, 0);
337 nir_ssa_def
*y_W
= nir_imm_int(b
, 0);
338 y_W
= nir_mask_shift_or(b
, y_W
, y_Y
, 0xfffffffe, 1);
339 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x8, -2);
340 y_W
= nir_mask_shift_or(b
, y_W
, x_Y
, 0x2, -1);
342 return nir_vec2(b
, x_W
, y_W
);
346 * Emit code to compensate for the difference between Y and W tiling.
348 * This code modifies the X and Y coordinates according to the formula:
350 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
352 * (See brw_blorp_build_nir_shader).
354 static inline nir_ssa_def
*
355 blorp_nir_retile_w_to_y(nir_builder
*b
, nir_ssa_def
*pos
)
357 assert(pos
->num_components
== 2);
358 nir_ssa_def
*x_W
= nir_channel(b
, pos
, 0);
359 nir_ssa_def
*y_W
= nir_channel(b
, pos
, 1);
361 /* Applying the same logic as above, but in reverse, we obtain the
364 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
365 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
367 nir_ssa_def
*x_Y
= nir_imm_int(b
, 0);
368 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0xfffffffa, 1);
369 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x2, 2);
370 x_Y
= nir_mask_shift_or(b
, x_Y
, y_W
, 0x1, 1);
371 x_Y
= nir_mask_shift_or(b
, x_Y
, x_W
, 0x1, 0);
373 nir_ssa_def
*y_Y
= nir_imm_int(b
, 0);
374 y_Y
= nir_mask_shift_or(b
, y_Y
, y_W
, 0xfffffffc, -1);
375 y_Y
= nir_mask_shift_or(b
, y_Y
, x_W
, 0x4, -2);
377 return nir_vec2(b
, x_Y
, y_Y
);
381 * Emit code to compensate for the difference between MSAA and non-MSAA
384 * This code modifies the X and Y coordinates according to the formula:
386 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
388 * (See brw_blorp_blit_program).
390 static inline nir_ssa_def
*
391 blorp_nir_encode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
392 unsigned num_samples
, enum isl_msaa_layout layout
)
394 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
397 case ISL_MSAA_LAYOUT_NONE
:
398 assert(pos
->num_components
== 2);
400 case ISL_MSAA_LAYOUT_ARRAY
:
401 /* No translation needed */
403 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
404 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
405 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
406 nir_ssa_def
*s_in
= pos
->num_components
== 2 ? nir_imm_int(b
, 0) :
407 nir_channel(b
, pos
, 2);
409 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
410 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
411 switch (num_samples
) {
414 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
415 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
418 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
419 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
420 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
422 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 1);
423 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
424 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
425 if (num_samples
== 2) {
428 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
429 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
430 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
435 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
436 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
438 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
440 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
441 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
442 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
443 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
444 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 1);
445 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
446 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
450 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
451 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
453 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
456 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffe, 2);
457 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x4, 0);
458 x_out
= nir_mask_shift_or(b
, x_out
, s_in
, 0x1, 1);
459 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
460 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffe, 2);
461 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x8, -1);
462 y_out
= nir_mask_shift_or(b
, y_out
, s_in
, 0x2, 0);
463 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
467 unreachable("Invalid number of samples for IMS layout");
470 return nir_vec2(b
, x_out
, y_out
);
474 unreachable("Invalid MSAA layout");
479 * Emit code to compensate for the difference between MSAA and non-MSAA
482 * This code modifies the X and Y coordinates according to the formula:
484 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
486 * (See brw_blorp_blit_program).
488 static inline nir_ssa_def
*
489 blorp_nir_decode_msaa(nir_builder
*b
, nir_ssa_def
*pos
,
490 unsigned num_samples
, enum isl_msaa_layout layout
)
492 assert(pos
->num_components
== 2 || pos
->num_components
== 3);
495 case ISL_MSAA_LAYOUT_NONE
:
496 /* No translation necessary, and S should already be zero. */
497 assert(pos
->num_components
== 2);
499 case ISL_MSAA_LAYOUT_ARRAY
:
500 /* No translation necessary. */
502 case ISL_MSAA_LAYOUT_INTERLEAVED
: {
503 assert(pos
->num_components
== 2);
505 nir_ssa_def
*x_in
= nir_channel(b
, pos
, 0);
506 nir_ssa_def
*y_in
= nir_channel(b
, pos
, 1);
508 nir_ssa_def
*x_out
= nir_imm_int(b
, 0);
509 nir_ssa_def
*y_out
= nir_imm_int(b
, 0);
510 nir_ssa_def
*s_out
= nir_imm_int(b
, 0);
511 switch (num_samples
) {
514 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
515 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
516 * S = (X & 0b10) >> 1
518 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
519 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
520 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
521 * S = (Y & 0b10) | (X & 0b10) >> 1
523 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffffc, -1);
524 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
525 if (num_samples
== 2) {
527 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
529 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
530 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
531 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
532 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
537 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
538 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
539 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
540 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
542 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
543 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
544 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffffc, -1);
545 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
546 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
547 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
548 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
552 /* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
553 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
554 * Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
555 * S = (Y & 0b100) << 1 | (X & 0b100) |
556 * (Y & 0b10) | (X & 0b10) >> 1
558 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0xfffffff8, -2);
559 x_out
= nir_mask_shift_or(b
, x_out
, x_in
, 0x1, 0);
560 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0xfffffff8, -2);
561 y_out
= nir_mask_shift_or(b
, y_out
, y_in
, 0x1, 0);
562 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x4, 1);
563 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x4, 0);
564 s_out
= nir_mask_shift_or(b
, s_out
, y_in
, 0x2, 0);
565 s_out
= nir_mask_shift_or(b
, s_out
, x_in
, 0x2, -1);
569 unreachable("Invalid number of samples for IMS layout");
572 return nir_vec3(b
, x_out
, y_out
, s_out
);
576 unreachable("Invalid MSAA layout");
581 * Count the number of trailing 1 bits in the given value. For example:
583 * count_trailing_one_bits(0) == 0
584 * count_trailing_one_bits(7) == 3
585 * count_trailing_one_bits(11) == 2
587 static inline int count_trailing_one_bits(unsigned value
)
589 #ifdef HAVE___BUILTIN_CTZ
590 return __builtin_ctz(~value
);
592 return _mesa_bitcount(value
& ~(value
+ 1));
597 blorp_nir_manual_blend_average(nir_builder
*b
, struct brw_blorp_blit_vars
*v
,
598 nir_ssa_def
*pos
, unsigned tex_samples
,
599 enum isl_aux_usage tex_aux_usage
,
600 nir_alu_type dst_type
)
602 /* If non-null, this is the outer-most if statement */
603 nir_if
*outer_if
= NULL
;
605 nir_variable
*color
=
606 nir_local_variable_create(b
->impl
, glsl_vec4_type(), "color");
608 nir_ssa_def
*mcs
= NULL
;
609 if (tex_aux_usage
== ISL_AUX_USAGE_MCS
)
610 mcs
= blorp_nir_txf_ms_mcs(b
, v
, pos
);
612 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
614 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
616 * This ensures that when all samples have the same value, no numerical
617 * precision is lost, since each addition operation always adds two equal
618 * values, and summing two equal floating point values does not lose
621 * We perform this computation by treating the texture_data array as a
622 * stack and performing the following operations:
624 * - push sample 0 onto stack
625 * - push sample 1 onto stack
626 * - add top two stack entries
627 * - push sample 2 onto stack
628 * - push sample 3 onto stack
629 * - add top two stack entries
630 * - add top two stack entries
631 * - divide top stack entry by 4
633 * Note that after pushing sample i onto the stack, the number of add
634 * operations we do is equal to the number of trailing 1 bits in i. This
635 * works provided the total number of samples is a power of two, which it
636 * always is for i965.
638 * For integer formats, we replace the add operations with average
639 * operations and skip the final division.
641 nir_ssa_def
*texture_data
[5];
642 unsigned stack_depth
= 0;
643 for (unsigned i
= 0; i
< tex_samples
; ++i
) {
644 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
646 /* Push sample i onto the stack */
647 assert(stack_depth
< ARRAY_SIZE(texture_data
));
649 nir_ssa_def
*ms_pos
= nir_vec3(b
, nir_channel(b
, pos
, 0),
650 nir_channel(b
, pos
, 1),
652 texture_data
[stack_depth
++] = blorp_nir_txf_ms(b
, v
, ms_pos
, mcs
, dst_type
);
654 if (i
== 0 && tex_aux_usage
== ISL_AUX_USAGE_MCS
) {
655 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
656 * suggests an optimization:
658 * "A simple optimization with probable large return in
659 * performance is to compare the MCS value to zero (indicating
660 * all samples are on sample slice 0), and sample only from
661 * sample slice 0 using ld2dss if MCS is zero."
663 * Note that in the case where the MCS value is zero, sampling from
664 * sample slice 0 using ld2dss and sampling from sample 0 using
665 * ld2dms are equivalent (since all samples are on sample slice 0).
666 * Since we have already sampled from sample 0, all we need to do is
667 * skip the remaining fetches and averaging if MCS is zero.
669 nir_ssa_def
*mcs_zero
=
670 nir_ieq(b
, nir_channel(b
, mcs
, 0), nir_imm_int(b
, 0));
671 if (tex_samples
== 16) {
672 mcs_zero
= nir_iand(b
, mcs_zero
,
673 nir_ieq(b
, nir_channel(b
, mcs
, 1), nir_imm_int(b
, 0)));
676 nir_if
*if_stmt
= nir_if_create(b
->shader
);
677 if_stmt
->condition
= nir_src_for_ssa(mcs_zero
);
678 nir_cf_node_insert(b
->cursor
, &if_stmt
->cf_node
);
680 b
->cursor
= nir_after_cf_list(&if_stmt
->then_list
);
681 nir_store_var(b
, color
, texture_data
[0], 0xf);
683 b
->cursor
= nir_after_cf_list(&if_stmt
->else_list
);
687 for (int j
= 0; j
< count_trailing_one_bits(i
); j
++) {
688 assert(stack_depth
>= 2);
691 assert(dst_type
== nir_type_float
);
692 texture_data
[stack_depth
- 1] =
693 nir_fadd(b
, texture_data
[stack_depth
- 1],
694 texture_data
[stack_depth
]);
698 /* We should have just 1 sample on the stack now. */
699 assert(stack_depth
== 1);
701 texture_data
[0] = nir_fmul(b
, texture_data
[0],
702 nir_imm_float(b
, 1.0 / tex_samples
));
704 nir_store_var(b
, color
, texture_data
[0], 0xf);
707 b
->cursor
= nir_after_cf_node(&outer_if
->cf_node
);
709 return nir_load_var(b
, color
);
712 static inline nir_ssa_def
*
713 nir_imm_vec2(nir_builder
*build
, float x
, float y
)
717 memset(&v
, 0, sizeof(v
));
721 return nir_build_imm(build
, 4, 32, v
);
725 blorp_nir_manual_blend_bilinear(nir_builder
*b
, nir_ssa_def
*pos
,
726 unsigned tex_samples
,
727 const struct brw_blorp_blit_prog_key
*key
,
728 struct brw_blorp_blit_vars
*v
)
730 nir_ssa_def
*pos_xy
= nir_channels(b
, pos
, 0x3);
731 nir_ssa_def
*rect_grid
= nir_load_var(b
, v
->v_rect_grid
);
732 nir_ssa_def
*scale
= nir_imm_vec2(b
, key
->x_scale
, key
->y_scale
);
734 /* Translate coordinates to lay out the samples in a rectangular grid
735 * roughly corresponding to sample locations.
737 pos_xy
= nir_fmul(b
, pos_xy
, scale
);
738 /* Adjust coordinates so that integers represent pixel centers rather
741 pos_xy
= nir_fadd(b
, pos_xy
, nir_imm_float(b
, -0.5));
742 /* Clamp the X, Y texture coordinates to properly handle the sampling of
743 * texels on texture edges.
745 pos_xy
= nir_fmin(b
, nir_fmax(b
, pos_xy
, nir_imm_float(b
, 0.0)),
746 nir_vec2(b
, nir_channel(b
, rect_grid
, 0),
747 nir_channel(b
, rect_grid
, 1)));
749 /* Store the fractional parts to be used as bilinear interpolation
752 nir_ssa_def
*frac_xy
= nir_ffract(b
, pos_xy
);
753 /* Round the float coordinates down to nearest integer */
754 pos_xy
= nir_fdiv(b
, nir_ftrunc(b
, pos_xy
), scale
);
756 nir_ssa_def
*tex_data
[4];
757 for (unsigned i
= 0; i
< 4; ++i
) {
758 float sample_off_x
= (float)(i
& 0x1) / key
->x_scale
;
759 float sample_off_y
= (float)((i
>> 1) & 0x1) / key
->y_scale
;
760 nir_ssa_def
*sample_off
= nir_imm_vec2(b
, sample_off_x
, sample_off_y
);
762 nir_ssa_def
*sample_coords
= nir_fadd(b
, pos_xy
, sample_off
);
763 nir_ssa_def
*sample_coords_int
= nir_f2i(b
, sample_coords
);
765 /* The MCS value we fetch has to match up with the pixel that we're
766 * sampling from. Since we sample from different pixels in each
767 * iteration of this "for" loop, the call to mcs_fetch() should be
768 * here inside the loop after computing the pixel coordinates.
770 nir_ssa_def
*mcs
= NULL
;
771 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
772 mcs
= blorp_nir_txf_ms_mcs(b
, v
, sample_coords_int
);
774 /* Compute sample index and map the sample index to a sample number.
775 * Sample index layout shows the numbering of slots in a rectangular
776 * grid of samples with in a pixel. Sample number layout shows the
777 * rectangular grid of samples roughly corresponding to the real sample
778 * locations with in a pixel.
779 * In case of 4x MSAA, layout of sample indices matches the layout of
787 * In case of 8x MSAA the two layouts don't match.
788 * sample index layout : --------- sample number layout : ---------
789 * | 0 | 1 | | 3 | 7 |
790 * --------- ---------
791 * | 2 | 3 | | 5 | 0 |
792 * --------- ---------
793 * | 4 | 5 | | 1 | 2 |
794 * --------- ---------
795 * | 6 | 7 | | 4 | 6 |
796 * --------- ---------
798 * Fortunately, this can be done fairly easily as:
799 * S' = (0x17306425 >> (S * 4)) & 0xf
801 * In the case of 16x MSAA the two layouts don't match.
802 * Sample index layout: Sample number layout:
803 * --------------------- ---------------------
804 * | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
805 * --------------------- ---------------------
806 * | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
807 * --------------------- ---------------------
808 * | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
809 * --------------------- ---------------------
810 * | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
811 * --------------------- ---------------------
813 * This is equivalent to
814 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
816 nir_ssa_def
*frac
= nir_ffract(b
, sample_coords
);
817 nir_ssa_def
*sample
=
818 nir_fdot2(b
, frac
, nir_imm_vec2(b
, key
->x_scale
,
819 key
->x_scale
* key
->y_scale
));
820 sample
= nir_f2i(b
, sample
);
822 if (tex_samples
== 8) {
823 sample
= nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0x64210573),
824 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
825 nir_imm_int(b
, 0xf));
826 } else if (tex_samples
== 16) {
827 nir_ssa_def
*sample_low
=
828 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xd31479af),
829 nir_ishl(b
, sample
, nir_imm_int(b
, 2))),
830 nir_imm_int(b
, 0xf));
831 nir_ssa_def
*sample_high
=
832 nir_iand(b
, nir_ishr(b
, nir_imm_int(b
, 0xe58b602c),
833 nir_ishl(b
, nir_iadd(b
, sample
,
836 nir_imm_int(b
, 0xf));
838 sample
= nir_bcsel(b
, nir_ilt(b
, sample
, nir_imm_int(b
, 8)),
839 sample_low
, sample_high
);
841 nir_ssa_def
*pos_ms
= nir_vec3(b
, nir_channel(b
, sample_coords_int
, 0),
842 nir_channel(b
, sample_coords_int
, 1),
844 tex_data
[i
] = blorp_nir_txf_ms(b
, v
, pos_ms
, mcs
, key
->texture_data_type
);
847 nir_ssa_def
*frac_x
= nir_channel(b
, frac_xy
, 0);
848 nir_ssa_def
*frac_y
= nir_channel(b
, frac_xy
, 1);
849 return nir_flrp(b
, nir_flrp(b
, tex_data
[0], tex_data
[1], frac_x
),
850 nir_flrp(b
, tex_data
[2], tex_data
[3], frac_x
),
855 * Generator for WM programs used in BLORP blits.
857 * The bulk of the work done by the WM program is to wrap and unwrap the
858 * coordinate transformations used by the hardware to store surfaces in
859 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
860 * sample index for a multisampled surface) to a memory offset by the
861 * following formulas:
863 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
864 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
866 * For a single-sampled surface, or for a multisampled surface using
867 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
870 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
871 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
872 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
873 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
875 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
876 * embeds the sample number into bit 1 of the X and Y coordinates:
878 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
879 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
880 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
881 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
882 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
883 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
884 * S = (Y & 0b10) | (X & 0b10) >> 1
886 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
887 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
890 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
891 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
892 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
893 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
894 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
895 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
896 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
898 * For X tiling, tile() combines together the low-order bits of the X and Y
899 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
900 * bytes wide and 8 rows high:
902 * tile(x_tiled, X, Y, S) = A
903 * where A = tile_num << 12 | offset
904 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
905 * offset = (Y' & 0b111) << 9
906 * | (X & 0b111111111)
908 * Y' = Y + S * qpitch
909 * detile(x_tiled, A) = (X, Y, S)
913 * Y' = (tile_num / tile_pitch) << 3
914 * | (A & 0b111000000000) >> 9
915 * X' = (tile_num % tile_pitch) << 9
916 * | (A & 0b111111111)
918 * (In all tiling formulas, cpp is the number of bytes occupied by a single
919 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
920 * to fill the width of the surface, and qpitch is the spacing (in rows)
921 * between array slices).
923 * For Y tiling, tile() combines together the low-order bits of the X and Y
924 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
925 * bytes wide and 32 rows high:
927 * tile(y_tiled, X, Y, S) = A
928 * where A = tile_num << 12 | offset
929 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
930 * offset = (X' & 0b1110000) << 5
931 * | (Y' & 0b11111) << 4
934 * Y' = Y + S * qpitch
935 * detile(y_tiled, A) = (X, Y, S)
939 * Y' = (tile_num / tile_pitch) << 5
940 * | (A & 0b111110000) >> 4
941 * X' = (tile_num % tile_pitch) << 7
942 * | (A & 0b111000000000) >> 5
945 * For W tiling, tile() combines together the low-order bits of the X and Y
946 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
947 * bytes wide and 64 rows high (note that W tiling is only used for stencil
948 * buffers, which always have cpp = 1 and S=0):
950 * tile(w_tiled, X, Y, S) = A
951 * where A = tile_num << 12 | offset
952 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
953 * offset = (X' & 0b111000) << 6
954 * | (Y' & 0b111100) << 3
955 * | (X' & 0b100) << 2
961 * Y' = Y + S * qpitch
962 * detile(w_tiled, A) = (X, Y, S)
963 * where X = X' / cpp = X'
964 * Y = Y' % qpitch = Y'
966 * Y' = (tile_num / tile_pitch) << 6
967 * | (A & 0b111100000) >> 3
968 * | (A & 0b1000) >> 2
970 * X' = (tile_num % tile_pitch) << 6
971 * | (A & 0b111000000000) >> 6
972 * | (A & 0b10000) >> 2
976 * Finally, for a non-tiled surface, tile() simply combines together the X and
977 * Y coordinates in the natural way:
979 * tile(untiled, X, Y, S) = A
980 * where A = Y * pitch + X'
982 * Y' = Y + S * qpitch
983 * detile(untiled, A) = (X, Y, S)
990 * (In these formulas, pitch is the number of bytes occupied by a single row
994 brw_blorp_build_nir_shader(struct blorp_context
*blorp
, void *mem_ctx
,
995 const struct brw_blorp_blit_prog_key
*key
)
997 const struct gen_device_info
*devinfo
= blorp
->isl_dev
->info
;
998 nir_ssa_def
*src_pos
, *dst_pos
, *color
;
1001 if (key
->dst_tiled_w
&& key
->rt_samples
> 1) {
1002 /* If the destination image is W tiled and multisampled, then the thread
1003 * must be dispatched once per sample, not once per pixel. This is
1004 * necessary because after conversion between W and Y tiling, there's no
1005 * guarantee that all samples corresponding to a single pixel will still
1008 assert(key
->persample_msaa_dispatch
);
1012 /* We are blending, which means we won't have an opportunity to
1013 * translate the tiling and sample count for the texture surface. So
1014 * the surface state for the texture must be configured with the correct
1015 * tiling and sample count.
1017 assert(!key
->src_tiled_w
);
1018 assert(key
->tex_samples
== key
->src_samples
);
1019 assert(key
->tex_layout
== key
->src_layout
);
1020 assert(key
->tex_samples
> 0);
1023 if (key
->persample_msaa_dispatch
) {
1024 /* It only makes sense to do persample dispatch if the render target is
1025 * configured as multisampled.
1027 assert(key
->rt_samples
> 0);
1030 /* Make sure layout is consistent with sample count */
1031 assert((key
->tex_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1032 (key
->tex_samples
<= 1));
1033 assert((key
->rt_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1034 (key
->rt_samples
<= 1));
1035 assert((key
->src_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1036 (key
->src_samples
<= 1));
1037 assert((key
->dst_layout
== ISL_MSAA_LAYOUT_NONE
) ==
1038 (key
->dst_samples
<= 1));
1041 nir_builder_init_simple_shader(&b
, mem_ctx
, MESA_SHADER_FRAGMENT
, NULL
);
1043 struct brw_blorp_blit_vars v
;
1044 brw_blorp_blit_vars_init(&b
, &v
, key
);
1046 dst_pos
= blorp_blit_get_frag_coords(&b
, key
, &v
);
1048 /* Render target and texture hardware don't support W tiling until Gen8. */
1049 const bool rt_tiled_w
= false;
1050 const bool tex_tiled_w
= devinfo
->gen
>= 8 && key
->src_tiled_w
;
1052 /* The address that data will be written to is determined by the
1053 * coordinates supplied to the WM thread and the tiling and sample count of
1054 * the render target, according to the formula:
1056 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1058 * If the actual tiling and sample count of the destination surface are not
1059 * the same as the configuration of the render target, then these
1060 * coordinates are wrong and we have to adjust them to compensate for the
1063 if (rt_tiled_w
!= key
->dst_tiled_w
||
1064 key
->rt_samples
!= key
->dst_samples
||
1065 key
->rt_layout
!= key
->dst_layout
) {
1066 dst_pos
= blorp_nir_encode_msaa(&b
, dst_pos
, key
->rt_samples
,
1068 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1069 if (rt_tiled_w
!= key
->dst_tiled_w
)
1070 dst_pos
= blorp_nir_retile_y_to_w(&b
, dst_pos
);
1071 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1072 dst_pos
= blorp_nir_decode_msaa(&b
, dst_pos
, key
->dst_samples
,
1076 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1078 * That is: X, Y and S now contain the true coordinates and sample index of
1079 * the data that the WM thread should output.
1081 * If we need to kill pixels that are outside the destination rectangle,
1082 * now is the time to do it.
1084 if (key
->use_kill
) {
1085 assert(!(key
->blend
&& key
->blit_scaled
));
1086 blorp_nir_discard_if_outside_rect(&b
, dst_pos
, &v
);
1089 src_pos
= blorp_blit_apply_transform(&b
, nir_i2f(&b
, dst_pos
), &v
);
1090 if (dst_pos
->num_components
== 3) {
1091 /* The sample coordinate is an integer that we want left alone but
1092 * blorp_blit_apply_transform() blindly applies the transform to all
1093 * three coordinates. Grab the original sample index.
1095 src_pos
= nir_vec3(&b
, nir_channel(&b
, src_pos
, 0),
1096 nir_channel(&b
, src_pos
, 1),
1097 nir_channel(&b
, dst_pos
, 2));
1100 /* If the source image is not multisampled, then we want to fetch sample
1101 * number 0, because that's the only sample there is.
1103 if (key
->src_samples
== 1)
1104 src_pos
= nir_channels(&b
, src_pos
, 0x3);
1106 /* X, Y, and S are now the coordinates of the pixel in the source image
1107 * that we want to texture from. Exception: if we are blending, then S is
1108 * irrelevant, because we are going to fetch all samples.
1110 if (key
->blend
&& !key
->blit_scaled
) {
1111 /* Resolves (effecively) use texelFetch, so we need integers and we
1112 * don't care about the sample index if we got one.
1114 src_pos
= nir_f2i(&b
, nir_channels(&b
, src_pos
, 0x3));
1116 if (devinfo
->gen
== 6) {
1117 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1118 * blending we need with a single linear-interpolated texture lookup
1119 * at the center of the sample. The texture coordinates to be odd
1120 * integers so that they correspond to the center of a 2x2 block
1121 * representing the four samples that maxe up a pixel. So we need
1122 * to multiply our X and Y coordinates each by 2 and then add 1.
1124 src_pos
= nir_ishl(&b
, src_pos
, nir_imm_int(&b
, 1));
1125 src_pos
= nir_iadd(&b
, src_pos
, nir_imm_int(&b
, 1));
1126 src_pos
= nir_i2f(&b
, src_pos
);
1127 color
= blorp_nir_tex(&b
, &v
, src_pos
, key
->texture_data_type
);
1129 /* Gen7+ hardware doesn't automaticaly blend. */
1130 color
= blorp_nir_manual_blend_average(&b
, &v
, src_pos
, key
->src_samples
,
1132 key
->texture_data_type
);
1134 } else if (key
->blend
&& key
->blit_scaled
) {
1135 assert(!key
->use_kill
);
1136 color
= blorp_nir_manual_blend_bilinear(&b
, src_pos
, key
->src_samples
, key
, &v
);
1138 if (key
->bilinear_filter
) {
1139 color
= blorp_nir_tex(&b
, &v
, src_pos
, key
->texture_data_type
);
1141 /* We're going to use texelFetch, so we need integers */
1142 if (src_pos
->num_components
== 2) {
1143 src_pos
= nir_f2i(&b
, src_pos
);
1145 assert(src_pos
->num_components
== 3);
1146 src_pos
= nir_vec3(&b
, nir_channel(&b
, nir_f2i(&b
, src_pos
), 0),
1147 nir_channel(&b
, nir_f2i(&b
, src_pos
), 1),
1148 nir_channel(&b
, src_pos
, 2));
1151 /* We aren't blending, which means we just want to fetch a single
1152 * sample from the source surface. The address that we want to fetch
1153 * from is related to the X, Y and S values according to the formula:
1155 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1157 * If the actual tiling and sample count of the source surface are
1158 * not the same as the configuration of the texture, then we need to
1159 * adjust the coordinates to compensate for the difference.
1161 if (tex_tiled_w
!= key
->src_tiled_w
||
1162 key
->tex_samples
!= key
->src_samples
||
1163 key
->tex_layout
!= key
->src_layout
) {
1164 src_pos
= blorp_nir_encode_msaa(&b
, src_pos
, key
->src_samples
,
1166 /* Now (X, Y, S) = detile(src_tiling, offset) */
1167 if (tex_tiled_w
!= key
->src_tiled_w
)
1168 src_pos
= blorp_nir_retile_w_to_y(&b
, src_pos
);
1169 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1170 src_pos
= blorp_nir_decode_msaa(&b
, src_pos
, key
->tex_samples
,
1174 if (key
->need_src_offset
)
1175 src_pos
= nir_iadd(&b
, src_pos
, nir_load_var(&b
, v
.v_src_offset
));
1177 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1179 * In other words: X, Y, and S now contain values which, when passed to
1180 * the texturing unit, will cause data to be read from the correct
1181 * memory location. So we can fetch the texel now.
1183 if (key
->src_samples
== 1) {
1184 color
= blorp_nir_txf(&b
, &v
, src_pos
, key
->texture_data_type
);
1186 nir_ssa_def
*mcs
= NULL
;
1187 if (key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
)
1188 mcs
= blorp_nir_txf_ms_mcs(&b
, &v
, src_pos
);
1190 color
= blorp_nir_txf_ms(&b
, &v
, src_pos
, mcs
, key
->texture_data_type
);
1196 /* The destination image is bound as a red texture three times as wide
1197 * as the actual image. Our shader is effectively running one color
1198 * component at a time. We need to pick off the appropriate component
1199 * from the source color and write that to destination red.
1201 assert(dst_pos
->num_components
== 2);
1203 nir_umod(&b
, nir_channel(&b
, dst_pos
, 0), nir_imm_int(&b
, 3));
1205 nir_ssa_def
*color_component
=
1206 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 0)),
1207 nir_channel(&b
, color
, 0),
1208 nir_bcsel(&b
, nir_ieq(&b
, comp
, nir_imm_int(&b
, 1)),
1209 nir_channel(&b
, color
, 1),
1210 nir_channel(&b
, color
, 2)));
1212 nir_ssa_def
*u
= nir_ssa_undef(&b
, 1, 32);
1213 color
= nir_vec4(&b
, color_component
, u
, u
, u
);
1216 nir_store_var(&b
, v
.color_out
, color
, 0xf);
1222 brw_blorp_get_blit_kernel(struct blorp_context
*blorp
,
1223 struct blorp_params
*params
,
1224 const struct brw_blorp_blit_prog_key
*prog_key
)
1226 if (blorp
->lookup_shader(blorp
, prog_key
, sizeof(*prog_key
),
1227 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
))
1230 void *mem_ctx
= ralloc_context(NULL
);
1232 const unsigned *program
;
1233 unsigned program_size
;
1234 struct brw_wm_prog_data prog_data
;
1236 nir_shader
*nir
= brw_blorp_build_nir_shader(blorp
, mem_ctx
, prog_key
);
1237 struct brw_wm_prog_key wm_key
;
1238 brw_blorp_init_wm_prog_key(&wm_key
);
1239 wm_key
.tex
.compressed_multisample_layout_mask
=
1240 prog_key
->tex_aux_usage
== ISL_AUX_USAGE_MCS
;
1241 wm_key
.tex
.msaa_16
= prog_key
->tex_samples
== 16;
1242 wm_key
.multisample_fbo
= prog_key
->rt_samples
> 1;
1244 program
= blorp_compile_fs(blorp
, mem_ctx
, nir
, &wm_key
, false,
1245 &prog_data
, &program_size
);
1247 blorp
->upload_shader(blorp
, prog_key
, sizeof(*prog_key
),
1248 program
, program_size
,
1249 &prog_data
.base
, sizeof(prog_data
),
1250 ¶ms
->wm_prog_kernel
, ¶ms
->wm_prog_data
);
1252 ralloc_free(mem_ctx
);
1256 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform
*xform
,
1257 GLfloat src0
, GLfloat src1
,
1258 GLfloat dst0
, GLfloat dst1
,
1261 double scale
= (double)(src1
- src0
) / (double)(dst1
- dst0
);
1263 /* When not mirroring a coordinate (say, X), we need:
1264 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1266 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1268 * blorp program uses "round toward zero" to convert the
1269 * transformed floating point coordinates to integer coordinates,
1270 * whereas the behaviour we actually want is "round to nearest",
1271 * so 0.5 provides the necessary correction.
1273 xform
->multiplier
= scale
;
1274 xform
->offset
= src0
+ (-(double)dst0
+ 0.5) * scale
;
1276 /* When mirroring X we need:
1277 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1279 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1281 xform
->multiplier
= -scale
;
1282 xform
->offset
= src0
+ ((double)dst1
- 0.5) * scale
;
1287 surf_get_intratile_offset_px(struct brw_blorp_surface_info
*info
,
1288 uint32_t *tile_x_px
, uint32_t *tile_y_px
)
1290 if (info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1291 struct isl_extent2d px_size_sa
=
1292 isl_get_interleaved_msaa_px_size_sa(info
->surf
.samples
);
1293 assert(info
->tile_x_sa
% px_size_sa
.width
== 0);
1294 assert(info
->tile_y_sa
% px_size_sa
.height
== 0);
1295 *tile_x_px
= info
->tile_x_sa
/ px_size_sa
.width
;
1296 *tile_y_px
= info
->tile_y_sa
/ px_size_sa
.height
;
1298 *tile_x_px
= info
->tile_x_sa
;
1299 *tile_y_px
= info
->tile_y_sa
;
1304 surf_convert_to_single_slice(const struct isl_device
*isl_dev
,
1305 struct brw_blorp_surface_info
*info
)
1307 /* Just bail if we have nothing to do. */
1308 if (info
->surf
.dim
== ISL_SURF_DIM_2D
&&
1309 info
->view
.base_level
== 0 && info
->view
.base_array_layer
== 0 &&
1310 info
->surf
.levels
== 1 && info
->surf
.logical_level0_px
.array_len
== 1)
1313 /* If this gets triggered then we've gotten here twice which. This
1314 * shouldn't happen thanks to the above early return.
1316 assert(info
->tile_x_sa
== 0 && info
->tile_y_sa
== 0);
1318 uint32_t layer
= 0, z
= 0;
1319 if (info
->surf
.dim
== ISL_SURF_DIM_3D
)
1320 z
= info
->view
.base_array_layer
+ info
->z_offset
;
1322 layer
= info
->view
.base_array_layer
;
1324 uint32_t x_offset_sa
, y_offset_sa
;
1325 isl_surf_get_image_offset_sa(&info
->surf
, info
->view
.base_level
,
1326 layer
, z
, &x_offset_sa
, &y_offset_sa
);
1328 uint32_t byte_offset
;
1329 isl_tiling_get_intratile_offset_sa(isl_dev
, info
->surf
.tiling
,
1330 info
->surf
.format
, info
->surf
.row_pitch
,
1331 x_offset_sa
, y_offset_sa
,
1333 &info
->tile_x_sa
, &info
->tile_y_sa
);
1334 info
->addr
.offset
+= byte_offset
;
1336 const uint32_t slice_width_px
=
1337 minify(info
->surf
.logical_level0_px
.width
, info
->view
.base_level
);
1338 const uint32_t slice_height_px
=
1339 minify(info
->surf
.logical_level0_px
.height
, info
->view
.base_level
);
1341 uint32_t tile_x_px
, tile_y_px
;
1342 surf_get_intratile_offset_px(info
, &tile_x_px
, &tile_y_px
);
1344 struct isl_surf_init_info init_info
= {
1345 .dim
= ISL_SURF_DIM_2D
,
1346 .format
= info
->surf
.format
,
1347 .width
= slice_width_px
+ tile_x_px
,
1348 .height
= slice_height_px
+ tile_y_px
,
1352 .samples
= info
->surf
.samples
,
1353 .min_pitch
= info
->surf
.row_pitch
,
1354 .usage
= info
->surf
.usage
,
1355 .tiling_flags
= 1 << info
->surf
.tiling
,
1358 isl_surf_init_s(isl_dev
, &info
->surf
, &init_info
);
1359 assert(info
->surf
.row_pitch
== init_info
.min_pitch
);
1361 /* The view is also different now. */
1362 info
->view
.base_level
= 0;
1363 info
->view
.levels
= 1;
1364 info
->view
.base_array_layer
= 0;
1365 info
->view
.array_len
= 1;
1370 surf_fake_interleaved_msaa(const struct isl_device
*isl_dev
,
1371 struct brw_blorp_surface_info
*info
)
1373 assert(info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
);
1375 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1376 surf_convert_to_single_slice(isl_dev
, info
);
1378 info
->surf
.logical_level0_px
= info
->surf
.phys_level0_sa
;
1379 info
->surf
.samples
= 1;
1380 info
->surf
.msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
1384 surf_retile_w_to_y(const struct isl_device
*isl_dev
,
1385 struct brw_blorp_surface_info
*info
)
1387 assert(info
->surf
.tiling
== ISL_TILING_W
);
1389 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1390 surf_convert_to_single_slice(isl_dev
, info
);
1392 /* On gen7+, we don't have interleaved multisampling for color render
1393 * targets so we have to fake it.
1395 * TODO: Are we sure we don't also need to fake it on gen6?
1397 if (isl_dev
->info
->gen
> 6 &&
1398 info
->surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1399 surf_fake_interleaved_msaa(isl_dev
, info
);
1402 if (isl_dev
->info
->gen
== 6) {
1403 /* Gen6 stencil buffers have a very large alignment coming in from the
1404 * miptree. It's out-of-bounds for what the surface state can handle.
1405 * Since we have a single layer and level, it doesn't really matter as
1406 * long as we don't pass a bogus value into isl_surf_fill_state().
1408 info
->surf
.image_alignment_el
= isl_extent3d(4, 2, 1);
1411 /* Now that we've converted everything to a simple 2-D surface with only
1412 * one miplevel, we can go about retiling it.
1414 const unsigned x_align
= 8, y_align
= info
->surf
.samples
!= 0 ? 8 : 4;
1415 info
->surf
.tiling
= ISL_TILING_Y0
;
1416 info
->surf
.logical_level0_px
.width
=
1417 ALIGN(info
->surf
.logical_level0_px
.width
, x_align
) * 2;
1418 info
->surf
.logical_level0_px
.height
=
1419 ALIGN(info
->surf
.logical_level0_px
.height
, y_align
) / 2;
1420 info
->tile_x_sa
*= 2;
1421 info
->tile_y_sa
/= 2;
1425 do_blorp_blit(struct blorp_batch
*batch
,
1426 struct blorp_params
*params
,
1427 struct brw_blorp_blit_prog_key
*wm_prog_key
,
1428 float src_x0
, float src_y0
,
1429 float src_x1
, float src_y1
,
1430 float dst_x0
, float dst_y0
,
1431 float dst_x1
, float dst_y1
,
1432 bool mirror_x
, bool mirror_y
)
1434 const struct gen_device_info
*devinfo
= batch
->blorp
->isl_dev
->info
;
1436 if (isl_format_has_sint_channel(params
->src
.view
.format
)) {
1437 wm_prog_key
->texture_data_type
= nir_type_int
;
1438 } else if (isl_format_has_uint_channel(params
->src
.view
.format
)) {
1439 wm_prog_key
->texture_data_type
= nir_type_uint
;
1441 wm_prog_key
->texture_data_type
= nir_type_float
;
1444 /* src_samples and dst_samples are the true sample counts */
1445 wm_prog_key
->src_samples
= params
->src
.surf
.samples
;
1446 wm_prog_key
->dst_samples
= params
->dst
.surf
.samples
;
1448 wm_prog_key
->tex_aux_usage
= params
->src
.aux_usage
;
1450 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1453 wm_prog_key
->src_layout
= params
->src
.surf
.msaa_layout
;
1454 wm_prog_key
->dst_layout
= params
->dst
.surf
.msaa_layout
;
1456 /* Round floating point values to nearest integer to avoid "off by one texel"
1457 * kind of errors when blitting.
1459 params
->x0
= params
->wm_inputs
.discard_rect
.x0
= roundf(dst_x0
);
1460 params
->y0
= params
->wm_inputs
.discard_rect
.y0
= roundf(dst_y0
);
1461 params
->x1
= params
->wm_inputs
.discard_rect
.x1
= roundf(dst_x1
);
1462 params
->y1
= params
->wm_inputs
.discard_rect
.y1
= roundf(dst_y1
);
1464 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[0],
1465 src_x0
, src_x1
, dst_x0
, dst_x1
, mirror_x
);
1466 brw_blorp_setup_coord_transform(¶ms
->wm_inputs
.coord_transform
[1],
1467 src_y0
, src_y1
, dst_y0
, dst_y1
, mirror_y
);
1469 if (devinfo
->gen
> 6 &&
1470 params
->dst
.surf
.msaa_layout
== ISL_MSAA_LAYOUT_INTERLEAVED
) {
1471 assert(params
->dst
.surf
.samples
> 1);
1473 /* We must expand the rectangle we send through the rendering pipeline,
1474 * to account for the fact that we are mapping the destination region as
1475 * single-sampled when it is in fact multisampled. We must also align
1476 * it to a multiple of the multisampling pattern, because the
1477 * differences between multisampled and single-sampled surface formats
1478 * will mean that pixels are scrambled within the multisampling pattern.
1479 * TODO: what if this makes the coordinates too large?
1481 * Note: this only works if the destination surface uses the IMS layout.
1482 * If it's UMS, then we have no choice but to set up the rendering
1483 * pipeline as multisampled.
1485 struct isl_extent2d px_size_sa
=
1486 isl_get_interleaved_msaa_px_size_sa(params
->dst
.surf
.samples
);
1487 params
->x0
= ROUND_DOWN_TO(params
->x0
, 2) * px_size_sa
.width
;
1488 params
->y0
= ROUND_DOWN_TO(params
->y0
, 2) * px_size_sa
.height
;
1489 params
->x1
= ALIGN(params
->x1
, 2) * px_size_sa
.width
;
1490 params
->y1
= ALIGN(params
->y1
, 2) * px_size_sa
.height
;
1492 surf_fake_interleaved_msaa(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1494 wm_prog_key
->use_kill
= true;
1495 wm_prog_key
->need_dst_offset
= true;
1498 if (params
->dst
.surf
.tiling
== ISL_TILING_W
) {
1499 /* We must modify the rectangle we send through the rendering pipeline
1500 * (and the size and x/y offset of the destination surface), to account
1501 * for the fact that we are mapping it as Y-tiled when it is in fact
1504 * Both Y tiling and W tiling can be understood as organizations of
1505 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1506 * is different, but the layout of the 32-byte sub-tiles within the 4k
1507 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1508 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1509 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1511 * Therefore, to account for the layout differences within the 32-byte
1512 * sub-tiles, we must expand the rectangle so the X coordinates of its
1513 * edges are multiples of 8 (the W sub-tile width), and its Y
1514 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1515 * Then we need to scale the X and Y coordinates of the rectangle to
1516 * account for the differences in aspect ratio between the Y and W
1517 * sub-tiles. We need to modify the layer width and height similarly.
1519 * A correction needs to be applied when MSAA is in use: since
1520 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1521 * we need to align the Y coordinates to multiples of 8, so that when
1522 * they are divided by two they are still multiples of 4.
1524 * Note: Since the x/y offset of the surface will be applied using the
1525 * SURFACE_STATE command packet, it will be invisible to the swizzling
1526 * code in the shader; therefore it needs to be in a multiple of the
1527 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1528 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1529 * buffer), and the miplevel alignment used for stencil buffers is 8
1530 * pixels horizontally and either 4 or 8 pixels vertically (see
1531 * intel_horizontal_texture_alignment_unit() and
1532 * intel_vertical_texture_alignment_unit()).
1534 * Note: Also, since the SURFACE_STATE command packet can only apply
1535 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1536 * vertically, it is important that the offsets will be multiples of
1537 * these sizes after they are converted into Y-tiled coordinates.
1538 * Fortunately they will be, since we know from above that the offsets
1539 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1540 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1542 * TODO: what if this makes the coordinates (or the texture size) too
1545 const unsigned x_align
= 8;
1546 const unsigned y_align
= params
->dst
.surf
.samples
!= 0 ? 8 : 4;
1547 params
->x0
= ROUND_DOWN_TO(params
->x0
, x_align
) * 2;
1548 params
->y0
= ROUND_DOWN_TO(params
->y0
, y_align
) / 2;
1549 params
->x1
= ALIGN(params
->x1
, x_align
) * 2;
1550 params
->y1
= ALIGN(params
->y1
, y_align
) / 2;
1552 /* Retile the surface to Y-tiled */
1553 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->dst
);
1555 wm_prog_key
->dst_tiled_w
= true;
1556 wm_prog_key
->use_kill
= true;
1557 wm_prog_key
->need_dst_offset
= true;
1559 if (params
->dst
.surf
.samples
> 1) {
1560 /* If the destination surface is a W-tiled multisampled stencil
1561 * buffer that we're mapping as Y tiled, then we need to arrange for
1562 * the WM program to run once per sample rather than once per pixel,
1563 * because the memory layout of related samples doesn't match between
1566 wm_prog_key
->persample_msaa_dispatch
= true;
1570 if (devinfo
->gen
< 8 && params
->src
.surf
.tiling
== ISL_TILING_W
) {
1571 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1572 * Broadwell adds support for sampling from stencil.
1574 * See the comments above concerning x/y offset alignment for the
1575 * destination surface.
1577 * TODO: what if this makes the texture size too large?
1579 surf_retile_w_to_y(batch
->blorp
->isl_dev
, ¶ms
->src
);
1581 wm_prog_key
->src_tiled_w
= true;
1582 wm_prog_key
->need_src_offset
= true;
1585 /* tex_samples and rt_samples are the sample counts that are set up in
1588 wm_prog_key
->tex_samples
= params
->src
.surf
.samples
;
1589 wm_prog_key
->rt_samples
= params
->dst
.surf
.samples
;
1591 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1592 * use to access the source and destination surfaces.
1594 wm_prog_key
->tex_layout
= params
->src
.surf
.msaa_layout
;
1595 wm_prog_key
->rt_layout
= params
->dst
.surf
.msaa_layout
;
1597 if (params
->src
.surf
.samples
> 0 && params
->dst
.surf
.samples
> 1) {
1598 /* We are blitting from a multisample buffer to a multisample buffer, so
1599 * we must preserve samples within a pixel. This means we have to
1600 * arrange for the WM program to run once per sample rather than once
1603 wm_prog_key
->persample_msaa_dispatch
= true;
1606 params
->num_samples
= params
->dst
.surf
.samples
;
1608 if (params
->src
.tile_x_sa
|| params
->src
.tile_y_sa
) {
1609 assert(wm_prog_key
->need_src_offset
);
1610 surf_get_intratile_offset_px(¶ms
->src
,
1611 ¶ms
->wm_inputs
.src_offset
.x
,
1612 ¶ms
->wm_inputs
.src_offset
.y
);
1615 if (params
->dst
.tile_x_sa
|| params
->dst
.tile_y_sa
) {
1616 assert(wm_prog_key
->need_dst_offset
);
1617 surf_get_intratile_offset_px(¶ms
->dst
,
1618 ¶ms
->wm_inputs
.dst_offset
.x
,
1619 ¶ms
->wm_inputs
.dst_offset
.y
);
1620 params
->x0
+= params
->wm_inputs
.dst_offset
.x
;
1621 params
->y0
+= params
->wm_inputs
.dst_offset
.y
;
1622 params
->x1
+= params
->wm_inputs
.dst_offset
.x
;
1623 params
->y1
+= params
->wm_inputs
.dst_offset
.y
;
1626 /* For some texture types, we need to pass the layer through the sampler. */
1627 params
->wm_inputs
.src_z
= params
->src
.z_offset
;
1629 brw_blorp_get_blit_kernel(batch
->blorp
, params
, wm_prog_key
);
1631 batch
->blorp
->exec(batch
, params
);
1635 blorp_blit(struct blorp_batch
*batch
,
1636 const struct blorp_surf
*src_surf
,
1637 unsigned src_level
, unsigned src_layer
,
1638 enum isl_format src_format
, struct isl_swizzle src_swizzle
,
1639 const struct blorp_surf
*dst_surf
,
1640 unsigned dst_level
, unsigned dst_layer
,
1641 enum isl_format dst_format
, struct isl_swizzle dst_swizzle
,
1642 float src_x0
, float src_y0
,
1643 float src_x1
, float src_y1
,
1644 float dst_x0
, float dst_y0
,
1645 float dst_x1
, float dst_y1
,
1646 GLenum filter
, bool mirror_x
, bool mirror_y
)
1648 struct blorp_params params
;
1649 blorp_params_init(¶ms
);
1651 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
1652 src_layer
, src_format
, false);
1653 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
1654 dst_layer
, dst_format
, true);
1656 params
.src
.view
.swizzle
= src_swizzle
;
1657 params
.dst
.view
.swizzle
= dst_swizzle
;
1659 struct brw_blorp_blit_prog_key wm_prog_key
= {
1660 .shader_type
= BLORP_SHADER_TYPE_BLIT
1663 /* Scaled blitting or not. */
1664 wm_prog_key
.blit_scaled
=
1665 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
1666 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
1668 /* Scaling factors used for bilinear filtering in multisample scaled
1671 if (params
.src
.surf
.samples
== 16)
1672 wm_prog_key
.x_scale
= 4.0f
;
1674 wm_prog_key
.x_scale
= 2.0f
;
1675 wm_prog_key
.y_scale
= params
.src
.surf
.samples
/ wm_prog_key
.x_scale
;
1677 if (filter
== GL_LINEAR
&&
1678 params
.src
.surf
.samples
<= 1 && params
.dst
.surf
.samples
<= 1)
1679 wm_prog_key
.bilinear_filter
= true;
1681 if ((params
.src
.surf
.usage
& ISL_SURF_USAGE_DEPTH_BIT
) == 0 &&
1682 (params
.src
.surf
.usage
& ISL_SURF_USAGE_STENCIL_BIT
) == 0 &&
1683 !isl_format_has_int_channel(params
.src
.surf
.format
) &&
1684 params
.src
.surf
.samples
> 1 && params
.dst
.surf
.samples
<= 1) {
1685 /* We are downsampling a non-integer color buffer, so blend.
1687 * Regarding integer color buffers, the OpenGL ES 3.2 spec says:
1689 * "If the source formats are integer types or stencil values, a
1690 * single sample's value is selected for each pixel."
1692 * This implies we should not blend in that case.
1694 wm_prog_key
.blend
= true;
1697 params
.wm_inputs
.rect_grid
.x1
=
1698 minify(params
.src
.surf
.logical_level0_px
.width
, src_level
) *
1699 wm_prog_key
.x_scale
- 1.0f
;
1700 params
.wm_inputs
.rect_grid
.y1
=
1701 minify(params
.src
.surf
.logical_level0_px
.height
, src_level
) *
1702 wm_prog_key
.y_scale
- 1.0f
;
1704 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
,
1705 src_x0
, src_y0
, src_x1
, src_y1
,
1706 dst_x0
, dst_y0
, dst_x1
, dst_y1
,
1707 mirror_x
, mirror_y
);
1710 static enum isl_format
1711 get_copy_format_for_bpb(const struct isl_device
*isl_dev
, unsigned bpb
)
1713 /* The choice of UNORM and UINT formats is very intentional here. Most
1714 * of the time, we want to use a UINT format to avoid any rounding error
1715 * in the blit. For stencil blits, R8_UINT is required by the hardware.
1716 * (It's the only format allowed in conjunction with W-tiling.) Also we
1717 * intentionally use the 4-channel formats whenever we can. This is so
1718 * that, when we do a RGB <-> RGBX copy, the two formats will line up
1719 * even though one of them is 3/4 the size of the other. The choice of
1720 * UNORM vs. UINT is also very intentional because we don't have 8 or
1721 * 16-bit RGB UINT formats until Sky Lake so we have to use UNORM there.
1722 * Fortunately, the only time we should ever use two different formats in
1723 * the table below is for RGB -> RGBA blits and so we will never have any
1724 * UNORM/UINT mismatch.
1726 if (ISL_DEV_GEN(isl_dev
) >= 9) {
1728 case 8: return ISL_FORMAT_R8_UINT
;
1729 case 16: return ISL_FORMAT_R8G8_UINT
;
1730 case 24: return ISL_FORMAT_R8G8B8_UINT
;
1731 case 32: return ISL_FORMAT_R8G8B8A8_UINT
;
1732 case 48: return ISL_FORMAT_R16G16B16_UINT
;
1733 case 64: return ISL_FORMAT_R16G16B16A16_UINT
;
1734 case 96: return ISL_FORMAT_R32G32B32_UINT
;
1735 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
1737 unreachable("Unknown format bpb");
1741 case 8: return ISL_FORMAT_R8_UINT
;
1742 case 16: return ISL_FORMAT_R8G8_UINT
;
1743 case 24: return ISL_FORMAT_R8G8B8_UNORM
;
1744 case 32: return ISL_FORMAT_R8G8B8A8_UNORM
;
1745 case 48: return ISL_FORMAT_R16G16B16_UNORM
;
1746 case 64: return ISL_FORMAT_R16G16B16A16_UNORM
;
1747 case 96: return ISL_FORMAT_R32G32B32_UINT
;
1748 case 128:return ISL_FORMAT_R32G32B32A32_UINT
;
1750 unreachable("Unknown format bpb");
1756 surf_convert_to_uncompressed(const struct isl_device
*isl_dev
,
1757 struct brw_blorp_surface_info
*info
,
1758 uint32_t *x
, uint32_t *y
,
1759 uint32_t *width
, uint32_t *height
)
1761 const struct isl_format_layout
*fmtl
=
1762 isl_format_get_layout(info
->surf
.format
);
1764 assert(fmtl
->bw
> 1 || fmtl
->bh
> 1);
1766 /* This is a compressed surface. We need to convert it to a single
1767 * slice (because compressed layouts don't perfectly match uncompressed
1768 * ones with the same bpb) and divide x, y, width, and height by the
1771 surf_convert_to_single_slice(isl_dev
, info
);
1773 if (width
|| height
) {
1775 uint32_t right_edge_px
= info
->tile_x_sa
+ *x
+ *width
;
1776 uint32_t bottom_edge_px
= info
->tile_y_sa
+ *y
+ *height
;
1777 assert(*width
% fmtl
->bw
== 0 ||
1778 right_edge_px
== info
->surf
.logical_level0_px
.width
);
1779 assert(*height
% fmtl
->bh
== 0 ||
1780 bottom_edge_px
== info
->surf
.logical_level0_px
.height
);
1782 *width
= DIV_ROUND_UP(*width
, fmtl
->bw
);
1783 *height
= DIV_ROUND_UP(*height
, fmtl
->bh
);
1786 assert(*x
% fmtl
->bw
== 0);
1787 assert(*y
% fmtl
->bh
== 0);
1791 info
->surf
.logical_level0_px
.width
=
1792 DIV_ROUND_UP(info
->surf
.logical_level0_px
.width
, fmtl
->bw
);
1793 info
->surf
.logical_level0_px
.height
=
1794 DIV_ROUND_UP(info
->surf
.logical_level0_px
.height
, fmtl
->bh
);
1796 assert(info
->surf
.phys_level0_sa
.width
% fmtl
->bw
== 0);
1797 assert(info
->surf
.phys_level0_sa
.height
% fmtl
->bh
== 0);
1798 info
->surf
.phys_level0_sa
.width
/= fmtl
->bw
;
1799 info
->surf
.phys_level0_sa
.height
/= fmtl
->bh
;
1801 assert(info
->tile_x_sa
% fmtl
->bw
== 0);
1802 assert(info
->tile_y_sa
% fmtl
->bh
== 0);
1803 info
->tile_x_sa
/= fmtl
->bw
;
1804 info
->tile_y_sa
/= fmtl
->bh
;
1806 /* It's now an uncompressed surface so we need an uncompressed format */
1807 info
->surf
.format
= get_copy_format_for_bpb(isl_dev
, fmtl
->bpb
);
1811 surf_fake_rgb_with_red(const struct isl_device
*isl_dev
,
1812 struct brw_blorp_surface_info
*info
,
1813 uint32_t *x
, uint32_t *width
)
1815 surf_convert_to_single_slice(isl_dev
, info
);
1817 info
->surf
.logical_level0_px
.width
*= 3;
1818 info
->surf
.phys_level0_sa
.width
*= 3;
1822 enum isl_format red_format
;
1823 switch (info
->view
.format
) {
1824 case ISL_FORMAT_R8G8B8_UNORM
:
1825 red_format
= ISL_FORMAT_R8_UNORM
;
1827 case ISL_FORMAT_R8G8B8_UINT
:
1828 red_format
= ISL_FORMAT_R8_UINT
;
1830 case ISL_FORMAT_R16G16B16_UNORM
:
1831 red_format
= ISL_FORMAT_R16_UNORM
;
1833 case ISL_FORMAT_R16G16B16_UINT
:
1834 red_format
= ISL_FORMAT_R16_UINT
;
1836 case ISL_FORMAT_R32G32B32_UINT
:
1837 red_format
= ISL_FORMAT_R32_UINT
;
1840 unreachable("Invalid RGB copy destination format");
1842 assert(isl_format_get_layout(red_format
)->channels
.r
.type
==
1843 isl_format_get_layout(info
->view
.format
)->channels
.r
.type
);
1844 assert(isl_format_get_layout(red_format
)->channels
.r
.bits
==
1845 isl_format_get_layout(info
->view
.format
)->channels
.r
.bits
);
1847 info
->surf
.format
= info
->view
.format
= red_format
;
1851 blorp_copy(struct blorp_batch
*batch
,
1852 const struct blorp_surf
*src_surf
,
1853 unsigned src_level
, unsigned src_layer
,
1854 const struct blorp_surf
*dst_surf
,
1855 unsigned dst_level
, unsigned dst_layer
,
1856 uint32_t src_x
, uint32_t src_y
,
1857 uint32_t dst_x
, uint32_t dst_y
,
1858 uint32_t src_width
, uint32_t src_height
)
1860 const struct isl_device
*isl_dev
= batch
->blorp
->isl_dev
;
1861 struct blorp_params params
;
1863 if (src_width
== 0 || src_height
== 0)
1866 blorp_params_init(¶ms
);
1867 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.src
, src_surf
, src_level
,
1868 src_layer
, ISL_FORMAT_UNSUPPORTED
, false);
1869 brw_blorp_surface_info_init(batch
->blorp
, ¶ms
.dst
, dst_surf
, dst_level
,
1870 dst_layer
, ISL_FORMAT_UNSUPPORTED
, true);
1872 struct brw_blorp_blit_prog_key wm_prog_key
= {
1873 .shader_type
= BLORP_SHADER_TYPE_BLIT
1876 const struct isl_format_layout
*src_fmtl
=
1877 isl_format_get_layout(params
.src
.surf
.format
);
1878 const struct isl_format_layout
*dst_fmtl
=
1879 isl_format_get_layout(params
.dst
.surf
.format
);
1881 params
.src
.view
.format
= get_copy_format_for_bpb(isl_dev
, src_fmtl
->bpb
);
1882 if (src_fmtl
->bw
> 1 || src_fmtl
->bh
> 1) {
1883 surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.src
,
1884 &src_x
, &src_y
, &src_width
, &src_height
);
1885 wm_prog_key
.need_src_offset
= true;
1888 params
.dst
.view
.format
= get_copy_format_for_bpb(isl_dev
, dst_fmtl
->bpb
);
1889 if (dst_fmtl
->bw
> 1 || dst_fmtl
->bh
> 1) {
1890 surf_convert_to_uncompressed(batch
->blorp
->isl_dev
, ¶ms
.dst
,
1891 &dst_x
, &dst_y
, NULL
, NULL
);
1892 wm_prog_key
.need_dst_offset
= true;
1895 /* Once both surfaces are stompped to uncompressed as needed, the
1896 * destination size is the same as the source size.
1898 uint32_t dst_width
= src_width
;
1899 uint32_t dst_height
= src_height
;
1901 if (dst_fmtl
->bpb
% 3 == 0) {
1902 surf_fake_rgb_with_red(batch
->blorp
->isl_dev
, ¶ms
.dst
,
1903 &dst_x
, &dst_width
);
1904 wm_prog_key
.dst_rgb
= true;
1905 wm_prog_key
.need_dst_offset
= true;
1908 do_blorp_blit(batch
, ¶ms
, &wm_prog_key
,
1909 src_x
, src_y
, src_x
+ src_width
, src_y
+ src_height
,
1910 dst_x
, dst_y
, dst_x
+ dst_width
, dst_y
+ dst_height
,