1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
//! The procedural macro for vulkano's shader system. //! Manages the compile-time compilation of GLSL into SPIR-V and generation of assosciated rust code. //! //! # Basic usage //! //! ``` //! mod vs { //! vulkano_shaders::shader!{ //! ty: "vertex", //! src: " //! #version 450 //! //! layout(location = 0) in vec3 position; //! //! void main() { //! gl_Position = vec4(position, 1.0); //! }" //! } //! } //! # fn main() {} //! ``` //! //! # Details //! //! If you want to take a look at what the macro generates, your best options //! are to either read through the code that handles the generation (the //! [`reflect`][reflect] function in the `vulkano-shaders` crate) or use a tool //! such as [cargo-expand][cargo-expand] to view the expansion of the macro in your //! own code. It is unfortunately not possible to provide a `generated_example` //! module like some normal macro crates do since derive macros cannot be used from //! the crate they are declared in. On the other hand, if you are looking for a //! high-level overview, you can see the below section. //! //! # Generated code overview //! //! The macro generates the following items of interest: //! * The `Shader` struct. This contains a single field, `shader`, which is an //! `Arc<ShaderModule>`. //! * The `Shader::load` constructor. This method takes an `Arc<Device>`, calls //! [`ShaderModule::new`][ShaderModule::new] with the passed-in device and the //! shader data provided via the macro, and returns `Result<Shader, OomError>`. //! Before doing so, it loops through every capability instruction in the shader //! data, verifying that the passed-in `Device` has the appropriate features //! enabled. **This function currently panics if a feature required by the shader //! is not enabled on the device.** At some point in the future it will return //! an error instead. //! * The `Shader::module` method. This method simply returns a reference to the //! `Arc<ShaderModule>` contained within the `shader` field of the `Shader` //! struct. //! * Methods for each entry point of the shader module. These construct and //! return the various entry point structs that can be found in the //! [vulkano::pipeline::shader][pipeline::shader] module. //! * A Rust struct translated from each struct contained in the shader data. //! * The `Layout` newtype. This contains a [`ShaderStages`][ShaderStages] struct. //! An implementation of [`PipelineLayoutDesc`][PipelineLayoutDesc] is also //! generated for the newtype. //! * The `SpecializationConstants` struct. This contains a field for every //! specialization constant found in the shader data. Implementations of //! `Default` and [`SpecializationConstants`][SpecializationConstants] are also //! generated for the struct. //! //! All of these generated items will be accessed through the module specified //! by `mod_name: foo` If you wanted to store the `Shader` in a struct of your own, //! you could do something like this: //! //! ``` //! # fn main() {} //! # use std::sync::Arc; //! # use vulkano::OomError; //! # use vulkano::device::Device; //! # //! # mod vs { //! # vulkano_shaders::shader!{ //! # ty: "vertex", //! # src: " //! # #version 450 //! # //! # layout(location = 0) in vec3 position; //! # //! # void main() { //! # gl_Position = vec4(position, 1.0); //! # }" //! # } //! # } //! // various use statements //! // `vertex_shader` module with shader derive //! //! pub struct Shaders { //! pub vs: vs::Shader //! } //! //! impl Shaders { //! pub fn load(device: Arc<Device>) -> Result<Self, OomError> { //! Ok(Self { //! vs: vs::Shader::load(device)?, //! }) //! } //! } //! ``` //! //! # Options //! //! The options available are in the form of the following attributes: //! //! ## `ty: "..."` //! //! This defines what shader type the given GLSL source will be compiled into. //! The type can be any of the following: //! //! * `vertex` //! * `fragment` //! * `geometry` //! * `tess_ctrl` //! * `tess_eval` //! * `compute` //! //! For details on what these shader types mean, [see Vulkano's documentation][pipeline]. //! //! ## `src: "..."` //! //! Provides the raw GLSL source to be compiled in the form of a string. Cannot //! be used in conjunction with the `path` field. //! //! ## `path: "..."` //! //! Provides the path to the GLSL source to be compiled, relative to `Cargo.toml`. //! Cannot be used in conjunction with the `src` field. //! //! ## `include: ["...", "...", ..., "..."]` //! //! Specifies the standard include directories to be searched through when using the //! `#include <...>` directive within a shader source. //! If `path` was specified, relative paths can also be used (`#include "..."`), without the need //! to specify one or more standard include directories. Relative paths are relative to the //! directory, which contains the source file the `#include "..."` directive is declared in. //! //! ## `dump: true` //! //! The crate fails to compile but prints the generated rust code to stdout. //! //! [reflect]: https://github.com/vulkano-rs/vulkano/blob/master/vulkano-shaders/src/lib.rs#L67 //! [cargo-expand]: https://github.com/dtolnay/cargo-expand //! [ShaderModule::new]: https://docs.rs/vulkano/*/vulkano/pipeline/shader/struct.ShaderModule.html#method.new //! [OomError]: https://docs.rs/vulkano/*/vulkano/enum.OomError.html //! [pipeline::shader]: https://docs.rs/vulkano/*/vulkano/pipeline/shader/index.html //! [descriptor]: https://docs.rs/vulkano/*/vulkano/descriptor/index.html //! [ShaderStages]: https://docs.rs/vulkano/*/vulkano/descriptor/descriptor/struct.ShaderStages.html //! [PipelineLayoutDesc]: https://docs.rs/vulkano/*/vulkano/descriptor/pipeline_layout/trait.PipelineLayoutDesc.html //! [SpecializationConstants]: https://docs.rs/vulkano/*/vulkano/pipeline/shader/trait.SpecializationConstants.html //! [pipeline]: https://docs.rs/vulkano/*/vulkano/pipeline/index.html #![doc(html_logo_url = "https://raw.githubusercontent.com/vulkano-rs/vulkano/master/logo.png")] #![recursion_limit = "1024"] #[macro_use] extern crate quote; #[macro_use] extern crate syn; extern crate proc_macro; use std::env; use std::fs::File; use std::io::{Read, Result as IoResult}; use std::path::Path; use syn::parse::{Parse, ParseStream, Result}; use syn::{Ident, LitStr, LitBool}; mod codegen; mod descriptor_sets; mod entry_point; mod enums; mod parse; mod spec_consts; mod structs; mod spirv_search; use crate::codegen::ShaderKind; enum SourceKind { Src(String), Path(String), } struct MacroInput { shader_kind: ShaderKind, source_kind: SourceKind, include_directories: Vec<String>, dump: bool, } impl Parse for MacroInput { fn parse(input: ParseStream) -> Result<Self> { let mut dump = None; let mut shader_kind = None; let mut source_kind = None; let mut include_directories = Vec::new(); while !input.is_empty() { let name: Ident = input.parse()?; input.parse::<Token![:]>()?; match name.to_string().as_ref() { "ty" => { if shader_kind.is_some() { panic!("Only one `ty` can be defined") } let ty: LitStr = input.parse()?; let ty = match ty.value().as_ref() { "vertex" => ShaderKind::Vertex, "fragment" => ShaderKind::Fragment, "geometry" => ShaderKind::Geometry, "tess_ctrl" => ShaderKind::TessControl, "tess_eval" => ShaderKind::TessEvaluation, "compute" => ShaderKind::Compute, _ => panic!("Unexpected shader type, valid values: vertex, fragment, geometry, tess_ctrl, tess_eval, compute") }; shader_kind = Some(ty); } "src" => { if source_kind.is_some() { panic!("Only one `src` or `path` can be defined") } let src: LitStr = input.parse()?; source_kind = Some(SourceKind::Src(src.value())); } "path" => { if source_kind.is_some() { panic!("Only one `src` or `path` can be defined") } let path: LitStr = input.parse()?; source_kind = Some(SourceKind::Path(path.value())); } "include" => { let in_brackets; bracketed!(in_brackets in input); while !in_brackets.is_empty() { let path: LitStr = in_brackets.parse()?; include_directories.push(path.value()); if !in_brackets.is_empty() { in_brackets.parse::<Token![,]>()?; } } } "dump" => { if dump.is_some() { panic!("Only one `dump` can be defined") } let dump_lit: LitBool = input.parse()?; dump = Some(dump_lit.value); } name => panic!(format!("Unknown field name: {}", name)) } if !input.is_empty() { input.parse::<Token![,]>()?; } } let shader_kind = match shader_kind { Some(shader_kind) => shader_kind, None => panic!("Please provide a shader type e.g. `ty: \"vertex\"`") }; let source_kind = match source_kind { Some(source_kind) => source_kind, None => panic!("Please provide a source e.g. `path: \"foo.glsl\"` or `src: \"glsl source code here ...\"`") }; let dump = dump.unwrap_or(false); Ok(MacroInput { shader_kind, source_kind, include_directories, dump }) } } pub(self) fn read_file_to_string(full_path: &Path) -> IoResult<String> { let mut buf = String::new(); File::open(full_path) .and_then(|mut file| file.read_to_string(&mut buf))?; Ok(buf) } #[proc_macro] pub fn shader(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input = parse_macro_input!(input as MacroInput); let (path, source_code) = match input.source_kind { SourceKind::Src(source) => (None, source), SourceKind::Path(path) => (Some(path.clone()), { let root = env::var("CARGO_MANIFEST_DIR").unwrap_or(".".into()); let full_path = Path::new(&root).join(&path); if full_path.is_file() { read_file_to_string(&full_path) .expect(&format!("Error reading source from {:?}", path)) } else { panic!("File {:?} was not found ; note that the path must be relative to your Cargo.toml", path); } }) }; let content = codegen::compile(path, &source_code, input.shader_kind, &input.include_directories).unwrap(); codegen::reflect("Shader", content.as_binary(), input.dump).unwrap().into() }