pub mod error;
use std::ops::Range;
pub use gpu_backend::Texture;
use ribir_geom::{DevicePoint, DeviceRect, DeviceSize};
use ribir_painter::{image::ColorFormat, Color, GradientStop, VertexBuffers};
mod gpu_backend;
use zerocopy::AsBytes;
#[cfg(feature = "wgpu")]
pub mod wgpu_impl;
pub use gpu_backend::*;
#[cfg(feature = "wgpu")]
pub use wgpu_impl::*;
pub trait GPUBackendImpl {
type Texture: Texture;
fn begin_frame(&mut self);
fn limits(&self) -> &DrawPhaseLimits;
fn new_texture(&mut self, size: DeviceSize, format: ColorFormat) -> Self::Texture;
fn load_alpha_vertices(&mut self, buffers: &VertexBuffers<()>);
fn draw_alpha_triangles(&mut self, indices: &Range<u32>, texture: &mut Self::Texture);
fn draw_alpha_triangles_with_scissor(
&mut self, indices: &Range<u32>, texture: &mut Self::Texture, scissor: DeviceRect,
);
fn load_textures(&mut self, textures: &[&Self::Texture]);
fn load_mask_layers(&mut self, layers: &[MaskLayer]);
fn load_color_vertices(&mut self, buffers: &VertexBuffers<ColorAttr>);
fn load_img_primitives(&mut self, primitives: &[ImgPrimitive]);
fn load_img_vertices(&mut self, buffers: &VertexBuffers<ImagePrimIndex>);
fn load_radial_gradient_primitives(&mut self, primitives: &[RadialGradientPrimitive]);
fn load_radial_gradient_stops(&mut self, stops: &[GradientStopPrimitive]);
fn load_radial_gradient_vertices(&mut self, buffers: &VertexBuffers<RadialGradientPrimIndex>);
fn load_linear_gradient_primitives(&mut self, primitives: &[LinearGradientPrimitive]);
fn load_linear_gradient_stops(&mut self, stops: &[GradientStopPrimitive]);
fn load_linear_gradient_vertices(&mut self, buffers: &VertexBuffers<LinearGradientPrimIndex>);
fn draw_color_triangles(
&mut self, texture: &mut Self::Texture, indices: Range<u32>, clear: Option<Color>,
);
fn draw_img_triangles(
&mut self, texture: &mut Self::Texture, indices: Range<u32>, clear: Option<Color>,
);
fn draw_radial_gradient_triangles(
&mut self, texture: &mut Self::Texture, indices: Range<u32>, clear: Option<Color>,
);
fn draw_linear_gradient_triangles(
&mut self, texture: &mut Self::Texture, indices: Range<u32>, clear: Option<Color>,
);
fn copy_texture_from_texture(
&mut self, dist_tex: &mut Self::Texture, copy_to: DevicePoint, from_tex: &Self::Texture,
from_rect: &DeviceRect,
);
fn end_frame(&mut self);
}
pub struct DrawPhaseLimits {
pub texture_size: DeviceSize,
pub max_tex_load: usize,
pub max_image_primitives: usize,
pub max_radial_gradient_primitives: usize,
pub max_linear_gradient_primitives: usize,
pub max_gradient_stop_primitives: usize,
pub max_mask_layers: usize,
}
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy)]
pub struct ColorAttr {
pub color: [u8; 4],
pub mask_head: i32,
}
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct ImagePrimIndex(u32);
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct RadialGradientPrimIndex(u32);
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct LinearGradientPrimIndex(u32);
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct GradientStopPrimitive {
pub color: u32,
pub offset: f32,
}
impl GradientStopPrimitive {
fn new(stop: &GradientStop) -> Self {
GradientStopPrimitive { color: stop.color.into_u32(), offset: stop.offset }
}
}
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct RadialGradientPrimitive {
pub transform: [f32; 6],
pub stop_start: u32,
pub stop_cnt: u32,
pub start_center: [f32; 2],
pub end_center: [f32; 2],
pub start_radius: f32,
pub end_radius: f32,
pub mask_head: i32,
pub spread: u32,
}
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy, Debug)]
pub struct LinearGradientPrimitive {
pub transform: [f32; 6],
pub start_position: [f32; 2],
pub end_position: [f32; 2],
pub stop: u32,
pub mask_head_and_spread: i32,
}
#[repr(packed)]
#[derive(AsBytes, PartialEq, Clone, Copy)]
pub struct ImgPrimitive {
pub transform: [f32; 6],
pub img_start: [f32; 2],
pub img_size: [f32; 2],
pub mask_head_and_tex_idx: i32,
pub opacity: f32,
}
#[derive(AsBytes, Clone)]
#[repr(packed)]
pub struct MaskLayer {
pub transform: [f32; 6],
pub min: [f32; 2],
pub max: [f32; 2],
pub mask_tex_idx: u32,
pub prev_mask_idx: i32,
}