gallium: add PIPE_SHADER_CAP_GLSL_16BIT_TEMPS for LowerPrecisionTemporaries
[mesa.git] / src / gallium / drivers / v3d / v3dx_draw.c
index efc8d249dce35aa904a958fc2b41f0b4d0a2f13e..9a5da07ec809c876fc9c9fcdf3b842e528ea51fe 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "util/u_blitter.h"
 #include "util/u_prim.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_pack_color.h"
 #include "util/u_prim_restart.h"
 #include "util/u_upload_mgr.h"
@@ -55,11 +55,14 @@ v3d_start_draw(struct v3d_context *v3d)
         job->submit.bcl_start = job->bcl.bo->offset;
         v3d_job_add_bo(job, job->bcl.bo);
 
+        uint32_t fb_layers = util_framebuffer_get_num_layers(&v3d->framebuffer);
+
         /* The PTB will request the tile alloc initial size per tile at start
          * of tile binning.
          */
-        uint32_t tile_alloc_size = (job->draw_tiles_x *
-                                    job->draw_tiles_y) * 64;
+        uint32_t tile_alloc_size =
+                MAX2(fb_layers, 1) * job->draw_tiles_x * job->draw_tiles_y * 64;
+
         /* The PTB allocates in aligned 4k chunks after the initial setup. */
         tile_alloc_size = align(tile_alloc_size, 4096);
 
@@ -79,10 +82,21 @@ v3d_start_draw(struct v3d_context *v3d)
                                        "tile_alloc");
         uint32_t tsda_per_tile_size = v3d->screen->devinfo.ver >= 40 ? 256 : 64;
         job->tile_state = v3d_bo_alloc(v3d->screen,
+                                       MAX2(fb_layers, 1) *
                                        job->draw_tiles_y *
                                        job->draw_tiles_x *
                                        tsda_per_tile_size,
                                        "TSDA");
+#if V3D_VERSION >= 41
+        /* This must go before the binning mode configuration. It is
+         * required for layered framebuffers to work.
+         */
+        if (fb_layers > 0) {
+                cl_emit(&job->bcl, NUMBER_OF_LAYERS, config) {
+                        config.number_of_layers = fb_layers;
+                }
+        }
+#endif
 
 #if V3D_VERSION >= 40
         cl_emit(&job->bcl, TILE_BINNING_MODE_CFG, config) {
@@ -137,6 +151,7 @@ v3d_start_draw(struct v3d_context *v3d)
         job->needs_flush = true;
         job->draw_width = v3d->framebuffer.width;
         job->draw_height = v3d->framebuffer.height;
+        job->num_layers = fb_layers;
 }
 
 static void
@@ -157,7 +172,8 @@ v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
                         v3d_update_shadow_texture(pctx, &view->base);
 
                 v3d_flush_jobs_writing_resource(v3d, view->texture,
-                                                V3D_FLUSH_DEFAULT);
+                                                V3D_FLUSH_DEFAULT,
+                                                s == PIPE_SHADER_COMPUTE);
         }
 
         /* Flush writes to UBOs. */
@@ -165,7 +181,8 @@ v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
                 struct pipe_constant_buffer *cb = &v3d->constbuf[s].cb[i];
                 if (cb->buffer) {
                         v3d_flush_jobs_writing_resource(v3d, cb->buffer,
-                                                        V3D_FLUSH_DEFAULT);
+                                                        V3D_FLUSH_DEFAULT,
+                                                        s == PIPE_SHADER_COMPUTE);
                 }
         }
 
@@ -174,7 +191,8 @@ v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
                 struct pipe_shader_buffer *sb = &v3d->ssbo[s].sb[i];
                 if (sb->buffer) {
                         v3d_flush_jobs_reading_resource(v3d, sb->buffer,
-                                                        V3D_FLUSH_NOT_CURRENT_JOB);
+                                                        V3D_FLUSH_NOT_CURRENT_JOB,
+                                                        s == PIPE_SHADER_COMPUTE);
                 }
         }
 
@@ -183,7 +201,8 @@ v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
                 struct v3d_image_view *view = &v3d->shaderimg[s].si[i];
 
                 v3d_flush_jobs_reading_resource(v3d, view->base.resource,
-                                                V3D_FLUSH_NOT_CURRENT_JOB);
+                                                V3D_FLUSH_NOT_CURRENT_JOB,
+                                                s == PIPE_SHADER_COMPUTE);
         }
 
         /* Flush writes to our vertex buffers (i.e. from transform feedback) */
@@ -192,7 +211,8 @@ v3d_predraw_check_stage_inputs(struct pipe_context *pctx,
                         struct pipe_vertex_buffer *vb = &v3d->vertexbuf.vb[i];
 
                         v3d_flush_jobs_writing_resource(v3d, vb->buffer.resource,
-                                                        V3D_FLUSH_DEFAULT);
+                                                        V3D_FLUSH_DEFAULT,
+                                                        false);
                 }
         }
 }
@@ -213,7 +233,8 @@ v3d_predraw_check_outputs(struct pipe_context *pctx)
                         const struct pipe_stream_output_target *target =
                                 so->targets[i];
                         v3d_flush_jobs_reading_resource(v3d, target->buffer,
-                                                        V3D_FLUSH_DEFAULT);
+                                                        V3D_FLUSH_DEFAULT,
+                                                        false);
                 }
         }
 }
@@ -328,6 +349,279 @@ v3d_emit_wait_for_tf_if_needed(struct v3d_context *v3d, struct v3d_job *job)
         }
 }
 
