bort_vk/
pipeline_compute.rs

1use crate::{
2    Device, DeviceOwned, PipelineAccess, PipelineCache, PipelineLayout, ShaderStage,
3    ALLOCATION_CALLBACK_NONE,
4};
5use ash::{
6    prelude::VkResult,
7    vk::{self, Handle},
8};
9use std::sync::Arc;
10
11pub struct ComputePipeline {
12    handle: vk::Pipeline,
13    properties: ComputePipelineProperties,
14
15    // dependencies
16    pipeline_layout: Arc<PipelineLayout>,
17    // note: we don't need to store references to `ShaderModule` or `PipelineCache` as per https://registry.khronos.org/vulkan/specs/1.0/html/vkspec.html#fundamentals-objectmodel-lifetime
18}
19
20impl ComputePipeline {
21    pub fn new(
22        pipeline_layout: Arc<PipelineLayout>,
23        properties: ComputePipelineProperties,
24        shader_stage: &ShaderStage,
25        pipeline_cache: Option<&PipelineCache>,
26    ) -> VkResult<Self> {
27        let shader_stage_vk = shader_stage.create_info_builder().build();
28
29        let create_info_builder = properties
30            .create_info_builder()
31            .stage(shader_stage_vk)
32            .layout(pipeline_layout.handle());
33
34        let cache_handle = if let Some(pipeline_cache) = pipeline_cache {
35            pipeline_cache.handle()
36        } else {
37            vk::PipelineCache::null()
38        };
39
40        let handles = unsafe {
41            pipeline_layout.device().inner().create_compute_pipelines(
42                cache_handle,
43                &[create_info_builder.build()],
44                ALLOCATION_CALLBACK_NONE,
45            )
46        }
47        .map_err(|(_pipelines, err_code)| err_code)?;
48        let handle = handles[0];
49
50        Ok(Self {
51            handle,
52            properties,
53            pipeline_layout,
54        })
55    }
56
57    pub fn properties(&self) -> &ComputePipelineProperties {
58        &self.properties
59    }
60}
61
62impl PipelineAccess for ComputePipeline {
63    fn handle(&self) -> vk::Pipeline {
64        self.handle
65    }
66
67    fn pipeline_layout(&self) -> &Arc<PipelineLayout> {
68        &self.pipeline_layout
69    }
70
71    fn bind_point(&self) -> vk::PipelineBindPoint {
72        vk::PipelineBindPoint::COMPUTE
73    }
74}
75
76impl DeviceOwned for ComputePipeline {
77    #[inline]
78    fn device(&self) -> &Arc<Device> {
79        &self.pipeline_layout.device()
80    }
81
82    #[inline]
83    fn handle_raw(&self) -> u64 {
84        self.handle.as_raw()
85    }
86}
87
88impl Drop for ComputePipeline {
89    fn drop(&mut self) {
90        unsafe {
91            self.device()
92                .inner()
93                .destroy_pipeline(self.handle, ALLOCATION_CALLBACK_NONE)
94        }
95    }
96}
97
98#[derive(Clone)]
99pub struct ComputePipelineProperties {
100    pub flags: vk::PipelineCreateFlags,
101}
102
103impl ComputePipelineProperties {
104    pub fn create_info_builder(&self) -> vk::ComputePipelineCreateInfoBuilder {
105        vk::ComputePipelineCreateInfo::builder().flags(self.flags)
106    }
107}