2020-09-21 16:00:11 +00:00
|
|
|
# Tint Architecture
|
|
|
|
|
|
|
|
```
|
2021-02-23 16:08:09 +00:00
|
|
|
┏━━━━━━━━┓ ┏━━━━━━┓
|
|
|
|
┃ SPIR━V ┃ ┃ WGSL ┃
|
|
|
|
┗━━━━┃━━━┛ ┗━━━┃━━┛
|
|
|
|
▼ ▼
|
|
|
|
┏━━━━━━━━━┃━━━━━━━━━━━━━━━━━━━━━━━━━━━┃━━━━━━━━┓
|
|
|
|
┃ ┃ Reader ┃ ┃
|
|
|
|
┃ ┃ ┃ ┃
|
|
|
|
┃ ┏━━━━━━━┻━━━━━━┓ ┏━━━━━━┻━━━━━━┓ ┃
|
|
|
|
┃ ┃ SPIRV-Reader ┃ ┃ WGSL-Reader ┃ ┃
|
|
|
|
┃ ┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛ ┃
|
|
|
|
┗━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┛
|
|
|
|
▼
|
|
|
|
┏━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━┓
|
|
|
|
┃ ProgramBuilder ┃
|
|
|
|
┃ (mutable) ┃
|
|
|
|
┏━━━━━━━━━━━━►┫ ┏━━━━━┓ ┏━━━━━━━┓ ┏━━━━━━━━━┓ ┃
|
|
|
|
┃ ┃ ┃ AST ┃ ┃ Types ┃ ┃ Symbols ┃ ┃
|
|
|
|
┃ ┃ ┗━━━━━┛ ┗━━━━━━━┛ ┗━━━━━━━━━┛ ┃
|
|
|
|
┃ ┗━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┛
|
|
|
|
┃ ▼
|
|
|
|
┃ ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┃┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
|
|
|
|
▲ ┆ Build ▼ ┆
|
|
|
|
┏━━━┻━━━┓ ┆ ┏━━━━━━━━┻━━━━━━━━┓ ┆
|
2021-03-09 10:54:37 +00:00
|
|
|
┃ Clone ┃ ┆ ┃ Resolver ┃ ┆
|
2021-02-23 16:08:09 +00:00
|
|
|
┗━━━┳━━━┛ ┆ ┗━━━━━━━━━━━━━━━━━┛ ┆
|
|
|
|
▲ └┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┃┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
|
|
|
|
┃ ▼
|
|
|
|
┃ ┏━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━┓
|
|
|
|
┃ ┃ Program ┃
|
|
|
|
┃ ┃ (immutable) ┃
|
|
|
|
┣━━━━━━◄┫ ┏━━━━━┓ ┏━━━━━━━┓ ┏━━━━━━━━━━┓ ┏━━━━━━━━━┓ ┃
|
|
|
|
┃ ┃ ┃ AST ┃ ┃ Types ┃ ┃ Semantic ┃ ┃ Symbols ┃ ┃
|
|
|
|
┃ ┃ ┗━━━━━┛ ┗━━━━━━━┛ ┗━━━━━━━━━━┛ ┗━━━━━━━━━┛ ┃
|
|
|
|
┃ ┗━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┛
|
|
|
|
▲ ▼
|
2021-04-08 14:08:47 +00:00
|
|
|
┏━━━━━┻━━━━━┓ ┃ ┏━━━━━━━━━━━┓
|
|
|
|
┃ Transform ┃◄━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━►┃ Inspector ┃
|
|
|
|
┗━━━━━━━━━━━┛ ┃ ┗━━━━━━━━━━━┛
|
2021-02-23 16:08:09 +00:00
|
|
|
▼
|
|
|
|
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
|
|
|
┃ Writer ┃
|
|
|
|
┃ ┃
|
|
|
|
┃ ┏━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━┓ ┃
|
|
|
|
┃ ┃ SPIRV-Writer ┃ ┃ WGSL-Writer ┃ ┃ HLSL-Writer ┃ ┃ MSL-Writer ┃ ┃
|
|
|
|
┃ ┗━━━━━━━┳━━━━━━┛ ┗━━━━━━┳━━━━━━┛ ┗━━━━━━┳━━━━━━┛ ┗━━━━━━┳━━━━━┛ ┃
|
|
|
|
┗━━━━━━━━━┃━━━━━━━━━━━━━━━━━━┃━━━━━━━━━━━━━━━━━━┃━━━━━━━━━━━━━━━━━━┃━━━━━━━┛
|
|
|
|
▼ ▼ ▼ ▼
|
|
|
|
┏━━━━┻━━━┓ ┏━━━┻━━┓ ┏━━━┻━━┓ ┏━━┻━━┓
|
|
|
|
┃ SPIR-V ┃ ┃ WGSL ┃ ┃ HLSL ┃ ┃ MSL ┃
|
|
|
|
┗━━━━━━━━┛ ┗━━━━━━┛ ┗━━━━━━┛ ┗━━━━━┛
|
2020-09-21 16:00:11 +00:00
|
|
|
```
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
## Reader
|
|
|
|
|
|
|
|
Readers are responsible for parsing a shader program and populating a
|
|
|
|
`ProgramBuilder` with the parsed AST, type and symbol information.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
|
|
|
The WGSL reader is a recursive descent parser. It closely follows the WGSL
|
|
|
|
grammar in the naming of the parse methods.
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
## ProgramBuilder
|
|
|
|
|
|
|
|
A `ProgramBuilder` is the primary interface to construct an immutable `Program`.
|
|
|
|
There are a number of methods exposed which make creating of the `Program`
|
|
|
|
simpler. A `ProgramBuilder` can only be used once, and must be discarded after
|
|
|
|
the `Program` is constructed.
|
|
|
|
|
|
|
|
A `Program` is built from the `ProgramBuilder` by `std::move()`ing the
|
2021-03-09 10:54:37 +00:00
|
|
|
`ProgramBuilder` to a new `Program` object. When built, resolution is performed
|
|
|
|
so the produced `Program` will contain all the needed semantic information.
|
2021-02-23 16:08:09 +00:00
|
|
|
|
|
|
|
At any time before building the `Program`, `ProgramBuilder::IsValid()` may be
|
|
|
|
called to ensure the AST is **structurally** correct. This checks that things
|
|
|
|
like `if` statements have a condition and body attached.
|
|
|
|
|
|
|
|
If further changes to the `Program` are needed (say via a `Transform`) then a
|
|
|
|
new `ProgramBuilder` can be produced by cloning the `Program` into a new
|
|
|
|
`ProgramBuilder`.
|
|
|
|
|
|
|
|
Unlike `Program`s, `ProgramBuilder`s are not part of the public Tint API.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
|
|
|
## AST
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
The Abstract Syntax Tree is a directed acyclic graph of `ast::Node`s which
|
|
|
|
encode the syntactic structure of the WGSL program.
|
|
|
|
|
|
|
|
The root of the AST is the `ast::Module` class which holds each of the declared
|
|
|
|
functions, variables and user defined types (type aliases and structures).
|
|
|
|
|
|
|
|
Each `ast::Node` represents a **single** part of the program's source, and so
|
|
|
|
`ast::Node`s are not shared.
|
|
|
|
|
|
|
|
The AST does not perform any verification of its content. For example, the
|
2022-02-02 23:07:11 +00:00
|
|
|
`ast::StrideAttribute` node has numeric stride parameter, which is a count of
|
2021-02-23 16:08:09 +00:00
|
|
|
the number of bytes from the start of one array element to the start of the
|
|
|
|
next. The AST node itself does not constrain the set of stride values that you
|
|
|
|
can set, aside from storing it as an unsigned integer.
|
|
|
|
|
|
|
|
## Types
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Types are constructed during the Reader and resolution phases, and are
|
2021-02-23 16:08:09 +00:00
|
|
|
held by the `Program` or `ProgramBuilder`. AST and semantic nodes can both
|
|
|
|
reference types.
|
|
|
|
|
|
|
|
Each `type::Type` node **uniquely** represents a particular spelling of a WGSL
|
|
|
|
type within the program, so you can compare `type::Type*` pointers to check for
|
|
|
|
equivalence of type expressions.
|
|
|
|
For example, there is only one `type::Type` node for the `i32` type, no matter
|
|
|
|
how many times it is mentioned in the source program.
|
|
|
|
However, if `MyI32` is a type alias for `i32`, then they will have two different
|
|
|
|
type nodes.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
## Semantic information
|
2020-10-08 21:18:45 +00:00
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
Semantic information is held by `sem::Node`s which describe the program at
|
2021-02-23 16:08:09 +00:00
|
|
|
a higher / more abstract level than the AST. This includes information such as
|
2022-02-02 23:07:11 +00:00
|
|
|
the resolved type of each expression, the resolved overload of a builtin
|
2021-02-23 16:08:09 +00:00
|
|
|
function call, and the module scoped variables used by each function.
|
2020-10-08 21:18:45 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Semantic information is generated by the `Resolver` when the `Program`
|
2021-02-23 16:08:09 +00:00
|
|
|
is built from a `ProgramBuilder`.
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
The `sem::Info` class holds a map of `ast::Node`s to `sem::Node`s.
|
2021-02-23 16:08:09 +00:00
|
|
|
This map is **many-to-one** - i.e. while a AST node might have a single
|
|
|
|
corresponding semantic node, the reverse may not be true. For example:
|
2021-04-16 19:07:51 +00:00
|
|
|
many `ast::IdentifierExpression` nodes may map to a single `sem::Variable`,
|
|
|
|
and so the `sem::Variable` does not have a single corresponding
|
2021-02-23 16:08:09 +00:00
|
|
|
`ast::Node`.
|
|
|
|
|
|
|
|
Unlike `ast::Node`s, semantic nodes may not necessarily form a directed acyclic
|
|
|
|
graph, and the semantic graph may contain diamonds.
|
|
|
|
|
|
|
|
## Symbols
|
|
|
|
|
|
|
|
Symbols represent a unique string identifier in the source program. These string
|
|
|
|
identifiers are transformed into symbols within the `Reader`s.
|
|
|
|
|
|
|
|
During the Writer phase, symbols may be emitted as strings using a `Namer`.
|
|
|
|
A `Namer` may output the symbol in any form that preserves the uniqueness of
|
|
|
|
that symbol.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
## Resolver
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
The `Resolver` will automatically run when a `Program` is built.
|
|
|
|
A `Resolver` creates the `Program`s semantic information by analyzing the
|
2021-02-23 16:08:09 +00:00
|
|
|
`Program`s AST and type information.
|
|
|
|
|
2021-04-08 14:08:47 +00:00
|
|
|
The `Resolver` will validate to make sure the generated `Program` is
|
|
|
|
semantically valid.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
## Program
|
|
|
|
|
|
|
|
A `Program` holds an immutable version of the information from the
|
|
|
|
`ProgramBuilder` along with semantic information generated by the
|
2021-03-09 10:54:37 +00:00
|
|
|
`Resolver`.
|
2021-02-23 16:08:09 +00:00
|
|
|
|
|
|
|
Like `ProgramBuilder`, `Program::IsValid()` may be called to ensure the AST is
|
2021-04-08 14:08:47 +00:00
|
|
|
structurally correct and semantically valid, and that the `Resolver` did not
|
|
|
|
report any errors.
|
2021-02-23 16:08:09 +00:00
|
|
|
|
|
|
|
Unlike the `ProgramBuilder`, a `Program` is fully immutable, and is part of the
|
|
|
|
public Tint API. The immutable nature of `Program`s make these entirely safe
|
|
|
|
to share between multiple threads without the use of synchronization primitives.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2020-10-08 14:24:55 +00:00
|
|
|
## Inspector
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
The inspectors job is to go through the `Program` and pull out various pieces of
|
2020-10-08 14:24:55 +00:00
|
|
|
information. The information may be used to pass information into the downstream
|
|
|
|
compilers (things like specialization constants) or may be used to pass into
|
|
|
|
transforms to update the AST before generating the resulting code.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
The input `Program` to the inspector must be valid (pass validation).
|
|
|
|
|
2020-09-21 16:00:11 +00:00
|
|
|
## Transforms
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
There maybe various transforms we want to run over the `Program`.
|
|
|
|
This is for things like Vertex Pulling or Robust Buffer Access.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
A transform operates by cloning the input `Program` into a new `ProgramBuilder`,
|
|
|
|
applying the required changes, and then finally building and returning a new
|
2021-03-09 10:54:37 +00:00
|
|
|
output `Program`. As the resolver is always run when a `Program` is built,
|
2021-02-23 16:08:09 +00:00
|
|
|
Transforms will always emit a `Program` with semantic information.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
The input `Program` to a transform must be valid (pass validation).
|
|
|
|
If the input `Program` of a transform is valid then the transform must guarantee
|
|
|
|
that the output program is also valid.
|
2020-09-21 16:00:11 +00:00
|
|
|
|
|
|
|
## Writers
|
|
|
|
|
2021-02-23 16:08:09 +00:00
|
|
|
A writer is responsible for writing the `Program` in the target shader language.
|
|
|
|
|
|
|
|
The input `Program` to a writer must be valid (pass validation).
|