#[allow(unused_imports)]
use crate::{CubeSurface, Surface};
#[repr(i32)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Channel {
R = 0,
G = 1,
B = 2,
A = 3,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Container {
Dds,
Dds10,
}
use nvtt_sys::NvttContainer;
impl From<Container> for NvttContainer {
fn from(val: Container) -> Self {
match val {
Container::Dds => NvttContainer::NVTT_Container_DDS,
Container::Dds10 => NvttContainer::NVTT_Container_DDS10,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum EdgeFixup {
None,
Stretch,
Warp,
}
impl From<EdgeFixup> for nvtt_sys::EdgeFixup {
fn from(val: EdgeFixup) -> Self {
match val {
EdgeFixup::None => nvtt_sys::EdgeFixup::NVTT_EdgeFixup_None,
EdgeFixup::Stretch => nvtt_sys::EdgeFixup::NVTT_EdgeFixup_Stretch,
EdgeFixup::Warp => nvtt_sys::EdgeFixup::NVTT_EdgeFixup_Warp,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Quality {
Fastest,
Normal,
Production,
Highest,
}
use nvtt_sys::NvttQuality;
impl From<Quality> for NvttQuality {
fn from(val: Quality) -> Self {
match val {
Quality::Fastest => NvttQuality::NVTT_Quality_Fastest,
Quality::Normal => NvttQuality::NVTT_Quality_Normal,
Quality::Production => NvttQuality::NVTT_Quality_Production,
Quality::Highest => NvttQuality::NVTT_Quality_Highest,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum AlphaMode {
None,
Transparency,
Premultiplied,
}
use nvtt_sys::NvttAlphaMode;
impl From<AlphaMode> for NvttAlphaMode {
fn from(val: AlphaMode) -> Self {
match val {
AlphaMode::None => NvttAlphaMode::NVTT_AlphaMode_None,
AlphaMode::Transparency => NvttAlphaMode::NVTT_AlphaMode_Transparency,
AlphaMode::Premultiplied => NvttAlphaMode::NVTT_AlphaMode_Premultiplied,
}
}
}
impl From<NvttAlphaMode> for AlphaMode {
fn from(other: NvttAlphaMode) -> Self {
match other {
NvttAlphaMode::NVTT_AlphaMode_None => Self::None,
NvttAlphaMode::NVTT_AlphaMode_Transparency => Self::Transparency,
NvttAlphaMode::NVTT_AlphaMode_Premultiplied => Self::Premultiplied,
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum Format {
Rgb,
Rgba, Dxt1,
Dxt1a,
Dxt3,
Dxt5,
Dxt5n,
Bc1, Bc1a, Bc2, Bc3, Bc3n, Bc4,
Bc4S,
Ati2,
Bc5,
Bc5S,
Bc6U,
Bc6S,
Bc7,
Bc3Rgbm,
AstcLdr4x4,
AstcLdr5x4,
AstcLdr5x5,
AstcLdr6x5,
AstcLdr6x6,
AstcLdr8x5,
AstcLdr8x6,
AstcLdr8x8,
AstcLdr10x5,
AstcLdr10x6,
AstcLdr10x8,
AstcLdr10x10,
AstcLdr12x10,
AstcLdr12x12,
}
use nvtt_sys::NvttFormat;
impl From<Format> for NvttFormat {
fn from(val: Format) -> Self {
match val {
Format::Rgb => NvttFormat::NVTT_Format_RGB,
Format::Rgba => NvttFormat::NVTT_Format_RGB, Format::Dxt1 => NvttFormat::NVTT_Format_DXT1,
Format::Dxt1a => NvttFormat::NVTT_Format_DXT1a,
Format::Dxt3 => NvttFormat::NVTT_Format_DXT3,
Format::Dxt5 => NvttFormat::NVTT_Format_DXT5,
Format::Dxt5n => NvttFormat::NVTT_Format_DXT5n,
Format::Bc1 => NvttFormat::NVTT_Format_DXT1, Format::Bc1a => NvttFormat::NVTT_Format_DXT1a, Format::Bc2 => NvttFormat::NVTT_Format_DXT3, Format::Bc3 => NvttFormat::NVTT_Format_DXT5, Format::Bc3n => NvttFormat::NVTT_Format_DXT5n, Format::Bc4 => NvttFormat::NVTT_Format_BC4,
Format::Bc4S => NvttFormat::NVTT_Format_BC4S,
Format::Ati2 => NvttFormat::NVTT_Format_ATI2,
Format::Bc5 => NvttFormat::NVTT_Format_BC5,
Format::Bc5S => NvttFormat::NVTT_Format_BC5S,
Format::Bc6U => NvttFormat::NVTT_Format_BC6U,
Format::Bc6S => NvttFormat::NVTT_Format_BC6S,
Format::Bc7 => NvttFormat::NVTT_Format_BC7,
Format::Bc3Rgbm => NvttFormat::NVTT_Format_BC3_RGBM,
Format::AstcLdr4x4 => NvttFormat::NVTT_Format_ASTC_LDR_4x4,
Format::AstcLdr5x4 => NvttFormat::NVTT_Format_ASTC_LDR_5x4,
Format::AstcLdr5x5 => NvttFormat::NVTT_Format_ASTC_LDR_5x5,
Format::AstcLdr6x5 => NvttFormat::NVTT_Format_ASTC_LDR_6x5,
Format::AstcLdr6x6 => NvttFormat::NVTT_Format_ASTC_LDR_6x6,
Format::AstcLdr8x5 => NvttFormat::NVTT_Format_ASTC_LDR_8x5,
Format::AstcLdr8x6 => NvttFormat::NVTT_Format_ASTC_LDR_8x6,
Format::AstcLdr8x8 => NvttFormat::NVTT_Format_ASTC_LDR_8x8,
Format::AstcLdr10x5 => NvttFormat::NVTT_Format_ASTC_LDR_10x5,
Format::AstcLdr10x6 => NvttFormat::NVTT_Format_ASTC_LDR_10x6,
Format::AstcLdr10x8 => NvttFormat::NVTT_Format_ASTC_LDR_10x8,
Format::AstcLdr10x10 => NvttFormat::NVTT_Format_ASTC_LDR_10x10,
Format::AstcLdr12x10 => NvttFormat::NVTT_Format_ASTC_LDR_12x10,
Format::AstcLdr12x12 => NvttFormat::NVTT_Format_ASTC_LDR_12x12,
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum InputFormat<'a> {
Bgra8Ub {
data: &'a [u8],
unsigned_to_signed: bool,
},
Bgra8Sb(&'a [u8]),
Rgba16f(&'a [u8]),
Rgba32f(&'a [u8]),
R32f(&'a [u8]),
}
#[derive(Clone, Copy, Debug)]
pub enum SplitInputFormat<'a> {
Bgra8Ub {
b: &'a [u8],
g: &'a [u8],
r: &'a [u8],
a: &'a [u8],
},
Bgra8Sb {
b: &'a [u8],
g: &'a [u8],
r: &'a [u8],
a: &'a [u8],
},
Rgba16f {
r: &'a [u8],
g: &'a [u8],
b: &'a [u8],
a: &'a [u8],
},
Rgba32f {
r: &'a [u8],
g: &'a [u8],
b: &'a [u8],
a: &'a [u8],
},
R32f(&'a [u8]),
}
macro_rules! impl_input_format {
($input:ident) => {
impl<'a> $input<'a> {
pub fn dim(&self) -> usize {
match self {
&Self::Bgra8Ub { .. } => 4,
&Self::Bgra8Sb { .. } => 4,
&Self::Rgba16f { .. } => 4,
&Self::Rgba32f { .. } => 1,
&Self::R32f { .. } => 1,
}
}
pub fn width(&self) -> usize {
match self {
&Self::Bgra8Ub { .. } => 1,
&Self::Bgra8Sb { .. } => 1,
&Self::Rgba16f { .. } => 2,
&Self::Rgba32f { .. } => 4,
&Self::R32f { .. } => 4,
}
}
pub(crate) fn into_nvtt(self) -> nvtt_sys::NvttInputFormat {
match self {
Self::Bgra8Ub { .. } => nvtt_sys::NvttInputFormat::NVTT_InputFormat_BGRA_8UB,
Self::Bgra8Sb { .. } => nvtt_sys::NvttInputFormat::NVTT_InputFormat_BGRA_8SB,
Self::Rgba16f { .. } => nvtt_sys::NvttInputFormat::NVTT_InputFormat_RGBA_16F,
Self::Rgba32f { .. } => nvtt_sys::NvttInputFormat::NVTT_InputFormat_RGBA_32F,
Self::R32f { .. } => nvtt_sys::NvttInputFormat::NVTT_InputFormat_R_32F,
}
}
}
};
}
impl_input_format!(InputFormat);
impl_input_format!(SplitInputFormat);
impl<'a> InputFormat<'a> {
pub fn data(&self) -> &[u8] {
match self {
Self::Bgra8Ub { data, .. } => data,
Self::Bgra8Sb(data) => data,
Self::Rgba16f(data) => data,
Self::Rgba32f(data) => data,
Self::R32f(data) => data,
}
}
pub(crate) fn min_bytes(&self, w: u32, h: u32, d: u32) -> u32 {
let pixel_count = (w * h * d) as usize;
let bytes_per_pixel = self.dim() * self.width();
let byte_count = pixel_count * bytes_per_pixel;
byte_count as u32
}
pub(crate) fn fit_dim(&self, w: u32, h: u32, d: u32) -> bool {
self.data().len() >= self.min_bytes(w, h, d) as usize
}
}
impl<'a> SplitInputFormat<'a> {
pub(crate) fn min_bytes(&self, w: u32, h: u32, d: u32) -> u32 {
let pixel_count = (w * h * d) as usize;
let bytes_per_pixel_per_channel = self.width();
let bytes_per_channel = pixel_count * bytes_per_pixel_per_channel;
bytes_per_channel as u32
}
pub(crate) fn shortest_slice_len(&self) -> u32 {
let mut lengths = [usize::MAX; 4];
match self {
Self::Bgra8Ub { b, g, r, a } => {
lengths[0] = b.len();
lengths[1] = g.len();
lengths[2] = r.len();
lengths[3] = a.len();
}
Self::Bgra8Sb { b, g, r, a } => {
lengths[0] = b.len();
lengths[1] = g.len();
lengths[2] = r.len();
lengths[3] = a.len();
}
Self::Rgba32f { r, g, b, a } => {
lengths[0] = r.len();
lengths[1] = g.len();
lengths[2] = b.len();
lengths[3] = a.len();
}
Self::Rgba16f { r, g, b, a } => {
lengths[0] = r.len();
lengths[1] = g.len();
lengths[2] = b.len();
lengths[3] = a.len();
}
Self::R32f(r) => lengths[0] = r.len(),
}
lengths.into_iter().min().unwrap() as u32
}
pub(crate) fn fit_dim(&self, w: u32, h: u32, d: u32) -> bool {
self.shortest_slice_len() >= self.min_bytes(w, h, d)
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum PixelType {
UnsignedNorm,
Float,
UnsignedFloat,
SharedExp,
}
use nvtt_sys::NvttPixelType;
impl From<PixelType> for NvttPixelType {
fn from(val: PixelType) -> Self {
match val {
PixelType::UnsignedNorm => NvttPixelType::NVTT_PixelType_UnsignedNorm,
PixelType::Float => NvttPixelType::NVTT_PixelType_Float,
PixelType::UnsignedFloat => NvttPixelType::NVTT_PixelType_UnsignedFloat,
PixelType::SharedExp => NvttPixelType::NVTT_PixelType_SharedExp,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum TextureType {
D2,
Cube,
D3,
}
use nvtt_sys::NvttTextureType;
impl From<TextureType> for NvttTextureType {
fn from(val: TextureType) -> Self {
match val {
TextureType::D2 => NvttTextureType::NVTT_TextureType_2D,
TextureType::Cube => NvttTextureType::NVTT_TextureType_Cube,
TextureType::D3 => NvttTextureType::NVTT_TextureType_3D,
}
}
}
impl From<NvttTextureType> for TextureType {
fn from(other: NvttTextureType) -> Self {
match other {
NvttTextureType::NVTT_TextureType_2D => Self::D2,
NvttTextureType::NVTT_TextureType_Cube => Self::Cube,
NvttTextureType::NVTT_TextureType_3D => Self::D3,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum WrapMode {
Clamp,
Repeat,
Mirror,
}
use nvtt_sys::NvttWrapMode;
impl From<WrapMode> for NvttWrapMode {
fn from(val: WrapMode) -> Self {
match val {
WrapMode::Clamp => NvttWrapMode::NVTT_WrapMode_Clamp,
WrapMode::Repeat => NvttWrapMode::NVTT_WrapMode_Repeat,
WrapMode::Mirror => NvttWrapMode::NVTT_WrapMode_Mirror,
}
}
}
impl From<NvttWrapMode> for WrapMode {
fn from(other: NvttWrapMode) -> Self {
match other {
NvttWrapMode::NVTT_WrapMode_Clamp => Self::Clamp,
NvttWrapMode::NVTT_WrapMode_Repeat => Self::Repeat,
NvttWrapMode::NVTT_WrapMode_Mirror => Self::Mirror,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Filter<T> {
pub width: f32,
pub algorithm: T,
}
impl Filter<Mipmap> {
pub const fn mipmap_box() -> Self {
Self {
width: 0.5,
algorithm: Mipmap::Box,
}
}
pub const fn mipmap_triangle() -> Self {
Self {
width: 1.0,
algorithm: Mipmap::Triangle,
}
}
pub const fn mipmap_kaiser() -> Self {
Self {
width: 3.0,
algorithm: Mipmap::Kaiser {
alpha: 4.0,
freq: 1.0,
},
}
}
pub const fn mipmap_min() -> Self {
Self {
width: 1.0,
algorithm: Mipmap::Min,
}
}
pub const fn mipmap_max() -> Self {
Self {
width: 1.0,
algorithm: Mipmap::Max,
}
}
pub(crate) fn params(&self) -> [f32; 2] {
let mut params = [0f32; 2];
match self.algorithm {
Mipmap::Box | Mipmap::Triangle | Mipmap::Min | Mipmap::Max => (),
Mipmap::Kaiser { alpha, freq } => {
params[0] = alpha;
params[1] = freq;
}
}
params
}
pub(crate) fn params_ptr(&self, params: &[f32; 2]) -> *const f32 {
match self.algorithm {
Mipmap::Box | Mipmap::Triangle | Mipmap::Min | Mipmap::Max => std::ptr::null(),
Mipmap::Kaiser { .. } => params.as_ptr(),
}
}
}
impl Filter<Resize> {
pub const fn box_resize() -> Self {
Self {
width: 0.5,
algorithm: Resize::Box,
}
}
pub const fn triangle_resize() -> Self {
Self {
width: 1.0,
algorithm: Resize::Triangle,
}
}
pub const fn kaiser_resize() -> Self {
Self {
width: 3.0,
algorithm: Resize::Kaiser {
alpha: 4.0,
freq: 1.0,
},
}
}
pub const fn mitchell_resize() -> Self {
Self {
width: 2.0,
algorithm: Resize::Mitchell {
b: 0.33333333,
c: 0.666_666_7,
},
}
}
pub(crate) fn params(&self) -> [f32; 2] {
let mut params = [0f32; 2];
match self.algorithm {
Resize::Box | Resize::Triangle => (),
Resize::Kaiser { alpha, freq } => {
params[0] = alpha;
params[1] = freq;
}
Resize::Mitchell { b, c } => {
params[0] = b;
params[1] = c;
}
}
params
}
pub(crate) fn params_ptr(&self, params: &[f32; 2]) -> *const f32 {
match self.algorithm {
Resize::Box | Resize::Triangle => std::ptr::null(),
Resize::Kaiser { .. } => params.as_ptr(),
Resize::Mitchell { .. } => params.as_ptr(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Mipmap {
Box,
Triangle,
Kaiser {
alpha: f32,
freq: f32,
},
Min,
Max,
}
impl Mipmap {
pub fn filter_width_default(self) -> f32 {
match self {
Self::Box => 0.5,
Self::Triangle => 1.0,
Self::Kaiser { .. } => 3.0,
Self::Min => 1.0,
Self::Max => 1.0,
}
}
pub fn kaiser_default() -> Self {
Self::Kaiser {
alpha: 4.0,
freq: 1.0,
}
}
}
use nvtt_sys::NvttMipmapFilter;
impl From<Mipmap> for NvttMipmapFilter {
fn from(val: Mipmap) -> Self {
match val {
Mipmap::Box => NvttMipmapFilter::NVTT_MipmapFilter_Box,
Mipmap::Triangle => NvttMipmapFilter::NVTT_MipmapFilter_Triangle,
Mipmap::Kaiser { .. } => NvttMipmapFilter::NVTT_MipmapFilter_Kaiser,
Mipmap::Min => NvttMipmapFilter::NVTT_MipmapFilter_Min,
Mipmap::Max => NvttMipmapFilter::NVTT_MipmapFilter_Max,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Resize {
Box,
Triangle,
Kaiser { alpha: f32, freq: f32 },
Mitchell {
b: f32,
c: f32,
},
}
use nvtt_sys::NvttResizeFilter;
impl From<Resize> for NvttResizeFilter {
fn from(val: Resize) -> Self {
match val {
Resize::Box => NvttResizeFilter::NVTT_ResizeFilter_Box,
Resize::Triangle => NvttResizeFilter::NVTT_ResizeFilter_Box,
Resize::Kaiser { .. } => NvttResizeFilter::NVTT_ResizeFilter_Kaiser,
Resize::Mitchell { .. } => NvttResizeFilter::NVTT_ResizeFilter_Mitchell,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum ToneMapper {
Linear,
Reinhard,
Halo,
}
use nvtt_sys::NvttToneMapper;
impl From<ToneMapper> for NvttToneMapper {
fn from(val: ToneMapper) -> Self {
match val {
ToneMapper::Linear => NvttToneMapper::NVTT_ToneMapper_Linear,
ToneMapper::Reinhard => NvttToneMapper::NVTT_ToneMapper_Reinhard,
ToneMapper::Halo => NvttToneMapper::NVTT_ToneMapper_Halo,
}
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum NormalTransform {
Orthographic,
Stereographic,
Paraboloid,
Quartic,
}
use nvtt_sys::NvttNormalTransform;
impl From<NormalTransform> for NvttNormalTransform {
fn from(val: NormalTransform) -> Self {
match val {
NormalTransform::Orthographic => NvttNormalTransform::NVTT_NormalTransform_Orthographic,
NormalTransform::Stereographic => {
NvttNormalTransform::NVTT_NormalTransform_Stereographic
}
NormalTransform::Paraboloid => NvttNormalTransform::NVTT_NormalTransform_Paraboloid,
NormalTransform::Quartic => NvttNormalTransform::NVTT_NormalTransform_Quartic,
}
}
}
#[repr(i32)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Swizzle {
R = 0,
G = 1,
B = 2,
A = 3,
NegOne = 4,
Zero = 5,
One = 6,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum RoundMode {
None,
ToNextPowerOfTwo,
ToNearestPowerOfTwo,
ToPreviousPowerOfTwo,
}
use nvtt_sys::NvttRoundMode;
impl From<RoundMode> for NvttRoundMode {
fn from(val: RoundMode) -> Self {
match val {
RoundMode::None => NvttRoundMode::NVTT_RoundMode_None,
RoundMode::ToNextPowerOfTwo => NvttRoundMode::NVTT_RoundMode_ToNextPowerOfTwo,
RoundMode::ToNearestPowerOfTwo => NvttRoundMode::NVTT_RoundMode_ToNearestPowerOfTwo,
RoundMode::ToPreviousPowerOfTwo => NvttRoundMode::NVTT_RoundMode_ToPreviousPowerOfTwo,
}
}
}
#[repr(i32)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum CubeFace {
PosX = 0,
NegX = 1,
PosY = 2,
NegY = 3,
PosZ = 4,
NegZ = 5,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum CubeLayout {
VerticalCross,
HorizontalCross,
Column,
Row,
}
impl CubeLayout {
pub fn dim_supported(&self, w: u32, h: u32) -> bool {
match self {
Self::VerticalCross => {
let w_supported = (w % 3 == 0) && w > 0;
let h_supported = (h % 4 == 0) && h > 0;
if w_supported && h_supported {
let w_subimage = w / 3;
let h_subimage = h / 4;
w_subimage == h_subimage
} else {
false
}
}
Self::HorizontalCross => {
let w_supported = (w % 4 == 0) && w > 0;
let h_supported = (h % 3 == 0) && h > 0;
if w_supported && h_supported {
let w_subimage = w / 4;
let h_subimage = h / 3;
w_subimage == h_subimage
} else {
false
}
}
Self::Column => {
let w_supported = w > 0;
let h_supported = (h % 6 == 0) && h > 0;
if w_supported && h_supported {
let w_subimage = w;
let h_subimage = h / 6;
w_subimage == h_subimage
} else {
false
}
}
Self::Row => {
let w_supported = (w % 6 == 0) && w > 0;
let h_supported = h > 0;
if w_supported && h_supported {
let w_subimage = w / 6;
let h_subimage = h;
w_subimage == h_subimage
} else {
false
}
}
}
}
}
impl From<CubeLayout> for nvtt_sys::NvttCubeLayout {
fn from(val: CubeLayout) -> Self {
match val {
CubeLayout::VerticalCross => nvtt_sys::NvttCubeLayout::NVTT_CubeLayout_VerticalCross,
CubeLayout::HorizontalCross => {
nvtt_sys::NvttCubeLayout::NVTT_CubeLayout_HorizontalCross
}
CubeLayout::Column => nvtt_sys::NvttCubeLayout::NVTT_CubeLayout_Column,
CubeLayout::Row => nvtt_sys::NvttCubeLayout::NVTT_CubeLayout_Row,
}
}
}