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