Skip to content

Compilation

Youness KAFIA edited this page Jan 19, 2024 · 3 revisions

Compilation

The compilation process goes through 3 steps :

  1. Parsing of shader mixins
  2. Generation of SPIR-V from mixins, resulting in SPIR-V mixins
  3. Mixing of those SPIR-V bytecode into a final shader

The previous implementation was working through the AST to do the mixin system, going for the SPIR-V mixin system helps us mix with binary data instead of textual. This also enables us to use SPIR-V Cross and Naga for cross shader compilation.

Parsing

Grammars

Parsing is done with Eto.Parse, the SDSL grammar is what defines the language.

There is also a grammar for comments and directives, the latter is not used fully functional so directives are handled by CPP.NET.

For the langage there is first a comment prepass where comments are removed, then the directive compilation and finally the resulting code is transformed into an AST

AST

The Abstract syntax tree is built right after the syntax parse tree is generated in the GetToken method.

As opposed to the previous shader system, those AST are not supposed to be operated on as they will only serve to generate partial spir-v binaries.

Semantic analysis

Some semantic analysis passes include :

  • Type checking
  • Scope resolution
  • Declaration rules
  • Flow control checks
  • Unique ID checks
  • Control flow analysis

The control flow analysis generates a control flow graph (CFG) that will help heavily in generating the SPIR-V assembly.

SPIR-V Assembler

The backend will use SoftTouch.Spirv to generate spir-v assembly. In this library the spir-v specification has been extended to include instructions for SDSL, helping the compiler build final spir-v shaders from mixins.

In this library a Mixer can be created to generate a new mixin. It can do operations like Inherit, Compose and also generate functions through a small abstraction over spir-v.

It's designed to have very low allocations, meaning all unecessary allocations are avoided, to make sure shader compilation doesn't hinder on performance.

Clone this wiki locally