Merge remote branch 'origin/7.8'
[mesa.git] / src / gallium / drivers / svga / svga_pipe_sampler.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "tgsi/tgsi_parse.h"
31
32 #include "svga_context.h"
33 #include "svga_resource_texture.h"
34
35 #include "svga_debug.h"
36
37 static INLINE unsigned
38 translate_wrap_mode(unsigned wrap)
39 {
40 switch (wrap) {
41 case PIPE_TEX_WRAP_REPEAT:
42 return SVGA3D_TEX_ADDRESS_WRAP;
43
44 case PIPE_TEX_WRAP_CLAMP:
45 return SVGA3D_TEX_ADDRESS_CLAMP;
46
47 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
48 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
49 * hardware.
50 */
51 return SVGA3D_TEX_ADDRESS_CLAMP;
52
53 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
54 return SVGA3D_TEX_ADDRESS_BORDER;
55
56 case PIPE_TEX_WRAP_MIRROR_REPEAT:
57 return SVGA3D_TEX_ADDRESS_MIRROR;
58
59 case PIPE_TEX_WRAP_MIRROR_CLAMP:
60 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
61 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
62 return SVGA3D_TEX_ADDRESS_MIRRORONCE;
63
64 default:
65 assert(0);
66 return SVGA3D_TEX_ADDRESS_WRAP;
67 }
68 }
69
70 static INLINE unsigned translate_img_filter( unsigned filter )
71 {
72 switch (filter) {
73 case PIPE_TEX_FILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
74 case PIPE_TEX_FILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
75 default:
76 assert(0);
77 return SVGA3D_TEX_FILTER_NEAREST;
78 }
79 }
80
81 static INLINE unsigned translate_mip_filter( unsigned filter )
82 {
83 switch (filter) {
84 case PIPE_TEX_MIPFILTER_NONE: return SVGA3D_TEX_FILTER_NONE;
85 case PIPE_TEX_MIPFILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
86 case PIPE_TEX_MIPFILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
87 default:
88 assert(0);
89 return SVGA3D_TEX_FILTER_NONE;
90 }
91 }
92
93 static void *
94 svga_create_sampler_state(struct pipe_context *pipe,
95 const struct pipe_sampler_state *sampler)
96 {
97 struct svga_context *svga = svga_context(pipe);
98 struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
99
100 cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
101 cso->magfilter = translate_img_filter( sampler->mag_img_filter );
102 cso->minfilter = translate_img_filter( sampler->min_img_filter );
103 cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );
104 if(sampler->max_anisotropy)
105 cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
106 cso->lod_bias = sampler->lod_bias;
107 cso->addressu = translate_wrap_mode(sampler->wrap_s);
108 cso->addressv = translate_wrap_mode(sampler->wrap_t);
109 cso->addressw = translate_wrap_mode(sampler->wrap_r);
110 cso->normalized_coords = sampler->normalized_coords;
111 cso->compare_mode = sampler->compare_mode;
112 cso->compare_func = sampler->compare_func;
113
114 {
115 uint32 r = float_to_ubyte(sampler->border_color[0]);
116 uint32 g = float_to_ubyte(sampler->border_color[1]);
117 uint32 b = float_to_ubyte(sampler->border_color[2]);
118 uint32 a = float_to_ubyte(sampler->border_color[3]);
119
120 cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;
121 }
122
123 /* No SVGA3D support for:
124 * - min/max LOD clamping
125 */
126 cso->min_lod = 0;
127 cso->view_min_lod = MAX2(sampler->min_lod, 0);
128 cso->view_max_lod = MAX2(sampler->max_lod, 0);
129
130 /* Use min_mipmap */
131 if (svga->debug.use_min_mipmap) {
132 if (cso->view_min_lod == cso->view_max_lod) {
133 cso->min_lod = cso->view_min_lod;
134 cso->view_min_lod = 0;
135 cso->view_max_lod = 1000; /* Just a high number */
136 cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
137 }
138 }
139
140 SVGA_DBG(DEBUG_VIEWS, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
141 cso->min_lod, cso->view_min_lod, cso->view_max_lod,
142 cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
143
144 return cso;
145 }
146
147 static void svga_bind_sampler_states(struct pipe_context *pipe,
148 unsigned num, void **sampler)
149 {
150 struct svga_context *svga = svga_context(pipe);
151 unsigned i;
152
153 assert(num <= PIPE_MAX_SAMPLERS);
154
155 /* Check for no-op */
156 if (num == svga->curr.num_samplers &&
157 !memcmp(svga->curr.sampler, sampler, num * sizeof(void *))) {
158 if (0) debug_printf("sampler noop\n");
159 return;
160 }
161
162 for (i = 0; i < num; i++)
163 svga->curr.sampler[i] = sampler[i];
164
165 for (i = num; i < svga->curr.num_samplers; i++)
166 svga->curr.sampler[i] = NULL;
167
168 svga->curr.num_samplers = num;
169 svga->dirty |= SVGA_NEW_SAMPLER;
170 }
171
172 static void svga_delete_sampler_state(struct pipe_context *pipe,
173 void *sampler)
174 {
175 FREE(sampler);
176 }
177
178
179 static struct pipe_sampler_view *
180 svga_create_sampler_view(struct pipe_context *pipe,
181 struct pipe_resource *texture,
182 const struct pipe_sampler_view *templ)
183 {
184 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
185
186 if (view) {
187 *view = *templ;
188 view->reference.count = 1;
189 view->texture = NULL;
190 pipe_resource_reference(&view->texture, texture);
191 view->context = pipe;
192 }
193
194 return view;
195 }
196
197
198 static void
199 svga_sampler_view_destroy(struct pipe_context *pipe,
200 struct pipe_sampler_view *view)
201 {
202 pipe_resource_reference(&view->texture, NULL);
203 FREE(view);
204 }
205
206 static void svga_set_sampler_views(struct pipe_context *pipe,
207 unsigned num,
208 struct pipe_sampler_view **views)
209 {
210 struct svga_context *svga = svga_context(pipe);
211 unsigned flag_1d = 0;
212 unsigned flag_srgb = 0;
213 uint i;
214
215 assert(num <= PIPE_MAX_SAMPLERS);
216
217 /* Check for no-op */
218 if (num == svga->curr.num_sampler_views &&
219 !memcmp(svga->curr.sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
220 if (0) debug_printf("texture noop\n");
221 return;
222 }
223
224 for (i = 0; i < num; i++) {
225 pipe_sampler_view_reference(&svga->curr.sampler_views[i],
226 views[i]);
227
228 if (!views[i])
229 continue;
230
231 if (views[i]->texture->format == PIPE_FORMAT_B8G8R8A8_SRGB)
232 flag_srgb |= 1 << i;
233
234 if (views[i]->texture->target == PIPE_TEXTURE_1D)
235 flag_1d |= 1 << i;
236 }
237
238 for (i = num; i < svga->curr.num_sampler_views; i++)
239 pipe_sampler_view_reference(&svga->curr.sampler_views[i],
240 NULL);
241
242 svga->curr.num_sampler_views = num;
243 svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
244
245 if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
246 flag_1d != svga->curr.tex_flags.flag_1d)
247 {
248 svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
249 svga->curr.tex_flags.flag_1d = flag_1d;
250 svga->curr.tex_flags.flag_srgb = flag_srgb;
251 }
252 }
253
254
255
256 void svga_init_sampler_functions( struct svga_context *svga )
257 {
258 svga->pipe.create_sampler_state = svga_create_sampler_state;
259 svga->pipe.bind_fragment_sampler_states = svga_bind_sampler_states;
260 svga->pipe.delete_sampler_state = svga_delete_sampler_state;
261 svga->pipe.set_fragment_sampler_views = svga_set_sampler_views;
262 svga->pipe.create_sampler_view = svga_create_sampler_view;
263 svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
264 }
265
266
267