vc4: Cook up the draw-time VPM setup info during shader compile.
[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 #define VC4_DIRTY_BLEND (1 << 0)
42 #define VC4_DIRTY_RASTERIZER (1 << 1)
43 #define VC4_DIRTY_ZSA (1 << 2)
44 #define VC4_DIRTY_FRAGTEX (1 << 3)
45 #define VC4_DIRTY_VERTTEX (1 << 4)
46 #define VC4_DIRTY_TEXSTATE (1 << 5)
47 #define VC4_DIRTY_PROG (1 << 6)
48 #define VC4_DIRTY_BLEND_COLOR (1 << 7)
49 #define VC4_DIRTY_STENCIL_REF (1 << 8)
50 #define VC4_DIRTY_SAMPLE_MASK (1 << 9)
51 #define VC4_DIRTY_FRAMEBUFFER (1 << 10)
52 #define VC4_DIRTY_STIPPLE (1 << 11)
53 #define VC4_DIRTY_VIEWPORT (1 << 12)
54 #define VC4_DIRTY_CONSTBUF (1 << 13)
55 #define VC4_DIRTY_VTXSTATE (1 << 14)
56 #define VC4_DIRTY_VTXBUF (1 << 15)
57 #define VC4_DIRTY_INDEXBUF (1 << 16)
58 #define VC4_DIRTY_SCISSOR (1 << 17)
59 #define VC4_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
60 #define VC4_DIRTY_PRIM_MODE (1 << 19)
61 #define VC4_DIRTY_CLIP (1 << 20)
62
63 #define VC4_SHADER_DIRTY_VP (1 << 0)
64 #define VC4_SHADER_DIRTY_FP (1 << 1)
65
66 struct vc4_texture_stateobj {
67 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
68 unsigned num_textures;
69 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
70 unsigned num_samplers;
71 unsigned dirty_samplers;
72 };
73
74 struct vc4_shader_uniform_info {
75 enum quniform_contents *contents;
76 uint32_t *data;
77 uint32_t count;
78 uint32_t num_texture_samples;
79 };
80
81 struct vc4_uncompiled_shader {
82 /** A name for this program, so you can track it in shader-db output. */
83 uint32_t program_id;
84 /** How many variants of this program were compiled, for shader-db. */
85 uint32_t compiled_variant_count;
86 struct pipe_shader_state base;
87 const struct tgsi_token *twoside_tokens;
88 };
89
90 struct vc4_ubo_range {
91 /**
92 * offset in bytes from the start of the ubo where this range is
93 * uploaded.
94 *
95 * Only set once used is set.
96 */
97 uint32_t dst_offset;
98
99 /**
100 * offset in bytes from the start of the gallium uniforms where the
101 * data comes from.
102 */
103 uint32_t src_offset;
104
105 /** size in bytes of this ubo range */
106 uint32_t size;
107 };
108
109 struct vc4_compiled_shader {
110 uint64_t program_id;
111 struct vc4_bo *bo;
112
113 struct vc4_shader_uniform_info uniforms;
114
115 struct vc4_ubo_range *ubo_ranges;
116 uint32_t num_ubo_ranges;
117 uint32_t ubo_size;
118
119 /** bitmask of which inputs are color inputs, for flat shade handling. */
120 uint32_t color_inputs;
121
122 uint8_t num_inputs;
123
124 /* Byte offsets for the start of the vertex attributes 0-7, and the
125 * total size as "attribute" 8.
126 */
127 uint8_t vattr_offsets[9];
128 uint8_t vattrs_live;
129
130 /**
131 * Array of the meanings of the VPM inputs this shader needs.
132 *
133 * It doesn't include those that aren't part of the VPM, like
134 * point/line coordinates.
135 */
136 struct vc4_varying_semantic *input_semantics;
137 };
138
139 struct vc4_program_stateobj {
140 struct vc4_uncompiled_shader *bind_vs, *bind_fs;
141 struct vc4_compiled_shader *cs, *vs, *fs;
142 uint32_t dirty;
143 uint8_t num_exports;
144 /* Indexed by semantic name or TGSI_SEMANTIC_COUNT + semantic index
145 * for TGSI_SEMANTIC_GENERIC. Special vs exports (position and point-
146 * size) are not included in this
147 */
148 uint8_t export_linkage[63];
149 };
150
151 struct vc4_constbuf_stateobj {
152 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
153 uint32_t enabled_mask;
154 uint32_t dirty_mask;
155 };
156
157 struct vc4_vertexbuf_stateobj {
158 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
159 unsigned count;
160 uint32_t enabled_mask;
161 uint32_t dirty_mask;
162 };
163
164 struct vc4_vertex_stateobj {
165 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
166 unsigned num_elements;
167 };
168
169 struct vc4_context {
170 struct pipe_context base;
171
172 int fd;
173 struct vc4_screen *screen;
174
175 struct vc4_cl bcl;
176 struct vc4_cl rcl;
177 struct vc4_cl shader_rec;
178 struct vc4_cl uniforms;
179 struct vc4_cl bo_handles;
180 struct vc4_cl bo_pointers;
181 uint32_t shader_rec_count;
182 /** @{
183 * Bounding box of the scissor across all queued drawing.
184 *
185 * Note that the max values are exclusive.
186 */
187 uint32_t draw_min_x;
188 uint32_t draw_min_y;
189 uint32_t draw_max_x;
190 uint32_t draw_max_y;
191 /** @} */
192
193 struct vc4_bo *tile_alloc;
194 struct vc4_bo *tile_state;
195
196 struct util_slab_mempool transfer_pool;
197 struct blitter_context *blitter;
198
199 /** bitfield of VC4_DIRTY_* */
200 uint32_t dirty;
201 /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
202 * first rendering.
203 */
204 uint32_t cleared;
205 /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
206 * (either clears or draws).
207 */
208 uint32_t resolve;
209 uint32_t clear_color[2];
210 uint32_t clear_depth; /**< 24-bit unorm depth */
211 uint8_t clear_stencil;
212
213 /**
214 * Set if some drawing (triangles, blits, or just a glClear()) has
215 * been done to the FBO, meaning that we need to
216 * DRM_IOCTL_VC4_SUBMIT_CL.
217 */
218 bool needs_flush;
219
220 /**
221 * Set when needs_flush, and the queued rendering is not just composed
222 * of full-buffer clears.
223 */
224 bool draw_call_queued;
225
226 struct primconvert_context *primconvert;
227
228 struct hash_table *fs_cache, *vs_cache;
229 uint32_t next_uncompiled_program_id;
230 uint64_t next_compiled_program_id;
231
232 struct ra_regs *regs;
233 unsigned int reg_class_any;
234 unsigned int reg_class_a;
235
236 uint8_t prim_mode;
237
238 /** Seqno of the last CL flush's job. */
239 uint64_t last_emit_seqno;
240
241 /** @{ Current pipeline state objects */
242 struct pipe_scissor_state scissor;
243 struct pipe_blend_state *blend;
244 struct vc4_rasterizer_state *rasterizer;
245 struct vc4_depth_stencil_alpha_state *zsa;
246
247 struct vc4_texture_stateobj verttex, fragtex;
248
249 struct vc4_program_stateobj prog;
250
251 struct vc4_vertex_stateobj *vtx;
252
253 struct pipe_blend_color blend_color;
254 struct pipe_stencil_ref stencil_ref;
255 unsigned sample_mask;
256 struct pipe_framebuffer_state framebuffer;
257 struct pipe_poly_stipple stipple;
258 struct pipe_clip_state clip;
259 struct pipe_viewport_state viewport;
260 struct vc4_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
261 struct vc4_vertexbuf_stateobj vertexbuf;
262 struct pipe_index_buffer indexbuf;
263 /** @} */
264 };
265
266 struct vc4_rasterizer_state {
267 struct pipe_rasterizer_state base;
268
269 /* VC4_CONFIGURATION_BITS */
270 uint8_t config_bits[3];
271
272 float point_size;
273
274 /**
275 * Half-float (1/8/7 bits) value of polygon offset units for
276 * VC4_PACKET_DEPTH_OFFSET
277 */
278 uint16_t offset_units;
279 /**
280 * Half-float (1/8/7 bits) value of polygon offset scale for
281 * VC4_PACKET_DEPTH_OFFSET
282 */
283 uint16_t offset_factor;
284 };
285
286 struct vc4_depth_stencil_alpha_state {
287 struct pipe_depth_stencil_alpha_state base;
288
289 /* VC4_CONFIGURATION_BITS */
290 uint8_t config_bits[3];
291
292 /** Uniforms for stencil state.
293 *
294 * Index 0 is either the front config, or the front-and-back config.
295 * Index 1 is the back config if doing separate back stencil.
296 * Index 2 is the writemask config if it's not a common mask value.
297 */
298 uint32_t stencil_uniforms[3];
299 };
300
301 static inline struct vc4_context *
302 vc4_context(struct pipe_context *pcontext)
303 {
304 return (struct vc4_context *)pcontext;
305 }
306
307 struct pipe_context *vc4_context_create(struct pipe_screen *pscreen,
308 void *priv);
309 void vc4_draw_init(struct pipe_context *pctx);
310 void vc4_state_init(struct pipe_context *pctx);
311 void vc4_program_init(struct pipe_context *pctx);
312 void vc4_program_fini(struct pipe_context *pctx);
313 void vc4_query_init(struct pipe_context *pctx);
314 void vc4_simulator_init(struct vc4_screen *screen);
315 int vc4_simulator_flush(struct vc4_context *vc4,
316 struct drm_vc4_submit_cl *args);
317
318 void vc4_write_uniforms(struct vc4_context *vc4,
319 struct vc4_compiled_shader *shader,
320 struct vc4_constbuf_stateobj *cb,
321 struct vc4_texture_stateobj *texstate);
322
323 void vc4_flush(struct pipe_context *pctx);
324 bool vc4_cl_references_bo(struct pipe_context *pctx, struct vc4_bo *bo);
325 void vc4_emit_state(struct pipe_context *pctx);
326 void vc4_generate_code(struct vc4_context *vc4, struct vc4_compile *c);
327 struct qpu_reg *vc4_register_allocate(struct vc4_context *vc4, struct vc4_compile *c);
328 void vc4_update_compiled_shaders(struct vc4_context *vc4, uint8_t prim_mode);
329
330 bool vc4_rt_format_supported(enum pipe_format f);
331 bool vc4_rt_format_is_565(enum pipe_format f);
332 bool vc4_tex_format_supported(enum pipe_format f);
333 uint8_t vc4_get_tex_format(enum pipe_format f);
334 const uint8_t *vc4_get_format_swizzle(enum pipe_format f);
335 void vc4_init_query_functions(struct vc4_context *vc4);
336 #endif /* VC4_CONTEXT_H */