Minor r200 vertex program cleanups. Remove disabled leftovers from r300 vertex progra...
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_sampler_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
36
37 #include "macros.h"
38
39
40
41 /* Samplers aren't strictly wm state from the hardware's perspective,
42 * but that is the only situation in which we use them in this driver.
43 */
44
45
46
47 /* The brw (and related graphics cores) do not support GL_CLAMP. The
48 * Intel drivers for "other operating systems" implement GL_CLAMP as
49 * GL_CLAMP_TO_EDGE, so the same is done here.
50 */
51 static GLuint translate_wrap_mode( GLenum wrap )
52 {
53 switch( wrap ) {
54 case GL_REPEAT:
55 return BRW_TEXCOORDMODE_WRAP;
56 case GL_CLAMP:
57 return BRW_TEXCOORDMODE_CLAMP_BORDER; /* conform likes it this way */
58 case GL_CLAMP_TO_EDGE:
59 return BRW_TEXCOORDMODE_CLAMP; /* conform likes it this way */
60 case GL_CLAMP_TO_BORDER:
61 return BRW_TEXCOORDMODE_CLAMP_BORDER;
62 case GL_MIRRORED_REPEAT:
63 return BRW_TEXCOORDMODE_MIRROR;
64 default:
65 return BRW_TEXCOORDMODE_WRAP;
66 }
67 }
68
69
70 static GLuint U_FIXED(GLfloat value, GLuint frac_bits)
71 {
72 value *= (1<<frac_bits);
73 return value < 0 ? 0 : value;
74 }
75
76 static GLint S_FIXED(GLfloat value, GLuint frac_bits)
77 {
78 return value * (1<<frac_bits);
79 }
80
81
82 static GLuint upload_default_color( struct brw_context *brw,
83 const GLfloat *color )
84 {
85 struct brw_sampler_default_color sdc;
86
87 COPY_4V(sdc.color, color);
88
89 return brw_cache_data( &brw->cache[BRW_SAMPLER_DEFAULT_COLOR], &sdc );
90 }
91
92
93 /*
94 */
95 static void brw_update_sampler_state( struct gl_texture_unit *texUnit,
96 struct gl_texture_object *texObj,
97 GLuint sdc_gs_offset,
98 struct brw_sampler_state *sampler)
99 {
100 _mesa_memset(sampler, 0, sizeof(*sampler));
101
102 switch (texObj->MinFilter) {
103 case GL_NEAREST:
104 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
105 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
106 break;
107 case GL_LINEAR:
108 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
109 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
110 break;
111 case GL_NEAREST_MIPMAP_NEAREST:
112 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
113 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
114 break;
115 case GL_LINEAR_MIPMAP_NEAREST:
116 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
117 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
118 break;
119 case GL_NEAREST_MIPMAP_LINEAR:
120 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
121 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
122 break;
123 case GL_LINEAR_MIPMAP_LINEAR:
124 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
125 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
126 break;
127 default:
128 break;
129 }
130
131 /* Set Anisotropy:
132 */
133 if ( texObj->MaxAnisotropy > 1.0 ) {
134 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
135 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
136
137 if (texObj->MaxAnisotropy > 2.0) {
138 sampler->ss3.max_aniso = MAX2((texObj->MaxAnisotropy - 2) / 2,
139 BRW_ANISORATIO_16);
140 }
141 }
142 else {
143 switch (texObj->MagFilter) {
144 case GL_NEAREST:
145 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
146 break;
147 case GL_LINEAR:
148 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
149 break;
150 default:
151 break;
152 }
153 }
154
155 sampler->ss1.r_wrap_mode = translate_wrap_mode(texObj->WrapR);
156 sampler->ss1.s_wrap_mode = translate_wrap_mode(texObj->WrapS);
157 sampler->ss1.t_wrap_mode = translate_wrap_mode(texObj->WrapT);
158
159 /* Fulsim complains if I don't do this. Hardware doesn't mind:
160 */
161 #if 0
162 if (texObj->Target == GL_TEXTURE_CUBE_MAP_ARB) {
163 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
164 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
165 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
166 }
167 #endif
168
169 /* Set shadow function:
170 */
171 if (texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
172 /* Shadowing is "enabled" by emitting a particular sampler
173 * message (sample_c). So need to recompile WM program when
174 * shadow comparison is enabled on each/any texture unit.
175 */
176 sampler->ss0.shadow_function = intel_translate_compare_func(texObj->CompareFunc);
177 }
178
179 /* Set LOD bias:
180 */
181 sampler->ss0.lod_bias = S_FIXED(texUnit->LodBias + texObj->LodBias, 6);
182
183 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
184 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
185
186 /* Set BaseMipLevel, MaxLOD, MinLOD:
187 *
188 * XXX: I don't think that using firstLevel, lastLevel works,
189 * because we always setup the surface state as if firstLevel ==
190 * level zero. Probably have to subtract firstLevel from each of
191 * these:
192 */
193 sampler->ss0.base_level = U_FIXED(0, 1);
194
195 sampler->ss1.max_lod = U_FIXED(MAX2(texObj->MaxLod, 0), 6);
196 sampler->ss1.min_lod = U_FIXED(MAX2(texObj->MinLod, 0), 6);
197
198 sampler->ss2.default_color_pointer = sdc_gs_offset >> 5;
199 }
200
201
202
203 /* All samplers must be uploaded in a single contiguous array, which
204 * complicates various things. However, this is still too confusing -
205 * FIXME: simplify all the different new texture state flags.
206 */
207 static void upload_wm_samplers( struct brw_context *brw )
208 {
209 GLuint unit;
210 GLuint sampler_count = 0;
211
212 /* _NEW_TEXTURE */
213 for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
214 if (brw->attribs.Texture->Unit[unit]._ReallyEnabled) {
215 struct gl_texture_unit *texUnit = &brw->attribs.Texture->Unit[unit];
216 struct gl_texture_object *texObj = texUnit->_Current;
217
218 GLuint sdc_gs_offset = upload_default_color(brw, texObj->BorderColor);
219
220 brw_update_sampler_state(texUnit,
221 texObj,
222 sdc_gs_offset,
223 &brw->wm.sampler[unit]);
224
225 sampler_count = unit + 1;
226 }
227 }
228
229 if (brw->wm.sampler_count != sampler_count) {
230 brw->wm.sampler_count = sampler_count;
231 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
232 }
233
234 brw->wm.sampler_gs_offset = 0;
235
236 if (brw->wm.sampler_count)
237 brw->wm.sampler_gs_offset =
238 brw_cache_data_sz(&brw->cache[BRW_SAMPLER],
239 brw->wm.sampler,
240 sizeof(struct brw_sampler_state) * brw->wm.sampler_count);
241 }
242
243
244 const struct brw_tracked_state brw_wm_samplers = {
245 .dirty = {
246 .mesa = _NEW_TEXTURE,
247 .brw = 0,
248 .cache = 0
249 },
250 .update = upload_wm_samplers
251 };
252
253