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