內(nèi)存管理對(duì)開(kāi)發(fā)者來(lái)說(shuō)永遠(yuǎn)是一個(gè)沉重的話題。

創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、雙橋網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城系統(tǒng)網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為雙橋等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
現(xiàn)有的高級(jí)語(yǔ)言都在通過(guò)各種努力試圖讓開(kāi)發(fā)者擺脫內(nèi)存管理的復(fù)雜工作,專注于業(yè)務(wù)邏輯的開(kāi)發(fā)。這樣的做法對(duì)開(kāi)發(fā)者是友好的,較低的語(yǔ)言門(mén)檻也能適應(yīng)當(dāng)前越來(lái)越快的敏捷開(kāi)發(fā)的節(jié)奏。
但是,世上沒(méi)有免費(fèi)的午餐,任何事情都有代價(jià)。除了應(yīng)用,沒(méi)人更清楚應(yīng)用的行為,有些應(yīng)用需要大量的小內(nèi)存,有些應(yīng)用只需要少量的大內(nèi)存,有些應(yīng)用會(huì)頻繁釋放和分頻內(nèi)存資源,有些應(yīng)用的內(nèi)存生命周期則很長(zhǎng)。面對(duì)復(fù)雜的應(yīng)用場(chǎng)景,我們很難有一個(gè)統(tǒng)一的策略可以解決所有問(wèn)題。不管是通過(guò)虛擬機(jī)還是驅(qū)動(dòng)程序,幫助應(yīng)用進(jìn)行內(nèi)存管理永遠(yuǎn)是低效的。
Vulkan將內(nèi)存管理的工作交給了開(kāi)發(fā)者自己負(fù)責(zé),如何分配釋放內(nèi)存,怎樣制定內(nèi)存策略都由開(kāi)發(fā)者自己決定,因?yàn)闆](méi)人比開(kāi)發(fā)者更清楚自己想做什么。
Vulkan將內(nèi)存劃分為兩大類: Host Memory 和 Device Memory 。
Host是運(yùn)行應(yīng)用程序的處理器,在PC機(jī)上就是指CPU。
Device是執(zhí)行Vulkan命令的處理器,在PC機(jī)上就是指GPU。
所以,Host memory指的是對(duì)Host可見(jiàn)的內(nèi)存,Device memory指的是對(duì)Device可見(jiàn)的內(nèi)存。
更詳細(xì)的,Vulkan系統(tǒng)中的內(nèi)存有四種類型:
并不是所有設(shè)備都支持這四種類型。一些嵌入式系統(tǒng)、移動(dòng)設(shè)備甚至是筆記本電腦的GPU,可能與CPU共享內(nèi)存控制器和內(nèi)存子系統(tǒng)。這種情況它的內(nèi)存只有一種類型,我們通常稱之為 unified memory architecture (統(tǒng)一內(nèi)存架構(gòu))。
Host Memory是CPU可以訪問(wèn)的常規(guī)內(nèi)存,一般是通過(guò)調(diào)用malloc或new分配。
Vulkan API創(chuàng)建的對(duì)象通常需要一定數(shù)量的Host Memory,用來(lái)儲(chǔ)存對(duì)象和數(shù)據(jù)結(jié)構(gòu)。
Vulkan對(duì)Host Memory的要求就是內(nèi)存地址是對(duì)齊的!
這是因?yàn)槟承└咝阅蹸PU指令在對(duì)齊的內(nèi)存地址上效果最佳。通過(guò)假定存儲(chǔ)CPU端數(shù)據(jù)結(jié)構(gòu)的分配是對(duì)齊的,Vulkan可以無(wú)條件使用這些高性能指令,從而提供顯著的性能優(yōu)勢(shì)。
如果內(nèi)存沒(méi)有對(duì)齊,你可能會(huì)發(fā)現(xiàn)程序運(yùn)行一段時(shí)間后神秘崩潰!
任何可以由Device訪問(wèn)的內(nèi)存,都被稱為Device Memory(設(shè)備內(nèi)存)。Device Memory距離Device更近,比Host Memory更有性能優(yōu)勢(shì)。Image object、Buffer object、UBO(uniform buffer objec)都由Device Memory分配。Vulkan中的所有資源都由Device Memory支持。
內(nèi)存是一種昂貴的資源,分配操作通常會(huì)有間接的系統(tǒng)代價(jià)。
Vulkan中通過(guò)資源池來(lái)平攤成本,一些創(chuàng)建比較頻繁的資源都由資源池統(tǒng)一管理:
內(nèi)存的作用是給資源做底層支持,不同的資源對(duì)內(nèi)存的要求并不一樣。
Vulkan有兩種基本資源類型:Buffer和Image。
Buffer 是最簡(jiǎn)單的資源類型,可以用來(lái)儲(chǔ)存線性的結(jié)構(gòu)化的數(shù)據(jù),也可以儲(chǔ)存內(nèi)存中原始字節(jié)。
Image 則相對(duì)比較復(fù)雜,具有特殊的布局(layout)和格式(format),可用來(lái)做flitering,blending,depth 和 stencil testing等。
Image的布局(layout)對(duì)內(nèi)存有特殊需求,主要有兩種主要的平鋪模式(tiling modes):
線性布局(linear layout)適合連續(xù)的單行的讀寫(xiě),但是大多數(shù)圖形操作都涉及在跨行讀寫(xiě)紋理元素(比如在計(jì)算A時(shí),需要參考ABCD四個(gè)紋理像素),如果圖像的寬度非常寬,相鄰行的訪問(wèn)在線性布局中會(huì)有非常大的跳轉(zhuǎn)。這可能會(huì)導(dǎo)致性能問(wèn)題。
優(yōu)化布局(optimal layout)的好處是內(nèi)存數(shù)據(jù)根據(jù)不同內(nèi)存子系統(tǒng)進(jìn)行優(yōu)化,比如將所有的紋理像素都優(yōu)化到一塊連續(xù)的內(nèi)存區(qū)域中,加快內(nèi)存處理速度。
GPU硬件通常傾向于使用優(yōu)化布局以實(shí)現(xiàn)更有效的渲染。但是優(yōu)化布局(optimal layout)通常是“不透明的”,這意味著優(yōu)化布局格式的詳細(xì)信息不會(huì)被其他需要讀取或?qū)懭雸D像數(shù)據(jù)的組件得知。
如果CPU如果想讀取生層的圖像信息,圖像在被讀取之前要進(jìn)行布局轉(zhuǎn)換,將optimal layout轉(zhuǎn)換為普通布局后,CPU才能正確識(shí)別圖像信息。
Vulkan不同的內(nèi)存類型有不同的使用場(chǎng)景:
在Vulkan中,我們?cè)谶M(jìn)行內(nèi)存管理時(shí)要考慮到以下問(wèn)題:
在使用Host Memory時(shí),一定要注意內(nèi)存對(duì)齊。不允許簡(jiǎn)單的直接把malloc hook到Vulkan的內(nèi)存分配函數(shù)上!
參考文檔:
What language
should I study for making shaders in Unity3d?
Yesterday I said Cg/HLSL was the best choice for writing
shaders in Unity. A couple of other interesting opinions came out so I did a
bit of reading before making this reply.
Metal API:
Others said that Metal API is the future for iOS. That’s great for you if you
don’t care about Android, PC, Xbox, PS, etc, etc. Anyway, Unity3d doesn’t
support writing in Metal and they said they have no intention to support it in
the near future. They only have support for automatic compiling into Metal API,
so you don’t really get much control over it. So it’s not a choice for a Unity
developer to start learning.
Vulkan:
Somebody said this is the future. Maybe. It’s made from a dead language called
Mantle developed by AMD, which only had a few games ever using it. They took
some ideas from Apple’s Metal and improved it, so people say. Apparently Vulkan
was announced in GDC 2015, but I didn’t see or hear it anywhere. Even if it is
the future, it is a far and distant uncertain future. Vulkan sounds nice, like
a dream come true in some ways, but it isn’t even released yet. Only Valve company
has used it. Do you work at Valve? Does your boss want to support this
technology yet? Too many questions. Plus, John Carmack hinted that Android
might have a similar kinda of Metal API of their own soon, so GPUs of Android
devices might not even need to adopt it. Well, when you consider everything
there is just too much questions surrounding this, too much unknown. In any
case, it’s not a choice for Unity at the moment.
GLSL: You can
write in GLSL in Unity now. Unity will compile this code to whatever platform
you like, though you may have to “-force-opengl” on a PC (or any non iOS/Apple)
device to get it to work properly. That will cause the Unity Profiler to be
unable to profile correctly on some GPUs of certain video cards. I’d love to
know if it’s possible to get around this. So, if you use a Mac and everyone in
your team uses a Mac, then sure you should probably use GLSL –it’s the logical
choice.
Surface Shaders:
You can use Unity shaderlab to do some cool stuff, but it won’t be very
efficient for mobile. Shaderlab is like a simplified and more humanly-readable
markup language, but you lose the control and performance options that you need
for mobile games. Since most of the jobs in China will be for making mobile
games, I don’t recommend it.
Cg/HLSL: Some
people said that Cg was a dead language. Well, the fact is that Cg and HLSL are
the same language which is maintained by two different companies, NVidia and
Microsoft. Now Nividia has stopped supporting Cg because there is no need since
Microsoft supports HLSL. People still use the name Cg, but hope nobody gets
confused about it. Cg/HLSL is certainly not dead, and after talking to several
high ups and forum veterans over at Unity, they all think that HLSL is the
future of shader writing in Unity because the company is doing a lot of console
and AAA promotion. Just look at Unity5 features and you will get my meaning.
Cg/HLSL can be compiled to any device you need and since Unity and Microsoft
seem like great friends now, I don’t think this is going to change.
Conclusion:
If you want to use Unity3d and make games for Windows,
Consoles, Android, and iOS, with decent performance on all platforms, you
should use Cg/HLSL.
If you want to use Unity3d and make games for mobile only
(don’t need to support Windows or Consoles), GLSL is a good choice, especially
if you work on Apple Macs.
If you don’t want to use Unity3d and only want to develop
for the newest iOS devices, then Metal is the best, though very few people in
China are using it right now.
If you want to use cocos2d, I am not the expert for
advice, but friends say GLSL is your best choice.
However, also, but, maybe…. In China, the dominant art
style is hand-painted stuff. This doesn’t require very deep shader knowledge,
and is very little work for an experienced guy to achieve. Shader coding is
mainly for trying to go beyond current gen graphics, or create specific
effects, or to get around some graphical issues. So you have to think about if
it’s worth to study, because many kinds of games may not need this.
非常感謝知友@王邢敏 的翻譯,他的微博:One Double()
昨天我說(shuō)過(guò),在Unity里寫(xiě)shader,Cg/ HLSL是最佳選擇。后來(lái)冒出了幾個(gè)很有意思的觀點(diǎn),所以我特意查閱了一下資料再來(lái)回復(fù)。
Metal API:有人說(shuō),Metal API是iOS的未來(lái)。如果你完全不關(guān)心Android,PC,Xbox,PS等平臺(tái),那么你的確可以奉之為圭臬。反正,Unity3d 是不支持用Metal寫(xiě)的,而且他們說(shuō)近期也沒(méi)有要去支持Metal的打算。他們只支持自動(dòng)編譯成Metal API,但這樣你能控制的就很少。因此,這不是一個(gè)Unity開(kāi)發(fā)者入門(mén)的選擇。
Vulkan:有人說(shuō)這才是未來(lái)。大概吧。AMD開(kāi)發(fā)過(guò)一個(gè)叫做Mantle 的語(yǔ)言,只有很少的幾個(gè)游戲用過(guò)它,然后它還死了,現(xiàn)在有人用它搞出了這個(gè)。人們說(shuō)他們從蘋(píng)果公司的Metal借鑒了一些理念,用之以完善Vulkan。很顯然,Vulkan是在2015年GDC公布的,但我沒(méi)有在其他任何地方聽(tīng)說(shuō)或看到。即使它真是未來(lái),那也是一個(gè)遠(yuǎn)到不著邊際的未來(lái)。Vulkan 聽(tīng)起來(lái)不錯(cuò),像是某種方式的夢(mèng)想成真,但它甚至沒(méi)有發(fā)行。只有Valve公司用過(guò)它。什么?你在Valve工作?你的上司是要支持這個(gè)技術(shù)了嗎?太多問(wèn)題了。此外,約翰·卡馬克暗示Android可能不久會(huì)有一個(gè)類似Metal API的東西,所以Android設(shè)備的GPU甚至不需要接受它。那么,當(dāng)你考慮所有事情時(shí),就是有太多的問(wèn)題圍繞著它,太多的未知。無(wú)論如何,它不是Unity當(dāng)下的選擇。
GLSL:現(xiàn)在你可以在Unity里寫(xiě)GLSL了。Unity會(huì)將這段代碼編譯到任何平臺(tái),雖然你可能要在PC(或任何非iOS / Apple設(shè)備)上執(zhí)行“-force-opengl”,以使之正常工作。那會(huì)導(dǎo)致在某些顯卡GPU上,Unity Profiler不能正確地執(zhí)行配置。我很想知道,是否有可能解決這問(wèn)題。所以,如果你用著Mac,團(tuán)隊(duì)里人人用著Mac,那么相信你應(yīng)該使用GLSL- 絕對(duì)是明智之選。
Surface Shaders:你可以用Unity shader庫(kù)做些很酷的東西,但它不一定會(huì)在移動(dòng)設(shè)備上有效果。 Shader庫(kù)就像一個(gè)簡(jiǎn)化的,更易于閱讀的標(biāo)記語(yǔ)言,但你失去了你手機(jī)游戲需要的控制和性能選項(xiàng)。鑒于大多數(shù)在中國(guó)的工作是做手游,我不推薦這樣做。
Cg/HLSL:有人說(shuō),Cg是死的語(yǔ)言。那么,事實(shí)是,Cg和HLSL是同一個(gè)語(yǔ)言,只是由兩個(gè)不同的公司,NVIDIA 和Microsoft分別維護(hù)。現(xiàn)在NIVIDIA已停止支持Cg,沒(méi)有必要啊是不,反正Microsoft在維護(hù)HLSL。人們?nèi)匀皇褂弥鳦g的名字,但愿沒(méi)有人對(duì)此困惑了。 Cg/ HLSL當(dāng)然沒(méi)有死,在跟幾個(gè)大牛以及論壇老兵聊過(guò)Unity,他們都認(rèn)為HLSL是Unity shader的未來(lái),因?yàn)楣緸橛螒驒C(jī)做了很多,為3A級(jí)提升了許多。只要看看Unity5的特性,你會(huì)懂我意思了。Cg/HLSL可以被編譯到你需要的任何設(shè)備,同時(shí)Unity和微軟已經(jīng)親密得像兄弟一樣,并且將一直親密下去直到地老天荒。
若使用的是vivo手機(jī),可進(jìn)入應(yīng)用商店搜索安裝“安兔兔評(píng)測(cè)”軟件,打開(kāi)安兔兔評(píng)測(cè),首頁(yè),選擇我的手機(jī),進(jìn)入硬件配置,上滑界面即可查看手機(jī)是否支持Vulkan及Vulkan版本。
電腦顯卡型號(hào)可在MuMu模擬器右上角-三條杠-幫助中心-問(wèn)題診斷-所有顯卡中查看到。可能是最新版本或較老版本的N卡驅(qū)動(dòng)不兼容導(dǎo)致,可以通過(guò)回退或更新顯卡驅(qū)動(dòng)解決,比如472以上版本。
本文標(biāo)題:go語(yǔ)言Vulkan,Go語(yǔ)言面試題
轉(zhuǎn)載來(lái)于:http://www.chinadenli.net/article14/hedide.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站收錄、標(biāo)簽優(yōu)化、網(wǎng)站建設(shè)、小程序開(kāi)發(fā)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)