Skip to content

Commit

Permalink
rt: redesign parameters to be thread-safe across FFI using arcswap an…
Browse files Browse the repository at this point in the history
…d atomicusize

This should allow C FFI consumers to modify frame parameters from a different thread without it being UB.
  • Loading branch information
chyyran committed Sep 7, 2024
1 parent ae76bf9 commit c447e40
Show file tree
Hide file tree
Showing 26 changed files with 216 additions and 310 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions librashader-capi/src/runtime/d3d11/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ extern_fn! {
chain: *mut libra_d3d11_filter_chain_t,
param_name: *const c_char,
value: f32
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

if chain.set_parameter(name, value).is_none() {
if chain.parameters().set_parameter(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
}
}
Expand All @@ -282,17 +282,17 @@ extern_fn! {
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d11_filter_chain_t`.
/// - `param_name` must be either null or a null terminated string.
fn libra_d3d11_filter_chain_get_param(
chain: *mut libra_d3d11_filter_chain_t,
chain: *const libra_d3d11_filter_chain_t,
param_name: *const c_char,
out: *mut MaybeUninit<f32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

let Some(value) = chain.get_parameter(name) else {
let Some(value) = chain.parameters().get_parameter(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
};

Expand All @@ -309,9 +309,9 @@ extern_fn! {
fn libra_d3d11_filter_chain_set_active_pass_count(
chain: *mut libra_d3d11_filter_chain_t,
value: u32
) mut |chain| {
assert_some_ptr!(mut chain);
chain.set_enabled_pass_count(value as usize);
) |chain| {
assert_some_ptr!(chain);
chain.parameters().set_passes_enabled(value as usize);
}
}

Expand All @@ -321,12 +321,12 @@ extern_fn! {
/// ## Safety
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d11_filter_chain_t`.
fn libra_d3d11_filter_chain_get_active_pass_count(
chain: *mut libra_d3d11_filter_chain_t,
chain: *const libra_d3d11_filter_chain_t,
out: *mut MaybeUninit<u32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
unsafe {
let value = chain.get_enabled_pass_count();
let value = chain.parameters().passes_enabled();
out.write(MaybeUninit::new(value as u32))
}
}
Expand Down
28 changes: 14 additions & 14 deletions librashader-capi/src/runtime/d3d12/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ extern_fn! {
chain: *mut libra_d3d12_filter_chain_t,
param_name: *const c_char,
value: f32
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

if chain.set_parameter(name, value).is_none() {
if chain.parameters().set_parameter(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
}
}
Expand All @@ -302,17 +302,17 @@ extern_fn! {
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d12_filter_chain_t`.
/// - `param_name` must be either null or a null terminated string.
fn libra_d3d12_filter_chain_get_param(
chain: *mut libra_d3d12_filter_chain_t,
chain: *const libra_d3d12_filter_chain_t,
param_name: *const c_char,
out: *mut MaybeUninit<f32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

let Some(value) = chain.get_parameter(name) else {
let Some(value) = chain.parameters().get_parameter(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
};

Expand All @@ -329,9 +329,9 @@ extern_fn! {
fn libra_d3d12_filter_chain_set_active_pass_count(
chain: *mut libra_d3d12_filter_chain_t,
value: u32
) mut |chain| {
assert_some_ptr!(mut chain);
chain.set_enabled_pass_count(value as usize);
) |chain| {
assert_some_ptr!(chain);
chain.parameters().set_passes_enabled(value as usize);
}
}

Expand All @@ -341,12 +341,12 @@ extern_fn! {
/// ## Safety
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d12_filter_chain_t`.
fn libra_d3d12_filter_chain_get_active_pass_count(
chain: *mut libra_d3d12_filter_chain_t,
chain: *const libra_d3d12_filter_chain_t,
out: *mut MaybeUninit<u32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
unsafe {
let value = chain.get_enabled_pass_count();
let value = chain.parameters().passes_enabled();
out.write(MaybeUninit::new(value as u32))
}
}
Expand Down
28 changes: 14 additions & 14 deletions librashader-capi/src/runtime/d3d9/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ extern_fn! {
chain: *mut libra_d3d9_filter_chain_t,
param_name: *const c_char,
value: f32
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

if chain.set_parameter(name, value).is_none() {
if chain.parameters().set_parameter(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
}
}
Expand All @@ -192,17 +192,17 @@ extern_fn! {
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d9_filter_chain_t`.
/// - `param_name` must be either null or a null terminated string.
fn libra_d3d9_filter_chain_get_param(
chain: *mut libra_d3d9_filter_chain_t,
chain: *const libra_d3d9_filter_chain_t,
param_name: *const c_char,
out: *mut MaybeUninit<f32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

let Some(value) = chain.get_parameter(name) else {
let Some(value) = chain.parameters().get_parameter(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
};

Expand All @@ -219,9 +219,9 @@ extern_fn! {
fn libra_d3d9_filter_chain_set_active_pass_count(
chain: *mut libra_d3d9_filter_chain_t,
value: u32
) mut |chain| {
assert_some_ptr!(mut chain);
chain.set_enabled_pass_count(value as usize);
) |chain| {
assert_some_ptr!(chain);
chain.parameters().set_passes_enabled(value as usize);
}
}

Expand All @@ -231,12 +231,12 @@ extern_fn! {
/// ## Safety
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_d3d9_filter_chain_t`.
fn libra_d3d9_filter_chain_get_active_pass_count(
chain: *mut libra_d3d9_filter_chain_t,
chain: *const libra_d3d9_filter_chain_t,
out: *mut MaybeUninit<u32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
unsafe {
let value = chain.get_enabled_pass_count();
let value = chain.parameters().passes_enabled();
out.write(MaybeUninit::new(value as u32))
}
}
Expand Down
22 changes: 11 additions & 11 deletions librashader-capi/src/runtime/gl/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ extern_fn! {
chain: *mut libra_gl_filter_chain_t,
param_name: *const c_char,
value: f32
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

if chain.set_parameter(name, value).is_none() {
if chain.parameters().set_parameter(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
}
}
Expand All @@ -249,17 +249,17 @@ extern_fn! {
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_gl_filter_chain_t`.
/// - `param_name` must be either null or a null terminated string.
fn libra_gl_filter_chain_get_param(
chain: *mut libra_gl_filter_chain_t,
chain: *const libra_gl_filter_chain_t,
param_name: *const c_char,
out: *mut MaybeUninit<f32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
assert_non_null!(param_name);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

let Some(value) = chain.get_parameter(name) else {
let Some(value) = chain.parameters().get_parameter(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
};

Expand All @@ -276,9 +276,9 @@ extern_fn! {
fn libra_gl_filter_chain_set_active_pass_count(
chain: *mut libra_gl_filter_chain_t,
value: u32
) mut |chain| {
assert_some_ptr!(mut chain);
chain.set_enabled_pass_count(value as usize);
) |chain| {
assert_some_ptr!(chain);
chain.parameters().set_passes_enabled(value as usize);
}
}

Expand All @@ -292,7 +292,7 @@ extern_fn! {
out: *mut MaybeUninit<u32>
) mut |chain| {
assert_some_ptr!(mut chain);
let value = chain.get_enabled_pass_count();
let value = chain.parameters().passes_enabled();
unsafe {
out.write(MaybeUninit::new(value as u32))
}
Expand Down
28 changes: 14 additions & 14 deletions librashader-capi/src/runtime/mtl/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ extern_fn! {
chain: *mut libra_mtl_filter_chain_t,
param_name: *const c_char,
value: f32
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);
unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

if chain.set_parameter(name, value).is_none() {
if chain.parameters().set_parameter(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
}
}
Expand All @@ -250,17 +250,17 @@ extern_fn! {
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_mtl_filter_chain_t`.
/// - `param_name` must be either null or a null terminated string.
fn libra_mtl_filter_chain_get_param(
chain: *mut libra_mtl_filter_chain_t,
chain: *const libra_mtl_filter_chain_t,
param_name: *const c_char,
out: *mut MaybeUninit<f32>
) mut |chain| {
assert_some_ptr!(mut chain);
) |chain| {
assert_some_ptr!(chain);

unsafe {
let name = CStr::from_ptr(param_name);
let name = name.to_str()?;

let Some(value) = chain.get_parameter(name) else {
let Some(value) = chain.parameters().get_parameter(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
};

Expand All @@ -277,9 +277,9 @@ extern_fn! {
fn libra_mtl_filter_chain_set_active_pass_count(
chain: *mut libra_mtl_filter_chain_t,
value: u32
) mut |chain| {
assert_some_ptr!(mut chain);
chain.set_enabled_pass_count(value as usize);
) |chain| {
assert_some_ptr!(chain);
chain.parameters().set_passes_enabled(value as usize);
}
}

Expand All @@ -289,11 +289,11 @@ extern_fn! {
/// ## Safety
/// - `chain` must be either null or a valid and aligned pointer to an initialized `libra_mtl_filter_chain_t`.
fn libra_mtl_filter_chain_get_active_pass_count(
chain: *mut libra_mtl_filter_chain_t,
chain: *const libra_mtl_filter_chain_t,
out: *mut MaybeUninit<u32>
) mut |chain| {
assert_some_ptr!(mut chain);
let value = chain.get_enabled_pass_count();
) |chain| {
assert_some_ptr!(chain);
let value = chain.parameters().passes_enabled();
unsafe {
out.write(MaybeUninit::new(value as u32))
}
Expand Down
Loading

0 comments on commit c447e40

Please sign in to comment.