i965: Enable EWA anisotropic filtering algorithm
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sampler_state.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "intel_batchbuffer.h"
28 #include "intel_mipmap_tree.h"
29
30 #include "main/macros.h"
31 #include "main/samplerobj.h"
32
33 /**
34 * Sets the sampler state for a single unit.
35 */
36 static void
37 gen7_update_sampler_state(struct brw_context *brw, int unit, int ss_index,
38 struct gen7_sampler_state *sampler,
39 uint32_t *sdc_offset)
40 {
41 struct gl_context *ctx = &brw->ctx;
42 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
43 struct gl_texture_object *texObj = texUnit->_Current;
44 struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
45 bool using_nearest = false;
46
47 /* These don't use samplers at all. */
48 if (texObj->Target == GL_TEXTURE_BUFFER)
49 return;
50
51 switch (gl_sampler->MinFilter) {
52 case GL_NEAREST:
53 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
54 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
55 using_nearest = true;
56 break;
57 case GL_LINEAR:
58 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
59 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
60 break;
61 case GL_NEAREST_MIPMAP_NEAREST:
62 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
63 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
64 break;
65 case GL_LINEAR_MIPMAP_NEAREST:
66 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
67 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
68 break;
69 case GL_NEAREST_MIPMAP_LINEAR:
70 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
71 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
72 break;
73 case GL_LINEAR_MIPMAP_LINEAR:
74 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
75 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
76 break;
77 default:
78 break;
79 }
80
81 /* Set Anisotropy: */
82 if (gl_sampler->MaxAnisotropy > 1.0) {
83 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
84 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
85 sampler->ss0.aniso_algorithm = 1;
86
87 if (gl_sampler->MaxAnisotropy > 2.0) {
88 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
89 BRW_ANISORATIO_16);
90 }
91 }
92 else {
93 switch (gl_sampler->MagFilter) {
94 case GL_NEAREST:
95 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
96 using_nearest = true;
97 break;
98 case GL_LINEAR:
99 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
100 break;
101 default:
102 break;
103 }
104 }
105
106 sampler->ss3.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
107 using_nearest);
108 sampler->ss3.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
109 using_nearest);
110 sampler->ss3.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
111 using_nearest);
112
113 /* Cube-maps on 965 and later must use the same wrap mode for all 3
114 * coordinate dimensions. Futher, only CUBE and CLAMP are valid.
115 */
116 if (texObj->Target == GL_TEXTURE_CUBE_MAP ||
117 texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
118 if ((ctx->Texture.CubeMapSeamless || gl_sampler->CubeMapSeamless) &&
119 (gl_sampler->MinFilter != GL_NEAREST ||
120 gl_sampler->MagFilter != GL_NEAREST)) {
121 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
122 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
123 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
124 } else {
125 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
126 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
127 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
128 }
129 } else if (texObj->Target == GL_TEXTURE_1D) {
130 /* There's a bug in 1D texture sampling - it actually pays
131 * attention to the wrap_t value, though it should not.
132 * Override the wrap_t value here to GL_REPEAT to keep
133 * any nonexistent border pixels from floating in.
134 */
135 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
136 }
137
138 /* Set shadow function: */
139 if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
140 /* Shadowing is "enabled" by emitting a particular sampler
141 * message (sample_c). So need to recompile WM program when
142 * shadow comparison is enabled on each/any texture unit.
143 */
144 sampler->ss1.shadow_function =
145 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
146 }
147
148 /* Set LOD bias: */
149 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
150 gl_sampler->LodBias, -16, 15), 8);
151
152 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
153 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
154
155 sampler->ss0.base_level = U_FIXED(0, 1);
156
157 sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 8);
158 sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 8);
159
160 /* The sampler can handle non-normalized texture rectangle coordinates
161 * natively
162 */
163 if (texObj->Target == GL_TEXTURE_RECTANGLE) {
164 sampler->ss3.non_normalized_coord = 1;
165 }
166
167 upload_default_color(brw, gl_sampler, unit, sdc_offset);
168
169 sampler->ss2.default_color_pointer = *sdc_offset >> 5;
170
171 if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
172 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
173 BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
174 BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
175 if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
176 sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
177 BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
178 BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
179 }
180
181
182 static void
183 gen7_upload_sampler_state_table(struct brw_context *brw,
184 struct gl_program *prog,
185 uint32_t sampler_count,
186 uint32_t *sst_offset,
187 uint32_t *sdc_offset)
188 {
189 struct gl_context *ctx = &brw->ctx;
190 struct gen7_sampler_state *samplers;
191
192 GLbitfield SamplersUsed = prog->SamplersUsed;
193
194 if (sampler_count == 0)
195 return;
196
197 samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
198 sampler_count * sizeof(*samplers),
199 32, sst_offset);
200 memset(samplers, 0, sampler_count * sizeof(*samplers));
201
202 for (unsigned s = 0; s < sampler_count; s++) {
203 if (SamplersUsed & (1 << s)) {
204 const unsigned unit = prog->SamplerUnits[s];
205 if (ctx->Texture.Unit[unit]._ReallyEnabled)
206 gen7_update_sampler_state(brw, unit, s, &samplers[s],
207 &sdc_offset[s]);
208 }
209 }
210
211 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
212 }
213
214 void
215 gen7_init_vtable_sampler_functions(struct brw_context *brw)
216 {
217 brw->vtbl.upload_sampler_state_table = gen7_upload_sampler_state_table;
218 }