intel/compiler: Add a "base class" for program keys
[mesa.git] / src / intel / blorp / blorp_blit.c
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "blorp_nir_builder.h"
25 #include "compiler/nir/nir_format_convert.h"
26
27 #include "blorp_priv.h"
28
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"
33
34 #define FILE_DEBUG_FLAG DEBUG_BLORP
35
36 static const bool split_blorp_blit_debug = false;
37
38 /**
39 * Enum to specify the order of arguments in a sampler message
40 */
41 enum sampler_message_arg
42 {
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,
51 };
52
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;
62
63 /* gl_FragCoord */
64 nir_variable *frag_coord;
65
66 /* gl_FragColor */
67 nir_variable *color_out;
68 };
69
70 static void
71 brw_blorp_blit_vars_init(nir_builder *b, struct brw_blorp_blit_vars *v,
72 const struct brw_blorp_blit_prog_key *key)
73 {
74 #define LOAD_INPUT(name, type)\
75 v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
76
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))
84
85 #undef LOAD_INPUT
86
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;
90
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;
94 }
95
96 static nir_ssa_def *
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)
100 {
101 nir_ssa_def *coord = nir_f2i32(b, nir_load_var(b, v->frag_coord));
102
103 /* Account for destination surface intratile offset
104 *
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
109 * intra-tile offset.
110 */
111 if (key->need_dst_offset)
112 coord = nir_isub(b, coord, nir_load_var(b, v->v_dst_offset));
113
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));
117 } else {
118 return nir_vec2(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1));
119 }
120 }
121
122 /**
123 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
124 * coordinates.
125 */
126 static nir_ssa_def *
127 blorp_blit_apply_transform(nir_builder *b, nir_ssa_def *src_pos,
128 struct brw_blorp_blit_vars *v)
129 {
130 nir_ssa_def *coord_transform = nir_load_var(b, v->v_coord_transform);
131
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));
136
137 return nir_fadd(b, nir_fmul(b, src_pos, mul), offset);
138 }
139
140 static inline void
141 blorp_nir_discard_if_outside_rect(nir_builder *b, nir_ssa_def *pos,
142 struct brw_blorp_blit_vars *v)
143 {
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);
150
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);
155
156 nir_ssa_def *oob = nir_ior(b, nir_ior(b, c0, c1), nir_ior(b, c2, c3));
157
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);
162 }
163
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)
168 {
169 nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
170
171 tex->op = op;
172
173 tex->dest_type = dst_type;
174 tex->is_array = false;
175 tex->is_shadow = false;
176
177 /* Blorp only has one texture and it's bound at unit 0 */
178 tex->texture_index = 0;
179 tex->sampler_index = 0;
180
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.
184 */
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));
188
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;
192
193 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
194
195 return tex;
196 }
197
198 static nir_ssa_def *
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)
201 {
202 if (key->need_src_offset)
203 pos = nir_fadd(b, pos, nir_i2f32(b, nir_load_var(b, v->v_src_offset)));
204
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));
208
209 nir_tex_instr *tex =
210 blorp_create_nir_tex_instr(b, v, nir_texop_tex, pos, 2,
211 key->texture_data_type);
212
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));
217
218 nir_builder_instr_insert(b, &tex->instr);
219
220 return &tex->dest.ssa;
221 }
222
223 static nir_ssa_def *
224 blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
225 nir_ssa_def *pos, nir_alu_type dst_type)
226 {
227 nir_tex_instr *tex =
228 blorp_create_nir_tex_instr(b, v, nir_texop_txf, pos, 2, dst_type);
229
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));
233
234 nir_builder_instr_insert(b, &tex->instr);
235
236 return &tex->dest.ssa;
237 }
238
239 static nir_ssa_def *
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)
242 {
243 nir_tex_instr *tex =
244 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms, pos,
245 mcs != NULL ? 3 : 2, dst_type);
246
247 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
248
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));
252 } else {
253 assert(pos->num_components == 3);
254 tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
255 }
256
257 if (mcs) {
258 tex->src[2].src_type = nir_tex_src_ms_mcs;
259 tex->src[2].src = nir_src_for_ssa(mcs);
260 }
261
262 nir_builder_instr_insert(b, &tex->instr);
263
264 return &tex->dest.ssa;
265 }
266
267 static nir_ssa_def *
268 blorp_blit_txf_ms_mcs(nir_builder *b, struct brw_blorp_blit_vars *v,
269 nir_ssa_def *pos)
270 {
271 nir_tex_instr *tex =
272 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms_mcs,
273 pos, 1, nir_type_int);
274
275 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
276
277 nir_builder_instr_insert(b, &tex->instr);
278
279 return &tex->dest.ssa;
280 }
281
282 /**
283 * Emit code to compensate for the difference between Y and W tiling.
284 *
285 * This code modifies the X and Y coordinates according to the formula:
286 *
287 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
288 *
289 * (See brw_blorp_build_nir_shader).
290 */
291 static inline nir_ssa_def *
292 blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
293 {
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);
297
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
300 * using W tiling.
301 *
302 * If we break down the low order bits of X and Y, using a
303 * single letter to represent each low-order bit:
304 *
305 * X = A << 7 | 0bBCDEFGH
306 * Y = J << 5 | 0bKLMNP (1)
307 *
308 * Then we can apply the Y tiling formula to see the memory offset being
309 * addressed:
310 *
311 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
312 *
313 * If we apply the W detiling formula to this memory location, that the
314 * corresponding X' and Y' coordinates are:
315 *
316 * X' = A << 6 | 0bBCDPFH (3)
317 * Y' = J << 6 | 0bKLMNEG
318 *
319 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
320 * we need to make the following computation:
321 *
322 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
323 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
324 */
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);
329
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);
334
335 return nir_vec2(b, x_W, y_W);
336 }
337
338 /**
339 * Emit code to compensate for the difference between Y and W tiling.
340 *
341 * This code modifies the X and Y coordinates according to the formula:
342 *
343 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
344 *
345 * (See brw_blorp_build_nir_shader).
346 */
347 static inline nir_ssa_def *
348 blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
349 {
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);
353
354 /* Applying the same logic as above, but in reverse, we obtain the
355 * formulas:
356 *
357 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
358 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
359 */
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);
365
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);
369
370 return nir_vec2(b, x_Y, y_Y);
371 }
372
373 /**
374 * Emit code to compensate for the difference between MSAA and non-MSAA
375 * surfaces.
376 *
377 * This code modifies the X and Y coordinates according to the formula:
378 *
379 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
380 *
381 * (See brw_blorp_blit_program).
382 */
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)
386 {
387 assert(pos->num_components == 2 || pos->num_components == 3);
388
389 switch (layout) {
390 case ISL_MSAA_LAYOUT_NONE:
391 assert(pos->num_components == 2);
392 return pos;
393 case ISL_MSAA_LAYOUT_ARRAY:
394 /* No translation needed */
395 return pos;
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);
401
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) {
405 case 2:
406 case 4:
407 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
408 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
409 * Y' = Y
410 *
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)
414 */
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) {
419 y_out = y_in;
420 } else {
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);
424 }
425 break;
426
427 case 8:
428 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
429 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
430 * | (X & 0b1)
431 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
432 */
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);
440 break;
441
442 case 16:
443 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
444 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
445 * | (X & 0b1)
446 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
447 * | (Y & 0b1)
448 */
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);
457 break;
458
459 default:
460 unreachable("Invalid number of samples for IMS layout");
461 }
462
463 return nir_vec2(b, x_out, y_out);
464 }
465
466 default:
467 unreachable("Invalid MSAA layout");
468 }
469 }
470
471 /**
472 * Emit code to compensate for the difference between MSAA and non-MSAA
473 * surfaces.
474 *
475 * This code modifies the X and Y coordinates according to the formula:
476 *
477 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
478 *
479 * (See brw_blorp_blit_program).
480 */
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)
484 {
485 assert(pos->num_components == 2 || pos->num_components == 3);
486
487 switch (layout) {
488 case ISL_MSAA_LAYOUT_NONE:
489 /* No translation necessary, and S should already be zero. */
490 assert(pos->num_components == 2);
491 return pos;
492 case ISL_MSAA_LAYOUT_ARRAY:
493 /* No translation necessary. */
494 return pos;
495 case ISL_MSAA_LAYOUT_INTERLEAVED: {
496 assert(pos->num_components == 2);
497
498 nir_ssa_def *x_in = nir_channel(b, pos, 0);
499 nir_ssa_def *y_in = nir_channel(b, pos, 1);
500
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) {
505 case 2:
506 case 4:
507 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
508 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
509 * S = (X & 0b10) >> 1
510 *
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
515 */
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) {
519 y_out = y_in;
520 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
521 } else {
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);
526 }
527 break;
528
529 case 8:
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
534 */
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);
542 break;
543
544 case 16:
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
550 */
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);
559 break;
560
561 default:
562 unreachable("Invalid number of samples for IMS layout");
563 }
564
565 return nir_vec3(b, x_out, y_out, s_out);
566 }
567
568 default:
569 unreachable("Invalid MSAA layout");
570 }
571 }
572
573 /**
574 * Count the number of trailing 1 bits in the given value. For example:
575 *
576 * count_trailing_one_bits(0) == 0
577 * count_trailing_one_bits(7) == 3
578 * count_trailing_one_bits(11) == 2
579 */
580 static inline int count_trailing_one_bits(unsigned value)
581 {
582 #ifdef HAVE___BUILTIN_CTZ
583 return __builtin_ctz(~value);
584 #else
585 return util_bitcount(value & ~(value + 1));
586 #endif
587 }
588
589 static nir_ssa_def *
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)
595 {
596 /* If non-null, this is the outer-most if statement */
597 nir_if *outer_if = NULL;
598
599 nir_variable *color =
600 nir_local_variable_create(b->impl, glsl_vec4_type(), "color");
601
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);
605
606 nir_op combine_op;
607 switch (filter) {
608 case BLORP_FILTER_AVERAGE:
609 assert(dst_type == nir_type_float);
610 combine_op = nir_op_fadd;
611 break;
612
613 case BLORP_FILTER_MIN_SAMPLE:
614 switch (dst_type) {
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");
619 }
620 break;
621
622 case BLORP_FILTER_MAX_SAMPLE:
623 switch (dst_type) {
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");
628 }
629 break;
630
631 default:
632 unreachable("Invalid filter");
633 }
634
635 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
636 *
637 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
638 *
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
642 * precision.
643 *
644 * We perform this computation by treating the texture_data array as a
645 * stack and performing the following operations:
646 *
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
655 *
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.
660 *
661 * For integer formats, we replace the add operations with average
662 * operations and skip the final division.
663 */
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 */
668
669 /* Push sample i onto the stack */
670 assert(stack_depth < ARRAY_SIZE(texture_data));
671
672 nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
673 nir_channel(b, pos, 1),
674 nir_imm_int(b, i));
675 texture_data[stack_depth++] = blorp_nir_txf_ms(b, v, ms_pos, mcs, dst_type);
676
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:
680 *
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."
685 *
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.
691 *
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
695 * when MCS == 0.
696 */
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)));
702 }
703 nir_ssa_def *mcs_clear =
704 blorp_nir_mcs_is_clear_color(b, mcs, tex_samples);
705
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);
709
710 b->cursor = nir_after_cf_list(&if_stmt->then_list);
711 nir_store_var(b, color, texture_data[0], 0xf);
712
713 b->cursor = nir_after_cf_list(&if_stmt->else_list);
714 outer_if = if_stmt;
715 }
716
717 for (int j = 0; j < count_trailing_one_bits(i); j++) {
718 assert(stack_depth >= 2);
719 --stack_depth;
720
721 texture_data[stack_depth - 1] =
722 nir_build_alu(b, combine_op,
723 texture_data[stack_depth - 1],
724 texture_data[stack_depth],
725 NULL, NULL);
726 }
727 }
728
729 /* We should have just 1 sample on the stack now. */
730 assert(stack_depth == 1);
731
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));
736 }
737
738 nir_store_var(b, color, texture_data[0], 0xf);
739
740 if (outer_if)
741 b->cursor = nir_after_cf_node(&outer_if->cf_node);
742
743 return nir_load_var(b, color);
744 }
745
746 static nir_ssa_def *
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)
751 {
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);
755
756 /* Translate coordinates to lay out the samples in a rectangular grid
757 * roughly corresponding to sample locations.
758 */
759 pos_xy = nir_fmul(b, pos_xy, scale);
760 /* Adjust coordinates so that integers represent pixel centers rather
761 * than pixel edges.
762 */
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.
766 */
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)));
770
771 /* Store the fractional parts to be used as bilinear interpolation
772 * coefficients.
773 */
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);
777
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);
783
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);
786
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.
791 */
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);
795
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.
801 *
802 * In the case of 2x MSAA, the layout of sample indices is reversed from
803 * the layout of sample numbers:
804 *
805 * sample index layout : --------- sample number layout : ---------
806 * | 0 | 1 | | 1 | 0 |
807 * --------- ---------
808 *
809 * In case of 4x MSAA, layout of sample indices matches the layout of
810 * sample numbers:
811 * ---------
812 * | 0 | 1 |
813 * ---------
814 * | 2 | 3 |
815 * ---------
816 *
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 * --------- ---------
827 *
828 * Fortunately, this can be done fairly easily as:
829 * S' = (0x17306425 >> (S * 4)) & 0xf
830 *
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 * --------------------- ---------------------
842 *
843 * This is equivalent to
844 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
845 */
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);
851
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,
866 nir_imm_int(b, -8)),
867 nir_imm_int(b, 2))),
868 nir_imm_int(b, 0xf));
869
870 sample = nir_bcsel(b, nir_ilt(b, sample, nir_imm_int(b, 8)),
871 sample_low, sample_high);
872 }
873 nir_ssa_def *pos_ms = nir_vec3(b, nir_channel(b, sample_coords_int, 0),
874 nir_channel(b, sample_coords_int, 1),
875 sample);
876 tex_data[i] = blorp_nir_txf_ms(b, v, pos_ms, mcs, key->texture_data_type);
877 }
878
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),
883 frac_y);
884 }
885
886 /** Perform a color bit-cast operation
887 *
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.
894 */
895 static nir_ssa_def *
896 bit_cast_color(struct nir_builder *b, nir_ssa_def *color,
897 const struct brw_blorp_blit_prog_key *key)
898 {
899 assert(key->texture_data_type == nir_type_uint);
900
901 if (key->src_format == key->dst_format)
902 return color;
903
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);
908
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);
913
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);
917
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);
921
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,
930 };
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,
938 };
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);
942 } else {
943 const unsigned src_bpc = src_fmtl->channels.r.bits;
944 const unsigned dst_bpc = dst_fmtl->channels.r.bits;
945
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);
958
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);
963
964 color = nir_format_bitcast_uvec_unmasked(b, color, src_bpc, dst_bpc);
965 }
966
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]);
973 }
974
975 static nir_ssa_def *
976 select_color_channel(struct nir_builder *b, nir_ssa_def *color,
977 nir_alu_type data_type,
978 enum isl_channel_select chan)
979 {
980 if (chan == ISL_CHANNEL_SELECT_ZERO) {
981 return nir_imm_int(b, 0);
982 } else if (chan == ISL_CHANNEL_SELECT_ONE) {
983 switch (data_type) {
984 case nir_type_int:
985 case nir_type_uint:
986 return nir_imm_int(b, 1);
987 case nir_type_float:
988 return nir_imm_float(b, 1);
989 default:
990 unreachable("Invalid data type");
991 }
992 } else {
993 assert((unsigned)(chan - ISL_CHANNEL_SELECT_RED) < 4);
994 return nir_channel(b, color, chan - ISL_CHANNEL_SELECT_RED);
995 }
996 }
997
998 static nir_ssa_def *
999 swizzle_color(struct nir_builder *b, nir_ssa_def *color,
1000 struct isl_swizzle swizzle, nir_alu_type data_type)
1001 {
1002 return nir_vec4(b,
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));
1007 }
1008
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)
1012 {
1013 /* All of our color conversions end up generating a single-channel color
1014 * value that we need to write out.
1015 */
1016 nir_ssa_def *value;
1017
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.
1023 */
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);
1033 } else {
1034 unreachable("Unsupported format conversion");
1035 }
1036
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);
1041 else
1042 out_comps[i] = nir_ssa_undef(b, 1, 32);
1043 }
1044 return nir_vec(b, out_comps, 4);
1045 }
1046
1047 /**
1048 * Generator for WM programs used in BLORP blits.
1049 *
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:
1055 *
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))
1058 *
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
1061 * function:
1062 *
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)
1067 *
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:
1070 *
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
1078 *
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
1081 * the Y coordinate:
1082 *
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
1090 *
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:
1094 *
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)
1100 * X' = X * cpp
1101 * Y' = Y + S * qpitch
1102 * detile(x_tiled, A) = (X, Y, S)
1103 * where X = X' / cpp
1104 * Y = Y' % qpitch
1105 * S = Y' / qpitch
1106 * Y' = (tile_num / tile_pitch) << 3
1107 * | (A & 0b111000000000) >> 9
1108 * X' = (tile_num % tile_pitch) << 9
1109 * | (A & 0b111111111)
1110 *
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).
1115 *
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:
1119 *
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
1125 * | (X' & 0b1111)
1126 * X' = X * cpp
1127 * Y' = Y + S * qpitch
1128 * detile(y_tiled, A) = (X, Y, S)
1129 * where X = X' / cpp
1130 * Y = Y' % qpitch
1131 * S = Y' / qpitch
1132 * Y' = (tile_num / tile_pitch) << 5
1133 * | (A & 0b111110000) >> 4
1134 * X' = (tile_num % tile_pitch) << 7
1135 * | (A & 0b111000000000) >> 5
1136 * | (A & 0b1111)
1137 *
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):
1142 *
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
1151 * | (Y' & 0b1) << 1
1152 * | (X' & 0b1)
1153 * X' = X * cpp = X
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
1162 * | (A & 0b10) >> 1
1163 * X' = (tile_num % tile_pitch) << 6
1164 * | (A & 0b111000000000) >> 6
1165 * | (A & 0b10000) >> 2
1166 * | (A & 0b100) >> 1
1167 * | (A & 0b1)
1168 *
1169 * Finally, for a non-tiled surface, tile() simply combines together the X and
1170 * Y coordinates in the natural way:
1171 *
1172 * tile(untiled, X, Y, S) = A
1173 * where A = Y * pitch + X'
1174 * X' = X * cpp
1175 * Y' = Y + S * qpitch
1176 * detile(untiled, A) = (X, Y, S)
1177 * where X = X' / cpp
1178 * Y = Y' % qpitch
1179 * S = Y' / qpitch
1180 * X' = A % pitch
1181 * Y' = A / pitch
1182 *
1183 * (In these formulas, pitch is the number of bytes occupied by a single row
1184 * of samples).
1185 */
1186 static nir_shader *
1187 brw_blorp_build_nir_shader(struct blorp_context *blorp, void *mem_ctx,
1188 const struct brw_blorp_blit_prog_key *key)
1189 {
1190 const struct gen_device_info *devinfo = blorp->isl_dev->info;
1191 nir_ssa_def *src_pos, *dst_pos, *color;
1192
1193 /* Sanity checks */
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
1199 * be together.
1200 */
1201 assert(key->persample_msaa_dispatch);
1202 }
1203
1204 if (key->persample_msaa_dispatch) {
1205 /* It only makes sense to do persample dispatch if the render target is
1206 * configured as multisampled.
1207 */
1208 assert(key->rt_samples > 0);
1209 }
1210
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));
1220
1221 nir_builder b;
1222 blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
1223
1224 struct brw_blorp_blit_vars v;
1225 brw_blorp_blit_vars_init(&b, &v, key);
1226
1227 dst_pos = blorp_blit_get_frag_coords(&b, key, &v);
1228
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;
1232
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:
1236 *
1237 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1238 *
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
1242 * difference.
1243 */
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,
1248 key->rt_layout);
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,
1254 key->dst_layout);
1255 }
1256
1257 nir_ssa_def *comp = NULL;
1258 if (key->dst_rgb) {
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.
1263 */
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));
1269 }
1270
1271 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1272 *
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.
1275 *
1276 * If we need to kill pixels that are outside the destination rectangle,
1277 * now is the time to do it.
1278 */
1279 if (key->use_kill)
1280 blorp_nir_discard_if_outside_rect(&b, dst_pos, &v);
1281
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.
1287 */
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));
1291 }
1292
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.
1295 */
1296 if (key->src_samples == 1)
1297 src_pos = nir_channels(&b, src_pos, 0x3);
1298
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.
1302 */
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);
1310 } else {
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));
1315 }
1316
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:
1320 *
1321 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1322 *
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.
1326 */
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,
1331 key->src_layout);
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,
1337 key->tex_layout);
1338 }
1339
1340 if (key->need_src_offset)
1341 src_pos = nir_iadd(&b, src_pos, nir_load_var(&b, v.v_src_offset));
1342
1343 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1344 *
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.
1348 */
1349 if (key->src_samples == 1) {
1350 color = blorp_nir_txf(&b, &v, src_pos, key->texture_data_type);
1351 } else {
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);
1355
1356 color = blorp_nir_txf_ms(&b, &v, src_pos, mcs, key->texture_data_type);
1357 }
1358 break;
1359
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);
1364
1365 if (key->src_samples == 1) {
1366 color = blorp_nir_tex(&b, &v, key, src_pos);
1367 } else {
1368 assert(!key->use_kill);
1369 color = blorp_nir_manual_blend_bilinear(&b, src_pos, key->src_samples,
1370 key, &v);
1371 }
1372 break;
1373
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);
1380
1381 /* Resolves (effecively) use texelFetch, so we need integers and we
1382 * don't care about the sample index if we got one.
1383 */
1384 src_pos = nir_f2i32(&b, nir_channels(&b, src_pos, 0x3));
1385
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.
1393 */
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);
1400 } else {
1401 /* Gen7+ hardware doesn't automaticaly blend. */
1402 color = blorp_nir_combine_samples(&b, &v, src_pos, key->src_samples,
1403 key->tex_aux_usage,
1404 key->texture_data_type,
1405 key->filter);
1406 }
1407 break;
1408
1409 default:
1410 unreachable("Invalid blorp filter");
1411 }
1412
1413 if (!isl_swizzle_is_identity(key->src_swizzle)) {
1414 color = swizzle_color(&b, color, key->src_swizzle,
1415 key->texture_data_type);
1416 }
1417
1418 if (!isl_swizzle_is_identity(key->dst_swizzle)) {
1419 color = swizzle_color(&b, color, isl_swizzle_invert(key->dst_swizzle),
1420 nir_type_int);
1421 }
1422
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.
1432 *
1433 * The Skylake PRM, volume 07, page 658 has a programming note:
1434 *
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."
1439 *
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).
1447 */
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));
1452 }
1453
1454 if (key->dst_rgb) {
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.
1459 */
1460 assert(dst_pos->num_components == 2);
1461
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)));
1468
1469 nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);
1470 color = nir_vec4(&b, color_component, u, u, u);
1471 }
1472
1473 nir_store_var(&b, v.color_out, color, 0xf);
1474
1475 return b.shader;
1476 }
1477
1478 static bool
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)
1482 {
1483 struct blorp_context *blorp = batch->blorp;
1484
1485 if (blorp->lookup_shader(batch, prog_key, sizeof(*prog_key),
1486 &params->wm_prog_kernel, &params->wm_prog_data))
1487 return true;
1488
1489 void *mem_ctx = ralloc_context(NULL);
1490
1491 const unsigned *program;
1492 struct brw_wm_prog_data prog_data;
1493
1494 nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
1495 nir->info.name = ralloc_strdup(nir, "BLORP-blit");
1496
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;
1503
1504 program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
1505 &prog_data);
1506
1507 bool result =
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 &params->wm_prog_kernel, &params->wm_prog_data);
1512
1513 ralloc_free(mem_ctx);
1514 return result;
1515 }
1516
1517 static void
1518 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
1519 GLfloat src0, GLfloat src1,
1520 GLfloat dst0, GLfloat dst1,
1521 bool mirror)
1522 {
1523 double scale = (double)(src1 - src0) / (double)(dst1 - dst0);
1524 if (!mirror) {
1525 /* When not mirroring a coordinate (say, X), we need:
1526 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1527 * Therefore:
1528 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1529 *
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.
1534 */
1535 xform->multiplier = scale;
1536 xform->offset = src0 + (-(double)dst0 + 0.5) * scale;
1537 } else {
1538 /* When mirroring X we need:
1539 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1540 * Therefore:
1541 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1542 */
1543 xform->multiplier = -scale;
1544 xform->offset = src0 + ((double)dst1 - 0.5) * scale;
1545 }
1546 }
1547
1548 static inline void
1549 surf_get_intratile_offset_px(struct brw_blorp_surface_info *info,
1550 uint32_t *tile_x_px, uint32_t *tile_y_px)
1551 {
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;
1559 } else {
1560 *tile_x_px = info->tile_x_sa;
1561 *tile_y_px = info->tile_y_sa;
1562 }
1563 }
1564
1565 void
1566 blorp_surf_convert_to_single_slice(const struct isl_device *isl_dev,
1567 struct brw_blorp_surface_info *info)
1568 {
1569 bool ok UNUSED;
1570
1571 /* It would be insane to try and do this on a compressed surface */
1572 assert(info->aux_usage == ISL_AUX_USAGE_NONE);
1573
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)
1578 return;
1579
1580 /* If this gets triggered then we've gotten here twice which. This
1581 * shouldn't happen thanks to the above early return.
1582 */
1583 assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
1584
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;
1588 else
1589 layer = info->view.base_array_layer;
1590
1591 uint32_t byte_offset;
1592 isl_surf_get_image_surf(isl_dev, &info->surf,
1593 info->view.base_level, layer, z,
1594 &info->surf,
1595 &byte_offset, &info->tile_x_sa, &info->tile_y_sa);
1596 info->addr.offset += byte_offset;
1597
1598 uint32_t tile_x_px, tile_y_px;
1599 surf_get_intratile_offset_px(info, &tile_x_px, &tile_y_px);
1600
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.
1605 */
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;
1610
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;
1616 info->z_offset = 0;
1617 }
1618
1619 static void
1620 surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
1621 struct brw_blorp_surface_info *info)
1622 {
1623 assert(info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1624
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);
1627
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;
1631 }
1632
1633 static void
1634 surf_retile_w_to_y(const struct isl_device *isl_dev,
1635 struct brw_blorp_surface_info *info)
1636 {
1637 assert(info->surf.tiling == ISL_TILING_W);
1638
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);
1641
1642 /* On gen7+, we don't have interleaved multisampling for color render
1643 * targets so we have to fake it.
1644 *
1645 * TODO: Are we sure we don't also need to fake it on gen6?
1646 */
1647 if (isl_dev->info->gen > 6 &&
1648 info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1649 surf_fake_interleaved_msaa(isl_dev, info);
1650 }
1651
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().
1657 */
1658 info->surf.image_alignment_el = isl_extent3d(4, 2, 1);
1659 }
1660
1661 /* Now that we've converted everything to a simple 2-D surface with only
1662 * one miplevel, we can go about retiling it.
1663 */
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;
1672 }
1673
1674 static bool
1675 can_shrink_surface(const struct brw_blorp_surface_info *surf)
1676 {
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.
1681 *
1682 * Currently the cases where we must split the blit are limited to cases
1683 * where we don't have a aux buffer.
1684 */
1685 if (surf->aux_addr.buffer != NULL)
1686 return false;
1687
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.
1691 */
1692 if (surf->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY)
1693 return false;
1694
1695 return true;
1696 }
1697
1698 static bool
1699 can_shrink_surfaces(const struct blorp_params *params)
1700 {
1701 return
1702 can_shrink_surface(&params->src) &&
1703 can_shrink_surface(&params->dst);
1704 }
1705
1706 static unsigned
1707 get_max_surface_size(const struct gen_device_info *devinfo,
1708 const struct blorp_params *params)
1709 {
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 */
1713 else
1714 return max;
1715 }
1716
1717 struct blt_axis {
1718 double src0, src1, dst0, dst1;
1719 bool mirror;
1720 };
1721
1722 struct blt_coords {
1723 struct blt_axis x, y;
1724 };
1725
1726 static enum isl_format
1727 get_red_format_for_rgb_format(enum isl_format format)
1728 {
1729 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1730
1731 switch (fmtl->channels.r.bits) {
1732 case 8:
1733 switch (fmtl->channels.r.type) {
1734 case ISL_UNORM:
1735 return ISL_FORMAT_R8_UNORM;
1736 case ISL_SNORM:
1737 return ISL_FORMAT_R8_SNORM;
1738 case ISL_UINT:
1739 return ISL_FORMAT_R8_UINT;
1740 case ISL_SINT:
1741 return ISL_FORMAT_R8_SINT;
1742 default:
1743 unreachable("Invalid 8-bit RGB channel type");
1744 }
1745 case 16:
1746 switch (fmtl->channels.r.type) {
1747 case ISL_UNORM:
1748 return ISL_FORMAT_R16_UNORM;
1749 case ISL_SNORM:
1750 return ISL_FORMAT_R16_SNORM;
1751 case ISL_SFLOAT:
1752 return ISL_FORMAT_R16_FLOAT;
1753 case ISL_UINT:
1754 return ISL_FORMAT_R16_UINT;
1755 case ISL_SINT:
1756 return ISL_FORMAT_R16_SINT;
1757 default:
1758 unreachable("Invalid 8-bit RGB channel type");
1759 }
1760 case 32:
1761 switch (fmtl->channels.r.type) {
1762 case ISL_SFLOAT:
1763 return ISL_FORMAT_R32_FLOAT;
1764 case ISL_UINT:
1765 return ISL_FORMAT_R32_UINT;
1766 case ISL_SINT:
1767 return ISL_FORMAT_R32_SINT;
1768 default:
1769 unreachable("Invalid 8-bit RGB channel type");
1770 }
1771 default:
1772 unreachable("Invalid number of red channel bits");
1773 }
1774 }
1775
1776 void
1777 surf_fake_rgb_with_red(const struct isl_device *isl_dev,
1778 struct brw_blorp_surface_info *info)
1779 {
1780 blorp_surf_convert_to_single_slice(isl_dev, info);
1781
1782 info->surf.logical_level0_px.width *= 3;
1783 info->surf.phys_level0_sa.width *= 3;
1784 info->tile_x_sa *= 3;
1785
1786 enum isl_format red_format =
1787 get_red_format_for_rgb_format(info->view.format);
1788
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);
1793
1794 info->surf.format = info->view.format = red_format;
1795 }
1796
1797 enum blit_shrink_status {
1798 BLIT_NO_SHRINK = 0,
1799 BLIT_WIDTH_SHRINK = 1,
1800 BLIT_HEIGHT_SHRINK = 2,
1801 };
1802
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.
1806 */
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)
1812 {
1813 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
1814
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;
1819 } else {
1820 wm_prog_key->texture_data_type = nir_type_float;
1821 }
1822
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;
1826
1827 wm_prog_key->tex_aux_usage = params->src.aux_usage;
1828
1829 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1830 * dst.
1831 */
1832 wm_prog_key->src_layout = params->src.surf.msaa_layout;
1833 wm_prog_key->dst_layout = params->dst.surf.msaa_layout;
1834
1835 /* Round floating point values to nearest integer to avoid "off by one texel"
1836 * kind of errors when blitting.
1837 */
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);
1842
1843 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[0],
1844 coords->x.src0, coords->x.src1,
1845 coords->x.dst0, coords->x.dst1,
1846 coords->x.mirror);
1847 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[1],
1848 coords->y.src0, coords->y.src1,
1849 coords->y.dst0, coords->y.dst1,
1850 coords->y.mirror);
1851
1852
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.
1856 */
1857 if (params->dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1858 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->dst);
1859 wm_prog_key->need_dst_offset = true;
1860 }
1861
1862 if (params->src.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1863 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->src);
1864 wm_prog_key->need_src_offset = true;
1865 }
1866 }
1867
1868 if (devinfo->gen > 6 &&
1869 params->dst.surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1870 assert(params->dst.surf.samples > 1);
1871
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?
1879 *
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.
1883 */
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;
1890
1891 surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params->dst);
1892
1893 wm_prog_key->use_kill = true;
1894 wm_prog_key->need_dst_offset = true;
1895 }
1896
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
1901 * W-tiled.
1902 *
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.
1909 *
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.
1917 *
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.
1922 *
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()).
1932 *
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.
1940 *
1941 * TODO: what if this makes the coordinates (or the texture size) too
1942 * large?
1943 */
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;
1950
1951 /* Retile the surface to Y-tiled */
1952 surf_retile_w_to_y(batch->blorp->isl_dev, &params->dst);
1953
1954 wm_prog_key->dst_tiled_w = true;
1955 wm_prog_key->use_kill = true;
1956 wm_prog_key->need_dst_offset = true;
1957
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
1963 * W and Y tiling.
1964 */
1965 wm_prog_key->persample_msaa_dispatch = true;
1966 }
1967 }
1968
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.
1972 *
1973 * See the comments above concerning x/y offset alignment for the
1974 * destination surface.
1975 *
1976 * TODO: what if this makes the texture size too large?
1977 */
1978 surf_retile_w_to_y(batch->blorp->isl_dev, &params->src);
1979
1980 wm_prog_key->src_tiled_w = true;
1981 wm_prog_key->need_src_offset = true;
1982 }
1983
1984 /* tex_samples and rt_samples are the sample counts that are set up in
1985 * SURFACE_STATE.
1986 */
1987 wm_prog_key->tex_samples = params->src.surf.samples;
1988 wm_prog_key->rt_samples = params->dst.surf.samples;
1989
1990 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1991 * use to access the source and destination surfaces.
1992 */
1993 wm_prog_key->tex_layout = params->src.surf.msaa_layout;
1994 wm_prog_key->rt_layout = params->dst.surf.msaa_layout;
1995
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
2000 * per pixel.
2001 */
2002 wm_prog_key->persample_msaa_dispatch = true;
2003 }
2004
2005 params->num_samples = params->dst.surf.samples;
2006
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);
2018 }
2019
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.
2025 */
2026 params->x0 *= 3;
2027 params->x1 *= 3;
2028
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;
2032
2033 surf_fake_rgb_with_red(batch->blorp->isl_dev, &params->dst);
2034
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;
2055 }
2056
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;
2061 } else {
2062 wm_prog_key->src_swizzle = ISL_SWIZZLE_IDENTITY;
2063 }
2064
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;
2068 } else {
2069 wm_prog_key->dst_swizzle = ISL_SWIZZLE_IDENTITY;
2070 }
2071
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(&params->src,
2075 &params->wm_inputs.src_offset.x,
2076 &params->wm_inputs.src_offset.y);
2077 }
2078
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(&params->dst,
2082 &params->wm_inputs.dst_offset.x,
2083 &params->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;
2088 }
2089
2090 /* For some texture types, we need to pass the layer through the sampler. */
2091 params->wm_inputs.src_z = params->src.z_offset;
2092
2093 if (!brw_blorp_get_blit_kernel(batch, params, wm_prog_key))
2094 return 0;
2095
2096 if (!blorp_ensure_sf_program(batch, params))
2097 return 0;
2098
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;
2107
2108 if (result == 0) {
2109 batch->blorp->exec(batch, params);
2110 }
2111
2112 return result;
2113 }
2114
2115 /* Adjust split blit source coordinates for the current destination
2116 * coordinates.
2117 */
2118 static void
2119 adjust_split_source_coords(const struct blt_axis *orig,
2120 struct blt_axis *split_coords,
2121 double scale)
2122 {
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.
2127 */
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);
2132 }
2133
2134 static struct isl_extent2d
2135 get_px_size_sa(const struct isl_surf *surf)
2136 {
2137 static const struct isl_extent2d one_to_one = { .w = 1, .h = 1 };
2138
2139 if (surf->msaa_layout != ISL_MSAA_LAYOUT_INTERLEAVED)
2140 return one_to_one;
2141 else
2142 return isl_get_interleaved_msaa_px_size_sa(surf->samples);
2143 }
2144
2145 static void
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)
2149 {
2150 uint32_t byte_offset, x_offset_sa, y_offset_sa, size;
2151 struct isl_extent2d px_size_sa;
2152 int adjust;
2153
2154 blorp_surf_convert_to_single_slice(dev, info);
2155
2156 px_size_sa = get_px_size_sa(&info->surf);
2157
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
2160 * calculations.
2161 */
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,
2167 &byte_offset,
2168 &info->tile_x_sa, &info->tile_y_sa);
2169
2170 info->addr.offset += byte_offset;
2171
2172 adjust = (int)info->tile_x_sa / px_size_sa.w - (int)*x0;
2173 *x0 += adjust;
2174 *x1 += adjust;
2175 info->tile_x_sa = 0;
2176
2177 adjust = (int)info->tile_y_sa / px_size_sa.h - (int)*y0;
2178 *y0 += adjust;
2179 *y1 += adjust;
2180 info->tile_y_sa = 0;
2181
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;
2185
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;
2189 }
2190
2191 static void
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)
2196 {
2197 /* Shrink source surface */
2198 shrink_surface_params(dev, &params->src, &coords->x.src0, &coords->x.src1,
2199 &coords->y.src0, &coords->y.src1);
2200 wm_prog_key->need_src_offset = false;
2201
2202 /* Shrink destination surface */
2203 shrink_surface_params(dev, &params->dst, &coords->x.dst0, &coords->x.dst1,
2204 &coords->y.dst0, &coords->y.dst1);
2205 wm_prog_key->need_dst_offset = false;
2206 }
2207
2208 static void
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)
2213 {
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;
2221 if (orig->x.mirror)
2222 x_scale = -x_scale;
2223 if (orig->y.mirror)
2224 y_scale = -y_scale;
2225
2226 bool x_done, y_done;
2227 bool shrink = split_blorp_blit_debug && can_shrink_surfaces(orig_params);
2228 do {
2229 params = *orig_params;
2230 blit_coords = split_coords;
2231 if (shrink)
2232 shrink_surfaces(batch->blorp->isl_dev, &params, wm_prog_key,
2233 &blit_coords);
2234 enum blit_shrink_status result =
2235 try_blorp_blit(batch, &params, wm_prog_key, &blit_coords);
2236
2237 if (result & BLIT_WIDTH_SHRINK) {
2238 w /= 2.0;
2239 assert(w >= 1.0);
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);
2242 }
2243 if (result & BLIT_HEIGHT_SHRINK) {
2244 h /= 2.0;
2245 assert(h >= 1.0);
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);
2248 }
2249
2250 if (result != 0) {
2251 assert(can_shrink_surfaces(orig_params));
2252 shrink = true;
2253 continue;
2254 }
2255
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);
2258 if (x_done) {
2259 break;
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);
2266 } else {
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);
2270 }
2271 } while (true);
2272 }
2273
2274 void
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)
2288 {
2289 struct blorp_params params;
2290 blorp_params_init(&params);
2291
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);
2297
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;
2304 }
2305 }
2306
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.
2312 */
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;
2316 }
2317
2318 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2319 src_layer, src_format, false);
2320 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2321 dst_layer, dst_format, true);
2322
2323 params.src.view.swizzle = src_swizzle;
2324 params.dst.view.swizzle = dst_swizzle;
2325
2326 const struct isl_format_layout *src_fmtl =
2327 isl_format_get_layout(params.src.view.format);
2328
2329 struct brw_blorp_blit_prog_key wm_prog_key = {
2330 .shader_type = BLORP_SHADER_TYPE_BLIT,
2331 .filter = filter,
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),
2338 };
2339
2340 /* Scaling factors used for bilinear filtering in multisample scaled
2341 * blits.
2342 */
2343 if (params.src.surf.samples == 16)
2344 wm_prog_key.x_scale = 4.0f;
2345 else
2346 wm_prog_key.x_scale = 2.0f;
2347 wm_prog_key.y_scale = params.src.surf.samples / wm_prog_key.x_scale;
2348
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;
2355
2356 struct blt_coords coords = {
2357 .x = {
2358 .src0 = src_x0,
2359 .src1 = src_x1,
2360 .dst0 = dst_x0,
2361 .dst1 = dst_x1,
2362 .mirror = mirror_x
2363 },
2364 .y = {
2365 .src0 = src_y0,
2366 .src1 = src_y1,
2367 .dst0 = dst_y0,
2368 .dst1 = dst_y1,
2369 .mirror = mirror_y
2370 }
2371 };
2372
2373 do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2374 }
2375
2376 static enum isl_format
2377 get_copy_format_for_bpb(const struct isl_device *isl_dev, unsigned bpb)
2378 {
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.
2391 */
2392 if (ISL_DEV_GEN(isl_dev) >= 9) {
2393 switch (bpb) {
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;
2402 default:
2403 unreachable("Unknown format bpb");
2404 }
2405 } else {
2406 switch (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;
2415 default:
2416 unreachable("Unknown format bpb");
2417 }
2418 }
2419 }
2420
2421 /** Returns a UINT format that is CCS-compatible with the given format
2422 *
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.
2429 *
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.
2436 */
2437 static enum isl_format
2438 get_ccs_compatible_uint_format(const struct isl_format_layout *fmtl)
2439 {
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;
2448
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;
2457
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;
2464
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;
2477
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;
2484
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;
2491
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;
2497
2498 default:
2499 unreachable("Not a compressible format");
2500 }
2501 }
2502
2503 void
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)
2508 {
2509 const struct isl_format_layout *fmtl =
2510 isl_format_get_layout(info->surf.format);
2511
2512 assert(fmtl->bw > 1 || fmtl->bh > 1);
2513
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
2517 * block size.
2518 */
2519 blorp_surf_convert_to_single_slice(isl_dev, info);
2520
2521 if (width && height) {
2522 #ifndef NDEBUG
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);
2529 #endif
2530 *width = DIV_ROUND_UP(*width, fmtl->bw);
2531 *height = DIV_ROUND_UP(*height, fmtl->bh);
2532 }
2533
2534 if (x && y) {
2535 assert(*x % fmtl->bw == 0);
2536 assert(*y % fmtl->bh == 0);
2537 *x /= fmtl->bw;
2538 *y /= fmtl->bh;
2539 }
2540
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);
2543
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;
2548
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);
2551 }
2552
2553 void
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)
2562 {
2563 const struct isl_device *isl_dev = batch->blorp->isl_dev;
2564 struct blorp_params params;
2565
2566 if (src_width == 0 || src_height == 0)
2567 return;
2568
2569 blorp_params_init(&params);
2570 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2571 src_layer, ISL_FORMAT_UNSUPPORTED, false);
2572 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2573 dst_layer, ISL_FORMAT_UNSUPPORTED, true);
2574
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,
2580 };
2581
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);
2586
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);
2593
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;
2600 } else {
2601 params.src.view.format =
2602 get_copy_format_for_bpb(isl_dev, src_fmtl->bpb);
2603 }
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;
2608 } else {
2609 params.dst.view.format =
2610 get_copy_format_for_bpb(isl_dev, dst_fmtl->bpb);
2611 }
2612 } else {
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);
2615 }
2616
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.
2626 */
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,
2630 linear_src_format,
2631 params.src.view.format));
2632 uint32_t packed[4];
2633 isl_color_value_pack(&params.src.clear_color,
2634 linear_src_format, packed);
2635 isl_color_value_unpack(&params.src.clear_color,
2636 params.src.view.format, packed);
2637 }
2638
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,
2644 linear_dst_format,
2645 params.dst.view.format));
2646 uint32_t packed[4];
2647 isl_color_value_pack(&params.dst.clear_color,
2648 linear_dst_format, packed);
2649 isl_color_value_unpack(&params.dst.clear_color,
2650 params.dst.view.format, packed);
2651 }
2652
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;
2656
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
2660 * the time anyway.
2661 */
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);
2666
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;
2671 }
2672 }
2673
2674 if (src_fmtl->bw > 1 || src_fmtl->bh > 1) {
2675 blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.src,
2676 &src_x, &src_y,
2677 &src_width, &src_height);
2678 wm_prog_key.need_src_offset = true;
2679 }
2680
2681 if (dst_fmtl->bw > 1 || dst_fmtl->bh > 1) {
2682 blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
2683 &dst_x, &dst_y, NULL, NULL);
2684 wm_prog_key.need_dst_offset = true;
2685 }
2686
2687 /* Once both surfaces are stompped to uncompressed as needed, the
2688 * destination size is the same as the source size.
2689 */
2690 uint32_t dst_width = src_width;
2691 uint32_t dst_height = src_height;
2692
2693 struct blt_coords coords = {
2694 .x = {
2695 .src0 = src_x,
2696 .src1 = src_x + src_width,
2697 .dst0 = dst_x,
2698 .dst1 = dst_x + dst_width,
2699 .mirror = false
2700 },
2701 .y = {
2702 .src0 = src_y,
2703 .src1 = src_y + src_height,
2704 .dst0 = dst_y,
2705 .dst1 = dst_y + dst_height,
2706 .mirror = false
2707 }
2708 };
2709
2710 do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2711 }
2712
2713 static enum isl_format
2714 isl_format_for_size(unsigned size_B)
2715 {
2716 switch (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;
2722 default:
2723 unreachable("Not a power-of-two format size");
2724 }
2725 }
2726
2727 /**
2728 * Returns the greatest common divisor of a and b that is a power of two.
2729 */
2730 static uint64_t
2731 gcd_pow2_u64(uint64_t a, uint64_t b)
2732 {
2733 assert(a > 0 || b > 0);
2734
2735 unsigned a_log2 = ffsll(a) - 1;
2736 unsigned b_log2 = ffsll(b) - 1;
2737
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.
2741 */
2742 return 1 << MIN2(a_log2, b_log2);
2743 }
2744
2745 static void
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)
2750 {
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.
2753 */
2754 enum isl_format format = isl_format_for_size(block_size);
2755
2756 UNUSED bool ok;
2757 struct isl_surf surf;
2758 ok = isl_surf_init(batch->blorp->isl_dev, &surf,
2759 .dim = ISL_SURF_DIM_2D,
2760 .format = format,
2761 .width = width,
2762 .height = height,
2763 .depth = 1,
2764 .levels = 1,
2765 .array_len = 1,
2766 .samples = 1,
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);
2771 assert(ok);
2772
2773 struct blorp_surf src_blorp_surf = {
2774 .surf = &surf,
2775 .addr = *src,
2776 };
2777
2778 struct blorp_surf dst_blorp_surf = {
2779 .surf = &surf,
2780 .addr = *dst,
2781 };
2782
2783 blorp_copy(batch, &src_blorp_surf, 0, 0, &dst_blorp_surf, 0, 0,
2784 0, 0, 0, 0, width, height);
2785 }
2786
2787 void
2788 blorp_buffer_copy(struct blorp_batch *batch,
2789 struct blorp_address src,
2790 struct blorp_address dst,
2791 uint64_t size)
2792 {
2793 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
2794 uint64_t copy_size = size;
2795
2796 /* This is maximum possible width/height our HW can handle */
2797 uint64_t max_surface_dim = 1 << (devinfo->gen >= 7 ? 14 : 13);
2798
2799 /* First, we compute the biggest format that can be used with the
2800 * given offsets and size.
2801 */
2802 int bs = 16;
2803 bs = gcd_pow2_u64(bs, src.offset);
2804 bs = gcd_pow2_u64(bs, dst.offset);
2805 bs = gcd_pow2_u64(bs, size);
2806
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;
2814 }
2815
2816 /* Now make a max-width copy */
2817 uint64_t height = copy_size / (max_surface_dim * bs);
2818 assert(height < max_surface_dim);
2819 if (height != 0) {
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;
2825 }
2826
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);
2830 }
2831 }