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