vanilladocs
vanillapunch.com
API / core/imagetexturecache

core/imagetexturecache

이미지 텍스처 캐시.

  • 원본은 WeakMap<image, texture> 로 원본 이미지가 수거되면 텍스처도 같이 사라진다.
  • 여기서는 Weak<Image> 를 들고, sweep() 이 원본이 사라진 텍스처를 지운다.
  • 올린 뒤에는 이미지의 CPU 픽셀을 놓는다. (Image.set_pixels_retained 로 남기게 한 그림은 둔다)
  • 놓은 픽셀이 다시 필요하면(Canvas2D 의 drawImage, createPattern) 그림이 기억한 텍스처를 GPU 에서 되읽는다. (restore_image_pixels_from_texture) 크롬이 GPU 에 있는 그림을 CPU 캔버스에 그릴 때 텍스처를 되읽는 것과 같은 길이다. 되읽을 GL 컨텍스트는 Graphic 이 만들 때 건다.

Image texture cache.

  • The original uses WeakMap<image, texture>, so the texture disappears along with the source image when it is collected.
  • Here it holds Weak<Image>, and sweep() removes textures whose source has disappeared.
  • After uploading, the image's CPU pixels are released. (images set to be kept with Image.set_pixels_retained are left alone)
  • When released pixels are needed again (Canvas2D drawImage, createPattern), the texture remembered by the image is read back from the GPU. (restore_image_pixels_from_texture) This is the same path Chrome takes when it reads back a texture to draw a GPU-resident image onto a CPU canvas. The GL context for reading back is set by Graphic when it is created.

ImageTextureCachestruct traits Default

소스source

이미지 텍스처 캐시. (이미지마다 GPU 텍스처 하나, Graphic 이 하나 들고 처음 그릴 때 올린다)

  • 이미지를 Weak 로 들어, 이미지가 사라지면 sweep 이 텍스처를 지운다. (Graphic 은 600 프레임마다 부른다)

Image texture cache. (one GPU texture per image, Graphic holds one and uploads on first draw)

  • Holds images as Weak, so sweep deletes the texture when the image is gone. (Graphic calls it every 600 frames)
예제Example빈 캐시Empty cache
use vanilla::core::imagetexturecache::ImageTextureCache;

let image_texture_cache = ImageTextureCache::new();
assert_eq!(image_texture_cache.get_texture_count(), 0);
예제Example텍스처를 올리고 이미지가 사라진 뒤 치우기Uploading a texture and cleaning up after the image is gone
use std::path::Path;
use vanilla::{Graphic, Image};
use vanilla::core::imagetexturecache::ImageTextureCache;

fn upload(graphic: &Graphic, image_texture_cache: &mut ImageTextureCache) -> Result<(), String> {
	let image = Image::load(Path::new("examples/assets/atlas/icons.png"))?;
	let texture = image_texture_cache.get_texture(graphic.get_gl(), &image, true, false);
	assert!(texture.is_some());
	assert!(!image.has_pixels());
	drop(image);
	image_texture_cache.sweep(graphic.get_gl());
	assert_eq!(image_texture_cache.get_texture_count(), 0);
	Ok(())
}
생성자constructorpub fn new() -> ImageTextureCache#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsImageTextureCache 빈 캐시.Empty cache.

메서드methodpub fn get_texture(&mut self, gl: &GlContext, image: &Rc<Image>, is_smoothing: bool, is_mipmap: bool) -> Option<crate::core::graphic::GlTexture>#0.3.2 부터since 0.3.2소스source

이미지의 텍스처 반환. (없으면 올리고 CPU 픽셀을 놓는다, 필터 설정이 바뀌었으면 다시 건다)

  • 다른 캐시가 올리며 픽셀을 놓은 그림은 그 텍스처에서 되읽어 올린다.
  • 픽셀 길이가 너비x높이x4 가 아니면(되읽지도 못하면) 올리지 않고 None.

Returns the texture of an image. (uploads it and releases the CPU pixels if missing, reapplies the filter if the filter settings changed)

  • An image whose pixels were released when another cache uploaded it is read back from that texture and uploaded.
  • If the pixel length is not width x height x 4 (and it cannot be read back either), it is not uploaded and None.
gl&GlContext
텍스처를 만들 GL 컨텍스트.GL context to create the texture with.
image&Rc<Image>
텍스처를 받을 이미지. (처음 올릴 때 CPU 픽셀을 놓는다, set_pixels_retained(true) 면 남긴다)Image to get the texture for. (releases the CPU pixels on the first upload, keeps them if set_pixels_retained(true))
is_smoothingbool
true 면 LINEAR, false 면 NEAREST 필터.true for a LINEAR filter, false for NEAREST.
is_mipmapbool
true 면 줄일 때 밉맵 필터를 쓴다. (밉맵은 올릴 때 늘 만든다)true uses a mipmap filter when minifying. (mipmaps are always built on upload)

반환ReturnsOption<crate::core::graphic::GlTexture> 이미지의 텍스처. GL 텍스처를 만들지 못하면 None.Texture of the image. None if the GL texture cannot be created.

메서드methodpub fn sweep(&mut self, gl: &GlContext)#0.3.2 부터since 0.3.2소스source

원본 이미지가 사라진 텍스처 지우기.

Deletes textures whose original image is gone.

gl&GlContext
텍스처를 만든 GL 컨텍스트.GL context that created the textures.
메서드methodpub fn get_texture_count(&self) -> usize#0.3.2 부터since 0.3.2소스source

올라가 있는 텍스처 수.

Number of uploaded textures.

반환Returnsusize 캐시에 든 텍스처 수. (이미지가 사라졌어도 sweep 전이면 센다)Number of textures in the cache. (counted even if the image is gone, until sweep)