i965: Fix packed depth/stencil textures to be Y-tiled as well.
[mesa.git] / src / mesa / drivers / dri / intel / intel_mipmap_tree.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 #include "intel_context.h"
29 #include "intel_mipmap_tree.h"
30 #include "intel_regions.h"
31 #include "intel_chipset.h"
32 #include "main/enums.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
35
36 static GLenum
37 target_to_target(GLenum target)
38 {
39 switch (target) {
40 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
46 return GL_TEXTURE_CUBE_MAP_ARB;
47 default:
48 return target;
49 }
50 }
51
52 static struct intel_mipmap_tree *
53 intel_miptree_create_internal(struct intel_context *intel,
54 GLenum target,
55 GLenum internal_format,
56 GLuint first_level,
57 GLuint last_level,
58 GLuint width0,
59 GLuint height0,
60 GLuint depth0, GLuint cpp, GLuint compress_byte,
61 uint32_t tiling)
62 {
63 GLboolean ok;
64 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
65
66 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
67 _mesa_lookup_enum_by_nr(target),
68 _mesa_lookup_enum_by_nr(internal_format),
69 first_level, last_level, mt);
70
71 mt->target = target_to_target(target);
72 mt->internal_format = internal_format;
73 mt->first_level = first_level;
74 mt->last_level = last_level;
75 mt->width0 = width0;
76 mt->height0 = height0;
77 mt->depth0 = depth0;
78 mt->cpp = compress_byte ? compress_byte : cpp;
79 mt->compressed = compress_byte ? 1 : 0;
80 mt->refcount = 1;
81 mt->pitch = 0;
82
83 #ifdef I915
84 if (IS_945(intel->intelScreen->deviceID))
85 ok = i945_miptree_layout(intel, mt, tiling);
86 else
87 ok = i915_miptree_layout(intel, mt, tiling);
88 #else
89 ok = brw_miptree_layout(intel, mt, tiling);
90 #endif
91
92 if (!ok) {
93 free(mt);
94 DBG("%s not okay - returning NULL\n", __FUNCTION__);
95 return NULL;
96 }
97
98 return mt;
99 }
100
101 struct intel_mipmap_tree *
102 intel_miptree_create(struct intel_context *intel,
103 GLenum target,
104 GLenum internal_format,
105 GLuint first_level,
106 GLuint last_level,
107 GLuint width0,
108 GLuint height0,
109 GLuint depth0, GLuint cpp, GLuint compress_byte,
110 GLboolean expect_accelerated_upload)
111 {
112 struct intel_mipmap_tree *mt;
113 uint32_t tiling;
114
115 if (intel->use_texture_tiling && compress_byte == 0 &&
116 intel->intelScreen->kernel_exec_fencing) {
117 if (IS_965(intel->intelScreen->deviceID) &&
118 (internal_format == GL_DEPTH_COMPONENT ||
119 internal_format == GL_DEPTH_STENCIL_EXT))
120 tiling = I915_TILING_Y;
121 else
122 tiling = I915_TILING_X;
123 } else
124 tiling = I915_TILING_NONE;
125
126 mt = intel_miptree_create_internal(intel, target, internal_format,
127 first_level, last_level, width0,
128 height0, depth0, cpp, compress_byte,
129 tiling);
130 /*
131 * pitch == 0 || height == 0 indicates the null texture
132 */
133 if (!mt || !mt->pitch || !mt->total_height)
134 return NULL;
135
136 mt->region = intel_region_alloc(intel,
137 tiling,
138 mt->cpp,
139 mt->pitch,
140 mt->total_height,
141 mt->pitch,
142 expect_accelerated_upload);
143
144 if (!mt->region) {
145 free(mt);
146 return NULL;
147 }
148
149 return mt;
150 }
151
152 struct intel_mipmap_tree *
153 intel_miptree_create_for_region(struct intel_context *intel,
154 GLenum target,
155 GLenum internal_format,
156 GLuint first_level,
157 GLuint last_level,
158 struct intel_region *region,
159 GLuint depth0,
160 GLuint compress_byte)
161 {
162 struct intel_mipmap_tree *mt;
163
164 mt = intel_miptree_create_internal(intel, target, internal_format,
165 first_level, last_level,
166 region->width, region->height, 1,
167 region->cpp, compress_byte,
168 I915_TILING_NONE);
169 if (!mt)
170 return mt;
171 #if 0
172 if (mt->pitch != region->pitch) {
173 fprintf(stderr,
174 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
175 region->pitch, mt->pitch);
176 free(mt);
177 return NULL;
178 }
179 #else
180 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
181 * to texture works, but we don't need that for texturing from a
182 * pixmap. Just override it here. */
183 mt->pitch = region->pitch;
184 #endif
185
186 intel_region_reference(&mt->region, region);
187
188 return mt;
189 }
190
191 /**
192 * intel_miptree_pitch_align:
193 *
194 * @intel: intel context pointer
195 *
196 * @mt: the miptree to compute pitch alignment for
197 *
198 * @pitch: the natural pitch value
199 *
200 * Given @pitch, compute a larger value which accounts for
201 * any necessary alignment required by the device
202 */
203
204 int intel_miptree_pitch_align (struct intel_context *intel,
205 struct intel_mipmap_tree *mt,
206 uint32_t tiling,
207 int pitch)
208 {
209 #ifdef I915
210 GLcontext *ctx = &intel->ctx;
211 #endif
212
213 if (!mt->compressed) {
214 int pitch_align;
215
216 if (intel->ttm) {
217 /* XXX: Align pitch to multiple of 64 bytes for now to allow
218 * render-to-texture to work in all cases. This should probably be
219 * replaced at some point by some scheme to only do this when really
220 * necessary.
221 */
222 pitch_align = 64;
223 } else {
224 pitch_align = 4;
225 }
226
227 if (tiling == I915_TILING_X)
228 pitch_align = 512;
229 else if (tiling == I915_TILING_Y)
230 pitch_align = 128;
231
232 pitch = ALIGN(pitch * mt->cpp, pitch_align);
233
234 #ifdef I915
235 /* XXX: At least the i915 seems very upset when the pitch is a multiple
236 * of 1024 and sometimes 512 bytes - performance can drop by several
237 * times. Go to the next multiple of the required alignment for now.
238 */
239 if (!(pitch & 511) &&
240 (pitch + pitch_align) < (1 << ctx->Const.MaxTextureLevels))
241 pitch += pitch_align;
242 #endif
243
244 pitch /= mt->cpp;
245 }
246 return pitch;
247 }
248
249 void
250 intel_miptree_reference(struct intel_mipmap_tree **dst,
251 struct intel_mipmap_tree *src)
252 {
253 src->refcount++;
254 *dst = src;
255 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
256 }
257
258 void
259 intel_miptree_release(struct intel_context *intel,
260 struct intel_mipmap_tree **mt)
261 {
262 if (!*mt)
263 return;
264
265 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
266 if (--(*mt)->refcount <= 0) {
267 GLuint i;
268
269 DBG("%s deleting %p\n", __FUNCTION__, *mt);
270
271 intel_region_release(&((*mt)->region));
272
273 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
274 if ((*mt)->level[i].image_offset)
275 free((*mt)->level[i].image_offset);
276
277 free(*mt);
278 }
279 *mt = NULL;
280 }
281
282
283
284
285 /* Can the image be pulled into a unified mipmap tree. This mirrors
286 * the completeness test in a lot of ways.
287 *
288 * Not sure whether I want to pass gl_texture_image here.
289 */
290 GLboolean
291 intel_miptree_match_image(struct intel_mipmap_tree *mt,
292 struct gl_texture_image *image,
293 GLuint face, GLuint level)
294 {
295 /* Images with borders are never pulled into mipmap trees.
296 */
297 if (image->Border ||
298 ((image->_BaseFormat == GL_DEPTH_COMPONENT) &&
299 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) ||
300 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER))))
301 return GL_FALSE;
302
303 if (image->InternalFormat != mt->internal_format ||
304 image->IsCompressed != mt->compressed)
305 return GL_FALSE;
306
307 if (!image->IsCompressed &&
308 !mt->compressed &&
309 image->TexFormat->TexelBytes != mt->cpp)
310 return GL_FALSE;
311
312 /* Test image dimensions against the base level image adjusted for
313 * minification. This will also catch images not present in the
314 * tree, changed targets, etc.
315 */
316 if (image->Width != mt->level[level].width ||
317 image->Height != mt->level[level].height ||
318 image->Depth != mt->level[level].depth)
319 return GL_FALSE;
320
321 return GL_TRUE;
322 }
323
324
325 void
326 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
327 GLuint level,
328 GLuint nr_images,
329 GLuint x, GLuint y,
330 GLuint w, GLuint h, GLuint d)
331 {
332 mt->level[level].width = w;
333 mt->level[level].height = h;
334 mt->level[level].depth = d;
335 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
336 mt->level[level].nr_images = nr_images;
337
338 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
339 level, w, h, d, x, y, mt->level[level].level_offset);
340
341 /* Not sure when this would happen, but anyway:
342 */
343 if (mt->level[level].image_offset) {
344 free(mt->level[level].image_offset);
345 mt->level[level].image_offset = NULL;
346 }
347
348 assert(nr_images);
349
350 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
351 mt->level[level].image_offset[0] = 0;
352 }
353
354
355
356 void
357 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
358 GLuint level, GLuint img,
359 GLuint x, GLuint y)
360 {
361 if (img == 0 && level == 0)
362 assert(x == 0 && y == 0);
363
364 assert(img < mt->level[level].nr_images);
365
366 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp;
367
368 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
369 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
370 }
371
372
373 /* Although we use the image_offset[] array to store relative offsets
374 * to cube faces, Mesa doesn't know anything about this and expects
375 * each cube face to be treated as a separate image.
376 *
377 * These functions present that view to mesa:
378 */
379 const GLuint *
380 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
381 {
382 static const GLuint zero = 0;
383
384 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
385 return &zero;
386 else
387 return mt->level[level].image_offset;
388 }
389
390
391 GLuint
392 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
393 GLuint face, GLuint level)
394 {
395 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
396 return (mt->level[level].level_offset +
397 mt->level[level].image_offset[face]);
398 else
399 return mt->level[level].level_offset;
400 }
401
402
403
404 /**
405 * Map a teximage in a mipmap tree.
406 * \param row_stride returns row stride in bytes
407 * \param image_stride returns image stride in bytes (for 3D textures).
408 * \param image_offsets pointer to array of pixel offsets from the returned
409 * pointer to each depth image
410 * \return address of mapping
411 */
412 GLubyte *
413 intel_miptree_image_map(struct intel_context * intel,
414 struct intel_mipmap_tree * mt,
415 GLuint face,
416 GLuint level,
417 GLuint * row_stride, GLuint * image_offsets)
418 {
419 DBG("%s \n", __FUNCTION__);
420
421 if (row_stride)
422 *row_stride = mt->pitch * mt->cpp;
423
424 if (mt->target == GL_TEXTURE_3D) {
425 int i;
426
427 for (i = 0; i < mt->level[level].depth; i++)
428 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp;
429 } else {
430 assert(mt->level[level].depth == 1);
431 assert(mt->target == GL_TEXTURE_CUBE_MAP ||
432 mt->level[level].image_offset[0] == 0);
433 image_offsets[0] = 0;
434 }
435
436 return (intel_region_map(intel, mt->region) +
437 intel_miptree_image_offset(mt, face, level));
438 }
439
440 void
441 intel_miptree_image_unmap(struct intel_context *intel,
442 struct intel_mipmap_tree *mt)
443 {
444 DBG("%s\n", __FUNCTION__);
445 intel_region_unmap(intel, mt->region);
446 }
447
448
449
450 /* Upload data for a particular image.
451 */
452 void
453 intel_miptree_image_data(struct intel_context *intel,
454 struct intel_mipmap_tree *dst,
455 GLuint face,
456 GLuint level,
457 void *src,
458 GLuint src_row_pitch,
459 GLuint src_image_pitch)
460 {
461 GLuint depth = dst->level[level].depth;
462 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
463 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
464 GLuint i;
465 GLuint height = 0;
466
467 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
468 for (i = 0; i < depth; i++) {
469 height = dst->level[level].height;
470 if(dst->compressed)
471 height = (height + 3) / 4;
472 intel_region_data(intel,
473 dst->region,
474 dst_offset + dst_depth_offset[i], /* dst_offset */
475 0, 0, /* dstx, dsty */
476 src,
477 src_row_pitch,
478 0, 0, /* source x, y */
479 dst->level[level].width, height); /* width, height */
480
481 src += src_image_pitch * dst->cpp;
482 }
483 }
484
485 extern GLuint intel_compressed_alignment(GLenum);
486 /* Copy mipmap image between trees
487 */
488 void
489 intel_miptree_image_copy(struct intel_context *intel,
490 struct intel_mipmap_tree *dst,
491 GLuint face, GLuint level,
492 struct intel_mipmap_tree *src)
493 {
494 GLuint width = src->level[level].width;
495 GLuint height = src->level[level].height;
496 GLuint depth = src->level[level].depth;
497 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
498 GLuint src_offset = intel_miptree_image_offset(src, face, level);
499 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
500 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
501 GLuint i;
502 GLboolean success;
503
504 if (dst->compressed) {
505 GLuint alignment = intel_compressed_alignment(dst->internal_format);
506 height = (height + 3) / 4;
507 width = ((width + alignment - 1) & ~(alignment - 1));
508 }
509
510 for (i = 0; i < depth; i++) {
511 success = intel_region_copy(intel,
512 dst->region, dst_offset + dst_depth_offset[i],
513 0, 0,
514 src->region, src_offset + src_depth_offset[i],
515 0, 0, width, height, GL_COPY);
516 if (!success) {
517 GLubyte *src_ptr, *dst_ptr;
518
519 src_ptr = intel_region_map(intel, src->region);
520 dst_ptr = intel_region_map(intel, dst->region);
521
522 _mesa_copy_rect(dst_ptr + dst_offset + dst_depth_offset[i],
523 dst->cpp,
524 dst->pitch,
525 0, 0, width, height,
526 src_ptr + src_offset + src_depth_offset[i],
527 src->pitch,
528 0, 0);
529 intel_region_unmap(intel, src->region);
530 intel_region_unmap(intel, dst->region);
531 }
532 }
533 }