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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
//! Ray tracing pipeline types
use {
super::{
device::Device,
merge_push_constant_ranges,
physical_device::RayTraceProperties,
shader::{DescriptorBindingMap, PipelineDescriptorInfo, Shader},
DriverError,
},
ash::vk,
derive_builder::{Builder, UninitializedFieldError},
log::warn,
std::{ffi::CString, ops::Deref, sync::Arc, thread::panicking},
};
/// Smart pointer handle to a [pipeline] object.
///
/// Also contains information about the object.
///
/// ## `Deref` behavior
///
/// `RayTracePipeline` automatically dereferences to [`vk::Pipeline`] (via the [`Deref`]
/// trait), so you can call `vk::Pipeline`'s methods on a value of type `RayTracePipeline`. To avoid
/// name clashes with `vk::Pipeline`'s methods, the methods of `RayTracePipeline` itself are
/// associated functions, called using [fully qualified syntax]:
///
/// [pipeline]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPipeline.html
/// [deref]: core::ops::Deref
/// [fully qualified syntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
#[derive(Debug)]
pub struct RayTracePipeline {
pub(crate) descriptor_bindings: DescriptorBindingMap,
pub(crate) descriptor_info: PipelineDescriptorInfo,
device: Arc<Device>,
/// Information used to create this object.
pub info: RayTracePipelineInfo,
pub(crate) layout: vk::PipelineLayout,
/// A descriptive name used in debugging messages.
pub name: Option<String>,
pub(crate) push_constants: Vec<vk::PushConstantRange>,
pipeline: vk::Pipeline,
shader_modules: Vec<vk::ShaderModule>,
shader_group_handles: Vec<u8>,
}
impl RayTracePipeline {
/// Creates a new ray trace pipeline on the given device.
///
/// The correct pipeline stages will be enabled based on the provided shaders. See [Shader] for
/// details on all available stages.
///
/// The number and composition of the `shader_groups` parameter must match the actual shaders
/// provided.
///
/// # Panics
///
/// If shader code is not a multiple of four bytes.
///
/// # Examples
///
/// Basic usage:
///
/// ```no_run
/// # use std::sync::Arc;
/// # use ash::vk;
/// # use screen_13::driver::DriverError;
/// # use screen_13::driver::device::{Device, DeviceInfo};
/// # use screen_13::driver::ray_trace::{RayTracePipeline, RayTracePipelineInfo, RayTraceShaderGroup};
/// # use screen_13::driver::shader::Shader;
/// # fn main() -> Result<(), DriverError> {
/// # let device = Arc::new(Device::create_headless(DeviceInfo::new())?);
/// # let my_rgen_code = [0u8; 1];
/// # let my_chit_code = [0u8; 1];
/// # let my_miss_code = [0u8; 1];
/// # let my_shadow_code = [0u8; 1];
/// // shader code is raw SPIR-V code as bytes
/// let info = RayTracePipelineInfo::new().max_ray_recursion_depth(1);
/// let pipeline = RayTracePipeline::create(
/// &device,
/// info,
/// [
/// Shader::new_ray_gen(my_rgen_code.as_slice()),
/// Shader::new_closest_hit(my_chit_code.as_slice()),
/// Shader::new_miss(my_miss_code.as_slice()),
/// Shader::new_miss(my_shadow_code.as_slice()),
/// ],
/// [
/// RayTraceShaderGroup::new_general(0),
/// RayTraceShaderGroup::new_triangles(1, None),
/// RayTraceShaderGroup::new_general(2),
/// RayTraceShaderGroup::new_general(3),
/// ],
/// )?;
///
/// assert_ne!(*pipeline, vk::Pipeline::null());
/// assert_eq!(pipeline.info.max_ray_recursion_depth, 1);
/// # Ok(()) }
/// ```
#[profiling::function]
pub fn create<S>(
device: &Arc<Device>,
info: impl Into<RayTracePipelineInfo>,
shaders: impl IntoIterator<Item = S>,
shader_groups: impl IntoIterator<Item = RayTraceShaderGroup>,
) -> Result<Self, DriverError>
where
S: Into<Shader>,
{
let info = info.into();
let shader_groups = shader_groups
.into_iter()
.map(|shader_group| shader_group.into())
.collect::<Vec<_>>();
let group_count = shader_groups.len();
let shaders = shaders
.into_iter()
.map(|shader| shader.into())
.collect::<Vec<Shader>>();
let push_constants = shaders
.iter()
.map(|shader| shader.push_constant_range())
.filter_map(|mut push_const| push_const.take())
.collect::<Vec<_>>();
// Use SPIR-V reflection to get the types and counts of all descriptors
let mut descriptor_bindings = Shader::merge_descriptor_bindings(
shaders
.iter()
.map(|shader| shader.descriptor_bindings(device))
.collect::<Result<Vec<_>, _>>()?,
);
for (descriptor_info, _) in descriptor_bindings.values_mut() {
if descriptor_info.binding_count() == 0 {
descriptor_info.set_binding_count(info.bindless_descriptor_count);
}
}
let descriptor_info = PipelineDescriptorInfo::create(device, &descriptor_bindings)?;
let descriptor_set_layout_handles = descriptor_info
.layouts
.values()
.map(|descriptor_set_layout| **descriptor_set_layout)
.collect::<Box<[_]>>();
unsafe {
let layout = device
.create_pipeline_layout(
&vk::PipelineLayoutCreateInfo::builder()
.set_layouts(&descriptor_set_layout_handles)
.push_constant_ranges(&push_constants),
None,
)
.map_err(|err| {
warn!("{err}");
DriverError::Unsupported
})?;
let mut entry_points: Vec<CString> = Vec::with_capacity(shaders.len()); // Keep entry point names alive, since build() forgets references.
let mut shader_stages: Vec<vk::PipelineShaderStageCreateInfo> =
Vec::with_capacity(shaders.len());
let create_shader_module =
|info: &Shader| -> Result<(vk::ShaderModule, String), DriverError> {
let shader_module_create_info = vk::ShaderModuleCreateInfo {
code_size: info.spirv.len(),
p_code: info.spirv.as_ptr() as *const u32,
..Default::default()
};
let shader_module = device
.create_shader_module(&shader_module_create_info, None)
.map_err(|err| {
warn!("{err}");
DriverError::Unsupported
})?;
Ok((shader_module, info.entry_name.clone()))
};
let mut specializations = Vec::with_capacity(shaders.len());
let mut shader_modules = Vec::with_capacity(shaders.len());
for shader in &shaders {
let res = create_shader_module(shader);
if res.is_err() {
device.destroy_pipeline_layout(layout, None);
for shader_module in &shader_modules {
device.destroy_shader_module(*shader_module, None);
}
}
let (module, entry_point) = res?;
entry_points.push(CString::new(entry_point).unwrap());
shader_modules.push(module);
let mut stage = vk::PipelineShaderStageCreateInfo::builder()
.module(module)
.name(entry_points.last().unwrap().as_ref())
.stage(shader.stage);
if let Some(spec_info) = &shader.specialization_info {
specializations.push(
vk::SpecializationInfo::builder()
.data(&spec_info.data)
.map_entries(&spec_info.map_entries)
.build(),
);
stage = stage.specialization_info(specializations.last().unwrap());
}
shader_stages.push(stage.build());
}
let mut dynamic_states = Vec::with_capacity(1);
if info.dynamic_stack_size {
dynamic_states.push(vk::DynamicState::RAY_TRACING_PIPELINE_STACK_SIZE_KHR);
}
let ray_trace_ext = device
.ray_trace_ext
.as_ref()
.ok_or(DriverError::Unsupported)?;
let pipeline = ray_trace_ext
.create_ray_tracing_pipelines(
vk::DeferredOperationKHR::null(),
vk::PipelineCache::null(),
&[vk::RayTracingPipelineCreateInfoKHR::builder()
.stages(&shader_stages)
.groups(&shader_groups)
.max_pipeline_ray_recursion_depth(
info.max_ray_recursion_depth.min(
device
.physical_device
.ray_trace_properties
.as_ref()
.unwrap()
.max_ray_recursion_depth,
),
)
.layout(layout)
.dynamic_state(
&vk::PipelineDynamicStateCreateInfo::builder()
.dynamic_states(&dynamic_states),
)
.build()],
None,
)
.map_err(|err| {
warn!("{err}");
device.destroy_pipeline_layout(layout, None);
for shader_module in &shader_modules {
device.destroy_shader_module(*shader_module, None);
}
DriverError::Unsupported
})?[0];
let device = Arc::clone(device);
let &RayTraceProperties {
shader_group_handle_size,
..
} = device
.physical_device
.ray_trace_properties
.as_ref()
.unwrap();
let push_constants = merge_push_constant_ranges(&push_constants);
// SAFETY:
// According to [vulkan spec](https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetRayTracingShaderGroupHandlesKHR.html)
// Valid usage of this function requires:
// 1. pipeline must be raytracing pipeline.
// 2. first_group must be less than the number of shader groups in the pipeline.
// 3. the sum of first group and group_count must be less or equal to the number of shader
// modules in the pipeline.
// 4. data_size must be at least shader_group_handle_size * group_count.
// 5. pipeline must not have been created with VK_PIPELINE_CREATE_LIBRARY_BIT_KHR.
//
let shader_group_handles = {
ray_trace_ext.get_ray_tracing_shader_group_handles(
pipeline,
0,
group_count as u32,
group_count * shader_group_handle_size as usize,
)
}
.map_err(|_| DriverError::InvalidData)?;
Ok(Self {
descriptor_bindings,
descriptor_info,
device,
info,
layout,
name: None,
push_constants,
pipeline,
shader_modules,
shader_group_handles,
})
}
}
/// Function returning a handle to a shader group of this pipeline.
/// This can be used to construct a sbt.
///
/// # Examples
///
/// See
/// [ray_trace.rs](https://github.com/attackgoat/screen-13/blob/master/examples/ray_trace.rs)
/// for a detail example which constructs a shader binding table buffer using this function.
pub fn group_handle(this: &Self, idx: usize) -> Result<&[u8], DriverError> {
let &RayTraceProperties {
shader_group_handle_size,
..
} = this
.device
.physical_device
.ray_trace_properties
.as_ref()
.ok_or(DriverError::Unsupported)?;
let start = idx * shader_group_handle_size as usize;
let end = start + shader_group_handle_size as usize;
Ok(&this.shader_group_handles[start..end])
}
/// Query ray trace pipeline shader group shader stack size.
///
/// The return value is the ray tracing pipeline stack size in bytes for the specified shader as
/// called from the specified shader group.
#[profiling::function]
pub fn group_stack_size(
this: &Self,
group: u32,
group_shader: vk::ShaderGroupShaderKHR,
) -> vk::DeviceSize {
unsafe {
// Safely use unchecked because ray_trace_ext is checked during pipeline creation
this.device
.ray_trace_ext
.as_ref()
.unwrap_unchecked()
.get_ray_tracing_shader_group_stack_size(this.pipeline, group, group_shader)
}
}
/// Sets the debugging name assigned to this pipeline.
pub fn with_name(mut this: Self, name: impl Into<String>) -> Self {
this.name = Some(name.into());
this
}
}
impl Deref for RayTracePipeline {
type Target = vk::Pipeline;
fn deref(&self) -> &Self::Target {
&self.pipeline
}
}
impl Drop for RayTracePipeline {
#[profiling::function]
fn drop(&mut self) {
if panicking() {
return;
}
unsafe {
self.device.destroy_pipeline(self.pipeline, None);
self.device.destroy_pipeline_layout(self.layout, None);
}
for shader_module in self.shader_modules.drain(..) {
unsafe {
self.device.destroy_shader_module(shader_module, None);
}
}
}
}
/// Information used to create a [`RayTracePipeline`] instance.
#[derive(Builder, Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[builder(
build_fn(
private,
name = "fallible_build",
error = "RayTracePipelineInfoBuilderError"
),
derive(Clone, Copy, Debug),
pattern = "owned"
)]
#[non_exhaustive]
pub struct RayTracePipelineInfo {
/// The number of descriptors to allocate for a given binding when using bindless (unbounded)
/// syntax.
///
/// The default is `8192`.
///
/// # Examples
///
/// Basic usage (GLSL):
///
/// ```
/// # inline_spirv::inline_spirv!(r#"
/// #version 460 core
/// #extension GL_EXT_nonuniform_qualifier : require
///
/// layout(set = 0, binding = 0, rgba8) readonly uniform image2D my_binding[];
///
/// void main()
/// {
/// // my_binding will have space for 8,192 images by default
/// }
/// # "#, rchit, vulkan1_2);
/// ```
#[builder(default = "8192")]
pub bindless_descriptor_count: u32,
/// Allow [setting the stack size dynamically] for a ray trace pipeline.
///
/// When set, you must manually set the stack size during ray trace passes using
/// [`RayTrace::set_stack_size`](crate::graph::pass_ref::RayTrace::set_stack_size).
///
/// [setting the stack size dynamically]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdSetRayTracingPipelineStackSizeKHR.html
#[builder(default)]
pub dynamic_stack_size: bool,
/// The [maximum recursion depth] of shaders executed by this pipeline.
///
/// The default is `16`.
///
/// [maximum recursion depth]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#ray-tracing-recursion-depth
#[builder(default = "16")]
pub max_ray_recursion_depth: u32,
}
impl RayTracePipelineInfo {
/// Creates a default `RayTracePipelineInfoBuilder`.
#[allow(clippy::new_ret_no_self)]
#[deprecated = "Use RayTracePipelineInfo::default()"]
#[doc(hidden)]
pub fn new() -> RayTracePipelineInfoBuilder {
Default::default()
}
/// Converts a `RayTracePipelineInfo` into a `RayTracePipelineInfoBuilder`.
#[inline(always)]
pub fn to_builder(self) -> RayTracePipelineInfoBuilder {
RayTracePipelineInfoBuilder {
bindless_descriptor_count: Some(self.bindless_descriptor_count),
dynamic_stack_size: Some(self.dynamic_stack_size),
max_ray_recursion_depth: Some(self.max_ray_recursion_depth),
}
}
}
impl Default for RayTracePipelineInfo {
fn default() -> Self {
Self {
bindless_descriptor_count: 8192,
dynamic_stack_size: false,
max_ray_recursion_depth: 16,
}
}
}
impl From<RayTracePipelineInfoBuilder> for RayTracePipelineInfo {
fn from(info: RayTracePipelineInfoBuilder) -> Self {
info.build()
}
}
impl RayTracePipelineInfoBuilder {
/// Builds a new `RayTracePipelineInfo`.
#[inline(always)]
pub fn build(self) -> RayTracePipelineInfo {
let res = self.fallible_build();
#[cfg(test)]
let res = res.unwrap();
#[cfg(not(test))]
let res = unsafe { res.unwrap_unchecked() };
res
}
}
#[derive(Debug)]
struct RayTracePipelineInfoBuilderError;
impl From<UninitializedFieldError> for RayTracePipelineInfoBuilderError {
fn from(_: UninitializedFieldError) -> Self {
Self
}
}
/// Describes the set of the shader stages to be included in each shader group in the ray trace
/// pipeline.
///
/// See
/// [VkRayTracingShaderGroupCreateInfoKHR](https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VkRayTracingShaderGroupCreateInfoKHR).
#[derive(Clone, Copy, Debug)]
pub struct RayTraceShaderGroup {
/// The optional index of the any-hit shader in the group if the shader group has type of
/// [RayTraceShaderGroupType::TrianglesHitGroup] or
/// [RayTraceShaderGroupType::ProceduralHitGroup].
pub any_hit_shader: Option<u32>,
/// The optional index of the closest hit shader in the group if the shader group has type of
/// [RayTraceShaderGroupType::TrianglesHitGroup] or
/// [RayTraceShaderGroupType::ProceduralHitGroup].
pub closest_hit_shader: Option<u32>,
/// The index of the ray generation, miss, or callable shader in the group if the shader group
/// has type of [RayTraceShaderGroupType::General].
pub general_shader: Option<u32>,
/// The index of the intersection shader in the group if the shader group has type of
/// [RayTraceShaderGroupType::ProceduralHitGroup].
pub intersection_shader: Option<u32>,
/// The type of hit group specified in this structure.
pub ty: RayTraceShaderGroupType,
}
impl RayTraceShaderGroup {
fn new(
ty: RayTraceShaderGroupType,
general_shader: impl Into<Option<u32>>,
intersection_shader: impl Into<Option<u32>>,
closest_hit_shader: impl Into<Option<u32>>,
any_hit_shader: impl Into<Option<u32>>,
) -> Self {
let any_hit_shader = any_hit_shader.into();
let closest_hit_shader = closest_hit_shader.into();
let general_shader = general_shader.into();
let intersection_shader = intersection_shader.into();
Self {
any_hit_shader,
closest_hit_shader,
general_shader,
intersection_shader,
ty,
}
}
/// Creates a new general-type shader group with the given general shader.
pub fn new_general(general_shader: impl Into<Option<u32>>) -> Self {
Self::new(
RayTraceShaderGroupType::General,
general_shader,
None,
None,
None,
)
}
/// Creates a new procedural-type shader group with the given intersection shader, and optional
/// closest-hit and any-hit shaders.
pub fn new_procedural(
intersection_shader: u32,
closest_hit_shader: impl Into<Option<u32>>,
any_hit_shader: impl Into<Option<u32>>,
) -> Self {
Self::new(
RayTraceShaderGroupType::ProceduralHitGroup,
None,
intersection_shader,
closest_hit_shader,
any_hit_shader,
)
}
/// Creates a new triangles-type shader group with the given closest-hit shader and optional any-hit
/// shader.
pub fn new_triangles(closest_hit_shader: u32, any_hit_shader: impl Into<Option<u32>>) -> Self {
Self::new(
RayTraceShaderGroupType::TrianglesHitGroup,
None,
None,
closest_hit_shader,
any_hit_shader,
)
}
}
impl From<RayTraceShaderGroup> for vk::RayTracingShaderGroupCreateInfoKHR {
fn from(shader_group: RayTraceShaderGroup) -> Self {
vk::RayTracingShaderGroupCreateInfoKHR::builder()
.ty(shader_group.ty.into())
.any_hit_shader(shader_group.any_hit_shader.unwrap_or(vk::SHADER_UNUSED_KHR))
.closest_hit_shader(
shader_group
.closest_hit_shader
.unwrap_or(vk::SHADER_UNUSED_KHR),
)
.general_shader(shader_group.general_shader.unwrap_or(vk::SHADER_UNUSED_KHR))
.intersection_shader(
shader_group
.intersection_shader
.unwrap_or(vk::SHADER_UNUSED_KHR),
)
.build()
}
}
/// Describes a type of ray tracing shader group, which is a collection of shaders which run in the
/// specified mode.
#[derive(Clone, Copy, Debug)]
pub enum RayTraceShaderGroupType {
/// A shader group with a general shader.
General,
/// A shader group with an intersection shader, and optional closest-hit and any-hit shaders.
ProceduralHitGroup,
/// A shader group with a closest-hit shader and optional any-hit shader.
TrianglesHitGroup,
}
impl From<RayTraceShaderGroupType> for vk::RayTracingShaderGroupTypeKHR {
fn from(ty: RayTraceShaderGroupType) -> Self {
match ty {
RayTraceShaderGroupType::General => vk::RayTracingShaderGroupTypeKHR::GENERAL,
RayTraceShaderGroupType::ProceduralHitGroup => {
vk::RayTracingShaderGroupTypeKHR::PROCEDURAL_HIT_GROUP
}
RayTraceShaderGroupType::TrianglesHitGroup => {
vk::RayTracingShaderGroupTypeKHR::TRIANGLES_HIT_GROUP
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
type Info = RayTracePipelineInfo;
type Builder = RayTracePipelineInfoBuilder;
#[test]
pub fn ray_trace_pipeline_info() {
let info = Info::default();
let builder = info.to_builder().build();
assert_eq!(info, builder);
}
#[test]
pub fn ray_trace_pipeline_info_builder() {
let info = Info::default();
let builder = Builder::default().build();
assert_eq!(info, builder);
}
}