Debian Codegraph @

third_party/rust/glsl/README.md

raw
1 [![Build Status](https://travis-ci.org/phaazon/glsl.svg?branch=master)](https://travis-ci.org/phaazon/glsl)
2 [![crates.io](https://img.shields.io/crates/v/glsl.svg)](https://crates.io/crates/glsl)
3 [![docs.rs](https://docs.rs/glsl/badge.svg)](https://docs.rs/glsl)
4 ![License](https://img.shields.io/badge/license-BSD3-blue.svg?style=flat)
5
6 <!-- cargo-sync-readme start -->
7
8 This crate is a GLSL450/GLSL460 compiler. It’s able to parse valid GLSL formatted source into
9 an abstract syntax tree (AST). That AST can then be transformed into SPIR-V, your own format or
10 even folded back to a raw GLSL [`String`] (think of a minifier, for instance).
11
12 You’ll find several modules:
13
14 - [`parser`], which exports the parsing interface. This is the place you will get most
15 interesting types and traits, such as [`Parse`] and [`ParseError`].
16 - [`syntax`], which exports the AST and language definitions. If you look into destructuring,
17 transpiling or getting information on the GLSL code that got parsed, you will likely
18 manipulate objects which types are defined in this module.
19 - [`transpiler`], which provides you with GLSL transpilers. For instance, you will find _GLSL
20 to GLSL_ transpiler, _GLSL to SPIR-V_ transpiler, etc.
21 - [`visitor`](visitor), which gives you a way to visit AST nodes and mutate them, both with
22 inner and outer mutation.
23
24 Feel free to inspect those modules for further information.
25
26 # GLSL parsing and transpiling
27
28 Parsing is the most common operation you will do. It is not required per-se (you can still
29 create your AST by hand or use [glsl-quasiquote] to create it at compile-time by using the GLSL
30 syntax directly in Rust). However, in this section, we are going to see how we can parse from a
31 string to several GLSL types.
32
33 ## Parsing architecture
34
35 Basically, the [`Parse`] trait gives you all you need to start parsing. This crate is designed
36 around the concept of type-driven parsing: parsers are hidden and you just have to state what
37 result type you expect.
38
39 The most common type you want to parse to is [`TranslationUnit`], which represents a set of
40 [`ExternalDeclaration`]s. An [`ExternalDeclaration`] is just a declaration at the top-most level
41 of a shader. It can be a global, uniform declarations, vertex attributes, a function, a
42 structure, etc. In that sense, a [`TranslationUnit`] is akin to a shader stage (vertex shader,
43 fragment shader, etc.).
44
45 You can parse any type that implements [`Parse`]. Parsers are mostly sensible to external
46 blanks, which means that parsing an [`Expr`] starting with a blank will not work (this is not
47 true for a [`TranslationUnit`] as it’s exceptionnally more permissive).
48
49 ## Parsing an expression
50
51 Let’s try to parse an expression.
52
53 ```rust
54 use glsl::parser::Parse as _;
55 use glsl::syntax::Expr;
56
57 let glsl = "(vec3(r, g, b) * cos(t * PI * .5)).xxz";
58 let expr = Expr::parse(glsl);
59 assert!(expr.is_ok());
60 ```
61
62 Here, `expr` is an AST which type is `Result<Expr, ParseError>` that represents the GLSL
63 expression `(vec3(r, g, b) * cos(t * PI * .5)).xxz`, which is an outer (scalar) multiplication
64 of an RGB color by a cosine of a time, the whole thing being
65 [swizzled](https://en.wikipedia.org/wiki/Swizzling_\(computer_graphics\)) with XXZ. It is your
66 responsibility to check if the parsing process has succeeded.
67
68 In the previous example, the GLSL string is a constant and hardcoded. It could come from a file,
69 network or built on the fly, but in the case of constant GLSL code, it would be preferable not
70 to parse the string at runtime, right? Well, [glsl-quasiquote] is there exactly for that. You
71 can ask **rustc** to parse that string and, if the parsing has succeeded, inject the AST
72 directly into your code. No [`Result`], just the pure AST. Have a look at [glsl-quasiquote] for
73 further details.
74
75 ## Parsing a whole shader
76
77 Vertex shaders, geometry shaders, fragment shaders and control and evaluation tessellation
78 shaders can be parsed the same way by using one of the `TranslationUnit` or `ShaderStage` types.
79
80 Here, a simple vertex shader being parsed.
81
82 ```rust
83 use glsl::parser::Parse as _;
84 use glsl::syntax::ShaderStage;
85
86 let glsl = "
87 layout (location = 0) in vec3 pos;
88 layout (location = 1) in vec4 col;
89
90 out vec4 v_col;
91
92 uniform mat4 projview;
93
94 void main() {
95 v_col = col; // pass color to the next stage
96 gl_Position = projview * vec4(pos, 1.);
97 }
98 ";
99 let stage = ShaderStage::parse(glsl);
100 assert!(stage.is_ok());
101 ```
102
103 ## Visiting AST nodes
104
105 The crate is also getting more and more combinators and functions to transform the AST or create
106 nodes with regular Rust. The [`Visitor`] trait will be a great friend of yours when you will
107 want to cope with deep mutation, filtering and validation. Have a look at the
108 [`visitor`](visitor) module for a tutorial on how to use visitors.
109
110 # About the GLSL versions…
111
112 This crate can parse both GLSL450 and GLSL460 formatted input sources. At the language level,
113 the difference between GLSL450 and GLSL460 is pretty much nothing, so both cases are covered.
114
115 > If you’re wondering, the only difference between both versions is that in GLSL460, it’s
116 > authorized to have semicolons (`;`) on empty lines at top-level in a shader.
117
118 [glsl-quasiquote]: https://crates.io/crates/glsl-quasiquote
119 [`Parse`]: crate::parser::Parse
120 [`ParseError`]: crate::parser::ParseError
121 [`ExternalDeclaration`]: crate::syntax::ExternalDeclaration
122 [`TranslationUnit`]: crate::syntax::TranslationUnit
123 [`Expr`]: crate::syntax::Expr
124 [`Visitor`]: crate::visitor::Visitor
125
126 <!-- cargo-sync-readme end -->