Merge branch '7.8' into master
[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
36
37 /*
38 * A note about min_lod & max_lod.
39 *
40 * There is a circular dependancy between the sampler state
41 * and the map state to be submitted to hw.
42 *
43 * Two condition must be meet:
44 * min_lod =< max_lod == true
45 * max_lod =< last_level == true
46 *
47 *
48 * This is all fine and dandy if it where for the fact that max_lod
49 * is set on the map state instead of the sampler state. That is
50 * the max_lod we submit on map is:
51 * max_lod = MIN2(last_level, max_lod);
52 *
53 * So we need to update the map state when we change samplers and
54 * we need to be change the sampler state when map state is changed.
55 * The first part is done by calling i915_update_texture in
56 * i915_update_samplers and the second part is done else where in
57 * code tracking the state changes.
58 */
59
60 static void
61 i915_update_texture(struct i915_context *i915,
62 uint unit,
63 const struct i915_texture *tex,
64 const struct i915_sampler_state *sampler,
65 uint state[6]);
66 /**
67 * Compute i915 texture sampling state.
68 *
69 * Recalculate all state from scratch. Perhaps not the most
70 * efficient, but this has gotten complex enough that we need
71 * something which is understandable and reliable.
72 * \param state returns the 3 words of compute state
73 */
74 static void update_sampler(struct i915_context *i915,
75 uint unit,
76 const struct i915_sampler_state *sampler,
77 const struct i915_texture *tex,
78 unsigned state[3] )
79 {
80 const struct pipe_texture *pt = &tex->base;
81 unsigned minlod, lastlod;
82
83 /* Need to do this after updating the maps, which call the
84 * intel_finalize_mipmap_tree and hence can update firstLevel:
85 */
86 state[0] = sampler->state[0];
87 state[1] = sampler->state[1];
88 state[2] = sampler->state[2];
89
90 if (pt->format == PIPE_FORMAT_UYVY ||
91 pt->format == PIPE_FORMAT_YUYV)
92 state[0] |= SS2_COLORSPACE_CONVERSION;
93
94 /* 3D textures don't seem to respect the border color.
95 * Fallback if there's ever a danger that they might refer to
96 * it.
97 *
98 * Effectively this means fallback on 3D clamp or
99 * clamp_to_border.
100 *
101 * XXX: Check if this is true on i945.
102 * XXX: Check if this bug got fixed in release silicon.
103 */
104 #if 0
105 {
106 const unsigned ws = sampler->templ->wrap_s;
107 const unsigned wt = sampler->templ->wrap_t;
108 const unsigned wr = sampler->templ->wrap_r;
109 if (pt->target == PIPE_TEXTURE_3D &&
110 (sampler->templ->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
111 sampler->templ->mag_img_filter != PIPE_TEX_FILTER_NEAREST) &&
112 (ws == PIPE_TEX_WRAP_CLAMP ||
113 wt == PIPE_TEX_WRAP_CLAMP ||
114 wr == PIPE_TEX_WRAP_CLAMP ||
115 ws == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
116 wt == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
117 wr == PIPE_TEX_WRAP_CLAMP_TO_BORDER)) {
118 if (i915->conformance_mode > 0) {
119 assert(0);
120 /* sampler->fallback = true; */
121 /* TODO */
122 }
123 }
124 }
125 #endif
126
127 /* See note at the top of file */
128 minlod = sampler->minlod;
129 lastlod = pt->last_level << 4;
130
131 if (lastlod < minlod) {
132 minlod = lastlod;
133 }
134
135 state[1] |= (sampler->minlod << SS3_MIN_LOD_SHIFT);
136 state[1] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
137 }
138
139
140 void i915_update_samplers( struct i915_context *i915 )
141 {
142 uint unit;
143
144 i915->current.sampler_enable_nr = 0;
145 i915->current.sampler_enable_flags = 0x0;
146
147 for (unit = 0; unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
148 unit++) {
149 /* determine unit enable/disable by looking for a bound texture */
150 /* could also examine the fragment program? */
151 if (i915->fragment_sampler_views[unit]) {
152 struct i915_texture *texture = (struct i915_texture *)i915->fragment_sampler_views[unit]->texture;
153
154 update_sampler( i915,
155 unit,
156 i915->sampler[unit], /* sampler state */
157 texture, /* texture */
158 i915->current.sampler[unit] /* the result */
159 );
160 i915_update_texture( i915,
161 unit,
162 texture, /* texture */
163 i915->sampler[unit], /* sampler state */
164 i915->current.texbuffer[unit] );
165
166 i915->current.sampler_enable_nr++;
167 i915->current.sampler_enable_flags |= (1 << unit);
168 }
169 }
170
171 i915->hardware_dirty |= I915_HW_SAMPLER | I915_HW_MAP;
172 }
173
174
175 static uint
176 translate_texture_format(enum pipe_format pipeFormat)
177 {
178 switch (pipeFormat) {
179 case PIPE_FORMAT_L8_UNORM:
180 return MAPSURF_8BIT | MT_8BIT_L8;
181 case PIPE_FORMAT_I8_UNORM:
182 return MAPSURF_8BIT | MT_8BIT_I8;
183 case PIPE_FORMAT_A8_UNORM:
184 return MAPSURF_8BIT | MT_8BIT_A8;
185 case PIPE_FORMAT_L8A8_UNORM:
186 return MAPSURF_16BIT | MT_16BIT_AY88;
187 case PIPE_FORMAT_B5G6R5_UNORM:
188 return MAPSURF_16BIT | MT_16BIT_RGB565;
189 case PIPE_FORMAT_B5G5R5A1_UNORM:
190 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
191 case PIPE_FORMAT_B4G4R4A4_UNORM:
192 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
193 case PIPE_FORMAT_B8G8R8A8_UNORM:
194 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
195 case PIPE_FORMAT_YUYV:
196 return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
197 case PIPE_FORMAT_UYVY:
198 return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
199 #if 0
200 case PIPE_FORMAT_RGB_FXT1:
201 case PIPE_FORMAT_RGBA_FXT1:
202 return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
203 #endif
204 case PIPE_FORMAT_Z16_UNORM:
205 return (MAPSURF_16BIT | MT_16BIT_L16);
206 #if 0
207 case PIPE_FORMAT_RGBA_DXT1:
208 case PIPE_FORMAT_RGB_DXT1:
209 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
210 case PIPE_FORMAT_RGBA_DXT3:
211 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
212 case PIPE_FORMAT_RGBA_DXT5:
213 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
214 #endif
215 case PIPE_FORMAT_Z24S8_UNORM:
216 return (MAPSURF_32BIT | MT_32BIT_xI824);
217 default:
218 debug_printf("i915: translate_texture_format() bad image format %x\n",
219 pipeFormat);
220 assert(0);
221 return 0;
222 }
223 }
224
225
226 static void
227 i915_update_texture(struct i915_context *i915,
228 uint unit,
229 const struct i915_texture *tex,
230 const struct i915_sampler_state *sampler,
231 uint state[6])
232 {
233 const struct pipe_texture *pt = &tex->base;
234 uint format, pitch;
235 const uint width = pt->width0, height = pt->height0, depth = pt->depth0;
236 const uint num_levels = pt->last_level;
237 unsigned max_lod = num_levels * 4;
238 unsigned tiled = MS3_USE_FENCE_REGS;
239
240 assert(tex);
241 assert(width);
242 assert(height);
243 assert(depth);
244
245 format = translate_texture_format(pt->format);
246 pitch = tex->stride;
247
248 assert(format);
249 assert(pitch);
250
251 if (tex->sw_tiled) {
252 assert(!((pitch - 1) & pitch));
253 tiled = MS3_TILED_SURFACE;
254 }
255
256 /* MS3 state */
257 state[0] =
258 (((height - 1) << MS3_HEIGHT_SHIFT)
259 | ((width - 1) << MS3_WIDTH_SHIFT)
260 | format
261 | tiled);
262
263 /*
264 * XXX When min_filter != mag_filter and there's just one mipmap level,
265 * set max_lod = 1 to make sure i915 chooses between min/mag filtering.
266 */
267
268 /* See note at the top of file */
269 if (max_lod > (sampler->maxlod >> 2))
270 max_lod = sampler->maxlod >> 2;
271
272 /* MS4 state */
273 state[1] =
274 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT)
275 | MS4_CUBE_FACE_ENA_MASK
276 | ((max_lod) << MS4_MAX_LOD_SHIFT)
277 | ((depth - 1) << MS4_VOLUME_DEPTH_SHIFT));
278 }
279
280
281 void
282 i915_update_textures(struct i915_context *i915)
283 {
284 uint unit;
285
286 for (unit = 0; unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
287 unit++) {
288 /* determine unit enable/disable by looking for a bound texture */
289 /* could also examine the fragment program? */
290 if (i915->fragment_sampler_views[unit]) {
291 struct i915_texture *texture = (struct i915_texture *)i915->fragment_sampler_views[unit]->texture;
292
293 i915_update_texture( i915,
294 unit,
295 texture, /* texture */
296 i915->sampler[unit], /* sampler state */
297 i915->current.texbuffer[unit] );
298 }
299 }
300
301 i915->hardware_dirty |= I915_HW_MAP;
302 }