softpipe: rework texture sampling code
[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
36 #include "sp_context.h"
37 #include "sp_context.h"
38 #include "sp_state.h"
39 #include "sp_texture.h"
40 #include "sp_tile_cache.h"
41 #include "sp_tex_sample.h"
42 #include "draw/draw_context.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_set_sampler_textures(struct pipe_context *pipe,
99 unsigned num, struct pipe_texture **texture)
100 {
101 struct softpipe_context *softpipe = softpipe_context(pipe);
102 uint i;
103
104 assert(num <= PIPE_MAX_SAMPLERS);
105
106 /* Check for no-op */
107 if (num == softpipe->num_textures &&
108 !memcmp(softpipe->texture, texture, num * sizeof(struct pipe_texture *)))
109 return;
110
111 draw_flush(softpipe->draw);
112
113 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
114 struct pipe_texture *tex = i < num ? texture[i] : NULL;
115
116 pipe_texture_reference(&softpipe->texture[i], tex);
117 sp_tile_cache_set_texture(softpipe->tex_cache[i], tex);
118 }
119
120 softpipe->num_textures = num;
121
122 softpipe->dirty |= SP_NEW_TEXTURE;
123 }
124
125
126
127 static struct sp_sampler_varient *
128 get_sampler_varient( struct sp_sampler *sampler,
129 struct pipe_texture *texture,
130 unsigned processor )
131 {
132 struct softpipe_texture *sp_texture = softpipe_texture(texture);
133 struct sp_sampler_varient *v = NULL;
134 union sp_sampler_key key;
135
136 key.bits.target = sp_texture->base.target;
137 key.bits.is_pot = sp_texture->pot;
138 key.bits.processor = processor;
139 key.bits.pad = 0;
140
141 if (sampler->current &&
142 key.value == sampler->current->key.value) {
143 v = sampler->current;
144 }
145
146 if (v == NULL) {
147 for (v = sampler->varients; v; v = v->next)
148 if (v->key.value == key.value)
149 break;
150
151 if (v == NULL) {
152 v = sp_create_sampler_varient( &sampler->base, key );
153 v->next = sampler->varients;
154 sampler->varients = v;
155 }
156 }
157
158 sampler->current = v;
159 return v;
160 }
161
162
163
164
165 void
166 softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
167 {
168 int i;
169
170 /* It's a bit hard to build these samplers ahead of time -- don't
171 * really know which samplers are going to be used for vertex and
172 * fragment programs.
173 */
174 for (i = 0; i <= softpipe->vs->max_sampler; i++) {
175 if (softpipe->sampler[i]) {
176 softpipe->tgsi.vert_samplers_list[i] =
177 get_sampler_varient( sp_sampler(softpipe->sampler[i]),
178 softpipe->texture[i],
179 TGSI_PROCESSOR_VERTEX );
180
181 sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
182 softpipe->tex_cache[i],
183 softpipe->texture[i] );
184 }
185 }
186
187 for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
188 if (softpipe->sampler[i]) {
189 softpipe->tgsi.frag_samplers_list[i] =
190 get_sampler_varient( sp_sampler(softpipe->sampler[i]),
191 softpipe->texture[i],
192 TGSI_PROCESSOR_FRAGMENT );
193
194 sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
195 softpipe->tex_cache[i],
196 softpipe->texture[i] );
197 }
198 }
199 }
200
201
202
203 void
204 softpipe_delete_sampler_state(struct pipe_context *pipe,
205 void *sampler)
206 {
207 struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
208 struct sp_sampler_varient *v, *tmp;
209
210 for (v = sp_sampler->varients; v; v = tmp) {
211 tmp = v->next;
212 sp_sampler_varient_destroy(v);
213 }
214
215 FREE( sampler );
216 }
217
218
219