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