easy_gpu/assets/compute/
pipeline.rs1use wgpu::{BindGroupLayout, BufferBindingType, ComputePipelineDescriptor, Device, ShaderModule, StorageTextureAccess, TextureFormat, TextureViewDimension};
2use crate::assets_manager::asset_manager::AssetManager;
3use crate::assets_manager::Handle;
4
5pub struct ComputePipeline{
6 pub pipeline: wgpu::ComputePipeline,
7 pub layout: BindGroupLayout,
8}
9
10pub struct ComputePipelineBuilder{
11 shader: Handle<ShaderModule>,
12 entries: Vec<wgpu::BindGroupLayoutEntry>,
13}
14
15impl ComputePipelineBuilder{
16 pub fn new(shader: Handle<ShaderModule>)->Self{
17 Self{
18 shader,
19 entries: vec![],
20 }
21 }
22 pub fn bind_group_layout(
23 mut self,
24 entries: &[wgpu::BindGroupLayoutEntry],
25 ) -> Self {
26 self.entries = entries.to_vec();
27 self
28 }
29 pub(crate) fn build(self,device: &Device,asset_manager: &AssetManager) -> ComputePipeline{
30 let bind_group_layout = device.create_bind_group_layout(
31 &wgpu::BindGroupLayoutDescriptor {
32 label: Some("material layout"),
33 entries: &self.entries,
34 }
35 );
36
37 let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor{
38 label: None,
39 bind_group_layouts: &[Some(&bind_group_layout)],
40 immediate_size: 0,
41 });
42
43 let pipeline = device.create_compute_pipeline(&ComputePipelineDescriptor{
44 label: Some("Compute Pipeline"),
45 layout: Some(&layout),
46 module: asset_manager.shaders.get(self.shader).unwrap(),
47 entry_point: Some("cs_main"),
48 compilation_options: Default::default(),
49 cache: None,
50 });
51
52 ComputePipeline{
53 pipeline,
54 layout: bind_group_layout,
55 }
56 }
57}
58
59pub fn storage(binding: u32,read_only: bool) -> wgpu::BindGroupLayoutEntry {
60 wgpu::BindGroupLayoutEntry {
61 binding,
62 visibility: wgpu::ShaderStages::COMPUTE,
63 ty: wgpu::BindingType::Buffer {
64 ty: BufferBindingType::Storage { read_only },
65 has_dynamic_offset: false,
66 min_binding_size: None,
67 },
68 count: None,
69 }
70}
71
72pub fn storage_texture(binding: u32) -> wgpu::BindGroupLayoutEntry {
73 wgpu::BindGroupLayoutEntry {
74 binding,
75 visibility: wgpu::ShaderStages::COMPUTE,
76 ty: wgpu::BindingType::StorageTexture {
77 access: StorageTextureAccess::WriteOnly,
78 format: TextureFormat::Rgba8Unorm,
79 view_dimension: TextureViewDimension::D2,
80 },
81 count: None,
82 }
83}
84
85