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