From 45237847d2d39917ca411aea140cd21e54d70143 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Mon, 1 Mar 2021 14:56:46 +0100 Subject: [PATCH 1/7] Add entrypoint attribute configuration. --- .../src/codegen_cx/declare.rs | 4 ++-- crates/rustc_codegen_spirv/src/symbols.rs | 16 ++++++++++++++++ crates/spirv-builder/src/test/basic.rs | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs index 2f001e14e7..d898e78ae7 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs @@ -114,8 +114,8 @@ impl<'tcx> CodegenCx<'tcx> { for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) { match attr { SpirvAttribute::Entry(entry) => { - let crate_relative_name = instance.to_string(); - self.entry_stub(&instance, &fn_abi, declared, crate_relative_name, entry) + let entry_name = entry.name.as_ref().map(ToString::to_string).unwrap_or_else(|| instance.to_string()); + self.entry_stub(&instance, &fn_abi, declared, entry_name, entry) } SpirvAttribute::UnrollLoops => { self.unroll_loops_decorations diff --git a/crates/rustc_codegen_spirv/src/symbols.rs b/crates/rustc_codegen_spirv/src/symbols.rs index f0e12bd95b..f8a281bd79 100644 --- a/crates/rustc_codegen_spirv/src/symbols.rs +++ b/crates/rustc_codegen_spirv/src/symbols.rs @@ -33,6 +33,7 @@ pub struct Symbols { pub spirv13: Symbol, pub spirv14: Symbol, pub spirv15: Symbol, + pub entry_point: Symbol, descriptor_set: Symbol, binding: Symbol, image_type: Symbol, @@ -370,6 +371,7 @@ impl Symbols { Self { fmt_decimal: Symbol::intern("fmt_decimal"), + entry_point: Symbol::intern("entry_point"), spirv: Symbol::intern("spirv"), spirv_std: Symbol::intern("spirv_std"), libm: Symbol::intern("libm"), @@ -437,6 +439,7 @@ impl AsRef<[u32]> for ExecutionModeExtra { pub struct Entry { pub execution_model: ExecutionModel, pub execution_modes: Vec<(ExecutionMode, ExecutionModeExtra)>, + pub name: Option, } impl From for Entry { @@ -444,6 +447,7 @@ impl From for Entry { Self { execution_model, execution_modes: Vec::new(), + name: None, } } } @@ -791,6 +795,18 @@ fn parse_entry_attrs( } } } + } else if attr_name.name == cx.sym.entry_point { + match attr.value_str() { + Some(sym) => { + entry.name = Some(sym); + } + None => { + cx.tcx.sess.span_err( + attr_name.span, + r#"#[spirv(entrypoint = "...")] expects a valid str literal."#, + ); + } + } } else { return Err(( attr_name.span, diff --git a/crates/spirv-builder/src/test/basic.rs b/crates/spirv-builder/src/test/basic.rs index 7ea104302e..2f09aaa45e 100644 --- a/crates/spirv-builder/src/test/basic.rs +++ b/crates/spirv-builder/src/test/basic.rs @@ -28,6 +28,16 @@ pub fn main() { "#); } +#[test] +fn custom_entry_point() { + val(r#" +#[allow(unused_attributes)] +#[spirv(fragment(entry_point="hello_world"))] +pub fn main() { +} +"#); +} + #[test] // blocked on: https://github.com/EmbarkStudios/rust-gpu/issues/69 #[ignore] From f071ebccc0097e57e8689d19c8492ee5a727f59a Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 09:14:27 +0100 Subject: [PATCH 2/7] Update with feedback --- crates/rustc_codegen_spirv/src/symbols.rs | 6 +++--- crates/spirv-builder/src/test/basic.rs | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/symbols.rs b/crates/rustc_codegen_spirv/src/symbols.rs index f8a281bd79..d5c9ac6923 100644 --- a/crates/rustc_codegen_spirv/src/symbols.rs +++ b/crates/rustc_codegen_spirv/src/symbols.rs @@ -33,7 +33,7 @@ pub struct Symbols { pub spirv13: Symbol, pub spirv14: Symbol, pub spirv15: Symbol, - pub entry_point: Symbol, + pub entry_point_name: Symbol, descriptor_set: Symbol, binding: Symbol, image_type: Symbol, @@ -371,7 +371,7 @@ impl Symbols { Self { fmt_decimal: Symbol::intern("fmt_decimal"), - entry_point: Symbol::intern("entry_point"), + entry_point_name: Symbol::intern("entry_point_name"), spirv: Symbol::intern("spirv"), spirv_std: Symbol::intern("spirv_std"), libm: Symbol::intern("libm"), @@ -795,7 +795,7 @@ fn parse_entry_attrs( } } } - } else if attr_name.name == cx.sym.entry_point { + } else if attr_name.name == cx.sym.entry_point_name { match attr.value_str() { Some(sym) => { entry.name = Some(sym); diff --git a/crates/spirv-builder/src/test/basic.rs b/crates/spirv-builder/src/test/basic.rs index 2f09aaa45e..e2175f29af 100644 --- a/crates/spirv-builder/src/test/basic.rs +++ b/crates/spirv-builder/src/test/basic.rs @@ -30,12 +30,21 @@ pub fn main() { #[test] fn custom_entry_point() { - val(r#" + dis_globals(r#" #[allow(unused_attributes)] -#[spirv(fragment(entry_point="hello_world"))] -pub fn main() { -} -"#); +#[spirv(fragment(entry_point_name="hello_world"))] +pub fn main() { } +"#, +r#"OpCapability Shader +OpCapability VulkanMemoryModel +OpCapability VariablePointers +OpExtension "SPV_KHR_vulkan_memory_model" +OpMemoryModel Logical Vulkan +OpEntryPoint Fragment %1 "hello_world" +OpExecutionMode %1 OriginUpperLeft +OpName %2 "test_project::main" +%3 = OpTypeVoid +%4 = OpTypeFunction %3"#); } #[test] From 3052a5e38ad1ba9f47c7a54f786e593df5236096 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 09:21:54 +0100 Subject: [PATCH 3/7] Add docs --- docs/src/attributes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/attributes.md b/docs/src/attributes.md index ef83eb5742..d8b0b115e5 100644 --- a/docs/src/attributes.md +++ b/docs/src/attributes.md @@ -17,6 +17,10 @@ fn main() { } Common values are `#[spirv(fragment)]` and `#[spirv(vertex)]`. A list of all supported names can be found in [spirv_headers](https://docs.rs/spirv_headers/1.5.0/spirv_headers/enum.ExecutionModel.html) - convert the enum name to snake_case for the rust-gpu attribute name. +### Override entry point name + +You can override the default `OpEntryPoint` name for any entry point with the `entry_point_name` sub-attribute on any of the execution model attributes. (e.g. `#[spirv(vertex(entry_point_name="foo"))]`) + ## Builtins When declaring inputs and outputs, sometimes you want to declare it as a "builtin". This means many things, but one example is `gl_Position` from glsl - the GPU assigns inherent meaning to the variable and uses it for placing the vertex in clip space. The equivalent in rust-gpu is called `position`. From 3d757168326f7d1fbb0009dce12f841252e23588 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 09:54:00 +0100 Subject: [PATCH 4/7] fmt --- crates/rustc_codegen_spirv/src/codegen_cx/declare.rs | 6 +++++- crates/spirv-builder/src/test/basic.rs | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs index d898e78ae7..847c22f28a 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs @@ -114,7 +114,11 @@ impl<'tcx> CodegenCx<'tcx> { for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) { match attr { SpirvAttribute::Entry(entry) => { - let entry_name = entry.name.as_ref().map(ToString::to_string).unwrap_or_else(|| instance.to_string()); + let entry_name = entry + .name + .as_ref() + .map(ToString::to_string) + .unwrap_or_else(|| instance.to_string()); self.entry_stub(&instance, &fn_abi, declared, entry_name, entry) } SpirvAttribute::UnrollLoops => { diff --git a/crates/spirv-builder/src/test/basic.rs b/crates/spirv-builder/src/test/basic.rs index e2175f29af..1eeaea662b 100644 --- a/crates/spirv-builder/src/test/basic.rs +++ b/crates/spirv-builder/src/test/basic.rs @@ -30,12 +30,13 @@ pub fn main() { #[test] fn custom_entry_point() { - dis_globals(r#" + dis_globals( + r#" #[allow(unused_attributes)] #[spirv(fragment(entry_point_name="hello_world"))] pub fn main() { } "#, -r#"OpCapability Shader + r#"OpCapability Shader OpCapability VulkanMemoryModel OpCapability VariablePointers OpExtension "SPV_KHR_vulkan_memory_model" @@ -44,7 +45,8 @@ OpEntryPoint Fragment %1 "hello_world" OpExecutionMode %1 OriginUpperLeft OpName %2 "test_project::main" %3 = OpTypeVoid -%4 = OpTypeFunction %3"#); +%4 = OpTypeFunction %3"#, + ); } #[test] From bed691dcc743bf61aebe2de97cbc9d34fd1ebf61 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 13:47:11 +0100 Subject: [PATCH 5/7] rm usused_attributes --- crates/spirv-builder/src/test/basic.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/spirv-builder/src/test/basic.rs b/crates/spirv-builder/src/test/basic.rs index 1eeaea662b..92abe5642a 100644 --- a/crates/spirv-builder/src/test/basic.rs +++ b/crates/spirv-builder/src/test/basic.rs @@ -32,7 +32,6 @@ pub fn main() { fn custom_entry_point() { dis_globals( r#" -#[allow(unused_attributes)] #[spirv(fragment(entry_point_name="hello_world"))] pub fn main() { } "#, From bc73aab2644f8f65ac1adce3c16277d50e0bd050 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 14:00:48 +0100 Subject: [PATCH 6/7] other changes --- crates/rustc_codegen_spirv/src/symbols.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/symbols.rs b/crates/rustc_codegen_spirv/src/symbols.rs index d5c9ac6923..e7f4eb907e 100644 --- a/crates/rustc_codegen_spirv/src/symbols.rs +++ b/crates/rustc_codegen_spirv/src/symbols.rs @@ -795,16 +795,20 @@ fn parse_entry_attrs( } } } - } else if attr_name.name == cx.sym.entry_point_name { + } else if attr_name.name == sym.entry_point_name { match attr.value_str() { Some(sym) => { entry.name = Some(sym); } None => { - cx.tcx.sess.span_err( - attr_name.span, - r#"#[spirv(entrypoint = "...")] expects a valid str literal."#, - ); + return Err(( + attr_name.span, + format!( + "#[spirv({}(..))] unknown attribute argument {}", + name.name.to_ident_string(), + attr_name.name.to_ident_string() + ), + )) } } } else { From 1fddf74cb731f971498460d5bb0b0643550e5e29 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Tue, 2 Mar 2021 14:01:19 +0100 Subject: [PATCH 7/7] fmt --- crates/rustc_codegen_spirv/src/symbols.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/symbols.rs b/crates/rustc_codegen_spirv/src/symbols.rs index e7f4eb907e..f8e7cdcc33 100644 --- a/crates/rustc_codegen_spirv/src/symbols.rs +++ b/crates/rustc_codegen_spirv/src/symbols.rs @@ -802,12 +802,12 @@ fn parse_entry_attrs( } None => { return Err(( - attr_name.span, - format!( - "#[spirv({}(..))] unknown attribute argument {}", - name.name.to_ident_string(), - attr_name.name.to_ident_string() - ), + attr_name.span, + format!( + "#[spirv({}(..))] unknown attribute argument {}", + name.name.to_ident_string(), + attr_name.name.to_ident_string() + ), )) } }