use super::*;
use wgpu::util::DeviceExt;
const SRC: &str = r#"
@group(0) @binding(0) var tex: texture_2d<f32>;
@group(0) @binding(1) var samp: sampler;
struct VOut {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
};
@vertex
fn vs_main(@location(0) clip: vec4<f32>, @location(1) uv: vec2<f32>) -> VOut {
var o: VOut;
o.position = clip;
o.uv = uv;
return o;
}
@fragment
fn fs_main(in: VOut) -> @location(0) vec4<f32> {
return textureSample(tex, samp, in.uv);
}
"#;
const ATTRS: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x4, 1 => Float32x2];
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
clip: [f32; 4],
uv: [f32; 2],
}
pub(super) struct BurnPipe {
pipeline: wgpu::RenderPipeline,
}
impl BurnPipe {
pub(super) fn new(device: &wgpu::Device, bgl_tex: &wgpu::BindGroupLayout) -> Self {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("burn"),
source: wgpu::ShaderSource::Wgsl(SRC.into()),
});
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("burn"),
bind_group_layouts: &[Some(bgl_tex)],
immediate_size: 0,
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("burn"),
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &module,
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &ATTRS,
})],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &module,
entry_point: Some("fs_main"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: INTERNAL_FORMAT,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
Self { pipeline }
}
}
fn quad(target: (u32, u32), rect: [i32; 4]) -> [Vertex; 4] {
let (w, h) = (target.0 as f32, target.1 as f32);
let [left, top, width, height] = rect.map(|v| v as f32);
let (sx, sy) = (width / w, height / h);
let (tx, ty) = (left / w, top / h);
[
(-1.0f32, 1.0f32, 0.0f32, 0.0f32),
(1.0, 1.0, 1.0, 0.0),
(-1.0, -1.0, 0.0, 1.0),
(1.0, -1.0, 1.0, 1.0),
]
.map(|(x, y, u, v)| Vertex {
clip: [x * sx, y * sy, 0.0, 1.0 + x * tx + y * ty],
uv: [u, v],
})
}
impl Renderer {
pub fn burn_texture(
&mut self,
rgba8: &[u8],
w: u32,
h: u32,
rect: [i32; 4],
) -> Result<(), RenderError> {
check_frames(
"burn texture",
self.device.limits().max_texture_dimension_2d,
w,
h,
&[rgba8],
)?;
let src = upload_2d(&self.device, &self.queue, w, h, rgba8, false, "burn");
let pipe = self
.pipes
.burn
.get_or_init(|| BurnPipe::new(&self.device, &self.pipes.bgl_tex));
let verts = quad((self.cfg.width, self.cfg.height), rect);
let buf = self
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("burn verts"),
contents: bytemuck::cast_slice(&verts),
usage: wgpu::BufferUsages::VERTEX,
});
let bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("burn"),
layout: &self.pipes.bgl_tex,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&src.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(self.pipes.sampler(
Filter::Point,
Address::Clamp,
true,
)),
},
],
});
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("burn"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("burn"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &self.main[self.main_idx].view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
})],
..Default::default()
});
pass.set_pipeline(&pipe.pipeline);
pass.set_bind_group(0, &bg, &[]);
pass.set_vertex_buffer(0, buf.slice(..));
pass.draw(0..4, 0..1);
}
self.queue.submit([encoder.finish()]);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::quad;
#[test]
fn origin_burn_scales_about_the_centre() {
let q = quad((100, 50), [0, 0, 50, 25]);
assert_eq!(q[0].clip, [-0.5, 0.5, 0.0, 1.0]);
assert_eq!(q[3].clip, [0.5, -0.5, 0.0, 1.0]);
}
}