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