OpenGL ES 框架詳細(xì)解析(十四) —— 使用紋理工具壓縮紋理

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.10.05

前言

OpenGL ES是一個(gè)強(qiáng)大的圖形庫,是跨平臺(tái)的圖形API,屬于OpenGL的一個(gè)簡(jiǎn)化版本。iOS系統(tǒng)可以利用OpenGL ES將圖像數(shù)據(jù)直接送入到GPU進(jìn)行渲染,這樣避免了從CPU進(jìn)行計(jì)算再送到顯卡渲染帶來的性能的高消耗,能帶來來更好的視頻效果和用戶體驗(yàn)。接下來幾篇就介紹下iOS 系統(tǒng)的 OpenGL ES框架。感興趣的可以看上面幾篇。
1. OpenGL ES 框架詳細(xì)解析(一) —— 基本概覽
2. OpenGL ES 框架詳細(xì)解析(二) —— 關(guān)于OpenGL ES
3. OpenGL ES 框架詳細(xì)解析(三) —— 構(gòu)建用于iOS的OpenGL ES應(yīng)用程序的清單
4. OpenGL ES 框架詳細(xì)解析(四) —— 配置OpenGL ES的上下文
5. OpenGL ES 框架詳細(xì)解析(五) —— 使用OpenGL ES和GLKit進(jìn)行繪制
6. OpenGL ES 框架詳細(xì)解析(六) —— 繪制到其他渲染目的地
7. OpenGL ES 框架詳細(xì)解析(七) —— 多任務(wù),高分辨率和其他iOS功能
8. OpenGL ES 框架詳細(xì)解析(八) —— OpenGL ES 設(shè)計(jì)指南
9. OpenGL ES 框架詳細(xì)解析(九) —— 調(diào)整您的OpenGL ES應(yīng)用程序
10. OpenGL ES 框架詳細(xì)解析(十) —— 使用頂點(diǎn)數(shù)據(jù)的最佳做法
11. OpenGL ES 框架詳細(xì)解析(十一) —— 并發(fā)和OpenGL ES
12. OpenGL ES 框架詳細(xì)解析(十二) —— 采用OpenGL ES 3.0
13. OpenGL ES 框架詳細(xì)解析(十三) —— Xcode OpenGL ES工具概述

Using texturetool to Compress Textures - 使用紋理工具壓縮紋理

iOS SDK包括一個(gè)將紋理壓縮為PVRTC或ASTC格式的工具,恰當(dāng)?shù)孛麨?code>texturetool。 如果您安裝了iOS 7.0 SDK或更高版本的Xcode,那么texturetool位于:/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool。

紋理工具提供了各種壓縮選項(xiàng),在圖像質(zhì)量和尺寸之間進(jìn)行權(quán)衡。 您需要對(duì)每個(gè)紋理進(jìn)行實(shí)驗(yàn),以確定哪個(gè)設(shè)置提供最佳折中。

注意:紋理工具可用的編碼器,格式和選項(xiàng)可能會(huì)更改。 本文檔介紹了從iOS 7開始可用的選項(xiàng)。


texturetool Parameters - texturetool參數(shù)

可以傳遞給紋理工具的參數(shù)在本節(jié)的其余部分進(jìn)行了描述。

user$ texturetool -h
 
Usage: texturetool [-hl]
       texturetool -c <reference_image> <input_image>
       texturetool [-ms] [-e <encoder>] [-p <preview_file>] -o <output> [-f <format>] <input_image>
 
first form:
    -h       Display this help menu.
    -l       List available encoders, individual encoder options, and file formats.
 
second form:
    -c       Compare <input_image> to <reference_image> and report differences.
 
third form:
    -m                Generate a complete mipmap chain from the input image.
    -s                Report numerical differences between <input_image> and the encoded image.
    -e <encoder>      Encode texture levels with <encoder>.
    -p <preview_file> Output a PNG preview of the encoded output to <preview_file>. Requires -e option
    -o <output>       Write processed image to <output>.
    -f <format>       Set file <format> for <output> image.

注意:-p選項(xiàng)表示需要-e選項(xiàng)。 它還需要-o選項(xiàng)。

// Listing C-1  Encoding options

user$ texturetool -l
Encoders:
 
