1b645c7262101611d8a9a2f2595a7081713fde64
[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 ((image->_BaseFormat == GL_DEPTH_COMPONENT) &&
270 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) ||
271 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER))))
272 return GL_FALSE;
273
274 if (image->InternalFormat != mt->internal_format ||
275 image->IsCompressed != mt->compressed)
276 return GL_FALSE;
277
278 if (!image->IsCompressed &&
279 !mt->compressed &&
280 image->TexFormat->TexelBytes != mt->cpp)
281 return GL_FALSE;
282
283 /* Test image dimensions against the base level image adjusted for
284 * minification. This will also catch images not present in the
285 * tree, changed targets, etc.
286 */
287 if (image->Width != mt->level[level].width ||
288 image->Height != mt->level[level].height ||
289 image->Depth != mt->level[level].depth)
290 return GL_FALSE;
291
292 return GL_TRUE;
293 }
294
295
296 void
297 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
298 GLuint level,
299 GLuint nr_images,
300 GLuint x, GLuint y,
301 GLuint w, GLuint h, GLuint d)
302 {
303 mt->level[level].width = w;
304 mt->level[level].height = h;
305 mt->level[level].depth = d;
306 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
307 mt->level[level].nr_images = nr_images;
308
309 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
310 level, w, h, d, x, y, mt->level[level].level_offset);
311
312 /* Not sure when this would happen, but anyway:
313 */
314 if (mt->level[level].image_offset) {
315 free(mt->level[level].image_offset);
316 mt->level[level].image_offset = NULL;
317 }
318
319 assert(nr_images);
320
321 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
322 mt->level[level].image_offset[0] = 0;
323 }
324
325
326
327 void
328 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
329 GLuint level, GLuint img,
330 GLuint x, GLuint y)
331 {
332 if (img == 0 && level == 0)
333 assert(x == 0 && y == 0);
334
335 assert(img < mt->level[level].nr_images);
336
337 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp;
338
339 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
340 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
341 }
342
343
344 /* Although we use the image_offset[] array to store relative offsets
345 * to cube faces, Mesa doesn't know anything about this and expects
346 * each cube face to be treated as a separate image.
347 *
348 * These functions present that view to mesa:
349 */
350 const GLuint *
351 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
352 {
353 static const GLuint zero = 0;
354
355 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
356 return &zero;
357 else
358 return mt->level[level].image_offset;
359 }
360
361
362 GLuint
363 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
364 GLuint face, GLuint level)
365 {
366 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
367 return (mt->level[level].level_offset +
368 mt->level[level].image_offset[face]);
369 else
370 return mt->level[level].level_offset;
371 }
372
373
374
375 /**
376 * Map a teximage in a mipmap tree.
377 * \param row_stride returns row stride in bytes
378 * \param image_stride returns image stride in bytes (for 3D textures).
379 * \param image_offsets pointer to array of pixel offsets from the returned
380 * pointer to each depth image
381 * \return address of mapping
382 */
383 GLubyte *
384 intel_miptree_image_map(struct intel_context * intel,
385 struct intel_mipmap_tree * mt,
386 GLuint face,
387 GLuint level,
388 GLuint * row_stride, GLuint * image_offsets)
389 {
390 DBG("%s \n", __FUNCTION__);
391
392 if (row_stride)
393 *row_stride = mt->pitch * mt->cpp;
394
395 if (mt->target == GL_TEXTURE_3D) {
396 int i;
397
398 for (i = 0; i < mt->level[level].depth; i++)
399 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp;
400 } else {
401 assert(mt->level[level].depth == 1);
402 assert(mt->target == GL_TEXTURE_CUBE_MAP ||
403 mt->level[level].image_offset[0] == 0);
404 image_offsets[0] = 0;
405 }
406
407 return (intel_region_map(intel, mt->region) +
408 intel_miptree_image_offset(mt, face, level));
409 }
410
411 void
412 intel_miptree_image_unmap(struct intel_context *intel,
413 struct intel_mipmap_tree *mt)
414 {
415 DBG("%s\n", __FUNCTION__);
416 intel_region_unmap(intel, mt->region);
417 }
418
419
420
421 /* Upload data for a particular image.
422 */
423 void
424 intel_miptree_image_data(struct intel_context *intel,
425 struct intel_mipmap_tree *dst,
426 GLuint face,
427 GLuint level,
428 void *src,
429 GLuint src_row_pitch,
430 GLuint src_image_pitch)
431 {
432 GLuint depth = dst->level[level].depth;
433 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
434 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
435 GLuint i;
436 GLuint height = 0;
437
438 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
439 for (i = 0; i < depth; i++) {
440 height = dst->level[level].height;
441 if(dst->compressed)
442 height = (height + 3) / 4;
443 intel_region_data(intel,
444 dst->region,
445 dst_offset + dst_depth_offset[i] * dst->cpp, /* dst_offset */
446 0, 0, /* dstx, dsty */
447 src,
448 src_row_pitch,
449 0, 0, /* source x, y */
450 dst->level[level].width, height); /* width, height */
451
452 src += src_image_pitch * dst->cpp;
453 }
454 }
455
456 extern GLuint intel_compressed_alignment(GLenum);
457 /* Copy mipmap image between trees
458 */
459 void
460 intel_miptree_image_copy(struct intel_context *intel,
461 struct intel_mipmap_tree *dst,
462 GLuint face, GLuint level,
463 struct intel_mipmap_tree *src)
464 {
465 GLuint width = src->level[level].width;
466 GLuint height = src->level[level].height;
467 GLuint depth = src->level[level].depth;
468 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
469 GLuint src_offset = intel_miptree_image_offset(src, face, level);
470 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
471 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
472 GLuint i;
473
474 if (dst->compressed) {
475 GLuint alignment = intel_compressed_alignment(dst->internal_format);
476 height = (height + 3) / 4;
477 width = ((width + alignment - 1) & ~(alignment - 1));
478 }
479
480 for (i = 0; i < depth; i++) {
481 intel_region_copy(intel,
482 dst->region, dst_offset + dst_depth_offset[i] * dst->cpp,
483 0,
484 0,
485 src->region, src_offset + src_depth_offset[i] * src->cpp,
486 0, 0, width, height);
487 }
488
489 }