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 
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 
34 struct lima_context_framebuffer {
35    struct pipe_framebuffer_state base;
36    int tiled_w, tiled_h;
37    int shift_w, shift_h;
38    int block_w, block_h;
39    int shift_min;
40 };
41 
42 struct lima_depth_stencil_alpha_state {
43    struct pipe_depth_stencil_alpha_state base;
44 };
45 
46 struct lima_fs_compiled_shader {
47    struct lima_bo *bo;
48    void *shader;
49    struct {
50       int shader_size;
51       int stack_size;
52       bool uses_discard;
53    } state;
54 };
55 
56 struct lima_fs_uncompiled_shader {
57    struct pipe_shader_state base;
58    unsigned char nir_sha1[20];
59 };
60 
61 struct lima_fs_key {
62    unsigned char nir_sha1[20];
63    struct {
64       uint8_t swizzle[4];
65    } tex[PIPE_MAX_SAMPLERS];
66 };
67 
68 #define LIMA_MAX_VARYING_NUM 13
69 
70 struct lima_varying_info {
71    int components;
72    int component_size;
73    int offset;
74 };
75 
76 struct lima_vs_compiled_shader {
77    struct lima_bo *bo;
78    void *shader;
79    void *constant;
80    struct {
81       int shader_size;
82       int prefetch;
83       int uniform_size;
84       int constant_size;
85       struct lima_varying_info varying[LIMA_MAX_VARYING_NUM];
86       int varying_stride;
87       int num_outputs;
88       int num_varyings;
89       int gl_pos_idx;
90       int point_size_idx;
91    } state;
92 };
93 
94 struct lima_vs_uncompiled_shader {
95    struct pipe_shader_state base;
96    unsigned char nir_sha1[20];
97 };
98 
99 struct lima_vs_key {
100    unsigned char nir_sha1[20];
101 };
102 
103 struct lima_rasterizer_state {
104    struct pipe_rasterizer_state base;
105 };
106 
107 struct lima_blend_state {
108    struct pipe_blend_state base;
109 };
110 
111 struct lima_vertex_element_state {
112    struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
113    unsigned num_elements;
114 };
115 
116 struct lima_context_vertex_buffer {
117    struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
118    unsigned count;
119    uint32_t enabled_mask;
120 };
121 
122 struct lima_context_viewport_state {
123    struct pipe_viewport_state transform;
124    float left, right, bottom, top;
125    float near, far;
126 };
127 
128 struct lima_context_constant_buffer {
129    const void *buffer;
130    uint32_t size;
131    bool dirty;
132 };
133 
134 enum lima_ctx_buff {
135    lima_ctx_buff_gp_varying_info,
136    lima_ctx_buff_gp_attribute_info,
137    lima_ctx_buff_gp_uniform,
138    lima_ctx_buff_pp_plb_rsw,
139    lima_ctx_buff_pp_uniform_array,
140    lima_ctx_buff_pp_uniform,
141    lima_ctx_buff_pp_tex_desc,
142    lima_ctx_buff_num,
143    lima_ctx_buff_num_gp = lima_ctx_buff_pp_plb_rsw,
144 };
145 
146 struct lima_ctx_buff_state {
147    struct pipe_resource *res;
148    unsigned offset;
149    unsigned size;
150 };
151 
152 struct lima_texture_stateobj {
153    struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
154    unsigned num_textures;
155    struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
156    unsigned num_samplers;
157 };
158 
159 struct lima_ctx_plb_pp_stream_key {
160    uint16_t plb_index;
161    /* Coordinates are in tiles */
162    uint16_t minx, miny, maxx, maxy;
163    /* FB params */
164    uint16_t shift_w, shift_h;
165    uint16_t block_w, block_h;
166 };
167 
168 struct lima_ctx_plb_pp_stream {
169    struct list_head lru_list;
170    struct lima_ctx_plb_pp_stream_key key;
171    struct lima_bo *bo;
172    uint32_t offset[8];
173 };
174 
175 struct lima_pp_stream_state {
176    void *map;
177    uint32_t va;
178    uint32_t offset[8];
179 };
180 
181 struct lima_context {
182    struct pipe_context base;
183 
184    enum {
185       LIMA_CONTEXT_DIRTY_FRAMEBUFFER  = (1 << 0),
186       LIMA_CONTEXT_DIRTY_CLEAR        = (1 << 1),
187       LIMA_CONTEXT_DIRTY_COMPILED_VS  = (1 << 2),
188       LIMA_CONTEXT_DIRTY_COMPILED_FS  = (1 << 3),
189       LIMA_CONTEXT_DIRTY_VERTEX_ELEM  = (1 << 4),
190       LIMA_CONTEXT_DIRTY_VERTEX_BUFF  = (1 << 5),
191       LIMA_CONTEXT_DIRTY_VIEWPORT     = (1 << 6),
192       LIMA_CONTEXT_DIRTY_SCISSOR      = (1 << 7),
193       LIMA_CONTEXT_DIRTY_RASTERIZER   = (1 << 8),
194       LIMA_CONTEXT_DIRTY_ZSA          = (1 << 9),
195       LIMA_CONTEXT_DIRTY_BLEND_COLOR  = (1 << 10),
196       LIMA_CONTEXT_DIRTY_BLEND        = (1 << 11),
197       LIMA_CONTEXT_DIRTY_STENCIL_REF  = (1 << 12),
198       LIMA_CONTEXT_DIRTY_CONST_BUFF   = (1 << 13),
199       LIMA_CONTEXT_DIRTY_TEXTURES     = (1 << 14),
200       LIMA_CONTEXT_DIRTY_CLIP         = (1 << 15),
201       LIMA_CONTEXT_DIRTY_UNCOMPILED_VS = (1 << 16),
202       LIMA_CONTEXT_DIRTY_UNCOMPILED_FS = (1 << 17),
203    } dirty;
204 
205    struct u_upload_mgr *uploader;
206    struct blitter_context *blitter;
207 
208    struct slab_child_pool transfer_pool;
209 
210    struct lima_context_framebuffer framebuffer;
211    struct lima_context_viewport_state viewport;
212    struct pipe_scissor_state scissor;
213    struct pipe_scissor_state clipped_scissor;
214    struct lima_vs_compiled_shader *vs;
215    struct lima_fs_compiled_shader *fs;
216    struct lima_vs_uncompiled_shader *uncomp_vs;
217    struct lima_fs_uncompiled_shader *uncomp_fs;
218    struct lima_vertex_element_state *vertex_elements;
219    struct lima_context_vertex_buffer vertex_buffers;
220    struct lima_rasterizer_state *rasterizer;
221    struct lima_depth_stencil_alpha_state *zsa;
222    struct pipe_blend_color blend_color;
223    struct lima_blend_state *blend;
224    struct pipe_stencil_ref stencil_ref;
225    struct pipe_clip_state clip;
226    struct lima_context_constant_buffer const_buffer[PIPE_SHADER_TYPES];
227    struct lima_texture_stateobj tex_stateobj;
228    struct lima_pp_stream_state pp_stream;
229 
230    unsigned min_index;
231    unsigned max_index;
232 
233    #define LIMA_CTX_PLB_MIN_NUM  1
234    #define LIMA_CTX_PLB_MAX_NUM  4
235    #define LIMA_CTX_PLB_DEF_NUM  2
236    #define LIMA_CTX_PLB_BLK_SIZE 512
237    unsigned plb_size;
238    unsigned plb_gp_size;
239 
240    struct lima_bo *plb[LIMA_CTX_PLB_MAX_NUM];
241    struct lima_bo *gp_tile_heap[LIMA_CTX_PLB_MAX_NUM];
242    uint32_t gp_tile_heap_size;
243    struct lima_bo *plb_gp_stream;
244    struct lima_bo *gp_output;
245    uint32_t gp_output_varyings_offt;
246    uint32_t gp_output_point_size_offt;
247 
248    struct hash_table *plb_pp_stream;
249    struct list_head plb_pp_stream_lru_list;
250    uint32_t plb_index;
251    size_t plb_stream_cache_size;
252 
253    struct hash_table *fs_cache;
254    struct hash_table *vs_cache;
255 
256    struct lima_ctx_buff_state buffer_state[lima_ctx_buff_num];
257 
258    /* current job */
259    struct lima_job *job;
260 
261    /* map from lima_job_key to lima_job */
262    struct hash_table *jobs;
263 
264    /* map from pipe_resource to lima_job which write to it */
265    struct hash_table *write_jobs;
266 
267    int in_sync_fd;
268    uint32_t in_sync[2];
269    uint32_t out_sync[2];
270 
271    int id;
272 
273    struct pipe_debug_callback debug;
274 
275    unsigned index_offset;
276    struct lima_resource *index_res;
277 };
278 
279 static inline struct lima_context *
lima_context(struct pipe_context * pctx)280 lima_context(struct pipe_context *pctx)
281 {
282    return (struct lima_context *)pctx;
283 }
284 
285 struct lima_sampler_state {
286    struct pipe_sampler_state base;
287 };
288 
289 static inline struct lima_sampler_state *
lima_sampler_state(struct pipe_sampler_state * psstate)290 lima_sampler_state(struct pipe_sampler_state *psstate)
291 {
292    return (struct lima_sampler_state *)psstate;
293 }
294 
295 struct lima_sampler_view {
296    struct pipe_sampler_view base;
297    uint8_t swizzle[4];
298 };
299 
300 static inline struct lima_sampler_view *
lima_sampler_view(struct pipe_sampler_view * psview)301 lima_sampler_view(struct pipe_sampler_view *psview)
302 {
303    return (struct lima_sampler_view *)psview;
304 }
305 
306 uint32_t lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff);
307 void *lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff);
308 void *lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
309                           unsigned size);
310 
311 void lima_state_init(struct lima_context *ctx);
312 void lima_state_fini(struct lima_context *ctx);
313 void lima_draw_init(struct lima_context *ctx);
314 void lima_program_init(struct lima_context *ctx);
315 void lima_program_fini(struct lima_context *ctx);
316 void lima_query_init(struct lima_context *ctx);
317 
318 struct pipe_context *
319 lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);
320 
321 void lima_flush(struct lima_context *ctx);
322 void lima_flush_job_accessing_bo(
323    struct lima_context *ctx, struct lima_bo *bo, bool write);
324 void lima_flush_previous_job_writing_resource(
325    struct lima_context *ctx, struct pipe_resource *prsc);
326 
327 #endif
328