Add entry point attribute configuration. (#458)

* Add entrypoint attribute configuration.

* Update with feedback

* Add docs

* fmt

* rm usused_attributes

* other changes

* fmt
This commit is contained in:
XAMPPRocky 2021-03-02 14:30:08 +01:00 committed by GitHub
parent 830652921c
commit 6678f9a49d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 2 deletions

View File

@ -114,8 +114,12 @@ impl<'tcx> CodegenCx<'tcx> {
for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) { for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) {
match attr { match attr {
SpirvAttribute::Entry(entry) => { SpirvAttribute::Entry(entry) => {
let crate_relative_name = instance.to_string(); let entry_name = entry
self.entry_stub(&instance, &fn_abi, declared, crate_relative_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 => { SpirvAttribute::UnrollLoops => {
self.unroll_loops_decorations self.unroll_loops_decorations

View File

@ -33,6 +33,7 @@ pub struct Symbols {
pub spirv13: Symbol, pub spirv13: Symbol,
pub spirv14: Symbol, pub spirv14: Symbol,
pub spirv15: Symbol, pub spirv15: Symbol,
pub entry_point_name: Symbol,
descriptor_set: Symbol, descriptor_set: Symbol,
binding: Symbol, binding: Symbol,
image_type: Symbol, image_type: Symbol,
@ -370,6 +371,7 @@ impl Symbols {
Self { Self {
fmt_decimal: Symbol::intern("fmt_decimal"), fmt_decimal: Symbol::intern("fmt_decimal"),
entry_point_name: Symbol::intern("entry_point_name"),
spirv: Symbol::intern("spirv"), spirv: Symbol::intern("spirv"),
spirv_std: Symbol::intern("spirv_std"), spirv_std: Symbol::intern("spirv_std"),
libm: Symbol::intern("libm"), libm: Symbol::intern("libm"),
@ -437,6 +439,7 @@ impl AsRef<[u32]> for ExecutionModeExtra {
pub struct Entry { pub struct Entry {
pub execution_model: ExecutionModel, pub execution_model: ExecutionModel,
pub execution_modes: Vec<(ExecutionMode, ExecutionModeExtra)>, pub execution_modes: Vec<(ExecutionMode, ExecutionModeExtra)>,
pub name: Option<Symbol>,
} }
impl From<ExecutionModel> for Entry { impl From<ExecutionModel> for Entry {
@ -444,6 +447,7 @@ impl From<ExecutionModel> for Entry {
Self { Self {
execution_model, execution_model,
execution_modes: Vec::new(), execution_modes: Vec::new(),
name: None,
} }
} }
} }
@ -791,6 +795,22 @@ fn parse_entry_attrs(
} }
} }
} }
} else if attr_name.name == sym.entry_point_name {
match attr.value_str() {
Some(sym) => {
entry.name = Some(sym);
}
None => {
return Err((
attr_name.span,
format!(
"#[spirv({}(..))] unknown attribute argument {}",
name.name.to_ident_string(),
attr_name.name.to_ident_string()
),
))
}
}
} else { } else {
return Err(( return Err((
attr_name.span, attr_name.span,

View File

@ -28,6 +28,26 @@ pub fn main() {
"#); "#);
} }
#[test]
fn custom_entry_point() {
dis_globals(
r#"
#[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] #[test]
// blocked on: https://github.com/EmbarkStudios/rust-gpu/issues/69 // blocked on: https://github.com/EmbarkStudios/rust-gpu/issues/69
#[ignore] #[ignore]

View File

@ -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. 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 ## 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`. 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`.