[Tint] Question about transpilers

20 views
Skip to first unread message

Ashwin Bhat

unread,
Nov 18, 2025, 6:54:40 PM (14 days ago) Nov 18
to Dawn Graphics
Hi
I have a few beginner question about transpiling using tint.  

1. Intermediate variables and assignments
We often see temporary variables created and some variables getting renamed when shader code from glsl is transpiled to wgsl. Why is generally done and what can we do in glsl source to avoid this? 

2. Retaining code comments, function names and order.
I understand comments are generally treated as whitespace by compilers and are stripped during the initial parsing/lexing phase as they are not part of the abstract syntax tree (AST) used for the actual compilation and transpilation process. But Is there a way to ensure that code comments are transferred to transpiled code? 

Apologies in advance if this has been already discussed before.
Thanks
Ashwin Bhat.

dan sinclair

unread,
Nov 18, 2025, 9:59:59 PM (14 days ago) Nov 18
to Ashwin Bhat, Dawn Graphics
On Tue, Nov 18, 2025 at 6:54 PM 'Ashwin Bhat' via Dawn Graphics <dawn-g...@googlegroups.com> wrote:
1. Intermediate variables and assignments
We often see temporary variables created and some variables getting renamed when shader code from glsl is transpiled to wgsl. Why is generally done and what can we do in glsl source to avoid this? 

The answer is probably, it depends. We're trying to reconstruct the original from the SPIR-V representation. That means we have to rediscover things like loops and if blocks. There are some constructs in SPIR-V that require extra variables for us to be able to convert to WGSL. (This can happen with OpPhi variables and SSA values which appear in nested constructs among other things).

It's hard to say without seeing the SPIR-V why we created extra variables. We try to reuse any OpName or OpMemberName which is provided in the source. There are also possible bugs in the SPIR-V Reader which aren't handling the names correctly.

 
2. Retaining code comments, function names and order.
I understand comments are generally treated as whitespace by compilers and are stripped during the initial parsing/lexing phase as they are not part of the abstract syntax tree (AST) used for the actual compilation and transpilation process. But Is there a way to ensure that code comments are transferred to transpiled code? 

There is no way to do this easily. In order to restore code comments, they'd need something like the debug source in the SPIR-V which provides the original source code and then the debug annotations. Then you'd need to extract the source code, find the line and search around for comments then provide a way to pass that through to the WGSL.

Function names should be preserved already as long as they're in the SPIR-V (this may depend on how you're compiling the GLSL to SPIR-V as long as that preserves the name we should as well). Function ordering, there is no way to preserve the order, there is no guarantee the order in the SPIR-V matches your source GLSL, so we don't know what order things were in to begin with. 

If you need this level of faithful GLSL compiled to WGSL you'd be better served to write your own program that can maintain these invariants for you instead of going through SPIR-V which ends up being a lossy step.

dan

Reply all
Reply to author
Forward
0 new messages