use super::{shader_source, WgpuRender, WgpuRenderBase};
use std::mem;
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct TransitionVertex {
pub position: [f32; 2],
pub tex_coords: [f32; 2],
}
unsafe impl bytemuck::Pod for TransitionVertex {}
unsafe impl bytemuck::Zeroable for TransitionVertex {}
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct TransitionUniforms {
pub progress: f32,
pub _padding1: [f32; 3], pub _padding2: [f32; 4], pub _padding3: [f32; 4], }
pub struct WgpuTransitionRender {
base: WgpuRenderBase,
progress: f32,
shader_idx: usize,
vertex_buffer: Option<wgpu::Buffer>,
index_buffer: Option<wgpu::Buffer>,
uniform_buffer: Option<wgpu::Buffer>,
render_pipelines: Vec<Option<wgpu::RenderPipeline>>,
bind_group_layout: Option<wgpu::BindGroupLayout>,
current_bind_group: Option<wgpu::BindGroup>,
texture_sampler: Option<wgpu::Sampler>,
}
impl WgpuRender for WgpuTransitionRender {
fn new(canvas_width: u32, canvas_height: u32) -> Self {
let mut render_pipelines = Vec::new();
for _ in 0..7 {
render_pipelines.push(None);
}
Self {
base: WgpuRenderBase::new(0, canvas_width, canvas_height),
progress: 0.0,
shader_idx: 0,
vertex_buffer: None,
index_buffer: None,
uniform_buffer: None,
render_pipelines,
bind_group_layout: None,
current_bind_group: None,
texture_sampler: None,
}
}
fn get_base(&mut self) -> &mut WgpuRenderBase {
&mut self.base
}
fn create_shader(&mut self, device: &wgpu::Device) {
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Transition Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
]
});
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Transition Texture Sampler"),
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
self.bind_group_layout = Some(bind_group_layout);
self.texture_sampler = Some(texture_sampler);
}
fn create_buffer(&mut self, device: &wgpu::Device) {
let vertices = [
TransitionVertex { position: [-1.0, -1.0], tex_coords: [0.0, 1.0] }, TransitionVertex { position: [ 1.0, -1.0], tex_coords: [1.0, 1.0] }, TransitionVertex { position: [ 1.0, 1.0], tex_coords: [1.0, 0.0] }, TransitionVertex { position: [-1.0, 1.0], tex_coords: [0.0, 0.0] }, ];
let indices: [u16; 6] = [0, 1, 2, 2, 3, 0];
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Transition Vertex Buffer"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Transition Index Buffer"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Transition Uniform Buffer"),
size: mem::size_of::<TransitionUniforms>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
self.vertex_buffer = Some(vertex_buffer);
self.index_buffer = Some(index_buffer);
self.uniform_buffer = Some(uniform_buffer);
}
fn prepare_draw(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
let uniforms = TransitionUniforms {
progress: self.progress,
_padding1: [0.0; 3],
_padding2: [0.0; 4],
_padding3: [0.0; 4],
};
if let Some(uniform_buffer) = &self.uniform_buffer {
queue.write_buffer(uniform_buffer, 0, bytemuck::cast_slice(&[uniforms]));
}
}
fn draw(&mut self, encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView) {
log::warn!("Transition draw() called without device and format. This method is deprecated.");
}
fn cleanup(&mut self, _device: &wgpu::Device) {
self.vertex_buffer = None;
self.index_buffer = None;
self.uniform_buffer = None;
self.render_pipelines.clear();
self.bind_group_layout = None;
self.current_bind_group = None;
self.texture_sampler = None;
}
}
impl TransitionVertex {
pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<TransitionVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
],
}
}
}
impl WgpuTransitionRender {
pub fn set_textures(
&mut self,
device: &wgpu::Device,
texture1: &wgpu::TextureView,
texture2: &wgpu::TextureView,
) {
if let (Some(bind_group_layout), Some(uniform_buffer), Some(sampler)) = (
&self.bind_group_layout,
&self.uniform_buffer,
&self.texture_sampler,
) {
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Transition Bind Group"),
layout: bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(texture1),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(texture2),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(sampler),
},
],
});
self.current_bind_group = Some(bind_group);
}
}
pub fn set_shader_index(&mut self, shader_idx: usize) -> &mut Self {
self.shader_idx = shader_idx.min(6); self
}
pub fn set_progress(&mut self, progress: f32) -> &mut Self {
self.progress = progress.clamp(0.0, 1.0);
self
}
fn get_or_create_pipeline(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat, shader_idx: usize) -> Option<&wgpu::RenderPipeline> {
if shader_idx < self.render_pipelines.len() && self.render_pipelines[shader_idx].is_some() {
return self.render_pipelines[shader_idx].as_ref();
}
let bind_group_layout = match &self.bind_group_layout {
Some(layout) => layout,
None => return None,
};
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Transition Pipeline Layout"),
bind_group_layouts: &[bind_group_layout],
push_constant_ranges: &[],
});
let transition_shaders = shader_source::get_transition_shaders();
if shader_idx >= transition_shaders.len() {
return None;
}
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(&format!("Transition Shader {}", shader_idx)),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(transition_shaders[shader_idx])),
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(&format!("Transition Render Pipeline {} (format: {:?})", shader_idx, format)),
layout: Some(&pipeline_layout),
cache: None,
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[TransitionVertex::desc()],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format, blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
});
while self.render_pipelines.len() <= shader_idx {
self.render_pipelines.push(None);
}
self.render_pipelines[shader_idx] = Some(render_pipeline);
self.render_pipelines[shader_idx].as_ref()
}
pub fn draw_transition(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
shader_idx: usize,
progress: f32,
) {
self.set_shader_index(shader_idx);
self.set_progress(progress);
self.prepare_draw(device, queue);
self.draw_with_format(device, encoder, view, wgpu::TextureFormat::Bgra8Unorm); }
pub fn draw_transition_with_format(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
target_format: wgpu::TextureFormat,
shader_idx: usize,
progress: f32,
) {
self.set_shader_index(shader_idx);
self.set_progress(progress);
self.prepare_draw(device, queue);
self.draw_with_format(device, encoder, view, target_format);
}
fn draw_with_format(
&mut self,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
target_format: wgpu::TextureFormat,
) {
let pipeline_exists = self.get_or_create_pipeline(device, target_format, self.shader_idx).is_some();
if !pipeline_exists {
return;
}
let pipeline = match &self.render_pipelines[self.shader_idx] {
Some(pipeline) => pipeline,
None => return,
};
let bind_group = match &self.current_bind_group {
Some(bind_group) => bind_group,
None => return,
};
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Transition Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
});
render_pass.set_pipeline(pipeline);
render_pass.set_bind_group(0, bind_group, &[]);
if let (Some(vertex_buffer), Some(index_buffer)) = (&self.vertex_buffer, &self.index_buffer) {
render_pass.set_vertex_buffer(0, vertex_buffer.slice(..));
render_pass.set_index_buffer(index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..6, 0, 0..1);
}
}
}