intel/blorp: remove stale comment
[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 = nir_variable_create(b->shader, nir_var_shader_in, \
71 type, #name); \
72 v->v_##name->data.interpolation = INTERP_MODE_FLAT; \
73 v->v_##name->data.location = VARYING_SLOT_VAR0 + \
74 offsetof(struct brw_blorp_wm_inputs, name) / (4 * sizeof(float)); \
75 v->v_##name->data.location_frac = \
76 (offsetof(struct brw_blorp_wm_inputs, name) / sizeof(float)) % 4;
77
78 LOAD_INPUT(discard_rect, glsl_vec4_type())
79 LOAD_INPUT(rect_grid, glsl_vec4_type())
80 LOAD_INPUT(coord_transform, glsl_vec4_type())
81 LOAD_INPUT(src_z, glsl_uint_type())
82 LOAD_INPUT(src_offset, glsl_vector_type(GLSL_TYPE_UINT, 2))
83 LOAD_INPUT(dst_offset, glsl_vector_type(GLSL_TYPE_UINT, 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_f2i(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_ffma(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 nir_ssa_def *pos, nir_alu_type dst_type)
204 {
205 nir_tex_instr *tex =
206 blorp_create_nir_tex_instr(b, v, nir_texop_tex, pos, 2, dst_type);
207
208 assert(pos->num_components == 2);
209 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
210 tex->src[1].src_type = nir_tex_src_lod;
211 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
212
213 nir_builder_instr_insert(b, &tex->instr);
214
215 return &tex->dest.ssa;
216 }
217
218 static nir_ssa_def *
219 blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
220 nir_ssa_def *pos, nir_alu_type dst_type)
221 {
222 nir_tex_instr *tex =
223 blorp_create_nir_tex_instr(b, v, nir_texop_txf, pos, 2, dst_type);
224
225 tex->sampler_dim = GLSL_SAMPLER_DIM_3D;
226 tex->src[1].src_type = nir_tex_src_lod;
227 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
228
229 nir_builder_instr_insert(b, &tex->instr);
230
231 return &tex->dest.ssa;
232 }
233
234 static nir_ssa_def *
235 blorp_nir_txf_ms(nir_builder *b, struct brw_blorp_blit_vars *v,
236 nir_ssa_def *pos, nir_ssa_def *mcs, nir_alu_type dst_type)
237 {
238 nir_tex_instr *tex =
239 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms, pos,
240 mcs != NULL ? 3 : 2, dst_type);
241
242 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
243
244 tex->src[1].src_type = nir_tex_src_ms_index;
245 if (pos->num_components == 2) {
246 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
247 } else {
248 assert(pos->num_components == 3);
249 tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
250 }
251
252 if (mcs) {
253 tex->src[2].src_type = nir_tex_src_ms_mcs;
254 tex->src[2].src = nir_src_for_ssa(mcs);
255 }
256
257 nir_builder_instr_insert(b, &tex->instr);
258
259 return &tex->dest.ssa;
260 }
261
262 static nir_ssa_def *
263 blorp_nir_txf_ms_mcs(nir_builder *b, struct brw_blorp_blit_vars *v, nir_ssa_def *pos)
264 {
265 nir_tex_instr *tex =
266 blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms_mcs,
267 pos, 1, nir_type_int);
268
269 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
270
271 nir_builder_instr_insert(b, &tex->instr);
272
273 return &tex->dest.ssa;
274 }
275
276 static nir_ssa_def *
277 nir_mask_shift_or(struct nir_builder *b, nir_ssa_def *dst, nir_ssa_def *src,
278 uint32_t src_mask, int src_left_shift)
279 {
280 nir_ssa_def *masked = nir_iand(b, src, nir_imm_int(b, src_mask));
281
282 nir_ssa_def *shifted;
283 if (src_left_shift > 0) {
284 shifted = nir_ishl(b, masked, nir_imm_int(b, src_left_shift));
285 } else if (src_left_shift < 0) {
286 shifted = nir_ushr(b, masked, nir_imm_int(b, -src_left_shift));
287 } else {
288 assert(src_left_shift == 0);
289 shifted = masked;
290 }
291
292 return nir_ior(b, dst, shifted);
293 }
294
295 /**
296 * Emit code to compensate for the difference between Y and W tiling.
297 *
298 * This code modifies the X and Y coordinates according to the formula:
299 *
300 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
301 *
302 * (See brw_blorp_build_nir_shader).
303 */
304 static inline nir_ssa_def *
305 blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
306 {
307 assert(pos->num_components == 2);
308 nir_ssa_def *x_Y = nir_channel(b, pos, 0);
309 nir_ssa_def *y_Y = nir_channel(b, pos, 1);
310
311 /* Given X and Y coordinates that describe an address using Y tiling,
312 * translate to the X and Y coordinates that describe the same address
313 * using W tiling.
314 *
315 * If we break down the low order bits of X and Y, using a
316 * single letter to represent each low-order bit:
317 *
318 * X = A << 7 | 0bBCDEFGH
319 * Y = J << 5 | 0bKLMNP (1)
320 *
321 * Then we can apply the Y tiling formula to see the memory offset being
322 * addressed:
323 *
324 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
325 *
326 * If we apply the W detiling formula to this memory location, that the
327 * corresponding X' and Y' coordinates are:
328 *
329 * X' = A << 6 | 0bBCDPFH (3)
330 * Y' = J << 6 | 0bKLMNEG
331 *
332 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
333 * we need to make the following computation:
334 *
335 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
336 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
337 */
338 nir_ssa_def *x_W = nir_imm_int(b, 0);
339 x_W = nir_mask_shift_or(b, x_W, x_Y, 0xfffffff4, -1);
340 x_W = nir_mask_shift_or(b, x_W, y_Y, 0x1, 2);
341 x_W = nir_mask_shift_or(b, x_W, x_Y, 0x1, 0);
342
343 nir_ssa_def *y_W = nir_imm_int(b, 0);
344 y_W = nir_mask_shift_or(b, y_W, y_Y, 0xfffffffe, 1);
345 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x8, -2);
346 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x2, -1);
347
348 return nir_vec2(b, x_W, y_W);
349 }
350
351 /**
352 * Emit code to compensate for the difference between Y and W tiling.
353 *
354 * This code modifies the X and Y coordinates according to the formula:
355 *
356 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
357 *
358 * (See brw_blorp_build_nir_shader).
359 */
360 static inline nir_ssa_def *
361 blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
362 {
363 assert(pos->num_components == 2);
364 nir_ssa_def *x_W = nir_channel(b, pos, 0);
365 nir_ssa_def *y_W = nir_channel(b, pos, 1);
366
367 /* Applying the same logic as above, but in reverse, we obtain the
368 * formulas:
369 *
370 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
371 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
372 */
373 nir_ssa_def *x_Y = nir_imm_int(b, 0);
374 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0xfffffffa, 1);
375 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x2, 2);
376 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x1, 1);
377 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0x1, 0);
378
379 nir_ssa_def *y_Y = nir_imm_int(b, 0);
380 y_Y = nir_mask_shift_or(b, y_Y, y_W, 0xfffffffc, -1);
381 y_Y = nir_mask_shift_or(b, y_Y, x_W, 0x4, -2);
382
383 return nir_vec2(b, x_Y, y_Y);
384 }
385
386 /**
387 * Emit code to compensate for the difference between MSAA and non-MSAA
388 * surfaces.
389 *
390 * This code modifies the X and Y coordinates according to the formula:
391 *
392 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
393 *
394 * (See brw_blorp_blit_program).
395 */
396 static inline nir_ssa_def *
397 blorp_nir_encode_msaa(nir_builder *b, nir_ssa_def *pos,
398 unsigned num_samples, enum isl_msaa_layout layout)
399 {
400 assert(pos->num_components == 2 || pos->num_components == 3);
401
402 switch (layout) {
403 case ISL_MSAA_LAYOUT_NONE:
404 assert(pos->num_components == 2);
405 return pos;
406 case ISL_MSAA_LAYOUT_ARRAY:
407 /* No translation needed */
408 return pos;
409 case ISL_MSAA_LAYOUT_INTERLEAVED: {
410 nir_ssa_def *x_in = nir_channel(b, pos, 0);
411 nir_ssa_def *y_in = nir_channel(b, pos, 1);
412 nir_ssa_def *s_in = pos->num_components == 2 ? nir_imm_int(b, 0) :
413 nir_channel(b, pos, 2);
414
415 nir_ssa_def *x_out = nir_imm_int(b, 0);
416 nir_ssa_def *y_out = nir_imm_int(b, 0);
417 switch (num_samples) {
418 case 2:
419 case 4:
420 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
421 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
422 * Y' = Y
423 *
424 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
425 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
426 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
427 */
428 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 1);
429 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
430 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
431 if (num_samples == 2) {
432 y_out = y_in;
433 } else {
434 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
435 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
436 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
437 }
438 break;
439
440 case 8:
441 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
442 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
443 * | (X & 0b1)
444 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
445 */
446 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
447 x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
448 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
449 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
450 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
451 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
452 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
453 break;
454
455 case 16:
456 /* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
457 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
458 * | (X & 0b1)
459 * Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
460 * | (Y & 0b1)
461 */
462 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
463 x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
464 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
465 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
466 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 2);
467 y_out = nir_mask_shift_or(b, y_out, s_in, 0x8, -1);
468 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
469 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
470 break;
471
472 default:
473 unreachable("Invalid number of samples for IMS layout");
474 }
475
476 return nir_vec2(b, x_out, y_out);
477 }
478
479 default:
480 unreachable("Invalid MSAA layout");
481 }
482 }
483
484 /**
485 * Emit code to compensate for the difference between MSAA and non-MSAA
486 * surfaces.
487 *
488 * This code modifies the X and Y coordinates according to the formula:
489 *
490 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
491 *
492 * (See brw_blorp_blit_program).
493 */
494 static inline nir_ssa_def *
495 blorp_nir_decode_msaa(nir_builder *b, nir_ssa_def *pos,
496 unsigned num_samples, enum isl_msaa_layout layout)
497 {
498 assert(pos->num_components == 2 || pos->num_components == 3);
499
500 switch (layout) {
501 case ISL_MSAA_LAYOUT_NONE:
502 /* No translation necessary, and S should already be zero. */
503 assert(pos->num_components == 2);
504 return pos;
505 case ISL_MSAA_LAYOUT_ARRAY:
506 /* No translation necessary. */
507 return pos;
508 case ISL_MSAA_LAYOUT_INTERLEAVED: {
509 assert(pos->num_components == 2);
510
511 nir_ssa_def *x_in = nir_channel(b, pos, 0);
512 nir_ssa_def *y_in = nir_channel(b, pos, 1);
513
514 nir_ssa_def *x_out = nir_imm_int(b, 0);
515 nir_ssa_def *y_out = nir_imm_int(b, 0);
516 nir_ssa_def *s_out = nir_imm_int(b, 0);
517 switch (num_samples) {
518 case 2:
519 case 4:
520 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
521 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
522 * S = (X & 0b10) >> 1
523 *
524 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
525 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
526 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
527 * S = (Y & 0b10) | (X & 0b10) >> 1
528 */
529 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffc, -1);
530 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
531 if (num_samples == 2) {
532 y_out = y_in;
533 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
534 } else {
535 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
536 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
537 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
538 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
539 }
540 break;
541
542 case 8:
543 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
544 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
545 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
546 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
547 */
548 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
549 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
550 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
551 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
552 s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
553 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
554 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
555 break;
556
557 case 16:
558 /* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
559 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
560 * Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
561 * S = (Y & 0b100) << 1 | (X & 0b100) |
562 * (Y & 0b10) | (X & 0b10) >> 1
563 */
564 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
565 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
566 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffff8, -2);
567 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
568 s_out = nir_mask_shift_or(b, s_out, y_in, 0x4, 1);
569 s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
570 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
571 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
572 break;
573
574 default:
575 unreachable("Invalid number of samples for IMS layout");
576 }
577
578 return nir_vec3(b, x_out, y_out, s_out);
579 }
580
581 default:
582 unreachable("Invalid MSAA layout");
583 }
584 }
585
586 /**
587 * Count the number of trailing 1 bits in the given value. For example:
588 *
589 * count_trailing_one_bits(0) == 0
590 * count_trailing_one_bits(7) == 3
591 * count_trailing_one_bits(11) == 2
592 */
593 static inline int count_trailing_one_bits(unsigned value)
594 {
595 #ifdef HAVE___BUILTIN_CTZ
596 return __builtin_ctz(~value);
597 #else
598 return _mesa_bitcount(value & ~(value + 1));
599 #endif
600 }
601
602 static nir_ssa_def *
603 blorp_nir_manual_blend_average(nir_builder *b, struct brw_blorp_blit_vars *v,
604 nir_ssa_def *pos, unsigned tex_samples,
605 enum isl_aux_usage tex_aux_usage,
606 nir_alu_type dst_type)
607 {
608 /* If non-null, this is the outer-most if statement */
609 nir_if *outer_if = NULL;
610
611 nir_variable *color =
612 nir_local_variable_create(b->impl, glsl_vec4_type(), "color");
613
614 nir_ssa_def *mcs = NULL;
615 if (tex_aux_usage == ISL_AUX_USAGE_MCS)
616 mcs = blorp_nir_txf_ms_mcs(b, v, pos);
617
618 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
619 *
620 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
621 *
622 * This ensures that when all samples have the same value, no numerical
623 * precision is lost, since each addition operation always adds two equal
624 * values, and summing two equal floating point values does not lose
625 * precision.
626 *
627 * We perform this computation by treating the texture_data array as a
628 * stack and performing the following operations:
629 *
630 * - push sample 0 onto stack
631 * - push sample 1 onto stack
632 * - add top two stack entries
633 * - push sample 2 onto stack
634 * - push sample 3 onto stack
635 * - add top two stack entries
636 * - add top two stack entries
637 * - divide top stack entry by 4
638 *
639 * Note that after pushing sample i onto the stack, the number of add
640 * operations we do is equal to the number of trailing 1 bits in i. This
641 * works provided the total number of samples is a power of two, which it
642 * always is for i965.
643 *
644 * For integer formats, we replace the add operations with average
645 * operations and skip the final division.
646 */
647 nir_ssa_def *texture_data[5];
648 unsigned stack_depth = 0;
649 for (unsigned i = 0; i < tex_samples; ++i) {
650 assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
651
652 /* Push sample i onto the stack */
653 assert(stack_depth < ARRAY_SIZE(texture_data));
654
655 nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
656 nir_channel(b, pos, 1),
657 nir_imm_int(b, i));
658 texture_data[stack_depth++] = blorp_nir_txf_ms(b, v, ms_pos, mcs, dst_type);
659
660 if (i == 0 && tex_aux_usage == ISL_AUX_USAGE_MCS) {
661 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
662 * suggests an optimization:
663 *
664 * "A simple optimization with probable large return in
665 * performance is to compare the MCS value to zero (indicating
666 * all samples are on sample slice 0), and sample only from
667 * sample slice 0 using ld2dss if MCS is zero."
668 *
669 * Note that in the case where the MCS value is zero, sampling from
670 * sample slice 0 using ld2dss and sampling from sample 0 using
671 * ld2dms are equivalent (since all samples are on sample slice 0).
672 * Since we have already sampled from sample 0, all we need to do is
673 * skip the remaining fetches and averaging if MCS is zero.
674 */
675 nir_ssa_def *mcs_zero =
676 nir_ieq(b, nir_channel(b, mcs, 0), nir_imm_int(b, 0));
677 if (tex_samples == 16) {
678 mcs_zero = nir_iand(b, mcs_zero,
679 nir_ieq(b, nir_channel(b, mcs, 1), nir_imm_int(b, 0)));
680 }
681
682 nir_if *if_stmt = nir_if_create(b->shader);
683 if_stmt->condition = nir_src_for_ssa(mcs_zero);
684 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
685
686 b->cursor = nir_after_cf_list(&if_stmt->then_list);
687 nir_store_var(b, color, texture_data[0], 0xf);
688
689 b->cursor = nir_after_cf_list(&if_stmt->else_list);
690 outer_if = if_stmt;
691 }
692
693 for (int j = 0; j < count_trailing_one_bits(i); j++) {
694 assert(stack_depth >= 2);
695 --stack_depth;
696
697 assert(dst_type == nir_type_float);
698 texture_data[stack_depth - 1] =
699 nir_fadd(b, texture_data[stack_depth - 1],
700 texture_data[stack_depth]);
701 }
702 }
703
704 /* We should have just 1 sample on the stack now. */
705 assert(stack_depth == 1);
706
707 texture_data[0] = nir_fmul(b, texture_data[0],
708 nir_imm_float(b, 1.0 / tex_samples));
709
710 nir_store_var(b, color, texture_data[0], 0xf);
711
712 if (outer_if)
713 b->cursor = nir_after_cf_node(&outer_if->cf_node);
714
715 return nir_load_var(b, color);
716 }
717
718 static inline nir_ssa_def *
719 nir_imm_vec2(nir_builder *build, float x, float y)
720 {
721 nir_const_value v;
722
723 memset(&v, 0, sizeof(v));
724 v.f32[0] = x;
725 v.f32[1] = y;
726
727 return nir_build_imm(build, 4, 32, v);
728 }
729
730 static nir_ssa_def *
731 blorp_nir_manual_blend_bilinear(nir_builder *b, nir_ssa_def *pos,
732 unsigned tex_samples,
733 const struct brw_blorp_blit_prog_key *key,
734 struct brw_blorp_blit_vars *v)
735 {
736 nir_ssa_def *pos_xy = nir_channels(b, pos, 0x3);
737 nir_ssa_def *rect_grid = nir_load_var(b, v->v_rect_grid);
738 nir_ssa_def *scale = nir_imm_vec2(b, key->x_scale, key->y_scale);
739
740 /* Translate coordinates to lay out the samples in a rectangular grid
741 * roughly corresponding to sample locations.
742 */
743 pos_xy = nir_fmul(b, pos_xy, scale);
744 /* Adjust coordinates so that integers represent pixel centers rather
745 * than pixel edges.
746 */
747 pos_xy = nir_fadd(b, pos_xy, nir_imm_float(b, -0.5));
748 /* Clamp the X, Y texture coordinates to properly handle the sampling of
749 * texels on texture edges.
750 */
751 pos_xy = nir_fmin(b, nir_fmax(b, pos_xy, nir_imm_float(b, 0.0)),
752 nir_vec2(b, nir_channel(b, rect_grid, 0),
753 nir_channel(b, rect_grid, 1)));
754
755 /* Store the fractional parts to be used as bilinear interpolation
756 * coefficients.
757 */
758 nir_ssa_def *frac_xy = nir_ffract(b, pos_xy);
759 /* Round the float coordinates down to nearest integer */
760 pos_xy = nir_fdiv(b, nir_ftrunc(b, pos_xy), scale);
761
762 nir_ssa_def *tex_data[4];
763 for (unsigned i = 0; i < 4; ++i) {
764 float sample_off_x = (float)(i & 0x1) / key->x_scale;
765 float sample_off_y = (float)((i >> 1) & 0x1) / key->y_scale;
766 nir_ssa_def *sample_off = nir_imm_vec2(b, sample_off_x, sample_off_y);
767
768 nir_ssa_def *sample_coords = nir_fadd(b, pos_xy, sample_off);
769 nir_ssa_def *sample_coords_int = nir_f2i(b, sample_coords);
770
771 /* The MCS value we fetch has to match up with the pixel that we're
772 * sampling from. Since we sample from different pixels in each
773 * iteration of this "for" loop, the call to mcs_fetch() should be
774 * here inside the loop after computing the pixel coordinates.
775 */
776 nir_ssa_def *mcs = NULL;
777 if (key->tex_aux_usage == ISL_AUX_USAGE_MCS)
778 mcs = blorp_nir_txf_ms_mcs(b, v, sample_coords_int);
779
780 /* Compute sample index and map the sample index to a sample number.
781 * Sample index layout shows the numbering of slots in a rectangular
782 * grid of samples with in a pixel. Sample number layout shows the
783 * rectangular grid of samples roughly corresponding to the real sample
784 * locations with in a pixel.
785 * In case of 4x MSAA, layout of sample indices matches the layout of
786 * sample numbers:
787 * ---------
788 * | 0 | 1 |
789 * ---------
790 * | 2 | 3 |
791 * ---------
792 *
793 * In case of 8x MSAA the two layouts don't match.
794 * sample index layout : --------- sample number layout : ---------
795 * | 0 | 1 | | 3 | 7 |
796 * --------- ---------
797 * | 2 | 3 | | 5 | 0 |
798 * --------- ---------
799 * | 4 | 5 | | 1 | 2 |
800 * --------- ---------
801 * | 6 | 7 | | 4 | 6 |
802 * --------- ---------
803 *
804 * Fortunately, this can be done fairly easily as:
805 * S' = (0x17306425 >> (S * 4)) & 0xf
806 *
807 * In the case of 16x MSAA the two layouts don't match.
808 * Sample index layout: Sample number layout:
809 * --------------------- ---------------------
810 * | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
811 * --------------------- ---------------------
812 * | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
813 * --------------------- ---------------------
814 * | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
815 * --------------------- ---------------------
816 * | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
817 * --------------------- ---------------------
818 *
819 * This is equivalent to
820 * S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
821 */
822 nir_ssa_def *frac = nir_ffract(b, sample_coords);
823 nir_ssa_def *sample =
824 nir_fdot2(b, frac, nir_imm_vec2(b, key->x_scale,
825 key->x_scale * key->y_scale));
826 sample = nir_f2i(b, sample);
827
828 if (tex_samples == 8) {
829 sample = nir_iand(b, nir_ishr(b, nir_imm_int(b, 0x64210573),
830 nir_ishl(b, sample, nir_imm_int(b, 2))),
831 nir_imm_int(b, 0xf));
832 } else if (tex_samples == 16) {
833 nir_ssa_def *sample_low =
834 nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xd31479af),
835 nir_ishl(b, sample, nir_imm_int(b, 2))),
836 nir_imm_int(b, 0xf));
837 nir_ssa_def *sample_high =
838 nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xe58b602c),
839 nir_ishl(b, nir_iadd(b, sample,
840 nir_imm_int(b, -8)),
841 nir_imm_int(b, 2))),
842 nir_imm_int(b, 0xf));
843
844 sample = nir_bcsel(b, nir_ilt(b, sample, nir_imm_int(b, 8)),
845 sample_low, sample_high);
846 }
847 nir_ssa_def *pos_ms = nir_vec3(b, nir_channel(b, sample_coords_int, 0),
848 nir_channel(b, sample_coords_int, 1),
849 sample);
850 tex_data[i] = blorp_nir_txf_ms(b, v, pos_ms, mcs, key->texture_data_type);
851 }
852
853 nir_ssa_def *frac_x = nir_channel(b, frac_xy, 0);
854 nir_ssa_def *frac_y = nir_channel(b, frac_xy, 1);
855 return nir_flrp(b, nir_flrp(b, tex_data[0], tex_data[1], frac_x),
856 nir_flrp(b, tex_data[2], tex_data[3], frac_x),
857 frac_y);
858 }
859
860 /**
861 * Generator for WM programs used in BLORP blits.
862 *
863 * The bulk of the work done by the WM program is to wrap and unwrap the
864 * coordinate transformations used by the hardware to store surfaces in
865 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
866 * sample index for a multisampled surface) to a memory offset by the
867 * following formulas:
868 *
869 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
870 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
871 *
872 * For a single-sampled surface, or for a multisampled surface using
873 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
874 * function:
875 *
876 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
877 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
878 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
879 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
880 *
881 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
882 * embeds the sample number into bit 1 of the X and Y coordinates:
883 *
884 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
885 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
886 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
887 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
888 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
889 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
890 * S = (Y & 0b10) | (X & 0b10) >> 1
891 *
892 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
893 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
894 * the Y coordinate:
895 *
896 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
897 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
898 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
899 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
900 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
901 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
902 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
903 *
904 * For X tiling, tile() combines together the low-order bits of the X and Y
905 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
906 * bytes wide and 8 rows high:
907 *
908 * tile(x_tiled, X, Y, S) = A
909 * where A = tile_num << 12 | offset
910 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
911 * offset = (Y' & 0b111) << 9
912 * | (X & 0b111111111)
913 * X' = X * cpp
914 * Y' = Y + S * qpitch
915 * detile(x_tiled, A) = (X, Y, S)
916 * where X = X' / cpp
917 * Y = Y' % qpitch
918 * S = Y' / qpitch
919 * Y' = (tile_num / tile_pitch) << 3
920 * | (A & 0b111000000000) >> 9
921 * X' = (tile_num % tile_pitch) << 9
922 * | (A & 0b111111111)
923 *
924 * (In all tiling formulas, cpp is the number of bytes occupied by a single
925 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
926 * to fill the width of the surface, and qpitch is the spacing (in rows)
927 * between array slices).
928 *
929 * For Y tiling, tile() combines together the low-order bits of the X and Y
930 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
931 * bytes wide and 32 rows high:
932 *
933 * tile(y_tiled, X, Y, S) = A
934 * where A = tile_num << 12 | offset
935 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
936 * offset = (X' & 0b1110000) << 5
937 * | (Y' & 0b11111) << 4
938 * | (X' & 0b1111)
939 * X' = X * cpp
940 * Y' = Y + S * qpitch
941 * detile(y_tiled, A) = (X, Y, S)
942 * where X = X' / cpp
943 * Y = Y' % qpitch
944 * S = Y' / qpitch
945 * Y' = (tile_num / tile_pitch) << 5
946 * | (A & 0b111110000) >> 4
947 * X' = (tile_num % tile_pitch) << 7
948 * | (A & 0b111000000000) >> 5
949 * | (A & 0b1111)
950 *
951 * For W tiling, tile() combines together the low-order bits of the X and Y
952 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
953 * bytes wide and 64 rows high (note that W tiling is only used for stencil
954 * buffers, which always have cpp = 1 and S=0):
955 *
956 * tile(w_tiled, X, Y, S) = A
957 * where A = tile_num << 12 | offset
958 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
959 * offset = (X' & 0b111000) << 6
960 * | (Y' & 0b111100) << 3
961 * | (X' & 0b100) << 2
962 * | (Y' & 0b10) << 2
963 * | (X' & 0b10) << 1
964 * | (Y' & 0b1) << 1
965 * | (X' & 0b1)
966 * X' = X * cpp = X
967 * Y' = Y + S * qpitch
968 * detile(w_tiled, A) = (X, Y, S)
969 * where X = X' / cpp = X'
970 * Y = Y' % qpitch = Y'
971 * S = Y / qpitch = 0
972 * Y' = (tile_num / tile_pitch) << 6
973 * | (A & 0b111100000) >> 3
974 * | (A & 0b1000) >> 2
975 * | (A & 0b10) >> 1
976 * X' = (tile_num % tile_pitch) << 6
977 * | (A & 0b111000000000) >> 6
978 * | (A & 0b10000) >> 2
979 * | (A & 0b100) >> 1
980 * | (A & 0b1)
981 *
982 * Finally, for a non-tiled surface, tile() simply combines together the X and
983 * Y coordinates in the natural way:
984 *
985 * tile(untiled, X, Y, S) = A
986 * where A = Y * pitch + X'
987 * X' = X * cpp
988 * Y' = Y + S * qpitch
989 * detile(untiled, A) = (X, Y, S)
990 * where X = X' / cpp
991 * Y = Y' % qpitch
992 * S = Y' / qpitch
993 * X' = A % pitch
994 * Y' = A / pitch
995 *
996 * (In these formulas, pitch is the number of bytes occupied by a single row
997 * of samples).
998 */
999 static nir_shader *
1000 brw_blorp_build_nir_shader(struct blorp_context *blorp, void *mem_ctx,
1001 const struct brw_blorp_blit_prog_key *key)
1002 {
1003 const struct gen_device_info *devinfo = blorp->isl_dev->info;
1004 nir_ssa_def *src_pos, *dst_pos, *color;
1005
1006 /* Sanity checks */
1007 if (key->dst_tiled_w && key->rt_samples > 1) {
1008 /* If the destination image is W tiled and multisampled, then the thread
1009 * must be dispatched once per sample, not once per pixel. This is
1010 * necessary because after conversion between W and Y tiling, there's no
1011 * guarantee that all samples corresponding to a single pixel will still
1012 * be together.
1013 */
1014 assert(key->persample_msaa_dispatch);
1015 }
1016
1017 if (key->blend) {
1018 /* We are blending, which means we won't have an opportunity to
1019 * translate the tiling and sample count for the texture surface. So
1020 * the surface state for the texture must be configured with the correct
1021 * tiling and sample count.
1022 */
1023 assert(!key->src_tiled_w);
1024 assert(key->tex_samples == key->src_samples);
1025 assert(key->tex_layout == key->src_layout);
1026 assert(key->tex_samples > 0);
1027 }
1028
1029 if (key->persample_msaa_dispatch) {
1030 /* It only makes sense to do persample dispatch if the render target is
1031 * configured as multisampled.
1032 */
1033 assert(key->rt_samples > 0);
1034 }
1035
1036 /* Make sure layout is consistent with sample count */
1037 assert((key->tex_layout == ISL_MSAA_LAYOUT_NONE) ==
1038 (key->tex_samples <= 1));
1039 assert((key->rt_layout == ISL_MSAA_LAYOUT_NONE) ==
1040 (key->rt_samples <= 1));
1041 assert((key->src_layout == ISL_MSAA_LAYOUT_NONE) ==
1042 (key->src_samples <= 1));
1043 assert((key->dst_layout == ISL_MSAA_LAYOUT_NONE) ==
1044 (key->dst_samples <= 1));
1045
1046 nir_builder b;
1047 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
1048
1049 struct brw_blorp_blit_vars v;
1050 brw_blorp_blit_vars_init(&b, &v, key);
1051
1052 dst_pos = blorp_blit_get_frag_coords(&b, key, &v);
1053
1054 /* Render target and texture hardware don't support W tiling until Gen8. */
1055 const bool rt_tiled_w = false;
1056 const bool tex_tiled_w = devinfo->gen >= 8 && key->src_tiled_w;
1057
1058 /* The address that data will be written to is determined by the
1059 * coordinates supplied to the WM thread and the tiling and sample count of
1060 * the render target, according to the formula:
1061 *
1062 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1063 *
1064 * If the actual tiling and sample count of the destination surface are not
1065 * the same as the configuration of the render target, then these
1066 * coordinates are wrong and we have to adjust them to compensate for the
1067 * difference.
1068 */
1069 if (rt_tiled_w != key->dst_tiled_w ||
1070 key->rt_samples != key->dst_samples ||
1071 key->rt_layout != key->dst_layout) {
1072 dst_pos = blorp_nir_encode_msaa(&b, dst_pos, key->rt_samples,
1073 key->rt_layout);
1074 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1075 if (rt_tiled_w != key->dst_tiled_w)
1076 dst_pos = blorp_nir_retile_y_to_w(&b, dst_pos);
1077 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1078 dst_pos = blorp_nir_decode_msaa(&b, dst_pos, key->dst_samples,
1079 key->dst_layout);
1080 }
1081
1082 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1083 *
1084 * That is: X, Y and S now contain the true coordinates and sample index of
1085 * the data that the WM thread should output.
1086 *
1087 * If we need to kill pixels that are outside the destination rectangle,
1088 * now is the time to do it.
1089 */
1090 if (key->use_kill) {
1091 assert(!(key->blend && key->blit_scaled));
1092 blorp_nir_discard_if_outside_rect(&b, dst_pos, &v);
1093 }
1094
1095 src_pos = blorp_blit_apply_transform(&b, nir_i2f(&b, dst_pos), &v);
1096 if (dst_pos->num_components == 3) {
1097 /* The sample coordinate is an integer that we want left alone but
1098 * blorp_blit_apply_transform() blindly applies the transform to all
1099 * three coordinates. Grab the original sample index.
1100 */
1101 src_pos = nir_vec3(&b, nir_channel(&b, src_pos, 0),
1102 nir_channel(&b, src_pos, 1),
1103 nir_channel(&b, dst_pos, 2));
1104 }
1105
1106 /* If the source image is not multisampled, then we want to fetch sample
1107 * number 0, because that's the only sample there is.
1108 */
1109 if (key->src_samples == 1)
1110 src_pos = nir_channels(&b, src_pos, 0x3);
1111
1112 /* X, Y, and S are now the coordinates of the pixel in the source image
1113 * that we want to texture from. Exception: if we are blending, then S is
1114 * irrelevant, because we are going to fetch all samples.
1115 */
1116 if (key->blend && !key->blit_scaled) {
1117 /* Resolves (effecively) use texelFetch, so we need integers and we
1118 * don't care about the sample index if we got one.
1119 */
1120 src_pos = nir_f2i(&b, nir_channels(&b, src_pos, 0x3));
1121
1122 if (devinfo->gen == 6) {
1123 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1124 * blending we need with a single linear-interpolated texture lookup
1125 * at the center of the sample. The texture coordinates to be odd
1126 * integers so that they correspond to the center of a 2x2 block
1127 * representing the four samples that maxe up a pixel. So we need
1128 * to multiply our X and Y coordinates each by 2 and then add 1.
1129 */
1130 src_pos = nir_ishl(&b, src_pos, nir_imm_int(&b, 1));
1131 src_pos = nir_iadd(&b, src_pos, nir_imm_int(&b, 1));
1132 src_pos = nir_i2f(&b, src_pos);
1133 color = blorp_nir_tex(&b, &v, src_pos, key->texture_data_type);
1134 } else {
1135 /* Gen7+ hardware doesn't automaticaly blend. */
1136 color = blorp_nir_manual_blend_average(&b, &v, src_pos, key->src_samples,
1137 key->tex_aux_usage,
1138 key->texture_data_type);
1139 }
1140 } else if (key->blend && key->blit_scaled) {
1141 assert(!key->use_kill);
1142 color = blorp_nir_manual_blend_bilinear(&b, src_pos, key->src_samples, key, &v);
1143 } else {
1144 if (key->bilinear_filter) {
1145 color = blorp_nir_tex(&b, &v, src_pos, key->texture_data_type);
1146 } else {
1147 /* We're going to use texelFetch, so we need integers */
1148 if (src_pos->num_components == 2) {
1149 src_pos = nir_f2i(&b, src_pos);
1150 } else {
1151 assert(src_pos->num_components == 3);
1152 src_pos = nir_vec3(&b, nir_channel(&b, nir_f2i(&b, src_pos), 0),
1153 nir_channel(&b, nir_f2i(&b, src_pos), 1),
1154 nir_channel(&b, src_pos, 2));
1155 }
1156
1157 /* We aren't blending, which means we just want to fetch a single
1158 * sample from the source surface. The address that we want to fetch
1159 * from is related to the X, Y and S values according to the formula:
1160 *
1161 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1162 *
1163 * If the actual tiling and sample count of the source surface are
1164 * not the same as the configuration of the texture, then we need to
1165 * adjust the coordinates to compensate for the difference.
1166 */
1167 if (tex_tiled_w != key->src_tiled_w ||
1168 key->tex_samples != key->src_samples ||
1169 key->tex_layout != key->src_layout) {
1170 src_pos = blorp_nir_encode_msaa(&b, src_pos, key->src_samples,
1171 key->src_layout);
1172 /* Now (X, Y, S) = detile(src_tiling, offset) */
1173 if (tex_tiled_w != key->src_tiled_w)
1174 src_pos = blorp_nir_retile_w_to_y(&b, src_pos);
1175 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1176 src_pos = blorp_nir_decode_msaa(&b, src_pos, key->tex_samples,
1177 key->tex_layout);
1178 }
1179
1180 if (key->need_src_offset)
1181 src_pos = nir_iadd(&b, src_pos, nir_load_var(&b, v.v_src_offset));
1182
1183 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1184 *
1185 * In other words: X, Y, and S now contain values which, when passed to
1186 * the texturing unit, will cause data to be read from the correct
1187 * memory location. So we can fetch the texel now.
1188 */
1189 if (key->src_samples == 1) {
1190 color = blorp_nir_txf(&b, &v, src_pos, key->texture_data_type);
1191 } else {
1192 nir_ssa_def *mcs = NULL;
1193 if (key->tex_aux_usage == ISL_AUX_USAGE_MCS)
1194 mcs = blorp_nir_txf_ms_mcs(&b, &v, src_pos);
1195
1196 color = blorp_nir_txf_ms(&b, &v, src_pos, mcs, key->texture_data_type);
1197 }
1198 }
1199 }
1200
1201 if (key->dst_rgb) {
1202 /* The destination image is bound as a red texture three times as wide
1203 * as the actual image. Our shader is effectively running one color
1204 * component at a time. We need to pick off the appropriate component
1205 * from the source color and write that to destination red.
1206 */
1207 assert(dst_pos->num_components == 2);
1208 nir_ssa_def *comp =
1209 nir_umod(&b, nir_channel(&b, dst_pos, 0), nir_imm_int(&b, 3));
1210
1211 nir_ssa_def *color_component =
1212 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 0)),
1213 nir_channel(&b, color, 0),
1214 nir_bcsel(&b, nir_ieq(&b, comp, nir_imm_int(&b, 1)),
1215 nir_channel(&b, color, 1),
1216 nir_channel(&b, color, 2)));
1217
1218 nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);
1219 color = nir_vec4(&b, color_component, u, u, u);
1220 }
1221
1222 nir_store_var(&b, v.color_out, color, 0xf);
1223
1224 return b.shader;
1225 }
1226
1227 static void
1228 brw_blorp_get_blit_kernel(struct blorp_context *blorp,
1229 struct blorp_params *params,
1230 const struct brw_blorp_blit_prog_key *prog_key)
1231 {
1232 if (blorp->lookup_shader(blorp, prog_key, sizeof(*prog_key),
1233 &params->wm_prog_kernel, &params->wm_prog_data))
1234 return;
1235
1236 void *mem_ctx = ralloc_context(NULL);
1237
1238 const unsigned *program;
1239 unsigned program_size;
1240 struct brw_blorp_prog_data prog_data;
1241
1242 nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
1243 struct brw_wm_prog_key wm_key;
1244 brw_blorp_init_wm_prog_key(&wm_key);
1245 wm_key.tex.compressed_multisample_layout_mask =
1246 prog_key->tex_aux_usage == ISL_AUX_USAGE_MCS;
1247 wm_key.tex.msaa_16 = prog_key->tex_samples == 16;
1248 wm_key.multisample_fbo = prog_key->rt_samples > 1;
1249
1250 program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
1251 &prog_data, &program_size);
1252
1253 blorp->upload_shader(blorp, prog_key, sizeof(*prog_key),
1254 program, program_size,
1255 &prog_data, sizeof(prog_data),
1256 &params->wm_prog_kernel, &params->wm_prog_data);
1257
1258 ralloc_free(mem_ctx);
1259 }
1260
1261 static void
1262 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
1263 GLfloat src0, GLfloat src1,
1264 GLfloat dst0, GLfloat dst1,
1265 bool mirror)
1266 {
1267 double scale = (double)(src1 - src0) / (double)(dst1 - dst0);
1268 if (!mirror) {
1269 /* When not mirroring a coordinate (say, X), we need:
1270 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1271 * Therefore:
1272 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1273 *
1274 * blorp program uses "round toward zero" to convert the
1275 * transformed floating point coordinates to integer coordinates,
1276 * whereas the behaviour we actually want is "round to nearest",
1277 * so 0.5 provides the necessary correction.
1278 */
1279 xform->multiplier = scale;
1280 xform->offset = src0 + (-(double)dst0 + 0.5) * scale;
1281 } else {
1282 /* When mirroring X we need:
1283 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1284 * Therefore:
1285 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1286 */
1287 xform->multiplier = -scale;
1288 xform->offset = src0 + ((double)dst1 - 0.5) * scale;
1289 }
1290 }
1291
1292 static inline void
1293 surf_get_intratile_offset_px(struct brw_blorp_surface_info *info,
1294 uint32_t *tile_x_px, uint32_t *tile_y_px)
1295 {
1296 if (info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1297 struct isl_extent2d px_size_sa =
1298 isl_get_interleaved_msaa_px_size_sa(info->surf.samples);
1299 assert(info->tile_x_sa % px_size_sa.width == 0);
1300 assert(info->tile_y_sa % px_size_sa.height == 0);
1301 *tile_x_px = info->tile_x_sa / px_size_sa.width;
1302 *tile_y_px = info->tile_y_sa / px_size_sa.height;
1303 } else {
1304 *tile_x_px = info->tile_x_sa;
1305 *tile_y_px = info->tile_y_sa;
1306 }
1307 }
1308
1309 static void
1310 surf_convert_to_single_slice(const struct isl_device *isl_dev,
1311 struct brw_blorp_surface_info *info)
1312 {
1313 /* Just bail if we have nothing to do. */
1314 if (info->surf.dim == ISL_SURF_DIM_2D &&
1315 info->view.base_level == 0 && info->view.base_array_layer == 0 &&
1316 info->surf.levels == 1 && info->surf.logical_level0_px.array_len == 1)
1317 return;
1318
1319 /* If this gets triggered then we've gotten here twice which. This
1320 * shouldn't happen thanks to the above early return.
1321 */
1322 assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
1323
1324 uint32_t layer = 0, z = 0;
1325 if (info->surf.dim == ISL_SURF_DIM_3D)
1326 z = info->view.base_array_layer + info->z_offset;
1327 else
1328 layer = info->view.base_array_layer;
1329
1330 uint32_t x_offset_sa, y_offset_sa;
1331 isl_surf_get_image_offset_sa(&info->surf, info->view.base_level,
1332 layer, z, &x_offset_sa, &y_offset_sa);
1333
1334 uint32_t byte_offset;
1335 isl_tiling_get_intratile_offset_sa(isl_dev, info->surf.tiling,
1336 info->surf.format, info->surf.row_pitch,
1337 x_offset_sa, y_offset_sa,
1338 &byte_offset,
1339 &info->tile_x_sa, &info->tile_y_sa);
1340 info->addr.offset += byte_offset;
1341
1342 const uint32_t slice_width_px =
1343 minify(info->surf.logical_level0_px.width, info->view.base_level);
1344 const uint32_t slice_height_px =
1345 minify(info->surf.logical_level0_px.height, info->view.base_level);
1346
1347 uint32_t tile_x_px, tile_y_px;
1348 surf_get_intratile_offset_px(info, &tile_x_px, &tile_y_px);
1349
1350 /* TODO: Once this file gets converted to C, we shouls just use designated
1351 * initializers.
1352 */
1353 struct isl_surf_init_info init_info = { 0, };
1354
1355 init_info.dim = ISL_SURF_DIM_2D;
1356 init_info.format = info->surf.format;
1357 init_info.width = slice_width_px + tile_x_px;
1358 init_info.height = slice_height_px + tile_y_px;
1359 init_info.depth = 1;
1360 init_info.levels = 1;
1361 init_info.array_len = 1;
1362 init_info.samples = info->surf.samples;
1363 init_info.min_pitch = info->surf.row_pitch;
1364 init_info.usage = info->surf.usage;
1365 init_info.tiling_flags = 1 << info->surf.tiling;
1366
1367 isl_surf_init_s(isl_dev, &info->surf, &init_info);
1368 assert(info->surf.row_pitch == init_info.min_pitch);
1369
1370 /* The view is also different now. */
1371 info->view.base_level = 0;
1372 info->view.levels = 1;
1373 info->view.base_array_layer = 0;
1374 info->view.array_len = 1;
1375 info->z_offset = 0;
1376 }
1377
1378 static void
1379 surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
1380 struct brw_blorp_surface_info *info)
1381 {
1382 assert(info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1383
1384 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1385 surf_convert_to_single_slice(isl_dev, info);
1386
1387 info->surf.logical_level0_px = info->surf.phys_level0_sa;
1388 info->surf.samples = 1;
1389 info->surf.msaa_layout = ISL_MSAA_LAYOUT_NONE;
1390 }
1391
1392 static void
1393 surf_retile_w_to_y(const struct isl_device *isl_dev,
1394 struct brw_blorp_surface_info *info)
1395 {
1396 assert(info->surf.tiling == ISL_TILING_W);
1397
1398 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1399 surf_convert_to_single_slice(isl_dev, info);
1400
1401 /* On gen7+, we don't have interleaved multisampling for color render
1402 * targets so we have to fake it.
1403 *
1404 * TODO: Are we sure we don't also need to fake it on gen6?
1405 */
1406 if (isl_dev->info->gen > 6 &&
1407 info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1408 surf_fake_interleaved_msaa(isl_dev, info);
1409 }
1410
1411 if (isl_dev->info->gen == 6) {
1412 /* Gen6 stencil buffers have a very large alignment coming in from the
1413 * miptree. It's out-of-bounds for what the surface state can handle.
1414 * Since we have a single layer and level, it doesn't really matter as
1415 * long as we don't pass a bogus value into isl_surf_fill_state().
1416 */
1417 info->surf.image_alignment_el = isl_extent3d(4, 2, 1);
1418 }
1419
1420 /* Now that we've converted everything to a simple 2-D surface with only
1421 * one miplevel, we can go about retiling it.
1422 */
1423 const unsigned x_align = 8, y_align = info->surf.samples != 0 ? 8 : 4;
1424 info->surf.tiling = ISL_TILING_Y0;
1425 info->surf.logical_level0_px.width =
1426 ALIGN(info->surf.logical_level0_px.width, x_align) * 2;
1427 info->surf.logical_level0_px.height =
1428 ALIGN(info->surf.logical_level0_px.height, y_align) / 2;
1429 info->tile_x_sa *= 2;
1430 info->tile_y_sa /= 2;
1431 }
1432
1433 static void
1434 do_blorp_blit(struct blorp_batch *batch,
1435 struct blorp_params *params,
1436 struct brw_blorp_blit_prog_key *wm_prog_key,
1437 float src_x0, float src_y0,
1438 float src_x1, float src_y1,
1439 float dst_x0, float dst_y0,
1440 float dst_x1, float dst_y1,
1441 bool mirror_x, bool mirror_y)
1442 {
1443 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
1444
1445 if (isl_format_has_sint_channel(params->src.view.format)) {
1446 wm_prog_key->texture_data_type = nir_type_int;
1447 } else if (isl_format_has_uint_channel(params->src.view.format)) {
1448 wm_prog_key->texture_data_type = nir_type_uint;
1449 } else {
1450 wm_prog_key->texture_data_type = nir_type_float;
1451 }
1452
1453 /* src_samples and dst_samples are the true sample counts */
1454 wm_prog_key->src_samples = params->src.surf.samples;
1455 wm_prog_key->dst_samples = params->dst.surf.samples;
1456
1457 wm_prog_key->tex_aux_usage = params->src.aux_usage;
1458
1459 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1460 * dst.
1461 */
1462 wm_prog_key->src_layout = params->src.surf.msaa_layout;
1463 wm_prog_key->dst_layout = params->dst.surf.msaa_layout;
1464
1465 /* Round floating point values to nearest integer to avoid "off by one texel"
1466 * kind of errors when blitting.
1467 */
1468 params->x0 = params->wm_inputs.discard_rect.x0 = roundf(dst_x0);
1469 params->y0 = params->wm_inputs.discard_rect.y0 = roundf(dst_y0);
1470 params->x1 = params->wm_inputs.discard_rect.x1 = roundf(dst_x1);
1471 params->y1 = params->wm_inputs.discard_rect.y1 = roundf(dst_y1);
1472
1473 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[0],
1474 src_x0, src_x1, dst_x0, dst_x1, mirror_x);
1475 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[1],
1476 src_y0, src_y1, dst_y0, dst_y1, mirror_y);
1477
1478 if (devinfo->gen > 6 &&
1479 params->dst.surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1480 assert(params->dst.surf.samples > 1);
1481
1482 /* We must expand the rectangle we send through the rendering pipeline,
1483 * to account for the fact that we are mapping the destination region as
1484 * single-sampled when it is in fact multisampled. We must also align
1485 * it to a multiple of the multisampling pattern, because the
1486 * differences between multisampled and single-sampled surface formats
1487 * will mean that pixels are scrambled within the multisampling pattern.
1488 * TODO: what if this makes the coordinates too large?
1489 *
1490 * Note: this only works if the destination surface uses the IMS layout.
1491 * If it's UMS, then we have no choice but to set up the rendering
1492 * pipeline as multisampled.
1493 */
1494 struct isl_extent2d px_size_sa =
1495 isl_get_interleaved_msaa_px_size_sa(params->dst.surf.samples);
1496 params->x0 = ROUND_DOWN_TO(params->x0, 2) * px_size_sa.width;
1497 params->y0 = ROUND_DOWN_TO(params->y0, 2) * px_size_sa.height;
1498 params->x1 = ALIGN(params->x1, 2) * px_size_sa.width;
1499 params->y1 = ALIGN(params->y1, 2) * px_size_sa.height;
1500
1501 surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params->dst);
1502
1503 wm_prog_key->use_kill = true;
1504 wm_prog_key->need_dst_offset = true;
1505 }
1506
1507 if (params->dst.surf.tiling == ISL_TILING_W) {
1508 /* We must modify the rectangle we send through the rendering pipeline
1509 * (and the size and x/y offset of the destination surface), to account
1510 * for the fact that we are mapping it as Y-tiled when it is in fact
1511 * W-tiled.
1512 *
1513 * Both Y tiling and W tiling can be understood as organizations of
1514 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1515 * is different, but the layout of the 32-byte sub-tiles within the 4k
1516 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1517 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1518 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1519 *
1520 * Therefore, to account for the layout differences within the 32-byte
1521 * sub-tiles, we must expand the rectangle so the X coordinates of its
1522 * edges are multiples of 8 (the W sub-tile width), and its Y
1523 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1524 * Then we need to scale the X and Y coordinates of the rectangle to
1525 * account for the differences in aspect ratio between the Y and W
1526 * sub-tiles. We need to modify the layer width and height similarly.
1527 *
1528 * A correction needs to be applied when MSAA is in use: since
1529 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1530 * we need to align the Y coordinates to multiples of 8, so that when
1531 * they are divided by two they are still multiples of 4.
1532 *
1533 * Note: Since the x/y offset of the surface will be applied using the
1534 * SURFACE_STATE command packet, it will be invisible to the swizzling
1535 * code in the shader; therefore it needs to be in a multiple of the
1536 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1537 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1538 * buffer), and the miplevel alignment used for stencil buffers is 8
1539 * pixels horizontally and either 4 or 8 pixels vertically (see
1540 * intel_horizontal_texture_alignment_unit() and
1541 * intel_vertical_texture_alignment_unit()).
1542 *
1543 * Note: Also, since the SURFACE_STATE command packet can only apply
1544 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1545 * vertically, it is important that the offsets will be multiples of
1546 * these sizes after they are converted into Y-tiled coordinates.
1547 * Fortunately they will be, since we know from above that the offsets
1548 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1549 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1550 *
1551 * TODO: what if this makes the coordinates (or the texture size) too
1552 * large?
1553 */
1554 const unsigned x_align = 8;
1555 const unsigned y_align = params->dst.surf.samples != 0 ? 8 : 4;
1556 params->x0 = ROUND_DOWN_TO(params->x0, x_align) * 2;
1557 params->y0 = ROUND_DOWN_TO(params->y0, y_align) / 2;
1558 params->x1 = ALIGN(params->x1, x_align) * 2;
1559 params->y1 = ALIGN(params->y1, y_align) / 2;
1560
1561 /* Retile the surface to Y-tiled */
1562 surf_retile_w_to_y(batch->blorp->isl_dev, &params->dst);
1563
1564 wm_prog_key->dst_tiled_w = true;
1565 wm_prog_key->use_kill = true;
1566 wm_prog_key->need_dst_offset = true;
1567
1568 if (params->dst.surf.samples > 1) {
1569 /* If the destination surface is a W-tiled multisampled stencil
1570 * buffer that we're mapping as Y tiled, then we need to arrange for
1571 * the WM program to run once per sample rather than once per pixel,
1572 * because the memory layout of related samples doesn't match between
1573 * W and Y tiling.
1574 */
1575 wm_prog_key->persample_msaa_dispatch = true;
1576 }
1577 }
1578
1579 if (devinfo->gen < 8 && params->src.surf.tiling == ISL_TILING_W) {
1580 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1581 * Broadwell adds support for sampling from stencil.
1582 *
1583 * See the comments above concerning x/y offset alignment for the
1584 * destination surface.
1585 *
1586 * TODO: what if this makes the texture size too large?
1587 */
1588 surf_retile_w_to_y(batch->blorp->isl_dev, &params->src);
1589
1590 wm_prog_key->src_tiled_w = true;
1591 wm_prog_key->need_src_offset = true;
1592 }
1593
1594 /* tex_samples and rt_samples are the sample counts that are set up in
1595 * SURFACE_STATE.
1596 */
1597 wm_prog_key->tex_samples = params->src.surf.samples;
1598 wm_prog_key->rt_samples = params->dst.surf.samples;
1599
1600 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1601 * use to access the source and destination surfaces.
1602 */
1603 wm_prog_key->tex_layout = params->src.surf.msaa_layout;
1604 wm_prog_key->rt_layout = params->dst.surf.msaa_layout;
1605
1606 if (params->src.surf.samples > 0 && params->dst.surf.samples > 1) {
1607 /* We are blitting from a multisample buffer to a multisample buffer, so
1608 * we must preserve samples within a pixel. This means we have to
1609 * arrange for the WM program to run once per sample rather than once
1610 * per pixel.
1611 */
1612 wm_prog_key->persample_msaa_dispatch = true;
1613 }
1614
1615 if (params->src.tile_x_sa || params->src.tile_y_sa) {
1616 assert(wm_prog_key->need_src_offset);
1617 surf_get_intratile_offset_px(&params->src,
1618 &params->wm_inputs.src_offset.x,
1619 &params->wm_inputs.src_offset.y);
1620 }
1621
1622 if (params->dst.tile_x_sa || params->dst.tile_y_sa) {
1623 assert(wm_prog_key->need_dst_offset);
1624 surf_get_intratile_offset_px(&params->dst,
1625 &params->wm_inputs.dst_offset.x,
1626 &params->wm_inputs.dst_offset.y);
1627 params->x0 += params->wm_inputs.dst_offset.x;
1628 params->y0 += params->wm_inputs.dst_offset.y;
1629 params->x1 += params->wm_inputs.dst_offset.x;
1630 params->y1 += params->wm_inputs.dst_offset.y;
1631 }
1632
1633 /* For some texture types, we need to pass the layer through the sampler. */
1634 params->wm_inputs.src_z = params->src.z_offset;
1635
1636 brw_blorp_get_blit_kernel(batch->blorp, params, wm_prog_key);
1637
1638 batch->blorp->exec(batch, params);
1639 }
1640
1641 void
1642 blorp_blit(struct blorp_batch *batch,
1643 const struct blorp_surf *src_surf,
1644 unsigned src_level, unsigned src_layer,
1645 enum isl_format src_format, struct isl_swizzle src_swizzle,
1646 const struct blorp_surf *dst_surf,
1647 unsigned dst_level, unsigned dst_layer,
1648 enum isl_format dst_format, struct isl_swizzle dst_swizzle,
1649 float src_x0, float src_y0,
1650 float src_x1, float src_y1,
1651 float dst_x0, float dst_y0,
1652 float dst_x1, float dst_y1,
1653 GLenum filter, bool mirror_x, bool mirror_y)
1654 {
1655 struct blorp_params params;
1656 blorp_params_init(&params);
1657
1658 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
1659 src_layer, src_format, false);
1660 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
1661 dst_layer, dst_format, true);
1662
1663 params.src.view.swizzle = src_swizzle;
1664 params.dst.view.swizzle = dst_swizzle;
1665
1666 struct brw_blorp_blit_prog_key wm_prog_key;
1667 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1668
1669 /* Scaled blitting or not. */
1670 wm_prog_key.blit_scaled =
1671 ((dst_x1 - dst_x0) == (src_x1 - src_x0) &&
1672 (dst_y1 - dst_y0) == (src_y1 - src_y0)) ? false : true;
1673
1674 /* Scaling factors used for bilinear filtering in multisample scaled
1675 * blits.
1676 */
1677 if (params.src.surf.samples == 16)
1678 wm_prog_key.x_scale = 4.0f;
1679 else
1680 wm_prog_key.x_scale = 2.0f;
1681 wm_prog_key.y_scale = params.src.surf.samples / wm_prog_key.x_scale;
1682
1683 if (filter == GL_LINEAR &&
1684 params.src.surf.samples <= 1 && params.dst.surf.samples <= 1)
1685 wm_prog_key.bilinear_filter = true;
1686
1687 if ((params.src.surf.usage & ISL_SURF_USAGE_DEPTH_BIT) == 0 &&
1688 (params.src.surf.usage & ISL_SURF_USAGE_STENCIL_BIT) == 0 &&
1689 !isl_format_has_int_channel(params.src.surf.format) &&
1690 params.src.surf.samples > 1 && params.dst.surf.samples <= 1) {
1691 /* We are downsampling a non-integer color buffer, so blend.
1692 *
1693 * Regarding integer color buffers, the OpenGL ES 3.2 spec says:
1694 *
1695 * "If the source formats are integer types or stencil values, a
1696 * single sample's value is selected for each pixel."
1697 *
1698 * This implies we should not blend in that case.
1699 */
1700 wm_prog_key.blend = true;
1701 }
1702
1703 params.wm_inputs.rect_grid.x1 =
1704 minify(params.src.surf.logical_level0_px.width, src_level) *
1705 wm_prog_key.x_scale - 1.0f;
1706 params.wm_inputs.rect_grid.y1 =
1707 minify(params.src.surf.logical_level0_px.height, src_level) *
1708 wm_prog_key.y_scale - 1.0f;
1709
1710 do_blorp_blit(batch, &params, &wm_prog_key,
1711 src_x0, src_y0, src_x1, src_y1,
1712 dst_x0, dst_y0, dst_x1, dst_y1,
1713 mirror_x, mirror_y);
1714 }
1715
1716 static enum isl_format
1717 get_copy_format_for_bpb(unsigned bpb)
1718 {
1719 /* The choice of UNORM and UINT formats is very intentional here. Most of
1720 * the time, we want to use a UINT format to avoid any rounding error in
1721 * the blit. For stencil blits, R8_UINT is required by the hardware.
1722 * (It's the only format allowed in conjunction with W-tiling.) Also we
1723 * intentionally use the 4-channel formats whenever we can. This is so
1724 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
1725 * though one of them is 3/4 the size of the other. The choice of UNORM
1726 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
1727 * 16-bit RGB UINT formats at all so we have to use UNORM there.
1728 * Fortunately, the only time we should ever use two different formats in
1729 * the table below is for RGB -> RGBA blits and so we will never have any
1730 * UNORM/UINT mismatch.
1731 */
1732 switch (bpb) {
1733 case 8: return ISL_FORMAT_R8_UINT;
1734 case 16: return ISL_FORMAT_R8G8_UINT;
1735 case 24: return ISL_FORMAT_R8G8B8_UNORM;
1736 case 32: return ISL_FORMAT_R8G8B8A8_UNORM;
1737 case 48: return ISL_FORMAT_R16G16B16_UNORM;
1738 case 64: return ISL_FORMAT_R16G16B16A16_UNORM;
1739 case 96: return ISL_FORMAT_R32G32B32_UINT;
1740 case 128:return ISL_FORMAT_R32G32B32A32_UINT;
1741 default:
1742 unreachable("Unknown format bpb");
1743 }
1744 }
1745
1746 static void
1747 surf_convert_to_uncompressed(const struct isl_device *isl_dev,
1748 struct brw_blorp_surface_info *info,
1749 uint32_t *x, uint32_t *y,
1750 uint32_t *width, uint32_t *height)
1751 {
1752 const struct isl_format_layout *fmtl =
1753 isl_format_get_layout(info->surf.format);
1754
1755 assert(fmtl->bw > 1 || fmtl->bh > 1);
1756
1757 /* This is a compressed surface. We need to convert it to a single
1758 * slice (because compressed layouts don't perfectly match uncompressed
1759 * ones with the same bpb) and divide x, y, width, and height by the
1760 * block size.
1761 */
1762 surf_convert_to_single_slice(isl_dev, info);
1763
1764 if (width || height) {
1765 #ifndef NDEBUG
1766 uint32_t right_edge_px = info->tile_x_sa + *x + *width;
1767 uint32_t bottom_edge_px = info->tile_y_sa + *y + *height;
1768 assert(*width % fmtl->bw == 0 ||
1769 right_edge_px == info->surf.logical_level0_px.width);
1770 assert(*height % fmtl->bh == 0 ||
1771 bottom_edge_px == info->surf.logical_level0_px.height);
1772 #endif
1773 *width = DIV_ROUND_UP(*width, fmtl->bw);
1774 *height = DIV_ROUND_UP(*height, fmtl->bh);
1775 }
1776
1777 assert(*x % fmtl->bw == 0);
1778 assert(*y % fmtl->bh == 0);
1779 *x /= fmtl->bw;
1780 *y /= fmtl->bh;
1781
1782 info->surf.logical_level0_px.width =
1783 DIV_ROUND_UP(info->surf.logical_level0_px.width, fmtl->bw);
1784 info->surf.logical_level0_px.height =
1785 DIV_ROUND_UP(info->surf.logical_level0_px.height, fmtl->bh);
1786
1787 assert(info->surf.phys_level0_sa.width % fmtl->bw == 0);
1788 assert(info->surf.phys_level0_sa.height % fmtl->bh == 0);
1789 info->surf.phys_level0_sa.width /= fmtl->bw;
1790 info->surf.phys_level0_sa.height /= fmtl->bh;
1791
1792 assert(info->tile_x_sa % fmtl->bw == 0);
1793 assert(info->tile_y_sa % fmtl->bh == 0);
1794 info->tile_x_sa /= fmtl->bw;
1795 info->tile_y_sa /= fmtl->bh;
1796
1797 /* It's now an uncompressed surface so we need an uncompressed format */
1798 info->surf.format = get_copy_format_for_bpb(fmtl->bpb);
1799 }
1800
1801 static void
1802 surf_fake_rgb_with_red(const struct isl_device *isl_dev,
1803 struct brw_blorp_surface_info *info,
1804 uint32_t *x, uint32_t *width)
1805 {
1806 surf_convert_to_single_slice(isl_dev, info);
1807
1808 info->surf.logical_level0_px.width *= 3;
1809 info->surf.phys_level0_sa.width *= 3;
1810 *x *= 3;
1811 *width *= 3;
1812
1813 enum isl_format red_format;
1814 switch (info->view.format) {
1815 case ISL_FORMAT_R8G8B8_UNORM:
1816 red_format = ISL_FORMAT_R8_UNORM;
1817 break;
1818 case ISL_FORMAT_R16G16B16_UNORM:
1819 red_format = ISL_FORMAT_R16_UNORM;
1820 break;
1821 case ISL_FORMAT_R32G32B32_UINT:
1822 red_format = ISL_FORMAT_R32_UINT;
1823 break;
1824 default:
1825 unreachable("Invalid RGB copy destination format");
1826 }
1827 assert(isl_format_get_layout(red_format)->channels.r.type ==
1828 isl_format_get_layout(info->view.format)->channels.r.type);
1829 assert(isl_format_get_layout(red_format)->channels.r.bits ==
1830 isl_format_get_layout(info->view.format)->channels.r.bits);
1831
1832 info->surf.format = info->view.format = red_format;
1833 }
1834
1835 void
1836 blorp_copy(struct blorp_batch *batch,
1837 const struct blorp_surf *src_surf,
1838 unsigned src_level, unsigned src_layer,
1839 const struct blorp_surf *dst_surf,
1840 unsigned dst_level, unsigned dst_layer,
1841 uint32_t src_x, uint32_t src_y,
1842 uint32_t dst_x, uint32_t dst_y,
1843 uint32_t src_width, uint32_t src_height)
1844 {
1845 struct blorp_params params;
1846
1847 if (src_width == 0 || src_height == 0)
1848 return;
1849
1850 blorp_params_init(&params);
1851 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
1852 src_layer, ISL_FORMAT_UNSUPPORTED, false);
1853 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
1854 dst_layer, ISL_FORMAT_UNSUPPORTED, true);
1855
1856 struct brw_blorp_blit_prog_key wm_prog_key;
1857 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1858
1859 const struct isl_format_layout *src_fmtl =
1860 isl_format_get_layout(params.src.surf.format);
1861 const struct isl_format_layout *dst_fmtl =
1862 isl_format_get_layout(params.dst.surf.format);
1863
1864 params.src.view.format = get_copy_format_for_bpb(src_fmtl->bpb);
1865 if (src_fmtl->bw > 1 || src_fmtl->bh > 1) {
1866 surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.src,
1867 &src_x, &src_y, &src_width, &src_height);
1868 wm_prog_key.need_src_offset = true;
1869 }
1870
1871 params.dst.view.format = get_copy_format_for_bpb(dst_fmtl->bpb);
1872 if (dst_fmtl->bw > 1 || dst_fmtl->bh > 1) {
1873 surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
1874 &dst_x, &dst_y, NULL, NULL);
1875 wm_prog_key.need_dst_offset = true;
1876 }
1877
1878 /* Once both surfaces are stompped to uncompressed as needed, the
1879 * destination size is the same as the source size.
1880 */
1881 uint32_t dst_width = src_width;
1882 uint32_t dst_height = src_height;
1883
1884 if (dst_fmtl->bpb % 3 == 0) {
1885 surf_fake_rgb_with_red(batch->blorp->isl_dev, &params.dst,
1886 &dst_x, &dst_width);
1887 wm_prog_key.dst_rgb = true;
1888 wm_prog_key.need_dst_offset = true;
1889 }
1890
1891 do_blorp_blit(batch, &params, &wm_prog_key,
1892 src_x, src_y, src_x + src_width, src_y + src_height,
1893 dst_x, dst_y, dst_x + dst_width, dst_y + dst_height,
1894 false, false);
1895 }