build: fix cbindgen not finding ctypes

* removes capi internal interface from librashader crate
* adds missing noop impls in header
* fix build on rustc < 1.74
This commit is contained in:
chyyran 2024-02-09 17:48:20 -05:00
parent 8f89b3e720
commit fa8ee5d143
23 changed files with 595 additions and 1715 deletions

11
Cargo.lock generated
View file

@ -1780,8 +1780,10 @@ dependencies = [
"nom_locate",
"num-traits",
"once_cell",
"os_str_bytes",
"regex",
"rustc-hash",
"rustversion",
"thiserror",
]
@ -2443,6 +2445,9 @@ name = "os_str_bytes"
version = "6.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1"
dependencies = [
"memchr",
]
[[package]]
name = "owned_ttf_parser"
@ -2893,6 +2898,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rustversion"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
[[package]]
name = "ryu"
version = "1.0.16"

View file

@ -107,38 +107,25 @@ enum LIBRA_PRESET_CTX_RUNTIME
typedef uint32_t LIBRA_PRESET_CTX_RUNTIME;
#endif // __cplusplus
/// A Direct3D 11 filter chain.
/// Opaque struct for a Direct3D 11 filter chain.
typedef struct _filter_chain_d3d11 _filter_chain_d3d11;
/// A Direct3D 12 filter chain.
/// Opaque struct for a Direct3D 12 filter chain.
typedef struct _filter_chain_d3d12 _filter_chain_d3d12;
/// An OpenGL filter chain.
/// Opaque struct for an OpenGL filter chain.
typedef struct _filter_chain_gl _filter_chain_gl;
/// A Vulkan filter chain.
/// Opaque struct for a Vulkan filter chain.
typedef struct _filter_chain_vk _filter_chain_vk;
/// The error type for librashader.
/// The error type for librashader C API.
typedef struct _libra_error _libra_error;
/// A shader preset including all specified parameters, textures, and paths to
/// specified shaders.
///
/// A shader preset can be used to create a filter chain runtime instance, or
/// reflected to get parameter metadata.
/// Opaque struct for a shader preset.
typedef struct _shader_preset _shader_preset;
/// A preset wildcard context.
///
/// Any items added after will have higher priority
/// when passed to the shader preset parser.
///
/// When passed to the preset parser, the preset parser
/// will automatically add inferred items at lowest priority.
///
/// Any items added by the user will override the automatically
/// inferred items.
/// Opaque struct for a preset context.
typedef struct _preset_ctx _preset_ctx;
/// A handle to a librashader error object.

View file

