rt(image): helper to load image from shaderpack TextureBuffer

This commit is contained in:
chyyran 2024-10-01 01:47:25 -04:00 committed by Ronny Chan
parent c19593e289
commit 828464c351
6 changed files with 39 additions and 8 deletions

1
Cargo.lock generated
View file

@ -1707,6 +1707,7 @@ dependencies = [
"bytemuck", "bytemuck",
"image", "image",
"librashader-common", "librashader-common",
"librashader-pack",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets", "librashader-presets",
"librashader-reflect", "librashader-reflect",

View file

@ -2,6 +2,13 @@
name = "librashader-pack" name = "librashader-pack"
version = "0.4.5" version = "0.4.5"
edition = "2021" edition = "2021"
license = "MPL-2.0 OR GPL-3.0-only"
authors = ["Ronny Chan <ronny@ronnychan.ca>"]
repository = "https://github.com/SnowflakePowered/librashader"
readme = "../README.md"
categories = ["emulators", "compilers", "graphics"]
keywords = ["shader", "retroarch", "SPIR-V"]
description = "RetroArch shaders for all."
[dependencies] [dependencies]
librashader-presets = { path = "../librashader-presets", version = "0.4.5", features = ["serde"] } librashader-presets = { path = "../librashader-presets", version = "0.4.5", features = ["serde"] }

View file

@ -160,8 +160,8 @@ impl ShaderPresetPack {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
mod serde_base64_or_bytes { mod serde_base64_or_bytes {
use base64::display::Base64Display; use base64::display::Base64Display;
use base64::Engine;
use base64::engine::general_purpose::STANDARD; use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use serde::{Deserializer, Serializer}; use serde::{Deserializer, Serializer};
#[allow(clippy::ptr_arg)] #[allow(clippy::ptr_arg)]

View file

@ -7,9 +7,7 @@ use crate::reflect::semantics::{
}; };
use librashader_common::map::{FastHashMap, ShortString}; use librashader_common::map::{FastHashMap, ShortString};
use librashader_preprocess::{PreprocessError, ShaderSource}; use librashader_preprocess::{PreprocessError, ShaderSource};
use librashader_presets::{ use librashader_presets::{ShaderPassConfig, ShaderPassMeta, ShaderPreset, TextureConfig};
ShaderPassConfig, ShaderPassMeta, ShaderPreset, TextureConfig,
};
/// Artifacts of a reflected and compiled shader pass. /// Artifacts of a reflected and compiled shader pass.
/// ///

View file

@ -15,6 +15,7 @@ description = "RetroArch shaders for all."
librashader-common = { path = "../librashader-common", version = "0.4.5" } librashader-common = { path = "../librashader-common", version = "0.4.5" }
librashader-presets = { path = "../librashader-presets", version = "0.4.5" } librashader-presets = { path = "../librashader-presets", version = "0.4.5" }
librashader-preprocess = { path = "../librashader-preprocess", version = "0.4.5" } librashader-preprocess = { path = "../librashader-preprocess", version = "0.4.5" }
librashader-pack = { path = "../librashader-pack", version = "0.4.5" }
librashader-reflect = { path = "../librashader-reflect", version = "0.4.5" } librashader-reflect = { path = "../librashader-reflect", version = "0.4.5" }
bytemuck = { version = "1.12.3", features = ["derive"] } bytemuck = { version = "1.12.3", features = ["derive"] }
num-traits = "0.2.15" num-traits = "0.2.15"

View file

@ -2,6 +2,9 @@ pub use image::ImageError;
use librashader_common::Size; use librashader_common::Size;
use std::marker::PhantomData; use std::marker::PhantomData;
use image::error::{LimitError, LimitErrorKind};
use image::DynamicImage;
use librashader_pack::TextureBuffer;
use std::path::Path; use std::path::Path;
/// An uncompressed raw image ready to upload to GPU buffers. /// An uncompressed raw image ready to upload to GPU buffers.
@ -66,13 +69,34 @@ pub enum UVDirection {
impl<P: PixelFormat> Image<P> { impl<P: PixelFormat> Image<P> {
/// Load the image from the path as RGBA8. /// Load the image from the path as RGBA8.
pub fn load(path: impl AsRef<Path>, direction: UVDirection) -> Result<Self, ImageError> { pub fn load(path: impl AsRef<Path>, direction: UVDirection) -> Result<Self, ImageError> {
let mut image = image::open(path.as_ref())?; let image = image::open(path.as_ref())?;
Ok(Self::convert(image, direction))
}
/// Load te image from a [`TextureBuffer`] from a [`ShaderPresetPack`](librashader_pack::ShaderPresetPack).
pub fn load_from_buffer(
buffer: TextureBuffer,
direction: UVDirection,
) -> Result<Self, ImageError> {
let Some(image) = buffer.into() else {
return Err(ImageError::Limits(LimitError::from_kind(
LimitErrorKind::InsufficientMemory,
)));
};
let image = DynamicImage::ImageRgba8(image);
Ok(Self::convert(image, direction))
}
fn convert(mut image: DynamicImage, direction: UVDirection) -> Self {
if direction == UVDirection::BottomLeft { if direction == UVDirection::BottomLeft {
image = image.flipv(); image = image.flipv();
} }
let image = image.to_rgba8(); let image = if let DynamicImage::ImageRgba8(image) = image {
image
} else {
image.to_rgba8()
};
let height = image.height(); let height = image.height();
let width = image.width(); let width = image.width();
@ -83,12 +107,12 @@ impl<P: PixelFormat> Image<P> {
let mut bytes = image.into_raw(); let mut bytes = image.into_raw();
P::convert(&mut bytes); P::convert(&mut bytes);
Ok(Image { Image {
bytes, bytes,
pitch, pitch,
size: Size { height, width }, size: Size { height, width },
_pd: Default::default(), _pd: Default::default(),
}) }
} }
} }