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