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
)
144 mt
->region
= intel_region_alloc(intel
,
150 expect_accelerated_upload
);
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 if (mt
->pitch
!= region
->pitch
) {
183 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
184 region
->pitch
, mt
->pitch
);
189 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
190 * to texture works, but we don't need that for texturing from a
191 * pixmap. Just override it here. */
192 mt
->pitch
= region
->pitch
;
195 intel_region_reference(&mt
->region
, region
);
202 * intel_miptree_pitch_align:
204 * @intel: intel context pointer
206 * @mt: the miptree to compute pitch alignment for
208 * @pitch: the natural pitch value
210 * Given @pitch, compute a larger value which accounts for
211 * any necessary alignment required by the device
213 int intel_miptree_pitch_align (struct intel_context
*intel
,
214 struct intel_mipmap_tree
*mt
,
219 GLcontext
*ctx
= &intel
->ctx
;
222 if (!mt
->compressed
) {
226 /* XXX: Align pitch to multiple of 64 bytes for now to allow
227 * render-to-texture to work in all cases. This should probably be
228 * replaced at some point by some scheme to only do this when really
236 if (tiling
== I915_TILING_X
)
238 else if (tiling
== I915_TILING_Y
)
241 pitch
= ALIGN(pitch
* mt
->cpp
, pitch_align
);
244 /* XXX: At least the i915 seems very upset when the pitch is a multiple
245 * of 1024 and sometimes 512 bytes - performance can drop by several
246 * times. Go to the next multiple of the required alignment for now.
248 if (!(pitch
& 511) &&
249 (pitch
+ pitch_align
) < (1 << ctx
->Const
.MaxTextureLevels
))
250 pitch
+= pitch_align
;
260 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
261 struct intel_mipmap_tree
*src
)
265 DBG("%s %p refcount now %d\n", __FUNCTION__
, src
, src
->refcount
);
270 intel_miptree_release(struct intel_context
*intel
,
271 struct intel_mipmap_tree
**mt
)
276 DBG("%s %p refcount will be %d\n", __FUNCTION__
, *mt
, (*mt
)->refcount
- 1);
277 if (--(*mt
)->refcount
<= 0) {
280 DBG("%s deleting %p\n", __FUNCTION__
, *mt
);
283 /* Free up cached binding tables holding a reference on our buffer, to
284 * avoid excessive memory consumption.
286 * This isn't as aggressive as we could be, as we'd like to do
287 * it from any time we free the last ref on a region. But intel_region.c
288 * is context-agnostic. Perhaps our constant state cache should be, as
291 brw_state_cache_bo_delete(&brw_context(&intel
->ctx
)->surface_cache
,
292 (*mt
)->region
->buffer
);
295 intel_region_release(&((*mt
)->region
));
297 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
298 if ((*mt
)->level
[i
].image_offset
)
299 free((*mt
)->level
[i
].image_offset
);
308 * Can the image be pulled into a unified mipmap tree? This mirrors
309 * the completeness test in a lot of ways.
311 * Not sure whether I want to pass gl_texture_image here.
314 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
315 struct gl_texture_image
*image
,
316 GLuint face
, GLuint level
)
318 /* Images with borders are never pulled into mipmap trees.
321 ((image
->_BaseFormat
== GL_DEPTH_COMPONENT
) &&
322 ((image
->TexObject
->WrapS
== GL_CLAMP_TO_BORDER
) ||
323 (image
->TexObject
->WrapT
== GL_CLAMP_TO_BORDER
))))
326 if (image
->InternalFormat
!= mt
->internal_format
||
327 image
->IsCompressed
!= mt
->compressed
)
330 if (!image
->IsCompressed
&&
332 image
->TexFormat
->TexelBytes
!= mt
->cpp
)
335 /* Test image dimensions against the base level image adjusted for
336 * minification. This will also catch images not present in the
337 * tree, changed targets, etc.
339 if (image
->Width
!= mt
->level
[level
].width
||
340 image
->Height
!= mt
->level
[level
].height
||
341 image
->Depth
!= mt
->level
[level
].depth
)
349 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
353 GLuint w
, GLuint h
, GLuint d
)
355 mt
->level
[level
].width
= w
;
356 mt
->level
[level
].height
= h
;
357 mt
->level
[level
].depth
= d
;
358 mt
->level
[level
].level_offset
= (x
+ y
* mt
->pitch
) * mt
->cpp
;
359 mt
->level
[level
].nr_images
= nr_images
;
361 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
362 level
, w
, h
, d
, x
, y
, mt
->level
[level
].level_offset
);
364 /* Not sure when this would happen, but anyway:
366 if (mt
->level
[level
].image_offset
) {
367 free(mt
->level
[level
].image_offset
);
368 mt
->level
[level
].image_offset
= NULL
;
373 mt
->level
[level
].image_offset
= malloc(nr_images
* sizeof(GLuint
));
374 mt
->level
[level
].image_offset
[0] = 0;
379 intel_miptree_set_image_offset_ex(struct intel_mipmap_tree
*mt
,
380 GLuint level
, GLuint img
,
384 if (img
== 0 && level
== 0)
385 assert(x
== 0 && y
== 0);
387 assert(img
< mt
->level
[level
].nr_images
);
389 mt
->level
[level
].image_offset
[img
] = (x
+ y
* mt
->pitch
) * mt
->cpp
+ offset
;
391 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
392 __FUNCTION__
, level
, img
, x
, y
, mt
->level
[level
].image_offset
[img
]);
397 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
398 GLuint level
, GLuint img
,
401 intel_miptree_set_image_offset_ex(mt
, level
, img
, x
, y
, 0);
406 * Return offset to the start of a 2D slice of a texture (a mipmap level,
407 * cube face, 3D Z slice).
408 * \param mt the texture object/miptree
409 * \param face cube map face in [0,5] or zero for non-cube textures
410 * \param level mipmap level
411 * \param zslice Z slice of a 3D texture, or zero for non-3D textures
414 intel_miptree_image_offset(const struct intel_mipmap_tree
*mt
,
415 GLuint face
, GLuint level
, GLuint zslice
)
417 GLuint offset
= mt
->level
[level
].level_offset
;
419 if (mt
->target
== GL_TEXTURE_CUBE_MAP_ARB
)
420 offset
+= mt
->level
[level
].image_offset
[face
];
421 else if (mt
->target
== GL_TEXTURE_3D
)
422 offset
+= mt
->level
[level
].image_offset
[zslice
];
429 * Map a teximage in a mipmap tree.
430 * \param row_stride returns row stride in bytes
431 * \param image_stride returns image stride in bytes (for 3D textures).
432 * \param image_offsets pointer to array of pixel offsets from the returned
433 * pointer to each depth image
434 * \return address of mapping
437 intel_miptree_image_map(struct intel_context
* intel
,
438 struct intel_mipmap_tree
* mt
,
441 GLuint
* row_stride
, GLuint
* image_offsets
)
443 DBG("%s \n", __FUNCTION__
);
446 *row_stride
= mt
->pitch
* mt
->cpp
;
448 if (mt
->target
== GL_TEXTURE_3D
) {
451 for (i
= 0; i
< mt
->level
[level
].depth
; i
++)
452 image_offsets
[i
] = mt
->level
[level
].image_offset
[i
] / mt
->cpp
;
454 assert(mt
->level
[level
].depth
== 1);
455 assert(mt
->target
== GL_TEXTURE_CUBE_MAP
||
456 mt
->level
[level
].image_offset
[0] == 0);
457 image_offsets
[0] = 0;
460 return (intel_region_map(intel
, mt
->region
) +
461 intel_miptree_image_offset(mt
, face
, level
, 0));
466 intel_miptree_image_unmap(struct intel_context
*intel
,
467 struct intel_mipmap_tree
*mt
)
469 DBG("%s\n", __FUNCTION__
);
470 intel_region_unmap(intel
, mt
->region
);
475 * Upload data for a particular image.
478 intel_miptree_image_data(struct intel_context
*intel
,
479 struct intel_mipmap_tree
*dst
,
483 GLuint src_row_pitch
,
484 GLuint src_image_pitch
)
486 const GLuint depth
= dst
->level
[level
].depth
;
489 DBG("%s: %d/%d\n", __FUNCTION__
, face
, level
);
490 for (i
= 0; i
< depth
; i
++) {
491 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
, i
);
492 GLuint height
= dst
->level
[level
].height
;
495 height
= (height
+ 3) / 4;
497 intel_region_data(intel
,
500 0, 0, /* dstx, dsty */
503 0, 0, /* source x, y */
504 dst
->level
[level
].width
, height
); /* width, height */
506 src
= (char *)src
+ src_image_pitch
* dst
->cpp
;
512 * Copy mipmap image between trees
515 intel_miptree_image_copy(struct intel_context
*intel
,
516 struct intel_mipmap_tree
*dst
,
517 GLuint face
, GLuint level
,
518 struct intel_mipmap_tree
*src
)
520 GLuint width
= src
->level
[level
].width
;
521 GLuint height
= src
->level
[level
].height
;
522 GLuint depth
= src
->level
[level
].depth
;
526 if (dst
->compressed
) {
527 GLuint align_w
, align_h
;
529 intel_get_texture_alignment_unit(dst
->internal_format
,
531 height
= (height
+ 3) / 4;
532 width
= ALIGN(width
, align_w
);
535 for (i
= 0; i
< depth
; i
++) {
536 GLuint dst_offset
= intel_miptree_image_offset(dst
, face
, level
, i
);
537 GLuint src_offset
= intel_miptree_image_offset(src
, face
, level
, i
);
539 success
= intel_region_copy(intel
,
540 dst
->region
, dst_offset
,
542 src
->region
, src_offset
,
543 0, 0, width
, height
, GL_COPY
);
545 GLubyte
*src_ptr
, *dst_ptr
;
547 src_ptr
= intel_region_map(intel
, src
->region
);
548 dst_ptr
= intel_region_map(intel
, dst
->region
);
550 _mesa_copy_rect(dst_ptr
+ dst_offset
,
554 src_ptr
+ src_offset
,
557 intel_region_unmap(intel
, src
->region
);
558 intel_region_unmap(intel
, dst
->region
);