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