freedreno_batch.h revision 9f464c52
1/*
2 * Copyright (C) 2016 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_BATCH_H_
28#define FREEDRENO_BATCH_H_
29
30#include "util/u_inlines.h"
31#include "util/u_queue.h"
32#include "util/list.h"
33
34#include "freedreno_util.h"
35
36struct fd_context;
37struct fd_resource;
38enum fd_resource_status;
39
40/* Bitmask of stages in rendering that a particular query query is
41 * active.  Queries will be automatically started/stopped (generating
42 * additional fd_hw_sample_period's) on entrance/exit from stages that
43 * are applicable to the query.
44 *
45 * NOTE: set the stage to NULL at end of IB to ensure no query is still
46 * active.  Things aren't going to work out the way you want if a query
47 * is active across IB's (or between tile IB and draw IB)
48 */
49enum fd_render_stage {
50	FD_STAGE_NULL     = 0x01,
51	FD_STAGE_DRAW     = 0x02,
52	FD_STAGE_CLEAR    = 0x04,
53	/* used for driver internal draws (ie. util_blitter_blit()): */
54	FD_STAGE_BLIT     = 0x08,
55	FD_STAGE_ALL      = 0xff,
56};
57
58#define MAX_HW_SAMPLE_PROVIDERS 5
59struct fd_hw_sample_provider;
60struct fd_hw_sample;
61
62/* A batch tracks everything about a cmdstream batch/submit, including the
63 * ringbuffers used for binning, draw, and gmem cmds, list of associated
64 * fd_resource-s, etc.
65 */
66struct fd_batch {
67	struct pipe_reference reference;
68	unsigned seqno;
69	unsigned idx;       /* index into cache->batches[] */
70
71	int in_fence_fd;
72	bool needs_out_fence_fd;
73	struct pipe_fence_handle *fence;
74
75	struct fd_context *ctx;
76
77	struct util_queue_fence flush_fence;
78
79	/* do we need to mem2gmem before rendering.  We don't, if for example,
80	 * there was a glClear() that invalidated the entire previous buffer
81	 * contents.  Keep track of which buffer(s) are cleared, or needs
82	 * restore.  Masks of PIPE_CLEAR_*
83	 *
84	 * The 'cleared' bits will be set for buffers which are *entirely*
85	 * cleared, and 'partial_cleared' bits will be set if you must
86	 * check cleared_scissor.
87	 *
88	 * The 'invalidated' bits are set for cleared buffers, and buffers
89	 * where the contents are undefined, ie. what we don't need to restore
90	 * to gmem.
91	 */
92	enum {
93		/* align bitmask values w/ PIPE_CLEAR_*.. since that is convenient.. */
94		FD_BUFFER_COLOR   = PIPE_CLEAR_COLOR,
95		FD_BUFFER_DEPTH   = PIPE_CLEAR_DEPTH,
96		FD_BUFFER_STENCIL = PIPE_CLEAR_STENCIL,
97		FD_BUFFER_ALL     = FD_BUFFER_COLOR | FD_BUFFER_DEPTH | FD_BUFFER_STENCIL,
98	} invalidated, cleared, fast_cleared, restore, resolve;
99
100	/* is this a non-draw batch (ie compute/blit which has no pfb state)? */
101	bool nondraw : 1;
102	bool needs_flush : 1;
103	bool flushed : 1;
104	bool blit : 1;
105	bool back_blit : 1;      /* only blit so far is resource shadowing back-blit */
106
107	/* Keep track if WAIT_FOR_IDLE is needed for registers we need
108	 * to update via RMW:
109	 */
110	bool needs_wfi : 1;
111
112	/* To decide whether to render to system memory, keep track of the
113	 * number of draws, and whether any of them require multisample,
114	 * depth_test (or depth write), stencil_test, blending, and
115	 * color_logic_Op (since those functions are disabled when by-
116	 * passing GMEM.
117	 */
118	enum {
119		FD_GMEM_CLEARS_DEPTH_STENCIL = 0x01,
120		FD_GMEM_DEPTH_ENABLED        = 0x02,
121		FD_GMEM_STENCIL_ENABLED      = 0x04,
122
123		FD_GMEM_BLEND_ENABLED        = 0x10,
124		FD_GMEM_LOGICOP_ENABLED      = 0x20,
125		FD_GMEM_FB_READ              = 0x40,
126	} gmem_reason;
127	unsigned num_draws;   /* number of draws in current batch */
128	unsigned num_vertices;   /* number of vertices in current batch */
129
130	/* Track the maximal bounds of the scissor of all the draws within a
131	 * batch.  Used at the tile rendering step (fd_gmem_render_tiles(),
132	 * mem2gmem/gmem2mem) to avoid needlessly moving data in/out of gmem.
133	 */
134	struct pipe_scissor_state max_scissor;
135
136	/* Keep track of DRAW initiators that need to be patched up depending
137	 * on whether we using binning or not:
138	 */
139	struct util_dynarray draw_patches;
140
141	/* texture state that needs patching for fb_read: */
142	struct util_dynarray fb_read_patches;
143
144	/* Keep track of writes to RB_RENDER_CONTROL which need to be patched
145	 * once we know whether or not to use GMEM, and GMEM tile pitch.
146	 *
147	 * (only for a3xx.. but having gen specific subclasses of fd_batch
148	 * seemed overkill for now)
149	 */
150	struct util_dynarray rbrc_patches;
151
152	/* Keep track of GMEM related values that need to be patched up once we
153	 * know the gmem layout:
154	 */
155	struct util_dynarray gmem_patches;
156
157	/* Keep track of pointer to start of MEM exports for a20x binning shaders
158	 *
159	 * this is so the end of the shader can be cut off at the right point
160	 * depending on the GMEM configuration
161	 */
162	struct util_dynarray shader_patches;
163
164	struct pipe_framebuffer_state framebuffer;
165
166	struct fd_submit *submit;
167
168	/** draw pass cmdstream: */
169	struct fd_ringbuffer *draw;
170	/** binning pass cmdstream: */
171	struct fd_ringbuffer *binning;
172	/** tiling/gmem (IB0) cmdstream: */
173	struct fd_ringbuffer *gmem;
174
175	// TODO maybe more generically split out clear and clear_binning rings?
176	struct fd_ringbuffer *lrz_clear;
177	struct fd_ringbuffer *tile_setup;
178	struct fd_ringbuffer *tile_fini;
179
180	union pipe_color_union clear_color[MAX_RENDER_TARGETS];
181	double clear_depth;
182	unsigned clear_stencil;
183
184	/**
185	 * hw query related state:
186	 */
187	/*@{*/
188	/* next sample offset.. incremented for each sample in the batch/
189	 * submit, reset to zero on next submit.
190	 */
191	uint32_t next_sample_offset;
192
193	/* cached samples (in case multiple queries need to reference
194	 * the same sample snapshot)
195	 */
196	struct fd_hw_sample *sample_cache[MAX_HW_SAMPLE_PROVIDERS];
197
198	/* which sample providers were active in the current batch: */
199	uint32_t active_providers;
200
201	/* tracking for current stage, to know when to start/stop
202	 * any active queries:
203	 */
204	enum fd_render_stage stage;
205
206	/* list of samples in current batch: */
207	struct util_dynarray samples;
208
209	/* current query result bo and tile stride: */
210	struct pipe_resource *query_buf;
211	uint32_t query_tile_stride;
212	/*@}*/
213
214
215	/* Set of resources used by currently-unsubmitted batch (read or
216	 * write).. does not hold a reference to the resource.
217	 */
218	struct set *resources;
219
220	/** key in batch-cache (if not null): */
221	const void *key;
222	uint32_t hash;
223
224	/** set of dependent batches.. holds refs to dependent batches: */
225	uint32_t dependents_mask;
226};
227
228struct fd_batch * fd_batch_create(struct fd_context *ctx, bool nondraw);
229
230void fd_batch_reset(struct fd_batch *batch);
231void fd_batch_sync(struct fd_batch *batch);
232void fd_batch_flush(struct fd_batch *batch, bool sync, bool force);
233void fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep);
234void fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write);
235void fd_batch_check_size(struct fd_batch *batch);
236
237/* not called directly: */
238void __fd_batch_describe(char* buf, const struct fd_batch *batch);
239void __fd_batch_destroy(struct fd_batch *batch);
240
241/*
242 * NOTE the rule is, you need to hold the screen->lock when destroying
243 * a batch..  so either use fd_batch_reference() (which grabs the lock
244 * for you) if you don't hold the lock, or fd_batch_reference_locked()
245 * if you do hold the lock.
246 *
247 * WARNING the _locked() version can briefly drop the lock.  Without
248 * recursive mutexes, I'm not sure there is much else we can do (since
249 * __fd_batch_destroy() needs to unref resources)
250 *
251 * WARNING you must acquire the screen->lock and use the _locked()
252 * version in case that the batch being ref'd can disappear under
253 * you.
254 */
255
256/* fwd-decl prototypes to untangle header dependency :-/ */
257static inline void fd_context_assert_locked(struct fd_context *ctx);
258static inline void fd_context_lock(struct fd_context *ctx);
259static inline void fd_context_unlock(struct fd_context *ctx);
260
261static inline void
262fd_batch_reference_locked(struct fd_batch **ptr, struct fd_batch *batch)
263{
264	struct fd_batch *old_batch = *ptr;
265
266	/* only need lock if a reference is dropped: */
267	if (old_batch)
268		fd_context_assert_locked(old_batch->ctx);
269
270	if (pipe_reference_described(&(*ptr)->reference, &batch->reference,
271			(debug_reference_descriptor)__fd_batch_describe))
272		__fd_batch_destroy(old_batch);
273
274	*ptr = batch;
275}
276
277static inline void
278fd_batch_reference(struct fd_batch **ptr, struct fd_batch *batch)
279{
280	struct fd_batch *old_batch = *ptr;
281	struct fd_context *ctx = old_batch ? old_batch->ctx : NULL;
282
283	if (ctx)
284		fd_context_lock(ctx);
285
286	fd_batch_reference_locked(ptr, batch);
287
288	if (ctx)
289		fd_context_unlock(ctx);
290}
291
292#include "freedreno_context.h"
293
294static inline void
295fd_reset_wfi(struct fd_batch *batch)
296{
297	batch->needs_wfi = true;
298}
299
300void fd_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring);
301
302/* emit a CP_EVENT_WRITE:
303 */
304static inline void
305fd_event_write(struct fd_batch *batch, struct fd_ringbuffer *ring,
306		enum vgt_event_type evt)
307{
308	OUT_PKT3(ring, CP_EVENT_WRITE, 1);
309	OUT_RING(ring, evt);
310	fd_reset_wfi(batch);
311}
312
313#endif /* FREEDRENO_BATCH_H_ */
314