+struct vpm_config {
+        uint32_t As;
+        uint32_t Vc;
+        uint32_t Gs;
+        uint32_t Gd;
+        uint32_t Gv;
+        uint32_t Ve;
+        uint32_t gs_width;
+};
+
+#if V3D_VERSION >= 41
+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)
+{
+        cl_emit(&job->indirect, GEOMETRY_SHADER_STATE_RECORD, shader) {
+                shader.geometry_bin_mode_shader_code_address =
+                        cl_address(v3d_resource(gs_bin->resource)->bo,
+                                   gs_bin->offset);
+                shader.geometry_bin_mode_shader_4_way_threadable =
+                        gs_bin->prog_data.gs->base.threads == 4;
+                shader.geometry_bin_mode_shader_start_in_final_thread_section =
+                        gs_bin->prog_data.gs->base.single_seg;
+                shader.geometry_bin_mode_shader_propagate_nans = true;
+                shader.geometry_bin_mode_shader_uniforms_address =
+                        gs_bin_uniforms;
+
+                shader.geometry_render_mode_shader_code_address =
+                        cl_address(v3d_resource(gs->resource)->bo, gs->offset);
+                shader.geometry_render_mode_shader_4_way_threadable =
+                        gs->prog_data.gs->base.threads == 4;
+                shader.geometry_render_mode_shader_start_in_final_thread_section =
+                        gs->prog_data.gs->base.single_seg;
+                shader.geometry_render_mode_shader_propagate_nans = true;
+                shader.geometry_render_mode_shader_uniforms_address =
+                        gs_render_uniforms;
+        }
+}
+
+static uint8_t
+v3d_gs_output_primitive(uint32_t prim_type)
+{
+    switch (prim_type) {
+    case GL_POINTS:
+        return GEOMETRY_SHADER_POINTS;
+    case GL_LINE_STRIP:
+        return GEOMETRY_SHADER_LINE_STRIP;
+    case GL_TRIANGLE_STRIP:
+        return GEOMETRY_SHADER_TRI_STRIP;
+    default:
+        unreachable("Unsupported primitive type");
+    }
+}
+
+static void
+v3d_emit_tes_gs_common_params(struct v3d_job *job,
+                              uint8_t gs_out_prim_type,
+                              uint8_t gs_num_invocations)
+{
+        /* This, and v3d_emit_tes_gs_shader_params below, fill in default
+         * values for tessellation fields even though we don't support
+         * tessellation yet because our packing functions (and the simulator)
+         * complain if we don't.
+         */
+        cl_emit(&job->indirect, TESSELLATION_GEOMETRY_COMMON_PARAMS, shader) {
+                shader.tessellation_type = TESSELLATION_TYPE_TRIANGLE;
+                shader.tessellation_point_mode = false;
+                shader.tessellation_edge_spacing = TESSELLATION_EDGE_SPACING_EVEN;
+                shader.tessellation_clockwise = true;
+                shader.tessellation_invocations = 1;
+
+                shader.geometry_shader_output_format =
+                        v3d_gs_output_primitive(gs_out_prim_type);
+                shader.geometry_shader_instances = gs_num_invocations & 0x1F;
+        }
+}
+
+static uint8_t
+simd_width_to_gs_pack_mode(uint32_t width)
+{
+    switch (width) {
+    case 16:
+        return V3D_PACK_MODE_16_WAY;
+    case 8:
+        return V3D_PACK_MODE_8_WAY;
+    case 4:
+        return V3D_PACK_MODE_4_WAY;
+    case 1:
+        return V3D_PACK_MODE_1_WAY;
+    default:
+        unreachable("Invalid SIMD width");
+    };
+}
+
+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)
+{
+        cl_emit(&job->indirect, TESSELLATION_GEOMETRY_SHADER_PARAMS, shader) {
+                shader.tcs_batch_flush_mode = V3D_TCS_FLUSH_MODE_FULLY_PACKED;
+                shader.per_patch_data_column_depth = 1;
+                shader.tcs_output_segment_size_in_sectors = 1;
+                shader.tcs_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
+                shader.tes_output_segment_size_in_sectors = 1;
+                shader.tes_output_segment_pack_mode = V3D_PACK_MODE_16_WAY;
+                shader.gs_output_segment_size_in_sectors = gs_vpm_output_size;
+                shader.gs_output_segment_pack_mode =
+                        simd_width_to_gs_pack_mode(gs_simd);
+                shader.tbg_max_patches_per_tcs_batch = 1;
+                shader.tbg_max_extra_vertex_segs_for_patches_after_first = 0;
+                shader.tbg_min_tcs_output_segments_required_in_play = 1;
+                shader.tbg_min_per_patch_data_segments_required_in_play = 1;
+                shader.tpg_max_patches_per_tes_batch = 1;
+                shader.tpg_max_vertex_segments_per_tes_batch = 0;
+                shader.tpg_max_tcs_output_segments_per_tes_batch = 1;
+                shader.tpg_min_tes_output_segments_required_in_play = 1;
+                shader.gbg_max_tes_output_vertex_segments_per_gs_batch =
+                        gs_max_vpm_input_size_per_batch;
+                shader.gbg_min_gs_output_segments_required_in_play = 1;
+        }
+}
+
+static inline uint32_t
+compute_vpm_size_in_sectors(const struct v3d_device_info *devinfo)
+{
+        assert(devinfo->vpm_size > 0);
+        const uint32_t sector_size = V3D_CHANNELS * sizeof(uint32_t) * 8;
+        return devinfo->vpm_size / sector_size;
+}
+
+/* Computes various parameters affecting VPM memory configuration for programs
+ * involving geometry shaders to ensure the program fits in memory and honors
+ * requirements described in section "VPM usage" of the programming manual.
+ */
+static void
+compute_vpm_config_gs(struct v3d_device_info *devinfo,
+                      struct v3d_vs_prog_data *vs,
+                      struct v3d_gs_prog_data *gs,
+                      struct vpm_config *vpm_cfg_out)
+{
+        const uint32_t A = vs->separate_segments ? 1 : 0;
+        const uint32_t Ad = vs->vpm_input_size;
+        const uint32_t Vd = vs->vpm_output_size;
+
+        const uint32_t vpm_size = compute_vpm_size_in_sectors(devinfo);
+
+        /* Try to fit program into our VPM memory budget by adjusting
+         * configurable parameters iteratively. We do this in two phases:
+         * the first phase tries to fit the program into the total available
+         * VPM memory. If we suceed at that, then the second phase attempts
+         * to fit the program into half of that budget so we can run bin and
+         * render programs in parallel.
+         */
+        struct vpm_config vpm_cfg[2];
+        struct vpm_config *final_vpm_cfg = NULL;
+        uint32_t phase = 0;
+
+        vpm_cfg[phase].As = 1;
+        vpm_cfg[phase].Gs = 1;
+        vpm_cfg[phase].Gd = gs->vpm_output_size;
+        vpm_cfg[phase].gs_width = gs->simd_width;
+
+        /* While there is a requirement that Vc >= [Vn / 16], this is
+         * always the case when tessellation is not present because in that
+         * case Vn can only be 6 at most (when input primitive is triangles
+         * with adjacency).
+         *
+         * We always choose Vc=2. We can't go lower than this due to GFXH-1744,
+         * and Broadcom has not found it worth it to increase it beyond this
+         * in general. Increasing Vc also increases VPM memory pressure which
+         * can turn up being detrimental for performance in some scenarios.
+         */
+        vpm_cfg[phase].Vc = 2;
+
+        /* Gv is a constraint on the hardware to not exceed the
+         * specified number of vertex segments per GS batch. If adding a
+         * new primitive to a GS batch would result in a range of more
+         * than Gv vertex segments being referenced by the batch, then
+         * the hardware will flush the batch and start a new one. This
+         * means that we can choose any value we want, we just need to
+         * be aware that larger values improve GS batch utilization
+         * at the expense of more VPM memory pressure (which can affect
+         * other performance aspects, such as GS dispatch width).
+         * We start with the largest value, and will reduce it if we
+         * find that total memory pressure is too high.
+         */
+        vpm_cfg[phase].Gv = 3;
+        do {
+                /* When GS is present in absence of TES, then we need to satisfy
+                 * that Ve >= Gv. We go with the smallest value of Ve to avoid
+                 * increasing memory pressure.
+                 */
+                vpm_cfg[phase].Ve = vpm_cfg[phase].Gv;
+
+                uint32_t vpm_sectors =
+                        A * vpm_cfg[phase].As * Ad +
+                        (vpm_cfg[phase].Vc + vpm_cfg[phase].Ve) * Vd +
+                        vpm_cfg[phase].Gs * vpm_cfg[phase].Gd;
+
+                /* Ideally we want to use no more than half of the available
+                 * memory so we can execute a bin and render program in parallel
+                 * without stalls. If we achieved that then we are done.
+                 */
+                if (vpm_sectors <= vpm_size / 2) {
+                        final_vpm_cfg = &vpm_cfg[phase];
+                        break;
+                }
+
+                /* At the very least, we should not allocate more than the
+                 * total available VPM memory. If we have a configuration that
+                 * succeeds at this we save it and continue to see if we can
+                 * meet the half-memory-use criteria too.
+                 */
+                if (phase == 0 && vpm_sectors <= vpm_size) {
+                        vpm_cfg[1] = vpm_cfg[0];
+                        phase = 1;
+                }
+
+                /* Try lowering Gv */
+                if (vpm_cfg[phase].Gv > 0) {
+                        vpm_cfg[phase].Gv--;
+                        continue;
+                }
+
+                /* Try lowering GS dispatch width */
+                if (vpm_cfg[phase].gs_width > 1) {
+                        do {
+                                vpm_cfg[phase].gs_width >>= 1;
+                                vpm_cfg[phase].Gd =
+                                        align(vpm_cfg[phase].Gd, 2) / 2;
+                        } while (vpm_cfg[phase].gs_width == 2);
+
+                        /* Reset Gv to max after dropping dispatch width */
+                        vpm_cfg[phase].Gv = 3;
+                        continue;
+                }
+
+                /* We ran out of options to reduce memory pressure. If we
+                 * are at phase 1 we have at least a valid configuration, so we
+                 * we use that.
+                 */
+                if (phase == 1)
+                       final_vpm_cfg = &vpm_cfg[0];
+                break;
+        } while (true);
+
+        if (!final_vpm_cfg) {
+                /* FIXME: maybe return a boolean to indicate failure and use
+                 * that to stop the submission for this draw call.
+                 */
+                fprintf(stderr, "Failed to allocate VPM memory.\n");
+                abort();
+        }
+
+        assert(final_vpm_cfg);
+        assert(final_vpm_cfg->Gd <= 16);
+        assert(final_vpm_cfg->Gv < 4);
+        assert(final_vpm_cfg->Ve < 4);
+        assert(final_vpm_cfg->Vc >= 2 && final_vpm_cfg->Vc <= 4);
+        assert(final_vpm_cfg->gs_width == 1 ||
+               final_vpm_cfg->gs_width == 4 ||
+               final_vpm_cfg->gs_width == 8 ||
+               final_vpm_cfg->gs_width == 16);
+
+        *vpm_cfg_out = *final_vpm_cfg;
+}
+#endif
+
 static void
 v3d_emit_gl_shader_state(struct v3d_context *v3d,
                          const struct pipe_draw_info *info)
