use super::common;
use crate::draw::ShaderManager;
use kas::draw::{PassId, color::Rgba};
use kas::geom::{Quad, Vec2};
use std::mem::size_of;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Vertex(Vec2, Rgba, Rgba, Vec2);
unsafe impl bytemuck::Zeroable for Vertex {}
unsafe impl bytemuck::Pod for Vertex {}
pub type Window = common::Window<Vertex>;
pub struct Pipeline {
render_pipeline: wgpu::RenderPipeline,
}
impl Pipeline {
pub fn new(
device: &wgpu::Device,
shaders: &ShaderManager,
bgl_common: &wgpu::BindGroupLayout,
) -> Self {
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("R2C pipeline_layout"),
bind_group_layouts: &[bgl_common],
immediate_size: 0,
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("R2C render_pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shaders.vert_round_2col,
entry_point: Some("main"),
compilation_options: Default::default(),
buffers: &[wgpu::VertexBufferLayout {
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x2,
1 => Float32x4,
2 => Float32x4,
3 => Float32x2,
],
}],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_round_2col,
entry_point: Some("main"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
Pipeline { render_pipeline }
}
pub fn render<'a>(
&'a self,
window: &'a Window,
pass: usize,
rpass: &mut wgpu::RenderPass<'a>,
bg_common: &'a wgpu::BindGroup,
) {
window.render(pass, rpass, &self.render_pipeline, bg_common);
}
}
impl Window {
pub fn circle(&mut self, pass: PassId, rect: Quad, col1: Rgba, col2: Rgba) {
let aa = rect.a;
let bb = rect.b;
if !(aa < bb) || (col1.a == 0.0 && col2.a == 0.0) {
return;
}
let ab = Vec2(aa.0, bb.1);
let ba = Vec2(bb.0, aa.1);
let mid = (aa + bb) * 0.5;
let n0 = Vec2::splat(0.0);
let nb = Vec2::ONE; let na = -nb;
let nab = Vec2(na.0, nb.1);
let nba = Vec2(nb.0, na.1);
let aa = Vertex(aa, col1, col2, na);
let ab = Vertex(ab, col1, col2, nab);
let ba = Vertex(ba, col1, col2, nba);
let bb = Vertex(bb, col1, col2, nb);
let mid = Vertex(mid, col1, col2, n0);
#[rustfmt::skip]
self.add_vertices(pass.pass(), &[
ba, mid, aa,
bb, mid, ba,
ab, mid, bb,
aa, mid, ab,
]);
}
pub fn frame(&mut self, pass: PassId, outer: Quad, inner: Quad, col1: Rgba, col2: Rgba) {
let aa = outer.a;
let bb = outer.b;
let mut cc = inner.a;
let mut dd = inner.b;
if !(aa < bb) || (col1.a == 0.0 && col2.a == 0.0) {
return;
}
if !(aa <= cc) || !(cc <= bb) {
cc = aa;
}
if !(aa <= dd) || !(dd <= bb) {
dd = bb;
}
if !(cc <= dd) {
dd = cc;
}
let ab = Vec2(aa.0, bb.1);
let ba = Vec2(bb.0, aa.1);
let cd = Vec2(cc.0, dd.1);
let dc = Vec2(dd.0, cc.1);
let n0 = Vec2::splat(0.0);
let nb = (bb - aa).sign();
let na = -nb;
let nab = Vec2(na.0, nb.1);
let nba = Vec2(nb.0, na.1);
let na0 = Vec2(na.0, 0.0);
let nb0 = Vec2(nb.0, 0.0);
let n0a = Vec2(0.0, na.1);
let n0b = Vec2(0.0, nb.1);
let ab = Vertex(ab, col1, col2, nab);
let ba = Vertex(ba, col1, col2, nba);
let cd = Vertex(cd, col1, col2, n0);
let dc = Vertex(dc, col1, col2, n0);
let ac = Vertex(Vec2(aa.0, cc.1), col1, col2, na0);
let ad = Vertex(Vec2(aa.0, dd.1), col1, col2, na0);
let bc = Vertex(Vec2(bb.0, cc.1), col1, col2, nb0);
let bd = Vertex(Vec2(bb.0, dd.1), col1, col2, nb0);
let ca = Vertex(Vec2(cc.0, aa.1), col1, col2, n0a);
let cb = Vertex(Vec2(cc.0, bb.1), col1, col2, n0b);
let da = Vertex(Vec2(dd.0, aa.1), col1, col2, n0a);
let db = Vertex(Vec2(dd.0, bb.1), col1, col2, n0b);
let aa = Vertex(aa, col1, col2, na);
let bb = Vertex(bb, col1, col2, nb);
let cc = Vertex(cc, col1, col2, n0);
let dd = Vertex(dd, col1, col2, n0);
#[rustfmt::skip]
self.add_vertices(pass.pass(), &[
ba, dc, da,
da, dc, cc,
da, cc, ca,
ca, cc, aa,
aa, cc, ac,
ac, cc, cd,
ac, cd, ad,
ad, cd, ab,
ab, cd, cb,
cb, cd, dd,
cb, dd, db,
db, dd, bb,
bb, dd, bd,
bd, dd, dc,
bd, dc, bc,
bc, dc, ba,
]);
}
}