use std::rc::Rc;
use mln_gfx::Size;
use mln_gfx::gpu::{Gpu, GpuError, SamplerId, TextureDesc, TextureFormat, TextureId, TextureRegion, TextureUsage};
use mln_gfx::rendering_stats::RenderingStats;
use mln_gfx::texture2d::SamplerState;
use mln_style::bitmap::PremultipliedImage;
use crate::draw::resources::{Device, SharedGpuTexture};
use crate::draw::texture_size;
pub(crate) struct Texture2D {
texture: Option<SharedGpuTexture>,
size: Size,
format: TextureFormat,
render_target: bool,
mipmapped: bool,
sampler: SamplerState,
sampler_id: Option<SamplerId>,
image: Option<Rc<PremultipliedImage>>,
}
impl Texture2D {
pub(crate) fn new() -> Texture2D {
Texture2D {
texture: None,
size: Size::new(0, 0),
format: TextureFormat::Rgba8Unorm,
render_target: false,
mipmapped: false,
sampler: SamplerState::default(),
sampler_id: None,
image: None,
}
}
#[must_use]
pub(crate) fn size(&self) -> Size {
self.size
}
fn set_size(&mut self, size: Size) {
if size != self.size {
self.release();
self.size = size;
}
}
#[must_use]
pub(crate) fn format(&self) -> TextureFormat {
self.format
}
fn set_format(&mut self, format: TextureFormat) {
if format != self.format {
self.release();
self.format = format;
}
}
#[must_use]
pub(crate) fn sampler(&self) -> SamplerState {
self.sampler
}
pub(crate) fn sampler_id(&self) -> Option<SamplerId> {
self.sampler_id
}
pub(crate) fn set_sampler_id(&mut self, sampler: SamplerId) {
self.sampler_id = Some(sampler);
}
pub(crate) fn has_image(&self) -> bool {
self.image.is_some()
}
pub(crate) fn gpu_texture(&self) -> Option<&SharedGpuTexture> {
self.texture.as_ref()
}
pub(crate) fn create(&mut self, gpu: &mut dyn Gpu, device: &mut Device) -> Result<TextureId, GpuError> {
if let Some(texture) = &self.texture {
return Ok(texture.id());
}
let desc = TextureDesc {
label: if self.render_target { "render target" } else { "texture" },
size: self.size,
format: self.format,
usage: TextureUsage { sampled: true, render_target: self.render_target, copy_src: self.render_target },
mipmapped: self.mipmapped,
};
let texture = device.texture(gpu, &desc, Some(self.bytes()))?;
let id = texture.id();
self.texture = Some(Rc::new(texture));
Ok(id)
}
pub(crate) fn upload_data(
&mut self,
gpu: &mut dyn Gpu,
device: &mut Device,
data: &[u8],
size: Size,
) -> Result<(), GpuError> {
self.set_size(size);
if size.is_empty() {
return Ok(());
}
let texture = self.create(gpu, device)?;
if data.is_empty() {
return Ok(());
}
write(gpu, &mut device.stats, texture, data, TextureRegion { x: 0, y: 0, size })
}
pub(crate) fn upload_sub_region(
&mut self,
gpu: &mut dyn Gpu,
device: &mut Device,
data: &[u8],
size: Size,
x: u16,
y: u16,
) -> Result<(), GpuError> {
let texture = self.create(gpu, device)?;
let region = TextureRegion { x: u32::from(x), y: u32::from(y), size };
write(gpu, &mut device.stats, texture, data, region)
}
pub(crate) fn upload(&mut self, gpu: &mut dyn Gpu, device: &mut Device) -> Result<(), GpuError> {
match self.image.take() {
Some(image) if image.valid() => self.upload_data(gpu, device, &image.data, texture_size(image.size)),
_ => Ok(()),
}
}
pub(crate) fn bind(&mut self, gpu: &mut dyn Gpu, device: &mut Device) -> Result<TextureId, GpuError> {
self.upload(gpu, device)?;
self.create(gpu, device)
}
pub(crate) fn bytes(&self) -> usize {
let texel = self.format.bytes_per_texel().unwrap_or(4) as usize;
let bytes = self.size.width as usize * self.size.height as usize * texel;
if self.mipmapped { bytes + bytes / 3 } else { bytes }
}
fn release(&mut self) {
self.texture = None;
}
}
pub(crate) struct TextureMut<'a> {
texture: &'a mut Texture2D,
}
impl<'a> TextureMut<'a> {
pub(crate) fn new(texture: &'a mut Texture2D) -> TextureMut<'a> {
TextureMut { texture }
}
pub(crate) fn set_size(&mut self, size: Size) {
self.texture.set_size(size);
}
pub(crate) fn set_format(&mut self, format: TextureFormat) {
self.texture.set_format(format);
}
pub(crate) fn set_render_target(&mut self) {
self.texture.render_target = true;
}
pub(crate) fn set_mipmapped(&mut self, mipmapped: bool) {
if mipmapped != self.texture.mipmapped {
self.texture.release();
self.texture.mipmapped = mipmapped;
}
}
pub(crate) fn set_sampler(&mut self, sampler: SamplerState) {
let texture = &mut *self.texture;
let sampler = SamplerState { max_anisotropy: sampler.max_anisotropy.max(1), ..sampler };
if sampler != texture.sampler {
texture.sampler = sampler;
texture.sampler_id = None;
}
}
pub(crate) fn set_image(&mut self, image: Option<Rc<PremultipliedImage>>) {
if let Some(image) = &image {
self.set_size(texture_size(image.size));
self.set_format(TextureFormat::Rgba8Unorm);
}
self.texture.image = image;
}
}
fn write(
gpu: &mut dyn Gpu,
stats: &mut RenderingStats,
texture: TextureId,
data: &[u8],
region: TextureRegion,
) -> Result<(), GpuError> {
gpu.write_texture(texture, region, data)?;
stats.num_texture_updates += 1;
stats.texture_update_bytes += data.len();
Ok(())
}