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