From d6403134b79e91a92a66506a666b4422d41c1f58 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 06:38:24 -0800 Subject: [PATCH 1/2] test(graphics): mirror indexbuffer queries against Rust stubs (Refs #5241) --- rust/obs-graphics/src/indexbuffer_queries.rs | 49 +++++++++++ rust/obs-graphics/src/lib.rs | 1 + .../tests/render/indexbuffer_queries.rs | 85 +++++++++++++++++++ rust/obs-graphics/tests/render/main.rs | 1 + 4 files changed, 136 insertions(+) create mode 100644 rust/obs-graphics/src/indexbuffer_queries.rs create mode 100644 rust/obs-graphics/tests/render/indexbuffer_queries.rs diff --git a/rust/obs-graphics/src/indexbuffer_queries.rs b/rust/obs-graphics/src/indexbuffer_queries.rs new file mode 100644 index 0000000000..715e6117d5 --- /dev/null +++ b/rust/obs-graphics/src/indexbuffer_queries.rs @@ -0,0 +1,49 @@ +//! Index-buffer queries without graphics layouts or GPU dependencies. + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Entry { + Data, + Type, +} + +impl Entry { + pub const fn name(self) -> &'static str { + match self { + Self::Data => "gs_indexbuffer_get_data", + Self::Type => "gs_indexbuffer_get_type", + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Rejection { + Context, + Buffer, +} + +impl Rejection { + pub const fn message(self) -> &'static str { + match self { + Self::Context => "called while not in a graphics context", + Self::Buffer => "Null 'indexbuffer' parameter", + } + } +} + +/// Callbacks are mandatory and reached only after context and buffer checks. +pub trait Host { + type Buffer: Copy; + type Data; + fn has_context(&self) -> bool; + fn debug(&mut self, entry: Entry, reason: Rejection); + fn data(&mut self, buffer: Self::Buffer) -> Option; + fn index_type(&mut self, buffer: Self::Buffer) -> u32; +} + +pub fn get_data(_host: &mut H, _buffer: Option) -> Option { + todo!("indexbuffer query port") +} + +pub fn get_type(_host: &mut H, _buffer: Option) -> u32 { + todo!("indexbuffer query port") +} diff --git a/rust/obs-graphics/src/lib.rs b/rust/obs-graphics/src/lib.rs index 63106af02a..505999fe46 100644 --- a/rust/obs-graphics/src/lib.rs +++ b/rust/obs-graphics/src/lib.rs @@ -76,6 +76,7 @@ pub mod immediate_arguments; pub mod immediate_scalars; pub mod indexbuffer_create; pub mod indexbuffer_maintenance; +pub mod indexbuffer_queries; pub mod linear_srgb; pub mod lzw; pub mod math_extra; diff --git a/rust/obs-graphics/tests/render/indexbuffer_queries.rs b/rust/obs-graphics/tests/render/indexbuffer_queries.rs new file mode 100644 index 0000000000..be02651dcf --- /dev/null +++ b/rust/obs-graphics/tests/render/indexbuffer_queries.rs @@ -0,0 +1,85 @@ +//! Tier 1 mirrors every assertion in test_graphics_indexbuffer_queries.c. +use obs_graphics::indexbuffer_queries::{Entry, Host, Rejection, get_data, get_type}; + +#[derive(Default)] +struct Fake { + context: bool, + expected_buffer: usize, + result: Option, + index_type: u32, + calls: [usize; 2], + logs: Vec, +} + +impl Host for Fake { + type Buffer = usize; + type Data = usize; + fn has_context(&self) -> bool { + self.context + } + fn debug(&mut self, entry: Entry, reason: Rejection) { + self.logs.push(format!("{}: {}", entry.name(), reason.message())); + } + fn data(&mut self, buffer: usize) -> Option { + assert!(self.context, "no projection without context"); + assert_eq!(buffer, self.expected_buffer); + self.calls[0] += 1; + self.result + } + fn index_type(&mut self, buffer: usize) -> u32 { + assert!(self.context, "no projection without context"); + assert_eq!(buffer, self.expected_buffer); + self.calls[1] += 1; + self.index_type + } +} + +fn diagnostic(host: &Fake, name: &str, reason: &str, count: usize) { + assert_eq!(host.logs.last().unwrap(), &format!("{name}: {reason}")); + assert_eq!(host.logs.len(), count); + assert_eq!(host.calls, [0, 0]); +} + +#[test] +fn test_graphics_indexbuffer_queries_context_before_pointers() { + let mut host = Fake::default(); + for (i, buffer) in [None, Some(0)].into_iter().enumerate() { + assert_eq!(get_data(&mut host, buffer), None); + diagnostic(&host, "gs_indexbuffer_get_data", "called while not in a graphics context", i * 2 + 1); + assert_eq!(get_type(&mut host, buffer), 0); + diagnostic(&host, "gs_indexbuffer_get_type", "called while not in a graphics context", i * 2 + 2); + } +} + +#[test] +fn test_graphics_indexbuffer_queries_pointer_guards() { + let mut host = Fake { context: true, ..Fake::default() }; + assert_eq!(get_data(&mut host, None), None); + diagnostic(&host, "gs_indexbuffer_get_data", "Null 'indexbuffer' parameter", 1); + assert_eq!(get_type(&mut host, None), 0); + diagnostic(&host, "gs_indexbuffer_get_type", "Null 'indexbuffer' parameter", 2); +} + +#[test] +fn test_graphics_indexbuffer_queries_backend_results_and_buffer_routing() { + let mut host = Fake { context: true, ..Fake::default() }; + for buffer in 0..2 { + host.expected_buffer = buffer; + for data in [None, Some(0), Some(1)] { + host.result = data; + let before = host.calls; + assert_eq!(get_data(&mut host, Some(buffer)), data); + assert_eq!(host.calls, [before[0] + 1, before[1]]); + assert!(host.logs.is_empty()); + } + // GS_UNSIGNED_SHORT and GS_UNSIGNED_LONG; Tier 3 checks real C values. + for index_type in [0, 1] { + host.index_type = index_type; + let before = host.calls; + assert_eq!(get_type(&mut host, Some(buffer)), index_type); + assert_eq!(host.calls, [before[0], before[1] + 1]); + assert!(host.logs.is_empty()); + } + } + assert_eq!(host.calls, [6, 4]); +} diff --git a/rust/obs-graphics/tests/render/main.rs b/rust/obs-graphics/tests/render/main.rs index 87f2ff6753..82d092e3fd 100644 --- a/rust/obs-graphics/tests/render/main.rs +++ b/rust/obs-graphics/tests/render/main.rs @@ -45,6 +45,7 @@ mod indexbuffer_create; mod indexbuffer_create_parity; mod indexbuffer_maintenance; mod indexbuffer_maintenance_parity; +mod indexbuffer_queries; mod lzw; mod lzw_parity; mod matrix_entrypoints; From f1e3ff53b6af20eca50b4b5453d9cc45a5dee1a0 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Thu, 19 Nov 2026 06:43:08 -0800 Subject: [PATCH 2/2] graphics: swap indexbuffer data and type queries to Rust (Refs #5241) Ci-Attestation: {"at":1795101944,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":292,"stamp":"90810a72dc103e11ac0971f27bf19638","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101944,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":292,"stamp":"0e0b4aee1e2ebf8aa13feb8793ada49b","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101944,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":292,"stamp":"67bc3e7b42f06752a2b55fe2748f4067","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101944,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":292,"stamp":"bfa23af5b44beffbffdaf864a797c380","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"general/all/client-lint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"2ffe79f40aa68212abd8b481bcd4417a","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101944,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":292,"stamp":"d67bcebdc24488c4a37d02da9edc7b4c","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"81c0fc7fe3cdf5d109a76b5213fb1268","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"6221383a869ad6cd74d6ffc70795a185","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"4fb10323e51b51e564b19e2b13162222","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"9040d880d35e3efc0c21c9301519237d","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"f71a846200d80980cd7cbb164e9d0c95","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"462f3c196f29ad9e60d0b62fe1d6daa1","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"bbe2dd94e1ae316db5258b90fc3171bd","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101651,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"c18de6e60195a66286315f893ef94867","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"e2dc01ef0df93e9d5eabe3075c996997","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"12adf31f722dc700ab284f85655edbb4","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"591a1eece8c586d1ba8ba9423be7a68e","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795101650,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"93111b4438f29efde821313aa55a364c102b350d709a098ddc35e7f45721ad09","lane":"client-lint","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":255,"stamp":"4059c0ecd9e95503622cbf59d1828291","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Ci-Attestation: {"at":1795102610,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"2acb01e87ea4b9ffc68558a62f29391f24c9672328a20149bd2683e85aaa6948","lane":"client-test-x86_64-unknown-linux-gnu","parents":["d6403134b79e91a92a66506a666b4422d41c1f58"],"secs":664,"stamp":"280cc3dfb5b788f2f188b59cf06f05bb","tree":"dd931add54df26111752eb5aa0a232d314e6cebf","v":1,"via":"run"} Client-Checks: {"checks":{"base":"2ab51880d128cc5892ab1cc4291cb6931d1d8230","coverage":"all-tracked-applicable-files","diff":["libobs/build/libobs.core-source+.h.c","libobs/build/libobs.graphics+.h.c","libobs/build/libobs.ported-graphics-indexbuffer-queries+.h.c","libobs/build/ported-source-sets/graphics-indexbuffer-queries.json","libobs/cmake/rust-exports.d/obs-graphics-indexbuffer_queries.txt","libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h","libobs/graphics/graphics-indexbuffer-queries.c.h","libobs/obs-source-audio-capture-callbacks.c.h","libobs/obs-source.c.h","rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c","rust/obs-c-oracle/oracle.d/graphics-indexbuffer-queries.list","rust/obs-c-oracle/oracle/indexbuffer_maintenance.c","rust/obs-graphics/src/ffi/indexbuffer_queries.rs","rust/obs-graphics/src/ffi/mod.rs","rust/obs-graphics/src/indexbuffer_queries.rs","rust/obs-graphics/src/lib.rs","rust/obs-graphics/tests/render/indexbuffer_queries.rs","rust/obs-graphics/tests/render/indexbuffer_queries_parity.rs","rust/obs-graphics/tests/render/main.rs","test/cmocka/build/test-graph.json","test/cmocka/test_source_audio_capture_callbacks.c","test/cmocka/tests.d/test_source_audio_capture_callbacks.cmake"],"head":"042d0733502c543ff9ef176793dd14592e97178b","tools":{"orchestrator-python":"3.14.7 (main, Aug 14 2026, 15:35:28) [Clang 22.1.3 ]","schema-dependencies":"3.14.7 (main, Aug 14 2026, 15:35:28) [Clang 22.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21504\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ISO8859X Regexps Automata RelaxNG Schemas Modules Debug"},"tracked-digest":"1a22112ec085a2b9f3eac8437bfe9196f4414c5b2e1db264cf634f0b1d17e786","tree":"dd931add54df26111752eb5aa0a232d314e6cebf"}} --- libobs/build/libobs.graphics+.h.c | 1 - ...s.ported-graphics-indexbuffer-queries+.h.c | 4 + .../graphics-indexbuffer-queries.json | 5 + .../obs-graphics-indexbuffer_queries.txt | 2 + ...raphics-indexbuffer-maintenance-fields.c.h | 12 ++ .../graphics/graphics-indexbuffer-queries.c.h | 32 +++- .../legacy/graphics-indexbuffer-queries.c | 28 ++++ .../graphics-indexbuffer-queries.list | 1 + .../oracle/indexbuffer_maintenance.c | 39 +++++ .../src/ffi/indexbuffer_queries.rs | 74 +++++++++ rust/obs-graphics/src/ffi/mod.rs | 1 + rust/obs-graphics/src/indexbuffer_queries.rs | 19 ++- .../tests/render/indexbuffer_queries.rs | 41 ++++- .../render/indexbuffer_queries_parity.rs | 150 ++++++++++++++++++ rust/obs-graphics/tests/render/main.rs | 1 + 15 files changed, 394 insertions(+), 16 deletions(-) create mode 100644 libobs/build/libobs.ported-graphics-indexbuffer-queries+.h.c create mode 100644 libobs/build/ported-source-sets/graphics-indexbuffer-queries.json create mode 100644 libobs/cmake/rust-exports.d/obs-graphics-indexbuffer_queries.txt create mode 100644 rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c create mode 100644 rust/obs-c-oracle/oracle.d/graphics-indexbuffer-queries.list create mode 100644 rust/obs-graphics/src/ffi/indexbuffer_queries.rs create mode 100644 rust/obs-graphics/tests/render/indexbuffer_queries_parity.rs diff --git a/libobs/build/libobs.graphics+.h.c b/libobs/build/libobs.graphics+.h.c index 17f38ebad4..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-indexbuffer-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-indexbuffer-queries+.h.c b/libobs/build/libobs.ported-graphics-indexbuffer-queries+.h.c new file mode 100644 index 0000000000..6f01e3534e --- /dev/null +++ b/libobs/build/libobs.ported-graphics-indexbuffer-queries+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original indexbuffer queries for the C reference. */ +#include "libobs/build/libobs-graphics.pch.h" + +#include "libobs/graphics/graphics-indexbuffer-queries.c.h" diff --git a/libobs/build/ported-source-sets/graphics-indexbuffer-queries.json b/libobs/build/ported-source-sets/graphics-indexbuffer-queries.json new file mode 100644 index 0000000000..23e973ec9a --- /dev/null +++ b/libobs/build/ported-source-sets/graphics-indexbuffer-queries.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-graphics-indexbuffer-queries+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-graphics-indexbuffer_queries.txt b/libobs/cmake/rust-exports.d/obs-graphics-indexbuffer_queries.txt new file mode 100644 index 0000000000..335a1a9496 --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-graphics-indexbuffer_queries.txt @@ -0,0 +1,2 @@ +gs_indexbuffer_get_data +gs_indexbuffer_get_type diff --git a/libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h b/libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h index ca37b04df0..603e8dc4f9 100644 --- a/libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h +++ b/libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h @@ -36,3 +36,15 @@ size_t obs_graphics_indexbuffer_maintenance_count(graphics_t *graphics, const gs { return graphics->exports.gs_indexbuffer_get_num_indices(buffer); } + +void *obs_graphics_indexbuffer_maintenance_data(graphics_t *graphics, const gs_indexbuffer_t *buffer); +void *obs_graphics_indexbuffer_maintenance_data(graphics_t *graphics, const gs_indexbuffer_t *buffer) +{ + return graphics->exports.gs_indexbuffer_get_data(buffer); +} + +enum gs_index_type obs_graphics_indexbuffer_maintenance_type(graphics_t *graphics, const gs_indexbuffer_t *buffer); +enum gs_index_type obs_graphics_indexbuffer_maintenance_type(graphics_t *graphics, const gs_indexbuffer_t *buffer) +{ + return graphics->exports.gs_indexbuffer_get_type(buffer); +} diff --git a/libobs/graphics/graphics-indexbuffer-queries.c.h b/libobs/graphics/graphics-indexbuffer-queries.c.h index dcc3783eee..8c4e88554c 100644 --- a/libobs/graphics/graphics-indexbuffer-queries.c.h +++ b/libobs/graphics/graphics-indexbuffer-queries.c.h @@ -1,9 +1,30 @@ #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_obj_valid(const void *obj, const char *f, const char *name); -static inline bool gs_valid(const char *f); +/* Reference-only validators; keep the original function bodies below. */ +#define thread_graphics gs_get_context() + +static inline bool indexbuffer_queries_ref_valid(const char *f) +{ + if (!thread_graphics) { + blog(LOG_DEBUG, "%s: called while not in a graphics context", f); + return false; + } + + return true; +} + +static inline bool indexbuffer_queries_ref_obj_valid(const void *obj, const char *f, const char *name) +{ + if (!obj) { + blog(LOG_DEBUG, "%s: Null '%s' parameter", f, name); + return false; + } + + return true; +} + +#define gs_valid indexbuffer_queries_ref_valid +#define gs_obj_valid indexbuffer_queries_ref_obj_valid #define ptr_valid(ptr, func) gs_obj_valid(ptr, func, #ptr) #define gs_valid_p(func, param1) (gs_valid(func) && ptr_valid(param1, func)) @@ -26,3 +47,6 @@ enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer) #undef gs_valid_p #undef ptr_valid +#undef gs_obj_valid +#undef gs_valid +#undef thread_graphics diff --git a/rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c b/rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c new file mode 100644 index 0000000000..dcc3783eee --- /dev/null +++ b/rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c @@ -0,0 +1,28 @@ +#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_obj_valid(const void *obj, const char *f, const char *name); +static inline bool gs_valid(const char *f); + +#define ptr_valid(ptr, func) gs_obj_valid(ptr, func, #ptr) +#define gs_valid_p(func, param1) (gs_valid(func) && ptr_valid(param1, func)) + +void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer) +{ + if (!gs_valid_p("gs_indexbuffer_get_data", indexbuffer)) + return NULL; + + return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer); +} + +enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer) +{ + if (!gs_valid_p("gs_indexbuffer_get_type", indexbuffer)) + return (enum gs_index_type)0; + + return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer); +} + +#undef gs_valid_p +#undef ptr_valid diff --git a/rust/obs-c-oracle/oracle.d/graphics-indexbuffer-queries.list b/rust/obs-c-oracle/oracle.d/graphics-indexbuffer-queries.list new file mode 100644 index 0000000000..56a8d49ce8 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/graphics-indexbuffer-queries.list @@ -0,0 +1 @@ +graphics-indexbuffer-queries.inc|rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c|auto diff --git a/rust/obs-c-oracle/oracle/indexbuffer_maintenance.c b/rust/obs-c-oracle/oracle/indexbuffer_maintenance.c index ef246e15b0..307cbd6419 100644 --- a/rust/obs-c-oracle/oracle/indexbuffer_maintenance.c +++ b/rust/obs-c-oracle/oracle/indexbuffer_maintenance.c @@ -8,6 +8,8 @@ #define gs_indexbuffer_flush oracle_gs_indexbuffer_flush #define gs_indexbuffer_flush_direct oracle_gs_indexbuffer_flush_direct #define gs_indexbuffer_get_num_indices oracle_gs_indexbuffer_get_num_indices +#define gs_indexbuffer_get_data oracle_gs_indexbuffer_get_data +#define gs_indexbuffer_get_type oracle_gs_indexbuffer_get_type #include "graphics/graphics-internal.h" #include "util/base.h" @@ -15,6 +17,8 @@ void oracle_gs_indexbuffer_destroy(gs_indexbuffer_t *buffer); void oracle_gs_indexbuffer_flush(gs_indexbuffer_t *buffer); void oracle_gs_indexbuffer_flush_direct(gs_indexbuffer_t *buffer, const void *data); size_t oracle_gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *buffer); +void *oracle_gs_indexbuffer_get_data(const gs_indexbuffer_t *buffer); +enum gs_index_type oracle_gs_indexbuffer_get_type(const gs_indexbuffer_t *buffer); static THREAD_LOCAL struct graphics_subsystem ibm_graphics; static THREAD_LOCAL unsigned ibm_calls, ibm_logs; @@ -38,6 +42,7 @@ static void ibm_blog(int level, const char *format, ...) #define blog ibm_blog #include "graphics-indexbuffer-maintenance.inc" +#include "graphics-indexbuffer-queries.inc" /* Original graphics.c.h validators, verbatim. */ static inline bool gs_valid(const char *f) @@ -156,7 +161,41 @@ const char *oracle_indexbuffer_maintenance_log(void) return ibm_log; } +/* The two queries share the same real adapter, TLS and recorder as maintenance. */ +static THREAD_LOCAL void *ibm_query_data; +static THREAD_LOCAL enum gs_index_type ibm_query_type; + +static void *ibm_get_data(const gs_indexbuffer_t *buffer) +{ + ibm_record(4, buffer, NULL); + return ibm_query_data; +} + +static enum gs_index_type ibm_get_type(const gs_indexbuffer_t *buffer) +{ + ibm_record(5, buffer, NULL); + return ibm_query_type; +} + +void oracle_indexbuffer_queries_reset(bool context, void *data, bool long_indices); +void oracle_indexbuffer_queries_reset(bool context, void *data, bool long_indices) +{ + oracle_indexbuffer_maintenance_reset(context, 0); + ibm_query_data = data; + ibm_query_type = long_indices ? GS_UNSIGNED_LONG : GS_UNSIGNED_SHORT; + ibm_graphics.exports.gs_indexbuffer_get_data = ibm_get_data; + ibm_graphics.exports.gs_indexbuffer_get_type = ibm_get_type; +} + +unsigned oracle_indexbuffer_queries_type(bool long_indices); +unsigned oracle_indexbuffer_queries_type(bool long_indices) +{ + return (unsigned)(long_indices ? GS_UNSIGNED_LONG : GS_UNSIGNED_SHORT); +} + #undef gs_indexbuffer_destroy #undef gs_indexbuffer_flush #undef gs_indexbuffer_flush_direct #undef gs_indexbuffer_get_num_indices +#undef gs_indexbuffer_get_data +#undef gs_indexbuffer_get_type diff --git a/rust/obs-graphics/src/ffi/indexbuffer_queries.rs b/rust/obs-graphics/src/ffi/indexbuffer_queries.rs new file mode 100644 index 0000000000..c2caac41b6 --- /dev/null +++ b/rust/obs-graphics/src/ffi/indexbuffer_queries.rs @@ -0,0 +1,74 @@ +//! Thin ABI shims over the shared indexbuffer context, log and dispatch seam. +use crate::indexbuffer_queries::{self as core, Entry, Host, Rejection}; +use std::ffi::{c_char, c_uint, c_void}; + +unsafe extern "C" { + fn obs_graphics_indexbuffer_maintenance_context() -> *mut c_void; + fn obs_graphics_indexbuffer_maintenance_debug(function: *const c_char, reason: *const c_char); + fn obs_graphics_indexbuffer_maintenance_data( + context: *mut c_void, + buffer: *const c_void, + ) -> *mut c_void; + fn obs_graphics_indexbuffer_maintenance_type( + context: *mut c_void, + buffer: *const c_void, + ) -> c_uint; +} + +struct Context(*mut c_void); + +fn current() -> Context { + // SAFETY: reads the current thread's context without projecting any fields. + Context(unsafe { obs_graphics_indexbuffer_maintenance_context() }) +} + +impl Host for Context { + type Buffer = *const c_void; + type Data = *mut c_void; + fn has_context(&self) -> bool { + !self.0.is_null() + } + fn debug(&mut self, entry: Entry, reason: Rejection) { + let name = match entry { + Entry::Data => c"gs_indexbuffer_get_data", + Entry::Type => c"gs_indexbuffer_get_type", + }; + let message = match reason { + Rejection::Context => c"called while not in a graphics context", + Rejection::Buffer => c"Null 'indexbuffer' parameter", + }; + // SAFETY: both strings are static and NUL-terminated. + unsafe { obs_graphics_indexbuffer_maintenance_debug(name.as_ptr(), message.as_ptr()) } + } + fn data(&mut self, buffer: *const c_void) -> Option<*mut c_void> { + // SAFETY: the safe core checked context and buffer before field projection. + let data = unsafe { obs_graphics_indexbuffer_maintenance_data(self.0, buffer) }; + (!data.is_null()).then_some(data) + } + fn index_type(&mut self, buffer: *const c_void) -> u32 { + // SAFETY: the safe core checked context and buffer before field projection. + unsafe { obs_graphics_indexbuffer_maintenance_type(self.0, buffer) } + } +} + +fn optional(buffer: *const c_void) -> Option<*const c_void> { + (!buffer.is_null()).then_some(buffer) +} + +/// Forward the backend's data pointer, including a successful NULL result. +/// # Safety +/// A current context must have a live backend and mandatory callback; a non-NULL +/// buffer must meet that backend's contract. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn gs_indexbuffer_get_data(buffer: *const c_void) -> *mut c_void { + core::get_data(&mut current(), optional(buffer)).unwrap_or(std::ptr::null_mut()) +} + +/// Forward the backend's index enum, preserving the C enum-cast-zero rejection. +/// # Safety +/// A current context must have a live backend and mandatory callback; a non-NULL +/// buffer must meet that backend's contract. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn gs_indexbuffer_get_type(buffer: *const c_void) -> c_uint { + core::get_type(&mut current(), optional(buffer)) +} diff --git a/rust/obs-graphics/src/ffi/mod.rs b/rust/obs-graphics/src/ffi/mod.rs index 3116f1bfb8..2f1cc6c0bb 100644 --- a/rust/obs-graphics/src/ffi/mod.rs +++ b/rust/obs-graphics/src/ffi/mod.rs @@ -32,6 +32,7 @@ pub mod immediate_arguments; pub mod immediate_scalars; pub mod indexbuffer_create; pub mod indexbuffer_maintenance; +pub mod indexbuffer_queries; pub mod linear_srgb; pub mod lzw; pub mod math_extra; diff --git a/rust/obs-graphics/src/indexbuffer_queries.rs b/rust/obs-graphics/src/indexbuffer_queries.rs index 715e6117d5..9a67a2778d 100644 --- a/rust/obs-graphics/src/indexbuffer_queries.rs +++ b/rust/obs-graphics/src/indexbuffer_queries.rs @@ -40,10 +40,21 @@ pub trait Host { fn index_type(&mut self, buffer: Self::Buffer) -> u32; } -pub fn get_data(_host: &mut H, _buffer: Option) -> Option { - todo!("indexbuffer query port") +fn valid(host: &mut H, entry: Entry, buffer: Option) -> Option { + if !host.has_context() { + host.debug(entry, Rejection::Context); + return None; + } + if buffer.is_none() { + host.debug(entry, Rejection::Buffer); + } + buffer +} + +pub fn get_data(host: &mut H, buffer: Option) -> Option { + valid(host, Entry::Data, buffer).and_then(|buffer| host.data(buffer)) } -pub fn get_type(_host: &mut H, _buffer: Option) -> u32 { - todo!("indexbuffer query port") +pub fn get_type(host: &mut H, buffer: Option) -> u32 { + valid(host, Entry::Type, buffer).map_or(0, |buffer| host.index_type(buffer)) } diff --git a/rust/obs-graphics/tests/render/indexbuffer_queries.rs b/rust/obs-graphics/tests/render/indexbuffer_queries.rs index be02651dcf..12411b87e8 100644 --- a/rust/obs-graphics/tests/render/indexbuffer_queries.rs +++ b/rust/obs-graphics/tests/render/indexbuffer_queries.rs @@ -18,7 +18,8 @@ impl Host for Fake { self.context } fn debug(&mut self, entry: Entry, reason: Rejection) { - self.logs.push(format!("{}: {}", entry.name(), reason.message())); + self.logs + .push(format!("{}: {}", entry.name(), reason.message())); } fn data(&mut self, buffer: usize) -> Option { assert!(self.context, "no projection without context"); @@ -45,24 +46,50 @@ fn test_graphics_indexbuffer_queries_context_before_pointers() { let mut host = Fake::default(); for (i, buffer) in [None, Some(0)].into_iter().enumerate() { assert_eq!(get_data(&mut host, buffer), None); - diagnostic(&host, "gs_indexbuffer_get_data", "called while not in a graphics context", i * 2 + 1); + diagnostic( + &host, + "gs_indexbuffer_get_data", + "called while not in a graphics context", + i * 2 + 1, + ); assert_eq!(get_type(&mut host, buffer), 0); - diagnostic(&host, "gs_indexbuffer_get_type", "called while not in a graphics context", i * 2 + 2); + diagnostic( + &host, + "gs_indexbuffer_get_type", + "called while not in a graphics context", + i * 2 + 2, + ); } } #[test] fn test_graphics_indexbuffer_queries_pointer_guards() { - let mut host = Fake { context: true, ..Fake::default() }; + let mut host = Fake { + context: true, + ..Fake::default() + }; assert_eq!(get_data(&mut host, None), None); - diagnostic(&host, "gs_indexbuffer_get_data", "Null 'indexbuffer' parameter", 1); + diagnostic( + &host, + "gs_indexbuffer_get_data", + "Null 'indexbuffer' parameter", + 1, + ); assert_eq!(get_type(&mut host, None), 0); - diagnostic(&host, "gs_indexbuffer_get_type", "Null 'indexbuffer' parameter", 2); + diagnostic( + &host, + "gs_indexbuffer_get_type", + "Null 'indexbuffer' parameter", + 2, + ); } #[test] fn test_graphics_indexbuffer_queries_backend_results_and_buffer_routing() { - let mut host = Fake { context: true, ..Fake::default() }; + let mut host = Fake { + context: true, + ..Fake::default() + }; for buffer in 0..2 { host.expected_buffer = buffer; for data in [None, Some(0), Some(1)] { diff --git a/rust/obs-graphics/tests/render/indexbuffer_queries_parity.rs b/rust/obs-graphics/tests/render/indexbuffer_queries_parity.rs new file mode 100644 index 0000000000..1e882ae49a --- /dev/null +++ b/rust/obs-graphics/tests/render/indexbuffer_queries_parity.rs @@ -0,0 +1,150 @@ +//! Tier 3 compares original C with the real Rust ABI and shared field adapters. +//! No intentional differences or exclusions; all backends are GPU-free fakes. +use obs_c_oracle as _; +use obs_graphics::ffi::indexbuffer_queries as rust; +use proptest::prelude::*; +use std::ffi::{CStr, c_char, c_int, c_uint, c_void}; +use std::ptr; + +unsafe extern "C" { + fn oracle_gs_indexbuffer_get_data(buffer: *const c_void) -> *mut c_void; + fn oracle_gs_indexbuffer_get_type(buffer: *const c_void) -> c_uint; + fn oracle_indexbuffer_queries_reset(context: bool, data: *mut c_void, long_indices: bool); + fn oracle_indexbuffer_queries_type(long_indices: bool) -> c_uint; + fn oracle_indexbuffer_maintenance_calls() -> c_uint; + fn oracle_indexbuffer_maintenance_logs() -> c_uint; + fn oracle_indexbuffer_maintenance_kind() -> c_int; + fn oracle_indexbuffer_maintenance_level() -> c_int; + fn oracle_indexbuffer_maintenance_buffer() -> *const c_void; + fn oracle_indexbuffer_maintenance_log() -> *const c_char; +} + +static TOKENS: [u8; 2] = [0; 2]; + +#[derive(Clone, Copy, Debug)] +struct Step { + context: bool, + buffer: Option, + data: Option, + long_indices: bool, + query_type: bool, +} + +#[derive(Debug, PartialEq)] +struct Observed { + data: *mut c_void, + index_type: c_uint, + calls: c_uint, + kind: c_int, + buffer: *const c_void, + logs: c_uint, + level: c_int, + log: Vec, +} + +fn token(value: Option) -> *mut c_void { + value.map_or(ptr::null_mut(), |i| { + ptr::from_ref(&TOKENS[i]).cast_mut().cast() + }) +} + +fn run(use_rust: bool, step: Step) -> Observed { + // SAFETY: the recorder and real C-layout context are thread-local; live + // static tokens are compared only by identity by the recording callbacks. + unsafe { + oracle_indexbuffer_queries_reset(step.context, token(step.data), step.long_indices); + let buffer = token(step.buffer); + let mut data = ptr::null_mut(); + let mut index_type = 0; + match (use_rust, step.query_type) { + (true, false) => data = rust::gs_indexbuffer_get_data(buffer), + (false, false) => data = oracle_gs_indexbuffer_get_data(buffer), + (true, true) => index_type = rust::gs_indexbuffer_get_type(buffer), + (false, true) => index_type = oracle_gs_indexbuffer_get_type(buffer), + } + Observed { + data, + index_type, + calls: oracle_indexbuffer_maintenance_calls(), + kind: oracle_indexbuffer_maintenance_kind(), + buffer: oracle_indexbuffer_maintenance_buffer(), + logs: oracle_indexbuffer_maintenance_logs(), + level: oracle_indexbuffer_maintenance_level(), + log: CStr::from_ptr(oracle_indexbuffer_maintenance_log()) + .to_bytes() + .to_vec(), + } + } +} + +fn compare(step: Step) { + let original = run(false, step); + let ported = run(true, step); + assert_eq!(ported, original, "{step:?}"); + if step.context && step.buffer.is_some() { + assert_eq!((ported.calls, ported.logs), (1, 0)); + assert_eq!(ported.kind, if step.query_type { 5 } else { 4 }); + assert_eq!(ported.buffer, token(step.buffer).cast_const()); + assert!(ported.log.is_empty()); + if step.query_type { + // SAFETY: this pure helper measures the real public C enum values. + assert_eq!(ported.index_type, unsafe { + oracle_indexbuffer_queries_type(step.long_indices) + }); + } else { + assert_eq!(ported.data, token(step.data)); + } + } else { + assert_eq!((ported.calls, ported.index_type), (0, 0)); + assert!(ported.data.is_null()); + assert!(ported.buffer.is_null()); + assert_eq!((ported.logs, ported.level), (1, 400)); + let name = if step.query_type { + "gs_indexbuffer_get_type" + } else { + "gs_indexbuffer_get_data" + }; + let reason = if step.context { + "Null 'indexbuffer' parameter" + } else { + "called while not in a graphics context" + }; + assert_eq!(ported.log, format!("{name}: {reason}").as_bytes()); + } +} + +#[test] +fn characterized_matrix_matches_c() { + for context in [false, true] { + for buffer in [None, Some(0), Some(1)] { + for data in [None, Some(0), Some(1)] { + for long_indices in [false, true] { + for query_type in [false, true] { + compare(Step { + context, + buffer, + data, + long_indices, + query_type, + }); + } + } + } + } + } +} + +proptest! { + #![proptest_config(ProptestConfig { + cases: obs_c_oracle::parity::cases(128), failure_persistence: None, ..ProptestConfig::default() + })] + #[test] + fn random_sequences_match_c(steps in prop::collection::vec( + (any::(), prop::option::of(0usize..2), prop::option::of(0usize..2), any::(), any::()), + 1..=16, + )) { + for (context, buffer, data, long_indices, query_type) in steps { + compare(Step { context, buffer, data, long_indices, query_type }); + } + } +} diff --git a/rust/obs-graphics/tests/render/main.rs b/rust/obs-graphics/tests/render/main.rs index 82d092e3fd..ad5a5bf32f 100644 --- a/rust/obs-graphics/tests/render/main.rs +++ b/rust/obs-graphics/tests/render/main.rs @@ -46,6 +46,7 @@ mod indexbuffer_create_parity; mod indexbuffer_maintenance; mod indexbuffer_maintenance_parity; mod indexbuffer_queries; +mod indexbuffer_queries_parity; mod lzw; mod lzw_parity; mod matrix_entrypoints;