xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/cso_cache/cso_cache.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007-2008 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28  /**
29   * @file
30   * Constant State Object (CSO) cache.
31   *
32   * The basic idea is that the states are created via the
33   * create_state/bind_state/delete_state semantics. The driver is expected to
34   * perform as much of the Gallium state translation to whatever its internal
35   * representation is during the create call. Gallium then has a caching
36   * mechanism where it stores the created states. When the pipeline needs an
37   * actual state change, a bind call is issued. In the bind call the driver
38   * gets its already translated representation.
39   *
40   * Those semantics mean that the driver doesn't do the repeated translations
41   * of states on every frame, but only once, when a new state is actually
42   * created.
43   *
44   * Even on hardware that doesn't do any kind of state cache, it makes the
45   * driver look a lot neater, plus it avoids all the redundant state
46   * translations on every frame.
47   *
48   * Currently our constant state objects are:
49   * - alpha test
50   * - blend
51   * - depth stencil
52   * - fragment shader
53   * - rasterizer (old setup)
54   * - sampler
55   * - vertex shader
56   * - vertex elements
57   *
58   * Things that are not constant state objects include:
59   * - blend_color
60   * - clip_state
61   * - clear_color_state
62   * - constant_buffer
63   * - feedback_state
64   * - framebuffer_state
65   * - polygon_stipple
66   * - scissor_state
67   * - texture_state
68   * - viewport_state
69   *
70   * @author Zack Rusin <[email protected]>
71   */
72 
73 #ifndef CSO_CACHE_H
74 #define CSO_CACHE_H
75 
76 #include "pipe/p_context.h"
77 #include "pipe/p_state.h"
78 
79 /* cso_hash.h is necessary for cso_hash_iter, as MSVC requires structures
80  * returned by value to be fully defined */
81 #include "cso_hash.h"
82 
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 enum cso_cache_type {
89    CSO_RASTERIZER,
90    CSO_BLEND,
91    CSO_DEPTH_STENCIL_ALPHA,
92    CSO_SAMPLER,
93    CSO_VELEMENTS,
94    CSO_CACHE_MAX,
95 };
96 
97 typedef void (*cso_delete_cso_callback)(void *ctx, void *state,
98                                         enum cso_cache_type type);
99 
100 typedef void (*cso_state_callback)(void *ctx, void *obj);
101 
102 typedef void (*cso_sanitize_callback)(struct cso_hash *hash,
103                                       enum cso_cache_type type,
104                                       int max_size,
105                                       void *user_data);
106 
107 struct cso_cache {
108    struct cso_hash hashes[CSO_CACHE_MAX];
109    int max_size;
110 
111    cso_sanitize_callback sanitize_cb;
112    void *sanitize_data;
113 
114    cso_delete_cso_callback delete_cso;
115    void *delete_cso_ctx;
116 };
117 
118 struct cso_blend {
119    struct pipe_blend_state state;
120    void *data;
121 };
122 
123 struct cso_depth_stencil_alpha {
124    struct pipe_depth_stencil_alpha_state state;
125    void *data;
126 };
127 
128 struct cso_rasterizer {
129    struct pipe_rasterizer_state state;
130    void *data;
131 };
132 
133 struct cso_sampler {
134    struct pipe_sampler_state state;
135    void *data;
136    unsigned hash_key;
137 };
138 
139 struct cso_velems_state {
140    unsigned count;
141    struct pipe_vertex_element velems[PIPE_MAX_ATTRIBS];
142 };
143 
144 struct cso_velements {
145    struct cso_velems_state state;
146    void *data;
147 };
148 
149 
150 void
151 cso_cache_init(struct cso_cache *sc, struct pipe_context *pipe);
152 
153 void
154 cso_cache_delete(struct cso_cache *sc);
155 
156 void
157 cso_cache_set_sanitize_callback(struct cso_cache *sc,
158                                 cso_sanitize_callback cb,
159                                 void *user_data);
160 void
161 cso_cache_set_delete_cso_callback(struct cso_cache *sc,
162                                   cso_delete_cso_callback delete_cso,
163                                   void *ctx);
164 
165 struct cso_hash_iter
166 cso_insert_state(struct cso_cache *sc,
167                  unsigned hash_key, enum cso_cache_type type,
168                  void *state);
169 
170 void
171 cso_set_maximum_cache_size(struct cso_cache *sc, int number);
172 
173 void
174 cso_delete_state(struct pipe_context *pipe, void *state,
175                  enum cso_cache_type type);
176 
177 
178 static ALWAYS_INLINE unsigned
cso_construct_key(const void * key,int key_size)179 cso_construct_key(const void *key, int key_size)
180 {
181    unsigned hash = 0;
182    const unsigned *ikey = (const unsigned *)key;
183    unsigned num_elements = key_size / 4;
184 
185    assert(key_size % 4 == 0);
186 
187    for (unsigned i = 0; i < num_elements; i++)
188       hash ^= ikey[i];
189 
190    return hash;
191 }
192 
193 static ALWAYS_INLINE struct cso_hash_iter
cso_find_state_template(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type,const void * key,unsigned key_size)194 cso_find_state_template(struct cso_cache *sc, unsigned hash_key,
195                         enum cso_cache_type type, const void *key,
196                         unsigned key_size)
197 {
198    struct cso_hash *hash = &sc->hashes[type];
199    struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
200 
201    while (!cso_hash_iter_is_null(iter)) {
202       void *iter_data = cso_hash_iter_data(iter);
203       if (!memcmp(iter_data, key, key_size))
204          return iter;
205       iter = cso_hash_iter_next(iter);
206    }
207    return iter;
208 }
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif
215