Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libobs/build/libobs.graphics+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "libobs/graphics/blend-entrypoints-fields.c.h"
#include "libobs/graphics/effect-parser-lifecycle-adapter.c.h"
#include "libobs/graphics/effect-parser.c.h"
#include "libobs/graphics/graphics-active-shader-queries.c.h"
#include "libobs/graphics/graphics-device-load-fields.c.h"
#include "libobs/graphics/graphics-device-queries-fields.c.h"
#include "libobs/graphics/graphics-imports.c.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Unity group (BUILD.md): reference-only active shader queries. */
#include "libobs/build/libobs-graphics.pch.h"

#include "libobs/graphics/graphics-active-shader-queries.c.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"LIBOBS_PORTED_SOURCES": [
"build/libobs.ported-graphics-active-shader-queries+.h.c"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gs_get_vertex_shader
gs_get_pixel_shader
20 changes: 17 additions & 3 deletions libobs/graphics/graphics-active-shader-queries.c.h
Original file line number Diff line number Diff line change
@@ -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: use the existing TLS context. */
#define thread_graphics gs_get_context()

static inline bool active_shader_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 active_shader_ref_valid

gs_shader_t *gs_get_vertex_shader(void)
{
Expand All @@ -23,3 +34,6 @@ gs_shader_t *gs_get_pixel_shader(void)

return graphics->exports.device_get_pixel_shader(graphics->device);
}

#undef gs_valid
#undef thread_graphics
12 changes: 12 additions & 0 deletions libobs/graphics/graphics-device-queries-fields.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ bool obs_graphics_device_queries_p010(graphics_t *graphics)
{
return graphics->exports.device_p010_available(graphics->device);
}

gs_shader_t *obs_graphics_device_queries_vertex_shader(graphics_t *graphics);
gs_shader_t *obs_graphics_device_queries_vertex_shader(graphics_t *graphics)
{
return graphics->exports.device_get_vertex_shader(graphics->device);
}

