OSDN Git Service

024224a79c1c4e1ca7165341986fc9aae86ccbf2
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i915 / i915_tex_layout.c
1 /**************************************************************************
2  * 
3  * Copyright 2006 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 /** @file i915_tex_layout.c
29  * Code to layout images in a mipmap tree for i830M-GM915 and G945 and beyond.
30  */
31
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex_layout.h"
34 #include "main/macros.h"
35 #include "intel_context.h"
36
37 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
38
39 static GLint initial_offsets[6][2] = {
40    [FACE_POS_X] = {0, 0},
41    [FACE_POS_Y] = {1, 0},
42    [FACE_POS_Z] = {1, 1},
43    [FACE_NEG_X] = {0, 2},
44    [FACE_NEG_Y] = {1, 2},
45    [FACE_NEG_Z] = {1, 3},
46 };
47
48
49 static GLint step_offsets[6][2] = {
50    [FACE_POS_X] = {0, 2},
51    [FACE_POS_Y] = {-1, 2},
52    [FACE_POS_Z] = {-1, 1},
53    [FACE_NEG_X] = {0, 2},
54    [FACE_NEG_Y] = {-1, 2},
55    [FACE_NEG_Z] = {-1, 1},
56 };
57
58
59 static GLint bottom_offsets[6] = {
60    [FACE_POS_X] = 16 + 0 * 8,
61    [FACE_POS_Y] = 16 + 1 * 8,
62    [FACE_POS_Z] = 16 + 2 * 8,
63    [FACE_NEG_X] = 16 + 3 * 8,
64    [FACE_NEG_Y] = 16 + 4 * 8,
65    [FACE_NEG_Z] = 16 + 5 * 8,
66 };
67
68
69 /**
70  * Cube texture map layout for i830M-GM915 and
71  * non-compressed cube texture map on GM945.
72  *
73  * Hardware layout looks like:
74  *
75  * +-------+-------+
76  * |       |       |
77  * |       |       |
78  * |       |       |
79  * |  +x   |  +y   |
80  * |       |       |
81  * |       |       |
82  * |       |       |
83  * |       |       |
84  * +---+---+-------+
85  * |   |   |       |
86  * | +x| +y|       |
87  * |   |   |       |
88  * |   |   |       |
89  * +-+-+---+  +z   |
90  * | | |   |       |
91  * +-+-+ +z|       |
92  *   | |   |       |
93  * +-+-+---+-------+
94  * |       |       |
95  * |       |       |
96  * |       |       |
97  * |  -x   |  -y   |
98  * |       |       |
99  * |       |       |
100  * |       |       |
101  * |       |       |
102  * +---+---+-------+
103  * |   |   |       |
104  * | -x| -y|       |
105  * |   |   |       |
106  * |   |   |       |
107  * +-+-+---+  -z   |
108  * | | |   |       |
109  * +-+-+ -z|       |
110  *   | |   |       |
111  *   +-+---+-------+
112  *
113  */
114 static void
115 i915_miptree_layout_cube(struct intel_mipmap_tree * mt)
116 {
117    const GLuint dim = mt->width0;
118    GLuint face;
119    GLuint lvlWidth = mt->width0, lvlHeight = mt->height0;
120    GLint level;
121
122    assert(lvlWidth == lvlHeight); /* cubemap images are square */
123
124    /* double pitch for cube layouts */
125    mt->total_width = dim * 2;
126    mt->total_height = dim * 4;
127
128    for (level = mt->first_level; level <= mt->last_level; level++) {
129       intel_miptree_set_level_info(mt, level,
130                                    0, 0,
131                                    lvlWidth, lvlHeight,
132                                    6);
133       lvlWidth /= 2;
134       lvlHeight /= 2;
135    }
136
137    for (face = 0; face < 6; face++) {
138       GLuint x = initial_offsets[face][0] * dim;
139       GLuint y = initial_offsets[face][1] * dim;
140       GLuint d = dim;
141
142       for (level = mt->first_level; level <= mt->last_level; level++) {
143          intel_miptree_set_image_offset(mt, level, face, x, y);
144
145          if (d == 0)
146             printf("cube mipmap %d/%d (%d..%d) is 0x0\n",
147                    face, level, mt->first_level, mt->last_level);
148
149          d >>= 1;
150          x += step_offsets[face][0] * d;
151          y += step_offsets[face][1] * d;
152       }
153    }
154 }
155
156 static void
157 i915_miptree_layout_3d(struct intel_mipmap_tree * mt)
158 {
159    GLuint width = mt->width0;
160    GLuint height = mt->height0;
161    GLuint depth = mt->depth0;
162    GLuint stack_height = 0;
163    GLint level;
164
165    /* Calculate the size of a single slice. */
166    mt->total_width = mt->width0;
167
168    /* XXX: hardware expects/requires 9 levels at minimum. */
169    for (level = mt->first_level; level <= MAX2(8, mt->last_level); level++) {
170       intel_miptree_set_level_info(mt, level, 0, mt->total_height,
171                                    width, height, depth);
172
173       stack_height += MAX2(2, height);
174
175       width = minify(width);
176       height = minify(height);
177       depth = minify(depth);
178    }
179
180    /* Fixup depth image_offsets: */
181    depth = mt->depth0;
182    for (level = mt->first_level; level <= mt->last_level; level++) {
183       GLuint i;
184       for (i = 0; i < depth; i++) {
185          intel_miptree_set_image_offset(mt, level, i,
186                                         0, i * stack_height);
187       }
188
189       depth = minify(depth);
190    }
191
192    /* Multiply slice size by texture depth for total size.  It's
193     * remarkable how wasteful of memory the i915 texture layouts
194     * are.  They are largely fixed in the i945.
195     */
196    mt->total_height = stack_height * mt->depth0;
197 }
198
199 static void
200 i915_miptree_layout_2d(struct intel_mipmap_tree * mt)
201 {
202    GLuint width = mt->width0;
203    GLuint height = mt->height0;
204    GLuint img_height;
205    GLint level;
206
207    mt->total_width = mt->width0;
208    mt->total_height = 0;
209
210    for (level = mt->first_level; level <= mt->last_level; level++) {
211       intel_miptree_set_level_info(mt, level,
212                                    0, mt->total_height,
213                                    width, height, 1);
214
215       if (mt->compressed)
216          img_height = ALIGN(height, 4) / 4;
217       else
218          img_height = ALIGN(height, 2);
219
220       mt->total_height += img_height;
221
222       width = minify(width);
223       height = minify(height);
224    }
225 }
226
227 void
228 i915_miptree_layout(struct intel_mipmap_tree * mt)
229 {
230    switch (mt->target) {
231    case GL_TEXTURE_CUBE_MAP:
232       i915_miptree_layout_cube(mt);
233       break;
234    case GL_TEXTURE_3D:
235       i915_miptree_layout_3d(mt);
236       break;
237    case GL_TEXTURE_1D:
238    case GL_TEXTURE_2D:
239    case GL_TEXTURE_RECTANGLE_ARB:
240    case GL_TEXTURE_EXTERNAL_OES:
241       i915_miptree_layout_2d(mt);
242       break;
243    default:
244       _mesa_problem(NULL, "Unexpected tex target in i915_miptree_layout()");
245       break;
246    }
247
248    DBG("%s: %dx%dx%d\n", __FUNCTION__,
249        mt->total_width, mt->total_height, mt->cpp);
250 }
251
252
253 /**
254  * Compressed cube texture map layout for GM945 and later.
255  *
256  * The hardware layout looks like the 830-915 layout, except for the small
257  * sizes.  A zoomed in view of the layout for 945 is:
258  *
259  * +-------+-------+
260  * |  8x8  |  8x8  |
261  * |       |       |
262  * |       |       |
263  * |  +x   |  +y   |
264  * |       |       |
265  * |       |       |
266  * |       |       |
267  * |       |       |
268  * +---+---+-------+
269  * |4x4|   |  8x8  |
270  * | +x|   |       |
271  * |   |   |       |
272  * |   |   |       |
273  * +---+   |  +z   |
274  * |4x4|   |       |
275  * | +y|   |       |
276  * |   |   |       |
277  * +---+   +-------+
278  *
279  * ...
280  *
281  * +-------+-------+
282  * |  8x8  |  8x8  |
283  * |       |       |
284  * |       |       |
285  * |  -x   |  -y   |
286  * |       |       |
287  * |       |       |
288  * |       |       |
289  * |       |       |
290  * +---+---+-------+
291  * |4x4|   |  8x8  |
292  * | -x|   |       |
293  * |   |   |       |
294  * |   |   |       |
295  * +---+   |  -z   |
296  * |4x4|   |       |
297  * | -y|   |       |
298  * |   |   |       |
299  * +---+   +---+---+---+---+---+---+---+---+---+
300  * |4x4|   |4x4|   |2x2|   |2x2|   |2x2|   |2x2|
301  * | +z|   | -z|   | +x|   | +y|   | +z|   | -x| ...
302  * |   |   |   |   |   |   |   |   |   |   |   |
303  * +---+   +---+   +---+   +---+   +---+   +---+
304  *
305  * The bottom row continues with the remaining 2x2 then the 1x1 mip contents
306  * in order, with each of them aligned to a 8x8 block boundary.  Thus, for
307  * 32x32 cube maps and smaller, the bottom row layout is going to dictate the
308  * pitch of the tree.  For a tree with 4x4 images, the pitch is at least
309  * 14 * 8 = 112 texels, for 2x2 it is at least 12 * 8 texels, and for 1x1
310  * it is 6 * 8 texels.
311  */
312
313 static void
314 i945_miptree_layout_cube(struct intel_mipmap_tree * mt)
315 {
316    const GLuint dim = mt->width0;
317    GLuint face;
318    GLuint lvlWidth = mt->width0, lvlHeight = mt->height0;
319    GLint level;
320
321    assert(lvlWidth == lvlHeight); /* cubemap images are square */
322
323    /* Depending on the size of the largest images, pitch can be
324     * determined either by the old-style packing of cubemap faces,
325     * or the final row of 4x4, 2x2 and 1x1 faces below this.
326     */
327    if (dim > 32)
328       mt->total_width = dim * 2;
329    else
330       mt->total_width = 14 * 8;
331
332    if (dim >= 4)
333       mt->total_height = dim * 4 + 4;
334    else
335       mt->total_height = 4;
336
337    /* Set all the levels to effectively occupy the whole rectangular region. */
338    for (level = mt->first_level; level <= mt->last_level; level++) {
339       intel_miptree_set_level_info(mt, level,
340                                    0, 0,
341                                    lvlWidth, lvlHeight, 6);
342       lvlWidth /= 2;
343       lvlHeight /= 2;
344    }
345
346    for (face = 0; face < 6; face++) {
347       GLuint x = initial_offsets[face][0] * dim;
348       GLuint y = initial_offsets[face][1] * dim;
349       GLuint d = dim;
350
351       if (dim == 4 && face >= 4) {
352          y = mt->total_height - 4;
353          x = (face - 4) * 8;
354       } else if (dim < 4 && (face > 0 || mt->first_level > 0)) {
355          y = mt->total_height - 4;
356          x = face * 8;
357       }
358
359       for (level = mt->first_level; level <= mt->last_level; level++) {
360          intel_miptree_set_image_offset(mt, level, face, x, y);
361
362          d >>= 1;
363
364          switch (d) {
365          case 4:
366             switch (face) {
367             case FACE_POS_X:
368             case FACE_NEG_X:
369                x += step_offsets[face][0] * d;
370                y += step_offsets[face][1] * d;
371                break;
372             case FACE_POS_Y:
373             case FACE_NEG_Y:
374                y += 12;
375                x -= 8;
376                break;
377             case FACE_POS_Z:
378             case FACE_NEG_Z:
379                y = mt->total_height - 4;
380                x = (face - 4) * 8;
381                break;
382             }
383             break;
384
385          case 2:
386             y = mt->total_height - 4;
387             x = bottom_offsets[face];
388             break;
389
390          case 1:
391             x += 48;
392             break;
393
394          default:
395             x += step_offsets[face][0] * d;
396             y += step_offsets[face][1] * d;
397             break;
398          }
399       }
400    }
401 }
402
403 static void
404 i945_miptree_layout_3d(struct intel_mipmap_tree * mt)
405 {
406    GLuint width = mt->width0;
407    GLuint height = mt->height0;
408    GLuint depth = mt->depth0;
409    GLuint pack_x_pitch, pack_x_nr;
410    GLuint pack_y_pitch;
411    GLuint level;
412
413    mt->total_width = mt->width0;
414    mt->total_height = 0;
415
416    pack_y_pitch = MAX2(mt->height0, 2);
417    pack_x_pitch = mt->total_width;
418    pack_x_nr = 1;
419
420    for (level = mt->first_level; level <= mt->last_level; level++) {
421       GLint x = 0;
422       GLint y = 0;
423       GLint q, j;
424
425       intel_miptree_set_level_info(mt, level,
426                                    0, mt->total_height,
427                                    width, height, depth);
428
429       for (q = 0; q < depth;) {
430          for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
431             intel_miptree_set_image_offset(mt, level, q, x, y);
432             x += pack_x_pitch;
433          }
434
435          x = 0;
436          y += pack_y_pitch;
437       }
438
439       mt->total_height += y;
440
441       if (pack_x_pitch > 4) {
442          pack_x_pitch >>= 1;
443          pack_x_nr <<= 1;
444          assert(pack_x_pitch * pack_x_nr <= mt->total_width);
445       }
446
447       if (pack_y_pitch > 2) {
448          pack_y_pitch >>= 1;
449       }
450
451       width = minify(width);
452       height = minify(height);
453       depth = minify(depth);
454    }
455 }
456
457 void
458 i945_miptree_layout(struct intel_mipmap_tree * mt)
459 {
460    switch (mt->target) {
461    case GL_TEXTURE_CUBE_MAP:
462       if (mt->compressed)
463          i945_miptree_layout_cube(mt);
464       else
465          i915_miptree_layout_cube(mt);
466       break;
467    case GL_TEXTURE_3D:
468       i945_miptree_layout_3d(mt);
469       break;
470    case GL_TEXTURE_1D:
471    case GL_TEXTURE_2D:
472    case GL_TEXTURE_RECTANGLE_ARB:
473    case GL_TEXTURE_EXTERNAL_OES:
474       i945_miptree_layout_2d(mt);
475       break;
476    default:
477       _mesa_problem(NULL, "Unexpected tex target in i945_miptree_layout()");
478       break;
479    }
480
481    DBG("%s: %dx%dx%d\n", __FUNCTION__,
482        mt->total_width, mt->total_height, mt->cpp);
483 }