[965] Replace the state cache suballocator with direct dri_bufmgr use.
[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 "macros.h"
38
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 /* The brw (and related graphics cores) do not support GL_CLAMP. The
48 * Intel drivers for "other operating systems" implement GL_CLAMP as
49 * GL_CLAMP_TO_EDGE, so the same is done here.
50 */
51 static GLuint translate_wrap_mode( GLenum wrap )
52 {
53 switch( wrap ) {
54 case GL_REPEAT:
55 return BRW_TEXCOORDMODE_WRAP;
56 case GL_CLAMP:
57 return BRW_TEXCOORDMODE_CLAMP;
58 case GL_CLAMP_TO_EDGE:
59 return BRW_TEXCOORDMODE_CLAMP; /* conform likes it this way */
60 case GL_CLAMP_TO_BORDER:
61 return BRW_TEXCOORDMODE_CLAMP_BORDER;
62 case GL_MIRRORED_REPEAT:
63 return BRW_TEXCOORDMODE_MIRROR;
64 default:
65 return BRW_TEXCOORDMODE_WRAP;
66 }
67 }
68
69
70 static GLuint U_FIXED(GLfloat value, GLuint frac_bits)
71 {
72 value *= (1<<frac_bits);
73 return value < 0 ? 0 : value;
74 }
75
76 static GLint S_FIXED(GLfloat value, GLuint frac_bits)
77 {
78 return value * (1<<frac_bits);
79 }
80
81
82 static dri_bo *upload_default_color( struct brw_context *brw,
83 const GLfloat *color )
84 {
85 struct brw_sampler_default_color sdc;
86
87 COPY_4V(sdc.color, color);
88
89 return brw_cache_data( &brw->cache, BRW_SAMPLER_DEFAULT_COLOR, &sdc,
90 NULL, 0 );
91 }
92
93
94 /*
95 */
96 static void brw_update_sampler_state( struct gl_texture_unit *texUnit,
97 struct gl_texture_object *texObj,
98 dri_bo *sdc_bo,
99 struct brw_sampler_state *sampler)
100 {
101 _mesa_memset(sampler, 0, sizeof(*sampler));
102
103 switch (texObj->MinFilter) {
104 case GL_NEAREST:
105 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
106 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
107 break;
108 case GL_LINEAR:
109 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
110 sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
111 break;
112 case GL_NEAREST_MIPMAP_NEAREST:
113 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
114 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
115 break;
116 case GL_LINEAR_MIPMAP_NEAREST:
117 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
118 sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
119 break;
120 case GL_NEAREST_MIPMAP_LINEAR:
121 sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
122 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
123 break;
124 case GL_LINEAR_MIPMAP_LINEAR:
125 sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
126 sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
127 break;
128 default:
129 break;
130 }
131
132 /* Set Anisotropy:
133 */
134 if ( texObj->MaxAnisotropy > 1.0 ) {
135 sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
136 sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
137
138 if (texObj->MaxAnisotropy > 2.0) {
139 sampler->ss3.max_aniso = MAX2((texObj->MaxAnisotropy - 2) / 2,
140 BRW_ANISORATIO_16);
141 }
142 }
143 else {
144 switch (texObj->MagFilter) {
145 case GL_NEAREST:
146 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
147 break;
148 case GL_LINEAR:
149 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
150 break;
151 default:
152 break;
153 }
154 }
155
156 sampler->ss1.r_wrap_mode = translate_wrap_mode(texObj->WrapR);
157 sampler->ss1.s_wrap_mode = translate_wrap_mode(texObj->WrapS);
158 sampler->ss1.t_wrap_mode = translate_wrap_mode(texObj->WrapT);
159
160 /* Fulsim complains if I don't do this. Hardware doesn't mind:
161 */
162 #if 0
163 if (texObj->Target == GL_TEXTURE_CUBE_MAP_ARB) {
164 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
165 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
166 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
167 }
168 #endif
169
170 /* Set shadow function:
171 */
172 if (texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
173 /* Shadowing is "enabled" by emitting a particular sampler
174 * message (sample_c). So need to recompile WM program when
175 * shadow comparison is enabled on each/any texture unit.
176 */
177 sampler->ss0.shadow_function = intel_translate_shadow_compare_func(texObj->CompareFunc);
178 }
179
180 /* Set LOD bias:
181 */
182 sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias + texObj->LodBias, -16, 15), 6);
183
184 sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
185 sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
186
187 /* Set BaseMipLevel, MaxLOD, MinLOD:
188 *
189 * XXX: I don't think that using firstLevel, lastLevel works,
190 * because we always setup the surface state as if firstLevel ==
191 * level zero. Probably have to subtract firstLevel from each of
192 * these:
193 */
194 sampler->ss0.base_level = U_FIXED(0, 1);
195
196 sampler->ss1.max_lod = U_FIXED(MIN2(MAX2(texObj->MaxLod, 0), 13), 6);
197 sampler->ss1.min_lod = U_FIXED(MIN2(MAX2(texObj->MinLod, 0), 13), 6);
198
199 sampler->ss2.default_color_pointer = sdc_bo->offset >> 5; /* reloc */
200 }
201
202
203
204 /* All samplers must be uploaded in a single contiguous array, which
205 * complicates various things. However, this is still too confusing -
206 * FIXME: simplify all the different new texture state flags.
207 */
208 static void upload_wm_samplers( struct brw_context *brw )
209 {
210 GLuint unit;
211 GLuint sampler_count = 0;
212 dri_bo *reloc_bufs[BRW_MAX_TEX_UNIT];
213
214 /* _NEW_TEXTURE */
215 for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
216 if (brw->attribs.Texture->Unit[unit]._ReallyEnabled) {
217 struct gl_texture_unit *texUnit = &brw->attribs.Texture->Unit[unit];
218 struct gl_texture_object *texObj = texUnit->_Current;
219
220 dri_bo_unreference(brw->wm.sdc_bo[unit]);
221 brw->wm.sdc_bo[unit] = upload_default_color(brw, texObj->BorderColor);
222
223 brw_update_sampler_state(texUnit,
224 texObj,
225 brw->wm.sdc_bo[unit],
226 &brw->wm.sampler[unit]);
227
228 sampler_count = unit + 1;
229 } else {
230 dri_bo_unreference(brw->wm.sdc_bo[unit]);
231 brw->wm.sdc_bo[unit] = NULL;
232 }
233 reloc_bufs[unit] = brw->wm.sdc_bo[unit];
234 }
235
236 if (brw->wm.sampler_count != sampler_count) {
237 brw->wm.sampler_count = sampler_count;
238 brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
239 }
240
241 dri_bo_unreference(brw->wm.sampler_bo);
242 if (brw->wm.sampler_count) {
243 brw->wm.sampler_bo =
244 brw_cache_data_sz(&brw->cache, BRW_SAMPLER,
245 brw->wm.sampler,
246 sizeof(struct brw_sampler_state) *
247 brw->wm.sampler_count,
248 reloc_bufs, BRW_MAX_TEX_UNIT);
249 } else {
250 brw->wm.sampler_bo = NULL;
251 }
252 }
253
254 static void emit_reloc_wm_samplers(struct brw_context *brw)
255 {
256 GLuint unit;
257
258 if (brw->wm.sampler_count == 0)
259 return;
260
261 /* Emit SDC relocations */
262 for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
263 if (!brw->attribs.Texture->Unit[unit]._ReallyEnabled)
264 continue;
265
266 dri_emit_reloc(brw->wm.sampler_bo,
267 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
268 0,
269 unit * sizeof(struct brw_sampler_state) +
270 offsetof(struct brw_sampler_state, ss2),
271 brw->wm.sdc_bo[unit]);
272 }
273 }
274
275 const struct brw_tracked_state brw_wm_samplers = {
276 .dirty = {
277 .mesa = _NEW_TEXTURE,
278 .brw = 0,
279 .cache = 0
280 },
281 .update = upload_wm_samplers,
282 .emit_reloc = emit_reloc_wm_samplers,
283 };
284
285