s/Tungsten Graphics/VMware/
[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 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 <keithw@vmware.com>
30 */
31
32
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
36 #include "intel_mipmap_tree.h"
37
38 #include "main/macros.h"
39 #include "main/samplerobj.h"
40
41
42 /* Samplers aren't strictly wm state from the hardware's perspective,
43 * but that is the only situation in which we use them in this driver.
44 */
45
46
47
48 uint32_t
49 translate_wrap_mode(GLenum wrap, bool using_nearest)
50 {
51 switch( wrap ) {
52 case GL_REPEAT:
53 return BRW_TEXCOORDMODE_WRAP;
54 case GL_CLAMP:
55 /* GL_CLAMP is the weird mode where coordinates are clamped to
56 * [0.0, 1.0], so linear filtering of coordinates outside of
57 * [0.0, 1.0] give you half edge texel value and half border
58 * color. The fragment shader will clamp the coordinates, and
59 * we set clamp_border here, which gets the result desired. We
60 * just use clamp(_to_edge) for nearest, because for nearest
61 * clamping to 1.0 gives border color instead of the desired
62 * edge texels.
63 */
64 if (using_nearest)
65 return BRW_TEXCOORDMODE_CLAMP;
66 else
67 return BRW_TEXCOORDMODE_CLAMP_BORDER;
68 case GL_CLAMP_TO_EDGE:
69 return BRW_TEXCOORDMODE_CLAMP;
70 case GL_CLAMP_TO_BORDER:
71 return BRW_TEXCOORDMODE_CLAMP_BORDER;
72 case GL_MIRRORED_REPEAT:
73 return BRW_TEXCOORDMODE_MIRROR;
74 case GL_MIRROR_CLAMP_TO_EDGE:
75 return BRW_TEXCOORDMODE_MIRROR_ONCE;
76 default:
77 return BRW_TEXCOORDMODE_WRAP;
78 }
79 }
80
81 /**
82 * Upload SAMPLER_BORDER_COLOR_STATE.
83 */
84 void
85 upload_default_color(struct brw_context *brw,
86 struct gl_sampler_object *sampler,
87 int unit,
88 uint32_t *sdc_offset)
89 {
90 struct gl_context *ctx = &brw->ctx;
91 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
92 struct gl_texture_object *texObj = texUnit->_Current;
93 struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
94 float color[4];
95
96 switch (firstImage->_BaseFormat) {
97 case GL_DEPTH_COMPONENT:
98 /* GL specs that border color for depth textures is taken from the
99 * R channel, while the hardware uses A. Spam R into all the
100 * channels for safety.
101 */
102 color[0] = sampler->BorderColor.f[0];
103 color[1] = sampler->BorderColor.f[0];
104 color[2] = sampler->BorderColor.f[0];
105 color[3] = sampler->BorderColor.f[0];
106 break;
107 case GL_ALPHA:
108 color[0] = 0.0;
109 color[1] = 0.0;
110 color[2] = 0.0;
111 color[3] = sampler->BorderColor.f[3];
112 break;
113 case GL_INTENSITY:
114 color[0] = sampler->BorderColor.f[0];
115 color[1] = sampler->BorderColor.f[0];
116 color[2] = sampler->BorderColor.f[0];
117 color[3] = sampler->BorderColor.f[0];
118 break;
119 case GL_LUMINANCE:
120 color[0] = sampler->BorderColor.f[0];
121 color[1] = sampler->BorderColor.f[0];
122 color[2] = sampler->BorderColor.f[0];
123 color[3] = 1.0;
124 break;
125 case GL_LUMINANCE_ALPHA:
126 color[0] = sampler->BorderColor.f[0];
127 color[1] = sampler->BorderColor.f[0];
128 color[2] = sampler->BorderColor.f[0];
129 color[3] = sampler->BorderColor.f[3];
130 break;
131 default:
132 color[0] = sampler->BorderColor.f[0];
133 color[1] = sampler->BorderColor.f[1];
134 color[2] = sampler->BorderColor.f[2];
135 color[3] = sampler->BorderColor.f[3];
136 break;
137 }
138
139 /* In some cases we use an RGBA surface format for GL RGB textures,
140 * where we've initialized the A channel to 1.0. We also have to set
141 * the border color alpha to 1.0 in that case.
142 */
143 if (firstImage->_BaseFormat == GL_RGB)
144 color[3] = 1.0;
145
146 if (brw->gen >= 8) {
147 /* On Broadwell, the border color is represented as four 32-bit floats,
148 * integers, or unsigned values, interpreted according to the surface
149 * format. This matches the sampler->BorderColor union exactly. Since
150 * we use floats both here and in the above reswizzling code, we preserve
151 * the original bit pattern. So we actually handle all three formats.
152 */
153 float *sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
154 4 * 4, 64, sdc_offset);
155 COPY_4FV(sdc, color);
156 } else if (brw->gen == 5 || brw->gen == 6) {
157 struct gen5_sampler_default_color *sdc;
158
159 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
160 sizeof(*sdc), 32, sdc_offset);
161
162 memset(sdc, 0, sizeof(*sdc));
163
164 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
165 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
166 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
167 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
168
169 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
170 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
171 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
172 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
173
174 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
175 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
176 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
177 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
178
179 sdc->hf[0] = _mesa_float_to_half(color[0]);
180 sdc->hf[1] = _mesa_float_to_half(color[1]);
181 sdc->hf[2] = _mesa_float_to_half(color[2]);
182 sdc->hf[3] = _mesa_float_to_half(color[3]);
183
184 sdc->b[0] = sdc->s[0] >> 8;
185 sdc->b[1] = sdc->s[1] >> 8;
186 sdc->b[2] = sdc->s[2] >> 8;
187 sdc->b[3] = sdc->s[3] >> 8;
188
189 sdc->f[0] = color[0];
190 sdc->f[1] = color[1];
191 sdc->f[2] = color[2];
192 sdc->f[3] = color[3];
193 } else {
194 struct brw_sampler_default_color *sdc;
195
196 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
197 sizeof(*sdc), 32, sdc_offset);
198
199 COPY_4V(sdc->color, color);
200 }
201 }
202
203 /**
204 * Sets the sampler state for a single unit based off of the sampler key
205 * entry.
206 */
207 static void brw_update_sampler_state(struct brw_context *brw,
208 int unit,
209 int ss_index,
210 struct brw_sampler_state *sampler,
211 uint32_t sampler_state_table_offset,
212 uint32_t *sdc_offset)
213 {
214 struct gl_context *ctx = &brw->ctx;
215 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
216 struct gl_texture_object *texObj = texUnit->_Current;
217 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
218 bool using_nearest = false;
219
220 /* These don't use samplers at all. */
221 if (texObj->Target == GL_TEXTURE_BUFFER)
222 return;
223
224 switch (gl_sampler->MinFilter) {
225 case GL_NEAREST:
226 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
227 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
228 using_nearest = true;
229 break;
230 case GL_LINEAR:
231 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
232 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
233 break;
234 case GL_NEAREST_MIPMAP_NEAREST:
235 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
236 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
237 break;
238 case GL_LINEAR_MIPMAP_NEAREST:
239 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
240 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
241 break;
242 case GL_NEAREST_MIPMAP_LINEAR:
243 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
244 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
245 break;
246 case GL_LINEAR_MIPMAP_LINEAR:
247 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
248 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
249 break;
250 default:
251 break;
252 }
253
254 /* Set Anisotropy:
255 */
256 if (gl_sampler->MaxAnisotropy > 1.0) {
257 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
258 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
259
260 if (gl_sampler->MaxAnisotropy > 2.0) {
261 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
262 BRW_ANISORATIO_16);
263 }
264 }
265 else {
266 switch (gl_sampler->MagFilter) {
267 case GL_NEAREST:
268 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
269 using_nearest = true;
270 break;
271 case GL_LINEAR:
272 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
273 break;
274 default:
275 break;
276 }
277 }
278
279 sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
280 using_nearest);
281 sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
282 using_nearest);
283 sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
284 using_nearest);
285
286 if (brw->gen >= 6 &&
287 sampler->ss0.min_filter != sampler->ss0.mag_filter)
288 sampler->ss0.min_mag_neq = 1;
289
290 /* Cube-maps on 965 and later must use the same wrap mode for all 3
291 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
292 */
293 if (texObj->Target == GL_TEXTURE_CUBE_MAP ||
294 texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
295 if ((ctx->Texture.CubeMapSeamless || gl_sampler->CubeMapSeamless) &&
296 (gl_sampler->MinFilter != GL_NEAREST ||
297 gl_sampler->MagFilter != GL_NEAREST)) {
298 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
299 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
300 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
301 } else {
302 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
303 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
304 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
305 }
306 } else if (texObj->Target == GL_TEXTURE_1D) {
307 /* There's a bug in 1D texture sampling - it actually pays
308 * attention to the wrap_t value, though it should not.
309 * Override the wrap_t value here to GL_REPEAT to keep
310 * any nonexistent border pixels from floating in.
311 */
312 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
313 }
314
315
316 /* Set shadow function:
317 */
318 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
319 /* Shadowing is "enabled" by emitting a particular sampler
320 * message (sample_c). So need to recompile WM program when
321 * shadow comparison is enabled on each/any texture unit.
322 */
323 sampler->ss0.shadow_function =
324 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
325 }
326
327 /* Set LOD bias:
328 */
329 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
330 gl_sampler->LodBias, -16, 15), 6);
331
332 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
333 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
334
335 sampler->ss0.base_level = U_FIXED(0, 1);
336
337 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
338 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
339
340 /* On Gen6+, the sampler can handle non-normalized texture
341 * rectangle coordinates natively
342 */
343 if (brw->gen >= 6 && texObj->Target == GL_TEXTURE_RECTANGLE) {
344 sampler->ss3.non_normalized_coord = 1;
345 }
346
347 upload_default_color(brw, gl_sampler, unit, sdc_offset);
348
349 if (brw->gen >= 6) {
350 sampler->ss2.default_color_pointer = *sdc_offset >> 5;
351 } else {
352 /* reloc */
353 sampler->ss2.default_color_pointer = (brw->batch.bo->offset +
354 *sdc_offset) >> 5;
355
356 drm_intel_bo_emit_reloc(brw->batch.bo,
357 sampler_state_table_offset +
358 ss_index * sizeof(struct brw_sampler_state) +
359 offsetof(struct brw_sampler_state, ss2),
360 brw->batch.bo, *sdc_offset,
361 I915_GEM_DOMAIN_SAMPLER, 0);
362 }
363
364 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
365 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
366 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
367 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
368 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
369 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
370 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
371 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
372 }
373
374
375 static void
376 brw_upload_sampler_state_table(struct brw_context *brw,
377 struct gl_program *prog,
378 uint32_t sampler_count,
379 uint32_t *sst_offset,
380 uint32_t *sdc_offset)
381 {
382 struct gl_context *ctx = &brw->ctx;
383 struct brw_sampler_state *samplers;
384
385 GLbitfield SamplersUsed = prog->SamplersUsed;
386
387 if (sampler_count == 0)
388 return;
389
390 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
391 sampler_count * sizeof(*samplers),
392 32, sst_offset);
393 memset(samplers, 0, sampler_count * sizeof(*samplers));
394
395 for (unsigned s = 0; s < sampler_count; s++) {
396 if (SamplersUsed & (1 << s)) {
397 const unsigned unit = prog->SamplerUnits[s];
398 if (ctx->Texture.Unit[unit]._ReallyEnabled)
399 brw_update_sampler_state(brw, unit, s, &samplers[s],
400 *sst_offset, &sdc_offset[s]);
401 }
402 }
403
404 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
405 }
406
407 static void
408 brw_upload_fs_samplers(struct brw_context *brw)
409 {
410 /* BRW_NEW_FRAGMENT_PROGRAM */
411 struct gl_program *fs = (struct gl_program *) brw->fragment_program;
412 brw->vtbl.upload_sampler_state_table(brw, fs,
413 brw->wm.base.sampler_count,
414 &brw->wm.base.sampler_offset,
415 brw->wm.base.sdc_offset);
416 }
417
418 const struct brw_tracked_state brw_fs_samplers = {
419 .dirty = {
420 .mesa = _NEW_TEXTURE,
421 .brw = BRW_NEW_BATCH |
422 BRW_NEW_FRAGMENT_PROGRAM,
423 .cache = 0
424 },
425 .emit = brw_upload_fs_samplers,
426 };
427
428 static void
429 brw_upload_vs_samplers(struct brw_context *brw)
430 {
431 struct brw_stage_state *stage_state = &brw->vs.base;
432
433 /* BRW_NEW_VERTEX_PROGRAM */
434 struct gl_program *vs = (struct gl_program *) brw->vertex_program;
435 brw->vtbl.upload_sampler_state_table(brw, vs,
436 stage_state->sampler_count,
437 &stage_state->sampler_offset,
438 stage_state->sdc_offset);
439 }
440
441
442 const struct brw_tracked_state brw_vs_samplers = {
443 .dirty = {
444 .mesa = _NEW_TEXTURE,
445 .brw = BRW_NEW_BATCH |
446 BRW_NEW_VERTEX_PROGRAM,
447 .cache = 0
448 },
449 .emit = brw_upload_vs_samplers,
450 };
451
452
453 static void
454 brw_upload_gs_samplers(struct brw_context *brw)
455 {
456 struct brw_stage_state *stage_state = &brw->gs.base;
457
458 /* BRW_NEW_GEOMETRY_PROGRAM */
459 struct gl_program *gs = (struct gl_program *) brw->geometry_program;
460 if (!gs)
461 return;
462
463 brw->vtbl.upload_sampler_state_table(brw, gs,
464 stage_state->sampler_count,
465 &stage_state->sampler_offset,
466 stage_state->sdc_offset);
467 }
468
469
470 const struct brw_tracked_state brw_gs_samplers = {
471 .dirty = {
472 .mesa = _NEW_TEXTURE,
473 .brw = BRW_NEW_BATCH |
474 BRW_NEW_GEOMETRY_PROGRAM,
475 .cache = 0
476 },
477 .emit = brw_upload_gs_samplers,
478 };
479
480
481 void
482 gen4_init_vtable_sampler_functions(struct brw_context *brw)
483 {
484 brw->vtbl.upload_sampler_state_table = brw_upload_sampler_state_table;
485 }