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 @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions libobs/build/libobs.ported-graphics-indexbuffer-queries+.h.c
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"LIBOBS_PORTED_SOURCES": [
"build/libobs.ported-graphics-indexbuffer-queries+.h.c"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gs_indexbuffer_get_data
gs_indexbuffer_get_type
12 changes: 12 additions & 0 deletions libobs/graphics/graphics-indexbuffer-maintenance-fields.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
32 changes: 28 additions & 4 deletions libobs/graphics/graphics-indexbuffer-queries.c.h
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
28 changes: 28 additions & 0 deletions rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
graphics-indexbuffer-queries.inc|rust/obs-c-oracle/legacy/graphics-indexbuffer-queries.c|auto
39 changes: 39 additions & 0 deletions rust/obs-c-oracle/oracle/indexbuffer_maintenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#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"

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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
74 changes: 74 additions & 0 deletions rust/obs-graphics/src/ffi/indexbuffer_queries.rs
Original file line number Diff line number Diff line change
@@ -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))
}
1 change: 1 addition & 0 deletions rust/obs-graphics/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions rust/obs-graphics/src/indexbuffer_queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! 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<Self::Data>;
fn index_type(&mut self, buffer: Self::Buffer) -> u32;
}

fn valid<H: Host>(host: &mut H, entry: Entry, buffer: Option<H::Buffer>) -> Option<H::Buffer> {
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<H: Host>(host: &mut H, buffer: Option<H::Buffer>) -> Option<H::Data> {
valid(host, Entry::Data, buffer).and_then(|buffer| host.data(buffer))
}

pub fn get_type<H: Host>(host: &mut H, buffer: Option<H::Buffer>) -> u32 {
valid(host, Entry::Type, buffer).map_or(0, |buffer| host.index_type(buffer))
}
1 change: 1 addition & 0 deletions rust/obs-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading