|
| 1 | +use crate::program::{self, Shader}; |
| 2 | +use crate::Transformation; |
| 3 | +use glow::HasContext; |
| 4 | +use iced_graphics::layer; |
| 5 | +#[cfg(feature = "image_rs")] |
| 6 | +use std::cell::RefCell; |
| 7 | + |
| 8 | +pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; |
| 9 | + |
| 10 | +#[cfg(feature = "image_rs")] |
| 11 | +use iced_graphics::image::raster; |
| 12 | + |
| 13 | +#[cfg(feature = "svg")] |
| 14 | +use iced_graphics::image::vector; |
| 15 | + |
| 16 | +mod textures; |
| 17 | +use textures::{Entry, Textures}; |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +pub(crate) struct Pipeline { |
| 21 | + program: <glow::Context as HasContext>::Program, |
| 22 | + vertex_array: <glow::Context as HasContext>::VertexArray, |
| 23 | + vertex_buffer: <glow::Context as HasContext>::Buffer, |
| 24 | + transform_location: <glow::Context as HasContext>::UniformLocation, |
| 25 | + textures: Textures, |
| 26 | + #[cfg(feature = "image_rs")] |
| 27 | + raster_cache: RefCell<raster::Cache<Textures>>, |
| 28 | + #[cfg(feature = "svg")] |
| 29 | + vector_cache: vector::Cache<Textures>, |
| 30 | +} |
| 31 | + |
| 32 | +impl Pipeline { |
| 33 | + pub fn new( |
| 34 | + gl: &glow::Context, |
| 35 | + shader_version: &program::Version, |
| 36 | + ) -> Pipeline { |
| 37 | + let program = unsafe { |
| 38 | + let vertex_shader = Shader::vertex( |
| 39 | + gl, |
| 40 | + shader_version, |
| 41 | + include_str!("shader/common/image.vert"), |
| 42 | + ); |
| 43 | + let fragment_shader = Shader::fragment( |
| 44 | + gl, |
| 45 | + shader_version, |
| 46 | + include_str!("shader/common/image.frag"), |
| 47 | + ); |
| 48 | + |
| 49 | + program::create( |
| 50 | + gl, |
| 51 | + &[vertex_shader, fragment_shader], |
| 52 | + &[(0, "i_Position")], |
| 53 | + ) |
| 54 | + }; |
| 55 | + |
| 56 | + let transform_location = |
| 57 | + unsafe { gl.get_uniform_location(program, "u_Transform") } |
| 58 | + .expect("Get transform location"); |
| 59 | + |
| 60 | + unsafe { |
| 61 | + gl.use_program(Some(program)); |
| 62 | + |
| 63 | + let transform: [f32; 16] = Transformation::identity().into(); |
| 64 | + gl.uniform_matrix_4_f32_slice( |
| 65 | + Some(&transform_location), |
| 66 | + false, |
| 67 | + &transform, |
| 68 | + ); |
| 69 | + |
| 70 | + gl.use_program(None); |
| 71 | + } |
| 72 | + |
| 73 | + let vertex_buffer = |
| 74 | + unsafe { gl.create_buffer().expect("Create vertex buffer") }; |
| 75 | + let vertex_array = |
| 76 | + unsafe { gl.create_vertex_array().expect("Create vertex array") }; |
| 77 | + |
| 78 | + unsafe { |
| 79 | + gl.bind_vertex_array(Some(vertex_array)); |
| 80 | + gl.bind_buffer(glow::ARRAY_BUFFER, Some(vertex_buffer)); |
| 81 | + |
| 82 | + let vertices = &[0u8, 0, 1, 0, 0, 1, 1, 1]; |
| 83 | + gl.buffer_data_size( |
| 84 | + glow::ARRAY_BUFFER, |
| 85 | + vertices.len() as i32, |
| 86 | + glow::STATIC_DRAW, |
| 87 | + ); |
| 88 | + gl.buffer_sub_data_u8_slice( |
| 89 | + glow::ARRAY_BUFFER, |
| 90 | + 0, |
| 91 | + bytemuck::cast_slice(vertices), |
| 92 | + ); |
| 93 | + |
| 94 | + gl.enable_vertex_attrib_array(0); |
| 95 | + gl.vertex_attrib_pointer_f32( |
| 96 | + 0, |
| 97 | + 2, |
| 98 | + glow::UNSIGNED_BYTE, |
| 99 | + false, |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + ); |
| 103 | + |
| 104 | + gl.bind_buffer(glow::ARRAY_BUFFER, None); |
| 105 | + gl.bind_vertex_array(None); |
| 106 | + } |
| 107 | + |
| 108 | + Pipeline { |
| 109 | + program, |
| 110 | + vertex_array, |
| 111 | + vertex_buffer, |
| 112 | + transform_location, |
| 113 | + textures: Textures::new(), |
| 114 | + #[cfg(feature = "image_rs")] |
| 115 | + raster_cache: RefCell::new(raster::Cache::default()), |
| 116 | + #[cfg(feature = "svg")] |
| 117 | + vector_cache: vector::Cache::default(), |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + #[cfg(feature = "image_rs")] |
| 122 | + pub fn dimensions( |
| 123 | + &self, |
| 124 | + handle: &iced_native::image::Handle, |
| 125 | + ) -> (u32, u32) { |
| 126 | + self.raster_cache.borrow_mut().load(handle).dimensions() |
| 127 | + } |
| 128 | + |
| 129 | + pub fn draw( |
| 130 | + &mut self, |
| 131 | + mut gl: &glow::Context, |
| 132 | + transformation: Transformation, |
| 133 | + _scale_factor: f32, |
| 134 | + images: &[layer::Image], |
| 135 | + ) { |
| 136 | + unsafe { |
| 137 | + gl.use_program(Some(self.program)); |
| 138 | + gl.bind_vertex_array(Some(self.vertex_array)); |
| 139 | + gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer)); |
| 140 | + } |
| 141 | + |
| 142 | + #[cfg(feature = "image_rs")] |
| 143 | + let mut raster_cache = self.raster_cache.borrow_mut(); |
| 144 | + for image in images { |
| 145 | + let (entry, bounds) = match &image { |
| 146 | + #[cfg(feature = "image_rs")] |
| 147 | + layer::Image::Raster { handle, bounds } => ( |
| 148 | + raster_cache.upload(handle, &mut gl, &mut self.textures), |
| 149 | + bounds, |
| 150 | + ), |
| 151 | + #[cfg(not(feature = "image_rs"))] |
| 152 | + layer::Image::Raster { handle: _, bounds } => (None, bounds), |
| 153 | + |
| 154 | + #[cfg(feature = "svg")] |
| 155 | + layer::Image::Vector { handle, bounds } => { |
| 156 | + let size = [bounds.width, bounds.height]; |
| 157 | + ( |
| 158 | + self.vector_cache.upload( |
| 159 | + handle, |
| 160 | + size, |
| 161 | + _scale_factor, |
| 162 | + &mut gl, |
| 163 | + &mut self.textures, |
| 164 | + ), |
| 165 | + bounds, |
| 166 | + ) |
| 167 | + } |
| 168 | + |
| 169 | + #[cfg(not(feature = "svg"))] |
| 170 | + layer::Image::Vector { handle: _, bounds } => (None, bounds), |
| 171 | + }; |
| 172 | + |
| 173 | + unsafe { |
| 174 | + if let Some(Entry { texture, .. }) = entry { |
| 175 | + gl.bind_texture(glow::TEXTURE_2D, Some(*texture)) |
| 176 | + } else { |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + let translate = Transformation::translate(bounds.x, bounds.y); |
| 181 | + let scale = Transformation::scale(bounds.width, bounds.height); |
| 182 | + let transformation = transformation * translate * scale; |
| 183 | + let matrix: [f32; 16] = transformation.into(); |
| 184 | + gl.uniform_matrix_4_f32_slice( |
| 185 | + Some(&self.transform_location), |
| 186 | + false, |
| 187 | + &matrix, |
| 188 | + ); |
| 189 | + |
| 190 | + gl.draw_arrays(glow::TRIANGLE_STRIP, 0, 4); |
| 191 | + |
| 192 | + gl.bind_texture(glow::TEXTURE_2D, None); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + unsafe { |
| 197 | + gl.bind_buffer(glow::ARRAY_BUFFER, None); |
| 198 | + gl.bind_vertex_array(None); |
| 199 | + gl.use_program(None); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + pub fn trim_cache(&mut self, mut gl: &glow::Context) { |
| 204 | + #[cfg(feature = "image_rs")] |
| 205 | + self.raster_cache |
| 206 | + .borrow_mut() |
| 207 | + .trim(&mut self.textures, &mut gl); |
| 208 | + |
| 209 | + #[cfg(feature = "svg")] |
| 210 | + self.vector_cache.trim(&mut self.textures, &mut gl); |
| 211 | + } |
| 212 | +} |
0 commit comments