1/*
2 * Copyright (c) 2017-2019 Lima Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#ifndef H_LIMA_CONTEXT
26#define H_LIMA_CONTEXT
27
28#include "util/list.h"
29#include "util/slab.h"
30#include "util/u_debug.h"
31
32#include "pipe/p_context.h"
33#include "pipe/p_state.h"
34
35struct lima_context_framebuffer {
36   struct pipe_framebuffer_state base;
37   int tiled_w, tiled_h;
38   int shift_w, shift_h;
39   int block_w, block_h;
40   int shift_min;
41};
42
43struct lima_depth_stencil_alpha_state {
44   struct pipe_depth_stencil_alpha_state base;
45};
46
47struct lima_fs_compiled_shader {
48   struct lima_bo *bo;
49   void *shader;
50   struct {
51      int shader_size;
52      int stack_size;
53      int frag_color0_reg;
54      int frag_color1_reg;
55      int frag_depth_reg;
56      bool uses_discard;
57   } state;
58};
59
60struct lima_fs_uncompiled_shader {
61   struct pipe_shader_state base;
62   unsigned char nir_sha1[20];
63};
64
65struct lima_fs_key {
66   unsigned char nir_sha1[20];
67   struct {
68      uint8_t swizzle[4];
69   } tex[PIPE_MAX_SAMPLERS];
70};
71
72#define LIMA_MAX_VARYING_NUM 13
73
74struct lima_varying_info {
75   int components;
76   int component_size;
77   int offset;
78};
79
80struct lima_vs_compiled_shader {
81   struct lima_bo *bo;
82   void *shader;
83   void *constant;
84   struct {
85      int shader_size;
86      int prefetch;
87      int uniform_size;
88      int constant_size;
89      struct lima_varying_info varying[LIMA_MAX_VARYING_NUM];
90      int varying_stride;
91      int num_outputs;
92      int num_varyings;
93      int gl_pos_idx;
94      int point_size_idx;
95   } state;
96};
97
98struct lima_vs_uncompiled_shader {
99   struct pipe_shader_state base;
100   unsigned char nir_sha1[20];
101};
102
103struct lima_vs_key {
104   unsigned char nir_sha1[20];
105};
106
107struct lima_rasterizer_state {
108   struct pipe_rasterizer_state base;
109};
110
111struct lima_blend_state {
112   struct pipe_blend_state base;
113};
114
115struct lima_vertex_element_state {
116   struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
117   unsigned num_elements;
118};
119
120struct lima_context_vertex_buffer {
121   struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
122   unsigned count;
123   uint32_t enabled_mask;
124};
125
126struct lima_context_viewport_state {
127   struct pipe_viewport_state transform;
128   float left, right, bottom, top;
129   float near, far;
130};
131
132struct lima_context_constant_buffer {
133   const void *buffer;
134   uint32_t size;
135   bool dirty;
136};
137
138enum lima_ctx_buff {
139   lima_ctx_buff_gp_varying_info,
140   lima_ctx_buff_gp_attribute_info,
141   lima_ctx_buff_gp_uniform,
142   lima_ctx_buff_pp_plb_rsw,
143   lima_ctx_buff_pp_uniform_array,
144   lima_ctx_buff_pp_uniform,
145   lima_ctx_buff_pp_tex_desc,
146   lima_ctx_buff_num,
147   lima_ctx_buff_num_gp = lima_ctx_buff_pp_plb_rsw,
148};
149
150struct lima_ctx_buff_state {
151   struct pipe_resource *res;
152   unsigned offset;
153   unsigned size;
154};
155
156struct lima_texture_stateobj {
157   struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
158   unsigned num_textures;
159   struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
160   unsigned num_samplers;
161};
162
163struct lima_ctx_plb_pp_stream_key {
164   uint16_t plb_index;
165   /* Coordinates are in tiles */
166   uint16_t minx, miny, maxx, maxy;
167   /* FB params */
168   uint16_t shift_w, shift_h;
169   uint16_t block_w, block_h;
170};
171
172struct lima_ctx_plb_pp_stream {
173   struct list_head lru_list;
174   struct lima_ctx_plb_pp_stream_key key;
175   struct lima_bo *bo;
176   uint32_t offset[8];
177};
178
179struct lima_pp_stream_state {
180   void *map;
181   uint32_t va;
182   uint32_t offset[8];
183};
184
185struct lima_context {
186   struct pipe_context base;
187
188   enum {
189      LIMA_CONTEXT_DIRTY_FRAMEBUFFER  = (1 << 0),
190      LIMA_CONTEXT_DIRTY_CLEAR        = (1 << 1),
191      LIMA_CONTEXT_DIRTY_COMPILED_VS  = (1 << 2),
192      LIMA_CONTEXT_DIRTY_COMPILED_FS  = (1 << 3),
193      LIMA_CONTEXT_DIRTY_VERTEX_ELEM  = (1 << 4),
194      LIMA_CONTEXT_DIRTY_VERTEX_BUFF  = (1 << 5),
195      LIMA_CONTEXT_DIRTY_VIEWPORT     = (1 << 6),
196      LIMA_CONTEXT_DIRTY_SCISSOR      = (1 << 7),
197      LIMA_CONTEXT_DIRTY_RASTERIZER   = (1 << 8),
198      LIMA_CONTEXT_DIRTY_ZSA          = (1 << 9),
199      LIMA_CONTEXT_DIRTY_BLEND_COLOR  = (1 << 10),
200      LIMA_CONTEXT_DIRTY_BLEND        = (1 << 11),
201      LIMA_CONTEXT_DIRTY_STENCIL_REF  = (1 << 12),
202      LIMA_CONTEXT_DIRTY_CONST_BUFF   = (1 << 13),
203      LIMA_CONTEXT_DIRTY_TEXTURES     = (1 << 14),
204      LIMA_CONTEXT_DIRTY_CLIP         = (1 << 15),
205      LIMA_CONTEXT_DIRTY_UNCOMPILED_VS = (1 << 16),
206      LIMA_CONTEXT_DIRTY_UNCOMPILED_FS = (1 << 17),
207      LIMA_CONTEXT_DIRTY_SAMPLE_MASK   = (1 << 18),
208   } dirty;
209
210   struct u_upload_mgr *uploader;
211   struct blitter_context *blitter;
212
213   struct slab_child_pool transfer_pool;
214
215   struct lima_context_framebuffer framebuffer;
216   struct lima_context_viewport_state viewport;
217   /* input for PLBU_CMD_VIEWPORT_* */
218   struct lima_context_viewport_state ext_viewport;
219   struct pipe_scissor_state scissor;
220   struct pipe_scissor_state clipped_scissor;
221   struct lima_vs_compiled_shader *vs;
222   struct lima_fs_compiled_shader *fs;
223   struct lima_vs_uncompiled_shader *uncomp_vs;
224   struct lima_fs_uncompiled_shader *uncomp_fs;
225   struct lima_vertex_element_state *vertex_elements;
226   struct lima_context_vertex_buffer vertex_buffers;
227   struct lima_rasterizer_state *rasterizer;
228   struct lima_depth_stencil_alpha_state *zsa;
229   struct pipe_blend_color blend_color;
230   struct lima_blend_state *blend;
231   struct pipe_stencil_ref stencil_ref;
232   struct pipe_clip_state clip;
233   struct lima_context_constant_buffer const_buffer[PIPE_SHADER_TYPES];
234   struct lima_texture_stateobj tex_stateobj;
235   struct lima_pp_stream_state pp_stream;
236
237   #define LIMA_MAX_SAMPLES 4
238   unsigned sample_mask;
239
240   unsigned min_index;
241   unsigned max_index;
242
243   #define LIMA_CTX_PLB_MIN_NUM  1
244   #define LIMA_CTX_PLB_MAX_NUM  4
245   #define LIMA_CTX_PLB_DEF_NUM  2
246   #define LIMA_CTX_PLB_BLK_SIZE 512
247   unsigned plb_size;
248   unsigned plb_gp_size;
249
250   struct lima_bo *plb[LIMA_CTX_PLB_MAX_NUM];
251   struct lima_bo *gp_tile_heap[LIMA_CTX_PLB_MAX_NUM];
252   uint32_t gp_tile_heap_size;
253   struct lima_bo *plb_gp_stream;
254   struct lima_bo *gp_output;
255   uint32_t gp_output_varyings_offt;
256   uint32_t gp_output_point_size_offt;
257
258   struct hash_table *plb_pp_stream;
259   struct list_head plb_pp_stream_lru_list;
260   uint32_t plb_index;
261   size_t plb_stream_cache_size;
262
263   struct hash_table *fs_cache;
264   struct hash_table *vs_cache;
265
266   struct lima_ctx_buff_state buffer_state[lima_ctx_buff_num];
267
268   /* current job */
269   struct lima_job *job;
270
271   /* map from lima_job_key to lima_job */
272   struct hash_table *jobs;
273
274   /* map from pipe_resource to lima_job which write to it */
275   struct hash_table *write_jobs;
276
277   int in_sync_fd;
278   uint32_t in_sync[2];
279   uint32_t out_sync[2];
280
281   int id;
282
283   struct util_debug_callback debug;
284
285   unsigned index_offset;
286   struct lima_resource *index_res;
287};
288
289static inline struct lima_context *
290lima_context(struct pipe_context *pctx)
291{
292   return (struct lima_context *)pctx;
293}
294
295struct lima_sampler_state {
296   struct pipe_sampler_state base;
297};
298
299static inline struct lima_sampler_state *
300lima_sampler_state(struct pipe_sampler_state *psstate)
301{
302   return (struct lima_sampler_state *)psstate;
303}
304
305struct lima_sampler_view {
306   struct pipe_sampler_view base;
307   uint8_t swizzle[4];
308};
309
310static inline struct lima_sampler_view *
311lima_sampler_view(struct pipe_sampler_view *psview)
312{
313   return (struct lima_sampler_view *)psview;
314}
315
316uint32_t lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff);
317void *lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff);
318void *lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
319                          unsigned size);
320
321void lima_state_init(struct lima_context *ctx);
322void lima_state_fini(struct lima_context *ctx);
323void lima_draw_init(struct lima_context *ctx);
324void lima_program_init(struct lima_context *ctx);
325void lima_program_fini(struct lima_context *ctx);
326void lima_query_init(struct lima_context *ctx);
327
328struct pipe_context *
329lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);
330
331void lima_flush(struct lima_context *ctx);
332void lima_flush_job_accessing_bo(
333   struct lima_context *ctx, struct lima_bo *bo, bool write);
334void lima_flush_previous_job_writing_resource(
335   struct lima_context *ctx, struct pipe_resource *prsc);
336
337#endif
338