Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[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_chipset.h"
32 #include "main/enums.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
35
36 static GLenum
37 target_to_target(GLenum target)
38 {
39 switch (target) {
40 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
46 return GL_TEXTURE_CUBE_MAP_ARB;
47 default:
48 return target;
49 }
50 }
51
52 static struct intel_mipmap_tree *
53 intel_miptree_create_internal(struct intel_context *intel,
54 GLenum target,
55 GLenum internal_format,
56 GLuint first_level,
57 GLuint last_level,
58 GLuint width0,
59 GLuint height0,
60 GLuint depth0, GLuint cpp, GLuint compress_byte)
61 {
62 GLboolean ok;
63 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
64
65 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
66 _mesa_lookup_enum_by_nr(target),
67 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
68
69 mt->target = target_to_target(target);
70 mt->internal_format = internal_format;
71 mt->first_level = first_level;
72 mt->last_level = last_level;
73 mt->width0 = width0;
74 mt->height0 = height0;
75 mt->depth0 = depth0;
76 mt->cpp = compress_byte ? compress_byte : cpp;
77 mt->compressed = compress_byte ? 1 : 0;
78 mt->refcount = 1;
79 mt->pitch = 0;
80
81 #ifdef I915
82 if (IS_945(intel->intelScreen->deviceID))
83 ok = i945_miptree_layout(intel, mt);
84 else
85 ok = i915_miptree_layout(intel, mt);
86 #else
87 ok = brw_miptree_layout(intel, mt);
88 #endif
89
90 if (!ok) {
91 free(mt);
92 return NULL;
93 }
94
95 return mt;
96 }
97
98 struct intel_mipmap_tree *
99 intel_miptree_create(struct intel_context *intel,
100 GLenum target,
101 GLenum internal_format,
102 GLuint first_level,
103 GLuint last_level,
104 GLuint width0,
105 GLuint height0,
106 GLuint depth0, GLuint cpp, GLuint compress_byte)
107 {
108 struct intel_mipmap_tree *mt;
109
110 mt = intel_miptree_create_internal(intel, target, internal_format,
111 first_level, last_level, width0,
112 height0, depth0, cpp, compress_byte);
113 /*
114 * pitch == 0 indicates the null texture
115 */
116 if (!mt || !mt->pitch)
117 return NULL;
118
119 mt->region = intel_region_alloc(intel,
120 mt->cpp,
121 mt->pitch,
122 mt->total_height,
123 mt->pitch);
124
125 if (!mt->region) {
126 free(mt);
127 return NULL;
128 }
129
130 return mt;
131 }
132
133 struct intel_mipmap_tree *
134 intel_miptree_create_for_region(struct intel_context *intel,
135 GLenum target,
136 GLenum internal_format,
137 GLuint first_level,
138 GLuint last_level,
139 struct intel_region *region,
140 GLuint depth0,
141 GLuint compress_byte)
142 {
143 struct intel_mipmap_tree *mt;
144
145 mt = intel_miptree_create_internal(intel, target, internal_format,
146 first_level, last_level,
147 region->width, region->height, 1,
148 region->cpp, compress_byte);
149 if (!mt)
150 return mt;
151 #if 0
152 if (mt->pitch != region->pitch) {
153 fprintf(stderr,
154 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
155 region->pitch, mt->pitch);
156 free(mt);
157 return NULL;
158 }
159 #else
160 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
161 * to texture works, but we don't need that for texturing from a
162 * pixmap. Just override it here. */
163 mt->pitch = region->pitch;
164 #endif
165
166 mt->region = region;
167
168 return mt;
169 }
170
171 /**
172 * intel_miptree_pitch_align:
173 *
174 * @intel: intel context pointer
175 *
176 * @mt: the miptree to compute pitch alignment for
177 *
178 * @pitch: the natural pitch value
179 *
180 * Given @pitch, compute a larger value which accounts for
181 * any necessary alignment required by the device
182 */
183
184 int intel_miptree_pitch_align (struct intel_context *intel,
185 struct intel_mipmap_tree *mt,
186 int pitch)
187 {
188 #ifdef I915
189 GLcontext *ctx = &intel->ctx;
190 #endif
191
192 if (!mt->compressed) {
193 int pitch_align;
194
195 if (intel->ttm) {
196 /* XXX: Align pitch to multiple of 64 bytes for now to allow
197 * render-to-texture to work in all cases. This should probably be
198 * replaced at some point by some scheme to only do this when really
199 * necessary.
200 */
201 pitch_align = 64;
202 } else {
203 pitch_align = 4;
204 }
205
206 pitch = ALIGN(pitch * mt->cpp, pitch_align);
207
208 #ifdef I915
209 /* XXX: At least the i915 seems very upset when the pitch is a multiple
210 * of 1024 and sometimes 512 bytes - performance can drop by several
211 * times. Go to the next multiple of the required alignment for now.
212 */
213 if (!(pitch & 511) &&
214 (pitch + pitch_align) < (1 << ctx->Const.MaxTextureLevels))
215 pitch += pitch_align;
216 #endif
217
218 pitch /= mt->cpp;
219 }
220 return pitch;
221 }
222
223 void
224 intel_miptree_reference(struct intel_mipmap_tree **dst,
225 struct intel_mipmap_tree *src)
226 {
227 src->refcount++;
228 *dst = src;
229 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
230 }
231
232 void
233 intel_miptree_release(struct intel_context *intel,
234 struct intel_mipmap_tree **mt)
235 {
236 if (!*mt)
237 return;
238
239 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
240 if (--(*mt)->refcount <= 0) {
241 GLuint i;
242
243 DBG("%s deleting %p\n", __FUNCTION__, *mt);
244
245 intel_region_release(&((*mt)->region));
246
247 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
248 if ((*mt)->level[i].image_offset)
249 free((*mt)->level[i].image_offset);
250
251 free(*mt);
252 }
253 *mt = NULL;
254 }
255
256
257
258
259 /* Can the image be pulled into a unified mipmap tree. This mirrors
260 * the completeness test in a lot of ways.
261 *
262 * Not sure whether I want to pass gl_texture_image here.
263 */
264 GLboolean
265 intel_miptree_match_image(struct intel_mipmap_tree *mt,
266 struct gl_texture_image *image,
267 GLuint face, GLuint level)
268 {
269 /* Images with borders are never pulled into mipmap trees.
270 */
271 if (image->Border ||
272 ((image->_BaseFormat == GL_DEPTH_COMPONENT) &&
273 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) ||
274 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER))))
275 return GL_FALSE;
276
277 if (image->InternalFormat != mt->internal_format ||
278 image->IsCompressed != mt->compressed)
279 return GL_FALSE;
280
281 if (!image->IsCompressed &&
282 !mt->compressed &&
283 image->TexFormat->TexelBytes != mt->cpp)
284 return GL_FALSE;
285
286 /* Test image dimensions against the base level image adjusted for
287 * minification. This will also catch images not present in the
288 * tree, changed targets, etc.
289 */
290 if (image->Width != mt->level[level].width ||
291 image->Height != mt->level[level].height ||
292 image->Depth != mt->level[level].depth)
293 return GL_FALSE;
294
295 return GL_TRUE;
296 }
297
298
299 void
300 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
301 GLuint level,
302 GLuint nr_images,
303 GLuint x, GLuint y,
304 GLuint w, GLuint h, GLuint d)
305 {
306 mt->level[level].width = w;
307 mt->level[level].height = h;
308 mt->level[level].depth = d;
309 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
310 mt->level[level].nr_images = nr_images;
311
312 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
313 level, w, h, d, x, y, mt->level[level].level_offset);
314
315 /* Not sure when this would happen, but anyway:
316 */
317 if (mt->level[level].image_offset) {
318 free(mt->level[level].image_offset);
319 mt->level[level].image_offset = NULL;
320 }
321
322 assert(nr_images);
323
324 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
325 mt->level[level].image_offset[0] = 0;
326 }
327
328
329
330 void
331 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
332 GLuint level, GLuint img,
333 GLuint x, GLuint y)
334 {
335 if (img == 0 && level == 0)
336 assert(x == 0 && y == 0);
337
338 assert(img < mt->level[level].nr_images);
339
340 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp;
341
342 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
343 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
344 }
345
346
347 /* Although we use the image_offset[] array to store relative offsets
348 * to cube faces, Mesa doesn't know anything about this and expects
349 * each cube face to be treated as a separate image.
350 *
351 * These functions present that view to mesa:
352 */
353 const GLuint *
354 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
355 {
356 static const GLuint zero = 0;
357
358 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
359 return &zero;
360 else
361 return mt->level[level].image_offset;
362 }
363
364
365 GLuint
366 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
367 GLuint face, GLuint level)
368 {
369 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
370 return (mt->level[level].level_offset +
371 mt->level[level].image_offset[face]);
372 else
373 return mt->level[level].level_offset;
374 }
375
376
377
378 /**
379 * Map a teximage in a mipmap tree.
380 * \param row_stride returns row stride in bytes
381 * \param image_stride returns image stride in bytes (for 3D textures).
382 * \param image_offsets pointer to array of pixel offsets from the returned
383 * pointer to each depth image
384 * \return address of mapping
385 */
386 GLubyte *
387 intel_miptree_image_map(struct intel_context * intel,
388 struct intel_mipmap_tree * mt,
389 GLuint face,
390 GLuint level,
391 GLuint * row_stride, GLuint * image_offsets)
392 {
393 DBG("%s \n", __FUNCTION__);
394
395 if (row_stride)
396 *row_stride = mt->pitch * mt->cpp;
397
398 if (mt->target == GL_TEXTURE_3D) {
399 int i;
400
401 for (i = 0; i < mt->level[level].depth; i++)
402 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp;
403 } else {
404 assert(mt->level[level].depth == 1);
405 assert(mt->target == GL_TEXTURE_CUBE_MAP ||
406 mt->level[level].image_offset[0] == 0);
407 image_offsets[0] = 0;
408 }
409
410 return (intel_region_map(intel, mt->region) +
411 intel_miptree_image_offset(mt, face, level));
412 }
413
414 void
415 intel_miptree_image_unmap(struct intel_context *intel,
416 struct intel_mipmap_tree *mt)
417 {
418 DBG("%s\n", __FUNCTION__);
419 intel_region_unmap(intel, mt->region);
420 }
421
422
423
424 /* Upload data for a particular image.
425 */
426 void
427 intel_miptree_image_data(struct intel_context *intel,
428 struct intel_mipmap_tree *dst,
429 GLuint face,
430 GLuint level,
431 void *src,
432 GLuint src_row_pitch,
433 GLuint src_image_pitch)
434 {
435 GLuint depth = dst->level[level].depth;
436 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
437 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
438 GLuint i;
439 GLuint height = 0;
440
441 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
442 for (i = 0; i < depth; i++) {
443 height = dst->level[level].height;
444 if(dst->compressed)
445 height = (height + 3) / 4;
446 intel_region_data(intel,
447 dst->region,
448 dst_offset + dst_depth_offset[i], /* dst_offset */
449 0, 0, /* dstx, dsty */
450 src,
451 src_row_pitch,
452 0, 0, /* source x, y */
453 dst->level[level].width, height); /* width, height */
454
455 src += src_image_pitch * dst->cpp;
456 }
457 }
458
459 extern GLuint intel_compressed_alignment(GLenum);
460 /* Copy mipmap image between trees
461 */
462 void
463 intel_miptree_image_copy(struct intel_context *intel,
464 struct intel_mipmap_tree *dst,
465 GLuint face, GLuint level,
466 struct intel_mipmap_tree *src)
467 {
468 GLuint width = src->level[level].width;
469 GLuint height = src->level[level].height;
470 GLuint depth = src->level[level].depth;
471 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
472 GLuint src_offset = intel_miptree_image_offset(src, face, level);
473 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
474 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
475 GLuint i;
476
477 if (dst->compressed) {
478 GLuint alignment = intel_compressed_alignment(dst->internal_format);
479 height = (height + 3) / 4;
480 width = ((width + alignment - 1) & ~(alignment - 1));
481 }
482
483 for (i = 0; i < depth; i++) {
484 intel_region_copy(intel,
485 dst->region, dst_offset + dst_depth_offset[i],
486 0,
487 0,
488 src->region, src_offset + src_depth_offset[i],
489 0, 0, width, height);
490 }
491
492 }