librashader/librashader-common/src/lib.rs

217 lines
5.2 KiB
Rust
Raw Normal View History

2023-01-16 23:09:07 -05:00
//! Common types and conversions for librashader.
2022-11-30 22:50:57 -05:00
/// OpenGL common conversions.
2022-11-30 01:38:05 -05:00
#[cfg(feature = "opengl")]
pub mod gl;
2022-12-05 21:01:15 -05:00
/// Vulkan common conversions.
#[cfg(feature = "vulkan")]
pub mod vk;
2023-11-30 01:49:22 -05:00
/// WGPU common conversions.
#[cfg(feature = "wgpu")]
pub mod wgpu;
2022-11-30 22:50:57 -05:00
/// DXGI common conversions.
#[cfg(all(target_os = "windows", feature = "dxgi"))]
2022-11-30 19:10:04 -05:00
pub mod dxgi;
2024-03-03 01:07:07 -05:00
/// Direct3D 9 common conversions.
#[cfg(all(target_os = "windows", feature = "d3d9"))]
pub mod d3d9;
2022-11-30 22:50:57 -05:00
/// Direct3D 11 common conversions.
#[cfg(all(target_os = "windows", feature = "d3d11"))]
2022-11-30 22:50:57 -05:00
pub mod d3d11;
2023-01-24 02:02:27 -05:00
/// Direct3D 12 common conversions.
#[cfg(all(target_os = "windows", feature = "d3d12"))]
pub mod d3d12;
2024-02-10 00:04:16 -05:00
#[cfg(all(target_vendor = "apple", feature = "metal"))]
2024-02-09 18:22:14 -05:00
pub mod metal;
2023-01-13 02:54:16 -05:00
mod viewport;
2023-11-30 01:49:22 -05:00
#[doc(hidden)]
pub mod map;
2023-01-13 02:54:16 -05:00
pub use viewport::Viewport;
2022-11-30 01:38:05 -05:00
use num_traits::AsPrimitive;
2022-10-21 21:04:00 -04:00
use std::convert::Infallible;
use std::str::FromStr;
#[repr(u32)]
2022-11-26 15:55:14 -05:00
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Hash)]
2022-11-30 22:50:57 -05:00
/// Supported image formats for textures.
2022-11-29 01:57:04 -05:00
pub enum ImageFormat {
2022-10-21 23:37:47 -04:00
#[default]
2022-10-21 21:04:00 -04:00
Unknown = 0,
/* 8-bit */
R8Unorm,
R8Uint,
R8Sint,
R8G8Unorm,
R8G8Uint,
R8G8Sint,
R8G8B8A8Unorm,
R8G8B8A8Uint,
R8G8B8A8Sint,
R8G8B8A8Srgb,
/* 10-bit */
A2B10G10R10UnormPack32,
A2B10G10R10UintPack32,
/* 16-bit */
R16Uint,
R16Sint,
R16Sfloat,
R16G16Uint,
R16G16Sint,
R16G16Sfloat,
R16G16B16A16Uint,
R16G16B16A16Sint,
R16G16B16A16Sfloat,
/* 32-bit */
R32Uint,
R32Sint,
R32Sfloat,
R32G32Uint,
R32G32Sint,
R32G32Sfloat,
R32G32B32A32Uint,
R32G32B32A32Sint,
R32G32B32A32Sfloat,
}
2022-11-14 00:14:05 -05:00
#[repr(i32)]
2022-11-26 15:55:14 -05:00
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
2022-11-30 22:50:57 -05:00
/// The filtering mode for a texture sampler.
2022-11-14 00:14:05 -05:00
pub enum FilterMode {
2022-11-30 22:50:57 -05:00
/// Linear filtering.
2022-11-14 00:14:05 -05:00
Linear = 0,
2022-11-30 22:50:57 -05:00
#[default]
2022-11-30 22:50:57 -05:00
/// Nearest-neighbour (point) filtering.
2022-11-14 00:14:05 -05:00
Nearest,
}
impl FromStr for WrapMode {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"clamp_to_border" => WrapMode::ClampToBorder,
"clamp_to_edge" => WrapMode::ClampToEdge,
"repeat" => WrapMode::Repeat,
"mirrored_repeat" => WrapMode::MirroredRepeat,
_ => WrapMode::ClampToBorder,
})
}
}
impl FromStr for FilterMode {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"linear" => FilterMode::Linear,
"nearest" => FilterMode::Nearest,
_ => FilterMode::Nearest,
})
}
}
2022-11-14 00:14:05 -05:00
#[repr(i32)]
2022-11-26 15:55:14 -05:00
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)]
2022-11-30 22:50:57 -05:00
/// The wrapping (address) mode for a texture sampler.
2022-11-14 00:14:05 -05:00
pub enum WrapMode {
#[default]
2022-11-30 22:50:57 -05:00
/// Clamp txture to border.
2022-11-14 00:14:05 -05:00
ClampToBorder = 0,
2022-11-30 22:50:57 -05:00
/// Clamp texture to edge.
2022-11-14 00:14:05 -05:00
ClampToEdge,
2022-11-30 22:50:57 -05:00
/// Repeat addressing mode.
2022-11-14 00:14:05 -05:00
Repeat,
2022-11-30 22:50:57 -05:00
/// Mirrored repeat addressing mode.
2022-11-14 00:14:05 -05:00
MirroredRepeat,
}
2022-11-29 01:57:04 -05:00
impl FromStr for ImageFormat {
2022-10-21 21:04:00 -04:00
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"UNKNOWN" => Self::Unknown,
"R8_UNORM" => Self::R8Unorm,
"R8_UINT" => Self::R8Uint,
"R8_SINT" => Self::R8Sint,
"R8G8_UNORM" => Self::R8G8Unorm,
"R8G8_UINT" => Self::R8Uint,
"R8G8_SINT" => Self::R8G8Sint,
"R8G8B8A8_UNORM" => Self::R8G8B8A8Unorm,
"R8G8B8A8_UINT" => Self::R8G8B8A8Uint,
"R8G8B8A8_SINT" => Self::R8G8B8A8Sint,
"R8G8B8A8_SRGB" => Self::R8G8B8A8Srgb,
"A2B10G10R10_UNORM_PACK32" => Self::A2B10G10R10UnormPack32,
"A2B10G10R10_UINT_PACK32" => Self::A2B10G10R10UintPack32,
"R16_UINT" => Self::R16Uint,
"R16_SINT" => Self::R16Sint,
"R16_SFLOAT" => Self::R16Sfloat,
"R16G16_UINT" => Self::R16G16Uint,
"R16G16_SINT" => Self::R16G16Sint,
"R16G16_SFLOAT" => Self::R16G16Sfloat,
"R16G16B16A16_UINT" => Self::R16G16B16A16Uint,
"R16G16B16A16_SINT" => Self::R16G16B16A16Sint,
"R16G16B16A16_SFLOAT" => Self::R16G16B16A16Sfloat,
"R32_UINT" => Self::R32Uint,
"R32_SINT" => Self::R32Sint,
"R32_SFLOAT" => Self::R32Sfloat,
"R32G32_UINT" => Self::R32G32Uint,
"R32G32_SINT" => Self::R32G32Sint,
"R32G32_SFLOAT" => Self::R32G32Sfloat,
2022-10-21 23:37:47 -04:00
"R32G32B32A32_UINT" => Self::R32G32B32A32Uint,
"R32G32B32A32_SINT" => Self::R32G32B32A32Sint,
"R32G32B32A32_SFLOAT" => Self::R32G32B32A32Sfloat,
2022-10-21 21:04:00 -04:00
_ => Self::Unknown,
})
}
}
2022-11-21 03:01:26 -05:00
2022-11-30 22:50:57 -05:00
/// A size with a width and height.
2022-11-21 03:01:26 -05:00
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Size<T> {
pub width: T,
pub height: T,
}
2022-11-21 16:21:50 -05:00
impl<T> Size<T> {
2022-11-30 22:50:57 -05:00
/// Create a new `Size<T>` with the given width and height.
2022-11-21 03:01:26 -05:00
pub fn new(width: T, height: T) -> Self {
2022-11-21 16:21:50 -05:00
Size { width, height }
2022-11-21 03:01:26 -05:00
}
2022-11-21 16:21:50 -05:00
}
impl<T> From<Size<T>> for [f32; 4]
2022-11-30 01:38:05 -05:00
where
T: Copy + AsPrimitive<f32>,
{
2022-11-30 22:50:57 -05:00
/// Convert a `Size<T>` to a `vec4` uniform.
fn from(value: Size<T>) -> Self {
[
value.width.as_(),
value.height.as_(),
1.0 / value.width.as_(),
1.0 / value.height.as_(),
]
}
}