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 V3D_CONTEXT_H
26 #define V3D_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 "drm-uapi/v3d_drm.h"
40 #include "v3d_screen.h"
41 #include "broadcom/common/v3d_limits.h"
42 
43 #include "broadcom/simulator/v3d_simulator.h"
44 #include "broadcom/compiler/v3d_compiler.h"
45 
46 struct v3d_job;
47 struct v3d_bo;
48 void v3d_job_add_bo(struct v3d_job *job, struct v3d_bo *bo);
49 
50 #include "v3d_bufmgr.h"
51 #include "v3d_resource.h"
52 #include "v3d_cl.h"
53 
54 #ifdef USE_V3D_SIMULATOR
55 #define using_v3d_simulator true
56 #else
57 #define using_v3d_simulator false
58 #endif
59 
60 #define V3D_DIRTY_BLEND               (1ull <<  0)
61 #define V3D_DIRTY_RASTERIZER          (1ull <<  1)
62 #define V3D_DIRTY_ZSA                 (1ull <<  2)
63 #define V3D_DIRTY_COMPTEX             (1ull <<  3)
64 #define V3D_DIRTY_VERTTEX             (1ull <<  4)
65 #define V3D_DIRTY_GEOMTEX             (1ull <<  5)
66 #define V3D_DIRTY_FRAGTEX             (1ull <<  6)
67 
68 #define V3D_DIRTY_SHADER_IMAGE        (1ull <<  9)
69 #define V3D_DIRTY_BLEND_COLOR         (1ull << 10)
70 #define V3D_DIRTY_STENCIL_REF         (1ull << 11)
71 #define V3D_DIRTY_SAMPLE_STATE        (1ull << 12)
72 #define V3D_DIRTY_FRAMEBUFFER         (1ull << 13)
73 #define V3D_DIRTY_STIPPLE             (1ull << 14)
74 #define V3D_DIRTY_VIEWPORT            (1ull << 15)
75 #define V3D_DIRTY_CONSTBUF            (1ull << 16)
76 #define V3D_DIRTY_VTXSTATE            (1ull << 17)
77 #define V3D_DIRTY_VTXBUF              (1ull << 18)
78 #define V3D_DIRTY_SCISSOR             (1ull << 19)
79 #define V3D_DIRTY_FLAT_SHADE_FLAGS    (1ull << 20)
80 #define V3D_DIRTY_PRIM_MODE           (1ull << 21)
81 #define V3D_DIRTY_CLIP                (1ull << 22)
82 #define V3D_DIRTY_UNCOMPILED_CS       (1ull << 23)
83 #define V3D_DIRTY_UNCOMPILED_VS       (1ull << 24)
84 #define V3D_DIRTY_UNCOMPILED_GS       (1ull << 25)
85 #define V3D_DIRTY_UNCOMPILED_FS       (1ull << 26)
86 
87 #define V3D_DIRTY_COMPILED_CS         (1ull << 29)
88 #define V3D_DIRTY_COMPILED_VS         (1ull << 30)
89 #define V3D_DIRTY_COMPILED_GS_BIN     (1ULL << 31)
90 #define V3D_DIRTY_COMPILED_GS         (1ULL << 32)
91 #define V3D_DIRTY_COMPILED_FS         (1ull << 33)
92 
93 #define V3D_DIRTY_FS_INPUTS           (1ull << 38)
94 #define V3D_DIRTY_GS_INPUTS           (1ull << 39)
95 #define V3D_DIRTY_STREAMOUT           (1ull << 40)
96 #define V3D_DIRTY_OQ                  (1ull << 41)
97 #define V3D_DIRTY_CENTROID_FLAGS      (1ull << 42)
98 #define V3D_DIRTY_NOPERSPECTIVE_FLAGS (1ull << 43)
99 #define V3D_DIRTY_SSBO                (1ull << 44)
100 
101 #define V3D_MAX_FS_INPUTS 64
102 
103 #define MAX_JOB_SCISSORS 16
104 
105 enum v3d_sampler_state_variant {
106         V3D_SAMPLER_STATE_BORDER_0,
107         V3D_SAMPLER_STATE_F16,
108         V3D_SAMPLER_STATE_F16_UNORM,
109         V3D_SAMPLER_STATE_F16_SNORM,
110         V3D_SAMPLER_STATE_F16_BGRA,
111         V3D_SAMPLER_STATE_F16_BGRA_UNORM,
112         V3D_SAMPLER_STATE_F16_BGRA_SNORM,
113         V3D_SAMPLER_STATE_F16_A,
114         V3D_SAMPLER_STATE_F16_A_SNORM,
115         V3D_SAMPLER_STATE_F16_A_UNORM,
116         V3D_SAMPLER_STATE_F16_LA,
117         V3D_SAMPLER_STATE_F16_LA_UNORM,
118         V3D_SAMPLER_STATE_F16_LA_SNORM,
119         V3D_SAMPLER_STATE_32,
120         V3D_SAMPLER_STATE_32_UNORM,
121         V3D_SAMPLER_STATE_32_SNORM,
122         V3D_SAMPLER_STATE_32_A,
123         V3D_SAMPLER_STATE_32_A_UNORM,
124         V3D_SAMPLER_STATE_32_A_SNORM,
125         V3D_SAMPLER_STATE_1010102U,
126         V3D_SAMPLER_STATE_16U,
127         V3D_SAMPLER_STATE_16I,
128         V3D_SAMPLER_STATE_8I,
129         V3D_SAMPLER_STATE_8U,
130 
131         V3D_SAMPLER_STATE_VARIANT_COUNT,
132 };
133 
134 enum v3d_flush_cond {
135         /* Flush job unless we are flushing for transform feedback, where we
136          * handle flushing in the driver via the 'Wait for TF' packet.
137          */
138         V3D_FLUSH_DEFAULT,
139         /* Always flush the job, even for cases where we would normally not
140          * do it, such as transform feedback.
141          */
142         V3D_FLUSH_ALWAYS,
143         /* Flush job if it is not the current FBO job. This is intended to
144          * skip automatic flushes of the current job for resources that we
145          * expect to be externally synchronized by the application using
146          * glMemoryBarrier(), such as SSBOs and shader images.
147          */
148         V3D_FLUSH_NOT_CURRENT_JOB,
149 };
150 
151 struct v3d_sampler_view {
152         struct pipe_sampler_view base;
153         uint32_t p0;
154         uint32_t p1;
155         /* Precomputed swizzles to pass in to the shader key. */
156         uint8_t swizzle[4];
157 
158         uint8_t texture_shader_state[32];
159         /* V3D 4.x: Texture state struct. */
160         struct v3d_bo *bo;
161 
162         enum v3d_sampler_state_variant sampler_variant;
163 
164         /* Actual texture to be read by this sampler view.  May be different
165          * from base.texture in the case of having a shadow tiled copy of a
166          * raster texture.
167          */
168         struct pipe_resource *texture;
169 
170         /* A serial ID used to identify cases where a new BO has been created
171          * and we need to rebind a sampler view that was created against the
172          * previous BO to to point to the new one.
173          */
174         uint32_t serial_id;
175 };
176 
177 struct v3d_sampler_state {
178         struct pipe_sampler_state base;
179         uint32_t p0;
180         uint32_t p1;
181 
182         /* V3D 3.x: Packed texture state. */
183         uint8_t texture_shader_state[32];
184         /* V3D 4.x: Sampler state struct. */
185         struct pipe_resource *sampler_state;
186         uint32_t sampler_state_offset[V3D_SAMPLER_STATE_VARIANT_COUNT];
187 
188         bool border_color_variants;
189 };
190 
191 struct v3d_texture_stateobj {
192         struct pipe_sampler_view *textures[V3D_MAX_TEXTURE_SAMPLERS];
193         unsigned num_textures;
194         struct pipe_sampler_state *samplers[V3D_MAX_TEXTURE_SAMPLERS];
195         unsigned num_samplers;
196         struct v3d_cl_reloc texture_state[V3D_MAX_TEXTURE_SAMPLERS];
197 };
198 
199 struct v3d_shader_uniform_info {
200         enum quniform_contents *contents;
201         uint32_t *data;
202         uint32_t count;
203 };
204 
205 struct v3d_uncompiled_shader {
206         /** A name for this program, so you can track it in shader-db output. */
207         uint32_t program_id;
208         /** How many variants of this program were compiled, for shader-db. */
209         uint32_t compiled_variant_count;
210         struct pipe_shader_state base;
211         uint32_t num_tf_outputs;
212         struct v3d_varying_slot *tf_outputs;
213         uint16_t tf_specs[16];
214         uint16_t tf_specs_psiz[16];
215         uint32_t num_tf_specs;
216 };
217 
218 struct v3d_compiled_shader {
219         struct pipe_resource *resource;
220         uint32_t offset;
221 
222         union {
223                 struct v3d_prog_data *base;
224                 struct v3d_vs_prog_data *vs;
225                 struct v3d_gs_prog_data *gs;
226                 struct v3d_fs_prog_data *fs;
227                 struct v3d_compute_prog_data *compute;
228         } prog_data;
229 
230         /**
231          * V3D_DIRTY_* flags that, when set in v3d->dirty, mean that the
232          * uniforms have to be rewritten (and therefore the shader state
233          * reemitted).
234          */
235         uint64_t uniform_dirty_bits;
236 };
237 
238 struct v3d_program_stateobj {
239         struct v3d_uncompiled_shader *bind_vs, *bind_gs, *bind_fs, *bind_compute;
240         struct v3d_compiled_shader *cs, *vs, *gs_bin, *gs, *fs, *compute;
241 
242         struct hash_table *cache[MESA_SHADER_STAGES];
243 
244         struct v3d_bo *spill_bo;
245         int spill_size_per_thread;
246 };
247 
248 struct v3d_constbuf_stateobj {
249         struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
250         uint32_t enabled_mask;
251         uint32_t dirty_mask;
252 };
253 
254 struct v3d_vertexbuf_stateobj {
255         struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
256         unsigned count;
257         uint32_t enabled_mask;
258         uint32_t dirty_mask;
259 };
260 
261 struct v3d_vertex_stateobj {
262         struct pipe_vertex_element pipe[V3D_MAX_VS_INPUTS / 4];
263         unsigned num_elements;
264 
265         uint8_t attrs[16 * (V3D_MAX_VS_INPUTS / 4)];
266         struct pipe_resource *defaults;
267         uint32_t defaults_offset;
268 };
269 
270 struct v3d_stream_output_target {
271         struct pipe_stream_output_target base;
272         /* Number of transform feedback vertices written to this target */
273         uint32_t recorded_vertex_count;
274 };
275 
276 struct v3d_streamout_stateobj {
277         struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
278         /* Number of vertices we've written into the buffer so far. */
279         uint32_t offsets[PIPE_MAX_SO_BUFFERS];
280         unsigned num_targets;
281 };
282 
283 struct v3d_ssbo_stateobj {
284         struct pipe_shader_buffer sb[PIPE_MAX_SHADER_BUFFERS];
285         uint32_t enabled_mask;
286 };
287 
288 /* Hash table key for v3d->jobs */
289 struct v3d_job_key {
290         struct pipe_surface *cbufs[V3D_MAX_DRAW_BUFFERS];
291         struct pipe_surface *zsbuf;
292         struct pipe_surface *bbuf;
293 };
294 
295 enum v3d_ez_state {
296         V3D_EZ_UNDECIDED = 0,
297         V3D_EZ_GT_GE,
298         V3D_EZ_LT_LE,
299         V3D_EZ_DISABLED,
300 };
301 
302 struct v3d_image_view {
303         struct pipe_image_view base;
304         /* V3D 4.x texture shader state struct */
305         struct pipe_resource *tex_state;
306         uint32_t tex_state_offset;
307 };
308 
309 struct v3d_shaderimg_stateobj {
310         struct v3d_image_view si[PIPE_MAX_SHADER_IMAGES];
311         uint32_t enabled_mask;
312 };
313 
314 struct v3d_perfmon_state {
315         /* The kernel perfmon id */
316         uint32_t kperfmon_id;
317         /* True if at least one job was submitted with this perfmon. */
318         bool job_submitted;
319         /* Fence to be signaled when the last job submitted with this perfmon
320          * is executed by the GPU.
321          */
322         struct v3d_fence *last_job_fence;
323         uint8_t counters[DRM_V3D_MAX_PERF_COUNTERS];
324         uint64_t values[DRM_V3D_MAX_PERF_COUNTERS];
325 };
326 
327 /**
328  * A complete bin/render job.
329  *
330  * This is all of the state necessary to submit a bin/render to the kernel.
331  * We want to be able to have multiple in progress at a time, so that we don't
332  * need to flush an existing CL just to switch to rendering to a new render
333  * target (which would mean reading back from the old render target when
334  * starting to render to it again).
335  */
336 struct v3d_job {
337         struct v3d_context *v3d;
338         struct v3d_cl bcl;
339         struct v3d_cl rcl;
340         struct v3d_cl indirect;
341         struct v3d_bo *tile_alloc;
342         struct v3d_bo *tile_state;
343 
344         struct drm_v3d_submit_cl submit;
345 
346         /**
347          * Set of all BOs referenced by the job.  This will be used for making
348          * the list of BOs that the kernel will need to have paged in to
349          * execute our job.
350          */
351         struct set *bos;
352 
353         /** Sum of the sizes of the BOs referenced by the job. */
354         uint32_t referenced_size;
355 
356         struct set *write_prscs;
357         struct set *tf_write_prscs;
358 
359         /* Size of the submit.bo_handles array. */
360         uint32_t bo_handles_size;
361 
362         /** @{
363          * Surfaces to submit rendering for.
364          * For blit operations, bbuf is the source surface, and cbufs[0] is
365          * the destination surface.
366          */
367         uint32_t nr_cbufs;
368         struct pipe_surface *cbufs[V3D_MAX_DRAW_BUFFERS];
369         struct pipe_surface *zsbuf;
370         struct pipe_surface *bbuf;
371         /** @} */
372         /** @{
373          * Bounding box of the scissor across all queued drawing.
374          *
375          * Note that the max values are exclusive.
376          */
377         uint32_t draw_min_x;
378         uint32_t draw_min_y;
379         uint32_t draw_max_x;
380         uint32_t draw_max_y;
381 
382         /** @} */
383         /** @{
384          * List of scissor rects used for all queued drawing. All scissor
385          * rects will be contained in the draw_{min/max}_{x/y} bounding box.
386          *
387          * This is used as an optimization when all drawing is scissored to
388          * limit tile flushing only to tiles that intersect a scissor rect.
389          * If scissor is used together with non-scissored drawing, then
390          * the optimization is disabled.
391          */
392         struct {
393                 bool disabled;
394                 uint32_t count;
395                 struct {
396                         uint32_t min_x, min_y;
397                         uint32_t max_x, max_y;
398                 } rects[MAX_JOB_SCISSORS];
399         } scissor;
400 
401         /** @} */
402         /** @{
403          * Width/height of the color framebuffer being rendered to,
404          * for V3D_TILE_RENDERING_MODE_CONFIG.
405          */
406         uint32_t draw_width;
407         uint32_t draw_height;
408         uint32_t num_layers;
409 
410         /** @} */
411         /** @{ Tile information, depending on MSAA and float color buffer. */
412         uint32_t draw_tiles_x; /** @< Number of tiles wide for framebuffer. */
413         uint32_t draw_tiles_y; /** @< Number of tiles high for framebuffer. */
414 
415         uint32_t tile_width; /** @< Width of a tile. */
416         uint32_t tile_height; /** @< Height of a tile. */
417         /** maximum internal_bpp of all color render targets. */
418         uint32_t internal_bpp;
419 
420         /** Whether the current rendering is in a 4X MSAA tile buffer. */
421         bool msaa;
422         /** @} */
423 
424         /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
425          * first rendering.
426          */
427         uint32_t clear;
428         /* Bitmask of PIPE_CLEAR_* of buffers that have been read by a draw
429          * call without having been cleared first.
430          */
431         uint32_t load;
432         /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
433          * (either clears or draws) and should be stored.
434          */
435         uint32_t store;
436         uint32_t clear_color[V3D_MAX_DRAW_BUFFERS][4];
437         float clear_z;
438         uint8_t clear_s;
439 
440         /* If TLB double-buffering is enabled for this job */
441         bool double_buffer;
442 
443         /**
444          * Set if some drawing (triangles, blits, or just a glClear()) has
445          * been done to the FBO, meaning that we need to
446          * DRM_IOCTL_V3D_SUBMIT_CL.
447          */
448         bool needs_flush;
449 
450         /* Set if any shader has dirtied cachelines in the TMU that need to be
451          * flushed before job end.
452          */
453         bool tmu_dirty_rcl;
454 
455         /**
456          * Set if a packet enabling TF has been emitted in the job (V3D 4.x).
457          */
458         bool tf_enabled;
459 
460         bool needs_primitives_generated;
461 
462         /**
463          * Current EZ state for drawing. Updated at the start of draw after
464          * we've decided on the shader being rendered.
465          */
466         enum v3d_ez_state ez_state;
467         /**
468          * The first EZ state that was used for drawing with a decided EZ
469          * direction (so either UNDECIDED, GT, or LT).
470          */
471         enum v3d_ez_state first_ez_state;
472 
473         /**
474          * Number of draw calls (not counting full buffer clears) queued in
475          * the current job.
476          */
477         uint32_t draw_calls_queued;
478 
479         /**
480          * Number of draw calls (not counting full buffer clears) queued in
481          * the current job during active transform feedback.
482          */
483         uint32_t tf_draw_calls_queued;
484 
485         struct v3d_job_key key;
486 };
487 
488 struct v3d_context {
489         struct pipe_context base;
490 
491         int fd;
492         struct v3d_screen *screen;
493 
494         /** The 3D rendering job for the currently bound FBO. */
495         struct v3d_job *job;
496 
497         /* Map from struct v3d_job_key to the job for that FBO.
498          */
499         struct hash_table *jobs;
500 
501         /**
502          * Map from v3d_resource to a job writing to that resource.
503          *
504          * Primarily for flushing jobs rendering to textures that are now
505          * being read from.
506          */
507         struct hash_table *write_jobs;
508 
509         struct slab_child_pool transfer_pool;
510         struct blitter_context *blitter;
511 
512         /** bitfield of V3D_DIRTY_* */
513         uint64_t dirty;
514 
515         uint32_t next_uncompiled_program_id;
516         uint64_t next_compiled_program_id;
517 
518         struct v3d_compiler_state *compiler_state;
519 
520         uint8_t prim_mode;
521 
522         /** Maximum index buffer valid for the current shader_rec. */
523         uint32_t max_index;
524 
525         /** Sync object that our RCL or TFU job will update as its out_sync. */
526         uint32_t out_sync;
527 
528         /* Stream uploader used by gallium internals.  This could also be used
529          * by driver internals, but we tend to use the v3d_cl.h interfaces
530          * instead.
531          */
532         struct u_upload_mgr *uploader;
533         /* State uploader used inside the driver.  This is for packing bits of
534          * long-term state inside buffers, since the kernel interfaces
535          * allocate a page at a time.
536          */
537         struct u_upload_mgr *state_uploader;
538 
539         struct pipe_shader_state *sand8_blit_vs;
540         struct pipe_shader_state *sand8_blit_fs_luma;
541         struct pipe_shader_state *sand8_blit_fs_chroma;
542 
543         /** @{ Current pipeline state objects */
544         struct pipe_scissor_state scissor;
545         struct v3d_blend_state *blend;
546         struct v3d_rasterizer_state *rasterizer;
547         struct v3d_depth_stencil_alpha_state *zsa;
548 
549         struct v3d_program_stateobj prog;
550         uint32_t compute_num_workgroups[3];
551         struct v3d_bo *compute_shared_memory;
552 
553         struct v3d_vertex_stateobj *vtx;
554 
555         struct {
556                 struct pipe_blend_color f;
557                 uint16_t hf[4];
558         } blend_color;
559         struct pipe_stencil_ref stencil_ref;
560         unsigned sample_mask;
561         struct pipe_framebuffer_state framebuffer;
562 
563         /* Per render target, whether we should swap the R and B fields in the
564          * shader's color output and in blending.  If render targets disagree
565          * on the R/B swap and use the constant color, then we would need to
566          * fall back to in-shader blending.
567          */
568         uint8_t swap_color_rb;
569 
570         /* Per render target, whether we should treat the dst alpha values as
571          * one in blending.
572          *
573          * For RGBX formats, the tile buffer's alpha channel will be
574          * undefined.
575          */
576         uint8_t blend_dst_alpha_one;
577 
578         bool active_queries;
579 
580         /**
581          * If a compute job writes a resource read by a non-compute stage we
582          * should sync on the last compute job.
583          */
584         bool sync_on_last_compute_job;
585 
586         uint32_t tf_prims_generated;
587         uint32_t prims_generated;
588 
589         uint32_t n_primitives_generated_queries_in_flight;
590 
591         struct pipe_poly_stipple stipple;
592         struct pipe_clip_state clip;
593         struct pipe_viewport_state viewport;
594         struct v3d_ssbo_stateobj ssbo[PIPE_SHADER_TYPES];
595         struct v3d_shaderimg_stateobj shaderimg[PIPE_SHADER_TYPES];
596         struct v3d_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
597         struct v3d_texture_stateobj tex[PIPE_SHADER_TYPES];
598         struct v3d_vertexbuf_stateobj vertexbuf;
599         struct v3d_streamout_stateobj streamout;
600         struct v3d_bo *current_oq;
601         struct pipe_resource *prim_counts;
602         uint32_t prim_counts_offset;
603         struct pipe_debug_callback debug;
604         struct v3d_perfmon_state *active_perfmon;
605         struct v3d_perfmon_state *last_perfmon;
606         /** @} */
607 };
608 
609 struct v3d_rasterizer_state {
610         struct pipe_rasterizer_state base;
611 
612         float point_size;
613 
614         uint8_t depth_offset[9];
615         uint8_t depth_offset_z16[9];
616 };
617 
618 struct v3d_depth_stencil_alpha_state {
619         struct pipe_depth_stencil_alpha_state base;
620 
621         enum v3d_ez_state ez_state;
622 
623         uint8_t stencil_front[6];
624         uint8_t stencil_back[6];
625 };
626 
627 struct v3d_blend_state {
628         struct pipe_blend_state base;
629 
630         /* Per-RT mask of whether blending is enabled. */
631         uint8_t blend_enables;
632 };
633 
634 #define perf_debug(...) do {                            \
635         if (unlikely(V3D_DEBUG & V3D_DEBUG_PERF))       \
636                 fprintf(stderr, __VA_ARGS__);           \
637         if (unlikely(v3d->debug.debug_message))         \
638                 pipe_debug_message(&v3d->debug, PERF_INFO, __VA_ARGS__);    \
639 } while (0)
640 
641 static inline struct v3d_context *
v3d_context(struct pipe_context * pcontext)642 v3d_context(struct pipe_context *pcontext)
643 {
644         return (struct v3d_context *)pcontext;
645 }
646 
647 static inline struct v3d_sampler_view *
v3d_sampler_view(struct pipe_sampler_view * psview)648 v3d_sampler_view(struct pipe_sampler_view *psview)
649 {
650         return (struct v3d_sampler_view *)psview;
651 }
652 
653 static inline struct v3d_sampler_state *
v3d_sampler_state(struct pipe_sampler_state * psampler)654 v3d_sampler_state(struct pipe_sampler_state *psampler)
655 {
656         return (struct v3d_sampler_state *)psampler;
657 }
658 
659 static inline struct v3d_stream_output_target *
v3d_stream_output_target(struct pipe_stream_output_target * ptarget)660 v3d_stream_output_target(struct pipe_stream_output_target *ptarget)
661 {
662         return (struct v3d_stream_output_target *)ptarget;
663 }
664 
665 static inline uint32_t
v3d_stream_output_target_get_vertex_count(struct pipe_stream_output_target * ptarget)666 v3d_stream_output_target_get_vertex_count(struct pipe_stream_output_target *ptarget)
667 {
668     return v3d_stream_output_target(ptarget)->recorded_vertex_count;
669 }
670 
671 int v3d_get_driver_query_group_info(struct pipe_screen *pscreen,
672                                     unsigned index,
673                                     struct pipe_driver_query_group_info *info);
674 int v3d_get_driver_query_info(struct pipe_screen *pscreen, unsigned index,
675                               struct pipe_driver_query_info *info);
676 
677 struct pipe_context *v3d_context_create(struct pipe_screen *pscreen,
678                                         void *priv, unsigned flags);
679 void v3d_program_init(struct pipe_context *pctx);
680 void v3d_program_fini(struct pipe_context *pctx);
681 void v3d_query_init(struct pipe_context *pctx);
682 
683 static inline int
v3d_ioctl(int fd,unsigned long request,void * arg)684 v3d_ioctl(int fd, unsigned long request, void *arg)
685 {
686         if (using_v3d_simulator)
687                 return v3d_simulator_ioctl(fd, request, arg);
688         else
689                 return drmIoctl(fd, request, arg);
690 }
691 
692 static inline bool
v3d_transform_feedback_enabled(struct v3d_context * v3d)693 v3d_transform_feedback_enabled(struct v3d_context *v3d)
694 {
695         return (v3d->prog.bind_vs->num_tf_specs != 0 ||
696                 (v3d->prog.bind_gs && v3d->prog.bind_gs->num_tf_specs != 0)) &&
697                v3d->active_queries;
698 }
699 
700 void v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader *shader);
701 struct v3d_cl_reloc v3d_write_uniforms(struct v3d_context *v3d,
702                                        struct v3d_job *job,
703                                        struct v3d_compiled_shader *shader,
704                                        enum pipe_shader_type stage);
705 
706 void v3d_flush(struct pipe_context *pctx);
707 void v3d_job_init(struct v3d_context *v3d);
708 struct v3d_job *v3d_job_create(struct v3d_context *v3d);
709 void v3d_job_free(struct v3d_context *v3d, struct v3d_job *job);
710 struct v3d_job *v3d_get_job(struct v3d_context *v3d,
711                             uint32_t nr_cbufs,
712                             struct pipe_surface **cbufs,
713                             struct pipe_surface *zsbuf,
714                             struct pipe_surface *bbuf);
715 struct v3d_job *v3d_get_job_for_fbo(struct v3d_context *v3d);
716 void v3d_job_add_bo(struct v3d_job *job, struct v3d_bo *bo);
717 void v3d_job_add_write_resource(struct v3d_job *job, struct pipe_resource *prsc);
718 void v3d_job_add_tf_write_resource(struct v3d_job *job, struct pipe_resource *prsc);
719 void v3d_job_submit(struct v3d_context *v3d, struct v3d_job *job);
720 void v3d_flush_jobs_using_bo(struct v3d_context *v3d, struct v3d_bo *bo);
721 void v3d_flush_jobs_writing_resource(struct v3d_context *v3d,
722                                      struct pipe_resource *prsc,
723                                      enum v3d_flush_cond flush_cond,
724                                      bool is_compute_pipeline);
725 void v3d_flush_jobs_reading_resource(struct v3d_context *v3d,
726                                      struct pipe_resource *prsc,
727                                      enum v3d_flush_cond flush_cond,
728                                      bool is_compute_pipeline);
729 void v3d_update_compiled_shaders(struct v3d_context *v3d, uint8_t prim_mode);
730 void v3d_update_compiled_cs(struct v3d_context *v3d);
731 
732 bool v3d_rt_format_supported(const struct v3d_device_info *devinfo,
733                              enum pipe_format f);
734 bool v3d_tex_format_supported(const struct v3d_device_info *devinfo,
735                               enum pipe_format f);
736 uint8_t v3d_get_rt_format(const struct v3d_device_info *devinfo, enum pipe_format f);
737 uint8_t v3d_get_tex_format(const struct v3d_device_info *devinfo, enum pipe_format f);
738 uint8_t v3d_get_tex_return_size(const struct v3d_device_info *devinfo,
739                                 enum pipe_format f,
740                                 enum pipe_tex_compare compare);
741 uint8_t v3d_get_tex_return_channels(const struct v3d_device_info *devinfo,
742                                     enum pipe_format f);
743 const uint8_t *v3d_get_format_swizzle(const struct v3d_device_info *devinfo,
744                                       enum pipe_format f);
745 void v3d_get_internal_type_bpp_for_output_format(const struct v3d_device_info *devinfo,
746                                                  uint32_t format,
747                                                  uint32_t *type,
748                                                  uint32_t *bpp);
749 bool v3d_tfu_supports_tex_format(const struct v3d_device_info *devinfo,
750                                  uint32_t tex_format,
751                                  bool for_mipmap);
752 bool v3d_format_supports_tlb_msaa_resolve(const struct v3d_device_info *devinfo,
753                                           enum pipe_format f);
754 
755 void v3d_init_query_functions(struct v3d_context *v3d);
756 void v3d_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info);
757 void v3d_blitter_save(struct v3d_context *v3d);
758 bool v3d_generate_mipmap(struct pipe_context *pctx,
759                          struct pipe_resource *prsc,
760                          enum pipe_format format,
761                          unsigned int base_level,
762                          unsigned int last_level,
763                          unsigned int first_layer,
764                          unsigned int last_layer);
765 
766 void
767 v3d_fence_unreference(struct v3d_fence **fence);
768 
769 struct v3d_fence *v3d_fence_create(struct v3d_context *v3d);
770 
771 bool v3d_fence_wait(struct v3d_screen *screen,
772                     struct v3d_fence *fence,
773                     uint64_t timeout_ns);
774 
775 void v3d_update_primitive_counters(struct v3d_context *v3d);
776 
777 bool v3d_line_smoothing_enabled(struct v3d_context *v3d);
778 
779 float v3d_get_real_line_width(struct v3d_context *v3d);
780 
781 void v3d_ensure_prim_counts_allocated(struct v3d_context *ctx);
782 
783 void v3d_flag_dirty_sampler_state(struct v3d_context *v3d,
784                                   enum pipe_shader_type shader);
785 
786 void v3d_create_texture_shader_state_bo(struct v3d_context *v3d,
787                                         struct v3d_sampler_view *so);
788 
789 void v3d_get_tile_buffer_size(bool is_msaa,
790                               bool double_buffer,
791                               uint32_t nr_cbufs,
792                               struct pipe_surface **cbufs,
793                               struct pipe_surface *bbuf,
794                               uint32_t *tile_width,
795                               uint32_t *tile_height,
796                               uint32_t *max_bpp);
797 
798 #ifdef ENABLE_SHADER_CACHE
799 struct v3d_compiled_shader *v3d_disk_cache_retrieve(struct v3d_context *v3d,
800                                                     const struct v3d_key *key);
801 
802 void v3d_disk_cache_store(struct v3d_context *v3d,
803                           const struct v3d_key *key,
804                           const struct v3d_compiled_shader *shader,
805                           uint64_t *qpu_insts,
806                           uint32_t qpu_size);
807 #endif /* ENABLE_SHADER_CACHE */
808 
809 #ifdef v3dX
810 #  include "v3dx_context.h"
811 #else
812 #  define v3dX(x) v3d33_##x
813 #  include "v3dx_context.h"
814 #  undef v3dX
815 
816 #  define v3dX(x) v3d41_##x
817 #  include "v3dx_context.h"
818 #  undef v3dX
819 #endif
820 
821 #endif /* V3D_CONTEXT_H */
822