Merge remote-tracking branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / drivers / i915 / i915_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "pipe/p_context.h"
29 #include "pipe/p_state.h"
30
31 #include "i915_state_inlines.h"
32 #include "i915_context.h"
33 #include "i915_reg.h"
34 #include "i915_state.h"
35 #include "i915_resource.h"
36
37
38 /*
39 * A note about min_lod & max_lod.
40 *
41 * There is a circular dependancy between the sampler state
42 * and the map state to be submitted to hw.
43 *
44 * Two condition must be meet:
45 * min_lod =< max_lod == true
46 * max_lod =< last_level == true
47 *
48 *
49 * This is all fine and dandy if it where for the fact that max_lod
50 * is set on the map state instead of the sampler state. That is
51 * the max_lod we submit on map is:
52 * max_lod = MIN2(last_level, max_lod);
53 *
54 * So we need to update the map state when we change samplers and
55 * we need to be change the sampler state when map state is changed.
56 * The first part is done by calling update_texture in update_samplers
57 * and the second part is done else where in code tracking the state
58 * changes.
59 */
60
61 static void update_map(struct i915_context *i915,
62 uint unit,
63 const struct i915_texture *tex,
64 const struct i915_sampler_state *sampler,
65 uint state[2]);
66
67
68
69 /***********************************************************************
70 * Samplers
71 */
72
73 /**
74 * Compute i915 texture sampling state.
75 *
76 * Recalculate all state from scratch. Perhaps not the most
77 * efficient, but this has gotten complex enough that we need
78 * something which is understandable and reliable.
79 * \param state returns the 3 words of compute state
80 */
81 static void update_sampler(struct i915_context *i915,
82 uint unit,
83 const struct i915_sampler_state *sampler,
84 const struct i915_texture *tex,
85 unsigned state[3])
86 {
87 const struct pipe_resource *pt = &tex->b.b;
88 unsigned minlod, lastlod;
89
90 state[0] = sampler->state[0];
91 state[1] = sampler->state[1];
92 state[2] = sampler->state[2];
93
94 if (pt->format == PIPE_FORMAT_UYVY ||
95 pt->format == PIPE_FORMAT_YUYV)
96 state[0] |= SS2_COLORSPACE_CONVERSION;
97
98 /* 3D textures don't seem to respect the border color.
99 * Fallback if there's ever a danger that they might refer to
100 * it.
101 *
102 * Effectively this means fallback on 3D clamp or
103 * clamp_to_border.
104 *
105 * XXX: Check if this is true on i945.
106 * XXX: Check if this bug got fixed in release silicon.
107 */
108 #if 0
109 {
110 const unsigned ws = sampler->templ->wrap_s;
111 const unsigned wt = sampler->templ->wrap_t;
112 const unsigned wr = sampler->templ->wrap_r;
113 if (pt->target == PIPE_TEXTURE_3D &&
114 (sampler->templ->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
115 sampler->templ->mag_img_filter != PIPE_TEX_FILTER_NEAREST) &&
116 (ws == PIPE_TEX_WRAP_CLAMP ||
117 wt == PIPE_TEX_WRAP_CLAMP ||
118 wr == PIPE_TEX_WRAP_CLAMP ||
119 ws == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
120 wt == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
121 wr == PIPE_TEX_WRAP_CLAMP_TO_BORDER)) {
122 if (i915->conformance_mode > 0) {
123 assert(0);
124 /* sampler->fallback = true; */
125 /* TODO */
126 }
127 }
128 }
129 #endif
130
131 /* See note at the top of file */
132 minlod = sampler->minlod;
133 lastlod = pt->last_level << 4;
134
135 if (lastlod < minlod) {
136 minlod = lastlod;
137 }
138
139 state[1] |= (sampler->minlod << SS3_MIN_LOD_SHIFT);
140 state[1] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
141 }
142
143 static void update_samplers(struct i915_context *i915)
144 {
145 uint unit;
146
147 i915->current.sampler_enable_nr = 0;
148 i915->current.sampler_enable_flags = 0x0;
149
150 for (unit = 0; unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
151 unit++) {
152 /* determine unit enable/disable by looking for a bound texture */
153 /* could also examine the fragment program? */
154 if (i915->fragment_sampler_views[unit]) {
155 struct i915_texture *texture = i915_texture(i915->fragment_sampler_views[unit]->texture);
156
157 update_sampler(i915,
158 unit,
159 i915->sampler[unit], /* sampler state */
160 texture, /* texture */
161 i915->current.sampler[unit]); /* the result */
162 update_map(i915,
163 unit,
164 texture, /* texture */
165 i915->sampler[unit], /* sampler state */
166 i915->current.texbuffer[unit]); /* the result */
167
168 i915->current.sampler_enable_nr++;
169 i915->current.sampler_enable_flags |= (1 << unit);
170 }
171 }
172
173 i915->hardware_dirty |= I915_HW_SAMPLER | I915_HW_MAP;
174 }
175
176 struct i915_tracked_state i915_hw_samplers = {
177 "samplers",
178 update_samplers,
179 I915_NEW_SAMPLER | I915_NEW_SAMPLER_VIEW
180 };
181
182
183
184 /***********************************************************************
185 * Sampler views
186 */
187
188 static uint translate_texture_format(enum pipe_format pipeFormat)
189 {
190 switch (pipeFormat) {
191 case PIPE_FORMAT_L8_UNORM:
192 return MAPSURF_8BIT | MT_8BIT_L8;
193 case PIPE_FORMAT_I8_UNORM:
194 return MAPSURF_8BIT | MT_8BIT_I8;
195 case PIPE_FORMAT_A8_UNORM:
196 return MAPSURF_8BIT | MT_8BIT_A8;
197 case PIPE_FORMAT_L8A8_UNORM:
198 return MAPSURF_16BIT | MT_16BIT_AY88;
199 case PIPE_FORMAT_B5G6R5_UNORM:
200 return MAPSURF_16BIT | MT_16BIT_RGB565;
201 case PIPE_FORMAT_B5G5R5A1_UNORM:
202 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
203 case PIPE_FORMAT_B4G4R4A4_UNORM:
204 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
205 case PIPE_FORMAT_B8G8R8A8_UNORM:
206 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
207 case PIPE_FORMAT_B8G8R8X8_UNORM:
208 return MAPSURF_32BIT | MT_32BIT_XRGB8888;
209 case PIPE_FORMAT_R8G8B8A8_UNORM:
210 return MAPSURF_32BIT | MT_32BIT_ABGR8888;
211 #if 0
212 case PIPE_FORMAT_R8G8B8X8_UNORM:
213 return MAPSURF_32BIT | MT_32BIT_XBGR8888;
214 #endif
215 case PIPE_FORMAT_YUYV:
216 return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
217 case PIPE_FORMAT_UYVY:
218 return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
219 #if 0
220 case PIPE_FORMAT_RGB_FXT1:
221 case PIPE_FORMAT_RGBA_FXT1:
222 return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
223 #endif
224 case PIPE_FORMAT_Z16_UNORM:
225 return (MAPSURF_16BIT | MT_16BIT_L16);
226 case PIPE_FORMAT_DXT1_RGBA:
227 case PIPE_FORMAT_DXT1_RGB:
228 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
229 case PIPE_FORMAT_DXT3_RGBA:
230 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
231 case PIPE_FORMAT_DXT5_RGBA:
232 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
233 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
234 case PIPE_FORMAT_Z24X8_UNORM:
235 return (MAPSURF_32BIT | MT_32BIT_xI824);
236 default:
237 debug_printf("i915: translate_texture_format() bad image format %x\n",
238 pipeFormat);
239 assert(0);
240 return 0;
241 }
242 }
243
244 static inline uint32_t
245 ms3_tiling_bits(enum i915_winsys_buffer_tile tiling)
246 {
247 uint32_t tiling_bits = 0;
248
249 switch (tiling) {
250 case I915_TILE_Y:
251 tiling_bits |= MS3_TILE_WALK_Y;
252 case I915_TILE_X:
253 tiling_bits |= MS3_TILED_SURFACE;
254 case I915_TILE_NONE:
255 break;
256 }
257
258 return tiling_bits;
259 }
260
261 static void update_map(struct i915_context *i915,
262 uint unit,
263 const struct i915_texture *tex,
264 const struct i915_sampler_state *sampler,
265 uint state[2])
266 {
267 const struct pipe_resource *pt = &tex->b.b;
268 uint format, pitch;
269 const uint width = pt->width0, height = pt->height0, depth = pt->depth0;
270 const uint num_levels = pt->last_level;
271 unsigned max_lod = num_levels * 4;
272
273 assert(tex);
274 assert(width);
275 assert(height);
276 assert(depth);
277
278 format = translate_texture_format(pt->format);
279 pitch = tex->stride;
280
281 assert(format);
282 assert(pitch);
283
284 /* MS3 state */
285 state[0] =
286 (((height - 1) << MS3_HEIGHT_SHIFT)
287 | ((width - 1) << MS3_WIDTH_SHIFT)
288 | format
289 | ms3_tiling_bits(tex->tiling));
290
291 /*
292 * XXX When min_filter != mag_filter and there's just one mipmap level,
293 * set max_lod = 1 to make sure i915 chooses between min/mag filtering.
294 */
295
296 /* See note at the top of file */
297 if (max_lod > (sampler->maxlod >> 2))
298 max_lod = sampler->maxlod >> 2;
299
300 /* MS4 state */
301 state[1] =
302 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT)
303 | MS4_CUBE_FACE_ENA_MASK
304 | ((max_lod) << MS4_MAX_LOD_SHIFT)
305 | ((depth - 1) << MS4_VOLUME_DEPTH_SHIFT));
306 }
307
308 static void update_maps(struct i915_context *i915)
309 {
310 uint unit;
311
312 for (unit = 0; unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
313 unit++) {
314 /* determine unit enable/disable by looking for a bound texture */
315 /* could also examine the fragment program? */
316 if (i915->fragment_sampler_views[unit]) {
317 struct i915_texture *texture = i915_texture(i915->fragment_sampler_views[unit]->texture);
318
319 update_map(i915,
320 unit,
321 texture, /* texture */
322 i915->sampler[unit], /* sampler state */
323 i915->current.texbuffer[unit]);
324 }
325 }
326
327 i915->hardware_dirty |= I915_HW_MAP;
328 }
329
330 struct i915_tracked_state i915_hw_sampler_views = {
331 "sampler_views",
332 update_maps,
333 I915_NEW_SAMPLER_VIEW
334 };