2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
37 #include "main/macros.h"
38 #include "main/samplerobj.h"
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.
48 translate_wrap_mode(GLenum wrap, bool using_nearest)
52 return BRW_TEXCOORDMODE_WRAP;
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
64 return BRW_TEXCOORDMODE_CLAMP;
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;
74 return BRW_TEXCOORDMODE_WRAP;
79 * Upload SAMPLER_BORDER_COLOR_STATE.
82 upload_default_color(struct brw_context *brw, struct gl_sampler_object *sampler,
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];
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.
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];
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];
108 if (intel->gen == 5 || intel->gen == 6) {
109 struct gen5_sampler_default_color *sdc;
111 sdc = brw_state_batch(brw, sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
113 memset(sdc, 0, sizeof(*sdc));
115 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
116 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
117 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
118 UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
120 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
121 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
122 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
123 UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
125 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
126 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
127 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
128 UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
130 sdc->hf[0] = _mesa_float_to_half(color[0]);
131 sdc->hf[1] = _mesa_float_to_half(color[1]);
132 sdc->hf[2] = _mesa_float_to_half(color[2]);
133 sdc->hf[3] = _mesa_float_to_half(color[3]);
135 sdc->b[0] = sdc->s[0] >> 8;
136 sdc->b[1] = sdc->s[1] >> 8;
137 sdc->b[2] = sdc->s[2] >> 8;
138 sdc->b[3] = sdc->s[3] >> 8;
140 sdc->f[0] = color[0];
141 sdc->f[1] = color[1];
142 sdc->f[2] = color[2];
143 sdc->f[3] = color[3];
145 struct brw_sampler_default_color *sdc;
147 sdc = brw_state_batch(brw, sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
149 COPY_4V(sdc->color, color);
154 * Sets the sampler state for a single unit based off of the sampler key
157 static void brw_update_sampler_state(struct brw_context *brw,
159 struct brw_sampler_state *sampler)
161 struct intel_context *intel = &brw->intel;
162 struct gl_context *ctx = &intel->ctx;
163 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
164 struct gl_texture_object *texObj = texUnit->_Current;
165 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
166 bool using_nearest = false;
168 switch (gl_sampler->MinFilter) {
170 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
171 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
172 using_nearest = true;
175 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
176 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
178 case GL_NEAREST_MIPMAP_NEAREST:
179 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
180 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
182 case GL_LINEAR_MIPMAP_NEAREST:
183 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
184 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
186 case GL_NEAREST_MIPMAP_LINEAR:
187 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
188 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
190 case GL_LINEAR_MIPMAP_LINEAR:
191 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
192 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
200 if (gl_sampler->MaxAnisotropy > 1.0) {
201 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
202 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
204 if (gl_sampler->MaxAnisotropy > 2.0) {
205 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
210 switch (gl_sampler->MagFilter) {
212 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
213 using_nearest = true;
216 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
223 sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
225 sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
227 sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
230 if (intel->gen >= 6 &&
231 sampler->ss0.min_filter != sampler->ss0.mag_filter)
232 sampler->ss0.min_mag_neq = 1;
234 /* Cube-maps on 965 and later must use the same wrap mode for all 3
235 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
237 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
238 if (ctx->Texture.CubeMapSeamless &&
239 (gl_sampler->MinFilter != GL_NEAREST ||
240 gl_sampler->MagFilter != GL_NEAREST)) {
241 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
242 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
243 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
245 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
246 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
247 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
249 } else if (texObj->Target == GL_TEXTURE_1D) {
250 /* There's a bug in 1D texture sampling - it actually pays
251 * attention to the wrap_t value, though it should not.
252 * Override the wrap_t value here to GL_REPEAT to keep
253 * any nonexistent border pixels from floating in.
255 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
259 /* Set shadow function:
261 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
262 /* Shadowing is "enabled" by emitting a particular sampler
263 * message (sample_c). So need to recompile WM program when
264 * shadow comparison is enabled on each/any texture unit.
266 sampler->ss0.shadow_function =
267 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
272 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
273 gl_sampler->LodBias, -16, 15), 6);
275 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
276 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
278 /* Set BaseMipLevel, MaxLOD, MinLOD:
280 * XXX: I don't think that using firstLevel, lastLevel works,
281 * because we always setup the surface state as if firstLevel ==
282 * level zero. Probably have to subtract firstLevel from each of
285 sampler->ss0.base_level = U_FIXED(0, 1);
287 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
288 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
290 upload_default_color(brw, gl_sampler, unit);
292 if (intel->gen >= 6) {
293 sampler->ss2.default_color_pointer = brw->wm.sdc_offset[unit] >> 5;
296 sampler->ss2.default_color_pointer = (intel->batch.bo->offset +
297 brw->wm.sdc_offset[unit]) >> 5;
299 drm_intel_bo_emit_reloc(intel->batch.bo,
300 brw->wm.sampler_offset +
301 unit * sizeof(struct brw_sampler_state) +
302 offsetof(struct brw_sampler_state, ss2),
303 intel->batch.bo, brw->wm.sdc_offset[unit],
304 I915_GEM_DOMAIN_SAMPLER, 0);
307 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
308 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
309 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
310 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
311 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
312 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
313 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
314 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
318 /* All samplers must be uploaded in a single contiguous array, which
319 * complicates various things. However, this is still too confusing -
320 * FIXME: simplify all the different new texture state flags.
323 prepare_wm_samplers(struct brw_context *brw)
325 struct gl_context *ctx = &brw->intel.ctx;
326 struct brw_sampler_state *samplers;
329 brw->wm.sampler_count = 0;
330 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
331 if (ctx->Texture.Unit[i]._ReallyEnabled)
332 brw->wm.sampler_count = i + 1;
335 if (brw->wm.sampler_count == 0)
338 samplers = brw_state_batch(brw, brw->wm.sampler_count * sizeof(*samplers),
339 32, &brw->wm.sampler_offset);
340 memset(samplers, 0, brw->wm.sampler_count * sizeof(*samplers));
342 for (i = 0; i < brw->wm.sampler_count; i++) {
343 if (ctx->Texture.Unit[i]._ReallyEnabled)
344 brw_update_sampler_state(brw, i, &samplers[i]);
347 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
350 const struct brw_tracked_state brw_wm_samplers = {
352 .mesa = _NEW_TEXTURE,
353 .brw = BRW_NEW_BATCH,
356 .prepare = prepare_wm_samplers,