1// Pictures packed into pages the canvas keeps, in shelves: rows of2// pictures as tall as their first, the next starting below the tallest.3// Each picture sits in a border of one pixel, its edge repeated for an4// image so linear filtering does not bleed in its neighbours, clear for5// a mask. A picture released leaves its area to the next that fits in6// it, and a page holding none is empty again.78import std::vector;9import std::option;10import std::math;11import pkg::alloy::gpu;1213import draw;14import frame;1516pub const PAGE: u32 = 1024; // a page's width and height17pub const LARGEST: u32 = 256; // a picture packed, larger ones getting a texture1819pub type Shelf = struct {20 pub y: u32,21 pub height: u32,22 pub x: u32, // where its free space starts23};2425// Areas packed in shelves, within a width and a height: a page's, or26// the glyph cache's plots'.27pub type Shelves = struct {28 pub rows: Vector<Shelf>,29 pub top: u32, // below the last shelf30};3132pub fn Shelves::empty() -> Shelves => Shelves { .rows = Vector<Shelf>::empty(), .top = 0 };3334pub fn is_empty(self shelves: &Shelves) -> bool => shelves.top == 0;3536pub fn clear(self shelves: &var Shelves) {37 shelves.rows.clear();38 shelves.top = 0;39}4041// Whether an area of the size has room, inside `room_width` by42// `room_height`.43pub fn fits(44 self shelves: &Shelves,45 width: u32,46 height: u32,47 room_width: u32,48 room_height: u3249) -> bool {50 for (shelves.rows.view()) |&s| {51 if (height <= s.height && s.x + width <= room_width) {52 return true;53 }54 }55 return shelves.top + height <= room_height && width <= room_width;56}5758// A new area of the size, inside `room_width` by `room_height`, if one59// has room.60pub fn place(61 self shelves: &var Shelves,62 width: u32,63 height: u32,64 room_width: u32,65 room_height: u3266) -> Option<PixelRect> {67 for (shelves.rows.items()) |&var s| {68 if (height <= s.height && s.x + width <= room_width) {69 const at = PixelRect { .x = s.x, .y = s.y, .width = width, .height = height };70 s.x += width;71 return ::Some(at);72 }73 }74 if (shelves.top + height > room_height || width > room_width) {75 return ::None;76 }77 shelves.rows.push(Shelf { .y = shelves.top, .height = height, .x = width });78 const at = PixelRect { .x = 0, .y = shelves.top, .width = width, .height = height };79 shelves.top += height;80 return ::Some(at);81}8283// A rectangle of a page, in whole pixels.84pub type PixelRect = struct {85 pub x: u32,86 pub y: u32,87 pub width: u32,88 pub height: u32,89};9091pub type Page = struct {92 pub texture: Texture, // of images, or of masks (R8Unorm)93 pub shelves: Shelves,94 pub held: u32, // how many pictures it holds95 pub freed: Vector<PixelRect>, // areas released pictures left96};9798// An empty page, `side` pixels wide and high, of the format.99pub fn Page::create(device: &var Device, side: u32, format: TextureFormat) -> Page => Page {100 .texture = device.texture(&TextureDescriptor {101 .width = side,102 .height = side,103 .format = format,104 .usage = TEXTURE_SAMPLED | TEXTURE_COPY_DESTINATION105 }),106 .shelves = Shelves::empty(),107 .held = 0,108 .freed = Vector<PixelRect>::empty()109};110111// Where an area went: its page among the pages, and its place there.112pub type Spot = struct {113 pub page: u32,114 pub area: PixelRect,115};116117// Where an area of the size goes among the pages of the format, a new118// page added when none has room.119pub fn place_in(120 pages: &var Vector<Page>,121 device: &var Device,122 side: u32,123 format: TextureFormat,124 width: u32,125 height: u32126) -> Option<Spot> {127 for (pages.items(), [..pages.length()]) |&var page, k| {128 if (page.texture.format == format && page.place(width, height) is ::Some |area|) {129 return ::Some(Spot { .page = k to u32, .area = area });130 }131 }132 var page = Page::create(&var device, side, format);133 const placed = page.place(width, height);134 pages.push(move page);135 if (placed is ::Some |area|) {136 return ::Some(Spot { .page = (pages.length() - 1) to u32, .area = area });137 }138 return ::None;139}140141// A free area of the size, if the page has one.142pub fn place(self page: &var Page, width: u32, height: u32) -> Option<PixelRect> {143 for ([..page.freed.length()]) |k| {144 const area = page.freed.get(k);145 if (width <= area.width && height <= area.height) {146 page.freed.swap_remove(k);147 if (area.width > width) {148 // what is left on its right stays free149 page.freed.push(PixelRect {150 .x = area.x + width,151 .y = area.y,152 .width = area.width - width,153 .height = area.height154 });155 }156 page.held += 1;157 return ::Some(PixelRect { .x = area.x, .y = area.y, .width = width, .height = height });158 }159 }160 const placed = page.place_new(width, height);161 if (placed is ::Some) {162 page.held += 1;163 }164 return placed;165}166167// Gives back the area of a picture packed in the page, borders included.168pub fn free_area(self page: &var Page, area: PixelRect) {169 page.held -= 1;170 if (page.held == 0) {171 page.clear();172 return;173 }174 page.freed.push(area);175}176177// Empties the page.178pub fn clear(self page: &var Page) {179 page.shelves.clear();180 page.freed.clear();181 page.held = 0;182}183184fn place_new(self page: &var Page, width: u32, height: u32) -> Option<PixelRect> {185 return page.shelves.place(width, height, page.texture.width, page.texture.height);186}187188// Writes the picture's pixels into its spot's area, a pixel larger on189// every side: the border repeats the picture's edge with `edges` (an190// image's), and is clear without (a mask's or a glyph's).191pub fn write_bordered(192 device: &var Device,193 texture: Texture,194 area: &PixelRect,195 pixels: &[u8],196 edges: bool197) {198 const pixel = texture.format.bytes_per_pixel();199 const w = area.width to u64;200 const h = area.height to u64;201 const height = h - 2;202 const row = (w - 2) * pixel;203 const line = w * pixel;204 var bordered = Vector<u8>::filled(0, h * line);205 const out = &var bordered.items();206 for ([..height]) |y| {207 const at = (y + 1) * line + pixel;208 out[at..at + row] = pixels[y * row..(y + 1) * row];209 if (edges) {210 for ([..pixel]) |c| {211 out[at - pixel + c] = pixels[y * row + c];212 out[at + row + c] = pixels[(y + 1) * row - pixel + c];213 }214 }215 }216 if (edges) {217 const top = Vector<u8>::from(&out[line..2 * line]);218 out[..line] = top.view();219 const bottom = Vector<u8>::from(&out[(h - 2) * line..(h - 1) * line]);220 out[(h - 1) * line..] = bottom.view();221 }222 device.write_texture_region(223 texture,224 area.x,225 area.y,226 area.width,227 area.height,228 &bordered.view()229 );230}231232pub fn pack(233 self g: &var Resources,234 device: &var Device,235 width: u32,236 height: u32,237 bytes: &[u8],238 format: TextureFormat239) -> Picture {240 if (bytes.length() < width to u64 * height to u64 * format.bytes_per_pixel()) {241 panic "pkg::alloy::draw: a picture given fewer bytes than its pixels";242 }243 if (width > LARGEST || height > LARGEST) {244 const texture = device.texture(&TextureDescriptor {245 .width = width,246 .height = height,247 .format = format,248 .usage = TEXTURE_SAMPLED | TEXTURE_COPY_DESTINATION249 });250 device.write_texture(texture, &bytes);251 g.owned.push(texture);252 return Picture::whole(texture);253 }254 const found = place_in(&var g.pages, &var device, PAGE, format, width + 2, height + 2);255 const spot = if (found is ::Some |s|) s else {256 panic "pkg::alloy::draw: no room for a picture";257 };258 const texture = g.pages.get(spot.page to u64).texture;259 write_bordered(&var device, texture, &spot.area, &bytes, !(format is ::R8Unorm));260 return Picture {261 .texture = texture,262 .region = Rect::at(263 (spot.area.x + 1) to f32,264 (spot.area.y + 1) to f32,265 width to f32,266 height to f32267 )268 };269}270271// Gives back what a picture packed by `pack` holds: its own texture, or272// its area of a page, released at the next frame once the page holds273// none; or for a texture of the program's, its bind groups.274pub fn unpack(self g: &var Resources, device: &var Device, picture: Picture) {275 for ([..g.owned.length()]) |k| {276 if (g.owned.get(k) == picture.texture) {277 g.owned.swap_remove(k);278 g.forget(&var device, picture.texture);279 device.release(picture.texture);280 return;281 }282 }283 for (g.pages.items()) |&var page| {284 if (page.texture == picture.texture) {285 const r = picture.region;286 page.free_area(PixelRect {287 .x = r.x to u32 - 1,288 .y = r.y to u32 - 1,289 .width = r.width to u32 + 2,290 .height = r.height to u32 + 2291 });292 return;293 }294 }295 // a texture of the program's (`Picture::whole`), which keeps it296 g.forget(&var device, picture.texture);297}