gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
11.51k stars 858 forks source link

DirectX IR #4302

Open Skepfyr opened 4 years ago

Skepfyr commented 4 years ago

DirectX 12, like Metal (#4), uses LLVM bitcode as its low-level representation which is actually quite well documented (DXIL). However, the diagram at the top of that documentation suggests we probably want to be targeting their high level IR (DXIR), which as far as I can tell is almost completly undocumented 😞, although is also LLVM IR. The optimisations done between DXIR and DXIL are:

  • optimize function parameter copies
  • inline functions
  • allocate and transform shader signatures
  • lower matrices, optimizing intermediate storage
  • linearize multi-dimensional arrays and user-defined type accesses
  • scalarize vectors

Which don't sound particularly fun, or reasonable, to implement, although it might be possible. This will involve linking to some part of the DirectXShaderCompiler, which shouldn't be too hard as it all currently builds (after jumping through every hoop) to a load of static libraries, once we've worked out which libraries we need.

Their compiler also can output back out to SPIR-V and they have a HLSL to SPIR-V feature map, which should come in handy.

Skepfyr commented 4 years ago

Some information about DXIL vs DXIR: https://github.com/microsoft/DirectXShaderCompiler/issues/2389

Lectem commented 3 years ago

Note that the DXIL document ( https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst#introduction ) says

Microsoft will publicly document DXIL and DXIR to attract domain language implementers and spur innovation.

Is releasing the DXIR specification still planned ?

Skepfyr commented 3 years ago

So as far as I can tell, no unless we bug them a lot or write it ourselves. The only guidance we've got is that issue I opened a while ago. Something else I worked out a while ago is that if we did produce DXIR we would need to ship dxcompiler in some form as it doesn't come with windows, which could cause some interesting licensing problems.

kvark commented 3 years ago

Here is the code in Mesa fork that Microsoft develops, it takes Mesa IR and targets DXIL (derived from LLVM 3.7 IR) without linking to LLVM. We should take it and develop our backend.

Edit: reading about http://www.jlekstrand.net/jason/projects/mesa/nir-notes/

jrmuizel commented 3 years ago

Having llvm bit code writing abilities could be useful targeting metal as well.

seanbaxter commented 3 years ago

DXIR has been formally disowned now: https://github.com/microsoft/DirectXShaderCompiler/commit/61c6573842be58a14e1dfc6b1b3def03d39d9988

I'm working on using the dxc llvm fork to emit DXIL from C++. However the code is extremely buggy, and a lot of the MS-written passes expect constructs coming in from the high-level HL/DXIR collection of types. If you try emitting DXIL intrinsics (like @dx.op.load/store/sample) and running the provided passes to perform inlining, scalarization, array flattening etc, stuff breaks. So I hack a lot on what they proivde. MS doesn't answer issues related to this. I think people who really want this working should get together and figure a way forward, because it's real hard to do this as an individual.

kvark commented 3 years ago

Latest matrix discussion. Thank you @seanbaxter for sharing the knowledge!