Vulkan简介

来源:互联网 发布:php扩展开发.pdf 编辑:程序博客网 时间:2024/05/16 17:13
    2016-02-24
偶尔发现一个刚发布的被传言很牛的东西:Vulkan,百度了一下(不要鄙视我,翻墙老掉下来才用的),发现关键字:秒杀、完爆、取代,这么牛,不是欺负我读书少吧。我一向不惮多怀疑一点,就想弄明白了到底是怎么一回事儿。wiki介绍 Vulkan:low Level rendering api。我们在讨论Vulkan的时候,不可避免的就要提到AMD在13年推出的Mantle。简单来说,Vulkan就是Mantle的继承者,也可以说是AMD把Mantle贡献给了Khroos Group,之后在此基础上产生了Vulkan。wiki中对Vulkan的特征写有:
  • Reduced driver overhead, reducing CPU workloads.
  • Reduced load on CPUs through the use of batching, leaving the CPU free to do additional computation or rendering than otherwise.
  • Intelligent and even CPU scaling for multi-core CPUs, which are by far the majority type of CPU on the market. Previous APIs like DirectX 11 and OpenGL 4 were initially designed for use with single-core CPUs and could not easily use multiple or scale workloads evenly across multiple, leaving some CPU cores underworked and some not even utilized at all, resulting in performance issues and frequent CPU bottlenecks.
  • OpenGL uses the high-level language GLSL for writing shaders which forces each OpenGL driver to implement its own compiler for GLSL that executes at application runtime to translate the program's shaders into executable code for the target platform. Vulkan will instead provide an intermediate binary format called SPIR-V (Standard Portable Intermediate Representation), analogous to the binary format thatHLSL shaders are compiled into in DirectX. This reduces the onus on driver vendors, allows shader pre-compilation, and permits application developers to write shaders in languages other than GLSL.
  • Cross-platform API supported on both mobile devices and high-end graphics cards.
  • OS agnostic to improve the portability of applications created using the API.
 
OpenGL
Vulkan
one single global state machine
object-based with no global state 
state is tied to a single context
all state concepts are localized to a command buffer 
GPU memory and synchronization are usually hidden
explicit control over memory management and synchronization
extensive error checking
Vulkan drivers do no error checking 
我们来看一下这些特征。
driver层变薄了,当然开销就小了。 
batching 和 instancing 类似,不知有何种改进。
多线程
                                      http://leonardo.wotaneage.com/attachments/vk_mt.png
 
这张图很形象化了,Vulkan可以多线程控制buffer,在OpenGL中难以做到这一点,如果能够做到,也需要采用各种trick。
 
  第二个重点就是shader部分了。
                                      http://leonardo.wotaneage.com/attachments/vk_arch.png
可以看到,OpenGL包含了很多东西成为一个整体,Vulkan则分成了相对独立的thin driver, GLSL编译器及二进制shader。与OpenGL不同,Vulkan采用SPIR-V 这种 bytecode format,那么各个GPU厂商的Vulkan实现只需要处理这种简单的bytecode,而不需要编译GLSL,这样就能更多的确保shader语言行为的一致性。这种bytecode也能被OpenCL使用。以前由NVIDIA,AMD各自实现的GLSL编译器前端,现在由Kronos Group提供,这也是开源的,以后可以研究研究。
并不是增加了这个中间层,就会导致运行效率的降低。Clang 也编译为 LLVM的中间层代码,出来的代码也并不比gcc编译的代码效率低。
 
我们前面一直在探讨Vulkan在渲染方面的特征,现在来谈谈在通用计算方面的应用吧。Vulkan写过GPU加速程序的同学应该知道CUDA和OpenCL,分属两大GPU厂商的标准。CUDA的整个工具链都很齐全,而OpenCL的工具链则相对弱些,胜在代码通用性更好,各种CPU都可以跑。我们写CPU程序,如果想要获取最佳的程序性能,也需要对CPU架构、缓存、指令集做好研究,同样,我相信要想写出高性能的GPU程序,也需要对其架构指令做好研究。现有的CUDA、OpenCL已经对GPU硬件有很好的抽象了,而且生态环境比较成熟,我不认为只做标准的Khronos Group仅凭标准的制定就能比NVIDIA、AMD在这方面做的更好。毕竟,这些东西都是以来硬件了。在通用计算方面,我不对Vulkan抱有大的期望。
 
