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