44ee128a4a24b7517c5cf0fc53af72abf44728ae
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_cache.h
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 *
57 * Things that are not constant state objects include:
58 * - blend_color
59 * - clip_state
60 * - clear_color_state
61 * - constant_buffer
62 * - feedback_state
63 * - framebuffer_state
64 * - polygon_stipple
65 * - scissor_state
66 * - texture_state
67 * - viewport_state
68 *
69 * @author Zack Rusin <zack@tungstengraphics.com>
70 */
71
72 #ifndef CSO_CACHE_H
73 #define CSO_CACHE_H
74
75 #include "pipe/p_context.h"
76 #include "pipe/p_state.h"
77
78 /* cso_hash.h is necessary for cso_hash_iter, as MSVC requires structures
79 * returned by value to be fully defined */
80 #include "cso_hash.h"
81
82
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86
87 struct cso_cache;
88
89 struct cso_blend {
90 struct pipe_blend_state state;
91 void *data;
92 void (*delete_state)(struct pipe_context *, void *);
93 struct pipe_context *context;
94 };
95
96 struct cso_depth_stencil_alpha {
97 struct pipe_depth_stencil_alpha_state state;
98 void *data;
99 void (*delete_state)(struct pipe_context *, void *);
100 struct pipe_context *context;
101 };
102
103 struct cso_rasterizer {
104 struct pipe_rasterizer_state state;
105 void *data;
106 void (*delete_state)(struct pipe_context *, void *);
107 struct pipe_context *context;
108 };
109
110 struct cso_fragment_shader {
111 struct pipe_shader_state state;
112 void *data;
113 void (*delete_state)(struct pipe_context *, void *);
114 struct pipe_context *context;
115 };
116
117 struct cso_vertex_shader {
118 struct pipe_shader_state state;
119 void *data;
120 void (*delete_state)(struct pipe_context *, void *);
121 struct pipe_context *context;
122 };
123
124 struct cso_sampler {
125 struct pipe_sampler_state state;
126 void *data;
127 void (*delete_state)(struct pipe_context *, void *);
128 struct pipe_context *context;
129 };
130
131
132 enum cso_cache_type {
133 CSO_BLEND,
134 CSO_SAMPLER,
135 CSO_DEPTH_STENCIL_ALPHA,
136 CSO_RASTERIZER,
137 CSO_FRAGMENT_SHADER,
138 CSO_VERTEX_SHADER
139 };
140
141 typedef void (*cso_state_callback)(void *, void *);
142
143 unsigned cso_construct_key(void *item, int item_size);
144
145 struct cso_cache *cso_cache_create(void);
146 void cso_cache_delete(struct cso_cache *sc);
147
148 struct cso_hash_iter cso_insert_state(struct cso_cache *sc,
149 unsigned hash_key, enum cso_cache_type type,
150 void *state);
151 struct cso_hash_iter cso_find_state(struct cso_cache *sc,
152 unsigned hash_key, enum cso_cache_type type);
153 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
154 unsigned hash_key, enum cso_cache_type type,
155 void *templ);
156 void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
157 cso_state_callback func, void *user_data);
158 void * cso_take_state(struct cso_cache *sc, unsigned hash_key,
159 enum cso_cache_type type);
160
161 void cso_set_maximum_cache_size(struct cso_cache *sc, int number);
162 int cso_maximum_cache_size(const struct cso_cache *sc);
163
164 #ifdef __cplusplus
165 }
166 #endif
167
168 #endif