mod cases;
#[rustfmt::skip]
mod glsl;
#[rustfmt::skip]
mod glsl_es;
#[rustfmt::skip]
mod msl;
pub mod programs;
mod terrain;
mod wgsl;
use mln_gfx::color_mode::ColorMode;
use mln_gfx::cull_face_mode::CullFaceMode;
use mln_gfx::depth_mode::DepthMode;
use mln_gfx::gfx_types::{AttributeDataType, DrawModeType};
use mln_gfx::gpu::{
BindGroupEntry, BindingsDesc, BindingsId, BufferDesc, BufferId, BufferUsage, ColorAttachment, DepthState,
DepthStencilAttachment, Draw, DynamicState, FixedState, GeometryDesc, GeometryId, Gpu, GpuError, LoadOp, PassDesc,
PipelineDesc, PipelineId, ResourceId, Scissor, ShaderId, ShaderKey, StencilState, TargetFormats, TextureDesc,
TextureFormat, TextureId, TextureUsage, VertexAttribute, VertexBufferLayout, VertexStep,
};
use mln_gfx::shaders::bindings::PROJECTION_ID;
use mln_gfx::shaders::layer_ubo::ProjectionUBO;
use mln_gfx::shaders::program::Program;
use mln_gfx::stencil_mode::{StencilMode, Test};
use mln_gfx::types::StencilOpType;
use mln_gfx::{Rgba, Size};
pub use self::cases::*;
pub use self::terrain::terrain_heights;
use crate::layers::background::programs::BACKGROUND;
use crate::layers::background::ubo::{BackgroundDrawableUnionUBO, BackgroundPropsUBO};
use crate::shaders::programs::CLIPPING_MASK;
pub const SIZE: Size = Size { width: 64, height: 32 };
const EXTENT: f32 = 8192.0;
fn tile_matrix(x_max: f32) -> [f32; 16] {
let sx = (x_max + 1.0) / EXTENT;
let sy = 2.0 / EXTENT;
[sx, 0.0, 0.0, 0.0, 0.0, sy, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0]
}
fn buffer(gpu: &mut (impl Gpu + ?Sized), usage: BufferUsage, data: &[u8]) -> Result<BufferId, GpuError> {
let buffer = gpu.create_buffer(&BufferDesc { label: "test", usage, size: data.len() as u64 })?;
gpu.write_buffer(buffer, 0, data)?;
Ok(buffer)
}
fn whole(id: usize, buffer: BufferId, data: &[u8]) -> BindGroupEntry {
BindGroupEntry::Buffer { id: id as u32, buffer, offset: 0, size: data.len() as u64 }
}
fn location(program: &Program, name: &str) -> u32 {
program.location(name) as u32
}
fn slot(program: &Program, name: &str) -> u32 {
program.texture_slot(name) as u32
}
fn position_layout(program: &Program) -> VertexBufferLayout {
let location = location(program, "pos");
VertexBufferLayout {
stride: 4,
step: VertexStep::Vertex,
attributes: vec![VertexAttribute { location, format: AttributeDataType::Short2, offset: 0 }],
}
}
fn dynamic() -> DynamicState {
DynamicState {
depth_range: (0.0, 1.0),
scissor: Scissor { x: 0, y: 0, width: SIZE.width, height: SIZE.height },
stencil_reference: 0,
blend_constant: Rgba::new(0.0, 0.0, 0.0, 0.0),
line_width: 1.0,
}
}
fn draw(pipeline: PipelineId, bindings: BindingsId, geometry: GeometryId, index_count: u32) -> Draw {
Draw {
pipeline,
bindings,
geometry,
first_index: 0,
index_count,
base_vertex: 0,
first_instance: 0,
instance_count: 1,
global_index: 0,
dynamic: dynamic(),
}
}
fn quad(gpu: &mut (impl Gpu + ?Sized)) -> Result<(BufferId, BufferId), GpuError> {
let e = EXTENT as i16;
let quad: [[i16; 2]; 4] = [[0, 0], [e, 0], [0, e], [e, e]];
let vertices = buffer(gpu, BufferUsage::Vertex, bytemuck::cast_slice(&quad))?;
let indices = buffer(gpu, BufferUsage::Index, bytemuck::cast_slice(&[0u16, 1, 2, 1, 2, 3]))?;
Ok((vertices, indices))
}
fn fixed(color: ColorMode, stencil: StencilState) -> FixedState {
FixedState {
primitive: DrawModeType::Triangles,
cull: CullFaceMode::disabled(),
blend: color.blend_function,
color_mask: color.mask,
depth: DepthState::from(&DepthMode::disabled()),
stencil,
}
}
pub fn clip_mask_and_background(gpu: &mut impl Gpu) -> Result<(), GpuError> {
let color = gpu.create_texture(&TextureDesc {
label: "color",
size: SIZE,
format: TextureFormat::Rgba8Unorm,
usage: TextureUsage { render_target: true, copy_src: true, ..TextureUsage::default() },
mipmapped: false,
})?;
let depth_stencil = gpu.create_texture(&TextureDesc {
label: "depth-stencil",
size: SIZE,
format: TextureFormat::Depth24PlusStencil8,
usage: TextureUsage { render_target: true, ..TextureUsage::default() },
mipmapped: false,
})?;
let frame = clipped_frame(gpu)?;
frame.draw(gpu, frame_pass(color, depth_stencil), true)?;
check_clipped(&read(gpu, color)?);
let unbound = gpu.create_bindings(&BindingsDesc {
label: "mask's",
shader: frame.background_shader,
entries: &frame.mask_entries,
});
assert!(matches!(unbound, Err(GpuError::Invalid(_))), "bindings without the shader's buffers");
Ok(())
}
fn frame_pass(color: TextureId, depth_stencil: TextureId) -> PassDesc<'static> {
PassDesc {
label: "frame",
color: Some(ColorAttachment { texture: color, load: LoadOp::Clear(Rgba::new(0.0, 0.0, 0.0, 1.0)) }),
depth_stencil: Some(DepthStencilAttachment {
texture: depth_stencil,
depth: LoadOp::Clear(1.0),
stencil: LoadOp::Clear(0),
}),
}
}
struct ClippedFrame {
mask: (PipelineId, BindingsId, GeometryId),
background: (PipelineId, BindingsId, GeometryId),
background_shader: ShaderId,
mask_entries: Vec<BindGroupEntry>,
}
impl ClippedFrame {
fn draw(&self, gpu: &mut impl Gpu, pass: PassDesc<'_>, mask: bool) -> Result<(), GpuError> {
gpu.begin_pass(&pass)?;
let with_reference = |(pipeline, bindings, geometry), global_index| Draw {
global_index,
dynamic: DynamicState { stencil_reference: 1, ..dynamic() },
..draw(pipeline, bindings, geometry, 6)
};
if mask {
gpu.draw(&with_reference(self.mask, 0))?;
}
gpu.draw(&with_reference(self.background, 1))?;
gpu.end_pass()
}
}
fn clipped_frame(gpu: &mut impl Gpu) -> Result<ClippedFrame, GpuError> {
let targets = TargetFormats {
color: Some(TextureFormat::Rgba8Unorm),
depth_stencil: Some(TextureFormat::Depth24PlusStencil8),
};
let (quad, indices) = quad(gpu)?;
let mask_shader = gpu.create_shader(&ShaderKey::new(&CLIPPING_MASK, []))?;
let mask_layouts = [position_layout(&CLIPPING_MASK)];
let mask_pipeline = gpu.create_pipeline(&PipelineDesc {
shader: mask_shader,
vertex_buffers: &mask_layouts,
state: fixed(
ColorMode::disabled(),
StencilState {
test: Test::Always,
write_mask: 0xFF,
fail: StencilOpType::Keep,
depth_fail: StencilOpType::Keep,
pass: StencilOpType::Replace,
},
),
targets,
})?;
let clip = ProjectionUBO { matrix: tile_matrix(0.0), ..ProjectionUBO::default() };
let clip_buffer = buffer(gpu, BufferUsage::Uniform, bytemuck::bytes_of(&clip))?;
let mask_entries = vec![whole(PROJECTION_ID, clip_buffer, bytemuck::bytes_of(&clip))];
let mask_bindings =
gpu.create_bindings(&BindingsDesc { label: "clip", shader: mask_shader, entries: &mask_entries })?;
let mask_geometry = gpu.create_geometry(&GeometryDesc {
shader: mask_shader,
layouts: &mask_layouts,
vertex_buffers: &[(quad, 0)],
index_buffer: indices,
})?;
let background_shader = gpu.create_shader(&ShaderKey::new(&BACKGROUND, []))?;
let background_layouts = [position_layout(&BACKGROUND)];
let background_pipeline = gpu.create_pipeline(&PipelineDesc {
shader: background_shader,
vertex_buffers: &background_layouts,
state: fixed(
ColorMode::unblended(),
StencilState::from(&StencilMode {
test: Test::Equal { mask: 0xFF },
ref_: 0,
mask: 0,
fail: StencilOpType::Keep,
depth_fail: StencilOpType::Keep,
pass: StencilOpType::Keep,
}),
),
targets,
})?;
let projections =
[ProjectionUBO::default(), ProjectionUBO { matrix: tile_matrix(1.0), ..ProjectionUBO::default() }];
let drawables = [BackgroundDrawableUnionUBO::default(); 2];
let props = BackgroundPropsUBO { color: [1.0, 0.0, 0.0, 1.0], opacity: 1.0, ..BackgroundPropsUBO::default() };
let projections = bytemuck::cast_slice(&projections);
let drawables = bytemuck::cast_slice(&drawables);
let props = bytemuck::bytes_of(&props);
let projection_buffer = buffer(gpu, BufferUsage::Storage, projections)?;
let drawable_buffer = buffer(gpu, BufferUsage::Storage, drawables)?;
let props_buffer = buffer(gpu, BufferUsage::Uniform, props)?;
let background_bindings = gpu.create_bindings(&BindingsDesc {
label: "background layer",
shader: background_shader,
entries: &[
whole(PROJECTION_ID, projection_buffer, projections),
whole(const { BACKGROUND.buffer_id("drawableVector") }, drawable_buffer, drawables),
whole(const { BACKGROUND.buffer_id("props") }, props_buffer, props),
],
})?;
let background_geometry = gpu.create_geometry(&GeometryDesc {
shader: background_shader,
layouts: &background_layouts,
vertex_buffers: &[(quad, 0)],
index_buffer: indices,
})?;
Ok(ClippedFrame {
mask: (mask_pipeline, mask_bindings, mask_geometry),
background: (background_pipeline, background_bindings, background_geometry),
background_shader,
mask_entries,
})
}
fn check_clipped(pixels: &[u8]) {
let pixel = |x, y| texel(pixels, x, y);
for y in [0, SIZE.height / 2, SIZE.height - 1] {
assert_eq!(pixel(0, y), [255, 0, 0, 255], "the mask's half has the background");
assert_eq!(pixel(SIZE.width / 2 - 1, y), [255, 0, 0, 255]);
assert_eq!(pixel(SIZE.width / 2, y), [0, 0, 0, 255], "the other half keeps the clear color");
assert_eq!(pixel(SIZE.width - 1, y), [0, 0, 0, 255]);
}
}
pub fn imported_textures<G: Gpu>(
gpu: &mut G,
mut import: impl FnMut(&mut G, TextureId) -> Result<TextureId, GpuError>,
) -> Result<(), GpuError> {
let color = gpu.create_texture(&TextureDesc {
label: "native color",
size: SIZE,
format: TextureFormat::Rgba8Unorm,
usage: TextureUsage { render_target: true, copy_src: true, ..TextureUsage::default() },
mipmapped: false,
})?;
let depth_stencil = gpu.create_texture(&TextureDesc {
label: "native depth-stencil",
size: SIZE,
format: TextureFormat::Depth24PlusStencil8,
usage: TextureUsage { render_target: true, ..TextureUsage::default() },
mipmapped: false,
})?;
let (imported_color, imported_depth_stencil) = (import(gpu, color)?, import(gpu, depth_stencil)?);
let frame = clipped_frame(gpu)?;
frame.draw(gpu, frame_pass(imported_color, imported_depth_stencil), true)?;
check_clipped(&read(gpu, imported_color)?);
gpu.destroy(ResourceId::Texture(imported_color))?;
gpu.destroy(ResourceId::Texture(imported_depth_stencil))?;
gpu.flush(true)?;
let gone = gpu.begin_pass(&frame_pass(imported_color, imported_depth_stencil));
assert!(matches!(gone, Err(GpuError::UnknownId("texture"))), "a pass into destroyed imports");
let (color, depth_stencil) = (import(gpu, color)?, import(gpu, depth_stencil)?);
check_clipped(&read(gpu, color)?);
let reloaded = PassDesc {
depth_stencil: Some(DepthStencilAttachment {
texture: depth_stencil,
depth: LoadOp::Load,
stencil: LoadOp::Load,
}),
..frame_pass(color, depth_stencil)
};
frame.draw(gpu, reloaded, false)?;
check_clipped(&read(gpu, color)?);
Ok(())
}