-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c52a39
commit 42c9d2c
Showing
5 changed files
with
329 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use wgpu::include_wgsl; | ||
|
||
pub struct Copy { | ||
pub bind_group_layout: wgpu::BindGroupLayout, | ||
pub bind_group: wgpu::BindGroup, | ||
pub pipeline: wgpu::ComputePipeline, | ||
} | ||
|
||
impl Copy { | ||
pub fn new( | ||
device: &wgpu::Device, | ||
config: &wgpu::SurfaceConfiguration, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
) -> Self { | ||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { | ||
label: Some("copy bind group layout"), | ||
entries: &[ | ||
wgpu::BindGroupLayoutEntry { | ||
binding: 0, | ||
ty: wgpu::BindingType::StorageTexture { | ||
access: wgpu::StorageTextureAccess::WriteOnly, | ||
format: wgpu::TextureFormat::Rgba8Unorm, //config.format, | ||
view_dimension: wgpu::TextureViewDimension::D2, | ||
}, | ||
count: None, | ||
visibility: wgpu::ShaderStages::COMPUTE, | ||
}, | ||
wgpu::BindGroupLayoutEntry { | ||
binding: 1, | ||
ty: wgpu::BindingType::StorageTexture { | ||
access: wgpu::StorageTextureAccess::WriteOnly, | ||
format: wgpu::TextureFormat::Rgba8Unorm, //config.format, | ||
view_dimension: wgpu::TextureViewDimension::D2, | ||
}, | ||
count: None, | ||
visibility: wgpu::ShaderStages::COMPUTE, | ||
}, | ||
], | ||
}); | ||
|
||
let bind_group = Self::create_bind_group(device, &bind_group_layout, source, destination); | ||
|
||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: Some("copy compute pipeline layout"), | ||
bind_group_layouts: &[], | ||
..Default::default() | ||
}); | ||
|
||
let module = device.create_shader_module(include_wgsl!("copy-compute.wgsl")); | ||
|
||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | ||
label: Some("copy compute pipeline"), | ||
layout: Some(&pipeline_layout), | ||
module: &module, | ||
entry_point: "cs_main", | ||
compilation_options: Default::default(), | ||
cache: None, | ||
}); | ||
|
||
Self { | ||
bind_group_layout, | ||
bind_group, | ||
pipeline, | ||
} | ||
} | ||
|
||
fn create_bind_group( | ||
device: &wgpu::Device, | ||
layout: &wgpu::BindGroupLayout, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
) -> wgpu::BindGroup { | ||
device.create_bind_group(&wgpu::BindGroupDescriptor { | ||
label: Some("copy compute bind group"), | ||
layout: &layout, | ||
entries: &[ | ||
wgpu::BindGroupEntry { | ||
binding: 0, | ||
resource: wgpu::BindingResource::TextureView(source), | ||
}, | ||
wgpu::BindGroupEntry { | ||
binding: 1, | ||
resource: wgpu::BindingResource::TextureView(destination), | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
pub fn resize( | ||
&mut self, | ||
device: &wgpu::Device, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
) { | ||
self.bind_group = | ||
Self::create_bind_group(device, &self.bind_group_layout, source, destination); | ||
} | ||
|
||
pub fn pass(&self, encoder: &mut wgpu::CommandEncoder, x: u32, y: u32) { | ||
{ | ||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { | ||
label: Some("copy compute pass"), | ||
..Default::default() | ||
}); | ||
pass.set_pipeline(&self.pipeline); | ||
pass.set_bind_group(0, &self.bind_group, &[]); | ||
pass.dispatch_workgroups(x, y, 1); | ||
//pass.dispatch_workgroups(x, y, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,208 @@ | ||
use wgpu::include_wgsl; | ||
|
||
pub struct Copy { | ||
pub input_texture: wgpu::Texture, | ||
pub input_texture_view: wgpu::TextureView, | ||
|
||
pub texture_sampler: wgpu::Sampler, | ||
|
||
pub bind_group_layout: wgpu::BindGroupLayout, | ||
pub bind_group: wgpu::BindGroup, | ||
pub pipeline: wgpu::ComputePipeline, | ||
pub render_pipeline: wgpu::RenderPipeline, | ||
} | ||
|
||
impl Copy { | ||
pub fn new( | ||
device: &wgpu::Device, | ||
config: &wgpu::SurfaceConfiguration, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
) -> Self { | ||
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self { | ||
let (input_texture, input_texture_view) = Self::create_input_texture(device, config); | ||
|
||
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor { | ||
address_mode_u: wgpu::AddressMode::ClampToEdge, | ||
address_mode_v: wgpu::AddressMode::ClampToEdge, | ||
address_mode_w: wgpu::AddressMode::ClampToEdge, | ||
mag_filter: wgpu::FilterMode::Linear, | ||
min_filter: wgpu::FilterMode::Linear, | ||
mipmap_filter: wgpu::FilterMode::Linear, | ||
..Default::default() | ||
}); | ||
|
||
let screen_triangle_shader_module = | ||
device.create_shader_module(wgpu::include_wgsl!("screen_triangle.wgsl")); | ||
let remix_shader_module = device.create_shader_module(wgpu::include_wgsl!("copy.wgsl")); | ||
|
||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { | ||
label: Some("copy bind group layout"), | ||
label: Some("remix bind group layout"), | ||
entries: &[ | ||
wgpu::BindGroupLayoutEntry { | ||
binding: 0, | ||
ty: wgpu::BindingType::StorageTexture { | ||
access: wgpu::StorageTextureAccess::WriteOnly, | ||
format: wgpu::TextureFormat::Rgba8Unorm, //config.format, | ||
visibility: wgpu::ShaderStages::FRAGMENT, | ||
ty: wgpu::BindingType::Texture { | ||
multisampled: false, | ||
view_dimension: wgpu::TextureViewDimension::D2, | ||
sample_type: wgpu::TextureSampleType::Float { filterable: true }, | ||
}, | ||
count: None, | ||
visibility: wgpu::ShaderStages::COMPUTE, | ||
}, | ||
wgpu::BindGroupLayoutEntry { | ||
binding: 1, | ||
ty: wgpu::BindingType::StorageTexture { | ||
access: wgpu::StorageTextureAccess::WriteOnly, | ||
format: wgpu::TextureFormat::Rgba8Unorm, //config.format, | ||
view_dimension: wgpu::TextureViewDimension::D2, | ||
}, | ||
visibility: wgpu::ShaderStages::FRAGMENT, | ||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), | ||
count: None, | ||
visibility: wgpu::ShaderStages::COMPUTE, | ||
}, | ||
], | ||
}); | ||
|
||
let bind_group = Self::create_bind_group(device, &bind_group_layout, source, destination); | ||
let bind_group = Self::create_bind_group( | ||
device, | ||
&bind_group_layout, | ||
&input_texture_view, | ||
&texture_sampler, | ||
); | ||
|
||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: Some("copy compute pipeline layout"), | ||
bind_group_layouts: &[], | ||
..Default::default() | ||
label: Some("remix pipeline layout"), | ||
bind_group_layouts: &[&bind_group_layout], | ||
push_constant_ranges: &[], | ||
}); | ||
|
||
let module = device.create_shader_module(include_wgsl!("copy-compute.wgsl")); | ||
|
||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { | ||
label: Some("copy compute pipeline"), | ||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { | ||
label: Some("remix render pipeline"), | ||
layout: Some(&pipeline_layout), | ||
module: &module, | ||
entry_point: "cs_main", | ||
compilation_options: Default::default(), | ||
vertex: wgpu::VertexState { | ||
module: &screen_triangle_shader_module, | ||
entry_point: "main", | ||
buffers: &[], | ||
compilation_options: Default::default(), | ||
}, | ||
fragment: Some(wgpu::FragmentState { | ||
module: &remix_shader_module, | ||
entry_point: "main", | ||
targets: &[Some(wgpu::ColorTargetState { | ||
format: config.format, | ||
blend: Some(wgpu::BlendState::REPLACE), | ||
write_mask: wgpu::ColorWrites::ALL, | ||
})], | ||
compilation_options: Default::default(), | ||
}), | ||
primitive: wgpu::PrimitiveState { | ||
topology: wgpu::PrimitiveTopology::TriangleList, | ||
strip_index_format: None, | ||
front_face: wgpu::FrontFace::Ccw, | ||
cull_mode: Some(wgpu::Face::Back), | ||
polygon_mode: wgpu::PolygonMode::Fill, | ||
unclipped_depth: false, | ||
conservative: false, | ||
}, | ||
depth_stencil: None, | ||
multisample: wgpu::MultisampleState { | ||
count: 1, | ||
mask: !0, | ||
alpha_to_coverage_enabled: false, | ||
}, | ||
multiview: None, | ||
cache: None, | ||
}); | ||
|
||
Self { | ||
input_texture, | ||
input_texture_view, | ||
|
||
texture_sampler, | ||
|
||
bind_group_layout, | ||
bind_group, | ||
pipeline, | ||
render_pipeline, | ||
} | ||
} | ||
|
||
pub fn input_texture_view(&self) -> &wgpu::TextureView { | ||
&self.input_texture_view | ||
} | ||
|
||
fn create_input_texture( | ||
device: &wgpu::Device, | ||
config: &wgpu::SurfaceConfiguration, | ||
) -> (wgpu::Texture, wgpu::TextureView) { | ||
let input_texture = device.create_texture(&wgpu::TextureDescriptor { | ||
label: Some("bloom input texture"), | ||
mip_level_count: 1, | ||
size: wgpu::Extent3d { | ||
width: config.width, | ||
height: config.height, | ||
depth_or_array_layers: 1, | ||
}, | ||
//format: wgpu::TextureFormat::Bgra8UnormSrgb, | ||
format: config.format, | ||
dimension: wgpu::TextureDimension::D2, | ||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, | ||
sample_count: 1, | ||
view_formats: &[], | ||
}); | ||
let input_texture_view = input_texture.create_view(&wgpu::TextureViewDescriptor::default()); | ||
(input_texture, input_texture_view) | ||
} | ||
|
||
// this creates all the bind groups for the final re-mix | ||
fn create_bind_group( | ||
device: &wgpu::Device, | ||
layout: &wgpu::BindGroupLayout, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
texture_view: &wgpu::TextureView, | ||
texture_sampler: &wgpu::Sampler, | ||
) -> wgpu::BindGroup { | ||
device.create_bind_group(&wgpu::BindGroupDescriptor { | ||
label: Some("copy compute bind group"), | ||
layout: &layout, | ||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { | ||
label: Some("remix bind group"), | ||
layout, | ||
entries: &[ | ||
wgpu::BindGroupEntry { | ||
binding: 0, | ||
resource: wgpu::BindingResource::TextureView(source), | ||
resource: wgpu::BindingResource::TextureView(texture_view), | ||
}, | ||
wgpu::BindGroupEntry { | ||
binding: 1, | ||
resource: wgpu::BindingResource::TextureView(destination), | ||
resource: wgpu::BindingResource::Sampler(texture_sampler), | ||
}, | ||
], | ||
}) | ||
}); | ||
bind_group | ||
} | ||
|
||
pub fn resize( | ||
&mut self, | ||
device: &wgpu::Device, | ||
source: &wgpu::TextureView, | ||
destination: &wgpu::TextureView, | ||
) { | ||
self.bind_group = | ||
Self::create_bind_group(device, &self.bind_group_layout, source, destination); | ||
pub fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) { | ||
(self.input_texture, self.input_texture_view) = Self::create_input_texture(device, config); | ||
self.bind_group = Self::create_bind_group( | ||
device, | ||
&self.bind_group_layout, | ||
&self.input_texture_view, | ||
&self.texture_sampler, | ||
); | ||
} | ||
|
||
pub fn pass(&self, encoder: &mut wgpu::CommandEncoder, x: u32, y: u32) { | ||
pub fn render( | ||
&self, | ||
encoder: &mut wgpu::CommandEncoder, | ||
output_view: Option<&wgpu::TextureView>, | ||
) { | ||
{ | ||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { | ||
label: Some("copy compute pass"), | ||
..Default::default() | ||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { | ||
label: Some("blur render pass"), | ||
color_attachments: &[output_view.map(|output_view| { | ||
wgpu::RenderPassColorAttachment { | ||
view: output_view, | ||
resolve_target: None, | ||
ops: wgpu::Operations { | ||
load: wgpu::LoadOp::Clear(wgpu::Color { | ||
r: 0.0, | ||
g: 0.0, | ||
b: 1.0, | ||
a: 1.0, | ||
}), | ||
store: wgpu::StoreOp::Store, | ||
}, | ||
} | ||
})], | ||
depth_stencil_attachment: None, | ||
timestamp_writes: None, | ||
occlusion_query_set: None, | ||
}); | ||
pass.set_pipeline(&self.pipeline); | ||
pass.set_bind_group(0, &self.bind_group, &[]); | ||
pass.dispatch_workgroups(x, y, 1); | ||
//pass.dispatch_workgroups(x, y, 1); | ||
render_pass.set_pipeline(&self.render_pipeline); | ||
render_pass.set_bind_group(0, &self.bind_group, &[]); | ||
render_pass.draw(0..3, 0..1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.