Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/intel/blorp/blorp_blit.c
7178 views
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 "blorp_nir_builder.h"
25
#include "compiler/nir/nir_format_convert.h"
26
27
#include "blorp_priv.h"
28
29
#include "util/format_rgb9e5.h"
30
/* header-only include needed for _mesa_unorm_to_float and friends. */
31
#include "mesa/main/format_utils.h"
32
#include "util/u_math.h"
33
34
#define FILE_DEBUG_FLAG DEBUG_BLORP
35
36
static const bool split_blorp_blit_debug = false;
37
38
struct brw_blorp_blit_vars {
39
/* Input values from brw_blorp_wm_inputs */
40
nir_variable *v_discard_rect;
41
nir_variable *v_rect_grid;
42
nir_variable *v_coord_transform;
43
nir_variable *v_src_z;
44
nir_variable *v_src_offset;
45
nir_variable *v_dst_offset;
46
nir_variable *v_src_inv_size;
47
};
48
49
static void
50
brw_blorp_blit_vars_init(nir_builder *b, struct brw_blorp_blit_vars *v,
51
const struct brw_blorp_blit_prog_key *key)
52
{
53
#define LOAD_INPUT(name, type)\
54
v->v_##name = BLORP_CREATE_NIR_INPUT(b->shader, name, type);
55
56
LOAD_INPUT(discard_rect, glsl_vec4_type())
57
LOAD_INPUT(rect_grid, glsl_vec4_type())
58
LOAD_INPUT(coord_transform, glsl_vec4_type())
59
LOAD_INPUT(src_z, glsl_float_type())
60
LOAD_INPUT(src_offset, glsl_vector_type(GLSL_TYPE_UINT, 2))
61
LOAD_INPUT(dst_offset, glsl_vector_type(GLSL_TYPE_UINT, 2))
62
LOAD_INPUT(src_inv_size, glsl_vector_type(GLSL_TYPE_FLOAT, 2))
63
64
#undef LOAD_INPUT
65
}
66
67
static nir_ssa_def *
68
blorp_blit_get_frag_coords(nir_builder *b,
69
const struct brw_blorp_blit_prog_key *key,
70
struct brw_blorp_blit_vars *v)
71
{
72
nir_ssa_def *coord = nir_f2i32(b, nir_load_frag_coord(b));
73
74
/* Account for destination surface intratile offset
75
*
76
* Transformation parameters giving translation from destination to source
77
* coordinates don't take into account possible intra-tile destination
78
* offset. Therefore it has to be first subtracted from the incoming
79
* coordinates. Vertices are set up based on coordinates containing the
80
* intra-tile offset.
81
*/
82
if (key->need_dst_offset)
83
coord = nir_isub(b, coord, nir_load_var(b, v->v_dst_offset));
84
85
if (key->persample_msaa_dispatch) {
86
return nir_vec3(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1),
87
nir_load_sample_id(b));
88
} else {
89
return nir_vec2(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1));
90
}
91
}
92
93
/**
94
* Emit code to translate from destination (X, Y) coordinates to source (X, Y)
95
* coordinates.
96
*/
97
static nir_ssa_def *
98
blorp_blit_apply_transform(nir_builder *b, nir_ssa_def *src_pos,
99
struct brw_blorp_blit_vars *v)
100
{
101
nir_ssa_def *coord_transform = nir_load_var(b, v->v_coord_transform);
102
103
nir_ssa_def *offset = nir_vec2(b, nir_channel(b, coord_transform, 1),
104
nir_channel(b, coord_transform, 3));
105
nir_ssa_def *mul = nir_vec2(b, nir_channel(b, coord_transform, 0),
106
nir_channel(b, coord_transform, 2));
107
108
return nir_fadd(b, nir_fmul(b, src_pos, mul), offset);
109
}
110
111
static inline void
112
blorp_nir_discard_if_outside_rect(nir_builder *b, nir_ssa_def *pos,
113
struct brw_blorp_blit_vars *v)
114
{
115
nir_ssa_def *c0, *c1, *c2, *c3;
116
nir_ssa_def *discard_rect = nir_load_var(b, v->v_discard_rect);
117
nir_ssa_def *dst_x0 = nir_channel(b, discard_rect, 0);
118
nir_ssa_def *dst_x1 = nir_channel(b, discard_rect, 1);
119
nir_ssa_def *dst_y0 = nir_channel(b, discard_rect, 2);
120
nir_ssa_def *dst_y1 = nir_channel(b, discard_rect, 3);
121
122
c0 = nir_ult(b, nir_channel(b, pos, 0), dst_x0);
123
c1 = nir_uge(b, nir_channel(b, pos, 0), dst_x1);
124
c2 = nir_ult(b, nir_channel(b, pos, 1), dst_y0);
125
c3 = nir_uge(b, nir_channel(b, pos, 1), dst_y1);
126
127
nir_ssa_def *oob = nir_ior(b, nir_ior(b, c0, c1), nir_ior(b, c2, c3));
128
nir_discard_if(b, oob);
129
}
130
131
static nir_tex_instr *
132
blorp_create_nir_tex_instr(nir_builder *b, struct brw_blorp_blit_vars *v,
133
nir_texop op, nir_ssa_def *pos, unsigned num_srcs,
134
nir_alu_type dst_type)
135
{
136
nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
137
138
tex->op = op;
139
140
tex->dest_type = dst_type | 32;
141
tex->is_array = false;
142
tex->is_shadow = false;
143
144
/* Blorp only has one texture and it's bound at unit 0 */
145
tex->texture_index = 0;
146
tex->sampler_index = 0;
147
148
/* To properly handle 3-D and 2-D array textures, we pull the Z component
149
* from an input. TODO: This is a bit magic; we should probably make this
150
* more explicit in the future.
151
*/
152
assert(pos->num_components >= 2);
153
if (op == nir_texop_txf || op == nir_texop_txf_ms || op == nir_texop_txf_ms_mcs) {
154
pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
155
nir_f2i32(b, nir_load_var(b, v->v_src_z)));
156
} else {
157
pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
158
nir_load_var(b, v->v_src_z));
159
}
160
161
tex->src[0].src_type = nir_tex_src_coord;
162
tex->src[0].src = nir_src_for_ssa(pos);
163
tex->coord_components = 3;
164
165
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
166
167
return tex;
168
}
169
170
static nir_ssa_def *
171
blorp_nir_tex(nir_builder *b, struct brw_blorp_blit_vars *v,
172
const struct brw_blorp_blit_prog_key *key, nir_ssa_def *pos)
173
{
174
if (key->need_src_offset)
175
pos = nir_fadd(b, pos, nir_i2f32(b, nir_load_var(b, v->v_src_offset)));
176
177
/* If the sampler requires normalized coordinates, we need to compensate. */
178
if (key->src_coords_normalized)
179
pos = nir_fmul(b, pos, nir_load_var(b, v->v_src_inv_size));
180
181
nir_tex_instr *tex =
182
blorp_create_nir_tex_instr(b, v, nir_texop_tex, pos, 2,
183
key->texture_data_type);
184
185
assert(pos->num_components == 2);
186
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
187
tex->src[1].src_type = nir_tex_src_lod;
188
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
189
190
nir_builder_instr_insert(b, &tex->instr);
191
192
return &tex->dest.ssa;
193
}
194
195
static nir_ssa_def *
196
blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
197
nir_ssa_def *pos, nir_alu_type dst_type)
198
{
199
nir_tex_instr *tex =
200
blorp_create_nir_tex_instr(b, v, nir_texop_txf, pos, 2, dst_type);
201
202
tex->sampler_dim = GLSL_SAMPLER_DIM_3D;
203
tex->src[1].src_type = nir_tex_src_lod;
204
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
205
206
nir_builder_instr_insert(b, &tex->instr);
207
208
return &tex->dest.ssa;
209
}
210
211
static nir_ssa_def *
212
blorp_nir_txf_ms(nir_builder *b, struct brw_blorp_blit_vars *v,
213
nir_ssa_def *pos, nir_ssa_def *mcs, nir_alu_type dst_type)
214
{
215
nir_tex_instr *tex =
216
blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms, pos,
217
mcs != NULL ? 3 : 2, dst_type);
218
219
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
220
221
tex->src[1].src_type = nir_tex_src_ms_index;
222
if (pos->num_components == 2) {
223
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
224
} else {
225
assert(pos->num_components == 3);
226
tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
227
}
228
229
if (mcs) {
230
tex->src[2].src_type = nir_tex_src_ms_mcs;
231
tex->src[2].src = nir_src_for_ssa(mcs);
232
}
233
234
nir_builder_instr_insert(b, &tex->instr);
235
236
return &tex->dest.ssa;
237
}
238
239
static nir_ssa_def *
240
blorp_blit_txf_ms_mcs(nir_builder *b, struct brw_blorp_blit_vars *v,
241
nir_ssa_def *pos)
242
{
243
nir_tex_instr *tex =
244
blorp_create_nir_tex_instr(b, v, nir_texop_txf_ms_mcs,
245
pos, 1, nir_type_int);
246
247
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
248
249
nir_builder_instr_insert(b, &tex->instr);
250
251
return &tex->dest.ssa;
252
}
253
254
/**
255
* Emit code to compensate for the difference between Y and W tiling.
256
*
257
* This code modifies the X and Y coordinates according to the formula:
258
*
259
* (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
260
*
261
* (See brw_blorp_build_nir_shader).
262
*/
263
static inline nir_ssa_def *
264
blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
265
{
266
assert(pos->num_components == 2);
267
nir_ssa_def *x_Y = nir_channel(b, pos, 0);
268
nir_ssa_def *y_Y = nir_channel(b, pos, 1);
269
270
/* Given X and Y coordinates that describe an address using Y tiling,
271
* translate to the X and Y coordinates that describe the same address
272
* using W tiling.
273
*
274
* If we break down the low order bits of X and Y, using a
275
* single letter to represent each low-order bit:
276
*
277
* X = A << 7 | 0bBCDEFGH
278
* Y = J << 5 | 0bKLMNP (1)
279
*
280
* Then we can apply the Y tiling formula to see the memory offset being
281
* addressed:
282
*
283
* offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
284
*
285
* If we apply the W detiling formula to this memory location, that the
286
* corresponding X' and Y' coordinates are:
287
*
288
* X' = A << 6 | 0bBCDPFH (3)
289
* Y' = J << 6 | 0bKLMNEG
290
*
291
* Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
292
* we need to make the following computation:
293
*
294
* X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
295
* Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
296
*/
297
nir_ssa_def *x_W = nir_imm_int(b, 0);
298
x_W = nir_mask_shift_or(b, x_W, x_Y, 0xfffffff4, -1);
299
x_W = nir_mask_shift_or(b, x_W, y_Y, 0x1, 2);
300
x_W = nir_mask_shift_or(b, x_W, x_Y, 0x1, 0);
301
302
nir_ssa_def *y_W = nir_imm_int(b, 0);
303
y_W = nir_mask_shift_or(b, y_W, y_Y, 0xfffffffe, 1);
304
y_W = nir_mask_shift_or(b, y_W, x_Y, 0x8, -2);
305
y_W = nir_mask_shift_or(b, y_W, x_Y, 0x2, -1);
306
307
return nir_vec2(b, x_W, y_W);
308
}
309
310
/**
311
* Emit code to compensate for the difference between Y and W tiling.
312
*
313
* This code modifies the X and Y coordinates according to the formula:
314
*
315
* (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
316
*
317
* (See brw_blorp_build_nir_shader).
318
*/
319
static inline nir_ssa_def *
320
blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
321
{
322
assert(pos->num_components == 2);
323
nir_ssa_def *x_W = nir_channel(b, pos, 0);
324
nir_ssa_def *y_W = nir_channel(b, pos, 1);
325
326
/* Applying the same logic as above, but in reverse, we obtain the
327
* formulas:
328
*
329
* X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
330
* Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
331
*/
332
nir_ssa_def *x_Y = nir_imm_int(b, 0);
333
x_Y = nir_mask_shift_or(b, x_Y, x_W, 0xfffffffa, 1);
334
x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x2, 2);
335
x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x1, 1);
336
x_Y = nir_mask_shift_or(b, x_Y, x_W, 0x1, 0);
337
338
nir_ssa_def *y_Y = nir_imm_int(b, 0);
339
y_Y = nir_mask_shift_or(b, y_Y, y_W, 0xfffffffc, -1);
340
y_Y = nir_mask_shift_or(b, y_Y, x_W, 0x4, -2);
341
342
return nir_vec2(b, x_Y, y_Y);
343
}
344
345
/**
346
* Emit code to compensate for the difference between MSAA and non-MSAA
347
* surfaces.
348
*
349
* This code modifies the X and Y coordinates according to the formula:
350
*
351
* (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
352
*
353
* (See brw_blorp_blit_program).
354
*/
355
static inline nir_ssa_def *
356
blorp_nir_encode_msaa(nir_builder *b, nir_ssa_def *pos,
357
unsigned num_samples, enum isl_msaa_layout layout)
358
{
359
assert(pos->num_components == 2 || pos->num_components == 3);
360
361
switch (layout) {
362
case ISL_MSAA_LAYOUT_NONE:
363
assert(pos->num_components == 2);
364
return pos;
365
case ISL_MSAA_LAYOUT_ARRAY:
366
/* No translation needed */
367
return pos;
368
case ISL_MSAA_LAYOUT_INTERLEAVED: {
369
nir_ssa_def *x_in = nir_channel(b, pos, 0);
370
nir_ssa_def *y_in = nir_channel(b, pos, 1);
371
nir_ssa_def *s_in = pos->num_components == 2 ? nir_imm_int(b, 0) :
372
nir_channel(b, pos, 2);
373
374
nir_ssa_def *x_out = nir_imm_int(b, 0);
375
nir_ssa_def *y_out = nir_imm_int(b, 0);
376
switch (num_samples) {
377
case 2:
378
case 4:
379
/* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
380
* where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
381
* Y' = Y
382
*
383
* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
384
* where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
385
* Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
386
*/
387
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 1);
388
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
389
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
390
if (num_samples == 2) {
391
y_out = y_in;
392
} else {
393
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
394
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
395
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
396
}
397
break;
398
399
case 8:
400
/* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
401
* where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
402
* | (X & 0b1)
403
* Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
404
*/
405
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
406
x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
407
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
408
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
409
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
410
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
411
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
412
break;
413
414
case 16:
415
/* encode_msaa(16, IMS, X, Y, S) = (X', Y', 0)
416
* where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
417
* | (X & 0b1)
418
* Y' = (Y & ~0b1) << 2 | (S & 0b1000) >> 1 (S & 0b10)
419
* | (Y & 0b1)
420
*/
421
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
422
x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
423
x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
424
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
425
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 2);
426
y_out = nir_mask_shift_or(b, y_out, s_in, 0x8, -1);
427
y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
428
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
429
break;
430
431
default:
432
unreachable("Invalid number of samples for IMS layout");
433
}
434
435
return nir_vec2(b, x_out, y_out);
436
}
437
438
default:
439
unreachable("Invalid MSAA layout");
440
}
441
}
442
443
/**
444
* Emit code to compensate for the difference between MSAA and non-MSAA
445
* surfaces.
446
*
447
* This code modifies the X and Y coordinates according to the formula:
448
*
449
* (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
450
*
451
* (See brw_blorp_blit_program).
452
*/
453
static inline nir_ssa_def *
454
blorp_nir_decode_msaa(nir_builder *b, nir_ssa_def *pos,
455
unsigned num_samples, enum isl_msaa_layout layout)
456
{
457
assert(pos->num_components == 2 || pos->num_components == 3);
458
459
switch (layout) {
460
case ISL_MSAA_LAYOUT_NONE:
461
/* No translation necessary, and S should already be zero. */
462
assert(pos->num_components == 2);
463
return pos;
464
case ISL_MSAA_LAYOUT_ARRAY:
465
/* No translation necessary. */
466
return pos;
467
case ISL_MSAA_LAYOUT_INTERLEAVED: {
468
assert(pos->num_components == 2);
469
470
nir_ssa_def *x_in = nir_channel(b, pos, 0);
471
nir_ssa_def *y_in = nir_channel(b, pos, 1);
472
473
nir_ssa_def *x_out = nir_imm_int(b, 0);
474
nir_ssa_def *y_out = nir_imm_int(b, 0);
475
nir_ssa_def *s_out = nir_imm_int(b, 0);
476
switch (num_samples) {
477
case 2:
478
case 4:
479
/* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
480
* where X' = (X & ~0b11) >> 1 | (X & 0b1)
481
* S = (X & 0b10) >> 1
482
*
483
* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
484
* where X' = (X & ~0b11) >> 1 | (X & 0b1)
485
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
486
* S = (Y & 0b10) | (X & 0b10) >> 1
487
*/
488
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffc, -1);
489
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
490
if (num_samples == 2) {
491
y_out = y_in;
492
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
493
} else {
494
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
495
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
496
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
497
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
498
}
499
break;
500
501
case 8:
502
/* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
503
* where X' = (X & ~0b111) >> 2 | (X & 0b1)
504
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
505
* S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
506
*/
507
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
508
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
509
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
510
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
511
s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
512
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
513
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
514
break;
515
516
case 16:
517
/* decode_msaa(16, IMS, X, Y, 0) = (X', Y', S)
518
* where X' = (X & ~0b111) >> 2 | (X & 0b1)
519
* Y' = (Y & ~0b111) >> 2 | (Y & 0b1)
520
* S = (Y & 0b100) << 1 | (X & 0b100) |
521
* (Y & 0b10) | (X & 0b10) >> 1
522
*/
523
x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
524
x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
525
y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffff8, -2);
526
y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
527
s_out = nir_mask_shift_or(b, s_out, y_in, 0x4, 1);
528
s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
529
s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
530
s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
531
break;
532
533
default:
534
unreachable("Invalid number of samples for IMS layout");
535
}
536
537
return nir_vec3(b, x_out, y_out, s_out);
538
}
539
540
default:
541
unreachable("Invalid MSAA layout");
542
}
543
}
544
545
/**
546
* Count the number of trailing 1 bits in the given value. For example:
547
*
548
* count_trailing_one_bits(0) == 0
549
* count_trailing_one_bits(7) == 3
550
* count_trailing_one_bits(11) == 2
551
*/
552
static inline int count_trailing_one_bits(unsigned value)
553
{
554
#ifdef HAVE___BUILTIN_CTZ
555
return __builtin_ctz(~value);
556
#else
557
return util_bitcount(value & ~(value + 1));
558
#endif
559
}
560
561
static nir_ssa_def *
562
blorp_nir_combine_samples(nir_builder *b, struct brw_blorp_blit_vars *v,
563
nir_ssa_def *pos, unsigned tex_samples,
564
enum isl_aux_usage tex_aux_usage,
565
nir_alu_type dst_type,
566
enum blorp_filter filter)
567
{
568
nir_variable *color =
569
nir_local_variable_create(b->impl, glsl_vec4_type(), "color");
570
571
nir_ssa_def *mcs = NULL;
572
if (isl_aux_usage_has_mcs(tex_aux_usage))
573
mcs = blorp_blit_txf_ms_mcs(b, v, pos);
574
575
nir_op combine_op;
576
switch (filter) {
577
case BLORP_FILTER_AVERAGE:
578
assert(dst_type == nir_type_float);
579
combine_op = nir_op_fadd;
580
break;
581
582
case BLORP_FILTER_MIN_SAMPLE:
583
switch (dst_type) {
584
case nir_type_int: combine_op = nir_op_imin; break;
585
case nir_type_uint: combine_op = nir_op_umin; break;
586
case nir_type_float: combine_op = nir_op_fmin; break;
587
default: unreachable("Invalid dst_type");
588
}
589
break;
590
591
case BLORP_FILTER_MAX_SAMPLE:
592
switch (dst_type) {
593
case nir_type_int: combine_op = nir_op_imax; break;
594
case nir_type_uint: combine_op = nir_op_umax; break;
595
case nir_type_float: combine_op = nir_op_fmax; break;
596
default: unreachable("Invalid dst_type");
597
}
598
break;
599
600
default:
601
unreachable("Invalid filter");
602
}
603
604
/* If true, we inserted an if statement that we need to pop at at the end.
605
*/
606
bool inserted_if = false;
607
608
/* We add together samples using a binary tree structure, e.g. for 4x MSAA:
609
*
610
* result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
611
*
612
* This ensures that when all samples have the same value, no numerical
613
* precision is lost, since each addition operation always adds two equal
614
* values, and summing two equal floating point values does not lose
615
* precision.
616
*
617
* We perform this computation by treating the texture_data array as a
618
* stack and performing the following operations:
619
*
620
* - push sample 0 onto stack
621
* - push sample 1 onto stack
622
* - add top two stack entries
623
* - push sample 2 onto stack
624
* - push sample 3 onto stack
625
* - add top two stack entries
626
* - add top two stack entries
627
* - divide top stack entry by 4
628
*
629
* Note that after pushing sample i onto the stack, the number of add
630
* operations we do is equal to the number of trailing 1 bits in i. This
631
* works provided the total number of samples is a power of two, which it
632
* always is for i965.
633
*
634
* For integer formats, we replace the add operations with average
635
* operations and skip the final division.
636
*/
637
nir_ssa_def *texture_data[5];
638
texture_data[0] = NULL; /* Avoid maybe-uninitialized warning with GCC 10 */
639
unsigned stack_depth = 0;
640
for (unsigned i = 0; i < tex_samples; ++i) {
641
assert(stack_depth == util_bitcount(i)); /* Loop invariant */
642
643
/* Push sample i onto the stack */
644
assert(stack_depth < ARRAY_SIZE(texture_data));
645
646
nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
647
nir_channel(b, pos, 1),
648
nir_imm_int(b, i));
649
texture_data[stack_depth++] = blorp_nir_txf_ms(b, v, ms_pos, mcs, dst_type);
650
651
if (i == 0 && isl_aux_usage_has_mcs(tex_aux_usage)) {
652
/* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
653
* suggests an optimization:
654
*
655
* "A simple optimization with probable large return in
656
* performance is to compare the MCS value to zero (indicating
657
* all samples are on sample slice 0), and sample only from
658
* sample slice 0 using ld2dss if MCS is zero."
659
*
660
* Note that in the case where the MCS value is zero, sampling from
661
* sample slice 0 using ld2dss and sampling from sample 0 using
662
* ld2dms are equivalent (since all samples are on sample slice 0).
663
* Since we have already sampled from sample 0, all we need to do is
664
* skip the remaining fetches and averaging if MCS is zero.
665
*
666
* It's also trivial to detect when the MCS has the magic clear color
667
* value. In this case, the txf we did on sample 0 will return the
668
* clear color and we can skip the remaining fetches just like we do
669
* when MCS == 0.
670
*/
671
nir_ssa_def *mcs_zero = nir_ieq_imm(b, nir_channel(b, mcs, 0), 0);
672
if (tex_samples == 16) {
673
mcs_zero = nir_iand(b, mcs_zero,
674
nir_ieq_imm(b, nir_channel(b, mcs, 1), 0));
675
}
676
nir_ssa_def *mcs_clear =
677
blorp_nir_mcs_is_clear_color(b, mcs, tex_samples);
678
679
nir_push_if(b, nir_ior(b, mcs_zero, mcs_clear));
680
nir_store_var(b, color, texture_data[0], 0xf);
681
682
nir_push_else(b, NULL);
683
inserted_if = true;
684
}
685
686
for (int j = 0; j < count_trailing_one_bits(i); j++) {
687
assert(stack_depth >= 2);
688
--stack_depth;
689
690
texture_data[stack_depth - 1] =
691
nir_build_alu(b, combine_op,
692
texture_data[stack_depth - 1],
693
texture_data[stack_depth],
694
NULL, NULL);
695
}
696
}
697
698
/* We should have just 1 sample on the stack now. */
699
assert(stack_depth == 1);
700
701
if (filter == BLORP_FILTER_AVERAGE) {
702
assert(dst_type == nir_type_float);
703
texture_data[0] = nir_fmul(b, texture_data[0],
704
nir_imm_float(b, 1.0 / tex_samples));
705
}
706
707
nir_store_var(b, color, texture_data[0], 0xf);
708
709
if (inserted_if)
710
nir_pop_if(b, NULL);
711
712
return nir_load_var(b, color);
713
}
714
715
static nir_ssa_def *
716
blorp_nir_manual_blend_bilinear(nir_builder *b, nir_ssa_def *pos,
717
unsigned tex_samples,
718
const struct brw_blorp_blit_prog_key *key,
719
struct brw_blorp_blit_vars *v)
720
{
721
nir_ssa_def *pos_xy = nir_channels(b, pos, 0x3);
722
nir_ssa_def *rect_grid = nir_load_var(b, v->v_rect_grid);
723
nir_ssa_def *scale = nir_imm_vec2(b, key->x_scale, key->y_scale);
724
725
/* Translate coordinates to lay out the samples in a rectangular grid
726
* roughly corresponding to sample locations.
727
*/
728
pos_xy = nir_fmul(b, pos_xy, scale);
729
/* Adjust coordinates so that integers represent pixel centers rather
730
* than pixel edges.
731
*/
732
pos_xy = nir_fadd(b, pos_xy, nir_imm_float(b, -0.5));
733
/* Clamp the X, Y texture coordinates to properly handle the sampling of
734
* texels on texture edges.
735
*/
736
pos_xy = nir_fmin(b, nir_fmax(b, pos_xy, nir_imm_float(b, 0.0)),
737
nir_vec2(b, nir_channel(b, rect_grid, 0),
738
nir_channel(b, rect_grid, 1)));
739
740
/* Store the fractional parts to be used as bilinear interpolation
741
* coefficients.
742
*/
743
nir_ssa_def *frac_xy = nir_ffract(b, pos_xy);
744
/* Round the float coordinates down to nearest integer */
745
pos_xy = nir_fdiv(b, nir_ftrunc(b, pos_xy), scale);
746
747
nir_ssa_def *tex_data[4];
748
for (unsigned i = 0; i < 4; ++i) {
749
float sample_off_x = (float)(i & 0x1) / key->x_scale;
750
float sample_off_y = (float)((i >> 1) & 0x1) / key->y_scale;
751
nir_ssa_def *sample_off = nir_imm_vec2(b, sample_off_x, sample_off_y);
752
753
nir_ssa_def *sample_coords = nir_fadd(b, pos_xy, sample_off);
754
nir_ssa_def *sample_coords_int = nir_f2i32(b, sample_coords);
755
756
/* The MCS value we fetch has to match up with the pixel that we're
757
* sampling from. Since we sample from different pixels in each
758
* iteration of this "for" loop, the call to mcs_fetch() should be
759
* here inside the loop after computing the pixel coordinates.
760
*/
761
nir_ssa_def *mcs = NULL;
762
if (isl_aux_usage_has_mcs(key->tex_aux_usage))
763
mcs = blorp_blit_txf_ms_mcs(b, v, sample_coords_int);
764
765
/* Compute sample index and map the sample index to a sample number.
766
* Sample index layout shows the numbering of slots in a rectangular
767
* grid of samples with in a pixel. Sample number layout shows the
768
* rectangular grid of samples roughly corresponding to the real sample
769
* locations with in a pixel.
770
*
771
* In the case of 2x MSAA, the layout of sample indices is reversed from
772
* the layout of sample numbers:
773
*
774
* sample index layout : --------- sample number layout : ---------
775
* | 0 | 1 | | 1 | 0 |
776
* --------- ---------
777
*
778
* In case of 4x MSAA, layout of sample indices matches the layout of
779
* sample numbers:
780
* ---------
781
* | 0 | 1 |
782
* ---------
783
* | 2 | 3 |
784
* ---------
785
*
786
* In case of 8x MSAA the two layouts don't match.
787
* sample index layout : --------- sample number layout : ---------
788
* | 0 | 1 | | 3 | 7 |
789
* --------- ---------
790
* | 2 | 3 | | 5 | 0 |
791
* --------- ---------
792
* | 4 | 5 | | 1 | 2 |
793
* --------- ---------
794
* | 6 | 7 | | 4 | 6 |
795
* --------- ---------
796
*
797
* Fortunately, this can be done fairly easily as:
798
* S' = (0x17306425 >> (S * 4)) & 0xf
799
*
800
* In the case of 16x MSAA the two layouts don't match.
801
* Sample index layout: Sample number layout:
802
* --------------------- ---------------------
803
* | 0 | 1 | 2 | 3 | | 15 | 10 | 9 | 7 |
804
* --------------------- ---------------------
805
* | 4 | 5 | 6 | 7 | | 4 | 1 | 3 | 13 |
806
* --------------------- ---------------------
807
* | 8 | 9 | 10 | 11 | | 12 | 2 | 0 | 6 |
808
* --------------------- ---------------------
809
* | 12 | 13 | 14 | 15 | | 11 | 8 | 5 | 14 |
810
* --------------------- ---------------------
811
*
812
* This is equivalent to
813
* S' = (0xe58b602cd31479af >> (S * 4)) & 0xf
814
*/
815
nir_ssa_def *frac = nir_ffract(b, sample_coords);
816
nir_ssa_def *sample =
817
nir_fdot2(b, frac, nir_imm_vec2(b, key->x_scale,
818
key->x_scale * key->y_scale));
819
sample = nir_f2i32(b, sample);
820
821
if (tex_samples == 2) {
822
sample = nir_isub(b, nir_imm_int(b, 1), sample);
823
} else if (tex_samples == 8) {
824
sample = nir_iand(b, nir_ishr(b, nir_imm_int(b, 0x64210573),
825
nir_ishl(b, sample, nir_imm_int(b, 2))),
826
nir_imm_int(b, 0xf));
827
} else if (tex_samples == 16) {
828
nir_ssa_def *sample_low =
829
nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xd31479af),
830
nir_ishl(b, sample, nir_imm_int(b, 2))),
831
nir_imm_int(b, 0xf));
832
nir_ssa_def *sample_high =
833
nir_iand(b, nir_ishr(b, nir_imm_int(b, 0xe58b602c),
834
nir_ishl(b, nir_iadd(b, sample,
835
nir_imm_int(b, -8)),
836
nir_imm_int(b, 2))),
837
nir_imm_int(b, 0xf));
838
839
sample = nir_bcsel(b, nir_ilt(b, sample, nir_imm_int(b, 8)),
840
sample_low, sample_high);
841
}
842
nir_ssa_def *pos_ms = nir_vec3(b, nir_channel(b, sample_coords_int, 0),
843
nir_channel(b, sample_coords_int, 1),
844
sample);
845
tex_data[i] = blorp_nir_txf_ms(b, v, pos_ms, mcs, key->texture_data_type);
846
}
847
848
nir_ssa_def *frac_x = nir_channel(b, frac_xy, 0);
849
nir_ssa_def *frac_y = nir_channel(b, frac_xy, 1);
850
return nir_flrp(b, nir_flrp(b, tex_data[0], tex_data[1], frac_x),
851
nir_flrp(b, tex_data[2], tex_data[3], frac_x),
852
frac_y);
853
}
854
855
/** Perform a color bit-cast operation
856
*
857
* For copy operations involving CCS, we may need to use different formats for
858
* the source and destination surfaces. The two formats must both be UINT
859
* formats and must have the same size but may have different bit layouts.
860
* For instance, we may be copying from R8G8B8A8_UINT to R32_UINT or R32_UINT
861
* to R16G16_UINT. This function generates code to shuffle bits around to get
862
* us from one to the other.
863
*/
864
static nir_ssa_def *
865
bit_cast_color(struct nir_builder *b, nir_ssa_def *color,
866
const struct brw_blorp_blit_prog_key *key)
867
{
868
if (key->src_format == key->dst_format)
869
return color;
870
871
const struct isl_format_layout *src_fmtl =
872
isl_format_get_layout(key->src_format);
873
const struct isl_format_layout *dst_fmtl =
874
isl_format_get_layout(key->dst_format);
875
876
/* They must be formats with the same bit size */
877
assert(src_fmtl->bpb == dst_fmtl->bpb);
878
879
if (src_fmtl->bpb <= 32) {
880
assert(src_fmtl->channels.r.type == ISL_UINT ||
881
src_fmtl->channels.r.type == ISL_UNORM);
882
assert(dst_fmtl->channels.r.type == ISL_UINT ||
883
dst_fmtl->channels.r.type == ISL_UNORM);
884
885
nir_ssa_def *packed = nir_imm_int(b, 0);
886
for (unsigned c = 0; c < 4; c++) {
887
if (src_fmtl->channels_array[c].bits == 0)
888
continue;
889
890
const unsigned chan_start_bit = src_fmtl->channels_array[c].start_bit;
891
const unsigned chan_bits = src_fmtl->channels_array[c].bits;
892
893
nir_ssa_def *chan = nir_channel(b, color, c);
894
if (src_fmtl->channels_array[c].type == ISL_UNORM)
895
chan = nir_format_float_to_unorm(b, chan, &chan_bits);
896
897
packed = nir_ior(b, packed, nir_shift_imm(b, chan, chan_start_bit));
898
}
899
900
nir_ssa_def *chans[4] = { };
901
for (unsigned c = 0; c < 4; c++) {
902
if (dst_fmtl->channels_array[c].bits == 0) {
903
chans[c] = nir_imm_int(b, 0);
904
continue;
905
}
906
907
const unsigned chan_start_bit = dst_fmtl->channels_array[c].start_bit;
908
const unsigned chan_bits = dst_fmtl->channels_array[c].bits;
909
chans[c] = nir_iand(b, nir_shift_imm(b, packed, -(int)chan_start_bit),
910
nir_imm_int(b, BITFIELD_MASK(chan_bits)));
911
912
if (dst_fmtl->channels_array[c].type == ISL_UNORM)
913
chans[c] = nir_format_unorm_to_float(b, chans[c], &chan_bits);
914
}
915
color = nir_vec(b, chans, 4);
916
} else {
917
/* This path only supports UINT formats */
918
assert(src_fmtl->channels.r.type == ISL_UINT);
919
assert(dst_fmtl->channels.r.type == ISL_UINT);
920
921
const unsigned src_bpc = src_fmtl->channels.r.bits;
922
const unsigned dst_bpc = dst_fmtl->channels.r.bits;
923
924
assert(src_fmtl->channels.g.bits == 0 ||
925
src_fmtl->channels.g.bits == src_fmtl->channels.r.bits);
926
assert(src_fmtl->channels.b.bits == 0 ||
927
src_fmtl->channels.b.bits == src_fmtl->channels.r.bits);
928
assert(src_fmtl->channels.a.bits == 0 ||
929
src_fmtl->channels.a.bits == src_fmtl->channels.r.bits);
930
assert(dst_fmtl->channels.g.bits == 0 ||
931
dst_fmtl->channels.g.bits == dst_fmtl->channels.r.bits);
932
assert(dst_fmtl->channels.b.bits == 0 ||
933
dst_fmtl->channels.b.bits == dst_fmtl->channels.r.bits);
934
assert(dst_fmtl->channels.a.bits == 0 ||
935
dst_fmtl->channels.a.bits == dst_fmtl->channels.r.bits);
936
937
/* Restrict to only the channels we actually have */
938
const unsigned src_channels =
939
isl_format_get_num_channels(key->src_format);
940
color = nir_channels(b, color, (1 << src_channels) - 1);
941
942
color = nir_format_bitcast_uvec_unmasked(b, color, src_bpc, dst_bpc);
943
}
944
945
/* Blorp likes to assume that colors are vec4s */
946
nir_ssa_def *u = nir_ssa_undef(b, 1, 32);
947
nir_ssa_def *chans[4] = { u, u, u, u };
948
for (unsigned i = 0; i < color->num_components; i++)
949
chans[i] = nir_channel(b, color, i);
950
return nir_vec4(b, chans[0], chans[1], chans[2], chans[3]);
951
}
952
953
static nir_ssa_def *
954
select_color_channel(struct nir_builder *b, nir_ssa_def *color,
955
nir_alu_type data_type,
956
enum isl_channel_select chan)
957
{
958
if (chan == ISL_CHANNEL_SELECT_ZERO) {
959
return nir_imm_int(b, 0);
960
} else if (chan == ISL_CHANNEL_SELECT_ONE) {
961
switch (data_type) {
962
case nir_type_int:
963
case nir_type_uint:
964
return nir_imm_int(b, 1);
965
case nir_type_float:
966
return nir_imm_float(b, 1);
967
default:
968
unreachable("Invalid data type");
969
}
970
} else {
971
assert((unsigned)(chan - ISL_CHANNEL_SELECT_RED) < 4);
972
return nir_channel(b, color, chan - ISL_CHANNEL_SELECT_RED);
973
}
974
}
975
976
static nir_ssa_def *
977
swizzle_color(struct nir_builder *b, nir_ssa_def *color,
978
struct isl_swizzle swizzle, nir_alu_type data_type)
979
{
980
return nir_vec4(b,
981
select_color_channel(b, color, data_type, swizzle.r),
982
select_color_channel(b, color, data_type, swizzle.g),
983
select_color_channel(b, color, data_type, swizzle.b),
984
select_color_channel(b, color, data_type, swizzle.a));
985
}
986
987
static nir_ssa_def *
988
convert_color(struct nir_builder *b, nir_ssa_def *color,
989
const struct brw_blorp_blit_prog_key *key)
990
{
991
/* All of our color conversions end up generating a single-channel color
992
* value that we need to write out.
993
*/
994
nir_ssa_def *value;
995
996
if (key->dst_format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {
997
/* The destination image is bound as R32_UINT but the data needs to be
998
* in R24_UNORM_X8_TYPELESS. The bottom 24 are the actual data and the
999
* top 8 need to be zero. We can accomplish this by simply multiplying
1000
* by a factor to scale things down.
1001
*/
1002
unsigned factor = (1 << 24) - 1;
1003
value = nir_fsat(b, nir_channel(b, color, 0));
1004
value = nir_f2i32(b, nir_fmul(b, value, nir_imm_float(b, factor)));
1005
} else if (key->dst_format == ISL_FORMAT_L8_UNORM_SRGB) {
1006
value = nir_format_linear_to_srgb(b, nir_channel(b, color, 0));
1007
} else if (key->dst_format == ISL_FORMAT_R8G8B8_UNORM_SRGB) {
1008
value = nir_format_linear_to_srgb(b, color);
1009
} else if (key->dst_format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
1010
value = nir_format_pack_r9g9b9e5(b, color);
1011
} else {
1012
unreachable("Unsupported format conversion");
1013
}
1014
1015
nir_ssa_def *out_comps[4];
1016
for (unsigned i = 0; i < 4; i++) {
1017
if (i < value->num_components)
1018
out_comps[i] = nir_channel(b, value, i);
1019
else
1020
out_comps[i] = nir_ssa_undef(b, 1, 32);
1021
}
1022
return nir_vec(b, out_comps, 4);
1023
}
1024
1025
/**
1026
* Generator for WM programs used in BLORP blits.
1027
*
1028
* The bulk of the work done by the WM program is to wrap and unwrap the
1029
* coordinate transformations used by the hardware to store surfaces in
1030
* memory. The hardware transforms a pixel location (X, Y, S) (where S is the
1031
* sample index for a multisampled surface) to a memory offset by the
1032
* following formulas:
1033
*
1034
* offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
1035
* (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
1036
*
1037
* For a single-sampled surface, or for a multisampled surface using
1038
* INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
1039
* function:
1040
*
1041
* encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1042
* decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1043
* encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1044
* decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1045
*
1046
* For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1047
* embeds the sample number into bit 1 of the X and Y coordinates:
1048
*
1049
* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1050
* where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1051
* Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
1052
* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1053
* where X' = (X & ~0b11) >> 1 | (X & 0b1)
1054
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1055
* S = (Y & 0b10) | (X & 0b10) >> 1
1056
*
1057
* For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1058
* embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
1059
* the Y coordinate:
1060
*
1061
* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1062
* where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
1063
* Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1064
* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1065
* where X' = (X & ~0b111) >> 2 | (X & 0b1)
1066
* Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1067
* S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1068
*
1069
* For X tiling, tile() combines together the low-order bits of the X and Y
1070
* coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
1071
* bytes wide and 8 rows high:
1072
*
1073
* tile(x_tiled, X, Y, S) = A
1074
* where A = tile_num << 12 | offset
1075
* tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
1076
* offset = (Y' & 0b111) << 9
1077
* | (X & 0b111111111)
1078
* X' = X * cpp
1079
* Y' = Y + S * qpitch
1080
* detile(x_tiled, A) = (X, Y, S)
1081
* where X = X' / cpp
1082
* Y = Y' % qpitch
1083
* S = Y' / qpitch
1084
* Y' = (tile_num / tile_pitch) << 3
1085
* | (A & 0b111000000000) >> 9
1086
* X' = (tile_num % tile_pitch) << 9
1087
* | (A & 0b111111111)
1088
*
1089
* (In all tiling formulas, cpp is the number of bytes occupied by a single
1090
* sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
1091
* to fill the width of the surface, and qpitch is the spacing (in rows)
1092
* between array slices).
1093
*
1094
* For Y tiling, tile() combines together the low-order bits of the X and Y
1095
* coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
1096
* bytes wide and 32 rows high:
1097
*
1098
* tile(y_tiled, X, Y, S) = A
1099
* where A = tile_num << 12 | offset
1100
* tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
1101
* offset = (X' & 0b1110000) << 5
1102
* | (Y' & 0b11111) << 4
1103
* | (X' & 0b1111)
1104
* X' = X * cpp
1105
* Y' = Y + S * qpitch
1106
* detile(y_tiled, A) = (X, Y, S)
1107
* where X = X' / cpp
1108
* Y = Y' % qpitch
1109
* S = Y' / qpitch
1110
* Y' = (tile_num / tile_pitch) << 5
1111
* | (A & 0b111110000) >> 4
1112
* X' = (tile_num % tile_pitch) << 7
1113
* | (A & 0b111000000000) >> 5
1114
* | (A & 0b1111)
1115
*
1116
* For W tiling, tile() combines together the low-order bits of the X and Y
1117
* coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
1118
* bytes wide and 64 rows high (note that W tiling is only used for stencil
1119
* buffers, which always have cpp = 1 and S=0):
1120
*
1121
* tile(w_tiled, X, Y, S) = A
1122
* where A = tile_num << 12 | offset
1123
* tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
1124
* offset = (X' & 0b111000) << 6
1125
* | (Y' & 0b111100) << 3
1126
* | (X' & 0b100) << 2
1127
* | (Y' & 0b10) << 2
1128
* | (X' & 0b10) << 1
1129
* | (Y' & 0b1) << 1
1130
* | (X' & 0b1)
1131
* X' = X * cpp = X
1132
* Y' = Y + S * qpitch
1133
* detile(w_tiled, A) = (X, Y, S)
1134
* where X = X' / cpp = X'
1135
* Y = Y' % qpitch = Y'
1136
* S = Y / qpitch = 0
1137
* Y' = (tile_num / tile_pitch) << 6
1138
* | (A & 0b111100000) >> 3
1139
* | (A & 0b1000) >> 2
1140
* | (A & 0b10) >> 1
1141
* X' = (tile_num % tile_pitch) << 6
1142
* | (A & 0b111000000000) >> 6
1143
* | (A & 0b10000) >> 2
1144
* | (A & 0b100) >> 1
1145
* | (A & 0b1)
1146
*
1147
* Finally, for a non-tiled surface, tile() simply combines together the X and
1148
* Y coordinates in the natural way:
1149
*
1150
* tile(untiled, X, Y, S) = A
1151
* where A = Y * pitch + X'
1152
* X' = X * cpp
1153
* Y' = Y + S * qpitch
1154
* detile(untiled, A) = (X, Y, S)
1155
* where X = X' / cpp
1156
* Y = Y' % qpitch
1157
* S = Y' / qpitch
1158
* X' = A % pitch
1159
* Y' = A / pitch
1160
*
1161
* (In these formulas, pitch is the number of bytes occupied by a single row
1162
* of samples).
1163
*/
1164
static nir_shader *
1165
brw_blorp_build_nir_shader(struct blorp_context *blorp, void *mem_ctx,
1166
const struct brw_blorp_blit_prog_key *key)
1167
{
1168
const struct intel_device_info *devinfo = blorp->isl_dev->info;
1169
nir_ssa_def *src_pos, *dst_pos, *color;
1170
1171
/* Sanity checks */
1172
if (key->dst_tiled_w && key->rt_samples > 1) {
1173
/* If the destination image is W tiled and multisampled, then the thread
1174
* must be dispatched once per sample, not once per pixel. This is
1175
* necessary because after conversion between W and Y tiling, there's no
1176
* guarantee that all samples corresponding to a single pixel will still
1177
* be together.
1178
*/
1179
assert(key->persample_msaa_dispatch);
1180
}
1181
1182
if (key->persample_msaa_dispatch) {
1183
/* It only makes sense to do persample dispatch if the render target is
1184
* configured as multisampled.
1185
*/
1186
assert(key->rt_samples > 0);
1187
}
1188
1189
/* Make sure layout is consistent with sample count */
1190
assert((key->tex_layout == ISL_MSAA_LAYOUT_NONE) ==
1191
(key->tex_samples <= 1));
1192
assert((key->rt_layout == ISL_MSAA_LAYOUT_NONE) ==
1193
(key->rt_samples <= 1));
1194
assert((key->src_layout == ISL_MSAA_LAYOUT_NONE) ==
1195
(key->src_samples <= 1));
1196
assert((key->dst_layout == ISL_MSAA_LAYOUT_NONE) ==
1197
(key->dst_samples <= 1));
1198
1199
nir_builder b;
1200
blorp_nir_init_shader(&b, mem_ctx, MESA_SHADER_FRAGMENT, NULL);
1201
1202
struct brw_blorp_blit_vars v;
1203
brw_blorp_blit_vars_init(&b, &v, key);
1204
1205
dst_pos = blorp_blit_get_frag_coords(&b, key, &v);
1206
1207
/* Render target and texture hardware don't support W tiling until Gfx8. */
1208
const bool rt_tiled_w = false;
1209
const bool tex_tiled_w = devinfo->ver >= 8 && key->src_tiled_w;
1210
1211
/* The address that data will be written to is determined by the
1212
* coordinates supplied to the WM thread and the tiling and sample count of
1213
* the render target, according to the formula:
1214
*
1215
* (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1216
*
1217
* If the actual tiling and sample count of the destination surface are not
1218
* the same as the configuration of the render target, then these
1219
* coordinates are wrong and we have to adjust them to compensate for the
1220
* difference.
1221
*/
1222
if (rt_tiled_w != key->dst_tiled_w ||
1223
key->rt_samples != key->dst_samples ||
1224
key->rt_layout != key->dst_layout) {
1225
dst_pos = blorp_nir_encode_msaa(&b, dst_pos, key->rt_samples,
1226
key->rt_layout);
1227
/* Now (X, Y, S) = detile(rt_tiling, offset) */
1228
if (rt_tiled_w != key->dst_tiled_w)
1229
dst_pos = blorp_nir_retile_y_to_w(&b, dst_pos);
1230
/* Now (X, Y, S) = detile(rt_tiling, offset) */
1231
dst_pos = blorp_nir_decode_msaa(&b, dst_pos, key->dst_samples,
1232
key->dst_layout);
1233
}
1234
1235
nir_ssa_def *comp = NULL;
1236
if (key->dst_rgb) {
1237
/* The destination image is bound as a red texture three times as wide
1238
* as the actual image. Our shader is effectively running one color
1239
* component at a time. We need to save off the component and adjust
1240
* the destination position.
1241
*/
1242
assert(dst_pos->num_components == 2);
1243
nir_ssa_def *dst_x = nir_channel(&b, dst_pos, 0);
1244
comp = nir_umod(&b, dst_x, nir_imm_int(&b, 3));
1245
dst_pos = nir_vec2(&b, nir_idiv(&b, dst_x, nir_imm_int(&b, 3)),
1246
nir_channel(&b, dst_pos, 1));
1247
}
1248
1249
/* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1250
*
1251
* That is: X, Y and S now contain the true coordinates and sample index of
1252
* the data that the WM thread should output.
1253
*
1254
* If we need to kill pixels that are outside the destination rectangle,
1255
* now is the time to do it.
1256
*/
1257
if (key->use_kill)
1258
blorp_nir_discard_if_outside_rect(&b, dst_pos, &v);
1259
1260
src_pos = blorp_blit_apply_transform(&b, nir_i2f32(&b, dst_pos), &v);
1261
if (dst_pos->num_components == 3) {
1262
/* The sample coordinate is an integer that we want left alone but
1263
* blorp_blit_apply_transform() blindly applies the transform to all
1264
* three coordinates. Grab the original sample index.
1265
*/
1266
src_pos = nir_vec3(&b, nir_channel(&b, src_pos, 0),
1267
nir_channel(&b, src_pos, 1),
1268
nir_channel(&b, dst_pos, 2));
1269
}
1270
1271
/* If the source image is not multisampled, then we want to fetch sample
1272
* number 0, because that's the only sample there is.
1273
*/
1274
if (key->src_samples == 1)
1275
src_pos = nir_channels(&b, src_pos, 0x3);
1276
1277
/* X, Y, and S are now the coordinates of the pixel in the source image
1278
* that we want to texture from. Exception: if we are blending, then S is
1279
* irrelevant, because we are going to fetch all samples.
1280
*/
1281
switch (key->filter) {
1282
case BLORP_FILTER_NONE:
1283
case BLORP_FILTER_NEAREST:
1284
case BLORP_FILTER_SAMPLE_0:
1285
/* We're going to use texelFetch, so we need integers */
1286
if (src_pos->num_components == 2) {
1287
src_pos = nir_f2i32(&b, src_pos);
1288
} else {
1289
assert(src_pos->num_components == 3);
1290
src_pos = nir_vec3(&b, nir_channel(&b, nir_f2i32(&b, src_pos), 0),
1291
nir_channel(&b, nir_f2i32(&b, src_pos), 1),
1292
nir_channel(&b, src_pos, 2));
1293
}
1294
1295
/* We aren't blending, which means we just want to fetch a single
1296
* sample from the source surface. The address that we want to fetch
1297
* from is related to the X, Y and S values according to the formula:
1298
*
1299
* (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1300
*
1301
* If the actual tiling and sample count of the source surface are
1302
* not the same as the configuration of the texture, then we need to
1303
* adjust the coordinates to compensate for the difference.
1304
*/
1305
if (tex_tiled_w != key->src_tiled_w ||
1306
key->tex_samples != key->src_samples ||
1307
key->tex_layout != key->src_layout) {
1308
src_pos = blorp_nir_encode_msaa(&b, src_pos, key->src_samples,
1309
key->src_layout);
1310
/* Now (X, Y, S) = detile(src_tiling, offset) */
1311
if (tex_tiled_w != key->src_tiled_w)
1312
src_pos = blorp_nir_retile_w_to_y(&b, src_pos);
1313
/* Now (X, Y, S) = detile(tex_tiling, offset) */
1314
src_pos = blorp_nir_decode_msaa(&b, src_pos, key->tex_samples,
1315
key->tex_layout);
1316
}
1317
1318
if (key->need_src_offset)
1319
src_pos = nir_iadd(&b, src_pos, nir_load_var(&b, v.v_src_offset));
1320
1321
/* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1322
*
1323
* In other words: X, Y, and S now contain values which, when passed to
1324
* the texturing unit, will cause data to be read from the correct
1325
* memory location. So we can fetch the texel now.
1326
*/
1327
if (key->src_samples == 1) {
1328
color = blorp_nir_txf(&b, &v, src_pos, key->texture_data_type);
1329
} else {
1330
nir_ssa_def *mcs = NULL;
1331
if (isl_aux_usage_has_mcs(key->tex_aux_usage))
1332
mcs = blorp_blit_txf_ms_mcs(&b, &v, src_pos);
1333
1334
color = blorp_nir_txf_ms(&b, &v, src_pos, mcs, key->texture_data_type);
1335
}
1336
break;
1337
1338
case BLORP_FILTER_BILINEAR:
1339
assert(!key->src_tiled_w);
1340
assert(key->tex_samples == key->src_samples);
1341
assert(key->tex_layout == key->src_layout);
1342
1343
if (key->src_samples == 1) {
1344
color = blorp_nir_tex(&b, &v, key, src_pos);
1345
} else {
1346
assert(!key->use_kill);
1347
color = blorp_nir_manual_blend_bilinear(&b, src_pos, key->src_samples,
1348
key, &v);
1349
}
1350
break;
1351
1352
case BLORP_FILTER_AVERAGE:
1353
case BLORP_FILTER_MIN_SAMPLE:
1354
case BLORP_FILTER_MAX_SAMPLE:
1355
assert(!key->src_tiled_w);
1356
assert(key->tex_samples == key->src_samples);
1357
assert(key->tex_layout == key->src_layout);
1358
1359
/* Resolves (effecively) use texelFetch, so we need integers and we
1360
* don't care about the sample index if we got one.
1361
*/
1362
src_pos = nir_f2i32(&b, nir_channels(&b, src_pos, 0x3));
1363
1364
if (devinfo->ver == 6) {
1365
/* Because gfx6 only supports 4x interleved MSAA, we can do all the
1366
* blending we need with a single linear-interpolated texture lookup
1367
* at the center of the sample. The texture coordinates to be odd
1368
* integers so that they correspond to the center of a 2x2 block
1369
* representing the four samples that maxe up a pixel. So we need
1370
* to multiply our X and Y coordinates each by 2 and then add 1.
1371
*/
1372
assert(key->src_coords_normalized);
1373
assert(key->filter == BLORP_FILTER_AVERAGE);
1374
src_pos = nir_fadd(&b,
1375
nir_i2f32(&b, src_pos),
1376
nir_imm_float(&b, 0.5f));
1377
color = blorp_nir_tex(&b, &v, key, src_pos);
1378
} else {
1379
/* Gfx7+ hardware doesn't automaticaly blend. */
1380
color = blorp_nir_combine_samples(&b, &v, src_pos, key->src_samples,
1381
key->tex_aux_usage,
1382
key->texture_data_type,
1383
key->filter);
1384
}
1385
break;
1386
1387
default:
1388
unreachable("Invalid blorp filter");
1389
}
1390
1391
if (!isl_swizzle_is_identity(key->src_swizzle)) {
1392
color = swizzle_color(&b, color, key->src_swizzle,
1393
key->texture_data_type);
1394
}
1395
1396
if (!isl_swizzle_is_identity(key->dst_swizzle)) {
1397
color = swizzle_color(&b, color, isl_swizzle_invert(key->dst_swizzle),
1398
nir_type_int);
1399
}
1400
1401
if (key->format_bit_cast) {
1402
assert(isl_swizzle_is_identity(key->src_swizzle));
1403
assert(isl_swizzle_is_identity(key->dst_swizzle));
1404
color = bit_cast_color(&b, color, key);
1405
} else if (key->dst_format) {
1406
color = convert_color(&b, color, key);
1407
} else if (key->uint32_to_sint) {
1408
/* Normally the hardware will take care of converting values from/to
1409
* the source and destination formats. But a few cases need help.
1410
*
1411
* The Skylake PRM, volume 07, page 658 has a programming note:
1412
*
1413
* "When using SINT or UINT rendertarget surface formats, Blending
1414
* must be DISABLED. The Pre-Blend Color Clamp Enable and Color
1415
* Clamp Range fields are ignored, and an implied clamp to the
1416
* rendertarget surface format is performed."
1417
*
1418
* For UINT to SINT blits, our sample operation gives us a uint32_t,
1419
* but our render target write expects a signed int32_t number. If we
1420
* simply passed the value along, the hardware would interpret a value
1421
* with bit 31 set as a negative value, clamping it to the largest
1422
* negative number the destination format could represent. But the
1423
* actual source value is a positive number, so we want to clamp it
1424
* to INT_MAX. To fix this, we explicitly take min(color, INT_MAX).
1425
*/
1426
color = nir_umin(&b, color, nir_imm_int(&b, INT32_MAX));
1427
} else if (key->sint32_to_uint) {
1428
/* Similar to above, but clamping negative numbers to zero. */
1429
color = nir_imax(&b, color, nir_imm_int(&b, 0));
1430
}
1431
1432
if (key->dst_rgb) {
1433
/* The destination image is bound as a red texture three times as wide
1434
* as the actual image. Our shader is effectively running one color
1435
* component at a time. We need to pick off the appropriate component
1436
* from the source color and write that to destination red.
1437
*/
1438
assert(dst_pos->num_components == 2);
1439
1440
nir_ssa_def *color_component =
1441
nir_bcsel(&b, nir_ieq_imm(&b, comp, 0),
1442
nir_channel(&b, color, 0),
1443
nir_bcsel(&b, nir_ieq_imm(&b, comp, 1),
1444
nir_channel(&b, color, 1),
1445
nir_channel(&b, color, 2)));
1446
1447
nir_ssa_def *u = nir_ssa_undef(&b, 1, 32);
1448
color = nir_vec4(&b, color_component, u, u, u);
1449
}
1450
1451
if (key->dst_usage == ISL_SURF_USAGE_RENDER_TARGET_BIT) {
1452
nir_variable *color_out =
1453
nir_variable_create(b.shader, nir_var_shader_out,
1454
glsl_vec4_type(), "gl_FragColor");
1455
color_out->data.location = FRAG_RESULT_COLOR;
1456
nir_store_var(&b, color_out, color, 0xf);
1457
} else if (key->dst_usage == ISL_SURF_USAGE_DEPTH_BIT) {
1458
nir_variable *depth_out =
1459
nir_variable_create(b.shader, nir_var_shader_out,
1460
glsl_float_type(), "gl_FragDepth");
1461
depth_out->data.location = FRAG_RESULT_DEPTH;
1462
nir_store_var(&b, depth_out, nir_channel(&b, color, 0), 0x1);
1463
} else if (key->dst_usage == ISL_SURF_USAGE_STENCIL_BIT) {
1464
nir_variable *stencil_out =
1465
nir_variable_create(b.shader, nir_var_shader_out,
1466
glsl_int_type(), "gl_FragStencilRef");
1467
stencil_out->data.location = FRAG_RESULT_STENCIL;
1468
nir_store_var(&b, stencil_out, nir_channel(&b, color, 0), 0x1);
1469
} else {
1470
unreachable("Invalid destination usage");
1471
}
1472
1473
return b.shader;
1474
}
1475
1476
static bool
1477
brw_blorp_get_blit_kernel(struct blorp_batch *batch,
1478
struct blorp_params *params,
1479
const struct brw_blorp_blit_prog_key *prog_key)
1480
{
1481
struct blorp_context *blorp = batch->blorp;
1482
1483
if (blorp->lookup_shader(batch, prog_key, sizeof(*prog_key),
1484
&params->wm_prog_kernel, &params->wm_prog_data))
1485
return true;
1486
1487
void *mem_ctx = ralloc_context(NULL);
1488
1489
const unsigned *program;
1490
struct brw_wm_prog_data prog_data;
1491
1492
nir_shader *nir = brw_blorp_build_nir_shader(blorp, mem_ctx, prog_key);
1493
nir->info.name =
1494
ralloc_strdup(nir, blorp_shader_type_to_name(prog_key->base.shader_type));
1495
1496
struct brw_wm_prog_key wm_key;
1497
brw_blorp_init_wm_prog_key(&wm_key);
1498
wm_key.base.tex.compressed_multisample_layout_mask =
1499
isl_aux_usage_has_mcs(prog_key->tex_aux_usage);
1500
wm_key.base.tex.msaa_16 = prog_key->tex_samples == 16;
1501
wm_key.multisample_fbo = prog_key->rt_samples > 1;
1502
1503
program = blorp_compile_fs(blorp, mem_ctx, nir, &wm_key, false,
1504
&prog_data);
1505
1506
bool result =
1507
blorp->upload_shader(batch, MESA_SHADER_FRAGMENT,
1508
prog_key, sizeof(*prog_key),
1509
program, prog_data.base.program_size,
1510
&prog_data.base, sizeof(prog_data),
1511
&params->wm_prog_kernel, &params->wm_prog_data);
1512
1513
ralloc_free(mem_ctx);
1514
return result;
1515
}
1516
1517
static void
1518
brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
1519
GLfloat src0, GLfloat src1,
1520
GLfloat dst0, GLfloat dst1,
1521
bool mirror)
1522
{
1523
double scale = (double)(src1 - src0) / (double)(dst1 - dst0);
1524
if (!mirror) {
1525
/* When not mirroring a coordinate (say, X), we need:
1526
* src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1527
* Therefore:
1528
* src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1529
*
1530
* blorp program uses "round toward zero" to convert the
1531
* transformed floating point coordinates to integer coordinates,
1532
* whereas the behaviour we actually want is "round to nearest",
1533
* so 0.5 provides the necessary correction.
1534
*/
1535
xform->multiplier = scale;
1536
xform->offset = src0 + (-(double)dst0 + 0.5) * scale;
1537
} else {
1538
/* When mirroring X we need:
1539
* src_x - src_x0 = dst_x1 - dst_x - 0.5
1540
* Therefore:
1541
* src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1542
*/
1543
xform->multiplier = -scale;
1544
xform->offset = src0 + ((double)dst1 - 0.5) * scale;
1545
}
1546
}
1547
1548
static inline void
1549
surf_get_intratile_offset_px(struct brw_blorp_surface_info *info,
1550
uint32_t *tile_x_px, uint32_t *tile_y_px)
1551
{
1552
if (info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1553
struct isl_extent2d px_size_sa =
1554
isl_get_interleaved_msaa_px_size_sa(info->surf.samples);
1555
assert(info->tile_x_sa % px_size_sa.width == 0);
1556
assert(info->tile_y_sa % px_size_sa.height == 0);
1557
*tile_x_px = info->tile_x_sa / px_size_sa.width;
1558
*tile_y_px = info->tile_y_sa / px_size_sa.height;
1559
} else {
1560
*tile_x_px = info->tile_x_sa;
1561
*tile_y_px = info->tile_y_sa;
1562
}
1563
}
1564
1565
void
1566
blorp_surf_convert_to_single_slice(const struct isl_device *isl_dev,
1567
struct brw_blorp_surface_info *info)
1568
{
1569
bool ok UNUSED;
1570
1571
/* It would be insane to try and do this on a compressed surface */
1572
assert(info->aux_usage == ISL_AUX_USAGE_NONE);
1573
1574
/* Just bail if we have nothing to do. */
1575
if (info->surf.dim == ISL_SURF_DIM_2D &&
1576
info->view.base_level == 0 && info->view.base_array_layer == 0 &&
1577
info->surf.levels == 1 && info->surf.logical_level0_px.array_len == 1)
1578
return;
1579
1580
/* If this gets triggered then we've gotten here twice which. This
1581
* shouldn't happen thanks to the above early return.
1582
*/
1583
assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
1584
1585
uint32_t layer = 0, z = 0;
1586
if (info->surf.dim == ISL_SURF_DIM_3D)
1587
z = info->view.base_array_layer + info->z_offset;
1588
else
1589
layer = info->view.base_array_layer;
1590
1591
uint32_t byte_offset;
1592
isl_surf_get_image_surf(isl_dev, &info->surf,
1593
info->view.base_level, layer, z,
1594
&info->surf,
1595
&byte_offset, &info->tile_x_sa, &info->tile_y_sa);
1596
info->addr.offset += byte_offset;
1597
1598
uint32_t tile_x_px, tile_y_px;
1599
surf_get_intratile_offset_px(info, &tile_x_px, &tile_y_px);
1600
1601
/* Instead of using the X/Y Offset fields in RENDER_SURFACE_STATE, we place
1602
* the image at the tile boundary and offset our sampling or rendering.
1603
* For this reason, we need to grow the image by the offset to ensure that
1604
* the hardware doesn't think we've gone past the edge.
1605
*/
1606
info->surf.logical_level0_px.w += tile_x_px;
1607
info->surf.logical_level0_px.h += tile_y_px;
1608
info->surf.phys_level0_sa.w += info->tile_x_sa;
1609
info->surf.phys_level0_sa.h += info->tile_y_sa;
1610
1611
/* The view is also different now. */
1612
info->view.base_level = 0;
1613
info->view.levels = 1;
1614
info->view.base_array_layer = 0;
1615
info->view.array_len = 1;
1616
info->z_offset = 0;
1617
}
1618
1619
void
1620
blorp_surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
1621
struct brw_blorp_surface_info *info)
1622
{
1623
assert(info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED);
1624
1625
/* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1626
blorp_surf_convert_to_single_slice(isl_dev, info);
1627
1628
info->surf.logical_level0_px = info->surf.phys_level0_sa;
1629
info->surf.samples = 1;
1630
info->surf.msaa_layout = ISL_MSAA_LAYOUT_NONE;
1631
}
1632
1633
void
1634
blorp_surf_retile_w_to_y(const struct isl_device *isl_dev,
1635
struct brw_blorp_surface_info *info)
1636
{
1637
assert(info->surf.tiling == ISL_TILING_W);
1638
1639
/* First, we need to convert it to a simple 1-level 1-layer 2-D surface */
1640
blorp_surf_convert_to_single_slice(isl_dev, info);
1641
1642
/* On gfx7+, we don't have interleaved multisampling for color render
1643
* targets so we have to fake it.
1644
*
1645
* TODO: Are we sure we don't also need to fake it on gfx6?
1646
*/
1647
if (isl_dev->info->ver > 6 &&
1648
info->surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1649
blorp_surf_fake_interleaved_msaa(isl_dev, info);
1650
}
1651
1652
if (isl_dev->info->ver == 6) {
1653
/* Gfx6 stencil buffers have a very large alignment coming in from the
1654
* miptree. It's out-of-bounds for what the surface state can handle.
1655
* Since we have a single layer and level, it doesn't really matter as
1656
* long as we don't pass a bogus value into isl_surf_fill_state().
1657
*/
1658
info->surf.image_alignment_el = isl_extent3d(4, 2, 1);
1659
}
1660
1661
/* Now that we've converted everything to a simple 2-D surface with only
1662
* one miplevel, we can go about retiling it.
1663
*/
1664
const unsigned x_align = 8, y_align = info->surf.samples != 0 ? 8 : 4;
1665
info->surf.tiling = ISL_TILING_Y0;
1666
info->surf.logical_level0_px.width =
1667
ALIGN(info->surf.logical_level0_px.width, x_align) * 2;
1668
info->surf.logical_level0_px.height =
1669
ALIGN(info->surf.logical_level0_px.height, y_align) / 2;
1670
info->tile_x_sa *= 2;
1671
info->tile_y_sa /= 2;
1672
}
1673
1674
static bool
1675
can_shrink_surface(const struct brw_blorp_surface_info *surf)
1676
{
1677
/* The current code doesn't support offsets into the aux buffers. This
1678
* should be possible, but we need to make sure the offset is page
1679
* aligned for both the surface and the aux buffer surface. Generally
1680
* this mean using the page aligned offset for the aux buffer.
1681
*
1682
* Currently the cases where we must split the blit are limited to cases
1683
* where we don't have a aux buffer.
1684
*/
1685
if (surf->aux_addr.buffer != NULL)
1686
return false;
1687
1688
/* We can't support splitting the blit for gen <= 7, because the qpitch
1689
* size is calculated by the hardware based on the surface height for
1690
* gen <= 7. In gen >= 8, the qpitch is controlled by the driver.
1691
*/
1692
if (surf->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY)
1693
return false;
1694
1695
return true;
1696
}
1697
1698
static unsigned
1699
get_max_surface_size(const struct intel_device_info *devinfo,
1700
const struct brw_blorp_surface_info *surf)
1701
{
1702
const unsigned max = devinfo->ver >= 7 ? 16384 : 8192;
1703
if (split_blorp_blit_debug && can_shrink_surface(surf))
1704
return max >> 4; /* A smaller restriction when debug is enabled */
1705
else
1706
return max;
1707
}
1708
1709
struct blt_axis {
1710
double src0, src1, dst0, dst1;
1711
bool mirror;
1712
};
1713
1714
struct blt_coords {
1715
struct blt_axis x, y;
1716
};
1717
1718
static enum isl_format
1719
get_red_format_for_rgb_format(enum isl_format format)
1720
{
1721
const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1722
1723
switch (fmtl->channels.r.bits) {
1724
case 8:
1725
switch (fmtl->channels.r.type) {
1726
case ISL_UNORM:
1727
return ISL_FORMAT_R8_UNORM;
1728
case ISL_SNORM:
1729
return ISL_FORMAT_R8_SNORM;
1730
case ISL_UINT:
1731
return ISL_FORMAT_R8_UINT;
1732
case ISL_SINT:
1733
return ISL_FORMAT_R8_SINT;
1734
default:
1735
unreachable("Invalid 8-bit RGB channel type");
1736
}
1737
case 16:
1738
switch (fmtl->channels.r.type) {
1739
case ISL_UNORM:
1740
return ISL_FORMAT_R16_UNORM;
1741
case ISL_SNORM:
1742
return ISL_FORMAT_R16_SNORM;
1743
case ISL_SFLOAT:
1744
return ISL_FORMAT_R16_FLOAT;
1745
case ISL_UINT:
1746
return ISL_FORMAT_R16_UINT;
1747
case ISL_SINT:
1748
return ISL_FORMAT_R16_SINT;
1749
default:
1750
unreachable("Invalid 8-bit RGB channel type");
1751
}
1752
case 32:
1753
switch (fmtl->channels.r.type) {
1754
case ISL_SFLOAT:
1755
return ISL_FORMAT_R32_FLOAT;
1756
case ISL_UINT:
1757
return ISL_FORMAT_R32_UINT;
1758
case ISL_SINT:
1759
return ISL_FORMAT_R32_SINT;
1760
default:
1761
unreachable("Invalid 8-bit RGB channel type");
1762
}
1763
default:
1764
unreachable("Invalid number of red channel bits");
1765
}
1766
}
1767
1768
void
1769
surf_fake_rgb_with_red(const struct isl_device *isl_dev,
1770
struct brw_blorp_surface_info *info)
1771
{
1772
blorp_surf_convert_to_single_slice(isl_dev, info);
1773
1774
info->surf.logical_level0_px.width *= 3;
1775
info->surf.phys_level0_sa.width *= 3;
1776
info->tile_x_sa *= 3;
1777
1778
enum isl_format red_format =
1779
get_red_format_for_rgb_format(info->view.format);
1780
1781
assert(isl_format_get_layout(red_format)->channels.r.type ==
1782
isl_format_get_layout(info->view.format)->channels.r.type);
1783
assert(isl_format_get_layout(red_format)->channels.r.bits ==
1784
isl_format_get_layout(info->view.format)->channels.r.bits);
1785
1786
info->surf.format = info->view.format = red_format;
1787
}
1788
1789
enum blit_shrink_status {
1790
BLIT_NO_SHRINK = 0,
1791
BLIT_SRC_WIDTH_SHRINK = (1 << 0),
1792
BLIT_DST_WIDTH_SHRINK = (1 << 1),
1793
BLIT_SRC_HEIGHT_SHRINK = (1 << 2),
1794
BLIT_DST_HEIGHT_SHRINK = (1 << 3),
1795
};
1796
1797
/* Try to blit. If the surface parameters exceed the size allowed by hardware,
1798
* then enum blit_shrink_status will be returned. If BLIT_NO_SHRINK is
1799
* returned, then the blit was successful.
1800
*/
1801
static enum blit_shrink_status
1802
try_blorp_blit(struct blorp_batch *batch,
1803
struct blorp_params *params,
1804
struct brw_blorp_blit_prog_key *wm_prog_key,
1805
struct blt_coords *coords)
1806
{
1807
const struct intel_device_info *devinfo = batch->blorp->isl_dev->info;
1808
1809
if (params->dst.surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
1810
if (devinfo->ver >= 7) {
1811
/* We can render as depth on Gfx5 but there's no real advantage since
1812
* it doesn't support MSAA or HiZ. On Gfx4, we can't always render
1813
* to depth due to issues with depth buffers and mip-mapping. On
1814
* Gfx6, we can do everything but we have weird offsetting for HiZ
1815
* and stencil. It's easier to just render using the color pipe
1816
* on those platforms.
1817
*/
1818
wm_prog_key->dst_usage = ISL_SURF_USAGE_DEPTH_BIT;
1819
} else {
1820
wm_prog_key->dst_usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1821
}
1822
} else if (params->dst.surf.usage & ISL_SURF_USAGE_STENCIL_BIT) {
1823
assert(params->dst.surf.format == ISL_FORMAT_R8_UINT);
1824
if (devinfo->ver >= 9) {
1825
wm_prog_key->dst_usage = ISL_SURF_USAGE_STENCIL_BIT;
1826
} else {
1827
wm_prog_key->dst_usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1828
}
1829
} else {
1830
wm_prog_key->dst_usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1831
}
1832
1833
if (isl_format_has_sint_channel(params->src.view.format)) {
1834
wm_prog_key->texture_data_type = nir_type_int;
1835
} else if (isl_format_has_uint_channel(params->src.view.format)) {
1836
wm_prog_key->texture_data_type = nir_type_uint;
1837
} else {
1838
wm_prog_key->texture_data_type = nir_type_float;
1839
}
1840
1841
/* src_samples and dst_samples are the true sample counts */
1842
wm_prog_key->src_samples = params->src.surf.samples;
1843
wm_prog_key->dst_samples = params->dst.surf.samples;
1844
1845
wm_prog_key->tex_aux_usage = params->src.aux_usage;
1846
1847
/* src_layout and dst_layout indicate the true MSAA layout used by src and
1848
* dst.
1849
*/
1850
wm_prog_key->src_layout = params->src.surf.msaa_layout;
1851
wm_prog_key->dst_layout = params->dst.surf.msaa_layout;
1852
1853
/* Round floating point values to nearest integer to avoid "off by one texel"
1854
* kind of errors when blitting.
1855
*/
1856
params->x0 = params->wm_inputs.discard_rect.x0 = round(coords->x.dst0);
1857
params->y0 = params->wm_inputs.discard_rect.y0 = round(coords->y.dst0);
1858
params->x1 = params->wm_inputs.discard_rect.x1 = round(coords->x.dst1);
1859
params->y1 = params->wm_inputs.discard_rect.y1 = round(coords->y.dst1);
1860
1861
brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[0],
1862
coords->x.src0, coords->x.src1,
1863
coords->x.dst0, coords->x.dst1,
1864
coords->x.mirror);
1865
brw_blorp_setup_coord_transform(&params->wm_inputs.coord_transform[1],
1866
coords->y.src0, coords->y.src1,
1867
coords->y.dst0, coords->y.dst1,
1868
coords->y.mirror);
1869
1870
1871
if (devinfo->ver == 4) {
1872
/* The MinLOD and MinimumArrayElement don't work properly for cube maps.
1873
* Convert them to a single slice on gfx4.
1874
*/
1875
if (params->dst.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1876
blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->dst);
1877
wm_prog_key->need_dst_offset = true;
1878
}
1879
1880
if (params->src.surf.usage & ISL_SURF_USAGE_CUBE_BIT) {
1881
blorp_surf_convert_to_single_slice(batch->blorp->isl_dev, &params->src);
1882
wm_prog_key->need_src_offset = true;
1883
}
1884
}
1885
1886
if (devinfo->ver > 6 &&
1887
!isl_surf_usage_is_depth_or_stencil(wm_prog_key->dst_usage) &&
1888
params->dst.surf.msaa_layout == ISL_MSAA_LAYOUT_INTERLEAVED) {
1889
assert(params->dst.surf.samples > 1);
1890
1891
/* We must expand the rectangle we send through the rendering pipeline,
1892
* to account for the fact that we are mapping the destination region as
1893
* single-sampled when it is in fact multisampled. We must also align
1894
* it to a multiple of the multisampling pattern, because the
1895
* differences between multisampled and single-sampled surface formats
1896
* will mean that pixels are scrambled within the multisampling pattern.
1897
* TODO: what if this makes the coordinates too large?
1898
*
1899
* Note: this only works if the destination surface uses the IMS layout.
1900
* If it's UMS, then we have no choice but to set up the rendering
1901
* pipeline as multisampled.
1902
*/
1903
struct isl_extent2d px_size_sa =
1904
isl_get_interleaved_msaa_px_size_sa(params->dst.surf.samples);
1905
params->x0 = ROUND_DOWN_TO(params->x0, 2) * px_size_sa.width;
1906
params->y0 = ROUND_DOWN_TO(params->y0, 2) * px_size_sa.height;
1907
params->x1 = ALIGN(params->x1, 2) * px_size_sa.width;
1908
params->y1 = ALIGN(params->y1, 2) * px_size_sa.height;
1909
1910
blorp_surf_fake_interleaved_msaa(batch->blorp->isl_dev, &params->dst);
1911
1912
wm_prog_key->use_kill = true;
1913
wm_prog_key->need_dst_offset = true;
1914
}
1915
1916
if (params->dst.surf.tiling == ISL_TILING_W &&
1917
wm_prog_key->dst_usage != ISL_SURF_USAGE_STENCIL_BIT) {
1918
/* We must modify the rectangle we send through the rendering pipeline
1919
* (and the size and x/y offset of the destination surface), to account
1920
* for the fact that we are mapping it as Y-tiled when it is in fact
1921
* W-tiled.
1922
*
1923
* Both Y tiling and W tiling can be understood as organizations of
1924
* 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
1925
* is different, but the layout of the 32-byte sub-tiles within the 4k
1926
* tile is the same (8 sub-tiles across by 16 sub-tiles down, in
1927
* column-major order). In Y tiling, the sub-tiles are 16 bytes wide
1928
* and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
1929
*
1930
* Therefore, to account for the layout differences within the 32-byte
1931
* sub-tiles, we must expand the rectangle so the X coordinates of its
1932
* edges are multiples of 8 (the W sub-tile width), and its Y
1933
* coordinates of its edges are multiples of 4 (the W sub-tile height).
1934
* Then we need to scale the X and Y coordinates of the rectangle to
1935
* account for the differences in aspect ratio between the Y and W
1936
* sub-tiles. We need to modify the layer width and height similarly.
1937
*
1938
* A correction needs to be applied when MSAA is in use: since
1939
* INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
1940
* we need to align the Y coordinates to multiples of 8, so that when
1941
* they are divided by two they are still multiples of 4.
1942
*
1943
* Note: Since the x/y offset of the surface will be applied using the
1944
* SURFACE_STATE command packet, it will be invisible to the swizzling
1945
* code in the shader; therefore it needs to be in a multiple of the
1946
* 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
1947
* pixels wide and 4 pixels high (when viewed as a W-tiled stencil
1948
* buffer), and the miplevel alignment used for stencil buffers is 8
1949
* pixels horizontally and either 4 or 8 pixels vertically (see
1950
* intel_horizontal_texture_alignment_unit() and
1951
* intel_vertical_texture_alignment_unit()).
1952
*
1953
* Note: Also, since the SURFACE_STATE command packet can only apply
1954
* offsets that are multiples of 4 pixels horizontally and 2 pixels
1955
* vertically, it is important that the offsets will be multiples of
1956
* these sizes after they are converted into Y-tiled coordinates.
1957
* Fortunately they will be, since we know from above that the offsets
1958
* are a multiple of the 32-byte sub-tile size, and in Y-tiled
1959
* coordinates the sub-tile is 16 pixels wide and 2 pixels high.
1960
*
1961
* TODO: what if this makes the coordinates (or the texture size) too
1962
* large?
1963
*/
1964
const unsigned x_align = 8;
1965
const unsigned y_align = params->dst.surf.samples != 0 ? 8 : 4;
1966
params->x0 = ROUND_DOWN_TO(params->x0, x_align) * 2;
1967
params->y0 = ROUND_DOWN_TO(params->y0, y_align) / 2;
1968
params->x1 = ALIGN(params->x1, x_align) * 2;
1969
params->y1 = ALIGN(params->y1, y_align) / 2;
1970
1971
/* Retile the surface to Y-tiled */
1972
blorp_surf_retile_w_to_y(batch->blorp->isl_dev, &params->dst);
1973
1974
wm_prog_key->dst_tiled_w = true;
1975
wm_prog_key->use_kill = true;
1976
wm_prog_key->need_dst_offset = true;
1977
1978
if (params->dst.surf.samples > 1) {
1979
/* If the destination surface is a W-tiled multisampled stencil
1980
* buffer that we're mapping as Y tiled, then we need to arrange for
1981
* the WM program to run once per sample rather than once per pixel,
1982
* because the memory layout of related samples doesn't match between
1983
* W and Y tiling.
1984
*/
1985
wm_prog_key->persample_msaa_dispatch = true;
1986
}
1987
}
1988
1989
if (devinfo->ver < 8 && params->src.surf.tiling == ISL_TILING_W) {
1990
/* On Haswell and earlier, we have to fake W-tiled sources as Y-tiled.
1991
* Broadwell adds support for sampling from stencil.
1992
*
1993
* See the comments above concerning x/y offset alignment for the
1994
* destination surface.
1995
*
1996
* TODO: what if this makes the texture size too large?
1997
*/
1998
blorp_surf_retile_w_to_y(batch->blorp->isl_dev, &params->src);
1999
2000
wm_prog_key->src_tiled_w = true;
2001
wm_prog_key->need_src_offset = true;
2002
}
2003
2004
/* tex_samples and rt_samples are the sample counts that are set up in
2005
* SURFACE_STATE.
2006
*/
2007
wm_prog_key->tex_samples = params->src.surf.samples;
2008
wm_prog_key->rt_samples = params->dst.surf.samples;
2009
2010
/* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2011
* use to access the source and destination surfaces.
2012
*/
2013
wm_prog_key->tex_layout = params->src.surf.msaa_layout;
2014
wm_prog_key->rt_layout = params->dst.surf.msaa_layout;
2015
2016
if (params->src.surf.samples > 0 && params->dst.surf.samples > 1) {
2017
/* We are blitting from a multisample buffer to a multisample buffer, so
2018
* we must preserve samples within a pixel. This means we have to
2019
* arrange for the WM program to run once per sample rather than once
2020
* per pixel.
2021
*/
2022
wm_prog_key->persample_msaa_dispatch = true;
2023
}
2024
2025
params->num_samples = params->dst.surf.samples;
2026
2027
if ((wm_prog_key->filter == BLORP_FILTER_AVERAGE ||
2028
wm_prog_key->filter == BLORP_FILTER_BILINEAR) &&
2029
batch->blorp->isl_dev->info->ver <= 6) {
2030
/* Gfx4-5 don't support non-normalized texture coordinates */
2031
wm_prog_key->src_coords_normalized = true;
2032
params->wm_inputs.src_inv_size[0] =
2033
1.0f / minify(params->src.surf.logical_level0_px.width,
2034
params->src.view.base_level);
2035
params->wm_inputs.src_inv_size[1] =
2036
1.0f / minify(params->src.surf.logical_level0_px.height,
2037
params->src.view.base_level);
2038
}
2039
2040
if (isl_format_get_layout(params->dst.view.format)->bpb % 3 == 0) {
2041
/* We can't render to RGB formats natively because they aren't a
2042
* power-of-two size. Instead, we fake them by using a red format
2043
* with the same channel type and size and emitting shader code to
2044
* only write one channel at a time.
2045
*/
2046
params->x0 *= 3;
2047
params->x1 *= 3;
2048
2049
/* If it happens to be sRGB, we need to force a conversion */
2050
if (params->dst.view.format == ISL_FORMAT_R8G8B8_UNORM_SRGB)
2051
wm_prog_key->dst_format = ISL_FORMAT_R8G8B8_UNORM_SRGB;
2052
2053
surf_fake_rgb_with_red(batch->blorp->isl_dev, &params->dst);
2054
2055
wm_prog_key->dst_rgb = true;
2056
wm_prog_key->need_dst_offset = true;
2057
} else if (isl_format_is_rgbx(params->dst.view.format)) {
2058
/* We can handle RGBX formats easily enough by treating them as RGBA */
2059
params->dst.view.format =
2060
isl_format_rgbx_to_rgba(params->dst.view.format);
2061
} else if (params->dst.view.format == ISL_FORMAT_R24_UNORM_X8_TYPELESS &&
2062
wm_prog_key->dst_usage != ISL_SURF_USAGE_DEPTH_BIT) {
2063
wm_prog_key->dst_format = params->dst.view.format;
2064
params->dst.view.format = ISL_FORMAT_R32_UINT;
2065
} else if (params->dst.view.format == ISL_FORMAT_A4B4G4R4_UNORM) {
2066
params->dst.view.swizzle =
2067
isl_swizzle_compose(params->dst.view.swizzle,
2068
ISL_SWIZZLE(ALPHA, RED, GREEN, BLUE));
2069
params->dst.view.format = ISL_FORMAT_B4G4R4A4_UNORM;
2070
} else if (params->dst.view.format == ISL_FORMAT_L8_UNORM_SRGB) {
2071
wm_prog_key->dst_format = params->dst.view.format;
2072
params->dst.view.format = ISL_FORMAT_R8_UNORM;
2073
} else if (params->dst.view.format == ISL_FORMAT_R9G9B9E5_SHAREDEXP) {
2074
wm_prog_key->dst_format = params->dst.view.format;
2075
params->dst.view.format = ISL_FORMAT_R32_UINT;
2076
}
2077
2078
if (devinfo->verx10 <= 70 &&
2079
!isl_swizzle_is_identity(params->src.view.swizzle)) {
2080
wm_prog_key->src_swizzle = params->src.view.swizzle;
2081
params->src.view.swizzle = ISL_SWIZZLE_IDENTITY;
2082
} else {
2083
wm_prog_key->src_swizzle = ISL_SWIZZLE_IDENTITY;
2084
}
2085
2086
if (!isl_swizzle_supports_rendering(devinfo, params->dst.view.swizzle)) {
2087
wm_prog_key->dst_swizzle = params->dst.view.swizzle;
2088
params->dst.view.swizzle = ISL_SWIZZLE_IDENTITY;
2089
} else {
2090
wm_prog_key->dst_swizzle = ISL_SWIZZLE_IDENTITY;
2091
}
2092
2093
if (params->src.tile_x_sa || params->src.tile_y_sa) {
2094
assert(wm_prog_key->need_src_offset);
2095
surf_get_intratile_offset_px(&params->src,
2096
&params->wm_inputs.src_offset.x,
2097
&params->wm_inputs.src_offset.y);
2098
}
2099
2100
if (params->dst.tile_x_sa || params->dst.tile_y_sa) {
2101
assert(wm_prog_key->need_dst_offset);
2102
surf_get_intratile_offset_px(&params->dst,
2103
&params->wm_inputs.dst_offset.x,
2104
&params->wm_inputs.dst_offset.y);
2105
params->x0 += params->wm_inputs.dst_offset.x;
2106
params->y0 += params->wm_inputs.dst_offset.y;
2107
params->x1 += params->wm_inputs.dst_offset.x;
2108
params->y1 += params->wm_inputs.dst_offset.y;
2109
}
2110
2111
/* For some texture types, we need to pass the layer through the sampler. */
2112
params->wm_inputs.src_z = params->src.z_offset;
2113
2114
if (!brw_blorp_get_blit_kernel(batch, params, wm_prog_key))
2115
return 0;
2116
2117
if (!blorp_ensure_sf_program(batch, params))
2118
return 0;
2119
2120
unsigned result = 0;
2121
unsigned max_src_surface_size = get_max_surface_size(devinfo, &params->src);
2122
if (params->src.surf.logical_level0_px.width > max_src_surface_size)
2123
result |= BLIT_SRC_WIDTH_SHRINK;
2124
if (params->src.surf.logical_level0_px.height > max_src_surface_size)
2125
result |= BLIT_SRC_HEIGHT_SHRINK;
2126
2127
unsigned max_dst_surface_size = get_max_surface_size(devinfo, &params->dst);
2128
if (params->dst.surf.logical_level0_px.width > max_dst_surface_size)
2129
result |= BLIT_DST_WIDTH_SHRINK;
2130
if (params->dst.surf.logical_level0_px.height > max_dst_surface_size)
2131
result |= BLIT_DST_HEIGHT_SHRINK;
2132
2133
if (result == 0) {
2134
if (wm_prog_key->dst_usage == ISL_SURF_USAGE_DEPTH_BIT) {
2135
params->depth = params->dst;
2136
memset(&params->dst, 0, sizeof(params->dst));
2137
} else if (wm_prog_key->dst_usage == ISL_SURF_USAGE_STENCIL_BIT) {
2138
params->stencil = params->dst;
2139
params->stencil_mask = 0xff;
2140
memset(&params->dst, 0, sizeof(params->dst));
2141
}
2142
2143
batch->blorp->exec(batch, params);
2144
}
2145
2146
return result;
2147
}
2148
2149
/* Adjust split blit source coordinates for the current destination
2150
* coordinates.
2151
*/
2152
static void
2153
adjust_split_source_coords(const struct blt_axis *orig,
2154
struct blt_axis *split_coords,
2155
double scale)
2156
{
2157
/* When scale is greater than 0, then we are growing from the start, so
2158
* src0 uses delta0, and src1 uses delta1. When scale is less than 0, the
2159
* source range shrinks from the end. In that case src0 is adjusted by
2160
* delta1, and src1 is adjusted by delta0.
2161
*/
2162
double delta0 = scale * (split_coords->dst0 - orig->dst0);
2163
double delta1 = scale * (split_coords->dst1 - orig->dst1);
2164
split_coords->src0 = orig->src0 + (scale >= 0.0 ? delta0 : delta1);
2165
split_coords->src1 = orig->src1 + (scale >= 0.0 ? delta1 : delta0);
2166
}
2167
2168
static struct isl_extent2d
2169
get_px_size_sa(const struct isl_surf *surf)
2170
{
2171
static const struct isl_extent2d one_to_one = { .w = 1, .h = 1 };
2172
2173
if (surf->msaa_layout != ISL_MSAA_LAYOUT_INTERLEAVED)
2174
return one_to_one;
2175
else
2176
return isl_get_interleaved_msaa_px_size_sa(surf->samples);
2177
}
2178
2179
static void
2180
shrink_surface_params(const struct isl_device *dev,
2181
struct brw_blorp_surface_info *info,
2182
double *x0, double *x1, double *y0, double *y1)
2183
{
2184
uint32_t byte_offset, x_offset_sa, y_offset_sa, size;
2185
struct isl_extent2d px_size_sa;
2186
int adjust;
2187
2188
blorp_surf_convert_to_single_slice(dev, info);
2189
2190
px_size_sa = get_px_size_sa(&info->surf);
2191
2192
/* Because this gets called after we lower compressed images, the tile
2193
* offsets may be non-zero and we need to incorporate them in our
2194
* calculations.
2195
*/
2196
x_offset_sa = (uint32_t)*x0 * px_size_sa.w + info->tile_x_sa;
2197
y_offset_sa = (uint32_t)*y0 * px_size_sa.h + info->tile_y_sa;
2198
uint32_t tile_z_sa, tile_a;
2199
isl_tiling_get_intratile_offset_sa(info->surf.tiling,
2200
info->surf.format, info->surf.row_pitch_B,
2201
info->surf.array_pitch_el_rows,
2202
x_offset_sa, y_offset_sa, 0, 0,
2203
&byte_offset,
2204
&info->tile_x_sa, &info->tile_y_sa,
2205
&tile_z_sa, &tile_a);
2206
assert(tile_z_sa == 0 && tile_a == 0);
2207
2208
info->addr.offset += byte_offset;
2209
2210
adjust = (int)info->tile_x_sa / px_size_sa.w - (int)*x0;
2211
*x0 += adjust;
2212
*x1 += adjust;
2213
info->tile_x_sa = 0;
2214
2215
adjust = (int)info->tile_y_sa / px_size_sa.h - (int)*y0;
2216
*y0 += adjust;
2217
*y1 += adjust;
2218
info->tile_y_sa = 0;
2219
2220
size = MIN2((uint32_t)ceil(*x1), info->surf.logical_level0_px.width);
2221
info->surf.logical_level0_px.width = size;
2222
info->surf.phys_level0_sa.width = size * px_size_sa.w;
2223
2224
size = MIN2((uint32_t)ceil(*y1), info->surf.logical_level0_px.height);
2225
info->surf.logical_level0_px.height = size;
2226
info->surf.phys_level0_sa.height = size * px_size_sa.h;
2227
}
2228
2229
static void
2230
do_blorp_blit(struct blorp_batch *batch,
2231
const struct blorp_params *orig_params,
2232
struct brw_blorp_blit_prog_key *wm_prog_key,
2233
const struct blt_coords *orig)
2234
{
2235
struct blorp_params params;
2236
struct blt_coords blit_coords;
2237
struct blt_coords split_coords = *orig;
2238
double w = orig->x.dst1 - orig->x.dst0;
2239
double h = orig->y.dst1 - orig->y.dst0;
2240
double x_scale = (orig->x.src1 - orig->x.src0) / w;
2241
double y_scale = (orig->y.src1 - orig->y.src0) / h;
2242
if (orig->x.mirror)
2243
x_scale = -x_scale;
2244
if (orig->y.mirror)
2245
y_scale = -y_scale;
2246
2247
enum blit_shrink_status shrink = BLIT_NO_SHRINK;
2248
if (split_blorp_blit_debug) {
2249
if (can_shrink_surface(&orig_params->src))
2250
shrink |= BLIT_SRC_WIDTH_SHRINK | BLIT_SRC_HEIGHT_SHRINK;
2251
if (can_shrink_surface(&orig_params->dst))
2252
shrink |= BLIT_DST_WIDTH_SHRINK | BLIT_DST_HEIGHT_SHRINK;
2253
}
2254
2255
bool x_done, y_done;
2256
do {
2257
params = *orig_params;
2258
blit_coords = split_coords;
2259
2260
if (shrink & (BLIT_SRC_WIDTH_SHRINK | BLIT_SRC_HEIGHT_SHRINK)) {
2261
shrink_surface_params(batch->blorp->isl_dev, &params.src,
2262
&blit_coords.x.src0, &blit_coords.x.src1,
2263
&blit_coords.y.src0, &blit_coords.y.src1);
2264
wm_prog_key->need_src_offset = false;
2265
}
2266
2267
if (shrink & (BLIT_DST_WIDTH_SHRINK | BLIT_DST_HEIGHT_SHRINK)) {
2268
shrink_surface_params(batch->blorp->isl_dev, &params.dst,
2269
&blit_coords.x.dst0, &blit_coords.x.dst1,
2270
&blit_coords.y.dst0, &blit_coords.y.dst1);
2271
wm_prog_key->need_dst_offset = false;
2272
}
2273
2274
enum blit_shrink_status result =
2275
try_blorp_blit(batch, &params, wm_prog_key, &blit_coords);
2276
2277
if (result & (BLIT_SRC_WIDTH_SHRINK | BLIT_SRC_HEIGHT_SHRINK))
2278
assert(can_shrink_surface(&orig_params->src));
2279
2280
if (result & (BLIT_DST_WIDTH_SHRINK | BLIT_DST_HEIGHT_SHRINK))
2281
assert(can_shrink_surface(&orig_params->dst));
2282
2283
if (result & (BLIT_SRC_WIDTH_SHRINK | BLIT_DST_WIDTH_SHRINK)) {
2284
w /= 2.0;
2285
assert(w >= 1.0);
2286
split_coords.x.dst1 = MIN2(split_coords.x.dst0 + w, orig->x.dst1);
2287
adjust_split_source_coords(&orig->x, &split_coords.x, x_scale);
2288
}
2289
if (result & (BLIT_SRC_HEIGHT_SHRINK | BLIT_DST_HEIGHT_SHRINK)) {
2290
h /= 2.0;
2291
assert(h >= 1.0);
2292
split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2293
adjust_split_source_coords(&orig->y, &split_coords.y, y_scale);
2294
}
2295
2296
if (result) {
2297
/* We may get less bits set on result than we had already, so make
2298
* sure we remember all the ways in which a resize is required.
2299
*/
2300
shrink |= result;
2301
continue;
2302
}
2303
2304
y_done = (orig->y.dst1 - split_coords.y.dst1 < 0.5);
2305
x_done = y_done && (orig->x.dst1 - split_coords.x.dst1 < 0.5);
2306
if (x_done) {
2307
break;
2308
} else if (y_done) {
2309
split_coords.x.dst0 += w;
2310
split_coords.x.dst1 = MIN2(split_coords.x.dst0 + w, orig->x.dst1);
2311
split_coords.y.dst0 = orig->y.dst0;
2312
split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2313
adjust_split_source_coords(&orig->x, &split_coords.x, x_scale);
2314
} else {
2315
split_coords.y.dst0 += h;
2316
split_coords.y.dst1 = MIN2(split_coords.y.dst0 + h, orig->y.dst1);
2317
adjust_split_source_coords(&orig->y, &split_coords.y, y_scale);
2318
}
2319
} while (true);
2320
}
2321
2322
void
2323
blorp_blit(struct blorp_batch *batch,
2324
const struct blorp_surf *src_surf,
2325
unsigned src_level, float src_layer,
2326
enum isl_format src_format, struct isl_swizzle src_swizzle,
2327
const struct blorp_surf *dst_surf,
2328
unsigned dst_level, unsigned dst_layer,
2329
enum isl_format dst_format, struct isl_swizzle dst_swizzle,
2330
float src_x0, float src_y0,
2331
float src_x1, float src_y1,
2332
float dst_x0, float dst_y0,
2333
float dst_x1, float dst_y1,
2334
enum blorp_filter filter,
2335
bool mirror_x, bool mirror_y)
2336
{
2337
struct blorp_params params;
2338
blorp_params_init(&params);
2339
params.snapshot_type = INTEL_SNAPSHOT_BLIT;
2340
2341
/* We cannot handle combined depth and stencil. */
2342
if (src_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT)
2343
assert(src_surf->surf->format == ISL_FORMAT_R8_UINT);
2344
if (dst_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT)
2345
assert(dst_surf->surf->format == ISL_FORMAT_R8_UINT);
2346
2347
if (dst_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT) {
2348
assert(src_surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT);
2349
/* Prior to Broadwell, we can't render to R8_UINT */
2350
if (batch->blorp->isl_dev->info->ver < 8) {
2351
src_format = ISL_FORMAT_R8_UNORM;
2352
dst_format = ISL_FORMAT_R8_UNORM;
2353
}
2354
}
2355
2356
brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2357
src_layer, src_format, false);
2358
brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2359
dst_layer, dst_format, true);
2360
2361
params.src.view.swizzle = src_swizzle;
2362
params.dst.view.swizzle = dst_swizzle;
2363
2364
const struct isl_format_layout *src_fmtl =
2365
isl_format_get_layout(params.src.view.format);
2366
2367
struct brw_blorp_blit_prog_key wm_prog_key = {
2368
.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_BLIT),
2369
.filter = filter,
2370
.sint32_to_uint = src_fmtl->channels.r.bits == 32 &&
2371
isl_format_has_sint_channel(params.src.view.format) &&
2372
isl_format_has_uint_channel(params.dst.view.format),
2373
.uint32_to_sint = src_fmtl->channels.r.bits == 32 &&
2374
isl_format_has_uint_channel(params.src.view.format) &&
2375
isl_format_has_sint_channel(params.dst.view.format),
2376
};
2377
2378
/* Scaling factors used for bilinear filtering in multisample scaled
2379
* blits.
2380
*/
2381
if (params.src.surf.samples == 16)
2382
wm_prog_key.x_scale = 4.0f;
2383
else
2384
wm_prog_key.x_scale = 2.0f;
2385
wm_prog_key.y_scale = params.src.surf.samples / wm_prog_key.x_scale;
2386
2387
params.wm_inputs.rect_grid.x1 =
2388
minify(params.src.surf.logical_level0_px.width, src_level) *
2389
wm_prog_key.x_scale - 1.0f;
2390
params.wm_inputs.rect_grid.y1 =
2391
minify(params.src.surf.logical_level0_px.height, src_level) *
2392
wm_prog_key.y_scale - 1.0f;
2393
2394
struct blt_coords coords = {
2395
.x = {
2396
.src0 = src_x0,
2397
.src1 = src_x1,
2398
.dst0 = dst_x0,
2399
.dst1 = dst_x1,
2400
.mirror = mirror_x
2401
},
2402
.y = {
2403
.src0 = src_y0,
2404
.src1 = src_y1,
2405
.dst0 = dst_y0,
2406
.dst1 = dst_y1,
2407
.mirror = mirror_y
2408
}
2409
};
2410
2411
do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2412
}
2413
2414
static enum isl_format
2415
get_copy_format_for_bpb(const struct isl_device *isl_dev, unsigned bpb)
2416
{
2417
/* The choice of UNORM and UINT formats is very intentional here. Most
2418
* of the time, we want to use a UINT format to avoid any rounding error
2419
* in the blit. For stencil blits, R8_UINT is required by the hardware.
2420
* (It's the only format allowed in conjunction with W-tiling.) Also we
2421
* intentionally use the 4-channel formats whenever we can. This is so
2422
* that, when we do a RGB <-> RGBX copy, the two formats will line up
2423
* even though one of them is 3/4 the size of the other. The choice of
2424
* UNORM vs. UINT is also very intentional because we don't have 8 or
2425
* 16-bit RGB UINT formats until Sky Lake so we have to use UNORM there.
2426
* Fortunately, the only time we should ever use two different formats in
2427
* the table below is for RGB -> RGBA blits and so we will never have any
2428
* UNORM/UINT mismatch.
2429
*/
2430
if (ISL_GFX_VER(isl_dev) >= 9) {
2431
switch (bpb) {
2432
case 8: return ISL_FORMAT_R8_UINT;
2433
case 16: return ISL_FORMAT_R8G8_UINT;
2434
case 24: return ISL_FORMAT_R8G8B8_UINT;
2435
case 32: return ISL_FORMAT_R8G8B8A8_UINT;
2436
case 48: return ISL_FORMAT_R16G16B16_UINT;
2437
case 64: return ISL_FORMAT_R16G16B16A16_UINT;
2438
case 96: return ISL_FORMAT_R32G32B32_UINT;
2439
case 128:return ISL_FORMAT_R32G32B32A32_UINT;
2440
default:
2441
unreachable("Unknown format bpb");
2442
}
2443
} else {
2444
switch (bpb) {
2445
case 8: return ISL_FORMAT_R8_UINT;
2446
case 16: return ISL_FORMAT_R8G8_UINT;
2447
case 24: return ISL_FORMAT_R8G8B8_UNORM;
2448
case 32: return ISL_FORMAT_R8G8B8A8_UNORM;
2449
case 48: return ISL_FORMAT_R16G16B16_UNORM;
2450
case 64: return ISL_FORMAT_R16G16B16A16_UNORM;
2451
case 96: return ISL_FORMAT_R32G32B32_UINT;
2452
case 128:return ISL_FORMAT_R32G32B32A32_UINT;
2453
default:
2454
unreachable("Unknown format bpb");
2455
}
2456
}
2457
}
2458
2459
/** Returns a UINT format that is CCS-compatible with the given format
2460
*
2461
* The PRM's say absolutely nothing about how render compression works. The
2462
* only thing they provide is a list of formats on which it is and is not
2463
* supported. Empirical testing indicates that the compression is only based
2464
* on the bit-layout of the format and the channel encoding doesn't matter.
2465
* So, while texture views don't work in general, you can create a view as
2466
* long as the bit-layout of the formats are the same.
2467
*
2468
* Fortunately, for every render compression capable format, the UINT format
2469
* with the same bit layout also supports render compression. This means that
2470
* we only need to handle UINT formats for copy operations. In order to do
2471
* copies between formats with different bit layouts, we attach both with a
2472
* UINT format and use bit_cast_color() to generate code to do the bit-cast
2473
* operation between the two bit layouts.
2474
*/
2475
static enum isl_format
2476
get_ccs_compatible_copy_format(const struct isl_format_layout *fmtl)
2477
{
2478
switch (fmtl->format) {
2479
case ISL_FORMAT_R32G32B32A32_FLOAT:
2480
case ISL_FORMAT_R32G32B32A32_SINT:
2481
case ISL_FORMAT_R32G32B32A32_UINT:
2482
case ISL_FORMAT_R32G32B32A32_UNORM:
2483
case ISL_FORMAT_R32G32B32A32_SNORM:
2484
case ISL_FORMAT_R32G32B32X32_FLOAT:
2485
return ISL_FORMAT_R32G32B32A32_UINT;
2486
2487
case ISL_FORMAT_R16G16B16A16_UNORM:
2488
case ISL_FORMAT_R16G16B16A16_SNORM:
2489
case ISL_FORMAT_R16G16B16A16_SINT:
2490
case ISL_FORMAT_R16G16B16A16_UINT:
2491
case ISL_FORMAT_R16G16B16A16_FLOAT:
2492
case ISL_FORMAT_R16G16B16X16_UNORM:
2493
case ISL_FORMAT_R16G16B16X16_FLOAT:
2494
return ISL_FORMAT_R16G16B16A16_UINT;
2495
2496
case ISL_FORMAT_R32G32_FLOAT:
2497
case ISL_FORMAT_R32G32_SINT:
2498
case ISL_FORMAT_R32G32_UINT:
2499
case ISL_FORMAT_R32G32_UNORM:
2500
case ISL_FORMAT_R32G32_SNORM:
2501
return ISL_FORMAT_R32G32_UINT;
2502
2503
case ISL_FORMAT_B8G8R8A8_UNORM:
2504
case ISL_FORMAT_B8G8R8A8_UNORM_SRGB:
2505
case ISL_FORMAT_R8G8B8A8_UNORM:
2506
case ISL_FORMAT_R8G8B8A8_UNORM_SRGB:
2507
case ISL_FORMAT_R8G8B8A8_SNORM:
2508
case ISL_FORMAT_R8G8B8A8_SINT:
2509
case ISL_FORMAT_R8G8B8A8_UINT:
2510
case ISL_FORMAT_B8G8R8X8_UNORM:
2511
case ISL_FORMAT_B8G8R8X8_UNORM_SRGB:
2512
case ISL_FORMAT_R8G8B8X8_UNORM:
2513
case ISL_FORMAT_R8G8B8X8_UNORM_SRGB:
2514
return ISL_FORMAT_R8G8B8A8_UINT;
2515
2516
case ISL_FORMAT_R16G16_UNORM:
2517
case ISL_FORMAT_R16G16_SNORM:
2518
case ISL_FORMAT_R16G16_SINT:
2519
case ISL_FORMAT_R16G16_UINT:
2520
case ISL_FORMAT_R16G16_FLOAT:
2521
return ISL_FORMAT_R16G16_UINT;
2522
2523
case ISL_FORMAT_R32_SINT:
2524
case ISL_FORMAT_R32_UINT:
2525
case ISL_FORMAT_R32_FLOAT:
2526
case ISL_FORMAT_R32_UNORM:
2527
case ISL_FORMAT_R32_SNORM:
2528
return ISL_FORMAT_R32_UINT;
2529
2530
case ISL_FORMAT_B10G10R10A2_UNORM:
2531
case ISL_FORMAT_B10G10R10A2_UNORM_SRGB:
2532
case ISL_FORMAT_R10G10B10A2_UNORM:
2533
case ISL_FORMAT_R10G10B10A2_UNORM_SRGB:
2534
case ISL_FORMAT_R10G10B10_FLOAT_A2_UNORM:
2535
case ISL_FORMAT_R10G10B10A2_UINT:
2536
return ISL_FORMAT_R10G10B10A2_UINT;
2537
2538
case ISL_FORMAT_R16_UNORM:
2539
case ISL_FORMAT_R16_SNORM:
2540
case ISL_FORMAT_R16_SINT:
2541
case ISL_FORMAT_R16_UINT:
2542
case ISL_FORMAT_R16_FLOAT:
2543
return ISL_FORMAT_R16_UINT;
2544
2545
case ISL_FORMAT_R8G8_UNORM:
2546
case ISL_FORMAT_R8G8_SNORM:
2547
case ISL_FORMAT_R8G8_SINT:
2548
case ISL_FORMAT_R8G8_UINT:
2549
return ISL_FORMAT_R8G8_UINT;
2550
2551
case ISL_FORMAT_B5G5R5X1_UNORM:
2552
case ISL_FORMAT_B5G5R5X1_UNORM_SRGB:
2553
case ISL_FORMAT_B5G5R5A1_UNORM:
2554
case ISL_FORMAT_B5G5R5A1_UNORM_SRGB:
2555
return ISL_FORMAT_B5G5R5A1_UNORM;
2556
2557
case ISL_FORMAT_A4B4G4R4_UNORM:
2558
case ISL_FORMAT_B4G4R4A4_UNORM:
2559
case ISL_FORMAT_B4G4R4A4_UNORM_SRGB:
2560
return ISL_FORMAT_B4G4R4A4_UNORM;
2561
2562
case ISL_FORMAT_B5G6R5_UNORM:
2563
case ISL_FORMAT_B5G6R5_UNORM_SRGB:
2564
return ISL_FORMAT_B5G6R5_UNORM;
2565
2566
case ISL_FORMAT_A1B5G5R5_UNORM:
2567
return ISL_FORMAT_A1B5G5R5_UNORM;
2568
2569
case ISL_FORMAT_A8_UNORM:
2570
case ISL_FORMAT_R8_UNORM:
2571
case ISL_FORMAT_R8_SNORM:
2572
case ISL_FORMAT_R8_SINT:
2573
case ISL_FORMAT_R8_UINT:
2574
return ISL_FORMAT_R8_UINT;
2575
2576
default:
2577
unreachable("Not a compressible format");
2578
}
2579
}
2580
2581
void
2582
blorp_surf_convert_to_uncompressed(const struct isl_device *isl_dev,
2583
struct brw_blorp_surface_info *info,
2584
uint32_t *x, uint32_t *y,
2585
uint32_t *width, uint32_t *height)
2586
{
2587
const struct isl_format_layout *fmtl =
2588
isl_format_get_layout(info->surf.format);
2589
2590
assert(fmtl->bw > 1 || fmtl->bh > 1);
2591
2592
/* This should be the first modification made to the surface */
2593
assert(info->tile_x_sa == 0 && info->tile_y_sa == 0);
2594
2595
if (width && height) {
2596
ASSERTED const uint32_t level_width =
2597
minify(info->surf.logical_level0_px.width, info->view.base_level);
2598
ASSERTED const uint32_t level_height =
2599
minify(info->surf.logical_level0_px.height, info->view.base_level);
2600
assert(*width % fmtl->bw == 0 || *x + *width == level_width);
2601
assert(*height % fmtl->bh == 0 || *y + *height == level_height);
2602
*width = DIV_ROUND_UP(*width, fmtl->bw);
2603
*height = DIV_ROUND_UP(*height, fmtl->bh);
2604
}
2605
2606
if (x && y) {
2607
assert(*x % fmtl->bw == 0);
2608
assert(*y % fmtl->bh == 0);
2609
*x /= fmtl->bw;
2610
*y /= fmtl->bh;
2611
}
2612
2613
/* We only want one level and slice */
2614
info->view.levels = 1;
2615
info->view.array_len = 1;
2616
2617
if (info->surf.dim == ISL_SURF_DIM_3D) {
2618
/* Roll the Z offset into the image view */
2619
info->view.base_array_layer += info->z_offset;
2620
info->z_offset = 0;
2621
}
2622
2623
uint32_t offset_B;
2624
ASSERTED bool ok =
2625
isl_surf_get_uncompressed_surf(isl_dev, &info->surf, &info->view,
2626
&info->surf, &info->view, &offset_B,
2627
&info->tile_x_sa, &info->tile_y_sa);
2628
assert(ok);
2629
info->addr.offset += offset_B;
2630
2631
/* BLORP doesn't use the actual intratile offsets. Instead, it needs the
2632
* surface to be a bit bigger and we offset the vertices instead.
2633
*/
2634
assert(info->surf.dim == ISL_SURF_DIM_2D);
2635
assert(info->surf.logical_level0_px.array_len == 1);
2636
info->surf.logical_level0_px.w += info->tile_x_sa;
2637
info->surf.logical_level0_px.h += info->tile_y_sa;
2638
info->surf.phys_level0_sa.w += info->tile_x_sa;
2639
info->surf.phys_level0_sa.h += info->tile_y_sa;
2640
}
2641
2642
void
2643
blorp_copy(struct blorp_batch *batch,
2644
const struct blorp_surf *src_surf,
2645
unsigned src_level, unsigned src_layer,
2646
const struct blorp_surf *dst_surf,
2647
unsigned dst_level, unsigned dst_layer,
2648
uint32_t src_x, uint32_t src_y,
2649
uint32_t dst_x, uint32_t dst_y,
2650
uint32_t src_width, uint32_t src_height)
2651
{
2652
const struct isl_device *isl_dev = batch->blorp->isl_dev;
2653
struct blorp_params params;
2654
2655
if (src_width == 0 || src_height == 0)
2656
return;
2657
2658
blorp_params_init(&params);
2659
params.snapshot_type = INTEL_SNAPSHOT_COPY;
2660
brw_blorp_surface_info_init(batch->blorp, &params.src, src_surf, src_level,
2661
src_layer, ISL_FORMAT_UNSUPPORTED, false);
2662
brw_blorp_surface_info_init(batch->blorp, &params.dst, dst_surf, dst_level,
2663
dst_layer, ISL_FORMAT_UNSUPPORTED, true);
2664
2665
struct brw_blorp_blit_prog_key wm_prog_key = {
2666
.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_COPY),
2667
.filter = BLORP_FILTER_NONE,
2668
.need_src_offset = src_surf->tile_x_sa || src_surf->tile_y_sa,
2669
.need_dst_offset = dst_surf->tile_x_sa || dst_surf->tile_y_sa,
2670
};
2671
2672
const struct isl_format_layout *src_fmtl =
2673
isl_format_get_layout(params.src.surf.format);
2674
const struct isl_format_layout *dst_fmtl =
2675
isl_format_get_layout(params.dst.surf.format);
2676
2677
assert(params.src.aux_usage == ISL_AUX_USAGE_NONE ||
2678
params.src.aux_usage == ISL_AUX_USAGE_HIZ ||
2679
params.src.aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT ||
2680
params.src.aux_usage == ISL_AUX_USAGE_MCS ||
2681
params.src.aux_usage == ISL_AUX_USAGE_MCS_CCS ||
2682
params.src.aux_usage == ISL_AUX_USAGE_CCS_E ||
2683
params.src.aux_usage == ISL_AUX_USAGE_GFX12_CCS_E ||
2684
params.src.aux_usage == ISL_AUX_USAGE_STC_CCS);
2685
2686
if (isl_aux_usage_has_hiz(params.src.aux_usage)) {
2687
/* In order to use HiZ, we have to use the real format for the source.
2688
* Depth <-> Color copies are not allowed.
2689
*/
2690
params.src.view.format = params.src.surf.format;
2691
params.dst.view.format = params.src.surf.format;
2692
} else if ((params.dst.surf.usage & ISL_SURF_USAGE_DEPTH_BIT) &&
2693
isl_dev->info->ver >= 7) {
2694
/* On Gfx7 and higher, we use actual depth writes for blits into depth
2695
* buffers so we need the real format.
2696
*/
2697
params.src.view.format = params.dst.surf.format;
2698
params.dst.view.format = params.dst.surf.format;
2699
} else if (params.dst.aux_usage == ISL_AUX_USAGE_CCS_E ||
2700
params.dst.aux_usage == ISL_AUX_USAGE_GFX12_CCS_E) {
2701
params.dst.view.format = get_ccs_compatible_copy_format(dst_fmtl);
2702
if (params.src.aux_usage == ISL_AUX_USAGE_CCS_E ||
2703
params.src.aux_usage == ISL_AUX_USAGE_GFX12_CCS_E) {
2704
params.src.view.format = get_ccs_compatible_copy_format(src_fmtl);
2705
} else if (src_fmtl->bpb == dst_fmtl->bpb) {
2706
params.src.view.format = params.dst.view.format;
2707
} else {
2708
params.src.view.format =
2709
get_copy_format_for_bpb(isl_dev, src_fmtl->bpb);
2710
}
2711
} else if (params.src.aux_usage == ISL_AUX_USAGE_CCS_E ||
2712
params.src.aux_usage == ISL_AUX_USAGE_GFX12_CCS_E) {
2713
params.src.view.format = get_ccs_compatible_copy_format(src_fmtl);
2714
if (src_fmtl->bpb == dst_fmtl->bpb) {
2715
params.dst.view.format = params.src.view.format;
2716
} else {
2717
params.dst.view.format =
2718
get_copy_format_for_bpb(isl_dev, dst_fmtl->bpb);
2719
}
2720
} else {
2721
params.dst.view.format = get_copy_format_for_bpb(isl_dev, dst_fmtl->bpb);
2722
params.src.view.format = get_copy_format_for_bpb(isl_dev, src_fmtl->bpb);
2723
}
2724
2725
if (params.src.view.format != params.dst.view.format) {
2726
enum isl_format src_cast_format = params.src.view.format;
2727
enum isl_format dst_cast_format = params.dst.view.format;
2728
2729
/* The BLORP bitcast code gets confused by RGB formats. Just treat them
2730
* as RGBA and then everything will be happy. This is perfectly safe
2731
* because BLORP likes to treat things as if they have vec4 colors all
2732
* the time anyway.
2733
*/
2734
if (isl_format_get_layout(src_cast_format)->bpb % 3 == 0)
2735
src_cast_format = isl_format_rgb_to_rgba(src_cast_format);
2736
if (isl_format_get_layout(dst_cast_format)->bpb % 3 == 0)
2737
dst_cast_format = isl_format_rgb_to_rgba(dst_cast_format);
2738
2739
if (src_cast_format != dst_cast_format) {
2740
wm_prog_key.format_bit_cast = true;
2741
wm_prog_key.src_format = src_cast_format;
2742
wm_prog_key.dst_format = dst_cast_format;
2743
}
2744
}
2745
2746
if (src_fmtl->bw > 1 || src_fmtl->bh > 1) {
2747
blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.src,
2748
&src_x, &src_y,
2749
&src_width, &src_height);
2750
wm_prog_key.need_src_offset = true;
2751
}
2752
2753
if (dst_fmtl->bw > 1 || dst_fmtl->bh > 1) {
2754
blorp_surf_convert_to_uncompressed(batch->blorp->isl_dev, &params.dst,
2755
&dst_x, &dst_y, NULL, NULL);
2756
wm_prog_key.need_dst_offset = true;
2757
}
2758
2759
/* Once both surfaces are stompped to uncompressed as needed, the
2760
* destination size is the same as the source size.
2761
*/
2762
uint32_t dst_width = src_width;
2763
uint32_t dst_height = src_height;
2764
2765
struct blt_coords coords = {
2766
.x = {
2767
.src0 = src_x,
2768
.src1 = src_x + src_width,
2769
.dst0 = dst_x,
2770
.dst1 = dst_x + dst_width,
2771
.mirror = false
2772
},
2773
.y = {
2774
.src0 = src_y,
2775
.src1 = src_y + src_height,
2776
.dst0 = dst_y,
2777
.dst1 = dst_y + dst_height,
2778
.mirror = false
2779
}
2780
};
2781
2782
do_blorp_blit(batch, &params, &wm_prog_key, &coords);
2783
}
2784
2785
static enum isl_format
2786
isl_format_for_size(unsigned size_B)
2787
{
2788
switch (size_B) {
2789
case 1: return ISL_FORMAT_R8_UINT;
2790
case 2: return ISL_FORMAT_R8G8_UINT;
2791
case 4: return ISL_FORMAT_R8G8B8A8_UINT;
2792
case 8: return ISL_FORMAT_R16G16B16A16_UINT;
2793
case 16: return ISL_FORMAT_R32G32B32A32_UINT;
2794
default:
2795
unreachable("Not a power-of-two format size");
2796
}
2797
}
2798
2799
/**
2800
* Returns the greatest common divisor of a and b that is a power of two.
2801
*/
2802
static uint64_t
2803
gcd_pow2_u64(uint64_t a, uint64_t b)
2804
{
2805
assert(a > 0 || b > 0);
2806
2807
unsigned a_log2 = ffsll(a) - 1;
2808
unsigned b_log2 = ffsll(b) - 1;
2809
2810
/* If either a or b is 0, then a_log2 or b_log2 till be UINT_MAX in which
2811
* case, the MIN2() will take the other one. If both are 0 then we will
2812
* hit the assert above.
2813
*/
2814
return 1 << MIN2(a_log2, b_log2);
2815
}
2816
2817
static void
2818
do_buffer_copy(struct blorp_batch *batch,
2819
struct blorp_address *src,
2820
struct blorp_address *dst,
2821
int width, int height, int block_size)
2822
{
2823
/* The actual format we pick doesn't matter as blorp will throw it away.
2824
* The only thing that actually matters is the size.
2825
*/
2826
enum isl_format format = isl_format_for_size(block_size);
2827
2828
UNUSED bool ok;
2829
struct isl_surf surf;
2830
ok = isl_surf_init(batch->blorp->isl_dev, &surf,
2831
.dim = ISL_SURF_DIM_2D,
2832
.format = format,
2833
.width = width,
2834
.height = height,
2835
.depth = 1,
2836
.levels = 1,
2837
.array_len = 1,
2838
.samples = 1,
2839
.row_pitch_B = width * block_size,
2840
.usage = ISL_SURF_USAGE_TEXTURE_BIT |
2841
ISL_SURF_USAGE_RENDER_TARGET_BIT,
2842
.tiling_flags = ISL_TILING_LINEAR_BIT);
2843
assert(ok);
2844
2845
struct blorp_surf src_blorp_surf = {
2846
.surf = &surf,
2847
.addr = *src,
2848
};
2849
2850
struct blorp_surf dst_blorp_surf = {
2851
.surf = &surf,
2852
.addr = *dst,
2853
};
2854
2855
blorp_copy(batch, &src_blorp_surf, 0, 0, &dst_blorp_surf, 0, 0,
2856
0, 0, 0, 0, width, height);
2857
}
2858
2859
void
2860
blorp_buffer_copy(struct blorp_batch *batch,
2861
struct blorp_address src,
2862
struct blorp_address dst,
2863
uint64_t size)
2864
{
2865
const struct intel_device_info *devinfo = batch->blorp->isl_dev->info;
2866
uint64_t copy_size = size;
2867
2868
/* This is maximum possible width/height our HW can handle */
2869
uint64_t max_surface_dim = 1 << (devinfo->ver >= 7 ? 14 : 13);
2870
2871
/* First, we compute the biggest format that can be used with the
2872
* given offsets and size.
2873
*/
2874
int bs = 16;
2875
bs = gcd_pow2_u64(bs, src.offset);
2876
bs = gcd_pow2_u64(bs, dst.offset);
2877
bs = gcd_pow2_u64(bs, size);
2878
2879
/* First, we make a bunch of max-sized copies */
2880
uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
2881
while (copy_size >= max_copy_size) {
2882
do_buffer_copy(batch, &src, &dst, max_surface_dim, max_surface_dim, bs);
2883
copy_size -= max_copy_size;
2884
src.offset += max_copy_size;
2885
dst.offset += max_copy_size;
2886
}
2887
2888
/* Now make a max-width copy */
2889
uint64_t height = copy_size / (max_surface_dim * bs);
2890
assert(height < max_surface_dim);
2891
if (height != 0) {
2892
uint64_t rect_copy_size = height * max_surface_dim * bs;
2893
do_buffer_copy(batch, &src, &dst, max_surface_dim, height, bs);
2894
copy_size -= rect_copy_size;
2895
src.offset += rect_copy_size;
2896
dst.offset += rect_copy_size;
2897
}
2898
2899
/* Finally, make a small copy to finish it off */
2900
if (copy_size != 0) {
2901
do_buffer_copy(batch, &src, &dst, copy_size / bs, 1, bs);
2902
}
2903
}
2904
2905