i965: Rename BRW_VARYING_SLOT_MAX -> BRW_VARYING_SLOT_COUNT.
[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 #include "main/samplerobj.h"
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 uint32_t
48 translate_wrap_mode(GLenum wrap, bool using_nearest)
49 {
50 switch( wrap ) {
51 case GL_REPEAT:
52 return BRW_TEXCOORDMODE_WRAP;
53 case GL_CLAMP:
54 /* GL_CLAMP is the weird mode where coordinates are clamped to
55 * [0.0, 1.0], so linear filtering of coordinates outside of
56 * [0.0, 1.0] give you half edge texel value and half border
57 * color. The fragment shader will clamp the coordinates, and
58 * we set clamp_border here, which gets the result desired. We
59 * just use clamp(_to_edge) for nearest, because for nearest
60 * clamping to 1.0 gives border color instead of the desired
61 * edge texels.
62 */
63 if (using_nearest)
64 return BRW_TEXCOORDMODE_CLAMP;
65 else
66 return BRW_TEXCOORDMODE_CLAMP_BORDER;
67 case GL_CLAMP_TO_EDGE:
68 return BRW_TEXCOORDMODE_CLAMP;
69 case GL_CLAMP_TO_BORDER:
70 return BRW_TEXCOORDMODE_CLAMP_BORDER;
71 case GL_MIRRORED_REPEAT:
72 return BRW_TEXCOORDMODE_MIRROR;
73 default:
74 return BRW_TEXCOORDMODE_WRAP;
75 }
76 }
77
78 /**
79 * Upload SAMPLER_BORDER_COLOR_STATE.
80 */
81 void
82 upload_default_color(struct brw_context *brw, struct gl_sampler_object *sampler,
83 int unit, int ss_index)
84 {
85 struct intel_context *intel = &brw->intel;
86 struct gl_context *ctx = &intel->ctx;
87 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
88 struct gl_texture_object *texObj = texUnit->_Current;
89 struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
90 float color[4];
91
92 switch (firstImage->_BaseFormat) {
93 case GL_DEPTH_COMPONENT:
94 /* GL specs that border color for depth textures is taken from the
95 * R channel, while the hardware uses A. Spam R into all the
96 * channels for safety.
97 */
98 color[0] = sampler->BorderColor.f[0];
99 color[1] = sampler->BorderColor.f[0];
100 color[2] = sampler->BorderColor.f[0];
101 color[3] = sampler->BorderColor.f[0];
102 break;
103 case GL_ALPHA:
104 color[0] = 0.0;
105 color[1] = 0.0;
106 color[2] = 0.0;
107 color[3] = sampler->BorderColor.f[3];
108 break;
109 case GL_INTENSITY:
110 color[0] = sampler->BorderColor.f[0];
111 color[1] = sampler->BorderColor.f[0];
112 color[2] = sampler->BorderColor.f[0];
113 color[3] = sampler->BorderColor.f[0];
114 break;
115 case GL_LUMINANCE:
116 color[0] = sampler->BorderColor.f[0];
117 color[1] = sampler->BorderColor.f[0];
118 color[2] = sampler->BorderColor.f[0];
119 color[3] = 1.0;
120 break;
121 case GL_LUMINANCE_ALPHA:
122 color[0] = sampler->BorderColor.f[0];
123 color[1] = sampler->BorderColor.f[0];
124 color[2] = sampler->BorderColor.f[0];
125 color[3] = sampler->BorderColor.f[3];
126 break;
127 default:
128 color[0] = sampler->BorderColor.f[0];
129 color[1] = sampler->BorderColor.f[1];
130 color[2] = sampler->BorderColor.f[2];
131 color[3] = sampler->BorderColor.f[3];
132 break;
133 }
134
135 /* In some cases we use an RGBA surface format for GL RGB textures,
136 * where we've initialized the A channel to 1.0. We also have to set
137 * the border color alpha to 1.0 in that case.
138 */
139 if (firstImage->_BaseFormat == GL_RGB)
140 color[3] = 1.0;
141
142 if (intel->gen == 5 || intel->gen == 6) {
143 struct gen5_sampler_default_color *sdc;
144
145 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
146 sizeof(*sdc), 32, &brw->wm.sdc_offset[ss_index]);
147
148 memset(sdc, 0, sizeof(*sdc));
149
150 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
151 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
152 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
153 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
154
155 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
156 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
157 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
158 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
159
160 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
161 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
162 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
163 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
164
165 sdc->hf[0] = _mesa_float_to_half(color[0]);
166 sdc->hf[1] = _mesa_float_to_half(color[1]);
167 sdc->hf[2] = _mesa_float_to_half(color[2]);
168 sdc->hf[3] = _mesa_float_to_half(color[3]);
169
170 sdc->b[0] = sdc->s[0] >> 8;
171 sdc->b[1] = sdc->s[1] >> 8;
172 sdc->b[2] = sdc->s[2] >> 8;
173 sdc->b[3] = sdc->s[3] >> 8;
174
175 sdc->f[0] = color[0];
176 sdc->f[1] = color[1];
177 sdc->f[2] = color[2];
178 sdc->f[3] = color[3];
179 } else {
180 struct brw_sampler_default_color *sdc;
181
182 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
183 sizeof(*sdc), 32, &brw->wm.sdc_offset[ss_index]);
184
185 COPY_4V(sdc->color, color);
186 }
187 }
188
189 /**
190 * Sets the sampler state for a single unit based off of the sampler key
191 * entry.
192 */
193 static void brw_update_sampler_state(struct brw_context *brw,
194 int unit,
195 int ss_index,
196 struct brw_sampler_state *sampler)
197 {
198 struct intel_context *intel = &brw->intel;
199 struct gl_context *ctx = &intel->ctx;
200 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
201 struct gl_texture_object *texObj = texUnit->_Current;
202 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
203 bool using_nearest = false;
204
205 /* These don't use samplers at all. */
206 if (texObj->Target == GL_TEXTURE_BUFFER)
207 return;
208
209 switch (gl_sampler->MinFilter) {
210 case GL_NEAREST:
211 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
212 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
213 using_nearest = true;
214 break;
215 case GL_LINEAR:
216 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
217 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
218 break;
219 case GL_NEAREST_MIPMAP_NEAREST:
220 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
221 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
222 break;
223 case GL_LINEAR_MIPMAP_NEAREST:
224 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
225 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
226 break;
227 case GL_NEAREST_MIPMAP_LINEAR:
228 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
229 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
230 break;
231 case GL_LINEAR_MIPMAP_LINEAR:
232 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
233 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
234 break;
235 default:
236 break;
237 }
238
239 /* Set Anisotropy:
240 */
241 if (gl_sampler->MaxAnisotropy > 1.0) {
242 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
243 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
244
245 if (gl_sampler->MaxAnisotropy > 2.0) {
246 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
247 BRW_ANISORATIO_16);
248 }
249 }
250 else {
251 switch (gl_sampler->MagFilter) {
252 case GL_NEAREST:
253 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
254 using_nearest = true;
255 break;
256 case GL_LINEAR:
257 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
258 break;
259 default:
260 break;
261 }
262 }
263
264 sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
265 using_nearest);
266 sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
267 using_nearest);
268 sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
269 using_nearest);
270
271 if (intel->gen >= 6 &&
272 sampler->ss0.min_filter != sampler->ss0.mag_filter)
273 sampler->ss0.min_mag_neq = 1;
274
275 /* Cube-maps on 965 and later must use the same wrap mode for all 3
276 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
277 */
278 if (texObj->Target == GL_TEXTURE_CUBE_MAP ||
279 texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
280 if (ctx->Texture.CubeMapSeamless &&
281 (gl_sampler->MinFilter != GL_NEAREST ||
282 gl_sampler->MagFilter != GL_NEAREST)) {
283 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
284 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
285 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
286 } else {
287 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
288 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
289 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
290 }
291 } else if (texObj->Target == GL_TEXTURE_1D) {
292 /* There's a bug in 1D texture sampling - it actually pays
293 * attention to the wrap_t value, though it should not.
294 * Override the wrap_t value here to GL_REPEAT to keep
295 * any nonexistent border pixels from floating in.
296 */
297 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
298 }
299
300
301 /* Set shadow function:
302 */
303 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
304 /* Shadowing is "enabled" by emitting a particular sampler
305 * message (sample_c). So need to recompile WM program when
306 * shadow comparison is enabled on each/any texture unit.
307 */
308 sampler->ss0.shadow_function =
309 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
310 }
311
312 /* Set LOD bias:
313 */
314 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
315 gl_sampler->LodBias, -16, 15), 6);
316
317 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
318 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
319
320 /* Set BaseMipLevel, MaxLOD, MinLOD:
321 *
322 * XXX: I don't think that using firstLevel, lastLevel works,
323 * because we always setup the surface state as if firstLevel ==
324 * level zero. Probably have to subtract firstLevel from each of
325 * these:
326 */
327 sampler->ss0.base_level = U_FIXED(0, 1);
328
329 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
330 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
331
332 /* On Gen6+, the sampler can handle non-normalized texture
333 * rectangle coordinates natively
334 */
335 if (intel->gen >= 6 && texObj->Target == GL_TEXTURE_RECTANGLE) {
336 sampler->ss3.non_normalized_coord = 1;
337 }
338
339 upload_default_color(brw, gl_sampler, unit, ss_index);
340
341 if (intel->gen >= 6) {
342 sampler->ss2.default_color_pointer = brw->wm.sdc_offset[ss_index] >> 5;
343 } else {
344 /* reloc */
345 sampler->ss2.default_color_pointer = (intel->batch.bo->offset +
346 brw->wm.sdc_offset[ss_index]) >> 5;
347
348 drm_intel_bo_emit_reloc(intel->batch.bo,
349 brw->sampler.offset +
350 ss_index * sizeof(struct brw_sampler_state) +
351 offsetof(struct brw_sampler_state, ss2),
352 intel->batch.bo, brw->wm.sdc_offset[ss_index],
353 I915_GEM_DOMAIN_SAMPLER, 0);
354 }
355
356 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
357 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
358 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
359 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
360 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
361 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
362 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
363 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
364 }
365
366
367 static void
368 brw_upload_samplers(struct brw_context *brw)
369 {
370 struct gl_context *ctx = &brw->intel.ctx;
371 struct brw_sampler_state *samplers;
372
373 /* BRW_NEW_VERTEX_PROGRAM and BRW_NEW_FRAGMENT_PROGRAM */
374 struct gl_program *vs = (struct gl_program *) brw->vertex_program;
375 struct gl_program *fs = (struct gl_program *) brw->fragment_program;
376
377 GLbitfield SamplersUsed = vs->SamplersUsed | fs->SamplersUsed;
378
379 /* ARB programs use the texture unit number as the sampler index, so we
380 * need to find the highest unit used. A bit-count will not work.
381 */
382 brw->sampler.count = _mesa_fls(SamplersUsed);
383
384 if (brw->sampler.count == 0)
385 return;
386
387 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
388 brw->sampler.count * sizeof(*samplers),
389 32, &brw->sampler.offset);
390 memset(samplers, 0, brw->sampler.count * sizeof(*samplers));
391
392 for (unsigned s = 0; s < brw->sampler.count; s++) {
393 if (SamplersUsed & (1 << s)) {
394 const unsigned unit = (fs->SamplersUsed & (1 << s)) ?
395 fs->SamplerUnits[s] : vs->SamplerUnits[s];
396 if (ctx->Texture.Unit[unit]._ReallyEnabled)
397 brw_update_sampler_state(brw, unit, s, &samplers[s]);
398 }
399 }
400
401 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
402 }
403
404 const struct brw_tracked_state brw_samplers = {
405 .dirty = {
406 .mesa = _NEW_TEXTURE,
407 .brw = BRW_NEW_BATCH |
408 BRW_NEW_VERTEX_PROGRAM |
409 BRW_NEW_FRAGMENT_PROGRAM,
410 .cache = 0
411 },
412 .emit = brw_upload_samplers,
413 };
414
415