Live data from Hacker News

A Guide to Rust Graphics Libraries as of 2019

wiki.alopex.li

1–10 of 27 posts

Re: A Guide to Rust Graphics Libraries as of 2019

#2
Great write-up! I'd really like to know how gfx-hal handles shaders. Do you write shaders in some high-level language, and gfx translates them appropriately for the vulkan/metal/dx backends, and send them off to the GPU driver? If so, is this transpiling slow? Do you get slightly different visuals on different backends? Or even glitches on some? Thanks for the write-up :)

Re: A Guide to Rust Graphics Libraries as of 2019

#3
post #2

Great write-up! I'd really like to know how gfx-hal handles shaders. Do you write shaders in some high-level language, and gfx translates them appropriately for the vulkan/metal/dx backends, and send them off to the GPU driver? If so, is this transpiling slow? Do you get slightly different visuals on different backends? Or even glitches on some? Thanks for the write-up :)

I'm not familiar with the specifics within gfx-hal, but it's most likely taking in SPIR-V and translating as needed.

AFAIK DX12 still requires DXIL so it might be saving both SPIR-V and DXIL, or taking in HLSL and compiling/translating as needed.

Microsoft has generally gotten a lot friendlier in the last decade, they have tools to compile HLSL down to SPIR-V, if you want to support everything including DX12 you'll probably be writing HLSL. Otherwise you can pick any language that compiles down to SPIR-V.

https://github.com/Microsoft/ShaderConductor

https://github.com/KhronosGroup/SPIRV-Cross

Re: A Guide to Rust Graphics Libraries as of 2019

#4
post #2

Great write-up! I'd really like to know how gfx-hal handles shaders. Do you write shaders in some high-level language, and gfx translates them appropriately for the vulkan/metal/dx backends, and send them off to the GPU driver? If so, is this transpiling slow? Do you get slightly different visuals on different backends? Or even glitches on some? Thanks for the write-up :)

I'm not familiar with the specifics within gfx-hal, but it's most likely taking in SPIR-V and translating as needed. AFAIK DX12 still requires DXIL so it might be saving both SPIR-V and DXIL, or taking in HLSL and compiling/translating as needed. Microsoft has generally gotten a lot friendlier in the last decade, they have tools to compile HLSL down to SPIR-V, if you want to support everything including DX12 you'll p…

So from what I can find, it seems gfx-hal works with GLSL, compiles it to SPIRV, and then uses that (somehow). I'm just not sure how it uses the SPIRV for each backend.

Re: A Guide to Rust Graphics Libraries as of 2019

#5
post #4

Earlier quoted context omitted.

I'm not familiar with the specifics within gfx-hal, but it's most likely taking in SPIR-V and translating as needed. AFAIK DX12 still requires DXIL so it might be saving both SPIR-V and DXIL, or taking in HLSL and compiling/translating as needed. Microsoft has generally gotten a lot friendlier in the last decade, they have tools to compile HLSL down to SPIR-V, if you want to support everything including DX12 you'll p…

So from what I can find, it seems gfx-hal works with GLSL, compiles it to SPIRV, and then uses that (somehow). I'm just not sure how it uses the SPIRV for each backend.

The first link I included has a wonderful diagram of this (but from the perspective of HLSL instead of GLSL), the gist of it is:

GLSL gets compiled to SPIR-V ahead of time by a compiler of your choice (probably glslangValidator). You can take that binary blob and feed it into Vulkan (vkCreateShaderModule) or OpenGL (glShaderBinary as long as you have the right extension).

For everything else, Khronos has a tool called SPIRV-Cross (second link), which reads the SPIR-V binary data and emits a text file/string in ESSL for OpenGL ES, MSL for Metal, or HLSL for DirectX <=11. Those all go through the "normal" paths for loading shader code in their own APIs.

Re: A Guide to Rust Graphics Libraries as of 2019

