1/*
2 * Copyright (C) 2018 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 IR3_CACHE_H_
28#define IR3_CACHE_H_
29
30#include "pipe/p_state.h"
31
32#include "ir3/ir3_shader.h"
33
34/*
35 * An in-memory cache for mapping shader state objects plus shader key to
36 * hw specific state object for the specified shader variant.  This is to
37 * allow re-using things like the register setup for varying linkage, etc.
38 */
39
40/* key into program state cache */
41struct ir3_cache_key {
42   struct ir3_shader_state *vs, *hs, *ds, *gs, *fs; // 5 pointers
43   struct ir3_shader_key key;                       // 7 dwords
44
45   /* Additional state that effects the cached program state, but
46    * not the compiled shader:
47    */
48   unsigned clip_plane_enable : PIPE_MAX_CLIP_PLANES;
49};
50
51/* per-gen backend program state object should subclass this for it's
52 * state object, mainly because we need a copy of the key that is not
53 * allocated on the stack
54 */
55struct ir3_program_state {
56   struct ir3_cache_key key;
57};
58
59struct ir3_cache_funcs {
60   struct ir3_program_state *(*create_state)(
61      void *data, struct ir3_shader_variant *bs, /* binning pass vs */
62      struct ir3_shader_variant *vs, struct ir3_shader_variant *hs,
63      struct ir3_shader_variant *ds, struct ir3_shader_variant *gs,
64      struct ir3_shader_variant *fs, const struct ir3_cache_key *key);
65   void (*destroy_state)(void *data, struct ir3_program_state *state);
66};
67
68struct ir3_cache;
69
70/* construct a shader cache.  Free with ralloc_free() */
71struct ir3_cache *ir3_cache_create(const struct ir3_cache_funcs *funcs,
72                                   void *data);
73void ir3_cache_destroy(struct ir3_cache *cache);
74
75/* debug callback is used for shader-db logs in case the lookup triggers
76 * shader variant compilation.
77 */
78struct ir3_program_state *ir3_cache_lookup(struct ir3_cache *cache,
79                                           const struct ir3_cache_key *key,
80                                           struct pipe_debug_callback *debug);
81
82/* call when an API level state object is destroyed, to invalidate
83 * cache entries which reference that state object.
84 */
85void ir3_cache_invalidate(struct ir3_cache *cache, void *stobj);
86
87#endif /* IR3_CACHE_H_ */
88