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