panfrost: Implement dispatch helpers
[mesa.git] / src / gallium / drivers / panfrost / pan_context.h
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #ifndef __BUILDER_H__
26 #define __BUILDER_H__
27
28 #define _LARGEFILE64_SOURCE 1
29 #define CACHE_LINE_SIZE 1024 /* TODO */
30 #include <sys/mman.h>
31 #include <assert.h>
32 #include "pan_resource.h"
33 #include "pan_job.h"
34
35 #include "pipe/p_compiler.h"
36 #include "pipe/p_config.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_format.h"
40 #include "pipe/p_screen.h"
41 #include "pipe/p_state.h"
42 #include "util/u_blitter.h"
43 #include "util/hash_table.h"
44
45 #include "midgard/midgard_compile.h"
46
47 /* Forward declare to avoid extra header dep */
48 struct prim_convert_context;
49
50 #define MAX_VARYINGS 4096
51
52 //#define PAN_DIRTY_CLEAR (1 << 0)
53 #define PAN_DIRTY_RASTERIZER (1 << 2)
54 #define PAN_DIRTY_FS (1 << 3)
55 #define PAN_DIRTY_FRAG_CORE (PAN_DIRTY_FS) /* Dirty writes are tied */
56 #define PAN_DIRTY_VS (1 << 4)
57 #define PAN_DIRTY_VERTEX (1 << 5)
58 #define PAN_DIRTY_VERT_BUF (1 << 6)
59 //#define PAN_DIRTY_VIEWPORT (1 << 7)
60 #define PAN_DIRTY_SAMPLERS (1 << 8)
61 #define PAN_DIRTY_TEXTURES (1 << 9)
62
63 #define SET_BIT(lval, bit, cond) \
64 if (cond) \
65 lval |= (bit); \
66 else \
67 lval &= ~(bit);
68
69 struct panfrost_constant_buffer {
70 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
71 uint32_t enabled_mask;
72 uint32_t dirty_mask;
73 };
74
75 struct panfrost_query {
76 /* Passthrough from Gallium */
77 unsigned type;
78 unsigned index;
79
80 /* Memory for the GPU to writeback the value of the query */
81 struct panfrost_transfer transfer;
82 };
83
84 struct panfrost_fence {
85 struct pipe_reference reference;
86 int fd;
87 };
88
89 #define PANFROST_MAX_TRANSIENT_ENTRIES 64
90
91 struct panfrost_transient_pool {
92 /* Memory blocks in the pool */
93 struct panfrost_memory_entry *entries[PANFROST_MAX_TRANSIENT_ENTRIES];
94
95 /* Number of entries we own */
96 unsigned entry_count;
97
98 /* Current entry that we are writing to, zero-indexed, strictly less than entry_count */
99 unsigned entry_index;
100
101 /* Number of bytes into the current entry we are */
102 off_t entry_offset;
103
104 /* Entry size (all entries must be homogenous) */
105 size_t entry_size;
106 };
107
108 struct panfrost_context {
109 /* Gallium context */
110 struct pipe_context base;
111
112 /* Bound job and map of panfrost_job_key to jobs */
113 struct panfrost_job *job;
114 struct hash_table *jobs;
115
116 /* panfrost_resource -> panfrost_job */
117 struct hash_table *write_jobs;
118
119 /* Bit mask for supported PIPE_DRAW for this hardware */
120 unsigned draw_modes;
121
122 struct pipe_framebuffer_state pipe_framebuffer;
123
124 /* The number of concurrent FBOs allowed depends on the number of pools
125 * used; pools are ringed for parallelism opportunities */
126
127 struct panfrost_transient_pool transient_pools[2];
128 int cmdstream_i;
129
130 struct panfrost_memory cmdstream_persistent;
131 struct panfrost_memory shaders;
132 struct panfrost_memory scratchpad;
133 struct panfrost_memory tiler_heap;
134 struct panfrost_memory varying_mem;
135 struct panfrost_memory tiler_polygon_list;
136 struct panfrost_memory tiler_dummy;
137 struct panfrost_memory depth_stencil_buffer;
138
139 struct panfrost_query *occlusion_query;
140
141 /* Each draw has corresponding vertex and tiler payloads */
142 struct midgard_payload_vertex_tiler payload_vertex;
143 struct midgard_payload_vertex_tiler payload_tiler;
144
145 /* The fragment shader binary itself is pointed here (for the tripipe) but
146 * also everything else in the shader core, including blending, the
147 * stencil/depth tests, etc. Refer to the presentations. */
148
149 struct mali_shader_meta fragment_shader_core;
150
151 /* Per-draw Dirty flags are setup like any other driver */
152 int dirty;
153
154 unsigned vertex_count;
155
156 union mali_attr attributes[PIPE_MAX_ATTRIBS];
157
158 unsigned varying_height;
159
160 struct mali_single_framebuffer vt_framebuffer_sfbd;
161 struct bifrost_framebuffer vt_framebuffer_mfbd;
162
163 /* TODO: Multiple uniform buffers (index =/= 0), finer updates? */
164
165 struct panfrost_constant_buffer constant_buffer[PIPE_SHADER_TYPES];
166
167 /* CSOs */
168 struct panfrost_rasterizer *rasterizer;
169
170 struct panfrost_shader_variants *vs;
171 struct panfrost_shader_variants *fs;
172
173 struct panfrost_vertex_state *vertex;
174
175 struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
176 uint32_t vb_mask;
177
178 struct panfrost_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
179 unsigned sampler_count[PIPE_SHADER_TYPES];
180
181 struct panfrost_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
182 unsigned sampler_view_count[PIPE_SHADER_TYPES];
183
184 struct primconvert_context *primconvert;
185 struct blitter_context *blitter;
186
187 /* Blitting the wallpaper (the old contents of the framebuffer back to
188 * itself) uses a dedicated u_blitter instance versus general blit()
189 * callbacks from Gallium, as the blit() callback can trigger
190 * wallpapering without Gallium realising, which in turns u_blitter
191 * errors due to unsupported reucrsion */
192
193 struct blitter_context *blitter_wallpaper;
194 struct panfrost_job *wallpaper_batch;
195
196 struct panfrost_blend_state *blend;
197
198 struct pipe_viewport_state pipe_viewport;
199 struct pipe_scissor_state scissor;
200 struct pipe_blend_color blend_color;
201 struct pipe_depth_stencil_alpha_state *depth_stencil;
202 struct pipe_stencil_ref stencil_ref;
203
204 /* True for t6XX, false for t8xx. */
205 bool is_t6xx;
206
207 /* If set, we'll require the use of single render-target framebuffer
208 * descriptors (SFBD), for older hardware -- specifically, <T760 hardware, If
209 * false, we'll use the MFBD no matter what. New hardware -does- retain support
210 * for SFBD, and in theory we could flip between them on a per-RT basis, but
211 * there's no real advantage to doing so */
212 bool require_sfbd;
213
214 uint32_t out_sync;
215 };
216
217 /* Corresponds to the CSO */
218
219 struct panfrost_rasterizer {
220 struct pipe_rasterizer_state base;
221
222 /* Bitmask of front face, etc */
223 unsigned tiler_gl_enables;
224 };
225
226 struct panfrost_blend_state {
227 struct pipe_blend_state base;
228
229 /* Whether a blend shader is in use */
230 bool has_blend_shader;
231
232 /* Compiled fixed function command */
233 struct mali_blend_equation equation;
234 float constant;
235
236 /* Compiled blend shader */
237 mali_ptr blend_shader;
238 int blend_work_count;
239 };
240
241 /* Variants bundle together to form the backing CSO, bundling multiple
242 * shaders with varying emulated features baked in (alpha test
243 * parameters, etc) */
244 #define MAX_SHADER_VARIANTS 8
245
246 /* A shader state corresponds to the actual, current variant of the shader */
247 struct panfrost_shader_state {
248 struct pipe_shader_state *base;
249
250 /* Compiled, mapped descriptor, ready for the hardware */
251 bool compiled;
252 struct mali_shader_meta *tripipe;
253 mali_ptr tripipe_gpu;
254
255 /* Non-descript information */
256 int uniform_count;
257 bool can_discard;
258 bool writes_point_size;
259 bool reads_point_coord;
260
261 struct mali_attr_meta varyings[PIPE_MAX_ATTRIBS];
262 gl_varying_slot varyings_loc[PIPE_MAX_ATTRIBS];
263
264 unsigned sysval_count;
265 unsigned sysval[MAX_SYSVAL_COUNT];
266
267 /* Information on this particular shader variant */
268 struct pipe_alpha_state alpha_state;
269
270 uint16_t point_sprite_mask;
271 unsigned point_sprite_upper_left : 1;
272 };
273
274 /* A collection of varyings (the CSO) */
275 struct panfrost_shader_variants {
276 struct pipe_shader_state base;
277
278 struct panfrost_shader_state variants[MAX_SHADER_VARIANTS];
279 unsigned variant_count;
280
281 /* The current active variant */
282 unsigned active_variant;
283 };
284
285 struct panfrost_vertex_state {
286 unsigned num_elements;
287
288 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
289 struct mali_attr_meta hw[PIPE_MAX_ATTRIBS];
290 };
291
292 struct panfrost_sampler_state {
293 struct pipe_sampler_state base;
294 struct mali_sampler_descriptor hw;
295 };
296
297 /* Misnomer: Sampler view corresponds to textures, not samplers */
298
299 struct panfrost_sampler_view {
300 struct pipe_sampler_view base;
301 struct mali_texture_descriptor hw;
302 };
303
304 static inline struct panfrost_context *
305 pan_context(struct pipe_context *pcontext)
306 {
307 return (struct panfrost_context *) pcontext;
308 }
309
310 struct pipe_context *
311 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags);
312
313 void
314 panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data);
315
316 struct panfrost_transfer
317 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler);
318
319 unsigned
320 panfrost_get_default_swizzle(unsigned components);
321
322 void
323 panfrost_flush(
324 struct pipe_context *pipe,
325 struct pipe_fence_handle **fence,
326 unsigned flags);
327
328 bool
329 panfrost_is_scanout(struct panfrost_context *ctx);
330
331 mali_ptr panfrost_sfbd_fragment(struct panfrost_context *ctx, bool has_draws);
332 mali_ptr panfrost_mfbd_fragment(struct panfrost_context *ctx, bool has_draws);
333
334 struct bifrost_framebuffer
335 panfrost_emit_mfbd(struct panfrost_context *ctx, unsigned vertex_count);
336
337 struct mali_single_framebuffer
338 panfrost_emit_sfbd(struct panfrost_context *ctx, unsigned vertex_count);
339
340 mali_ptr
341 panfrost_fragment_job(struct panfrost_context *ctx, bool has_draws);
342
343 void
344 panfrost_shader_compile(struct panfrost_context *ctx, struct mali_shader_meta *meta, const char *src, int type, struct panfrost_shader_state *state);
345
346 void
347 panfrost_pack_work_groups_compute(
348 struct mali_vertex_tiler_prefix *out,
349 unsigned num_x,
350 unsigned num_y,
351 unsigned num_z,
352 unsigned size_x,
353 unsigned size_y,
354 unsigned size_z);
355
356 void
357 panfrost_pack_work_groups_fused(
358 struct mali_vertex_tiler_prefix *vertex,
359 struct mali_vertex_tiler_prefix *tiler,
360 unsigned num_x,
361 unsigned num_y,
362 unsigned num_z,
363 unsigned size_x,
364 unsigned size_y,
365 unsigned size_z);
366
367
368
369 #endif