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