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
//!
//! Pipeline that sphere marches the voxel grid of atoms in a compute shader.
//!

use crate::utils::{load_glsl, ShaderStage};
use wgpu;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct RaymarchGlobals {
    pub projection: [f32; 16],
    pub camera_origin: [f32; 3],
    pub padd0: f32,

    pub bb_min: [f32; 3],
    pub padd1: f32,
    pub bb_max: [f32; 3],
    pub padd2: f32,
    pub bb_diff: [f32; 3],
    pub padd3: f32,
    pub bb_size: [f32; 3],
    pub voxel_length: f32,

    pub time: f32,
    pub solvent_radius: f32,
    pub max_neighbours: i32,
    pub save: i32,
}

unsafe impl bytemuck::Zeroable for RaymarchGlobals {}
unsafe impl bytemuck::Pod for RaymarchGlobals {}

impl Default for RaymarchGlobals {
    fn default() -> Self {
        Self {
            projection: [0.0; 16],
            camera_origin: [0.0; 3],
            padd0: 0.0,

            bb_min: [0.0; 3],
            padd1: 0.0,
            bb_max: [0.0; 3],
            padd2: 0.0,
            bb_diff: [0.0; 3],
            padd3: 0.0,
            bb_size: [0.0; 3],
            voxel_length: 0.0,

            time: 0.0,
            solvent_radius: 0.0,
            max_neighbours: 0,
            save: 0,
        }
    }
}

pub struct RaymarchPipeline {
    pub pipeline: wgpu::ComputePipeline,
    pub bind_group_layout: wgpu::BindGroupLayout,
}

impl RaymarchPipeline {
    pub fn new(device: &wgpu::Device) -> Self {
        // Shaders
        let cs_bytes = load_glsl(include_str!("raymarch.comp"), ShaderStage::Compute);
        let cs_module = device.create_shader_module(&cs_bytes);

        // Bind Groups
        let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("Raymarch bind group layout"),
            bindings: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::UniformBuffer { dynamic: false },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageTexture {
                        dimension: wgpu::TextureViewDimension::D2,
                        component_type: wgpu::TextureComponentType::Float,
                        format: wgpu::TextureFormat::Rgba32Float,
                        readonly: false,
                    },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageBuffer {
                        dynamic: false,
                        readonly: true,
                    },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 3,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageBuffer {
                        dynamic: false,
                        readonly: true,
                    },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 4,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageTexture {
                        dimension: wgpu::TextureViewDimension::D2,
                        component_type: wgpu::TextureComponentType::Float,
                        format: wgpu::TextureFormat::R32Float,
                        readonly: false,
                    },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 5,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageTexture {
                        dimension: wgpu::TextureViewDimension::D2,
                        component_type: wgpu::TextureComponentType::Float,
                        format: wgpu::TextureFormat::Rgba32Float,
                        readonly: false,
                    },
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 6,
                    visibility: wgpu::ShaderStage::COMPUTE,
                    ty: wgpu::BindingType::StorageTexture {
                        dimension: wgpu::TextureViewDimension::D2,
                        component_type: wgpu::TextureComponentType::Float,
                        format: wgpu::TextureFormat::Rgba32Float,
                        readonly: false,
                    },
                },
            ],
        });

        // Pipeline
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            bind_group_layouts: &[&bind_group_layout],
        });

        let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            layout: &pipeline_layout,
            compute_stage: wgpu::ProgrammableStageDescriptor {
                module: &cs_module,
                entry_point: "main",
            },
        });

        Self {
            pipeline,
            bind_group_layout,
        }
    }
}