thatcosmonaut / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
3 stars 3 forks source link

Clean up SDL_GpuShaderFormat #109

Closed mcclure closed 1 month ago

mcclure commented 1 month ago

I am very interested in the upcoming SDL-GPU but unfortunately I have not had time to review and test it. However a friend of mine showed me its approach to shader languages, the SDL_GpuShaderFormat enum:

https://github.com/thatcosmonaut/SDL/blob/gpu/include/SDL3/SDL_gpu.h#L193

I have not yet figured out where the documentation for this enum is (not totally clear if there's a specific place SDL-GPU documentation lives yet separate from the headers… a question is asked in https://github.com/libsdl-org/SDL/pull/9312 about this enum but it is not answered) but, I would like to request an entry for WGSL.

As you may know, WGSL is the shader language for WebGPU; although it is frankly infuriating that a decision was made to make WGSL mandatory for WebGPU web targets, once you get past that WGSL is actually pretty nice. It is nice enough that I would, at present, use WGSL when writing WebGPU even if I were targeting solely desktop targets, which support SPIR-V. Since I don't know HLSL and I wouldn't want to go back to GLSL after trying WGSL, if I were to try and choose to adopt SDL-GPU I'd probably initially prefer to use WGSL. I could get this by selecting SDL_GPU_SHADERFORMAT_SPIRV and compiling from WGSL, but a WGSL primitive might help with a future SDL-GPU port for wasm because it could allow wgsl to just be passed through on that platform.

Complications:

flibitijibibo commented 1 month ago

We ended up deciding to split the SPIRV-Cross dependency, so it'll be native-to-native only without any built in cross compilers:

https://github.com/thatcosmonaut/SDL/issues/106

So for built-in stuff I wouldn't worry about trying to upstream any translators, it can be a single-file header like it will be for SPIR-V.

I do think we could put SECRET into slot 0 though, and WGSL can be added later if anyone ever makes a webgpu backend.

thatcosmonaut commented 1 month ago

Worth noting that wgpu already has a WGSL -> SPIR-V compiler: https://github.com/gfx-rs/wgpu/tree/trunk/naga

With that tool you should be able to use WGSL if you prefer.

The only reason we accept MSL / HLSL directly is because they are able to be accepted by the backends which use them. If we ever got a webgpu backend then I think adding WGSL would be appropriate, but for now it would force a hard dependency on the webgpu toolchain which is no good.

thatcosmonaut commented 1 month ago

I actually noticed that naga has support for emitting HLSL and MSL now, so if your project's source language is WGSL you can emit shader code for all of our current backends without even needing a SPIRV-Cross dependency.

mcclure commented 1 month ago

Naga is what I use to compile wgsl in my Rust projects (it's the default) and in my experience it works well.

@kg pointed out to me it also currently advertises it can do ("primary support" level) Spir-V as input and ("secondary support" level, whatever that means) WGSL as output. So it is of utility both for WGSL as an input language when targeting non-WebGPU-on-web platforms, and for WGSL as a platform target (assuming either that your input is GLSL or wgsl, or you've already added a step to compile it to spir-v).

flibitijibibo commented 1 month ago

The enum cleanup is now in gpu_next, and it should be set up properly to support a WGSL value in the future if needed.