fix get_result_vector() since fp output slots are not fixed anymore
[mesa.git] / src / mesa / pipe / i915simple / 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 "mtypes.h"
29 //#include "enums.h"
30 //#include "texformat.h"
31 //#include "macros.h"
32 //#include "dri_bufmgr.h"
33
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_util.h"
38
39 #include "i915_state_inlines.h"
40 #include "i915_context.h"
41 #include "i915_reg.h"
42 #include "i915_state.h"
43 //#include "i915_cache.h"
44
45 static uint
46 bitcount(uint k)
47 {
48 uint count = 0;
49 while (k) {
50 if (k & 1)
51 count++;
52 k = k >> 1;
53 }
54 return count;
55 }
56
57
58 static boolean
59 is_power_of_two_texture(const struct pipe_mipmap_tree *mt)
60 {
61 if (bitcount(mt->width0) == 1 &&
62 bitcount(mt->height0) == 1 &&
63 bitcount(mt->depth0) == 1) {
64 return 1;
65 }
66 else
67 return 0;
68 }
69
70
71 /**
72 * Compute i915 texture sampling state.
73 *
74 * Recalculate all state from scratch. Perhaps not the most
75 * efficient, but this has gotten complex enough that we need
76 * something which is understandable and reliable.
77 * \param state returns the 3 words of compute state
78 */
79 static void update_sampler(struct i915_context *i915,
80 uint unit,
81 const struct i915_sampler_state *sampler,
82 const struct pipe_mipmap_tree *mt,
83 unsigned state[3] )
84 {
85 /* Need to do this after updating the maps, which call the
86 * intel_finalize_mipmap_tree and hence can update firstLevel:
87 */
88 state[0] = sampler->state[0];
89 state[1] = sampler->state[1];
90 state[2] = sampler->state[2];
91
92 if (mt->format == PIPE_FORMAT_YCBCR ||
93 mt->format == PIPE_FORMAT_YCBCR_REV)
94 state[0] |= SS2_COLORSPACE_CONVERSION;
95
96 /* 3D textures don't seem to respect the border color.
97 * Fallback if there's ever a danger that they might refer to
98 * it.
99 *
100 * Effectively this means fallback on 3D clamp or
101 * clamp_to_border.
102 *
103 * XXX: Check if this is true on i945.
104 * XXX: Check if this bug got fixed in release silicon.
105 */
106 #if 0
107 {
108 const unsigned ws = sampler->templ->wrap_s;
109 const unsigned wt = sampler->templ->wrap_t;
110 const unsigned wr = sampler->templ->wrap_r;
111 if (mt->target == PIPE_TEXTURE_3D &&
112 (sampler->templ->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
113 sampler->templ->mag_img_filter != PIPE_TEX_FILTER_NEAREST) &&
114 (ws == PIPE_TEX_WRAP_CLAMP ||
115 wt == PIPE_TEX_WRAP_CLAMP ||
116 wr == PIPE_TEX_WRAP_CLAMP ||
117 ws == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
118 wt == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
119 wr == PIPE_TEX_WRAP_CLAMP_TO_BORDER)) {
120 if (i915->strict_conformance) {
121 assert(0);
122 /* sampler->fallback = true; */
123 /* TODO */
124 }
125 }
126 }
127 #endif
128
129 state[1] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
130
131 if (is_power_of_two_texture(mt)) {
132 state[1] |= SS3_NORMALIZED_COORDS;
133 }
134 }
135
136 void i915_update_samplers( struct i915_context *i915 )
137 {
138 uint unit;
139
140 i915->current.sampler_enable_nr = 0;
141 i915->current.sampler_enable_flags = 0x0;
142
143 for (unit = 0; unit < I915_TEX_UNITS; unit++) {
144 /* determine unit enable/disable by looking for a bound mipmap tree */
145 /* could also examine the fragment program? */
146 if (i915->texture[unit]) {
147 update_sampler( i915,
148 unit,
149 i915->sampler[unit], /* sampler state */
150 i915->texture[unit], /* mipmap tree */
151 i915->current.sampler[unit] /* the result */
152 );
153
154 i915->current.sampler_enable_nr++;
155 i915->current.sampler_enable_flags |= (1 << unit);
156 }
157 }
158
159 i915->hardware_dirty |= I915_HW_SAMPLER;
160 }
161
162
163
164
165 static uint
166 translate_texture_format(uint pipeFormat)
167 {
168 switch (pipeFormat) {
169 case PIPE_FORMAT_U_L8:
170 return MAPSURF_8BIT | MT_8BIT_L8;
171 case PIPE_FORMAT_U_I8:
172 return MAPSURF_8BIT | MT_8BIT_I8;
173 case PIPE_FORMAT_U_A8:
174 return MAPSURF_8BIT | MT_8BIT_A8;
175 case PIPE_FORMAT_U_A8_L8:
176 return MAPSURF_16BIT | MT_16BIT_AY88;
177 case PIPE_FORMAT_U_R5_G6_B5:
178 return MAPSURF_16BIT | MT_16BIT_RGB565;
179 case PIPE_FORMAT_U_A1_R5_G5_B5:
180 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
181 case PIPE_FORMAT_U_A4_R4_G4_B4:
182 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
183 case PIPE_FORMAT_U_A8_R8_G8_B8:
184 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
185 case PIPE_FORMAT_YCBCR_REV:
186 return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
187 case PIPE_FORMAT_YCBCR:
188 return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
189 #if 0
190 case PIPE_FORMAT_RGB_FXT1:
191 case PIPE_FORMAT_RGBA_FXT1:
192 return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
193 #endif
194 case PIPE_FORMAT_U_Z16:
195 return (MAPSURF_16BIT | MT_16BIT_L16);
196 #if 0
197 case PIPE_FORMAT_RGBA_DXT1:
198 case PIPE_FORMAT_RGB_DXT1:
199 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
200 case PIPE_FORMAT_RGBA_DXT3:
201 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
202 case PIPE_FORMAT_RGBA_DXT5:
203 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
204 #endif
205 case PIPE_FORMAT_S8_Z24:
206 return (MAPSURF_32BIT | MT_32BIT_xL824);
207 default:
208 fprintf(stderr, "i915: translate_texture_format() bad image format %x\n",
209 pipeFormat);
210 assert(0);
211 return 0;
212 }
213 }
214
215
216 #define I915_TEXREG_MS3 1
217 #define I915_TEXREG_MS4 2
218
219
220 static void
221 i915_update_texture(struct i915_context *i915, uint unit,
222 uint state[6])
223 {
224 const struct pipe_mipmap_tree *mt = i915->texture[unit];
225 uint format, pitch;
226 const uint width = mt->width0, height = mt->height0, depth = mt->depth0;
227 const uint num_levels = mt->last_level - mt->first_level;
228
229 assert(mt);
230 assert(width);
231 assert(height);
232 assert(depth);
233
234 #if 0
235 if (i915->state.tex_buffer[unit] != NULL) {
236 driBOUnReference(i915->state.tex_buffer[unit]);
237 i915->state.tex_buffer[unit] = NULL;
238 }
239 #endif
240
241
242 {
243 /*struct pipe_buffer_handle *p =*/ driBOReference(mt->region->buffer);
244 }
245
246 #if 0
247 i915->state.tex_buffer[unit] = driBOReference(intelObj->mt->region->
248 buffer);
249 i915->state.tex_offset[unit] = intel_miptree_image_offset(intelObj->mt,
250 0, intelObj->
251 firstLevel);
252 #endif
253
254 format = translate_texture_format(mt->format);
255 pitch = mt->pitch * mt->cpp;
256
257 assert(format);
258 assert(pitch);
259
260 /*
261 printf("texture format = 0x%x\n", format);
262 */
263
264 /* MS3 state */
265 state[0] =
266 (((height - 1) << MS3_HEIGHT_SHIFT)
267 | ((width - 1) << MS3_WIDTH_SHIFT)
268 | format
269 | MS3_USE_FENCE_REGS);
270
271 /* MS4 state */
272 state[1] =
273 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT)
274 | MS4_CUBE_FACE_ENA_MASK
275 | ((num_levels * 4) << MS4_MAX_LOD_SHIFT)
276 | ((depth - 1) << MS4_VOLUME_DEPTH_SHIFT));
277 }
278
279
280
281 void
282 i915_update_textures(struct i915_context *i915)
283 {
284 uint unit;
285
286 for (unit = 0; unit < I915_TEX_UNITS; unit++) {
287 /* determine unit enable/disable by looking for a bound mipmap tree */
288 /* could also examine the fragment program? */
289 if (i915->texture[unit]) {
290 i915_update_texture(i915, unit, i915->current.texbuffer[unit]);
291 }
292 }
293
294 i915->hardware_dirty |= I915_HW_MAP;
295 }