freedreno: rebind resource in all contexts
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.h
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 NONINFRINGEMENT. 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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #ifndef FREEDRENO_CONTEXT_H_
28 #define FREEDRENO_CONTEXT_H_
29
30 #include "pipe/p_context.h"
31 #include "indices/u_primconvert.h"
32 #include "util/u_blitter.h"
33 #include "util/list.h"
34 #include "util/slab.h"
35 #include "util/u_string.h"
36
37 #include "freedreno_batch.h"
38 #include "freedreno_screen.h"
39 #include "freedreno_gmem.h"
40 #include "freedreno_util.h"
41
42 #define BORDER_COLOR_UPLOAD_SIZE (2 * PIPE_MAX_SAMPLERS * BORDERCOLOR_SIZE)
43
44 struct fd_vertex_stateobj;
45
46 struct fd_texture_stateobj {
47 struct pipe_sampler_view *textures[PIPE_MAX_SAMPLERS];
48 unsigned num_textures;
49 unsigned valid_textures;
50 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
51 unsigned num_samplers;
52 unsigned valid_samplers;
53 /* number of samples per sampler, 2 bits per sampler: */
54 uint32_t samples;
55 };
56
57 struct fd_program_stateobj {
58 void *vs, *hs, *ds, *gs, *fs;
59 };
60
61 struct fd_constbuf_stateobj {
62 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
63 uint32_t enabled_mask;
64 };
65
66 struct fd_shaderbuf_stateobj {
67 struct pipe_shader_buffer sb[PIPE_MAX_SHADER_BUFFERS];
68 uint32_t enabled_mask;
69 uint32_t writable_mask;
70 };
71
72 struct fd_shaderimg_stateobj {
73 struct pipe_image_view si[PIPE_MAX_SHADER_IMAGES];
74 uint32_t enabled_mask;
75 };
76
77 struct fd_vertexbuf_stateobj {
78 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
79 unsigned count;
80 uint32_t enabled_mask;
81 };
82
83 struct fd_vertex_stateobj {
84 struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
85 unsigned num_elements;
86 };
87
88 struct fd_streamout_stateobj {
89 struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
90 /* Bitmask of stream that should be reset. */
91 unsigned reset;
92
93 unsigned num_targets;
94 /* Track offset from vtxcnt for streamout data. This counter
95 * is just incremented by # of vertices on each draw until
96 * reset or new streamout buffer bound.
97 *
98 * When we eventually have GS, the CPU won't actually know the
99 * number of vertices per draw, so I think we'll have to do
100 * something more clever.
101 */
102 unsigned offsets[PIPE_MAX_SO_BUFFERS];
103 };
104
105 #define MAX_GLOBAL_BUFFERS 16
106 struct fd_global_bindings_stateobj {
107 struct pipe_resource *buf[MAX_GLOBAL_BUFFERS];
108 uint32_t enabled_mask;
109 };
110
111 /* group together the vertex and vertexbuf state.. for ease of passing
112 * around, and because various internal operations (gmem<->mem, etc)
113 * need their own vertex state:
114 */
115 struct fd_vertex_state {
116 struct fd_vertex_stateobj *vtx;
117 struct fd_vertexbuf_stateobj vertexbuf;
118 };
119
120 /* global 3d pipeline dirty state: */
121 enum fd_dirty_3d_state {
122 FD_DIRTY_BLEND = BIT(0),
123 FD_DIRTY_RASTERIZER = BIT(1),
124 FD_DIRTY_ZSA = BIT(2),
125 FD_DIRTY_BLEND_COLOR = BIT(3),
126 FD_DIRTY_STENCIL_REF = BIT(4),
127 FD_DIRTY_SAMPLE_MASK = BIT(5),
128 FD_DIRTY_FRAMEBUFFER = BIT(6),
129 FD_DIRTY_STIPPLE = BIT(7),
130 FD_DIRTY_VIEWPORT = BIT(8),
131 FD_DIRTY_VTXSTATE = BIT(9),
132 FD_DIRTY_VTXBUF = BIT(10),
133 FD_DIRTY_MIN_SAMPLES = BIT(11),
134
135 FD_DIRTY_SCISSOR = BIT(12),
136 FD_DIRTY_STREAMOUT = BIT(13),
137 FD_DIRTY_UCP = BIT(14),
138 FD_DIRTY_BLEND_DUAL = BIT(15),
139
140 /* These are a bit redundent with fd_dirty_shader_state, and possibly
141 * should be removed. (But OTOH kinda convenient in some places)
142 */
143 FD_DIRTY_PROG = BIT(16),
144 FD_DIRTY_CONST = BIT(17),
145 FD_DIRTY_TEX = BIT(18),
146 FD_DIRTY_IMAGE = BIT(19),
147 FD_DIRTY_SSBO = BIT(20),
148
149 /* only used by a2xx.. possibly can be removed.. */
150 FD_DIRTY_TEXSTATE = BIT(21),
151 };
152
153 /* per shader-stage dirty state: */
154 enum fd_dirty_shader_state {
155 FD_DIRTY_SHADER_PROG = BIT(0),
156 FD_DIRTY_SHADER_CONST = BIT(1),
157 FD_DIRTY_SHADER_TEX = BIT(2),
158 FD_DIRTY_SHADER_SSBO = BIT(3),
159 FD_DIRTY_SHADER_IMAGE = BIT(4),
160 };
161
162 struct fd_context {
163 struct pipe_context base;
164
165 struct list_head node; /* node in screen->context_list */
166
167 /* We currently need to serialize emitting GMEM batches, because of
168 * VSC state access in the context.
169 *
170 * In practice this lock should not be contended, since pipe_context
171 * use should be single threaded. But it is needed to protect the
172 * case, with batch reordering where a ctxB batch triggers flushing
173 * a ctxA batch
174 */
175 mtx_t gmem_lock;
176
177 struct fd_device *dev;
178 struct fd_screen *screen;
179 struct fd_pipe *pipe;
180
181 struct blitter_context *blitter;
182 void *clear_rs_state;
183 struct primconvert_context *primconvert;
184
185 /* slab for pipe_transfer allocations: */
186 struct slab_child_pool transfer_pool;
187
188 /**
189 * query related state:
190 */
191 /*@{*/
192 /* slabs for fd_hw_sample and fd_hw_sample_period allocations: */
193 struct slab_mempool sample_pool;
194 struct slab_mempool sample_period_pool;
195
196 /* sample-providers for hw queries: */
197 const struct fd_hw_sample_provider *hw_sample_providers[MAX_HW_SAMPLE_PROVIDERS];
198
199 /* list of active queries: */
200 struct list_head hw_active_queries;
201
202 /* sample-providers for accumulating hw queries: */
203 const struct fd_acc_sample_provider *acc_sample_providers[MAX_HW_SAMPLE_PROVIDERS];
204
205 /* list of active accumulating queries: */
206 struct list_head acc_active_queries;
207 /*@}*/
208
209 /* Whether we need to walk the acc_active_queries next fd_set_stage() to
210 * update active queries (even if stage doesn't change).
211 */
212 bool update_active_queries;
213
214 /* Current state of pctx->set_active_query_state() (i.e. "should drawing
215 * be counted against non-perfcounter queries")
216 */
217 bool active_queries;
218
219 /* table with PIPE_PRIM_MAX entries mapping PIPE_PRIM_x to
220 * DI_PT_x value to use for draw initiator. There are some
221 * slight differences between generation:
222 */
223 const uint8_t *primtypes;
224 uint32_t primtype_mask;
225
226 /* shaders used by clear, and gmem->mem blits: */
227 struct fd_program_stateobj solid_prog; // TODO move to screen?
228
229 /* shaders used by mem->gmem blits: */
230 struct fd_program_stateobj blit_prog[MAX_RENDER_TARGETS]; // TODO move to screen?
231 struct fd_program_stateobj blit_z, blit_zs;
232
233 /* Stats/counters:
234 */
235 struct {
236 uint64_t prims_emitted;
237 uint64_t prims_generated;
238 uint64_t draw_calls;
239 uint64_t batch_total, batch_sysmem, batch_gmem, batch_nondraw, batch_restore;
240 uint64_t staging_uploads, shadow_uploads;
241 uint64_t vs_regs, hs_regs, ds_regs, gs_regs, fs_regs;
242 } stats;
243
244 /* Current batch.. the rule here is that you can deref ctx->batch
245 * in codepaths from pipe_context entrypoints. But not in code-
246 * paths from fd_batch_flush() (basically, the stuff that gets
247 * called from GMEM code), since in those code-paths the batch
248 * you care about is not necessarily the same as ctx->batch.
249 */
250 struct fd_batch *batch;
251
252 /* NULL if there has been rendering since last flush. Otherwise
253 * keeps a reference to the last fence so we can re-use it rather
254 * than having to flush no-op batch.
255 */
256 struct pipe_fence_handle *last_fence;
257
258 /* track last known reset status globally and per-context to
259 * determine if more resets occurred since then. If global reset
260 * count increases, it means some other context crashed. If
261 * per-context reset count increases, it means we crashed the
262 * gpu.
263 */
264 uint32_t context_reset_count, global_reset_count;
265
266 /* Are we in process of shadowing a resource? Used to detect recursion
267 * in transfer_map, and skip unneeded synchronization.
268 */
269 bool in_shadow : 1;
270
271 /* Ie. in blit situation where we no longer care about previous framebuffer
272 * contents. Main point is to eliminate blits from fd_try_shadow_resource().
273 * For example, in case of texture upload + gen-mipmaps.
274 */
275 bool in_discard_blit : 1;
276
277 struct pipe_scissor_state scissor;
278
279 /* we don't have a disable/enable bit for scissor, so instead we keep
280 * a disabled-scissor state which matches the entire bound framebuffer
281 * and use that when scissor is not enabled.
282 */
283 struct pipe_scissor_state disabled_scissor;
284
285 /* Per vsc pipe bo's (a2xx-a5xx): */
286 struct fd_bo *vsc_pipe_bo[32];
287
288 /* which state objects need to be re-emit'd: */
289 enum fd_dirty_3d_state dirty;
290
291 /* per shader-stage dirty status: */
292 enum fd_dirty_shader_state dirty_shader[PIPE_SHADER_TYPES];
293
294 void *compute;
295 struct pipe_blend_state *blend;
296 struct pipe_rasterizer_state *rasterizer;
297 struct pipe_depth_stencil_alpha_state *zsa;
298
299 struct fd_texture_stateobj tex[PIPE_SHADER_TYPES];
300
301 struct fd_program_stateobj prog;
302
303 struct fd_vertex_state vtx;
304
305 struct pipe_blend_color blend_color;
306 struct pipe_stencil_ref stencil_ref;
307 unsigned sample_mask;
308 unsigned min_samples;
309 /* local context fb state, for when ctx->batch is null: */
310 struct pipe_framebuffer_state framebuffer;
311 struct pipe_poly_stipple stipple;
312 struct pipe_viewport_state viewport;
313 struct pipe_scissor_state viewport_scissor;
314 struct fd_constbuf_stateobj constbuf[PIPE_SHADER_TYPES];
315 struct fd_shaderbuf_stateobj shaderbuf[PIPE_SHADER_TYPES];
316 struct fd_shaderimg_stateobj shaderimg[PIPE_SHADER_TYPES];
317 struct fd_streamout_stateobj streamout;
318 struct fd_global_bindings_stateobj global_bindings;
319 struct pipe_clip_state ucp;
320
321 struct pipe_query *cond_query;
322 bool cond_cond; /* inverted rendering condition */
323 uint cond_mode;
324
325 struct pipe_debug_callback debug;
326
327 /* GMEM/tile handling fxns: */
328 void (*emit_tile_init)(struct fd_batch *batch);
329 void (*emit_tile_prep)(struct fd_batch *batch, const struct fd_tile *tile);
330 void (*emit_tile_mem2gmem)(struct fd_batch *batch, const struct fd_tile *tile);
331 void (*emit_tile_renderprep)(struct fd_batch *batch, const struct fd_tile *tile);
332 void (*emit_tile)(struct fd_batch *batch, const struct fd_tile *tile);
333 void (*emit_tile_gmem2mem)(struct fd_batch *batch, const struct fd_tile *tile);
334 void (*emit_tile_fini)(struct fd_batch *batch); /* optional */
335
336 /* optional, for GMEM bypass: */
337 void (*emit_sysmem_prep)(struct fd_batch *batch);
338 void (*emit_sysmem_fini)(struct fd_batch *batch);
339
340 /* draw: */
341 bool (*draw_vbo)(struct fd_context *ctx, const struct pipe_draw_info *info,
342 unsigned index_offset);
343 bool (*clear)(struct fd_context *ctx, unsigned buffers,
344 const union pipe_color_union *color, double depth, unsigned stencil);
345
346 /* compute: */
347 void (*launch_grid)(struct fd_context *ctx, const struct pipe_grid_info *info);
348
349 /* query: */
350 struct fd_query * (*create_query)(struct fd_context *ctx, unsigned query_type, unsigned index);
351 void (*query_prepare)(struct fd_batch *batch, uint32_t num_tiles);
352 void (*query_prepare_tile)(struct fd_batch *batch, uint32_t n,
353 struct fd_ringbuffer *ring);
354 void (*query_set_stage)(struct fd_batch *batch, enum fd_render_stage stage);
355
356 /* blitter: */
357 bool (*blit)(struct fd_context *ctx, const struct pipe_blit_info *info);
358
359 /* handling for barriers: */
360 void (*framebuffer_barrier)(struct fd_context *ctx);
361
362 /* logger: */
363 void (*record_timestamp)(struct fd_ringbuffer *ring, struct fd_bo *bo, unsigned offset);
364 uint64_t (*ts_to_ns)(uint64_t ts);
365
366 struct list_head log_chunks; /* list of flushed log chunks in fifo order */
367 unsigned frame_nr; /* frame counter (for fd_log) */
368 FILE *log_out;
369
370 /*
371 * Common pre-cooked VBO state (used for a3xx and later):
372 */
373
374 /* for clear/gmem->mem vertices, and mem->gmem */
375 struct pipe_resource *solid_vbuf;
376
377 /* for mem->gmem tex coords: */
378 struct pipe_resource *blit_texcoord_vbuf;
379
380 /* vertex state for solid_vbuf:
381 * - solid_vbuf / 12 / R32G32B32_FLOAT
382 */
383 struct fd_vertex_state solid_vbuf_state;
384
385 /* vertex state for blit_prog:
386 * - blit_texcoord_vbuf / 8 / R32G32_FLOAT
387 * - solid_vbuf / 12 / R32G32B32_FLOAT
388 */
389 struct fd_vertex_state blit_vbuf_state;
390
391 /*
392 * Info about state of previous draw, for state that comes from
393 * pipe_draw_info (ie. not part of a CSO). This allows us to
394 * skip some register emit when the state doesn't change from
395 * draw-to-draw
396 */
397 struct {
398 bool dirty; /* last draw state unknown */
399 bool primitive_restart;
400 uint32_t index_start;
401 uint32_t instance_start;
402 uint32_t restart_index;
403 } last;
404 };
405
406 static inline struct fd_context *
407 fd_context(struct pipe_context *pctx)
408 {
409 return (struct fd_context *)pctx;
410 }
411
412 static inline void
413 fd_context_assert_locked(struct fd_context *ctx)
414 {
415 pipe_mutex_assert_locked(ctx->screen->lock);
416 }
417
418 static inline void
419 fd_context_lock(struct fd_context *ctx)
420 {
421 mtx_lock(&ctx->screen->lock);
422 }
423
424 static inline void
425 fd_context_unlock(struct fd_context *ctx)
426 {
427 mtx_unlock(&ctx->screen->lock);
428 }
429
430 /* mark all state dirty: */
431 static inline void
432 fd_context_all_dirty(struct fd_context *ctx)
433 {
434 ctx->last.dirty = true;
435 ctx->dirty = ~0;
436 for (unsigned i = 0; i < PIPE_SHADER_TYPES; i++)
437 ctx->dirty_shader[i] = ~0;
438 }
439
440 static inline void
441 fd_context_all_clean(struct fd_context *ctx)
442 {
443 ctx->dirty = 0;
444 for (unsigned i = 0; i < PIPE_SHADER_TYPES; i++) {
445 /* don't mark compute state as clean, since it is not emitted
446 * during normal draw call. The places that call _all_dirty(),
447 * it is safe to mark compute state dirty as well, but the
448 * inverse is not true.
449 */
450 if (i == PIPE_SHADER_COMPUTE)
451 continue;
452 ctx->dirty_shader[i] = 0;
453 }
454 }
455
456 static inline struct pipe_scissor_state *
457 fd_context_get_scissor(struct fd_context *ctx)
458 {
459 if (ctx->rasterizer && ctx->rasterizer->scissor)
460 return &ctx->scissor;
461 return &ctx->disabled_scissor;
462 }
463
464 static inline bool
465 fd_supported_prim(struct fd_context *ctx, unsigned prim)
466 {
467 return (1 << prim) & ctx->primtype_mask;
468 }
469
470 static inline struct fd_batch *
471 fd_context_batch(struct fd_context *ctx)
472 {
473 if (unlikely(!ctx->batch)) {
474 struct fd_batch *batch =
475 fd_batch_from_fb(&ctx->screen->batch_cache, ctx, &ctx->framebuffer);
476 util_copy_framebuffer_state(&batch->framebuffer, &ctx->framebuffer);
477 ctx->batch = batch;
478 fd_context_all_dirty(ctx);
479 }
480 return ctx->batch;
481 }
482
483 static inline void
484 fd_batch_set_stage(struct fd_batch *batch, enum fd_render_stage stage)
485 {
486 struct fd_context *ctx = batch->ctx;
487
488 if (ctx->query_set_stage)
489 ctx->query_set_stage(batch, stage);
490
491 batch->stage = stage;
492 }
493
494 void fd_context_setup_common_vbos(struct fd_context *ctx);
495 void fd_context_cleanup_common_vbos(struct fd_context *ctx);
496
497 struct pipe_context * fd_context_init(struct fd_context *ctx,
498 struct pipe_screen *pscreen, const uint8_t *primtypes,
499 void *priv, unsigned flags);
500
501 void fd_context_destroy(struct pipe_context *pctx);
502
503 #endif /* FREEDRENO_CONTEXT_H_ */