v3d: fix TF primitive counts for resume without draw
[mesa.git] / src / gallium / drivers / v3d / v3d_context.h
index 7c920dbc3db380683d37bc3064c82efda172b65d..9022ac351d4aa19cff90103dd8837d4016636cd7 100644 (file)
@@ -36,8 +36,9 @@
 #include "util/bitset.h"
 #include "util/slab.h"
 #include "xf86drm.h"
-#include "v3d_drm.h"
+#include "drm-uapi/v3d_drm.h"
 #include "v3d_screen.h"
+#include "broadcom/common/v3d_limits.h"
 
 struct v3d_job;
 struct v3d_bo;
@@ -53,37 +54,90 @@ void v3d_job_add_bo(struct v3d_job *job, struct v3d_bo *bo);
 #define using_v3d_simulator false
 #endif
 
-#define VC5_DIRTY_BLEND         (1 <<  0)
-#define VC5_DIRTY_RASTERIZER    (1 <<  1)
-#define VC5_DIRTY_ZSA           (1 <<  2)
-#define VC5_DIRTY_FRAGTEX       (1 <<  3)
-#define VC5_DIRTY_VERTTEX       (1 <<  4)
-
-#define VC5_DIRTY_BLEND_COLOR   (1 <<  7)
-#define VC5_DIRTY_STENCIL_REF   (1 <<  8)
-#define VC5_DIRTY_SAMPLE_STATE  (1 <<  9)
-#define VC5_DIRTY_FRAMEBUFFER   (1 << 10)
-#define VC5_DIRTY_STIPPLE       (1 << 11)
-#define VC5_DIRTY_VIEWPORT      (1 << 12)
-#define VC5_DIRTY_CONSTBUF      (1 << 13)
-#define VC5_DIRTY_VTXSTATE      (1 << 14)
-#define VC5_DIRTY_VTXBUF        (1 << 15)
-#define VC5_DIRTY_SCISSOR       (1 << 17)
-#define VC5_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
-#define VC5_DIRTY_PRIM_MODE     (1 << 19)
-#define VC5_DIRTY_CLIP          (1 << 20)
-#define VC5_DIRTY_UNCOMPILED_VS (1 << 21)
-#define VC5_DIRTY_UNCOMPILED_FS (1 << 22)
-#define VC5_DIRTY_COMPILED_CS   (1 << 23)
-#define VC5_DIRTY_COMPILED_VS   (1 << 24)
-#define VC5_DIRTY_COMPILED_FS   (1 << 25)
-#define VC5_DIRTY_FS_INPUTS     (1 << 26)
-#define VC5_DIRTY_STREAMOUT     (1 << 27)
-#define VC5_DIRTY_OQ            (1 << 28)
-#define VC5_DIRTY_CENTROID_FLAGS (1 << 29)
+#define VC5_DIRTY_BLEND               (1ull <<  0)
+#define VC5_DIRTY_RASTERIZER          (1ull <<  1)
+#define VC5_DIRTY_ZSA                 (1ull <<  2)
+#define VC5_DIRTY_COMPTEX             (1ull <<  3)
+#define VC5_DIRTY_VERTTEX             (1ull <<  4)
+#define VC5_DIRTY_FRAGTEX             (1ull <<  5)
+
+#define VC5_DIRTY_SHADER_IMAGE        (1ull <<  9)
+#define VC5_DIRTY_BLEND_COLOR         (1ull << 10)
+#define VC5_DIRTY_STENCIL_REF         (1ull << 11)
+#define VC5_DIRTY_SAMPLE_STATE        (1ull << 12)
+#define VC5_DIRTY_FRAMEBUFFER         (1ull << 13)
+#define VC5_DIRTY_STIPPLE             (1ull << 14)
+#define VC5_DIRTY_VIEWPORT            (1ull << 15)
+#define VC5_DIRTY_CONSTBUF            (1ull << 16)
+#define VC5_DIRTY_VTXSTATE            (1ull << 17)
+#define VC5_DIRTY_VTXBUF              (1ull << 18)
+#define VC5_DIRTY_SCISSOR             (1ull << 19)
+#define VC5_DIRTY_FLAT_SHADE_FLAGS    (1ull << 20)
+#define VC5_DIRTY_PRIM_MODE           (1ull << 21)
+#define VC5_DIRTY_CLIP                (1ull << 22)
+#define VC5_DIRTY_UNCOMPILED_CS       (1ull << 23)
+#define VC5_DIRTY_UNCOMPILED_VS       (1ull << 24)
+#define VC5_DIRTY_UNCOMPILED_FS       (1ull << 25)
+
+#define VC5_DIRTY_COMPILED_CS         (1ull << 29)
+#define VC5_DIRTY_COMPILED_VS         (1ull << 30)
+#define VC5_DIRTY_COMPILED_FS         (1ull << 31)
+
+#define VC5_DIRTY_FS_INPUTS           (1ull << 35)
+#define VC5_DIRTY_STREAMOUT           (1ull << 36)
+#define VC5_DIRTY_OQ                  (1ull << 37)
+#define VC5_DIRTY_CENTROID_FLAGS      (1ull << 38)
+#define VC5_DIRTY_NOPERSPECTIVE_FLAGS (1ull << 39)
+#define VC5_DIRTY_SSBO                (1ull << 40)
 
 #define VC5_MAX_FS_INPUTS 64
 
