Adapt for winsys interface changes.
[mesa.git] / src / mesa / state_tracker / st_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 * Authors:
30 * Zack Rusin <zack@tungstengraphics.com>
31 */
32
33 #include "st_cache.h"
34
35 #include "st_context.h"
36
37 #include "pipe/p_state.h"
38
39 #include "pipe/cso_cache/cso_cache.h"
40 #include "pipe/cso_cache/cso_hash.h"
41
42
43 /* Those function will either find the state of the given template
44 * in the cache or they will create a new state from the given
45 * template, insert it in the cache and return it.
46 */
47
48 /*
49 * If the driver returns 0 from the create method then they will assign
50 * the data member of the cso to be the template itself.
51 */
52
53 const struct cso_blend * st_cached_blend_state(struct st_context *st,
54 const struct pipe_blend_state *templ)
55 {
56 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_blend_state));
57 struct cso_hash_iter iter = cso_find_state_template(st->cache,
58 hash_key, CSO_BLEND,
59 (void*)templ);
60 if (cso_hash_iter_is_null(iter)) {
61 struct cso_blend *cso = malloc(sizeof(struct cso_blend));
62 memcpy(&cso->state, templ, sizeof(struct pipe_blend_state));
63 cso->data = st->pipe->create_blend_state(st->pipe, &cso->state);
64 if (!cso->data)
65 cso->data = &cso->state;
66 iter = cso_insert_state(st->cache, hash_key, CSO_BLEND, cso);
67 }
68 return ((struct cso_blend *)cso_hash_iter_data(iter));
69 }
70
71 const struct cso_sampler *
72 st_cached_sampler_state(struct st_context *st,
73 const struct pipe_sampler_state *templ)
74 {
75 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
76 struct cso_hash_iter iter = cso_find_state_template(st->cache,
77 hash_key, CSO_SAMPLER,
78 (void*)templ);
79 if (cso_hash_iter_is_null(iter)) {
80 struct cso_sampler *cso = malloc(sizeof(struct cso_sampler));
81 memcpy(&cso->state, templ, sizeof(struct pipe_sampler_state));
82 cso->data = st->pipe->create_sampler_state(st->pipe, &cso->state);
83 if (!cso->data)
84 cso->data = &cso->state;
85 iter = cso_insert_state(st->cache, hash_key, CSO_SAMPLER, cso);
86 }
87 return (struct cso_sampler*)(cso_hash_iter_data(iter));
88 }
89
90 const struct cso_depth_stencil *
91 st_cached_depth_stencil_state(struct st_context *st,
92 const struct pipe_depth_stencil_state *templ)
93 {
94 unsigned hash_key = cso_construct_key((void*)templ,
95 sizeof(struct pipe_depth_stencil_state));
96 struct cso_hash_iter iter = cso_find_state_template(st->cache,
97 hash_key, CSO_DEPTH_STENCIL,
98 (void*)templ);
99 if (cso_hash_iter_is_null(iter)) {
100 struct cso_depth_stencil *cso = malloc(sizeof(struct cso_depth_stencil));
101 memcpy(&cso->state, templ, sizeof(struct pipe_depth_stencil_state));
102 cso->data = st->pipe->create_depth_stencil_state(st->pipe, &cso->state);
103 if (!cso->data)
104 cso->data = &cso->state;
105 iter = cso_insert_state(st->cache, hash_key, CSO_DEPTH_STENCIL, cso);
106 }
107 return (struct cso_depth_stencil*)(cso_hash_iter_data(iter));
108 }
109
110 const struct cso_rasterizer* st_cached_rasterizer_state(
111 struct st_context *st,
112 const struct pipe_rasterizer_state *templ)
113 {
114 unsigned hash_key = cso_construct_key((void*)templ,
115 sizeof(struct pipe_rasterizer_state));
116 struct cso_hash_iter iter = cso_find_state_template(st->cache,
117 hash_key, CSO_RASTERIZER,
118 (void*)templ);
119 if (cso_hash_iter_is_null(iter)) {
120 struct cso_rasterizer *cso = malloc(sizeof(struct cso_rasterizer));
121 memcpy(&cso->state, templ, sizeof(struct pipe_rasterizer_state));
122 cso->data = st->pipe->create_rasterizer_state(st->pipe, &cso->state);
123 if (!cso->data)
124 cso->data = &cso->state;
125 iter = cso_insert_state(st->cache, hash_key, CSO_RASTERIZER, cso);
126 }
127 return (struct cso_rasterizer*)(cso_hash_iter_data(iter));
128 }
129
130 const struct cso_fragment_shader *
131 st_cached_fs_state(struct st_context *st,
132 const struct pipe_shader_state *templ)
133 {
134 unsigned hash_key = cso_construct_key((void*)templ,
135 sizeof(struct pipe_shader_state));
136 struct cso_hash_iter iter = cso_find_state_template(st->cache,
137 hash_key, CSO_FRAGMENT_SHADER,
138 (void*)templ);
139 if (cso_hash_iter_is_null(iter)) {
140 struct cso_fragment_shader *cso = malloc(sizeof(struct cso_fragment_shader));
141 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
142 cso->data = st->pipe->create_fs_state(st->pipe, &cso->state);
143 if (!cso->data)
144 cso->data = &cso->state;
145 iter = cso_insert_state(st->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
146 }
147 return (struct cso_fragment_shader*)(cso_hash_iter_data(iter));
148 }
149
150 const struct cso_vertex_shader *
151 st_cached_vs_state(struct st_context *st,
152 const struct pipe_shader_state *templ)
153 {
154 unsigned hash_key = cso_construct_key((void*)templ,
155 sizeof(struct pipe_shader_state));
156 struct cso_hash_iter iter = cso_find_state_template(st->cache,
157 hash_key, CSO_VERTEX_SHADER,
158 (void*)templ);
159 if (cso_hash_iter_is_null(iter)) {
160 struct cso_vertex_shader *cso = malloc(sizeof(struct cso_vertex_shader));
161 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
162 cso->data = st->pipe->create_vs_state(st->pipe, &cso->state);
163 if (!cso->data)
164 cso->data = &cso->state;
165 iter = cso_insert_state(st->cache, hash_key, CSO_VERTEX_SHADER, cso);
166 }
167 return (struct cso_vertex_shader*)(cso_hash_iter_data(iter));
168 }
169
170 const struct cso_alpha_test *
171 st_cached_alpha_test_state(struct st_context *st,
172 const struct pipe_alpha_test_state *templ)
173 {
174 unsigned hash_key = cso_construct_key((void*)templ,
175 sizeof(struct pipe_alpha_test_state));
176 struct cso_hash_iter iter = cso_find_state_template(st->cache,
177 hash_key, CSO_ALPHA_TEST,
178 (void*)templ);
179 if (cso_hash_iter_is_null(iter)) {
180 struct cso_alpha_test *cso = malloc(sizeof(struct cso_alpha_test));
181 memcpy(&cso->state, templ, sizeof(struct pipe_alpha_test_state));
182 cso->data = st->pipe->create_alpha_test_state(st->pipe, &cso->state);
183 if (!cso->data)
184 cso->data = &cso->state;
185 iter = cso_insert_state(st->cache, hash_key, CSO_ALPHA_TEST, cso);
186 }
187 return ((struct cso_alpha_test *)cso_hash_iter_data(iter));
188 }