diff --git a/libobs/build/libobs.graphics+.h.c b/libobs/build/libobs.graphics+.h.c index 852403d661..8c40f13883 100644 --- a/libobs/build/libobs.graphics+.h.c +++ b/libobs/build/libobs.graphics+.h.c @@ -9,7 +9,6 @@ #include "libobs/graphics/graphics-device-queries-fields.c.h" #include "libobs/graphics/graphics-imports.c.h" #include "libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h" -#include "libobs/graphics/graphics-planar-capability-queries.c.h" #include "libobs/graphics/graphics-render-target-setters-fields.c.h" #include "libobs/graphics/graphics-samplerstate-lifecycle-fields.c.h" #include "libobs/graphics/graphics-shader-matrix-queries-fields.c.h" diff --git a/libobs/build/libobs.ported-graphics-planar-capability-queries+.h.c b/libobs/build/libobs.ported-graphics-planar-capability-queries+.h.c new file mode 100644 index 0000000000..fb2ad987d7 --- /dev/null +++ b/libobs/build/libobs.ported-graphics-planar-capability-queries+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original planar capability queries for the C reference. */ +#include "libobs/build/libobs-graphics.pch.h" + +#include "libobs/graphics/graphics-planar-capability-queries.c.h" diff --git a/libobs/build/ported-source-sets/graphics-planar-capability-queries.json b/libobs/build/ported-source-sets/graphics-planar-capability-queries.json new file mode 100644 index 0000000000..603c57ffb7 --- /dev/null +++ b/libobs/build/ported-source-sets/graphics-planar-capability-queries.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-graphics-planar-capability-queries+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-graphics-planar_capability_queries.txt b/libobs/cmake/rust-exports.d/obs-graphics-planar_capability_queries.txt new file mode 100644 index 0000000000..1484ff3a9e --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-graphics-planar_capability_queries.txt @@ -0,0 +1,2 @@ +gs_nv12_available +gs_p010_available diff --git a/libobs/graphics/graphics-device-queries-fields.c.h b/libobs/graphics/graphics-device-queries-fields.c.h index bc48545c65..7aac244912 100644 --- a/libobs/graphics/graphics-device-queries-fields.c.h +++ b/libobs/graphics/graphics-device-queries-fields.c.h @@ -30,3 +30,27 @@ uint32_t obs_graphics_device_queries_count(graphics_t *graphics) { return graphics->exports.gs_get_adapter_count(); } + +bool obs_graphics_device_queries_has_nv12(graphics_t *graphics); +bool obs_graphics_device_queries_has_nv12(graphics_t *graphics) +{ + return graphics->exports.device_nv12_available != NULL; +} + +bool obs_graphics_device_queries_nv12(graphics_t *graphics); +bool obs_graphics_device_queries_nv12(graphics_t *graphics) +{ + return graphics->exports.device_nv12_available(graphics->device); +} + +bool obs_graphics_device_queries_has_p010(graphics_t *graphics); +bool obs_graphics_device_queries_has_p010(graphics_t *graphics) +{ + return graphics->exports.device_p010_available != NULL; +} + +bool obs_graphics_device_queries_p010(graphics_t *graphics); +bool obs_graphics_device_queries_p010(graphics_t *graphics) +{ + return graphics->exports.device_p010_available(graphics->device); +} diff --git a/libobs/graphics/graphics-planar-capability-queries.c.h b/libobs/graphics/graphics-planar-capability-queries.c.h index 239aa04951..8b757c48b4 100644 --- a/libobs/graphics/graphics-planar-capability-queries.c.h +++ b/libobs/graphics/graphics-planar-capability-queries.c.h @@ -1,8 +1,19 @@ #include "libobs/build/libobs-graphics.pch.h" -/* Private dependencies defined in graphics.c.h, later in the unity group. */ -static THREAD_LOCAL graphics_t *thread_graphics; -static inline bool gs_valid(const char *f); +/* Reference-only: read the existing TLS context without owning another one. */ +#define thread_graphics gs_get_context() + +static inline bool planar_capability_ref_valid(const char *f) +{ + if (!thread_graphics) { + blog(LOG_DEBUG, "%s: called while not in a graphics context", f); + return false; + } + + return true; +} + +#define gs_valid planar_capability_ref_valid bool gs_nv12_available(void) { @@ -25,3 +36,6 @@ bool gs_p010_available(void) return thread_graphics->exports.device_p010_available(thread_graphics->device); } + +#undef gs_valid +#undef thread_graphics diff --git a/rust/README.md b/rust/README.md index 140c83098f..4906cab00e 100644 --- a/rust/README.md +++ b/rust/README.md @@ -155,6 +155,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/graphics/graphics-indexbuffer-maintenance.c.h` | `obs-graphics::indexbuffer_maintenance` | Swapped in hybrid `libobs` (#5049; prerequisite #5046, Refs #5039). The four maintenance entry points preserve context-before-pointer guards, silent NULL destroy in context, direct-data rejection order, exact diagnostics, pointer identity and full `size_t` forwarding. Safe Rust owns validation; private C adapters own layout and dispatch. Original C remains in a reference-only group and a verbatim legacy oracle copy. Tier 1 mirrors unchanged `test_graphics_indexbuffer_maintenance`; Tier 3 compares boundary matrices and bounded random inputs through the Rust ABI in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. | | `libobs/graphics/graphics-render-target-setters.c.h` | `obs-graphics::render_target_setters` | Swapped in hybrid `libobs` (#5076; prerequisite #5058 / #5048). Safe Rust guards the context before private C layout/dispatch adapters; both nullable target/depth handles and every cube face value reach the backend unchanged. Exact diagnostics, device/pointer identity and single dispatch are preserved. Original C remains reference-only and in a verbatim legacy oracle. Tier 1 mirrors unchanged `test_graphics_render_target_setters`; Tier 3 compares boundary matrices and bounded random sequences through the Rust ABI in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. | | `libobs/graphics/graphics-samplerstate-lifecycle.c.h` | `obs-graphics::samplerstate_lifecycle` | Swapped in hybrid `libobs` (Refs #5149; prerequisite #5128/#5148). Safe Rust preserves context-before-argument validation, exact LOG_DEBUG diagnostics, silent NULL destruction in context, settings/opaque-handle identity and backend failure returns. Private C adapters retain TLS, native layouts and exact device-vtable dispatch; sampler objects stay backend-owned without wrapper allocation or tracking. Tier 1 mirrors all four unchanged C characterization cases; Tier 3 compares the original C and real Rust ABI/adapters over boundary and bounded random sequences in the Graphics category. No intentional differences; Tier 4 does not apply. | +| `libobs/graphics/graphics-planar-capability-queries.c.h` | `obs-graphics::planar_capability_queries` | Swapped in hybrid `libobs` (Refs #5218; prerequisite #5217). Safe Rust checks context and optional NV12/P010 callbacks before dispatch through the existing device-query seam and shared demand-linked support archive. Unchanged fake-device Tier 2 characterization, Tier 1 mirrors and Tier 3 original-C differential sequences preserve exact diagnostics, independent dispatch, results and device identity. No host GPU assumptions or intentional differences; Tier 4 does not apply. | | `libobs/graphics/graphics-shader-matrix-queries.c.h` | `obs-graphics::shader_matrix_queries` | Swapped in hybrid `libobs` (Refs #5185; prerequisite #5132/#5184). Safe Rust checks context before shader, then private C adapters dispatch the viewproj/world callback with the exact shader pointer and return the borrowed parameter unchanged, NULL included. Exact LOG_DEBUG messages and single dispatch agree with the reference-only C and a verbatim legacy oracle. Tier 1 mirrors unchanged `test_shader_matrix_queries`; Tier 3 compares boundary matrices and bounded random sequences through the real ABI and adapters in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. | | `libobs/graphics/graphics-target-size.c.h` | `obs-graphics::target_size` | Swapped in hybrid `libobs` (Refs #5100; prerequisite #5087/#5107). Safe Rust checks context before private C layout/dispatch adapters, preserves zero scalar defaults and untouched outputs without context, and forwards nullable/aliased outputs unchanged. Exact LOG_DEBUG messages, device/output identities and single matching callbacks agree with the reference-only C and verbatim legacy oracle. Tier 1 mirrors unchanged `test_graphics_target_size`; Tier 3 covers boundary and bounded random queries in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. | | `libobs/graphics/graphics-texture-copy.c.h` | `obs-graphics::texture_copy` | Swapped in hybrid `libobs` (Refs #5062; prerequisite #5074/#5059). Safe Rust preserves context-before-pointer validation, whole-copy destination-then-source guards and region-copy destination-only validation, including NULL sources, aliasing and all six unchanged offsets/extents. Private C adapters own layout and backend dispatch. Exact LOG_DEBUG messages, device and texture identity match the reference-only original C and verbatim Tier 3 legacy oracle. Tier 1 mirrors unchanged `test_graphics_texture_copy`; Tier 3 compares boundary matrices and bounded random inputs through the real ABI and adapters in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. | diff --git a/rust/obs-c-oracle/legacy/graphics-planar-capability-queries.c b/rust/obs-c-oracle/legacy/graphics-planar-capability-queries.c new file mode 100644 index 0000000000..239aa04951 --- /dev/null +++ b/rust/obs-c-oracle/legacy/graphics-planar-capability-queries.c @@ -0,0 +1,27 @@ +#include "libobs/build/libobs-graphics.pch.h" + +/* Private dependencies defined in graphics.c.h, later in the unity group. */ +static THREAD_LOCAL graphics_t *thread_graphics; +static inline bool gs_valid(const char *f); + +bool gs_nv12_available(void) +{ + if (!gs_valid("gs_nv12_available")) + return false; + + if (!thread_graphics->exports.device_nv12_available) + return false; + + return thread_graphics->exports.device_nv12_available(thread_graphics->device); +} + +bool gs_p010_available(void) +{ + if (!gs_valid("gs_p010_available")) + return false; + + if (!thread_graphics->exports.device_p010_available) + return false; + + return thread_graphics->exports.device_p010_available(thread_graphics->device); +} diff --git a/rust/obs-c-oracle/oracle.d/graphics-planar-capability-queries.list b/rust/obs-c-oracle/oracle.d/graphics-planar-capability-queries.list new file mode 100644 index 0000000000..bae3cbf0c9 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/graphics-planar-capability-queries.list @@ -0,0 +1 @@ +graphics-planar-capability-queries.inc|rust/obs-c-oracle/legacy/graphics-planar-capability-queries.c|auto diff --git a/rust/obs-c-oracle/oracle/device_queries.c b/rust/obs-c-oracle/oracle/device_queries.c index bd868e07ae..efb773e210 100644 --- a/rust/obs-c-oracle/oracle/device_queries.c +++ b/rust/obs-c-oracle/oracle/device_queries.c @@ -34,6 +34,14 @@ static void dq_blog(int level, const char *format, ...) #define blog dq_blog #include "graphics-device-queries.inc" +#define gs_nv12_available oracle_gs_nv12_available +#define gs_p010_available oracle_gs_p010_available +bool oracle_gs_nv12_available(void); +bool oracle_gs_p010_available(void); +#include "graphics-planar-capability-queries.inc" +#undef gs_p010_available +#undef gs_nv12_available + /* Original graphics.c.h context validator, verbatim. */ static inline bool gs_valid(const char *f) { @@ -114,3 +122,54 @@ const char *oracle_device_queries_log(void) #undef gs_get_adapter_count #undef gs_get_device_type + +/* Planar queries share the real device-query TLS/logging adapters above. */ +static THREAD_LOCAL int dq_planar_devices[2]; +static THREAD_LOCAL bool dq_planar_results[2]; +static THREAD_LOCAL unsigned dq_planar_calls[2], dq_planar_last_device; + +static bool dq_planar_nv12(gs_device_t *device) +{ + assert(thread_graphics == &dq_graphics); + assert(device == dq_graphics.device); + assert(device == (gs_device_t *)&dq_planar_devices[0] || device == (gs_device_t *)&dq_planar_devices[1]); + dq_planar_last_device = device == (gs_device_t *)&dq_planar_devices[1] ? 1u : 0u; + dq_planar_calls[0]++; + return dq_planar_results[0]; +} + +static bool dq_planar_p010(gs_device_t *device) +{ + assert(thread_graphics == &dq_graphics); + assert(device == dq_graphics.device); + assert(device == (gs_device_t *)&dq_planar_devices[0] || device == (gs_device_t *)&dq_planar_devices[1]); + dq_planar_last_device = device == (gs_device_t *)&dq_planar_devices[1] ? 1u : 0u; + dq_planar_calls[1]++; + return dq_planar_results[1]; +} + +void oracle_planar_capability_reset(bool context, bool nv12, bool p010, bool nv12_result, bool p010_result, + bool device); +void oracle_planar_capability_reset(bool context, bool nv12, bool p010, bool nv12_result, bool p010_result, bool device) +{ + oracle_device_queries_reset(context, false, false, 0, 0); + dq_graphics.device = (gs_device_t *)&dq_planar_devices[device ? 1 : 0]; + dq_graphics.exports.device_nv12_available = nv12 ? dq_planar_nv12 : NULL; + dq_graphics.exports.device_p010_available = p010 ? dq_planar_p010 : NULL; + dq_planar_results[0] = nv12_result; + dq_planar_results[1] = p010_result; + dq_planar_calls[0] = dq_planar_calls[1] = 0; + dq_planar_last_device = 2; +} + +unsigned oracle_planar_capability_calls(bool p010); +unsigned oracle_planar_capability_calls(bool p010) +{ + return dq_planar_calls[p010 ? 1 : 0]; +} + +unsigned oracle_planar_capability_last_device(void); +unsigned oracle_planar_capability_last_device(void) +{ + return dq_planar_last_device; +} diff --git a/rust/obs-graphics/src/ffi/mod.rs b/rust/obs-graphics/src/ffi/mod.rs index 6b31718c58..3116f1bfb8 100644 --- a/rust/obs-graphics/src/ffi/mod.rs +++ b/rust/obs-graphics/src/ffi/mod.rs @@ -39,6 +39,7 @@ pub mod matrix3; pub mod matrix4; pub mod matrix_entrypoints; pub mod perspective; +pub mod planar_capability_queries; pub mod plane; pub mod present_entrypoints; pub mod quat; diff --git a/rust/obs-graphics/src/ffi/planar_capability_queries.rs b/rust/obs-graphics/src/ffi/planar_capability_queries.rs new file mode 100644 index 0000000000..43a752b8cb --- /dev/null +++ b/rust/obs-graphics/src/ffi/planar_capability_queries.rs @@ -0,0 +1,67 @@ +//! Thin ABI shims using the existing device-query TLS/logging/field seam. +use crate::planar_capability_queries::{self as core, Entry, Host}; +use std::ffi::{c_char, c_void}; + +unsafe extern "C" { + fn obs_graphics_device_queries_context() -> *mut c_void; + fn obs_graphics_device_queries_debug(function: *const c_char); + fn obs_graphics_device_queries_has_nv12(context: *mut c_void) -> bool; + fn obs_graphics_device_queries_nv12(context: *mut c_void) -> bool; + fn obs_graphics_device_queries_has_p010(context: *mut c_void) -> bool; + fn obs_graphics_device_queries_p010(context: *mut c_void) -> bool; +} + +struct Context(*mut c_void); + +fn current() -> Context { + // SAFETY: reads the calling thread's context without dereferencing it. + Context(unsafe { obs_graphics_device_queries_context() }) +} + +impl Host for Context { + fn has_context(&self) -> bool { + !self.0.is_null() + } + fn debug(&mut self, entry: Entry) { + let name = match entry { + Entry::Nv12 => c"gs_nv12_available", + Entry::P010 => c"gs_p010_available", + }; + // SAFETY: the function name is static and NUL-terminated. + unsafe { obs_graphics_device_queries_debug(name.as_ptr()) } + } + fn has_callback(&self, entry: Entry) -> bool { + // SAFETY: the core checked the context before any field projection. + unsafe { + match entry { + Entry::Nv12 => obs_graphics_device_queries_has_nv12(self.0), + Entry::P010 => obs_graphics_device_queries_has_p010(self.0), + } + } + } + fn dispatch(&mut self, entry: Entry) -> bool { + // SAFETY: the core checked context and the matching callback first. + unsafe { + match entry { + Entry::Nv12 => obs_graphics_device_queries_nv12(self.0), + Entry::P010 => obs_graphics_device_queries_p010(self.0), + } + } + } +} + +/// Query the optional NV12 backend capability. +/// # Safety +/// A current context and any supplied backend callback must be live. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn gs_nv12_available() -> bool { + core::available(&mut current(), Entry::Nv12) +} + +/// Query the optional P010 backend capability. +/// # Safety +/// A current context and any supplied backend callback must be live. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn gs_p010_available() -> bool { + core::available(&mut current(), Entry::P010) +} diff --git a/rust/obs-graphics/src/lib.rs b/rust/obs-graphics/src/lib.rs index db35083044..63106af02a 100644 --- a/rust/obs-graphics/src/lib.rs +++ b/rust/obs-graphics/src/lib.rs @@ -83,6 +83,7 @@ pub mod matrix3; pub mod matrix4; pub mod matrix_entrypoints; pub mod perspective; +pub mod planar_capability_queries; pub mod plane; mod platform; pub mod present_entrypoints; diff --git a/rust/obs-graphics/src/planar_capability_queries.rs b/rust/obs-graphics/src/planar_capability_queries.rs new file mode 100644 index 0000000000..9348dd5e39 --- /dev/null +++ b/rust/obs-graphics/src/planar_capability_queries.rs @@ -0,0 +1,32 @@ +//! Optional planar capability queries over a caller-owned graphics context. + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Entry { + Nv12, + P010, +} + +impl Entry { + pub const fn name(self) -> &'static str { + match self { + Self::Nv12 => "gs_nv12_available", + Self::P010 => "gs_p010_available", + } + } +} + +/// Field access and dispatch are reached only after the corresponding checks. +pub trait Host { + fn has_context(&self) -> bool; + fn debug(&mut self, entry: Entry); + fn has_callback(&self, entry: Entry) -> bool; + fn dispatch(&mut self, entry: Entry) -> bool; +} + +pub fn available(host: &mut H, entry: Entry) -> bool { + if !host.has_context() { + host.debug(entry); + return false; + } + host.has_callback(entry) && host.dispatch(entry) +} diff --git a/rust/obs-graphics/tests/render/main.rs b/rust/obs-graphics/tests/render/main.rs index 8946e06083..87f2ff6753 100644 --- a/rust/obs-graphics/tests/render/main.rs +++ b/rust/obs-graphics/tests/render/main.rs @@ -51,6 +51,8 @@ mod matrix_entrypoints; mod matrix_entrypoints_parity; mod perspective; mod perspective_parity; +mod planar_capability_queries; +mod planar_capability_queries_parity; mod present_entrypoints; mod present_entrypoints_parity; mod raster_entrypoints; diff --git a/rust/obs-graphics/tests/render/planar_capability_queries.rs b/rust/obs-graphics/tests/render/planar_capability_queries.rs new file mode 100644 index 0000000000..fe58d45fdf --- /dev/null +++ b/rust/obs-graphics/tests/render/planar_capability_queries.rs @@ -0,0 +1,114 @@ +//! Tier 1 mirrors test_graphics_planar_capability_queries.c, using fake devices. +use obs_graphics::planar_capability_queries::{Entry, Host, available}; + +#[derive(Default)] +struct Fake { + context: bool, + callbacks: [bool; 2], + results: [bool; 2], + device: usize, + calls: [usize; 2], + devices: Vec, + logs: Vec, +} + +fn index(entry: Entry) -> usize { + match entry { + Entry::Nv12 => 0, + Entry::P010 => 1, + } +} + +impl Host for Fake { + fn has_context(&self) -> bool { + self.context + } + fn debug(&mut self, entry: Entry) { + self.logs.push(format!( + "{}: called while not in a graphics context", + entry.name() + )); + } + fn has_callback(&self, entry: Entry) -> bool { + assert!(self.context, "no projections without a context"); + self.callbacks[index(entry)] + } + fn dispatch(&mut self, entry: Entry) -> bool { + assert!(self.context); + assert!(self.callbacks[index(entry)]); + self.calls[index(entry)] += 1; + self.devices.push(self.device); + self.results[index(entry)] + } +} + +#[test] +fn test_graphics_planar_capability_queries_no_context() { + let mut host = Fake { + callbacks: [true; 2], + ..Fake::default() + }; + for i in 0..2 { + assert!(!available(&mut host, Entry::Nv12)); + assert_eq!( + host.logs.last().unwrap(), + "gs_nv12_available: called while not in a graphics context" + ); + assert_eq!(host.logs.len(), 2 * i + 1); + assert!(!available(&mut host, Entry::P010)); + assert_eq!( + host.logs.last().unwrap(), + "gs_p010_available: called while not in a graphics context" + ); + assert_eq!(host.logs.len(), 2 * i + 2); + assert_eq!(host.calls, [0, 0]); + assert!(host.devices.is_empty()); + host.callbacks = [false; 2]; + } +} + +#[test] +fn test_graphics_planar_capability_queries_optional_callbacks_are_independent() { + let mut host = Fake { + context: true, + callbacks: [false, true], + results: [true; 2], + ..Fake::default() + }; + assert!(!available(&mut host, Entry::Nv12)); + assert!(available(&mut host, Entry::P010)); + assert_eq!(host.calls, [0, 1]); + host.callbacks = [true, false]; + assert!(!available(&mut host, Entry::P010)); + assert!(available(&mut host, Entry::Nv12)); + assert_eq!(host.calls, [1, 1]); + host.callbacks = [false; 2]; + assert!(!available(&mut host, Entry::Nv12)); + assert!(!available(&mut host, Entry::P010)); + assert_eq!(host.calls, [1, 1]); + assert!(host.logs.is_empty()); +} + +#[test] +fn test_graphics_planar_capability_queries_callback_results_and_device_routing() { + let mut host = Fake { + callbacks: [true; 2], + ..Fake::default() + }; + for device in 0..2 { + host.device = device; + host.context = true; + for results in 0..4 { + host.results = [results & 1 != 0, results & 2 != 0]; + let calls = device * 4 + results; + assert_eq!(available(&mut host, Entry::Nv12), host.results[0]); + assert_eq!(host.calls, [calls + 1, calls]); + assert_eq!(available(&mut host, Entry::P010), host.results[1]); + assert_eq!(host.calls, [calls + 1, calls + 1]); + assert_eq!(&host.devices[2 * calls..], &[device, device]); + assert!(host.logs.is_empty()); + } + host.context = false; + assert!(!host.has_context()); + } +} diff --git a/rust/obs-graphics/tests/render/planar_capability_queries_parity.rs b/rust/obs-graphics/tests/render/planar_capability_queries_parity.rs new file mode 100644 index 0000000000..0879d5ec34 --- /dev/null +++ b/rust/obs-graphics/tests/render/planar_capability_queries_parity.rs @@ -0,0 +1,145 @@ +//! Tier 3: original C and the Rust ABI use real adapters over fake TLS devices. +//! No intentional differences; no host capability or GPU is queried. +use obs_c_oracle as _; +use obs_graphics::ffi::planar_capability_queries as rust; +use proptest::prelude::*; +use std::ffi::{CStr, c_char}; + +unsafe extern "C" { + fn oracle_gs_nv12_available() -> bool; + fn oracle_gs_p010_available() -> bool; + fn oracle_planar_capability_reset( + context: bool, + nv12: bool, + p010: bool, + nv12_result: bool, + p010_result: bool, + device: bool, + ); + fn oracle_planar_capability_calls(p010: bool) -> u32; + fn oracle_planar_capability_last_device() -> u32; + fn oracle_device_queries_logs() -> u32; + fn oracle_device_queries_level() -> i32; + fn oracle_device_queries_log() -> *const c_char; +} + +#[derive(Clone, Copy, Debug)] +struct State { + context: bool, + callbacks: [bool; 2], + results: [bool; 2], + device: bool, +} + +#[derive(Debug, PartialEq, Eq)] +struct Observed { + value: bool, + calls: [u32; 2], + last_device: u32, + logs: u32, + level: i32, + message: Vec, +} + +fn run(ported: bool, state: State, queries: &[bool]) -> Vec { + // SAFETY: all fixture state, devices, logging and callbacks are thread-local + // and live throughout the sequence. The C headers supply the real layouts. + unsafe { + oracle_planar_capability_reset( + state.context, + state.callbacks[0], + state.callbacks[1], + state.results[0], + state.results[1], + state.device, + ); + queries + .iter() + .map(|p010| { + let value = match (ported, p010) { + (true, false) => rust::gs_nv12_available(), + (true, true) => rust::gs_p010_available(), + (false, false) => oracle_gs_nv12_available(), + (false, true) => oracle_gs_p010_available(), + }; + Observed { + value, + calls: [ + oracle_planar_capability_calls(false), + oracle_planar_capability_calls(true), + ], + last_device: oracle_planar_capability_last_device(), + logs: oracle_device_queries_logs(), + level: oracle_device_queries_level(), + message: CStr::from_ptr(oracle_device_queries_log()) + .to_bytes() + .to_vec(), + } + }) + .collect() + } +} + +fn compare(state: State, queries: &[bool]) { + let original = run(false, state, queries); + let ported = run(true, state, queries); + assert_eq!(ported, original, "{state:?}, {queries:?}"); + let mut calls = [0; 2]; + let mut device = 2; + for (i, (&p010, observed)) in queries.iter().zip(ported).enumerate() { + let index = usize::from(p010); + let dispatch = state.context && state.callbacks[index]; + assert_eq!(observed.value, dispatch && state.results[index]); + if dispatch { + calls[index] += 1; + device = u32::from(state.device); + } + assert_eq!(observed.calls, calls); + assert_eq!(observed.last_device, device); + if state.context { + assert_eq!((observed.logs, observed.level), (0, 0)); + assert!(observed.message.is_empty()); + } else { + assert_eq!(observed.logs as usize, i + 1); + assert_eq!(observed.level, 400); + let function = if p010 { + "gs_p010_available" + } else { + "gs_nv12_available" + }; + assert_eq!( + observed.message, + format!("{function}: called while not in a graphics context").as_bytes() + ); + } + } +} + +#[test] +fn all_fake_device_callback_and_result_combinations_match_c() { + for bits in 0..64 { + compare( + State { + context: bits & 1 != 0, + callbacks: [bits & 2 != 0, bits & 4 != 0], + results: [bits & 8 != 0, bits & 16 != 0], + device: bits & 32 != 0, + }, + &[false, true, true, false], + ); + } +} + +proptest! { + #![proptest_config(ProptestConfig { + cases: obs_c_oracle::parity::cases(128), failure_persistence: None, ..ProptestConfig::default() + })] + #[test] + fn bounded_query_sequences_match_c( + context in any::(), callbacks in any::<[bool; 2]>(), + results in any::<[bool; 2]>(), device in any::(), + queries in prop::collection::vec(any::(), 0..=16), + ) { + compare(State { context, callbacks, results, device }, &queries); + } +}