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