use super::common;
use crate::draw::ShaderManager;
use kas::draw::{PassId, color::Rgba};
use kas::geom::{Quad, Vec2};
use std::f32::consts::FRAC_PI_2;
use std::mem::size_of;
const OFFSET: f32 = 0.5 * std::f32::consts::FRAC_1_SQRT_2;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Vertex(Vec2, Rgba, Vec2, Vec2, Vec2);
unsafe impl bytemuck::Zeroable for Vertex {}
unsafe impl bytemuck::Pod for Vertex {}
impl Vertex {
fn new2(v: Vec2, col: Rgba, n: Vec2, adjust: Vec2, p: Vec2) -> Self {
Vertex(v, col, n, adjust, p)
}
}
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("SR pipeline_layout"),
bind_group_layouts: &[bgl_common],
immediate_size: 0,
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("SR render_pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shaders.vert_shaded_round,
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 => Float32x2,
3 => Float32x2,
4 => 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_shaded_round,
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, mut norm: Vec2, col: Rgba) {
let aa = rect.a;
let bb = rect.b;
if !(aa < bb) {
return;
}
if !(Vec2::splat(-1.0) <= norm) || !(norm <= Vec2::splat(1.0)) {
norm = Vec2::splat(0.0);
}
let adjust = Vec2(FRAC_PI_2 * norm.0, norm.1 - norm.0);
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 nbb = (bb - aa).sign();
let naa = -nbb;
let nab = Vec2(naa.0, nbb.1);
let nba = Vec2(nbb.0, naa.1);
let p = nbb / (bb - mid) * OFFSET;
let aa = Vertex::new2(aa, col, naa, adjust, p);
let ab = Vertex::new2(ab, col, nab, adjust, p);
let ba = Vertex::new2(ba, col, nba, adjust, p);
let bb = Vertex::new2(bb, col, nbb, adjust, p);
let mid = Vertex::new2(mid, col, n0, adjust, p);
#[rustfmt::skip]
self.add_vertices(pass.pass(), &[
aa, ba, mid,
mid, ba, bb,
bb, ab, mid,
mid, ab, aa,
]);
}
pub fn shaded_frame(
&mut self,
pass: PassId,
outer: Quad,
inner: Quad,
mut norm: Vec2,
col: Rgba,
) {
let aa = outer.a;
let bb = outer.b;
let mut cc = inner.a;
let mut dd = inner.b;
if !(aa < bb) {
return;
}
if !(aa <= cc) || !(cc <= bb) {
cc = aa;
}
if !(aa <= dd) || !(dd <= bb) {
dd = bb;
}
if !(cc <= dd) {
dd = cc;
}
if !(Vec2::splat(-1.0) <= norm) || !(norm <= Vec2::splat(1.0)) {
norm = Vec2::splat(0.0);
}
let adjust = Vec2(FRAC_PI_2 * norm.0, norm.1 - norm.0);
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 nbb = (bb - aa).sign();
let naa = -nbb;
let nab = Vec2(naa.0, nbb.1);
let nba = Vec2(nbb.0, naa.1);
let na0 = Vec2(naa.0, 0.0);
let nb0 = Vec2(nbb.0, 0.0);
let n0a = Vec2(0.0, naa.1);
let n0b = Vec2(0.0, nbb.1);
let paa = naa / (aa - cc) * OFFSET;
let pab = nab / (ab - cd) * OFFSET;
let pba = nba / (ba - dc) * OFFSET;
let pbb = nbb / (bb - dd) * OFFSET;
let ab = Vertex::new2(ab, col, nab, adjust, pab);
let ba = Vertex::new2(ba, col, nba, adjust, pba);
let cd = Vertex::new2(cd, col, n0, adjust, pab);
let dc = Vertex::new2(dc, col, n0, adjust, pba);
let ac = Vertex(Vec2(aa.0, cc.1), col, na0, adjust, paa);
let ad = Vertex(Vec2(aa.0, dd.1), col, na0, adjust, pab);
let bc = Vertex(Vec2(bb.0, cc.1), col, nb0, adjust, pba);
let bd = Vertex(Vec2(bb.0, dd.1), col, nb0, adjust, pbb);
let ca = Vertex(Vec2(cc.0, aa.1), col, n0a, adjust, paa);
let cb = Vertex(Vec2(cc.0, bb.1), col, n0b, adjust, pab);
let da = Vertex(Vec2(dd.0, aa.1), col, n0a, adjust, pba);
let db = Vertex(Vec2(dd.0, bb.1), col, n0b, adjust, pbb);
let aa = Vertex::new2(aa, col, naa, adjust, paa);
let bb = Vertex::new2(bb, col, nbb, adjust, pbb);
let cc = Vertex::new2(cc, col, n0, adjust, paa);
let dd = Vertex::new2(dd, col, n0, adjust, pbb);
#[rustfmt::skip]
self.add_vertices(pass.pass(), &[
ba, dc, da,
da, dc, ca,
dc, 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,
]);
}
}