intel/blorp: Rework our usage of ralloc when compiling shaders
[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 /* Try and compile with NIR first. If that fails, fall back to the old
1243 * method of building shaders manually.
1244 */
1245 nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
1246 struct brw_wm_prog_key wm_key;
1247 brw_blorp_init_wm_prog_key(&wm_key);
1248 wm_key.tex.compressed_multisample_layout_mask =
1249 prog_key->tex_aux_usage == ISL_AUX_USAGE_MCS;
1250 wm_key.tex.msaa_16 = prog_key->tex_samples == 16;
1251 wm_key.multisample_fbo = prog_key->rt_samples > 1;
1252
1253 program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
1254 &prog_data, &program_size);
1255
1256 blorp->upload_shader(blorp, prog_key, sizeof(*prog_key),
1257 program, program_size,
1258 &prog_data, sizeof(prog_data),
1259 &params->wm_prog_kernel, &params->wm_prog_data);
1260
1261 ralloc_free(mem_ctx);
1262 }
1263
1264 static void
1265 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
1266 GLfloat src0, GLfloat src1,
1267 GLfloat dst0, GLfloat dst1,
1268 bool mirror)
1269 {
1270 double scale = (double)(src1 - src0) / (double)(dst1 - dst0);
1271 if (!mirror) {
1272 /* When not mirroring a coordinate (say, X), we need:
1273 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1274 * Therefore:
1275 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1276 *
1277 * blorp program uses "round toward zero" to convert the
1278 * transformed floating point coordinates to integer coordinates,
1279 * whereas the behaviour we actually want is "round to nearest",
1280 * so 0.5 provides the necessary correction.
1281 */
1282 xform->multiplier = scale;
1283 xform->offset = src0 + (-(double)dst0 + 0.5) * scale;
1284 } else {
1285 /* When mirroring X we need:
1286 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1287 * Therefore:
1288 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1289 */
1290 xform->multiplier = -scale;
1291 xform->offset = src0 + ((double)dst1 - 0.5) * scale;
1292 }
1293 }
1294
1295 static inline void
1296 surf_get_intratile_offset_px(struct brw_blorp_surface_info *info,
1297 uint32_t *tile_x_px, uint32_t *tile_y_px)
1298 {
1299 if (info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1300 struct isl_extent2d px_size_sa =
1301 isl_get_interleaved_msaa_px_size_sa(info->surf.samples);
1302 assert(info->tile_x_sa % px_size_sa.width == 0);
1303 assert(info->tile_y_sa % px_size_sa.height == 0);
1304 *tile_x_px = info->tile_x_sa / px_size_sa.width;
1305 *tile_y_px = info->tile_y_sa / px_size_sa.height;
1306 } else {
1307 *tile_x_px = info->tile_x_sa;
1308 *tile_y_px = info->tile_y_sa;
1309 }
1310 }
1311
1312 static void
1313 surf_convert_to_single_slice(const struct isl_device *isl_dev,
1314 struct brw_blorp_surface_info *info)
1315 {
1316 /* Just bail if we have nothing to do. */
1317 if (info->surf.dim == ISL_SURF_DIM_2D &&
1318 info->view.base_level == 0 && info->view.base_array_layer == 0 &&
1319 info->surf.levels == 1 && info->surf.logical_level0_px.array_len == 1)
1320 return;
1321
1322 /* If this gets triggered then we've gotten here twice which. This
1323 * shouldn't happen thanks to the above early return.
1324 */
1325 assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
1326
1327 uint32_t layer = 0, z = 0;
1328 if (info->surf.dim == ISL_SURF_DIM_3D)
1329 z = info->view.base_array_layer + info->z_offset;
1330 else
1331 layer = info->view.base_array_layer;
1332
1333 uint32_t x_offset_sa, y_offset_sa;
1334 isl_surf_get_image_offset_sa(&info->surf, info->view.base_level,
1335 layer, z, &x_offset_sa, &y_offset_sa);
1336
1337 uint32_t byte_offset;
1338 isl_tiling_get_intratile_offset_sa(isl_dev, info->surf.tiling,
1339 info->surf.format, info->surf.row_pitch,
1340 x_offset_sa, y_offset_sa,
1341 &byte_offset,
1342 &info->tile_x_sa, &info->tile_y_sa);
1343 info->addr.offset += byte_offset;
1344
1345 const uint32_t slice_width_px =
1346 minify(info->surf.logical_level0_px.width, info->view.base_level);
1347 const uint32_t slice_height_px =
1348 minify(info->surf.logical_level0_px.height, info->view.base_level);
1349
1350 uint32_t tile_x_px, tile_y_px;
1351 surf_get_intratile_offset_px(info, &tile_x_px, &tile_y_px);
1352
1353 /* TODO: Once this file gets converted to C, we shouls just use designated
1354 * initializers.
1355 */
1356 struct isl_surf_init_info init_info = { 0, };
1357
1358 init_info.dim = ISL_SURF_DIM_2D;
1359 init_info.format = info->surf.format;
1360 init_info.width = slice_width_px + tile_x_px;
1361 init_info.height = slice_height_px + tile_y_px;
1362 init_info.depth = 1;
1363 init_info.levels = 1;
1364 init_info.array_len = 1;
1365 init_info.samples = info->surf.samples;
1366 init_info.min_pitch = info->surf.row_pitch;
1367 init_info.usage = info->surf.usage;
1368 init_info.tiling_flags = 1 << info->surf.tiling;
1369
1370 isl_surf_init_s(isl_dev, &info->surf, &init_info);
1371 assert(info->surf.row_pitch == init_info.min_pitch);
1372
1373 /* The view is also different now. */
1374 info->view.base_level = 0;
1375 info->view.levels = 1;
1376 info->view.base_array_layer = 0;
1377 info->view.array_len = 1;
1378 info->z_offset = 0;
1379 }
1380
1381 static void
1382 surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
1383 struct brw_blorp_surface_info *info)
1384 {
1385 assert(info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1386
1387 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1388 surf_convert_to_single_slice(isl_dev, info);
1389
1390 info->surf.logical_level0_px = info->surf.phys_level0_sa;
1391 info->surf.samples = 1;
1392 info->surf.msaa_layout = ISL_MSAA_LAYOUT_NONE;
1393 }
1394
1395 static void
1396 surf_retile_w_to_y(const struct isl_device *isl_dev,
1397 struct brw_blorp_surface_info *info)
1398 {
1399 assert(info->surf.tiling == ISL_TILING_W);
1400
1401 /* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1402 surf_convert_to_single_slice(isl_dev, info);
1403
1404 /* On gen7+, we don't have interleaved multisampling for color render
1405 * targets so we have to fake it.
1406 *
1407 * TODO: Are we sure we don't also need to fake it on gen6?
1408 */
1409 if (isl_dev->info->gen > 6 &&
1410 info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1411 surf_fake_interleaved_msaa(isl_dev, info);
1412 }
1413
1414 if (isl_dev->info->gen == 6) {
1415 /* Gen6 stencil buffers have a very large alignment coming in from the
1416 * miptree. It's out-of-bounds for what the surface state can handle.
1417 * Since we have a single layer and level, it doesn't really matter as
1418 * long as we don't pass a bogus value into isl_surf_fill_state().
1419 */
1420 info->surf.image_alignment_el = isl_extent3d(4, 2, 1);
1421 }
1422
1423 /* Now that we've converted everything to a simple 2-D surface with only
1424 * one miplevel, we can go about retiling it.
1425 */
1426 const unsigned x_align = 8, y_align = info->surf.samples != 0 ? 8 : 4;
1427 info->surf.tiling = ISL_TILING_Y0;
1428 info->surf.logical_level0_px.width =
1429 ALIGN(info->surf.logical_level0_px.width, x_align) * 2;
1430 info->surf.logical_level0_px.height =
1431 ALIGN(info->surf.logical_level0_px.height, y_align) / 2;
1432 info->tile_x_sa *= 2;
1433 info->tile_y_sa /= 2;
1434 }
1435
1436 static void
1437 do_blorp_blit(struct blorp_batch *batch,
1438 struct blorp_params *params,
1439 struct brw_blorp_blit_prog_key *wm_prog_key,
1440 float src_x0, float src_y0,
1441 float src_x1, float src_y1,
1442 float dst_x0, float dst_y0,
1443 float dst_x1, float dst_y1,
1444 bool mirror_x, bool mirror_y)
1445 {
1446 const struct gen_device_info *devinfo = batch->blorp->isl_dev->info;
1447
1448 if (isl_format_has_sint_channel(params->src.view.format)) {
1449 wm_prog_key->texture_data_type = nir_type_int;
1450 } else if (isl_format_has_uint_channel(params->src.view.format)) {
1451 wm_prog_key->texture_data_type = nir_type_uint;
1452 } else {
1453 wm_prog_key->texture_data_type = nir_type_float;
1454 }
1455
1456 /* src_samples and dst_samples are the true sample counts */
1457 wm_prog_key->src_samples = params->src.surf.samples;
1458 wm_prog_key->dst_samples = params->dst.surf.samples;
1459
1460 wm_prog_key->tex_aux_usage = params->src.aux_usage;
1461
1462 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1463 * dst.
1464 */
1465 wm_prog_key->src_layout = params->src.surf.msaa_layout;
1466 wm_prog_key->dst_layout = params->dst.surf.msaa_layout;
1467
1468 /* Round floating point values to nearest integer to avoid "off by one texel"
1469 * kind of errors when blitting.
1470 */
1471 params->x0 = params->wm_inputs.discard_rect.x0 = roundf(dst_x0);
1472 params->y0 = params->wm_inputs.discard_rect.y0 = roundf(dst_y0);
1473 params->x1 = params->wm_inputs.discard_rect.x1 = roundf(dst_x1);
1474 params->y1 = params->wm_inputs.discard_rect.y1 = roundf(dst_y1);
1475
1476 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[0],
1477 src_x0, src_x1, dst_x0, dst_x1, mirror_x);
1478 brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[1],
1479 src_y0, src_y1, dst_y0, dst_y1, mirror_y);
1480
1481 if (devinfo->gen > 6 &&
1482 params->dst.surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1483 assert(params->dst.surf.samples > 1);
1484
1485 /* We must expand the rectangle we send through the rendering pipeline,
1486 * to account for the fact that we are mapping the destination region as
1487 * single-sampled when it is in fact multisampled. We must also align
1488 * it to a multiple of the multisampling pattern, because the
1489 * differences between multisampled and single-sampled surface formats
1490 * will mean that pixels are scrambled within the multisampling pattern.
1491 * TODO: what if this makes the coordinates too large?
1492 *
1493 * Note: this only works if the destination surface uses the IMS layout.
1494 * If it's UMS, then we have no choice but to set up the rendering
1495 * pipeline as multisampled.
1496 */
1497 struct isl_extent2d px_size_sa =
1498 isl_get_interleaved_msaa_px_size_sa(params->dst.surf.samples);
1499 params->x0 = ROUND_DOWN_TO(params->x0, 2) * px_size_sa.width;
1500 params->y0 = ROUND_DOWN_TO(params->y0, 2) * px_size_sa.height;
1501 params->x1 = ALIGN(params->x1, 2) * px_size_sa.width;
1502 params->y1 = ALIGN(params->y1, 2) * px_size_sa.height;
1503
1504 surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params->dst);
1505
1506 wm_prog_key->use_kill = true;
1507 wm_prog_key->need_dst_offset = true;
1508 }
1509
1510 if (params->dst.surf.tiling == ISL_TILING_W) {
1511 /* We must modify the rectangle we send through the rendering pipeline
1512 * (and the size and x/y offset of the destination surface), to account
1513 * for the fact that we are mapping it as Y-tiled when it is in fact
1514 * W-tiled.
1515 *
1516 * Both Y tiling and W tiling can be understood as organizations of
1517 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1518 * is different, but the layout of the 32-byte sub-tiles within the 4k
1519 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1520 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1521 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1522 *
1523 * Therefore, to account for the layout differences within the 32-byte
1524 * sub-tiles, we must expand the rectangle so the X coordinates of its
1525 * edges are multiples of 8 (the W sub-tile width), and its Y
1526 * coordinates of its edges are multiples of 4 (the W sub-tile height).
1527 * Then we need to scale the X and Y coordinates of the rectangle to
1528 * account for the differences in aspect ratio between the Y and W
1529 * sub-tiles. We need to modify the layer width and height similarly.
1530 *
1531 * A correction needs to be applied when MSAA is in use: since
1532 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1533 * we need to align the Y coordinates to multiples of 8, so that when
1534 * they are divided by two they are still multiples of 4.
1535 *
1536 * Note: Since the x/y offset of the surface will be applied using the
1537 * SURFACE_STATE command packet, it will be invisible to the swizzling
1538 * code in the shader; therefore it needs to be in a multiple of the
1539 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1540 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1541 * buffer), and the miplevel alignment used for stencil buffers is 8
1542 * pixels horizontally and either 4 or 8 pixels vertically (see
1543 * intel_horizontal_texture_alignment_unit() and
1544 * intel_vertical_texture_alignment_unit()).
1545 *
1546 * Note: Also, since the SURFACE_STATE command packet can only apply
1547 * offsets that are multiples of 4 pixels horizontally and 2 pixels
1548 * vertically, it is important that the offsets will be multiples of
1549 * these sizes after they are converted into Y-tiled coordinates.
1550 * Fortunately they will be, since we know from above that the offsets
1551 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
1552 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1553 *
1554 * TODO: what if this makes the coordinates (or the texture size) too
1555 * large?
1556 */
1557 const unsigned x_align = 8;
1558 const unsigned y_align = params->dst.surf.samples != 0 ? 8 : 4;
1559 params->x0 = ROUND_DOWN_TO(params->x0, x_align) * 2;
1560 params->y0 = ROUND_DOWN_TO(params->y0, y_align) / 2;
1561 params->x1 = ALIGN(params->x1, x_align) * 2;
1562 params->y1 = ALIGN(params->y1, y_align) / 2;
1563
1564 /* Retile the surface to Y-tiled */
1565 surf_retile_w_to_y(batch->blorp->isl_dev, &params->dst);
1566
1567 wm_prog_key->dst_tiled_w = true;
1568 wm_prog_key->use_kill = true;
1569 wm_prog_key->need_dst_offset = true;
1570
1571 if (params->dst.surf.samples > 1) {
1572 /* If the destination surface is a W-tiled multisampled stencil
1573 * buffer that we're mapping as Y tiled, then we need to arrange for
1574 * the WM program to run once per sample rather than once per pixel,
1575 * because the memory layout of related samples doesn't match between
1576 * W and Y tiling.
1577 */
1578 wm_prog_key->persample_msaa_dispatch = true;
1579 }
1580 }
1581
1582 if (devinfo->gen < 8 && params->src.surf.tiling == ISL_TILING_W) {
1583 /* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1584 * Broadwell adds support for sampling from stencil.
1585 *
1586 * See the comments above concerning x/y offset alignment for the
1587 * destination surface.
1588 *
1589 * TODO: what if this makes the texture size too large?
1590 */
1591 surf_retile_w_to_y(batch->blorp->isl_dev, &params->src);
1592
1593 wm_prog_key->src_tiled_w = true;
1594 wm_prog_key->need_src_offset = true;
1595 }
1596
1597 /* tex_samples and rt_samples are the sample counts that are set up in
1598 * SURFACE_STATE.
1599 */
1600 wm_prog_key->tex_samples = params->src.surf.samples;
1601 wm_prog_key->rt_samples = params->dst.surf.samples;
1602
1603 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1604 * use to access the source and destination surfaces.
1605 */
1606 wm_prog_key->tex_layout = params->src.surf.msaa_layout;
1607 wm_prog_key->rt_layout = params->dst.surf.msaa_layout;
1608
1609 if (params->src.surf.samples > 0 && params->dst.surf.samples > 1) {
1610 /* We are blitting from a multisample buffer to a multisample buffer, so
1611 * we must preserve samples within a pixel. This means we have to
1612 * arrange for the WM program to run once per sample rather than once
1613 * per pixel.
1614 */
1615 wm_prog_key->persample_msaa_dispatch = true;
1616 }
1617
1618 if (params->src.tile_x_sa || params->src.tile_y_sa) {
1619 assert(wm_prog_key->need_src_offset);
1620 surf_get_intratile_offset_px(&params->src,
1621 &params->wm_inputs.src_offset.x,
1622 &params->wm_inputs.src_offset.y);
1623 }
1624
1625 if (params->dst.tile_x_sa || params->dst.tile_y_sa) {
1626 assert(wm_prog_key->need_dst_offset);
1627 surf_get_intratile_offset_px(&params->dst,
1628 &params->wm_inputs.dst_offset.x,
1629 &params->wm_inputs.dst_offset.y);
1630 params->x0 += params->wm_inputs.dst_offset.x;
1631 params->y0 += params->wm_inputs.dst_offset.y;
1632 params->x1 += params->wm_inputs.dst_offset.x;
1633 params->y1 += params->wm_inputs.dst_offset.y;
1634 }
1635
1636 /* For some texture types, we need to pass the layer through the sampler. */
1637 params->wm_inputs.src_z = params->src.z_offset;
1638
1639 brw_blorp_get_blit_kernel(batch->blorp, params, wm_prog_key);
1640
1641 batch->blorp->exec(batch, params);
1642 }
1643
1644 void
1645 blorp_blit(struct blorp_batch *batch,
1646 const struct blorp_surf *src_surf,
1647 unsigned src_level, unsigned src_layer,
1648 enum isl_format src_format, struct isl_swizzle src_swizzle,
1649 const struct blorp_surf *dst_surf,
1650 unsigned dst_level, unsigned dst_layer,
1651 enum isl_format dst_format, struct isl_swizzle dst_swizzle,
1652 float src_x0, float src_y0,
1653 float src_x1, float src_y1,
1654 float dst_x0, float dst_y0,
1655 float dst_x1, float dst_y1,
1656 GLenum filter, bool mirror_x, bool mirror_y)
1657 {
1658 struct blorp_params params;
1659 blorp_params_init(&params);
1660
1661 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
1662 src_layer, src_format, false);
1663 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
1664 dst_layer, dst_format, true);
1665
1666 params.src.view.swizzle = src_swizzle;
1667 params.dst.view.swizzle = dst_swizzle;
1668
1669 struct brw_blorp_blit_prog_key wm_prog_key;
1670 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1671
1672 /* Scaled blitting or not. */
1673 wm_prog_key.blit_scaled =
1674 ((dst_x1 - dst_x0) == (src_x1 - src_x0) &&
1675 (dst_y1 - dst_y0) == (src_y1 - src_y0)) ? false : true;
1676
1677 /* Scaling factors used for bilinear filtering in multisample scaled
1678 * blits.
1679 */
1680 if (params.src.surf.samples == 16)
1681 wm_prog_key.x_scale = 4.0f;
1682 else
1683 wm_prog_key.x_scale = 2.0f;
1684 wm_prog_key.y_scale = params.src.surf.samples / wm_prog_key.x_scale;
1685
1686 if (filter == GL_LINEAR &&
1687 params.src.surf.samples <= 1 && params.dst.surf.samples <= 1)
1688 wm_prog_key.bilinear_filter = true;
1689
1690 if ((params.src.surf.usage & ISL_SURF_USAGE_DEPTH_BIT) == 0 &&
1691 (params.src.surf.usage & ISL_SURF_USAGE_STENCIL_BIT) == 0 &&
1692 !isl_format_has_int_channel(params.src.surf.format) &&
1693 params.src.surf.samples > 1 && params.dst.surf.samples <= 1) {
1694 /* We are downsampling a non-integer color buffer, so blend.
1695 *
1696 * Regarding integer color buffers, the OpenGL ES 3.2 spec says:
1697 *
1698 * "If the source formats are integer types or stencil values, a
1699 * single sample's value is selected for each pixel."
1700 *
1701 * This implies we should not blend in that case.
1702 */
1703 wm_prog_key.blend = true;
1704 }
1705
1706 params.wm_inputs.rect_grid.x1 =
1707 minify(params.src.surf.logical_level0_px.width, src_level) *
1708 wm_prog_key.x_scale - 1.0f;
1709 params.wm_inputs.rect_grid.y1 =
1710 minify(params.src.surf.logical_level0_px.height, src_level) *
1711 wm_prog_key.y_scale - 1.0f;
1712
1713 do_blorp_blit(batch, &params, &wm_prog_key,
1714 src_x0, src_y0, src_x1, src_y1,
1715 dst_x0, dst_y0, dst_x1, dst_y1,
1716 mirror_x, mirror_y);
1717 }
1718
1719 static enum isl_format
1720 get_copy_format_for_bpb(unsigned bpb)
1721 {
1722 /* The choice of UNORM and UINT formats is very intentional here. Most of
1723 * the time, we want to use a UINT format to avoid any rounding error in
1724 * the blit. For stencil blits, R8_UINT is required by the hardware.
1725 * (It's the only format allowed in conjunction with W-tiling.) Also we
1726 * intentionally use the 4-channel formats whenever we can. This is so
1727 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
1728 * though one of them is 3/4 the size of the other. The choice of UNORM
1729 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
1730 * 16-bit RGB UINT formats at all so we have to use UNORM there.
1731 * Fortunately, the only time we should ever use two different formats in
1732 * the table below is for RGB -> RGBA blits and so we will never have any
1733 * UNORM/UINT mismatch.
1734 */
1735 switch (bpb) {
1736 case 8: return ISL_FORMAT_R8_UINT;
1737 case 16: return ISL_FORMAT_R8G8_UINT;
1738 case 24: return ISL_FORMAT_R8G8B8_UNORM;
1739 case 32: return ISL_FORMAT_R8G8B8A8_UNORM;
1740 case 48: return ISL_FORMAT_R16G16B16_UNORM;
1741 case 64: return ISL_FORMAT_R16G16B16A16_UNORM;
1742 case 96: return ISL_FORMAT_R32G32B32_UINT;
1743 case 128:return ISL_FORMAT_R32G32B32A32_UINT;
1744 default:
1745 unreachable("Unknown format bpb");
1746 }
1747 }
1748
1749 static void
1750 surf_convert_to_uncompressed(const struct isl_device *isl_dev,
1751 struct brw_blorp_surface_info *info,
1752 uint32_t *x, uint32_t *y,
1753 uint32_t *width, uint32_t *height)
1754 {
1755 const struct isl_format_layout *fmtl =
1756 isl_format_get_layout(info->surf.format);
1757
1758 assert(fmtl->bw > 1 || fmtl->bh > 1);
1759
1760 /* This is a compressed surface. We need to convert it to a single
1761 * slice (because compressed layouts don't perfectly match uncompressed
1762 * ones with the same bpb) and divide x, y, width, and height by the
1763 * block size.
1764 */
1765 surf_convert_to_single_slice(isl_dev, info);
1766
1767 if (width || height) {
1768 #ifndef NDEBUG
1769 uint32_t right_edge_px = info->tile_x_sa + *x + *width;
1770 uint32_t bottom_edge_px = info->tile_y_sa + *y + *height;
1771 assert(*width % fmtl->bw == 0 ||
1772 right_edge_px == info->surf.logical_level0_px.width);
1773 assert(*height % fmtl->bh == 0 ||
1774 bottom_edge_px == info->surf.logical_level0_px.height);
1775 #endif
1776 *width = DIV_ROUND_UP(*width, fmtl->bw);
1777 *height = DIV_ROUND_UP(*height, fmtl->bh);
1778 }
1779
1780 assert(*x % fmtl->bw == 0);
1781 assert(*y % fmtl->bh == 0);
1782 *x /= fmtl->bw;
1783 *y /= fmtl->bh;
1784
1785 info->surf.logical_level0_px.width =
1786 DIV_ROUND_UP(info->surf.logical_level0_px.width, fmtl->bw);
1787 info->surf.logical_level0_px.height =
1788 DIV_ROUND_UP(info->surf.logical_level0_px.height, fmtl->bh);
1789
1790 assert(info->surf.phys_level0_sa.width % fmtl->bw == 0);
1791 assert(info->surf.phys_level0_sa.height % fmtl->bh == 0);
1792 info->surf.phys_level0_sa.width /= fmtl->bw;
1793 info->surf.phys_level0_sa.height /= fmtl->bh;
1794
1795 assert(info->tile_x_sa % fmtl->bw == 0);
1796 assert(info->tile_y_sa % fmtl->bh == 0);
1797 info->tile_x_sa /= fmtl->bw;
1798 info->tile_y_sa /= fmtl->bh;
1799
1800 /* It's now an uncompressed surface so we need an uncompressed format */
1801 info->surf.format = get_copy_format_for_bpb(fmtl->bpb);
1802 }
1803
1804 static void
1805 surf_fake_rgb_with_red(const struct isl_device *isl_dev,
1806 struct brw_blorp_surface_info *info,
1807 uint32_t *x, uint32_t *width)
1808 {
1809 surf_convert_to_single_slice(isl_dev, info);
1810
1811 info->surf.logical_level0_px.width *= 3;
1812 info->surf.phys_level0_sa.width *= 3;
1813 *x *= 3;
1814 *width *= 3;
1815
1816 enum isl_format red_format;
1817 switch (info->view.format) {
1818 case ISL_FORMAT_R8G8B8_UNORM:
1819 red_format = ISL_FORMAT_R8_UNORM;
1820 break;
1821 case ISL_FORMAT_R16G16B16_UNORM:
1822 red_format = ISL_FORMAT_R16_UNORM;
1823 break;
1824 case ISL_FORMAT_R32G32B32_UINT:
1825 red_format = ISL_FORMAT_R32_UINT;
1826 break;
1827 default:
1828 unreachable("Invalid RGB copy destination format");
1829 }
1830 assert(isl_format_get_layout(red_format)->channels.r.type ==
1831 isl_format_get_layout(info->view.format)->channels.r.type);
1832 assert(isl_format_get_layout(red_format)->channels.r.bits ==
1833 isl_format_get_layout(info->view.format)->channels.r.bits);
1834
1835 info->surf.format = info->view.format = red_format;
1836 }
1837
1838 void
1839 blorp_copy(struct blorp_batch *batch,
1840 const struct blorp_surf *src_surf,
1841 unsigned src_level, unsigned src_layer,
1842 const struct blorp_surf *dst_surf,
1843 unsigned dst_level, unsigned dst_layer,
1844 uint32_t src_x, uint32_t src_y,
1845 uint32_t dst_x, uint32_t dst_y,
1846 uint32_t src_width, uint32_t src_height)
1847 {
1848 struct blorp_params params;
1849
1850 if (src_width == 0 || src_height == 0)
1851 return;
1852
1853 blorp_params_init(&params);
1854 brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
1855 src_layer, ISL_FORMAT_UNSUPPORTED, false);
1856 brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
1857 dst_layer, ISL_FORMAT_UNSUPPORTED, true);
1858
1859 struct brw_blorp_blit_prog_key wm_prog_key;
1860 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1861
1862 const struct isl_format_layout *src_fmtl =
1863 isl_format_get_layout(params.src.surf.format);
1864 const struct isl_format_layout *dst_fmtl =
1865 isl_format_get_layout(params.dst.surf.format);
1866
1867 params.src.view.format = get_copy_format_for_bpb(src_fmtl->bpb);
1868 if (src_fmtl->bw > 1 || src_fmtl->bh > 1) {
1869 surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.src,
1870 &src_x, &src_y, &src_width, &src_height);
1871 wm_prog_key.need_src_offset = true;
1872 }
1873
1874 params.dst.view.format = get_copy_format_for_bpb(dst_fmtl->bpb);
1875 if (dst_fmtl->bw > 1 || dst_fmtl->bh > 1) {
1876 surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
1877 &dst_x, &dst_y, NULL, NULL);
1878 wm_prog_key.need_dst_offset = true;
1879 }
1880
1881 /* Once both surfaces are stompped to uncompressed as needed, the
1882 * destination size is the same as the source size.
1883 */
1884 uint32_t dst_width = src_width;
1885 uint32_t dst_height = src_height;
1886
1887 if (dst_fmtl->bpb % 3 == 0) {
1888 surf_fake_rgb_with_red(batch->blorp->isl_dev, &params.dst,
1889 &dst_x, &dst_width);
1890 wm_prog_key.dst_rgb = true;
1891 wm_prog_key.need_dst_offset = true;
1892 }
1893
1894 do_blorp_blit(batch, &params, &wm_prog_key,
1895 src_x, src_y, src_x + src_width, src_y + src_height,
1896 dst_x, dst_y, dst_x + dst_width, dst_y + dst_height,
1897 false, false);
1898 }