Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_sampler.c
1
2 #include "util/u_memory.h"
3 #include "util/u_math.h"
4
5 #include "pipe/p_context.h"
6 #include "pipe/p_state.h"
7
8 #include "brw_context.h"
9 #include "brw_defines.h"
10 #include "brw_debug.h"
11
12
13
14 /* The brw (and related graphics cores) do not support GL_CLAMP. The
15 * Intel drivers for "other operating systems" implement GL_CLAMP as
16 * GL_CLAMP_TO_EDGE, so the same is done here.
17 */
18 static GLuint translate_wrap_mode( unsigned wrap )
19 {
20 switch( wrap ) {
21 case PIPE_TEX_WRAP_REPEAT:
22 return BRW_TEXCOORDMODE_WRAP;
23
24 case PIPE_TEX_WRAP_CLAMP:
25 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
26 return BRW_TEXCOORDMODE_CLAMP;
27
28 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
29 return BRW_TEXCOORDMODE_CLAMP_BORDER;
30
31 case PIPE_TEX_WRAP_MIRROR_REPEAT:
32 return BRW_TEXCOORDMODE_MIRROR;
33
34 case PIPE_TEX_WRAP_MIRROR_CLAMP:
35 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
36 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
37 return BRW_TEXCOORDMODE_MIRROR_ONCE;
38
39 default:
40 return BRW_TEXCOORDMODE_WRAP;
41 }
42 }
43
44 static GLuint translate_img_filter( unsigned filter )
45 {
46 switch (filter) {
47 case PIPE_TEX_FILTER_NEAREST:
48 return BRW_MAPFILTER_NEAREST;
49 case PIPE_TEX_FILTER_LINEAR:
50 return BRW_MAPFILTER_LINEAR;
51 default:
52 assert(0);
53 return BRW_MAPFILTER_NEAREST;
54 }
55 }
56
57 static GLuint translate_mip_filter( unsigned filter )
58 {
59 switch (filter) {
60 case PIPE_TEX_MIPFILTER_NONE:
61 return BRW_MIPFILTER_NONE;
62 case PIPE_TEX_MIPFILTER_NEAREST:
63 return BRW_MIPFILTER_NEAREST;
64 case PIPE_TEX_MIPFILTER_LINEAR:
65 return BRW_MIPFILTER_LINEAR;
66 default:
67 assert(0);
68 return BRW_MIPFILTER_NONE;
69 }
70 }
71
72 /* XXX: not sure why there are special translations for the shadow tex
73 * compare functions. In particular ALWAYS is translated to NEVER.
74 * Is this a hardware issue? Does i965 really suffer from this?
75 */
76 static GLuint translate_shadow_compare_func( unsigned func )
77 {
78 switch (func) {
79 case PIPE_FUNC_NEVER:
80 return BRW_COMPAREFUNCTION_ALWAYS;
81 case PIPE_FUNC_LESS:
82 return BRW_COMPAREFUNCTION_LEQUAL;
83 case PIPE_FUNC_LEQUAL:
84 return BRW_COMPAREFUNCTION_LESS;
85 case PIPE_FUNC_GREATER:
86 return BRW_COMPAREFUNCTION_GEQUAL;
87 case PIPE_FUNC_GEQUAL:
88 return BRW_COMPAREFUNCTION_GREATER;
89 case PIPE_FUNC_NOTEQUAL:
90 return BRW_COMPAREFUNCTION_EQUAL;
91 case PIPE_FUNC_EQUAL:
92 return BRW_COMPAREFUNCTION_NOTEQUAL;
93 case PIPE_FUNC_ALWAYS:
94 return BRW_COMPAREFUNCTION_NEVER;
95 default:
96 assert(0);
97 return BRW_COMPAREFUNCTION_NEVER;
98 }
99 }
100
101
102
103
104 static void *
105 brw_create_sampler_state( struct pipe_context *pipe,
106 const struct pipe_sampler_state *template )
107 {
108 struct brw_sampler *sampler = CALLOC_STRUCT(brw_sampler);
109
110 sampler->ss0.min_filter = translate_img_filter( template->min_img_filter );
111 sampler->ss0.mag_filter = translate_img_filter( template->mag_img_filter );
112 sampler->ss0.mip_filter = translate_mip_filter( template->min_mip_filter );
113
114
115 /* XXX: anisotropy logic slightly changed:
116 */
117 if (template->max_anisotropy > 1.0) {
118 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
119 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
120
121 if (template->max_anisotropy > 2.0) {
122 sampler->ss3.max_aniso = MIN2((template->max_anisotropy - 2) / 2,
123 BRW_ANISORATIO_16);
124 }
125 }
126
127 sampler->ss1.r_wrap_mode = translate_wrap_mode(template->wrap_r);
128 sampler->ss1.s_wrap_mode = translate_wrap_mode(template->wrap_s);
129 sampler->ss1.t_wrap_mode = translate_wrap_mode(template->wrap_t);
130
131 /* Set LOD bias:
132 */
133 sampler->ss0.lod_bias =
134 util_signed_fixed(CLAMP(template->lod_bias, -16, 15), 6);
135
136
137 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
138 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
139
140 /* Set shadow function:
141 */
142 if (template->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
143
144 /* Shadowing is "enabled" by emitting a particular sampler
145 * message (sample_c). So need to recompile WM program when
146 * shadow comparison is enabled on each/any texture unit.
147 */
148 sampler->ss0.shadow_function =
149 translate_shadow_compare_func(template->compare_func);
150 }
151
152 /* Set BaseMipLevel, MaxLOD, MinLOD:
153 */
154 sampler->ss0.base_level =
155 util_unsigned_fixed(0, 1);
156
157 sampler->ss1.max_lod =
158 util_unsigned_fixed(CLAMP(template->max_lod, 0, 13), 6);
159
160 sampler->ss1.min_lod =
161 util_unsigned_fixed(CLAMP(template->min_lod, 0, 13), 6);
162
163 return (void *)sampler;
164 }
165
166 static void brw_bind_sampler_state(struct pipe_context *pipe,
167 unsigned num, void **sampler)
168 {
169 struct brw_context *brw = brw_context(pipe);
170 int i;
171
172 for (i = 0; i < num; i++)
173 brw->curr.sampler[i] = sampler[i];
174
175 for (i = num; i < brw->curr.num_samplers; i++)
176 brw->curr.sampler[i] = NULL;
177
178 brw->curr.num_samplers = num;
179 brw->state.dirty.mesa |= PIPE_NEW_SAMPLERS;
180 }
181
182 static void brw_delete_sampler_state(struct pipe_context *pipe,
183 void *cso)
184 {
185 FREE(cso);
186 }
187
188 static void brw_set_sampler_textures(struct pipe_context *pipe,
189 unsigned num,
190 struct pipe_texture **texture)
191 {
192 struct brw_context *brw = brw_context(pipe);
193 int i;
194
195 for (i = 0; i < num; i++)
196 pipe_texture_reference(&brw->curr.texture[i], texture[i]);
197
198 for (i = num; i < brw->curr.num_textures; i++)
199 pipe_texture_reference(&brw->curr.texture[i], NULL);
200
201 brw->curr.num_textures = num;
202 brw->state.dirty.mesa |= PIPE_NEW_BOUND_TEXTURES;
203 }
204
205 static void brw_set_vertex_sampler_textures(struct pipe_context *pipe,
206 unsigned num,
207 struct pipe_texture **texture)
208 {
209 }
210
211 static void brw_bind_vertex_sampler_state(struct pipe_context *pipe,
212 unsigned num, void **sampler)
213 {
214 }
215
216
217 void brw_pipe_sampler_init( struct brw_context *brw )
218 {
219 brw->base.create_sampler_state = brw_create_sampler_state;
220 brw->base.delete_sampler_state = brw_delete_sampler_state;
221
222 brw->base.set_fragment_sampler_textures = brw_set_sampler_textures;
223 brw->base.bind_fragment_sampler_states = brw_bind_sampler_state;
224
225 brw->base.set_vertex_sampler_textures = brw_set_vertex_sampler_textures;
226 brw->base.bind_vertex_sampler_states = brw_bind_vertex_sampler_state;
227
228 }
229 void brw_pipe_sampler_cleanup( struct brw_context *brw )
230 {
231 }