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