天天看点

使用 PVRTC 压缩格式创建纹理 Creating textures in the PVRTC compression format Creating textures in the PVRTC compression format

转自:https://developer.apple.com/library/ios/qa/qa1611/_index.html

Technical Q&A QA1611

A: How do I create textures in the PVRTC compression format?

<a target="_blank"></a>

The iPhone SDK includes a tool that allows you to create textures in the PVRTC compression format, aptly named <code>texturetool</code>. If you have Xcode installed with the iPhone OS 2.2

SDK in the default location (<code>/Developer/Platforms/</code>), then <code>texturetool</code> is located at: <code>/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool</code>.

<code>texturetool</code> allows you to create four variants of PVRTC data, the primary difference being tradeoffs between quality and size. You will have to experiment with these variants

to determine which setting is the best compromise for each individual texture image.

Important: If you are using PVRTexTool, then you must create textures that are square and a power of two in length. The results of using non-square or non-power-of-two PVRTC

texture data on iPhone OS are undefined.

The parameters that may be passed to <code>texturetool</code> and their function is presented in the rest of this document.

Note: The encoders, formats and options available with <code>texturetool</code> are subject to change.

This document describes those options available as of iPhone OS 2.2. Options not compatible with previous versions of iPhone OS are noted.

Note: The -p option indicates that it requires the -e option. It also requires the -o option.

Listing 1  Encoding Options.

The <code>texturetool</code> will default to <code>--bits-per-pixel-4</code>, <code>--channel-weighting-linear</code>,

and <code>-f</code> Raw if no other options are provided.

The <code>--bits-per-pixel-2</code> and <code>--bits-per-pixel-4</code> options create PVRTC data that encodes source

pixels into 2 or 4 bits per pixel. These options represent a fixed 16:1 and 8:1 compression ratio over the uncompressed 32-bit RGBA image data. There is a minimum data size of 32 bytes; the compressor will never produce files smaller than this, and at least

that many bytes are expected when uploading compressed texture data.

When compressing specifying <code>--channel-weighting-linear</code> will spread compression error equally across all channels. By contrast specifying <code>--channel-weighting-perceptual</code> attempts

to reduce error in the green channel compared to the linear option. In general, PVRTC compression does better with photographic images than with line art.

The <code>-m</code> option allows you to automatically generate mipmap levels for the source image. This levels are provided as additional image data in the archive created. If you use the

Raw image format, then each level of image data will be appended one after another to the archive. If you use the PVR archive format, then each mipmap image will be provided as a separate image in the archive.

Warning: Valid PVRTC data is at least 32 bytes in size. This means that images that are 8x8 or smaller will be padded to 32 bytes total before being written to the archive. For example, compressing a 4x4 image consumes 32

bytes, even though it only requires 8 bytes of storage. If you are using the mipmap option with Raw data files, ensure that you take this into consideration as you are reading them.

With iPhone OS 2.2, the <code>texturetool</code> now additional supports a format (<code>-f</code>) parameter that

allows you to control the format of its output file. While this parameter is not available with iPhone OS 2.1 or earlier, the data files produced are compatible with those versions if iPhone OS.

The default format is Raw, which is equivalent to the format that <code>texturetool</code> produced under iPhone SDK 2.0 and 2.1. This format is raw compressed texture data, either for a

single texture level (without the <code>-m</code> option) or for each texture level concatenated together (with the <code>-m</code> option).

Each texture level stored in the file will be at least 32 bytes in size, and must be uploaded to the GPU in its entirety.

for an example of working with texture data in this format.

Important: Source images for the encoder must satisfy these requirements:

Height and Width must be a power of 2.

Must be square (height==width).

Source images must be in a format that Image IO accepts. This includes PNG, JPG, TIFF and others.

Listing 2  Encoding images into the PVRTC compression format.

Listing 3  Encoding images into the PVRTC compression format while creating a preview.

Note: It is not possible to create a preview without also specifying the -o parameter and a valid output file. Preview images are always in PNG format.

The sample images produced by these settings are shown below.

Table 1  Sample Images.

4bpp, Linear

Original

4bpp, Perceptual

Table 2  Sample Images.

2bpp, Linear

2bpp, Perceptual

Listing 4 demonstrates how you can upload Raw PVRTC data to the GPU via the <code>texImage2DPVRTC</code> sample

Listing 4  Example of uploading PVRTC data to the graphics chip.

Date

Notes

2009-07-14

Revised to point to the OpenGL ES Programming Guide. Please refer to that documentation for further updates.

2009-01-27

Clarified non-square texture support (it isn't supported). Linked to the PVRTextureLoader sample.

2008-11-24

Changes for iPhone SDK 2.2, which include the new location for the texturetool and new options for its use.

2008-10-13

New document that describes how to create textures in the PVRTC compression format.