go语言Vulkan,Go语言面试题

Vulkan 内存管理

内存管理对开发者来说永远是一个沉重的话题。

创新互联公司专注于企业全网营销推广、网站重做改版、双桥网站定制设计、自适应品牌网站建设、H5建站商城系统网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为双桥等各大城市提供网站开发制作服务。

现有的高级语言都在通过各种努力试图让开发者摆脱内存管理的复杂工作,专注于业务逻辑的开发。这样的做法对开发者是友好的,较低的语言门槛也能适应当前越来越快的敏捷开发的节奏。

但是,世上没有免费的午餐,任何事情都有代价。除了应用,没人更清楚应用的行为,有些应用需要大量的小内存,有些应用只需要少量的大内存,有些应用会频繁释放和分频内存资源,有些应用的内存生命周期则很长。面对复杂的应用场景,我们很难有一个统一的策略可以解决所有问题。不管是通过虚拟机还是驱动程序,帮助应用进行内存管理永远是低效的。

Vulkan将内存管理的工作交给了开发者自己负责,如何分配释放内存,怎样制定内存策略都由开发者自己决定,因为没人比开发者更清楚自己想做什么。

Vulkan将内存划分为两大类: Host Memory 和 Device Memory 。

Host是运行应用程序的处理器,在PC机上就是指CPU。

Device是执行Vulkan命令的处理器,在PC机上就是指GPU。

所以,Host memory指的是对Host可见的内存,Device memory指的是对Device可见的内存。

更详细的,Vulkan系统中的内存有四种类型:

并不是所有设备都支持这四种类型。一些嵌入式系统、移动设备甚至是笔记本电脑的GPU,可能与CPU共享内存控制器和内存子系统。这种情况它的内存只有一种类型,我们通常称之为 unified memory architecture (统一内存架构)。

Host Memory是CPU可以访问的常规内存,一般是通过调用malloc或new分配。

Vulkan API创建的对象通常需要一定数量的Host Memory,用来储存对象和数据结构。

Vulkan对Host Memory的要求就是内存地址是对齐的!

这是因为某些高性能CPU指令在对齐的内存地址上效果最佳。通过假定存储CPU端数据结构的分配是对齐的,Vulkan可以无条件使用这些高性能指令,从而提供显著的性能优势。

如果内存没有对齐,你可能会发现程序运行一段时间后神秘崩溃!

任何可以由Device访问的内存,都被称为Device Memory(设备内存)。Device Memory距离Device更近,比Host Memory更有性能优势。Image object、Buffer object、UBO(uniform buffer objec)都由Device Memory分配。Vulkan中的所有资源都由Device Memory支持。

内存是一种昂贵的资源,分配操作通常会有间接的系统代价。

Vulkan中通过资源池来平摊成本,一些创建比较频繁的资源都由资源池统一管理:

内存的作用是给资源做底层支持,不同的资源对内存的要求并不一样。

Vulkan有两种基本资源类型:Buffer和Image。

Buffer 是最简单的资源类型,可以用来储存线性的结构化的数据,也可以储存内存中原始字节。

Image 则相对比较复杂,具有特殊的布局(layout)和格式(format),可用来做flitering,blending,depth 和 stencil testing等。

Image的布局(layout)对内存有特殊需求,主要有两种主要的平铺模式(tiling modes):

线性布局(linear layout)适合连续的单行的读写,但是大多数图形操作都涉及在跨行读写纹理元素(比如在计算A时,需要参考ABCD四个纹理像素),如果图像的宽度非常宽,相邻行的访问在线性布局中会有非常大的跳转。这可能会导致性能问题。

优化布局(optimal layout)的好处是内存数据根据不同内存子系统进行优化,比如将所有的纹理像素都优化到一块连续的内存区域中,加快内存处理速度。

GPU硬件通常倾向于使用优化布局以实现更有效的渲染。但是优化布局(optimal layout)通常是“不透明的”,这意味着优化布局格式的详细信息不会被其他需要读取或写入图像数据的组件得知。

