softpipe: make sampler state functions static
[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 static 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 static 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 static 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 draw_set_samplers(softpipe->draw,
122 softpipe->vertex_samplers,
123 softpipe->num_vertex_samplers);
124
125 softpipe->dirty |= SP_NEW_SAMPLER;
126 }
127
128 static void
129 softpipe_bind_geometry_sampler_states(struct pipe_context *pipe,
130 unsigned num_samplers,
131 void **samplers)
132 {
133 struct softpipe_context *softpipe = softpipe_context(pipe);
134 unsigned i;
135
136 assert(num_samplers <= PIPE_MAX_GEOMETRY_SAMPLERS);
137
138 /* Check for no-op */
139 if (num_samplers == softpipe->num_geometry_samplers &&
140 !memcmp(softpipe->geometry_samplers, samplers, num_samplers * sizeof(void *)))
141 return;
142
143 draw_flush(softpipe->draw);
144
145 for (i = 0; i < num_samplers; ++i)
146 softpipe->geometry_samplers[i] = samplers[i];
147 for (i = num_samplers; i < PIPE_MAX_GEOMETRY_SAMPLERS; ++i)
148 softpipe->geometry_samplers[i] = NULL;
149
150 softpipe->num_geometry_samplers = num_samplers;
151
152 softpipe->dirty |= SP_NEW_SAMPLER;
153 }
154
155
156 static struct pipe_sampler_view *
157 softpipe_create_sampler_view(struct pipe_context *pipe,
158 struct pipe_resource *resource,
159 const struct pipe_sampler_view *templ)
160 {
161 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
162
163 if (view) {
164 *view = *templ;
165 view->reference.count = 1;
166 view->texture = NULL;
167 pipe_resource_reference(&view->texture, resource);
168 view->context = pipe;
169 }
170
171 return view;
172 }
173
174
175 static void
176 softpipe_sampler_view_destroy(struct pipe_context *pipe,
177 struct pipe_sampler_view *view)
178 {
179 pipe_resource_reference(&view->texture, NULL);
180 FREE(view);
181 }
182
183
184 static void
185 softpipe_set_sampler_views(struct pipe_context *pipe,
186 unsigned num,
187 struct pipe_sampler_view **views)
188 {
189 struct softpipe_context *softpipe = softpipe_context(pipe);
190 uint i;
191
192 assert(num <= PIPE_MAX_SAMPLERS);
193
194 /* Check for no-op */
195 if (num == softpipe->num_sampler_views &&
196 !memcmp(softpipe->sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
197 return;
198
199 draw_flush(softpipe->draw);
200
201 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
202 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
203
204 pipe_sampler_view_reference(&softpipe->sampler_views[i], view);
205 sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[i], view);
206 }
207
208 softpipe->num_sampler_views = num;
209
210 softpipe->dirty |= SP_NEW_TEXTURE;
211 }
212
213
214 static void
215 softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
216 unsigned num,
217 struct pipe_sampler_view **views)
218 {
219 struct softpipe_context *softpipe = softpipe_context(pipe);
220 uint i;
221
222 assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
223
224 /* Check for no-op */
225 if (num == softpipe->num_vertex_sampler_views &&
226 !memcmp(softpipe->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
227 return;
228 }
229
230 draw_flush(softpipe->draw);
231
232 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
233 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
234
235 pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], view);
236 sp_tex_tile_cache_set_sampler_view(softpipe->vertex_tex_cache[i], view);
237 }
238
239 softpipe->num_vertex_sampler_views = num;
240
241 draw_set_sampler_views(softpipe->draw,
242 softpipe->vertex_sampler_views,
243 softpipe->num_vertex_sampler_views);
244
245 softpipe->dirty |= SP_NEW_TEXTURE;
246 }
247
248
249 static void
250 softpipe_set_geometry_sampler_views(struct pipe_context *pipe,
251 unsigned num,
252 struct pipe_sampler_view **views)
253 {
254 struct softpipe_context *softpipe = softpipe_context(pipe);
255 uint i;
256
257 assert(num <= PIPE_MAX_GEOMETRY_SAMPLERS);
258
259 /* Check for no-op */
260 if (num == softpipe->num_geometry_sampler_views &&
261 !memcmp(softpipe->geometry_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
262 return;
263 }
264
265 draw_flush(softpipe->draw);
266
267 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
268 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
269
270 pipe_sampler_view_reference(&softpipe->geometry_sampler_views[i], view);
271 sp_tex_tile_cache_set_sampler_view(softpipe->geometry_tex_cache[i], view);
272 }
273
274 softpipe->num_geometry_sampler_views = num;
275
276 softpipe->dirty |= SP_NEW_TEXTURE;
277 }
278
279
280 /**
281 * Find/create an sp_sampler_varient object for sampling the given texture,
282 * sampler and tex unit.
283 *
284 * Note that the tex unit is significant. We can't re-use a sampler
285 * varient for multiple texture units because the sampler varient contains
286 * the texture object pointer. If the texture object pointer were stored
287 * somewhere outside the sampler varient, we could re-use samplers for
288 * multiple texture units.
289 */
290 static struct sp_sampler_varient *
291 get_sampler_varient( unsigned unit,
292 struct sp_sampler *sampler,
293 struct pipe_resource *resource,
294 unsigned processor )
295 {
296 struct softpipe_resource *sp_texture = softpipe_resource(resource);
297 struct sp_sampler_varient *v = NULL;
298 union sp_sampler_key key;
299
300 /* if this fails, widen the key.unit field and update this assertion */
301 assert(PIPE_MAX_SAMPLERS <= 16);
302
303 key.bits.target = sp_texture->base.target;
304 key.bits.is_pot = sp_texture->pot;
305 key.bits.processor = processor;
306 key.bits.unit = unit;
307 key.bits.pad = 0;
308
309 if (sampler->current &&
310 key.value == sampler->current->key.value) {
311 v = sampler->current;
312 }
313
314 if (v == NULL) {
315 for (v = sampler->varients; v; v = v->next)
316 if (v->key.value == key.value)
317 break;
318
319 if (v == NULL) {
320 v = sp_create_sampler_varient( &sampler->base, key );
321 v->next = sampler->varients;
322 sampler->varients = v;
323 }
324 }
325
326 sampler->current = v;
327 return v;
328 }
329
330
331 void
332 softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
333 {
334 int i;
335
336 /* It's a bit hard to build these samplers ahead of time -- don't
337 * really know which samplers are going to be used for vertex and
338 * fragment programs.
339 */
340 for (i = 0; i <= softpipe->vs->max_sampler; i++) {
341 if (softpipe->vertex_samplers[i]) {
342 struct pipe_resource *texture = NULL;
343
344 if (softpipe->vertex_sampler_views[i]) {
345 texture = softpipe->vertex_sampler_views[i]->texture;
346 }
347
348 softpipe->tgsi.vert_samplers_list[i] =
349 get_sampler_varient( i,
350 sp_sampler(softpipe->vertex_samplers[i]),
351 texture,
352 TGSI_PROCESSOR_VERTEX );
353
354 sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
355 softpipe->vertex_tex_cache[i],
356 texture );
357 }
358 }
359
360 if (softpipe->gs) {
361 for (i = 0; i <= softpipe->gs->max_sampler; i++) {
362 if (softpipe->geometry_samplers[i]) {
363 struct pipe_resource *texture = NULL;
364
365 if (softpipe->geometry_sampler_views[i]) {
366 texture = softpipe->geometry_sampler_views[i]->texture;
367 }
368
369 softpipe->tgsi.geom_samplers_list[i] =
370 get_sampler_varient(
371 i,
372 sp_sampler(softpipe->geometry_samplers[i]),
373 texture,
374 TGSI_PROCESSOR_GEOMETRY );
375
376 sp_sampler_varient_bind_texture(
377 softpipe->tgsi.geom_samplers_list[i],
378 softpipe->geometry_tex_cache[i],
379 texture );
380 }
381 }
382 }
383
384 for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
385 if (softpipe->sampler[i]) {
386 struct pipe_resource *texture = NULL;
387
388 if (softpipe->sampler_views[i]) {
389 texture = softpipe->sampler_views[i]->texture;
390 }
391
392 softpipe->tgsi.frag_samplers_list[i] =
393 get_sampler_varient( i,
394 sp_sampler(softpipe->sampler[i]),
395 texture,
396 TGSI_PROCESSOR_FRAGMENT );
397
398 sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
399 softpipe->tex_cache[i],
400 texture );
401 }
402 }
403 }
404
405 static void
406 softpipe_delete_sampler_state(struct pipe_context *pipe,
407 void *sampler)
408 {
409 struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
410 struct sp_sampler_varient *v, *tmp;
411
412 for (v = sp_sampler->varients; v; v = tmp) {
413 tmp = v->next;
414 sp_sampler_varient_destroy(v);
415 }
416
417 FREE( sampler );
418 }
419
420
421 void
422 softpipe_init_sampler_funcs(struct pipe_context *pipe)
423 {
424 pipe->create_sampler_state = softpipe_create_sampler_state;
425 pipe->bind_fragment_sampler_states = softpipe_bind_sampler_states;
426 pipe->bind_vertex_sampler_states = softpipe_bind_vertex_sampler_states;
427 pipe->bind_geometry_sampler_states = softpipe_bind_geometry_sampler_states;
428 pipe->delete_sampler_state = softpipe_delete_sampler_state;
429
430 pipe->set_fragment_sampler_views = softpipe_set_sampler_views;
431 pipe->set_vertex_sampler_views = softpipe_set_vertex_sampler_views;
432 pipe->set_geometry_sampler_views = softpipe_set_geometry_sampler_views;
433
434 pipe->create_sampler_view = softpipe_create_sampler_view;
435 pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
436 }
437