Merge branch 'mesa_7_7_branch'
[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 "main/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;
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 static dri_bo *upload_default_color( struct brw_context *brw,
70 const GLfloat *color )
71 {
72 struct brw_sampler_default_color sdc;
73
74 COPY_4V(sdc.color, color);
75
76 return brw_cache_data(&brw->cache, BRW_SAMPLER_DEFAULT_COLOR,
77 &sdc, sizeof(sdc), NULL, 0);
78 }
79
80
81 struct wm_sampler_key {
82 int sampler_count;
83
84 struct wm_sampler_entry {
85 GLenum tex_target;
86 GLenum wrap_r, wrap_s, wrap_t;
87 float maxlod, minlod;
88 float lod_bias;
89 float max_aniso;
90 GLenum minfilter, magfilter;
91 GLenum comparemode, comparefunc;
92 dri_bo *sdc_bo;
93
94 /** If target is cubemap, take context setting.
95 */
96 GLboolean seamless_cube_map;
97 } sampler[BRW_MAX_TEX_UNIT];
98 };
99
100 /**
101 * Sets the sampler state for a single unit based off of the sampler key
102 * entry.
103 */
104 static void brw_update_sampler_state(struct wm_sampler_entry *key,
105 dri_bo *sdc_bo,
106 struct brw_sampler_state *sampler)
107 {
108 _mesa_memset(sampler, 0, sizeof(*sampler));
109
110 switch (key->minfilter) {
111 case GL_NEAREST:
112 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
113 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
114 break;
115 case GL_LINEAR:
116 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
117 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
118 break;
119 case GL_NEAREST_MIPMAP_NEAREST:
120 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
121 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
122 break;
123 case GL_LINEAR_MIPMAP_NEAREST:
124 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
125 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
126 break;
127 case GL_NEAREST_MIPMAP_LINEAR:
128 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
129 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
130 break;
131 case GL_LINEAR_MIPMAP_LINEAR:
132 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
133 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
134 break;
135 default:
136 break;
137 }
138
139 /* Set Anisotropy:
140 */
141 if (key->max_aniso > 1.0) {
142 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
143 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
144
145 if (key->max_aniso > 2.0) {
146 sampler->ss3.max_aniso = MIN2((key->max_aniso - 2) / 2,
147 BRW_ANISORATIO_16);
148 }
149 }
150 else {
151 switch (key->magfilter) {
152 case GL_NEAREST:
153 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
154 break;
155 case GL_LINEAR:
156 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
157 break;
158 default:
159 break;
160 }
161 }
162
163 sampler->ss1.r_wrap_mode = translate_wrap_mode(key->wrap_r);
164 sampler->ss1.s_wrap_mode = translate_wrap_mode(key->wrap_s);
165 sampler->ss1.t_wrap_mode = translate_wrap_mode(key->wrap_t);
166
167 /* Cube-maps on 965 and later must use the same wrap mode for all 3
168 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
169 */
170 if (key->tex_target == GL_TEXTURE_CUBE_MAP) {
171 if (key->seamless_cube_map &&
172 (key->minfilter != GL_NEAREST || key->magfilter != GL_NEAREST)) {
173 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
174 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
175 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
176 } else {
177 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
178 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
179 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
180 }
181 } else if (key->tex_target == GL_TEXTURE_1D) {
182 /* There's a bug in 1D texture sampling - it actually pays
183 * attention to the wrap_t value, though it should not.
184 * Override the wrap_t value here to GL_REPEAT to keep
185 * any nonexistent border pixels from floating in.
186 */
187 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
188 }
189
190
191 /* Set shadow function:
192 */
193 if (key->comparemode == GL_COMPARE_R_TO_TEXTURE_ARB) {
194 /* Shadowing is "enabled" by emitting a particular sampler
195 * message (sample_c). So need to recompile WM program when
196 * shadow comparison is enabled on each/any texture unit.
197 */
198 sampler->ss0.shadow_function =
199 intel_translate_shadow_compare_func(key->comparefunc);
200 }
201
202 /* Set LOD bias:
203 */
204 sampler->ss0.lod_bias = S_FIXED(CLAMP(key->lod_bias, -16, 15), 6);
205
206 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
207 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
208
209 /* Set BaseMipLevel, MaxLOD, MinLOD:
210 *
211 * XXX: I don't think that using firstLevel, lastLevel works,
212 * because we always setup the surface state as if firstLevel ==
213 * level zero. Probably have to subtract firstLevel from each of
214 * these:
215 */
216 sampler->ss0.base_level = U_FIXED(0, 1);
217
218 sampler->ss1.max_lod = U_FIXED(CLAMP(key->maxlod, 0, 13), 6);
219 sampler->ss1.min_lod = U_FIXED(CLAMP(key->minlod, 0, 13), 6);
220
221 sampler->ss2.default_color_pointer = sdc_bo->offset >> 5; /* reloc */
222 }
223
224
225 /** Sets up the cache key for sampler state for all texture units */
226 static void
227 brw_wm_sampler_populate_key(struct brw_context *brw,
228 struct wm_sampler_key *key)
229 {
230 GLcontext *ctx = &brw->intel.ctx;
231 int unit;
232
233 memset(key, 0, sizeof(*key));
234
235 for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
236 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
237 struct wm_sampler_entry *entry = &key->sampler[unit];
238 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
239 struct gl_texture_object *texObj = texUnit->_Current;
240 struct intel_texture_object *intelObj = intel_texture_object(texObj);
241 struct gl_texture_image *firstImage =
242 texObj->Image[0][intelObj->firstLevel];
243
244 entry->tex_target = texObj->Target;
245
246 entry->seamless_cube_map = (texObj->Target == GL_TEXTURE_CUBE_MAP)
247 ? ctx->Texture.CubeMapSeamless : GL_FALSE;
248
249 entry->wrap_r = texObj->WrapR;
250 entry->wrap_s = texObj->WrapS;
251 entry->wrap_t = texObj->WrapT;
252
253 entry->maxlod = texObj->MaxLod;
254 entry->minlod = texObj->MinLod;
255 entry->lod_bias = texUnit->LodBias + texObj->LodBias;
256 entry->max_aniso = texObj->MaxAnisotropy;
257 entry->minfilter = texObj->MinFilter;
258 entry->magfilter = texObj->MagFilter;
259 entry->comparemode = texObj->CompareMode;
260 entry->comparefunc = texObj->CompareFunc;
261
262 dri_bo_unreference(brw->wm.sdc_bo[unit]);
263 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
264 float bordercolor[4] = {
265 texObj->BorderColor.f[0],
266 texObj->BorderColor.f[0],
267 texObj->BorderColor.f[0],
268 texObj->BorderColor.f[0]
269 };
270 /* GL specs that border color for depth textures is taken from the
271 * R channel, while the hardware uses A. Spam R into all the
272 * channels for safety.
273 */
274 brw->wm.sdc_bo[unit] = upload_default_color(brw, bordercolor);
275 } else {
276 brw->wm.sdc_bo[unit] = upload_default_color(brw,
277 texObj->BorderColor.f);
278 }
279 key->sampler_count = unit + 1;
280 }
281 }
282 }
283
284 /* All samplers must be uploaded in a single contiguous array, which
285 * complicates various things. However, this is still too confusing -
286 * FIXME: simplify all the different new texture state flags.
287 */
288 static void upload_wm_samplers( struct brw_context *brw )
289 {
290 GLcontext *ctx = &brw->intel.ctx;
291 struct wm_sampler_key key;
292 int i;
293
294 brw_wm_sampler_populate_key(brw, &key);
295
296 if (brw->wm.sampler_count != key.sampler_count) {
297 brw->wm.sampler_count = key.sampler_count;
298 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
299 }
300
301 dri_bo_unreference(brw->wm.sampler_bo);
302 brw->wm.sampler_bo = NULL;
303 if (brw->wm.sampler_count == 0)
304 return;
305
306 brw->wm.sampler_bo = brw_search_cache(&brw->cache, BRW_SAMPLER,
307 &key, sizeof(key),
308 brw->wm.sdc_bo, key.sampler_count,
309 NULL);
310
311 /* If we didnt find it in the cache, compute the state and put it in the
312 * cache.
313 */
314 if (brw->wm.sampler_bo == NULL) {
315 struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT];
316
317 memset(sampler, 0, sizeof(sampler));
318 for (i = 0; i < key.sampler_count; i++) {
319 if (brw->wm.sdc_bo[i] == NULL)
320 continue;
321
322 brw_update_sampler_state(&key.sampler[i], brw->wm.sdc_bo[i],
323 &sampler[i]);
324 }
325
326 brw->wm.sampler_bo = brw_upload_cache(&brw->cache, BRW_SAMPLER,
327 &key, sizeof(key),
328 brw->wm.sdc_bo, key.sampler_count,
329 &sampler, sizeof(sampler));
330
331 /* Emit SDC relocations */
332 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
333 if (!ctx->Texture.Unit[i]._ReallyEnabled)
334 continue;
335
336 dri_bo_emit_reloc(brw->wm.sampler_bo,
337 I915_GEM_DOMAIN_SAMPLER, 0,
338 0,
339 i * sizeof(struct brw_sampler_state) +
340 offsetof(struct brw_sampler_state, ss2),
341 brw->wm.sdc_bo[i]);
342 }
343 }
344 }
345
346 const struct brw_tracked_state brw_wm_samplers = {
347 .dirty = {
348 .mesa = _NEW_TEXTURE,
349 .brw = 0,
350 .cache = 0
351 },
352 .prepare = upload_wm_samplers,
353 };
354
355