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