3a63af8f2b00c503f40142fdc3036defc7ced118
[mesa.git] / src / gallium / drivers / vc4 / vc4_context.h
1 /*
2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #ifndef VC4_CONTEXT_H
26 #define VC4_CONTEXT_H
27
28 #include <stdio.h>
29
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "util/u_slab.h"
33
34 #define __user
35 #include "vc4_drm.h"
36 #include "vc4_bufmgr.h"
37 #include "vc4_resource.h"
38 #include "vc4_cl.h"
39 #include "vc4_qir.h"
40
41 #ifdef USE_VC4_SIMULATOR
42 #define using_vc4_simulator true
43 #else
44 #define using_vc4_simulator false
45 #endif
46
47 #define VC4_DIRTY_BLEND (1 << 0)
48 #define VC4_DIRTY_RASTERIZER (1 << 1)
49 #define VC4_DIRTY_ZSA (1 << 2)
50 #define VC4_DIRTY_FRAGTEX (1 << 3)
51 #define VC4_DIRTY_VERTTEX (1 << 4)
52 #define VC4_DIRTY_TEXSTATE (1 << 5)
53
54 #define VC4_DIRTY_BLEND_COLOR (1 << 7)
55 #define VC4_DIRTY_STENCIL_REF (1 << 8)
56 #define VC4_DIRTY_SAMPLE_MASK (1 << 9)
57 #define VC4_DIRTY_FRAMEBUFFER (1 << 10)
58 #define VC4_DIRTY_STIPPLE (1 << 11)
59 #define VC4_DIRTY_VIEWPORT (1 << 12)
60 #define VC4_DIRTY_CONSTBUF (1 << 13)
61 #define VC4_DIRTY_VTXSTATE (1 << 14)
62 #define VC4_DIRTY_VTXBUF (1 << 15)
63 #define VC4_DIRTY_INDEXBUF (1 << 16)
64 #define VC4_DIRTY_SCISSOR (1 << 17)
65 #define VC4_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
66 #define VC4_DIRTY_PRIM_MODE (1 << 19)
67 #define VC4_DIRTY_CLIP (1 << 20)
68 #define VC4_DIRTY_UNCOMPILED_VS (1 << 21)
69 #define VC4_DIRTY_UNCOMPILED_FS (1 << 22)
70 #define VC4_DIRTY_COMPILED_CS (1 << 23)
71 #define VC4_DIRTY_COMPILED_VS (1 << 24)
72 #define VC4_DIRTY_COMPILED_FS (1 << 25)
73
74 struct vc4_sampler_view {
75 struct pipe_sampler_view base;
76 uint32_t texture_p0;
77 uint32_t texture_p1;
78 };
79
80 struct vc4_sampler_state {
81 struct pipe_sampler_state base;
82 uint32_t texture_p1;
83 };
84
85 struct vc4_texture_stateobj {
86 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
87 unsigned num_textures;
88 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
89 unsigned num_samplers;
90 unsigned dirty_samplers;
91 };
92
93 struct vc4_shader_uniform_info {
94 enum quniform_contents *contents;
95 uint32_t *data;
96 uint32_t count;
97 uint32_t num_texture_samples;
98 };
99
100 struct vc4_uncompiled_shader {
101 /** A name for this program, so you can track it in shader-db output. */
102 uint32_t program_id;
103 /** How many variants of this program were compiled, for shader-db. */
104 uint32_t compiled_variant_count;
105 struct pipe_shader_state base;
106 const struct tgsi_token *twoside_tokens;
107 };
108
109 struct vc4_ubo_range {
110 /**
111 * offset in bytes from the start of the ubo where this range is
112 * uploaded.
113 *
114 * Only set once used is set.
115 */
116 uint32_t dst_offset;
117
118 /**
119 * offset in bytes from the start of the gallium uniforms where the
120 * data comes from.
121 */
122 uint32_t src_offset;
123
124 /** size in bytes of this ubo range */
125 uint32_t size;
126 };
127
128 struct vc4_compiled_shader {
129 uint64_t program_id;
130 struct vc4_bo *bo;
131
132 struct vc4_shader_uniform_info uniforms;
133
134 struct vc4_ubo_range *ubo_ranges;
135 uint32_t num_ubo_ranges;
136 uint32_t ubo_size;
137 /**
138 * VC4_DIRTY_* flags that, when set in vc4->dirty, mean that the
139 * uniforms have to be rewritten (and therefore the shader state
140 * reemitted).
141 */
142 uint32_t uniform_dirty_bits;
143
144 /** bitmask of which inputs are color inputs, for flat shade handling. */
145 uint32_t color_inputs;
146
147 uint8_t num_inputs;
148
149 /* Byte offsets for the start of the vertex attributes 0-7, and the
150 * total size as "attribute" 8.
151 */
152 uint8_t vattr_offsets[9];
153 uint8_t vattrs_live;
154
155 /**
156 * Array of the meanings of the VPM inputs this shader needs.
157 *
158 * It doesn't include those that aren't part of the VPM, like
159 * point/line coordinates.
160 */
161 struct vc4_varying_semantic *input_semantics;
162 };
163
164 struct vc4_program_stateobj {
165 struct vc4_uncompiled_shader *bind_vs, *bind_fs;
166 struct vc4_compiled_shader *cs, *vs, *fs;
167 uint8_t num_exports;
168 /* Indexed by semantic name or TGSI_SEMANTIC_COUNT + semantic index
169 * for TGSI_SEMANTIC_GENERIC. Special vs exports (position and point-
170 * size) are not included in this
171 */
172 uint8_t export_linkage[63];
173 };
174
175 struct vc4_constbuf_stateobj {
176 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
177 uint32_t enabled_mask;
178 uint32_t dirty_mask;
179 };
180
181 struct vc4_vertexbuf_stateobj {
182 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
183 unsigned count;
184 uint32_t enabled_mask;
185 uint32_t dirty_mask;
186 };
187
188 struct vc4_vertex_stateobj {
189 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
190 unsigned num_elements;
191 };
192
193 struct vc4_context {
194 struct pipe_context base;
195
196 int fd;
197 struct vc4_screen *screen;
198
199 struct vc4_cl bcl;
200 struct vc4_cl shader_rec;
201 struct vc4_cl uniforms;
202 struct vc4_cl bo_handles;
203 struct vc4_cl bo_pointers;
204 uint32_t shader_rec_count;
205
206 /** @{ Surfaces to submit rendering for. */
207 struct pipe_surface *color_read;
208 struct pipe_surface *color_write;
209 struct pipe_surface *zs_read;
210 struct pipe_surface *zs_write;
211 /** @} */
212 /** @{
213 * Bounding box of the scissor across all queued drawing.
214 *
215 * Note that the max values are exclusive.
216 */
217 uint32_t draw_min_x;
218 uint32_t draw_min_y;
219 uint32_t draw_max_x;
220 uint32_t draw_max_y;
221 /** @} */
222 /** @{
223 * Width/height of the color framebuffer being rendered to,
224 * for VC4_TILE_RENDERING_MODE_CONFIG.
225 */
226 uint32_t draw_width;
227 uint32_t draw_height;
228 /** @} */
229
230 struct util_slab_mempool transfer_pool;
231 struct blitter_context *blitter;
232
233 /** bitfield of VC4_DIRTY_* */
234 uint32_t dirty;
235 /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
236 * first rendering.
237 */
238 uint32_t cleared;
239 /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
240 * (either clears or draws).
241 */
242 uint32_t resolve;
243 uint32_t clear_color[2];
244 uint32_t clear_depth; /**< 24-bit unorm depth */
245 uint8_t clear_stencil;
246
247 /**
248 * Set if some drawing (triangles, blits, or just a glClear()) has
249 * been done to the FBO, meaning that we need to
250 * DRM_IOCTL_VC4_SUBMIT_CL.
251 */
252 bool needs_flush;
253
254 /**
255 * Set when needs_flush, and the queued rendering is not just composed
256 * of full-buffer clears.
257 */
258 bool draw_call_queued;
259
260 /** Maximum index buffer valid for the current shader_rec. */
261 uint32_t max_index;
262 /** Last index bias baked into the current shader_rec. */
263 uint32_t last_index_bias;
264
265 struct primconvert_context *primconvert;
266
267 struct hash_table *fs_cache, *vs_cache;
268 uint32_t next_uncompiled_program_id;
269 uint64_t next_compiled_program_id;
270
271 struct ra_regs *regs;
272 unsigned int reg_class_any;
273 unsigned int reg_class_a_or_b_or_acc;
274 unsigned int reg_class_r4_or_a;
275 unsigned int reg_class_a;
276
277 uint8_t prim_mode;
278
279 /** Seqno of the last CL flush's job. */
280 uint64_t last_emit_seqno;
281
282 struct u_upload_mgr *uploader;
283
284 /** @{ Current pipeline state objects */
285 struct pipe_scissor_state scissor;
286 struct pipe_blend_state *blend;
287 struct vc4_rasterizer_state *rasterizer;
288 struct vc4_depth_stencil_alpha_state *zsa;
289
290 struct vc4_texture_stateobj verttex, fragtex;
291
292 struct vc4_program_stateobj prog;
293
294 struct vc4_vertex_stateobj *vtx;
295
296 struct pipe_blend_color blend_color;
297 struct pipe_stencil_ref stencil_ref;
298 unsigned sample_mask;
299 struct pipe_framebuffer_state framebuffer;
300 struct pipe_poly_stipple stipple;
301 struct pipe_clip_state clip;
302 struct pipe_viewport_state viewport;
303 struct vc4_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
304 struct vc4_vertexbuf_stateobj vertexbuf;
305 struct pipe_index_buffer indexbuf;
306 /** @} */
307 };
308
309 struct vc4_rasterizer_state {
310 struct pipe_rasterizer_state base;
311
312 /* VC4_CONFIGURATION_BITS */
313 uint8_t config_bits[3];
314
315 float point_size;
316
317 /**
318 * Half-float (1/8/7 bits) value of polygon offset units for
319 * VC4_PACKET_DEPTH_OFFSET
320 */
321 uint16_t offset_units;
322 /**
323 * Half-float (1/8/7 bits) value of polygon offset scale for
324 * VC4_PACKET_DEPTH_OFFSET
325 */
326 uint16_t offset_factor;
327 };
328
329 struct vc4_depth_stencil_alpha_state {
330 struct pipe_depth_stencil_alpha_state base;
331
332 /* VC4_CONFIGURATION_BITS */
333 uint8_t config_bits[3];
334
335 /** Uniforms for stencil state.
336 *
337 * Index 0 is either the front config, or the front-and-back config.
338 * Index 1 is the back config if doing separate back stencil.
339 * Index 2 is the writemask config if it's not a common mask value.
340 */
341 uint32_t stencil_uniforms[3];
342 };
343
344 #define perf_debug(...) do { \
345 if (unlikely(vc4_debug & VC4_DEBUG_PERF)) \
346 fprintf(stderr, __VA_ARGS__); \
347 } while (0)
348
349 static inline struct vc4_context *
350 vc4_context(struct pipe_context *pcontext)
351 {
352 return (struct vc4_context *)pcontext;
353 }
354
355 static inline struct vc4_sampler_view *
356 vc4_sampler_view(struct pipe_sampler_view *psview)
357 {
358 return (struct vc4_sampler_view *)psview;
359 }
360
361 static inline struct vc4_sampler_state *
362 vc4_sampler_state(struct pipe_sampler_state *psampler)
363 {
364 return (struct vc4_sampler_state *)psampler;
365 }
366
367 struct pipe_context *vc4_context_create(struct pipe_screen *pscreen,
368 void *priv);
369 void vc4_draw_init(struct pipe_context *pctx);
370 void vc4_state_init(struct pipe_context *pctx);
371 void vc4_program_init(struct pipe_context *pctx);
372 void vc4_program_fini(struct pipe_context *pctx);
373 void vc4_query_init(struct pipe_context *pctx);
374 void vc4_simulator_init(struct vc4_screen *screen);
375 int vc4_simulator_flush(struct vc4_context *vc4,
376 struct drm_vc4_submit_cl *args);
377
378 void vc4_set_shader_uniform_dirty_flags(struct vc4_compiled_shader *shader);
379 void vc4_write_uniforms(struct vc4_context *vc4,
380 struct vc4_compiled_shader *shader,
381 struct vc4_constbuf_stateobj *cb,
382 struct vc4_texture_stateobj *texstate);
383
384 void vc4_flush(struct pipe_context *pctx);
385 void vc4_job_init(struct vc4_context *vc4);
386 void vc4_job_submit(struct vc4_context *vc4);
387 void vc4_job_reset(struct vc4_context *vc4);
388 bool vc4_cl_references_bo(struct pipe_context *pctx, struct vc4_bo *bo);
389 void vc4_emit_state(struct pipe_context *pctx);
390 void vc4_generate_code(struct vc4_context *vc4, struct vc4_compile *c);
391 struct qpu_reg *vc4_register_allocate(struct vc4_context *vc4, struct vc4_compile *c);
392 void vc4_update_compiled_shaders(struct vc4_context *vc4, uint8_t prim_mode);
393
394 bool vc4_rt_format_supported(enum pipe_format f);
395 bool vc4_rt_format_is_565(enum pipe_format f);
396 bool vc4_tex_format_supported(enum pipe_format f);
397 uint8_t vc4_get_tex_format(enum pipe_format f);
398 const uint8_t *vc4_get_format_swizzle(enum pipe_format f);
399 void vc4_init_query_functions(struct vc4_context *vc4);
400 void vc4_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info);
401 #endif /* VC4_CONTEXT_H */