From 828464c351886aa8a359d0ac1715e15a9f46fc1a Mon Sep 17 00:00:00 2001 From: chyyran Date: Tue, 1 Oct 2024 01:47:25 -0400 Subject: [PATCH] rt(image): helper to load image from shaderpack `TextureBuffer` --- Cargo.lock | 1 + librashader-pack/Cargo.toml | 7 +++++ librashader-pack/src/lib.rs | 2 +- librashader-reflect/src/reflect/presets.rs | 4 +-- librashader-runtime/Cargo.toml | 1 + librashader-runtime/src/image.rs | 32 +++++++++++++++++++--- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 08baf33..87d219b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1707,6 +1707,7 @@ dependencies = [ "bytemuck", "image", "librashader-common", + "librashader-pack", "librashader-preprocess", "librashader-presets", "librashader-reflect", diff --git a/librashader-pack/Cargo.toml b/librashader-pack/Cargo.toml index e64a436..c398150 100644 --- a/librashader-pack/Cargo.toml +++ b/librashader-pack/Cargo.toml @@ -2,6 +2,13 @@ name = "librashader-pack" version = "0.4.5" edition = "2021" +license = "MPL-2.0 OR GPL-3.0-only" +authors = ["Ronny Chan "] +repository = "https://github.com/SnowflakePowered/librashader" +readme = "../README.md" +categories = ["emulators", "compilers", "graphics"] +keywords = ["shader", "retroarch", "SPIR-V"] +description = "RetroArch shaders for all." [dependencies] librashader-presets = { path = "../librashader-presets", version = "0.4.5", features = ["serde"] } diff --git a/librashader-pack/src/lib.rs b/librashader-pack/src/lib.rs index 7f406a1..2ed22e6 100644 --- a/librashader-pack/src/lib.rs +++ b/librashader-pack/src/lib.rs @@ -160,8 +160,8 @@ impl ShaderPresetPack { #[cfg(feature = "serde")] mod serde_base64_or_bytes { use base64::display::Base64Display; - use base64::Engine; use base64::engine::general_purpose::STANDARD; + use base64::Engine; use serde::{Deserializer, Serializer}; #[allow(clippy::ptr_arg)] diff --git a/librashader-reflect/src/reflect/presets.rs b/librashader-reflect/src/reflect/presets.rs index 309cb4e..16c77f1 100644 --- a/librashader-reflect/src/reflect/presets.rs +++ b/librashader-reflect/src/reflect/presets.rs @@ -7,9 +7,7 @@ use crate::reflect::semantics::{ }; use librashader_common::map::{FastHashMap, ShortString}; use librashader_preprocess::{PreprocessError, ShaderSource}; -use librashader_presets::{ - ShaderPassConfig, ShaderPassMeta, ShaderPreset, TextureConfig, -}; +use librashader_presets::{ShaderPassConfig, ShaderPassMeta, ShaderPreset, TextureConfig}; /// Artifacts of a reflected and compiled shader pass. /// diff --git a/librashader-runtime/Cargo.toml b/librashader-runtime/Cargo.toml index e0d1828..04c002b 100644 --- a/librashader-runtime/Cargo.toml +++ b/librashader-runtime/Cargo.toml @@ -15,6 +15,7 @@ description = "RetroArch shaders for all." librashader-common = { path = "../librashader-common", version = "0.4.5" } librashader-presets = { path = "../librashader-presets", 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" } bytemuck = { version = "1.12.3", features = ["derive"] } num-traits = "0.2.15" diff --git a/librashader-runtime/src/image.rs b/librashader-runtime/src/image.rs index 9794a40..656c387 100644 --- a/librashader-runtime/src/image.rs +++ b/librashader-runtime/src/image.rs @@ -2,6 +2,9 @@ pub use image::ImageError; use librashader_common::Size; use std::marker::PhantomData; +use image::error::{LimitError, LimitErrorKind}; +use image::DynamicImage; +use librashader_pack::TextureBuffer; use std::path::Path; /// An uncompressed raw image ready to upload to GPU buffers. @@ -66,13 +69,34 @@ pub enum UVDirection { impl Image

{ /// Load the image from the path as RGBA8. pub fn load(path: impl AsRef, direction: UVDirection) -> Result { - 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 { + 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 { 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 width = image.width(); @@ -83,12 +107,12 @@ impl Image

{ let mut bytes = image.into_raw(); P::convert(&mut bytes); - Ok(Image { + Image { bytes, pitch, size: Size { height, width }, _pd: Default::default(), - }) + } } }