Tizen 2.0 Release
[profile/ivi/osmesa.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 #include "intel_tex.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
35
36 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
37
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
56 static struct intel_mipmap_tree *
57 intel_miptree_create_internal(struct intel_context *intel,
58                               GLenum target,
59                               gl_format format,
60                               GLuint first_level,
61                               GLuint last_level,
62                               GLuint width0,
63                               GLuint height0,
64                               GLuint depth0,
65                               uint32_t tiling)
66 {
67    GLboolean ok;
68    struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
69    int compress_byte = 0;
70
71    DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
72        _mesa_lookup_enum_by_nr(target),
73        _mesa_get_format_name(format),
74        first_level, last_level, mt);
75
76    if (_mesa_is_format_compressed(format))
77       compress_byte = intel_compressed_num_bytes(format);
78
79    mt->target = target_to_target(target);
80    mt->format = format;
81    mt->first_level = first_level;
82    mt->last_level = last_level;
83    mt->width0 = width0;
84    mt->height0 = height0;
85    mt->depth0 = depth0;
86    mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
87    mt->compressed = compress_byte ? 1 : 0;
88    mt->refcount = 1; 
89
90 #ifdef I915
91    if (intel->is_945)
92       ok = i945_miptree_layout(intel, mt, tiling);
93    else
94       ok = i915_miptree_layout(intel, mt, tiling);
95 #else
96    ok = brw_miptree_layout(intel, mt, tiling);
97 #endif
98
99    if (!ok) {
100       free(mt);
101       DBG("%s not okay - returning NULL\n", __FUNCTION__);
102       return NULL;
103    }
104
105    return mt;
106 }
107
108
109 struct intel_mipmap_tree *
110 intel_miptree_create(struct intel_context *intel,
111                      GLenum target,
112                      gl_format format,
113                      GLuint first_level,
114                      GLuint last_level,
115                      GLuint width0,
116                      GLuint height0,
117                      GLuint depth0,
118                      GLboolean expect_accelerated_upload)
119 {
120    struct intel_mipmap_tree *mt;
121    uint32_t tiling = I915_TILING_NONE;
122    GLenum base_format = _mesa_get_format_base_format(format);
123
124    if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
125       if (intel->gen >= 4 &&
126           (base_format == GL_DEPTH_COMPONENT ||
127            base_format == GL_DEPTH_STENCIL_EXT))
128          tiling = I915_TILING_Y;
129       else if (width0 >= 64)
130          tiling = I915_TILING_X;
131    }
132
133    mt = intel_miptree_create_internal(intel, target, format,
134                                       first_level, last_level, width0,
135                                       height0, depth0,
136                                       tiling);
137    /*
138     * pitch == 0 || height == 0  indicates the null texture
139     */
140    if (!mt || !mt->total_width || !mt->total_height) {
141       free(mt);
142       return NULL;
143    }
144
145    mt->region = intel_region_alloc(intel->intelScreen,
146                                    tiling,
147                                    mt->cpp,
148                                    mt->total_width,
149                                    mt->total_height,
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                                 gl_format format,
165                                 struct intel_region *region,
166                                 GLuint depth0)
167 {
168    struct intel_mipmap_tree *mt;
169
170    mt = intel_miptree_create_internal(intel, target, format,
171                                       0, 0,
172                                       region->width, region->height, 1,
173                                       I915_TILING_NONE);
174    if (!mt)
175       return mt;
176
177    intel_region_reference(&mt->region, region);
178
179    return mt;
180 }
181
182 void
183 intel_miptree_reference(struct intel_mipmap_tree **dst,
184                         struct intel_mipmap_tree *src)
185 {
186    src->refcount++;
187    *dst = src;
188    DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
189 }
190
191
192 void
193 intel_miptree_release(struct intel_context *intel,
194                       struct intel_mipmap_tree **mt)
195 {
196    if (!*mt)
197       return;
198
199    DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
200    if (--(*mt)->refcount <= 0) {
201       GLuint i;
202
203       DBG("%s deleting %p\n", __FUNCTION__, *mt);
204
205       intel_region_release(&((*mt)->region));
206       intel_region_release(&((*mt)->hiz_region));
207
208       for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
209          free((*mt)->level[i].x_offset);
210          free((*mt)->level[i].y_offset);
211       }
212
213       free(*mt);
214    }
215    *mt = NULL;
216 }
217
218
219 /**
220  * Can the image be pulled into a unified mipmap tree?  This mirrors
221  * the completeness test in a lot of ways.
222  *
223  * Not sure whether I want to pass gl_texture_image here.
224  */
225 GLboolean
226 intel_miptree_match_image(struct intel_mipmap_tree *mt,
227                           struct gl_texture_image *image)
228 {
229    struct intel_texture_image *intelImage = intel_texture_image(image);
230    GLuint level = intelImage->level;
231
232    /* Images with borders are never pulled into mipmap trees. */
233    if (image->Border)
234       return GL_FALSE;
235
236    if (image->TexFormat != mt->format)
237       return GL_FALSE;
238
239    /* Test image dimensions against the base level image adjusted for
240     * minification.  This will also catch images not present in the
241     * tree, changed targets, etc.
242     */
243    if (image->Width != mt->level[level].width ||
244        image->Height != mt->level[level].height ||
245        image->Depth != mt->level[level].depth)
246       return GL_FALSE;
247
248    return GL_TRUE;
249 }
250
251
252 void
253 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
254                              GLuint level,
255                              GLuint nr_images,
256                              GLuint x, GLuint y,
257                              GLuint w, GLuint h, GLuint d)
258 {
259    mt->level[level].width = w;
260    mt->level[level].height = h;
261    mt->level[level].depth = d;
262    mt->level[level].level_x = x;
263    mt->level[level].level_y = y;
264    mt->level[level].nr_images = nr_images;
265
266    DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
267        level, w, h, d, x, y);
268
269    assert(nr_images);
270    assert(!mt->level[level].x_offset);
271
272    mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint));
273    mt->level[level].x_offset[0] = mt->level[level].level_x;
274    mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint));
275    mt->level[level].y_offset[0] = mt->level[level].level_y;
276 }
277
278
279 void
280 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
281                                GLuint level, GLuint img,
282                                GLuint x, GLuint y)
283 {
284    if (img == 0 && level == 0)
285       assert(x == 0 && y == 0);
286
287    assert(img < mt->level[level].nr_images);
288
289    mt->level[level].x_offset[img] = mt->level[level].level_x + x;
290    mt->level[level].y_offset[img] = mt->level[level].level_y + y;
291
292    DBG("%s level %d img %d pos %d,%d\n",
293        __FUNCTION__, level, img,
294        mt->level[level].x_offset[img], mt->level[level].y_offset[img]);
295 }
296
297
298 void
299 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
300                                GLuint level, GLuint face, GLuint depth,
301                                GLuint *x, GLuint *y)
302 {
303    if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
304       *x = mt->level[level].x_offset[face];
305       *y = mt->level[level].y_offset[face];
306    } else if (mt->target == GL_TEXTURE_3D) {
307       *x = mt->level[level].x_offset[depth];
308       *y = mt->level[level].y_offset[depth];
309    } else {
310       *x = mt->level[level].x_offset[0];
311       *y = mt->level[level].y_offset[0];
312    }
313 }
314
315 /**
316  * Map a teximage in a mipmap tree.
317  * \param row_stride  returns row stride in bytes
318  * \param image_stride  returns image stride in bytes (for 3D textures).
319  * \param image_offsets pointer to array of pixel offsets from the returned
320  *        pointer to each depth image
321  * \return address of mapping
322  */
323 GLubyte *
324 intel_miptree_image_map(struct intel_context * intel,
325                         struct intel_mipmap_tree * mt,
326                         GLuint face,
327                         GLuint level,
328                         GLuint * row_stride, GLuint * image_offsets)
329 {
330    GLuint x, y;
331
332    if (row_stride)
333       *row_stride = mt->region->pitch * mt->cpp;
334
335    if (mt->target == GL_TEXTURE_3D) {
336       int i;
337
338       for (i = 0; i < mt->level[level].depth; i++) {
339
340          intel_miptree_get_image_offset(mt, level, face, i,
341                                         &x, &y);
342          image_offsets[i] = x + y * mt->region->pitch;
343       }
344
345       DBG("%s \n", __FUNCTION__);
346
347       return intel_region_map(intel, mt->region);
348    } else {
349       assert(mt->level[level].depth == 1);
350       intel_miptree_get_image_offset(mt, level, face, 0,
351                                      &x, &y);
352       image_offsets[0] = 0;
353
354       DBG("%s: (%d,%d) -> (%d, %d)/%d\n",
355           __FUNCTION__, face, level, x, y, mt->region->pitch * mt->cpp);
356
357       return intel_region_map(intel, mt->region) +
358          (x + y * mt->region->pitch) * mt->cpp;
359    }
360 }
361
362
363 void
364 intel_miptree_image_unmap(struct intel_context *intel,
365                           struct intel_mipmap_tree *mt)
366 {
367    DBG("%s\n", __FUNCTION__);
368    intel_region_unmap(intel, mt->region);
369 }
370
371
372 /**
373  * Upload data for a particular image.
374  */
375 void
376 intel_miptree_image_data(struct intel_context *intel,
377                          struct intel_mipmap_tree *dst,
378                          GLuint face,
379                          GLuint level,
380                          void *src,
381                          GLuint src_row_pitch,
382                          GLuint src_image_pitch)
383 {
384    const GLuint depth = dst->level[level].depth;
385    GLuint i;
386
387    for (i = 0; i < depth; i++) {
388       GLuint dst_x, dst_y, height, width;
389
390       intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
391
392       height = dst->level[level].height;
393       width = dst->level[level].width;
394       if (dst->compressed) {
395          unsigned int align_w, align_h;
396
397          intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
398          height = (height + align_h - 1) / align_h;
399          width = ALIGN(width, align_w);
400       }
401
402       DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
403           __FUNCTION__, face, level,
404           src, src_row_pitch * dst->cpp,
405           dst_x, dst_y, dst->region->pitch * dst->cpp,
406           width, height);
407
408       intel_region_data(intel,
409                         dst->region, 0, dst_x, dst_y,
410                         src,
411                         src_row_pitch,
412                         0, 0,                             /* source x, y */
413                         width, height);
414
415       src = (char *)src + src_image_pitch * dst->cpp;
416    }
417 }
418
419
420 /**
421  * Copy mipmap image between trees
422  */
423 void
424 intel_miptree_image_copy(struct intel_context *intel,
425                          struct intel_mipmap_tree *dst,
426                          GLuint face, GLuint level,
427                          struct intel_mipmap_tree *src)
428 {
429    GLuint width = src->level[level].width;
430    GLuint height = src->level[level].height;
431    GLuint depth = src->level[level].depth;
432    GLuint src_x, src_y, dst_x, dst_y;
433    GLuint i;
434    GLboolean success;
435
436    if (dst->compressed) {
437        GLuint align_w, align_h;
438
439        intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
440        height = (height + 3) / 4;
441        width = ALIGN(width, align_w);
442    }
443
444    intel_prepare_render(intel);
445
446    for (i = 0; i < depth; i++) {
447       intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y);
448       intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
449       success = intel_region_copy(intel,
450                                   dst->region, 0, dst_x, dst_y,
451                                   src->region, 0, src_x, src_y,
452                                   width, height, GL_FALSE,
453                                   GL_COPY);
454       if (!success) {
455          GLubyte *src_ptr, *dst_ptr;
456
457          src_ptr = intel_region_map(intel, src->region);
458          dst_ptr = intel_region_map(intel, dst->region);
459
460          _mesa_copy_rect(dst_ptr,
461                          dst->cpp,
462                          dst->region->pitch,
463                          dst_x, dst_y, width, height,
464                          src_ptr,
465                          src->region->pitch,
466                          src_x, src_y);
467          intel_region_unmap(intel, src->region);
468          intel_region_unmap(intel, dst->region);
469       }
470    }
471 }