2ced6ac86b2f1f49c1807ffdcb2e9f5688319315
[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 #include "main/enums.h"
33 #include "main/formats.h"
34
35 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
36
37
38 static GLenum
39 target_to_target(GLenum target)
40 {
41 switch (target) {
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
46 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
47 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
48 return GL_TEXTURE_CUBE_MAP_ARB;
49 default:
50 return target;
51 }
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 levels,
60 GLuint width0,
61 GLuint height0,
62 GLuint depth0, GLuint cpp, GLuint compress_byte,
63 uint32_t tiling)
64 {
65 GLboolean ok;
66 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
67
68 DBG("%s target %s format %s levels %d <-- %p\n", __FUNCTION__,
69 _mesa_lookup_enum_by_nr(target),
70 _mesa_lookup_enum_by_nr(internal_format),
71 levels, mt);
72
73 mt->target = target_to_target(target);
74 mt->internal_format = internal_format;
75 mt->levels = levels;
76 mt->width0 = width0;
77 mt->height0 = height0;
78 mt->depth0 = depth0;
79 mt->cpp = compress_byte ? compress_byte : cpp;
80 mt->compressed = compress_byte ? 1 : 0;
81 mt->refcount = 1;
82
83 #ifdef I915
84 if (intel->is_945)
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
102 struct intel_mipmap_tree *
103 intel_miptree_create(struct intel_context *intel,
104 GLenum target,
105 GLenum base_format,
106 GLenum internal_format,
107 GLuint levels,
108 GLuint width0,
109 GLuint height0,
110 GLuint depth0, GLuint cpp, GLuint compress_byte,
111 GLboolean expect_accelerated_upload)
112 {
113 struct intel_mipmap_tree *mt;
114 uint32_t tiling = I915_TILING_NONE;
115
116 if (intel->use_texture_tiling && compress_byte == 0) {
117 if (intel->gen >= 4 &&
118 (base_format == GL_DEPTH_COMPONENT ||
119 base_format == GL_DEPTH_STENCIL_EXT))
120 tiling = I915_TILING_Y;
121 else if (width0 >= 64)
122 tiling = I915_TILING_X;
123 }
124
125 mt = intel_miptree_create_internal(intel, target, internal_format,
126 levels, width0,
127 height0, depth0, cpp, compress_byte,
128 tiling);
129 /*
130 * pitch == 0 || height == 0 indicates the null texture
131 */
132 if (!mt || !mt->total_height) {
133 free(mt);
134 return NULL;
135 }
136
137 mt->region = intel_region_alloc(intel->intelScreen,
138 tiling,
139 mt->cpp,
140 mt->total_width,
141 mt->total_height,
142 expect_accelerated_upload);
143
144 if (!mt->region) {
145 free(mt);
146 return NULL;
147 }
148
149 return mt;
150 }
151
152
153 struct intel_mipmap_tree *
154 intel_miptree_create_for_region(struct intel_context *intel,
155 GLenum target,
156 GLenum internal_format,
157 struct intel_region *region,
158 GLuint depth0,
159 GLuint compress_byte)
160 {
161 struct intel_mipmap_tree *mt;
162
163 mt = intel_miptree_create_internal(intel, target, internal_format,
164 1,
165 region->width, region->height, 1,
166 region->cpp, compress_byte,
167 I915_TILING_NONE);
168 if (!mt)
169 return mt;
170
171 intel_region_reference(&mt->region, region);
172
173 return mt;
174 }
175
176 void
177 intel_miptree_reference(struct intel_mipmap_tree **dst,
178 struct intel_mipmap_tree *src)
179 {
180 src->refcount++;
181 *dst = src;
182 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
183 }
184
185
186 void
187 intel_miptree_release(struct intel_context *intel,
188 struct intel_mipmap_tree **mt)
189 {
190 if (!*mt)
191 return;
192
193 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
194 if (--(*mt)->refcount <= 0) {
195 GLuint i;
196
197 DBG("%s deleting %p\n", __FUNCTION__, *mt);
198
199 intel_region_release(&((*mt)->region));
200
201 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
202 free((*mt)->level[i].x_offset);
203 free((*mt)->level[i].y_offset);
204 }
205
206 free(*mt);
207 }
208 *mt = NULL;
209 }
210
211
212 /**
213 * Can the image be pulled into a unified mipmap tree? This mirrors
214 * the completeness test in a lot of ways.
215 *
216 * Not sure whether I want to pass gl_texture_image here.
217 */
218 GLboolean
219 intel_miptree_match_image(struct intel_mipmap_tree *mt,
220 struct gl_texture_image *image)
221 {
222 GLboolean isCompressed = _mesa_is_format_compressed(image->TexFormat);
223 struct intel_texture_image *intelImage = intel_texture_image(image);
224 GLuint level = intelImage->level;
225
226 /* Images with borders are never pulled into mipmap trees. */
227 if (image->Border)
228 return GL_FALSE;
229
230 if (image->InternalFormat != mt->internal_format ||
231 isCompressed != mt->compressed)
232 return GL_FALSE;
233
234 if (!isCompressed &&
235 !mt->compressed &&
236 _mesa_get_format_bytes(image->TexFormat) != mt->cpp)
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;
389
390 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
391
392 height = dst->level[level].height;
393 if(dst->compressed)
394 height = (height + 3) / 4;
395
396 DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
397 __FUNCTION__, face, level,
398 src, src_row_pitch * dst->cpp,
399 dst_x, dst_y, dst->region->pitch * dst->cpp,
400 dst->level[level].width, height);
401
402 intel_region_data(intel,
403 dst->region, 0, dst_x, dst_y,
404 src,
405 src_row_pitch,
406 0, 0, /* source x, y */
407 dst->level[level].width, height); /* width, height */
408
409 src = (char *)src + src_image_pitch * dst->cpp;
410 }
411 }
412
413
414 /**
415 * Copy mipmap image between trees
416 */
417 void
418 intel_miptree_image_copy(struct intel_context *intel,
419 struct intel_mipmap_tree *dst,
420 GLuint face, GLuint level,
421 struct intel_mipmap_tree *src)
422 {
423 GLuint width = src->level[level].width;
424 GLuint height = src->level[level].height;
425 GLuint depth = src->level[level].depth;
426 GLuint src_x, src_y, dst_x, dst_y;
427 GLuint i;
428 GLboolean success;
429
430 if (dst->compressed) {
431 GLuint align_w, align_h;
432
433 intel_get_texture_alignment_unit(dst->internal_format,
434 &align_w, &align_h);
435 height = (height + 3) / 4;
436 width = ALIGN(width, align_w);
437 }
438
439 intel_prepare_render(intel);
440
441 for (i = 0; i < depth; i++) {
442 intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y);
443 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
444 success = intel_region_copy(intel,
445 dst->region, 0, dst_x, dst_y,
446 src->region, 0, src_x, src_y,
447 width, height, GL_FALSE,
448 GL_COPY);
449 if (!success) {
450 GLubyte *src_ptr, *dst_ptr;
451
452 src_ptr = intel_region_map(intel, src->region);
453 dst_ptr = intel_region_map(intel, dst->region);
454
455 _mesa_copy_rect(dst_ptr,
456 dst->cpp,
457 dst->region->pitch,
458 dst_x, dst_y, width, height,
459 src_ptr,
460 src->region->pitch,
461 src_x, src_y);
462 intel_region_unmap(intel, src->region);
463 intel_region_unmap(intel, dst->region);
464 }
465 }
466 }