如果CPU如果想读取生层的图像信息,图像在被读取之前要进行布局转换,将optimal layout转换为普通布局后,CPU才能正确识别图像信息。

Vulkan不同的内存类型有不同的使用场景:

在Vulkan中,我们在进行内存管理时要考虑到以下问题:

在使用Host Memory时,一定要注意内存对齐。不允许简单的直接把malloc hook到Vulkan的内存分配函数上!

参考文档:

如何选择Unity 3d Shader编写语言

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()

昨天我说过,在Unity里写shader,Cg/ HLSL是最佳选择。后来冒出了几个很有意思的观点,所以我特意查阅了一下资料再来回复。

Metal API:有人说,Metal API是iOS的未来。如果你完全不关心Android,PC,Xbox,PS等平台,那么你的确可以奉之为圭臬。反正,Unity3d 是不支持用Metal写的,而且他们说近期也没有要去支持Metal的打算。他们只支持自动编译成Metal API,但这样你能控制的就很少。因此,这不是一个Unity开发者入门的选择。

Vulkan:有人说这才是未来。大概吧。AMD开发过一个叫做Mantle 的语言,只有很少的几个游戏用过它,然后它还死了,现在有人用它搞出了这个。人们说他们从苹果公司的Metal借鉴了一些理念,用之以完善Vulkan。很显然,Vulkan是在2015年GDC公布的,但我没有在其他任何地方听说或看到。即使它真是未来,那也是一个远到不着边际的未来。Vulkan 听起来不错,像是某种方式的梦想成真,但它甚至没有发行。只有Valve公司用过它。什么?你在Valve工作?你的上司是要支持这个技术了吗?太多问题了。此外,约翰·卡马克暗示Android可能不久会有一个类似Metal API的东西,所以Android设备的GPU甚至不需要接受它。那么,当你考虑所有事情时,就是有太多的问题围绕着它,太多的未知。无论如何,它不是Unity当下的选择。

GLSL:现在你可以在Unity里写GLSL了。Unity会将这段代码编译到任何平台,虽然你可能要在PC(或任何非iOS / Apple设备)上执行“-force-opengl”,以使之正常工作。那会导致在某些显卡GPU上,Unity Profiler不能正确地执行配置。我很想知道,是否有可能解决这问题。所以,如果你用着Mac,团队里人人用着Mac,那么相信你应该使用GLSL- 绝对是明智之选。

Surface Shaders:你可以用Unity shader库做些很酷的东西,但它不一定会在移动设备上有效果。 Shader库就像一个简化的,更易于阅读的标记语言,但你失去了你手机游戏需要的控制和性能选项。鉴于大多数在中国的工作是做手游,我不推荐这样做。

Cg/HLSL:有人说,Cg是死的语言。那么,事实是,Cg和HLSL是同一个语言,只是由两个不同的公司,NVIDIA 和Microsoft分别维护。现在NIVIDIA已停止支持Cg,没有必要啊是不,反正Microsoft在维护HLSL。人们仍然使用着Cg的名字,但愿没有人对此困惑了。 Cg/ HLSL当然没有死,在跟几个大牛以及论坛老兵聊过Unity,他们都认为HLSL是Unity shader的未来,因为公司为游戏机做了很多,为3A级提升了许多。只要看看Unity5的特性,你会懂我意思了。Cg/HLSL可以被编译到你需要的任何设备,同时Unity和微软已经亲密得像兄弟一样,并且将一直亲密下去直到地老天荒。

为什么安装不上vulkan语言库

若使用的是vivo手机,可进入应用商店搜索安装“安兔兔评测”软件,打开安兔兔评测,首页,选择我的手机,进入硬件配置,上滑界面即可查看手机是否支持Vulkan及Vulkan版本。

电脑显卡型号可在MuMu模拟器右上角-三条杠-帮助中心-问题诊断-所有显卡中查看到。可能是最新版本或较老版本的N卡驱动不兼容导致,可以通过回退或更新显卡驱动解决,比如472以上版本。


网站题目:go语言Vulkan,Go语言面试题
分享URL:http://csdahua.cn/article/hedide.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流