Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / softpipe / sp_state_sampler.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 /* Authors:
29 * Brian Paul
30 */
31
32 #include "util/u_memory.h"
33
34 #include "draw/draw_context.h"
35 #include "draw/draw_context.h"
36
37 #include "sp_context.h"
38 #include "sp_state.h"
39 #include "sp_texture.h"
40 #include "sp_tex_sample.h"
41 #include "sp_tex_tile_cache.h"
42
43
44 struct sp_sampler {
45 struct pipe_sampler_state base;
46 struct sp_sampler_varient *varients;
47 struct sp_sampler_varient *current;
48 };
49
50 static struct sp_sampler *sp_sampler( struct pipe_sampler_state *sampler )
51 {
52 return (struct sp_sampler *)sampler;
53 }
54
55
56 void *
57 softpipe_create_sampler_state(struct pipe_context *pipe,
58 const struct pipe_sampler_state *sampler)
59 {
60 struct sp_sampler *sp_sampler = CALLOC_STRUCT(sp_sampler);
61
62 sp_sampler->base = *sampler;
63 sp_sampler->varients = NULL;
64
65 return (void *)sp_sampler;
66 }
67
68
69 void
70 softpipe_bind_sampler_states(struct pipe_context *pipe,
71 unsigned num, void **sampler)
72 {
73 struct softpipe_context *softpipe = softpipe_context(pipe);
74 unsigned i;
75
76 assert(num <= PIPE_MAX_SAMPLERS);
77
78 /* Check for no-op */
79 if (num == softpipe->num_samplers &&
80 !memcmp(softpipe->sampler, sampler, num * sizeof(void *)))
81 return;
82
83 draw_flush(softpipe->draw);
84
85 for (i = 0; i < num; ++i)
86 softpipe->sampler[i] = sampler[i];
87 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
88 softpipe->sampler[i] = NULL;
89
90 softpipe->num_samplers = num;
91
92 softpipe->dirty |= SP_NEW_SAMPLER;
93 }
94
95
96 void
97 softpipe_bind_vertex_sampler_states(struct pipe_context *pipe,
98 unsigned num_samplers,
99 void **samplers)
100 {
101 struct softpipe_context *softpipe = softpipe_context(pipe);
102 unsigned i;
103
104 assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
105
106 /* Check for no-op */
107 if (num_samplers == softpipe->num_vertex_samplers &&
108 !memcmp(softpipe->vertex_samplers, samplers, num_samplers * sizeof(void *)))
109 return;
110
111 draw_flush(softpipe->draw);
112
113 for (i = 0; i < num_samplers; ++i)
114 softpipe->vertex_samplers[i] = samplers[i];
115 for (i = num_samplers; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
116 softpipe->vertex_samplers[i] = NULL;
117
118 softpipe->num_vertex_samplers = num_samplers;
119
120 softpipe->dirty |= SP_NEW_SAMPLER;
121 }
122
123
124 void
125 softpipe_set_sampler_textures(struct pipe_context *pipe,
126 unsigned num, struct pipe_texture **texture)
127 {
128 struct softpipe_context *softpipe = softpipe_context(pipe);
129 uint i;
130
131 assert(num <= PIPE_MAX_SAMPLERS);
132
133 /* Check for no-op */
134 if (num == softpipe->num_textures &&
135 !memcmp(softpipe->texture, texture, num * sizeof(struct pipe_texture *)))
136 return;
137
138 draw_flush(softpipe->draw);
139
140 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
141 struct pipe_texture *tex = i < num ? texture[i] : NULL;
142
143 pipe_texture_reference(&softpipe->texture[i], tex);
144 sp_tex_tile_cache_set_texture(softpipe->tex_cache[i], tex);
145 }
146
147 softpipe->num_textures = num;
148
149 softpipe->dirty |= SP_NEW_TEXTURE;
150 }
151
152
153 void
154 softpipe_set_vertex_sampler_textures(struct pipe_context *pipe,
155 unsigned num_textures,
156 struct pipe_texture **textures)
157 {
158 struct softpipe_context *softpipe = softpipe_context(pipe);
159 uint i;
160
161 assert(num_textures <= PIPE_MAX_VERTEX_SAMPLERS);
162
163 /* Check for no-op */
164 if (num_textures == softpipe->num_vertex_textures &&
165 !memcmp(softpipe->vertex_textures, textures, num_textures * sizeof(struct pipe_texture *))) {
166 return;
167 }
168
169 draw_flush(softpipe->draw);
170
171 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
172 struct pipe_texture *tex = i < num_textures ? textures[i] : NULL;
173
174 pipe_texture_reference(&softpipe->vertex_textures[i], tex);
175 sp_tex_tile_cache_set_texture(softpipe->vertex_tex_cache[i], tex);
176 }
177
178 softpipe->num_vertex_textures = num_textures;
179
180 softpipe->dirty |= SP_NEW_TEXTURE;
181 }
182
183
184 /**
185 * Find/create an sp_sampler_varient object for sampling the given texture,
186 * sampler and tex unit.
187 *
188 * Note that the tex unit is significant. We can't re-use a sampler
189 * varient for multiple texture units because the sampler varient contains
190 * the texture object pointer. If the texture object pointer were stored
191 * somewhere outside the sampler varient, we could re-use samplers for
192 * multiple texture units.
193 */
194 static struct sp_sampler_varient *
195 get_sampler_varient( unsigned unit,
196 struct sp_sampler *sampler,
197 struct pipe_texture *texture,
198 unsigned processor )
199 {
200 struct softpipe_texture *sp_texture = softpipe_texture(texture);
201 struct sp_sampler_varient *v = NULL;
202 union sp_sampler_key key;
203
204 /* if this fails, widen the key.unit field and update this assertion */
205 assert(PIPE_MAX_SAMPLERS <= 16);
206
207 key.bits.target = sp_texture->base.target;
208 key.bits.is_pot = sp_texture->pot;
209 key.bits.processor = processor;
210 key.bits.unit = unit;
211 key.bits.pad = 0;
212
213 if (sampler->current &&
214 key.value == sampler->current->key.value) {
215 v = sampler->current;
216 }
217
218 if (v == NULL) {
219 for (v = sampler->varients; v; v = v->next)
220 if (v->key.value == key.value)
221 break;
222
223 if (v == NULL) {
224 v = sp_create_sampler_varient( &sampler->base, key );
225 v->next = sampler->varients;
226 sampler->varients = v;
227 }
228 }
229
230 sampler->current = v;
231 return v;
232 }
233
234
235
236
237 void
238 softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
239 {
240 int i;
241
242 /* It's a bit hard to build these samplers ahead of time -- don't
243 * really know which samplers are going to be used for vertex and
244 * fragment programs.
245 */
246 for (i = 0; i <= softpipe->vs->max_sampler; i++) {
247 if (softpipe->vertex_samplers[i]) {
248 softpipe->tgsi.vert_samplers_list[i] =
249 get_sampler_varient( i,
250 sp_sampler(softpipe->vertex_samplers[i]),
251 softpipe->vertex_textures[i],
252 TGSI_PROCESSOR_VERTEX );
253
254 sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
255 softpipe->vertex_tex_cache[i],
256 softpipe->vertex_textures[i] );
257 }
258 }
259
260 for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
261 if (softpipe->sampler[i]) {
262 softpipe->tgsi.frag_samplers_list[i] =
263 get_sampler_varient( i,
264 sp_sampler(softpipe->sampler[i]),
265 softpipe->texture[i],
266 TGSI_PROCESSOR_FRAGMENT );
267
268 sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
269 softpipe->tex_cache[i],
270 softpipe->texture[i] );
271 }
272 }
273 }
274
275
276
277 void
278 softpipe_delete_sampler_state(struct pipe_context *pipe,
279 void *sampler)
280 {
281 struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
282 struct sp_sampler_varient *v, *tmp;
283
284 for (v = sp_sampler->varients; v; v = tmp) {
285 tmp = v->next;
286 sp_sampler_varient_destroy(v);
287 }
288
289 FREE( sampler );
290 }
291
292
293