gallium: add lima driver
[mesa.git] / src / gallium / drivers / lima / lima_context.h
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/slab.h"
29 #include "util/u_dynarray.h"
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33
34 struct lima_context_framebuffer {
35 struct pipe_surface *cbuf, *zsbuf;
36 int width, height;
37 int tiled_w, tiled_h;
38 int shift_w, shift_h;
39 int block_w, block_h;
40 int shift_min;
41 int samples;
42 };
43
44 struct lima_context_clear {
45 unsigned buffers;
46 uint32_t color_8pc;
47 uint32_t depth;
48 uint32_t stencil;
49 uint64_t color_16pc;
50 };
51
52 struct lima_depth_stencil_alpha_state {
53 struct pipe_depth_stencil_alpha_state base;
54 };
55
56 struct lima_fs_shader_state {
57 void *shader;
58 int shader_size;
59 int stack_size;
60 struct lima_bo *bo;
61 };
62
63 #define LIMA_MAX_VARYING_NUM 13
64
65 struct lima_varying_info {
66 int components;
67 int component_size;
68 int offset;
69 };
70
71 struct lima_vs_shader_state {
72 void *shader;
73 int shader_size;
74 int prefetch;
75
76 /* pipe_constant_buffer.size is aligned with some pad bytes,
77 * so record here for the real start place of gpir lowered
78 * uniforms */
79 int uniform_pending_offset;
80
81 void *constant;
82 int constant_size;
83
84 struct lima_varying_info varying[LIMA_MAX_VARYING_NUM];
85 int varying_stride;
86 int num_varying;
87
88 struct lima_bo *bo;
89 };
90
91 struct lima_rasterizer_state {
92 struct pipe_rasterizer_state base;
93 };
94
95 struct lima_blend_state {
96 struct pipe_blend_state base;
97 };
98
99 struct lima_vertex_element_state {
100 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
101 unsigned num_elements;
102 };
103
104 struct lima_context_vertex_buffer {
105 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
106 unsigned count;
107 uint32_t enabled_mask;
108 };
109
110 struct lima_context_viewport_state {
111 struct pipe_viewport_state transform;
112 float x, y, width, height;
113 float near, far;
114 };
115
116 struct lima_context_constant_buffer {
117 const void *buffer;
118 uint32_t size;
119 bool dirty;
120 };
121
122 enum lima_ctx_buff {
123 lima_ctx_buff_sh_varying,
124 lima_ctx_buff_sh_gl_pos,
125 lima_ctx_buff_gp_varying_info,
126 lima_ctx_buff_gp_attribute_info,
127 lima_ctx_buff_gp_uniform,
128 lima_ctx_buff_gp_vs_cmd,
129 lima_ctx_buff_gp_plbu_cmd,
130 lima_ctx_buff_pp_plb_rsw,
131 lima_ctx_buff_pp_uniform_array,
132 lima_ctx_buff_pp_uniform,
133 lima_ctx_buff_pp_tex_desc,
134 lima_ctx_buff_num,
135 };
136
137 struct lima_ctx_buff_state {
138 struct pipe_resource *res;
139 unsigned offset;
140 unsigned size;
141 };
142
143 struct lima_texture_stateobj {
144 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
145 unsigned num_textures;
146 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
147 unsigned num_samplers;
148 };
149
150 struct lima_ctx_plb_pp_stream_key {
151 uint32_t plb_index;
152 uint32_t tiled_w;
153 uint32_t tiled_h;
154 };
155
156 struct lima_ctx_plb_pp_stream {
157 struct lima_ctx_plb_pp_stream_key key;
158 uint32_t refcnt;
159 struct lima_bo *bo;
160 uint32_t offset[4];
161 };
162
163 struct lima_damage_state {
164 struct pipe_scissor_state *region;
165 unsigned num_region;
166 bool aligned;
167 };
168
169 struct lima_pp_stream_state {
170 struct lima_bo *bo;
171 uint32_t bo_offset;
172 uint32_t offset[8];
173 };
174
175 struct lima_context {
176 struct pipe_context base;
177
178 enum {
179 LIMA_CONTEXT_DIRTY_FRAMEBUFFER = (1 << 0),
180 LIMA_CONTEXT_DIRTY_CLEAR = (1 << 1),
181 LIMA_CONTEXT_DIRTY_SHADER_VERT = (1 << 2),
182 LIMA_CONTEXT_DIRTY_SHADER_FRAG = (1 << 3),
183 LIMA_CONTEXT_DIRTY_VERTEX_ELEM = (1 << 4),
184 LIMA_CONTEXT_DIRTY_VERTEX_BUFF = (1 << 5),
185 LIMA_CONTEXT_DIRTY_VIEWPORT = (1 << 6),
186 LIMA_CONTEXT_DIRTY_SCISSOR = (1 << 7),
187 LIMA_CONTEXT_DIRTY_RASTERIZER = (1 << 8),
188 LIMA_CONTEXT_DIRTY_ZSA = (1 << 9),
189 LIMA_CONTEXT_DIRTY_BLEND_COLOR = (1 << 10),
190 LIMA_CONTEXT_DIRTY_BLEND = (1 << 11),
191 LIMA_CONTEXT_DIRTY_STENCIL_REF = (1 << 12),
192 LIMA_CONTEXT_DIRTY_CONST_BUFF = (1 << 13),
193 LIMA_CONTEXT_DIRTY_TEXTURES = (1 << 14),
194 } dirty;
195
196 struct u_upload_mgr *uploader;
197 struct u_suballocator *suballocator;
198
199 struct slab_child_pool transfer_pool;
200
201 struct lima_context_framebuffer framebuffer;
202 struct lima_context_viewport_state viewport;
203 struct pipe_scissor_state scissor;
204 struct lima_context_clear clear;
205 struct lima_vs_shader_state *vs;
206 struct lima_fs_shader_state *fs;
207 struct lima_vertex_element_state *vertex_elements;
208 struct lima_context_vertex_buffer vertex_buffers;
209 struct lima_rasterizer_state *rasterizer;
210 struct lima_depth_stencil_alpha_state *zsa;
211 struct pipe_blend_color blend_color;
212 struct lima_blend_state *blend;
213 struct pipe_stencil_ref stencil_ref;
214 struct lima_context_constant_buffer const_buffer[PIPE_SHADER_TYPES];
215 struct lima_texture_stateobj tex_stateobj;
216 struct lima_damage_state damage;
217 struct lima_pp_stream_state pp_stream;
218
219 unsigned min_index;
220 unsigned max_index;
221
222 #define LIMA_CTX_PLB_MIN_NUM 1
223 #define LIMA_CTX_PLB_MAX_NUM 4
224 #define LIMA_CTX_PLB_DEF_NUM 2
225 #define LIMA_CTX_PLB_BLK_SIZE 512
226 unsigned plb_max_blk;
227 unsigned plb_size;
228 unsigned plb_gp_size;
229
230 struct lima_bo *plb[LIMA_CTX_PLB_MAX_NUM];
231 struct lima_bo *plb_gp_stream;
232 struct hash_table *plb_pp_stream;
233 uint32_t plb_index;
234
235 struct lima_ctx_buff_state buffer_state[lima_ctx_buff_num];
236
237 struct util_dynarray vs_cmd_array;
238 struct util_dynarray plbu_cmd_array;
239
240 struct lima_submit *gp_submit;
241 struct lima_submit *pp_submit;
242
243 int id;
244 };
245
246 static inline struct lima_context *
247 lima_context(struct pipe_context *pctx)
248 {
249 return (struct lima_context *)pctx;
250 }
251
252 struct lima_sampler_state {
253 struct pipe_sampler_state base;
254 };
255
256 static inline struct lima_sampler_state *
257 lima_sampler_state(struct pipe_sampler_state *psstate)
258 {
259 return (struct lima_sampler_state *)psstate;
260 }
261
262 struct lima_sampler_view {
263 struct pipe_sampler_view base;
264 };
265
266 static inline struct lima_sampler_view *
267 lima_sampler_view(struct pipe_sampler_view *psview)
268 {
269 return (struct lima_sampler_view *)psview;
270 }
271
272 #define LIMA_CTX_BUFF_SUBMIT_GP (1 << 0)
273 #define LIMA_CTX_BUFF_SUBMIT_PP (1 << 1)
274
275 uint32_t lima_ctx_buff_va(struct lima_context *ctx, enum lima_ctx_buff buff,
276 unsigned submit);
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, bool uploader);
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
292 bool lima_need_flush(struct lima_context *ctx, struct lima_bo *bo, bool write);
293
294 #endif