他们都是类似于DX12的接口方式。关于这一点我感到有些奇怪,DX12明明是高级API,Vulkan 是低级API,为什么说类似呢?什么地方类似了?这一点我也不是很明了,以后再说。
Khronos Group负责制定Vulkan的接口和SPIR-V 中间层指令码标准,各大GPU厂商只要符合这两样标准即可。Vulkan要求GPU厂商提供新的driver,这是很薄的一层,原来的由驱动层面负责的优化工作现在就会落到普通开发者的身上。当然了,如果接口合理,我们也乐于见到能更加深层次的控制GPU。如果我们对这个东西逐渐熟悉了,我们可以自己增加一个抽象层。这样也意味着需要我们掌握更多的GPU的知识了,写一个简单的绘制程序也会比OpenGL 多一些代码。
OpenGL是单CPU时代的产物,绝大部分应用场景都是单线程内更新数据并渲染。如果想要多线程渲染,需要自己控制glContext,这倒不是多麻烦的事情。前一段时间我们的项目中对多线程渲染做了一次尝试。多显卡下的确能够工作,出图的效率提升了不少,不过,需要一张quadro卡支持。
去年我在blog就写到过,OpenGL在desktop和mobile两者上很快就会统一。没有想到新来的Vulkan就没快就实现了,且OpenGL被直接舍弃了。但是,我认为这两种GPU架构差异性太大了,短时间内难以用相同的代码在两类GPU上都跑起来。或许在两三年的时间内我们需要对Vulkan写出不同的抽象层来满足需求。这样一来就类似于OpenGL与OpenGL ES了,所谓的统一,在工程难度和运行效率上或许会有些影响吧。也许将来三四年内移动端GPU的性能会有客观提升,彼时可能会完全统一吧。所以,我觉得桌面端和移动端GPU代码统一的条件是GPU性能足够强悍,架构上类似于桌面GPU,而不是推出一种新的API即可。 再等两年来验证这条吧。
前景如何? Google已经公开支持Vulkan了,未来其将成为Android平台的首选图形API。 AMD自然是非常积极,NVIDIA的driver推出速度也很快,过一段时间在看看其他处理器厂商的动态吧。
 
附: 如果你有空余时间,还是找来Vulkan spec来过一遍为佳,花费不了多少时间的。实际编码的时候再仔细参考即可。
 
NVIDIA driver 361.91 不支持Vulkan。 下载LunarG的程序,运行 vulkaninfo 程序,就可以知道安装Vulkan Driver 成功与否了。 NVIDIA的 Vulkan beta 版驱动不稳定,我的台式机一天之内蓝屏死机三次了。
 
Ref
  1. http://news.mydrivers.com/1/470/470310.htm
  2. https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html   
  3. https://developer.nvidia.com/Vulkan  
  4. https://en.wikipedia.org/wiki/Vulkan_%28API%29
  5. https://en.wikipedia.org/wiki/Comparison_of_OpenGL_and_Direct3D
  6. http://www.extremetech.com/gaming/133824-valve-opengl-is-faster-than-directx-even-on-windows  Valve 的文章很好,果然是大公司
  7. http://www.slideshare.net/CassEveritt/approaching-zero-driver-overhead
  8. https://www.youtube.com/watch?v=qZLzz3OOl3A      AMD提供的 介绍Vulkan 起源的简述
  9. https://www.youtube.com/watch?v=rGY0PTh8rho      Vulkan vs. DirectX 11 Benchmark
  10. https://www.youtube.com/watch?v=NqensKmmRfE  这是介绍Vulkan设计的很好的一个讲解视频
  11. https://www.youtube.com/watch?v=zFJs8pQYdvs      这里展示了Vulkan明显比OpenGL ES 高效
  12. https://www.khronos.org/assets/uploads/developers/library/overview/vulkan-overview.pdf
  13. https://vulkan-tutorial.com/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules
[ 主页] 
1 0