Merge branch 'master' into gallium-sampler-view
[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 struct pipe_sampler_view *
125 softpipe_create_sampler_view(struct pipe_context *pipe,
126 struct pipe_texture *texture,
127 const struct pipe_sampler_view *templ)
128 {
129 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
130
131 *view = *templ;
132 view->reference.count = 1;
133 view->texture = NULL;
134 pipe_texture_reference(&view->texture, texture);
135 view->context = pipe;
136
137 return view;
138 }
139
140
141 void
142 softpipe_sampler_view_destroy(struct pipe_context *pipe,
143 struct pipe_sampler_view *view)
144 {
145 pipe_texture_reference(&view->texture, NULL);
146 FREE(view);
147 }
148
149
150 void
151 softpipe_set_sampler_views(struct pipe_context *pipe,
152 unsigned num,
153 struct pipe_sampler_view **views)
154 {
155 struct softpipe_context *softpipe = softpipe_context(pipe);
156 uint i;
157
158 assert(num <= PIPE_MAX_SAMPLERS);
159
160 /* Check for no-op */
161 if (num == softpipe->num_sampler_views &&
162 !memcmp(softpipe->sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
163 return;
164
165 draw_flush(softpipe->draw);
166
167 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
168 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
169
170 pipe_sampler_view_reference(&softpipe->sampler_views[i], view);
171 sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[i], view);
172 }
173
174 softpipe->num_sampler_views = num;
175
176 softpipe->dirty |= SP_NEW_TEXTURE;
177 }
178
179
180 void
181 softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
182 unsigned num,
183 struct pipe_sampler_view **views)
184 {
185 struct softpipe_context *softpipe = softpipe_context(pipe);
186 uint i;
187
188 assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
189
190 /* Check for no-op */
191 if (num == softpipe->num_vertex_sampler_views &&
192 !memcmp(softpipe->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
193 return;
194 }
195
196 draw_flush(softpipe->draw);
197
198 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
199 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
200
201 pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], view);
202 sp_tex_tile_cache_set_sampler_view(softpipe->vertex_tex_cache[i], view);
203 }
204
205 softpipe->num_vertex_sampler_views = num;
206
207 softpipe->dirty |= SP_NEW_TEXTURE;
208 }
209
210
211 /**
212 * Find/create an sp_sampler_varient object for sampling the given texture,
213 * sampler and tex unit.
214 *
215 * Note that the tex unit is significant. We can't re-use a sampler
216 * varient for multiple texture units because the sampler varient contains
217 * the texture object pointer. If the texture object pointer were stored
218 * somewhere outside the sampler varient, we could re-use samplers for
219 * multiple texture units.
220 */
221 static struct sp_sampler_varient *
222 get_sampler_varient( unsigned unit,
223 struct sp_sampler *sampler,
224 struct pipe_texture *texture,
225 unsigned processor )
226 {
227 struct softpipe_texture *sp_texture = softpipe_texture(texture);
228 struct sp_sampler_varient *v = NULL;
229 union sp_sampler_key key;
230
231 /* if this fails, widen the key.unit field and update this assertion */
232 assert(PIPE_MAX_SAMPLERS <= 16);
233
234 key.bits.target = sp_texture->base.target;
235 key.bits.is_pot = sp_texture->pot;
236 key.bits.processor = processor;
237 key.bits.unit = unit;
238 key.bits.pad = 0;
239
240 if (sampler->current &&
241 key.value == sampler->current->key.value) {
242 v = sampler->current;
243 }
244
245 if (v == NULL) {
246 for (v = sampler->varients; v; v = v->next)
247 if (v->key.value == key.value)
248 break;
249
250 if (v == NULL) {
251 v = sp_create_sampler_varient( &sampler->base, key );
252 v->next = sampler->varients;
253 sampler->varients = v;
254 }
255 }
256
257 sampler->current = v;
258 return v;
259 }
260
261
262
263
264 void
265 softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
266 {
267 int i;
268
269 /* It's a bit hard to build these samplers ahead of time -- don't
270 * really know which samplers are going to be used for vertex and
271 * fragment programs.
272 */
273 for (i = 0; i <= softpipe->vs->max_sampler; i++) {
274 if (softpipe->vertex_samplers[i]) {
275 struct pipe_texture *texture = NULL;
276
277 if (softpipe->vertex_sampler_views[i]) {
278 texture = softpipe->vertex_sampler_views[i]->texture;
279 }
280
281 softpipe->tgsi.vert_samplers_list[i] =
282 get_sampler_varient( i,
283 sp_sampler(softpipe->vertex_samplers[i]),
284 texture,
285 TGSI_PROCESSOR_VERTEX );
286
287 sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
288 softpipe->vertex_tex_cache[i],
289 texture );
290 }
291 }
292
293 for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
294 if (softpipe->sampler[i]) {
295 struct pipe_texture *texture = NULL;
296
297 if (softpipe->sampler_views[i]) {
298 texture = softpipe->sampler_views[i]->texture;
299 }
300
301 softpipe->tgsi.frag_samplers_list[i] =
302 get_sampler_varient( i,
303 sp_sampler(softpipe->sampler[i]),
304 texture,
305 TGSI_PROCESSOR_FRAGMENT );
306
307 sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
308 softpipe->tex_cache[i],
309 texture );
310 }
311 }
312 }
313
314
315
316 void
317 softpipe_delete_sampler_state(struct pipe_context *pipe,
318 void *sampler)
319 {
320 struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
321 struct sp_sampler_varient *v, *tmp;
322
323 for (v = sp_sampler->varients; v; v = tmp) {
324 tmp = v->next;
325 sp_sampler_varient_destroy(v);
326 }
327
328 FREE( sampler );
329 }
330
331
332