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