gallium: unused var silence warning
[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 "cso_cache/cso_cache.h"
40 #include "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 cso->delete_state = (cso_state_callback)st->pipe->delete_blend_state;
67 cso->context = st->pipe;
68 iter = cso_insert_state(st->cache, hash_key, CSO_BLEND, cso);
69 }
70 return ((struct cso_blend *)cso_hash_iter_data(iter));
71 }
72
73 const struct cso_sampler *
74 st_cached_sampler_state(struct st_context *st,
75 const struct pipe_sampler_state *templ)
76 {
77 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
78 struct cso_hash_iter iter = cso_find_state_template(st->cache,
79 hash_key, CSO_SAMPLER,
80 (void*)templ);
81 if (cso_hash_iter_is_null(iter)) {
82 struct cso_sampler *cso = malloc(sizeof(struct cso_sampler));
83 memcpy(&cso->state, templ, sizeof(struct pipe_sampler_state));
84 cso->data = st->pipe->create_sampler_state(st->pipe, &cso->state);
85 if (!cso->data)
86 cso->data = &cso->state;
87 cso->delete_state = (cso_state_callback)st->pipe->delete_sampler_state;
88 cso->context = st->pipe;
89 iter = cso_insert_state(st->cache, hash_key, CSO_SAMPLER, cso);
90 }
91 return (struct cso_sampler*)(cso_hash_iter_data(iter));
92 }
93
94 const struct cso_depth_stencil_alpha *
95 st_cached_depth_stencil_alpha_state(struct st_context *st,
96 const struct pipe_depth_stencil_alpha_state *templ)
97 {
98 unsigned hash_key = cso_construct_key((void*)templ,
99 sizeof(struct pipe_depth_stencil_alpha_state));
100 struct cso_hash_iter iter = cso_find_state_template(st->cache,
101 hash_key,
102 CSO_DEPTH_STENCIL_ALPHA,
103 (void*)templ);
104 if (cso_hash_iter_is_null(iter)) {
105 struct cso_depth_stencil_alpha *cso = malloc(sizeof(struct cso_depth_stencil_alpha));
106 memcpy(&cso->state, templ, sizeof(struct pipe_depth_stencil_alpha_state));
107 cso->data = st->pipe->create_depth_stencil_alpha_state(st->pipe, &cso->state);
108 if (!cso->data)
109 cso->data = &cso->state;
110 cso->delete_state = (cso_state_callback)st->pipe->delete_depth_stencil_alpha_state;
111 cso->context = st->pipe;
112 iter = cso_insert_state(st->cache, hash_key, CSO_DEPTH_STENCIL_ALPHA, cso);
113 }
114 return (struct cso_depth_stencil_alpha*)(cso_hash_iter_data(iter));
115 }
116
117 const struct cso_rasterizer* st_cached_rasterizer_state(
118 struct st_context *st,
119 const struct pipe_rasterizer_state *templ)
120 {
121 unsigned hash_key = cso_construct_key((void*)templ,
122 sizeof(struct pipe_rasterizer_state));
123 struct cso_hash_iter iter = cso_find_state_template(st->cache,
124 hash_key, CSO_RASTERIZER,
125 (void*)templ);
126 if (cso_hash_iter_is_null(iter)) {
127 struct cso_rasterizer *cso = malloc(sizeof(struct cso_rasterizer));
128 memcpy(&cso->state, templ, sizeof(struct pipe_rasterizer_state));
129 cso->data = st->pipe->create_rasterizer_state(st->pipe, &cso->state);
130 if (!cso->data)
131 cso->data = &cso->state;
132 cso->delete_state = (cso_state_callback)st->pipe->delete_rasterizer_state;
133 cso->context = st->pipe;
134 iter = cso_insert_state(st->cache, hash_key, CSO_RASTERIZER, cso);
135 }
136 return (struct cso_rasterizer*)(cso_hash_iter_data(iter));
137 }
138
139 const struct cso_fragment_shader *
140 st_cached_fs_state(struct st_context *st,
141 const struct pipe_shader_state *templ)
142 {
143 unsigned hash_key = cso_construct_key((void*)templ,
144 sizeof(struct pipe_shader_state));
145 struct cso_hash_iter iter = cso_find_state_template(st->cache,
146 hash_key, CSO_FRAGMENT_SHADER,
147 (void*)templ);
148 if (cso_hash_iter_is_null(iter)) {
149 struct cso_fragment_shader *cso = malloc(sizeof(struct cso_fragment_shader));
150 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
151 cso->data = st->pipe->create_fs_state(st->pipe, &cso->state);
152 if (!cso->data)
153 cso->data = &cso->state;
154 cso->delete_state = (cso_state_callback)st->pipe->delete_fs_state;
155 cso->context = st->pipe;
156 iter = cso_insert_state(st->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
157 }
158 return (struct cso_fragment_shader*)(cso_hash_iter_data(iter));
159 }
160
161 const struct cso_vertex_shader *
162 st_cached_vs_state(struct st_context *st,
163 const struct pipe_shader_state *templ)
164 {
165 unsigned hash_key = cso_construct_key((void*)templ,
166 sizeof(struct pipe_shader_state));
167 struct cso_hash_iter iter = cso_find_state_template(st->cache,
168 hash_key, CSO_VERTEX_SHADER,
169 (void*)templ);
170 if (cso_hash_iter_is_null(iter)) {
171 struct cso_vertex_shader *cso = malloc(sizeof(struct cso_vertex_shader));
172 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
173 cso->data = st->pipe->create_vs_state(st->pipe, &cso->state);
174 if (!cso->data)
175 cso->data = &cso->state;
176 cso->delete_state = (cso_state_callback)st->pipe->delete_vs_state;
177 cso->context = st->pipe;
178 iter = cso_insert_state(st->cache, hash_key, CSO_VERTEX_SHADER, cso);
179 }
180 return (struct cso_vertex_shader*)(cso_hash_iter_data(iter));
181 }