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