LoginSignup
16
17

More than 5 years have passed since last update.

Vulkanことはじめ

Posted at

Vulkanに手を出してみた。

Windowsで試してみたところ7でも10でも新しめのGPUならばわりと動く。
OSではなくGPUに依存するようで、D3D12と違ってWindows7でも動くのは地味にありがたい。
クンフーを積みながらVulkanが動くAndroidが手に入るのを待とうかという気持ち。

でも、先に言っておくと三角形出すのに2000行以上かかる。
結構大変。

今回のコード。

ss.png

環境は、Windows10 + vs2015。ウインドウを出すのにglfwを使ってみた。

はじめ、
https://github.com/ColonelThirtyTwo/dvulkan
でやろうとしていたのだけど途中で嵌って解決できなかったのでC++で腕慣らしでございます。
ある程度マスターしたらD言語で再挑戦するつもりである。
APIの呼び出し手順等はコードを見てもらった方が早いので、以下それ以外のことを書く。

デバッグプリント機能は必須

これがあるとAPIの呼び出しを間違えたときにエラーメッセージを得ることができるのでとても捗る。
vkCreateInstanceの直後にやるとよい。

参考

ある程度OpenGLやDirectXの知識がある人向けに違うところをさらっと説明してくれる。

Vulkanが扱うシェーダーはSPIR-V

バイナリ形式の仕様が決まっていてSPIR-Vというらしい。
どうやってSPIR-V形式を得るのかといえばコンパイラがgithubにある。

これをビルドすると

glslangValidator.exe

を得られるのだけど、SDKにビルド済みが最初から入っている。

C:/VulkanSDK/1.0.13.0/Bin/glslangValidator.exe

これがあればglslをSPIR-V形式にプリコンパイルできる。

Usage: glslangValidator [option]... [file]...

Where: each 'file' ends in .<stage>, where <stage> is one of
    .conf   to provide an optional config file that replaces the default configuration
            (see -c option below for generating a template)
    .vert   for a vertex shader
    .tesc   for a tessellation control shader
    .tese   for a tessellation evaluation shader
    .geom   for a geometry shader
    .frag   for a fragment shader
    .comp   for a compute shader

Compilation warnings and errors will be printed to stdout.

To get other information, use one of the following options:
Each option must be specified separately.
  -V          create SPIR-V binary, under Vulkan semantics; turns on -l;
              default file name is <stage>.spv (-o overrides this)
              (unless -o is specified, which overrides the default file name)
  -G          create SPIR-V binary, under OpenGL semantics; turns on -l;
              default file name is <stage>.spv (-o overrides this)
  -H          print human readable form of SPIR-V; turns on -V
  -E          print pre-processed GLSL; cannot be used with -l;
              errors will appear on stderr.
  -c          configuration dump;
              creates the default configuration file (redirect to a .conf file)
  -d          default to desktop (#version 110) when there is no shader #version
              (default is ES version 100)
  -D          input is HLSL
  -e          specify entry-point name
  -h          print this usage message
  -i          intermediate tree (glslang AST) is printed out
  -l          link all input files together to form a single module
  -m          memory leak mode
  -o  <file>  save binary into <file>, requires a binary option (e.g., -V)
  -q          dump reflection query database
  -r          relaxed semantic error-checking mode
  -s          silent mode
  -t          multi-threaded mode
  -v          print version strings
  -w          suppress warnings (except as required by #extension : warn)

> glslangValidator.exe hoge.vert -V -l -o hoge.vert.spv

参考にした
https://github.com/LunarG/VulkanSamples/blob/master/API-Samples/15-draw_cube/15-draw_cube.cpp
はライブラリとして使って、glslからSPIR-Vにコンパイルして使うコードが入っていたので取り入れてみた。
オープンソースのライブラリになっているのでリフレクションとかシェーダーエディタとかツールの開発ができそうなのでそっち方面にも期待。中身は全然確認できていないのだけどhlslのディレクトリもあるので、いろんなシェーダー言語をSPIR-V形式にコンパイルするということですな。しかし、これ何と発音すればいいのだろう。「すぴあーぶい」とかなのか・・・

終わり

Vulkanが流行るのかどうかまだ分らないけれど、新しめのGPUに最新のドライバをインストールしていれば、VulkanSDKをインストールするだけでわりと開発できる。もう始めても良いかと思う。

16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17