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