@@ -340,20 +634,57 @@ v3d_emit_gl_shader_state(struct v3d_context *v3d,
 
         /* Upload the uniforms to the indirect CL first */
         struct v3d_cl_reloc fs_uniforms =
-                v3d_write_uniforms(v3d, v3d->prog.fs,
+                v3d_write_uniforms(v3d, job, v3d->prog.fs,
                                    PIPE_SHADER_FRAGMENT);
+
+        struct v3d_cl_reloc gs_uniforms = { NULL, 0 };
+        struct v3d_cl_reloc gs_bin_uniforms = { NULL, 0 };
+        if (v3d->prog.gs) {
+                gs_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs,
+                                                 PIPE_SHADER_GEOMETRY);
+        }
+        if (v3d->prog.gs_bin) {
+                gs_bin_uniforms = v3d_write_uniforms(v3d, job, v3d->prog.gs_bin,
+                                                     PIPE_SHADER_GEOMETRY);
+        }
+
         struct v3d_cl_reloc vs_uniforms =
-                v3d_write_uniforms(v3d, v3d->prog.vs,
+                v3d_write_uniforms(v3d, job, v3d->prog.vs,
                                    PIPE_SHADER_VERTEX);
         struct v3d_cl_reloc cs_uniforms =
-                v3d_write_uniforms(v3d, v3d->prog.cs,
+                v3d_write_uniforms(v3d, job, v3d->prog.cs,
                                    PIPE_SHADER_VERTEX);
 
+        /* Update the cache dirty flag based on the shader progs data */
+        job->tmu_dirty_rcl |= v3d->prog.cs->prog_data.vs->base.tmu_dirty_rcl;
+        job->tmu_dirty_rcl |= v3d->prog.vs->prog_data.vs->base.tmu_dirty_rcl;
+        if (v3d->prog.gs_bin) {
+                job->tmu_dirty_rcl |=
+                        v3d->prog.gs_bin->prog_data.gs->base.tmu_dirty_rcl;
+        }
+        if (v3d->prog.gs) {
+                job->tmu_dirty_rcl |=
+                        v3d->prog.gs->prog_data.gs->base.tmu_dirty_rcl;
+        }
+        job->tmu_dirty_rcl |= v3d->prog.fs->prog_data.fs->base.tmu_dirty_rcl;
+
         /* See GFXH-930 workaround below */
         uint32_t num_elements_to_emit = MAX2(vtx->num_elements, 1);
+
+        uint32_t shader_state_record_length =
+                cl_packet_length(GL_SHADER_STATE_RECORD);
+#if V3D_VERSION >= 41
+        if (v3d->prog.gs) {
+                shader_state_record_length +=
+                        cl_packet_length(GEOMETRY_SHADER_STATE_RECORD) +
+                        cl_packet_length(TESSELLATION_GEOMETRY_COMMON_PARAMS) +
+                        2 * cl_packet_length(TESSELLATION_GEOMETRY_SHADER_PARAMS);
+        }
+#endif
+
         uint32_t shader_rec_offset =
-                v3d_cl_ensure_space(&job->indirect,
-                                    cl_packet_length(GL_SHADER_STATE_RECORD) +
+                    v3d_cl_ensure_space(&job->indirect,
+                                    shader_state_record_length +
                                     num_elements_to_emit *
                                     cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD),
                                     32);
@@ -362,6 +693,56 @@ v3d_emit_gl_shader_state(struct v3d_context *v3d,
          * compile time, so that we mostly just have to OR the VS and FS
          * records together at draw time.
          */
+
+        struct vpm_config vpm_cfg_bin, vpm_cfg;
+
+        assert(v3d->screen->devinfo.ver >= 41 || !v3d->prog.gs);
+        if (!v3d->prog.gs) {
+                vpm_cfg_bin.As = 1;
+                vpm_cfg_bin.Ve = 0;
+                vpm_cfg_bin.Vc = v3d->prog.cs->prog_data.vs->vcm_cache_size;
+
+                vpm_cfg.As = 1;
+                vpm_cfg.Ve = 0;
+                vpm_cfg.Vc = v3d->prog.vs->prog_data.vs->vcm_cache_size;
+        }
+        else {
+#if V3D_VERSION >= 41
+                v3d_emit_gs_state_record(v3d->job,
+                                         v3d->prog.gs_bin, gs_bin_uniforms,
+                                         v3d->prog.gs, gs_uniforms);
+
+                struct v3d_gs_prog_data *gs = v3d->prog.gs->prog_data.gs;
+                struct v3d_gs_prog_data *gs_bin = v3d->prog.gs_bin->prog_data.gs;
+
+                v3d_emit_tes_gs_common_params(v3d->job,
+                                              gs->out_prim_type,
+                                              gs->num_invocations);
+
+                /* Bin Tes/Gs params */
+                struct v3d_vs_prog_data *vs_bin = v3d->prog.cs->prog_data.vs;
+                compute_vpm_config_gs(&v3d->screen->devinfo,
+                                      vs_bin, gs_bin, &vpm_cfg_bin);
+
+                v3d_emit_tes_gs_shader_params(v3d->job,
+                                              vpm_cfg_bin.gs_width,
+                                              vpm_cfg_bin.Gd,
+                                              vpm_cfg_bin.Gv);
+
+                /* Render Tes/Gs params */
+                struct v3d_vs_prog_data *vs = v3d->prog.vs->prog_data.vs;
+                compute_vpm_config_gs(&v3d->screen->devinfo,
+                                      vs, gs, &vpm_cfg);
+
+                v3d_emit_tes_gs_shader_params(v3d->job,
+                                              vpm_cfg.gs_width,
+                                              vpm_cfg.Gd,
+                                              vpm_cfg.Gv);
+#else
+                unreachable("No GS support pre-4.1");
+#endif
+        }
+
         cl_emit(&job->indirect, GL_SHADER_STATE_RECORD, shader) {
                 shader.enable_clipping = true;
                 /* VC5_DIRTY_PRIM_MODE | VC5_DIRTY_RASTERIZER */
@@ -385,6 +766,12 @@ v3d_emit_gl_shader_state(struct v3d_context *v3d,
                 shader.fragment_shader_uses_real_pixel_centre_w_in_addition_to_centroid_w2 =
                         v3d->prog.fs->prog_data.fs->uses_center_w;
 
+#if V3D_VERSION >= 41
+                shader.any_shader_reads_hardware_written_primitive_id =
+                        v3d->prog.gs ? v3d->prog.gs->prog_data.gs->uses_pid :
+                                       false;
+#endif
+
 #if V3D_VERSION >= 40
                shader.do_scoreboard_wait_on_first_thread_switch =
                         v3d->prog.fs->prog_data.fs->lock_scoreboard_on_first_thrsw;
@@ -434,8 +821,15 @@ v3d_emit_gl_shader_state(struct v3d_context *v3d,
                 shader.fragment_shader_uniforms_address = fs_uniforms;
 
 #if V3D_VERSION >= 41
-                shader.min_coord_shader_input_segments_required_in_play = 1;
-                shader.min_vertex_shader_input_segments_required_in_play = 1;
+                shader.min_coord_shader_input_segments_required_in_play =
+                        vpm_cfg_bin.As;
+                shader.min_vertex_shader_input_segments_required_in_play =
+                        vpm_cfg.As;
+
+                shader.min_coord_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
+                        vpm_cfg_bin.Ve;
+                shader.min_vertex_shader_output_segments_required_in_play_in_addition_to_vcm_cache_size =
+                        vpm_cfg.Ve;
 
                 shader.coordinate_shader_4_way_threadable =
                         v3d->prog.cs->prog_data.vs->base.threads == 4;
@@ -539,34 +933,54 @@ v3d_emit_gl_shader_state(struct v3d_context *v3d,
         }
 
         cl_emit(&job->bcl, VCM_CACHE_SIZE, vcm) {
-                vcm.number_of_16_vertex_batches_for_binning =
-                        v3d->prog.cs->prog_data.vs->vcm_cache_size;
-                vcm.number_of_16_vertex_batches_for_rendering =
-                        v3d->prog.vs->prog_data.vs->vcm_cache_size;
+                vcm.number_of_16_vertex_batches_for_binning = vpm_cfg_bin.Vc;
+                vcm.number_of_16_vertex_batches_for_rendering = vpm_cfg.Vc;
         }
 
+#if V3D_VERSION >= 41
+        if (v3d->prog.gs) {
+                cl_emit(&job->bcl, GL_SHADER_STATE_INCLUDING_GS, state) {
+                        state.address = cl_address(job->indirect.bo,
+                                                   shader_rec_offset);
+                        state.number_of_attribute_arrays = num_elements_to_emit;
+                }
+        } else {
+                cl_emit(&job->bcl, GL_SHADER_STATE, state) {
+                        state.address = cl_address(job->indirect.bo,
+                                                   shader_rec_offset);
+                        state.number_of_attribute_arrays = num_elements_to_emit;
+                }
+        }
+#else
+        assert(!v3d->prog.gs);
         cl_emit(&job->bcl, GL_SHADER_STATE, state) {
                 state.address = cl_address(job->indirect.bo, shader_rec_offset);
                 state.number_of_attribute_arrays = num_elements_to_emit;
         }
+#endif
 
         v3d_bo_unreference(&cs_uniforms.bo);
         v3d_bo_unreference(&vs_uniforms.bo);
+        if (gs_uniforms.bo)
+                v3d_bo_unreference(&gs_uniforms.bo);
+        if (gs_bin_uniforms.bo)
+                v3d_bo_unreference(&gs_bin_uniforms.bo);
         v3d_bo_unreference(&fs_uniforms.bo);
-
-        job->shader_rec_count++;
 }
 
 /**
- * Updates the number of primitvies generated from the number of vertices
- * to draw. We do this here instead of using PRIMITIVE_COUNTS_FEEDBACK because
- * using the GPU packet for this might require sync waits and this is trivial
- * to handle in the CPU instead.
+ * Updates the number of primitives generated from the number of vertices
+ * to draw. This only works when no GS is present, since otherwise the number
+ * of primitives generated cannot be determined in advance and we need to
+ * use the PRIMITIVE_COUNTS_FEEDBACK command instead, however, that requires
+ * a sync wait for the draw to complete, so we only use that when GS is present.
  */
 static void
 v3d_update_primitives_generated_counter(struct v3d_context *v3d,
                                         const struct pipe_draw_info *info)
 {
+        assert(!v3d->prog.gs);
+
         if (!v3d->active_queries)
                 return;
 
@@ -618,6 +1032,57 @@ v3d_update_job_ez(struct v3d_context *v3d, struct v3d_job *job)
                 job->first_ez_state = job->ez_state;
 }
 
+static uint32_t
+v3d_hw_prim_type(enum pipe_prim_type prim_type)
+{
+        switch (prim_type) {
+        case PIPE_PRIM_POINTS:
+        case PIPE_PRIM_LINES:
+        case PIPE_PRIM_LINE_LOOP:
+        case PIPE_PRIM_LINE_STRIP:
+        case PIPE_PRIM_TRIANGLES:
+        case PIPE_PRIM_TRIANGLE_STRIP:
+        case PIPE_PRIM_TRIANGLE_FAN:
+                return prim_type;
+
+        case PIPE_PRIM_LINES_ADJACENCY:
+        case PIPE_PRIM_LINE_STRIP_ADJACENCY:
+        case PIPE_PRIM_TRIANGLES_ADJACENCY:
+        case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
+                return 8 + (prim_type - PIPE_PRIM_LINES_ADJACENCY);
+
+        default:
+                unreachable("Unsupported primitive type");
+        }
+}
+
+static bool
+v3d_check_compiled_shaders(struct v3d_context *v3d)
+{
+        static bool warned[5] = { 0 };
+
+        uint32_t failed_stage = MESA_SHADER_NONE;
+        if (!v3d->prog.vs->resource || !v3d->prog.cs->resource) {
+                failed_stage = MESA_SHADER_VERTEX;
+        } else if ((v3d->prog.gs_bin && !v3d->prog.gs_bin->resource) ||
+                   (v3d->prog.gs && !v3d->prog.gs->resource)) {
+                failed_stage = MESA_SHADER_GEOMETRY;
+        } else if (v3d->prog.fs && !v3d->prog.fs->resource) {
+                failed_stage = MESA_SHADER_FRAGMENT;
+        }
+
+        if (likely(failed_stage == MESA_SHADER_NONE))
+                return true;
+
+        if (!warned[failed_stage]) {
+                fprintf(stderr,
+                        "%s shader failed to compile. Expect corruption.\n",
+                        _mesa_shader_stage_to_string(failed_stage));
+                warned[failed_stage] = true;
+        }
+        return false;
+}
+
 static void
 v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
 {
@@ -648,7 +1113,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                 }
         }
 
-        if (info->mode >= PIPE_PRIM_QUADS) {
+        if (info->mode >= PIPE_PRIM_QUADS && info->mode <= PIPE_PRIM_POLYGON) {
                 util_primconvert_save_rasterizer_state(v3d->primconvert, &v3d->rasterizer->base);
                 util_primconvert_draw_vbo(v3d->primconvert, info);
                 perf_debug("Fallback conversion for %d %s vertices\n",
@@ -664,7 +1129,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
 
         if (info->indirect) {
                 v3d_flush_jobs_writing_resource(v3d, info->indirect->buffer,
-                                                V3D_FLUSH_DEFAULT);
+                                                V3D_FLUSH_DEFAULT, false);
         }
 
         v3d_predraw_check_outputs(pctx);
@@ -677,7 +1142,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
          */
         if (v3d->streamout.num_targets > 0 &&
             u_base_prim_type(info->mode) != u_base_prim_type(v3d->prim_mode)) {
-                v3d_tf_update_counters(v3d);
+                v3d_update_primitive_counters(v3d);
         }
 
         struct v3d_job *job = v3d_get_job_for_fbo(v3d);
@@ -696,8 +1161,16 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                 job->submit.in_sync_bcl = v3d->out_sync;
         }
 
-        /* Mark SSBOs as being written.  We don't actually know which ones are
-         * read vs written, so just assume the worst
+        /* We also need to ensure that compute is complete when render depends
+         * on resources written by it.
+         */
+        if (v3d->sync_on_last_compute_job) {
+                job->submit.in_sync_bcl = v3d->out_sync;
+                v3d->sync_on_last_compute_job = false;
+        }
+
+        /* Mark SSBOs and images as being written.  We don't actually know
+         * which ones are read vs written, so just assume the worst.
          */
         for (int s = 0; s < PIPE_SHADER_COMPUTE; s++) {
                 foreach_bit(i, v3d->ssbo[s].enabled_mask) {
@@ -725,6 +1198,8 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
 
         v3d_start_draw(v3d);
         v3d_update_compiled_shaders(v3d, info->mode);
+        if (!v3d_check_compiled_shaders(v3d))
+                return;
         v3d_update_job_ez(v3d, job);
 
         /* If this job was writing to transform feedback buffers before this
@@ -749,9 +1224,15 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                           VC5_DIRTY_RASTERIZER |
                           VC5_DIRTY_COMPILED_CS |
                           VC5_DIRTY_COMPILED_VS |
+                          VC5_DIRTY_COMPILED_GS_BIN |
+                          VC5_DIRTY_COMPILED_GS |
                           VC5_DIRTY_COMPILED_FS |
                           v3d->prog.cs->uniform_dirty_bits |
                           v3d->prog.vs->uniform_dirty_bits |
+                          (v3d->prog.gs_bin ?
+                                    v3d->prog.gs_bin->uniform_dirty_bits : 0) |
+                          (v3d->prog.gs ?
+                                    v3d->prog.gs->uniform_dirty_bits : 0) |
                           v3d->prog.fs->uniform_dirty_bits)) {
                 v3d_emit_gl_shader_state(v3d, info);
         }
@@ -777,11 +1258,10 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                 prim_tf_enable = (V3D_PRIM_POINTS_TF - V3D_PRIM_POINTS);
 #endif
 
-        v3d_update_primitives_generated_counter(v3d, info);
+        if (!v3d->prog.gs)
+                v3d_update_primitives_generated_counter(v3d, info);
 
-        /* Note that the primitive type fields match with OpenGL/gallium
-         * definitions, up to but not including QUADS.
-         */
+        uint32_t hw_prim_type = v3d_hw_prim_type(info->mode);
         if (info->index_size) {
                 uint32_t index_size = info->index_size;
                 uint32_t offset = info->start * index_size;
@@ -811,7 +1291,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                                 prim.address_of_indices_list =
                                         cl_address(rsc->bo, offset);
 #endif /* V3D_VERSION < 40 */
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.enable_primitive_restarts = info->primitive_restart;
 
                                 prim.number_of_draw_indirect_indexed_records = info->indirect->draw_count;
@@ -830,7 +1310,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                                 prim.address_of_indices_list =
                                         cl_address(rsc->bo, offset);
 #endif /* V3D_VERSION < 40 */
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.enable_primitive_restarts = info->primitive_restart;
 
                                 prim.number_of_instances = info->instance_count;
@@ -847,7 +1327,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                                 prim.address_of_indices_list =
                                         cl_address(rsc->bo, offset);
 #endif /* V3D_VERSION < 40 */
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.enable_primitive_restarts = info->primitive_restart;
                         }
                 }
@@ -857,7 +1337,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
         } else {
                 if (info->indirect) {
                         cl_emit(&job->bcl, INDIRECT_VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.number_of_draw_indirect_array_records = info->indirect->draw_count;
 
                                 prim.stride_in_multiples_of_4_bytes = info->indirect->stride >> 2;
@@ -871,7 +1351,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                                 v3d_stream_output_target_get_vertex_count(so) :
                                 info->count;
                         cl_emit(&job->bcl, VERTEX_ARRAY_INSTANCED_PRIMS, prim) {
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.index_of_first_vertex = info->start;
                                 prim.number_of_instances = info->instance_count;
                                 prim.instance_length = vert_count;
@@ -883,7 +1363,7 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                                 v3d_stream_output_target_get_vertex_count(so) :
                                 info->count;
                         cl_emit(&job->bcl, VERTEX_ARRAY_PRIMS, prim) {
-                                prim.mode = info->mode | prim_tf_enable;
+                                prim.mode = hw_prim_type | prim_tf_enable;
                                 prim.length = vert_count;
                                 prim.index_of_first_vertex = info->start;
                         }
@@ -955,6 +1435,178 @@ v3d_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
                 v3d_flush(pctx);
 }
 
+#if V3D_VERSION >= 41
+#define V3D_CSD_CFG012_WG_COUNT_SHIFT 16
+#define V3D_CSD_CFG012_WG_OFFSET_SHIFT 0
+/* Allow this dispatch to start while the last one is still running. */
+#define V3D_CSD_CFG3_OVERLAP_WITH_PREV (1 << 26)
+/* Maximum supergroup ID.  6 bits. */
+#define V3D_CSD_CFG3_MAX_SG_ID_SHIFT 20
+/* Batches per supergroup minus 1.  8 bits. */
+#define V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT 12
+/* Workgroups per supergroup, 0 means 16 */
+#define V3D_CSD_CFG3_WGS_PER_SG_SHIFT 8
+#define V3D_CSD_CFG3_WG_SIZE_SHIFT 0
+
+#define V3D_CSD_CFG5_PROPAGATE_NANS (1 << 2)
+#define V3D_CSD_CFG5_SINGLE_SEG (1 << 1)
+#define V3D_CSD_CFG5_THREADING (1 << 0)
+
+static void
+v3d_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
+{
+        struct v3d_context *v3d = v3d_context(pctx);
+        struct v3d_screen *screen = v3d->screen;
+
+        v3d_predraw_check_stage_inputs(pctx, PIPE_SHADER_COMPUTE);
+
+        v3d_update_compiled_cs(v3d);
+
+        if (!v3d->prog.compute->resource) {
+                static bool warned = false;
+                if (!warned) {
+                        fprintf(stderr,
+                                "Compute shader failed to compile.  "
+                                "Expect corruption.\n");
+                        warned = true;
+                }
+                return;
+        }
+
+        /* Some of the units of scale:
+         *
+         * - Batches of 16 work items (shader invocations) that will be queued
+         *   to the run on a QPU at once.
+         *
+         * - Workgroups composed of work items based on the shader's layout
+         *   declaration.
+         *
+         * - Supergroups of 1-16 workgroups.  There can only be 16 supergroups
+         *   running at a time on the core, so we want to keep them large to
+         *   keep the QPUs busy, but a whole supergroup will sync at a barrier
+         *   so we want to keep them small if one is present.
+         */
+        struct drm_v3d_submit_csd submit = { 0 };
+        struct v3d_job *job = v3d_job_create(v3d);
+
+        /* Set up the actual number of workgroups, synchronously mapping the
+         * indirect buffer if necessary to get the dimensions.
+         */
+        if (info->indirect) {
+                struct pipe_transfer *transfer;
+                uint32_t *map = pipe_buffer_map_range(pctx, info->indirect,
+                                                      info->indirect_offset,
+                                                      3 * sizeof(uint32_t),
+                                                      PIPE_TRANSFER_READ,
+                                                      &transfer);
+                memcpy(v3d->compute_num_workgroups, map, 3 * sizeof(uint32_t));
+                pipe_buffer_unmap(pctx, transfer);
+
+                if (v3d->compute_num_workgroups[0] == 0 ||
+                    v3d->compute_num_workgroups[1] == 0 ||
+                    v3d->compute_num_workgroups[2] == 0) {
+                        /* Nothing to dispatch, so skip the draw (CSD can't
+                         * handle 0 workgroups).
+                         */
+                        return;
+                }
+        } else {
+                v3d->compute_num_workgroups[0] = info->grid[0];
+                v3d->compute_num_workgroups[1] = info->grid[1];
+                v3d->compute_num_workgroups[2] = info->grid[2];
+        }
+
+        for (int i = 0; i < 3; i++) {
+                submit.cfg[i] |= (v3d->compute_num_workgroups[i] <<
+                                  V3D_CSD_CFG012_WG_COUNT_SHIFT);
+        }
+
+        perf_debug("CSD only using single WG per SG currently, "
+                   "should increase that when possible.");
+        int wgs_per_sg = 1;
+        int wg_size = info->block[0] * info->block[1] * info->block[2];
+        submit.cfg[3] |= wgs_per_sg << V3D_CSD_CFG3_WGS_PER_SG_SHIFT;
+        submit.cfg[3] |= ((DIV_ROUND_UP(wgs_per_sg * wg_size, 16) - 1) <<
+                          V3D_CSD_CFG3_BATCHES_PER_SG_M1_SHIFT);
+        submit.cfg[3] |= (wg_size & 0xff) << V3D_CSD_CFG3_WG_SIZE_SHIFT;
+
+        int batches_per_wg = DIV_ROUND_UP(wg_size, 16);
+        /* Number of batches the dispatch will invoke (minus 1). */
+        submit.cfg[4] = batches_per_wg * (v3d->compute_num_workgroups[0] *
+                                          v3d->compute_num_workgroups[1] *
+                                          v3d->compute_num_workgroups[2]) - 1;
+
+        /* Make sure we didn't accidentally underflow. */
+        assert(submit.cfg[4] != ~0);
+
+        v3d_job_add_bo(job, v3d_resource(v3d->prog.compute->resource)->bo);
+        submit.cfg[5] = (v3d_resource(v3d->prog.compute->resource)->bo->offset +
+                         v3d->prog.compute->offset);
+        submit.cfg[5] |= V3D_CSD_CFG5_PROPAGATE_NANS;
+        if (v3d->prog.compute->prog_data.base->single_seg)
+                submit.cfg[5] |= V3D_CSD_CFG5_SINGLE_SEG;
+        if (v3d->prog.compute->prog_data.base->threads == 4)
+                submit.cfg[5] |= V3D_CSD_CFG5_THREADING;
+
+        if (v3d->prog.compute->prog_data.compute->shared_size) {
+                v3d->compute_shared_memory =
+                        v3d_bo_alloc(v3d->screen,
+                                     v3d->prog.compute->prog_data.compute->shared_size *
+                                     wgs_per_sg,
+                                     "shared_vars");
+        }
+
+        struct v3d_cl_reloc uniforms = v3d_write_uniforms(v3d, job,
+                                                          v3d->prog.compute,
+                                                          PIPE_SHADER_COMPUTE);
+        v3d_job_add_bo(job, uniforms.bo);
+        submit.cfg[6] = uniforms.bo->offset + uniforms.offset;
+
+        /* Pull some job state that was stored in a SUBMIT_CL struct out to
+         * our SUBMIT_CSD struct
+         */
+        submit.bo_handles = job->submit.bo_handles;
+        submit.bo_handle_count = job->submit.bo_handle_count;
+
+        /* Serialize this in the rest of our command stream. */
+        submit.in_sync = v3d->out_sync;
+        submit.out_sync = v3d->out_sync;
+
+        if (!(V3D_DEBUG & V3D_DEBUG_NORAST)) {
+                int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_SUBMIT_CSD,
+                                    &submit);
+                static bool warned = false;
+                if (ret && !warned) {
+                        fprintf(stderr, "CSD submit call returned %s.  "
+                                "Expect corruption.\n", strerror(errno));
+                        warned = true;
+                }
+        }
+
+        v3d_job_free(v3d, job);
+
+        /* Mark SSBOs as being written.. we don't actually know which ones are
+         * read vs written, so just assume the worst
+         */
+        foreach_bit(i, v3d->ssbo[PIPE_SHADER_COMPUTE].enabled_mask) {
+                struct v3d_resource *rsc = v3d_resource(
+                        v3d->ssbo[PIPE_SHADER_COMPUTE].sb[i].buffer);
+                rsc->writes++;
+                rsc->compute_written = true;
+        }
+
+        foreach_bit(i, v3d->shaderimg[PIPE_SHADER_COMPUTE].enabled_mask) {
+                struct v3d_resource *rsc = v3d_resource(
+                        v3d->shaderimg[PIPE_SHADER_COMPUTE].si[i].base.resource);
+                rsc->writes++;
+                rsc->compute_written = true;
+        }
+
+        v3d_bo_unreference(&uniforms.bo);
+        v3d_bo_unreference(&v3d->compute_shared_memory);
+}
+#endif
+
 /**
  * Implements gallium's clear() hook (glClear()) by drawing a pair of triangles.
  */
@@ -1094,7 +1746,7 @@ v3d_tlb_clear(struct v3d_job *job, unsigned buffers,
 }
 
 static void
-v3d_clear(struct pipe_context *pctx, unsigned buffers,
+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)
 {
         struct v3d_context *v3d = v3d_context(pctx);
@@ -1131,4 +1783,8 @@ v3dX(draw_init)(struct pipe_context *pctx)
         pctx->clear = v3d_clear;
         pctx->clear_render_target = v3d_clear_render_target;
         pctx->clear_depth_stencil = v3d_clear_depth_stencil;
+#if V3D_VERSION >= 41
+        if (v3d_context(pctx)->screen->has_csd)
+                pctx->launch_grid = v3d_launch_grid;
+#endif
 }