@ -1132,7 +1132,30 @@ libra_instance_t __librashader_make_null_instance() {
.instance_abi_version = __librashader__noop_instance_abi_version,
.instance_api_version = __librashader__noop_instance_api_version,
.preset_ctx_create = __librashader__noop_preset_ctx_create,
.preset_ctx_free = __librashader__noop_preset_ctx_free,
.preset_ctx_set_core_name =
__librashader__noop_preset_ctx_set_core_name,
.preset_ctx_set_content_dir =
__librashader__noop_preset_ctx_set_content_dir,
.preset_ctx_set_param = __librashader__noop_preset_ctx_set_param,
.preset_ctx_set_runtime = __librashader__noop_preset_ctx_set_runtime,
.preset_ctx_set_core_rotation =
__librashader__noop_preset_ctx_set_core_rotation,
.preset_ctx_set_user_rotation =
__librashader__noop_preset_ctx_set_user_rotation,
.preset_ctx_set_screen_orientation =
__librashader__noop_preset_ctx_set_screen_orientation,
.preset_ctx_set_allow_rotation =
__librashader__noop_preset_ctx_set_allow_rotation,
.preset_ctx_set_view_aspect_orientation =
__librashader__noop_preset_ctx_set_view_aspect_orientation,
.preset_ctx_set_core_aspect_orientation =
__librashader__noop_preset_ctx_set_core_aspect_orientation,
.preset_create = __librashader__noop_preset_create,
.preset_create_with_context =
__librashader__noop_preset_create_with_context,
.preset_free = __librashader__noop_preset_free,
.preset_set_param = __librashader__noop_preset_set_param,
.preset_get_param = __librashader__noop_preset_get_param,

View file

@ -23,7 +23,7 @@ runtime-d3d12 = ["windows", "librashader/runtime-d3d12", "windows/Win32_Graphics
runtime-vulkan = ["ash", "librashader/runtime-vk"]
[dependencies]
librashader = { path = "../librashader", version = "0.2.0-beta.8", features = ["internal"] }
librashader = { path = "../librashader", version = "0.2.0-beta.8" }
thiserror = "1.0.37"
paste = "1.0.9"
gl = { version = "0.14.0", optional = true }

View file

@ -68,15 +68,7 @@ typedef void D3D12_CPU_DESCRIPTOR_HANDLE;
[parse]
parse_deps = true
include = ["librashader",
"librashader-presets",
"librashader-preprocess",
"librashader-reflect",
"librashader-runtime-gl",
"librashader-runtime-vk",
"librashader-runtime-d3d11",
"librashader-runtime-d3d12",
]
include = ["librashader"]
expand = ["librashader-capi"]
[struct]
@ -161,8 +153,6 @@ include = [
"PFN_libra_d3d12_filter_chain_set_active_pass_count",
"PFN_libra_d3d12_filter_chain_get_active_pass_count",
"PFN_libra_d3d12_filter_chain_free",
"WildcardPresetContext"
]
exclude = ["Option_ID3D11DeviceContext"]
@ -171,9 +161,7 @@ exclude = ["Option_ID3D11DeviceContext"]
"LibrashaderError" = "_libra_error"
"ShaderPreset" = "_shader_preset"
# I don't know why its literally just WildcardContext??
"WildcardContext" = "_preset_ctx"
"WildcardPresetContext" = "_aaaa_preset_ctx"
"FilterChainGL" = "_filter_chain_gl"
"FilterChainVulkan" = "_filter_chain_vk"

File diff suppressed because it is too large Load diff

View file

@ -55,28 +55,39 @@ impl From<LIBRA_PRESET_CTX_RUNTIME> for VideoDriver {
}
}
#[cfg(feature = "runtime-opengl")]
use librashader::runtime::gl::FilterChain as FilterChainGL;
/// A handle to a OpenGL filter chain.
#[cfg(feature = "runtime-opengl")]
#[doc(cfg(feature = "runtime-opengl"))]
pub type libra_gl_filter_chain_t = Option<NonNull<librashader::runtime::gl::capi::FilterChainGL>>;
pub type libra_gl_filter_chain_t = Option<NonNull<FilterChainGL>>;
/// A handle to a Direct3D 11 filter chain.
#[cfg(all(target_os = "windows", feature = "runtime-d3d11"))]
use librashader::runtime::d3d11::FilterChain as FilterChainD3D11;
/// A handle to a Direct3D 11 filter chain.
#[cfg(all(target_os = "windows", feature = "runtime-d3d11"))]
#[doc(cfg(all(target_os = "windows", feature = "runtime-d3d11")))]
pub type libra_d3d11_filter_chain_t =
Option<NonNull<librashader::runtime::d3d11::capi::FilterChainD3D11>>;
Option<NonNull<FilterChainD3D11>>;
#[cfg(all(target_os = "windows", feature = "runtime-d3d12"))]
use librashader::runtime::d3d12::FilterChain as FilterChainD3D12;
/// A handle to a Direct3D 12 filter chain.
#[cfg(all(target_os = "windows", feature = "runtime-d3d12"))]
#[doc(cfg(all(target_os = "windows", feature = "runtime-d3d12")))]
pub type libra_d3d12_filter_chain_t =
Option<NonNull<librashader::runtime::d3d12::capi::FilterChainD3D12>>;
Option<NonNull<FilterChainD3D12>>;
#[cfg(feature = "runtime-vulkan")]
use librashader::runtime::vk::FilterChain as FilterChainVulkan;
/// A handle to a Vulkan filter chain.
#[cfg(feature = "runtime-vulkan")]
#[doc(cfg(feature = "runtime-vulkan"))]
pub type libra_vk_filter_chain_t =
Option<NonNull<librashader::runtime::vk::capi::FilterChainVulkan>>;
Option<NonNull<FilterChainVulkan>>;
/// Defines the output viewport for a rendered frame.
#[repr(C)]
@ -132,3 +143,35 @@ macro_rules! config_struct {
pub(crate) use config_set_field;
pub(crate) use config_struct;
pub(crate) use config_version_set;
#[doc(hidden)]
#[deny(deprecated)]
#[deprecated = "Forward declarations for cbindgen, do not use."]
mod __cbindgen_opaque_forward_declarations {
macro_rules! typedef_struct {
($($(#[$($attrss:tt)*])* $name:ident;)*) => {
$($(#[$($attrss)*])*
#[allow(unused)]
#[doc(hidden)]
#[deny(deprecated)]
#[deprecated]
pub struct $name;
)*
};
}
typedef_struct! {
/// Opaque struct for a preset context.
WildcardContext;
/// Opaque struct for a shader preset.
ShaderPreset;
/// Opaque struct for an OpenGL filter chain.
FilterChainGL;
/// Opaque struct for a Direct3D 11 filter chain.
FilterChainD3D11;
/// Opaque struct for a Direct3D 12 filter chain.
FilterChainD3D12;
/// Opaque struct for a Vulkan filter chain.
FilterChainVulkan;
}
}

View file

@ -5,7 +5,7 @@ use std::mem::MaybeUninit;
use std::ptr::NonNull;
use thiserror::Error;
/// The error type for librashader.
/// The error type for librashader C API.
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum LibrashaderError {

View file

@ -68,6 +68,7 @@
#![feature(pointer_is_aligned)]
#![feature(vec_into_raw_parts)]
#![deny(unsafe_op_in_unsafe_fn)]
#![deny(deprecated)]
pub mod ctypes;
pub mod error;

View file

@ -3,7 +3,7 @@ use crate::ctypes::{
};
use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError};
use crate::ffi::extern_fn;
use librashader::runtime::d3d11::{D3D11InputView, D3D11OutputView};
use librashader::runtime::d3d11::{FilterChain, FilterChainOptions, FrameOptions, D3D11InputView, D3D11OutputView};
use std::ffi::c_char;
use std::ffi::CStr;
use std::mem::{ManuallyDrop, MaybeUninit};
@ -13,9 +13,6 @@ use windows::Win32::Graphics::Direct3D11::{
ID3D11Device, ID3D11DeviceContext, ID3D11RenderTargetView, ID3D11ShaderResourceView,
};
use librashader::runtime::d3d11::capi::options::FilterChainOptionsD3D11;
use librashader::runtime::d3d11::capi::options::FrameOptionsD3D11;
use crate::LIBRASHADER_API_VERSION;
use librashader::runtime::{FilterChainParameters, Size, Viewport};
@ -58,7 +55,7 @@ pub struct filter_chain_d3d11_opt_t {
}
config_struct! {
impl FilterChainOptionsD3D11 => filter_chain_d3d11_opt_t {
impl FilterChainOptions => filter_chain_d3d11_opt_t {
0 => [force_no_mipmaps, disable_cache];
}
}
@ -77,7 +74,7 @@ pub struct frame_d3d11_opt_t {
}
config_struct! {
impl FrameOptionsD3D11 => frame_d3d11_opt_t {
impl FrameOptions => frame_d3d11_opt_t {
0 => [clear_history, frame_direction];
}
}
@ -114,7 +111,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::d3d11::capi::FilterChainD3D11::load_from_preset(
let chain = FilterChain::load_from_preset(
*preset,
&device,
options.as_ref(),
@ -174,7 +171,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::d3d11::capi::FilterChainD3D11::load_from_preset_deferred(
let chain = FilterChain::load_from_preset_deferred(
*preset,
&device,
&device_context,

View file

@ -13,11 +13,8 @@ use windows::Win32::Graphics::Direct3D12::{
};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
use librashader::runtime::d3d12::capi::options::FilterChainOptionsD3D12;
use librashader::runtime::d3d12::capi::options::FrameOptionsD3D12;
use crate::LIBRASHADER_API_VERSION;
use librashader::runtime::d3d12::{D3D12InputImage, D3D12OutputView};
use librashader::runtime::d3d12::{FilterChain, FilterChainOptions, FrameOptions, D3D12InputImage, D3D12OutputView};
use librashader::runtime::{FilterChainParameters, Size, Viewport};
/// Direct3D 12 parameters for the source image.
@ -58,7 +55,7 @@ pub struct frame_d3d12_opt_t {
}
config_struct! {
impl FrameOptionsD3D12 => frame_d3d12_opt_t {
impl FrameOptions => frame_d3d12_opt_t {
0 => [clear_history, frame_direction];
}
}
@ -84,7 +81,7 @@ pub struct filter_chain_d3d12_opt_t {
}
config_struct! {
impl FilterChainOptionsD3D12 => filter_chain_d3d12_opt_t {
impl FilterChainOptions => filter_chain_d3d12_opt_t {
0 => [force_hlsl_pipeline, force_no_mipmaps, disable_cache];
}
}
@ -136,7 +133,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::d3d12::capi::FilterChainD3D12::load_from_preset(
let chain = FilterChain::load_from_preset(
*preset,
&device,
options.as_ref(),
@ -188,7 +185,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::d3d12::capi::FilterChainD3D12::load_from_preset_deferred(
let chain = FilterChain::load_from_preset_deferred(
*preset,
&device,
&command_list,

View file

@ -3,7 +3,7 @@ use crate::ctypes::{
};
use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError};
use crate::ffi::extern_fn;
use librashader::runtime::gl::{GLFramebuffer, GLImage};
use librashader::runtime::gl::{FilterChain, FilterChainOptions, FrameOptions, GLFramebuffer, GLImage};
use std::ffi::CStr;
use std::ffi::{c_char, c_void, CString};
use std::mem::MaybeUninit;
@ -11,8 +11,6 @@ use std::ptr::NonNull;
use std::slice;
use crate::LIBRASHADER_API_VERSION;
use librashader::runtime::gl::capi::options::FilterChainOptionsGL;
use librashader::runtime::gl::capi::options::FrameOptionsGL;
use librashader::runtime::FilterChainParameters;
use librashader::runtime::{Size, Viewport};
@ -67,7 +65,7 @@ pub struct frame_gl_opt_t {
}
config_struct! {
impl FrameOptionsGL => frame_gl_opt_t {
impl FrameOptions => frame_gl_opt_t {
0 => [clear_history, frame_direction];
}
}
@ -92,7 +90,7 @@ pub struct filter_chain_gl_opt_t {
}
config_struct! {
impl FilterChainOptionsGL => filter_chain_gl_opt_t {
impl FilterChainOptions => filter_chain_gl_opt_t {
0 => [glsl_version, use_dsa, force_no_mipmaps, disable_cache];
}
}
@ -149,7 +147,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::gl::capi::FilterChainGL::load_from_preset(*preset, options.as_ref())?;
let chain = FilterChain::load_from_preset(*preset, options.as_ref())?;
out.write(MaybeUninit::new(NonNull::new(Box::into_raw(Box::new(
chain,

View file

@ -3,15 +3,13 @@ use crate::ctypes::{
};
use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError};
use crate::ffi::extern_fn;
use librashader::runtime::vk::{VulkanImage, VulkanInstance};
use librashader::runtime::vk::{FrameOptions, FilterChainOptions, FilterChain, VulkanImage, VulkanInstance};
use std::ffi::CStr;
use std::ffi::{c_char, c_void};
use std::mem::MaybeUninit;
use std::ptr::NonNull;
use std::slice;
use librashader::runtime::vk::capi::options::FilterChainOptionsVulkan;
use librashader::runtime::vk::capi::options::FrameOptionsVulkan;
use librashader::runtime::FilterChainParameters;
use librashader::runtime::{Size, Viewport};
@ -96,7 +94,7 @@ pub struct frame_vk_opt_t {
}
config_struct! {
impl FrameOptionsVulkan => frame_vk_opt_t {
impl FrameOptions => frame_vk_opt_t {
0 => [clear_history, frame_direction];
}
}
@ -120,7 +118,7 @@ pub struct filter_chain_vk_opt_t {
}
config_struct! {
impl FilterChainOptionsVulkan => filter_chain_vk_opt_t {
impl FilterChainOptions => filter_chain_vk_opt_t {
0 => [frames_in_flight, force_no_mipmaps, use_render_pass, disable_cache];
}
}
@ -161,7 +159,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::vk::capi::FilterChainVulkan::load_from_preset(*preset, vulkan, options.as_ref())?;
let chain = FilterChain::load_from_preset(*preset, vulkan, options.as_ref())?;
out.write(MaybeUninit::new(NonNull::new(Box::into_raw(Box::new(
chain,
@ -212,7 +210,7 @@ extern_fn! {
let options = options.map(FromUninit::from_uninit);
unsafe {
let chain = librashader::runtime::vk::capi::FilterChainVulkan::load_from_preset_deferred(*preset,
let chain = FilterChain::load_from_preset_deferred(*preset,
vulkan,
command_buffer,
options.as_ref())?;

View file

@ -21,6 +21,10 @@ once_cell = "1"
# we don't need unicode
regex = { version = "1", default-features = false, features = ["perf"] }
rustc-hash = "1.1.0"
rustversion = "1.0"
os_str_bytes = { version = "6", features = ["conversions"] }
[features]
parse_legacy_glsl = []

View file

@ -0,0 +1,460 @@
// pub use librashader_presets_context::*;
//! Shader preset wildcard replacement context handling.
//!
//! Implements wildcard replacement of shader paths specified in
//! [RetroArch#15023](https://github.com/libretro/RetroArch/pull/15023).
use once_cell::sync::Lazy;
use regex::bytes::Regex;
use rustc_hash::FxHashMap;
use std::collections::VecDeque;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Add;
use std::path::{Component, Path, PathBuf};
/// Valid video driver or runtime. This list is non-exhaustive.
#[repr(u32)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum VideoDriver {
/// None (`null`)
None = 0,
/// OpenGL Core (`glcore`)
GlCore,
/// Legacy OpenGL (`gl`)
Gl,
/// Vulkan (`vulkan`)
Vulkan,
/// Direct3D 9 (`d3d9_hlsl`)
Direct3D9Hlsl,
/// Direct3D 11 (`d3d11`)
Direct3D11,
/// Direct3D12 (`d3d12`)
Direct3D12,
/// Metal (`metal`)
Metal,
}
impl Display for VideoDriver {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
VideoDriver::None => f.write_str("null"),
VideoDriver::GlCore => f.write_str("glcore"),
VideoDriver::Gl => f.write_str("gl"),
VideoDriver::Vulkan => f.write_str("vulkan"),
VideoDriver::Direct3D11 => f.write_str("d3d11"),
VideoDriver::Direct3D9Hlsl => f.write_str("d3d9_hlsl"),
VideoDriver::Direct3D12 => f.write_str("d3d12"),
VideoDriver::Metal => f.write_str("metal"),
}
}
}
/// Valid extensions for shader extensions.
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum ShaderExtension {
/// `.slang`
Slang = 0,
/// `.glsl`
Glsl,
/// `.cg`
Cg,
}
impl Display for ShaderExtension {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ShaderExtension::Slang => f.write_str("slang"),
ShaderExtension::Glsl => f.write_str("glsl"),
ShaderExtension::Cg => f.write_str("cg"),
}
}
}
/// Valid extensions for shader presets
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum PresetExtension {
/// `.slangp`
Slangp = 0,
/// `.glslp`
Glslp,
/// `.cgp`
Cgp,
}
impl Display for PresetExtension {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PresetExtension::Slangp => f.write_str("slangp"),
PresetExtension::Glslp => f.write_str("glslp"),
PresetExtension::Cgp => f.write_str("cgp"),
}
}
}
/// Rotation of the viewport.
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum Rotation {
/// Zero
Zero = 0,
/// 90 degrees
Right = 1,
/// 180 degrees
Straight = 2,
/// 270 degrees
Reflex = 3,
}
impl From<u32> for Rotation {
fn from(value: u32) -> Self {
let value = value % 4;
match value {
0 => Rotation::Zero,
1 => Rotation::Right,
2 => Rotation::Straight,
3 => Rotation::Reflex,
_ => unreachable!(),
}
}
}
impl Add for Rotation {
type Output = Rotation;
fn add(self, rhs: Self) -> Self::Output {
let lhs = self as u32;
let out = lhs + rhs as u32;
Rotation::from(out)
}
}
impl Display for Rotation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Rotation::Zero => f.write_str("0"),
Rotation::Right => f.write_str("90"),
Rotation::Straight => f.write_str("180"),
Rotation::Reflex => f.write_str("270"),
}
}
}
/// Orientation of aspect ratios
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
pub enum Orientation {
/// Vertical orientation.
Vertical = 0,
/// Horizontal orientation.
Horizontal,
}
impl Display for Orientation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Orientation::Vertical => f.write_str("VERT"),
Orientation::Horizontal => f.write_str("HORZ"),
}
}
}
/// An item representing a variable that can be replaced in a path preset.
#[derive(Debug, Clone)]
pub enum ContextItem {
/// The content directory of the game (`CONTENT-DIR`)
ContentDirectory(String),
/// The name of the libretro core (`CORE`)
CoreName(String),
/// The filename of the game (`GAME`)
GameName(String),
/// The name of the preset (`PRESET`)
Preset(String),
/// The name of the preset directory (`PRESET_DIR`)
PresetDirectory(String),
/// The video driver (runtime) (`VID-DRV`)
VideoDriver(VideoDriver),
/// The extension of shader types supported by the driver (`VID-DRV-SHADER-EXT`)
VideoDriverShaderExtension(ShaderExtension),
/// The extension of shader presets supported by the driver (`VID-DRV-PRESET-EXT`)
VideoDriverPresetExtension(PresetExtension),
/// The rotation that the core is requesting (`CORE-REQ-ROT`)
CoreRequestedRotation(Rotation),
/// Whether or not to allow core-requested rotation (`VID-ALLOW-CORE-ROT`)
AllowCoreRotation(bool),
/// The rotation the user is requesting (`VID-USER-ROT`)
UserRotation(Rotation),
/// The final rotation (`VID-FINAL-ROT`) calculated by the sum of `VID-USER-ROT` and `CORE-REQ-ROT`
FinalRotation(Rotation),
/// The user-adjusted screen orientation (`SCREEN-ORIENT`)
ScreenOrientation(Rotation),
/// The orientation of the viewport aspect ratio (`VIEW-ASPECT-ORIENT`)
ViewAspectOrientation(Orientation),
/// The orientation of the aspect ratio requested by the core (`CORE-ASPECT-ORIENT`)
CoreAspectOrientation(Orientation),
/// An external, arbitrary context variable.
ExternContext(String, String),
}
impl ContextItem {
fn toggle_str(v: bool) -> &'static str {
if v {
"ON"
} else {
"OFF"
}
}
pub fn key(&self) -> &str {
match self {
ContextItem::ContentDirectory(_) => "CONTENT-DIR",
ContextItem::CoreName(_) => "CORE",
ContextItem::GameName(_) => "GAME",
ContextItem::Preset(_) => "PRESET",
ContextItem::PresetDirectory(_) => "PRESET_DIR",
ContextItem::VideoDriver(_) => "VID-DRV",
ContextItem::CoreRequestedRotation(_) => "CORE-REQ-ROT",
ContextItem::AllowCoreRotation(_) => "VID-ALLOW-CORE-ROT",
ContextItem::UserRotation(_) => "VID-USER-ROT",
ContextItem::FinalRotation(_) => "VID-FINAL-ROT",
ContextItem::ScreenOrientation(_) => "SCREEN-ORIENT",
ContextItem::ViewAspectOrientation(_) => "VIEW-ASPECT-ORIENT",
ContextItem::CoreAspectOrientation(_) => "CORE-ASPECT-ORIENT",
ContextItem::VideoDriverShaderExtension(_) => "VID-DRV-SHADER-EXT",
ContextItem::VideoDriverPresetExtension(_) => "VID-DRV-PRESET-EXT",
ContextItem::ExternContext(key, _) => key,
}
}
}
impl Display for ContextItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ContextItem::ContentDirectory(v) => f.write_str(v),
ContextItem::CoreName(v) => f.write_str(v),
ContextItem::GameName(v) => f.write_str(v),
ContextItem::Preset(v) => f.write_str(v),
ContextItem::PresetDirectory(v) => f.write_str(v),
ContextItem::VideoDriver(v) => f.write_fmt(format_args!("{}", v)),
ContextItem::CoreRequestedRotation(v) => {
f.write_fmt(format_args!("{}-{}", self.key(), v))
}
ContextItem::AllowCoreRotation(v) => f.write_fmt(format_args!(
"{}-{}",
self.key(),
ContextItem::toggle_str(*v)
)),
ContextItem::UserRotation(v) => f.write_fmt(format_args!("{}-{}", self.key(), v)),
ContextItem::FinalRotation(v) => f.write_fmt(format_args!("{}-{}", self.key(), v)),
ContextItem::ScreenOrientation(v) => f.write_fmt(format_args!("{}-{}", self.key(), v)),
ContextItem::ViewAspectOrientation(v) => {
f.write_fmt(format_args!("{}-{}", self.key(), v))
}
ContextItem::CoreAspectOrientation(v) => {
f.write_fmt(format_args!("{}-{}", self.key(), v))
}
ContextItem::VideoDriverShaderExtension(v) => f.write_fmt(format_args!("{}", v)),
ContextItem::VideoDriverPresetExtension(v) => f.write_fmt(format_args!("{}", v)),
ContextItem::ExternContext(_, v) => f.write_fmt(format_args!("{}", v)),
}
}
}
/// A preset wildcard context.
///
/// Any items added after will have higher priority
/// when passed to the shader preset parser.
///
/// When passed to the preset parser, the preset parser
/// will automatically add inferred items at lowest priority.
///
/// Any items added by the user will override the automatically
/// inferred items.
#[derive(Debug, Clone)]
pub struct WildcardContext(VecDeque<ContextItem>);
impl WildcardContext {
/// Create a new wildcard context.
pub fn new() -> Self {
Self(VecDeque::new())
}
/// Prepend an item to the context builder.
pub fn prepend_item(&mut self, item: ContextItem) {
self.0.push_front(item);
}
/// Append an item to the context builder.
/// The new item will take precedence over all items added before it.
pub fn append_item(&mut self, item: ContextItem) {
self.0.push_back(item);
}
/// Prepend sensible defaults for the given video driver.
///
/// Any values added, either previously or afterwards will not be overridden.
pub fn add_video_driver_defaults(&mut self, video_driver: VideoDriver) {
self.prepend_item(ContextItem::VideoDriverPresetExtension(
PresetExtension::Slangp,
));
self.prepend_item(ContextItem::VideoDriverShaderExtension(
ShaderExtension::Slang,
));
self.prepend_item(ContextItem::VideoDriver(video_driver));
}
/// Prepend default entries from the path of the preset.
///
/// Any values added, either previously or afterwards will not be overridden.
pub fn add_path_defaults(&mut self, path: impl AsRef<Path>) {
let path = path.as_ref();
if let Some(preset_name) = path.file_stem() {
let preset_name = preset_name.to_string_lossy();
self.prepend_item(ContextItem::Preset(preset_name.into()))
}
if let Some(preset_dir_name) = path.parent().and_then(|p| {
if !p.is_dir() {
return None;
};
p.file_name()
}) {
let preset_dir_name = preset_dir_name.to_string_lossy();
self.prepend_item(ContextItem::PresetDirectory(preset_dir_name.into()))
}
}
pub fn to_hashmap(mut self) -> FxHashMap<String, String> {
let mut map = FxHashMap::default();
let last_user_rot = self
.0
.iter()
.rfind(|i| matches!(i, ContextItem::UserRotation(_)));
let last_core_rot = self
.0
.iter()
.rfind(|i| matches!(i, ContextItem::CoreRequestedRotation(_)));
let final_rot = match (last_core_rot, last_user_rot) {
(Some(ContextItem::UserRotation(u)), None) => Some(ContextItem::FinalRotation(*u)),
(None, Some(ContextItem::CoreRequestedRotation(c))) => {
Some(ContextItem::FinalRotation(*c))
}
(Some(ContextItem::UserRotation(u)), Some(ContextItem::CoreRequestedRotation(c))) => {
Some(ContextItem::FinalRotation(*u + *c))
}
_ => None,
};
if let Some(final_rot) = final_rot {
self.prepend_item(final_rot);
}
for item in self.0 {
map.insert(String::from(item.key()), item.to_string());
}
map
}
}
#[rustversion::since(1.74)]
pub(crate) fn apply_context(path: &mut PathBuf, context: &FxHashMap<String, String>) {
use std::ffi::{OsStr, OsString};
static WILDCARD_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("\\$([A-Z-_]+)\\$").unwrap());
if context.is_empty() {
return;
}
// Don't want to do any extra work if there's no match.
if !WILDCARD_REGEX.is_match(path.as_os_str().as_encoded_bytes()) {
return;
}
let mut new_path = PathBuf::with_capacity(path.capacity());
for component in path.components() {
match component {
Component::Normal(path) => {
let haystack = path.as_encoded_bytes();
let replaced =
WILDCARD_REGEX.replace_all(haystack, |caps: &regex::bytes::Captures| {
let Some(name) = caps.get(1) else {
return caps[0].to_vec();
};
let Ok(key) = std::str::from_utf8(name.as_bytes()) else {
return caps[0].to_vec();
};
if let Some(replacement) = context.get(key) {
return OsString::from(replacement.to_string()).into_encoded_bytes();
}
return caps[0].to_vec();
});
// SAFETY: The original source is valid encoded bytes, and our replacement is
// valid encoded bytes. This upholds the safety requirements of `from_encoded_bytes_unchecked`.
new_path.push(unsafe { OsStr::from_encoded_bytes_unchecked(&replaced.as_ref()) })
}
_ => new_path.push(component),
}
}
// If no wildcards are found within the path, or the path after replacing the wildcards does not exist on disk, the path returned will be unaffected.
if let Ok(true) = new_path.try_exists() {
*path = new_path;
}
}
#[rustversion::before(1.74)]
pub(crate) fn apply_context(path: &mut PathBuf, context: &FxHashMap<String, String>) {
use os_str_bytes::RawOsStr;
static WILDCARD_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("\\$([A-Z-_]+)\\$").unwrap());
if context.is_empty() {
return;
}
let path_str = RawOsStr::new(path.as_os_str());
let path_bytes = path_str.to_raw_bytes();
// Don't want to do any extra work if there's no match.
if !WILDCARD_REGEX.is_match(&path_bytes) {
return;
}
let mut new_path = PathBuf::with_capacity(path.capacity());
for component in path.components() {
match component {
Component::Normal(path) => {
let haystack = RawOsStr::new(path);
let haystack = haystack.to_raw_bytes();
let replaced =
WILDCARD_REGEX.replace_all(&haystack, |caps: &regex::bytes::Captures| {
let Some(name) = caps.get(1) else {
return caps[0].to_vec();
};
let Ok(key) = std::str::from_utf8(name.as_bytes()) else {
return caps[0].to_vec();
};
if let Some(replacement) = context.get(key) {
return RawOsStr::from_str(replacement).to_raw_bytes().to_vec();
}
return caps[0].to_vec();
});
// SAFETY: The original source is valid encoded bytes, and our replacement is
// valid encoded bytes. This upholds the safety requirements of `from_encoded_bytes_unchecked`.
new_path.push(RawOsStr::assert_cow_from_raw_bytes(&replaced.as_ref()).to_os_str())
}
_ => new_path.push(component),
}
}
// If no wildcards are found within the path, or the path after replacing the wildcards does not exist on disk, the path returned will be unaffected.
if let Ok(true) = new_path.try_exists() {
*path = new_path;
}
}

View file

@ -148,7 +148,7 @@ where
#[cfg(test)]
mod test {
use crate::MakeExtractIf;
use crate::extract_if::MakeExtractIf;
#[test]
fn drain_filter_empty() {
let mut vec: Vec<i32> = vec![];

View file

@ -9,14 +9,14 @@
//! Re-exported as [`librashader::presets`](https://docs.rs/librashader/latest/librashader/presets/index.html).
#![allow(stable_features)]
#![feature(os_str_bytes)]
#![allow(unstable_name_collisions)]
mod error;
mod extract_if;
mod parse;
mod preset;
pub mod context;
pub use context::WildcardContext;
pub use error::*;
pub use parse::context;
pub use preset::*;

View file

@ -3,7 +3,6 @@ use std::path::Path;
use nom_locate::LocatedSpan;
use std::str;
pub mod context;
mod preset;
mod token;
mod value;
@ -12,7 +11,7 @@ pub(crate) type Span<'a> = LocatedSpan<&'a str>;
pub(crate) use token::Token;
use crate::error::ParsePresetError;
use crate::parse::context::{VideoDriver, WildcardContext};
use crate::context::{VideoDriver, WildcardContext};
use crate::parse::preset::resolve_values;
use crate::parse::value::parse_preset;
use crate::ShaderPreset;

View file

@ -171,7 +171,7 @@ pub fn do_lex(input: &str) -> Result<Vec<Token>, ParsePresetError> {
#[cfg(test)]
mod test {
use crate::parse::token::{do_lex, single_comment};
use crate::parse::token::{single_comment};
#[test]
fn parses_single_line_comment() {

View file

@ -1,5 +1,5 @@
use crate::error::{ParseErrorKind, ParsePresetError};
use crate::parse::{context, remove_if, Span, Token};
use crate::parse::{remove_if, Span, Token};
use crate::{ScaleFactor, ScaleType};
use nom::bytes::complete::tag;
use nom::character::complete::digit1;
@ -18,7 +18,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use crate::extract_if::MakeExtractIf;
use crate::parse::context::WildcardContext;
use crate::context::{apply_context, WildcardContext};
#[derive(Debug)]
pub enum Value {
@ -172,7 +172,7 @@ fn load_child_reference_strings(
// enter the current root
reference_depth += 1;
// canonicalize current root
context::apply_context(&mut reference_root, context);
apply_context(&mut reference_root, context);
let reference_root = reference_root
.canonicalize()
.map_err(|e| ParsePresetError::IOError(reference_root.to_path_buf(), e))?;
@ -182,7 +182,7 @@ fn load_child_reference_strings(
for path in referenced_paths {
let mut path = reference_root.join(path.clone());
context::apply_context(&mut path, context);
apply_context(&mut path, context);
let mut path = path
.canonicalize()
@ -219,7 +219,7 @@ pub(crate) fn parse_preset(
let mut path = path.to_path_buf();
let context = context.to_hashmap();
context::apply_context(&mut path, &context);
apply_context(&mut path, &context);
let path = path
.canonicalize()
@ -616,12 +616,13 @@ pub fn parse_values(
mod test {
use crate::parse::value::parse_preset;
use std::path::PathBuf;
use crate::WildcardContext;
#[test]
pub fn parse_basic() {
let root =
PathBuf::from("../test/slang-shaders/bezel/Mega_Bezel/Presets/Base_CRT_Presets/MBZ__3__STD__MEGATRON-NTSC.slangp");
let basic = parse_preset(root);
let basic = parse_preset(root, WildcardContext::new());
eprintln!("{basic:?}");
assert!(basic.is_ok());
}

View file

@ -1,7 +1,6 @@
use glob::glob;
use librashader_presets::context::{ContextItem, VideoDriver, WildcardContext};
use librashader_presets::ShaderPreset;
use std::collections::HashMap;
#[test]
fn parses_all_slang_presets() {

View file

@ -228,15 +228,6 @@ pub mod runtime {
options::{FilterChainOptionsGL as FilterChainOptions, FrameOptionsGL as FrameOptions},
FilterChainGL as FilterChain, GLFramebuffer, GLImage,
};
#[doc(hidden)]
#[cfg(feature = "internal")]
/// Re-exports names to deal with C API conflicts.
///
/// This is internal to librashader-capi and is exempt from semantic versioning.
pub mod capi {
pub use librashader_runtime_gl::*;
}
}
#[cfg(all(target_os = "windows", feature = "runtime-d3d11"))]
@ -250,15 +241,6 @@ pub mod runtime {
},
D3D11InputView, D3D11OutputView, FilterChainD3D11 as FilterChain,
};
#[doc(hidden)]
#[cfg(feature = "internal")]
/// Re-exports names to deal with C API conflicts.
///
/// This is internal to librashader-capi and is exempt from semantic versioning.
pub mod capi {
pub use librashader_runtime_d3d11::*;
}
}
#[cfg(all(target_os = "windows", feature = "runtime-d3d12"))]
@ -272,15 +254,6 @@ pub mod runtime {
},
D3D12InputImage, D3D12OutputView, FilterChainD3D12 as FilterChain,
};
#[doc(hidden)]
#[cfg(feature = "internal")]
/// Re-exports names to deal with C API conflicts.
///
/// This is internal to librashader-capi and is exempt from semantic versioning.
pub mod capi {
pub use librashader_runtime_d3d12::*;
}
}
#[cfg(feature = "runtime-vk")]
@ -294,15 +267,6 @@ pub mod runtime {
},
FilterChainVulkan as FilterChain, VulkanImage, VulkanInstance, VulkanObjects,
};
#[doc(hidden)]
#[cfg(feature = "internal")]
/// Re-exports names to deal with C API conflicts.
///
/// This is internal to librashader-capi and is exempt from semantic versioning.
pub mod capi {
pub use librashader_runtime_vk::*;
}
}
#[cfg(feature = "runtime-wgpu")]

View file

@ -15,10 +15,17 @@ int main()
std::cout << "Hello World!\n";
std::cout << std::filesystem::current_path() << std::endl;
auto instance = librashader_load_instance();
libra_preset_ctx_t context;
instance.preset_ctx_create(&context);
instance.preset_ctx_set_core_name(&context, "Hello");
libra_shader_preset_t preset;
auto error = instance.preset_create(
auto error = instance.preset_create_with_context(
"../../../shaders_slang/border/gameboy-player/"
"gameboy-player-crt-royale.slangp",
&context,
&preset);
/* libra_shader_preset_t preset2;