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