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