1/* 2 * Copyright © 2014-2017 Broadcom 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 "util/u_blitter.h" 25#include "util/u_draw.h" 26#include "util/u_prim.h" 27#include "util/format/u_format.h" 28#include "util/u_helpers.h" 29#include "util/u_pack_color.h" 30#include "util/u_prim_restart.h" 31#include "util/u_upload_mgr.h" 32 33#include "v3d_context.h" 34#include "v3d_resource.h" 35#include "v3d_cl.h" 36#include "broadcom/compiler/v3d_compiler.h" 37#include "broadcom/common/v3d_macros.h" 38#include "broadcom/common/v3d_util.h" 39#include "broadcom/cle/v3dx_pack.h" 40 41void 42v3dX(start_binning)(struct v3d_context *v3d, struct v3d_job *job) 43{ 44 assert(job->needs_flush); 45 46 /* Get space to emit our BCL state, using a branch to jump to a new BO 47 * if necessary. 48 */ 49 50 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */); 51 52 job->submit.bcl_start = job->bcl.bo->offset; 53 v3d_job_add_bo(job, job->bcl.bo); 54 55 /* The PTB will request the tile alloc initial size per tile at start 56 * of tile binning. 57 */ 58 uint32_t tile_alloc_size = 59 MAX2(job->num_layers, 1) * job->draw_tiles_x * job->draw_tiles_y * 64; 60 61 /* The PTB allocates in aligned 4k chunks after the initial setup. */ 62 tile_alloc_size = align(tile_alloc_size, 4096); 63 64 /* Include the first two chunk allocations that the PTB does so that 65 * we definitely clear the OOM condition before triggering one (the HW 66 * won't trigger OOM during the first allocations). 67 */ 68 tile_alloc_size += 8192; 69 70 /* For performance, allocate some extra initial memory after the PTB's 71 * minimal allocations, so that we hopefully don't have to block the 72 * GPU on the kernel handling an OOM signal. 73 */ 74 tile_alloc_size += 512 * 1024; 75 76 job->tile_alloc = v3d_bo_alloc(v3d->screen, tile_alloc_size, 77 "tile_alloc"); 78 uint32_t tsda_per_tile_size = v3d->screen->devinfo.ver >= 40 ? 256 : 64; 79 job->tile_state = v3d_bo_alloc(v3d->screen, 80 MAX2(job->num_layers, 1) * 81 job->draw_tiles_y * 82 job->draw_tiles_x * 83 tsda_per_tile_size, 84 "TSDA"); 85 86#if V3D_VERSION >= 41 87 /* This must go before the binning mode configuration. It is 88 * required for layered framebuffers to work. 89 */ 90 if (job->num_layers > 0) { 91 cl_emit(&job->bcl, NUMBER_OF_LAYERS, config) { 92 config.number_of_layers = job->num_layers; 93 } 94 } 95#endif 96 97 assert(!job->msaa || !job->double_buffer); 98#if V3D_VERSION >= 40 99 cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) { 100 config.width_in_pixels = job->draw_width; 101 config.height_in_pixels = job->draw_height; 102 config.number_of_render_targets = 103 MAX2(job->nr_cbufs, 1); 104 105 config.multisample_mode_4x = job->msaa; 106 config.double_buffer_in_non_ms_mode = job->double_buffer; 107 108 config.maximum_bpp_of_all_render_targets = job->internal_bpp; 109 } 110#else /* V3D_VERSION < 40 */ 111 /* "Binning mode lists start with a Tile Binning Mode Configuration 112 * item (120)" 113 * 114 * Part1 signals the end of binning config setup. 115 */ 116 cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART2, config) { 117 config.tile_allocation_memory_address = 118 cl_address(job->tile_alloc, 0); 119 config.tile_allocation_memory_size = job->tile_alloc->size; 120 } 121 122 cl_emit(&job->bcl, TILE_BINNING_MODE_CFG_PART1, config) { 123 config.tile_state_data_array_base_address = 124 cl_address(job->tile_state, 0); 125 126 config.width_in_tiles = job->draw_tiles_x; 127 config.height_in_tiles = job->draw_tiles_y; 128 /* Must be >= 1 */ 129 config.number_of_render_targets = 130 MAX2(job->nr_cbufs, 1); 131 132 config.multisample_mode_4x = job->msaa; 133 config.double_buffer_in_non_ms_mode = job->double_buffer; 134 135 config.maximum_bpp_of_all_render_targets = job->internal_bpp; 136 } 137#endif /* V3D_VERSION < 40 */ 138 139 /* There's definitely nothing in the VCD cache we want. */ 140 cl_emit(&job->bcl, FLUSH_VCD_CACHE, bin); 141 142 /* Disable any leftover OQ state from another job. */ 143 cl_emit(&job->bcl, OCCLUSION_QUERY_COUNTER, counter); 144 145 /* "Binning mode lists must have a Start Tile Binning item (6) after 146 * any prefix state data before the binning list proper starts." 147 */ 148 cl_emit(&job->bcl, START_TILE_BINNING, bin); 149} 150/** 151 * Does the initial bining command list setup for drawing to a given FBO. 152 */ 153static void 154v3d_start_draw(struct v3d_context *v3d) 155{ 156 struct v3d_job *job = v3d->job; 157 158 if (job->needs_flush) 159 return; 160 161 job->needs_flush = true; 162 job->draw_width = v3d->framebuffer.width; 163 job->draw_height = v3d->framebuffer.height; 164 job->num_layers = util_framebuffer_get_num_layers(&v3d->framebuffer); 165 166 v3dX(start_binning)(v3d, job); 167} 168 169static void 170v3d_predraw_check_stage_inputs(struct pipe_context *pctx, 171 enum pipe_shader_type s) 172{ 173 struct v3d_context *v3d = v3d_context(pctx); 174 175 /* Flush writes to textures we're sampling. */ 176 for (int i = 0; i < v3d->tex[s].num_textures; i++) { 177 struct pipe_sampler_view *pview = v3d->tex[s].textures[i]; 178 if (!pview) 179 continue; 180 struct v3d_sampler_view *view = v3d_sampler_view(pview); 181 182 if (view->texture != view->base.texture && 183 view->base.format != PIPE_FORMAT_X32_S8X24_UINT) 184 v3d_update_shadow_texture(pctx, &view->base); 185 186 v3d_flush_jobs_writing_resource(v3d, view->texture, 187 V3D_FLUSH_DEFAULT, 188 s == PIPE_SHADER_COMPUTE); 189 } 190 191 /* Flush writes to UBOs. */ 192 u_foreach_bit(i, v3d->constbuf[s].enabled_mask) { 193 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i]; 194 if (cb->buffer) { 195 v3d_flush_jobs_writing_resource(v3d, cb->buffer, 196 V3D_FLUSH_DEFAULT, 197 s == PIPE_SHADER_COMPUTE); 198 } 199 } 200 201 /* Flush reads/writes to our SSBOs */ 202 u_foreach_bit(i, v3d->ssbo[s].enabled_mask) { 203 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i]; 204 if (sb->buffer) { 205 v3d_flush_jobs_reading_resource(v3d, sb->buffer, 206 V3D_FLUSH_NOT_CURRENT_JOB, 207 s == PIPE_SHADER_COMPUTE); 208 } 209 } 210 211 /* Flush reads/writes to our image views */ 212 u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) { 213 struct v3d_image_view *view = &v3d->shaderimg[s].si[i]; 214 215 v3d_flush_jobs_reading_resource(v3d, view->base.resource, 216 V3D_FLUSH_NOT_CURRENT_JOB, 217 s == PIPE_SHADER_COMPUTE); 218 } 219 220 /* Flush writes to our vertex buffers (i.e. from transform feedback) */ 221 if (s == PIPE_SHADER_VERTEX) { 222 u_foreach_bit(i, v3d->vertexbuf.enabled_mask) { 223 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i]; 224 225 v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource, 226 V3D_FLUSH_DEFAULT, 227 false); 228 } 229 } 230} 231 232static void 233v3d_predraw_check_outputs(struct pipe_context *pctx) 234{ 235 struct v3d_context *v3d = v3d_context(pctx); 236 237 /* Flush jobs reading from TF buffers that we are about to write. */ 238 if (v3d_transform_feedback_enabled(v3d)) { 239 struct v3d_streamout_stateobj *so = &v3d->streamout; 240 241 for (int i = 0; i < so->num_targets; i++) { 242 if (!so->targets[i]) 243 continue; 244 245 const struct pipe_stream_output_target *target = 246 so->targets[i]; 247 v3d_flush_jobs_reading_resource(v3d, target->buffer, 248 V3D_FLUSH_DEFAULT, 249 false); 250 } 251 } 252} 253 254/** 255 * Checks if the state for the current draw reads a particular resource in 256 * in the given shader stage. 257 */ 258static bool 259v3d_state_reads_resource(struct v3d_context *v3d, 260 struct pipe_resource *prsc, 261 enum pipe_shader_type s) 262{ 263 struct v3d_resource *rsc = v3d_resource(prsc); 264 265 /* Vertex buffers */ 266 if (s == PIPE_SHADER_VERTEX) { 267 u_foreach_bit(i, v3d->vertexbuf.enabled_mask) { 268 struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i]; 269 if (!vb->buffer.resource) 270 continue; 271 272 struct v3d_resource *vb_rsc = 273 v3d_resource(vb->buffer.resource); 274 if (rsc->bo == vb_rsc->bo) 275 return true; 276 } 277 } 278 279 /* Constant buffers */ 280 u_foreach_bit(i, v3d->constbuf[s].enabled_mask) { 281 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i]; 282 if (!cb->buffer) 283 continue; 284 285 struct v3d_resource *cb_rsc = v3d_resource(cb->buffer); 286 if (rsc->bo == cb_rsc->bo) 287 return true; 288 } 289 290 /* Shader storage buffers */ 291 u_foreach_bit(i, v3d->ssbo[s].enabled_mask) { 292 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i]; 293 if (!sb->buffer) 294 continue; 295 296 struct v3d_resource *sb_rsc = v3d_resource(sb->buffer); 297 if (rsc->bo == sb_rsc->bo) 298 return true; 299 } 300 301 /* Textures */ 302 for (int i = 0; i < v3d->tex[s].num_textures; i++) { 303 struct pipe_sampler_view *pview = v3d->tex[s].textures[i]; 304 if (!pview) 305 continue; 306 307 struct v3d_sampler_view *view = v3d_sampler_view(pview); 308 struct v3d_resource *v_rsc = v3d_resource(view->texture); 309 if (rsc->bo == v_rsc->bo) 310 return true; 311 } 312 313 return false; 314} 315 316static void 317v3d_emit_wait_for_tf(struct v3d_job *job) 318{ 319 /* XXX: we might be able to skip this in some cases, for now we 320 * always emit it. 321 */ 322 cl_emit(&job->bcl, FLUSH_TRANSFORM_FEEDBACK_DATA, flush); 323 324 cl_emit(&job->bcl, WAIT_FOR_TRANSFORM_FEEDBACK, wait) { 325 /* XXX: Wait for all outstanding writes... maybe we can do 326 * better in some cases. 327 */ 328 wait.block_count = 255; 329 } 330 331 /* We have just flushed all our outstanding TF work in this job so make 332 * sure we don't emit TF flushes again for any of it again. 333 */ 334 _mesa_set_clear(job->tf_write_prscs, NULL); 335} 336 337static void 338v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job) 339{ 340 if (!job->tf_enabled) 341 return; 342 343 set_foreach(job->tf_write_prscs, entry) { 344 struct pipe_resource *prsc = (struct pipe_resource *)entry->key; 345 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) { 346 /* Fragment shaders can only start executing after all 347 * binning (and thus TF) is complete. 348 * 349 * XXX: For VS/GS/TES, if the binning shader does not 350 * read the resource then we could also avoid emitting 351 * the wait. 352 */ 353 if (s == PIPE_SHADER_FRAGMENT) 354 continue; 355 356 if (v3d_state_reads_resource(v3d, prsc, s)) { 357 v3d_emit_wait_for_tf(job); 358 return; 359 } 360 } 361 } 362} 363 364#if V3D_VERSION >= 41 365static void 366v3d_emit_gs_state_record(struct v3d_job *job, 367 struct v3d_compiled_shader *gs_bin, 368 struct v3d_cl_reloc gs_bin_uniforms, 369 struct v3d_compiled_shader *gs, 370 struct v3d_cl_reloc gs_render_uniforms) 371{ 372 cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) { 373 shader.geometry_bin_mode_shader_code_address = 374 cl_address(v3d_resource(gs_bin->resource)->bo, 375 gs_bin->offset); 376 shader.geometry_bin_mode_shader_4_way_threadable = 377 gs_bin->prog_data.gs->base.threads == 4; 378 shader.geometry_bin_mode_shader_start_in_final_thread_section = 379 gs_bin->prog_data.gs->base.single_seg; 380 shader.geometry_bin_mode_shader_propagate_nans = true; 381 shader.geometry_bin_mode_shader_uniforms_address = 382 gs_bin_uniforms; 383 384 shader.geometry_render_mode_shader_code_address = 385 cl_address(v3d_resource(gs->resource)->bo, gs->offset); 386 shader.geometry_render_mode_shader_4_way_threadable = 387 gs->prog_data.gs->base.threads == 4; 388 shader.geometry_render_mode_shader_start_in_final_thread_section = 389 gs->prog_data.gs->base.single_seg; 390 shader.geometry_render_mode_shader_propagate_nans = true; 391 shader.geometry_render_mode_shader_uniforms_address = 392 gs_render_uniforms; 393 } 394} 395 396static uint8_t 397v3d_gs_output_primitive(enum shader_prim prim_type) 398{ 399 switch (prim_type) { 400 case SHADER_PRIM_POINTS: 401 return GEOMETRY_SHADER_POINTS; 402 case SHADER_PRIM_LINE_STRIP: 403 return GEOMETRY_SHADER_LINE_STRIP; 404 case SHADER_PRIM_TRIANGLE_STRIP: 405 return GEOMETRY_SHADER_TRI_STRIP; 406 default: 407 unreachable("Unsupported primitive type"); 408 } 409} 410 411static void 412v3d_emit_tes_gs_common_params(struct v3d_job *job, 413 uint8_t gs_out_prim_type, 414 uint8_t gs_num_invocations) 415{ 416 /* This, and v3d_emit_tes_gs_shader_params below, fill in default 417 * values for tessellation fields even though we don't support 418 * tessellation yet because our packing functions (and the simulator) 419 * complain if we don't. 420 */ 421 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) { 422 shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE; 423 shader.tessellation_point_mode = false; 424 shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN; 425 shader.tessellation_clockwise = true; 426 shader.tessellation_invocations = 1; 427 428 shader.geometry_shader_output_format = 429 v3d_gs_output_primitive(gs_out_prim_type); 430 shader.geometry_shader_instances = gs_num_invocations & 0x1F; 431 } 432} 433 434static uint8_t 435simd_width_to_gs_pack_mode(uint32_t width) 436{ 437 switch (width) { 438 case 16: 439 return V3D_PACK_MODE_16_WAY; 440 case 8: 441 return V3D_PACK_MODE_8_WAY; 442 case 4: 443 return V3D_PACK_MODE_4_WAY; 444 case 1: 445 return V3D_PACK_MODE_1_WAY; 446 default: 447 unreachable("Invalid SIMD width"); 448 }; 449} 450 451static void 452v3d_emit_tes_gs_shader_params(struct v3d_job *job, 453 uint32_t gs_simd, 454 uint32_t gs_vpm_output_size, 455 uint32_t gs_max_vpm_input_size_per_batch) 456{ 457 cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) { 458 shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED; 459 shader.per_patch_data_column_depth = 1; 460 shader.tcs_output_segment_size_in_sectors = 1; 461 shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY; 462 shader.tes_output_segment_size_in_sectors = 1; 463 shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY; 464 shader.gs_output_segment_size_in_sectors = gs_vpm_output_size; 465 shader.gs_output_segment_pack_mode = 466 simd_width_to_gs_pack_mode(gs_simd); 467 shader.tbg_max_patches_per_tcs_batch = 1; 468 shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0; 469 shader.tbg_min_tcs_output_segments_required_in_play = 1; 470 shader.tbg_min_per_patch_data_segments_required_in_play = 1; 471 shader.tpg_max_patches_per_tes_batch = 1; 472 shader.tpg_max_vertex_segments_per_tes_batch = 0; 473 shader.tpg_max_tcs_output_segments_per_tes_batch = 1; 474 shader.tpg_min_tes_output_segments_required_in_play = 1; 475 shader.gbg_max_tes_output_vertex_segments_per_gs_batch = 476 gs_max_vpm_input_size_per_batch; 477 shader.gbg_min_gs_output_segments_required_in_play = 1; 478 } 479} 480#endif 481 482static void 483v3d_emit_gl_shader_state(struct v3d_context *v3d, 484 const struct pipe_draw_info *info) 485{ 486 struct v3d_job *job = v3d->job; 487 /* V3D_DIRTY_VTXSTATE */ 488 struct v3d_vertex_stateobj *vtx = v3d->vtx; 489 /* V3D_DIRTY_VTXBUF */ 490 struct v3d_vertexbuf_stateobj *vertexbuf = &v3d->vertexbuf; 491 492 /* Upload the uniforms to the indirect CL first */ 493 struct v3d_cl_reloc fs_uniforms = 494 v3d_write_uniforms(v3d, job, v3d->prog.fs, 495 PIPE_SHADER_FRAGMENT); 496 497 struct v3d_cl_reloc gs_uniforms = { NULL, 0 }; 498 struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 }; 499 if (v3d->prog.gs) { 500 gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs, 501 PIPE_SHADER_GEOMETRY); 502 } 503 if (v3d->prog.gs_bin) { 504 gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin, 505 PIPE_SHADER_GEOMETRY); 506 } 507 508 struct v3d_cl_reloc vs_uniforms = 509 v3d_write_uniforms(v3d, job, v3d->prog.vs, 510 PIPE_SHADER_VERTEX); 511 struct v3d_cl_reloc cs_uniforms = 512 v3d_write_uniforms(v3d, job, v3d->prog.cs, 513 PIPE_SHADER_VERTEX); 514 515 /* Update the cache dirty flag based on the shader progs data */ 516 job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl; 517 job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl; 518 if (v3d->prog.gs_bin) { 519 job->tmu_dirty_rcl |= 520 v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl; 521 } 522 if (v3d->prog.gs) { 523 job->tmu_dirty_rcl |= 524 v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl; 525 } 526 job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl; 527 528 uint32_t num_elements_to_emit = 0; 529 for (int i = 0; i < vtx->num_elements; i++) { 530 struct pipe_vertex_element *elem = &vtx->pipe[i]; 531 struct pipe_vertex_buffer *vb = 532 &vertexbuf->vb[elem->vertex_buffer_index]; 533 if (vb->buffer.resource) 534 num_elements_to_emit++; 535 } 536 537 uint32_t shader_state_record_length = 538 cl_packet_length(GL_SHADER_STATE_RECORD); 539#if V3D_VERSION >= 41 540 if (v3d->prog.gs) { 541 shader_state_record_length += 542 cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) + 543 cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) + 544 2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS); 545 } 546#endif 547 548 /* See GFXH-930 workaround below */ 549 uint32_t shader_rec_offset = 550 v3d_cl_ensure_space(&job->indirect, 551 shader_state_record_length + 552 MAX2(num_elements_to_emit, 1) * 553 cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD), 554 32); 555 556 /* XXX perf: We should move most of the SHADER_STATE_RECORD setup to 557 * compile time, so that we mostly just have to OR the VS and FS 558 * records together at draw time. 559 */ 560 561 struct vpm_config vpm_cfg_bin, vpm_cfg; 562 563 assert(v3d->screen->devinfo.ver >= 41 || !v3d->prog.gs); 564 v3d_compute_vpm_config(&v3d->screen->devinfo, 565 v3d->prog.cs->prog_data.vs, 566 v3d->prog.vs->prog_data.vs, 567 v3d->prog.gs ? v3d->prog.gs_bin->prog_data.gs : NULL, 568 v3d->prog.gs ? v3d->prog.gs->prog_data.gs : NULL, 569 &vpm_cfg_bin, 570 &vpm_cfg); 571 572 if (v3d->prog.gs) { 573#if V3D_VERSION >= 41 574 v3d_emit_gs_state_record(v3d->job, 575 v3d->prog.gs_bin, gs_bin_uniforms, 576 v3d->prog.gs, gs_uniforms); 577 578 struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs; 579 v3d_emit_tes_gs_common_params(v3d->job, 580 gs->out_prim_type, 581 gs->num_invocations); 582 583 /* Bin Tes/Gs params */ 584 v3d_emit_tes_gs_shader_params(v3d->job, 585 vpm_cfg_bin.gs_width, 586 vpm_cfg_bin.Gd, 587 vpm_cfg_bin.Gv); 588 589 /* Render Tes/Gs params */ 590 v3d_emit_tes_gs_shader_params(v3d->job, 591 vpm_cfg.gs_width, 592 vpm_cfg.Gd, 593 vpm_cfg.Gv); 594#else 595 unreachable("No GS support pre-4.1"); 596#endif 597 } 598 599 cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) { 600 shader.enable_clipping = true; 601 /* V3D_DIRTY_PRIM_MODE | V3D_DIRTY_RASTERIZER */ 602 shader.point_size_in_shaded_vertex_data = 603 (info->mode == PIPE_PRIM_POINTS && 604 v3d->rasterizer->base.point_size_per_vertex); 605 606 /* Must be set if the shader modifies Z, discards, or modifies 607 * the sample mask. For any of these cases, the fragment 608 * shader needs to write the Z value (even just discards). 609 */ 610 shader.fragment_shader_does_z_writes = 611 v3d->prog.fs->prog_data.fs->writes_z; 612 613 /* Set if the EZ test must be disabled (due to shader side 614 * effects and the early_z flag not being present in the 615 * shader). 616 */ 617 shader.turn_off_early_z_test = 618 v3d->prog.fs->prog_data.fs->disable_ez; 619 620 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 = 621 v3d->prog.fs->prog_data.fs->uses_center_w; 622 623#if V3D_VERSION >= 41 624 shader.any_shader_reads_hardware_written_primitive_id = 625 (v3d->prog.gs && v3d->prog.gs->prog_data.gs->uses_pid) || 626 v3d->prog.fs->prog_data.fs->uses_pid; 627 shader.insert_primitive_id_as_first_varying_to_fragment_shader = 628 !v3d->prog.gs && v3d->prog.fs->prog_data.fs->uses_pid; 629#endif 630 631#if V3D_VERSION >= 40 632 shader.do_scoreboard_wait_on_first_thread_switch = 633 v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw; 634 shader.disable_implicit_point_line_varyings = 635 !v3d->prog.fs->prog_data.fs->uses_implicit_point_line_varyings; 636#endif 637 638 shader.number_of_varyings_in_fragment_shader = 639 v3d->prog.fs->prog_data.fs->num_inputs; 640 641 shader.coordinate_shader_propagate_nans = true; 642 shader.vertex_shader_propagate_nans = true; 643 shader.fragment_shader_propagate_nans = true; 644 645 shader.coordinate_shader_code_address = 646 cl_address(v3d_resource(v3d->prog.cs->resource)->bo, 647 v3d->prog.cs->offset); 648 shader.vertex_shader_code_address = 649 cl_address(v3d_resource(v3d->prog.vs->resource)->bo, 650 v3d->prog.vs->offset); 651 shader.fragment_shader_code_address = 652 cl_address(v3d_resource(v3d->prog.fs->resource)->bo, 653 v3d->prog.fs->offset); 654 655 /* XXX: Use combined input/output size flag in the common 656 * case. 657 */ 658 shader.coordinate_shader_has_separate_input_and_output_vpm_blocks = 659 v3d->prog.cs->prog_data.vs->separate_segments; 660 shader.vertex_shader_has_separate_input_and_output_vpm_blocks = 661 v3d->prog.vs->prog_data.vs->separate_segments; 662 663 shader.coordinate_shader_input_vpm_segment_size = 664 v3d->prog.cs->prog_data.vs->separate_segments ? 665 v3d->prog.cs->prog_data.vs->vpm_input_size : 1; 666 shader.vertex_shader_input_vpm_segment_size = 667 v3d->prog.vs->prog_data.vs->separate_segments ? 668 v3d->prog.vs->prog_data.vs->vpm_input_size : 1; 669 670 shader.coordinate_shader_output_vpm_segment_size = 671 v3d->prog.cs->prog_data.vs->vpm_output_size; 672 shader.vertex_shader_output_vpm_segment_size = 673 v3d->prog.vs->prog_data.vs->vpm_output_size; 674 675 shader.coordinate_shader_uniforms_address = cs_uniforms; 676 shader.vertex_shader_uniforms_address = vs_uniforms; 677 shader.fragment_shader_uniforms_address = fs_uniforms; 678 679#if V3D_VERSION >= 41 680 shader.min_coord_shader_input_segments_required_in_play = 681 vpm_cfg_bin.As; 682 shader.min_vertex_shader_input_segments_required_in_play = 683 vpm_cfg.As; 684 685 shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size = 686 vpm_cfg_bin.Ve; 687 shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size = 688 vpm_cfg.Ve; 689 690 shader.coordinate_shader_4_way_threadable = 691 v3d->prog.cs->prog_data.vs->base.threads == 4; 692 shader.vertex_shader_4_way_threadable = 693 v3d->prog.vs->prog_data.vs->base.threads == 4; 694 shader.fragment_shader_4_way_threadable = 695 v3d->prog.fs->prog_data.fs->base.threads == 4; 696 697 shader.coordinate_shader_start_in_final_thread_section = 698 v3d->prog.cs->prog_data.vs->base.single_seg; 699 shader.vertex_shader_start_in_final_thread_section = 700 v3d->prog.vs->prog_data.vs->base.single_seg; 701 shader.fragment_shader_start_in_final_thread_section = 702 v3d->prog.fs->prog_data.fs->base.single_seg; 703#else 704 shader.coordinate_shader_4_way_threadable = 705 v3d->prog.cs->prog_data.vs->base.threads == 4; 706 shader.coordinate_shader_2_way_threadable = 707 v3d->prog.cs->prog_data.vs->base.threads == 2; 708 shader.vertex_shader_4_way_threadable = 709 v3d->prog.vs->prog_data.vs->base.threads == 4; 710 shader.vertex_shader_2_way_threadable = 711 v3d->prog.vs->prog_data.vs->base.threads == 2; 712 shader.fragment_shader_4_way_threadable = 713 v3d->prog.fs->prog_data.fs->base.threads == 4; 714 shader.fragment_shader_2_way_threadable = 715 v3d->prog.fs->prog_data.fs->base.threads == 2; 716#endif 717 718 shader.vertex_id_read_by_coordinate_shader = 719 v3d->prog.cs->prog_data.vs->uses_vid; 720 shader.instance_id_read_by_coordinate_shader = 721 v3d->prog.cs->prog_data.vs->uses_iid; 722 shader.vertex_id_read_by_vertex_shader = 723 v3d->prog.vs->prog_data.vs->uses_vid; 724 shader.instance_id_read_by_vertex_shader = 725 v3d->prog.vs->prog_data.vs->uses_iid; 726 727 shader.address_of_default_attribute_values = 728 cl_address(v3d_resource(vtx->defaults)->bo, 729 vtx->defaults_offset); 730 } 731 732 bool cs_loaded_any = false; 733 for (int i = 0; i < vtx->num_elements; i++) { 734 struct pipe_vertex_element *elem = &vtx->pipe[i]; 735 struct pipe_vertex_buffer *vb = 736 &vertexbuf->vb[elem->vertex_buffer_index]; 737 struct v3d_resource *rsc = v3d_resource(vb->buffer.resource); 738 739 if (!rsc) 740 continue; 741 742 enum { size = cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD) }; 743 cl_emit_with_prepacked(&job->indirect, 744 GL_SHADER_STATE_ATTRIBUTE_RECORD, 745 &vtx->attrs[i * size], attr) { 746 attr.stride = vb->stride; 747 attr.address = cl_address(rsc->bo, 748 vb->buffer_offset + 749 elem->src_offset); 750 attr.number_of_values_read_by_coordinate_shader = 751 v3d->prog.cs->prog_data.vs->vattr_sizes[i]; 752 attr.number_of_values_read_by_vertex_shader = 753 v3d->prog.vs->prog_data.vs->vattr_sizes[i]; 754 755 /* GFXH-930: At least one attribute must be enabled 756 * and read by CS and VS. If we have attributes being 757 * consumed by the VS but not the CS, then set up a 758 * dummy load of the last attribute into the CS's VPM 759 * inputs. (Since CS is just dead-code-elimination 760 * compared to VS, we can't have CS loading but not 761 * VS). 762 */ 763 if (v3d->prog.cs->prog_data.vs->vattr_sizes[i]) 764 cs_loaded_any = true; 765 if (i == vtx->num_elements - 1 && !cs_loaded_any) { 766 attr.number_of_values_read_by_coordinate_shader = 1; 767 } 768#if V3D_VERSION >= 41 769 attr.maximum_index = 0xffffff; 770#endif 771 } 772 STATIC_ASSERT(sizeof(vtx->attrs) >= V3D_MAX_VS_INPUTS / 4 * size); 773 } 774 775 if (num_elements_to_emit == 0) { 776 /* GFXH-930: At least one attribute must be enabled and read 777 * by CS and VS. If we have no attributes being consumed by 778 * the shader, set up a dummy to be loaded into the VPM. 779 */ 780 cl_emit(&job->indirect, GL_SHADER_STATE_ATTRIBUTE_RECORD, attr) { 781 /* Valid address of data whose value will be unused. */ 782 attr.address = cl_address(job->indirect.bo, 0); 783 784 attr.type = ATTRIBUTE_FLOAT; 785 attr.stride = 0; 786 attr.vec_size = 1; 787 788 attr.number_of_values_read_by_coordinate_shader = 1; 789 attr.number_of_values_read_by_vertex_shader = 1; 790 } 791 num_elements_to_emit = 1; 792 } 793 794 cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) { 795 vcm.number_of_16_vertex_batches_for_binning = vpm_cfg_bin.Vc; 796 vcm.number_of_16_vertex_batches_for_rendering = vpm_cfg.Vc; 797 } 798 799#if V3D_VERSION >= 41 800 if (v3d->prog.gs) { 801 cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) { 802 state.address = cl_address(job->indirect.bo, 803 shader_rec_offset); 804 state.number_of_attribute_arrays = num_elements_to_emit; 805 } 806 } else { 807 cl_emit(&job->bcl, GL_SHADER_STATE, state) { 808 state.address = cl_address(job->indirect.bo, 809 shader_rec_offset); 810 state.number_of_attribute_arrays = num_elements_to_emit; 811 } 812 } 813#else 814 assert(!v3d->prog.gs); 815 cl_emit(&job->bcl, GL_SHADER_STATE, state) { 816 state.address = cl_address(job->indirect.bo, shader_rec_offset); 817 state.number_of_attribute_arrays = num_elements_to_emit; 818 } 819#endif 820 821 v3d_bo_unreference(&cs_uniforms.bo); 822 v3d_bo_unreference(&vs_uniforms.bo); 823 if (gs_uniforms.bo) 824 v3d_bo_unreference(&gs_uniforms.bo); 825 if (gs_bin_uniforms.bo) 826 v3d_bo_unreference(&gs_bin_uniforms.bo); 827 v3d_bo_unreference(&fs_uniforms.bo); 828} 829 830/** 831 * Updates the number of primitives generated from the number of vertices 832 * to draw. This only works when no GS is present, since otherwise the number 833 * of primitives generated cannot be determined in advance and we need to 834 * use the PRIMITIVE_COUNTS_FEEDBACK command instead, however, that requires 835 * a sync wait for the draw to complete, so we only use that when GS is present. 836 */ 837static void 838v3d_update_primitives_generated_counter(struct v3d_context *v3d, 839 const struct pipe_draw_info *info, 840 const struct pipe_draw_start_count_bias *draw) 841{ 842 assert(!v3d->prog.gs); 843 844 if (!v3d->active_queries) 845 return; 846 847 uint32_t prims = u_prims_for_vertices(info->mode, draw->count); 848 v3d->prims_generated += prims; 849} 850 851static void 852v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job) 853{ 854 /* If first_ez_state is V3D_EZ_DISABLED it means that we have already 855 * determined that we should disable EZ completely for all draw calls 856 * in this job. This will cause us to disable EZ for the entire job in 857 * the Tile Rendering Mode RCL packet and when we do that we need to 858 * make sure we never emit a draw call in the job with EZ enabled in 859 * the CFG_BITS packet, so ez_state must also be V3D_EZ_DISABLED. 860 */ 861 if (job->first_ez_state == V3D_EZ_DISABLED) { 862 assert(job->ez_state == V3D_EZ_DISABLED); 863 return; 864 } 865 866 /* If this is the first time we update EZ state for this job we first 867 * check if there is anything that requires disabling it completely 868 * for the entire job (based on state that is not related to the 869 * current draw call and pipeline state). 870 */ 871 if (!job->decided_global_ez_enable) { 872 job->decided_global_ez_enable = true; 873 874 if (!job->zsbuf) { 875 job->first_ez_state = V3D_EZ_DISABLED; 876 job->ez_state = V3D_EZ_DISABLED; 877 return; 878 } 879 880 /* GFXH-1918: the early-Z buffer may load incorrect depth 881 * values if the frame has odd width or height. Disable early-Z 882 * in this case. 883 */ 884 bool needs_depth_load = v3d->zsa && job->zsbuf && 885 v3d->zsa->base.depth_enabled && 886 (PIPE_CLEAR_DEPTH & ~job->clear); 887 if (needs_depth_load && 888 ((job->draw_width % 2 != 0) || (job->draw_height % 2 != 0))) { 889 perf_debug("Loading depth buffer for framebuffer with odd width " 890 "or height disables early-Z tests\n"); 891 job->first_ez_state = V3D_EZ_DISABLED; 892 job->ez_state = V3D_EZ_DISABLED; 893 return; 894 } 895 } 896 897 switch (v3d->zsa->ez_state) { 898 case V3D_EZ_UNDECIDED: 899 /* If the Z/S state didn't pick a direction but didn't 900 * disable, then go along with the current EZ state. This 901 * allows EZ optimization for Z func == EQUAL or NEVER. 902 */ 903 break; 904 905 case V3D_EZ_LT_LE: 906 case V3D_EZ_GT_GE: 907 /* If the Z/S state picked a direction, then it needs to match 908 * the current direction if we've decided on one. 909 */ 910 if (job->ez_state == V3D_EZ_UNDECIDED) 911 job->ez_state = v3d->zsa->ez_state; 912 else if (job->ez_state != v3d->zsa->ez_state) 913 job->ez_state = V3D_EZ_DISABLED; 914 break; 915 916 case V3D_EZ_DISABLED: 917 /* If the current Z/S state disables EZ because of a bad Z 918 * func or stencil operation, then we can't do any more EZ in 919 * this frame. 920 */ 921 job->ez_state = V3D_EZ_DISABLED; 922 break; 923 } 924 925 /* If the FS affects the Z of the pixels, then it may update against 926 * the chosen EZ direction (though we could use 927 * ARB_conservative_depth's hints to avoid this) 928 */ 929 if (v3d->prog.fs->prog_data.fs->writes_z && 930 !v3d->prog.fs->prog_data.fs->writes_z_from_fep) { 931 job->ez_state = V3D_EZ_DISABLED; 932 } 933 934 if (job->first_ez_state == V3D_EZ_UNDECIDED && 935 (job->ez_state != V3D_EZ_DISABLED || job->draw_calls_queued == 0)) 936 job->first_ez_state = job->ez_state; 937} 938 939static bool 940v3d_check_compiled_shaders(struct v3d_context *v3d) 941{ 942 static bool warned[5] = { 0 }; 943 944 uint32_t failed_stage = MESA_SHADER_NONE; 945 if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) { 946 failed_stage = MESA_SHADER_VERTEX; 947 } else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) || 948 (v3d->prog.gs && !v3d->prog.gs->resource)) { 949 failed_stage = MESA_SHADER_GEOMETRY; 950 } else if (v3d->prog.fs && !v3d->prog.fs->resource) { 951 failed_stage = MESA_SHADER_FRAGMENT; 952 } 953 954 if (likely(failed_stage == MESA_SHADER_NONE)) 955 return true; 956 957 if (!warned[failed_stage]) { 958 fprintf(stderr, 959 "%s shader failed to compile. Expect corruption.\n", 960 _mesa_shader_stage_to_string(failed_stage)); 961 warned[failed_stage] = true; 962 } 963 return false; 964} 965 966static void 967v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, 968 unsigned drawid_offset, 969 const struct pipe_draw_indirect_info *indirect, 970 const struct pipe_draw_start_count_bias *draws, 971 unsigned num_draws) 972{ 973 if (num_draws > 1) { 974 util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws); 975 return; 976 } 977 978 if (!indirect && (!draws[0].count || !info->instance_count)) 979 return; 980 981 struct v3d_context *v3d = v3d_context(pctx); 982 983 if (!indirect && 984 !info->primitive_restart && 985 !u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count)) 986 return; 987 988 /* Fall back for weird desktop GL primitive restart values. */ 989 if (info->primitive_restart && 990 info->index_size) { 991 uint32_t mask = util_prim_restart_index_from_size(info->index_size); 992 if (info->restart_index != mask) { 993 util_draw_vbo_without_prim_restart(pctx, info, drawid_offset, indirect, &draws[0]); 994 return; 995 } 996 } 997 998 /* Before setting up the draw, flush anything writing to the resources 999 * that we read from or reading from resources we write to. 1000 */ 1001 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) 1002 v3d_predraw_check_stage_inputs(pctx, s); 1003 1004 if (indirect && indirect->buffer) { 1005 v3d_flush_jobs_writing_resource(v3d, indirect->buffer, 1006 V3D_FLUSH_DEFAULT, false); 1007 } 1008 1009 v3d_predraw_check_outputs(pctx); 1010 1011 /* If transform feedback is active and we are switching primitive type 1012 * we need to submit the job before drawing and update the vertex count 1013 * written to TF based on the primitive type since we will need to 1014 * know the exact vertex count if the application decides to call 1015 * glDrawTransformFeedback() later. 1016 */ 1017 if (v3d->streamout.num_targets > 0 && 1018 u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) { 1019 v3d_update_primitive_counters(v3d); 1020 } 1021 1022 struct v3d_job *job = v3d_get_job_for_fbo(v3d); 1023 1024 /* If vertex texturing depends on the output of rendering, we need to 1025 * ensure that that rendering is complete before we run a coordinate 1026 * shader that depends on it. 1027 * 1028 * Given that doing that is unusual, for now we just block the binner 1029 * on the last submitted render, rather than tracking the last 1030 * rendering to each texture's BO. 1031 */ 1032 if (v3d->tex[PIPE_SHADER_VERTEX].num_textures || (indirect && indirect->buffer)) { 1033 perf_debug("Blocking binner on last render " 1034 "due to vertex texturing or indirect drawing.\n"); 1035 job->submit.in_sync_bcl = v3d->out_sync; 1036 } 1037 1038 /* We also need to ensure that compute is complete when render depends 1039 * on resources written by it. 1040 */ 1041 if (v3d->sync_on_last_compute_job) { 1042 job->submit.in_sync_bcl = v3d->out_sync; 1043 v3d->sync_on_last_compute_job = false; 1044 } 1045 1046 /* Mark SSBOs and images as being written. We don't actually know 1047 * which ones are read vs written, so just assume the worst. 1048 */ 1049 for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) { 1050 u_foreach_bit(i, v3d->ssbo[s].enabled_mask) { 1051 v3d_job_add_write_resource(job, 1052 v3d->ssbo[s].sb[i].buffer); 1053 job->tmu_dirty_rcl = true; 1054 } 1055 1056 u_foreach_bit(i, v3d->shaderimg[s].enabled_mask) { 1057 v3d_job_add_write_resource(job, 1058 v3d->shaderimg[s].si[i].base.resource); 1059 job->tmu_dirty_rcl = true; 1060 } 1061 } 1062 1063 /* Get space to emit our draw call into the BCL, using a branch to 1064 * jump to a new BO if necessary. 1065 */ 1066 v3d_cl_ensure_space_with_branch(&job->bcl, 256 /* XXX */); 1067 1068 if (v3d->prim_mode != info->mode) { 1069 v3d->prim_mode = info->mode; 1070 v3d->dirty |= V3D_DIRTY_PRIM_MODE; 1071 } 1072 1073 v3d_start_draw(v3d); 1074 v3d_update_compiled_shaders(v3d, info->mode); 1075 if (!v3d_check_compiled_shaders(v3d)) 1076 return; 1077 v3d_update_job_ez(v3d, job); 1078 1079 /* If this job was writing to transform feedback buffers before this 1080 * draw and we are reading from them here, then we need to wait for TF 1081 * to complete before we emit this draw. 1082 * 1083 * Notice this check needs to happen before we emit state for the 1084 * current draw call, where we update job->tf_enabled, so we can ensure 1085 * that we only check TF writes for prior draws. 1086 */ 1087 v3d_emit_wait_for_tf_if_needed(v3d, job); 1088 1089#if V3D_VERSION >= 41 1090 v3d41_emit_state(pctx); 1091#else 1092 v3d33_emit_state(pctx); 1093#endif 1094 1095 if (v3d->dirty & (V3D_DIRTY_VTXBUF | 1096 V3D_DIRTY_VTXSTATE | 1097 V3D_DIRTY_PRIM_MODE | 1098 V3D_DIRTY_RASTERIZER | 1099 V3D_DIRTY_COMPILED_CS | 1100 V3D_DIRTY_COMPILED_VS | 1101 V3D_DIRTY_COMPILED_GS_BIN | 1102 V3D_DIRTY_COMPILED_GS | 1103 V3D_DIRTY_COMPILED_FS | 1104 v3d->prog.cs->uniform_dirty_bits | 1105 v3d->prog.vs->uniform_dirty_bits | 1106 (v3d->prog.gs_bin ? 1107 v3d->prog.gs_bin->uniform_dirty_bits : 0) | 1108 (v3d->prog.gs ? 1109 v3d->prog.gs->uniform_dirty_bits : 0) | 1110 v3d->prog.fs->uniform_dirty_bits)) { 1111 v3d_emit_gl_shader_state(v3d, info); 1112 } 1113 1114 v3d->dirty = 0; 1115 1116 /* The Base Vertex/Base Instance packet sets those values to nonzero 1117 * for the next draw call only. 1118 */ 1119 if ((info->index_size && draws->index_bias) || info->start_instance) { 1120 cl_emit(&job->bcl, BASE_VERTEX_BASE_INSTANCE, base) { 1121 base.base_instance = info->start_instance; 1122 base.base_vertex = info->index_size ? draws->index_bias : 0; 1123 } 1124 } 1125 1126 uint32_t prim_tf_enable = 0; 1127#if V3D_VERSION < 40 1128 /* V3D 3.x: The HW only processes transform feedback on primitives 1129 * with the flag set. 1130 */ 1131 if (v3d->streamout.num_targets) 1132 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS); 1133#endif 1134 1135 if (!v3d->prog.gs) 1136 v3d_update_primitives_generated_counter(v3d, info, &draws[0]); 1137 1138 uint32_t hw_prim_type = v3d_hw_prim_type(info->mode); 1139 if (info->index_size) { 1140 uint32_t index_size = info->index_size; 1141 uint32_t offset = draws[0].start * index_size; 1142 struct pipe_resource *prsc; 1143 if (info->has_user_indices) { 1144 unsigned start_offset = draws[0].start * info->index_size; 1145 prsc = NULL; 1146 u_upload_data(v3d->uploader, start_offset, 1147 draws[0].count * info->index_size, 4, 1148 (char*)info->index.user + start_offset, 1149 &offset, &prsc); 1150 } else { 1151 prsc = info->index.resource; 1152 } 1153 struct v3d_resource *rsc = v3d_resource(prsc); 1154 1155#if V3D_VERSION >= 40 1156 cl_emit(&job->bcl, INDEX_BUFFER_SETUP, ib) { 1157 ib.address = cl_address(rsc->bo, 0); 1158 ib.size = rsc->bo->size; 1159 } 1160#endif 1161 1162 if (indirect && indirect->buffer) { 1163 cl_emit(&job->bcl, INDIRECT_INDEXED_INSTANCED_PRIM_LIST, prim) { 1164 prim.index_type = ffs(info->index_size) - 1; 1165#if V3D_VERSION < 40 1166 prim.address_of_indices_list = 1167 cl_address(rsc->bo, offset); 1168#endif /* V3D_VERSION < 40 */ 1169 prim.mode = hw_prim_type | prim_tf_enable; 1170 prim.enable_primitive_restarts = info->primitive_restart; 1171 1172 prim.number_of_draw_indirect_indexed_records = indirect->draw_count; 1173 1174 prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2; 1175 prim.address = cl_address(v3d_resource(indirect->buffer)->bo, 1176 indirect->offset); 1177 } 1178 } else if (info->instance_count > 1) { 1179 cl_emit(&job->bcl, INDEXED_INSTANCED_PRIM_LIST, prim) { 1180 prim.index_type = ffs(info->index_size) - 1; 1181#if V3D_VERSION >= 40 1182 prim.index_offset = offset; 1183#else /* V3D_VERSION < 40 */ 1184 prim.maximum_index = (1u << 31) - 1; /* XXX */ 1185 prim.address_of_indices_list = 1186 cl_address(rsc->bo, offset); 1187#endif /* V3D_VERSION < 40 */ 1188 prim.mode = hw_prim_type | prim_tf_enable; 1189 prim.enable_primitive_restarts = info->primitive_restart; 1190 1191 prim.number_of_instances = info->instance_count; 1192 prim.instance_length = draws[0].count; 1193 } 1194 } else { 1195 cl_emit(&job->bcl, INDEXED_PRIM_LIST, prim) { 1196 prim.index_type = ffs(info->index_size) - 1; 1197 prim.length = draws[0].count; 1198#if V3D_VERSION >= 40 1199 prim.index_offset = offset; 1200#else /* V3D_VERSION < 40 */ 1201 prim.maximum_index = (1u << 31) - 1; /* XXX */ 1202 prim.address_of_indices_list = 1203 cl_address(rsc->bo, offset); 1204#endif /* V3D_VERSION < 40 */ 1205 prim.mode = hw_prim_type | prim_tf_enable; 1206 prim.enable_primitive_restarts = info->primitive_restart; 1207 } 1208 } 1209 1210 if (info->has_user_indices) 1211 pipe_resource_reference(&prsc, NULL); 1212 } else { 1213 if (indirect && indirect->buffer) { 1214 cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) { 1215 prim.mode = hw_prim_type | prim_tf_enable; 1216 prim.number_of_draw_indirect_array_records = indirect->draw_count; 1217 1218 prim.stride_in_multiples_of_4_bytes = indirect->stride >> 2; 1219 prim.address = cl_address(v3d_resource(indirect->buffer)->bo, 1220 indirect->offset); 1221 } 1222 } else if (info->instance_count > 1) { 1223 struct pipe_stream_output_target *so = 1224 indirect && indirect->count_from_stream_output ? 1225 indirect->count_from_stream_output : NULL; 1226 uint32_t vert_count = so ? 1227 v3d_stream_output_target_get_vertex_count(so) : 1228 draws[0].count; 1229 cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) { 1230 prim.mode = hw_prim_type | prim_tf_enable; 1231 prim.index_of_first_vertex = draws[0].start; 1232 prim.number_of_instances = info->instance_count; 1233 prim.instance_length = vert_count; 1234 } 1235 } else { 1236 struct pipe_stream_output_target *so = 1237 indirect && indirect->count_from_stream_output ? 1238 indirect->count_from_stream_output : NULL; 1239 uint32_t vert_count = so ? 1240 v3d_stream_output_target_get_vertex_count(so) : 1241 draws[0].count; 1242 cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) { 1243 prim.mode = hw_prim_type | prim_tf_enable; 1244 prim.length = vert_count; 1245 prim.index_of_first_vertex = draws[0].start; 1246 } 1247 } 1248 } 1249 1250 /* A flush is required in between a TF draw and any following TF specs 1251 * packet, or the GPU may hang. Just flush each time for now. 1252 */ 1253 if (v3d->streamout.num_targets) 1254 cl_emit(&job->bcl, TRANSFORM_FEEDBACK_FLUSH_AND_COUNT, flush); 1255 1256 job->draw_calls_queued++; 1257 if (v3d->streamout.num_targets) 1258 job->tf_draw_calls_queued++; 1259 1260 /* Increment the TF offsets by how many verts we wrote. XXX: This 1261 * needs some clamping to the buffer size. 1262 */ 1263 for (int i = 0; i < v3d->streamout.num_targets; i++) 1264 v3d->streamout.offsets[i] += draws[0].count; 1265 1266 if (v3d->zsa && job->zsbuf && v3d->zsa->base.depth_enabled) { 1267 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture); 1268 v3d_job_add_bo(job, rsc->bo); 1269 1270 job->load |= PIPE_CLEAR_DEPTH & ~job->clear; 1271 if (v3d->zsa->base.depth_writemask) 1272 job->store |= PIPE_CLEAR_DEPTH; 1273 rsc->initialized_buffers = PIPE_CLEAR_DEPTH; 1274 } 1275 1276 if (v3d->zsa && job->zsbuf && v3d->zsa->base.stencil[0].enabled) { 1277 struct v3d_resource *rsc = v3d_resource(job->zsbuf->texture); 1278 if (rsc->separate_stencil) 1279 rsc = rsc->separate_stencil; 1280 1281 v3d_job_add_bo(job, rsc->bo); 1282 1283 job->load |= PIPE_CLEAR_STENCIL & ~job->clear; 1284 if (v3d->zsa->base.stencil[0].writemask || 1285 v3d->zsa->base.stencil[1].writemask) { 1286 job->store |= PIPE_CLEAR_STENCIL; 1287 } 1288 rsc->initialized_buffers |= PIPE_CLEAR_STENCIL; 1289 } 1290 1291 for (int i = 0; i < job->nr_cbufs; i++) { 1292 uint32_t bit = PIPE_CLEAR_COLOR0 << i; 1293 int blend_rt = v3d->blend->base.independent_blend_enable ? i : 0; 1294 1295 if (job->store & bit || !job->cbufs[i]) 1296 continue; 1297 struct v3d_resource *rsc = v3d_resource(job->cbufs[i]->texture); 1298 1299 job->load |= bit & ~job->clear; 1300 if (v3d->blend->base.rt[blend_rt].colormask) 1301 job->store |= bit; 1302 v3d_job_add_bo(job, rsc->bo); 1303 } 1304 1305 if (job->referenced_size > 768 * 1024 * 1024) { 1306 perf_debug("Flushing job with %dkb to try to free up memory\n", 1307 job->referenced_size / 1024); 1308 v3d_flush(pctx); 1309 } 1310 1311 if (unlikely(V3D_DEBUG & V3D_DEBUG_ALWAYS_FLUSH)) 1312 v3d_flush(pctx); 1313} 1314 1315#if V3D_VERSION >= 41 1316#define V3D_CSD_CFG012_WG_COUNT_SHIFT 16 1317#define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0 1318/* Allow this dispatch to start while the last one is still running. */ 1319#define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26) 1320/* Maximum supergroup ID. 6 bits. */ 1321#define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20 1322/* Batches per supergroup minus 1. 8 bits. */ 1323#define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12 1324/* Workgroups per supergroup, 0 means 16 */ 1325#define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8 1326#define V3D_CSD_CFG3_WG_SIZE_SHIFT 0 1327 1328#define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2) 1329#define V3D_CSD_CFG5_SINGLE_SEG (1 << 1) 1330#define V3D_CSD_CFG5_THREADING (1 << 0) 1331 1332static void 1333v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info) 1334{ 1335 struct v3d_context *v3d = v3d_context(pctx); 1336 struct v3d_screen *screen = v3d->screen; 1337 1338 v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE); 1339 1340 v3d_update_compiled_cs(v3d); 1341 1342 if (!v3d->prog.compute->resource) { 1343 static bool warned = false; 1344 if (!warned) { 1345 fprintf(stderr, 1346 "Compute shader failed to compile. " 1347 "Expect corruption.\n"); 1348 warned = true; 1349 } 1350 return; 1351 } 1352 1353 /* Some of the units of scale: 1354 * 1355 * - Batches of 16 work items (shader invocations) that will be queued 1356 * to the run on a QPU at once. 1357 * 1358 * - Workgroups composed of work items based on the shader's layout 1359 * declaration. 1360 * 1361 * - Supergroups of 1-16 workgroups. There can only be 16 supergroups 1362 * running at a time on the core, so we want to keep them large to 1363 * keep the QPUs busy, but a whole supergroup will sync at a barrier 1364 * so we want to keep them small if one is present. 1365 */ 1366 struct drm_v3d_submit_csd submit = { 0 }; 1367 struct v3d_job *job = v3d_job_create(v3d); 1368 1369 /* Set up the actual number of workgroups, synchronously mapping the 1370 * indirect buffer if necessary to get the dimensions. 1371 */ 1372 if (info->indirect) { 1373 struct pipe_transfer *transfer; 1374 uint32_t *map = pipe_buffer_map_range(pctx, info->indirect, 1375 info->indirect_offset, 1376 3 * sizeof(uint32_t), 1377 PIPE_MAP_READ, 1378 &transfer); 1379 memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t)); 1380 pipe_buffer_unmap(pctx, transfer); 1381 1382 if (v3d->compute_num_workgroups[0] == 0 || 1383 v3d->compute_num_workgroups[1] == 0 || 1384 v3d->compute_num_workgroups[2] == 0) { 1385 /* Nothing to dispatch, so skip the draw (CSD can't 1386 * handle 0 workgroups). 1387 */ 1388 return; 1389 } 1390 } else { 1391 v3d->compute_num_workgroups[0] = info->grid[0]; 1392 v3d->compute_num_workgroups[1] = info->grid[1]; 1393 v3d->compute_num_workgroups[2] = info->grid[2]; 1394 } 1395 1396 uint32_t num_wgs = 1; 1397 for (int i = 0; i < 3; i++) { 1398 num_wgs *= v3d->compute_num_workgroups[i]; 1399 submit.cfg[i] |= (v3d->compute_num_workgroups[i] << 1400 V3D_CSD_CFG012_WG_COUNT_SHIFT); 1401 } 1402 1403 uint32_t wg_size = info->block[0] * info->block[1] * info->block[2]; 1404 1405 struct v3d_compute_prog_data *compute = 1406 v3d->prog.compute->prog_data.compute; 1407 uint32_t wgs_per_sg = 1408 v3d_csd_choose_workgroups_per_supergroup( 1409 &v3d->screen->devinfo, 1410 compute->has_subgroups, 1411 compute->base.has_control_barrier, 1412 compute->base.threads, 1413 num_wgs, wg_size); 1414 1415 uint32_t batches_per_sg = DIV_ROUND_UP(wgs_per_sg * wg_size, 16); 1416 uint32_t whole_sgs = num_wgs / wgs_per_sg; 1417 uint32_t rem_wgs = num_wgs - whole_sgs * wgs_per_sg; 1418 uint32_t num_batches = batches_per_sg * whole_sgs + 1419 DIV_ROUND_UP(rem_wgs * wg_size, 16); 1420 1421 submit.cfg[3] |= (wgs_per_sg & 0xf) << V3D_CSD_CFG3_WGS_PER_SG_SHIFT; 1422 submit.cfg[3] |= 1423 (batches_per_sg - 1) << V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT; 1424 submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT; 1425 1426 1427 /* Number of batches the dispatch will invoke (minus 1). */ 1428 submit.cfg[4] = num_batches - 1; 1429 1430 /* Make sure we didn't accidentally underflow. */ 1431 assert(submit.cfg[4] != ~0); 1432 1433 v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo); 1434 submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset + 1435 v3d->prog.compute->offset); 1436 submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS; 1437 if (v3d->prog.compute->prog_data.base->single_seg) 1438 submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG; 1439 if (v3d->prog.compute->prog_data.base->threads == 4) 1440 submit.cfg[5] |= V3D_CSD_CFG5_THREADING; 1441 1442 if (v3d->prog.compute->prog_data.compute->shared_size) { 1443 v3d->compute_shared_memory = 1444 v3d_bo_alloc(v3d->screen, 1445 v3d->prog.compute->prog_data.compute->shared_size * 1446 wgs_per_sg, 1447 "shared_vars"); 1448 } 1449 1450 struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job, 1451 v3d->prog.compute, 1452 PIPE_SHADER_COMPUTE); 1453 v3d_job_add_bo(job, uniforms.bo); 1454 submit.cfg[6] = uniforms.bo->offset + uniforms.offset; 1455 1456 /* Pull some job state that was stored in a SUBMIT_CL struct out to 1457 * our SUBMIT_CSD struct 1458 */ 1459 submit.bo_handles = job->submit.bo_handles; 1460 submit.bo_handle_count = job->submit.bo_handle_count; 1461 1462 /* Serialize this in the rest of our command stream. */ 1463 submit.in_sync = v3d->out_sync; 1464 submit.out_sync = v3d->out_sync; 1465 1466 if (v3d->active_perfmon) { 1467 assert(screen->has_perfmon); 1468 submit.perfmon_id = v3d->active_perfmon->kperfmon_id; 1469 } 1470 1471 v3d->last_perfmon = v3d->active_perfmon; 1472 1473 if (!(unlikely(V3D_DEBUG & V3D_DEBUG_NORAST))) { 1474 int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD, 1475 &submit); 1476 static bool warned = false; 1477 if (ret && !warned) { 1478 fprintf(stderr, "CSD submit call returned %s. " 1479 "Expect corruption.\n", strerror(errno)); 1480 warned = true; 1481 } else if (!ret) { 1482 if (v3d->active_perfmon) 1483 v3d->active_perfmon->job_submitted = true; 1484 } 1485 } 1486 1487 v3d_job_free(v3d, job); 1488 1489 /* Mark SSBOs as being written.. we don't actually know which ones are 1490 * read vs written, so just assume the worst 1491 */ 1492 u_foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) { 1493 struct v3d_resource *rsc = v3d_resource( 1494 v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer); 1495 rsc->writes++; 1496 rsc->compute_written = true; 1497 } 1498 1499 u_foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) { 1500 struct v3d_resource *rsc = v3d_resource( 1501 v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource); 1502 rsc->writes++; 1503 rsc->compute_written = true; 1504 } 1505 1506 v3d_bo_unreference(&uniforms.bo); 1507 v3d_bo_unreference(&v3d->compute_shared_memory); 1508} 1509#endif 1510 1511/** 1512 * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles. 1513 */ 1514static void 1515v3d_draw_clear(struct v3d_context *v3d, 1516 unsigned buffers, 1517 const union pipe_color_union *color, 1518 double depth, unsigned stencil) 1519{ 1520 v3d_blitter_save(v3d, false); 1521 util_blitter_clear(v3d->blitter, 1522 v3d->framebuffer.width, 1523 v3d->framebuffer.height, 1524 util_framebuffer_get_num_layers(&v3d->framebuffer), 1525 buffers, color, depth, stencil, 1526 util_framebuffer_get_num_samples(&v3d->framebuffer) > 1); 1527} 1528 1529/** 1530 * Attempts to perform the GL clear by using the TLB's fast clear at the start 1531 * of the frame. 1532 */ 1533static unsigned 1534v3d_tlb_clear(struct v3d_job *job, unsigned buffers, 1535 const union pipe_color_union *color, 1536 double depth, unsigned stencil) 1537{ 1538 struct v3d_context *v3d = job->v3d; 1539 1540 if (job->draw_calls_queued) { 1541 /* If anything in the CL has drawn using the buffer, then the 1542 * TLB clear we're trying to add now would happen before that 1543 * drawing. 1544 */ 1545 buffers &= ~(job->load | job->store); 1546 } 1547 1548 /* GFXH-1461: If we were to emit a load of just depth or just stencil, 1549 * then the clear for the other may get lost. We need to decide now 1550 * if it would be possible to need to emit a load of just one after 1551 * we've set up our TLB clears. 1552 */ 1553 if (buffers & PIPE_CLEAR_DEPTHSTENCIL && 1554 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL && 1555 job->zsbuf && 1556 util_format_is_depth_and_stencil(job->zsbuf->texture->format)) { 1557 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL; 1558 } 1559 1560 for (int i = 0; i < job->nr_cbufs; i++) { 1561 uint32_t bit = PIPE_CLEAR_COLOR0 << i; 1562 if (!(buffers & bit)) 1563 continue; 1564 1565 struct pipe_surface *psurf = v3d->framebuffer.cbufs[i]; 1566 struct v3d_surface *surf = v3d_surface(psurf); 1567 struct v3d_resource *rsc = v3d_resource(psurf->texture); 1568 1569 union util_color uc; 1570 uint32_t internal_size = 4 << surf->internal_bpp; 1571 1572 /* While hardware supports clamping, this is not applied on 1573 * the clear values, so we need to do it manually. 1574 * 1575 * "Clamping is performed on color values immediately as they 1576 * enter the TLB and after blending. Clamping is not 1577 * performed on the clear color." 1578 */ 1579 union pipe_color_union clamped_color = 1580 util_clamp_color(psurf->format, color); 1581 1582 if (v3d->swap_color_rb & (1 << i)) { 1583 union pipe_color_union orig_color = clamped_color; 1584 clamped_color.f[0] = orig_color.f[2]; 1585 clamped_color.f[1] = orig_color.f[1]; 1586 clamped_color.f[2] = orig_color.f[0]; 1587 clamped_color.f[3] = orig_color.f[3]; 1588 } 1589 1590 switch (surf->internal_type) { 1591 case V3D_INTERNAL_TYPE_8: 1592 util_pack_color(clamped_color.f, PIPE_FORMAT_R8G8B8A8_UNORM, 1593 &uc); 1594 memcpy(job->clear_color[i], uc.ui, internal_size); 1595 break; 1596 case V3D_INTERNAL_TYPE_8I: 1597 case V3D_INTERNAL_TYPE_8UI: 1598 job->clear_color[i][0] = ((clamped_color.ui[0] & 0xff) | 1599 (clamped_color.ui[1] & 0xff) << 8 | 1600 (clamped_color.ui[2] & 0xff) << 16 | 1601 (clamped_color.ui[3] & 0xff) << 24); 1602 break; 1603 case V3D_INTERNAL_TYPE_16F: 1604 util_pack_color(clamped_color.f, PIPE_FORMAT_R16G16B16A16_FLOAT, 1605 &uc); 1606 memcpy(job->clear_color[i], uc.ui, internal_size); 1607 break; 1608 case V3D_INTERNAL_TYPE_16I: 1609 case V3D_INTERNAL_TYPE_16UI: 1610 job->clear_color[i][0] = ((clamped_color.ui[0] & 0xffff) | 1611 clamped_color.ui[1] << 16); 1612 job->clear_color[i][1] = ((clamped_color.ui[2] & 0xffff) | 1613 clamped_color.ui[3] << 16); 1614 break; 1615 case V3D_INTERNAL_TYPE_32F: 1616 case V3D_INTERNAL_TYPE_32I: 1617 case V3D_INTERNAL_TYPE_32UI: 1618 memcpy(job->clear_color[i], clamped_color.ui, internal_size); 1619 break; 1620 } 1621 1622 rsc->initialized_buffers |= bit; 1623 } 1624 1625 unsigned zsclear = buffers & PIPE_CLEAR_DEPTHSTENCIL; 1626 if (zsclear) { 1627 struct v3d_resource *rsc = 1628 v3d_resource(v3d->framebuffer.zsbuf->texture); 1629 1630 if (zsclear & PIPE_CLEAR_DEPTH) 1631 job->clear_z = depth; 1632 if (zsclear & PIPE_CLEAR_STENCIL) 1633 job->clear_s = stencil; 1634 1635 rsc->initialized_buffers |= zsclear; 1636 } 1637 1638 job->draw_min_x = 0; 1639 job->draw_min_y = 0; 1640 job->draw_max_x = v3d->framebuffer.width; 1641 job->draw_max_y = v3d->framebuffer.height; 1642 job->clear |= buffers; 1643 job->store |= buffers; 1644 job->scissor.disabled = true; 1645 1646 v3d_start_draw(v3d); 1647 1648 return buffers; 1649} 1650 1651static void 1652v3d_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state, 1653 const union pipe_color_union *color, double depth, unsigned stencil) 1654{ 1655 struct v3d_context *v3d = v3d_context(pctx); 1656 struct v3d_job *job = v3d_get_job_for_fbo(v3d); 1657 1658 buffers &= ~v3d_tlb_clear(job, buffers, color, depth, stencil); 1659 1660 if (buffers) 1661 v3d_draw_clear(v3d, buffers, color, depth, stencil); 1662} 1663 1664static void 1665v3d_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps, 1666 const union pipe_color_union *color, 1667 unsigned x, unsigned y, unsigned w, unsigned h, 1668 bool render_condition_enabled) 1669{ 1670 fprintf(stderr, "unimpl: clear RT\n"); 1671} 1672 1673static void 1674v3d_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps, 1675 unsigned buffers, double depth, unsigned stencil, 1676 unsigned x, unsigned y, unsigned w, unsigned h, 1677 bool render_condition_enabled) 1678{ 1679 fprintf(stderr, "unimpl: clear DS\n"); 1680} 1681 1682void 1683v3dX(draw_init)(struct pipe_context *pctx) 1684{ 1685 pctx->draw_vbo = v3d_draw_vbo; 1686 pctx->clear = v3d_clear; 1687 pctx->clear_render_target = v3d_clear_render_target; 1688 pctx->clear_depth_stencil = v3d_clear_depth_stencil; 1689#if V3D_VERSION >= 41 1690 if (v3d_context(pctx)->screen->has_csd) 1691 pctx->launch_grid = v3d_launch_grid; 1692#endif 1693} 1694