pygfx / shadertoy

Shadertoy implementation based on wgpu-py
BSD 2-Clause "Simplified" License
15 stars 2 forks source link

Linting shaders #5

Open Korijn opened 11 months ago

Korijn commented 11 months ago

Can naga be used as a cli tool to lint shader files? Or are there other tools that can be used?

Vipitis commented 11 months ago

naga-cli can validate and translate shaders. This will give you errors (sometimes panics without an error) but not necessarily give you feedback about style or unused names etc. I have seen some "glsl lint" extensions for VSCode, but no experience with how they work or what they provide.

Korijn commented 10 months ago

I'm thinking this would mean building a separate pip installable CLI tool that uses naga-cli to validate .wgsl and .glsl files.

Vipitis commented 10 months ago

wgpu uses naga already to validate shaders. And errors do get passed all the way to Python. I use a simple try/except for my project. And this does work, apart from edge cases where the called process panics (it doesn't have any exit code). Some of these are already addressed and next wgpu-core release is targeting 17th January, which I hope includes all the naga fixes of the past 2 months.

So using naga-cli seems redundant. I wanted to parse the error message into a Python Exception with corrected span etc later on, since the shadertoy code isn't a valid fragment shader on it's own. You always have to add the compatibility code before it. perhaps introducing a .validate_shadertoy method directly to the Shadertoy class might be a good idea.

Korijn commented 10 months ago

I'm mostly thinking about having a CLI tool available that can be run on CI to validate all .glsl and .wgsl files. I think with what you're saying, it means we could build something like that on wgpu-py instead of on naga-cli.

Vipitis commented 10 months ago

yes, adding a cli tool for the functionality of the shadetoy util is also something I wanted to consider. But that requires features like file I/O for example.

Korijn commented 10 months ago

https://github.com/PolyMeilex/vscode-wgsl

Looks like this has both syntax highlighting and a CLI linting functionality

almarklein commented 10 months ago

I hacked together a VSCode extension to syntax-highlight shaders in Python strings: https://github.com/pygfx/python-string-shaders (using a similar extension for sql as inspiration).

The workflow is not as nice as when the shader is in its own file, because it only does highlighting, the language server does not kick in, so there is no autocompletion and code analysis. But perhaps a nice middle ground.