408 lines
14 KiB
Rust
408 lines
14 KiB
Rust
#[cfg(feature = "image")]
|
|
mod raster;
|
|
#[cfg(feature = "svg")]
|
|
mod vector;
|
|
|
|
use crate::Transformation;
|
|
use iced_native::{image, svg, Rectangle};
|
|
|
|
use std::mem;
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
use std::cell::RefCell;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Pipeline {
|
|
#[cfg(feature = "image")]
|
|
raster_cache: RefCell<raster::Cache>,
|
|
#[cfg(feature = "svg")]
|
|
vector_cache: RefCell<vector::Cache>,
|
|
|
|
pipeline: wgpu::RenderPipeline,
|
|
uniforms: wgpu::Buffer,
|
|
vertices: wgpu::Buffer,
|
|
indices: wgpu::Buffer,
|
|
instances: wgpu::Buffer,
|
|
constants: wgpu::BindGroup,
|
|
texture_layout: wgpu::BindGroupLayout,
|
|
}
|
|
|
|
impl Pipeline {
|
|
pub fn new(device: &wgpu::Device) -> Self {
|
|
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
mag_filter: wgpu::FilterMode::Linear,
|
|
min_filter: wgpu::FilterMode::Linear,
|
|
mipmap_filter: wgpu::FilterMode::Linear,
|
|
lod_min_clamp: -100.0,
|
|
lod_max_clamp: 100.0,
|
|
compare_function: wgpu::CompareFunction::Always,
|
|
});
|
|
|
|
let constant_layout =
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
bindings: &[
|
|
wgpu::BindGroupLayoutBinding {
|
|
binding: 0,
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
|
},
|
|
wgpu::BindGroupLayoutBinding {
|
|
binding: 1,
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
ty: wgpu::BindingType::Sampler,
|
|
},
|
|
],
|
|
});
|
|
|
|
let uniforms = Uniforms {
|
|
transform: Transformation::identity().into(),
|
|
};
|
|
|
|
let uniforms_buffer = device
|
|
.create_buffer_mapped(
|
|
1,
|
|
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
)
|
|
.fill_from_slice(&[uniforms]);
|
|
|
|
let constant_bind_group =
|
|
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
layout: &constant_layout,
|
|
bindings: &[
|
|
wgpu::Binding {
|
|
binding: 0,
|
|
resource: wgpu::BindingResource::Buffer {
|
|
buffer: &uniforms_buffer,
|
|
range: 0..std::mem::size_of::<Uniforms>() as u64,
|
|
},
|
|
},
|
|
wgpu::Binding {
|
|
binding: 1,
|
|
resource: wgpu::BindingResource::Sampler(&sampler),
|
|
},
|
|
],
|
|
});
|
|
|
|
let texture_layout =
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
bindings: &[wgpu::BindGroupLayoutBinding {
|
|
binding: 0,
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
ty: wgpu::BindingType::SampledTexture {
|
|
multisampled: false,
|
|
dimension: wgpu::TextureViewDimension::D2,
|
|
},
|
|
}],
|
|
});
|
|
|
|
let layout =
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
bind_group_layouts: &[&constant_layout, &texture_layout],
|
|
});
|
|
|
|
let vs = include_bytes!("shader/image.vert.spv");
|
|
let vs_module = device.create_shader_module(
|
|
&wgpu::read_spirv(std::io::Cursor::new(&vs[..]))
|
|
.expect("Read image vertex shader as SPIR-V"),
|
|
);
|
|
|
|
let fs = include_bytes!("shader/image.frag.spv");
|
|
let fs_module = device.create_shader_module(
|
|
&wgpu::read_spirv(std::io::Cursor::new(&fs[..]))
|
|
.expect("Read image fragment shader as SPIR-V"),
|
|
);
|
|
|
|
let pipeline =
|
|
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
layout: &layout,
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
module: &vs_module,
|
|
entry_point: "main",
|
|
},
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
module: &fs_module,
|
|
entry_point: "main",
|
|
}),
|
|
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
|
front_face: wgpu::FrontFace::Cw,
|
|
cull_mode: wgpu::CullMode::None,
|
|
depth_bias: 0,
|
|
depth_bias_slope_scale: 0.0,
|
|
depth_bias_clamp: 0.0,
|
|
}),
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
color_states: &[wgpu::ColorStateDescriptor {
|
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
color_blend: wgpu::BlendDescriptor {
|
|
src_factor: wgpu::BlendFactor::SrcAlpha,
|
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
operation: wgpu::BlendOperation::Add,
|
|
},
|
|
alpha_blend: wgpu::BlendDescriptor {
|
|
src_factor: wgpu::BlendFactor::One,
|
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
operation: wgpu::BlendOperation::Add,
|
|
},
|
|
write_mask: wgpu::ColorWrite::ALL,
|
|
}],
|
|
depth_stencil_state: None,
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
vertex_buffers: &[
|
|
wgpu::VertexBufferDescriptor {
|
|
stride: mem::size_of::<Vertex>() as u64,
|
|
step_mode: wgpu::InputStepMode::Vertex,
|
|
attributes: &[wgpu::VertexAttributeDescriptor {
|
|
shader_location: 0,
|
|
format: wgpu::VertexFormat::Float2,
|
|
offset: 0,
|
|
}],
|
|
},
|
|
wgpu::VertexBufferDescriptor {
|
|
stride: mem::size_of::<Instance>() as u64,
|
|
step_mode: wgpu::InputStepMode::Instance,
|
|
attributes: &[
|
|
wgpu::VertexAttributeDescriptor {
|
|
shader_location: 1,
|
|
format: wgpu::VertexFormat::Float2,
|
|
offset: 0,
|
|
},
|
|
wgpu::VertexAttributeDescriptor {
|
|
shader_location: 2,
|
|
format: wgpu::VertexFormat::Float2,
|
|
offset: 4 * 2,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
sample_count: 1,
|
|
sample_mask: !0,
|
|
alpha_to_coverage_enabled: false,
|
|
});
|
|
|
|
let vertices = device
|
|
.create_buffer_mapped(QUAD_VERTS.len(), wgpu::BufferUsage::VERTEX)
|
|
.fill_from_slice(&QUAD_VERTS);
|
|
|
|
let indices = device
|
|
.create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX)
|
|
.fill_from_slice(&QUAD_INDICES);
|
|
|
|
let instances = device.create_buffer(&wgpu::BufferDescriptor {
|
|
size: mem::size_of::<Instance>() as u64,
|
|
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
|
});
|
|
|
|
Pipeline {
|
|
#[cfg(feature = "image")]
|
|
raster_cache: RefCell::new(raster::Cache::new()),
|
|
#[cfg(feature = "svg")]
|
|
vector_cache: RefCell::new(vector::Cache::new()),
|
|
|
|
pipeline,
|
|
uniforms: uniforms_buffer,
|
|
vertices,
|
|
indices,
|
|
instances,
|
|
constants: constant_bind_group,
|
|
texture_layout,
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "image")]
|
|
pub fn dimensions(&self, handle: &image::Handle) -> (u32, u32) {
|
|
let mut cache = self.raster_cache.borrow_mut();
|
|
let memory = cache.load(&handle);
|
|
|
|
memory.dimensions()
|
|
}
|
|
|
|
#[cfg(feature = "svg")]
|
|
pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) {
|
|
let mut cache = self.vector_cache.borrow_mut();
|
|
let svg = cache.load(&handle);
|
|
|
|
svg.viewport_dimensions()
|
|
}
|
|
|
|
pub fn draw(
|
|
&mut self,
|
|
device: &mut wgpu::Device,
|
|
encoder: &mut wgpu::CommandEncoder,
|
|
instances: &[Image],
|
|
transformation: Transformation,
|
|
bounds: Rectangle<u32>,
|
|
target: &wgpu::TextureView,
|
|
_scale: f32,
|
|
) {
|
|
let uniforms_buffer = device
|
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
|
.fill_from_slice(&[Uniforms {
|
|
transform: transformation.into(),
|
|
}]);
|
|
|
|
encoder.copy_buffer_to_buffer(
|
|
&uniforms_buffer,
|
|
0,
|
|
&self.uniforms,
|
|
0,
|
|
std::mem::size_of::<Uniforms>() as u64,
|
|
);
|
|
|
|
// TODO: Batch draw calls using a texture atlas
|
|
// Guillotière[1] by @nical can help us a lot here.
|
|
//
|
|
// [1]: https://github.com/nical/guillotiere
|
|
for image in instances {
|
|
let uploaded_texture = match &image.handle {
|
|
Handle::Raster(_handle) => {
|
|
#[cfg(feature = "image")]
|
|
{
|
|
let mut cache = self.raster_cache.borrow_mut();
|
|
let memory = cache.load(&_handle);
|
|
|
|
memory.upload(device, encoder, &self.texture_layout)
|
|
}
|
|
|
|
#[cfg(not(feature = "image"))]
|
|
None
|
|
}
|
|
Handle::Vector(_handle) => {
|
|
#[cfg(feature = "svg")]
|
|
{
|
|
let mut cache = self.vector_cache.borrow_mut();
|
|
|
|
cache.upload(
|
|
_handle,
|
|
image.scale,
|
|
_scale,
|
|
device,
|
|
encoder,
|
|
&self.texture_layout,
|
|
)
|
|
}
|
|
|
|
#[cfg(not(feature = "svg"))]
|
|
None
|
|
}
|
|
};
|
|
|
|
if let Some(texture) = uploaded_texture {
|
|
let instance_buffer = device
|
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
|
.fill_from_slice(&[Instance {
|
|
_position: image.position,
|
|
_scale: image.scale,
|
|
}]);
|
|
|
|
encoder.copy_buffer_to_buffer(
|
|
&instance_buffer,
|
|
0,
|
|
&self.instances,
|
|
0,
|
|
mem::size_of::<Instance>() as u64,
|
|
);
|
|
|
|
{
|
|
let mut render_pass = encoder.begin_render_pass(
|
|
&wgpu::RenderPassDescriptor {
|
|
color_attachments: &[
|
|
wgpu::RenderPassColorAttachmentDescriptor {
|
|
attachment: target,
|
|
resolve_target: None,
|
|
load_op: wgpu::LoadOp::Load,
|
|
store_op: wgpu::StoreOp::Store,
|
|
clear_color: wgpu::Color {
|
|
r: 0.0,
|
|
g: 0.0,
|
|
b: 0.0,
|
|
a: 0.0,
|
|
},
|
|
},
|
|
],
|
|
depth_stencil_attachment: None,
|
|
},
|
|
);
|
|
|
|
render_pass.set_pipeline(&self.pipeline);
|
|
render_pass.set_bind_group(0, &self.constants, &[]);
|
|
render_pass.set_bind_group(1, &texture, &[]);
|
|
render_pass.set_index_buffer(&self.indices, 0);
|
|
render_pass.set_vertex_buffers(
|
|
0,
|
|
&[(&self.vertices, 0), (&self.instances, 0)],
|
|
);
|
|
render_pass.set_scissor_rect(
|
|
bounds.x,
|
|
bounds.y,
|
|
bounds.width,
|
|
bounds.height,
|
|
);
|
|
|
|
render_pass.draw_indexed(
|
|
0..QUAD_INDICES.len() as u32,
|
|
0,
|
|
0..1 as u32,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn trim_cache(&mut self) {
|
|
#[cfg(feature = "image")]
|
|
self.raster_cache.borrow_mut().trim();
|
|
|
|
#[cfg(feature = "svg")]
|
|
self.vector_cache.borrow_mut().trim();
|
|
}
|
|
}
|
|
|
|
pub struct Image {
|
|
pub handle: Handle,
|
|
pub position: [f32; 2],
|
|
pub scale: [f32; 2],
|
|
}
|
|
|
|
pub enum Handle {
|
|
Raster(image::Handle),
|
|
Vector(svg::Handle),
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
pub struct Vertex {
|
|
_position: [f32; 2],
|
|
}
|
|
|
|
const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3];
|
|
|
|
const QUAD_VERTS: [Vertex; 4] = [
|
|
Vertex {
|
|
_position: [0.0, 0.0],
|
|
},
|
|
Vertex {
|
|
_position: [1.0, 0.0],
|
|
},
|
|
Vertex {
|
|
_position: [1.0, 1.0],
|
|
},
|
|
Vertex {
|
|
_position: [0.0, 1.0],
|
|
},
|
|
];
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy)]
|
|
struct Instance {
|
|
_position: [f32; 2],
|
|
_scale: [f32; 2],
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug, Clone, Copy)]
|
|
struct Uniforms {
|
|
transform: [f32; 16],
|
|
}
|