use std::alloc::Layout;
use std::collections::{HashMap, HashSet};
use std::os::raw::c_void;
use std::sync::{LazyLock, Mutex};
use crate::sys::*;
use crate::PluginInstance;
macro_rules! gpu_context_or_bail {
($effect_ref:expr, $who:literal) => {{
if $effect_ref.is_null() {
log::error!(concat!($who, ": effect_ref is null"));
return PF_Err_BAD_CALLBACK_PARAM;
}
let instance = match PluginInstance::get_instance_ptr($effect_ref) {
Some(ptr) => unsafe { ptr.as_ref() },
None => {
log::error!(concat!($who, ": no plugin instance for effect_ref"));
return PF_Err_BAD_CALLBACK_PARAM;
}
};
match instance.gpu_context() {
Some(ctx) => ctx,
None => {
log::error!(concat!($who, ": GPU context not initialised"));
return PF_Err_BAD_CALLBACK_PARAM;
}
}
}};
}
macro_rules! gpu_context_mut_or_bail {
($effect_ref:expr, $who:literal) => {{
if $effect_ref.is_null() {
log::error!(concat!($who, ": effect_ref is null"));
return PF_Err_BAD_CALLBACK_PARAM;
}
let instance = match PluginInstance::get_instance_ptr($effect_ref) {
Some(mut ptr) => unsafe { ptr.as_mut() },
None => {
log::error!(concat!($who, ": no plugin instance for effect_ref"));
return PF_Err_BAD_CALLBACK_PARAM;
}
};
match instance.gpu_context_mut() {
Some(ctx) => ctx,
None => {
log::error!(concat!($who, ": GPU context not initialised"));
return PF_Err_BAD_CALLBACK_PARAM;
}
}
}};
}
unsafe extern "C" fn get_device_count(effect_ref: PF_ProgPtr, device_countP: *mut A_u_long) -> PF_Err {
crate::core::ffi::guard("get_device_count", || {
let _ = gpu_context_or_bail!(effect_ref, "GetDeviceCount");
if device_countP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
unsafe { *device_countP = 1 };
PF_Err_NONE
})
}
unsafe extern "C" fn get_device_info(
effect_ref: PF_ProgPtr,
_device_index: A_u_long,
device_infoP: *mut PF_GPUDeviceInfo,
) -> PF_Err {
crate::core::ffi::guard("get_device_info", || {
let ctx = gpu_context_or_bail!(effect_ref, "GetDeviceInfo");
if device_infoP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
let info = PF_GPUDeviceInfo {
device_framework: ctx.framework(),
compatibleB: 1,
platformPV: std::ptr::null_mut(),
devicePV: ctx.device_ptr(),
contextPV: ctx.context_ptr(),
command_queuePV: ctx.queue_ptr(),
offscreen_opengl_contextPV: std::ptr::null_mut(),
offscreen_opengl_devicePV: std::ptr::null_mut(),
};
unsafe { *device_infoP = info };
PF_Err_NONE
})
}
unsafe extern "C" fn get_gpu_world_data(
effect_ref: PF_ProgPtr,
worldP: *mut PF_EffectWorld,
pixPP: *mut *mut c_void,
) -> PF_Err {
crate::core::ffi::guard("get_gpu_world_data", || {
let ctx = gpu_context_or_bail!(effect_ref, "GetGPUWorldData");
if worldP.is_null() || pixPP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
match ctx.buffer_object_ptr(worldP as usize) {
Some(buffer) => {
unsafe { *pixPP = buffer };
PF_Err_NONE
}
None => {
log::error!(
"GetGPUWorldData: no GPU buffer registered for world {:#x}",
worldP as usize
);
PF_Err_BAD_CALLBACK_PARAM
}
}
})
}
unsafe extern "C" fn allocate_device_memory(
effect_ref: PF_ProgPtr,
_device_index: A_u_long,
size: usize,
memoryPP: *mut *mut c_void,
) -> PF_Err {
crate::core::ffi::guard("allocate_device_memory", || {
let ctx = gpu_context_mut_or_bail!(effect_ref, "AllocateDeviceMemory");
if memoryPP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
match ctx.alloc_raw(size) {
Some(memory) => {
unsafe { *memoryPP = memory };
PF_Err_NONE
}
None => PF_Err_OUT_OF_MEMORY,
}
})
}
unsafe extern "C" fn free_device_memory(
effect_ref: PF_ProgPtr,
_device_index: A_u_long,
memoryP: *mut c_void,
) -> PF_Err {
crate::core::ffi::guard("free_device_memory", || {
let ctx = gpu_context_mut_or_bail!(effect_ref, "FreeDeviceMemory");
if ctx.free_raw(memoryP) {
PF_Err_NONE
} else {
log::error!("FreeDeviceMemory: unknown allocation {:#x}", memoryP as usize);
PF_Err_BAD_CALLBACK_PARAM
}
})
}
unsafe extern "C" fn get_gpu_world_size(
_effect_ref: PF_ProgPtr,
worldP: *mut PF_EffectWorld,
size_in_bytesP: *mut usize,
) -> PF_Err {
crate::core::ffi::guard("get_gpu_world_size", || {
if worldP.is_null() || size_in_bytesP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
let world = unsafe { &*worldP };
unsafe { *size_in_bytesP = world.height as usize * world.rowbytes as usize };
PF_Err_NONE
})
}
unsafe extern "C" fn get_gpu_world_device_index(
_effect_ref: PF_ProgPtr,
worldP: *mut PF_EffectWorld,
device_indexP: *mut A_u_long,
) -> PF_Err {
crate::core::ffi::guard("get_gpu_world_device_index", || {
if worldP.is_null() || device_indexP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
unsafe { *device_indexP = 0 };
PF_Err_NONE
})
}
unsafe extern "C" fn acquire_exclusive_device_access(_effect_ref: PF_ProgPtr, _device_index: A_u_long) -> PF_Err {
crate::core::ffi::guard("acquire_exclusive_device_access", || PF_Err_NONE)
}
unsafe extern "C" fn release_exclusive_device_access(_effect_ref: PF_ProgPtr, _device_index: A_u_long) -> PF_Err {
crate::core::ffi::guard("release_exclusive_device_access", || PF_Err_NONE)
}
unsafe extern "C" fn purge_device_memory(
_effect_ref: PF_ProgPtr,
_device_index: A_u_long,
_size: usize,
bytes_purgedP0: *mut usize,
) -> PF_Err {
crate::core::ffi::guard("purge_device_memory", || {
if let Some(out) = unsafe { bytes_purgedP0.as_mut() } {
*out = 0;
}
PF_Err_NONE
})
}
const HOST_ALIGN: usize = 64;
static HOST_ALLOCS: LazyLock<Mutex<HashMap<usize, usize>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
unsafe extern "C" fn allocate_host_memory(
_effect_ref: PF_ProgPtr,
_device_index: A_u_long,
size: usize,
memoryPP: *mut *mut c_void,
) -> PF_Err {
crate::core::ffi::guard("allocate_host_memory", || {
let Some(out) = (unsafe { memoryPP.as_mut() }) else {
return PF_Err_BAD_CALLBACK_PARAM;
};
let Ok(layout) = Layout::from_size_align(size.max(1), HOST_ALIGN) else {
return PF_Err_OUT_OF_MEMORY;
};
let ptr = unsafe { std::alloc::alloc(layout) };
if ptr.is_null() {
return PF_Err_OUT_OF_MEMORY;
}
HOST_ALLOCS
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(ptr as usize, layout.size());
*out = ptr as *mut c_void;
PF_Err_NONE
})
}
unsafe extern "C" fn free_host_memory(
_effect_ref: PF_ProgPtr,
_device_index: A_u_long,
memoryP: *mut c_void,
) -> PF_Err {
crate::core::ffi::guard("free_host_memory", || {
let size = HOST_ALLOCS
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&(memoryP as usize));
let Some(size) = size else {
log::error!("FreeHostMemory: unknown allocation {:#x}", memoryP as usize);
return PF_Err_BAD_CALLBACK_PARAM;
};
unsafe { std::alloc::dealloc(memoryP as *mut u8, Layout::from_size_align_unchecked(size, HOST_ALIGN)) };
PF_Err_NONE
})
}
unsafe extern "C" fn purge_host_memory(
_effect_ref: PF_ProgPtr,
_device_index: A_u_long,
_bytes_to_try: usize,
bytes_purgedP0: *mut usize,
) -> PF_Err {
crate::core::ffi::guard("purge_host_memory", || {
if let Some(out) = unsafe { bytes_purgedP0.as_mut() } {
*out = 0;
}
PF_Err_NONE
})
}
static CREATED_GPU_WORLDS: LazyLock<Mutex<HashSet<usize>>> = LazyLock::new(|| Mutex::new(HashSet::new()));
#[allow(clippy::too_many_arguments)]
unsafe extern "C" fn create_gpu_world(
effect_ref: PF_ProgPtr,
_device_index: A_u_long,
width: A_long,
height: A_long,
pixel_aspect_ratio: PF_RationalScale,
_field: PF_Field,
pixel_format: PF_PixelFormat,
clear_pixB: PF_Boolean,
worldPP: *mut *mut PF_EffectWorld,
) -> PF_Err {
crate::core::ffi::guard("create_gpu_world", || {
let ctx = gpu_context_mut_or_bail!(effect_ref, "CreateGPUWorld");
let Some(out) = (unsafe { worldPP.as_mut() }) else {
return PF_Err_BAD_CALLBACK_PARAM;
};
if pixel_format as u32 != PF_PixelFormat_GPU_BGRA128 as u32 || width <= 0 || height <= 0 {
log::error!("CreateGPUWorld: unsupported {width}x{height} world of format {pixel_format:#x}");
return PF_Err_BAD_CALLBACK_PARAM;
}
let rowbytes = width * crate::gpu::GPU_BYTES_PER_PIXEL as A_long;
let byte_len = rowbytes as usize * height as usize;
let mut world: PF_EffectWorld = unsafe { std::mem::zeroed() };
world.world_flags = PF_WorldFlag_WRITEABLE;
world.rowbytes = rowbytes;
world.width = width;
world.height = height;
world.extent_hint = PF_UnionableRect {
left: 0,
top: 0,
right: width,
bottom: height,
};
world.pix_aspect_ratio = pixel_aspect_ratio;
let world = Box::into_raw(Box::new(world));
let key = world as usize;
if !ctx.ensure_buffer(key, byte_len) || (clear_pixB != 0 && !ctx.write_buffer(key, &vec![0; byte_len])) {
ctx.release_buffer(key);
drop(unsafe { Box::from_raw(world) });
return PF_Err_OUT_OF_MEMORY;
}
CREATED_GPU_WORLDS.lock().unwrap_or_else(|e| e.into_inner()).insert(key);
*out = world;
PF_Err_NONE
})
}
unsafe extern "C" fn dispose_gpu_world(effect_ref: PF_ProgPtr, worldP: *mut PF_EffectWorld) -> PF_Err {
crate::core::ffi::guard("dispose_gpu_world", || {
let ctx = gpu_context_mut_or_bail!(effect_ref, "DisposeGPUWorld");
if !CREATED_GPU_WORLDS
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&(worldP as usize))
{
log::error!(
"DisposeGPUWorld: {:#x} was not created by CreateGPUWorld",
worldP as usize
);
return PF_Err_BAD_CALLBACK_PARAM;
}
ctx.release_buffer(worldP as usize);
drop(unsafe { Box::from_raw(worldP) });
PF_Err_NONE
})
}
pub const fn create_gpu_device_suite_1() -> PF_GPUDeviceSuite1 {
PF_GPUDeviceSuite1 {
GetDeviceCount: Some(get_device_count),
GetDeviceInfo: Some(get_device_info),
AcquireExclusiveDeviceAccess: Some(acquire_exclusive_device_access),
ReleaseExclusiveDeviceAccess: Some(release_exclusive_device_access),
AllocateDeviceMemory: Some(allocate_device_memory),
FreeDeviceMemory: Some(free_device_memory),
PurgeDeviceMemory: Some(purge_device_memory),
AllocateHostMemory: Some(allocate_host_memory),
FreeHostMemory: Some(free_host_memory),
PurgeHostMemory: Some(purge_host_memory),
CreateGPUWorld: Some(create_gpu_world),
DisposeGPUWorld: Some(dispose_gpu_world),
GetGPUWorldData: Some(get_gpu_world_data),
GetGPUWorldSize: Some(get_gpu_world_size),
GetGPUWorldDeviceIndex: Some(get_gpu_world_device_index),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn host_memory_round_trips_and_rejects_foreign_pointers() {
let suite = create_gpu_device_suite_1();
let mut mem: *mut c_void = std::ptr::null_mut();
unsafe {
assert_eq!(
suite.AllocateHostMemory.unwrap()(std::ptr::null_mut(), 0, 100, &mut mem),
PF_Err_NONE
);
assert_eq!(mem as usize % HOST_ALIGN, 0);
std::ptr::write_bytes(mem as *mut u8, 0xab, 100);
assert_eq!(suite.FreeHostMemory.unwrap()(std::ptr::null_mut(), 0, mem), PF_Err_NONE);
assert_ne!(suite.FreeHostMemory.unwrap()(std::ptr::null_mut(), 0, mem), PF_Err_NONE);
let mut purged = 1;
suite.PurgeHostMemory.unwrap()(std::ptr::null_mut(), 0, 1 << 20, &mut purged);
assert_eq!(purged, 0);
}
}
}