@@ -31,40 +31,46 @@ There are two main ways to setup your shader project locally.
31
31
flags in your cargo configuration to enable you to run ` cargo build ` in your
32
32
shader crate.
33
33
34
-
35
34
### Using ` spirv-builder `
36
35
If you're writing a bigger application and you want to integrate SPIR-V shader
37
36
crates to display, it's recommended to use ` spirv-builder ` in a build script.
38
37
39
38
1 . Copy the [ ` rust-toolchain ` ] file to your project. (You must use the same
40
- version of Rust as ` rust-gpu ` .)
39
+ version of Rust as ` rust-gpu ` . Utimately, the build will fail with a nice
40
+ error message when you don't use the exact same version)
41
41
2 . Reference ` spirv-builder ` in your Cargo.toml:
42
42
``` toml
43
43
[build-dependencies ]
44
- spirv-builder = { git = " https://github.com/EmbarkStudios/rust-gpu " }
44
+ spirv-builder = " 0.4 "
45
45
```
46
- (we currently do not publish spirv-builder on crates.io)
46
+ All dependent crates are published on [ crates.io](https://crates.io).
47
47
3. Create a `build.rs` in your project root.
48
48
49
49
# ### `build.rs`
50
- Paste the following into the `main` for your build script.
50
+ Paste the following into ` build.rs`
51
51
52
52
```rust,no_run
53
- SpirvBuilder::new(path_to_shader, target)
54
- .print_metadata(MetadataPrintout::Full)
55
- .build()?;
53
+ use spirv_builder::{MetadataPrintout, SpirvBuilder};
54
+
55
+ fn main() {
56
+ SpirvBuilder::new(shader_crate, target)
57
+ .print_metadata(MetadataPrintout::Full)
58
+ .build()
59
+ .unwrap();
60
+ }
56
61
```
57
62
58
- The values available for the ` target ` parameter are available
63
+ Substituting ` shader_crate ` with a relative path to your shader crate. The values available for the ` target ` parameter are available
59
64
[ here] ( ./platform-support.md ) . For example, if building for vulkan 1.1, use
60
65
` "spirv-unknown-vulkan1.1" ` .
61
66
62
67
The ` SpirvBuilder ` struct has numerous configuration options available, see
63
- rustdoc for documentation.
68
+ [ documentation] ( https://embarkstudios.github.io/rust-gpu/api/spirv_builder/struct.SpirvBuilder.html ) .
64
69
65
70
#### ` main.rs `
71
+ The following will directly include the shader module binary into your application.
66
72
``` rust,no_run
67
- const SHADER: &[u8] = include_bytes!(env!("<shader_name >.spv"));
73
+ const SHADER: &[u8] = include_bytes!(env!("<shader_crate >.spv"));
68
74
```
69
75
70
76
> ** Note** If your shader name contains hyphens, the name of environment variable will be the name with hyphens changed to underscores.
@@ -100,7 +106,7 @@ necessary flags. Before you can do that however you need to do a couple of steps
100
106
first to build the compiler backend.
101
107
102
108
1 . Clone the ` rust-gpu ` repository
103
- 3 . ` cargo build --release ` in ` rust-gpu ` .
109
+ 2 . ` cargo build --release ` in ` rust-gpu ` .
104
110
105
111
Now you should have a ` librustc_codegen_spirv ` dynamic library available in
106
112
` target/release ` . You'll need to keep this somewhere stable that you can
@@ -134,3 +140,26 @@ Now you should have `<project_name>.spv` SPIR-V file in `target/debug` that you
134
140
can give to a renderer.
135
141
136
142
[ `rust-toolchain` ] : https://github.com/EmbarkStudios/rust-gpu/blob/main/rust-toolchain
143
+
144
+ ## Writing your first shader
145
+
146
+ Configure your shader crate as a ` "dylib" ` type crate, and add ` spirv-std ` to its dependencies. The following example also enables the ` glam ` vector library.
147
+
148
+ ``` toml
149
+ [dependencies ]
150
+ spirv-std = { version = " 0.4" , features = [" glam" ] }
151
+ ```
152
+
153
+ Make sure your shader code uses the ` no_std ` attribute and makes the ` spirv ` attribute visibile in the global scope. Then, you're ready to write your first shader. Here's a very simple fragment shader called ` main_fs ` as an example that outputs the color red:
154
+
155
+ ``` rust,norun
156
+ #![no_std]
157
+
158
+ use spirv_std::spirv;
159
+ use spirv_std::glam::{vec4, Vec4};
160
+
161
+ #[spirv(fragment)]
162
+ pub fn main_fs(output: &mut Vec4) {
163
+ *output = vec4(1.0, 0.0, 0.0, 1.0);
164
+ }
165
+ ```
0 commit comments