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
35 struct 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
43 struct lima_depth_stencil_alpha_state {
44 struct pipe_depth_stencil_alpha_state base;
45 };
46
47 struct 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
60 struct lima_fs_uncompiled_shader {
61 struct pipe_shader_state base;
62 unsigned char nir_sha1[20];
63 };
64
65 struct 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
74 struct lima_varying_info {
75 int components;
76 int component_size;
77 int offset;
78 };
79
80 struct 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
98 struct lima_vs_uncompiled_shader {
99 struct pipe_shader_state base;
100 unsigned char nir_sha1[20];
101 };
102
103 struct lima_vs_key {
104 unsigned char nir_sha1[20];
105 };
106
107 struct lima_rasterizer_state {
108 struct pipe_rasterizer_state base;
109 };
110
111 struct lima_blend_state {
112 struct pipe_blend_state base;
113 };
114
115 struct lima_vertex_element_state {
116 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
117 unsigned num_elements;
118 };
119
120 struct lima_context_vertex_buffer {
121 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
122 unsigned count;
123 uint32_t enabled_mask;
124 };
125
126 struct lima_context_viewport_state {
127 struct pipe_viewport_state transform;
128 float left, right, bottom, top;
129 float near, far;
130 };
131
132 struct lima_context_constant_buffer {
133 const void *buffer;
134 uint32_t size;
135 bool dirty;
136 };
137
138 enum 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
150 struct lima_ctx_buff_state {
151 struct pipe_resource *res;
152 unsigned offset;
153 unsigned size;
154 };
155
156 struct 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
163 struct 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
172 struct 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
179 struct lima_pp_stream_state {
180 void *map;
181 uint32_t va;
182 uint32_t offset[8];
183 };
184
185 struct 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
289 static inline struct lima_context *
lima_context(struct pipe_context * pctx)290 lima_context(struct pipe_context *pctx)
291 {
292 return (struct lima_context *)pctx;
293 }
294
295 struct lima_sampler_state {
296 struct pipe_sampler_state base;
297 };
298
299 static inline struct lima_sampler_state *
lima_sampler_state(struct pipe_sampler_state * psstate)300 lima_sampler_state(struct pipe_sampler_state *psstate)
301 {
302 return (struct lima_sampler_state *)psstate;
303 }
304
305 struct lima_sampler_view {
306 struct pipe_sampler_view base;
307 uint8_t swizzle[4];
308 };
309
310 static inline struct lima_sampler_view *
lima_sampler_view(struct pipe_sampler_view * psview)311 lima_sampler_view(struct pipe_sampler_view *psview)
312 {
313 return (struct lima_sampler_view *)psview;
314 }
315
316 uint32_t lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff);
317 void *lima_ctx_buff_map(struct lima_context *ctx, enum lima_ctx_buff buff);
318 void *lima_ctx_buff_alloc(struct lima_context *ctx, enum lima_ctx_buff buff,
319 unsigned size);
320
321 void lima_state_init(struct lima_context *ctx);
322 void lima_state_fini(struct lima_context *ctx);
323 void lima_draw_init(struct lima_context *ctx);
324 void lima_program_init(struct lima_context *ctx);
325 void lima_program_fini(struct lima_context *ctx);
326 void lima_query_init(struct lima_context *ctx);
327
328 struct pipe_context *
329 lima_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);
330
331 void lima_flush(struct lima_context *ctx);
332 void lima_flush_job_accessing_bo(
333 struct lima_context *ctx, struct lima_bo *bo, bool write);
334 void lima_flush_previous_job_writing_resource(
335 struct lima_context *ctx, struct pipe_resource *prsc);
336
337 #endif
338