Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/intel/blorp/blorp.c
7130 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 <errno.h>
25
26
#include "program/prog_instruction.h"
27
28
#include "blorp_priv.h"
29
#include "compiler/brw_compiler.h"
30
#include "compiler/brw_nir.h"
31
#include "dev/intel_debug.h"
32
33
const char *
34
blorp_shader_type_to_name(enum blorp_shader_type type)
35
{
36
static const char *shader_name[] = {
37
[BLORP_SHADER_TYPE_COPY] = "BLORP-copy",
38
[BLORP_SHADER_TYPE_BLIT] = "BLORP-blit",
39
[BLORP_SHADER_TYPE_CLEAR] = "BLORP-clear",
40
[BLORP_SHADER_TYPE_MCS_PARTIAL_RESOLVE] = "BLORP-mcs-partial-resolve",
41
[BLORP_SHADER_TYPE_LAYER_OFFSET_VS] = "BLORP-layer-offset-vs",
42
[BLORP_SHADER_TYPE_GFX4_SF] = "BLORP-gfx4-sf",
43
};
44
assert(type < ARRAY_SIZE(shader_name));
45
46
return shader_name[type];
47
}
48
49
void
50
blorp_init(struct blorp_context *blorp, void *driver_ctx,
51
struct isl_device *isl_dev)
52
{
53
blorp->driver_ctx = driver_ctx;
54
blorp->isl_dev = isl_dev;
55
}
56
57
void
58
blorp_finish(struct blorp_context *blorp)
59
{
60
blorp->driver_ctx = NULL;
61
}
62
63
void
64
blorp_batch_init(struct blorp_context *blorp,
65
struct blorp_batch *batch, void *driver_batch,
66
enum blorp_batch_flags flags)
67
{
68
batch->blorp = blorp;
69
batch->driver_batch = driver_batch;
70
batch->flags = flags;
71
}
72
73
void
74
blorp_batch_finish(struct blorp_batch *batch)
75
{
76
batch->blorp = NULL;
77
}
78
79
void
80
brw_blorp_surface_info_init(struct blorp_context *blorp,
81
struct brw_blorp_surface_info *info,
82
const struct blorp_surf *surf,
83
unsigned int level, float layer,
84
enum isl_format format, bool is_render_target)
85
{
86
memset(info, 0, sizeof(*info));
87
assert(level < surf->surf->levels);
88
assert(layer < MAX2(surf->surf->logical_level0_px.depth >> level,
89
surf->surf->logical_level0_px.array_len));
90
91
info->enabled = true;
92
93
if (format == ISL_FORMAT_UNSUPPORTED)
94
format = surf->surf->format;
95
96
info->surf = *surf->surf;
97
info->addr = surf->addr;
98
99
info->aux_usage = surf->aux_usage;
100
if (info->aux_usage != ISL_AUX_USAGE_NONE) {
101
info->aux_surf = *surf->aux_surf;
102
info->aux_addr = surf->aux_addr;
103
}
104
105
info->clear_color = surf->clear_color;
106
info->clear_color_addr = surf->clear_color_addr;
107
108
info->view = (struct isl_view) {
109
.usage = is_render_target ? ISL_SURF_USAGE_RENDER_TARGET_BIT :
110
ISL_SURF_USAGE_TEXTURE_BIT,
111
.format = format,
112
.base_level = level,
113
.levels = 1,
114
.swizzle = ISL_SWIZZLE_IDENTITY,
115
};
116
117
info->view.array_len = MAX2(info->surf.logical_level0_px.depth,
118
info->surf.logical_level0_px.array_len);
119
120
if (!is_render_target &&
121
(info->surf.dim == ISL_SURF_DIM_3D ||
122
info->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY)) {
123
/* 3-D textures don't support base_array layer and neither do 2-D
124
* multisampled textures on IVB so we need to pass it through the
125
* sampler in those cases. These are also two cases where we are
126
* guaranteed that we won't be doing any funny surface hacks.
127
*/
128
info->view.base_array_layer = 0;
129
info->z_offset = layer;
130
} else {
131
info->view.base_array_layer = layer;
132
133
assert(info->view.array_len >= info->view.base_array_layer);
134
info->view.array_len -= info->view.base_array_layer;
135
info->z_offset = 0;
136
}
137
138
/* Sandy Bridge and earlier have a limit of a maximum of 512 layers for
139
* layered rendering.
140
*/
141
if (is_render_target && blorp->isl_dev->info->ver <= 6)
142
info->view.array_len = MIN2(info->view.array_len, 512);
143
144
if (surf->tile_x_sa || surf->tile_y_sa) {
145
/* This is only allowed on simple 2D surfaces without MSAA */
146
assert(info->surf.dim == ISL_SURF_DIM_2D);
147
assert(info->surf.samples == 1);
148
assert(info->surf.levels == 1);
149
assert(info->surf.logical_level0_px.array_len == 1);
150
assert(info->aux_usage == ISL_AUX_USAGE_NONE);
151
152
info->tile_x_sa = surf->tile_x_sa;
153
info->tile_y_sa = surf->tile_y_sa;
154
155
/* Instead of using the X/Y Offset fields in RENDER_SURFACE_STATE, we
156
* place the image at the tile boundary and offset our sampling or
157
* rendering. For this reason, we need to grow the image by the offset
158
* to ensure that the hardware doesn't think we've gone past the edge.
159
*/
160
info->surf.logical_level0_px.w += surf->tile_x_sa;
161
info->surf.logical_level0_px.h += surf->tile_y_sa;
162
info->surf.phys_level0_sa.w += surf->tile_x_sa;
163
info->surf.phys_level0_sa.h += surf->tile_y_sa;
164
}
165
}
166
167
168
void
169
blorp_params_init(struct blorp_params *params)
170
{
171
memset(params, 0, sizeof(*params));
172
params->num_samples = 1;
173
params->num_draw_buffers = 1;
174
params->num_layers = 1;
175
}
176
177
void
178
brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key)
179
{
180
memset(wm_key, 0, sizeof(*wm_key));
181
wm_key->nr_color_regions = 1;
182
for (int i = 0; i < MAX_SAMPLERS; i++)
183
wm_key->base.tex.swizzles[i] = SWIZZLE_XYZW;
184
}
185
186
const unsigned *
187
blorp_compile_fs(struct blorp_context *blorp, void *mem_ctx,
188
struct nir_shader *nir,
189
struct brw_wm_prog_key *wm_key,
190
bool use_repclear,
191
struct brw_wm_prog_data *wm_prog_data)
192
{
193
const struct brw_compiler *compiler = blorp->compiler;
194
195
nir->options =
196
compiler->glsl_compiler_options[MESA_SHADER_FRAGMENT].NirOptions;
197
198
memset(wm_prog_data, 0, sizeof(*wm_prog_data));
199
200
wm_prog_data->base.nr_params = 0;
201
wm_prog_data->base.param = NULL;
202
203
/* BLORP always uses the first two binding table entries:
204
* - Surface 0 is the render target (which always start from 0)
205
* - Surface 1 is the source texture
206
*/
207
wm_prog_data->base.binding_table.texture_start = BLORP_TEXTURE_BT_INDEX;
208
209
brw_preprocess_nir(compiler, nir, NULL);
210
nir_remove_dead_variables(nir, nir_var_shader_in, NULL);
211
nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
212
213
if (blorp->compiler->devinfo->ver < 6) {
214
if (nir->info.fs.uses_discard)
215
wm_key->iz_lookup |= BRW_WM_IZ_PS_KILL_ALPHATEST_BIT;
216
217
wm_key->input_slots_valid = nir->info.inputs_read | VARYING_BIT_POS;
218
}
219
220
struct brw_compile_fs_params params = {
221
.nir = nir,
222
.key = wm_key,
223
.prog_data = wm_prog_data,
224
225
.use_rep_send = use_repclear,
226
.log_data = blorp->driver_ctx,
227
228
.debug_flag = DEBUG_BLORP,
229
};
230
231
return brw_compile_fs(compiler, mem_ctx, &params);
232
}
233
234
const unsigned *
235
blorp_compile_vs(struct blorp_context *blorp, void *mem_ctx,
236
struct nir_shader *nir,
237
struct brw_vs_prog_data *vs_prog_data)
238
{
239
const struct brw_compiler *compiler = blorp->compiler;
240
241
nir->options =
242
compiler->glsl_compiler_options[MESA_SHADER_VERTEX].NirOptions;
243
244
brw_preprocess_nir(compiler, nir, NULL);
245
nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
246
247
vs_prog_data->inputs_read = nir->info.inputs_read;
248
249
brw_compute_vue_map(compiler->devinfo,
250
&vs_prog_data->base.vue_map,
251
nir->info.outputs_written,
252
nir->info.separate_shader,
253
1);
254
255
struct brw_vs_prog_key vs_key = { 0, };
256
257
struct brw_compile_vs_params params = {
258
.nir = nir,
259
.key = &vs_key,
260
.prog_data = vs_prog_data,
261
.log_data = blorp->driver_ctx,
262
263
.debug_flag = DEBUG_BLORP,
264
};
265
266
return brw_compile_vs(compiler, mem_ctx, &params);
267
}
268
269
struct blorp_sf_key {
270
struct brw_blorp_base_key base;
271
struct brw_sf_prog_key key;
272
};
273
274
bool
275
blorp_ensure_sf_program(struct blorp_batch *batch,
276
struct blorp_params *params)
277
{
278
struct blorp_context *blorp = batch->blorp;
279
const struct brw_wm_prog_data *wm_prog_data = params->wm_prog_data;
280
assert(params->wm_prog_data);
281
282
/* Gfx6+ doesn't need a strips and fans program */
283
if (blorp->compiler->devinfo->ver >= 6)
284
return true;
285
286
struct blorp_sf_key key = {
287
.base = BRW_BLORP_BASE_KEY_INIT(BLORP_SHADER_TYPE_GFX4_SF),
288
};
289
290
/* Everything gets compacted in vertex setup, so we just need a
291
* pass-through for the correct number of input varyings.
292
*/
293
const uint64_t slots_valid = VARYING_BIT_POS |
294
((1ull << wm_prog_data->num_varying_inputs) - 1) << VARYING_SLOT_VAR0;
295
296
key.key.attrs = slots_valid;
297
key.key.primitive = BRW_SF_PRIM_TRIANGLES;
298
key.key.contains_flat_varying = wm_prog_data->contains_flat_varying;
299
300
STATIC_ASSERT(sizeof(key.key.interp_mode) ==
301
sizeof(wm_prog_data->interp_mode));
302
memcpy(key.key.interp_mode, wm_prog_data->interp_mode,
303
sizeof(key.key.interp_mode));
304
305
if (blorp->lookup_shader(batch, &key, sizeof(key),
306
&params->sf_prog_kernel, &params->sf_prog_data))
307
return true;
308
309
void *mem_ctx = ralloc_context(NULL);
310
311
const unsigned *program;
312
unsigned program_size;
313
314
struct brw_vue_map vue_map;
315
brw_compute_vue_map(blorp->compiler->devinfo, &vue_map, slots_valid, false, 1);
316
317
struct brw_sf_prog_data prog_data_tmp;
318
program = brw_compile_sf(blorp->compiler, mem_ctx, &key.key,
319
&prog_data_tmp, &vue_map, &program_size);
320
321
bool result =
322
blorp->upload_shader(batch, MESA_SHADER_NONE,
323
&key, sizeof(key), program, program_size,
324
(void *)&prog_data_tmp, sizeof(prog_data_tmp),
325
&params->sf_prog_kernel, &params->sf_prog_data);
326
327
ralloc_free(mem_ctx);
328
329
return result;
330
}
331
332
void
333
blorp_hiz_op(struct blorp_batch *batch, struct blorp_surf *surf,
334
uint32_t level, uint32_t start_layer, uint32_t num_layers,
335
enum isl_aux_op op)
336
{
337
const struct intel_device_info *devinfo = batch->blorp->isl_dev->info;
338
339
struct blorp_params params;
340
blorp_params_init(&params);
341
342
params.hiz_op = op;
343
params.full_surface_hiz_op = true;
344
switch (op) {
345
case ISL_AUX_OP_FULL_RESOLVE:
346
params.snapshot_type = INTEL_SNAPSHOT_HIZ_RESOLVE;
347
break;
348
case ISL_AUX_OP_AMBIGUATE:
349
params.snapshot_type = INTEL_SNAPSHOT_HIZ_AMBIGUATE;
350
break;
351
case ISL_AUX_OP_FAST_CLEAR:
352
params.snapshot_type = INTEL_SNAPSHOT_HIZ_CLEAR;
353
break;
354
case ISL_AUX_OP_PARTIAL_RESOLVE:
355
case ISL_AUX_OP_NONE:
356
unreachable("Invalid HiZ op");
357
}
358
359
for (uint32_t a = 0; a < num_layers; a++) {
360
const uint32_t layer = start_layer + a;
361
362
brw_blorp_surface_info_init(batch->blorp, &params.depth, surf, level,
363
layer, surf->surf->format, true);
364
365
/* Align the rectangle primitive to 8x4 pixels.
366
*
367
* During fast depth clears, the emitted rectangle primitive must be
368
* aligned to 8x4 pixels. From the Ivybridge PRM, Vol 2 Part 1 Section
369
* 11.5.3.1 Depth Buffer Clear (and the matching section in the
370
* Sandybridge PRM):
371
*
372
* If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
373
* aligned to an 8x4 pixel block relative to the upper left corner
374
* of the depth buffer [...]
375
*
376
* For hiz resolves, the rectangle must also be 8x4 aligned. Item
377
* WaHizAmbiguate8x4Aligned from the Haswell workarounds page and the
378
* Ivybridge simulator require the alignment.
379
*
380
* To be safe, let's just align the rect for all hiz operations and all
381
* hardware generations.
382
*
383
* However, for some miptree slices of a Z24 texture, emitting an 8x4
384
* aligned rectangle that covers the slice may clobber adjacent slices
385
* if we strictly adhered to the texture alignments specified in the
386
* PRM. The Ivybridge PRM, Section "Alignment Unit Size", states that
387
* SURFACE_STATE.Surface_Horizontal_Alignment should be 4 for Z24
388
* surfaces, not 8. But commit 1f112cc increased the alignment from 4 to
389
* 8, which prevents the clobbering.
390
*/
391
params.x1 = minify(params.depth.surf.logical_level0_px.width,
392
params.depth.view.base_level);
393
params.y1 = minify(params.depth.surf.logical_level0_px.height,
394
params.depth.view.base_level);
395
params.x1 = ALIGN(params.x1, 8);
396
params.y1 = ALIGN(params.y1, 4);
397
398
if (params.depth.view.base_level == 0) {
399
/* TODO: What about MSAA? */
400
params.depth.surf.logical_level0_px.width = params.x1;
401
params.depth.surf.logical_level0_px.height = params.y1;
402
} else if (devinfo->ver >= 8 && devinfo->ver <= 9 &&
403
op == ISL_AUX_OP_AMBIGUATE) {
404
/* On some platforms, it's not enough to just adjust the clear
405
* rectangle when the LOD is greater than 0.
406
*
407
* From the BDW and SKL PRMs, Vol 7, "Optimized Hierarchical Depth
408
* Buffer Resolve":
409
*
410
* The following is required when performing a hierarchical depth
411
* buffer resolve:
412
*
413
* - A rectangle primitive covering the full render target must be
414
* programmed on Xmin, Ymin, Xmax, and Ymax in the
415
* 3DSTATE_WM_HZ_OP command.
416
*
417
* - The rectangle primitive size must be aligned to 8x4 pixels.
418
*
419
* And from the Clear Rectangle programming note in 3DSTATE_WM_HZ_OP
420
* (Vol 2a):
421
*
422
* Hence the max values must be less than or equal to: ( Surface
423
* Width » LOD ) and ( Surface Height » LOD ) for X Max and Y Max
424
* respectively.
425
*
426
* This means that the extent of the LOD must be naturally
427
* 8x4-aligned after minification of the base LOD. Since the base LOD
428
* dimensions affect the placement of smaller LODs, it's not trivial
429
* (nor possible, at times) to satisfy the requirement by adjusting
430
* the base LOD extent. Just assert that the caller is accessing an
431
* LOD that satisfies this requirement.
432
*/
433
assert(minify(params.depth.surf.logical_level0_px.width,
434
params.depth.view.base_level) == params.x1);
435
assert(minify(params.depth.surf.logical_level0_px.height,
436
params.depth.view.base_level) == params.y1);
437
}
438
439
params.dst.surf.samples = params.depth.surf.samples;
440
params.dst.surf.logical_level0_px = params.depth.surf.logical_level0_px;
441
params.depth_format =
442
isl_format_get_depth_format(surf->surf->format, false);
443
params.num_samples = params.depth.surf.samples;
444
445
batch->blorp->exec(batch, &params);
446
}
447
}
448
449