freedreno: per-context fd_pipe
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.h
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #ifndef FREEDRENO_CONTEXT_H_
30 #define FREEDRENO_CONTEXT_H_
31
32 #include "pipe/p_context.h"
33 #include "indices/u_primconvert.h"
34 #include "util/u_blitter.h"
35 #include "util/list.h"
36 #include "util/slab.h"
37 #include "util/u_string.h"
38
39 #include "freedreno_batch.h"
40 #include "freedreno_screen.h"
41 #include "freedreno_gmem.h"
42 #include "freedreno_util.h"
43
44 #define BORDER_COLOR_UPLOAD_SIZE (2 * PIPE_MAX_SAMPLERS * BORDERCOLOR_SIZE)
45
46 struct fd_vertex_stateobj;
47
48 struct fd_texture_stateobj {
49 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
50 unsigned num_textures;
51 unsigned valid_textures;
52 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
53 unsigned num_samplers;
54 unsigned valid_samplers;
55 };
56
57 struct fd_program_stateobj {
58 void *vp, *fp;
59
60 /* rest only used by fd2.. split out: */
61 uint8_t num_exports;
62 /* Indexed by semantic name or TGSI_SEMANTIC_COUNT + semantic index
63 * for TGSI_SEMANTIC_GENERIC. Special vs exports (position and point-
64 * size) are not included in this
65 */
66 uint8_t export_linkage[63];
67 };
68
69 struct fd_constbuf_stateobj {
70 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
71 uint32_t enabled_mask;
72 uint32_t dirty_mask;
73 };
74
75 struct fd_shaderbuf_stateobj {
76 struct pipe_shader_buffer sb[PIPE_MAX_SHADER_BUFFERS];
77 uint32_t enabled_mask;
78 uint32_t dirty_mask;
79 };
80
81 struct fd_vertexbuf_stateobj {
82 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
83 unsigned count;
84 uint32_t enabled_mask;
85 uint32_t dirty_mask;
86 };
87
88 struct fd_vertex_stateobj {
89 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
90 unsigned num_elements;
91 };
92
93 struct fd_streamout_stateobj {
94 struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
95 unsigned num_targets;
96 /* Track offset from vtxcnt for streamout data. This counter
97 * is just incremented by # of vertices on each draw until
98 * reset or new streamout buffer bound.
99 *
100 * When we eventually have GS, the CPU won't actually know the
101 * number of vertices per draw, so I think we'll have to do
102 * something more clever.
103 */
104 unsigned offsets[PIPE_MAX_SO_BUFFERS];
105 };
106
107 /* group together the vertex and vertexbuf state.. for ease of passing
108 * around, and because various internal operations (gmem<->mem, etc)
109 * need their own vertex state:
110 */
111 struct fd_vertex_state {
112 struct fd_vertex_stateobj *vtx;
113 struct fd_vertexbuf_stateobj vertexbuf;
114 };
115
116 /* global 3d pipeline dirty state: */
117 enum fd_dirty_3d_state {
118 FD_DIRTY_BLEND = BIT(0),
119 FD_DIRTY_RASTERIZER = BIT(1),
120 FD_DIRTY_ZSA = BIT(2),
121 FD_DIRTY_BLEND_COLOR = BIT(3),
122 FD_DIRTY_STENCIL_REF = BIT(4),
123 FD_DIRTY_SAMPLE_MASK = BIT(5),
124 FD_DIRTY_FRAMEBUFFER = BIT(6),
125 FD_DIRTY_STIPPLE = BIT(7),
126 FD_DIRTY_VIEWPORT = BIT(8),
127 FD_DIRTY_VTXSTATE = BIT(9),
128 FD_DIRTY_VTXBUF = BIT(10),
129
130 FD_DIRTY_SCISSOR = BIT(12),
131 FD_DIRTY_STREAMOUT = BIT(13),
132 FD_DIRTY_UCP = BIT(14),
133 FD_DIRTY_BLEND_DUAL = BIT(15),
134
135 /* These are a bit redundent with fd_dirty_shader_state, and possibly
136 * should be removed. (But OTOH kinda convenient in some places)
137 */
138 FD_DIRTY_PROG = BIT(16),
139 FD_DIRTY_CONST = BIT(17),
140 FD_DIRTY_TEX = BIT(18),
141
142 /* only used by a2xx.. possibly can be removed.. */
143 FD_DIRTY_TEXSTATE = BIT(19),
144 };
145
146 /* per shader-stage dirty state: */
147 enum fd_dirty_shader_state {
148 FD_DIRTY_SHADER_PROG = BIT(0),
149 FD_DIRTY_SHADER_CONST = BIT(1),
150 FD_DIRTY_SHADER_TEX = BIT(2),
151 FD_DIRTY_SHADER_SSBO = BIT(3),
152 };
153
154 struct fd_context {
155 struct pipe_context base;
156
157 struct fd_device *dev;
158 struct fd_screen *screen;
159 struct fd_pipe *pipe;
160
161 struct util_queue flush_queue;
162
163 struct blitter_context *blitter;
164 void *clear_rs_state;
165 struct primconvert_context *primconvert;
166
167 /* slab for pipe_transfer allocations: */
168 struct slab_child_pool transfer_pool;
169
170 /**
171 * query related state:
172 */
173 /*@{*/
174 /* slabs for fd_hw_sample and fd_hw_sample_period allocations: */
175 struct slab_mempool sample_pool;
176 struct slab_mempool sample_period_pool;
177
178 /* sample-providers for hw queries: */
179 const struct fd_hw_sample_provider *hw_sample_providers[MAX_HW_SAMPLE_PROVIDERS];
180
181 /* list of active queries: */
182 struct list_head hw_active_queries;
183
184 /* sample-providers for accumulating hw queries: */
185 const struct fd_acc_sample_provider *acc_sample_providers[MAX_HW_SAMPLE_PROVIDERS];
186
187 /* list of active accumulating queries: */
188 struct list_head acc_active_queries;
189 /*@}*/
190
191 /* table with PIPE_PRIM_MAX entries mapping PIPE_PRIM_x to
192 * DI_PT_x value to use for draw initiator. There are some
193 * slight differences between generation:
194 */
195 const uint8_t *primtypes;
196 uint32_t primtype_mask;
197
198 /* shaders used by clear, and gmem->mem blits: */
199 struct fd_program_stateobj solid_prog; // TODO move to screen?
200
201 /* shaders used by mem->gmem blits: */
202 struct fd_program_stateobj blit_prog[MAX_RENDER_TARGETS]; // TODO move to screen?
203 struct fd_program_stateobj blit_z, blit_zs;
204
205 /* Stats/counters:
206 */
207 struct {
208 uint64_t prims_emitted;
209 uint64_t prims_generated;
210 uint64_t draw_calls;
211 uint64_t batch_total, batch_sysmem, batch_gmem, batch_restore;
212 } stats;
213
214 /* Current batch.. the rule here is that you can deref ctx->batch
215 * in codepaths from pipe_context entrypoints. But not in code-
216 * paths from fd_batch_flush() (basically, the stuff that gets
217 * called from GMEM code), since in those code-paths the batch
218 * you care about is not necessarily the same as ctx->batch.
219 */
220 struct fd_batch *batch;
221
222 struct pipe_fence_handle *last_fence;
223
224 /* Are we in process of shadowing a resource? Used to detect recursion
225 * in transfer_map, and skip unneeded synchronization.
226 */
227 bool in_shadow : 1;
228
229 /* Ie. in blit situation where we no longer care about previous framebuffer
230 * contents. Main point is to eliminate blits from fd_try_shadow_resource().
231 * For example, in case of texture upload + gen-mipmaps.
232 */
233 bool in_blit : 1;
234
235 struct pipe_scissor_state scissor;
236
237 /* we don't have a disable/enable bit for scissor, so instead we keep
238 * a disabled-scissor state which matches the entire bound framebuffer
239 * and use that when scissor is not enabled.
240 */
241 struct pipe_scissor_state disabled_scissor;
242
243 /* Current gmem/tiling configuration.. gets updated on render_tiles()
244 * if out of date with current maximal-scissor/cpp:
245 *
246 * (NOTE: this is kind of related to the batch, but moving it there
247 * means we'd always have to recalc tiles ever batch)
248 */
249 struct fd_gmem_stateobj gmem;
250 struct fd_vsc_pipe vsc_pipe[16];
251 struct fd_tile tile[512];
252
253 /* which state objects need to be re-emit'd: */
254 enum fd_dirty_3d_state dirty;
255
256 /* per shader-stage dirty status: */
257 enum fd_dirty_shader_state dirty_shader[PIPE_SHADER_TYPES];
258
259 void *compute;
260 struct pipe_blend_state *blend;
261 struct pipe_rasterizer_state *rasterizer;
262 struct pipe_depth_stencil_alpha_state *zsa;
263
264 struct fd_texture_stateobj tex[PIPE_SHADER_TYPES];
265
266 struct fd_program_stateobj prog;
267
268 struct fd_vertex_state vtx;
269
270 struct pipe_blend_color blend_color;
271 struct pipe_stencil_ref stencil_ref;
272 unsigned sample_mask;
273 struct pipe_poly_stipple stipple;
274 struct pipe_viewport_state viewport;
275 struct fd_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
276 struct fd_shaderbuf_stateobj shaderbuf[PIPE_SHADER_TYPES];
277 struct fd_streamout_stateobj streamout;
278 struct pipe_clip_state ucp;
279
280 struct pipe_query *cond_query;
281 bool cond_cond; /* inverted rendering condition */
282 uint cond_mode;
283
284 struct pipe_debug_callback debug;
285
286 /* GMEM/tile handling fxns: */
287 void (*emit_tile_init)(struct fd_batch *batch);
288 void (*emit_tile_prep)(struct fd_batch *batch, struct fd_tile *tile);
289 void (*emit_tile_mem2gmem)(struct fd_batch *batch, struct fd_tile *tile);
290 void (*emit_tile_renderprep)(struct fd_batch *batch, struct fd_tile *tile);
291 void (*emit_tile_gmem2mem)(struct fd_batch *batch, struct fd_tile *tile);
292 void (*emit_tile_fini)(struct fd_batch *batch); /* optional */
293
294 /* optional, for GMEM bypass: */
295 void (*emit_sysmem_prep)(struct fd_batch *batch);
296 void (*emit_sysmem_fini)(struct fd_batch *batch);
297
298 /* draw: */
299 bool (*draw_vbo)(struct fd_context *ctx, const struct pipe_draw_info *info,
300 unsigned index_offset);
301 bool (*clear)(struct fd_context *ctx, unsigned buffers,
302 const union pipe_color_union *color, double depth, unsigned stencil);
303
304 /* compute: */
305 void (*launch_grid)(struct fd_context *ctx, const struct pipe_grid_info *info);
306
307 /* constant emit: (note currently not used/needed for a2xx) */
308 void (*emit_const)(struct fd_ringbuffer *ring, enum shader_t type,
309 uint32_t regid, uint32_t offset, uint32_t sizedwords,
310 const uint32_t *dwords, struct pipe_resource *prsc);
311 /* emit bo addresses as constant: */
312 void (*emit_const_bo)(struct fd_ringbuffer *ring, enum shader_t type, boolean write,
313 uint32_t regid, uint32_t num, struct pipe_resource **prscs, uint32_t *offsets);
314
315 /* indirect-branch emit: */
316 void (*emit_ib)(struct fd_ringbuffer *ring, struct fd_ringbuffer *target);
317
318 /* query: */
319 struct fd_query * (*create_query)(struct fd_context *ctx, unsigned query_type);
320 void (*query_prepare)(struct fd_batch *batch, uint32_t num_tiles);
321 void (*query_prepare_tile)(struct fd_batch *batch, uint32_t n,
322 struct fd_ringbuffer *ring);
323 void (*query_set_stage)(struct fd_batch *batch, enum fd_render_stage stage);
324
325 /*
326 * Common pre-cooked VBO state (used for a3xx and later):
327 */
328
329 /* for clear/gmem->mem vertices, and mem->gmem */
330 struct pipe_resource *solid_vbuf;
331
332 /* for mem->gmem tex coords: */
333 struct pipe_resource *blit_texcoord_vbuf;
334
335 /* vertex state for solid_vbuf:
336 * - solid_vbuf / 12 / R32G32B32_FLOAT
337 */
338 struct fd_vertex_state solid_vbuf_state;
339
340 /* vertex state for blit_prog:
341 * - blit_texcoord_vbuf / 8 / R32G32_FLOAT
342 * - solid_vbuf / 12 / R32G32B32_FLOAT
343 */
344 struct fd_vertex_state blit_vbuf_state;
345 };
346
347 static inline struct fd_context *
348 fd_context(struct pipe_context *pctx)
349 {
350 return (struct fd_context *)pctx;
351 }
352
353 static inline void
354 fd_context_assert_locked(struct fd_context *ctx)
355 {
356 pipe_mutex_assert_locked(ctx->screen->lock);
357 }
358
359 static inline void
360 fd_context_lock(struct fd_context *ctx)
361 {
362 mtx_lock(&ctx->screen->lock);
363 }
364
365 static inline void
366 fd_context_unlock(struct fd_context *ctx)
367 {
368 mtx_unlock(&ctx->screen->lock);
369 }
370
371 /* mark all state dirty: */
372 static inline void
373 fd_context_all_dirty(struct fd_context *ctx)
374 {
375 ctx->dirty = ~0;
376 for (unsigned i = 0; i < PIPE_SHADER_TYPES; i++)
377 ctx->dirty_shader[i] = ~0;
378 }
379
380 static inline void
381 fd_context_all_clean(struct fd_context *ctx)
382 {
383 ctx->dirty = 0;
384 for (unsigned i = 0; i < PIPE_SHADER_TYPES; i++) {
385 /* don't mark compute state as clean, since it is not emitted
386 * during normal draw call. The places that call _all_dirty(),
387 * it is safe to mark compute state dirty as well, but the
388 * inverse is not true.
389 */
390 if (i == PIPE_SHADER_COMPUTE)
391 continue;
392 ctx->dirty_shader[i] = 0;
393 }
394 }
395
396 static inline struct pipe_scissor_state *
397 fd_context_get_scissor(struct fd_context *ctx)
398 {
399 if (ctx->rasterizer && ctx->rasterizer->scissor)
400 return &ctx->scissor;
401 return &ctx->disabled_scissor;
402 }
403
404 static inline bool
405 fd_supported_prim(struct fd_context *ctx, unsigned prim)
406 {
407 return (1 << prim) & ctx->primtype_mask;
408 }
409
410 static inline void
411 fd_batch_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
412 {
413 struct fd_context *ctx = batch->ctx;
414
415 /* special case: internal blits (like mipmap level generation)
416 * go through normal draw path (via util_blitter_blit()).. but
417 * we need to ignore the FD_STAGE_DRAW which will be set, so we
418 * don't enable queries which should be paused during internal
419 * blits:
420 */
421 if ((batch->stage == FD_STAGE_BLIT) &&
422 (stage != FD_STAGE_NULL))
423 return;
424
425 if (ctx->query_set_stage)
426 ctx->query_set_stage(batch, stage);
427
428 batch->stage = stage;
429 }
430
431 void fd_context_setup_common_vbos(struct fd_context *ctx);
432 void fd_context_cleanup_common_vbos(struct fd_context *ctx);
433
434 struct pipe_context * fd_context_init(struct fd_context *ctx,
435 struct pipe_screen *pscreen, const uint8_t *primtypes,
436 void *priv, unsigned flags);
437
438 void fd_context_destroy(struct pipe_context *pctx);
439
440 #endif /* FREEDRENO_CONTEXT_H_ */