i965: Delete vestiges of resource streamer code.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_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 * @file brw_sampler_state.c
34 *
35 * This file contains code for emitting SAMPLER_STATE structures, which
36 * specifies filter modes, wrap modes, border color, and so on.
37 */
38
39 #include "brw_context.h"
40 #include "brw_state.h"
41 #include "brw_defines.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_mipmap_tree.h"
44
45 #include "main/macros.h"
46 #include "main/samplerobj.h"
47 #include "util/half_float.h"
48
49 /**
50 * Emit a 3DSTATE_SAMPLER_STATE_POINTERS_{VS,HS,GS,DS,PS} packet.
51 */
52 static void
53 gen7_emit_sampler_state_pointers_xs(struct brw_context *brw,
54 struct brw_stage_state *stage_state)
55 {
56 static const uint16_t packet_headers[] = {
57 [MESA_SHADER_VERTEX] = _3DSTATE_SAMPLER_STATE_POINTERS_VS,
58 [MESA_SHADER_TESS_CTRL] = _3DSTATE_SAMPLER_STATE_POINTERS_HS,
59 [MESA_SHADER_TESS_EVAL] = _3DSTATE_SAMPLER_STATE_POINTERS_DS,
60 [MESA_SHADER_GEOMETRY] = _3DSTATE_SAMPLER_STATE_POINTERS_GS,
61 [MESA_SHADER_FRAGMENT] = _3DSTATE_SAMPLER_STATE_POINTERS_PS,
62 };
63
64 /* Ivybridge requires a workaround flush before VS packets. */
65 if (brw->gen == 7 && !brw->is_haswell && !brw->is_baytrail &&
66 stage_state->stage == MESA_SHADER_VERTEX) {
67 gen7_emit_vs_workaround_flush(brw);
68 }
69
70 BEGIN_BATCH(2);
71 OUT_BATCH(packet_headers[stage_state->stage] << 16 | (2 - 2));
72 OUT_BATCH(stage_state->sampler_offset);
73 ADVANCE_BATCH();
74 }
75
76 /**
77 * Emit a SAMPLER_STATE structure, given all the fields.
78 */
79 void
80 brw_emit_sampler_state(struct brw_context *brw,
81 uint32_t *ss,
82 uint32_t batch_offset_for_sampler_state,
83 unsigned min_filter,
84 unsigned mag_filter,
85 unsigned mip_filter,
86 unsigned max_anisotropy,
87 unsigned address_rounding,
88 unsigned wrap_s,
89 unsigned wrap_t,
90 unsigned wrap_r,
91 unsigned base_level,
92 unsigned min_lod,
93 unsigned max_lod,
94 int lod_bias,
95 unsigned shadow_function,
96 bool non_normalized_coordinates,
97 uint32_t border_color_offset)
98 {
99 ss[0] = BRW_SAMPLER_LOD_PRECLAMP_ENABLE |
100 SET_FIELD(mip_filter, BRW_SAMPLER_MIP_FILTER) |
101 SET_FIELD(mag_filter, BRW_SAMPLER_MAG_FILTER) |
102 SET_FIELD(min_filter, BRW_SAMPLER_MIN_FILTER);
103
104 ss[2] = border_color_offset;
105 if (brw->gen < 6) {
106 ss[2] += brw->batch.bo->offset64; /* reloc */
107 drm_intel_bo_emit_reloc(brw->batch.bo,
108 batch_offset_for_sampler_state + 8,
109 brw->batch.bo, border_color_offset,
110 I915_GEM_DOMAIN_SAMPLER, 0);
111 }
112
113 ss[3] = SET_FIELD(max_anisotropy, BRW_SAMPLER_MAX_ANISOTROPY) |
114 SET_FIELD(address_rounding, BRW_SAMPLER_ADDRESS_ROUNDING);
115
116 if (brw->gen >= 7) {
117 ss[0] |= SET_FIELD(lod_bias & 0x1fff, GEN7_SAMPLER_LOD_BIAS);
118
119 if (min_filter == BRW_MAPFILTER_ANISOTROPIC)
120 ss[0] |= GEN7_SAMPLER_EWA_ANISOTROPIC_ALGORITHM;
121
122 ss[1] = SET_FIELD(min_lod, GEN7_SAMPLER_MIN_LOD) |
123 SET_FIELD(max_lod, GEN7_SAMPLER_MAX_LOD) |
124 SET_FIELD(shadow_function, GEN7_SAMPLER_SHADOW_FUNCTION);
125
126 ss[3] |= SET_FIELD(wrap_s, BRW_SAMPLER_TCX_WRAP_MODE) |
127 SET_FIELD(wrap_t, BRW_SAMPLER_TCY_WRAP_MODE) |
128 SET_FIELD(wrap_r, BRW_SAMPLER_TCZ_WRAP_MODE);
129
130 if (non_normalized_coordinates)
131 ss[3] |= GEN7_SAMPLER_NON_NORMALIZED_COORDINATES;
132 } else {
133 ss[0] |= SET_FIELD(lod_bias & 0x7ff, GEN4_SAMPLER_LOD_BIAS) |
134 SET_FIELD(shadow_function, GEN4_SAMPLER_SHADOW_FUNCTION);
135
136 /* This field has existed since the original i965, but is declared MBZ
137 * until Sandy Bridge. According to the PRM:
138 *
139 * "This was added to match OpenGL semantics"
140 *
141 * In particular, OpenGL allowed you to offset by 0.5 in certain cases
142 * to get slightly better filtering. On Ivy Bridge and above, it
143 * appears that this is added to RENDER_SURFACE_STATE::SurfaceMinLOD so
144 * the right value is 0.0 or 0.5 (if you want the wacky behavior). On
145 * Sandy Bridge, however, this sum does not seem to occur and you have
146 * to set it to the actual base level of the texture.
147 */
148 if (brw->gen == 6)
149 ss[0] |= SET_FIELD(base_level, BRW_SAMPLER_BASE_MIPLEVEL);
150
151 if (brw->gen == 6 && min_filter != mag_filter)
152 ss[0] |= GEN6_SAMPLER_MIN_MAG_NOT_EQUAL;
153
154 ss[1] = SET_FIELD(min_lod, GEN4_SAMPLER_MIN_LOD) |
155 SET_FIELD(max_lod, GEN4_SAMPLER_MAX_LOD) |
156 SET_FIELD(wrap_s, BRW_SAMPLER_TCX_WRAP_MODE) |
157 SET_FIELD(wrap_t, BRW_SAMPLER_TCY_WRAP_MODE) |
158 SET_FIELD(wrap_r, BRW_SAMPLER_TCZ_WRAP_MODE);
159
160 if (brw->gen >= 6 && non_normalized_coordinates)
161 ss[3] |= GEN6_SAMPLER_NON_NORMALIZED_COORDINATES;
162 }
163 }
164
165 static uint32_t
166 translate_wrap_mode(struct brw_context *brw, GLenum wrap, bool using_nearest)
167 {
168 switch (wrap) {
169 case GL_REPEAT:
170 return BRW_TEXCOORDMODE_WRAP;
171 case GL_CLAMP:
172 /* GL_CLAMP is the weird mode where coordinates are clamped to
173 * [0.0, 1.0], so linear filtering of coordinates outside of
174 * [0.0, 1.0] give you half edge texel value and half border
175 * color.
176 *
177 * Gen8+ supports this natively.
178 */
179 if (brw->gen >= 8)
180 return GEN8_TEXCOORDMODE_HALF_BORDER;
181
182 /* On Gen4-7.5, we clamp the coordinates in the fragment shader
183 * and set clamp_border here, which gets the result desired.
184 * We just use clamp(_to_edge) for nearest, because for nearest
185 * clamping to 1.0 gives border color instead of the desired
186 * edge texels.
187 */
188 if (using_nearest)
189 return BRW_TEXCOORDMODE_CLAMP;
190 else
191 return BRW_TEXCOORDMODE_CLAMP_BORDER;
192 case GL_CLAMP_TO_EDGE:
193 return BRW_TEXCOORDMODE_CLAMP;
194 case GL_CLAMP_TO_BORDER:
195 return BRW_TEXCOORDMODE_CLAMP_BORDER;
196 case GL_MIRRORED_REPEAT:
197 return BRW_TEXCOORDMODE_MIRROR;
198 case GL_MIRROR_CLAMP_TO_EDGE:
199 return BRW_TEXCOORDMODE_MIRROR_ONCE;
200 default:
201 return BRW_TEXCOORDMODE_WRAP;
202 }
203 }
204
205 /**
206 * Return true if the given wrap mode requires the border color to exist.
207 */
208 static bool
209 wrap_mode_needs_border_color(unsigned wrap_mode)
210 {
211 return wrap_mode == BRW_TEXCOORDMODE_CLAMP_BORDER ||
212 wrap_mode == GEN8_TEXCOORDMODE_HALF_BORDER;
213 }
214
215 static bool
216 has_component(mesa_format format, int i)
217 {
218 if (_mesa_is_format_color_format(format))
219 return _mesa_format_has_color_component(format, i);
220
221 /* depth and stencil have only one component */
222 return i == 0;
223 }
224
225 /**
226 * Upload SAMPLER_BORDER_COLOR_STATE.
227 */
228 static void
229 upload_default_color(struct brw_context *brw,
230 const struct gl_sampler_object *sampler,
231 mesa_format format, GLenum base_format,
232 bool is_integer_format, bool is_stencil_sampling,
233 uint32_t *sdc_offset)
234 {
235 union gl_color_union color;
236
237 switch (base_format) {
238 case GL_DEPTH_COMPONENT:
239 /* GL specs that border color for depth textures is taken from the
240 * R channel, while the hardware uses A. Spam R into all the
241 * channels for safety.
242 */
243 color.ui[0] = sampler->BorderColor.ui[0];
244 color.ui[1] = sampler->BorderColor.ui[0];
245 color.ui[2] = sampler->BorderColor.ui[0];
246 color.ui[3] = sampler->BorderColor.ui[0];
247 break;
248 case GL_ALPHA:
249 color.ui[0] = 0u;
250 color.ui[1] = 0u;
251 color.ui[2] = 0u;
252 color.ui[3] = sampler->BorderColor.ui[3];
253 break;
254 case GL_INTENSITY:
255 color.ui[0] = sampler->BorderColor.ui[0];
256 color.ui[1] = sampler->BorderColor.ui[0];
257 color.ui[2] = sampler->BorderColor.ui[0];
258 color.ui[3] = sampler->BorderColor.ui[0];
259 break;
260 case GL_LUMINANCE:
261 color.ui[0] = sampler->BorderColor.ui[0];
262 color.ui[1] = sampler->BorderColor.ui[0];
263 color.ui[2] = sampler->BorderColor.ui[0];
264 color.ui[3] = float_as_int(1.0);
265 break;
266 case GL_LUMINANCE_ALPHA:
267 color.ui[0] = sampler->BorderColor.ui[0];
268 color.ui[1] = sampler->BorderColor.ui[0];
269 color.ui[2] = sampler->BorderColor.ui[0];
270 color.ui[3] = sampler->BorderColor.ui[3];
271 break;
272 default:
273 color.ui[0] = sampler->BorderColor.ui[0];
274 color.ui[1] = sampler->BorderColor.ui[1];
275 color.ui[2] = sampler->BorderColor.ui[2];
276 color.ui[3] = sampler->BorderColor.ui[3];
277 break;
278 }
279
280 /* In some cases we use an RGBA surface format for GL RGB textures,
281 * where we've initialized the A channel to 1.0. We also have to set
282 * the border color alpha to 1.0 in that case.
283 */
284 if (base_format == GL_RGB)
285 color.ui[3] = float_as_int(1.0);
286
287 if (brw->gen >= 8) {
288 /* On Broadwell, the border color is represented as four 32-bit floats,
289 * integers, or unsigned values, interpreted according to the surface
290 * format. This matches the sampler->BorderColor union exactly; just
291 * memcpy the values.
292 */
293 uint32_t *sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
294 4 * 4, 64, sdc_offset);
295 memcpy(sdc, color.ui, 4 * 4);
296 } else if (brw->is_haswell && (is_integer_format || is_stencil_sampling)) {
297 /* Haswell's integer border color support is completely insane:
298 * SAMPLER_BORDER_COLOR_STATE is 20 DWords. The first four are
299 * for float colors. The next 12 DWords are MBZ and only exist to
300 * pad it out to a 64 byte cacheline boundary. DWords 16-19 then
301 * contain integer colors; these are only used if SURFACE_STATE
302 * has the "Integer Surface Format" bit set. Even then, the
303 * arrangement of the RGBA data devolves into madness.
304 */
305 uint32_t *sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
306 20 * 4, 512, sdc_offset);
307 memset(sdc, 0, 20 * 4);
308 sdc = &sdc[16];
309
310 bool stencil = format == MESA_FORMAT_S_UINT8 || is_stencil_sampling;
311 const int bits_per_channel =
312 _mesa_get_format_bits(format, stencil ? GL_STENCIL_BITS : GL_RED_BITS);
313
314 /* From the Haswell PRM, "Command Reference: Structures", Page 36:
315 * "If any color channel is missing from the surface format,
316 * corresponding border color should be programmed as zero and if
317 * alpha channel is missing, corresponding Alpha border color should
318 * be programmed as 1."
319 */
320 unsigned c[4] = { 0, 0, 0, 1 };
321 for (int i = 0; i < 4; i++) {
322 if (has_component(format, i))
323 c[i] = color.ui[i];
324 }
325
326 switch (bits_per_channel) {
327 case 8:
328 /* Copy RGBA in order. */
329 for (int i = 0; i < 4; i++)
330 ((uint8_t *) sdc)[i] = c[i];
331 break;
332 case 10:
333 /* R10G10B10A2_UINT is treated like a 16-bit format. */
334 case 16:
335 ((uint16_t *) sdc)[0] = c[0]; /* R -> DWord 0, bits 15:0 */
336 ((uint16_t *) sdc)[1] = c[1]; /* G -> DWord 0, bits 31:16 */
337 /* DWord 1 is Reserved/MBZ! */
338 ((uint16_t *) sdc)[4] = c[2]; /* B -> DWord 2, bits 15:0 */
339 ((uint16_t *) sdc)[5] = c[3]; /* A -> DWord 3, bits 31:16 */
340 break;
341 case 32:
342 if (base_format == GL_RG) {
343 /* Careful inspection of the tables reveals that for RG32 formats,
344 * the green channel needs to go where blue normally belongs.
345 */
346 sdc[0] = c[0];
347 sdc[2] = c[1];
348 sdc[3] = 1;
349 } else {
350 /* Copy RGBA in order. */
351 for (int i = 0; i < 4; i++)
352 sdc[i] = c[i];
353 }
354 break;
355 default:
356 assert(!"Invalid number of bits per channel in integer format.");
357 break;
358 }
359 } else if (brw->gen == 5 || brw->gen == 6) {
360 struct gen5_sampler_default_color *sdc;
361
362 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
363 sizeof(*sdc), 32, sdc_offset);
364
365 memset(sdc, 0, sizeof(*sdc));
366
367 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color.f[0]);
368 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color.f[1]);
369 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color.f[2]);
370 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color.f[3]);
371
372 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color.f[0]);
373 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color.f[1]);
374 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color.f[2]);
375 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color.f[3]);
376
377 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color.f[0]);
378 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color.f[1]);
379 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color.f[2]);
380 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color.f[3]);
381
382 sdc->hf[0] = _mesa_float_to_half(color.f[0]);
383 sdc->hf[1] = _mesa_float_to_half(color.f[1]);
384 sdc->hf[2] = _mesa_float_to_half(color.f[2]);
385 sdc->hf[3] = _mesa_float_to_half(color.f[3]);
386
387 sdc->b[0] = sdc->s[0] >> 8;
388 sdc->b[1] = sdc->s[1] >> 8;
389 sdc->b[2] = sdc->s[2] >> 8;
390 sdc->b[3] = sdc->s[3] >> 8;
391
392 sdc->f[0] = color.f[0];
393 sdc->f[1] = color.f[1];
394 sdc->f[2] = color.f[2];
395 sdc->f[3] = color.f[3];
396 } else {
397 float *sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
398 4 * 4, 32, sdc_offset);
399 memcpy(sdc, color.f, 4 * 4);
400 }
401 }
402
403 /**
404 * Sets the sampler state for a single unit based off of the sampler key
405 * entry.
406 */
407 static void
408 brw_update_sampler_state(struct brw_context *brw,
409 GLenum target, bool tex_cube_map_seamless,
410 GLfloat tex_unit_lod_bias,
411 mesa_format format, GLenum base_format,
412 const struct gl_texture_object *texObj,
413 const struct gl_sampler_object *sampler,
414 uint32_t *sampler_state,
415 uint32_t batch_offset_for_sampler_state)
416 {
417 unsigned min_filter, mag_filter, mip_filter;
418
419 /* Select min and mip filters. */
420 switch (sampler->MinFilter) {
421 case GL_NEAREST:
422 min_filter = BRW_MAPFILTER_NEAREST;
423 mip_filter = BRW_MIPFILTER_NONE;
424 break;
425 case GL_LINEAR:
426 min_filter = BRW_MAPFILTER_LINEAR;
427 mip_filter = BRW_MIPFILTER_NONE;
428 break;
429 case GL_NEAREST_MIPMAP_NEAREST:
430 min_filter = BRW_MAPFILTER_NEAREST;
431 mip_filter = BRW_MIPFILTER_NEAREST;
432 break;
433 case GL_LINEAR_MIPMAP_NEAREST:
434 min_filter = BRW_MAPFILTER_LINEAR;
435 mip_filter = BRW_MIPFILTER_NEAREST;
436 break;
437 case GL_NEAREST_MIPMAP_LINEAR:
438 min_filter = BRW_MAPFILTER_NEAREST;
439 mip_filter = BRW_MIPFILTER_LINEAR;
440 break;
441 case GL_LINEAR_MIPMAP_LINEAR:
442 min_filter = BRW_MAPFILTER_LINEAR;
443 mip_filter = BRW_MIPFILTER_LINEAR;
444 break;
445 default:
446 unreachable("not reached");
447 }
448
449 /* Select mag filter. */
450 if (sampler->MagFilter == GL_LINEAR)
451 mag_filter = BRW_MAPFILTER_LINEAR;
452 else
453 mag_filter = BRW_MAPFILTER_NEAREST;
454
455 /* Enable anisotropic filtering if desired. */
456 unsigned max_anisotropy = BRW_ANISORATIO_2;
457 if (sampler->MaxAnisotropy > 1.0f) {
458 min_filter = BRW_MAPFILTER_ANISOTROPIC;
459 mag_filter = BRW_MAPFILTER_ANISOTROPIC;
460
461 if (sampler->MaxAnisotropy > 2.0f) {
462 max_anisotropy =
463 MIN2((sampler->MaxAnisotropy - 2) / 2, BRW_ANISORATIO_16);
464 }
465 }
466
467 /* Set address rounding bits if not using nearest filtering. */
468 unsigned address_rounding = 0;
469 if (min_filter != BRW_MAPFILTER_NEAREST) {
470 address_rounding |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
471 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
472 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
473 }
474 if (mag_filter != BRW_MAPFILTER_NEAREST) {
475 address_rounding |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
476 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
477 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
478 }
479
480 bool either_nearest =
481 sampler->MinFilter == GL_NEAREST || sampler->MagFilter == GL_NEAREST;
482 unsigned wrap_s = translate_wrap_mode(brw, sampler->WrapS, either_nearest);
483 unsigned wrap_t = translate_wrap_mode(brw, sampler->WrapT, either_nearest);
484 unsigned wrap_r = translate_wrap_mode(brw, sampler->WrapR, either_nearest);
485
486 if (target == GL_TEXTURE_CUBE_MAP ||
487 target == GL_TEXTURE_CUBE_MAP_ARRAY) {
488 /* Cube maps must use the same wrap mode for all three coordinate
489 * dimensions. Prior to Haswell, only CUBE and CLAMP are valid.
490 *
491 * Ivybridge and Baytrail seem to have problems with CUBE mode and
492 * integer formats. Fall back to CLAMP for now.
493 */
494 if ((tex_cube_map_seamless || sampler->CubeMapSeamless) &&
495 !(brw->gen == 7 && !brw->is_haswell && texObj->_IsIntegerFormat)) {
496 wrap_s = BRW_TEXCOORDMODE_CUBE;
497 wrap_t = BRW_TEXCOORDMODE_CUBE;
498 wrap_r = BRW_TEXCOORDMODE_CUBE;
499 } else {
500 wrap_s = BRW_TEXCOORDMODE_CLAMP;
501 wrap_t = BRW_TEXCOORDMODE_CLAMP;
502 wrap_r = BRW_TEXCOORDMODE_CLAMP;
503 }
504 } else if (target == GL_TEXTURE_1D) {
505 /* There's a bug in 1D texture sampling - it actually pays
506 * attention to the wrap_t value, though it should not.
507 * Override the wrap_t value here to GL_REPEAT to keep
508 * any nonexistent border pixels from floating in.
509 */
510 wrap_t = BRW_TEXCOORDMODE_WRAP;
511 }
512
513 /* Set shadow function. */
514 unsigned shadow_function = 0;
515 if (sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
516 shadow_function =
517 intel_translate_shadow_compare_func(sampler->CompareFunc);
518 }
519
520 const int lod_bits = brw->gen >= 7 ? 8 : 6;
521 const float hw_max_lod = brw->gen >= 7 ? 14 : 13;
522 const unsigned base_level =
523 U_FIXED(CLAMP(texObj->MinLevel + texObj->BaseLevel, 0, hw_max_lod), 1);
524 const unsigned min_lod =
525 U_FIXED(CLAMP(sampler->MinLod, 0, hw_max_lod), lod_bits);
526 const unsigned max_lod =
527 U_FIXED(CLAMP(sampler->MaxLod, 0, hw_max_lod), lod_bits);
528 const int lod_bias =
529 S_FIXED(CLAMP(tex_unit_lod_bias + sampler->LodBias, -16, 15), lod_bits);
530
531 /* Upload the border color if necessary. If not, just point it at
532 * offset 0 (the start of the batch) - the color should be ignored,
533 * but that address won't fault in case something reads it anyway.
534 */
535 uint32_t border_color_offset = 0;
536 if (wrap_mode_needs_border_color(wrap_s) ||
537 wrap_mode_needs_border_color(wrap_t) ||
538 wrap_mode_needs_border_color(wrap_r)) {
539 upload_default_color(brw, sampler, format, base_format,
540 texObj->_IsIntegerFormat, texObj->StencilSampling,
541 &border_color_offset);
542 }
543
544 const bool non_normalized_coords = target == GL_TEXTURE_RECTANGLE;
545
546 brw_emit_sampler_state(brw,
547 sampler_state,
548 batch_offset_for_sampler_state,
549 min_filter, mag_filter, mip_filter,
550 max_anisotropy,
551 address_rounding,
552 wrap_s, wrap_t, wrap_r,
553 base_level, min_lod, max_lod, lod_bias,
554 shadow_function,
555 non_normalized_coords,
556 border_color_offset);
557 }
558
559 static void
560 update_sampler_state(struct brw_context *brw,
561 int unit,
562 uint32_t *sampler_state,
563 uint32_t batch_offset_for_sampler_state)
564 {
565 struct gl_context *ctx = &brw->ctx;
566 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
567 const struct gl_texture_object *texObj = texUnit->_Current;
568 const struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
569
570 /* These don't use samplers at all. */
571 if (texObj->Target == GL_TEXTURE_BUFFER)
572 return;
573
574 struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
575 brw_update_sampler_state(brw, texObj->Target, ctx->Texture.CubeMapSeamless,
576 texUnit->LodBias,
577 firstImage->TexFormat, firstImage->_BaseFormat,
578 texObj, sampler,
579 sampler_state, batch_offset_for_sampler_state);
580 }
581
582 static void
583 brw_upload_sampler_state_table(struct brw_context *brw,
584 struct gl_program *prog,
585 struct brw_stage_state *stage_state)
586 {
587 struct gl_context *ctx = &brw->ctx;
588 uint32_t sampler_count = stage_state->sampler_count;
589
590 GLbitfield SamplersUsed = prog->SamplersUsed;
591
592 if (sampler_count == 0)
593 return;
594
595 /* SAMPLER_STATE is 4 DWords on all platforms. */
596 const int dwords = 4;
597 const int size_in_bytes = dwords * sizeof(uint32_t);
598
599 uint32_t *sampler_state = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
600 sampler_count * size_in_bytes,
601 32, &stage_state->sampler_offset);
602 memset(sampler_state, 0, sampler_count * size_in_bytes);
603
604 uint32_t batch_offset_for_sampler_state = stage_state->sampler_offset;
605
606 for (unsigned s = 0; s < sampler_count; s++) {
607 if (SamplersUsed & (1 << s)) {
608 const unsigned unit = prog->SamplerUnits[s];
609 if (ctx->Texture.Unit[unit]._Current) {
610 update_sampler_state(brw, unit, sampler_state,
611 batch_offset_for_sampler_state);
612 }
613 }
614
615 sampler_state += dwords;
616 batch_offset_for_sampler_state += size_in_bytes;
617 }
618
619 if (brw->gen >= 7 && stage_state->stage != MESA_SHADER_COMPUTE) {
620 /* Emit a 3DSTATE_SAMPLER_STATE_POINTERS_XS packet. */
621 gen7_emit_sampler_state_pointers_xs(brw, stage_state);
622 } else {
623 /* Flag that the sampler state table pointer has changed; later atoms
624 * will handle it.
625 */
626 brw->ctx.NewDriverState |= BRW_NEW_SAMPLER_STATE_TABLE;
627 }
628 }
629
630 static void
631 brw_upload_fs_samplers(struct brw_context *brw)
632 {
633 /* BRW_NEW_FRAGMENT_PROGRAM */
634 struct gl_program *fs = (struct gl_program *) brw->fragment_program;
635 brw_upload_sampler_state_table(brw, fs, &brw->wm.base);
636 }
637
638 const struct brw_tracked_state brw_fs_samplers = {
639 .dirty = {
640 .mesa = _NEW_TEXTURE,
641 .brw = BRW_NEW_BATCH |
642 BRW_NEW_BLORP |
643 BRW_NEW_FRAGMENT_PROGRAM,
644 },
645 .emit = brw_upload_fs_samplers,
646 };
647
648 static void
649 brw_upload_vs_samplers(struct brw_context *brw)
650 {
651 /* BRW_NEW_VERTEX_PROGRAM */
652 struct gl_program *vs = (struct gl_program *) brw->vertex_program;
653 brw_upload_sampler_state_table(brw, vs, &brw->vs.base);
654 }
655
656
657 const struct brw_tracked_state brw_vs_samplers = {
658 .dirty = {
659 .mesa = _NEW_TEXTURE,
660 .brw = BRW_NEW_BATCH |
661 BRW_NEW_BLORP |
662 BRW_NEW_VERTEX_PROGRAM,
663 },
664 .emit = brw_upload_vs_samplers,
665 };
666
667
668 static void
669 brw_upload_gs_samplers(struct brw_context *brw)
670 {
671 /* BRW_NEW_GEOMETRY_PROGRAM */
672 struct gl_program *gs = (struct gl_program *) brw->geometry_program;
673 if (!gs)
674 return;
675
676 brw_upload_sampler_state_table(brw, gs, &brw->gs.base);
677 }
678
679
680 const struct brw_tracked_state brw_gs_samplers = {
681 .dirty = {
682 .mesa = _NEW_TEXTURE,
683 .brw = BRW_NEW_BATCH |
684 BRW_NEW_BLORP |
685 BRW_NEW_GEOMETRY_PROGRAM,
686 },
687 .emit = brw_upload_gs_samplers,
688 };
689
690
691 static void
692 brw_upload_tcs_samplers(struct brw_context *brw)
693 {
694 /* BRW_NEW_TESS_PROGRAMS */
695 struct gl_program *tcs = (struct gl_program *) brw->tess_ctrl_program;
696 if (!tcs)
697 return;
698
699 brw_upload_sampler_state_table(brw, tcs, &brw->tcs.base);
700 }
701
702
703 const struct brw_tracked_state brw_tcs_samplers = {
704 .dirty = {
705 .mesa = _NEW_TEXTURE,
706 .brw = BRW_NEW_BATCH |
707 BRW_NEW_BLORP |
708 BRW_NEW_TESS_PROGRAMS,
709 },
710 .emit = brw_upload_tcs_samplers,
711 };
712
713
714 static void
715 brw_upload_tes_samplers(struct brw_context *brw)
716 {
717 /* BRW_NEW_TESS_PROGRAMS */
718 struct gl_program *tes = (struct gl_program *) brw->tess_eval_program;
719 if (!tes)
720 return;
721
722 brw_upload_sampler_state_table(brw, tes, &brw->tes.base);
723 }
724
725
726 const struct brw_tracked_state brw_tes_samplers = {
727 .dirty = {
728 .mesa = _NEW_TEXTURE,
729 .brw = BRW_NEW_BATCH |
730 BRW_NEW_BLORP |
731 BRW_NEW_TESS_PROGRAMS,
732 },
733 .emit = brw_upload_tes_samplers,
734 };
735
736 static void
737 brw_upload_cs_samplers(struct brw_context *brw)
738 {
739 /* BRW_NEW_COMPUTE_PROGRAM */
740 struct gl_program *cs = (struct gl_program *) brw->compute_program;
741 if (!cs)
742 return;
743
744 brw_upload_sampler_state_table(brw, cs, &brw->cs.base);
745 }
746
747 const struct brw_tracked_state brw_cs_samplers = {
748 .dirty = {
749 .mesa = _NEW_TEXTURE,
750 .brw = BRW_NEW_BATCH |
751 BRW_NEW_BLORP |
752 BRW_NEW_COMPUTE_PROGRAM,
753 },
754 .emit = brw_upload_cs_samplers,
755 };