Merge branch 'shader-file-reorg'
[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 #include "util/u_inlines.h"
34
35 #include "draw/draw_context.h"
36 #include "draw/draw_context.h"
37
38 #include "sp_context.h"
39 #include "sp_state.h"
40 #include "sp_texture.h"
41 #include "sp_tex_sample.h"
42 #include "sp_tex_tile_cache.h"
43
44
45 struct sp_sampler {
46 struct pipe_sampler_state base;
47 struct sp_sampler_varient *varients;
48 struct sp_sampler_varient *current;
49 };
50
51 static struct sp_sampler *sp_sampler( struct pipe_sampler_state *sampler )
52 {
53 return (struct sp_sampler *)sampler;
54 }
55
56
57 void *
58 softpipe_create_sampler_state(struct pipe_context *pipe,
59 const struct pipe_sampler_state *sampler)
60 {
61 struct sp_sampler *sp_sampler = CALLOC_STRUCT(sp_sampler);
62
63 sp_sampler->base = *sampler;
64 sp_sampler->varients = NULL;
65
66 return (void *)sp_sampler;
67 }
68
69
70 void
71 softpipe_bind_sampler_states(struct pipe_context *pipe,
72 unsigned num, void **sampler)
73 {
74 struct softpipe_context *softpipe = softpipe_context(pipe);
75 unsigned i;
76
77 assert(num <= PIPE_MAX_SAMPLERS);
78
79 /* Check for no-op */
80 if (num == softpipe->num_samplers &&
81 !memcmp(softpipe->sampler, sampler, num * sizeof(void *)))
82 return;
83
84 draw_flush(softpipe->draw);
85
86 for (i = 0; i < num; ++i)
87 softpipe->sampler[i] = sampler[i];
88 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
89 softpipe->sampler[i] = NULL;
90
91 softpipe->num_samplers = num;
92
93 softpipe->dirty |= SP_NEW_SAMPLER;
94 }
95
96
97 void
98 softpipe_bind_vertex_sampler_states(struct pipe_context *pipe,
99 unsigned num_samplers,
100 void **samplers)
101 {
102 struct softpipe_context *softpipe = softpipe_context(pipe);
103 unsigned i;
104
105 assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
106
107 /* Check for no-op */
108 if (num_samplers == softpipe->num_vertex_samplers &&
109 !memcmp(softpipe->vertex_samplers, samplers, num_samplers * sizeof(void *)))
110 return;
111
112 draw_flush(softpipe->draw);
113
114 for (i = 0; i < num_samplers; ++i)
115 softpipe->vertex_samplers[i] = samplers[i];
116 for (i = num_samplers; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
117 softpipe->vertex_samplers[i] = NULL;
118
119 softpipe->num_vertex_samplers = num_samplers;
120
121 softpipe->dirty |= SP_NEW_SAMPLER;
122 }
123
124 void
125 softpipe_bind_geometry_sampler_states(struct pipe_context *pipe,
126 unsigned num_samplers,
127 void **samplers)
128 {
129 struct softpipe_context *softpipe = softpipe_context(pipe);
130 unsigned i;
131
132 assert(num_samplers <= PIPE_MAX_GEOMETRY_SAMPLERS);
133
134 /* Check for no-op */
135 if (num_samplers == softpipe->num_geometry_samplers &&
136 !memcmp(softpipe->geometry_samplers, samplers, num_samplers * sizeof(void *)))
137 return;
138
139 draw_flush(softpipe->draw);
140
141 for (i = 0; i < num_samplers; ++i)
142 softpipe->geometry_samplers[i] = samplers[i];
143 for (i = num_samplers; i < PIPE_MAX_GEOMETRY_SAMPLERS; ++i)
144 softpipe->geometry_samplers[i] = NULL;
145
146 softpipe->num_geometry_samplers = num_samplers;
147
148 softpipe->dirty |= SP_NEW_SAMPLER;
149 }
150
151
152 struct pipe_sampler_view *
153 softpipe_create_sampler_view(struct pipe_context *pipe,
154 struct pipe_resource *resource,
155 const struct pipe_sampler_view *templ)
156 {
157 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
158
159 if (view) {
160 *view = *templ;
161 view->reference.count = 1;
162 view->texture = NULL;
163 pipe_resource_reference(&view->texture, resource);
164 view->context = pipe;
165 }
166
167 return view;
168 }
169
170
171 void
172 softpipe_sampler_view_destroy(struct pipe_context *pipe,
173 struct pipe_sampler_view *view)
174 {
175 pipe_resource_reference(&view->texture, NULL);
176 FREE(view);
177 }
178
179
180 void
181 softpipe_set_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_SAMPLERS);
189
190 /* Check for no-op */
191 if (num == softpipe->num_sampler_views &&
192 !memcmp(softpipe->sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
193 return;
194
195 draw_flush(softpipe->draw);
196
197 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
198 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
199
200 pipe_sampler_view_reference(&softpipe->sampler_views[i], view);
201 sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[i], view);
202 }
203
204 softpipe->num_sampler_views = num;
205
206 softpipe->dirty |= SP_NEW_TEXTURE;
207 }
208
209
210 void
211 softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
212 unsigned num,
213 struct pipe_sampler_view **views)
214 {
215 struct softpipe_context *softpipe = softpipe_context(pipe);
216 uint i;
217
218 assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
219
220 /* Check for no-op */
221 if (num == softpipe->num_vertex_sampler_views &&
222 !memcmp(softpipe->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
223 return;
224 }
225
226 draw_flush(softpipe->draw);
227
228 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
229 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
230
231 pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], view);
232 sp_tex_tile_cache_set_sampler_view(softpipe->vertex_tex_cache[i], view);
233 }
234
235 softpipe->num_vertex_sampler_views = num;
236
237 softpipe->dirty |= SP_NEW_TEXTURE;
238 }
239
240 void
241 softpipe_set_geometry_sampler_views(struct pipe_context *pipe,
242 unsigned num,
243 struct pipe_sampler_view **views)
244 {
245 struct softpipe_context *softpipe = softpipe_context(pipe);
246 uint i;
247
248 assert(num <= PIPE_MAX_GEOMETRY_SAMPLERS);
249
250 /* Check for no-op */
251 if (num == softpipe->num_geometry_sampler_views &&
252 !memcmp(softpipe->geometry_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
253 return;
254 }
255
256 draw_flush(softpipe->draw);
257
258 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
259 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
260
261 pipe_sampler_view_reference(&softpipe->geometry_sampler_views[i], view);
262 sp_tex_tile_cache_set_sampler_view(softpipe->geometry_tex_cache[i], view);
263 }
264
265 softpipe->num_geometry_sampler_views = num;
266
267 softpipe->dirty |= SP_NEW_TEXTURE;
268 }
269
270
271 /**
272 * Find/create an sp_sampler_varient object for sampling the given texture,
273 * sampler and tex unit.
274 *
275 * Note that the tex unit is significant. We can't re-use a sampler
276 * varient for multiple texture units because the sampler varient contains
277 * the texture object pointer. If the texture object pointer were stored
278 * somewhere outside the sampler varient, we could re-use samplers for
279 * multiple texture units.
280 */
281 static struct sp_sampler_varient *
282 get_sampler_varient( unsigned unit,
283 struct sp_sampler *sampler,
284 struct pipe_resource *resource,
285 unsigned processor )
286 {
287 struct softpipe_resource *sp_texture = softpipe_resource(resource);
288 struct sp_sampler_varient *v = NULL;
289 union sp_sampler_key key;
290
291 /* if this fails, widen the key.unit field and update this assertion */
292 assert(PIPE_MAX_SAMPLERS <= 16);
293
294 key.bits.target = sp_texture->base.target;
295 key.bits.is_pot = sp_texture->pot;
296 key.bits.processor = processor;
297 key.bits.unit = unit;
298 key.bits.pad = 0;
299
300 if (sampler->current &&
301 key.value == sampler->current->key.value) {
302 v = sampler->current;
303 }
304
305 if (v == NULL) {
306 for (v = sampler->varients; v; v = v->next)
307 if (v->key.value == key.value)
308 break;
309
310 if (v == NULL) {
311 v = sp_create_sampler_varient( &sampler->base, key );
312 v->next = sampler->varients;
313 sampler->varients = v;
314 }
315 }
316
317 sampler->current = v;
318 return v;
319 }
320
321
322
323
324 void
325 softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
326 {
327 int i;
328
329 /* It's a bit hard to build these samplers ahead of time -- don't
330 * really know which samplers are going to be used for vertex and
331 * fragment programs.
332 */
333 for (i = 0; i <= softpipe->vs->max_sampler; i++) {
334 if (softpipe->vertex_samplers[i]) {
335 struct pipe_resource *texture = NULL;
336
337 if (softpipe->vertex_sampler_views[i]) {
338 texture = softpipe->vertex_sampler_views[i]->texture;
339 }
340
341 softpipe->tgsi.vert_samplers_list[i] =
342 get_sampler_varient( i,
343 sp_sampler(softpipe->vertex_samplers[i]),
344 texture,
345 TGSI_PROCESSOR_VERTEX );
346
347 sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
348 softpipe->vertex_tex_cache[i],
349 texture );
350 }
351 }
352
353 if (softpipe->gs) {
354 for (i = 0; i <= softpipe->gs->max_sampler; i++) {
355 if (softpipe->geometry_samplers[i]) {
356 struct pipe_resource *texture = NULL;
357
358 if (softpipe->geometry_sampler_views[i]) {
359 texture = softpipe->geometry_sampler_views[i]->texture;
360 }
361
362 softpipe->tgsi.geom_samplers_list[i] =
363 get_sampler_varient(
364 i,
365 sp_sampler(softpipe->geometry_samplers[i]),
366 texture,
367 TGSI_PROCESSOR_GEOMETRY );
368
369 sp_sampler_varient_bind_texture(
370 softpipe->tgsi.geom_samplers_list[i],
371 softpipe->geometry_tex_cache[i],
372 texture );
373 }
374 }
375 }
376
377 for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
378 if (softpipe->sampler[i]) {
379 struct pipe_resource *texture = NULL;
380
381 if (softpipe->sampler_views[i]) {
382 texture = softpipe->sampler_views[i]->texture;
383 }
384
385 softpipe->tgsi.frag_samplers_list[i] =
386 get_sampler_varient( i,
387 sp_sampler(softpipe->sampler[i]),
388 texture,
389 TGSI_PROCESSOR_FRAGMENT );
390
391 sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
392 softpipe->tex_cache[i],
393 texture );
394 }
395 }
396 }
397
398
399
400 void
401 softpipe_delete_sampler_state(struct pipe_context *pipe,
402 void *sampler)
403 {
404 struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
405 struct sp_sampler_varient *v, *tmp;
406
407 for (v = sp_sampler->varients; v; v = tmp) {
408 tmp = v->next;
409 sp_sampler_varient_destroy(v);
410 }
411
412 FREE( sampler );
413 }
414
415
416