i965: Remove four and a half year old TODO comments about samplers.
[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)
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 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
93 /* GL specs that border color for depth textures is taken from the
94 * R channel, while the hardware uses A. Spam R into all the
95 * channels for safety.
96 */
97 color[0] = sampler->BorderColor.f[0];
98 color[1] = sampler->BorderColor.f[0];
99 color[2] = sampler->BorderColor.f[0];
100 color[3] = sampler->BorderColor.f[0];
101 } else {
102 color[0] = sampler->BorderColor.f[0];
103 color[1] = sampler->BorderColor.f[1];
104 color[2] = sampler->BorderColor.f[2];
105 color[3] = sampler->BorderColor.f[3];
106 }
107
108 if (intel->gen == 5 || intel->gen == 6) {
109 struct gen5_sampler_default_color *sdc;
110
111 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
112 sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
113
114 memset(sdc, 0, sizeof(*sdc));
115
116 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
117 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
118 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
119 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
120
121 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
122 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
123 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
124 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
125
126 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
127 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
128 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
129 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
130
131 sdc->hf[0] = _mesa_float_to_half(color[0]);
132 sdc->hf[1] = _mesa_float_to_half(color[1]);
133 sdc->hf[2] = _mesa_float_to_half(color[2]);
134 sdc->hf[3] = _mesa_float_to_half(color[3]);
135
136 sdc->b[0] = sdc->s[0] >> 8;
137 sdc->b[1] = sdc->s[1] >> 8;
138 sdc->b[2] = sdc->s[2] >> 8;
139 sdc->b[3] = sdc->s[3] >> 8;
140
141 sdc->f[0] = color[0];
142 sdc->f[1] = color[1];
143 sdc->f[2] = color[2];
144 sdc->f[3] = color[3];
145 } else {
146 struct brw_sampler_default_color *sdc;
147
148 sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
149 sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
150
151 COPY_4V(sdc->color, color);
152 }
153 }
154
155 /**
156 * Sets the sampler state for a single unit based off of the sampler key
157 * entry.
158 */
159 static void brw_update_sampler_state(struct brw_context *brw,
160 int unit,
161 struct brw_sampler_state *sampler)
162 {
163 struct intel_context *intel = &brw->intel;
164 struct gl_context *ctx = &intel->ctx;
165 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
166 struct gl_texture_object *texObj = texUnit->_Current;
167 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
168 bool using_nearest = false;
169
170 /* These don't use samplers at all. */
171 if (texObj->Target == GL_TEXTURE_BUFFER)
172 return;
173
174 switch (gl_sampler->MinFilter) {
175 case GL_NEAREST:
176 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
177 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
178 using_nearest = true;
179 break;
180 case GL_LINEAR:
181 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
182 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
183 break;
184 case GL_NEAREST_MIPMAP_NEAREST:
185 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
186 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
187 break;
188 case GL_LINEAR_MIPMAP_NEAREST:
189 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
190 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
191 break;
192 case GL_NEAREST_MIPMAP_LINEAR:
193 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
194 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
195 break;
196 case GL_LINEAR_MIPMAP_LINEAR:
197 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
198 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
199 break;
200 default:
201 break;
202 }
203
204 /* Set Anisotropy:
205 */
206 if (gl_sampler->MaxAnisotropy > 1.0) {
207 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
208 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
209
210 if (gl_sampler->MaxAnisotropy > 2.0) {
211 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
212 BRW_ANISORATIO_16);
213 }
214 }
215 else {
216 switch (gl_sampler->MagFilter) {
217 case GL_NEAREST:
218 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
219 using_nearest = true;
220 break;
221 case GL_LINEAR:
222 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
223 break;
224 default:
225 break;
226 }
227 }
228
229 sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
230 using_nearest);
231 sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
232 using_nearest);
233 sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
234 using_nearest);
235
236 if (intel->gen >= 6 &&
237 sampler->ss0.min_filter != sampler->ss0.mag_filter)
238 sampler->ss0.min_mag_neq = 1;
239
240 /* Cube-maps on 965 and later must use the same wrap mode for all 3
241 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
242 */
243 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
244 if (ctx->Texture.CubeMapSeamless &&
245 (gl_sampler->MinFilter != GL_NEAREST ||
246 gl_sampler->MagFilter != GL_NEAREST)) {
247 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
248 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
249 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
250 } else {
251 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
252 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
253 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
254 }
255 } else if (texObj->Target == GL_TEXTURE_1D) {
256 /* There's a bug in 1D texture sampling - it actually pays
257 * attention to the wrap_t value, though it should not.
258 * Override the wrap_t value here to GL_REPEAT to keep
259 * any nonexistent border pixels from floating in.
260 */
261 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
262 }
263
264
265 /* Set shadow function:
266 */
267 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
268 /* Shadowing is "enabled" by emitting a particular sampler
269 * message (sample_c). So need to recompile WM program when
270 * shadow comparison is enabled on each/any texture unit.
271 */
272 sampler->ss0.shadow_function =
273 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
274 }
275
276 /* Set LOD bias:
277 */
278 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
279 gl_sampler->LodBias, -16, 15), 6);
280
281 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
282 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
283
284 /* Set BaseMipLevel, MaxLOD, MinLOD:
285 *
286 * XXX: I don't think that using firstLevel, lastLevel works,
287 * because we always setup the surface state as if firstLevel ==
288 * level zero. Probably have to subtract firstLevel from each of
289 * these:
290 */
291 sampler->ss0.base_level = U_FIXED(0, 1);
292
293 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
294 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
295
296 /* On Gen6+, the sampler can handle non-normalized texture
297 * rectangle coordinates natively
298 */
299 if (intel->gen >= 6 && texObj->Target == GL_TEXTURE_RECTANGLE) {
300 sampler->ss3.non_normalized_coord = 1;
301 }
302
303 upload_default_color(brw, gl_sampler, unit);
304
305 if (intel->gen >= 6) {
306 sampler->ss2.default_color_pointer = brw->wm.sdc_offset[unit] >> 5;
307 } else {
308 /* reloc */
309 sampler->ss2.default_color_pointer = (intel->batch.bo->offset +
310 brw->wm.sdc_offset[unit]) >> 5;
311
312 drm_intel_bo_emit_reloc(intel->batch.bo,
313 brw->sampler.offset +
314 unit * sizeof(struct brw_sampler_state) +
315 offsetof(struct brw_sampler_state, ss2),
316 intel->batch.bo, brw->wm.sdc_offset[unit],
317 I915_GEM_DOMAIN_SAMPLER, 0);
318 }
319
320 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
321 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
322 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
323 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
324 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
325 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
326 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
327 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
328 }
329
330
331 static void
332 brw_upload_samplers(struct brw_context *brw)
333 {
334 struct gl_context *ctx = &brw->intel.ctx;
335 struct brw_sampler_state *samplers;
336 int i;
337
338 brw->sampler.count = 0;
339 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
340 if (ctx->Texture.Unit[i]._ReallyEnabled)
341 brw->sampler.count = i + 1;
342 }
343
344 if (brw->sampler.count == 0)
345 return;
346
347 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
348 brw->sampler.count * sizeof(*samplers),
349 32, &brw->sampler.offset);
350 memset(samplers, 0, brw->sampler.count * sizeof(*samplers));
351
352 for (i = 0; i < brw->sampler.count; i++) {
353 if (ctx->Texture.Unit[i]._ReallyEnabled)
354 brw_update_sampler_state(brw, i, &samplers[i]);
355 }
356
357 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
358 }
359
360 const struct brw_tracked_state brw_samplers = {
361 .dirty = {
362 .mesa = _NEW_TEXTURE,
363 .brw = BRW_NEW_BATCH,
364 .cache = 0
365 },
366 .emit = brw_upload_samplers,
367 };
368
369