|
| 1 | +use crate::WgpuContext; |
| 2 | +use glam::{DAffine2, UVec2, Vec2}; |
| 3 | + |
| 4 | +pub struct Resampler { |
| 5 | + pipeline: wgpu::RenderPipeline, |
| 6 | + bind_group_layout: wgpu::BindGroupLayout, |
| 7 | +} |
| 8 | + |
| 9 | +impl Resampler { |
| 10 | + pub fn new(device: &wgpu::Device) -> Self { |
| 11 | + let shader = device.create_shader_module(wgpu::include_wgsl!("resample_shader.wgsl")); |
| 12 | + |
| 13 | + let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 14 | + label: Some("resample_bind_group_layout"), |
| 15 | + entries: &[ |
| 16 | + wgpu::BindGroupLayoutEntry { |
| 17 | + binding: 0, |
| 18 | + visibility: wgpu::ShaderStages::FRAGMENT, |
| 19 | + ty: wgpu::BindingType::Texture { |
| 20 | + multisampled: false, |
| 21 | + view_dimension: wgpu::TextureViewDimension::D2, |
| 22 | + sample_type: wgpu::TextureSampleType::Float { filterable: false }, |
| 23 | + }, |
| 24 | + count: None, |
| 25 | + }, |
| 26 | + wgpu::BindGroupLayoutEntry { |
| 27 | + binding: 1, |
| 28 | + visibility: wgpu::ShaderStages::FRAGMENT, |
| 29 | + ty: wgpu::BindingType::Buffer { |
| 30 | + ty: wgpu::BufferBindingType::Uniform, |
| 31 | + has_dynamic_offset: false, |
| 32 | + min_binding_size: None, |
| 33 | + }, |
| 34 | + count: None, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }); |
| 38 | + |
| 39 | + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { |
| 40 | + label: Some("resample_pipeline_layout"), |
| 41 | + bind_group_layouts: &[&bind_group_layout], |
| 42 | + push_constant_ranges: &[], |
| 43 | + }); |
| 44 | + |
| 45 | + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { |
| 46 | + label: Some("resample_pipeline"), |
| 47 | + layout: Some(&pipeline_layout), |
| 48 | + vertex: wgpu::VertexState { |
| 49 | + module: &shader, |
| 50 | + entry_point: Some("vs_main"), |
| 51 | + buffers: &[], |
| 52 | + compilation_options: wgpu::PipelineCompilationOptions::default(), |
| 53 | + }, |
| 54 | + fragment: Some(wgpu::FragmentState { |
| 55 | + module: &shader, |
| 56 | + entry_point: Some("fs_main"), |
| 57 | + targets: &[Some(wgpu::ColorTargetState { |
| 58 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 59 | + blend: None, |
| 60 | + write_mask: wgpu::ColorWrites::ALL, |
| 61 | + })], |
| 62 | + compilation_options: wgpu::PipelineCompilationOptions::default(), |
| 63 | + }), |
| 64 | + primitive: wgpu::PrimitiveState { |
| 65 | + topology: wgpu::PrimitiveTopology::TriangleList, |
| 66 | + ..Default::default() |
| 67 | + }, |
| 68 | + depth_stencil: None, |
| 69 | + multisample: wgpu::MultisampleState::default(), |
| 70 | + multiview: None, |
| 71 | + cache: None, |
| 72 | + }); |
| 73 | + |
| 74 | + Resampler { pipeline, bind_group_layout } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn resample(&self, context: &WgpuContext, source: &wgpu::Texture, target_size: UVec2, transform: &DAffine2) -> wgpu::Texture { |
| 78 | + let device = &context.device; |
| 79 | + let queue = &context.queue; |
| 80 | + |
| 81 | + let output_texture = device.create_texture(&wgpu::TextureDescriptor { |
| 82 | + label: Some("resample_output"), |
| 83 | + size: wgpu::Extent3d { |
| 84 | + width: target_size.x.max(1), |
| 85 | + height: target_size.y.max(1), |
| 86 | + depth_or_array_layers: 1, |
| 87 | + }, |
| 88 | + mip_level_count: 1, |
| 89 | + sample_count: 1, |
| 90 | + dimension: wgpu::TextureDimension::D2, |
| 91 | + format: wgpu::TextureFormat::Rgba8Unorm, |
| 92 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::TEXTURE_BINDING, |
| 93 | + view_formats: &[], |
| 94 | + }); |
| 95 | + |
| 96 | + let source_view = source.create_view(&wgpu::TextureViewDescriptor::default()); |
| 97 | + let output_view = output_texture.create_view(&wgpu::TextureViewDescriptor::default()); |
| 98 | + |
| 99 | + let params_buffer = device.create_buffer(&wgpu::BufferDescriptor { |
| 100 | + label: Some("resample_params"), |
| 101 | + size: 32, |
| 102 | + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 103 | + mapped_at_creation: false, |
| 104 | + }); |
| 105 | + |
| 106 | + let params_data = [transform.matrix2.x_axis.as_vec2(), transform.matrix2.y_axis.as_vec2(), transform.translation.as_vec2(), Vec2::ZERO]; |
| 107 | + queue.write_buffer(¶ms_buffer, 0, bytemuck::cast_slice(¶ms_data)); |
| 108 | + |
| 109 | + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 110 | + label: Some("resample_bind_group"), |
| 111 | + layout: &self.bind_group_layout, |
| 112 | + entries: &[ |
| 113 | + wgpu::BindGroupEntry { |
| 114 | + binding: 0, |
| 115 | + resource: wgpu::BindingResource::TextureView(&source_view), |
| 116 | + }, |
| 117 | + wgpu::BindGroupEntry { |
| 118 | + binding: 1, |
| 119 | + resource: params_buffer.as_entire_binding(), |
| 120 | + }, |
| 121 | + ], |
| 122 | + }); |
| 123 | + |
| 124 | + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("resample_encoder") }); |
| 125 | + |
| 126 | + { |
| 127 | + let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 128 | + label: Some("resample_pass"), |
| 129 | + color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 130 | + view: &output_view, |
| 131 | + resolve_target: None, |
| 132 | + ops: wgpu::Operations { |
| 133 | + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), |
| 134 | + store: wgpu::StoreOp::Store, |
| 135 | + }, |
| 136 | + depth_slice: None, |
| 137 | + })], |
| 138 | + depth_stencil_attachment: None, |
| 139 | + timestamp_writes: None, |
| 140 | + occlusion_query_set: None, |
| 141 | + }); |
| 142 | + |
| 143 | + render_pass.set_pipeline(&self.pipeline); |
| 144 | + render_pass.set_bind_group(0, &bind_group, &[]); |
| 145 | + render_pass.draw(0..3, 0..1); |
| 146 | + } |
| 147 | + |
| 148 | + queue.submit([encoder.finish()]); |
| 149 | + |
| 150 | + output_texture |
| 151 | + } |
| 152 | +} |
0 commit comments