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