xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/panfrost/pan_job.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2019 Alyssa Rosenzweig
3  * Copyright (C) 2014-2017 Broadcom
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  */
25 
26 #ifndef __PAN_JOB_H__
27 #define __PAN_JOB_H__
28 
29 #include "pipe/p_state.h"
30 #include "util/u_dynarray.h"
31 #include "pan_csf.h"
32 #include "pan_desc.h"
33 #include "pan_jm.h"
34 #include "pan_mempool.h"
35 #include "pan_resource.h"
36 
37 /* Simple tri-state data structure. In the default "don't care" state, the value
38  * may be set to true or false. However, once the value is set, it must not be
39  * changed. Declared inside of a struct to prevent casting to bool, which is an
40  * error. The getter needs to be used instead.
41  */
42 struct pan_tristate {
43    enum {
44       PAN_TRISTATE_DONTCARE,
45       PAN_TRISTATE_FALSE,
46       PAN_TRISTATE_TRUE,
47    } v;
48 };
49 
50 /*
51  * Try to set a tristate value to a desired boolean value. Returns whether the
52  * operation is successful.
53  */
54 static inline bool
pan_tristate_set(struct pan_tristate * state,bool value)55 pan_tristate_set(struct pan_tristate *state, bool value)
56 {
57    switch (state->v) {
58    case PAN_TRISTATE_DONTCARE:
59       state->v = value ? PAN_TRISTATE_TRUE : PAN_TRISTATE_FALSE;
60       return true;
61 
62    case PAN_TRISTATE_FALSE:
63       return (value == false);
64 
65    case PAN_TRISTATE_TRUE:
66       return (value == true);
67 
68    default:
69       unreachable("Invalid tristate value");
70    }
71 }
72 
73 /*
74  * Read the boolean value of a tristate. Return value undefined in the don't
75  * care state.
76  */
77 static inline bool
pan_tristate_get(struct pan_tristate state)78 pan_tristate_get(struct pan_tristate state)
79 {
80    return (state.v == PAN_TRISTATE_TRUE);
81 }
82 
83 /* A panfrost_batch corresponds to a bound FBO we're rendering to,
84  * collecting over multiple draws. */
85 
86 struct panfrost_batch {
87    struct panfrost_context *ctx;
88    struct pipe_framebuffer_state key;
89 
90    /* Sequence number used to implement LRU eviction when all batch slots are
91     * used */
92    uint64_t seqnum;
93 
94    /* Buffers cleared (PIPE_CLEAR_* bitmask) */
95    unsigned clear;
96 
97    /* Buffers drawn */
98    unsigned draws;
99 
100    /* Buffers read */
101    unsigned read;
102 
103    /* Buffers needing resolve to memory */
104    unsigned resolve;
105 
106    /* Packed clear values, indexed by both render target as well as word.
107     * Essentially, a single pixel is packed, with some padding to bring it
108     * up to a 32-bit interval; that pixel is then duplicated over to fill
109     * all 16-bytes */
110 
111    uint32_t clear_color[PIPE_MAX_COLOR_BUFS][4];
112    float clear_depth;
113    unsigned clear_stencil;
114 
115    /* Amount of thread local storage required per thread */
116    unsigned stack_size;
117 
118    /* Amount of shared memory needed per workgroup (for compute) */
119    unsigned shared_size;
120 
121    /* The bounding box covered by this job, taking scissors into account.
122     * Basically, the bounding box we have to run fragment shaders for */
123 
124    unsigned minx, miny;
125    unsigned maxx, maxy;
126 
127    /* Acts as a rasterizer discard */
128    bool scissor_culls_everything;
129 
130    /* BOs referenced not in the pool */
131    unsigned num_bos;
132    struct util_dynarray bos;
133 
134    /* Pool owned by this batch (released when the batch is released) used for
135     * temporary descriptors */
136    struct panfrost_pool pool;
137 
138    /* Pool also owned by this batch that is not CPU mapped (created as
139     * INVISIBLE) used for private GPU-internal structures, particularly
140     * varyings */
141    struct panfrost_pool invisible_pool;
142 
143    /* Scratchpad BO bound to the batch, or NULL if none bound yet */
144    struct panfrost_bo *scratchpad;
145 
146    /* Shared memory BO bound to the batch, or NULL if none bound yet */
147    struct panfrost_bo *shared_memory;
148 
149    /* Framebuffer descriptor. */
150    struct panfrost_ptr framebuffer;
151 
152    /* Thread local storage descriptor. */
153    struct panfrost_ptr tls;
154 
155    /* Vertex count */
156    uint32_t vertex_count;
157 
158    /* Tiler context */
159    struct pan_tiler_context tiler_ctx;
160 
161    /* Only used on midgard. */
162    struct panfrost_bo *polygon_list_bo;
163 
164    /* Keep the num_work_groups sysval around for indirect dispatch */
165    mali_ptr num_wg_sysval[3];
166 
167    /* Cached descriptors */
168    mali_ptr viewport;
169    mali_ptr rsd[PIPE_SHADER_TYPES];
170    mali_ptr textures[PIPE_SHADER_TYPES];
171    mali_ptr samplers[PIPE_SHADER_TYPES];
172    mali_ptr attribs[PIPE_SHADER_TYPES];
173    mali_ptr attrib_bufs[PIPE_SHADER_TYPES];
174    mali_ptr uniform_buffers[PIPE_SHADER_TYPES];
175    mali_ptr push_uniforms[PIPE_SHADER_TYPES];
176    mali_ptr depth_stencil;
177    mali_ptr blend;
178 
179    unsigned nr_push_uniforms[PIPE_SHADER_TYPES];
180    unsigned nr_uniform_buffers[PIPE_SHADER_TYPES];
181 
182    /* Varying related pointers */
183    struct {
184       mali_ptr bufs;
185       unsigned nr_bufs;
186       mali_ptr vs;
187       mali_ptr fs;
188       mali_ptr pos;
189       mali_ptr psiz;
190    } varyings;
191 
192    /* Index array */
193    mali_ptr indices;
194 
195    /* Valhall: struct mali_scissor_packed */
196    unsigned scissor[2];
197    float minimum_z, maximum_z;
198 
199    /* Used on Valhall only. Midgard includes attributes in-band with
200     * attributes, wildly enough.
201     */
202    mali_ptr images[PIPE_SHADER_TYPES];
203 
204    /* SSBOs. */
205    mali_ptr ssbos[PIPE_SHADER_TYPES];
206 
207    /* On Valhall, these are properties of the batch. On Bifrost, they are
208     * per draw.
209     */
210    struct pan_tristate sprite_coord_origin;
211    struct pan_tristate first_provoking_vertex;
212 
213    /** This one is always on the batch */
214    struct pan_tristate line_smoothing;
215 
216    /* Number of effective draws in the batch. Draws with rasterization disabled
217     * don't count as effective draws. It's basically the number of IDVS or
218     * <vertex,tiler> jobs present in the batch.
219     */
220    uint32_t draw_count;
221 
222    /* Number of compute jobs in the batch. */
223    uint32_t compute_count;
224 
225    /* Set when cycle count is required for this batch. */
226    bool need_job_req_cycle_count;
227 
228    /* The batch contains a time query. */
229    bool has_time_query;
230 
231    /* Job frontend specific fields. */
232    union {
233       struct panfrost_jm_batch jm;
234       struct panfrost_csf_batch csf;
235    };
236 };
237 
238 /* Functions for managing the above */
239 
240 struct panfrost_batch *panfrost_get_batch_for_fbo(struct panfrost_context *ctx);
241 
242 struct panfrost_batch *
243 panfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx,
244                                  const char *reason);
245 
246 void panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
247                            enum pipe_shader_type stage);
248 
249 void panfrost_batch_write_bo(struct panfrost_batch *batch,
250                              struct panfrost_bo *bo,
251                              enum pipe_shader_type stage);
252 
253 void panfrost_batch_read_rsrc(struct panfrost_batch *batch,
254                               struct panfrost_resource *rsrc,
255                               enum pipe_shader_type stage);
256 
257 void panfrost_batch_write_rsrc(struct panfrost_batch *batch,
258                                struct panfrost_resource *rsrc,
259                                enum pipe_shader_type stage);
260 
261 bool panfrost_any_batch_reads_rsrc(struct panfrost_context *ctx,
262                                    struct panfrost_resource *rsrc);
263 
264 bool panfrost_any_batch_writes_rsrc(struct panfrost_context *ctx,
265                                     struct panfrost_resource *rsrc);
266 
267 struct panfrost_bo *panfrost_batch_create_bo(struct panfrost_batch *batch,
268                                              size_t size, uint32_t create_flags,
269                                              enum pipe_shader_type stage,
270                                              const char *label);
271 
272 void panfrost_flush_all_batches(struct panfrost_context *ctx,
273                                 const char *reason);
274 
275 void panfrost_flush_batches_accessing_rsrc(struct panfrost_context *ctx,
276                                            struct panfrost_resource *rsrc,
277                                            const char *reason);
278 
279 void panfrost_flush_writer(struct panfrost_context *ctx,
280                            struct panfrost_resource *rsrc, const char *reason);
281 
282 void panfrost_batch_adjust_stack_size(struct panfrost_batch *batch);
283 
284 struct panfrost_bo *panfrost_batch_get_scratchpad(struct panfrost_batch *batch,
285                                                   unsigned size,
286                                                   unsigned thread_tls_alloc,
287                                                   unsigned core_id_range);
288 
289 struct panfrost_bo *
290 panfrost_batch_get_shared_memory(struct panfrost_batch *batch, unsigned size,
291                                  unsigned workgroup_count);
292 
293 void panfrost_batch_clear(struct panfrost_batch *batch, unsigned buffers,
294                           const union pipe_color_union *color, double depth,
295                           unsigned stencil);
296 
297 void panfrost_batch_union_scissor(struct panfrost_batch *batch, unsigned minx,
298                                   unsigned miny, unsigned maxx, unsigned maxy);
299 
300 bool panfrost_batch_skip_rasterization(struct panfrost_batch *batch);
301 
302 static inline bool
panfrost_has_fragment_job(struct panfrost_batch * batch)303 panfrost_has_fragment_job(struct panfrost_batch *batch)
304 {
305    return batch->draw_count > 0 || batch->clear;
306 }
307 
308 #endif
309