Skip to content

Commit 6a11717

Browse files
committed
generated: commit generated files
1 parent c3137c8 commit 6a11717

File tree

22 files changed

+2462
-1
lines changed

22 files changed

+2462
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
target
22
.idea
3-
generated

generated/cargo-gpu/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[workspace]
2+
members = [
3+
"ash-graphics",
4+
"ash-graphics-shaders"
5+
]
6+
resolver = "3"
7+
8+
[workspace.package]
9+
version = "0.1.0"
10+
authors = ["firestar99 <[email protected]>"]
11+
edition = "2024"
12+
license = "MIT"
13+
repository = ""
14+
15+
[workspace.lints.rust]
16+
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] }
17+
18+
[workspace.dependencies]
19+
cargo-gpu = { git = "https://github.com/Rust-GPU/cargo-gpu", rev = "bf24eb6060e0c7b0013eceddd23b5d7cee68f4cc" }
20+
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "6fb875068d0d35b1a81ae89b73cf7a464f8c60f7" }
21+
22+
glam = { version = "0.30.9", default-features = false }
23+
bytemuck = { version = "1.24.0", features = ["derive"] }
24+
ash = "0.38"
25+
ash-window = "0.13"
26+
raw-window-handle = "0.6.2"
27+
winit = "0.30.0"
28+
cfg-if = "1.0.0"
29+
anyhow = "1.0.98"
30+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "ash-graphics-shaders"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lints]
7+
workspace = true
8+
9+
[lib]
10+
crate-type = ["lib", "dylib"]
11+
12+
[dependencies]
13+
spirv-std.workspace = true
14+
glam.workspace = true
15+
bytemuck.workspace = true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_std]
2+
3+
use bytemuck::{Pod, Zeroable};
4+
use core::f32::consts::PI;
5+
use glam::{Vec3, Vec4, vec2, vec3};
6+
#[cfg(target_arch = "spirv")]
7+
use spirv_std::num_traits::Float;
8+
use spirv_std::spirv;
9+
10+
#[derive(Copy, Clone, Pod, Zeroable)]
11+
#[repr(C)]
12+
pub struct ShaderConstants {
13+
pub width: u32,
14+
pub height: u32,
15+
pub time: f32,
16+
}
17+
18+
#[spirv(fragment)]
19+
pub fn main_fs(vtx_color: Vec3, output: &mut Vec4) {
20+
*output = Vec4::from((vtx_color, 1.));
21+
}
22+
23+
#[spirv(vertex)]
24+
pub fn main_vs(
25+
#[spirv(vertex_index)] vert_id: i32,
26+
#[spirv(push_constant)] constants: &ShaderConstants,
27+
#[spirv(position)] vtx_pos: &mut Vec4,
28+
vtx_color: &mut Vec3,
29+
) {
30+
let speed = 0.4;
31+
let time = constants.time * speed + vert_id as f32 * (2. * PI * 120. / 360.);
32+
let position = vec2(f32::sin(time), f32::cos(time));
33+
*vtx_pos = Vec4::from((position, 0.0, 1.0));
34+
35+
*vtx_color = [vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 0., 1.)][vert_id as usize % 3];
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "ash-graphics"
3+
publish = false
4+
version.workspace = true
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
10+
[lints]
11+
workspace = true
12+
13+
[dependencies]
14+
ash-graphics-shaders = { path = "../ash-graphics-shaders" }
15+
16+
ash.workspace = true
17+
ash-window.workspace = true
18+
raw-window-handle.workspace = true
19+
winit.workspace = true
20+
anyhow.workspace = true
21+
bytemuck.workspace = true
22+
23+
[build-dependencies]
24+
cargo-gpu.workspace = true
25+
anyhow.workspace = true
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cargo_gpu::Install;
2+
use cargo_gpu::spirv_builder::{MetadataPrintout, ShaderPanicStrategy};
3+
use std::path::PathBuf;
4+
5+
pub fn main() -> anyhow::Result<()> {
6+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
7+
let crate_path = [manifest_dir, "..", "ash-graphics-shaders"]
8+
.iter()
9+
.copied()
10+
.collect::<PathBuf>();
11+
12+
let install = Install::from_shader_crate(crate_path.clone()).run()?;
13+
let mut builder = install.to_spirv_builder(crate_path, "spirv-unknown-vulkan1.3");
14+
builder.print_metadata = MetadataPrintout::DependencyOnly;
15+
builder.shader_panic_strategy = ShaderPanicStrategy::DebugPrintfThenExit {
16+
print_inputs: true,
17+
print_backtrace: true,
18+
};
19+
20+
let compile_result = builder.build()?;
21+
let spv_path = compile_result.module.unwrap_single();
22+
println!("cargo::rustc-env=SHADER_SPV_PATH={}", spv_path.display());
23+
Ok(())
24+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
use anyhow::{Context, anyhow};
2+
use ash::{ext, khr, vk};
3+
use std::borrow::Cow;
4+
use std::ffi::{CStr, c_char};
5+
use std::ops::Deref;
6+
use std::sync::Arc;
7+
8+
/// Central struct containing the Vulkan instance and device, among others
9+
pub struct MyDevice {
10+
pub entry: ash::Entry,
11+
pub instance: ash::Instance,
12+
pub physical_device: vk::PhysicalDevice,
13+
pub device: ash::Device,
14+
pub main_queue_family: u32,
15+
pub main_queue: vk::Queue,
16+
pub debug_ext_instance: ext::debug_utils::Instance,
17+
pub debug_ext_device: ext::debug_utils::Device,
18+
pub surface_ext: khr::surface::Instance,
19+
pub swapchain_ext: khr::swapchain::Device,
20+
debug_callback: vk::DebugUtilsMessengerEXT,
21+
}
22+
23+
impl Deref for MyDevice {
24+
type Target = ash::Device;
25+
26+
fn deref(&self) -> &Self::Target {
27+
&self.device
28+
}
29+
}
30+
31+
impl MyDevice {
32+
pub fn new(extension_names: &[*const c_char], debug_layer: bool) -> anyhow::Result<Arc<Self>> {
33+
unsafe {
34+
let entry = ash::Entry::load()?;
35+
36+
let instance = {
37+
let layer_names: &'static [_] = if debug_layer {
38+
const { &[c"VK_LAYER_KHRONOS_validation".as_ptr()] }
39+
} else {
40+
&[]
41+
};
42+
43+
let mut extension_names_raw = extension_names.to_vec();
44+
extension_names_raw.push(ext::debug_utils::NAME.as_ptr());
45+
46+
let app_name = c"VulkanTriangle";
47+
entry
48+
.create_instance(
49+
&vk::InstanceCreateInfo::default()
50+
.application_info(
51+
&vk::ApplicationInfo::default()
52+
.application_name(app_name)
53+
.application_version(0)
54+
.engine_name(app_name)
55+
.engine_version(0)
56+
.api_version(vk::make_api_version(0, 1, 3, 0)),
57+
)
58+
.enabled_layer_names(layer_names)
59+
.enabled_extension_names(&extension_names_raw),
60+
None,
61+
)
62+
.context("create_instance")?
63+
};
64+
65+
let debug_instance = ext::debug_utils::Instance::new(&entry, &instance);
66+
let debug_callback = {
67+
debug_instance.create_debug_utils_messenger(
68+
&vk::DebugUtilsMessengerCreateInfoEXT::default()
69+
.message_severity(
70+
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
71+
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
72+
| vk::DebugUtilsMessageSeverityFlagsEXT::INFO,
73+
)
74+
.message_type(
75+
vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
76+
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
77+
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
78+
)
79+
.pfn_user_callback(Some(vulkan_debug_callback)),
80+
None,
81+
)?
82+
};
83+
84+
let physical_device = {
85+
instance
86+
.enumerate_physical_devices()?
87+
.into_iter()
88+
.min_by_key(|phy| {
89+
match instance.get_physical_device_properties(*phy).device_type {
90+
vk::PhysicalDeviceType::DISCRETE_GPU => 1,
91+
vk::PhysicalDeviceType::VIRTUAL_GPU => 2,
92+
vk::PhysicalDeviceType::INTEGRATED_GPU => 3,
93+
vk::PhysicalDeviceType::CPU => 4,
94+
_ => 5,
95+
}
96+
})
97+
.ok_or(anyhow!("No physical devices available"))?
98+
};
99+
100+
let main_queue_family = {
101+
instance
102+
.get_physical_device_queue_family_properties(physical_device)
103+
.into_iter()
104+
.enumerate()
105+
.find(|(_, prop)| prop.queue_flags.contains(vk::QueueFlags::GRAPHICS))
106+
.ok_or(anyhow!(
107+
"No graphics + compute queues on physical device available"
108+
))?
109+
.0 as u32
110+
};
111+
112+
let device = instance
113+
.create_device(
114+
physical_device,
115+
&vk::DeviceCreateInfo::default()
116+
.push_next(
117+
&mut vk::PhysicalDeviceVulkan12Features::default()
118+
.vulkan_memory_model(true),
119+
)
120+
.push_next(
121+
&mut vk::PhysicalDeviceVulkan13Features::default()
122+
.synchronization2(true)
123+
.dynamic_rendering(true),
124+
)
125+
.queue_create_infos(&[vk::DeviceQueueCreateInfo::default()
126+
.queue_family_index(main_queue_family)
127+
.queue_priorities(&[1.0])])
128+
.enabled_extension_names(&[
129+
khr::swapchain::NAME.as_ptr(),
130+
khr::shader_non_semantic_info::NAME.as_ptr(),
131+
]),
132+
None,
133+
)
134+
.context("create_device")?;
135+
let main_queue = device.get_device_queue(main_queue_family, 0);
136+
137+
Ok(Arc::new(Self {
138+
debug_ext_device: ext::debug_utils::Device::new(&instance, &device),
139+
surface_ext: khr::surface::Instance::new(&entry, &instance),
140+
swapchain_ext: khr::swapchain::Device::new(&instance, &device),
141+
entry,
142+
instance,
143+
physical_device,
144+
device,
145+
main_queue_family,
146+
main_queue,
147+
debug_ext_instance: debug_instance,
148+
debug_callback,
149+
}))
150+
}
151+
}
152+
}
153+
154+
impl Drop for MyDevice {
155+
fn drop(&mut self) {
156+
unsafe {
157+
self.debug_ext_instance
158+
.destroy_debug_utils_messenger(self.debug_callback, None);
159+
self.device.destroy_device(None);
160+
self.instance.destroy_instance(None);
161+
}
162+
}
163+
}
164+
165+
unsafe extern "system" fn vulkan_debug_callback(
166+
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
167+
_message_type: vk::DebugUtilsMessageTypeFlagsEXT,
168+
p_callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT<'_>,
169+
_user_data: *mut std::os::raw::c_void,
170+
) -> vk::Bool32 {
171+
unsafe {
172+
let callback_data = *p_callback_data;
173+
let message_id_name = callback_data
174+
.message_id_name_as_c_str()
175+
.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
176+
let message = callback_data
177+
.message_as_c_str()
178+
.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
179+
180+
println!("{message_severity:?}: [{message_id_name}] : {message}");
181+
vk::FALSE
182+
}
183+
}

0 commit comments

Comments
 (0)