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 #include "pan_blend.h"
35 #include "pan_encoder.h"
36 #include "pan_texture.h"
37 #include "midgard_pack.h"
38 
39 #include "pipe/p_compiler.h"
40 #include "pipe/p_config.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_format.h"
44 #include "pipe/p_screen.h"
45 #include "pipe/p_state.h"
46 #include "util/u_blitter.h"
47 #include "util/hash_table.h"
48 
49 #include "midgard/midgard_compile.h"
50 #include "compiler/shader_enums.h"
51 
52 /* Forward declare to avoid extra header dep */
53 struct prim_convert_context;
54 
55 #define MAX_VARYINGS   4096
56 
57 #define SET_BIT(lval, bit, cond) \
58 	if (cond) \
59 		lval |= (bit); \
60 	else \
61 		lval &= ~(bit);
62 
63 struct panfrost_constant_buffer {
64         struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
65         uint32_t enabled_mask;
66         uint32_t dirty_mask;
67 };
68 
69 struct panfrost_query {
70         /* Passthrough from Gallium */
71         unsigned type;
72         unsigned index;
73 
74         /* For computed queries. 64-bit to prevent overflow */
75         struct {
76                 uint64_t start;
77                 uint64_t end;
78         };
79 
80         /* Memory for the GPU to writeback the value of the query */
81         struct panfrost_bo *bo;
82 
83         /* Whether an occlusion query is for a MSAA framebuffer */
84         bool msaa;
85 };
86 
87 struct panfrost_fence {
88         struct pipe_reference reference;
89         uint32_t syncobj;
90         bool signaled;
91 };
92 
93 struct panfrost_streamout_target {
94         struct pipe_stream_output_target base;
95         uint32_t offset;
96 };
97 
98 struct panfrost_streamout {
99         struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
100         unsigned num_targets;
101 };
102 
103 struct panfrost_context {
104         /* Gallium context */
105         struct pipe_context base;
106 
107         /* Upload manager for small resident GPU-internal data structures, like
108          * sampler descriptors. We use an upload manager since the minimum BO
109          * size from the kernel is 4kb */
110         struct u_upload_mgr *state_uploader;
111 
112         /* Sync obj used to keep track of in-flight jobs. */
113         uint32_t syncobj;
114 
115         /* Bound job batch and map of panfrost_batch_key to job batches */
116         struct panfrost_batch *batch;
117         struct hash_table *batches;
118 
119         /* panfrost_bo -> panfrost_bo_access */
120         struct hash_table *accessed_bos;
121 
122         /* Within a launch_grid call.. */
123         const struct pipe_grid_info *compute_grid;
124 
125         /* Bit mask for supported PIPE_DRAW for this hardware */
126         unsigned draw_modes;
127 
128         struct pipe_framebuffer_state pipe_framebuffer;
129         struct panfrost_streamout streamout;
130 
131         bool active_queries;
132         uint64_t prims_generated;
133         uint64_t tf_prims_generated;
134         struct panfrost_query *occlusion_query;
135 
136         unsigned vertex_count;
137         unsigned instance_count;
138         unsigned offset_start;
139         enum pipe_prim_type active_prim;
140 
141         /* If instancing is enabled, vertex count padded for instance; if
142          * it is disabled, just equal to plain vertex count */
143         unsigned padded_count;
144 
145         /* TODO: Multiple uniform buffers (index =/= 0), finer updates? */
146 
147         struct panfrost_constant_buffer constant_buffer[PIPE_SHADER_TYPES];
148 
149         struct panfrost_rasterizer *rasterizer;
150         struct panfrost_shader_variants *shader[PIPE_SHADER_TYPES];
151         struct panfrost_vertex_state *vertex;
152 
153         struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
154         uint32_t vb_mask;
155 
156         struct pipe_shader_buffer ssbo[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_BUFFERS];
157         uint32_t ssbo_mask[PIPE_SHADER_TYPES];
158 
159         struct panfrost_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
160         unsigned sampler_count[PIPE_SHADER_TYPES];
161 
162         struct panfrost_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
163         unsigned sampler_view_count[PIPE_SHADER_TYPES];
164 
165         struct primconvert_context *primconvert;
166         struct blitter_context *blitter;
167 
168         struct panfrost_blend_state *blend;
169 
170         struct pipe_viewport_state pipe_viewport;
171         struct pipe_scissor_state scissor;
172         struct pipe_blend_color blend_color;
173         struct panfrost_zsa_state *depth_stencil;
174         struct pipe_stencil_ref stencil_ref;
175         unsigned sample_mask;
176         unsigned min_samples;
177 
178         struct panfrost_blend_state *blit_blend;
179         struct hash_table *blend_shaders;
180 };
181 
182 /* Corresponds to the CSO */
183 
184 struct panfrost_rasterizer {
185         struct pipe_rasterizer_state base;
186 };
187 
188 /* Variants bundle together to form the backing CSO, bundling multiple
189  * shaders with varying emulated features baked in */
190 
191 /* A shader state corresponds to the actual, current variant of the shader */
192 struct panfrost_shader_state {
193         /* Compiled, mapped descriptor, ready for the hardware */
194         bool compiled;
195 
196         /* Uploaded shader descriptor (TODO: maybe stuff the packed unuploaded
197          * bits in a union to save some memory?) */
198 
199         struct {
200                 struct pipe_resource *rsrc;
201                 uint32_t offset;
202         } upload;
203 
204         struct MALI_SHADER shader;
205         struct MALI_RENDERER_PROPERTIES properties;
206         struct MALI_PRELOAD preload;
207 
208         /* Non-descript information */
209         unsigned uniform_count;
210         unsigned work_reg_count;
211         bool can_discard;
212         bool writes_point_size;
213         bool writes_depth;
214         bool writes_stencil;
215         bool reads_point_coord;
216         bool reads_face;
217         bool reads_frag_coord;
218         bool writes_global;
219         unsigned stack_size;
220         unsigned shared_size;
221 
222         /* Does the fragment shader have side effects? In particular, if output
223          * is masked out, is it legal to skip shader execution? */
224         bool fs_sidefx;
225 
226         /* For Bifrost - output type for each RT */
227         enum mali_bifrost_register_file_format blend_types[MALI_BIFROST_BLEND_MAX_RT];
228 
229         unsigned attribute_count, varying_count, ubo_count;
230         enum mali_format varyings[PIPE_MAX_ATTRIBS];
231         gl_varying_slot varyings_loc[PIPE_MAX_ATTRIBS];
232         struct pipe_stream_output_info stream_output;
233         uint64_t so_mask;
234 
235         unsigned sysval_count;
236         unsigned sysval[MAX_SYSVAL_COUNT];
237 
238         /* Should we enable helper invocations */
239         bool helper_invocations;
240 
241         /* GPU-executable memory */
242         struct panfrost_bo *bo;
243 
244         BITSET_WORD outputs_read;
245         enum pipe_format rt_formats[8];
246 
247         /* Blend return addresses */
248         uint32_t blend_ret_addrs[8];
249 };
250 
251 /* A collection of varyings (the CSO) */
252 struct panfrost_shader_variants {
253         /* A panfrost_shader_variants can represent a shader for
254          * either graphics or compute */
255 
256         bool is_compute;
257 
258         union {
259                 struct pipe_shader_state base;
260                 struct pipe_compute_state cbase;
261         };
262 
263         struct panfrost_shader_state *variants;
264         unsigned variant_space;
265 
266         unsigned variant_count;
267 
268         /* The current active variant */
269         unsigned active_variant;
270 };
271 
272 struct panfrost_vertex_state {
273         unsigned num_elements;
274 
275         struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
276         unsigned formats[PIPE_MAX_ATTRIBS];
277 };
278 
279 struct panfrost_zsa_state {
280         struct pipe_depth_stencil_alpha_state base;
281 
282         /* Precomputed stencil state */
283         struct MALI_STENCIL stencil_front;
284         struct MALI_STENCIL stencil_back;
285         u8 stencil_mask_front;
286         u8 stencil_mask_back;
287 };
288 
289 struct panfrost_sampler_state {
290         struct pipe_sampler_state base;
291         struct mali_midgard_sampler_packed hw;
292 };
293 
294 /* Misnomer: Sampler view corresponds to textures, not samplers */
295 
296 struct panfrost_sampler_view {
297         struct pipe_sampler_view base;
298         struct panfrost_bo *bo;
299         struct mali_bifrost_texture_packed bifrost_descriptor;
300         mali_ptr texture_bo;
301         uint64_t modifier;
302 };
303 
304 static inline struct panfrost_context *
pan_context(struct pipe_context * pcontext)305 pan_context(struct pipe_context *pcontext)
306 {
307         return (struct panfrost_context *) pcontext;
308 }
309 
310 static inline struct panfrost_streamout_target *
pan_so_target(struct pipe_stream_output_target * target)311 pan_so_target(struct pipe_stream_output_target *target)
312 {
313         return (struct panfrost_streamout_target *)target;
314 }
315 
316 static inline struct panfrost_shader_state *
panfrost_get_shader_state(struct panfrost_context * ctx,enum pipe_shader_type st)317 panfrost_get_shader_state(struct panfrost_context *ctx,
318                           enum pipe_shader_type st)
319 {
320         struct panfrost_shader_variants *all = ctx->shader[st];
321 
322         if (!all)
323                 return NULL;
324 
325         return &all->variants[all->active_variant];
326 }
327 
328 struct pipe_context *
329 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags);
330 
331 bool
332 panfrost_writes_point_size(struct panfrost_context *ctx);
333 
334 struct panfrost_ptr
335 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler);
336 
337 void
338 panfrost_flush(
339         struct pipe_context *pipe,
340         struct pipe_fence_handle **fence,
341         unsigned flags);
342 
343 mali_ptr panfrost_sfbd_fragment(struct panfrost_batch *batch, bool has_draws);
344 mali_ptr panfrost_mfbd_fragment(struct panfrost_batch *batch, bool has_draws);
345 
346 void
347 panfrost_attach_mfbd(struct panfrost_batch *batch, unsigned vertex_count);
348 
349 void
350 panfrost_attach_sfbd(struct panfrost_batch *batch, unsigned vertex_count);
351 
352 void
353 panfrost_emit_midg_tiler(struct panfrost_batch *batch,
354                          struct mali_midgard_tiler_packed *tp,
355                          unsigned vertex_count);
356 
357 mali_ptr
358 panfrost_fragment_job(struct panfrost_batch *batch, bool has_draws);
359 
360 void
361 panfrost_shader_compile(struct panfrost_context *ctx,
362                         enum pipe_shader_ir ir_type,
363                         const void *ir,
364                         gl_shader_stage stage,
365                         struct panfrost_shader_state *state,
366                         uint64_t *outputs_written);
367 
368 void
369 panfrost_create_sampler_view_bo(struct panfrost_sampler_view *so,
370                                 struct pipe_context *pctx,
371                                 struct pipe_resource *texture);
372 
373 /* Instancing */
374 
375 mali_ptr
376 panfrost_vertex_buffer_address(struct panfrost_context *ctx, unsigned i);
377 
378 /* Compute */
379 
380 void
381 panfrost_compute_context_init(struct pipe_context *pctx);
382 
383 
384 #endif
385