ad52ea61290220ff60eba2388dc376ccad7430c3
[mesa.git] / src / gallium / drivers / vc5 / vc5_context.h
1 /*
2 * Copyright © 2014-2017 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #ifndef VC5_CONTEXT_H
26 #define VC5_CONTEXT_H
27
28 #ifdef V3D_VERSION
29 #include "broadcom/common/v3d_macros.h"
30 #endif
31
32 #include <stdio.h>
33
34 #include "pipe/p_context.h"
35 #include "pipe/p_state.h"
36 #include "util/bitset.h"
37 #include "util/slab.h"
38 #include "xf86drm.h"
39 #include "v3d_drm.h"
40 #include "vc5_screen.h"
41
42 struct vc5_job;
43 struct vc5_bo;
44 void vc5_job_add_bo(struct vc5_job *job, struct vc5_bo *bo);
45
46 #include "vc5_bufmgr.h"
47 #include "vc5_resource.h"
48 #include "vc5_cl.h"
49
50 #ifdef USE_VC5_SIMULATOR
51 #define using_vc5_simulator true
52 #else
53 #define using_vc5_simulator false
54 #endif
55
56 #define VC5_DIRTY_BLEND (1 << 0)
57 #define VC5_DIRTY_RASTERIZER (1 << 1)
58 #define VC5_DIRTY_ZSA (1 << 2)
59 #define VC5_DIRTY_FRAGTEX (1 << 3)
60 #define VC5_DIRTY_VERTTEX (1 << 4)
61
62 #define VC5_DIRTY_BLEND_COLOR (1 << 7)
63 #define VC5_DIRTY_STENCIL_REF (1 << 8)
64 #define VC5_DIRTY_SAMPLE_MASK (1 << 9)
65 #define VC5_DIRTY_FRAMEBUFFER (1 << 10)
66 #define VC5_DIRTY_STIPPLE (1 << 11)
67 #define VC5_DIRTY_VIEWPORT (1 << 12)
68 #define VC5_DIRTY_CONSTBUF (1 << 13)
69 #define VC5_DIRTY_VTXSTATE (1 << 14)
70 #define VC5_DIRTY_VTXBUF (1 << 15)
71 #define VC5_DIRTY_SCISSOR (1 << 17)
72 #define VC5_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
73 #define VC5_DIRTY_PRIM_MODE (1 << 19)
74 #define VC5_DIRTY_CLIP (1 << 20)
75 #define VC5_DIRTY_UNCOMPILED_VS (1 << 21)
76 #define VC5_DIRTY_UNCOMPILED_FS (1 << 22)
77 #define VC5_DIRTY_COMPILED_CS (1 << 23)
78 #define VC5_DIRTY_COMPILED_VS (1 << 24)
79 #define VC5_DIRTY_COMPILED_FS (1 << 25)
80 #define VC5_DIRTY_FS_INPUTS (1 << 26)
81 #define VC5_DIRTY_STREAMOUT (1 << 27)
82 #define VC5_DIRTY_OQ (1 << 28)
83 #define VC5_DIRTY_CENTROID_FLAGS (1 << 29)
84
85 #define VC5_MAX_FS_INPUTS 64
86
87 struct vc5_sampler_view {
88 struct pipe_sampler_view base;
89 uint32_t p0;
90 uint32_t p1;
91 /* Precomputed swizzles to pass in to the shader key. */
92 uint8_t swizzle[4];
93
94 uint8_t texture_shader_state[32];
95 /* V3D 4.x: Texture state struct. */
96 struct vc5_bo *bo;
97 };
98
99 struct vc5_sampler_state {
100 struct pipe_sampler_state base;
101 uint32_t p0;
102 uint32_t p1;
103
104 /* V3D 3.x: Packed texture state. */
105 uint8_t texture_shader_state[32];
106 /* V3D 4.x: Sampler state struct. */
107 struct vc5_bo *bo;
108 };
109
110 struct vc5_texture_stateobj {
111 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
112 unsigned num_textures;
113 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
114 unsigned num_samplers;
115 struct vc5_cl_reloc texture_state[PIPE_MAX_SAMPLERS];
116 };
117
118 struct vc5_shader_uniform_info {
119 enum quniform_contents *contents;
120 uint32_t *data;
121 uint32_t count;
122 };
123
124 struct vc5_uncompiled_shader {
125 /** A name for this program, so you can track it in shader-db output. */
126 uint32_t program_id;
127 /** How many variants of this program were compiled, for shader-db. */
128 uint32_t compiled_variant_count;
129 struct pipe_shader_state base;
130 uint32_t num_tf_outputs;
131 struct v3d_varying_slot *tf_outputs;
132 uint16_t tf_specs[16];
133 uint16_t tf_specs_psiz[16];
134 uint32_t num_tf_specs;
135
136 /**
137 * Flag for if the NIR in this shader originally came from TGSI. If
138 * so, we need to do some fixups at compile time, due to missing
139 * information in TGSI that exists in NIR.
140 */
141 bool was_tgsi;
142 };
143
144 struct vc5_compiled_shader {
145 struct vc5_bo *bo;
146
147 union {
148 struct v3d_prog_data *base;
149 struct v3d_vs_prog_data *vs;
150 struct v3d_fs_prog_data *fs;
151 } prog_data;
152
153 /**
154 * VC5_DIRTY_* flags that, when set in vc5->dirty, mean that the
155 * uniforms have to be rewritten (and therefore the shader state
156 * reemitted).
157 */
158 uint32_t uniform_dirty_bits;
159 };
160
161 struct vc5_program_stateobj {
162 struct vc5_uncompiled_shader *bind_vs, *bind_fs;
163 struct vc5_compiled_shader *cs, *vs, *fs;
164
165 struct vc5_bo *spill_bo;
166 int spill_size_per_thread;
167 };
168
169 struct vc5_constbuf_stateobj {
170 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
171 uint32_t enabled_mask;
172 uint32_t dirty_mask;
173 };
174
175 struct vc5_vertexbuf_stateobj {
176 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
177 unsigned count;
178 uint32_t enabled_mask;
179 uint32_t dirty_mask;
180 };
181
182 struct vc5_vertex_stateobj {
183 struct pipe_vertex_element pipe[VC5_MAX_ATTRIBUTES];
184 unsigned num_elements;
185
186 uint8_t attrs[12 * VC5_MAX_ATTRIBUTES];
187 struct vc5_bo *default_attribute_values;
188 };
189
190 struct vc5_streamout_stateobj {
191 struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
192 unsigned num_targets;
193 };
194
195 /* Hash table key for vc5->jobs */
196 struct vc5_job_key {
197 struct pipe_surface *cbufs[4];
198 struct pipe_surface *zsbuf;
199 };
200
201 enum vc5_ez_state {
202 VC5_EZ_UNDECIDED = 0,
203 VC5_EZ_GT_GE,
204 VC5_EZ_LT_LE,
205 VC5_EZ_DISABLED,
206 };
207
208 /**
209 * A complete bin/render job.
210 *
211 * This is all of the state necessary to submit a bin/render to the kernel.
212 * We want to be able to have multiple in progress at a time, so that we don't
213 * need to flush an existing CL just to switch to rendering to a new render
214 * target (which would mean reading back from the old render target when
215 * starting to render to it again).
216 */
217 struct vc5_job {
218 struct vc5_context *vc5;
219 struct vc5_cl bcl;
220 struct vc5_cl rcl;
221 struct vc5_cl indirect;
222 struct vc5_bo *tile_alloc;
223 struct vc5_bo *tile_state;
224 uint32_t shader_rec_count;
225
226 struct drm_v3d_submit_cl submit;
227
228 /**
229 * Set of all BOs referenced by the job. This will be used for making
230 * the list of BOs that the kernel will need to have paged in to
231 * execute our job.
232 */
233 struct set *bos;
234
235 /** Sum of the sizes of the BOs referenced by the job. */
236 uint32_t referenced_size;
237
238 struct set *write_prscs;
239
240 /* Size of the submit.bo_handles array. */
241 uint32_t bo_handles_size;
242
243 /** @{ Surfaces to submit rendering for. */
244 struct pipe_surface *cbufs[4];
245 struct pipe_surface *zsbuf;
246 /** @} */
247 /** @{
248 * Bounding box of the scissor across all queued drawing.
249 *
250 * Note that the max values are exclusive.
251 */
252 uint32_t draw_min_x;
253 uint32_t draw_min_y;
254 uint32_t draw_max_x;
255 uint32_t draw_max_y;
256 /** @} */
257 /** @{
258 * Width/height of the color framebuffer being rendered to,
259 * for VC5_TILE_RENDERING_MODE_CONFIG.
260 */
261 uint32_t draw_width;
262 uint32_t draw_height;
263 /** @} */
264 /** @{ Tile information, depending on MSAA and float color buffer. */
265 uint32_t draw_tiles_x; /** @< Number of tiles wide for framebuffer. */
266 uint32_t draw_tiles_y; /** @< Number of tiles high for framebuffer. */
267
268 uint32_t tile_width; /** @< Width of a tile. */
269 uint32_t tile_height; /** @< Height of a tile. */
270 /** maximum internal_bpp of all color render targets. */
271 uint32_t internal_bpp;
272
273 /** Whether the current rendering is in a 4X MSAA tile buffer. */
274 bool msaa;
275 /** @} */
276
277 /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
278 * first rendering.
279 */
280 uint32_t cleared;
281 /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
282 * (either clears or draws).
283 */
284 uint32_t resolve;
285 uint32_t clear_color[4][4];
286 float clear_z;
287 uint8_t clear_s;
288
289 /**
290 * Set if some drawing (triangles, blits, or just a glClear()) has
291 * been done to the FBO, meaning that we need to
292 * DRM_IOCTL_VC5_SUBMIT_CL.
293 */
294 bool needs_flush;
295
296 /**
297 * Set if there is a nonzero address for OCCLUSION_QUERY_COUNTER. If
298 * so, we need to disable it and flush before ending the CL, to keep
299 * the next tile from starting with it enabled.
300 */
301 bool oq_enabled;
302
303 /**
304 * Set when a packet enabling TF on all further primitives has been
305 * emitted.
306 */
307 bool tf_enabled;
308
309 /**
310 * Current EZ state for drawing. Updated at the start of draw after
311 * we've decided on the shader being rendered.
312 */
313 enum vc5_ez_state ez_state;
314 /**
315 * The first EZ state that was used for drawing with a decided EZ
316 * direction (so either UNDECIDED, GT, or LT).
317 */
318 enum vc5_ez_state first_ez_state;
319
320 /**
321 * Number of draw calls (not counting full buffer clears) queued in
322 * the current job.
323 */
324 uint32_t draw_calls_queued;
325
326 struct vc5_job_key key;
327 };
328
329 struct vc5_context {
330 struct pipe_context base;
331
332 int fd;
333 struct vc5_screen *screen;
334
335 /** The 3D rendering job for the currently bound FBO. */
336 struct vc5_job *job;
337
338 /* Map from struct vc5_job_key to the job for that FBO.
339 */
340 struct hash_table *jobs;
341
342 /**
343 * Map from vc5_resource to a job writing to that resource.
344 *
345 * Primarily for flushing jobs rendering to textures that are now
346 * being read from.
347 */
348 struct hash_table *write_jobs;
349
350 struct slab_child_pool transfer_pool;
351 struct blitter_context *blitter;
352
353 /** bitfield of VC5_DIRTY_* */
354 uint32_t dirty;
355
356 struct primconvert_context *primconvert;
357
358 struct hash_table *fs_cache, *vs_cache;
359 uint32_t next_uncompiled_program_id;
360 uint64_t next_compiled_program_id;
361
362 struct vc5_compiler_state *compiler_state;
363
364 uint8_t prim_mode;
365
366 /** Maximum index buffer valid for the current shader_rec. */
367 uint32_t max_index;
368
369 /** Sync object that our RCL will update as its out_sync. */
370 uint32_t out_sync;
371
372 struct u_upload_mgr *uploader;
373
374 /** @{ Current pipeline state objects */
375 struct pipe_scissor_state scissor;
376 struct pipe_blend_state *blend;
377 struct vc5_rasterizer_state *rasterizer;
378 struct vc5_depth_stencil_alpha_state *zsa;
379
380 struct vc5_texture_stateobj verttex, fragtex;
381
382 struct vc5_program_stateobj prog;
383
384 struct vc5_vertex_stateobj *vtx;
385
386 struct {
387 struct pipe_blend_color f;
388 uint16_t hf[4];
389 } blend_color;
390 struct pipe_stencil_ref stencil_ref;
391 unsigned sample_mask;
392 struct pipe_framebuffer_state framebuffer;
393
394 /* Per render target, whether we should swap the R and B fields in the
395 * shader's color output and in blending. If render targets disagree
396 * on the R/B swap and use the constant color, then we would need to
397 * fall back to in-shader blending.
398 */
399 uint8_t swap_color_rb;
400
401 /* Per render target, whether we should treat the dst alpha values as
402 * one in blending.
403 *
404 * For RGBX formats, the tile buffer's alpha channel will be
405 * undefined.
406 */
407 uint8_t blend_dst_alpha_one;
408
409 bool active_queries;
410
411 uint32_t tf_prims_generated;
412 uint32_t prims_generated;
413
414 struct pipe_poly_stipple stipple;
415 struct pipe_clip_state clip;
416 struct pipe_viewport_state viewport;
417 struct vc5_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
418 struct vc5_vertexbuf_stateobj vertexbuf;
419 struct vc5_streamout_stateobj streamout;
420 struct vc5_bo *current_oq;
421 /** @} */
422 };
423
424 struct vc5_rasterizer_state {
425 struct pipe_rasterizer_state base;
426
427 /* VC5_CONFIGURATION_BITS */
428 uint8_t config_bits[3];
429
430 float point_size;
431
432 /**
433 * Half-float (1/8/7 bits) value of polygon offset units for
434 * VC5_PACKET_DEPTH_OFFSET
435 */
436 uint16_t offset_units;
437 /**
438 * Half-float (1/8/7 bits) value of polygon offset scale for
439 * VC5_PACKET_DEPTH_OFFSET
440 */
441 uint16_t offset_factor;
442 };
443
444 struct vc5_depth_stencil_alpha_state {
445 struct pipe_depth_stencil_alpha_state base;
446
447 enum vc5_ez_state ez_state;
448
449 /** Uniforms for stencil state.
450 *
451 * Index 0 is either the front config, or the front-and-back config.
452 * Index 1 is the back config if doing separate back stencil.
453 * Index 2 is the writemask config if it's not a common mask value.
454 */
455 uint32_t stencil_uniforms[3];
456
457 uint8_t stencil_front[6];
458 uint8_t stencil_back[6];
459 };
460
461 #define perf_debug(...) do { \
462 if (unlikely(V3D_DEBUG & V3D_DEBUG_PERF)) \
463 fprintf(stderr, __VA_ARGS__); \
464 } while (0)
465
466 static inline struct vc5_context *
467 vc5_context(struct pipe_context *pcontext)
468 {
469 return (struct vc5_context *)pcontext;
470 }
471
472 static inline struct vc5_sampler_view *
473 vc5_sampler_view(struct pipe_sampler_view *psview)
474 {
475 return (struct vc5_sampler_view *)psview;
476 }
477
478 static inline struct vc5_sampler_state *
479 vc5_sampler_state(struct pipe_sampler_state *psampler)
480 {
481 return (struct vc5_sampler_state *)psampler;
482 }
483
484 struct pipe_context *vc5_context_create(struct pipe_screen *pscreen,
485 void *priv, unsigned flags);
486 void vc5_program_init(struct pipe_context *pctx);
487 void vc5_program_fini(struct pipe_context *pctx);
488 void vc5_query_init(struct pipe_context *pctx);
489
490 void vc5_simulator_init(struct vc5_screen *screen);
491 void vc5_simulator_destroy(struct vc5_screen *screen);
492 int vc5_simulator_flush(struct vc5_context *vc5,
493 struct drm_v3d_submit_cl *args,
494 struct vc5_job *job);
495 int vc5_simulator_ioctl(int fd, unsigned long request, void *arg);
496 void vc5_simulator_open_from_handle(int fd, uint32_t winsys_stride,
497 int handle, uint32_t size);
498
499 static inline int
500 vc5_ioctl(int fd, unsigned long request, void *arg)
501 {
502 if (using_vc5_simulator)
503 return vc5_simulator_ioctl(fd, request, arg);
504 else
505 return drmIoctl(fd, request, arg);
506 }
507
508 void vc5_set_shader_uniform_dirty_flags(struct vc5_compiled_shader *shader);
509 struct vc5_cl_reloc vc5_write_uniforms(struct vc5_context *vc5,
510 struct vc5_compiled_shader *shader,
511 struct vc5_constbuf_stateobj *cb,
512 struct vc5_texture_stateobj *texstate);
513
514 void vc5_flush(struct pipe_context *pctx);
515 void vc5_job_init(struct vc5_context *vc5);
516 struct vc5_job *vc5_get_job(struct vc5_context *vc5,
517 struct pipe_surface **cbufs,
518 struct pipe_surface *zsbuf);
519 struct vc5_job *vc5_get_job_for_fbo(struct vc5_context *vc5);
520 void vc5_job_add_bo(struct vc5_job *job, struct vc5_bo *bo);
521 void vc5_job_add_write_resource(struct vc5_job *job, struct pipe_resource *prsc);
522 void vc5_job_submit(struct vc5_context *vc5, struct vc5_job *job);
523 void vc5_flush_jobs_writing_resource(struct vc5_context *vc5,
524 struct pipe_resource *prsc);
525 void vc5_flush_jobs_reading_resource(struct vc5_context *vc5,
526 struct pipe_resource *prsc);
527 void vc5_update_compiled_shaders(struct vc5_context *vc5, uint8_t prim_mode);
528
529 bool vc5_rt_format_supported(const struct v3d_device_info *devinfo,
530 enum pipe_format f);
531 bool vc5_tex_format_supported(const struct v3d_device_info *devinfo,
532 enum pipe_format f);
533 uint8_t vc5_get_rt_format(const struct v3d_device_info *devinfo, enum pipe_format f);
534 uint8_t vc5_get_tex_format(const struct v3d_device_info *devinfo, enum pipe_format f);
535 uint8_t vc5_get_tex_return_size(const struct v3d_device_info *devinfo,
536 enum pipe_format f,
537 enum pipe_tex_compare compare);
538 uint8_t vc5_get_tex_return_channels(const struct v3d_device_info *devinfo,
539 enum pipe_format f);
540 const uint8_t *vc5_get_format_swizzle(const struct v3d_device_info *devinfo,
541 enum pipe_format f);
542 void vc5_get_internal_type_bpp_for_output_format(const struct v3d_device_info *devinfo,
543 uint32_t format,
544 uint32_t *type,
545 uint32_t *bpp);
546
547 void vc5_init_query_functions(struct vc5_context *vc5);
548 void vc5_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info);
549 void vc5_blitter_save(struct vc5_context *vc5);
550
551 struct vc5_fence *vc5_fence_create(struct vc5_context *vc5);
552
553 #ifdef v3dX
554 # include "v3dx_context.h"
555 #else
556 # define v3dX(x) v3d33_##x
557 # include "v3dx_context.h"
558 # undef v3dX
559
560 # define v3dX(x) v3d41_##x
561 # include "v3dx_context.h"
562 # undef v3dX
563 #endif
564
565 #endif /* VC5_CONTEXT_H */