gs_shader_t *obs_graphics_device_queries_pixel_shader(graphics_t *graphics);
gs_shader_t *obs_graphics_device_queries_pixel_shader(graphics_t *graphics)
{
return graphics->exports.device_get_pixel_shader(graphics->device);
}
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac
| `libobs/graphics/effect-param-types.c.h` | `obs-graphics::effect_param_types` | Swapped in hybrid `libobs` (group `libobs.ported-effect-param-types`). The parameter type decisions of the effect parser (#3162), moved out of `effect-parser.c` first (an extraction, checked old against new C over 200 000 type names in 8 builds), as seven functions on a type name, declared in `effect-param-types.h` without `EXPORT` and hidden, each asked where the C made its decision and read its byte: `obs_effect_param_type` (`get_effect_param_type()`, the shader parameter type for `ep_compile_annotations` and `ep_compile_param`; the core calls the ported `shader_param_names::shader_param_type`, the same map), `obs_effect_param_route` (which default-value parser `ep_parse_param_assignment_val` calls: the texture flag, exact `int` and `float`, the `int` and `float` prefixes, the `string` prefix, exact `bool`, otherwise the "Invalid type" error) and, for `ep_parse_param_assign_intfloat_array`, `obs_effect_param_rows_bad` (before the row error), `_rows` (the row digit, the value count so far), `_has_cols` (an `x` after it), `_cols_bad` (before the column error) and `_cols` (the count times the column digit). `effect-parser.c` keeps every token call, every `cf_adderror()`, the value parsers (their int values and array counts are `effect-param-numbers.c.h`, below), the brace and constructor handling, the loop over the values and the compile. No intentional differences. Characterized, not endorsed, and kept: any name starting with `texture` is a texture; `strings` takes the string parser and `int1` or `float3x3` array values, with an unknown type; a bad digit still counts as a C `char` minus `'0'` (`c_char`, so a non-ASCII byte counts as on each target). A null name, which the C hands to `strcmp()`, reads as `""`. Tests: `test_effect_param_types_steps` (the moved C) and `test_effect_param_types_steps_rust` (the shims), Tier 1 `shader::effect_param_types`, Tier 3 `shader::effect_param_types_parity` (every name of up to four bytes over the bytes that matter, every known name with every tail of up to three, and every byte as a digit). |
| `libobs/graphics/effect-param-numbers.c.h` | `obs-graphics::effect_param_types` | Swapped in hybrid `libobs` (group `libobs.ported-effect-param-types`, beside `effect-param-types.c.h`). The number reads of the effect parser (#3464), moved out of `effect-parser.c` first (an extraction into its own live group, checked old against new C over 200 000 texts at every length in 8 builds), as two functions declared in `effect-param-types.h` without `EXPORT` and hidden: `obs_effect_param_int_value` (the `int` value of `ep_parse_param_assign_intfloat`: `strtol()` of the number token's text, negated after a `-` token) and `obs_effect_param_array_count` (the count of `ep_parse_param_array`: `valid_int_str()` on the token's length, then `strtol()`; the count is written only when it is valid). `effect-parser.c` keeps every token call, the `CFTOKEN_NUM` tests, the float values (`os_strtod`) and the push of each value. The text runs on past the token, as the C passes it: the shims hand the core one byte at a time, which it reads once each in the C's order (`util/lexer.c`'s `valid_int_str()` loop first for a count, then `strtol()`), so it reads no byte the C would not and never scans the effect source to its end (`valid_int_str()` is followed in the core rather than called from obs-util, whose shim reads ahead up to `len + 2` bytes). Characterized, not endorsed, and kept: a float or hex token reads as its leading digits (`1.5` is 1, `0x10` is 0), and a count checks only the token's length while `strtol()` reads every digit after it. One intentional difference (#3464): past `INT_MAX` the C depends on the width of `long` (`3000000000` reads as -1294967296 on Linux and macOS, `-2147483648` as -2147483647 on Windows); the port reads the digits exactly, applies the sign and saturates to `INT_MIN..INT_MAX` on every platform. A null text, which the C hands to `strtol()`, reads as `""` and false. Tests: `test_effect_param_numbers_steps` (the moved C; no `_rust` variant, as its edge cases are the intentional difference) and `test_effect_number_defaults` (the callers through the real libobs), Tier 1 `shader::effect_param_numbers`, Tier 3 `shader::effect_param_numbers_parity` (fixed tokens with more digits after them, random texts of up to 25 bytes at every length, values outside `int` checked against the saturating contract). |
| `libobs/graphics/graphics-device-load.c.h` | `obs-graphics::device_load` | Swapped in hybrid `libobs` (#5032; prerequisite #5023, Refs #5018). The five `gs_load_*` buffer, texture and sampler entry points check the context in safe Rust, preserve exact original-function debug diagnostics, and forward device identity, nullable handles (NULL unbinds), signed units and both default-sampler 3D values unchanged through private C field adapters. The original C remains in its own reference-only ported group and a verbatim legacy oracle copy. Tier 1 mirrors every assertion of unchanged `test_graphics_device_load`; Tier 3 compares the Rust ABI with original C on boundary matrices and random sequences in the existing Graphics category binary. No intentional differences; Tier 4 does not apply. |
| `libobs/graphics/graphics-active-shader-queries.c.h` | `obs-graphics::active_shader_queries` | Swapped in hybrid `libobs` (Refs #5248; prerequisite #5247). Safe Rust checks NULL context before any private field projection, preserves exact debug diagnostics, and forwards the borrowed shader through separate mandatory callbacks with the original device. The existing real device-query adapter archive supports every standalone Rust-static consumer. Tier 1 mirrors the frozen C characterization; Tier 3 compares original-C call/result/diagnostic traces. No intentional differences; Tier 4 is inapplicable. |
| `libobs/graphics/graphics-device-queries.c.h` | `obs-graphics::device_queries` | Swapped in hybrid `libobs` (Refs #5070; prerequisite #5068). Safe Rust checks context before private C field/dispatch adapters, distinguishes missing context from backend -1/zero, and preserves the optional adapter-count callback fallback and exact diagnostics. Original bodies remain reference-only and in a verbatim legacy oracle. Tier 1 mirrors unchanged `test_graphics_device_queries`; Tier 3 compares signed/unsigned boundaries and bounded random queries through the real adapters in the existing graphics binary. |
| `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. |
Expand Down
25 changes: 25 additions & 0 deletions rust/obs-c-oracle/legacy/graphics-active-shader-queries.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#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);

gs_shader_t *gs_get_vertex_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_vertex_shader"))
return NULL;

return graphics->exports.device_get_vertex_shader(graphics->device);
}

gs_shader_t *gs_get_pixel_shader(void)
{
graphics_t *graphics = thread_graphics;

if (!gs_valid("gs_get_pixel_shader"))
return NULL;

return graphics->exports.device_get_pixel_shader(graphics->device);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
graphics-active-shader-queries.inc|rust/obs-c-oracle/legacy/graphics-active-shader-queries.c|auto
84 changes: 84 additions & 0 deletions rust/obs-c-oracle/oracle/device_queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ bool oracle_gs_p010_available(void);
#undef gs_p010_available
#undef gs_nv12_available

#define gs_get_vertex_shader oracle_gs_get_vertex_shader
#define gs_get_pixel_shader oracle_gs_get_pixel_shader
gs_shader_t *oracle_gs_get_vertex_shader(void);
gs_shader_t *oracle_gs_get_pixel_shader(void);
#include "graphics-active-shader-queries.inc"
#undef gs_get_pixel_shader
#undef gs_get_vertex_shader

/* Original graphics.c.h context validator, verbatim. */
static inline bool gs_valid(const char *f)
{
Expand Down Expand Up @@ -173,3 +181,79 @@ unsigned oracle_planar_capability_last_device(void)
{
return dq_planar_last_device;
}

/* Active shader callbacks use the export table's const gs_device_t signature. */
static THREAD_LOCAL int dq_shader_devices[2], dq_shader_tokens[2];
static THREAD_LOCAL gs_shader_t *dq_shader_results[2];
static THREAD_LOCAL unsigned dq_shader_calls[2], dq_shader_last_device;

static gs_shader_t *dq_vertex_shader(const gs_device_t *device)
{
assert(thread_graphics == &dq_graphics);
assert(dq_graphics.ref == 1);
assert(device == dq_graphics.device);
assert(device == (gs_device_t *)&dq_shader_devices[0] || device == (gs_device_t *)&dq_shader_devices[1]);
dq_shader_last_device = device == (gs_device_t *)&dq_shader_devices[1] ? 1u : 0u;
dq_shader_calls[0]++;
return dq_shader_results[0];
}

static gs_shader_t *dq_pixel_shader(const gs_device_t *device)
{
assert(thread_graphics == &dq_graphics);
assert(dq_graphics.ref == 1);
assert(device == dq_graphics.device);
assert(device == (gs_device_t *)&dq_shader_devices[0] || device == (gs_device_t *)&dq_shader_devices[1]);
dq_shader_last_device = device == (gs_device_t *)&dq_shader_devices[1] ? 1u : 0u;
dq_shader_calls[1]++;
return dq_shader_results[1];
}

gs_shader_t *oracle_active_shader_token(unsigned token);
gs_shader_t *oracle_active_shader_token(unsigned token)
{
assert(token <= 2);
return token ? (gs_shader_t *)&dq_shader_tokens[token - 1] : NULL;
}

void oracle_active_shader_context(bool context);
void oracle_active_shader_context(bool context)
{
thread_graphics = context ? &dq_graphics : NULL;
dq_graphics.ref = context ? 1 : 0;
}

void oracle_active_shader_reset(bool vertex, bool pixel, unsigned vertex_result, unsigned pixel_result, bool device);
void oracle_active_shader_reset(bool vertex, bool pixel, unsigned vertex_result, unsigned pixel_result, bool device)
{
oracle_device_queries_reset(false, false, false, 0, 0);
dq_graphics.device = (gs_device_t *)&dq_shader_devices[device ? 1 : 0];
dq_graphics.exports.device_get_vertex_shader = vertex ? dq_vertex_shader : NULL;
dq_graphics.exports.device_get_pixel_shader = pixel ? dq_pixel_shader : NULL;
dq_shader_tokens[0] = 17;
dq_shader_tokens[1] = 29;
dq_shader_results[0] = oracle_active_shader_token(vertex_result);
dq_shader_results[1] = oracle_active_shader_token(pixel_result);
dq_shader_calls[0] = dq_shader_calls[1] = 0;
dq_shader_last_device = 2;
}

unsigned oracle_active_shader_calls(bool pixel);
unsigned oracle_active_shader_calls(bool pixel)
{
return dq_shader_calls[pixel ? 1 : 0];
}

unsigned oracle_active_shader_last_device(void);
unsigned oracle_active_shader_last_device(void)
{
return dq_shader_last_device;
}

int oracle_active_shader_ref(void);
int oracle_active_shader_ref(void)
{
assert(dq_shader_tokens[0] == 17 && dq_shader_tokens[1] == 29);
assert(dq_graphics.ref == 0 || dq_graphics.ref == 1);
return dq_graphics.ref == 1 ? 1 : 0;
}
33 changes: 33 additions & 0 deletions rust/obs-graphics/src/active_shader_queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Borrowed active shader queries over a caller-owned graphics context.

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Entry {
Vertex,
Pixel,
}

impl Entry {
pub const fn name(self) -> &'static str {
match self {
Self::Vertex => "gs_get_vertex_shader",
Self::Pixel => "gs_get_pixel_shader",
}
}
}

/// Dispatch projects the context only after the core has checked its presence.
/// Shader values are borrowed identities; their default represents NULL.
pub trait Host {
type Shader: Default;
fn has_context(&self) -> bool;
fn debug(&mut self, entry: Entry);
fn dispatch(&mut self, entry: Entry) -> Self::Shader;
}

pub fn shader<H: Host>(host: &mut H, entry: Entry) -> H::Shader {
if !host.has_context() {
host.debug(entry);
return H::Shader::default();
}
host.dispatch(entry)
}
58 changes: 58 additions & 0 deletions rust/obs-graphics/src/ffi/active_shader_queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Thin ABI shims over the real device-query TLS, logging and dispatch seam.
use crate::active_shader_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_vertex_shader(context: *mut c_void) -> *mut c_void;
fn obs_graphics_device_queries_pixel_shader(context: *mut c_void) -> *mut c_void;
}

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 {
type Shader = *mut c_void;
fn has_context(&self) -> bool {
!self.0.is_null()
}
fn debug(&mut self, entry: Entry) {
let name = match entry {
Entry::Vertex => c"gs_get_vertex_shader",
Entry::Pixel => c"gs_get_pixel_shader",
};
// SAFETY: the function name is static and NUL-terminated.
unsafe { obs_graphics_device_queries_debug(name.as_ptr()) }
}
fn dispatch(&mut self, entry: Entry) -> Self::Shader {
// SAFETY: the core checked NULL before any projection. A live graphics
// context supplies the mandatory callback with its real const-device ABI.
unsafe {
match entry {
Entry::Vertex => obs_graphics_device_queries_vertex_shader(self.0),
Entry::Pixel => obs_graphics_device_queries_pixel_shader(self.0),
}
}
}
}

/// Borrow the current vertex shader, or return NULL without a context.
/// # Safety
/// Any current context, device and mandatory backend callback must be live.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn gs_get_vertex_shader() -> *mut c_void {
core::shader(&mut current(), Entry::Vertex)
}

/// Borrow the current pixel shader, or return NULL without a context.
/// # Safety
/// Any current context, device and mandatory backend callback must be live.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn gs_get_pixel_shader() -> *mut c_void {
core::shader(&mut current(), Entry::Pixel)
}
1 change: 1 addition & 0 deletions rust/obs-graphics/src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! C ABI shims replacing the original libobs `EXPORT` symbols.

pub mod active_shader_queries;
pub mod axisang;
pub mod blend_entrypoints;
pub mod bounds;
Expand Down
1 change: 1 addition & 0 deletions rust/obs-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#[cfg(test)]
use obs_c_oracle as _; // links the test bmalloc/bfree (oracle/test_bmem.c)

pub mod active_shader_queries;
pub mod axisang;
pub mod blend_entrypoints;
pub mod bounds;
Expand Down
Loading
Loading