1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "intel_context.h"
29 #include "intel_mipmap_tree.h"
30 #include "intel_regions.h"
31 #include "intel_tex_layout.h"
32 #include "intel_chipset.h"
34 #include "brw_state.h"
36 #include "main/enums.h"
38 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
42 target_to_target(GLenum target
)
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
51 return GL_TEXTURE_CUBE_MAP_ARB
;
58 static struct intel_mipmap_tree
*
59 intel_miptree_create_internal(struct intel_context
*intel
,
61 GLenum internal_format
,
66 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
70 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
72 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
73 _mesa_lookup_enum_by_nr(target
),
74 _mesa_lookup_enum_by_nr(internal_format
),
75 first_level
, last_level
, mt
);
77 mt
->target
= target_to_target(target
);
78 mt
->internal_format
= internal_format
;
79 mt
->first_level
= first_level
;
80 mt
->last_level
= last_level
;
82 mt
->height0
= height0
;
84 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
85 mt
->compressed
= compress_byte
? 1 : 0;
90 if (IS_945(intel
->intelScreen
->deviceID
))
91 ok
= i945_miptree_layout(intel
, mt
, tiling
);
93 ok
= i915_miptree_layout(intel
, mt
, tiling
);
95 ok
= brw_miptree_layout(intel
, mt
, tiling
);
100 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
108 struct intel_mipmap_tree
*
109 intel_miptree_create(struct intel_context
*intel
,
112 GLenum internal_format
,
117 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
118 GLboolean expect_accelerated_upload
)
120 struct intel_mipmap_tree
*mt
;
123 if (intel
->use_texture_tiling
&& compress_byte
== 0 &&
124 intel
->intelScreen
->kernel_exec_fencing
) {
125 if (IS_965(intel
->intelScreen
->deviceID
) &&
126 (base_format
== GL_DEPTH_COMPONENT
||
127 base_format
== GL_DEPTH_STENCIL_EXT
))
128 tiling
= I915_TILING_Y
;
130 tiling
= I915_TILING_X
;
132 tiling
= I915_TILING_NONE
;
134 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
135 first_level
, last_level
, width0
,
136 height0
, depth0
, cpp
, compress_byte
,
139 * pitch == 0 || height == 0 indicates the null texture
141 if (!mt
|| !mt
->pitch
|| !mt
->total_height
) {
146 mt
->region
= intel_region_alloc(intel
,
152 expect_accelerated_upload
);
163 struct intel_mipmap_tree
*
164 intel_miptree_create_for_region(struct intel_context
*intel
,
166 GLenum internal_format
,
169 struct intel_region
*region
,
171 GLuint compress_byte
)
173 struct intel_mipmap_tree
*mt
;
175 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
176 first_level
, last_level
,
177 region
->width
, region
->height
, 1,
178 region
->cpp
, compress_byte
,
183 if (mt
->pitch
!= region
->pitch
) {
185 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
186 region
->pitch
, mt
->pitch
);
191 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
192 * to texture works, but we don't need that for texturing from a
193 * pixmap. Just override it here. */
194 mt
->pitch
= region
->pitch
;
197 intel_region_reference(&mt
->region
, region
);
204 * intel_miptree_pitch_align:
206 * @intel: intel context pointer
208 * @mt: the miptree to compute pitch alignment for
210 * @pitch: the natural pitch value
212 * Given @pitch, compute a larger value which accounts for
213 * any necessary alignment required by the device
215 int intel_miptree_pitch_align (struct intel_context
*intel
,
216 struct intel_mipmap_tree
*mt
,
221 GLcontext
*ctx
= &intel
->ctx
;
224 if (!mt
->compressed
) {
228 /* XXX: Align pitch to multiple of 64 bytes for now to allow
229 * render-to-texture to work in all cases. This should probably be
230 * replaced at some point by some scheme to only do this when really
238 if (tiling
== I915_TILING_X
)
240 else if (tiling
== I915_TILING_Y
)
243 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
246 /* XXX: At least the i915 seems very upset when the pitch is a multiple
247 * of 1024 and sometimes 512 bytes - performance can drop by several
248 * times. Go to the next multiple of the required alignment for now.
250 if (!(pitch
& 511) &&
251 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
252 pitch
+= pitch_align
;
262 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
263 struct intel_mipmap_tree
*src
)
267 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
272 intel_miptree_release(struct intel_context
*intel
,
273 struct intel_mipmap_tree
**mt
)
278 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
279 if (--(*mt
)->refcount
<= 0) {
282 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
285 /* Free up cached binding tables holding a reference on our buffer, to
286 * avoid excessive memory consumption.
288 * This isn't as aggressive as we could be, as we'd like to do
289 * it from any time we free the last ref on a region. But intel_region.c
290 * is context-agnostic. Perhaps our constant state cache should be, as
293 brw_state_cache_bo_delete(&brw_context(&intel
->ctx
)->surface_cache
,
294 (*mt
)->region
->buffer
);
297 intel_region_release(&((*mt
)->region
));
299 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
300 free((*mt
)->level
[i
].x_offset
);
301 free((*mt
)->level
[i
].y_offset
);
311 * Can the image be pulled into a unified mipmap tree? This mirrors
312 * the completeness test in a lot of ways.
314 * Not sure whether I want to pass gl_texture_image here.
317 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
318 struct gl_texture_image
*image
,
319 GLuint face
, GLuint level
)
321 GLboolean isCompressed
= _mesa_is_format_compressed(image
->TexFormat
);
323 /* Images with borders are never pulled into mipmap trees.
326 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
327 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
328 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
331 if (image
->InternalFormat
!= mt
->internal_format
||
332 isCompressed
!= mt
->compressed
)
337 _mesa_get_format_bytes(image
->TexFormat
) != mt
->cpp
)
340 /* Test image dimensions against the base level image adjusted for
341 * minification. This will also catch images not present in the
342 * tree, changed targets, etc.
344 if (image
->Width
!= mt
->level
[level
].width
||
345 image
->Height
!= mt
->level
[level
].height
||
346 image
->Depth
!= mt
->level
[level
].depth
)
354 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
358 GLuint w
, GLuint h
, GLuint d
)
360 mt
->level
[level
].width
= w
;
361 mt
->level
[level
].height
= h
;
362 mt
->level
[level
].depth
= d
;
363 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
364 mt
->level
[level
].level_x
= x
;
365 mt
->level
[level
].level_y
= y
;
366 mt
->level
[level
].nr_images
= nr_images
;
368 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
369 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
372 assert(!mt
->level
[level
].x_offset
);
374 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
375 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
376 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
377 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
382 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
383 GLuint level
, GLuint img
,
386 if (img
== 0 && level
== 0)
387 assert(x
== 0 && y
== 0);
389 assert(img
< mt
->level
[level
].nr_images
);
391 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
392 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
394 DBG("%s level %d img %d pos %d,%d\n",
395 __FUNCTION__
, level
, img
,
396 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
401 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
402 GLuint level
, GLuint face
, GLuint depth
,
403 GLuint
*x
, GLuint
*y
)
405 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
) {
406 *x
= mt
->level
[level
].x_offset
[face
];
407 *y
= mt
->level
[level
].y_offset
[face
];
408 } else if (mt
->target
== GL_TEXTURE_3D
) {
409 *x
= mt
->level
[level
].x_offset
[depth
];
410 *y
= mt
->level
[level
].y_offset
[depth
];
412 *x
= mt
->level
[level
].x_offset
[0];
413 *y
= mt
->level
[level
].y_offset
[0];
418 * Map a teximage in a mipmap tree.
419 * \param row_stride returns row stride in bytes
420 * \param image_stride returns image stride in bytes (for 3D textures).
421 * \param image_offsets pointer to array of pixel offsets from the returned
422 * pointer to each depth image
423 * \return address of mapping
426 intel_miptree_image_map(struct intel_context
* intel
,
427 struct intel_mipmap_tree
* mt
,
430 GLuint
* row_stride
, GLuint
* image_offsets
)
433 DBG("%s \n", __FUNCTION__
);
436 *row_stride
= mt
->pitch
* mt
->cpp
;
438 if (mt
->target
== GL_TEXTURE_3D
) {
441 for (i
= 0; i
< mt
->level
[level
].depth
; i
++) {
443 intel_miptree_get_image_offset(mt
, level
, face
, i
,
445 image_offsets
[i
] = x
+ y
* mt
->pitch
;
448 return intel_region_map(intel
, mt
->region
);
450 assert(mt
->level
[level
].depth
== 1);
451 intel_miptree_get_image_offset(mt
, level
, face
, 0,
453 image_offsets
[0] = 0;
455 return intel_region_map(intel
, mt
->region
) +
456 (x
+ y
* mt
->pitch
) * mt
->cpp
;
462 intel_miptree_image_unmap(struct intel_context
*intel
,
463 struct intel_mipmap_tree
*mt
)
465 DBG("%s\n", __FUNCTION__
);
466 intel_region_unmap(intel
, mt
->region
);
471 * Upload data for a particular image.
474 intel_miptree_image_data(struct intel_context
*intel
,
475 struct intel_mipmap_tree
*dst
,
479 GLuint src_row_pitch
,
480 GLuint src_image_pitch
)
482 const GLuint depth
= dst
->level
[level
].depth
;
485 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
486 for (i
= 0; i
< depth
; i
++) {
487 GLuint dst_x
, dst_y
, height
;
489 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
491 height
= dst
->level
[level
].height
;
493 height
= (height
+ 3) / 4;
495 intel_region_data(intel
,
496 dst
->region
, 0, dst_x
, dst_y
,
499 0, 0, /* source x, y */
500 dst
->level
[level
].width
, height
); /* width, height */
502 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
508 * Copy mipmap image between trees
511 intel_miptree_image_copy(struct intel_context
*intel
,
512 struct intel_mipmap_tree
*dst
,
513 GLuint face
, GLuint level
,
514 struct intel_mipmap_tree
*src
)
516 GLuint width
= src
->level
[level
].width
;
517 GLuint height
= src
->level
[level
].height
;
518 GLuint depth
= src
->level
[level
].depth
;
519 GLuint src_x
, src_y
, dst_x
, dst_y
;
523 if (dst
->compressed
) {
524 GLuint align_w
, align_h
;
526 intel_get_texture_alignment_unit(dst
->internal_format
,
528 height
= (height
+ 3) / 4;
529 width
= ALIGN(width
, align_w
);
532 for (i
= 0; i
< depth
; i
++) {
533 intel_miptree_get_image_offset(src
, level
, face
, i
, &src_x
, &src_y
);
534 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
535 success
= intel_region_copy(intel
,
536 dst
->region
, 0, dst_x
, dst_y
,
537 src
->region
, 0, src_x
, src_y
, width
, height
,
540 GLubyte
*src_ptr
, *dst_ptr
;
542 src_ptr
= intel_region_map(intel
, src
->region
);
543 dst_ptr
= intel_region_map(intel
, dst
->region
);
545 _mesa_copy_rect(dst_ptr
+ dst
->cpp
* (dst_x
+ dst_y
* dst
->pitch
),
549 src_ptr
+ src
->cpp
* (src_x
+ src_y
* src
->pitch
),
552 intel_region_unmap(intel
, src
->region
);
553 intel_region_unmap(intel
, dst
->region
);