1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Zack Rusin <zack@tungstengraphics.com>
35 #include "st_context.h"
37 #include "pipe/p_state.h"
39 #include "pipe/cso_cache/cso_cache.h"
40 #include "pipe/cso_cache/cso_hash.h"
42 /* This function will either find the state of the given template
43 * in the cache or it will create a new state state from the given
44 * template, will insert it in the cache and return it.
46 const struct cso_blend
* st_cached_blend_state(struct st_context
*st
,
47 const struct pipe_blend_state
*templ
)
49 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_blend_state
));
50 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
53 if (cso_hash_iter_is_null(iter
)) {
54 struct cso_blend
*cso
= malloc(sizeof(struct cso_blend
));
55 memcpy(&cso
->state
, templ
, sizeof(struct pipe_blend_state
));
56 cso
->data
= st
->pipe
->create_blend_state(st
->pipe
, templ
);
58 cso
->data
= &cso
->state
;
59 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_BLEND
, cso
);
61 return ((struct cso_blend
*)cso_hash_iter_data(iter
));
64 struct pipe_sampler_state
* st_cached_sampler_state(
65 struct st_context
*st
,
66 const struct pipe_sampler_state
*sampler
)
68 unsigned hash_key
= cso_construct_key((void*)sampler
, sizeof(struct pipe_sampler_state
));
69 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
70 hash_key
, CSO_SAMPLER
,
72 if (cso_hash_iter_is_null(iter
)) {
73 const struct pipe_sampler_state
*created_state
= st
->pipe
->create_sampler_state(
75 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_SAMPLER
,
76 (void*)created_state
);
78 return (struct pipe_sampler_state
*)(cso_hash_iter_data(iter
));
81 struct pipe_depth_stencil_state
* st_cached_depth_stencil_state(
82 struct st_context
*st
,
83 const struct pipe_depth_stencil_state
*depth_stencil
)
85 unsigned hash_key
= cso_construct_key((void*)depth_stencil
, sizeof(struct pipe_depth_stencil_state
));
86 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
87 hash_key
, CSO_DEPTH_STENCIL
,
88 (void*)depth_stencil
);
89 if (cso_hash_iter_is_null(iter
)) {
90 const struct pipe_depth_stencil_state
*created_state
= st
->pipe
->create_depth_stencil_state(
91 st
->pipe
, depth_stencil
);
92 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_DEPTH_STENCIL
,
93 (void*)created_state
);
95 return (struct pipe_depth_stencil_state
*)(cso_hash_iter_data(iter
));
98 const struct cso_rasterizer
* st_cached_rasterizer_state(
99 struct st_context
*st
,
100 const struct pipe_rasterizer_state
*templ
)
102 unsigned hash_key
= cso_construct_key((void*)templ
,
103 sizeof(struct pipe_rasterizer_state
));
104 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
105 hash_key
, CSO_RASTERIZER
,
107 if (cso_hash_iter_is_null(iter
)) {
108 struct cso_rasterizer
*cso
= malloc(sizeof(struct cso_rasterizer
));
109 memcpy(&cso
->state
, templ
, sizeof(struct pipe_rasterizer_state
));
110 cso
->data
= st
->pipe
->create_rasterizer_state(st
->pipe
, templ
);
112 cso
->data
= &cso
->state
;
113 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
115 return (struct cso_rasterizer
*)(cso_hash_iter_data(iter
));
118 const struct cso_fragment_shader
*
119 st_cached_fs_state(struct st_context
*st
,
120 const struct pipe_shader_state
*templ
)
122 unsigned hash_key
= cso_construct_key((void*)templ
,
123 sizeof(struct pipe_shader_state
));
124 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
125 hash_key
, CSO_FRAGMENT_SHADER
,
127 if (cso_hash_iter_is_null(iter
)) {
128 struct cso_fragment_shader
*cso
= malloc(sizeof(struct cso_fragment_shader
));
129 memcpy(&cso
->state
, templ
, sizeof(struct pipe_shader_state
));
130 cso
->data
= st
->pipe
->create_fs_state(st
->pipe
, templ
);
132 cso
->data
= &cso
->state
;
133 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
135 return (struct cso_fragment_shader
*)(cso_hash_iter_data(iter
));
138 const struct cso_vertex_shader
*
139 st_cached_vs_state(struct st_context
*st
,
140 const struct pipe_shader_state
*templ
)
142 unsigned hash_key
= cso_construct_key((void*)templ
,
143 sizeof(struct pipe_shader_state
));
144 struct cso_hash_iter iter
= cso_find_state_template(st
->cache
,
145 hash_key
, CSO_VERTEX_SHADER
,
147 if (cso_hash_iter_is_null(iter
)) {
148 struct cso_vertex_shader
*cso
= malloc(sizeof(struct cso_vertex_shader
));
149 memcpy(&cso
->state
, templ
, sizeof(struct pipe_shader_state
));
150 cso
->data
= st
->pipe
->create_vs_state(st
->pipe
, templ
);
152 cso
->data
= &cso
->state
;
153 iter
= cso_insert_state(st
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
155 return (struct cso_vertex_shader
*)(cso_hash_iter_data(iter
));