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