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