19f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark/*
29f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
39f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark *
49f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * Permission is hereby granted, free of charge, to any person obtaining a
59f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * copy of this software and associated documentation files (the "Software"),
69f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * to deal in the Software without restriction, including without limitation
79f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * the rights to use, copy, modify, merge, publish, distribute, sublicense,
89f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * and/or sell copies of the Software, and to permit persons to whom the
99f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * Software is furnished to do so, subject to the following conditions:
109f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark *
119f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * The above copyright notice and this permission notice (including the next
129f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * paragraph) shall be included in all copies or substantial portions of the
139f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * Software.
149f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark *
159f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
179f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
189f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
199f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
209f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
219f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * SOFTWARE.
229f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark *
239f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * Authors:
249f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark *    Rob Clark <robclark@freedesktop.org>
259f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark */
269f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
279f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark#ifndef FREEDRENO_BATCH_CACHE_H_
289f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark#define FREEDRENO_BATCH_CACHE_H_
299f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
309f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark#include "pipe/p_state.h"
319f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
32e684c32d2fdda204b79661ecf26881eae133d64aRob Clarkstruct fd_resource;
33e684c32d2fdda204b79661ecf26881eae133d64aRob Clarkstruct fd_batch;
34e684c32d2fdda204b79661ecf26881eae133d64aRob Clarkstruct fd_context;
359f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
369f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkstruct hash_table;
379f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
389f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkstruct fd_batch_cache {
399f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	struct hash_table *ht;
409f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	unsigned cnt;
419f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
429f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	/* set of active batches.. there is an upper limit on the number of
439f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 * in-flight batches, for two reasons:
449f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 * 1) to avoid big spikes in number of batches in edge cases, such as
459f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 *    game startup (ie, lots of texture uploads, but no usages yet of
469f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 *    the textures), etc.
479f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 * 2) so we can use a simple bitmask in fd_resource to track which
489f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 *    batches have reference to the resource
499f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	 */
509f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	struct fd_batch *batches[32];
519f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	uint32_t batch_mask;
529f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark};
539f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
549f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark/* note: if batches get unref'd in the body of the loop, they are removed
559f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * from the various masks.. but since we copy the mask at the beginning of
569f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * the loop into _m, we need the &= at the end of the loop to make sure
579f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark * we don't have stale bits in _m
589f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark */
599f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark#define foreach_batch(batch, cache, mask) \
609f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark	for (uint32_t _m = (mask); _m && ((batch) = (cache)->batches[u_bit_scan(&_m)]); _m &= (mask))
619f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
629f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkvoid fd_bc_init(struct fd_batch_cache *cache);
639f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkvoid fd_bc_fini(struct fd_batch_cache *cache);
649f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
6516f6ceaca9e25f86bcdc509fc0abb48e2d51c3faRob Clarkvoid fd_bc_flush(struct fd_batch_cache *cache, struct fd_context *ctx);
669f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
679f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkvoid fd_bc_invalidate_context(struct fd_context *ctx);
689f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkvoid fd_bc_invalidate_batch(struct fd_batch *batch, bool destroy);
699f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkvoid fd_bc_invalidate_resource(struct fd_resource *rsc, bool destroy);
709f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkstruct fd_batch * fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx);
719f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
729f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clarkstruct fd_batch * fd_batch_from_fb(struct fd_batch_cache *cache,
739f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark		struct fd_context *ctx, const struct pipe_framebuffer_state *pfb);
749f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark
759f219c7047b51561f6f69274d445e6a6ec41c5f8Rob Clark#endif /* FREEDRENO_BATCH_CACHE_H_ */
76