iOS推出Metal渲染庫為了取代OpenGL。Metal有自己的Shader語言,渲染效率比OpenGL高。在這里我們一起探索:Metal使用C++限制、預(yù)處理定義、動態(tài)鏈接配置、GPU編譯配置、設(shè)備坐標(biāo)系、視口坐標(biāo)系、紋理坐標(biāo)系、矢量類型、矩陣類型、采樣器狀態(tài)、矩陣相乘。
1、使用C++限制
C++14的一些特性無法在Metal使用,具體如下:
- lambda表達(dá)式
- dynamic_cast操作
- 類型檢測
- new和delete操作
- noexcept操作
- goto跳轉(zhuǎn)
- register、thread_local關(guān)鍵字
- virtual虛函數(shù)
- 派生類
- 異常處理
另外,不要在Metal代碼使用C++標(biāo)準(zhǔn)庫。函數(shù)指針在Metal2.3才支持。
2、預(yù)處理定義
預(yù)處理定義包括:Metal版本、在macOS或iOS平臺編譯。如下所示:
__METAL_VERSION__ // Set to the Metal language revision
__METAL_MACOS__ // Set if compiled with the macOS Metal language
__METAL_IOS__ // Set if compiled with the iOS Metal language
__METAL__ // Set if compiled with the unified Metal language
還有一些TARGET_OS的預(yù)定義:
TARGET_OS_MAC // run on MacOS
TARGET_OS_OSX // run on OSX
TARGET_OS_IPHONE // run on devices or simulator
TARGET_OS_IOS // run on iOS
TARGET_OS_TV // run on Apple TV OS
TARGET_OS_MACCATALYST // run on MacOS
TARGET_OS_SIMULATOR // run on simulator
3、動態(tài)鏈接配置
Metal動態(tài)鏈接包括指定動態(tài)庫/靜態(tài)庫、安裝路徑,如下所示:
- -dynamiclib:指定為動態(tài)庫
- -install_name:動態(tài)庫安裝路徑
4、GPU編譯配置
在iOS16或macOS13以后,支持配置GPU編譯,具體配置選項(xiàng)如下:
-arch // 指定系統(tǒng)架構(gòu)
-gpu-family // MTLGPUFamily架構(gòu)
-N // 描述Metal腳本格式,后綴.mtlp-json
5、設(shè)備坐標(biāo)系
Normalized Device Coordinate歸一化設(shè)備坐標(biāo)系,簡稱NDC。由xyz三維空間構(gòu)成,其中z坐標(biāo)點(diǎn)在0.0和1.0之間。如下圖所示:
?
6、視口坐標(biāo)系
光柵化階段把設(shè)備坐標(biāo)系轉(zhuǎn)換為視口坐標(biāo)系,測量單位為pixel像素,原點(diǎn)(0, 0)在左上角,往右下角增大。如下圖所示:
?
7、紋理坐標(biāo)系
紋理坐標(biāo)系與視口坐標(biāo)系類似,只是紋理坐標(biāo)已被歸一化,范圍從0.0到1.0,中心點(diǎn)為(0.5, 0.5),如下圖所示:
?
8、矢量類型
矢量類型包括:bool、char、short、int、long、half、float等,同時后面跟著數(shù)值(2、3、4)。值得注意的是,這里有內(nèi)存對齊,為2的n次方。這里以float展開,其他類型只列舉數(shù)值2,如下表所示:
type | size | alignment |
float2 | 8 | 8 |
float3 | 16 | 16 |
float4 | 16 | 16 |
bool2 | 2 | 2 |
char2 | 2 | 2 |
short2 | 4 | 4 |
half2 | 4 | 4 |
int2 | 8 | 8 |
long2 | 16 | 16 |
9、矩陣類型
矩陣類型包括half和float,而mxn陣列包括2x2、2x3、2x4、3x2、3x3、3x4、4x2、4x3、4x4,如下圖所示:
?
10、采樣器狀態(tài)
Sampler采樣器的狀態(tài)枚舉值,如下表所示:
變量 | 數(shù)值 | 描述 |
coord | normalized(默認(rèn)) pixel |
指定紋理坐標(biāo)為歸一化或像素 |
address | repeat mirrored_repeat clamp_to_edge clamp_to_zero clamp_to_border |
紋理坐標(biāo)的地址模式 |
border_color | transparent_black(默認(rèn)) opaque_black opaque_white |
邊界顏色 |
filter | nearest(默認(rèn)) linear |
magnification和minification 過濾模式 |
mag_filter | nearest(默認(rèn)) linear |
magnification過濾模式 |
mig_filter | nearest(默認(rèn)) linear |
minification過濾模式 |
mip_filter | nearest(默認(rèn)) linear |
mipmap過濾模式 |
compare_func | never(默認(rèn)) less /?less_equal greater /?greater_equal equal /?not_equal always |
設(shè)置比較函數(shù) |
11、矩陣相乘
矩陣相乘有左乘與右乘的區(qū)別,類型包括:scalar乘以matrix、matrix乘以scalar、vector乘以matrix、matrix乘以vector、matrix乘以matrix。文章來源:http://www.zghlxwxcb.cn/news/detail-482170.html
參考鏈接:Metal-Shading-Language-Specification文章來源地址http://www.zghlxwxcb.cn/news/detail-482170.html
到了這里,關(guān)于探索iOS之Metal編程指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!