• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2003 VMware, Inc.
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 VMWARE 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_context.h"
32 #include "i915_reg.h"
33 #include "i915_resource.h"
34 #include "i915_state.h"
35 #include "i915_state_inlines.h"
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 were 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 change the sampler state when map state is changed.
55  * The first part is done by calling update_texture in update_samplers
56  * and the second part is done else where in code tracking the state
57  * changes.
58  */
59 
60 /***********************************************************************
61  * Samplers
62  */
63 
64 /**
65  * Compute i915 texture sampling state.
66  *
67  * Recalculate all state from scratch.  Perhaps not the most
68  * efficient, but this has gotten complex enough that we need
69  * something which is understandable and reliable.
70  * \param state  returns the 3 words of compute state
71  */
72 static void
update_sampler(struct i915_context * i915,uint32_t unit,const struct i915_sampler_state * sampler,const struct i915_texture * tex,const struct pipe_sampler_view * view,unsigned state[3])73 update_sampler(struct i915_context *i915, uint32_t unit,
74                const struct i915_sampler_state *sampler,
75                const struct i915_texture *tex,
76                const struct pipe_sampler_view *view, unsigned state[3])
77 {
78    const struct pipe_resource *pt = &tex->b;
79    unsigned minlod;
80 
81    state[0] = sampler->state[0];
82    state[1] = sampler->state[1];
83    state[2] = sampler->state[2];
84 
85    if (pt->format == PIPE_FORMAT_UYVY || pt->format == PIPE_FORMAT_YUYV)
86       state[0] |= SS2_COLORSPACE_CONVERSION;
87 
88    if (util_format_is_srgb(pt->format)) {
89       state[0] |= SS2_REVERSE_GAMMA_ENABLE;
90    }
91 
92    /* There is no HW support for 1D textures, so we just make them 2D textures
93     * with h=1, but that means we need to make the Y coordinate not contribute
94     * to bringing any border color in.  Clearing it sets it to WRAP.
95     */
96    if (pt->target == PIPE_TEXTURE_1D) {
97       state[1] &= ~SS3_TCY_ADDR_MODE_MASK;
98    }
99 
100    /* The GLES2 spec says textures are incomplete (return 0,0,0,1) if:
101     *
102     * "A cube map sampler is called, any of the corresponding texture images are
103     * non-power-of-two images, and either the texture wrap mode is not
104     * CLAMP_TO_EDGE, or the minification filter is neither NEAREST nor LINEAR."
105     *
106     * while the i915 spec says:
107     *
108     * "When using cube map texture coordinates, only TEXCOORDMODE_CLAMP and *
109     *  TEXCOORDMODE_CUBE settings are valid, and each TC component must have the
110     *  same Address Control mode. TEXCOORDMODE_CUBE is not valid unless the
111     *  width and height of the cube map are power-of-2."
112     *
113     * We don't expose support for the seamless cube map extension, so always use
114     * edge clamping.
115     */
116    if (pt->target == PIPE_TEXTURE_CUBE) {
117       state[1] &= ~(SS3_TCX_ADDR_MODE_MASK | SS3_TCY_ADDR_MODE_MASK |
118                     SS3_TCZ_ADDR_MODE_MASK);
119       state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCX_ADDR_MODE_SHIFT);
120       state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCY_ADDR_MODE_SHIFT);
121       state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCZ_ADDR_MODE_SHIFT);
122    }
123 
124    /* 3D textures don't seem to respect the border color.
125     * Fallback if there's ever a danger that they might refer to
126     * it.
127     *
128     * Effectively this means fallback on 3D clamp or
129     * clamp_to_border.
130     *
131     * XXX: Check if this is true on i945.
132     * XXX: Check if this bug got fixed in release silicon.
133     */
134 #if 0
135    {
136       const unsigned ws = sampler->templ->wrap_s;
137       const unsigned wt = sampler->templ->wrap_t;
138       const unsigned wr = sampler->templ->wrap_r;
139       if (pt->target == PIPE_TEXTURE_3D &&
140           (sampler->templ->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
141            sampler->templ->mag_img_filter != PIPE_TEX_FILTER_NEAREST) &&
142           (ws == PIPE_TEX_WRAP_CLAMP ||
143            wt == PIPE_TEX_WRAP_CLAMP ||
144            wr == PIPE_TEX_WRAP_CLAMP ||
145            ws == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
146            wt == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
147            wr == PIPE_TEX_WRAP_CLAMP_TO_BORDER)) {
148          if (i915->conformance_mode > 0) {
149             assert(0);
150             /*             sampler->fallback = true; */
151             /* TODO */
152          }
153       }
154    }
155 #endif
156 
157    if (sampler->templ.min_mip_filter == PIPE_TEX_MIPFILTER_NONE)
158       minlod = view->u.tex.first_level * 16;
159    else
160       minlod = MIN2(sampler->minlod + view->u.tex.first_level * 16, view->u.tex.last_level * 16);
161 
162    state[1] |= (minlod << SS3_MIN_LOD_SHIFT);
163    state[1] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
164 }
165 
166 /***********************************************************************
167  * Sampler views
168  */
169 
170 static uint32_t
translate_texture_format(enum pipe_format pipeFormat,const struct pipe_sampler_view * view)171 translate_texture_format(enum pipe_format pipeFormat,
172                          const struct pipe_sampler_view *view)
173 {
174    if ((view->swizzle_r != PIPE_SWIZZLE_X ||
175         view->swizzle_g != PIPE_SWIZZLE_Y ||
176         view->swizzle_b != PIPE_SWIZZLE_Z ||
177         view->swizzle_a != PIPE_SWIZZLE_W) &&
178        pipeFormat != PIPE_FORMAT_Z24_UNORM_S8_UINT &&
179        pipeFormat != PIPE_FORMAT_Z24X8_UNORM)
180       debug_printf("i915: unsupported texture swizzle for format %d\n",
181                    pipeFormat);
182 
183    switch (pipeFormat) {
184    case PIPE_FORMAT_L8_UNORM:
185       return MAPSURF_8BIT | MT_8BIT_L8;
186    case PIPE_FORMAT_I8_UNORM:
187       return MAPSURF_8BIT | MT_8BIT_I8;
188    case PIPE_FORMAT_A8_UNORM:
189       return MAPSURF_8BIT | MT_8BIT_A8;
190    case PIPE_FORMAT_L8A8_UNORM:
191       return MAPSURF_16BIT | MT_16BIT_AY88;
192    case PIPE_FORMAT_B5G6R5_UNORM:
193       return MAPSURF_16BIT | MT_16BIT_RGB565;
194    case PIPE_FORMAT_B5G5R5A1_UNORM:
195       return MAPSURF_16BIT | MT_16BIT_ARGB1555;
196    case PIPE_FORMAT_B4G4R4A4_UNORM:
197       return MAPSURF_16BIT | MT_16BIT_ARGB4444;
198    case PIPE_FORMAT_B10G10R10A2_UNORM:
199       return MAPSURF_32BIT | MT_32BIT_ARGB2101010;
200    case PIPE_FORMAT_B8G8R8A8_UNORM:
201    case PIPE_FORMAT_B8G8R8A8_SRGB:
202       return MAPSURF_32BIT | MT_32BIT_ARGB8888;
203    case PIPE_FORMAT_B8G8R8X8_UNORM:
204       return MAPSURF_32BIT | MT_32BIT_XRGB8888;
205    case PIPE_FORMAT_R8G8B8A8_UNORM:
206       return MAPSURF_32BIT | MT_32BIT_ABGR8888;
207    case PIPE_FORMAT_R8G8B8X8_UNORM:
208       return MAPSURF_32BIT | MT_32BIT_XBGR8888;
209    case PIPE_FORMAT_YUYV:
210       return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
211    case PIPE_FORMAT_UYVY:
212       return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
213    case PIPE_FORMAT_FXT1_RGB:
214    case PIPE_FORMAT_FXT1_RGBA:
215       return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
216    case PIPE_FORMAT_Z16_UNORM:
217       return (MAPSURF_16BIT | MT_16BIT_L16);
218    case PIPE_FORMAT_DXT1_RGB:
219    case PIPE_FORMAT_DXT1_SRGB:
220       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1_RGB);
221    case PIPE_FORMAT_DXT1_RGBA:
222    case PIPE_FORMAT_DXT1_SRGBA:
223       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
224    case PIPE_FORMAT_DXT3_RGBA:
225    case PIPE_FORMAT_DXT3_SRGBA:
226       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
227    case PIPE_FORMAT_DXT5_RGBA:
228    case PIPE_FORMAT_DXT5_SRGBA:
229       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
230    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
231    case PIPE_FORMAT_Z24X8_UNORM: {
232       if (view->swizzle_r == PIPE_SWIZZLE_X &&
233           view->swizzle_g == PIPE_SWIZZLE_X &&
234           view->swizzle_b == PIPE_SWIZZLE_X &&
235           view->swizzle_a == PIPE_SWIZZLE_1)
236          return (MAPSURF_32BIT | MT_32BIT_xL824);
237       if (view->swizzle_r == PIPE_SWIZZLE_X &&
238           view->swizzle_g == PIPE_SWIZZLE_X &&
239           view->swizzle_b == PIPE_SWIZZLE_X &&
240           view->swizzle_a == PIPE_SWIZZLE_X)
241          return (MAPSURF_32BIT | MT_32BIT_xI824);
242       if (view->swizzle_r == PIPE_SWIZZLE_0 &&
243           view->swizzle_g == PIPE_SWIZZLE_0 &&
244           view->swizzle_b == PIPE_SWIZZLE_0 &&
245           view->swizzle_a == PIPE_SWIZZLE_X)
246          return (MAPSURF_32BIT | MT_32BIT_xA824);
247       debug_printf("i915: unsupported depth swizzle %d %d %d %d\n",
248                    view->swizzle_r, view->swizzle_g, view->swizzle_b,
249                    view->swizzle_a);
250       return (MAPSURF_32BIT | MT_32BIT_xL824);
251    }
252    default:
253       debug_printf("i915: translate_texture_format() bad image format %x\n",
254                    pipeFormat);
255       assert(0);
256       return 0;
257    }
258 }
259 
260 static inline uint32_t
ms3_tiling_bits(enum i915_winsys_buffer_tile tiling)261 ms3_tiling_bits(enum i915_winsys_buffer_tile tiling)
262 {
263    uint32_t tiling_bits = 0;
264 
265    switch (tiling) {
266    case I915_TILE_Y:
267       tiling_bits |= MS3_TILE_WALK_Y;
268       FALLTHROUGH;
269    case I915_TILE_X:
270       tiling_bits |= MS3_TILED_SURFACE;
271       FALLTHROUGH;
272    case I915_TILE_NONE:
273       break;
274    }
275 
276    return tiling_bits;
277 }
278 
279 static void
update_map(struct i915_context * i915,uint32_t unit,const struct i915_texture * tex,const struct i915_sampler_state * sampler,const struct pipe_sampler_view * view,uint32_t state[3])280 update_map(struct i915_context *i915, uint32_t unit,
281            const struct i915_texture *tex,
282            const struct i915_sampler_state *sampler,
283            const struct pipe_sampler_view *view, uint32_t state[3])
284 {
285    const struct pipe_resource *pt = &tex->b;
286    uint32_t width = pt->width0, height = pt->height0, depth = pt->depth0;
287    unsigned maxlod;
288    uint32_t format, pitch;
289 
290    assert(tex);
291    assert(width);
292    assert(height);
293    assert(depth);
294 
295    format = translate_texture_format(pt->format, view);
296    pitch = tex->stride;
297 
298    assert(format);
299    assert(pitch);
300 
301    /* MS3 state */
302    state[0] =
303       (((height - 1) << MS3_HEIGHT_SHIFT) | ((width - 1) << MS3_WIDTH_SHIFT) |
304        format | ms3_tiling_bits(tex->tiling));
305 
306    if (sampler->templ.min_mip_filter == PIPE_TEX_MIPFILTER_NONE)
307       maxlod = view->u.tex.first_level * 4;
308    else
309       maxlod = MIN2((sampler->maxlod >> 2) + view->u.tex.first_level * 4, view->u.tex.last_level * 4);
310 
311    /*
312     * XXX When min_filter != mag_filter and there's just one mipmap level,
313     * set max_lod = 1 to make sure i915 chooses between min/mag filtering.
314     */
315    if (maxlod == 0)
316       maxlod = 1;
317 
318    /* MS4 state */
319    state[1] = ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) | MS4_CUBE_FACE_ENA_MASK |
320                ((maxlod) << MS4_MAX_LOD_SHIFT) |
321                ((depth - 1) << MS4_VOLUME_DEPTH_SHIFT));
322 
323    state[2] = 0;
324 }
325 
326 static void
update_samplers(struct i915_context * i915)327 update_samplers(struct i915_context *i915)
328 {
329    uint32_t unit;
330 
331    i915->current.sampler_enable_nr = 0;
332    i915->current.sampler_enable_flags = 0x0;
333 
334    for (unit = 0;
335         unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
336         unit++) {
337       /* determine unit enable/disable by looking for a bound texture */
338       /* could also examine the fragment program? */
339       if (i915->fragment_sampler_views[unit]) {
340          struct i915_texture *texture =
341             i915_texture(i915->fragment_sampler_views[unit]->texture);
342 
343          update_sampler(i915, unit,
344                         i915->fragment_sampler[unit],   /* sampler state */
345                         texture,                        /* texture */
346                         i915->fragment_sampler_views[unit], /* sampler view */
347                         i915->current.sampler[unit]);   /* the result */
348          update_map(i915, unit, texture,                /* texture */
349                     i915->fragment_sampler[unit],       /* sampler state */
350                     i915->fragment_sampler_views[unit], /* sampler view */
351                     i915->current.texbuffer[unit]);     /* the result */
352 
353          i915->current.sampler_enable_nr++;
354          i915->current.sampler_enable_flags |= (1 << unit);
355       }
356    }
357 
358    i915->hardware_dirty |= I915_HW_SAMPLER | I915_HW_MAP;
359 }
360 
361 struct i915_tracked_state i915_hw_samplers = {
362    "samplers", update_samplers, I915_NEW_SAMPLER | I915_NEW_SAMPLER_VIEW};
363