dymokomi/luce-ui
Source of release 0.19.1
0.19.1 / src / texture_cache.lucb
## The GPU side of a CPU picture (an Image, a Raster): its texture is made on the
## device of the first target it is drawn on and re-uploaded only when the
## picture's pixels changed since the last upload.
from luce_gpu import gpu
## A picture's texture and the device holding it, and the revision last uploaded.
## The picture bumps its own revision on every pixel change.
pub struct TextureCache:
var texture: gpu.Texture?
var device: gpu.Device?
var uploaded: u64
pub func init():
self.texture = none
self.device = none
self.uploaded = 0
## The texture for a `columns` by `rows` picture in `pixel_format` on `target`'s
## device, made on first use; none when a standalone frame has no device.
pub func texture_for(target: const gpu.RenderTarget*, columns: u32, rows: u32, pixel_format: gpu.Format) -> gpu.Texture?!:
let device = self.device else gpu.device_of(target) catch failure:
if failure.code == gpu.unavailable:
return none
error(failure.code, failure.message)
self.device = device
let texture = self.texture else try gpu.Texture.create(device, columns, rows, pixel_format)
if self.texture == none:
self.texture = texture
self.uploaded = 0
return texture
## Whether a texture was made: never for a picture only drawn on standalone frames.
pub func has_texture() -> bool:
return self.texture != none
## Whether the texture lags the picture's `revision`.
pub func stale(revision: u64) -> bool:
return self.uploaded != revision
## Record that the texture now holds `revision`.
pub func mark_uploaded(revision: u64):
self.uploaded = revision
## Destroy the texture and release the device; the next draw makes them again.
pub func release():
if let texture = self.texture:
var owned = texture
owned.destroy()
self.texture = none
if let device = self.device:
var owned = device
owned.destroy()
self.device = none