+enum v3d_sampler_state_variant {
+        V3D_SAMPLER_STATE_BORDER_0,
+        V3D_SAMPLER_STATE_F16,
+        V3D_SAMPLER_STATE_F16_UNORM,
+        V3D_SAMPLER_STATE_F16_SNORM,
+        V3D_SAMPLER_STATE_F16_BGRA,
+        V3D_SAMPLER_STATE_F16_BGRA_UNORM,
+        V3D_SAMPLER_STATE_F16_BGRA_SNORM,
+        V3D_SAMPLER_STATE_F16_A,
+        V3D_SAMPLER_STATE_F16_A_SNORM,
+        V3D_SAMPLER_STATE_F16_A_UNORM,
+        V3D_SAMPLER_STATE_F16_LA,
+        V3D_SAMPLER_STATE_F16_LA_UNORM,
+        V3D_SAMPLER_STATE_F16_LA_SNORM,
+        V3D_SAMPLER_STATE_32,
+        V3D_SAMPLER_STATE_32_UNORM,
+        V3D_SAMPLER_STATE_32_SNORM,
+        V3D_SAMPLER_STATE_32_A,
+        V3D_SAMPLER_STATE_32_A_UNORM,
+        V3D_SAMPLER_STATE_32_A_SNORM,
+        V3D_SAMPLER_STATE_1010102U,
+        V3D_SAMPLER_STATE_16U,
+        V3D_SAMPLER_STATE_16I,
+        V3D_SAMPLER_STATE_8I,
+        V3D_SAMPLER_STATE_8U,
+
+        V3D_SAMPLER_STATE_VARIANT_COUNT,
+};
+
+enum v3d_flush_cond {
+        /* Flush job unless we are flushing for transform feedback, where we
+         * handle flushing in the driver via the 'Wait for TF' packet.
+         */
+        V3D_FLUSH_DEFAULT,
+        /* Always flush the job, even for cases where we would normally not
+         * do it, such as transform feedback.
+         */
+        V3D_FLUSH_ALWAYS,
+        /* Flush job if it is not the current FBO job. This is intended to
+         * skip automatic flushes of the current job for resources that we
+         * expect to be externally synchronized by the application using
+         * glMemoryBarrier(), such as SSBOs and shader images.
+         */
+        V3D_FLUSH_NOT_CURRENT_JOB,
+};
+
 struct v3d_sampler_view {
         struct pipe_sampler_view base;
         uint32_t p0;
@@ -94,6 +148,14 @@ struct v3d_sampler_view {
         uint8_t texture_shader_state[32];
         /* V3D 4.x: Texture state struct. */
         struct v3d_bo *bo;
+
+        enum v3d_sampler_state_variant sampler_variant;
+
+        /* Actual texture to be read by this sampler view.  May be different
+         * from base.texture in the case of having a shadow tiled copy of a
+         * raster texture.
+         */
+        struct pipe_resource *texture;
 };
 
 struct v3d_sampler_state {
@@ -104,15 +166,18 @@ struct v3d_sampler_state {
         /* V3D 3.x: Packed texture state. */
         uint8_t texture_shader_state[32];
         /* V3D 4.x: Sampler state struct. */
-        struct v3d_bo *bo;
+        struct pipe_resource *sampler_state;
+        uint32_t sampler_state_offset[V3D_SAMPLER_STATE_VARIANT_COUNT];
+
+        bool border_color_variants;
 };
 
 struct v3d_texture_stateobj {
-        struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
+        struct pipe_sampler_view *textures[V3D_MAX_TEXTURE_SAMPLERS];
         unsigned num_textures;
-        struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
+        struct pipe_sampler_state *samplers[V3D_MAX_TEXTURE_SAMPLERS];
         unsigned num_samplers;
-        struct v3d_cl_reloc texture_state[PIPE_MAX_SAMPLERS];
+        struct v3d_cl_reloc texture_state[V3D_MAX_TEXTURE_SAMPLERS];
 };
 
 struct v3d_shader_uniform_info {
@@ -132,22 +197,17 @@ struct v3d_uncompiled_shader {
         uint16_t tf_specs[16];
         uint16_t tf_specs_psiz[16];
         uint32_t num_tf_specs;
-
-        /**
-         * Flag for if the NIR in this shader originally came from TGSI.  If
-         * so, we need to do some fixups at compile time, due to missing
-         * information in TGSI that exists in NIR.
-         */
-        bool was_tgsi;
 };
 
 struct v3d_compiled_shader {
-        struct v3d_bo *bo;
+        struct pipe_resource *resource;
+        uint32_t offset;
 
         union {
                 struct v3d_prog_data *base;
                 struct v3d_vs_prog_data *vs;
                 struct v3d_fs_prog_data *fs;
+                struct v3d_compute_prog_data *compute;
         } prog_data;
 
         /**
@@ -155,12 +215,14 @@ struct v3d_compiled_shader {
          * uniforms have to be rewritten (and therefore the shader state
          * reemitted).
          */
-        uint32_t uniform_dirty_bits;
+        uint64_t uniform_dirty_bits;
 };
 
 struct v3d_program_stateobj {
-        struct v3d_uncompiled_shader *bind_vs, *bind_fs;
-        struct v3d_compiled_shader *cs, *vs, *fs;
+        struct v3d_uncompiled_shader *bind_vs, *bind_fs, *bind_compute;
+        struct v3d_compiled_shader *cs, *vs, *fs, *compute;
+
+        struct hash_table *cache[MESA_SHADER_STAGES];
 
         struct v3d_bo *spill_bo;
         int spill_size_per_thread;
@@ -180,11 +242,18 @@ struct v3d_vertexbuf_stateobj {
 };
 
 struct v3d_vertex_stateobj {
-        struct pipe_vertex_element pipe[VC5_MAX_ATTRIBUTES];
+        struct pipe_vertex_element pipe[V3D_MAX_VS_INPUTS / 4];
         unsigned num_elements;
 
-        uint8_t attrs[16 * VC5_MAX_ATTRIBUTES];
-        struct v3d_bo *default_attribute_values;
+        uint8_t attrs[16 * (V3D_MAX_VS_INPUTS / 4)];
+        struct pipe_resource *defaults;
+        uint32_t defaults_offset;
+};
+
+struct v3d_stream_output_target {
+        struct pipe_stream_output_target base;
+        /* Number of transform feedback vertices written to this target */
+        uint32_t recorded_vertex_count;
 };
 
 struct v3d_streamout_stateobj {
@@ -194,6 +263,11 @@ struct v3d_streamout_stateobj {
         unsigned num_targets;
 };
 
+struct v3d_ssbo_stateobj {
+        struct pipe_shader_buffer sb[PIPE_MAX_SHADER_BUFFERS];
+        uint32_t enabled_mask;
+};
+
 /* Hash table key for v3d->jobs */
 struct v3d_job_key {
         struct pipe_surface *cbufs[4];
@@ -207,6 +281,18 @@ enum v3d_ez_state {
         VC5_EZ_DISABLED,
 };
 
+struct v3d_image_view {
+        struct pipe_image_view base;
+        /* V3D 4.x texture shader state struct */
+        struct pipe_resource *tex_state;
+        uint32_t tex_state_offset;
+};
+
+struct v3d_shaderimg_stateobj {
+        struct v3d_image_view si[PIPE_MAX_SHADER_IMAGES];
+        uint32_t enabled_mask;
+};
+
 /**
  * A complete bin/render job.
  *
@@ -238,6 +324,7 @@ struct v3d_job {
         uint32_t referenced_size;
 
         struct set *write_prscs;
+        struct set *tf_write_prscs;
 
         /* Size of the submit.bo_handles array. */
         uint32_t bo_handles_size;
@@ -279,11 +366,15 @@ struct v3d_job {
         /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
          * first rendering.
          */
-        uint32_t cleared;
+        uint32_t clear;
+        /* Bitmask of PIPE_CLEAR_* of buffers that have been read by a draw
+         * call without having been cleared first.
+         */
+        uint32_t load;
         /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
-         * (either clears or draws).
+         * (either clears or draws) and should be stored.
          */
-        uint32_t resolve;
+        uint32_t store;
         uint32_t clear_color[4][4];
         float clear_z;
         uint8_t clear_s;
@@ -295,16 +386,13 @@ struct v3d_job {
          */
         bool needs_flush;
 
-        /**
-         * Set if there is a nonzero address for OCCLUSION_QUERY_COUNTER.  If
-         * so, we need to disable it and flush before ending the CL, to keep
-         * the next tile from starting with it enabled.
+        /* Set if any shader has dirtied cachelines in the TMU that need to be
+         * flushed before job end.
          */
-        bool oq_enabled;
+        bool tmu_dirty_rcl;
 
         /**
-         * Set when a packet enabling TF on all further primitives has been
-         * emitted.
+         * Set if a packet enabling TF has been emitted in the job (V3D 4.x).
          */
         bool tf_enabled;
 
@@ -325,6 +413,12 @@ struct v3d_job {
          */
         uint32_t draw_calls_queued;
 
+        /**
+         * Number of draw calls (not counting full buffer clears) queued in
+         * the current job during active transform feedback.
+         */
+        uint32_t tf_draw_calls_queued;
+
         struct v3d_job_key key;
 };
 
@@ -353,11 +447,10 @@ struct v3d_context {
         struct blitter_context *blitter;
 
         /** bitfield of VC5_DIRTY_* */
-        uint32_t dirty;
+        uint64_t dirty;
 
         struct primconvert_context *primconvert;
 
-        struct hash_table *fs_cache, *vs_cache;
         uint32_t next_uncompiled_program_id;
         uint64_t next_compiled_program_id;
 
@@ -368,20 +461,29 @@ struct v3d_context {
         /** Maximum index buffer valid for the current shader_rec. */
         uint32_t max_index;
 
-        /** Sync object that our RCL will update as its out_sync. */
+        /** Sync object that our RCL or TFU job will update as its out_sync. */
         uint32_t out_sync;
 
+        /* Stream uploader used by gallium internals.  This could also be used
+         * by driver internals, but we tend to use the v3d_cl.h interfaces
+         * instead.
+         */
         struct u_upload_mgr *uploader;
+        /* State uploader used inside the driver.  This is for packing bits of
+         * long-term state inside buffers, since the kernel interfaces
+         * allocate a page at a time.
+         */
+        struct u_upload_mgr *state_uploader;
 
         /** @{ Current pipeline state objects */
         struct pipe_scissor_state scissor;
-        struct pipe_blend_state *blend;
+        struct v3d_blend_state *blend;
         struct v3d_rasterizer_state *rasterizer;
         struct v3d_depth_stencil_alpha_state *zsa;
 
-        struct v3d_texture_stateobj verttex, fragtex;
-
         struct v3d_program_stateobj prog;
+        uint32_t compute_num_workgroups[3];
+        struct v3d_bo *compute_shared_memory;
 
         struct v3d_vertex_stateobj *vtx;
 
@@ -416,10 +518,16 @@ struct v3d_context {
         struct pipe_poly_stipple stipple;
         struct pipe_clip_state clip;
         struct pipe_viewport_state viewport;
+        struct v3d_ssbo_stateobj ssbo[PIPE_SHADER_TYPES];
+        struct v3d_shaderimg_stateobj shaderimg[PIPE_SHADER_TYPES];
         struct v3d_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
+        struct v3d_texture_stateobj tex[PIPE_SHADER_TYPES];
         struct v3d_vertexbuf_stateobj vertexbuf;
         struct v3d_streamout_stateobj streamout;
         struct v3d_bo *current_oq;
+        struct pipe_resource *prim_counts;
+        uint32_t prim_counts_offset;
+        struct pipe_debug_callback debug;
         /** @} */
 };
 
@@ -428,21 +536,8 @@ struct v3d_rasterizer_state {
 
         float point_size;
 
-        /**
-         * Half-float (1/8/7 bits) value of polygon offset units for
-         * VC5_PACKET_DEPTH_OFFSET
-         */
-        uint16_t offset_units;
-        /**
-         * The HW treats polygon offset units based on a Z24 buffer, so we
-         * need to scale up offset_units if we're only Z16.
-         */
-        uint16_t z16_offset_units;
-        /**
-         * Half-float (1/8/7 bits) value of polygon offset scale for
-         * VC5_PACKET_DEPTH_OFFSET
-         */
-        uint16_t offset_factor;
+        uint8_t depth_offset[9];
+        uint8_t depth_offset_z16[9];
 };
 
 struct v3d_depth_stencil_alpha_state {
@@ -454,11 +549,23 @@ struct v3d_depth_stencil_alpha_state {
         uint8_t stencil_back[6];
 };
 
+struct v3d_blend_state {
+        struct pipe_blend_state base;
+
+        /* Per-RT mask of whether blending is enabled. */
+        uint8_t blend_enables;
+};
+
 #define perf_debug(...) do {                            \
         if (unlikely(V3D_DEBUG & V3D_DEBUG_PERF))       \
                 fprintf(stderr, __VA_ARGS__);           \
+        if (unlikely(v3d->debug.debug_message))         \
+                pipe_debug_message(&v3d->debug, PERF_INFO, __VA_ARGS__);    \
 } while (0)
 
+#define foreach_bit(b, mask)                                            \
+        for (uint32_t _m = (mask), b; _m && ({(b) = u_bit_scan(&_m); 1;});)
+
 static inline struct v3d_context *
 v3d_context(struct pipe_context *pcontext)
 {
@@ -477,6 +584,18 @@ v3d_sampler_state(struct pipe_sampler_state *psampler)
         return (struct v3d_sampler_state *)psampler;
 }
 
+static inline struct v3d_stream_output_target *
+v3d_stream_output_target(struct pipe_stream_output_target *ptarget)
+{
+        return (struct v3d_stream_output_target *)ptarget;
+}
+
+static inline uint32_t
+v3d_stream_output_target_get_vertex_count(struct pipe_stream_output_target *ptarget)
+{
+    return v3d_stream_output_target(ptarget)->recorded_vertex_count;
+}
+
 struct pipe_context *v3d_context_create(struct pipe_screen *pscreen,
                                         void *priv, unsigned flags);
 void v3d_program_init(struct pipe_context *pctx);
@@ -485,12 +604,9 @@ void v3d_query_init(struct pipe_context *pctx);
 
 void v3d_simulator_init(struct v3d_screen *screen);
 void v3d_simulator_destroy(struct v3d_screen *screen);
-int v3d_simulator_flush(struct v3d_context *v3d,
-                        struct drm_v3d_submit_cl *args,
-                        struct v3d_job *job);
+uint32_t v3d_simulator_get_spill(uint32_t spill_size);
 int v3d_simulator_ioctl(int fd, unsigned long request, void *arg);
-void v3d_simulator_open_from_handle(int fd, uint32_t winsys_stride,
-                                    int handle, uint32_t size);
+void v3d_simulator_open_from_handle(int fd, int handle, uint32_t size);
 
 static inline int
 v3d_ioctl(int fd, unsigned long request, void *arg)
@@ -501,11 +617,17 @@ v3d_ioctl(int fd, unsigned long request, void *arg)
                 return drmIoctl(fd, request, arg);
 }
 
+static inline bool
+v3d_transform_feedback_enabled(struct v3d_context *v3d)
+{
+        return v3d->prog.bind_vs->num_tf_specs != 0 &&
+               v3d->active_queries;
+}
+
 void v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader);
 struct v3d_cl_reloc v3d_write_uniforms(struct v3d_context *v3d,
                                        struct v3d_compiled_shader *shader,
-                                       struct v3d_constbuf_stateobj *cb,
-                                       struct v3d_texture_stateobj *texstate);
+                                       enum pipe_shader_type stage);
 
 void v3d_flush(struct pipe_context *pctx);
 void v3d_job_init(struct v3d_context *v3d);
@@ -515,12 +637,17 @@ struct v3d_job *v3d_get_job(struct v3d_context *v3d,
 struct v3d_job *v3d_get_job_for_fbo(struct v3d_context *v3d);
 void v3d_job_add_bo(struct v3d_job *job, struct v3d_bo *bo);
 void v3d_job_add_write_resource(struct v3d_job *job, struct pipe_resource *prsc);
+void v3d_job_add_tf_write_resource(struct v3d_job *job, struct pipe_resource *prsc);
 void v3d_job_submit(struct v3d_context *v3d, struct v3d_job *job);
+void v3d_flush_jobs_using_bo(struct v3d_context *v3d, struct v3d_bo *bo);
 void v3d_flush_jobs_writing_resource(struct v3d_context *v3d,
-                                     struct pipe_resource *prsc);
+                                     struct pipe_resource *prsc,
+                                     enum v3d_flush_cond flush_cond);
 void v3d_flush_jobs_reading_resource(struct v3d_context *v3d,
-                                     struct pipe_resource *prsc);
+                                     struct pipe_resource *prsc,
+                                     enum v3d_flush_cond flush_cond);
 void v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode);
+void v3d_update_compiled_cs(struct v3d_context *v3d);
 
 bool v3d_rt_format_supported(const struct v3d_device_info *devinfo,
                              enum pipe_format f);
@@ -539,13 +666,24 @@ void v3d_get_internal_type_bpp_for_output_format(const struct v3d_device_info *d
                                                  uint32_t format,
                                                  uint32_t *type,
                                                  uint32_t *bpp);
+bool v3d_tfu_supports_tex_format(const struct v3d_device_info *devinfo,
+                                 uint32_t tex_format);
 
 void v3d_init_query_functions(struct v3d_context *v3d);
 void v3d_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info);
 void v3d_blitter_save(struct v3d_context *v3d);
+bool v3d_generate_mipmap(struct pipe_context *pctx,
+                         struct pipe_resource *prsc,
+                         enum pipe_format format,
+                         unsigned int base_level,
+                         unsigned int last_level,
+                         unsigned int first_layer,
+                         unsigned int last_layer);
 
 struct v3d_fence *v3d_fence_create(struct v3d_context *v3d);
 
+void v3d_tf_update_counters(struct v3d_context *v3d);
+
 #ifdef v3dX
 #  include "v3dx_context.h"
 #else