intel/blorp: Assert that we don't re-layout a compressed surface
[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 v->frag_coord->data.origin_upper_left = true;
91
92 v->color_out = nir_variable_create(b->shader, nir_var_shader_out,
93 glsl_vec4_type(), "gl_FragColor");
94 v->color_out->data.location = FRAG_RESULT_COLOR;
95 }
96
97 static nir_ssa_def *
98 blorp_blit_get_frag_coords(nir_builder *b,
99 const struct brw_blorp_blit_prog_key *key,
100 struct brw_blorp_blit_vars *v)
101 {
102 nir_ssa_def *coord = nir_f2i32(b, nir_load_var(b, v->frag_coord));
103
104 /* Account for destination surface intratile offset
105 *
106 * Transformation parameters giving translation from destination to source
107 * coordinates don't take into account possible intra-tile destination
108 * offset. Therefore it has to be first subtracted from the incoming
109 * coordinates. Vertices are set up based on coordinates containing the
110 * intra-tile offset.
111 */
112 if (key->need_dst_offset)
113 coord = nir_isub(b, coord, nir_load_var(b, v->v_dst_offset));
114
115 if (key->persample_msaa_dispatch) {
116 return nir_vec3(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1),
117 nir_load_sample_id(b));
118 } else {
119 return nir_vec2(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1));
120 }
121 }
122
123 /**
124 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
125 * coordinates.
126 */
127 static nir_ssa_def *
128 blorp_blit_apply_transform(nir_builder *b, nir_ssa_def *src_pos,
129 struct brw_blorp_blit_vars *v)
130 {
131 nir_ssa_def *coord_transform = nir_load_var(b, v->v_coord_transform);
132
133 nir_ssa_def *offset = nir_vec2(b, nir_channel(b, coord_transform, 1),
134 nir_channel(b, coord_transform, 3));
135 nir_ssa_def *mul = nir_vec2(b, nir_channel(b, coord_transform, 0),
136 nir_channel(b, coord_transform, 2));
137
138 return nir_fadd(b, nir_fmul(b, src_pos, mul), offset);
139 }
140
141 static inline void
142 blorp_nir_discard_if_outside_rect(nir_builder *b, nir_ssa_def *pos,
143 struct brw_blorp_blit_vars *v)
144 {
145 nir_ssa_def *c0, *c1, *c2, *c3;
146 nir_ssa_def *discard_rect = nir_load_var(b, v->v_discard_rect);
147 nir_ssa_def *dst_x0 = nir_channel(b, discard_rect, 0);
148 nir_ssa_def *dst_x1 = nir_channel(b, discard_rect, 1);
149 nir_ssa_def *dst_y0 = nir_channel(b, discard_rect, 2);
150 nir_ssa_def *dst_y1 = nir_channel(b, discard_rect, 3);
151
152 c0 = nir_ult(b, nir_channel(b, pos, 0), dst_x0);
153 c1 = nir_uge(b, nir_channel(b, pos, 0), dst_x1);
154 c2 = nir_ult(b, nir_channel(b, pos, 1), dst_y0);
155 c3 = nir_uge(b, nir_channel(b, pos, 1), dst_y1);
156
157 nir_ssa_def *oob = nir_ior(b, nir_ior(b, c0, c1), nir_ior(b, c2, c3));
158
159 nir_intrinsic_instr *discard =
160 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
161 discard->src[0] = nir_src_for_ssa(oob);
162 nir_builder_instr_insert(b, &discard->instr);
163 }
164
165 static nir_tex_instr *
166 blorp_create_nir_tex_instr(nir_builder *b, struct brw_blorp_blit_vars *v,
167 nir_texop op, nir_ssa_def *pos, unsigned num_srcs,
168 nir_alu_type dst_type)
169 {
170 nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
171
172 tex->op = op;
173
174 tex->dest_type = dst_type;
175 tex->is_array = false;
176 tex->is_shadow = false;
177
178 /* Blorp only has one texture and it's bound at unit 0 */
179 tex->texture_index = 0;
180 tex->sampler_index = 0;
181
182 /* To properly handle 3-D and 2-D array textures, we pull the Z component
183 * from an input. TODO: This is a bit magic; we should probably make this
184 * more explicit in the future.
185 */
186 assert(pos->num_components >= 2);
187 pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
188 nir_load_var(b, v->v_src_z));
189
190 tex->src[0].src_type = nir_tex_src_coord;
191 tex->src[0].src = nir_src_for_ssa(pos);
192 tex->coord_components = 3;
193
194 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
195
196 return tex;
197 }
198
199 static nir_ssa_def *
200 blorp_nir_tex(nir_builder *b, struct brw_blorp_blit_vars *v,
201 const struct brw_blorp_blit_prog_key *key, nir_ssa_def *pos)
202 {
203 if (key->need_src_offset)
204 pos = nir_fadd(b, pos, nir_i2f32(b, nir_load_var(b, v->v_src_offset)));
205
206 /* If the sampler requires normalized coordinates, we need to compensate. */
207 if (key->src_coords_normalized)
208 pos = nir_fmul(b, pos, nir_load_var(b, v->v_src_inv_size));
209
210 nir_tex_instr *tex =
211 blorp_create_nir_tex_instr(b, v, nir_texop_tex, pos, 2,
212 key->texture_data_type);
213
214 assert(pos->num_components == 2);
215 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
216 tex->src[1].src_type = nir_tex_src_lod;
217 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
218
219 nir_builder_instr_insert(b, &tex->instr);
220
221 return &tex->dest.ssa;
222 }
223
224 static nir_ssa_def *
225 blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
226 nir_ssa_def *pos, nir_alu_type dst_type)
227 {
228 nir_tex_instr *tex =
229 blorp_create_nir_tex_instr(b, v, nir_texop_txf, pos, 2, dst_type);
230
231 tex->sampler_dim = GLSL_SAMPLER_DIM_3D;
232 tex->src[1].src_type = nir_tex_src_lod;
233 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
234
235 nir_builder_instr_insert(b, &tex->instr);
236
237 return &tex->dest.ssa;
238 }
239
240 static nir_ssa_def *
241 blorp_nir_txf_ms(nir_builder *b, struct brw_blorp_blit_vars *v,
242 nir_ssa_def *pos, nir_ssa_def *mcs, nir_alu_type dst_type)
243 {
244 nir_tex_instr *tex =
245 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms, pos,
246 mcs != NULL ? 3 : 2, dst_type);
247
248 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
249
250 tex->src[1].src_type = nir_tex_src_ms_index;
251 if (pos->num_components == 2) {
252 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
253 } else {
254 assert(pos->num_components == 3);
255 tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
256 }
257
258 if (mcs) {
259 tex->src[2].src_type = nir_tex_src_ms_mcs;
260 tex->src[2].src = nir_src_for_ssa(mcs);
261 }
262
263 nir_builder_instr_insert(b, &tex->instr);
264
265 return &tex->dest.ssa;
266 }
267
268 static nir_ssa_def *
269 blorp_blit_txf_ms_mcs(nir_builder *b, struct brw_blorp_blit_vars *v,
270 nir_ssa_def *pos)
271 {
272 nir_tex_instr *tex =
273 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms_mcs,
274 pos, 1, nir_type_int);
275
276 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
277
278 nir_builder_instr_insert(b, &tex->instr);
279
280 return &tex->dest.ssa;
281 }
282
283 /**
284 * Emit code to compensate for the difference between Y and W tiling.
285 *
286 * This code modifies the X and Y coordinates according to the formula:
287 *
288 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
289 *
290 * (See brw_blorp_build_nir_shader).
291 */
292 static inline nir_ssa_def *
293 blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
294 {
295 assert(pos->num_components == 2);
296 nir_ssa_def *x_Y = nir_channel(b, pos, 0);
297 nir_ssa_def *y_Y = nir_channel(b, pos, 1);
298
299 /* Given X and Y coordinates that describe an address using Y tiling,
300 * translate to the X and Y coordinates that describe the same address
301 * using W tiling.
302 *
303 * If we break down the low order bits of X and Y, using a
304 * single letter to represent each low-order bit:
305 *
306 * X = A << 7 | 0bBCDEFGH
307 * Y = J << 5 | 0bKLMNP (1)
308 *
309 * Then we can apply the Y tiling formula to see the memory offset being
310 * addressed:
311 *
312 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
313 *
314 * If we apply the W detiling formula to this memory location, that the
315 * corresponding X' and Y' coordinates are:
316 *
317 * X' = A << 6 | 0bBCDPFH (3)
318 * Y' = J << 6 | 0bKLMNEG
319 *
320 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
321 * we need to make the following computation:
322 *
323 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
324 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
325 */
326 nir_ssa_def *x_W = nir_imm_int(b, 0);
327 x_W = nir_mask_shift_or(b, x_W, x_Y, 0xfffffff4, -1);
328 x_W = nir_mask_shift_or(b, x_W, y_Y, 0x1, 2);
329 x_W = nir_mask_shift_or(b, x_W, x_Y, 0x1, 0);
330
331 nir_ssa_def *y_W = nir_imm_int(b, 0);
332 y_W = nir_mask_shift_or(b, y_W, y_Y, 0xfffffffe, 1);
333 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x8, -2);
334 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x2, -1);
335
336 return nir_vec2(b, x_W, y_W);
337 }
338
339 /**
340 * Emit code to compensate for the difference between Y and W tiling.
341 *
342 * This code modifies the X and Y coordinates according to the formula:
343 *
344 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
345 *
346 * (See brw_blorp_build_nir_shader).
347 */
348 static inline nir_ssa_def *
349 blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
350 {
351 assert(pos->num_components == 2);
352 nir_ssa_def *x_W = nir_channel(b, pos, 0);
353 nir_ssa_def *y_W = nir_channel(b, pos, 1);
354
355 /* Applying the same logic as above, but in reverse, we obtain the
356 * formulas:
357 *
358 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
359 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
360 */
361 nir_ssa_def *x_Y = nir_imm_int(b, 0);
362 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0xfffffffa, 1);
363 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x2, 2);
364 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x1, 1);
365 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0x1, 0);
366
367 nir_ssa_def *y_Y = nir_imm_int(b, 0);
368 y_Y = nir_mask_shift_or(b, y_Y, y_W, 0xfffffffc, -1);
369 y_Y = nir_mask_shift_or(b, y_Y, x_W, 0x4, -2);
370
371 return nir_vec2(b, x_Y, y_Y);
372 }
373
374 /**
375 * Emit code to compensate for the difference between MSAA and non-MSAA
376 * surfaces.
377 *
378 * This code modifies the X and Y coordinates according to the formula:
379 *
380 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
381 *
382 * (See brw_blorp_blit_program).
383 */
384 static inline nir_ssa_def *
385 blorp_nir_encode_msaa(nir_builder *b, nir_ssa_def *pos,
386 unsigned num_samples, enum isl_msaa_layout layout)
387 {
388 assert(pos->num_components == 2 || pos->num_components == 3);
389
390 switch (layout) {
391 case ISL_MSAA_LAYOUT_NONE:
392 assert(pos->num_components == 2);
393 return pos;
394 case ISL_MSAA_LAYOUT_ARRAY:
395 /* No translation needed */
396 return pos;
397 case ISL_MSAA_LAYOUT_INTERLEAVED: {
398 nir_ssa_def *x_in = nir_channel(b, pos, 0);
399 nir_ssa_def *y_in = nir_channel(b, pos, 1);
400 nir_ssa_def *s_in = pos->num_components == 2 ? nir_imm_int(b, 0) :
401 nir_channel(b, pos, 2);
402
403 nir_ssa_def *x_out = nir_imm_int(b, 0);
404 nir_ssa_def *y_out = nir_imm_int(b, 0);
405 switch (num_samples) {
406 case 2:
407 case 4:
408 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
409 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
410 * Y' = Y
411 *
412 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
413 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
414 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
415 */
416 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 1);
417 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
418 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
419 if (num_samples == 2) {
420 y_out = y_in;
421 } else {
422 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
423 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
424 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
425 }
426 break;
427
428 case 8:
429 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
430 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
431 * | (X & 0b1)
432 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
433 */
434 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
435 x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
436 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
437 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
438 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
439 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
440 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
441 break;
442
443 case 16:
444 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
445 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
446 * | (X & 0b1)
447 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
448 * | (Y & 0b1)
449 */
450 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
451 x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
452 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
453 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
454 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 2);
455 y_out = nir_mask_shift_or(b, y_out, s_in, 0x8, -1);
456 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
457 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
458 break;
459
460 default:
461 unreachable("Invalid number of samples for IMS layout");
462 }
463
464 return nir_vec2(b, x_out, y_out);
465 }
466
467 default:
468 unreachable("Invalid MSAA layout");
469 }
470 }
471
472 /**
473 * Emit code to compensate for the difference between MSAA and non-MSAA
474 * surfaces.
475 *
476 * This code modifies the X and Y coordinates according to the formula:
477 *
478 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
479 *
480 * (See brw_blorp_blit_program).
481 */
482 static inline nir_ssa_def *
483 blorp_nir_decode_msaa(nir_builder *b, nir_ssa_def *pos,
484 unsigned num_samples, enum isl_msaa_layout layout)
485 {
486 assert(pos->num_components == 2 || pos->num_components == 3);
487
488 switch (layout) {
489 case ISL_MSAA_LAYOUT_NONE:
490 /* No translation necessary, and S should already be zero. */
491 assert(pos->num_components == 2);
492 return pos;
493 case ISL_MSAA_LAYOUT_ARRAY:
494 /* No translation necessary. */
495 return pos;
496 case ISL_MSAA_LAYOUT_INTERLEAVED: {
497 assert(pos->num_components == 2);
498
499 nir_ssa_def *x_in = nir_channel(b, pos, 0);
500 nir_ssa_def *y_in = nir_channel(b, pos, 1);
501
502 nir_ssa_def *x_out = nir_imm_int(b, 0);
503 nir_ssa_def *y_out = nir_imm_int(b, 0);
504 nir_ssa_def *s_out = nir_imm_int(b, 0);
505 switch (num_samples) {
506 case 2:
507 case 4:
508 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
509 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
510 * S = (X & 0b10) >> 1
511 *
512 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
513 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
514 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
515 * S = (Y & 0b10) | (X & 0b10) >> 1
516 */
517 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffc, -1);
518 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
519 if (num_samples == 2) {
520 y_out = y_in;
521 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
522 } else {
523 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
524 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
525 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
526 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
527 }
528 break;
529
530 case 8:
531 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
532 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
533 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
534 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
535 */
536 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
537 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
538 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
539 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
540 s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
541 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
542 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
543 break;
544
545 case 16:
546 /* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
547 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
548 * Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
549 * S = (Y & 0b100) << 1 | (X & 0b100) |
550 * (Y & 0b10) | (X & 0b10) >> 1
551 */
552 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
553 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
554 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffff8, -2);
555 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
556 s_out = nir_mask_shift_or(b, s_out, y_in, 0x4, 1);
557 s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
558 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
559 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
560 break;
561
562 default:
563 unreachable("Invalid number of samples for IMS layout");
564 }
565
566 return nir_vec3(b, x_out, y_out, s_out);
567 }
568
569 default:
570 unreachable("Invalid MSAA layout");
571 }
572 }
573
574 /**
575 * Count the number of trailing 1 bits in the given value. For example:
576 *
577 * count_trailing_one_bits(0) == 0
578 * count_trailing_one_bits(7) == 3
579 * count_trailing_one_bits(11) == 2
580 */
581 static inline int count_trailing_one_bits(unsigned value)
582 {
583 #ifdef HAVE___BUILTIN_CTZ
584 return __builtin_ctz(~value);
585 #else
586 return util_bitcount(value & ~(value + 1));
587 #endif
588 }
589
590 static nir_ssa_def *
591 blorp_nir_manual_blend_average(nir_builder *b, struct brw_blorp_blit_vars *v,
592 nir_ssa_def *pos, unsigned tex_samples,
593 enum isl_aux_usage tex_aux_usage,
594 nir_alu_type dst_type)
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 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
607 *
608 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
609 *
610 * This ensures that when all samples have the same value, no numerical
611 * precision is lost, since each addition operation always adds two equal
612 * values, and summing two equal floating point values does not lose
613 * precision.
614 *
615 * We perform this computation by treating the texture_data array as a
616 * stack and performing the following operations:
617 *
618 * - push sample 0 onto stack
619 * - push sample 1 onto stack
620 * - add top two stack entries
621 * - push sample 2 onto stack
622 * - push sample 3 onto stack
623 * - add top two stack entries
624 * - add top two stack entries
625 * - divide top stack entry by 4
626 *
627 * Note that after pushing sample i onto the stack, the number of add
628 * operations we do is equal to the number of trailing 1 bits in i. This
629 * works provided the total number of samples is a power of two, which it
630 * always is for i965.
631 *
632 * For integer formats, we replace the add operations with average
633 * operations and skip the final division.
634 */
635 nir_ssa_def *texture_data[5];
636 unsigned stack_depth = 0;
637 for (unsigned i = 0; i < tex_samples; ++i) {
638 assert(stack_depth == util_bitcount(i)); /* Loop invariant */
639
640 /* Push sample i onto the stack */
641 assert(stack_depth < ARRAY_SIZE(texture_data));
642
643 nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
644 nir_channel(b, pos, 1),
645 nir_imm_int(b, i));
646 texture_data[stack_depth++] = blorp_nir_txf_ms(b, v, ms_pos, mcs, dst_type);
647
648 if (i == 0 && tex_aux_usage == ISL_AUX_USAGE_MCS) {
649 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
650 * suggests an optimization:
651 *
652 * "A simple optimization with probable large return in
653 * performance is to compare the MCS value to zero (indicating
654 * all samples are on sample slice 0), and sample only from
655 * sample slice 0 using ld2dss if MCS is zero."
656 *
657 * Note that in the case where the MCS value is zero, sampling from
658 * sample slice 0 using ld2dss and sampling from sample 0 using
659 * ld2dms are equivalent (since all samples are on sample slice 0).
660 * Since we have already sampled from sample 0, all we need to do is
661 * skip the remaining fetches and averaging if MCS is zero.
662 *
663 * It's also trivial to detect when the MCS has the magic clear color
664 * value. In this case, the txf we did on sample 0 will return the
665 * clear color and we can skip the remaining fetches just like we do
666 * when MCS == 0.
667 */
668 nir_ssa_def *mcs_zero =
669 nir_ieq(b, nir_channel(b, mcs, 0), nir_imm_int(b, 0));
670 if (tex_samples == 16) {
671 mcs_zero = nir_iand(b, mcs_zero,
672 nir_ieq(b, nir_channel(b, mcs, 1), nir_imm_int(b, 0)));
673 }
674 nir_ssa_def *mcs_clear =
675 blorp_nir_mcs_is_clear_color(b, mcs, tex_samples);
676
677 nir_if *if_stmt = nir_if_create(b->shader);
678 if_stmt->condition = nir_src_for_ssa(nir_ior(b, mcs_zero, mcs_clear));
679 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
680
681 b->cursor = nir_after_cf_list(&if_stmt->then_list);
682 nir_store_var(b, color, texture_data[0], 0xf);
683
684 b->cursor = nir_after_cf_list(&if_stmt->else_list);
685 outer_if = if_stmt;
686 }
687
688 for (int j = 0; j < count_trailing_one_bits(i); j++) {
689 assert(stack_depth >= 2);
690 --stack_depth;
691
692 assert(dst_type == nir_type_float);
693 texture_data[stack_depth - 1] =
694 nir_fadd(b, texture_data[stack_depth - 1],
695 texture_data[stack_depth]);
696 }
697 }
698
699 /* We should have just 1 sample on the stack now. */
700 assert(stack_depth == 1);
701
702 texture_data[0] = nir_fmul(b, texture_data[0],
703 nir_imm_float(b, 1.0 / tex_samples));
704
705 nir_store_var(b, color, texture_data[0], 0xf);
706
707 if (outer_if)
708 b->cursor = nir_after_cf_node(&outer_if->cf_node);
709
710 return nir_load_var(b, color);
711 }
712
713 static inline nir_ssa_def *
714 nir_imm_vec2(nir_builder *build, float x, float y)
715 {
716 nir_const_value v;
717
718 memset(&v, 0, sizeof(v));
719 v.f32[0] = x;
720 v.f32[1] = y;
721
722 return nir_build_imm(build, 4, 32, v);
723 }
724
725 static nir_ssa_def *
726 blorp_nir_manual_blend_bilinear(nir_builder *b, nir_ssa_def *pos,
727 unsigned tex_samples,
728 const struct brw_blorp_blit_prog_key *key,
729 struct brw_blorp_blit_vars *v)
730 {
731 nir_ssa_def *pos_xy = nir_channels(b, pos, 0x3);
732 nir_ssa_def *rect_grid = nir_load_var(b, v->v_rect_grid);
733 nir_ssa_def *scale = nir_imm_vec2(b, key->x_scale, key->y_scale);
734
735 /* Translate coordinates to lay out the samples in a rectangular grid
736 * roughly corresponding to sample locations.
737 */
738 pos_xy = nir_fmul(b, pos_xy, scale);
739 /* Adjust coordinates so that integers represent pixel centers rather
740 * than pixel edges.
741 */
742 pos_xy = nir_fadd(b, pos_xy, nir_imm_float(b, -0.5));
743 /* Clamp the X, Y texture coordinates to properly handle the sampling of
744 * texels on texture edges.
745 */
746 pos_xy = nir_fmin(b, nir_fmax(b, pos_xy, nir_imm_float(b, 0.0)),
747 nir_vec2(b, nir_channel(b, rect_grid, 0),
748 nir_channel(b, rect_grid, 1)));
749
750 /* Store the fractional parts to be used as bilinear interpolation
751 * coefficients.
752 */
753 nir_ssa_def *frac_xy = nir_ffract(b, pos_xy);
754 /* Round the float coordinates down to nearest integer */
755 pos_xy = nir_fdiv(b, nir_ftrunc(b, pos_xy), scale);
756
757 nir_ssa_def *tex_data[4];
758 for (unsigned i = 0; i < 4; ++i) {
759 float sample_off_x = (float)(i & 0x1) / key->x_scale;
760 float sample_off_y = (float)((i >> 1) & 0x1) / key->y_scale;
761 nir_ssa_def *sample_off = nir_imm_vec2(b, sample_off_x, sample_off_y);
762
763 nir_ssa_def *sample_coords = nir_fadd(b, pos_xy, sample_off);
764 nir_ssa_def *sample_coords_int = nir_f2i32(b, sample_coords);
765
766 /* The MCS value we fetch has to match up with the pixel that we're
767 * sampling from. Since we sample from different pixels in each
768 * iteration of this "for" loop, the call to mcs_fetch() should be
769 * here inside the loop after computing the pixel coordinates.
770 */
771 nir_ssa_def *mcs = NULL;
772 if (key->tex_aux_usage == ISL_AUX_USAGE_MCS)
773 mcs = blorp_blit_txf_ms_mcs(b, v, sample_coords_int);
774
775 /* Compute sample index and map the sample index to a sample number.
776 * Sample index layout shows the numbering of slots in a rectangular
777 * grid of samples with in a pixel. Sample number layout shows the
778 * rectangular grid of samples roughly corresponding to the real sample
779 * locations with in a pixel.
780 *
781 * In the case of 2x MSAA, the layout of sample indices is reversed from
782 * the layout of sample numbers:
783 *
784 * sample index layout : --------- sample number layout : ---------
785 * | 0 | 1 | | 1 | 0 |
786 * --------- ---------
787 *
788 * In case of 4x MSAA, layout of sample indices matches the layout of
789 * sample numbers:
790 * ---------
791 * | 0 | 1 |
792 * ---------
793 * | 2 | 3 |
794 * ---------
795 *
796 * In case of 8x MSAA the two layouts don't match.
797 * sample index layout : --------- sample number layout : ---------
798 * | 0 | 1 | | 3 | 7 |
799 * --------- ---------
800 * | 2 | 3 | | 5 | 0 |
801 * --------- ---------
802 * | 4 | 5 | | 1 | 2 |
803 * --------- ---------
804 * | 6 | 7 | | 4 | 6 |
805 * --------- ---------
806 *
807 * Fortunately, this can be done fairly easily as:
808 * S' = (0x17306425 >> (S * 4)) & 0xf
809 *
810 * In the case of 16x MSAA the two layouts don't match.
811 * Sample index layout: Sample number layout:
812 * --------------------- ---------------------
813 * | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
814 * --------------------- ---------------------
815 * | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
816 * --------------------- ---------------------
817 * | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
818 * --------------------- ---------------------
819 * | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
820 * --------------------- ---------------------
821 *
822 * This is equivalent to
823 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
824 */
825 nir_ssa_def *frac = nir_ffract(b, sample_coords);
826 nir_ssa_def *sample =
827 nir_fdot2(b, frac, nir_imm_vec2(b, key->x_scale,
828 key->x_scale * key->y_scale));
829 sample = nir_f2i32(b, sample);
830
831 if (tex_samples == 2) {
832 sample = nir_isub(b, nir_imm_int(b, 1), sample);
833 } else if (tex_samples == 8) {
834 sample = nir_iand(b, nir_ishr(b, nir_imm_int(b, 0x64210573),
835 nir_ishl(b, sample, nir_imm_int(b, 2))),
836 nir_imm_int(b, 0xf));
837 } else if (tex_samples == 16) {
838 nir_ssa_def *sample_low =
839 nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xd31479af),
840 nir_ishl(b, sample, nir_imm_int(b, 2))),
841 nir_imm_int(b, 0xf));
842 nir_ssa_def *sample_high =
843 nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xe58b602c),
844 nir_ishl(b, nir_iadd(b, sample,
845 nir_imm_int(b, -8)),
846 nir_imm_int(b, 2))),
847 nir_imm_int(b, 0xf));
848
849 sample = nir_bcsel(b, nir_ilt(b, sample, nir_imm_int(b, 8)),
850 sample_low, sample_high);
851 }
852 nir_ssa_def *pos_ms = nir_vec3(b, nir_channel(b, sample_coords_int, 0),
853 nir_channel(b, sample_coords_int, 1),
854 sample);
855 tex_data[i] = blorp_nir_txf_ms(b, v, pos_ms, mcs, key->texture_data_type);
856 }
857
858 nir_ssa_def *frac_x = nir_channel(b, frac_xy, 0);
859 nir_ssa_def *frac_y = nir_channel(b, frac_xy, 1);
860 return nir_flrp(b, nir_flrp(b, tex_data[0], tex_data[1], frac_x),
861 nir_flrp(b, tex_data[2], tex_data[3], frac_x),
862 frac_y);
863 }
864
865 /** Perform a color bit-cast operation
866 *
867 * For copy operations involving CCS, we may need to use different formats for
868 * the source and destination surfaces. The two formats must both be UINT
869 * formats and must have the same size but may have different bit layouts.
870 * For instance, we may be copying from R8G8B8A8_UINT to R32_UINT or R32_UINT
871 * to R16G16_UINT. This function generates code to shuffle bits around to get
872 * us from one to the other.
873 */
874 static nir_ssa_def *
875 bit_cast_color(struct nir_builder *b, nir_ssa_def *color,
876 const struct brw_blorp_blit_prog_key *key)
877 {
878 assert(key->texture_data_type == nir_type_uint);
879
880 if (key->src_format == key->dst_format)
881 return color;
882
883 const struct isl_format_layout *src_fmtl =
884 isl_format_get_layout(key->src_format);
885 const struct isl_format_layout *dst_fmtl =
886 isl_format_get_layout(key->dst_format);
887
888 /* They must be uint formats with the same bit size */
889 assert(src_fmtl->bpb == dst_fmtl->bpb);
890 assert(src_fmtl->channels.r.type == ISL_UINT);
891 assert(dst_fmtl->channels.r.type == ISL_UINT);
892
893 /* They must be in regular color formats (no luminance or alpha) */
894 assert(src_fmtl->channels.r.bits > 0);
895 assert(dst_fmtl->channels.r.bits > 0);
896
897 /* They must be in RGBA order (possibly with channels missing) */
898 assert(src_fmtl->channels.r.start_bit == 0);
899 assert(dst_fmtl->channels.r.start_bit == 0);
900
901 if (src_fmtl->bpb <= 32) {
902 const unsigned src_channels =
903 isl_format_get_num_channels(key->src_format);
904 const unsigned src_bits[4] = {
905 src_fmtl->channels.r.bits,
906 src_fmtl->channels.g.bits,
907 src_fmtl->channels.b.bits,
908 src_fmtl->channels.a.bits,
909 };
910 const unsigned dst_channels =
911 isl_format_get_num_channels(key->dst_format);
912 const unsigned dst_bits[4] = {
913 dst_fmtl->channels.r.bits,
914 dst_fmtl->channels.g.bits,
915 dst_fmtl->channels.b.bits,
916 dst_fmtl->channels.a.bits,
917 };
918 nir_ssa_def *packed =
919 nir_format_pack_uint_unmasked(b, color, src_bits, src_channels);
920 color = nir_format_unpack_uint(b, packed, dst_bits, dst_channels);
921 } else {
922 const unsigned src_bpc = src_fmtl->channels.r.bits;
923 const unsigned dst_bpc = dst_fmtl->channels.r.bits;
924
925 assert(src_fmtl->channels.g.bits == 0 ||
926 src_fmtl->channels.g.bits == src_fmtl->channels.r.bits);
927 assert(src_fmtl->channels.b.bits == 0 ||
928 src_fmtl->channels.b.bits == src_fmtl->channels.r.bits);
929 assert(src_fmtl->channels.a.bits == 0 ||
930 src_fmtl->channels.a.bits == src_fmtl->channels.r.bits);
931 assert(dst_fmtl->channels.g.bits == 0 ||
932 dst_fmtl->channels.g.bits == dst_fmtl->channels.r.bits);
933 assert(dst_fmtl->channels.b.bits == 0 ||
934 dst_fmtl->channels.b.bits == dst_fmtl->channels.r.bits);
935 assert(dst_fmtl->channels.a.bits == 0 ||
936 dst_fmtl->channels.a.bits == dst_fmtl->channels.r.bits);
937
938 /* Restrict to only the channels we actually have */
939 const unsigned src_channels =
940 isl_format_get_num_channels(key->src_format);
941 color = nir_channels(b, color, (1 << src_channels) - 1);
942
943 color = nir_format_bitcast_uvec_unmasked(b, color, src_bpc, dst_bpc);
944 }
945
946 /* Blorp likes to assume that colors are vec4s */
947 nir_ssa_def *u = nir_ssa_undef(b, 1, 32);
948 nir_ssa_def *chans[4] = { u, u, u, u };
949 for (unsigned i = 0; i < color->num_components; i++)
950 chans[i] = nir_channel(b, color, i);
951 return nir_vec4(b, chans[0], chans[1], chans[2], chans[3]);
952 }
953
954 static nir_ssa_def *
955 select_color_channel(struct nir_builder *b, nir_ssa_def *color,
956 nir_alu_type data_type,
957 enum isl_channel_select chan)
958 {
959 if (chan == ISL_CHANNEL_SELECT_ZERO) {
960 return nir_imm_int(b, 0);
961 } else if (chan == ISL_CHANNEL_SELECT_ONE) {
962 switch (data_type) {
963 case nir_type_int:
964 case nir_type_uint:
965 return nir_imm_int(b, 1);
966 case nir_type_float:
967 return nir_imm_float(b, 1);
968 default:
969 unreachable("Invalid data type");
970 }
971 } else {
972 assert((unsigned)(chan - ISL_CHANNEL_SELECT_RED) < 4);
973 return nir_channel(b, color, chan - ISL_CHANNEL_SELECT_RED);
974 }
975 }
976
977 static nir_ssa_def *
978 swizzle_color(struct nir_builder *b, nir_ssa_def *color,
979 struct isl_swizzle swizzle, nir_alu_type data_type)
980 {
981 return nir_vec4(b,
982 select_color_channel(b, color, data_type, swizzle.r),
983 select_color_channel(b, color, data_type, swizzle.g),
984 select_color_channel(b, color, data_type, swizzle.b),
985 select_color_channel(b, color, data_type, swizzle.a));
986 }
987
988 static nir_ssa_def *
989 convert_color(struct nir_builder *b, nir_ssa_def *color,
990 const struct brw_blorp_blit_prog_key *key)
991 {
992 /* All of our color conversions end up generating a single-channel color
993 * value that we need to write out.
994 */
995 nir_ssa_def *value;
996
997 if (key->dst_format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {
998 /* The destination image is bound as R32_UINT but the data needs to be
999 * in R24_UNORM_X8_TYPELESS. The bottom 24 are the actual data and the
1000 * top 8 need to be zero. We can accomplish this by simply multiplying
1001 * by a factor to scale things down.
1002 */
1003 unsigned factor = (1 << 24) - 1;
1004 value = nir_fsat(b, nir_channel(b, color, 0));
1005 value = nir_f2i32(b, nir_fmul(b, value, nir_imm_float(b, factor)));
1006 } else if (key->dst_format == ISL_FORMAT_L8_UNORM_SRGB) {
1007 value = nir_format_linear_to_srgb(b, nir_channel(b, color, 0));
1008 } else if (key->dst_format == ISL_FORMAT_R8G8B8_UNORM_SRGB) {
1009 value = nir_format_linear_to_srgb(b, color);
1010 } else if (key->dst_format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
1011 value = nir_format_pack_r9g9b9e5(b, color);
1012 } else {
1013 unreachable("Unsupported format conversion");
1014 }
1015
1016 nir_ssa_def *out_comps[4];
1017 for (unsigned i = 0; i < 4; i++) {
1018 if (i < value->num_components)
1019 out_comps[i] = nir_channel(b, value, i);
1020 else
1021 out_comps[i] = nir_ssa_undef(b, 1, 32);
1022 }
1023 return nir_vec(b, out_comps, 4);
1024 }
1025
1026 /**
1027 * Generator for WM programs used in BLORP blits.
1028 *
1029 * The bulk of the work done by the WM program is to wrap and unwrap the
1030 * coordinate transformations used by the hardware to store surfaces in
1031 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
1032 * sample index for a multisampled surface) to a memory offset by the
1033 * following formulas:
1034 *
1035 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
1036 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
1037 *
1038 * For a single-sampled surface, or for a multisampled surface using
1039 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
1040 * function:
1041 *
1042 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1043 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1044 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1045 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1046 *
1047 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1048 * embeds the sample number into bit 1 of the X and Y coordinates:
1049 *
1050 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1051 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1052 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
1053 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1054 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1055 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1056 * S = (Y & 0b10) | (X & 0b10) >> 1
1057 *
1058 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1059 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
1060 * the Y coordinate:
1061 *
1062 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1063 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
1064 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1065 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1066 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1067 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1068 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1069 *
1070 * For X tiling, tile() combines together the low-order bits of the X and Y
1071 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
1072 * bytes wide and 8 rows high:
1073 *
1074 * tile(x_tiled, X, Y, S) = A
1075 * where A = tile_num << 12 | offset
1076 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
1077 * offset = (Y' & 0b111) << 9
1078 * | (X & 0b111111111)
1079 * X' = X * cpp
1080 * Y' = Y + S * qpitch
1081 * detile(x_tiled, A) = (X, Y, S)
1082 * where X = X' / cpp
1083 * Y = Y' % qpitch
1084 * S = Y' / qpitch
1085 * Y' = (tile_num / tile_pitch) << 3
1086 * | (A & 0b111000000000) >> 9
1087 * X' = (tile_num % tile_pitch) << 9
1088 * | (A & 0b111111111)
1089 *
1090 * (In all tiling formulas, cpp is the number of bytes occupied by a single
1091 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
1092 * to fill the width of the surface, and qpitch is the spacing (in rows)
1093 * between array slices).
1094 *
1095 * For Y tiling, tile() combines together the low-order bits of the X and Y
1096 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
1097 * bytes wide and 32 rows high:
1098 *
1099 * tile(y_tiled, X, Y, S) = A
1100 * where A = tile_num << 12 | offset
1101 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
1102 * offset = (X' & 0b1110000) << 5
1103 * | (Y' & 0b11111) << 4
1104 * | (X' & 0b1111)
1105 * X' = X * cpp
1106 * Y' = Y + S * qpitch
1107 * detile(y_tiled, A) = (X, Y, S)
1108 * where X = X' / cpp
1109 * Y = Y' % qpitch
1110 * S = Y' / qpitch
1111 * Y' = (tile_num / tile_pitch) << 5
1112 * | (A & 0b111110000) >> 4
1113 * X' = (tile_num % tile_pitch) << 7
1114 * | (A & 0b111000000000) >> 5
1115 * | (A & 0b1111)
1116 *
1117 * For W tiling, tile() combines together the low-order bits of the X and Y
1118 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
1119 * bytes wide and 64 rows high (note that W tiling is only used for stencil
1120 * buffers, which always have cpp = 1 and S=0):
1121 *
1122 * tile(w_tiled, X, Y, S) = A
1123 * where A = tile_num << 12 | offset
1124 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
1125 * offset = (X' & 0b111000) << 6
1126 * | (Y' & 0b111100) << 3
1127 * | (X' & 0b100) << 2
1128 * | (Y' & 0b10) << 2
1129 * | (X' & 0b10) << 1
1130 * | (Y' & 0b1) << 1
1131 * | (X' & 0b1)
1132 * X' = X * cpp = X
1133 * Y' = Y + S * qpitch
1134 * detile(w_tiled, A) = (X, Y, S)
1135 * where X = X' / cpp = X'
1136 * Y = Y' % qpitch = Y'
1137 * S = Y / qpitch = 0
1138 * Y' = (tile_num / tile_pitch) << 6
1139 * | (A & 0b111100000) >> 3
1140 * | (A & 0b1000) >> 2
1141 * | (A & 0b10) >> 1
1142 * X' = (tile_num % tile_pitch) << 6
1143 * | (A & 0b111000000000) >> 6
1144 * | (A & 0b10000) >> 2
1145 * | (A & 0b100) >> 1
1146 * | (A & 0b1)
1147 *
1148 * Finally, for a non-tiled surface, tile() simply combines together the X and
1149 * Y coordinates in the natural way:
1150 *
1151 * tile(untiled, X, Y, S) = A
1152 * where A = Y * pitch + X'
1153 * X' = X * cpp
1154 * Y' = Y + S * qpitch
1155 * detile(untiled, A) = (X, Y, S)
1156 * where X = X' / cpp
1157 * Y = Y' % qpitch
1158 * S = Y' / qpitch
1159 * X' = A % pitch
1160 * Y' = A / pitch
1161 *
1162 * (In these formulas, pitch is the number of bytes occupied by a single row
1163 * of samples).
1164 */
1165 static nir_shader *
1166 brw_blorp_build_nir_shader(struct blorp_context *blorp, void *mem_ctx,
1167 const struct brw_blorp_blit_prog_key *key)
1168 {
1169 const struct gen_device_info *devinfo = blorp->isl_dev->info;
1170 nir_ssa_def *src_pos, *dst_pos, *color;
1171
1172 /* Sanity checks */
1173 if (key->dst_tiled_w && key->rt_samples > 1) {
1174 /* If the destination image is W tiled and multisampled, then the thread
1175 * must be dispatched once per sample, not once per pixel. This is
1176 * necessary because after conversion between W and Y tiling, there's no
1177 * guarantee that all samples corresponding to a single pixel will still
1178 * be together.
1179 */
1180 assert(key->persample_msaa_dispatch);
1181 }
1182
1183 if (key->persample_msaa_dispatch) {
1184 /* It only makes sense to do persample dispatch if the render target is
1185 * configured as multisampled.
1186 */
1187 assert(key->rt_samples > 0);
1188 }
1189
1190 /* Make sure layout is consistent with sample count */
1191 assert((key->tex_layout == ISL_MSAA_LAYOUT_NONE) ==
1192 (key->tex_samples <= 1));
1193 assert((key->rt_layout == ISL_MSAA_LAYOUT_NONE) ==
1194 (key->rt_samples <= 1));
1195 assert((key->src_layout == ISL_MSAA_LAYOUT_NONE) ==
1196 (key->src_samples <= 1));
1197 assert((key->dst_layout == ISL_MSAA_LAYOUT_NONE) ==
1198 (key->dst_samples <= 1));
1199
1200 nir_builder b;
1201 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
1202
1203 struct brw_blorp_blit_vars v;
1204 brw_blorp_blit_vars_init(&b, &v, key);
1205
1206 dst_pos = blorp_blit_get_frag_coords(&b, key, &v);
1207
1208 /* Render target and texture hardware don't support W tiling until Gen8. */
1209 const bool rt_tiled_w = false;
1210 const bool tex_tiled_w = devinfo->gen >= 8 && key->src_tiled_w;
1211
1212 /* The address that data will be written to is determined by the
1213 * coordinates supplied to the WM thread and the tiling and sample count of
1214 * the render target, according to the formula:
1215 *
1216 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1217 *
1218 * If the actual tiling and sample count of the destination surface are not
1219 * the same as the configuration of the render target, then these
1220 * coordinates are wrong and we have to adjust them to compensate for the
1221 * difference.
1222 */
1223 if (rt_tiled_w != key->dst_tiled_w ||
1224 key->rt_samples != key->dst_samples ||
1225 key->rt_layout != key->dst_layout) {
1226 dst_pos = blorp_nir_encode_msaa(&b, dst_pos, key->rt_samples,
1227 key->rt_layout);
1228 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1229 if (rt_tiled_w != key->dst_tiled_w)
1230 dst_pos = blorp_nir_retile_y_to_w(&b, dst_pos);
1231 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1232 dst_pos = blorp_nir_decode_msaa(&b, dst_pos, key->dst_samples,
1233 key->dst_layout);
1234 }
1235
1236 nir_ssa_def *comp = NULL;
1237 if (key->dst_rgb) {
1238 /* The destination image is bound as a red texture three times as wide
1239 * as the actual image. Our shader is effectively running one color
1240 * component at a time. We need to save off the component and adjust
1241 * the destination position.
1242 */
1243 assert(dst_pos->num_components == 2);
1244 nir_ssa_def *dst_x = nir_channel(&b, dst_pos, 0);
1245 comp = nir_umod(&b, dst_x, nir_imm_int(&b, 3));
1246 dst_pos = nir_vec2(&b, nir_idiv(&b, dst_x, nir_imm_int(&b, 3)),
1247 nir_channel(&b, dst_pos, 1));
1248 }
1249
1250 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1251 *
1252 * That is: X, Y and S now contain the true coordinates and sample index of
1253 * the data that the WM thread should output.
1254 *
1255 * If we need to kill pixels that are outside the destination rectangle,
1256 * now is the time to do it.
1257 */
1258 if (key->use_kill)
1259 blorp_nir_discard_if_outside_rect(&b, dst_pos, &v);
1260
1261 src_pos = blorp_blit_apply_transform(&b, nir_i2f32(&b, dst_pos), &v);
1262 if (dst_pos->num_components == 3) {
1263 /* The sample coordinate is an integer that we want left alone but
1264 * blorp_blit_apply_transform() blindly applies the transform to all
1265 * three coordinates. Grab the original sample index.
1266 */
1267 src_pos = nir_vec3(&b, nir_channel(&b, src_pos, 0),
1268 nir_channel(&b, src_pos, 1),
1269 nir_channel(&b, dst_pos, 2));
1270 }
1271
1272 /* If the source image is not multisampled, then we want to fetch sample
1273 * number 0, because that's the only sample there is.
1274 */
1275 if (key->src_samples == 1)
1276 src_pos = nir_channels(&b, src_pos, 0x3);
1277
1278 /* X, Y, and S are now the coordinates of the pixel in the source image
1279 * that we want to texture from. Exception: if we are blending, then S is
1280 * irrelevant, because we are going to fetch all samples.
1281 */
1282 switch (key->filter) {
1283 case BLORP_FILTER_NONE:
1284 case BLORP_FILTER_NEAREST:
1285 case BLORP_FILTER_SAMPLE_0:
1286 /* We're going to use texelFetch, so we need integers */
1287 if (src_pos->num_components == 2) {
1288 src_pos = nir_f2i32(&b, src_pos);
1289 } else {
1290 assert(src_pos->num_components == 3);
1291 src_pos = nir_vec3(&b, nir_channel(&b, nir_f2i32(&b, src_pos), 0),
1292 nir_channel(&b, nir_f2i32(&b, src_pos), 1),
1293 nir_channel(&b, src_pos, 2));
1294 }
1295
1296 /* We aren't blending, which means we just want to fetch a single
1297 * sample from the source surface. The address that we want to fetch
1298 * from is related to the X, Y and S values according to the formula:
1299 *
1300 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1301 *
1302 * If the actual tiling and sample count of the source surface are
1303 * not the same as the configuration of the texture, then we need to
1304 * adjust the coordinates to compensate for the difference.
1305 */
1306 if (tex_tiled_w != key->src_tiled_w ||
1307 key->tex_samples != key->src_samples ||
1308 key->tex_layout != key->src_layout) {
1309 src_pos = blorp_nir_encode_msaa(&b, src_pos, key->src_samples,
1310 key->src_layout);
1311 /* Now (X, Y, S) = detile(src_tiling, offset) */
1312 if (tex_tiled_w != key->src_tiled_w)
1313 src_pos = blorp_nir_retile_w_to_y(&b, src_pos);
1314 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1315 src_pos = blorp_nir_decode_msaa(&b, src_pos, key->tex_samples,
1316 key->tex_layout);
1317 }
1318
1319 if (key->need_src_offset)
1320 src_pos = nir_iadd(&b, src_pos, nir_load_var(&b, v.v_src_offset));
1321
1322 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1323 *
1324 * In other words: X, Y, and S now contain values which, when passed to
1325 * the texturing unit, will cause data to be read from the correct
1326 * memory location. So we can fetch the texel now.
1327 */
1328 if (key->src_samples == 1) {
1329 color = blorp_nir_txf(&b, &v, src_pos, key->texture_data_type);
1330 } else {
1331 nir_ssa_def *mcs = NULL;
1332 if (key->tex_aux_usage == ISL_AUX_USAGE_MCS)
1333 mcs = blorp_blit_txf_ms_mcs(&b, &v, src_pos);
1334
1335 color = blorp_nir_txf_ms(&b, &v, src_pos, mcs, key->texture_data_type);
1336 }
1337 break;
1338
1339 case BLORP_FILTER_BILINEAR:
1340 assert(!key->src_tiled_w);
1341 assert(key->tex_samples == key->src_samples);
1342 assert(key->tex_layout == key->src_layout);
1343
1344 if (key->src_samples == 1) {
1345 color = blorp_nir_tex(&b, &v, key, src_pos);
1346 } else {
1347 assert(!key->use_kill);
1348 color = blorp_nir_manual_blend_bilinear(&b, src_pos, key->src_samples,
1349 key, &v);
1350 }
1351 break;
1352
1353 case BLORP_FILTER_AVERAGE:
1354 assert(!key->src_tiled_w);
1355 assert(key->tex_samples == key->src_samples);
1356 assert(key->tex_layout == key->src_layout);
1357
1358 /* Resolves (effecively) use texelFetch, so we need integers and we
1359 * don't care about the sample index if we got one.
1360 */
1361 src_pos = nir_f2i32(&b, nir_channels(&b, src_pos, 0x3));
1362
1363 if (devinfo->gen == 6) {
1364 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1365 * blending we need with a single linear-interpolated texture lookup
1366 * at the center of the sample. The texture coordinates to be odd
1367 * integers so that they correspond to the center of a 2x2 block
1368 * representing the four samples that maxe up a pixel. So we need
1369 * to multiply our X and Y coordinates each by 2 and then add 1.
1370 */
1371 assert(key->src_coords_normalized);
1372 src_pos = nir_fadd(&b,
1373 nir_i2f32(&b, src_pos),
1374 nir_imm_float(&b, 0.5f));
1375 color = blorp_nir_tex(&b, &v, key, src_pos);
1376 } else {
1377 /* Gen7+ hardware doesn't automaticaly blend. */
1378 color = blorp_nir_manual_blend_average(&b, &v, src_pos, key->src_samples,
1379 key->tex_aux_usage,
1380 key->texture_data_type);
1381 }
1382 break;
1383
1384 default:
1385 unreachable("Invalid blorp filter");
1386 }
1387
1388 if (!isl_swizzle_is_identity(key->src_swizzle)) {
1389 color = swizzle_color(&b, color, key->src_swizzle,
1390 key->texture_data_type);
1391 }
1392
1393 if (!isl_swizzle_is_identity(key->dst_swizzle)) {
1394 color = swizzle_color(&b, color, isl_swizzle_invert(key->dst_swizzle),
1395 nir_type_int);
1396 }
1397
1398 if (key->format_bit_cast) {
1399 assert(isl_swizzle_is_identity(key->src_swizzle));
1400 assert(isl_swizzle_is_identity(key->dst_swizzle));
1401 color = bit_cast_color(&b, color, key);
1402 } else if (key->dst_format) {
1403 color = convert_color(&b, color, key);
1404 }
1405
1406 if (key->dst_rgb) {
1407 /* The destination image is bound as a red texture three times as wide
1408 * as the actual image. Our shader is effectively running one color
1409 * component at a time. We need to pick off the appropriate component
1410 * from the source color and write that to destination red.
1411 */
1412 assert(dst_pos->num_components == 2);
1413
1414 nir_ssa_def *color_component =
1415 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 0)),
1416 nir_channel(&b, color, 0),
1417 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 1)),
1418 nir_channel(&b, color, 1),
1419 nir_channel(&b, color, 2)));
1420
1421 nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);
1422 color = nir_vec4(&b, color_component, u, u, u);
1423 }
1424
1425 nir_store_var(&b, v.color_out, color, 0xf);
1426
1427 return b.shader;
1428 }
1429
1430 static bool
1431 brw_blorp_get_blit_kernel(struct blorp_context *blorp,
1432 struct blorp_params *params,
1433 const struct brw_blorp_blit_prog_key *prog_key)
1434 {
1435 if (blorp->lookup_shader(blorp, prog_key, sizeof(*prog_key),
1436 &params->wm_prog_kernel, &params->wm_prog_data))
1437 return true;
1438
1439 void *mem_ctx = ralloc_context(NULL);
1440
1441 const unsigned *program;
1442 struct brw_wm_prog_data prog_data;
1443
1444 nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
1445 nir->info.name = ralloc_strdup(nir, "BLORP-blit");
1446
1447 struct brw_wm_prog_key wm_key;
1448 brw_blorp_init_wm_prog_key(&wm_key);
1449 wm_key.tex.compressed_multisample_layout_mask =
1450 prog_key->tex_aux_usage == ISL_AUX_USAGE_MCS;
1451 wm_key.tex.msaa_16 = prog_key->tex_samples == 16;
1452 wm_key.multisample_fbo = prog_key->rt_samples > 1;
1453
1454 program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
1455 &prog_data);
1456
1457 bool result =
1458 blorp->upload_shader(blorp, prog_key, sizeof(*prog_key),
1459 program, prog_data.base.program_size,
1460 &prog_data.base, sizeof(prog_data),
1461 &params->wm_prog_kernel, &params->wm_prog_data);
1462
1463 ralloc_free(mem_ctx);
1464 return result;
1465 }
1466
1467 static void
1468 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
1469 GLfloat src0, GLfloat src1,
1470 GLfloat dst0, GLfloat dst1,
1471 bool mirror)
1472 {
1473 double scale = (double)(src1 - src0) / (double)(dst1 - dst0);
1474 if (!mirror) {
1475 /* When not mirroring a coordinate (say, X), we need:
1476 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1477 * Therefore:
1478 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1479 *
1480 * blorp program uses "round toward zero" to convert the
1481 * transformed floating point coordinates to integer coordinates,
1482 * whereas the behaviour we actually want is "round to nearest",
1483 * so 0.5 provides the necessary correction.
1484 */
1485 xform->multiplier = scale;
1486 xform->offset = src0 + (-(double)dst0 + 0.5) * scale;
1487 } else {
1488 /* When mirroring X we need:
1489 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1490 * Therefore:
1491 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1492 */
1493 xform->multiplier = -scale;
1494 xform->offset = src0 + ((double)dst1 - 0.5) * scale;
1495 }
1496 }
1497
1498 static inline void
1499 surf_get_intratile_offset_px(struct brw_blorp_surface_info *info,
1500 uint32_t *tile_x_px, uint32_t *tile_y_px)
1501 {
1502 if (info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1503 struct isl_extent2d px_size_sa =
1504 isl_get_interleaved_msaa_px_size_sa(info->surf.samples);
1505 assert(info->tile_x_sa % px_size_sa.width == 0);
1506 assert(info->tile_y_sa % px_size_sa.height == 0);
1507 *tile_x_px = info->tile_x_sa / px_size_sa.width;
1508 *tile_y_px = info->tile_y_sa / px_size_sa.height;
1509 } else {
1510 *tile_x_px = info->tile_x_sa;
1511 *tile_y_px = info->tile_y_sa;
1512 }
1513 }
1514
1515 void
1516 blorp_surf_convert_to_single_slice(const struct isl_device *isl_dev,
1517 struct brw_blorp_surface_info *info)
1518 {
1519 bool ok UNUSED;
1520
1521 /* It would be insane to try and do this on a compressed surface */
1522 assert(info->aux_usage == ISL_AUX_USAGE_NONE);
1523
1524 /* Just bail if we have nothing to do. */
1525 if (info->surf.dim == ISL_SURF_DIM_2D &&
1526 info->view.base_level == 0 && info->view.base_array_layer == 0 &&
1527 info->surf.levels == 1 && info->surf.logical_level0_px.array_len == 1)
1528 return;
1529
1530 /* If this gets triggered then we've gotten here twice which. This
1531 * shouldn't happen thanks to the above early return.
1532 */
1533 assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
1534
1535 uint32_t layer = 0, z = 0;
1536 if (info->surf.dim == ISL_SURF_DIM_3D)
1537 z = info->view.base_array_layer + info->z_offset;
1538 else
1539 layer = info->view.base_array_layer;
1540
1541 uint32_t byte_offset;
1542 isl_surf_get_image_surf(isl_dev, &info->surf,
1543 info->view.base_level, layer, z,
1544 &info->surf,
1545 &byte_offset, &info->tile_x_sa, &info->tile_y_sa);
1546 info->addr.offset += byte_offset;
1547
1548 uint32_t tile_x_px, tile_y_px;
1549 surf_get_intratile_offset_px(info, &tile_x_px, &tile_y_px);
1550
1551 /* Instead of using the X/Y Offset fields in RENDER_SURFACE_STATE, we place
1552 * the image at the tile boundary and offset our sampling or rendering.
1553 * For this reason, we need to grow the image by the offset to ensure that
1554 * the hardware doesn't think we've gone past the edge.
1555 */
1556 info->surf.logical_level0_px.w += tile_x_px;
1557 info->surf.logical_level0_px.h += tile_y_px;
1558 info->surf.phys_level0_sa.w += info->tile_x_sa;
1559 info->surf.phys_level0_sa.h += info->tile_y_sa;
1560
1561 /* The view is also different now. */
1562 info->view.base_level = 0;
1563 info->view.levels = 1;
1564 info->view.base_array_layer = 0;
1565 info->view.array_len = 1;
1566 info->z_offset = 0;
1567 }
1568
1569 static void
1570 surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
1571 struct brw_blorp_surface_info *info)
1572 {
1573 assert(info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1574
1575 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1576 blorp_surf_convert_to_single_slice(isl_dev, info);
1577
1578 info->surf.logical_level0_px = info->surf.phys_level0_sa;
1579 info->surf.samples = 1;
1580 info->surf.msaa_layout = ISL_MSAA_LAYOUT_NONE;
1581 }
1582
1583 static void
1584 surf_retile_w_to_y(const struct isl_device *isl_dev,
1585 struct brw_blorp_surface_info *info)
1586 {
1587 assert(info->surf.tiling == ISL_TILING_W);
1588
1589 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1590 blorp_surf_convert_to_single_slice(isl_dev, info);
1591
1592 /* On gen7+, we don't have interleaved multisampling for color render
1593 * targets so we have to fake it.
1594 *
1595 * TODO: Are we sure we don't also need to fake it on gen6?
1596 */
1597 if (isl_dev->info->gen > 6 &&
1598 info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1599 surf_fake_interleaved_msaa(isl_dev, info);
1600 }
1601
1602 if (isl_dev->info->gen == 6) {
1603 /* Gen6 stencil buffers have a very large alignment coming in from the
1604 * miptree. It's out-of-bounds for what the surface state can handle.
1605 * Since we have a single layer and level, it doesn't really matter as
1606 * long as we don't pass a bogus value into isl_surf_fill_state().
1607 */
1608 info->surf.image_alignment_el = isl_extent3d(4, 2, 1);
1609 }
1610
1611 /* Now that we've converted everything to a simple 2-D surface with only
1612 * one miplevel, we can go about retiling it.
1613 */
1614 const unsigned x_align = 8, y_align = info->surf.samples != 0 ? 8 : 4;
1615 info->surf.tiling = ISL_TILING_Y0;
1616 info->surf.logical_level0_px.width =
1617 ALIGN(info->surf.logical_level0_px.width, x_align) * 2;
1618 info->surf.logical_level0_px.height =
1619 ALIGN(info->surf.logical_level0_px.height, y_align) / 2;
1620 info->tile_x_sa *= 2;
1621 info->tile_y_sa /= 2;
1622 }
1623
1624 static bool
1625 can_shrink_surface(const struct brw_blorp_surface_info *surf)
1626 {
1627 /* The current code doesn't support offsets into the aux buffers. This
1628 * should be possible, but we need to make sure the offset is page
1629 * aligned for both the surface and the aux buffer surface. Generally
1630 * this mean using the page aligned offset for the aux buffer.
1631 *
1632 * Currently the cases where we must split the blit are limited to cases
1633 * where we don't have a aux buffer.
1634 */
1635 if (surf->aux_addr.buffer != NULL)
1636 return false;
1637
1638 /* We can't support splitting the blit for gen <= 7, because the qpitch
1639 * size is calculated by the hardware based on the surface height for
1640 * gen <= 7. In gen >= 8, the qpitch is controlled by the driver.
1641 */
1642 if (surf->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY)
1643 return false;
1644
1645 return true;
1646 }
1647
1648 static bool
1649 can_shrink_surfaces(const struct blorp_params *params)
1650 {
1651 return
1652 can_shrink_surface(&params->src) &&
1653 can_shrink_surface(&params->dst);
1654 }
1655
1656 static unsigned
1657 get_max_surface_size(const struct gen_device_info *devinfo,
1658 const struct blorp_params *params)
1659 {
1660 const unsigned max = devinfo->gen >= 7 ? 16384 : 8192;
1661 if (split_blorp_blit_debug && can_shrink_surfaces(params))
1662 return max >> 4; /* A smaller restriction when debug is enabled */
1663 else
1664 return max;
1665 }
1666
1667 struct blt_axis {
1668 double src0, src1, dst0, dst1;
1669 bool mirror;
1670 };
1671
1672 struct blt_coords {
1673 struct blt_axis x, y;
1674 };
1675
1676 static enum isl_format
1677 get_red_format_for_rgb_format(enum isl_format format)
1678 {
1679 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1680
1681 switch (fmtl->channels.r.bits) {
1682 case 8:
1683 switch (fmtl->channels.r.type) {
1684 case ISL_UNORM:
1685 return ISL_FORMAT_R8_UNORM;
1686 case ISL_SNORM:
1687 return ISL_FORMAT_R8_SNORM;
1688 case ISL_UINT:
1689 return ISL_FORMAT_R8_UINT;
1690 case ISL_SINT:
1691 return ISL_FORMAT_R8_SINT;
1692 default:
1693 unreachable("Invalid 8-bit RGB channel type");
1694 }
1695 case 16:
1696 switch (fmtl->channels.r.type) {
1697 case ISL_UNORM:
1698 return ISL_FORMAT_R16_UNORM;
1699 case ISL_SNORM:
1700 return ISL_FORMAT_R16_SNORM;
1701 case ISL_SFLOAT:
1702 return ISL_FORMAT_R16_FLOAT;
1703 case ISL_UINT:
1704 return ISL_FORMAT_R16_UINT;
1705 case ISL_SINT:
1706 return ISL_FORMAT_R16_SINT;
1707 default:
1708 unreachable("Invalid 8-bit RGB channel type");
1709 }
1710 case 32:
1711 switch (fmtl->channels.r.type) {
1712 case ISL_SFLOAT:
1713 return ISL_FORMAT_R32_FLOAT;
1714 case ISL_UINT:
1715 return ISL_FORMAT_R32_UINT;
1716 case ISL_SINT:
1717 return ISL_FORMAT_R32_SINT;
1718 default:
1719 unreachable("Invalid 8-bit RGB channel type");
1720 }
1721 default:
1722 unreachable("Invalid number of red channel bits");
1723 }
1724 }
1725
1726 void
1727 surf_fake_rgb_with_red(const struct isl_device *isl_dev,
1728 struct brw_blorp_surface_info *info)
1729 {
1730 blorp_surf_convert_to_single_slice(isl_dev, info);
1731
1732 info->surf.logical_level0_px.width *= 3;
1733 info->surf.phys_level0_sa.width *= 3;
1734 info->tile_x_sa *= 3;
1735
1736 enum isl_format red_format =
1737 get_red_format_for_rgb_format(info->view.format);
1738
1739 assert(isl_format_get_layout(red_format)->channels.r.type ==
1740 isl_format_get_layout(info->view.format)->channels.r.type);
1741 assert(isl_format_get_layout(red_format)->channels.r.bits ==
1742 isl_format_get_layout(info->view.format)->channels.r.bits);
1743
1744 info->surf.format = info->view.format = red_format;
1745 }
1746
1747 enum blit_shrink_status {
1748 BLIT_NO_SHRINK = 0,
1749 BLIT_WIDTH_SHRINK = 1,
1750 BLIT_HEIGHT_SHRINK = 2,
1751 };
1752
1753 /* Try to blit. If the surface parameters exceed the size allowed by hardware,
1754 * then enum blit_shrink_status will be returned. If BLIT_NO_SHRINK is
1755 * returned, then the blit was successful.
1756 */
1757 static enum blit_shrink_status
1758 try_blorp_blit(struct blorp_batch *batch,
1759 struct blorp_params *params,
1760 struct brw_blorp_blit_prog_key *wm_prog_key,
1761 struct blt_coords *coords)
1762 {
1763 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
1764
1765 if (isl_format_has_sint_channel(params->src.view.format)) {
1766 wm_prog_key->texture_data_type = nir_type_int;
1767 } else if (isl_format_has_uint_channel(params->src.view.format)) {
1768 wm_prog_key->texture_data_type = nir_type_uint;
1769 } else {
1770 wm_prog_key->texture_data_type = nir_type_float;
1771 }
1772
1773 /* src_samples and dst_samples are the true sample counts */
1774 wm_prog_key->src_samples = params->src.surf.samples;
1775 wm_prog_key->dst_samples = params->dst.surf.samples;
1776
1777 wm_prog_key->tex_aux_usage = params->src.aux_usage;
1778
1779 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1780 * dst.
1781 */
1782 wm_prog_key->src_layout = params->src.surf.msaa_layout;
1783 wm_prog_key->dst_layout = params->dst.surf.msaa_layout;
1784
1785 /* Round floating point values to nearest integer to avoid "off by one texel"
1786 * kind of errors when blitting.
1787 */
1788 params->x0 = params->wm_inputs.discard_rect.x0 = round(coords->x.dst0);
1789 params->y0 = params->wm_inputs.discard_rect.y0 = round(coords->y.dst0);
1790 params->x1 = params->wm_inputs.discard_rect.x1 = round(coords->x.dst1);
1791 params->y1 = params->wm_inputs.discard_rect.y1 = round(coords->y.dst1);
1792
1793 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[0],
1794 coords->x.src0, coords->x.src1,
1795 coords->x.dst0, coords->x.dst1,
1796 coords->x.mirror);
1797 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[1],
1798 coords->y.src0, coords->y.src1,
1799 coords->y.dst0, coords->y.dst1,
1800 coords->y.mirror);
1801
1802
1803 if (devinfo->gen == 4) {
1804 /* The MinLOD and MinimumArrayElement don't work properly for cube maps.
1805 * Convert them to a single slice on gen4.
1806 */
1807 if (params->dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1808 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->dst);
1809 wm_prog_key->need_dst_offset = true;
1810 }
1811
1812 if (params->src.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1813 blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->src);
1814 wm_prog_key->need_src_offset = true;
1815 }
1816 }
1817
1818 if (devinfo->gen > 6 &&
1819 params->dst.surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1820 assert(params->dst.surf.samples > 1);
1821
1822 /* We must expand the rectangle we send through the rendering pipeline,
1823 * to account for the fact that we are mapping the destination region as
1824 * single-sampled when it is in fact multisampled. We must also align
1825 * it to a multiple of the multisampling pattern, because the
1826 * differences between multisampled and single-sampled surface formats
1827 * will mean that pixels are scrambled within the multisampling pattern.
1828 * TODO: what if this makes the coordinates too large?
1829 *
1830 * Note: this only works if the destination surface uses the IMS layout.
1831 * If it's UMS, then we have no choice but to set up the rendering
1832 * pipeline as multisampled.
1833 */
1834 struct isl_extent2d px_size_sa =
1835 isl_get_interleaved_msaa_px_size_sa(params->dst.surf.samples);
1836 params->x0 = ROUND_DOWN_TO(params->x0, 2) * px_size_sa.width;
1837 params->y0 = ROUND_DOWN_TO(params->y0, 2) * px_size_sa.height;
1838 params->x1 = ALIGN(params->x1, 2) * px_size_sa.width;
1839 params->y1 = ALIGN(params->y1, 2) * px_size_sa.height;
1840
1841 surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params->dst);
1842
1843 wm_prog_key->use_kill = true;
1844 wm_prog_key->need_dst_offset = true;
1845 }
1846
1847 if (params->dst.surf.tiling == ISL_TILING_W) {
1848 /* We must modify the rectangle we send through the rendering pipeline
1849 * (and the size and x/y offset of the destination surface), to account
1850 * for the fact that we are mapping it as Y-tiled when it is in fact
1851 * W-tiled.
1852 *
1853 * Both Y tiling and W tiling can be understood as organizations of
1854 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1855 * is different, but the layout of the 32-byte sub-tiles within the 4k
1856 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1857 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1858 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1859 *
1860 * Therefore, to account for the layout differences within the 32-byte
1861 * sub-tiles, we must expand the rectangle so the X coordinates of its
1862 * edges are multiples of 8 (the W sub-tile width), and its Y
1863 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1864 * Then we need to scale the X and Y coordinates of the rectangle to
1865 * account for the differences in aspect ratio between the Y and W
1866 * sub-tiles. We need to modify the layer width and height similarly.
1867 *
1868 * A correction needs to be applied when MSAA is in use: since
1869 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1870 * we need to align the Y coordinates to multiples of 8, so that when
1871 * they are divided by two they are still multiples of 4.
1872 *
1873 * Note: Since the x/y offset of the surface will be applied using the
1874 * SURFACE_STATE command packet, it will be invisible to the swizzling
1875 * code in the shader; therefore it needs to be in a multiple of the
1876 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1877 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1878 * buffer), and the miplevel alignment used for stencil buffers is 8
1879 * pixels horizontally and either 4 or 8 pixels vertically (see
1880 * intel_horizontal_texture_alignment_unit() and
1881 * intel_vertical_texture_alignment_unit()).
1882 *
1883 * Note: Also, since the SURFACE_STATE command packet can only apply
1884 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1885 * vertically, it is important that the offsets will be multiples of
1886 * these sizes after they are converted into Y-tiled coordinates.
1887 * Fortunately they will be, since we know from above that the offsets
1888 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1889 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1890 *
1891 * TODO: what if this makes the coordinates (or the texture size) too
1892 * large?
1893 */
1894 const unsigned x_align = 8;
1895 const unsigned y_align = params->dst.surf.samples != 0 ? 8 : 4;
1896 params->x0 = ROUND_DOWN_TO(params->x0, x_align) * 2;
1897 params->y0 = ROUND_DOWN_TO(params->y0, y_align) / 2;
1898 params->x1 = ALIGN(params->x1, x_align) * 2;
1899 params->y1 = ALIGN(params->y1, y_align) / 2;
1900
1901 /* Retile the surface to Y-tiled */
1902 surf_retile_w_to_y(batch->blorp->isl_dev, &params->dst);
1903
1904 wm_prog_key->dst_tiled_w = true;
1905 wm_prog_key->use_kill = true;
1906 wm_prog_key->need_dst_offset = true;
1907
1908 if (params->dst.surf.samples > 1) {
1909 /* If the destination surface is a W-tiled multisampled stencil
1910 * buffer that we're mapping as Y tiled, then we need to arrange for
1911 * the WM program to run once per sample rather than once per pixel,
1912 * because the memory layout of related samples doesn't match between
1913 * W and Y tiling.
1914 */
1915 wm_prog_key->persample_msaa_dispatch = true;
1916 }
1917 }
1918
1919 if (devinfo->gen < 8 && params->src.surf.tiling == ISL_TILING_W) {
1920 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1921 * Broadwell adds support for sampling from stencil.
1922 *
1923 * See the comments above concerning x/y offset alignment for the
1924 * destination surface.
1925 *
1926 * TODO: what if this makes the texture size too large?
1927 */
1928 surf_retile_w_to_y(batch->blorp->isl_dev, &params->src);
1929
1930 wm_prog_key->src_tiled_w = true;
1931 wm_prog_key->need_src_offset = true;
1932 }
1933
1934 /* tex_samples and rt_samples are the sample counts that are set up in
1935 * SURFACE_STATE.
1936 */
1937 wm_prog_key->tex_samples = params->src.surf.samples;
1938 wm_prog_key->rt_samples = params->dst.surf.samples;
1939
1940 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1941 * use to access the source and destination surfaces.
1942 */
1943 wm_prog_key->tex_layout = params->src.surf.msaa_layout;
1944 wm_prog_key->rt_layout = params->dst.surf.msaa_layout;
1945
1946 if (params->src.surf.samples > 0 && params->dst.surf.samples > 1) {
1947 /* We are blitting from a multisample buffer to a multisample buffer, so
1948 * we must preserve samples within a pixel. This means we have to
1949 * arrange for the WM program to run once per sample rather than once
1950 * per pixel.
1951 */
1952 wm_prog_key->persample_msaa_dispatch = true;
1953 }
1954
1955 params->num_samples = params->dst.surf.samples;
1956
1957 if ((wm_prog_key->filter == BLORP_FILTER_AVERAGE ||
1958 wm_prog_key->filter == BLORP_FILTER_BILINEAR) &&
1959 batch->blorp->isl_dev->info->gen <= 6) {
1960 /* Gen4-5 don't support non-normalized texture coordinates */
1961 wm_prog_key->src_coords_normalized = true;
1962 params->wm_inputs.src_inv_size[0] =
1963 1.0f / minify(params->src.surf.logical_level0_px.width,
1964 params->src.view.base_level);
1965 params->wm_inputs.src_inv_size[1] =
1966 1.0f / minify(params->src.surf.logical_level0_px.height,
1967 params->src.view.base_level);
1968 }
1969
1970 if (isl_format_get_layout(params->dst.view.format)->bpb % 3 == 0) {
1971 /* We can't render to RGB formats natively because they aren't a
1972 * power-of-two size. Instead, we fake them by using a red format
1973 * with the same channel type and size and emitting shader code to
1974 * only write one channel at a time.
1975 */
1976 params->x0 *= 3;
1977 params->x1 *= 3;
1978
1979 /* If it happens to be sRGB, we need to force a conversion */
1980 if (params->dst.view.format == ISL_FORMAT_R8G8B8_UNORM_SRGB)
1981 wm_prog_key->dst_format = ISL_FORMAT_R8G8B8_UNORM_SRGB;
1982
1983 surf_fake_rgb_with_red(batch->blorp->isl_dev, &params->dst);
1984
1985 wm_prog_key->dst_rgb = true;
1986 wm_prog_key->need_dst_offset = true;
1987 } else if (isl_format_is_rgbx(params->dst.view.format)) {
1988 /* We can handle RGBX formats easily enough by treating them as RGBA */
1989 params->dst.view.format =
1990 isl_format_rgbx_to_rgba(params->dst.view.format);
1991 } else if (params->dst.view.format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {
1992 wm_prog_key->dst_format = params->dst.view.format;
1993 params->dst.view.format = ISL_FORMAT_R32_UINT;
1994 } else if (params->dst.view.format == ISL_FORMAT_A4B4G4R4_UNORM) {
1995 params->dst.view.swizzle =
1996 isl_swizzle_compose(params->dst.view.swizzle,
1997 ISL_SWIZZLE(ALPHA, RED, GREEN, BLUE));
1998 params->dst.view.format = ISL_FORMAT_B4G4R4A4_UNORM;
1999 } else if (params->dst.view.format == ISL_FORMAT_L8_UNORM_SRGB) {
2000 wm_prog_key->dst_format = params->dst.view.format;
2001 params->dst.view.format = ISL_FORMAT_R8_UNORM;
2002 } else if (params->dst.view.format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
2003 wm_prog_key->dst_format = params->dst.view.format;
2004 params->dst.view.format = ISL_FORMAT_R32_UINT;
2005 }
2006
2007 if (devinfo->gen <= 7 && !devinfo->is_haswell &&
2008 !isl_swizzle_is_identity(params->src.view.swizzle)) {
2009 wm_prog_key->src_swizzle = params->src.view.swizzle;
2010 params->src.view.swizzle = ISL_SWIZZLE_IDENTITY;
2011 } else {
2012 wm_prog_key->src_swizzle = ISL_SWIZZLE_IDENTITY;
2013 }
2014
2015 if (!isl_swizzle_supports_rendering(devinfo, params->dst.view.swizzle)) {
2016 wm_prog_key->dst_swizzle = params->dst.view.swizzle;
2017 params->dst.view.swizzle = ISL_SWIZZLE_IDENTITY;
2018 } else {
2019 wm_prog_key->dst_swizzle = ISL_SWIZZLE_IDENTITY;
2020 }
2021
2022 if (params->src.tile_x_sa || params->src.tile_y_sa) {
2023 assert(wm_prog_key->need_src_offset);
2024 surf_get_intratile_offset_px(&params->src,
2025 &params->wm_inputs.src_offset.x,
2026 &params->wm_inputs.src_offset.y);
2027 }
2028
2029 if (params->dst.tile_x_sa || params->dst.tile_y_sa) {
2030 assert(wm_prog_key->need_dst_offset);
2031 surf_get_intratile_offset_px(&params->dst,
2032 &params->wm_inputs.dst_offset.x,
2033 &params->wm_inputs.dst_offset.y);
2034 params->x0 += params->wm_inputs.dst_offset.x;
2035 params->y0 += params->wm_inputs.dst_offset.y;
2036 params->x1 += params->wm_inputs.dst_offset.x;
2037 params->y1 += params->wm_inputs.dst_offset.y;
2038 }
2039
2040 /* For some texture types, we need to pass the layer through the sampler. */
2041 params->wm_inputs.src_z = params->src.z_offset;
2042
2043 if (!brw_blorp_get_blit_kernel(batch->blorp, params, wm_prog_key))
2044 return 0;
2045
2046 if (!blorp_ensure_sf_program(batch->blorp, params))
2047 return 0;
2048
2049 unsigned result = 0;
2050 unsigned max_surface_size = get_max_surface_size(devinfo, params);
2051 if (params->src.surf.logical_level0_px.width > max_surface_size ||
2052 params->dst.surf.logical_level0_px.width > max_surface_size)
2053 result |= BLIT_WIDTH_SHRINK;
2054 if (params->src.surf.logical_level0_px.height > max_surface_size ||
2055 params->dst.surf.logical_level0_px.height > max_surface_size)
2056 result |= BLIT_HEIGHT_SHRINK;
2057
2058 if (result == 0) {
2059 batch->blorp->exec(batch, params);
2060 }
2061
2062 return result;
2063 }
2064
2065 /* Adjust split blit source coordinates for the current destination
2066 * coordinates.
2067 */
2068 static void
2069 adjust_split_source_coords(const struct blt_axis *orig,
2070 struct blt_axis *split_coords,
2071 double scale)
2072 {
2073 /* When scale is greater than 0, then we are growing from the start, so
2074 * src0 uses delta0, and src1 uses delta1. When scale is less than 0, the
2075 * source range shrinks from the end. In that case src0 is adjusted by
2076 * delta1, and src1 is adjusted by delta0.
2077 */
2078 double delta0 = scale * (split_coords->dst0 - orig->dst0);
2079 double delta1 = scale * (split_coords->dst1 - orig->dst1);
2080 split_coords->src0 = orig->src0 + (scale >= 0.0 ? delta0 : delta1);
2081 split_coords->src1 = orig->src1 + (scale >= 0.0 ? delta1 : delta0);
2082 }
2083
2084 static struct isl_extent2d
2085 get_px_size_sa(const struct isl_surf *surf)
2086 {
2087 static const struct isl_extent2d one_to_one = { .w = 1, .h = 1 };
2088
2089 if (surf->msaa_layout != ISL_MSAA_LAYOUT_INTERLEAVED)
2090 return one_to_one;
2091 else
2092 return isl_get_interleaved_msaa_px_size_sa(surf->samples);
2093 }
2094
2095 static void
2096 shrink_surface_params(const struct isl_device *dev,
2097 struct brw_blorp_surface_info *info,
2098 double *x0, double *x1, double *y0, double *y1)
2099 {
2100 uint32_t byte_offset, x_offset_sa, y_offset_sa, size;
2101 struct isl_extent2d px_size_sa;
2102 int adjust;
2103
2104 blorp_surf_convert_to_single_slice(dev, info);
2105
2106 px_size_sa = get_px_size_sa(&info->surf);
2107
2108 /* Because this gets called after we lower compressed images, the tile
2109 * offsets may be non-zero and we need to incorporate them in our
2110 * calculations.
2111 */
2112 x_offset_sa = (uint32_t)*x0 * px_size_sa.w + info->tile_x_sa;
2113 y_offset_sa = (uint32_t)*y0 * px_size_sa.h + info->tile_y_sa;
2114 isl_tiling_get_intratile_offset_sa(info->surf.tiling,
2115 info->surf.format, info->surf.row_pitch_B,
2116 x_offset_sa, y_offset_sa,
2117 &byte_offset,
2118 &info->tile_x_sa, &info->tile_y_sa);
2119
2120 info->addr.offset += byte_offset;
2121
2122 adjust = (int)info->tile_x_sa / px_size_sa.w - (int)*x0;
2123 *x0 += adjust;
2124 *x1 += adjust;
2125 info->tile_x_sa = 0;
2126
2127 adjust = (int)info->tile_y_sa / px_size_sa.h - (int)*y0;
2128 *y0 += adjust;
2129 *y1 += adjust;
2130 info->tile_y_sa = 0;
2131
2132 size = MIN2((uint32_t)ceil(*x1), info->surf.logical_level0_px.width);
2133 info->surf.logical_level0_px.width = size;
2134 info->surf.phys_level0_sa.width = size * px_size_sa.w;
2135
2136 size = MIN2((uint32_t)ceil(*y1), info->surf.logical_level0_px.height);
2137 info->surf.logical_level0_px.height = size;
2138 info->surf.phys_level0_sa.height = size * px_size_sa.h;
2139 }
2140
2141 static void
2142 shrink_surfaces(const struct isl_device *dev,
2143 struct blorp_params *params,
2144 struct brw_blorp_blit_prog_key *wm_prog_key,
2145 struct blt_coords *coords)
2146 {
2147 /* Shrink source surface */
2148 shrink_surface_params(dev, &params->src, &coords->x.src0, &coords->x.src1,
2149 &coords->y.src0, &coords->y.src1);
2150 wm_prog_key->need_src_offset = false;
2151
2152 /* Shrink destination surface */
2153 shrink_surface_params(dev, &params->dst, &coords->x.dst0, &coords->x.dst1,
2154 &coords->y.dst0, &coords->y.dst1);
2155 wm_prog_key->need_dst_offset = false;
2156 }
2157
2158 static void
2159 do_blorp_blit(struct blorp_batch *batch,
2160 const struct blorp_params *orig_params,
2161 struct brw_blorp_blit_prog_key *wm_prog_key,
2162 const struct blt_coords *orig)
2163 {
2164 struct blorp_params params;
2165 struct blt_coords blit_coords;
2166 struct blt_coords split_coords = *orig;
2167 double w = orig->x.dst1 - orig->x.dst0;
2168 double h = orig->y.dst1 - orig->y.dst0;
2169 double x_scale = (orig->x.src1 - orig->x.src0) / w;
2170 double y_scale = (orig->y.src1 - orig->y.src0) / h;
2171 if (orig->x.mirror)
2172 x_scale = -x_scale;
2173 if (orig->y.mirror)
2174 y_scale = -y_scale;
2175
2176 bool x_done, y_done;
2177 bool shrink = split_blorp_blit_debug && can_shrink_surfaces(orig_params);
2178 do {
2179 params = *orig_params;
2180 blit_coords = split_coords;
2181 if (shrink)
2182 shrink_surfaces(batch->blorp->isl_dev, &params, wm_prog_key,
2183 &blit_coords);
2184 enum blit_shrink_status result =
2185 try_blorp_blit(batch, &params, wm_prog_key, &blit_coords);
2186
2187 if (result & BLIT_WIDTH_SHRINK) {
2188 w /= 2.0;
2189 assert(w >= 1.0);
2190 split_coords.x.dst1 = MIN2(split_coords.x.dst0 + w, orig->x.dst1);
2191 adjust_split_source_coords(&orig->x, &split_coords.x, x_scale);
2192 }
2193 if (result & BLIT_HEIGHT_SHRINK) {
2194 h /= 2.0;
2195 assert(h >= 1.0);
2196 split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2197 adjust_split_source_coords(&orig->y, &split_coords.y, y_scale);
2198 }
2199
2200 if (result != 0) {
2201 assert(can_shrink_surfaces(orig_params));
2202 shrink = true;
2203 continue;
2204 }
2205
2206 y_done = (orig->y.dst1 - split_coords.y.dst1 < 0.5);
2207 x_done = y_done && (orig->x.dst1 - split_coords.x.dst1 < 0.5);
2208 if (x_done) {
2209 break;
2210 } else if (y_done) {
2211 split_coords.x.dst0 += w;
2212 split_coords.x.dst1 = MIN2(split_coords.x.dst0 + w, orig->x.dst1);
2213 split_coords.y.dst0 = orig->y.dst0;
2214 split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2215 adjust_split_source_coords(&orig->x, &split_coords.x, x_scale);
2216 } else {
2217 split_coords.y.dst0 += h;
2218 split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2219 adjust_split_source_coords(&orig->y, &split_coords.y, y_scale);
2220 }
2221 } while (true);
2222 }
2223
2224 void
2225 blorp_blit(struct blorp_batch *batch,
2226 const struct blorp_surf *src_surf,
2227 unsigned src_level, unsigned src_layer,
2228 enum isl_format src_format, struct isl_swizzle src_swizzle,
2229 const struct blorp_surf *dst_surf,
2230 unsigned dst_level, unsigned dst_layer,
2231 enum isl_format dst_format, struct isl_swizzle dst_swizzle,
2232 float src_x0, float src_y0,
2233 float src_x1, float src_y1,
2234 float dst_x0, float dst_y0,
2235 float dst_x1, float dst_y1,
2236 enum blorp_filter filter,
2237 bool mirror_x, bool mirror_y)
2238 {
2239 struct blorp_params params;
2240 blorp_params_init(&params);
2241
2242 /* We cannot handle combined depth and stencil. */
2243 if (src_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT)
2244 assert(src_surf->surf->format == ISL_FORMAT_R8_UINT);
2245 if (dst_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT)
2246 assert(dst_surf->surf->format == ISL_FORMAT_R8_UINT);
2247
2248 if (dst_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT) {
2249 assert(src_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT);
2250 /* Prior to Broadwell, we can't render to R8_UINT */
2251 if (batch->blorp->isl_dev->info->gen < 8) {
2252 src_format = ISL_FORMAT_R8_UNORM;
2253 dst_format = ISL_FORMAT_R8_UNORM;
2254 }
2255 }
2256
2257 /* ISL_FORMAT_R24_UNORM_X8_TYPELESS it isn't supported as a render target,
2258 * which requires shader math to render to it. Blitting Z24X8 to Z24X8
2259 * is fairly common though, so we'd like to avoid it. Since we don't need
2260 * to blend depth values, we can simply pick a renderable format with the
2261 * right number of bits-per-pixel, like 8-bit BGRA.
2262 */
2263 if (dst_surf->surf->format == ISL_FORMAT_R24_UNORM_X8_TYPELESS &&
2264 src_surf->surf->format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {
2265 src_format = dst_format = ISL_FORMAT_B8G8R8A8_UNORM;
2266 }
2267
2268 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2269 src_layer, src_format, false);
2270 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2271 dst_layer, dst_format, true);
2272
2273 params.src.view.swizzle = src_swizzle;
2274 params.dst.view.swizzle = dst_swizzle;
2275
2276 struct brw_blorp_blit_prog_key wm_prog_key = {
2277 .shader_type = BLORP_SHADER_TYPE_BLIT,
2278 .filter = filter,
2279 };
2280
2281 /* Scaling factors used for bilinear filtering in multisample scaled
2282 * blits.
2283 */
2284 if (params.src.surf.samples == 16)
2285 wm_prog_key.x_scale = 4.0f;
2286 else
2287 wm_prog_key.x_scale = 2.0f;
2288 wm_prog_key.y_scale = params.src.surf.samples / wm_prog_key.x_scale;
2289
2290 params.wm_inputs.rect_grid.x1 =
2291 minify(params.src.surf.logical_level0_px.width, src_level) *
2292 wm_prog_key.x_scale - 1.0f;
2293 params.wm_inputs.rect_grid.y1 =
2294 minify(params.src.surf.logical_level0_px.height, src_level) *
2295 wm_prog_key.y_scale - 1.0f;
2296
2297 struct blt_coords coords = {
2298 .x = {
2299 .src0 = src_x0,
2300 .src1 = src_x1,
2301 .dst0 = dst_x0,
2302 .dst1 = dst_x1,
2303 .mirror = mirror_x
2304 },
2305 .y = {
2306 .src0 = src_y0,
2307 .src1 = src_y1,
2308 .dst0 = dst_y0,
2309 .dst1 = dst_y1,
2310 .mirror = mirror_y
2311 }
2312 };
2313
2314 do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2315 }
2316
2317 static enum isl_format
2318 get_copy_format_for_bpb(const struct isl_device *isl_dev, unsigned bpb)
2319 {
2320 /* The choice of UNORM and UINT formats is very intentional here. Most
2321 * of the time, we want to use a UINT format to avoid any rounding error
2322 * in the blit. For stencil blits, R8_UINT is required by the hardware.
2323 * (It's the only format allowed in conjunction with W-tiling.) Also we
2324 * intentionally use the 4-channel formats whenever we can. This is so
2325 * that, when we do a RGB <-> RGBX copy, the two formats will line up
2326 * even though one of them is 3/4 the size of the other. The choice of
2327 * UNORM vs. UINT is also very intentional because we don't have 8 or
2328 * 16-bit RGB UINT formats until Sky Lake so we have to use UNORM there.
2329 * Fortunately, the only time we should ever use two different formats in
2330 * the table below is for RGB -> RGBA blits and so we will never have any
2331 * UNORM/UINT mismatch.
2332 */
2333 if (ISL_DEV_GEN(isl_dev) >= 9) {
2334 switch (bpb) {
2335 case 8: return ISL_FORMAT_R8_UINT;
2336 case 16: return ISL_FORMAT_R8G8_UINT;
2337 case 24: return ISL_FORMAT_R8G8B8_UINT;
2338 case 32: return ISL_FORMAT_R8G8B8A8_UINT;
2339 case 48: return ISL_FORMAT_R16G16B16_UINT;
2340 case 64: return ISL_FORMAT_R16G16B16A16_UINT;
2341 case 96: return ISL_FORMAT_R32G32B32_UINT;
2342 case 128:return ISL_FORMAT_R32G32B32A32_UINT;
2343 default:
2344 unreachable("Unknown format bpb");
2345 }
2346 } else {
2347 switch (bpb) {
2348 case 8: return ISL_FORMAT_R8_UINT;
2349 case 16: return ISL_FORMAT_R8G8_UINT;
2350 case 24: return ISL_FORMAT_R8G8B8_UNORM;
2351 case 32: return ISL_FORMAT_R8G8B8A8_UNORM;
2352 case 48: return ISL_FORMAT_R16G16B16_UNORM;
2353 case 64: return ISL_FORMAT_R16G16B16A16_UNORM;
2354 case 96: return ISL_FORMAT_R32G32B32_UINT;
2355 case 128:return ISL_FORMAT_R32G32B32A32_UINT;
2356 default:
2357 unreachable("Unknown format bpb");
2358 }
2359 }
2360 }
2361
2362 /** Returns a UINT format that is CCS-compatible with the given format
2363 *
2364 * The PRM's say absolutely nothing about how render compression works. The
2365 * only thing they provide is a list of formats on which it is and is not
2366 * supported. Empirical testing indicates that the compression is only based
2367 * on the bit-layout of the format and the channel encoding doesn't matter.
2368 * So, while texture views don't work in general, you can create a view as
2369 * long as the bit-layout of the formats are the same.
2370 *
2371 * Fortunately, for every render compression capable format, the UINT format
2372 * with the same bit layout also supports render compression. This means that
2373 * we only need to handle UINT formats for copy operations. In order to do
2374 * copies between formats with different bit layouts, we attach both with a
2375 * UINT format and use bit_cast_color() to generate code to do the bit-cast
2376 * operation between the two bit layouts.
2377 */
2378 static enum isl_format
2379 get_ccs_compatible_uint_format(const struct isl_format_layout *fmtl)
2380 {
2381 switch (fmtl->format) {
2382 case ISL_FORMAT_R32G32B32A32_FLOAT:
2383 case ISL_FORMAT_R32G32B32A32_SINT:
2384 case ISL_FORMAT_R32G32B32A32_UINT:
2385 case ISL_FORMAT_R32G32B32A32_UNORM:
2386 case ISL_FORMAT_R32G32B32A32_SNORM:
2387 case ISL_FORMAT_R32G32B32X32_FLOAT:
2388 return ISL_FORMAT_R32G32B32A32_UINT;
2389
2390 case ISL_FORMAT_R16G16B16A16_UNORM:
2391 case ISL_FORMAT_R16G16B16A16_SNORM:
2392 case ISL_FORMAT_R16G16B16A16_SINT:
2393 case ISL_FORMAT_R16G16B16A16_UINT:
2394 case ISL_FORMAT_R16G16B16A16_FLOAT:
2395 case ISL_FORMAT_R16G16B16X16_UNORM:
2396 case ISL_FORMAT_R16G16B16X16_FLOAT:
2397 return ISL_FORMAT_R16G16B16A16_UINT;
2398
2399 case ISL_FORMAT_R32G32_FLOAT:
2400 case ISL_FORMAT_R32G32_SINT:
2401 case ISL_FORMAT_R32G32_UINT:
2402 case ISL_FORMAT_R32G32_UNORM:
2403 case ISL_FORMAT_R32G32_SNORM:
2404 return ISL_FORMAT_R32G32_UINT;
2405
2406 case ISL_FORMAT_B8G8R8A8_UNORM:
2407 case ISL_FORMAT_B8G8R8A8_UNORM_SRGB:
2408 case ISL_FORMAT_R8G8B8A8_UNORM:
2409 case ISL_FORMAT_R8G8B8A8_UNORM_SRGB:
2410 case ISL_FORMAT_R8G8B8A8_SNORM:
2411 case ISL_FORMAT_R8G8B8A8_SINT:
2412 case ISL_FORMAT_R8G8B8A8_UINT:
2413 case ISL_FORMAT_B8G8R8X8_UNORM:
2414 case ISL_FORMAT_B8G8R8X8_UNORM_SRGB:
2415 case ISL_FORMAT_R8G8B8X8_UNORM:
2416 case ISL_FORMAT_R8G8B8X8_UNORM_SRGB:
2417 return ISL_FORMAT_R8G8B8A8_UINT;
2418
2419 case ISL_FORMAT_R16G16_UNORM:
2420 case ISL_FORMAT_R16G16_SNORM:
2421 case ISL_FORMAT_R16G16_SINT:
2422 case ISL_FORMAT_R16G16_UINT:
2423 case ISL_FORMAT_R16G16_FLOAT:
2424 return ISL_FORMAT_R16G16_UINT;
2425
2426 case ISL_FORMAT_R32_SINT:
2427 case ISL_FORMAT_R32_UINT:
2428 case ISL_FORMAT_R32_FLOAT:
2429 case ISL_FORMAT_R32_UNORM:
2430 case ISL_FORMAT_R32_SNORM:
2431 return ISL_FORMAT_R32_UINT;
2432
2433 case ISL_FORMAT_B10G10R10A2_UNORM:
2434 case ISL_FORMAT_B10G10R10A2_UNORM_SRGB:
2435 case ISL_FORMAT_R10G10B10A2_UNORM:
2436 case ISL_FORMAT_R10G10B10A2_UINT:
2437 return ISL_FORMAT_R10G10B10A2_UINT;
2438
2439 default:
2440 unreachable("Not a compressible format");
2441 }
2442 }
2443
2444 void
2445 blorp_surf_convert_to_uncompressed(const struct isl_device *isl_dev,
2446 struct brw_blorp_surface_info *info,
2447 uint32_t *x, uint32_t *y,
2448 uint32_t *width, uint32_t *height)
2449 {
2450 const struct isl_format_layout *fmtl =
2451 isl_format_get_layout(info->surf.format);
2452
2453 assert(fmtl->bw > 1 || fmtl->bh > 1);
2454
2455 /* This is a compressed surface. We need to convert it to a single
2456 * slice (because compressed layouts don't perfectly match uncompressed
2457 * ones with the same bpb) and divide x, y, width, and height by the
2458 * block size.
2459 */
2460 blorp_surf_convert_to_single_slice(isl_dev, info);
2461
2462 if (width && height) {
2463 #ifndef NDEBUG
2464 uint32_t right_edge_px = info->tile_x_sa + *x + *width;
2465 uint32_t bottom_edge_px = info->tile_y_sa + *y + *height;
2466 assert(*width % fmtl->bw == 0 ||
2467 right_edge_px == info->surf.logical_level0_px.width);
2468 assert(*height % fmtl->bh == 0 ||
2469 bottom_edge_px == info->surf.logical_level0_px.height);
2470 #endif
2471 *width = DIV_ROUND_UP(*width, fmtl->bw);
2472 *height = DIV_ROUND_UP(*height, fmtl->bh);
2473 }
2474
2475 if (x && y) {
2476 assert(*x % fmtl->bw == 0);
2477 assert(*y % fmtl->bh == 0);
2478 *x /= fmtl->bw;
2479 *y /= fmtl->bh;
2480 }
2481
2482 info->surf.logical_level0_px.width =
2483 DIV_ROUND_UP(info->surf.logical_level0_px.width, fmtl->bw);
2484 info->surf.logical_level0_px.height =
2485 DIV_ROUND_UP(info->surf.logical_level0_px.height, fmtl->bh);
2486
2487 assert(info->surf.phys_level0_sa.width % fmtl->bw == 0);
2488 assert(info->surf.phys_level0_sa.height % fmtl->bh == 0);
2489 info->surf.phys_level0_sa.width /= fmtl->bw;
2490 info->surf.phys_level0_sa.height /= fmtl->bh;
2491
2492 assert(info->tile_x_sa % fmtl->bw == 0);
2493 assert(info->tile_y_sa % fmtl->bh == 0);
2494 info->tile_x_sa /= fmtl->bw;
2495 info->tile_y_sa /= fmtl->bh;
2496
2497 /* It's now an uncompressed surface so we need an uncompressed format */
2498 info->surf.format = get_copy_format_for_bpb(isl_dev, fmtl->bpb);
2499 }
2500
2501 void
2502 blorp_copy(struct blorp_batch *batch,
2503 const struct blorp_surf *src_surf,
2504 unsigned src_level, unsigned src_layer,
2505 const struct blorp_surf *dst_surf,
2506 unsigned dst_level, unsigned dst_layer,
2507 uint32_t src_x, uint32_t src_y,
2508 uint32_t dst_x, uint32_t dst_y,
2509 uint32_t src_width, uint32_t src_height)
2510 {
2511 const struct isl_device *isl_dev = batch->blorp->isl_dev;
2512 struct blorp_params params;
2513
2514 if (src_width == 0 || src_height == 0)
2515 return;
2516
2517 blorp_params_init(&params);
2518 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2519 src_layer, ISL_FORMAT_UNSUPPORTED, false);
2520 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2521 dst_layer, ISL_FORMAT_UNSUPPORTED, true);
2522
2523 struct brw_blorp_blit_prog_key wm_prog_key = {
2524 .shader_type = BLORP_SHADER_TYPE_BLIT,
2525 .filter = BLORP_FILTER_NONE,
2526 .need_src_offset = src_surf->tile_x_sa || src_surf->tile_y_sa,
2527 .need_dst_offset = dst_surf->tile_x_sa || dst_surf->tile_y_sa,
2528 };
2529
2530 const struct isl_format_layout *src_fmtl =
2531 isl_format_get_layout(params.src.surf.format);
2532 const struct isl_format_layout *dst_fmtl =
2533 isl_format_get_layout(params.dst.surf.format);
2534
2535 assert(params.src.aux_usage == ISL_AUX_USAGE_NONE ||
2536 params.src.aux_usage == ISL_AUX_USAGE_MCS ||
2537 params.src.aux_usage == ISL_AUX_USAGE_CCS_E);
2538 assert(params.dst.aux_usage == ISL_AUX_USAGE_NONE ||
2539 params.dst.aux_usage == ISL_AUX_USAGE_MCS ||
2540 params.dst.aux_usage == ISL_AUX_USAGE_CCS_E);
2541
2542 if (params.dst.aux_usage == ISL_AUX_USAGE_CCS_E) {
2543 params.dst.view.format = get_ccs_compatible_uint_format(dst_fmtl);
2544 if (params.src.aux_usage == ISL_AUX_USAGE_CCS_E) {
2545 params.src.view.format = get_ccs_compatible_uint_format(src_fmtl);
2546 } else if (src_fmtl->bpb == dst_fmtl->bpb) {
2547 params.src.view.format = params.dst.view.format;
2548 } else {
2549 params.src.view.format =
2550 get_copy_format_for_bpb(isl_dev, src_fmtl->bpb);
2551 }
2552 } else if (params.src.aux_usage == ISL_AUX_USAGE_CCS_E) {
2553 params.src.view.format = get_ccs_compatible_uint_format(src_fmtl);
2554 if (src_fmtl->bpb == dst_fmtl->bpb) {
2555 params.dst.view.format = params.src.view.format;
2556 } else {
2557 params.dst.view.format =
2558 get_copy_format_for_bpb(isl_dev, dst_fmtl->bpb);
2559 }
2560 } else {
2561 params.dst.view.format = get_copy_format_for_bpb(isl_dev, dst_fmtl->bpb);
2562 params.src.view.format = get_copy_format_for_bpb(isl_dev, src_fmtl->bpb);
2563 }
2564
2565 if (params.src.aux_usage == ISL_AUX_USAGE_CCS_E) {
2566 /* It's safe to do a blorp_copy between things which are sRGB with CCS_E
2567 * enabled even though CCS_E doesn't technically do sRGB on SKL because
2568 * we stomp everything to UINT anyway. The one thing we have to be
2569 * careful of is clear colors. Because fast clear colors for sRGB on
2570 * gen9 are encoded as the float values between format conversion and
2571 * sRGB curve application, a given clear color float will convert to the
2572 * same bits regardless of whether the format is UNORM or sRGB.
2573 * Therefore, we can handle sRGB without any special cases.
2574 */
2575 UNUSED enum isl_format linear_src_format =
2576 isl_format_srgb_to_linear(src_surf->surf->format);
2577 assert(isl_formats_are_ccs_e_compatible(batch->blorp->isl_dev->info,
2578 linear_src_format,
2579 params.src.view.format));
2580 uint32_t packed[4];
2581 isl_color_value_pack(&params.src.clear_color,
2582 linear_src_format, packed);
2583 isl_color_value_unpack(&params.src.clear_color,
2584 params.src.view.format, packed);
2585 }
2586
2587 if (params.dst.aux_usage == ISL_AUX_USAGE_CCS_E) {
2588 /* See above where we handle linear_src_format */
2589 UNUSED enum isl_format linear_dst_format =
2590 isl_format_srgb_to_linear(dst_surf->surf->format);
2591 assert(isl_formats_are_ccs_e_compatible(batch->blorp->isl_dev->info,
2592 linear_dst_format,
2593 params.dst.view.format));
2594 uint32_t packed[4];
2595 isl_color_value_pack(&params.dst.clear_color,
2596 linear_dst_format, packed);
2597 isl_color_value_unpack(&params.dst.clear_color,
2598 params.dst.view.format, packed);
2599 }
2600
2601 if (params.src.view.format != params.dst.view.format) {
2602 enum isl_format src_cast_format = params.src.view.format;
2603 enum isl_format dst_cast_format = params.dst.view.format;
2604
2605 /* The BLORP bitcast code gets confused by RGB formats. Just treat them
2606 * as RGBA and then everything will be happy. This is perfectly safe
2607 * because BLORP likes to treat things as if they have vec4 colors all
2608 * the time anyway.
2609 */
2610 if (isl_format_is_rgb(src_cast_format))
2611 src_cast_format = isl_format_rgb_to_rgba(src_cast_format);
2612 if (isl_format_is_rgb(dst_cast_format))
2613 dst_cast_format = isl_format_rgb_to_rgba(dst_cast_format);
2614
2615 if (src_cast_format != dst_cast_format) {
2616 wm_prog_key.format_bit_cast = true;
2617 wm_prog_key.src_format = src_cast_format;
2618 wm_prog_key.dst_format = dst_cast_format;
2619 }
2620 }
2621
2622 if (src_fmtl->bw > 1 || src_fmtl->bh > 1) {
2623 blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.src,
2624 &src_x, &src_y,
2625 &src_width, &src_height);
2626 wm_prog_key.need_src_offset = true;
2627 }
2628
2629 if (dst_fmtl->bw > 1 || dst_fmtl->bh > 1) {
2630 blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
2631 &dst_x, &dst_y, NULL, NULL);
2632 wm_prog_key.need_dst_offset = true;
2633 }
2634
2635 /* Once both surfaces are stompped to uncompressed as needed, the
2636 * destination size is the same as the source size.
2637 */
2638 uint32_t dst_width = src_width;
2639 uint32_t dst_height = src_height;
2640
2641 struct blt_coords coords = {
2642 .x = {
2643 .src0 = src_x,
2644 .src1 = src_x + src_width,
2645 .dst0 = dst_x,
2646 .dst1 = dst_x + dst_width,
2647 .mirror = false
2648 },
2649 .y = {
2650 .src0 = src_y,
2651 .src1 = src_y + src_height,
2652 .dst0 = dst_y,
2653 .dst1 = dst_y + dst_height,
2654 .mirror = false
2655 }
2656 };
2657
2658 do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2659 }
2660
2661 static enum isl_format
2662 isl_format_for_size(unsigned size_B)
2663 {
2664 switch (size_B) {
2665 case 1: return ISL_FORMAT_R8_UINT;
2666 case 2: return ISL_FORMAT_R8G8_UINT;
2667 case 4: return ISL_FORMAT_R8G8B8A8_UINT;
2668 case 8: return ISL_FORMAT_R16G16B16A16_UINT;
2669 case 16: return ISL_FORMAT_R32G32B32A32_UINT;
2670 default:
2671 unreachable("Not a power-of-two format size");
2672 }
2673 }
2674
2675 /**
2676 * Returns the greatest common divisor of a and b that is a power of two.
2677 */
2678 static uint64_t
2679 gcd_pow2_u64(uint64_t a, uint64_t b)
2680 {
2681 assert(a > 0 || b > 0);
2682
2683 unsigned a_log2 = ffsll(a) - 1;
2684 unsigned b_log2 = ffsll(b) - 1;
2685
2686 /* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
2687 * case, the MIN2() will take the other one. If both are 0 then we will
2688 * hit the assert above.
2689 */
2690 return 1 << MIN2(a_log2, b_log2);
2691 }
2692
2693 static void
2694 do_buffer_copy(struct blorp_batch *batch,
2695 struct blorp_address *src,
2696 struct blorp_address *dst,
2697 int width, int height, int block_size)
2698 {
2699 /* The actual format we pick doesn't matter as blorp will throw it away.
2700 * The only thing that actually matters is the size.
2701 */
2702 enum isl_format format = isl_format_for_size(block_size);
2703
2704 UNUSED bool ok;
2705 struct isl_surf surf;
2706 ok = isl_surf_init(batch->blorp->isl_dev, &surf,
2707 .dim = ISL_SURF_DIM_2D,
2708 .format = format,
2709 .width = width,
2710 .height = height,
2711 .depth = 1,
2712 .levels = 1,
2713 .array_len = 1,
2714 .samples = 1,
2715 .row_pitch_B = width * block_size,
2716 .usage = ISL_SURF_USAGE_TEXTURE_BIT |
2717 ISL_SURF_USAGE_RENDER_TARGET_BIT,
2718 .tiling_flags = ISL_TILING_LINEAR_BIT);
2719 assert(ok);
2720
2721 struct blorp_surf src_blorp_surf = {
2722 .surf = &surf,
2723 .addr = *src,
2724 };
2725
2726 struct blorp_surf dst_blorp_surf = {
2727 .surf = &surf,
2728 .addr = *dst,
2729 };
2730
2731 blorp_copy(batch, &src_blorp_surf, 0, 0, &dst_blorp_surf, 0, 0,
2732 0, 0, 0, 0, width, height);
2733 }
2734
2735 void
2736 blorp_buffer_copy(struct blorp_batch *batch,
2737 struct blorp_address src,
2738 struct blorp_address dst,
2739 uint64_t size)
2740 {
2741 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
2742 uint64_t copy_size = size;
2743
2744 /* This is maximum possible width/height our HW can handle */
2745 uint64_t max_surface_dim = 1 << (devinfo->gen >= 7 ? 14 : 13);
2746
2747 /* First, we compute the biggest format that can be used with the
2748 * given offsets and size.
2749 */
2750 int bs = 16;
2751 bs = gcd_pow2_u64(bs, src.offset);
2752 bs = gcd_pow2_u64(bs, dst.offset);
2753 bs = gcd_pow2_u64(bs, size);
2754
2755 /* First, we make a bunch of max-sized copies */
2756 uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
2757 while (copy_size >= max_copy_size) {
2758 do_buffer_copy(batch, &src, &dst, max_surface_dim, max_surface_dim, bs);
2759 copy_size -= max_copy_size;
2760 src.offset += max_copy_size;
2761 dst.offset += max_copy_size;
2762 }
2763
2764 /* Now make a max-width copy */
2765 uint64_t height = copy_size / (max_surface_dim * bs);
2766 assert(height < max_surface_dim);
2767 if (height != 0) {
2768 uint64_t rect_copy_size = height * max_surface_dim * bs;
2769 do_buffer_copy(batch, &src, &dst, max_surface_dim, height, bs);
2770 copy_size -= rect_copy_size;
2771 src.offset += rect_copy_size;
2772 dst.offset += rect_copy_size;
2773 }
2774
2775 /* Finally, make a small copy to finish it off */
2776 if (copy_size != 0) {
2777 do_buffer_copy(batch, &src, &dst, copy_size / bs, 1, bs);
2778 }
2779 }