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