#6
BTW, I just wanted to give a shout out to ggez (the author's game library). I've been playing with it and it's quite a lot of fun to use. I'm a novice in Rust and I found it to be quite a nice introduction to the language. There are a couple of bugs here and there, but pretty much it does what's printed on the box. Very nice documentation and lots of examples to work with too. And the cherry on the cake is raising an issue is a welcoming rather than harrowing experience. You get the feeling that whatever issues might crop up, you can work your way through them.

Edit: grammar

Re: A Guide to Rust Graphics Libraries as of 2019

#7
post #4

Earlier quoted context omitted.

So from what I can find, it seems gfx-hal works with GLSL, compiles it to SPIRV, and then uses that (somehow). I'm just not sure how it uses the SPIRV for each backend.

The first link I included has a wonderful diagram of this (but from the perspective of HLSL instead of GLSL), the gist of it is: GLSL gets compiled to SPIR-V ahead of time by a compiler of your choice (probably glslangValidator). You can take that binary blob and feed it into Vulkan (vkCreateShaderModule) or OpenGL (glShaderBinary as long as you have the right extension). For everything else, Khronos has a tool calle…

Thanks so much for explaining how that works :)

Is it just me or does decompiling the binary SPIR-V to ESL/HLSL source code then recompiling sound like a recipe for massive inefficiency? Or in practice does it work out pretty nicely?

Re: A Guide to Rust Graphics Libraries as of 2019

#8
post #4

Earlier quoted context omitted.

I'm not familiar with the specifics within gfx-hal, but it's most likely taking in SPIR-V and translating as needed. AFAIK DX12 still requires DXIL so it might be saving both SPIR-V and DXIL, or taking in HLSL and compiling/translating as needed. Microsoft has generally gotten a lot friendlier in the last decade, they have tools to compile HLSL down to SPIR-V, if you want to support everything including DX12 you'll p…

So from what I can find, it seems gfx-hal works with GLSL, compiles it to SPIRV, and then uses that (somehow). I'm just not sure how it uses the SPIRV for each backend.

Take another look at SPIRV-Cross. It's a C++ library that can convert SPIR-V to HLSL, MSL or legacy GLSL/ESSL. I'm not too familiar with Rust or gfx, but it seems like the dx11, dx12, metal and gl backends reference the spirv_cross crate, so that's probably what it's using.

To try to answer your original question, I've never used gfx, but we use SPIRV-Cross in mpv to translate video processing shaders written in GLSL to HLSL for Direct3D 11, and it works quite well. Modern GLSL and HLSL seem to be pretty similar, apart from using different coordinate systems. Most HLSL concepts directly map to a GLSL concept (eg. GLSL UBOs become HLSL cbuffers, etc.) Shader translation with SPIRV-Cross definitely isn't the slowest part of our shader compilation pipeline, either. D3DCompile is much slower.

Re: A Guide to Rust Graphics Libraries as of 2019

#9

BTW, I just wanted to give a shout out to ggez (the author's game library). I've been playing with it and it's quite a lot of fun to use. I'm a novice in Rust and I found it to be quite a nice introduction to the language. There are a couple of bugs here and there, but pretty much it does what's printed on the box. Very nice documentation and lots of examples to work with too. And the cherry on the cake is raising an…

If you are interested in hearing more from the author of ggez, check out their talk from Rust Belt Rust 2018:

- Evolving API design in Rust — Simon Nicholas Heath (https://www.youtube.com/watch?v=Xt1JOVeQ5hw)

Re: A Guide to Rust Graphics Libraries as of 2019

#10
post #2

Great write-up! I'd really like to know how gfx-hal handles shaders. Do you write shaders in some high-level language, and gfx translates them appropriately for the vulkan/metal/dx backends, and send them off to the GPU driver? If so, is this transpiling slow? Do you get slightly different visuals on different backends? Or even glitches on some? Thanks for the write-up :)

[deleted]
Post reply on HN