use crate::textures::{
CubeTexture, CubeUvAtlas, Texture, TextureFilter, TextureFormat, TextureWrap,
};
use std::sync::Arc;
const TEXTURE_ROW_ALIGN: u32 = 256;
fn pad_rows_for_upload(pixels: &[u8], width: u32, height: u32, bpp: u32) -> (Vec<u8>, u32) {
let unpadded = width * bpp;
let padded = unpadded.div_ceil(TEXTURE_ROW_ALIGN) * TEXTURE_ROW_ALIGN;
if padded == unpadded {
return (pixels.to_vec(), padded);
}
let mut out = vec![0u8; (padded * height) as usize];
for y in 0..height as usize {
let src = y * unpadded as usize;
let dst = y * padded as usize;
out[dst..dst + unpadded as usize].copy_from_slice(&pixels[src..src + unpadded as usize]);
}
(out, padded)
}
pub fn f32_to_f16_bits(v: f32) -> u16 {
let bits = v.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exp = ((bits >> 23) & 0xff) as i32;
let mant = bits & 0x007f_ffff;
if exp == 0xff {
return sign | 0x7c00 | if mant != 0 { 0x0200 } else { 0 };
}
let e = exp - 127 + 15;
if e >= 0x1f {
return sign | 0x7c00; }
if e <= 0 {
if e < -10 {
return sign; }
let m = mant | 0x0080_0000;
let shift = (14 - e) as u32;
let half = (m >> shift) as u16;
let round = ((m >> (shift - 1)) & 1) as u16;
return sign | (half + round);
}
let half = ((e as u32) << 10) as u16 | (mant >> 13) as u16;
let sticky = mant & 0x1fff;
let round = u16::from(sticky > 0x1000 || (sticky == 0x1000 && (half & 1) == 1));
sign | (half + round)
}
pub fn f16_bits_to_f32(h: u16) -> f32 {
crate::renderer::readback::f16_to_f32(h)
}
fn pad_rows_rgba16f(f32_pixels: &[f32], width: u32, height: u32) -> (Vec<u8>, u32) {
let bpp = 8u32;
let unpadded = width * bpp;
let padded = unpadded.div_ceil(TEXTURE_ROW_ALIGN) * TEXTURE_ROW_ALIGN;
let mut out = vec![0u8; (padded * height) as usize];
for y in 0..height {
for x in 0..width {
let fi = ((y * width + x) * 4) as usize;
let dst = (y * padded + x * bpp) as usize;
for c in 0..4usize {
let h = f32_to_f16_bits(f32_pixels[fi + c]);
out[dst + c * 2..dst + c * 2 + 2].copy_from_slice(&h.to_le_bytes());
}
}
}
(out, padded)
}
pub struct MipGenerator {
shader: wgpu::ShaderModule,
layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
pipelines: crate::renderer::fullscreen::FormatPipelines<wgpu::RenderPipeline>,
}
impl MipGenerator {
pub fn new(device: &wgpu::Device) -> Self {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("threers mipgen"),
source: wgpu::ShaderSource::Wgsl(
format!(
"{}\n{}",
crate::renderer::fullscreen::VS,
r#"
@group(0) @binding(0) var src : texture_2d<f32>;
@group(0) @binding(1) var samp : sampler;
@fragment
fn fs(in : FullscreenVertex) -> @location(0) vec4<f32> {
// The destination texel centre lands exactly between four source texels,
// so a single bilinear tap returns their average.
return textureSampleLevel(src, samp, in.uv, 0.0);
}
"#
)
.into(),
),
});
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("threers mipgen layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("threers mipgen sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::MipmapFilterMode::Nearest,
..Default::default()
});
Self {
shader,
layout,
sampler,
pipelines: crate::renderer::fullscreen::FormatPipelines::new(),
}
}
fn renderable(format: wgpu::TextureFormat) -> bool {
matches!(
format,
wgpu::TextureFormat::Rgba8Unorm
| wgpu::TextureFormat::Rgba8UnormSrgb
| wgpu::TextureFormat::Rgba16Float
| wgpu::TextureFormat::R8Unorm
)
}
fn ensure_pipeline(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat) {
let layout = &self.layout;
let shader = &self.shader;
self.pipelines.get_or_build(format, || {
let pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("threers mipgen pipeline layout"),
bind_group_layouts: &[Some(layout)],
immediate_size: 0,
});
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("threers mipgen"),
layout: Some(&pl),
vertex: wgpu::VertexState {
module: shader,
entry_point: Some(crate::renderer::fullscreen::VS_ENTRY),
buffers: &[],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: shader,
entry_point: Some("fs"),
targets: &[Some(format.into())],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview_mask: None,
cache: None,
})
});
}
pub fn generate(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
texture: &wgpu::Texture,
format: wgpu::TextureFormat,
levels: u32,
) {
if levels < 2 {
return;
}
self.ensure_pipeline(device, format);
let pipeline = self
.pipelines
.get(format)
.expect("ensure_pipeline just built it");
let layout = &self.layout;
let sampler = &self.sampler;
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("threers mipgen"),
});
for level in 1..levels {
let src = texture.create_view(&wgpu::TextureViewDescriptor {
base_mip_level: level - 1,
mip_level_count: Some(1),
..Default::default()
});
let dst = texture.create_view(&wgpu::TextureViewDescriptor {
base_mip_level: level,
mip_level_count: Some(1),
..Default::default()
});
let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("threers mipgen bind"),
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&src),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(sampler),
},
],
});
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("threers mipgen pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &dst,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(pipeline);
pass.set_bind_group(0, &bind, &[]);
pass.draw(0..3, 0..1);
}
queue.submit(Some(encoder.finish()));
}
}
pub struct GpuTexture {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
}
pub struct GpuCubeTexture {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub cube_uv_texture: Option<wgpu::Texture>,
pub cube_uv_view: Option<wgpu::TextureView>,
}
pub(crate) type CubeFaces = [std::sync::Arc<Vec<u8>>; 6];
pub(crate) fn mip_chain(
format: TextureFormat,
size: u32,
faces: &CubeFaces,
) -> (Vec<CubeFaces>, Vec<u32>) {
use std::sync::Arc;
let four_byte = matches!(
format,
TextureFormat::Rgba8Unorm | TextureFormat::Rgba8UnormSrgb
);
let srgb = matches!(format, TextureFormat::Rgba8UnormSrgb);
let mut sizes = vec![size];
let mut chain: Vec<[Arc<Vec<u8>>; 6]> = vec![faces.clone()];
if !four_byte {
return (chain, sizes);
}
let lin = |b: u8| -> f32 {
let s = b as f32 / 255.0;
if !srgb {
s
} else if s <= 0.04045 {
s / 12.92
} else {
((s + 0.055) / 1.055).powf(2.4)
}
};
let enc = |l: f32| -> u8 {
let s = if !srgb {
l
} else if l <= 0.0031308 {
l * 12.92
} else {
1.055 * l.powf(1.0 / 2.4) - 0.055
};
(s.clamp(0.0, 1.0) * 255.0).round() as u8
};
let mut cur = faces.clone();
let mut w = size;
while w > 1 {
let half = w / 2;
let next: [Arc<Vec<u8>>; 6] = std::array::from_fn(|f| {
let src = &cur[f];
let mut out = vec![0u8; (half * half) as usize * 4];
for y in 0..half {
for x in 0..half {
for c in 0..4 {
let mut acc = 0.0f32;
for dy in 0..2 {
for dx in 0..2 {
let i = (((y * 2 + dy) * w + (x * 2 + dx)) * 4) as usize + c;
acc += if c == 3 {
src[i] as f32 / 255.0
} else {
lin(src[i])
};
}
}
let v = acc * 0.25;
out[((y * half + x) * 4) as usize + c] = if c == 3 {
(v.clamp(0.0, 1.0) * 255.0).round() as u8
} else {
enc(v)
};
}
}
}
Arc::new(out)
});
chain.push(next.clone());
sizes.push(half);
cur = next;
w = half;
}
(chain, sizes)
}
impl GpuCubeTexture {
pub fn upload(device: &wgpu::Device, queue: &wgpu::Queue, src: &CubeTexture) -> Self {
if let Some(atlas) = &src.cube_uv_atlas {
return Self::upload_cube_uv(device, queue, src, atlas);
}
if let (Some(mips), Some(sizes)) = (&src.pmrem_mips, &src.pmrem_sizes) {
return Self::upload_pmrem(device, queue, src.format, mips, sizes);
}
let (mips, sizes) = mip_chain(src.format, src.size.max(1), &src.faces);
Self::upload_pmrem(device, queue, src.format, &mips, &sizes)
}
fn upload_cube_uv(
device: &wgpu::Device,
queue: &wgpu::Queue,
src: &CubeTexture,
atlas: &CubeUvAtlas,
) -> Self {
let wgpu_fmt = wgpu_format(src.format);
let cube = Self::upload_faces(device, queue, src.format, src.size.max(1), 1, &src.faces, 0);
let (uv_fmt, upload_bytes, bytes_per_row) = if let Some(f32_px) = &atlas.pixels_f32 {
let (bytes, bpr) = pad_rows_rgba16f(f32_px, atlas.width, atlas.height);
(wgpu::TextureFormat::Rgba16Float, bytes, bpr)
} else {
let bpp = 4u32;
let (bytes, bpr) =
pad_rows_for_upload(atlas.pixels.as_ref(), atlas.width, atlas.height, bpp);
(wgpu_fmt, bytes, bpr)
};
let uv_tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some("threers cube uv atlas"),
size: wgpu::Extent3d {
width: atlas.width,
height: atlas.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: uv_fmt,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &uv_tex,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&upload_bytes,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(atlas.height),
},
wgpu::Extent3d {
width: atlas.width,
height: atlas.height,
depth_or_array_layers: 1,
},
);
let uv_view = uv_tex.create_view(&wgpu::TextureViewDescriptor::default());
Self {
texture: cube.texture,
view: cube.view,
cube_uv_texture: Some(uv_tex),
cube_uv_view: Some(uv_view),
}
}
fn upload_pmrem(
device: &wgpu::Device,
queue: &wgpu::Queue,
format: TextureFormat,
mips: &[[Arc<Vec<u8>>; 6]],
sizes: &[u32],
) -> Self {
let base_size = sizes[0].max(1);
let mip_count = sizes.len() as u32;
Self::upload_mip_faces(device, queue, format, base_size, mip_count, mips, sizes)
}
fn upload_faces(
device: &wgpu::Device,
queue: &wgpu::Queue,
format: TextureFormat,
size: u32,
mip_count: u32,
faces: &[Arc<Vec<u8>>; 6],
_base_level: u32,
) -> Self {
let wgpu_fmt = wgpu_format(format);
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("threers cube texture"),
size: wgpu::Extent3d {
width: size.max(1),
height: size.max(1),
depth_or_array_layers: 6,
},
mip_level_count: mip_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu_fmt,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
assert!(
!format.is_block_compressed(),
"cube textures are not block-compressed ({format:?})"
);
let bytes_per_pixel = format.bytes_per_block();
let bytes_per_row = size * bytes_per_pixel as u32;
for (layer, face) in faces.iter().enumerate() {
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: 0,
y: 0,
z: layer as u32,
},
aspect: wgpu::TextureAspect::All,
},
face,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(size),
},
wgpu::Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
);
}
let view = texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("threers cube view"),
dimension: Some(wgpu::TextureViewDimension::Cube),
..Default::default()
});
Self {
texture,
view,
cube_uv_texture: None,
cube_uv_view: None,
}
}
fn upload_mip_faces(
device: &wgpu::Device,
queue: &wgpu::Queue,
format: TextureFormat,
base_size: u32,
mip_count: u32,
mips: &[[Arc<Vec<u8>>; 6]],
sizes: &[u32],
) -> Self {
let wgpu_fmt = wgpu_format(format);
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("threers pmrem cube texture"),
size: wgpu::Extent3d {
width: base_size,
height: base_size,
depth_or_array_layers: 6,
},
mip_level_count: mip_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu_fmt,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
assert!(
!format.is_block_compressed(),
"cube textures are not block-compressed ({format:?})"
);
let bytes_per_pixel = format.bytes_per_block();
for (level, (faces, size)) in mips.iter().zip(sizes.iter()).enumerate() {
let size = *size;
let bytes_per_row = size * bytes_per_pixel as u32;
for (layer, face) in faces.iter().enumerate() {
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: level as u32,
origin: wgpu::Origin3d {
x: 0,
y: 0,
z: layer as u32,
},
aspect: wgpu::TextureAspect::All,
},
face,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(size),
},
wgpu::Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
);
}
}
let view = texture.create_view(&wgpu::TextureViewDescriptor {
label: Some("threers pmrem cube view"),
dimension: Some(wgpu::TextureViewDimension::Cube),
..Default::default()
});
Self {
texture,
view,
cube_uv_texture: None,
cube_uv_view: None,
}
}
}
pub type CubeCacheKey = (u64, u8, u32);
pub fn cube_cache_key(t: &Arc<CubeTexture>) -> CubeCacheKey {
(t.id, t.format as u8, t.size)
}
fn oriented(src: &Texture) -> std::borrow::Cow<'_, Texture> {
if src.format.is_block_compressed() {
assert!(
!src.flip_y,
"flip_y on a block-compressed texture ({:?}): rows of blocks cannot \
be reversed -- encode it the right way up",
src.format
);
return std::borrow::Cow::Borrowed(src);
}
let stride = src.width as usize * src.bytes_per_pixel();
if !src.flip_y
|| src.external_rt_id.is_some()
|| stride == 0
|| src.data.len() != stride * src.height as usize
{
return std::borrow::Cow::Borrowed(src);
}
let mut data = Vec::with_capacity(src.data.len());
for row in src.data.chunks_exact(stride).rev() {
data.extend_from_slice(row);
}
let mut flipped = src.clone();
flipped.data = std::sync::Arc::new(data);
std::borrow::Cow::Owned(flipped)
}
impl GpuTexture {
pub fn upload_with_mipgen(
device: &wgpu::Device,
queue: &wgpu::Queue,
src: &Texture,
mipgen: &mut MipGenerator,
) -> Self {
let oriented = &*oriented(src);
let format = wgpu_format(oriented.format);
let (width, height) = (oriented.width.max(1), oriented.height.max(1));
if oriented.format.is_block_compressed() {
return Self::upload(device, queue, src);
}
let bpp = oriented.bytes_per_pixel() as u32;
let expected = (width as usize) * (height as usize) * bpp as usize;
if !MipGenerator::renderable(format)
|| oriented.external_rt_id.is_some()
|| oriented.data.len() < expected
|| (width == 1 && height == 1)
{
return Self::upload(device, queue, src);
}
let levels = 1 + (width.max(height) as f32).log2().floor() as u32;
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("threers texture"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: levels,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&oriented.data,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(width * bpp),
rows_per_image: Some(height),
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
mipgen.generate(device, queue, &texture, format, levels);
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self { texture, view }
}
pub fn upload(device: &wgpu::Device, queue: &wgpu::Queue, src: &Texture) -> Self {
let src = &*oriented(src);
let format = wgpu_format(src.format);
let (width, height) = (src.width.max(1), src.height.max(1));
let size = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
if src.format.is_block_compressed() {
let (bw, bh) = src.format.block_dim();
assert!(
width % bw == 0 && height % bh == 0,
"{:?} texture is {width}x{height}: a block-compressed base size \
must be a multiple of {bw}x{bh} (mip levels may be ragged, the \
base may not)",
src.format
);
}
let mips = build_mip_chain(src);
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("threers texture"),
size,
mip_level_count: 1 + mips.len() as u32 + src.mips.len() as u32,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let (bw, bh) = src.format.block_dim();
let write = |level: u32, w: u32, h: u32, data: &[u8]| {
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: level,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
data,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(src.format.bytes_per_row(w)),
rows_per_image: Some(src.format.rows_per_image(h)),
},
wgpu::Extent3d {
width: w.div_ceil(bw) * bw,
height: h.div_ceil(bh) * bh,
depth_or_array_layers: 1,
},
);
};
write(0, width, height, &src.data);
for (level, (w, h, data)) in mips.iter().enumerate() {
write(level as u32 + 1, *w, *h, data);
}
let mut lw = width;
let mut lh = height;
for (i, level) in src.mips.iter().enumerate() {
lw = (lw / 2).max(1);
lh = (lh / 2).max(1);
let want = src.format.data_len(lw, lh);
if level.len() != want {
eprintln!(
"threers: texture mip {} is {} bytes, expected {want} for {lw}x{lh} {:?} \
-- skipping the rest of the chain",
i + 1,
level.len(),
src.format
);
break;
}
write(i as u32 + 1, lw, lh, level);
}
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self { texture, view }
}
}
pub fn mip_chain_for(src: &Texture) -> usize {
build_mip_chain(src).len()
}
fn build_mip_chain(src: &Texture) -> Vec<(u32, u32, Vec<u8>)> {
if src.format.is_block_compressed() {
return Vec::new();
}
let bpp = src.bytes_per_pixel();
let (w, h) = (src.width.max(1), src.height.max(1));
if src.external_rt_id.is_some()
|| src.data.len() < (w as usize) * (h as usize) * bpp
|| (w == 1 && h == 1)
{
return Vec::new();
}
if matches!(src.format, TextureFormat::Rgba16Float) {
return half_mip_chain(src);
}
build_mip_chain_8(src, w, h, bpp)
}
fn half_mip_chain(src: &Texture) -> Vec<(u32, u32, Vec<u8>)> {
let (mut w, mut h) = (src.width.max(1), src.height.max(1));
let mut level: Vec<f32> = src
.data
.chunks_exact(2)
.map(|b| f16_bits_to_f32(u16::from_le_bytes([b[0], b[1]])))
.collect();
let mut out = Vec::new();
while w > 1 || h > 1 {
let (nw, nh) = ((w / 2).max(1), (h / 2).max(1));
let mut next = vec![0f32; (nw as usize) * (nh as usize) * 4];
for y in 0..nh as usize {
let y0 = (y * 2).min(h as usize - 1);
let y1 = (y * 2 + 1).min(h as usize - 1);
for x in 0..nw as usize {
let x0 = (x * 2).min(w as usize - 1);
let x1 = (x * 2 + 1).min(w as usize - 1);
let taps = [
(y0 * w as usize + x0) * 4,
(y0 * w as usize + x1) * 4,
(y1 * w as usize + x0) * 4,
(y1 * w as usize + x1) * 4,
];
let dst = (y * nw as usize + x) * 4;
for c in 0..4 {
next[dst + c] = taps.iter().map(|t| level[t + c]).sum::<f32>() * 0.25;
}
}
}
let bytes = next
.iter()
.flat_map(|&v| f32_to_f16_bits(v).to_le_bytes())
.collect();
out.push((nw, nh, bytes));
level = next;
w = nw;
h = nh;
}
out
}
fn build_mip_chain_8(src: &Texture, w: u32, h: u32, bpp: usize) -> Vec<(u32, u32, Vec<u8>)> {
let (mut w, mut h) = (w, h);
let srgb = matches!(src.format, TextureFormat::Rgba8UnormSrgb);
let color_channels = if bpp >= 3 { 3 } else { bpp };
let decode = srgb_decode_table();
let mut level: Vec<u8> = src.data.as_ref().clone();
let mut out = Vec::new();
while w > 1 || h > 1 {
let (nw, nh) = ((w / 2).max(1), (h / 2).max(1));
let mut next = vec![0u8; (nw as usize) * (nh as usize) * bpp];
for y in 0..nh as usize {
let y0 = (y * 2).min(h as usize - 1);
let y1 = (y * 2 + 1).min(h as usize - 1);
for x in 0..nw as usize {
let x0 = (x * 2).min(w as usize - 1);
let x1 = (x * 2 + 1).min(w as usize - 1);
let taps = [
(y0 * w as usize + x0) * bpp,
(y0 * w as usize + x1) * bpp,
(y1 * w as usize + x0) * bpp,
(y1 * w as usize + x1) * bpp,
];
let dst = (y * nw as usize + x) * bpp;
for c in 0..bpp {
let encoded = srgb && c < color_channels;
let sum: f32 = taps
.iter()
.map(|t| {
let b = level[t + c];
if encoded {
decode[b as usize]
} else {
b as f32 / 255.0
}
})
.sum();
let avg = sum * 0.25;
let avg = if encoded {
linear_to_srgb_fast(avg)
} else {
avg
};
next[dst + c] = (avg * 255.0).round().clamp(0.0, 255.0) as u8;
}
}
}
out.push((nw, nh, next.clone()));
level = next;
w = nw;
h = nh;
}
out
}
fn srgb_decode_table() -> &'static [f32; 256] {
static TABLE: std::sync::OnceLock<[f32; 256]> = std::sync::OnceLock::new();
TABLE.get_or_init(|| {
let mut t = [0.0f32; 256];
for (i, v) in t.iter_mut().enumerate() {
*v = srgb_to_linear(i as f32 / 255.0);
}
t
})
}
fn srgb_to_linear(v: f32) -> f32 {
if v <= 0.04045 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
}
const SRGB_ENCODE_N: usize = 2048;
fn srgb_encode_table() -> &'static [f32; SRGB_ENCODE_N + 1] {
static TABLE: std::sync::OnceLock<[f32; SRGB_ENCODE_N + 1]> = std::sync::OnceLock::new();
TABLE.get_or_init(|| {
let mut t = [0.0f32; SRGB_ENCODE_N + 1];
for (i, v) in t.iter_mut().enumerate() {
let x = i as f32 / SRGB_ENCODE_N as f32;
*v = 1.055 * x.powf(1.0 / 2.4) - 0.055;
}
t
})
}
fn linear_to_srgb_fast(v: f32) -> f32 {
if v <= 0.0031308 {
return v * 12.92;
}
let x = v.min(1.0) * SRGB_ENCODE_N as f32;
let i = x as usize;
let f = x - i as f32;
let t = srgb_encode_table();
t[i] + (t[(i + 1).min(SRGB_ENCODE_N)] - t[i]) * f
}
#[cfg_attr(not(test), allow(dead_code))]
fn linear_to_srgb(v: f32) -> f32 {
if v <= 0.0031308 {
v * 12.92
} else {
1.055 * v.powf(1.0 / 2.4) - 0.055
}
}
pub fn wgpu_format(f: TextureFormat) -> wgpu::TextureFormat {
match f {
TextureFormat::Rgba8UnormSrgb => wgpu::TextureFormat::Rgba8UnormSrgb,
TextureFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm,
TextureFormat::R8Unorm => wgpu::TextureFormat::R8Unorm,
TextureFormat::Rgba16Float => wgpu::TextureFormat::Rgba16Float,
TextureFormat::Bc1RgbaUnormSrgb => wgpu::TextureFormat::Bc1RgbaUnormSrgb,
TextureFormat::Bc7RgbaUnormSrgb => wgpu::TextureFormat::Bc7RgbaUnormSrgb,
}
}
pub fn wgpu_filter(f: TextureFilter) -> wgpu::FilterMode {
match f {
TextureFilter::Nearest => wgpu::FilterMode::Nearest,
TextureFilter::Linear => wgpu::FilterMode::Linear,
}
}
pub fn wgpu_wrap(w: TextureWrap) -> wgpu::AddressMode {
match w {
TextureWrap::ClampToEdge => wgpu::AddressMode::ClampToEdge,
TextureWrap::Repeat => wgpu::AddressMode::Repeat,
TextureWrap::MirroredRepeat => wgpu::AddressMode::MirrorRepeat,
}
}
pub type TexCacheKey = (u64, u64, u32, u32, u32, bool);
pub fn tex_cache_key(t: &Arc<Texture>) -> TexCacheKey {
let format = match t.format {
TextureFormat::Rgba8Unorm => 0,
TextureFormat::Rgba8UnormSrgb => 1,
TextureFormat::R8Unorm => 2,
TextureFormat::Rgba16Float => 3,
TextureFormat::Bc1RgbaUnormSrgb => 4,
TextureFormat::Bc7RgbaUnormSrgb => 5,
};
(t.id, t.upload_seq, t.width, t.height, format, t.flip_y)
}
#[cfg(test)]
mod mip_tests {
use super::*;
use crate::textures::{Texture, TextureFormat};
#[test]
fn the_fast_srgb_encode_matches_the_exact_one() {
let mut worst = 0.0f32;
for i in 0..=20000 {
let v = i as f32 / 20000.0;
worst = worst.max((linear_to_srgb_fast(v) - linear_to_srgb(v)).abs() * 255.0);
}
assert!(
worst < 0.5,
"fast sRGB encode is off by {worst} levels of 255"
);
}
#[test]
fn half_float_survives_the_round_trip_above_one() {
for v in [
0.0, 1e-4, 0.1, 0.5, 1.0, 1.9, 2.0, 2.1, 5.0, 20.6, 100.0, 550.0, 6000.0, 60000.0,
] {
let back = f16_bits_to_f32(f32_to_f16_bits(v));
let tol = (v * 0.002).max(1e-6);
assert!(
(back - v).abs() <= tol,
"{v} round-tripped to {back}, off by more than {tol}"
);
}
for v in [-1.0f32, -3.5, -900.0] {
let back = f16_bits_to_f32(f32_to_f16_bits(v));
assert!(
(back - v).abs() <= (v.abs() * 0.002).max(1e-6),
"{v} round-tripped to {back}"
);
}
assert!(f16_bits_to_f32(f32_to_f16_bits(1e9)) > 60000.0);
}
#[test]
fn half_float_maps_get_a_mip_chain() {
let texels = 8 * 4;
let mut data = Vec::with_capacity(texels * 8);
for _ in 0..texels {
for c in [4.0f32, 2.0, 1.0, 1.0] {
data.extend_from_slice(&f32_to_f16_bits(c).to_le_bytes());
}
}
let src = Texture::new(8, 4, TextureFormat::Rgba16Float, data);
let mips = build_mip_chain(&src);
let sizes: Vec<(u32, u32)> = mips.iter().map(|(w, h, _)| (*w, *h)).collect();
assert_eq!(sizes, vec![(4, 2), (2, 1), (1, 1)]);
let (_, _, last) = mips.last().unwrap();
let red = f16_bits_to_f32(u16::from_le_bytes([last[0], last[1]]));
assert!((red - 4.0).abs() < 0.01, "1x1 mip came out at {red}");
}
#[test]
fn a_chain_runs_down_to_one_by_one() {
let src = Texture::new(8, 4, TextureFormat::Rgba8Unorm, vec![128; 8 * 4 * 4]);
let mips = build_mip_chain(&src);
let sizes: Vec<(u32, u32)> = mips.iter().map(|(w, h, _)| (*w, *h)).collect();
assert_eq!(sizes, vec![(4, 2), (2, 1), (1, 1)]);
for (w, h, data) in &mips {
assert_eq!(data.len(), (*w as usize) * (*h as usize) * 4);
}
}
#[test]
fn non_power_of_two_sizes_still_terminate() {
let src = Texture::new(5, 3, TextureFormat::Rgba8Unorm, vec![7; 5 * 3 * 4]);
let sizes: Vec<(u32, u32)> = build_mip_chain(&src)
.iter()
.map(|(w, h, _)| (*w, *h))
.collect();
assert_eq!(sizes, vec![(2, 1), (1, 1)]);
}
#[test]
fn a_flat_image_downsamples_to_the_same_value() {
for format in [TextureFormat::Rgba8Unorm, TextureFormat::Rgba8UnormSrgb] {
let src = Texture::new(4, 4, format, vec![200; 4 * 4 * 4]);
for (_, _, data) in build_mip_chain(&src) {
assert!(
data.iter().all(|&v| (v as i32 - 200).abs() <= 1),
"{format:?} drifted: {data:?}"
);
}
}
}
#[test]
fn srgb_averages_in_linear_light() {
let mut data = vec![0u8; 8];
data[4..8].copy_from_slice(&[255, 255, 255, 255]);
let srgb = Texture::new(2, 1, TextureFormat::Rgba8UnormSrgb, data.clone());
let (_, _, mip) = build_mip_chain(&srgb).remove(0);
assert!((mip[0] as i32 - 188).abs() <= 2, "sRGB mip was {}", mip[0]);
let linear = Texture::new(2, 1, TextureFormat::Rgba8Unorm, data);
let (_, _, mip) = build_mip_chain(&linear).remove(0);
assert!(
(mip[0] as i32 - 128).abs() <= 1,
"linear mip was {}",
mip[0]
);
}
#[test]
fn sources_without_pixels_to_filter_are_skipped() {
let rt = Texture::from_render_target(1, 64, 64, TextureFormat::Rgba8Unorm);
assert!(build_mip_chain(&rt).is_empty());
let short = Texture::new(16, 16, TextureFormat::Rgba8Unorm, vec![0; 8]);
assert!(build_mip_chain(&short).is_empty());
}
#[test]
fn single_channel_textures_chain_too() {
let src = Texture::new(4, 4, TextureFormat::R8Unorm, vec![64; 16]);
let mips = build_mip_chain(&src);
assert_eq!(mips.len(), 2);
assert!(mips[0].2.iter().all(|&v| v == 64));
}
}