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