PVRTC
 --channel-weighting-linear
 --channel-weighting-perceptual
 --bits-per-pixel-2
 --bits-per-pixel-4
 --alpha-is-independent
 --alpha-is-opacity
 --punchthrough-unused
 --punchthrough-allowed
 --punchthrough-forced
ASTC
 --block-width-4
 --block-width-5
 --block-width-6
 --block-width-8
 --block-width-10
 --block-width-12
 --compression-mode-veryfast
 --compression-mode-fast
 --compression-mode-medium
 --compression-mode-thorough
 --compression-mode-exhaustive
 --srgb-yes
 --srgb-no
 --block-height-4
 --block-height-5
 --block-height-6
 --block-height-8
 --block-height-10
 --block-height-12
 
Formats:
 
Raw
PVR
KTX

texturetool如果不提供其他選項(xiàng)的話,默認(rèn)為--bits-per-pixel-4, --channel-weighting-linear-f Raw。

--bits-per-pixel-2--bits-per-pixel-4選項(xiàng)創(chuàng)建PVRTC數(shù)據(jù),將源像素編碼為每像素2或4位。 與未壓縮的32位RGBA圖像數(shù)據(jù)相比,這些選項(xiàng)代表固定的16:1和8:1壓縮率。 最小數(shù)據(jù)大小為32字節(jié); 壓縮器從不產(chǎn)生比此更小的文件,并且至少在上傳壓縮紋理數(shù)據(jù)時(shí)預(yù)計(jì)會(huì)有許多字節(jié)。

當(dāng)壓縮時(shí),指定 --channel-weighting-linear所有通道的平均壓縮誤差。 相反,與線性選項(xiàng)相比,指定--channel-weighting-perceptual嘗試減少綠色通道中的錯(cuò)誤。 一般來說,PVRTC壓縮比攝影圖像比線條藝術(shù)更好。

-m選項(xiàng)會(huì)自動(dòng)為源圖像生成mipmap級(jí)別。 這些級(jí)別作為創(chuàng)建的存檔中的附加映像數(shù)據(jù)提供。 如果您使用原始圖像格式,則每個(gè)級(jí)別的圖像數(shù)據(jù)將一個(gè)接一個(gè)地附加到存檔。 如果使用PVR歸檔格式,則每個(gè)mipmap圖像都將作為存檔中的單獨(dú)圖像提供。

(-f)參數(shù)控制其輸出文件的格式。 默認(rèn)格式為Raw。 此格式是原始?jí)嚎s紋理數(shù)據(jù),無論是單個(gè)紋理級(jí)別(不使用-m選項(xiàng))還是連接在一起的每個(gè)紋理級(jí)別(使用-m選項(xiàng))。 存儲(chǔ)在文件中的每個(gè)紋理級(jí)別的大小至少為32字節(jié),并且必須將其全部上傳到GPU。 PVR格式與Imagination TechnologiesPowerVR SDK中找到的PVRTexTool所使用的格式相匹配。 要加載PVR壓縮紋理,請(qǐng)使用 GLKTextureLoader類。

-s-c選項(xiàng)在編碼期間打印錯(cuò)誤指標(biāo)。 -s選項(xiàng)將輸入(未壓縮)圖像與輸出(編碼)圖像進(jìn)行比較,-c選項(xiàng)可以比較任何兩個(gè)圖像。 比較結(jié)果包括均方根誤差(rms),感知加權(quán)pRms,最壞情況紋理錯(cuò)誤(max)和每個(gè)統(tǒng)計(jì)量(rmsC,pRmsC和maxC)的基于合成的版本。 基于合成的錯(cuò)誤假定圖像的Alpha通道用于不透明度,并且每個(gè)紋素的顏色與最壞情況的目標(biāo)顏色混合。

在優(yōu)化壓縮圖像時(shí),與-s-c選項(xiàng)以及編碼器一起使用的錯(cuò)誤指標(biāo)默認(rèn)情況下將圖像的Alpha通道視為獨(dú)立通道(或使用--alpha-is-independent選項(xiàng)時(shí))。--alpha-is-opacity選項(xiàng)根據(jù)標(biāo)準(zhǔn)混合操作將錯(cuò)誤度量值更改為一個(gè),如通過調(diào)用glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)來實(shí)現(xiàn)的。

