Merge remote branch 'origin/master' into lp-binning
[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 "pipe/p_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_pack_color.h"
31 #include "tgsi/tgsi_parse.h"
32
33 #include "svga_context.h"
34 #include "svga_screen_texture.h"
35 #include "svga_state.h"
36
37 #include "svga_hw_reg.h"
38
39 #include "svga_debug.h"
40
41 static INLINE unsigned
42 translate_wrap_mode(unsigned wrap)
43 {
44 switch (wrap) {
45 case PIPE_TEX_WRAP_REPEAT:
46 return SVGA3D_TEX_ADDRESS_WRAP;
47
48 case PIPE_TEX_WRAP_CLAMP:
49 return SVGA3D_TEX_ADDRESS_CLAMP;
50
51 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
52 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
53 * hardware.
54 */
55 return SVGA3D_TEX_ADDRESS_CLAMP;
56
57 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
58 return SVGA3D_TEX_ADDRESS_BORDER;
59
60 case PIPE_TEX_WRAP_MIRROR_REPEAT:
61 return SVGA3D_TEX_ADDRESS_MIRROR;
62
63 case PIPE_TEX_WRAP_MIRROR_CLAMP:
64 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
65 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
66 return SVGA3D_TEX_ADDRESS_MIRRORONCE;
67
68 default:
69 assert(0);
70 return SVGA3D_TEX_ADDRESS_WRAP;
71 }
72 }
73
74 static INLINE unsigned translate_img_filter( unsigned filter )
75 {
76 switch (filter) {
77 case PIPE_TEX_FILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
78 case PIPE_TEX_FILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
79 default:
80 assert(0);
81 return SVGA3D_TEX_FILTER_NEAREST;
82 }
83 }
84
85 static INLINE unsigned translate_mip_filter( unsigned filter )
86 {
87 switch (filter) {
88 case PIPE_TEX_MIPFILTER_NONE: return SVGA3D_TEX_FILTER_NONE;
89 case PIPE_TEX_MIPFILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
90 case PIPE_TEX_MIPFILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
91 default:
92 assert(0);
93 return SVGA3D_TEX_FILTER_NONE;
94 }
95 }
96
97 static void *
98 svga_create_sampler_state(struct pipe_context *pipe,
99 const struct pipe_sampler_state *sampler)
100 {
101 struct svga_context *svga = svga_context(pipe);
102 struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
103 union util_color uc;
104
105 cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
106 cso->magfilter = translate_img_filter( sampler->mag_img_filter );
107 cso->minfilter = translate_img_filter( sampler->min_img_filter );
108 cso->aniso_level = MAX2( (unsigned) sampler->max_anisotropy, 1 );
109 if(cso->aniso_level != 1)
110 cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
111 cso->lod_bias = sampler->lod_bias;
112 cso->addressu = translate_wrap_mode(sampler->wrap_s);
113 cso->addressv = translate_wrap_mode(sampler->wrap_t);
114 cso->addressw = translate_wrap_mode(sampler->wrap_r);
115 cso->normalized_coords = sampler->normalized_coords;
116 cso->compare_mode = sampler->compare_mode;
117 cso->compare_func = sampler->compare_func;
118
119 {
120 ubyte r = float_to_ubyte(sampler->border_color[0]);
121 ubyte g = float_to_ubyte(sampler->border_color[1]);
122 ubyte b = float_to_ubyte(sampler->border_color[2]);
123 ubyte a = float_to_ubyte(sampler->border_color[3]);
124
125 util_pack_color_ub( r, g, b, a,
126 PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
127 cso->bordercolor = uc.ui;
128 }
129
130 /* No SVGA3D support for:
131 * - min/max LOD clamping
132 */
133 cso->min_lod = 0;
134 cso->view_min_lod = MAX2(sampler->min_lod, 0);
135 cso->view_max_lod = MAX2(sampler->max_lod, 0);
136
137 /* Use min_mipmap */
138 if (svga->debug.use_min_mipmap) {
139 if (cso->view_min_lod == cso->view_max_lod) {
140 cso->min_lod = cso->view_min_lod;
141 cso->view_min_lod = 0;
142 cso->view_max_lod = 1000; /* Just a high number */
143 cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
144 }
145 }
146
147 SVGA_DBG(DEBUG_VIEWS, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
148 cso->min_lod, cso->view_min_lod, cso->view_max_lod,
149 cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
150
151 return cso;
152 }
153
154 static void svga_bind_sampler_states(struct pipe_context *pipe,
155 unsigned num, void **sampler)
156 {
157 struct svga_context *svga = svga_context(pipe);
158 unsigned i;
159
160 assert(num <= PIPE_MAX_SAMPLERS);
161
162 /* Check for no-op */
163 if (num == svga->curr.num_samplers &&
164 !memcmp(svga->curr.sampler, sampler, num * sizeof(void *))) {
165 debug_printf("sampler noop\n");
166 return;
167 }
168
169 for (i = 0; i < num; i++)
170 svga->curr.sampler[i] = sampler[i];
171
172 for (i = num; i < svga->curr.num_samplers; i++)
173 svga->curr.sampler[i] = NULL;
174
175 svga->curr.num_samplers = num;
176 svga->dirty |= SVGA_NEW_SAMPLER;
177 }
178
179 static void svga_delete_sampler_state(struct pipe_context *pipe,
180 void *sampler)
181 {
182 FREE(sampler);
183 }
184
185
186 static void svga_set_sampler_textures(struct pipe_context *pipe,
187 unsigned num,
188 struct pipe_texture **texture)
189 {
190 struct svga_context *svga = svga_context(pipe);
191 unsigned flag_1d = 0;
192 unsigned flag_srgb = 0;
193 uint i;
194
195 assert(num <= PIPE_MAX_SAMPLERS);
196
197 /* Check for no-op */
198 if (num == svga->curr.num_textures &&
199 !memcmp(svga->curr.texture, texture, num * sizeof(struct pipe_texture *))) {
200 if (0) debug_printf("texture noop\n");
201 return;
202 }
203
204 for (i = 0; i < num; i++) {
205 pipe_texture_reference(&svga->curr.texture[i],
206 texture[i]);
207
208 if (!texture[i])
209 continue;
210
211 if (texture[i]->format == PIPE_FORMAT_A8R8G8B8_SRGB)
212 flag_srgb |= 1 << i;
213
214 if (texture[i]->target == PIPE_TEXTURE_1D)
215 flag_1d |= 1 << i;
216 }
217
218 for (i = num; i < svga->curr.num_textures; i++)
219 pipe_texture_reference(&svga->curr.texture[i],
220 NULL);
221
222 svga->curr.num_textures = num;
223 svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
224
225 if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
226 flag_1d != svga->curr.tex_flags.flag_1d)
227 {
228 svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
229 svga->curr.tex_flags.flag_1d = flag_1d;
230 svga->curr.tex_flags.flag_srgb = flag_srgb;
231 }
232 }
233
234
235
236 void svga_init_sampler_functions( struct svga_context *svga )
237 {
238 svga->pipe.create_sampler_state = svga_create_sampler_state;
239 svga->pipe.bind_fragment_sampler_states = svga_bind_sampler_states;
240 svga->pipe.delete_sampler_state = svga_delete_sampler_state;
241 svga->pipe.set_fragment_sampler_textures = svga_set_sampler_textures;
242 }
243
244
245