Tizen 2.0 Release
[profile/ivi/osmesa.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
148 svga_bind_fragment_sampler_states(struct pipe_context *pipe,
149                                   unsigned num, void **sampler)
150 {
151    struct svga_context *svga = svga_context(pipe);
152    unsigned i;
153
154    assert(num <= PIPE_MAX_SAMPLERS);
155
156    /* Check for no-op */
157    if (num == svga->curr.num_samplers &&
158        !memcmp(svga->curr.sampler, sampler, num * sizeof(void *))) {
159       if (0) debug_printf("sampler noop\n");
160       return;
161    }
162
163    for (i = 0; i < num; i++)
164       svga->curr.sampler[i] = sampler[i];
165
166    for (i = num; i < svga->curr.num_samplers; i++)
167       svga->curr.sampler[i] = NULL;
168
169    svga->curr.num_samplers = num;
170    svga->dirty |= SVGA_NEW_SAMPLER;
171 }
172
173 static void svga_delete_sampler_state(struct pipe_context *pipe,
174                                       void *sampler)
175 {
176    FREE(sampler);
177 }
178
179
180 static struct pipe_sampler_view *
181 svga_create_sampler_view(struct pipe_context *pipe,
182                          struct pipe_resource *texture,
183                          const struct pipe_sampler_view *templ)
184 {
185    struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
186
187    if (view) {
188       *view = *templ;
189       view->reference.count = 1;
190       view->texture = NULL;
191       pipe_resource_reference(&view->texture, texture);
192       view->context = pipe;
193    }
194
195    return view;
196 }
197
198
199 static void
200 svga_sampler_view_destroy(struct pipe_context *pipe,
201                           struct pipe_sampler_view *view)
202 {
203    pipe_resource_reference(&view->texture, NULL);
204    FREE(view);
205 }
206
207 static void
208 svga_set_fragment_sampler_views(struct pipe_context *pipe,
209                                 unsigned num,
210                                 struct pipe_sampler_view **views)
211 {
212    struct svga_context *svga = svga_context(pipe);
213    unsigned flag_1d = 0;
214    unsigned flag_srgb = 0;
215    uint i;
216
217    assert(num <= PIPE_MAX_SAMPLERS);
218
219    /* Check for no-op */
220    if (num == svga->curr.num_sampler_views &&
221        !memcmp(svga->curr.sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
222       if (0) debug_printf("texture noop\n");
223       return;
224    }
225
226    for (i = 0; i < num; i++) {
227       pipe_sampler_view_reference(&svga->curr.sampler_views[i],
228                                   views[i]);
229
230       if (!views[i])
231          continue;
232
233       if (views[i]->texture->format == PIPE_FORMAT_B8G8R8A8_SRGB)
234          flag_srgb |= 1 << i;
235
236       if (views[i]->texture->target == PIPE_TEXTURE_1D)
237          flag_1d |= 1 << i;
238    }
239
240    for (i = num; i < svga->curr.num_sampler_views; i++)
241       pipe_sampler_view_reference(&svga->curr.sampler_views[i],
242                                   NULL);
243
244    svga->curr.num_sampler_views = num;
245    svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
246
247    if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
248        flag_1d != svga->curr.tex_flags.flag_1d) 
249    {
250       svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
251       svga->curr.tex_flags.flag_1d = flag_1d;
252       svga->curr.tex_flags.flag_srgb = flag_srgb;
253    }  
254 }
255
256
257
258 void svga_init_sampler_functions( struct svga_context *svga )
259 {
260    svga->pipe.create_sampler_state = svga_create_sampler_state;
261    svga->pipe.bind_fragment_sampler_states = svga_bind_fragment_sampler_states;
262    svga->pipe.delete_sampler_state = svga_delete_sampler_state;
263    svga->pipe.set_fragment_sampler_views = svga_set_fragment_sampler_views;
264    svga->pipe.create_sampler_view = svga_create_sampler_view;
265    svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
266 }
267
268
269