PVR紋理壓縮支持特殊的穿透模式,可以在每個(gè)4x4塊的基礎(chǔ)上啟用。 該模式限制了塊內(nèi)可以使用的顏色漸變,但是引入了將像素的Alpha值強(qiáng)制為0的選項(xiàng)。它可以打敗PVRTC平滑色彩插值,引入塊邊界偽像,因此應(yīng)謹(jǐn)慎使用。 三個(gè)突破性選項(xiàng)是:

  • --punchthrough-unused。沒有穿透(默認(rèn)選項(xiàng))。
  • --punchthrough-allowed。當(dāng)優(yōu)化圖像質(zhì)量時(shí),編碼器可以逐塊地實(shí)現(xiàn)穿透。 該選項(xiàng)通常改進(jìn)了壓縮算法使用的目標(biāo)(每像素)誤差度量,但可能會(huì)引入主觀的偽像。
  • --punchthrough-forced。 每個(gè)塊都啟用Punchthrough,限制顏色漸變,但是可以讓塊中的任何像素的alpha為0。此選項(xiàng)主要用于完整性,但是當(dāng)結(jié)果在視覺上與其他選項(xiàng)進(jìn)行比較時(shí)還是有用的。

重要信息:編碼器的源圖像必須滿足以下要求:

  • 高度和寬度必須至少為8。
  • 高度和寬度必須是2的冪。
  • 必須是正方形(height == width)
  • 源圖像必須采用Image IO在OS X中接受的格式。為獲得最佳效果,原始紋理應(yīng)以未壓縮的數(shù)據(jù)格式開始。

重要提示:如果您使用PVRTexTool來壓縮紋理,那么您必須創(chuàng)建張方形的并且是2的次冪長度的紋理。 如果您的應(yīng)用程序嘗試在iOS中加載非平方或非2的次冪長度的紋理,則會(huì)返回錯(cuò)誤。

// Listing C-2  Encoding images into the PVRTC compression format

Encode Image.png into PVRTC using linear weights and 4 bpp, and saving as ImageL4.pvrtc
user$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-4 -o ImageL4.pvrtc Image.png

Encode Image.png into PVRTC using perceptual weights and 4 bpp, and saving as ImageP4.pvrtc
user$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-4 -o ImageP4.pvrtc Image.png

Encode Image.png into PVRTC using linear weights and 2 bpp, and saving as ImageL2.pvrtc
user$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-2 -o ImageL2.pvrtc Image.png

Encode Image.png into PVRTC using perceptual weights and 2 bpp, and saving as ImageP2.pvrtc
user$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-2 -o ImageP2.pvrtc Image.png
// Listing C-3  Encoding images into the PVRTC compression format while creating a preview

Encode Image.png into PVRTC using linear weights and 4 bpp, and saving the output as ImageL4.pvrtc and a PNG preview as ImageL4.png
user$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-4 -o ImageL4.pvrtc -p ImageL4.png Image.png

Encode Image.png into PVRTC using perceptual weights and 4 bpp, and saving the output as ImageP4.pvrtc and a PNG preview as ImageP4.png
user$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-4 -o ImageP4.pvrtc -p ImageP4.png Image.png

Encode Image.png into PVRTC using linear weights and 2 bpp, and saving the output as ImageL2.pvrtc and a PNG preview as ImageL2.png
user$ texturetool -e PVRTC --channel-weighting-linear --bits-per-pixel-2 -o ImageL2.pvrtc -p ImageL2.png Image.png

Encode Image.png into PVRTC using perceptual weights and 2 bpp, and saving the output as ImageP2.pvrtc and a PNG preview as ImageP2.png
user$ texturetool -e PVRTC --channel-weighting-perceptual --bits-per-pixel-2 -o ImageP2.pvrtc -p ImageP2.png Image.png

注意:無需創(chuàng)建預(yù)覽也不能指定-o參數(shù)和有效的輸出文件。 預(yù)覽圖像始終為PNG格式。

要加載PVR壓縮紋理,請(qǐng)使用GLKTextureLoader類。

有關(guān)直接使用PVR壓縮數(shù)據(jù)的示例,請(qǐng)參閱PVRTextureLoader 示例。

后記

未完,待續(xù)~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容