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"
33 #include "brw_state.h"
35 #include "main/enums.h"
37 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
41 target_to_target(GLenum target
)
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
46 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
47 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
48 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
49 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
50 return GL_TEXTURE_CUBE_MAP_ARB
;
57 static struct intel_mipmap_tree
*
58 intel_miptree_create_internal(struct intel_context
*intel
,
60 GLenum internal_format
,
65 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
69 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
71 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__
,
72 _mesa_lookup_enum_by_nr(target
),
73 _mesa_lookup_enum_by_nr(internal_format
),
74 first_level
, last_level
, mt
);
76 mt
->target
= target_to_target(target
);
77 mt
->internal_format
= internal_format
;
78 mt
->first_level
= first_level
;
79 mt
->last_level
= last_level
;
81 mt
->height0
= height0
;
83 mt
->cpp
= compress_byte
? compress_byte
: cpp
;
84 mt
->compressed
= compress_byte
? 1 : 0;
90 ok
= i945_miptree_layout(intel
, mt
, tiling
);
92 ok
= i915_miptree_layout(intel
, mt
, tiling
);
94 ok
= brw_miptree_layout(intel
, mt
, tiling
);
99 DBG("%s not okay - returning NULL\n", __FUNCTION__
);
107 struct intel_mipmap_tree
*
108 intel_miptree_create(struct intel_context
*intel
,
111 GLenum internal_format
,
116 GLuint depth0
, GLuint cpp
, GLuint compress_byte
,
117 GLboolean expect_accelerated_upload
)
119 struct intel_mipmap_tree
*mt
;
122 if (intel
->use_texture_tiling
&& compress_byte
== 0) {
123 if (intel
->gen
>= 4 &&
124 (base_format
== GL_DEPTH_COMPONENT
||
125 base_format
== GL_DEPTH_STENCIL_EXT
))
126 tiling
= I915_TILING_Y
;
128 tiling
= I915_TILING_X
;
130 tiling
= I915_TILING_NONE
;
132 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
133 first_level
, last_level
, width0
,
134 height0
, depth0
, cpp
, compress_byte
,
137 * pitch == 0 || height == 0 indicates the null texture
139 if (!mt
|| !mt
->pitch
|| !mt
->total_height
) {
144 mt
->region
= intel_region_alloc(intel
,
149 expect_accelerated_upload
);
150 mt
->pitch
= mt
->region
->pitch
;
161 struct intel_mipmap_tree
*
162 intel_miptree_create_for_region(struct intel_context
*intel
,
164 GLenum internal_format
,
167 struct intel_region
*region
,
169 GLuint compress_byte
)
171 struct intel_mipmap_tree
*mt
;
173 mt
= intel_miptree_create_internal(intel
, target
, internal_format
,
174 first_level
, last_level
,
175 region
->width
, region
->height
, 1,
176 region
->cpp
, compress_byte
,
181 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
182 * to texture works, but we don't need that for texturing from a
183 * pixmap. Just override it here. */
184 mt
->pitch
= region
->pitch
;
186 intel_region_reference(&mt
->region
, region
);
193 * intel_miptree_pitch_align:
195 * @intel: intel context pointer
197 * @mt: the miptree to compute pitch alignment for
199 * @pitch: the natural pitch value
201 * Given @pitch, compute a larger value which accounts for
202 * any necessary alignment required by the device
204 int intel_miptree_pitch_align (struct intel_context
*intel
,
205 struct intel_mipmap_tree
*mt
,
210 GLcontext
*ctx
= &intel
->ctx
;
213 if (!mt
->compressed
) {
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
223 if (tiling
== I915_TILING_X
)
225 else if (tiling
== I915_TILING_Y
)
228 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
231 /* Do a little adjustment to linear allocations so that we avoid
232 * hitting the same channel of memory for 2 different pages when
233 * reading a 2x2 subspan or doing bilinear filtering.
235 if (tiling
== I915_TILING_NONE
&& !(pitch
& 511) &&
236 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
237 pitch
+= pitch_align
;
247 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
248 struct intel_mipmap_tree
*src
)
252 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
257 intel_miptree_release(struct intel_context
*intel
,
258 struct intel_mipmap_tree
**mt
)
263 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
264 if (--(*mt
)->refcount
<= 0) {
267 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
270 /* Free up cached binding tables holding a reference on our buffer, to
271 * avoid excessive memory consumption.
273 * This isn't as aggressive as we could be, as we'd like to do
274 * it from any time we free the last ref on a region. But intel_region.c
275 * is context-agnostic. Perhaps our constant state cache should be, as
278 brw_state_cache_bo_delete(&brw_context(&intel
->ctx
)->surface_cache
,
279 (*mt
)->region
->buffer
);
282 intel_region_release(&((*mt
)->region
));
284 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
285 free((*mt
)->level
[i
].x_offset
);
286 free((*mt
)->level
[i
].y_offset
);
296 * Can the image be pulled into a unified mipmap tree? This mirrors
297 * the completeness test in a lot of ways.
299 * Not sure whether I want to pass gl_texture_image here.
302 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
303 struct gl_texture_image
*image
)
305 GLboolean isCompressed
= _mesa_is_format_compressed(image
->TexFormat
);
306 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
307 GLuint level
= intelImage
->level
;
309 /* Images with borders are never pulled into mipmap trees. */
313 if (image
->InternalFormat
!= mt
->internal_format
||
314 isCompressed
!= mt
->compressed
)
319 _mesa_get_format_bytes(image
->TexFormat
) != mt
->cpp
)
322 /* Test image dimensions against the base level image adjusted for
323 * minification. This will also catch images not present in the
324 * tree, changed targets, etc.
326 if (image
->Width
!= mt
->level
[level
].width
||
327 image
->Height
!= mt
->level
[level
].height
||
328 image
->Depth
!= mt
->level
[level
].depth
)
336 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
340 GLuint w
, GLuint h
, GLuint d
)
342 mt
->level
[level
].width
= w
;
343 mt
->level
[level
].height
= h
;
344 mt
->level
[level
].depth
= d
;
345 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
346 mt
->level
[level
].level_x
= x
;
347 mt
->level
[level
].level_y
= y
;
348 mt
->level
[level
].nr_images
= nr_images
;
350 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
351 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
354 assert(!mt
->level
[level
].x_offset
);
356 mt
->level
[level
].x_offset
= malloc(nr_images
* sizeof(GLuint
));
357 mt
->level
[level
].x_offset
[0] = mt
->level
[level
].level_x
;
358 mt
->level
[level
].y_offset
= malloc(nr_images
* sizeof(GLuint
));
359 mt
->level
[level
].y_offset
[0] = mt
->level
[level
].level_y
;
364 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
365 GLuint level
, GLuint img
,
368 if (img
== 0 && level
== 0)
369 assert(x
== 0 && y
== 0);
371 assert(img
< mt
->level
[level
].nr_images
);
373 mt
->level
[level
].x_offset
[img
] = mt
->level
[level
].level_x
+ x
;
374 mt
->level
[level
].y_offset
[img
] = mt
->level
[level
].level_y
+ y
;
376 DBG("%s level %d img %d pos %d,%d\n",
377 __FUNCTION__
, level
, img
,
378 mt
->level
[level
].x_offset
[img
], mt
->level
[level
].y_offset
[img
]);
383 intel_miptree_get_image_offset(struct intel_mipmap_tree
*mt
,
384 GLuint level
, GLuint face
, GLuint depth
,
385 GLuint
*x
, GLuint
*y
)
387 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
) {
388 *x
= mt
->level
[level
].x_offset
[face
];
389 *y
= mt
->level
[level
].y_offset
[face
];
390 } else if (mt
->target
== GL_TEXTURE_3D
) {
391 *x
= mt
->level
[level
].x_offset
[depth
];
392 *y
= mt
->level
[level
].y_offset
[depth
];
394 *x
= mt
->level
[level
].x_offset
[0];
395 *y
= mt
->level
[level
].y_offset
[0];
400 * Map a teximage in a mipmap tree.
401 * \param row_stride returns row stride in bytes
402 * \param image_stride returns image stride in bytes (for 3D textures).
403 * \param image_offsets pointer to array of pixel offsets from the returned
404 * pointer to each depth image
405 * \return address of mapping
408 intel_miptree_image_map(struct intel_context
* intel
,
409 struct intel_mipmap_tree
* mt
,
412 GLuint
* row_stride
, GLuint
* image_offsets
)
415 DBG("%s \n", __FUNCTION__
);
418 *row_stride
= mt
->pitch
* mt
->cpp
;
420 if (mt
->target
== GL_TEXTURE_3D
) {
423 for (i
= 0; i
< mt
->level
[level
].depth
; i
++) {
425 intel_miptree_get_image_offset(mt
, level
, face
, i
,
427 image_offsets
[i
] = x
+ y
* mt
->pitch
;
430 return intel_region_map(intel
, mt
->region
);
432 assert(mt
->level
[level
].depth
== 1);
433 intel_miptree_get_image_offset(mt
, level
, face
, 0,
435 image_offsets
[0] = 0;
437 return intel_region_map(intel
, mt
->region
) +
438 (x
+ y
* mt
->pitch
) * mt
->cpp
;
444 intel_miptree_image_unmap(struct intel_context
*intel
,
445 struct intel_mipmap_tree
*mt
)
447 DBG("%s\n", __FUNCTION__
);
448 intel_region_unmap(intel
, mt
->region
);
453 * Upload data for a particular image.
456 intel_miptree_image_data(struct intel_context
*intel
,
457 struct intel_mipmap_tree
*dst
,
461 GLuint src_row_pitch
,
462 GLuint src_image_pitch
)
464 const GLuint depth
= dst
->level
[level
].depth
;
467 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
468 for (i
= 0; i
< depth
; i
++) {
469 GLuint dst_x
, dst_y
, height
;
471 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
473 height
= dst
->level
[level
].height
;
475 height
= (height
+ 3) / 4;
477 intel_region_data(intel
,
478 dst
->region
, 0, dst_x
, dst_y
,
481 0, 0, /* source x, y */
482 dst
->level
[level
].width
, height
); /* width, height */
484 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
490 * Copy mipmap image between trees
493 intel_miptree_image_copy(struct intel_context
*intel
,
494 struct intel_mipmap_tree
*dst
,
495 GLuint face
, GLuint level
,
496 struct intel_mipmap_tree
*src
)
498 GLuint width
= src
->level
[level
].width
;
499 GLuint height
= src
->level
[level
].height
;
500 GLuint depth
= src
->level
[level
].depth
;
501 GLuint src_x
, src_y
, dst_x
, dst_y
;
505 if (dst
->compressed
) {
506 GLuint align_w
, align_h
;
508 intel_get_texture_alignment_unit(dst
->internal_format
,
510 height
= (height
+ 3) / 4;
511 width
= ALIGN(width
, align_w
);
514 intel_prepare_render(intel
);
516 for (i
= 0; i
< depth
; i
++) {
517 intel_miptree_get_image_offset(src
, level
, face
, i
, &src_x
, &src_y
);
518 intel_miptree_get_image_offset(dst
, level
, face
, i
, &dst_x
, &dst_y
);
519 success
= intel_region_copy(intel
,
520 dst
->region
, 0, dst_x
, dst_y
,
521 src
->region
, 0, src_x
, src_y
, width
, height
,
524 GLubyte
*src_ptr
, *dst_ptr
;
526 src_ptr
= intel_region_map(intel
, src
->region
);
527 dst_ptr
= intel_region_map(intel
, dst
->region
);
529 _mesa_copy_rect(dst_ptr
+ dst
->cpp
* (dst_x
+ dst_y
* dst
->pitch
),
533 src_ptr
+ src
->cpp
* (src_x
+ src_y
* src
->pitch
),
536 intel_region_unmap(intel
, src
->region
);
537 intel_region_unmap(intel
, dst
->region
);