From 955a334f0764a7600cdafb5289eac83a2b1dfde2 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:13:25 -0400 Subject: [PATCH 1/6] libobs: Move the source and filter render routing into obs-source-render-route.c Behaviour-neutral extraction before the Rust port. The decisions of source_render_video(), obs_source_main_render(), can_bypass(), obs_source_process_filter_begin_with_color_space(), obs_source_process_filter_tech_end() and obs_source_skip_video_filter() in obs-source.c.h move to libobs/obs-source-render-route.c.h as 23 functions on plain values, declared in the new libobs/obs-source-render-route.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_render_route_filter(): the target and parent check of the three filter functions, no target first, then no parent (#2177's guard in obs_source_skip_video_filter()); - source_render_video(): _video_reads_flags() and _video_none() (neither video nor a filter), _async_reads_flags(), _async_reads_rendering_filter() and _async_update() (the async texture update), _reads_enabled() and _inactive() (no data or disabled); - the draw, as a route over enum obs_source_render_route_draw: _draw() starts source_render_video()'s, _skip() a skipped filter's and _filter_render() the draw into a filter's texrender; _draw_next() takes the value a READ_ step asks for and returns the next step or the draw (default render, filters, main render, filter target, deinterlace or async video); - obs_source_main_render(): _main_custom_draw(), _main_srgb_aware(), _main_reads_filters() and _main_default_effect(); - can_bypass(): _bypass_reads_space() and _bypass(); then _size_skip() (zero width or height) and _texrender_stale() (the format check); - obs_source_process_filter_tech_end(): _tech_end_linear_srgb(), _tech_name() ("Draw" for NULL, else the pointer given) and _tech_end() (bypass or texture). obs-source.c.h keeps every gs_* call and texrender, the refs, the render calls, get_base_width() and get_base_height(), the debug markers and every field read, in their order and under their conditions. That is why the decisions are many small calls and not one plan per function: each old short circuit read a field or called gs_get_color_space() or deinterlacing_enabled() only on one side, so a _reads_ function says whether to read the next value, and the draw chains read one value per READ_ step, in the old order (the plan's single route struct would have hoisted those reads). Two adjustments to the plan's sketch: the texrender reuse is one decision on the format gs_texrender_get_format() returns (C still checks the pointer before calling it, and again after the destroy); and the update_deinterlace flag stays the C call deinterlacing_enabled(), as there is nothing else to decide. Types, the direct render choice, colour spaces and formats cross as int; obs-source.c.h checks that the four enums are int-sized. A value outside an enum takes the path it took before. Checked against the unchanged functions of origin/master (outside the tree, not committed): both versions extracted from obs-source.c.h, every source->field, filter->field, target->field and parent->field access rewritten into a logged access, and compiled against stubs that log every call with its arguments (obs_filter_get_target/_parent, get_base_width/_height, obs_source_video_render and the other renders, the async and deinterlace updates, gs_get_linear_srgb/gs_set_linear_srgb, gs_get_effect, gs_get_color_space, every gs_texrender_* call, the blend, clear and ortho calls, render_filter_bypass/_tex with the technique name, blog, the debug markers). 6,496,768 cases: source_render_video() for every type (and -1, 4) x 32 flag patterns x every data, enabled, rendering_filter, video_render, filter target, filter parent (and which), deinterlace and linear sRGB combination x 0-2 filters (with the skip into obs_source_skip_video_filter() and obs_source_main_render()); obs_source_main_render() alone; the filter begin for 6 wirings (no filter, no target, no parent, neither, the target is the parent, another filter) x 4 filter flags x 14 parent flags x 4 direct render choices x 6 x 6 requested and current spaces x 4 x 4 sizes (0, 1, 1920, UINT32_MAX) x texrender present, matching and begin result; the tech end for every wiring, 32 flags, bypass, texture, linear sRGB and a NULL, "Draw" or custom technique; the skip for every wiring, 32 parent flags, video_render and deinterlace of each target. The traces are identical byte for byte under GCC -O2 and clang -O0. Planted changes to the SRGB match of the bypass, the deinterlace step, the inactive rule and the enabled read are each caught. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs #2189 --- libobs/build/libobs.core-source+.h.c | 1 + libobs/build/libobs.pch.h | 1 + libobs/build/source-sets.json | 1 + libobs/obs-source-render-route.c.h | 195 +++++++++++++++++++++++ libobs/obs-source-render-route.h | 228 +++++++++++++++++++++++++++ libobs/obs-source.c.h | 164 +++++++++++++------ 6 files changed, 544 insertions(+), 46 deletions(-) create mode 100644 libobs/obs-source-render-route.c.h create mode 100644 libobs/obs-source-render-route.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index c4cf0c47b3..a7eadb07af 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -7,6 +7,7 @@ #include "libobs/obs-scene-item-audio-actions.c.h" #include "libobs/obs-scene.c.h" #include "libobs/obs-source-deinterlace.c.h" +#include "libobs/obs-source-render-route.c.h" #include "libobs/obs-source-transition.c.h" #include "libobs/obs-source.c.h" #include "libobs/obs-view.c.h" diff --git a/libobs/build/libobs.pch.h b/libobs/build/libobs.pch.h index bf83582380..be39ff0bcb 100644 --- a/libobs/build/libobs.pch.h +++ b/libobs/build/libobs.pch.h @@ -149,6 +149,7 @@ #include "obs-source-conversion-plan.h" #include "obs-source-draw-plan.h" #include "obs-source-ptm-gate.h" +#include "obs-source-render-route.h" #include "obs-source-size.h" #include "obs-source-volume.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index fd804ae2e8..ddd054fc33 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -56,6 +56,7 @@ "obs-source-draw-technique.h", "obs-source-load.h", "obs-source-ptm-gate.h", + "obs-source-render-route.h", "obs-source-size.h", "obs-source-transition-clock.h", "obs-source-transition-render.h", diff --git a/libobs/obs-source-render-route.c.h b/libobs/obs-source-render-route.c.h new file mode 100644 index 0000000000..fabf26f811 --- /dev/null +++ b/libobs/obs-source-render-route.c.h @@ -0,0 +1,195 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "libobs/build/libobs.pch.h" + +/* The source and filter render routing of obs-source.c, moved from + * source_render_video(), obs_source_main_render(), can_bypass(), + * obs_source_process_filter_begin_with_color_space(), + * obs_source_process_filter_tech_end() and obs_source_skip_video_filter(): + * the decisions, on plain values. obs-source.c keeps every gs_* call and + * texrender, the refs, the render calls, the sizes, the debug markers and + * every field read in its order, passes the values in and acts on what comes + * back. Declared in obs-source-render-route.h without EXPORT. In hybrid + * libobs the Rust port (rust/obs-source) replaces this file. */ + +enum obs_source_render_route_filter obs_source_render_route_filter(bool has_target, bool has_parent) +{ + if (!has_target) + return OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET; + if (!has_parent) + return OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT; + return OBS_SOURCE_RENDER_ROUTE_FILTER_READY; +} + +bool obs_source_render_route_video_reads_flags(int type) +{ + return (enum obs_source_type)type != OBS_SOURCE_TYPE_FILTER; +} + +bool obs_source_render_route_video_none(int type, uint32_t flags) +{ + return (enum obs_source_type)type != OBS_SOURCE_TYPE_FILTER && (flags & OBS_SOURCE_VIDEO) == 0; +} + +bool obs_source_render_route_async_reads_flags(int type) +{ + return (enum obs_source_type)type == OBS_SOURCE_TYPE_INPUT; +} + +bool obs_source_render_route_async_reads_rendering_filter(int type, uint32_t flags) +{ + return (enum obs_source_type)type == OBS_SOURCE_TYPE_INPUT && (flags & OBS_SOURCE_ASYNC) != 0; +} + +bool obs_source_render_route_async_update(int type, uint32_t flags, bool rendering_filter) +{ + return (enum obs_source_type)type == OBS_SOURCE_TYPE_INPUT && (flags & OBS_SOURCE_ASYNC) != 0 && + !rendering_filter; +} + +bool obs_source_render_route_reads_enabled(bool has_data) +{ + return has_data; +} + +bool obs_source_render_route_inactive(bool has_data, bool enabled) +{ + return !has_data || !enabled; +} + +enum obs_source_render_route_draw obs_source_render_route_draw(bool has_filters) +{ + return has_filters ? OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER + : OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER; +} + +enum obs_source_render_route_draw obs_source_render_route_draw_next(enum obs_source_render_route_draw step, bool value) +{ + switch (step) { + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER: + return !value ? OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS : OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER: + return value ? OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN : OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET: + return value ? OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET : OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING: + return value ? OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE : OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER: + return value ? OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN + : OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING: + return value ? OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE : OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC; + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT: + case OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS: + case OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN: + case OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET: + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE: + case OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC: + break; + } + + return step; +} + +bool obs_source_render_route_main_custom_draw(uint32_t flags) +{ + return (flags & OBS_SOURCE_CUSTOM_DRAW) != 0; +} + +bool obs_source_render_route_main_srgb_aware(uint32_t flags) +{ + return (flags & OBS_SOURCE_SRGB) != 0; +} + +bool obs_source_render_route_main_reads_filters(bool has_filter_parent) +{ + return !has_filter_parent; +} + +bool obs_source_render_route_main_default_effect(uint32_t flags, bool has_filter_parent, bool has_filters) +{ + bool custom_draw = (flags & OBS_SOURCE_CUSTOM_DRAW) != 0; + return !has_filter_parent && !has_filters && !custom_draw; +} + +bool obs_source_render_route_bypass_reads_space(bool target_is_parent, int allow_direct, uint32_t filter_flags, + uint32_t parent_flags) +{ + return target_is_parent && ((enum obs_allow_direct_render)allow_direct == OBS_ALLOW_DIRECT_RENDERING) && + ((parent_flags & OBS_SOURCE_CUSTOM_DRAW) == 0) && ((parent_flags & OBS_SOURCE_ASYNC) == 0) && + ((filter_flags & OBS_SOURCE_SRGB) == (parent_flags & OBS_SOURCE_SRGB)); +} + +bool obs_source_render_route_bypass(bool target_is_parent, int allow_direct, uint32_t filter_flags, + uint32_t parent_flags, int space, int current_space) +{ + return obs_source_render_route_bypass_reads_space(target_is_parent, allow_direct, filter_flags, parent_flags) && + (enum gs_color_space)space == (enum gs_color_space)current_space; +} + +bool obs_source_render_route_size_skip(int cx, int cy) +{ + return !cx || !cy; +} + +bool obs_source_render_route_texrender_stale(int texrender_format, int format) +{ + return (enum gs_color_format)texrender_format != (enum gs_color_format)format; +} + +enum obs_source_render_route_draw obs_source_render_route_filter_render(bool target_is_parent, uint32_t parent_flags) +{ + bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0; + bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0; + + if (target_is_parent && !custom_draw && !async) + return OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT; + else + return OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET; +} + +bool obs_source_render_route_tech_end_linear_srgb(uint32_t filter_flags) +{ + return (filter_flags & OBS_SOURCE_SRGB) != 0; +} + +const char *obs_source_render_route_tech_name(const char *tech_name) +{ + return tech_name ? tech_name : "Draw"; +} + +enum obs_source_render_route_tech_end obs_source_render_route_tech_end(bool bypass_active) +{ + return bypass_active ? OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS : OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE; +} + +enum obs_source_render_route_draw obs_source_render_route_skip(bool target_is_parent, uint32_t parent_flags) +{ + bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0; + bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0; + + if (target_is_parent) { + if (!custom_draw && !async) + return OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT; + else + return OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER; + + } else { + return OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET; + } +} diff --git a/libobs/obs-source-render-route.h b/libobs/obs-source-render-route.h new file mode 100644 index 0000000000..a99147baef --- /dev/null +++ b/libobs/obs-source-render-route.h @@ -0,0 +1,228 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include +#include + +/* The source and filter render routing of obs-source.c, on plain values + * (obs-source-render-route.c): + * - source_render_video(): skip a source that is neither video nor a filter, + * update the async textures of an async input not drawn as a filter, skip + * a source with no data or one that is disabled, then pick the draw: + * filters, main render, filter target, deinterlace or async video; + * - obs_source_main_render(): the default-effect rule, the sRGB-aware rule + * and the effect passed to the source (none for custom draw); + * - obs_source_process_filter_begin_with_color_space(): no target or no + * parent, the direct-render bypass (can_bypass()), the skip at zero size, + * texrender reuse when its format matches, and default render versus + * video render of the target; + * - obs_source_process_filter_tech_end(): bypass or texture, the technique + * name ("Draw" when none is given) and the linear sRGB flag; + * - obs_source_skip_video_filter(): the NULL parent and target guard (#2177) + * and the draw of the target. + * + * obs-source.c keeps every gs_* call and texrender, the source refs, the + * render functions themselves, get_base_width() and get_base_height() (it + * passes the sizes), the debug markers and every field read, in their order + * and under their conditions. Where the old code read a field or called a + * function only on one side of a short circuit, a _reads_ function says + * whether to read it, and a draw route that needs a field it has not read + * yet asks for it with a READ_ step: obs-source.c reads that one value, + * passes it to obs_source_render_route_draw_next() and goes on. None of + * these functions takes a lock, calls into libobs or keeps state. + * + * Source types (enum obs_source_type), the direct render choice (enum + * obs_allow_direct_render), colour spaces (enum gs_color_space) and colour + * formats (enum gs_color_format) are passed as int; obs-source.c checks the + * sizes. A value outside an enum takes the path it took before. Output flags + * are the uint32_t OBS_SOURCE_* bits. + * + * Hidden in libobs: not EXPORT. */ + +/* What a filter render step finds, in the order the old code checked. */ +enum obs_source_render_route_filter { + /* a target and a parent: go on */ + OBS_SOURCE_RENDER_ROUTE_FILTER_READY, + /* no target (checked first) */ + OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET, + /* a target but no parent */ + OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT, +}; + +/* A draw, or the value to read before the draw is known. In + * source_render_video() the source draws itself; in + * obs_source_skip_video_filter() and + * obs_source_process_filter_begin_with_color_space() the filter draws its + * target. */ +enum obs_source_render_route_draw { + /* obs_source_default_render(target) */ + OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT, + /* obs_source_render_filters(source) */ + OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS, + /* obs_source_main_render() of the source or the target */ + OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN, + /* obs_source_video_render() of the filter target */ + OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET, + /* deinterlace_render() of the source or the target */ + OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE, + /* obs_source_render_async_video() of the source or the target */ + OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, + /* read source->rendering_filter */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER, + /* read source->info.video_render != NULL */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER, + /* read source->filter_target != NULL */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET, + /* call deinterlacing_enabled(source) */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING, + /* read target->info.video_render != NULL */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER, + /* call deinterlacing_enabled(target) */ + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING, +}; + +/* What obs_source_process_filter_tech_end() draws. */ +enum obs_source_render_route_tech_end { + /* render_filter_bypass(): the target, straight through the effect */ + OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS, + /* render_filter_tex(): the filter's texrender, if it has a texture */ + OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE, +}; + +/* --- filters ----------------------------------------------------------- */ + +/* The target and parent check of obs_source_process_filter_begin*(), + * obs_source_process_filter_tech_end() and obs_source_skip_video_filter(): + * no target first, then no parent. A filter removed while it renders has + * neither (#2177). */ +enum obs_source_render_route_filter obs_source_render_route_filter(bool has_target, bool has_parent); + +/* --- source_render_video() ------------------------------------------------ */ + +/* Whether the "no video" check reads the source's output flags: for any + * type but OBS_SOURCE_TYPE_FILTER. */ +bool obs_source_render_route_video_reads_flags(int type); + +/* A source that is neither a filter nor OBS_SOURCE_VIDEO draws nothing (a + * filter in a chain skips itself). flags is 0 when not read. */ +bool obs_source_render_route_video_none(int type, uint32_t flags); + +/* Whether the async check reads the output flags (again): for an + * OBS_SOURCE_TYPE_INPUT. */ +bool obs_source_render_route_async_reads_flags(int type); + +/* Whether the async check reads source->rendering_filter: an + * OBS_SOURCE_TYPE_INPUT with OBS_SOURCE_ASYNC. */ +bool obs_source_render_route_async_reads_rendering_filter(int type, uint32_t flags); + +/* Whether to update the async video textures (and first the deinterlace + * ones, when deinterlacing is on): an async input not being drawn by its own + * filter chain. flags is 0 and rendering_filter false when not read. */ +bool obs_source_render_route_async_update(int type, uint32_t flags, bool rendering_filter); + +/* Whether the active check reads source->enabled: only with data. */ +bool obs_source_render_route_reads_enabled(bool has_data); + +/* A source with no data, or a disabled one, draws nothing (a filter in a + * chain skips itself). enabled is false when not read. */ +bool obs_source_render_route_inactive(bool has_data, bool enabled); + +/* The first step of the draw: READ_RENDERING_FILTER with filters, else + * READ_VIDEO_RENDER. */ +enum obs_source_render_route_draw obs_source_render_route_draw(bool has_filters); + +/* The next step after reading the value a READ_ step asks for: + * - READ_RENDERING_FILTER: FILTERS unless already rendering the chain, else + * READ_VIDEO_RENDER; + * - READ_VIDEO_RENDER: MAIN with a video_render callback, else + * READ_FILTER_TARGET; + * - READ_FILTER_TARGET: TARGET with a filter target, else READ_DEINTERLACING; + * - READ_DEINTERLACING: DEINTERLACE when on, else ASYNC; + * - READ_TARGET_VIDEO_RENDER: MAIN with a video_render callback, else + * READ_TARGET_DEINTERLACING; + * - READ_TARGET_DEINTERLACING: DEINTERLACE when on, else ASYNC. + * A draw, or a value outside the enum, comes back unchanged. */ +enum obs_source_render_route_draw obs_source_render_route_draw_next(enum obs_source_render_route_draw step, bool value); + +/* --- obs_source_main_render() --------------------------------------------- */ + +/* OBS_SOURCE_CUSTOM_DRAW: the source gets no effect (NULL instead of + * gs_get_effect(), which is not called). */ +bool obs_source_render_route_main_custom_draw(uint32_t flags); + +/* OBS_SOURCE_SRGB: the source keeps the linear sRGB flag; otherwise + * obs_source_main_render() saves it, clears it and restores it. */ +bool obs_source_render_route_main_srgb_aware(uint32_t flags); + +/* Whether the default-effect rule reads source->filters.num: only for a + * source that is not a filter. */ +bool obs_source_render_route_main_reads_filters(bool has_filter_parent); + +/* The default-effect rule: not a filter, no filters and not custom draw, + * so obs_source_default_render() draws it with the "Draw" technique. + * has_filters is false when not read. */ +bool obs_source_render_route_main_default_effect(uint32_t flags, bool has_filter_parent, bool has_filters); + +/* --- obs_source_process_filter_begin_with_color_space() --------------------- */ + +/* Whether can_bypass() reads gs_get_color_space(): the target is the parent, + * direct rendering is allowed, the parent is neither custom draw nor async + * and the filter and the parent agree on OBS_SOURCE_SRGB. */ +bool obs_source_render_route_bypass_reads_space(bool target_is_parent, int allow_direct, uint32_t filter_flags, + uint32_t parent_flags); + +/* can_bypass(): the filter renders the parent directly through its effect, + * with no texrender, when obs_source_render_route_bypass_reads_space() holds + * and the requested space is the current one. current_space is + * gs_get_color_space(), any value when not read. */ +bool obs_source_render_route_bypass(bool target_is_parent, int allow_direct, uint32_t filter_flags, + uint32_t parent_flags, int space, int current_space); + +/* A target of zero width or height: the filter skips itself and draws + * nothing more. */ +bool obs_source_render_route_size_skip(int cx, int cy); + +/* The filter's texrender has another format than the one asked for: destroy + * it (a new one is created). */ +bool obs_source_render_route_texrender_stale(int texrender_format, int format); + +/* How the filter draws its target into the texrender: DEFAULT when the + * target is the parent and the parent is neither custom draw nor async, + * TARGET otherwise. */ +enum obs_source_render_route_draw obs_source_render_route_filter_render(bool target_is_parent, uint32_t parent_flags); + +/* --- obs_source_process_filter_tech_end() --------------------------------- */ + +/* The linear sRGB flag the filter's draw runs with: its OBS_SOURCE_SRGB. */ +bool obs_source_render_route_tech_end_linear_srgb(uint32_t filter_flags); + +/* The technique to draw with: tech_name, or "Draw" when it is NULL. The + * pointer is passed through and never read. */ +const char *obs_source_render_route_tech_name(const char *tech_name); + +/* Bypass, as obs_source_process_filter_begin*() decided, or the texture. */ +enum obs_source_render_route_tech_end obs_source_render_route_tech_end(bool bypass_active); + +/* --- obs_source_skip_video_filter() --------------------------------------- */ + +/* The first step of a skipped filter's draw: TARGET when the target is + * another filter, DEFAULT for a parent that is neither custom draw nor + * async, else READ_TARGET_VIDEO_RENDER (then main render, deinterlace or + * async video of the parent). */ +enum obs_source_render_route_draw obs_source_render_route_skip(bool target_is_parent, uint32_t parent_flags); diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index cd28607019..af946ce250 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -2318,12 +2318,20 @@ void obs_source_default_render(obs_source_t *source) } } +_Static_assert(sizeof(enum obs_source_type) == sizeof(int), "enum obs_source_type is passed as int"); +_Static_assert(sizeof(enum obs_allow_direct_render) == sizeof(int), "enum obs_allow_direct_render is passed as int"); +_Static_assert(sizeof(enum gs_color_space) == sizeof(int), "enum gs_color_space is passed as int"); +_Static_assert(sizeof(enum gs_color_format) == sizeof(int), "enum gs_color_format is passed as int"); + static inline void obs_source_main_render(obs_source_t *source) { uint32_t flags = source->info.output_flags; - bool custom_draw = (flags & OBS_SOURCE_CUSTOM_DRAW) != 0; - bool srgb_aware = (flags & OBS_SOURCE_SRGB) != 0; - bool default_effect = !source->filter_parent && source->filters.num == 0 && !custom_draw; + bool custom_draw = obs_source_render_route_main_custom_draw(flags); + bool srgb_aware = obs_source_render_route_main_srgb_aware(flags); + const bool has_filter_parent = source->filter_parent != NULL; + bool default_effect = obs_source_render_route_main_default_effect( + flags, has_filter_parent, + obs_source_render_route_main_reads_filters(has_filter_parent) && source->filters.num != 0); bool previous_srgb = false; if (!srgb_aware) { @@ -2359,22 +2367,49 @@ static const char *get_type_format(enum obs_source_type type) } #endif +/* The draw of source_render_video(): each READ_ step reads one value, in the + * old order, and only when the route asks for it. */ +static inline enum obs_source_render_route_draw source_draw_route(obs_source_t *source) +{ + enum obs_source_render_route_draw route = obs_source_render_route_draw(source->filters.num != 0); + + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER) + route = obs_source_render_route_draw_next(route, source->rendering_filter); + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER) + route = obs_source_render_route_draw_next(route, source->info.video_render != NULL); + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET) + route = obs_source_render_route_draw_next(route, source->filter_target != NULL); + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING) + route = obs_source_render_route_draw_next(route, deinterlacing_enabled(source)); + + return route; +} + static inline void source_render_video(obs_source_t *source) { - if (source->info.type != OBS_SOURCE_TYPE_FILTER && (source->info.output_flags & OBS_SOURCE_VIDEO) == 0) { + int type = (int)source->info.type; + uint32_t flags = obs_source_render_route_video_reads_flags(type) ? source->info.output_flags : 0; + + if (obs_source_render_route_video_none(type, flags)) { if (source->filter_parent) obs_source_skip_video_filter(source); return; } - if (source->info.type == OBS_SOURCE_TYPE_INPUT && (source->info.output_flags & OBS_SOURCE_ASYNC) != 0 && - !source->rendering_filter) { + /* the async check reads the type and the flags again, as before */ + type = (int)source->info.type; + flags = obs_source_render_route_async_reads_flags(type) ? source->info.output_flags : 0; + if (obs_source_render_route_async_update(type, flags, + obs_source_render_route_async_reads_rendering_filter(type, flags) && + source->rendering_filter)) { if (deinterlacing_enabled(source)) deinterlace_update_async_video(source); obs_source_update_async_video(source); } - if (!source->context.data || !source->enabled) { + const bool has_data = source->context.data != NULL; + if (obs_source_render_route_inactive(has_data, + obs_source_render_route_reads_enabled(has_data) && source->enabled)) { if (source->filter_parent) obs_source_skip_video_filter(source); return; @@ -2383,20 +2418,32 @@ static inline void source_render_video(obs_source_t *source) GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_SOURCE, get_type_format(source->info.type), obs_source_get_name(source)); - if (source->filters.num && !source->rendering_filter) + switch (source_draw_route(source)) { + case OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS: obs_source_render_filters(source); - - else if (source->info.video_render) + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN: obs_source_main_render(source); - - else if (source->filter_target) + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET: obs_source_video_render(source->filter_target); - - else if (deinterlacing_enabled(source)) + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE: deinterlace_render(source); - - else + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC: obs_source_render_async_video(source); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING: + /* not a draw of source_render_video() */ + break; + } GS_DEBUG_MARKER_END(); } @@ -3760,12 +3807,15 @@ static inline void render_filter_tex(gs_texture_t *tex, gs_effect_t *effect, uin gs_enable_framebuffer_srgb(previous); } +/* can_bypass(): gs_get_color_space() is called only when the other + * conditions hold, as before. */ static inline bool can_bypass(obs_source_t *target, obs_source_t *parent, uint32_t filter_flags, uint32_t parent_flags, enum obs_allow_direct_render allow_direct, enum gs_color_space space) { - return (target == parent) && (allow_direct == OBS_ALLOW_DIRECT_RENDERING) && - ((parent_flags & OBS_SOURCE_CUSTOM_DRAW) == 0) && ((parent_flags & OBS_SOURCE_ASYNC) == 0) && - ((filter_flags & OBS_SOURCE_SRGB) == (parent_flags & OBS_SOURCE_SRGB) && space == gs_get_color_space()); + const bool reads_space = obs_source_render_route_bypass_reads_space(target == parent, (int)allow_direct, + filter_flags, parent_flags); + return obs_source_render_route_bypass(target == parent, (int)allow_direct, filter_flags, parent_flags, + (int)space, reads_space ? (int)gs_get_color_space() : (int)GS_CS_SRGB); } bool obs_source_process_filter_begin(obs_source_t *filter, enum gs_color_format format, @@ -3790,13 +3840,15 @@ bool obs_source_process_filter_begin_with_color_space(obs_source_t *filter, enum target = obs_filter_get_target(filter); parent = obs_filter_get_parent(filter); - if (!target) { + switch (obs_source_render_route_filter(target != NULL, parent != NULL)) { + case OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET: blog(LOG_INFO, "filter '%s' being processed with no target!", filter->context.name); return false; - } - if (!parent) { + case OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT: blog(LOG_INFO, "filter '%s' being processed with no parent!", filter->context.name); return false; + case OBS_SOURCE_RENDER_ROUTE_FILTER_READY: + break; } filter_flags = filter->info.output_flags; @@ -3815,12 +3867,13 @@ bool obs_source_process_filter_begin_with_color_space(obs_source_t *filter, enum return true; } - if (!cx || !cy) { + if (obs_source_render_route_size_skip(cx, cy)) { obs_source_skip_video_filter(filter); return false; } - if (filter->filter_texrender && (gs_texrender_get_format(filter->filter_texrender) != format)) { + if (filter->filter_texrender && obs_source_render_route_texrender_stale( + (int)gs_texrender_get_format(filter->filter_texrender), (int)format)) { gs_texrender_destroy(filter->filter_texrender); filter->filter_texrender = NULL; } @@ -3833,15 +3886,14 @@ bool obs_source_process_filter_begin_with_color_space(obs_source_t *filter, enum gs_blend_state_push(); gs_blend_function_separate(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA, GS_BLEND_ONE, GS_BLEND_INVSRCALPHA); - bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0; - bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0; struct vec4 clear_color; vec4_zero(&clear_color); gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0); gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f); - if (target == parent && !custom_draw && !async) + if (obs_source_render_route_filter_render(target == parent, parent_flags) == + OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT) obs_source_default_render(target); else obs_source_video_render(target); @@ -3869,22 +3921,25 @@ void obs_source_process_filter_tech_end(obs_source_t *filter, gs_effect_t *effec target = obs_filter_get_target(filter); parent = obs_filter_get_parent(filter); - if (!target || !parent) + if (obs_source_render_route_filter(target != NULL, parent != NULL) != OBS_SOURCE_RENDER_ROUTE_FILTER_READY) return; filter_flags = filter->info.output_flags; - const bool previous = gs_set_linear_srgb((filter_flags & OBS_SOURCE_SRGB) != 0); + const bool previous = gs_set_linear_srgb(obs_source_render_route_tech_end_linear_srgb(filter_flags)); - const char *tech = tech_name ? tech_name : "Draw"; + const char *tech = obs_source_render_route_tech_name(tech_name); - if (filter_bypass_active) { + switch (obs_source_render_route_tech_end(filter_bypass_active)) { + case OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS: render_filter_bypass(target, effect, tech); - } else { + break; + case OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE: texture = gs_texrender_get_texture(filter->filter_texrender); if (texture) { render_filter_tex(texture, effect, width, height, tech); } + break; } gs_set_linear_srgb(previous); @@ -3901,7 +3956,6 @@ void obs_source_process_filter_end(obs_source_t *filter, gs_effect_t *effect, ui void obs_source_skip_video_filter(obs_source_t *filter) { obs_source_t *target, *parent; - bool custom_draw, async; uint32_t parent_flags; if (!obs_ptr_valid(filter, "obs_source_skip_video_filter")) @@ -3913,25 +3967,43 @@ void obs_source_skip_video_filter(obs_source_t *filter) /* A filter removed while it renders has no parent or target any * more: draw nothing this frame, as obs_source_process_filter_begin * does. */ - if (!target || !parent) + if (obs_source_render_route_filter(target != NULL, parent != NULL) != OBS_SOURCE_RENDER_ROUTE_FILTER_READY) return; parent_flags = parent->info.output_flags; - custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0; - async = (parent_flags & OBS_SOURCE_ASYNC) != 0; - if (target == parent) { - if (!custom_draw && !async) - obs_source_default_render(target); - else if (target->info.video_render) - obs_source_main_render(target); - else if (deinterlacing_enabled(target)) - deinterlace_render(target); - else - obs_source_render_async_video(target); + /* each READ_ step reads one value of the target, in the old order */ + enum obs_source_render_route_draw route = obs_source_render_route_skip(target == parent, parent_flags); + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER) + route = obs_source_render_route_draw_next(route, target->info.video_render != NULL); + if (route == OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING) + route = obs_source_render_route_draw_next(route, deinterlacing_enabled(target)); - } else { + switch (route) { + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT: + obs_source_default_render(target); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN: + obs_source_main_render(target); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE: + deinterlace_render(target); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC: + obs_source_render_async_video(target); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET: obs_source_video_render(target); + break; + case OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING: + /* not a draw of a skipped filter */ + break; } } From 1409a938eb368b63cf98b192cae9acdfce348ed7 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:25:28 -0400 Subject: [PATCH 2/6] test: Characterize the source and filter render routing test_source_render_route_steps compiles libobs/obs-source-render-route.c.h (ASan and UBSan) and calls only its 23 hidden functions. 18 cases: - enum_values: the filter check, draw and tech end numbers the C acts on (and the port will return), that each enum is int-sized, and that the four libobs enums obs-source.c passes as int are int-sized; - filter_target_and_parent: ready, no target, no parent, and no target first when both are missing (#2177's guard); - video_none_every_type_and_flags, video_none_filter_and_audio: for each of nine types (the enum, -1, 4, 99 and the ends of int) and every combination of VIDEO, ASYNC, CUSTOM_DRAW, SRGB, AUDIO and CAP_DISABLED (and UINT32_MAX), whether the flags are read and whether the source is skipped; a filter draws whatever its flags; - async_update_every_type_flags_and_filter, async_update_cases: the reads and the update for every type, flag pattern and rendering_filter; - inactive_every_input: no data or disabled, enabled read only with data; - draw_every_source: source_render_video()'s draw for all 32 combinations of filters, rendering_filter, video_render, filter target and deinterlacing, against the old if/else chain, with the READ_ steps in order (so a value the old chain did not read is never asked for); - draw_next_every_step: both values of each READ_ step; draws and values outside the enum (-1, 12, 99, the ends of int) come back unchanged; - skip_every_target_and_flags, skip_cases: obs_source_skip_video_filter()'s draw for the target being the parent or not, every parent flag pattern, video_render and deinterlacing, against the old chain; - main_render_every_input: the custom draw, sRGB-aware and default-effect rules, filters.num read only for a source that is not a filter; - bypass_every_input, bypass_cases: can_bypass() for both wirings, six direct render choices, every pair of filter and parent flag patterns and every pair of eight spaces (the enum, -1, 4, the ends of int): when gs_get_color_space() is read and when the filter bypasses; the SRGB and space rules of #1493's filter colour space class; - size_skip_every_size, texrender_stale_every_format, filter_render_every_input: the zero size skip, the format check for every pair of formats around the enum, and the default or target render into the texrender; - tech_end_every_input: the linear sRGB flag, bypass or texture, "Draw" for a NULL name and the given pointer otherwise. Characterized, not endorsed: - ASYNC without VIDEO is no video, yet an input with it would update its async textures if it got that far (it does not: the "no video" check returns first); - a direct render choice outside the enum is not direct; spaces outside the enum bypass when they are equal; - cx and cy are get_base_width()'s and get_base_height()'s uint32_t in an int, so a size of 2^31 or more is negative there and does not skip; - an empty technique name is passed on as it is. The test is in the native test graph in name order, with libm except on MSVC, in the core suite. On the C backend, the hybrid and libobs-c-reference it passes 18 of 18 (it compiles the C file into itself on each). test_filter_color_space, test_filter_skip_removed, test_scroll_filter_wide_source, test_color_correction_16f, test_darken_transparent, test_scene_rotated_scale_filter, test_slideshow_size, test_texture_render and the other 28 tests that touch filters pass on all three, unchanged. Refs #2189 --- test/cmocka/build/test-graph.json | 29 + test/cmocka/test_source_render_route_steps.c | 625 ++++++++++++++++++ .../test_source_render_route_steps.cmake | 14 + 3 files changed, 668 insertions(+) create mode 100644 test/cmocka/test_source_render_route_steps.c create mode 100644 test/cmocka/tests.d/test_source_render_route_steps.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index bf18d2614a..ebfe4bce5a 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2858,6 +2858,29 @@ "OBS::native-test-cmocka" ] }, + "test_source_render_route_steps": { + "sources": [ + "test/cmocka/test_source_render_route_steps.c", + "libobs/obs-source-render-route.c.h" + ], + "includes": [ + "libobs", + "deps/libcaption" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + { + "name": "m", + "requires": { + "not": "MSVC" + } + }, + "OBS::native-test-cmocka" + ], + "sanitizers": "address,undefined" + }, "test_source_size_route_steps": { "sources": [ "test/cmocka/test_source_size_route_steps.c", @@ -4697,6 +4720,12 @@ ] ] }, + "test_source_render_route_steps": { + "target": "test_source_render_route_steps", + "command": [ + "${target}" + ] + }, "test_source_size_route_steps": { "target": "test_source_size_route_steps", "command": [ diff --git a/test/cmocka/test_source_render_route_steps.c b/test/cmocka/test_source_render_route_steps.c new file mode 100644 index 0000000000..aef464c9f1 --- /dev/null +++ b/test/cmocka/test_source_render_route_steps.c @@ -0,0 +1,625 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "obs.h" +#include "obs-source-render-route.h" + +/* + * Characterization of the source and filter render routing of + * libobs/obs-source.c (source_render_video(), obs_source_main_render(), + * can_bypass(), obs_source_process_filter_begin_with_color_space(), + * obs_source_process_filter_tech_end() and obs_source_skip_video_filter()), + * moved to libobs/obs-source-render-route.c before its Rust port: + * - obs_source_render_route_filter(): the target and parent check, no target + * first (#2177's guard in obs_source_skip_video_filter()); + * - the "no video", async update and inactive checks of + * source_render_video(), and which fields each one reads; + * - obs_source_render_route_draw(), _draw_next() and _skip(): the draw of a + * source, of a skipped filter's target and of a filter's target into its + * texrender, one value read at a time in the old order; + * - the default-effect, sRGB-aware and custom draw rules of + * obs_source_main_render(); + * - can_bypass() (when gs_get_color_space() is read, and the bypass), the + * skip at zero size and the texrender format check; + * - the technique name, the linear sRGB flag and the bypass or texture draw + * of obs_source_process_filter_tech_end(). + * Every boolean input is taken both ways, every source type and colour space + * (and values outside the enums), every combination of the output flags the + * rules read, and the NULL parent and target cases. Cases marked + * "characterized, not endorsed" are odd but real. + * + * The functions are hidden in libobs, so this test compiles the C source into + * itself and calls only them. test_filter_color_space, test_filter_skip_removed, + * test_scroll_filter_wide_source, test_color_correction_16f, + * test_darken_transparent, test_scene_rotated_scale_filter, + * test_slideshow_size and test_texture_render cover the callers through the + * real libobs. + */ + +/* source types: the enum and values outside it */ +static const int TYPES[] = {OBS_SOURCE_TYPE_INPUT, + OBS_SOURCE_TYPE_FILTER, + OBS_SOURCE_TYPE_TRANSITION, + OBS_SOURCE_TYPE_SCENE, + -1, + 4, + 99, + INT_MIN, + INT_MAX}; +#define TYPE_COUNT (sizeof(TYPES) / sizeof(TYPES[0])) + +/* colour spaces: the enum and values outside it */ +static const int SPACES[] = {GS_CS_SRGB, GS_CS_SRGB_16F, GS_CS_709_EXTENDED, GS_CS_709_SCRGB, -1, 4, INT_MIN, INT_MAX}; +#define SPACE_COUNT (sizeof(SPACES) / sizeof(SPACES[0])) + +/* the direct render choice: the enum and values outside it */ +static const int ALLOW[] = {OBS_NO_DIRECT_RENDERING, OBS_ALLOW_DIRECT_RENDERING, 2, -1, INT_MIN, INT_MAX}; +#define ALLOW_COUNT (sizeof(ALLOW) / sizeof(ALLOW[0])) + +/* The output flags the rules read, and two they do not: every combination of + * these 6 bits is taken, then UINT32_MAX. */ +static const uint32_t FLAG_BITS[] = {OBS_SOURCE_VIDEO, OBS_SOURCE_ASYNC, OBS_SOURCE_CUSTOM_DRAW, + OBS_SOURCE_SRGB, OBS_SOURCE_AUDIO, OBS_SOURCE_CAP_DISABLED}; +#define FLAG_BIT_COUNT (sizeof(FLAG_BITS) / sizeof(FLAG_BITS[0])) +#define FLAG_COMBOS ((1u << FLAG_BIT_COUNT) + 1) + +static uint32_t flags_of(unsigned combo) +{ + if (combo == (1u << FLAG_BIT_COUNT)) + return UINT32_MAX; + uint32_t flags = 0; + for (size_t b = 0; b < FLAG_BIT_COUNT; b++) + if (combo & (1u << b)) + flags |= FLAG_BITS[b]; + return flags; +} + +static void check_int(long long actual, long long expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %lld, expected %lld\n", line, what, actual, expected); + fail(); + } +} + +static void check_bool(bool actual, bool expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %s, expected %s\n", line, what, actual ? "true" : "false", + expected ? "true" : "false"); + fail(); + } +} + +#define CHECK_INT(actual, expected) check_int((long long)(actual), (long long)(expected), #actual, __LINE__) +#define CHECK_BOOL(actual, expected) check_bool((actual), (expected), #actual, __LINE__) + +/* --- enum values ------------------------------------------------------- */ + +/* The Rust port returns these numbers; the C acts on them. */ +static void enum_values(void **state) +{ + (void)state; + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_FILTER_READY, 0); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET, 1); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT, 2); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT, 0); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS, 1); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN, 2); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET, 3); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE, 4); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, 5); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER, 6); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER, 7); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET, 8); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING, 9); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER, 10); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING, 11); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS, 0); + CHECK_INT(OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE, 1); + CHECK_INT(sizeof(enum obs_source_render_route_filter), sizeof(int)); + CHECK_INT(sizeof(enum obs_source_render_route_draw), sizeof(int)); + CHECK_INT(sizeof(enum obs_source_render_route_tech_end), sizeof(int)); + /* obs-source.c passes these as int */ + CHECK_INT(sizeof(enum obs_source_type), sizeof(int)); + CHECK_INT(sizeof(enum obs_allow_direct_render), sizeof(int)); + CHECK_INT(sizeof(enum gs_color_space), sizeof(int)); + CHECK_INT(sizeof(enum gs_color_format), sizeof(int)); +} + +/* --- the target and parent check ----------------------------------------- */ + +/* No target is checked first, then no parent (#2177: a filter removed while + * it renders has neither, and draws nothing). */ +static void filter_target_and_parent(void **state) +{ + (void)state; + CHECK_INT(obs_source_render_route_filter(true, true), OBS_SOURCE_RENDER_ROUTE_FILTER_READY); + CHECK_INT(obs_source_render_route_filter(false, true), OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET); + CHECK_INT(obs_source_render_route_filter(true, false), OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT); + CHECK_INT(obs_source_render_route_filter(false, false), OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET); +} + +/* --- source_render_video(): the checks before the draw -------------------- */ + +/* The old check: type != FILTER && (output_flags & VIDEO) == 0, the flags read + * only for a type that is not a filter. A type outside the enum is not a + * filter. */ +static void video_none_every_type_and_flags(void **state) +{ + (void)state; + for (size_t t = 0; t < TYPE_COUNT; t++) { + const bool filter = TYPES[t] == OBS_SOURCE_TYPE_FILTER; + CHECK_BOOL(obs_source_render_route_video_reads_flags(TYPES[t]), !filter); + for (unsigned c = 0; c < FLAG_COMBOS; c++) { + const uint32_t flags = flags_of(c); + CHECK_BOOL(obs_source_render_route_video_none(TYPES[t], flags), + !filter && (flags & OBS_SOURCE_VIDEO) == 0); + } + } +} + +/* A filter draws whatever its flags (a filter in a chain must render or skip + * itself), and an audio-only source of any other type draws nothing. */ +static void video_none_filter_and_audio(void **state) +{ + (void)state; + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_FILTER, 0), false); + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_FILTER, OBS_SOURCE_AUDIO), false); + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_AUDIO), true); + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_SCENE, 0), true); + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_TRANSITION, OBS_SOURCE_VIDEO), false); + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_ASYNC_VIDEO), false); + /* characterized, not endorsed: ASYNC without VIDEO is no video */ + CHECK_BOOL(obs_source_render_route_video_none(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_ASYNC), true); +} + +/* The old check: type == INPUT && (output_flags & ASYNC) && + * !rendering_filter; the flags read only for an input and rendering_filter + * only for an async one. */ +static void async_update_every_type_flags_and_filter(void **state) +{ + (void)state; + for (size_t t = 0; t < TYPE_COUNT; t++) { + const bool input = TYPES[t] == OBS_SOURCE_TYPE_INPUT; + CHECK_BOOL(obs_source_render_route_async_reads_flags(TYPES[t]), input); + for (unsigned c = 0; c < FLAG_COMBOS; c++) { + const uint32_t flags = flags_of(c); + const bool async = input && (flags & OBS_SOURCE_ASYNC) != 0; + CHECK_BOOL(obs_source_render_route_async_reads_rendering_filter(TYPES[t], flags), async); + for (int rf = 0; rf < 2; rf++) + CHECK_BOOL(obs_source_render_route_async_update(TYPES[t], flags, rf != 0), + async && !rf); + } + } +} + +/* An async input drawn by its own filter chain updates its textures once, on + * the outer call (obs_source_render_filters() sets rendering_filter). A + * scene, a transition or a filter with the ASYNC flag never updates. */ +static void async_update_cases(void **state) +{ + (void)state; + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_ASYNC_VIDEO, false), true); + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_ASYNC_VIDEO, true), false); + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_VIDEO, false), false); + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_FILTER, OBS_SOURCE_ASYNC_VIDEO, false), false); + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_SCENE, OBS_SOURCE_ASYNC_VIDEO, false), false); + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_TRANSITION, OBS_SOURCE_ASYNC_VIDEO, false), + false); + /* characterized, not endorsed: ASYNC without VIDEO still updates (the + * "no video" check returned before for such a source) */ + CHECK_BOOL(obs_source_render_route_async_update(OBS_SOURCE_TYPE_INPUT, OBS_SOURCE_ASYNC, false), true); +} + +/* The old check: !context.data || !enabled, enabled read only with data. */ +static void inactive_every_input(void **state) +{ + (void)state; + CHECK_BOOL(obs_source_render_route_reads_enabled(false), false); + CHECK_BOOL(obs_source_render_route_reads_enabled(true), true); + CHECK_BOOL(obs_source_render_route_inactive(false, false), true); + CHECK_BOOL(obs_source_render_route_inactive(false, true), true); + CHECK_BOOL(obs_source_render_route_inactive(true, false), true); + CHECK_BOOL(obs_source_render_route_inactive(true, true), false); +} + +/* --- the draw, one read at a time ---------------------------------------- */ + +/* What the old chain read and drew, as a READ_ step sequence. */ +struct walk { + enum obs_source_render_route_draw route; + /* the READ_ steps asked for, in order */ + int reads[8]; + size_t read_count; +}; + +static bool step_value(enum obs_source_render_route_draw step, const bool values[6]) +{ + switch (step) { + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER: + return values[0]; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER: + return values[1]; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET: + return values[2]; + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING: + case OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING: + return values[3]; + default: + fprintf(stderr, "step %d is not a read\n", (int)step); + fail(); + } + return false; +} + +static bool is_read(enum obs_source_render_route_draw step) +{ + return step >= OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER && + step <= OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING; +} + +/* Walks from first, reading values[] (rendering_filter, video_render, + * filter_target, deinterlacing) as the C does. */ +static struct walk walk_route(enum obs_source_render_route_draw first, const bool values[6]) +{ + struct walk w = {first, {0}, 0}; + while (is_read(w.route)) { + if (w.read_count == sizeof(w.reads) / sizeof(w.reads[0])) { + fprintf(stderr, "route does not end\n"); + fail(); + } + w.reads[w.read_count++] = (int)w.route; + w.route = obs_source_render_route_draw_next(w.route, step_value(w.route, values)); + } + return w; +} + +static void check_reads(const struct walk *w, const int *expected, size_t count, int line) +{ + check_int((long long)w->read_count, (long long)count, "read count", line); + for (size_t i = 0; i < count; i++) + check_int(w->reads[i], expected[i], "read", line); +} + +/* source_render_video()'s draw for every combination of has_filters, + * rendering_filter, video_render, filter_target and deinterlacing, against + * the old chain: filters && !rendering_filter -> filters, else video_render + * -> main, else filter_target -> target, else deinterlacing -> deinterlace, + * else async; each value read only when the chain gets to it. */ +static void draw_every_source(void **state) +{ + (void)state; + for (unsigned m = 0; m < 32; m++) { + const bool has_filters = (m & 1) != 0; + const bool values[6] = {(m & 2) != 0, (m & 4) != 0, (m & 8) != 0, (m & 16) != 0}; + const bool rendering_filter = values[0], video_render = values[1], filter_target = values[2], + deinterlacing = values[3]; + + struct walk w = walk_route(obs_source_render_route_draw(has_filters), values); + + int expected_reads[4]; + size_t n = 0; + enum obs_source_render_route_draw expected; + if (has_filters) + expected_reads[n++] = OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER; + if (has_filters && !rendering_filter) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS; + } else { + expected_reads[n++] = OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER; + if (video_render) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN; + } else { + expected_reads[n++] = OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET; + if (filter_target) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET; + } else { + expected_reads[n++] = OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING; + expected = deinterlacing ? OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE + : OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC; + } + } + } + CHECK_INT(w.route, expected); + check_reads(&w, expected_reads, n, __LINE__); + } +} + +/* Each step on its own: both values for every READ_ step; a draw and values + * outside the enum come back unchanged. */ +static void draw_next_every_step(void **state) +{ + (void)state; + CHECK_INT(obs_source_render_route_draw(true), OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER); + CHECK_INT(obs_source_render_route_draw(false), OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER); + + static const struct { + enum obs_source_render_route_draw step; + enum obs_source_render_route_draw if_false, if_true; + } steps[] = { + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER, OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS, + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER}, + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER, OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET, + OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN}, + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET, OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING, + OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET}, + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING, OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, + OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE}, + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER, + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING, OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN}, + {OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING, OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, + OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE}, + }; + for (size_t i = 0; i < sizeof(steps) / sizeof(steps[0]); i++) { + CHECK_INT(obs_source_render_route_draw_next(steps[i].step, false), steps[i].if_false); + CHECK_INT(obs_source_render_route_draw_next(steps[i].step, true), steps[i].if_true); + } + + static const int unchanged[] = {OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT, + OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS, + OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN, + OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET, + OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE, + OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, + -1, + 12, + 99, + INT_MIN, + INT_MAX}; + for (size_t i = 0; i < sizeof(unchanged) / sizeof(unchanged[0]); i++) + for (int v = 0; v < 2; v++) + /* as int: the enum is unsigned on some compilers */ + CHECK_INT((int)obs_source_render_route_draw_next( + (enum obs_source_render_route_draw)unchanged[i], v != 0), + unchanged[i]); +} + +/* obs_source_skip_video_filter()'s draw for every parent flag combination, + * the target being the parent or not, video_render and deinterlacing, + * against the old chain: the target is the parent -> (neither custom draw nor + * async -> default render, else video_render -> main, else deinterlacing -> + * deinterlace, else async), else the target's video render. */ +static void skip_every_target_and_flags(void **state) +{ + (void)state; + for (int same = 0; same < 2; same++) + for (unsigned c = 0; c < FLAG_COMBOS; c++) + for (unsigned m = 0; m < 4; m++) { + const uint32_t flags = flags_of(c); + const bool custom_draw = (flags & OBS_SOURCE_CUSTOM_DRAW) != 0; + const bool async = (flags & OBS_SOURCE_ASYNC) != 0; + const bool values[6] = {false, (m & 1) != 0, false, (m & 2) != 0}; + + struct walk w = walk_route(obs_source_render_route_skip(same != 0, flags), values); + + int expected_reads[4]; + size_t n = 0; + enum obs_source_render_route_draw expected; + if (!same) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET; + } else if (!custom_draw && !async) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT; + } else { + expected_reads[n++] = OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER; + if (values[1]) { + expected = OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN; + } else { + expected_reads[n++] = + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING; + expected = values[3] ? OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE + : OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC; + } + } + CHECK_INT(w.route, expected); + check_reads(&w, expected_reads, n, __LINE__); + } +} + +/* A skipped filter whose target is another filter hands the draw to it, even + * for a custom draw or async parent; a plain parent draws with the default + * effect. */ +static void skip_cases(void **state) +{ + (void)state; + CHECK_INT(obs_source_render_route_skip(false, OBS_SOURCE_VIDEO), OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET); + CHECK_INT(obs_source_render_route_skip(false, OBS_SOURCE_CUSTOM_DRAW), OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET); + CHECK_INT(obs_source_render_route_skip(true, OBS_SOURCE_VIDEO), OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT); + CHECK_INT(obs_source_render_route_skip(true, OBS_SOURCE_ASYNC_VIDEO), + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER); + CHECK_INT(obs_source_render_route_skip(true, OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW), + OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER); + /* the SRGB flag plays no part */ + CHECK_INT(obs_source_render_route_skip(true, OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB), + OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT); +} + +/* --- obs_source_main_render() --------------------------------------------- */ + +/* The old rules: custom_draw and srgb_aware from the flags, default_effect = + * !filter_parent && filters.num == 0 && !custom_draw, filters.num read only + * for a source that is not a filter. */ +static void main_render_every_input(void **state) +{ + (void)state; + CHECK_BOOL(obs_source_render_route_main_reads_filters(false), true); + CHECK_BOOL(obs_source_render_route_main_reads_filters(true), false); + for (unsigned c = 0; c < FLAG_COMBOS; c++) { + const uint32_t flags = flags_of(c); + const bool custom_draw = (flags & OBS_SOURCE_CUSTOM_DRAW) != 0; + CHECK_BOOL(obs_source_render_route_main_custom_draw(flags), custom_draw); + CHECK_BOOL(obs_source_render_route_main_srgb_aware(flags), (flags & OBS_SOURCE_SRGB) != 0); + for (int parent = 0; parent < 2; parent++) + for (int filters = 0; filters < 2; filters++) + CHECK_BOOL(obs_source_render_route_main_default_effect(flags, parent != 0, + filters != 0), + !parent && !filters && !custom_draw); + } +} + +/* --- obs_source_process_filter_begin_with_color_space() --------------------- */ + +/* can_bypass(), for every wiring, direct render choice, pair of filter and + * parent flags and pair of spaces: (target == parent) && allow_direct == + * OBS_ALLOW_DIRECT_RENDERING && !custom draw && !async parent && the SRGB + * flags agree, and only then gs_get_color_space() is read and compared. */ +static void bypass_every_input(void **state) +{ + (void)state; + for (int same = 0; same < 2; same++) + for (size_t a = 0; a < ALLOW_COUNT; a++) + for (unsigned fc = 0; fc < FLAG_COMBOS; fc++) + for (unsigned pc = 0; pc < FLAG_COMBOS; pc++) { + const uint32_t ff = flags_of(fc), pf = flags_of(pc); + const bool reads = same && ALLOW[a] == OBS_ALLOW_DIRECT_RENDERING && + (pf & OBS_SOURCE_CUSTOM_DRAW) == 0 && + (pf & OBS_SOURCE_ASYNC) == 0 && + (ff & OBS_SOURCE_SRGB) == (pf & OBS_SOURCE_SRGB); + CHECK_BOOL(obs_source_render_route_bypass_reads_space(same != 0, ALLOW[a], ff, + pf), + reads); + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t cs = 0; cs < SPACE_COUNT; cs++) + CHECK_BOOL(obs_source_render_route_bypass(same != 0, ALLOW[a], + ff, pf, SPACES[s], + SPACES[cs]), + reads && SPACES[s] == SPACES[cs]); + } +} + +/* The colour space rules of #1493's class: a filter and a parent that differ + * on OBS_SOURCE_SRGB, or a space other than the current one, go through the + * texrender; a direct render keeps the space. */ +static void bypass_cases(void **state) +{ + (void)state; + const int direct = OBS_ALLOW_DIRECT_RENDERING; + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, GS_CS_SRGB), true); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, OBS_SOURCE_SRGB, OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB, + GS_CS_709_EXTENDED, GS_CS_709_EXTENDED), + true); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, OBS_SOURCE_SRGB, OBS_SOURCE_VIDEO, GS_CS_SRGB, + GS_CS_SRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB, GS_CS_SRGB, + GS_CS_SRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, GS_CS_709_SCRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB_16F, GS_CS_SRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(false, direct, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, GS_CS_SRGB), false); + CHECK_BOOL(obs_source_render_route_bypass(true, OBS_NO_DIRECT_RENDERING, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, + GS_CS_SRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_ASYNC_VIDEO, GS_CS_SRGB, GS_CS_SRGB), + false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW, + GS_CS_SRGB, GS_CS_SRGB), + false); + /* characterized, not endorsed: a direct render choice outside the enum + * is not direct; spaces outside the enum bypass when they are equal */ + CHECK_BOOL(obs_source_render_route_bypass(true, 2, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, GS_CS_SRGB), false); + CHECK_BOOL(obs_source_render_route_bypass(true, direct, 0, OBS_SOURCE_VIDEO, 99, 99), true); + /* the current space is not looked at when the rest does not hold */ + CHECK_BOOL(obs_source_render_route_bypass(false, direct, 0, OBS_SOURCE_VIDEO, GS_CS_SRGB, INT_MIN), false); +} + +/* The skip at zero size: either size 0. Characterized, not endorsed: the + * sizes are get_base_width()'s and get_base_height()'s uint32_t in an int, so + * a size of 2^31 or more is negative there and does not skip. */ +static void size_skip_every_size(void **state) +{ + (void)state; + static const int sizes[] = {0, 1, 2, 1080, 1920, -1, INT_MIN, INT_MAX}; + for (size_t x = 0; x < sizeof(sizes) / sizeof(sizes[0]); x++) + for (size_t y = 0; y < sizeof(sizes) / sizeof(sizes[0]); y++) + CHECK_BOOL(obs_source_render_route_size_skip(sizes[x], sizes[y]), + sizes[x] == 0 || sizes[y] == 0); +} + +/* The texrender is kept for the same format and replaced for any other, for + * every pair of formats in the enum and around it. */ +static void texrender_stale_every_format(void **state) +{ + (void)state; + for (int a = -2; a <= GS_RG16 + 2; a++) + for (int b = -2; b <= GS_RG16 + 2; b++) + CHECK_BOOL(obs_source_render_route_texrender_stale(a, b), a != b); + CHECK_BOOL(obs_source_render_route_texrender_stale(INT_MIN, INT_MIN), false); + CHECK_BOOL(obs_source_render_route_texrender_stale(INT_MIN, INT_MAX), true); +} + +/* The draw into the texrender: the default render for the parent itself when + * it is neither custom draw nor async, else the target's video render. */ +static void filter_render_every_input(void **state) +{ + (void)state; + for (int same = 0; same < 2; same++) + for (unsigned c = 0; c < FLAG_COMBOS; c++) { + const uint32_t flags = flags_of(c); + const bool plain = (flags & OBS_SOURCE_CUSTOM_DRAW) == 0 && (flags & OBS_SOURCE_ASYNC) == 0; + CHECK_INT(obs_source_render_route_filter_render(same != 0, flags), + same && plain ? OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT + : OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET); + } +} + +/* --- obs_source_process_filter_tech_end() --------------------------------- */ + +/* The filter's OBS_SOURCE_SRGB sets the linear sRGB flag; bypass or texture + * as begin decided; the technique "Draw" when none is given, else the given + * pointer itself. */ +static void tech_end_every_input(void **state) +{ + (void)state; + for (unsigned c = 0; c < FLAG_COMBOS; c++) { + const uint32_t flags = flags_of(c); + CHECK_BOOL(obs_source_render_route_tech_end_linear_srgb(flags), (flags & OBS_SOURCE_SRGB) != 0); + } + CHECK_INT(obs_source_render_route_tech_end(true), OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS); + CHECK_INT(obs_source_render_route_tech_end(false), OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE); + + const char *draw = obs_source_render_route_tech_name(NULL); + assert_non_null(draw); + assert_string_equal(draw, "Draw"); + static const char custom[] = "DrawCustom"; + static const char empty[] = ""; + assert_ptr_equal(obs_source_render_route_tech_name(custom), custom); + /* characterized, not endorsed: an empty name is passed on as it is */ + assert_ptr_equal(obs_source_render_route_tech_name(empty), empty); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(enum_values), + cmocka_unit_test(filter_target_and_parent), + cmocka_unit_test(video_none_every_type_and_flags), + cmocka_unit_test(video_none_filter_and_audio), + cmocka_unit_test(async_update_every_type_flags_and_filter), + cmocka_unit_test(async_update_cases), + cmocka_unit_test(inactive_every_input), + cmocka_unit_test(draw_every_source), + cmocka_unit_test(draw_next_every_step), + cmocka_unit_test(skip_every_target_and_flags), + cmocka_unit_test(skip_cases), + cmocka_unit_test(main_render_every_input), + cmocka_unit_test(bypass_every_input), + cmocka_unit_test(bypass_cases), + cmocka_unit_test(size_skip_every_size), + cmocka_unit_test(texrender_stale_every_format), + cmocka_unit_test(filter_render_every_input), + cmocka_unit_test(tech_end_every_input), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_source_render_route_steps.cmake b/test/cmocka/tests.d/test_source_render_route_steps.cmake new file mode 100644 index 0000000000..f86fc12e72 --- /dev/null +++ b/test/cmocka/tests.d/test_source_render_route_steps.cmake @@ -0,0 +1,14 @@ +# Characterization of the source and filter render routing of +# libobs/obs-source.c (source_render_video(), obs_source_main_render(), +# can_bypass(), obs_source_process_filter_begin_with_color_space(), +# obs_source_process_filter_tech_end() and obs_source_skip_video_filter()), +# moved to libobs/obs-source-render-route.c: the target and parent check +# (#2177), the "no video", async update and inactive checks, every draw route +# with the values each one reads, the default-effect, sRGB and custom draw +# rules, the direct-render bypass for every flag, wiring and pair of colour +# spaces, the skip at zero size, the texrender format check and the technique +# name. Builds the moved file into itself. The functions are hidden in libobs, +# so this characterizes that C source; the test calls only them. +# test_filter_color_space, test_filter_skip_removed and the other filter tests +# cover the callers through the real libobs. +# Target and test are declared in ../build/test-graph.json. From d54214533c6b6624317207ad554879be814151aa Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:27:10 -0400 Subject: [PATCH 3/6] obs-source: Add Tier 1 tests for the source and filter render routing port Tests for the port of libobs/obs-source-render-route.c.h to the render_route module of rust/obs-source. The safe core is a stub (todo!()): - filter_check() (a FilterCheck), video_reads_flags(), video_none(), async_reads_flags(), async_reads_rendering_filter(), async_update(), reads_enabled(), inactive(), draw(), draw_next() and draw_next_value() (a Draw, or the C's value, which comes back unchanged outside the enum), main_custom_draw(), main_srgb_aware(), main_reads_filters(), main_default_effect(), bypass_reads_space(), bypass(), size_skip(), texrender_stale(), filter_render(), tech_end_linear_srgb(), tech_name() (a TechName), tech_end() (a TechEnd) and skip(), on plain values: types, the direct render choice, spaces and formats as the C's int, output flags as u32; - the enum obs_source_type, OBS_SOURCE_* flag, obs_allow_direct_render and gs_color_space values, and FilterCheck, Draw and TechEnd with the C's numbers. tests/source/render_route.rs ports test_source_render_route_steps 1:1: its 18 cases under the same names (steps_*), in the same order, with the same inputs and expectations; the draw cases walk the READ_ steps against the old if/else chains written out, with the values each one reads. Edge cases (edge_*): every rule over all 65,536 low output flag patterns, each also with the high 16 bits set; types, direct render choices and spaces from -300 to 300 and near the ends of i32; sizes and formats over the same range; every C value a draw step can come back as (0 to 40 and the ends of u32) and every step reaching a draw in at most four reads. Proptests (4000 cases each): the checks, the draw and skip walks, the bypass, the main render rules, sizes and formats over full-range values. soldr cargo test -p obs-data --test obs-tests-core source::render_route fails 28 of 28 with "not yet implemented". The regression file proptest wrote for the stub is not kept. Refs #2189 --- rust/obs-source/src/lib.rs | 1 + rust/obs-source/src/render_route.rs | 390 ++++++++++ rust/obs-source/tests/source/main.rs | 1 + rust/obs-source/tests/source/render_route.rs | 732 +++++++++++++++++++ 4 files changed, 1124 insertions(+) create mode 100644 rust/obs-source/src/render_route.rs create mode 100644 rust/obs-source/tests/source/render_route.rs diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index d798250983..a81efc4e11 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -48,6 +48,7 @@ pub mod deinterlace_pacing; pub mod draw_plan; pub mod draw_technique; pub mod ptm_gate; +pub mod render_route; pub mod size_route; pub mod transition_clock; pub mod transition_render; diff --git a/rust/obs-source/src/render_route.rs b/rust/obs-source/src/render_route.rs new file mode 100644 index 0000000000..ef88650cbf --- /dev/null +++ b/rust/obs-source/src/render_route.rs @@ -0,0 +1,390 @@ +//! Safe core of `libobs/obs-source-render-route.c.h`: the source and filter +//! render routing of `obs-source.c`. +//! +//! - [`filter_check`]: the target and parent check of a filter render step, +//! no target first (#2177's guard in `obs_source_skip_video_filter()`); +//! - `source_render_video()`: [`video_reads_flags`] and [`video_none`] (a +//! source that is neither video nor a filter), [`async_reads_flags`], +//! [`async_reads_rendering_filter`] and [`async_update`] (the async +//! texture update), [`reads_enabled`] and [`inactive`] (no data or +//! disabled), and [`draw`] then [`draw_next`]: filters, main render, +//! filter target, deinterlace or async video; +//! - `obs_source_main_render()`: [`main_custom_draw`], +//! [`main_srgb_aware`], [`main_reads_filters`] and +//! [`main_default_effect`]; +//! - `obs_source_process_filter_begin_with_color_space()`: +//! [`bypass_reads_space`] and [`bypass`] (`can_bypass()`), [`size_skip`], +//! [`texrender_stale`] and [`filter_render`]; +//! - `obs_source_process_filter_tech_end()`: [`tech_end_linear_srgb`], +//! [`tech_name`] and [`tech_end`]; +//! - `obs_source_skip_video_filter()`: [`skip`] then [`draw_next`]. +//! +//! `obs-source.c.h` keeps every `gs_*` call and texrender, the source refs, +//! the render calls, `get_base_width()` and `get_base_height()`, the debug +//! markers and every field read, in their order and under their conditions. +//! A `reads_` function says whether C reads a value the old code read only on +//! one side of a short circuit; a draw route that needs a value C has not read +//! yet returns a `Read*` step, and C reads that one value and passes it to +//! [`draw_next`]. Nothing here keeps state, takes a lock or calls libobs. +//! +//! Types, the direct render choice, colour spaces and formats are the C's +//! `int`: a value outside an enum takes the path it takes in the C. Output +//! flags are the `uint32_t` `OBS_SOURCE_*` bits. Characterized, not endorsed +//! (`test_source_render_route_steps`), and kept: a filter type draws whatever +//! its flags; `ASYNC` without `VIDEO` is no video; a size of 2^31 or more is +//! negative in the C's `int` and does not skip. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::unimplemented, + clippy::unreachable +)] + +/// `OBS_SOURCE_TYPE_INPUT` (`enum obs_source_type`, `obs-source.h`). +pub const TYPE_INPUT: i32 = 0; +/// `OBS_SOURCE_TYPE_FILTER`. +pub const TYPE_FILTER: i32 = 1; +/// `OBS_SOURCE_TYPE_TRANSITION`. +pub const TYPE_TRANSITION: i32 = 2; +/// `OBS_SOURCE_TYPE_SCENE`. +pub const TYPE_SCENE: i32 = 3; + +/// `OBS_SOURCE_VIDEO` (`obs-source.h`). +pub const FLAG_VIDEO: u32 = 1 << 0; +/// `OBS_SOURCE_AUDIO`: not read here. +pub const FLAG_AUDIO: u32 = 1 << 1; +/// `OBS_SOURCE_ASYNC`. +pub const FLAG_ASYNC: u32 = 1 << 2; +/// `OBS_SOURCE_CUSTOM_DRAW`. +pub const FLAG_CUSTOM_DRAW: u32 = 1 << 3; +/// `OBS_SOURCE_CAP_DISABLED`: not read here. +pub const FLAG_CAP_DISABLED: u32 = 1 << 10; +/// `OBS_SOURCE_SRGB`. +pub const FLAG_SRGB: u32 = 1 << 15; + +/// `OBS_NO_DIRECT_RENDERING` (`enum obs_allow_direct_render`, `obs.h`). +pub const NO_DIRECT_RENDERING: i32 = 0; +/// `OBS_ALLOW_DIRECT_RENDERING`. +pub const ALLOW_DIRECT_RENDERING: i32 = 1; + +/// `GS_CS_SRGB` (`enum gs_color_space`, `graphics/graphics.h`). +pub const GS_CS_SRGB: i32 = 0; +/// `GS_CS_SRGB_16F`. +pub const GS_CS_SRGB_16F: i32 = 1; +/// `GS_CS_709_EXTENDED`. +pub const GS_CS_709_EXTENDED: i32 = 2; +/// `GS_CS_709_SCRGB`. +pub const GS_CS_709_SCRGB: i32 = 3; + +/// `GS_RG16`, the last `enum gs_color_format` value. +pub const GS_RG16: i32 = 22; + +/// `enum obs_source_render_route_filter`: what a filter render step finds. +/// The discriminants are the C's. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum FilterCheck { + /// A target and a parent: go on. + Ready = 0, + /// No target (checked first). + NoTarget = 1, + /// A target but no parent. + NoParent = 2, +} + +impl FilterCheck { + /// The C's value. + pub const fn value(self) -> u32 { + let _ = (self,); + todo!() + } +} + +/// `enum obs_source_render_route_draw`: a draw, or the value to read before +/// the draw is known. The discriminants are the C's. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Draw { + /// `obs_source_default_render(target)`. + Default = 0, + /// `obs_source_render_filters(source)`. + Filters = 1, + /// `obs_source_main_render()` of the source or the target. + Main = 2, + /// `obs_source_video_render()` of the filter target. + Target = 3, + /// `deinterlace_render()` of the source or the target. + Deinterlace = 4, + /// `obs_source_render_async_video()` of the source or the target. + Async = 5, + /// Read `source->rendering_filter`. + ReadRenderingFilter = 6, + /// Read `source->info.video_render != NULL`. + ReadVideoRender = 7, + /// Read `source->filter_target != NULL`. + ReadFilterTarget = 8, + /// Call `deinterlacing_enabled(source)`. + ReadDeinterlacing = 9, + /// Read `target->info.video_render != NULL`. + ReadTargetVideoRender = 10, + /// Call `deinterlacing_enabled(target)`. + ReadTargetDeinterlacing = 11, +} + +impl Draw { + /// Every value, in the C's order. + pub const ALL: [Self; 12] = [ + Self::Default, + Self::Filters, + Self::Main, + Self::Target, + Self::Deinterlace, + Self::Async, + Self::ReadRenderingFilter, + Self::ReadVideoRender, + Self::ReadFilterTarget, + Self::ReadDeinterlacing, + Self::ReadTargetVideoRender, + Self::ReadTargetDeinterlacing, + ]; + + /// The C's value. + pub const fn value(self) -> u32 { + let _ = (self,); + todo!() + } + + /// The step of a C value, `None` outside the enum. + pub const fn from_value(value: u32) -> Option { + let _ = (value,); + todo!() + } + + /// Whether C has a value to read before the draw is known. + pub const fn is_read(self) -> bool { + let _ = (self,); + todo!() + } +} + +/// `enum obs_source_render_route_tech_end`: what +/// `obs_source_process_filter_tech_end()` draws. The discriminants are the +/// C's. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TechEnd { + /// `render_filter_bypass()`: the target, straight through the effect. + Bypass = 0, + /// `render_filter_tex()`: the filter's texrender, if it has a texture. + Texture = 1, +} + +impl TechEnd { + /// The C's value. + pub const fn value(self) -> u32 { + let _ = (self,); + todo!() + } +} + +/// The technique `obs_source_process_filter_tech_end()` draws with. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TechName { + /// The name the filter gave. + Given, + /// `"Draw"`: the filter gave none. + Draw, +} + +/// `obs_source_render_route_filter()`: no target first, then no parent. A +/// filter removed while it renders has neither (#2177). +pub fn filter_check(has_target: bool, has_parent: bool) -> FilterCheck { + let _ = (has_target, has_parent); + todo!() +} + +/// `obs_source_render_route_video_reads_flags()`: the "no video" check +/// reads the output flags for any type but a filter. +pub fn video_reads_flags(source_type: i32) -> bool { + let _ = (source_type,); + todo!() +} + +/// `obs_source_render_route_video_none()`: a source that is neither a +/// filter nor [`FLAG_VIDEO`] draws nothing. `flags` is 0 when not read. +pub fn video_none(source_type: i32, flags: u32) -> bool { + let _ = (source_type, flags); + todo!() +} + +/// `obs_source_render_route_async_reads_flags()`: the async check reads the +/// output flags (again) for an input. +pub fn async_reads_flags(source_type: i32) -> bool { + let _ = (source_type,); + todo!() +} + +/// `obs_source_render_route_async_reads_rendering_filter()`: the async +/// check reads `rendering_filter` for an input with [`FLAG_ASYNC`]. +pub fn async_reads_rendering_filter(source_type: i32, flags: u32) -> bool { + let _ = (source_type, flags); + todo!() +} + +/// `obs_source_render_route_async_update()`: update the async textures of +/// an async input not being drawn by its own filter chain. `flags` is 0 +/// and `rendering_filter` false when not read. +pub fn async_update(source_type: i32, flags: u32, rendering_filter: bool) -> bool { + let _ = (source_type, flags, rendering_filter); + todo!() +} + +/// `obs_source_render_route_reads_enabled()`: `enabled` is read only with +/// data. +pub fn reads_enabled(has_data: bool) -> bool { + let _ = (has_data,); + todo!() +} + +/// `obs_source_render_route_inactive()`: no data, or disabled. `enabled` is +/// false when not read. +pub fn inactive(has_data: bool, enabled: bool) -> bool { + let _ = (has_data, enabled); + todo!() +} + +/// `obs_source_render_route_draw()`: the first step of +/// `source_render_video()`'s draw. +pub fn draw(has_filters: bool) -> Draw { + let _ = (has_filters,); + todo!() +} + +/// `obs_source_render_route_draw_next()`: the next step after reading the +/// value a `Read*` step asks for. A draw comes back unchanged. +pub fn draw_next(step: Draw, value: bool) -> Draw { + let _ = (step, value); + todo!() +} + +/// [`draw_next`] on the C's value: a value outside the enum comes back +/// unchanged. +pub fn draw_next_value(step: u32, value: bool) -> u32 { + let _ = (step, value); + todo!() +} + +/// `obs_source_render_route_main_custom_draw()`: [`FLAG_CUSTOM_DRAW`], the +/// source gets no effect. +pub fn main_custom_draw(flags: u32) -> bool { + let _ = (flags,); + todo!() +} + +/// `obs_source_render_route_main_srgb_aware()`: [`FLAG_SRGB`], the source +/// keeps the linear sRGB flag. +pub fn main_srgb_aware(flags: u32) -> bool { + let _ = (flags,); + todo!() +} + +/// `obs_source_render_route_main_reads_filters()`: `filters.num` is read +/// only for a source that is not a filter. +pub fn main_reads_filters(has_filter_parent: bool) -> bool { + let _ = (has_filter_parent,); + todo!() +} + +/// `obs_source_render_route_main_default_effect()`: not a filter, no +/// filters and not custom draw. `has_filters` is false when not read. +pub fn main_default_effect(flags: u32, has_filter_parent: bool, has_filters: bool) -> bool { + let _ = (flags, has_filter_parent, has_filters); + todo!() +} + +/// `obs_source_render_route_bypass_reads_space()`: the target is the +/// parent, direct rendering is allowed, the parent is neither custom draw +/// nor async and the filter and the parent agree on [`FLAG_SRGB`]. +pub fn bypass_reads_space( + target_is_parent: bool, + allow_direct: i32, + filter_flags: u32, + parent_flags: u32, +) -> bool { + let _ = (target_is_parent, allow_direct, filter_flags, parent_flags); + todo!() +} + +/// `obs_source_render_route_bypass()` (`can_bypass()`): +/// [`bypass_reads_space`] and the requested space is the current one. +/// `current_space` is any value when not read. +pub fn bypass( + target_is_parent: bool, + allow_direct: i32, + filter_flags: u32, + parent_flags: u32, + space: i32, + current_space: i32, +) -> bool { + let _ = ( + target_is_parent, + allow_direct, + filter_flags, + parent_flags, + space, + current_space, + ); + todo!() +} + +/// `obs_source_render_route_size_skip()`: a target of zero width or height. +pub fn size_skip(cx: i32, cy: i32) -> bool { + let _ = (cx, cy); + todo!() +} + +/// `obs_source_render_route_texrender_stale()`: the texrender has another +/// format than the one asked for. +pub fn texrender_stale(texrender_format: i32, format: i32) -> bool { + let _ = (texrender_format, format); + todo!() +} + +/// `obs_source_render_route_filter_render()`: [`Draw::Default`] for the +/// parent itself when it is neither custom draw nor async, else +/// [`Draw::Target`]. +pub fn filter_render(target_is_parent: bool, parent_flags: u32) -> Draw { + let _ = (target_is_parent, parent_flags); + todo!() +} + +/// `obs_source_render_route_tech_end_linear_srgb()`: the filter's +/// [`FLAG_SRGB`]. +pub fn tech_end_linear_srgb(filter_flags: u32) -> bool { + let _ = (filter_flags,); + todo!() +} + +/// `obs_source_render_route_tech_name()`: the given name, or `"Draw"`. +pub fn tech_name(has_tech_name: bool) -> TechName { + let _ = (has_tech_name,); + todo!() +} + +/// `obs_source_render_route_tech_end()`: bypass, as begin decided, or the +/// texture. +pub fn tech_end(bypass_active: bool) -> TechEnd { + let _ = (bypass_active,); + todo!() +} + +/// `obs_source_render_route_skip()`: the first step of a skipped filter's +/// draw. [`Draw::Target`] when the target is another filter, +/// [`Draw::Default`] for a parent that is neither custom draw nor async, +/// else [`Draw::ReadTargetVideoRender`]. +pub fn skip(target_is_parent: bool, parent_flags: u32) -> Draw { + let _ = (target_is_parent, parent_flags); + todo!() +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 8484274baf..81a9c57336 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -18,6 +18,7 @@ mod draw_plan; mod draw_plan_parity; mod ptm_gate; mod ptm_gate_parity; +mod render_route; mod size_route; mod size_route_parity; mod transition_clock; diff --git a/rust/obs-source/tests/source/render_route.rs b/rust/obs-source/tests/source/render_route.rs new file mode 100644 index 0000000000..fdc08f7122 --- /dev/null +++ b/rust/obs-source/tests/source/render_route.rs @@ -0,0 +1,732 @@ +//! Tier 1: `obs_source::render_route`, the source and filter render routing. +//! +//! The `steps_*` cases port `test/cmocka/test_source_render_route_steps.c` +//! 1:1, under the same case names, in the same order, with the same inputs +//! and expectations. +//! +//! The `edge_*` cases are new: every rule over every one of the 2^16 low +//! output flag patterns and the high bits, over types, direct render choices +//! and spaces from -300 to 300 and near the ends of `i32`; every step of the +//! draw against the old `if`/`else if` chains written out, with the values +//! each one reads; and proptests of each over full-range values. + +use obs_source::render_route::{ + ALLOW_DIRECT_RENDERING, Draw, FLAG_ASYNC, FLAG_AUDIO, FLAG_CAP_DISABLED, FLAG_CUSTOM_DRAW, + FLAG_SRGB, FLAG_VIDEO, FilterCheck, GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, + GS_CS_SRGB_16F, GS_RG16, NO_DIRECT_RENDERING, TYPE_FILTER, TYPE_INPUT, TYPE_SCENE, + TYPE_TRANSITION, TechEnd, TechName, async_reads_flags, async_reads_rendering_filter, + async_update, bypass, bypass_reads_space, draw, draw_next, draw_next_value, filter_check, + filter_render, inactive, main_custom_draw, main_default_effect, main_reads_filters, + main_srgb_aware, reads_enabled, size_skip, skip, tech_end, tech_end_linear_srgb, tech_name, + texrender_stale, video_none, video_reads_flags, +}; +use proptest::prelude::*; + +/// Source types, as the C test's `TYPES`. +const TYPES: [i32; 9] = [ + TYPE_INPUT, + TYPE_FILTER, + TYPE_TRANSITION, + TYPE_SCENE, + -1, + 4, + 99, + i32::MIN, + i32::MAX, +]; + +/// Colour spaces, as the C test's `SPACES`. +const SPACES: [i32; 8] = [ + GS_CS_SRGB, + GS_CS_SRGB_16F, + GS_CS_709_EXTENDED, + GS_CS_709_SCRGB, + -1, + 4, + i32::MIN, + i32::MAX, +]; + +/// Direct render choices, as the C test's `ALLOW`. +const ALLOW: [i32; 6] = [ + NO_DIRECT_RENDERING, + ALLOW_DIRECT_RENDERING, + 2, + -1, + i32::MIN, + i32::MAX, +]; + +/// The flag bits the C test combines. +const FLAG_BITS: [u32; 6] = [ + FLAG_VIDEO, + FLAG_ASYNC, + FLAG_CUSTOM_DRAW, + FLAG_SRGB, + FLAG_AUDIO, + FLAG_CAP_DISABLED, +]; + +/// Every combination of [`FLAG_BITS`], then `u32::MAX`, as the C test's +/// `flags_of()`. +fn flag_combos() -> Vec { + let mut all: Vec = (0u32..64) + .map(|combo| { + FLAG_BITS + .iter() + .enumerate() + .filter(|(bit, _)| combo & (1 << bit) != 0) + .fold(0, |flags, (_, flag)| flags | flag) + }) + .collect(); + all.push(u32::MAX); + all +} + +/// A walk through the draw: the route and the `Read*` steps, as the C +/// test's `walk_route()`. +fn walk(first: Draw, values: [bool; 4]) -> (Draw, Vec) { + let mut route = first; + let mut reads = Vec::new(); + while route.is_read() { + assert!(reads.len() < 8, "route does not end"); + reads.push(route); + let value = match route { + Draw::ReadRenderingFilter => values[0], + Draw::ReadVideoRender | Draw::ReadTargetVideoRender => values[1], + Draw::ReadFilterTarget => values[2], + _ => values[3], + }; + route = draw_next(route, value); + } + (route, reads) +} + +/// `source_render_video()`'s old chain, with the values it reads. +fn old_draw(has_filters: bool, values: [bool; 4]) -> (Draw, Vec) { + let [rendering_filter, video_render, filter_target, deinterlacing] = values; + let mut reads = Vec::new(); + if has_filters { + reads.push(Draw::ReadRenderingFilter); + } + if has_filters && !rendering_filter { + return (Draw::Filters, reads); + } + reads.push(Draw::ReadVideoRender); + if video_render { + return (Draw::Main, reads); + } + reads.push(Draw::ReadFilterTarget); + if filter_target { + return (Draw::Target, reads); + } + reads.push(Draw::ReadDeinterlacing); + if deinterlacing { + (Draw::Deinterlace, reads) + } else { + (Draw::Async, reads) + } +} + +/// `obs_source_skip_video_filter()`'s old chain, with the values it reads. +fn old_skip(same: bool, flags: u32, video_render: bool, deinterlacing: bool) -> (Draw, Vec) { + let custom_draw = flags & FLAG_CUSTOM_DRAW != 0; + let async_ = flags & FLAG_ASYNC != 0; + if !same { + return (Draw::Target, Vec::new()); + } + if !custom_draw && !async_ { + return (Draw::Default, Vec::new()); + } + let mut reads = vec![Draw::ReadTargetVideoRender]; + if video_render { + return (Draw::Main, reads); + } + reads.push(Draw::ReadTargetDeinterlacing); + if deinterlacing { + (Draw::Deinterlace, reads) + } else { + (Draw::Async, reads) + } +} + +/// `can_bypass()` written out. +fn old_bypass_reads(same: bool, allow: i32, ff: u32, pf: u32) -> bool { + same && allow == ALLOW_DIRECT_RENDERING + && pf & FLAG_CUSTOM_DRAW == 0 + && pf & FLAG_ASYNC == 0 + && ff & FLAG_SRGB == pf & FLAG_SRGB +} + +// --- enum values --------------------------------------------------------- + +#[test] +fn steps_enum_values() { + // The C acts on these numbers. + assert_eq!(FilterCheck::Ready.value(), 0); + assert_eq!(FilterCheck::NoTarget.value(), 1); + assert_eq!(FilterCheck::NoParent.value(), 2); + let draws = [ + (Draw::Default, 0), + (Draw::Filters, 1), + (Draw::Main, 2), + (Draw::Target, 3), + (Draw::Deinterlace, 4), + (Draw::Async, 5), + (Draw::ReadRenderingFilter, 6), + (Draw::ReadVideoRender, 7), + (Draw::ReadFilterTarget, 8), + (Draw::ReadDeinterlacing, 9), + (Draw::ReadTargetVideoRender, 10), + (Draw::ReadTargetDeinterlacing, 11), + ]; + for (d, v) in draws { + assert_eq!(d.value(), v, "{d:?}"); + assert_eq!(d as u32, v, "{d:?} discriminant"); + assert_eq!(Draw::from_value(v), Some(d)); + } + assert_eq!( + Draw::ALL.map(Draw::value), + core::array::from_fn(|i| u32::try_from(i).unwrap()) + ); + assert_eq!(TechEnd::Bypass.value(), 0); + assert_eq!(TechEnd::Texture.value(), 1); + assert_eq!(TYPE_INPUT, 0); + assert_eq!(TYPE_FILTER, 1); + assert_eq!(TYPE_TRANSITION, 2); + assert_eq!(TYPE_SCENE, 3); + assert_eq!(FLAG_VIDEO, 1); + assert_eq!(FLAG_AUDIO, 2); + assert_eq!(FLAG_ASYNC, 4); + assert_eq!(FLAG_CUSTOM_DRAW, 8); + assert_eq!(FLAG_CAP_DISABLED, 1 << 10); + assert_eq!(FLAG_SRGB, 1 << 15); + assert_eq!(NO_DIRECT_RENDERING, 0); + assert_eq!(ALLOW_DIRECT_RENDERING, 1); + assert_eq!(GS_RG16, 22); +} + +// --- the target and parent check ----------------------------------------- + +#[test] +fn steps_filter_target_and_parent() { + assert_eq!(filter_check(true, true), FilterCheck::Ready); + assert_eq!(filter_check(false, true), FilterCheck::NoTarget); + assert_eq!(filter_check(true, false), FilterCheck::NoParent); + assert_eq!(filter_check(false, false), FilterCheck::NoTarget); +} + +// --- source_render_video(): the checks before the draw ------------------- + +#[test] +fn steps_video_none_every_type_and_flags() { + for t in TYPES { + let filter = t == TYPE_FILTER; + assert_eq!(video_reads_flags(t), !filter, "type {t}"); + for flags in flag_combos() { + assert_eq!( + video_none(t, flags), + !filter && flags & FLAG_VIDEO == 0, + "type {t} flags {flags:#x}" + ); + } + } +} + +#[test] +fn steps_video_none_filter_and_audio() { + assert!(!video_none(TYPE_FILTER, 0)); + assert!(!video_none(TYPE_FILTER, FLAG_AUDIO)); + assert!(video_none(TYPE_INPUT, FLAG_AUDIO)); + assert!(video_none(TYPE_SCENE, 0)); + assert!(!video_none(TYPE_TRANSITION, FLAG_VIDEO)); + assert!(!video_none(TYPE_INPUT, FLAG_ASYNC | FLAG_VIDEO)); + // Characterized, not endorsed: ASYNC without VIDEO is no video. + assert!(video_none(TYPE_INPUT, FLAG_ASYNC)); +} + +#[test] +fn steps_async_update_every_type_flags_and_filter() { + for t in TYPES { + let input = t == TYPE_INPUT; + assert_eq!(async_reads_flags(t), input, "type {t}"); + for flags in flag_combos() { + let async_ = input && flags & FLAG_ASYNC != 0; + assert_eq!(async_reads_rendering_filter(t, flags), async_); + for rf in [false, true] { + assert_eq!( + async_update(t, flags, rf), + async_ && !rf, + "{t} {flags:#x} {rf}" + ); + } + } + } +} + +#[test] +fn steps_async_update_cases() { + let av = FLAG_ASYNC | FLAG_VIDEO; + assert!(async_update(TYPE_INPUT, av, false)); + assert!(!async_update(TYPE_INPUT, av, true)); + assert!(!async_update(TYPE_INPUT, FLAG_VIDEO, false)); + assert!(!async_update(TYPE_FILTER, av, false)); + assert!(!async_update(TYPE_SCENE, av, false)); + assert!(!async_update(TYPE_TRANSITION, av, false)); + // Characterized, not endorsed: ASYNC without VIDEO still updates. + assert!(async_update(TYPE_INPUT, FLAG_ASYNC, false)); +} + +#[test] +fn steps_inactive_every_input() { + assert!(!reads_enabled(false)); + assert!(reads_enabled(true)); + assert!(inactive(false, false)); + assert!(inactive(false, true)); + assert!(inactive(true, false)); + assert!(!inactive(true, true)); +} + +// --- the draw, one read at a time ---------------------------------------- + +#[test] +fn steps_draw_every_source() { + for m in 0u32..32 { + let has_filters = m & 1 != 0; + let values = [m & 2 != 0, m & 4 != 0, m & 8 != 0, m & 16 != 0]; + assert_eq!( + walk(draw(has_filters), values), + old_draw(has_filters, values), + "{m:#b}" + ); + } +} + +#[test] +fn steps_draw_next_every_step() { + assert_eq!(draw(true), Draw::ReadRenderingFilter); + assert_eq!(draw(false), Draw::ReadVideoRender); + let steps = [ + ( + Draw::ReadRenderingFilter, + Draw::Filters, + Draw::ReadVideoRender, + ), + (Draw::ReadVideoRender, Draw::ReadFilterTarget, Draw::Main), + ( + Draw::ReadFilterTarget, + Draw::ReadDeinterlacing, + Draw::Target, + ), + (Draw::ReadDeinterlacing, Draw::Async, Draw::Deinterlace), + ( + Draw::ReadTargetVideoRender, + Draw::ReadTargetDeinterlacing, + Draw::Main, + ), + ( + Draw::ReadTargetDeinterlacing, + Draw::Async, + Draw::Deinterlace, + ), + ]; + for (step, if_false, if_true) in steps { + assert_eq!(draw_next(step, false), if_false, "{step:?}"); + assert_eq!(draw_next(step, true), if_true, "{step:?}"); + } + for step in [ + Draw::Default, + Draw::Filters, + Draw::Main, + Draw::Target, + Draw::Deinterlace, + Draw::Async, + ] { + for v in [false, true] { + assert_eq!(draw_next(step, v), step); + } + } + // -1, 12, 99, INT_MIN and INT_MAX as the C's enum value. + for outside in [u32::MAX, 12, 99, 0x8000_0000, 0x7fff_ffff] { + for v in [false, true] { + assert_eq!(draw_next_value(outside, v), outside); + } + } +} + +#[test] +fn steps_skip_every_target_and_flags() { + for same in [false, true] { + for flags in flag_combos() { + for m in 0u32..4 { + let (vr, de) = (m & 1 != 0, m & 2 != 0); + assert_eq!( + walk(skip(same, flags), [false, vr, false, de]), + old_skip(same, flags, vr, de), + "{same} {flags:#x} {m}" + ); + } + } + } +} + +#[test] +fn steps_skip_cases() { + assert_eq!(skip(false, FLAG_VIDEO), Draw::Target); + assert_eq!(skip(false, FLAG_CUSTOM_DRAW), Draw::Target); + assert_eq!(skip(true, FLAG_VIDEO), Draw::Default); + assert_eq!( + skip(true, FLAG_ASYNC | FLAG_VIDEO), + Draw::ReadTargetVideoRender + ); + assert_eq!( + skip(true, FLAG_VIDEO | FLAG_CUSTOM_DRAW), + Draw::ReadTargetVideoRender + ); + // The SRGB flag plays no part. + assert_eq!(skip(true, FLAG_VIDEO | FLAG_SRGB), Draw::Default); +} + +// --- obs_source_main_render() -------------------------------------------- + +#[test] +fn steps_main_render_every_input() { + assert!(main_reads_filters(false)); + assert!(!main_reads_filters(true)); + for flags in flag_combos() { + let custom_draw = flags & FLAG_CUSTOM_DRAW != 0; + assert_eq!(main_custom_draw(flags), custom_draw); + assert_eq!(main_srgb_aware(flags), flags & FLAG_SRGB != 0); + for parent in [false, true] { + for filters in [false, true] { + assert_eq!( + main_default_effect(flags, parent, filters), + !parent && !filters && !custom_draw + ); + } + } + } +} + +// --- obs_source_process_filter_begin_with_color_space() ------------------- + +#[test] +fn steps_bypass_every_input() { + let combos = flag_combos(); + for same in [false, true] { + for allow in ALLOW { + for &ff in &combos { + for &pf in &combos { + let reads = old_bypass_reads(same, allow, ff, pf); + assert_eq!(bypass_reads_space(same, allow, ff, pf), reads); + for s in SPACES { + for cs in SPACES { + assert_eq!(bypass(same, allow, ff, pf, s, cs), reads && s == cs); + } + } + } + } + } + } +} + +#[test] +fn steps_bypass_cases() { + let direct = ALLOW_DIRECT_RENDERING; + assert!(bypass(true, direct, 0, FLAG_VIDEO, GS_CS_SRGB, GS_CS_SRGB)); + assert!(bypass( + true, + direct, + FLAG_SRGB, + FLAG_VIDEO | FLAG_SRGB, + GS_CS_709_EXTENDED, + GS_CS_709_EXTENDED + )); + assert!(!bypass( + true, direct, FLAG_SRGB, FLAG_VIDEO, GS_CS_SRGB, GS_CS_SRGB + )); + assert!(!bypass( + true, + direct, + 0, + FLAG_VIDEO | FLAG_SRGB, + GS_CS_SRGB, + GS_CS_SRGB + )); + assert!(!bypass( + true, + direct, + 0, + FLAG_VIDEO, + GS_CS_SRGB, + GS_CS_709_SCRGB + )); + assert!(!bypass( + true, + direct, + 0, + FLAG_VIDEO, + GS_CS_SRGB_16F, + GS_CS_SRGB + )); + assert!(!bypass( + false, direct, 0, FLAG_VIDEO, GS_CS_SRGB, GS_CS_SRGB + )); + assert!(!bypass( + true, + NO_DIRECT_RENDERING, + 0, + FLAG_VIDEO, + GS_CS_SRGB, + GS_CS_SRGB + )); + assert!(!bypass( + true, + direct, + 0, + FLAG_ASYNC | FLAG_VIDEO, + GS_CS_SRGB, + GS_CS_SRGB + )); + assert!(!bypass( + true, + direct, + 0, + FLAG_VIDEO | FLAG_CUSTOM_DRAW, + GS_CS_SRGB, + GS_CS_SRGB + )); + // Characterized, not endorsed: a direct render choice outside the enum + // is not direct; spaces outside the enum bypass when they are equal. + assert!(!bypass(true, 2, 0, FLAG_VIDEO, GS_CS_SRGB, GS_CS_SRGB)); + assert!(bypass(true, direct, 0, FLAG_VIDEO, 99, 99)); + // The current space is not looked at when the rest does not hold. + assert!(!bypass(false, direct, 0, FLAG_VIDEO, GS_CS_SRGB, i32::MIN)); +} + +#[test] +fn steps_size_skip_every_size() { + let sizes = [0, 1, 2, 1080, 1920, -1, i32::MIN, i32::MAX]; + for x in sizes { + for y in sizes { + assert_eq!(size_skip(x, y), x == 0 || y == 0, "{x} {y}"); + } + } +} + +#[test] +fn steps_texrender_stale_every_format() { + for a in -2..=GS_RG16 + 2 { + for b in -2..=GS_RG16 + 2 { + assert_eq!(texrender_stale(a, b), a != b); + } + } + assert!(!texrender_stale(i32::MIN, i32::MIN)); + assert!(texrender_stale(i32::MIN, i32::MAX)); +} + +#[test] +fn steps_filter_render_every_input() { + for same in [false, true] { + for flags in flag_combos() { + let plain = flags & FLAG_CUSTOM_DRAW == 0 && flags & FLAG_ASYNC == 0; + let expected = if same && plain { + Draw::Default + } else { + Draw::Target + }; + assert_eq!(filter_render(same, flags), expected, "{same} {flags:#x}"); + } + } +} + +// --- obs_source_process_filter_tech_end() -------------------------------- + +#[test] +fn steps_tech_end_every_input() { + for flags in flag_combos() { + assert_eq!(tech_end_linear_srgb(flags), flags & FLAG_SRGB != 0); + } + assert_eq!(tech_end(true), TechEnd::Bypass); + assert_eq!(tech_end(false), TechEnd::Texture); + assert_eq!(tech_name(false), TechName::Draw); + assert_eq!(tech_name(true), TechName::Given); +} + +// --- edge cases ---------------------------------------------------------- + +/// Values from -300 to 300 and near the ends of `i32`. +fn edge_ints() -> Vec { + let mut all: Vec = (-300..=300).collect(); + all.extend((0..4).map(|i| i32::MIN + i)); + all.extend((0..4).map(|i| i32::MAX - i)); + all +} + +/// Every low 16-bit flag pattern, each also with the high bits set. +fn edge_flags() -> impl Iterator { + (0u32..=0xffff).flat_map(|low| [low, low | 0xffff_0000]) +} + +#[test] +fn edge_checks_every_type_and_flag_pattern() { + for t in edge_ints() { + assert_eq!(video_reads_flags(t), t != 1); + assert_eq!(async_reads_flags(t), t == 0); + } + for flags in edge_flags() { + for t in [ + TYPE_INPUT, + TYPE_FILTER, + TYPE_TRANSITION, + TYPE_SCENE, + -1, + i32::MAX, + ] { + assert_eq!(video_none(t, flags), t != 1 && flags & 1 == 0); + let async_ = t == 0 && flags & 4 != 0; + assert_eq!(async_reads_rendering_filter(t, flags), async_); + assert_eq!(async_update(t, flags, false), async_); + assert!(!async_update(t, flags, true)); + } + let custom = flags & 8 != 0; + assert_eq!(main_custom_draw(flags), custom); + assert_eq!(main_srgb_aware(flags), flags & 0x8000 != 0); + assert_eq!(main_default_effect(flags, false, false), !custom); + assert!(!main_default_effect(flags, true, false)); + assert!(!main_default_effect(flags, false, true)); + assert_eq!(tech_end_linear_srgb(flags), flags & 0x8000 != 0); + let plain = flags & 0xc == 0; + assert_eq!(filter_render(true, flags) == Draw::Default, plain); + assert_eq!(filter_render(false, flags), Draw::Target); + let s = skip(true, flags); + assert_eq!( + s, + if plain { + Draw::Default + } else { + Draw::ReadTargetVideoRender + } + ); + assert_eq!(skip(false, flags), Draw::Target); + } +} + +#[test] +fn edge_bypass_every_flag_pattern_and_space() { + // The filter's flags matter only through SRGB. + for pf in edge_flags() { + for ff in [0, FLAG_SRGB, u32::MAX, u32::MAX ^ FLAG_SRGB] { + let reads = old_bypass_reads(true, 1, ff, pf); + assert_eq!( + bypass_reads_space(true, 1, ff, pf), + reads, + "{ff:#x} {pf:#x}" + ); + assert!(!bypass_reads_space(false, 1, ff, pf)); + assert!(!bypass_reads_space(true, 0, ff, pf)); + assert_eq!(bypass(true, 1, ff, pf, 0, 0), reads); + assert!(!bypass(true, 1, ff, pf, 0, 1)); + } + } + let ints = edge_ints(); + for &allow in &ints { + assert_eq!(bypass_reads_space(true, allow, 0, 0), allow == 1); + } + for &s in &ints { + for &cs in &[-300, -1, 0, 1, 2, 3, 4, 300, i32::MIN, i32::MAX] { + assert_eq!(bypass(true, 1, 0, FLAG_VIDEO, s, cs), s == cs); + assert!(!bypass(true, 0, 0, FLAG_VIDEO, s, cs)); + } + } +} + +#[test] +fn edge_sizes_and_formats() { + let ints = edge_ints(); + for &x in &ints { + for &y in &[0, 1, -1, i32::MIN, i32::MAX] { + assert_eq!(size_skip(x, y), x == 0 || y == 0); + assert_eq!(size_skip(y, x), x == 0 || y == 0); + } + for &f in &[0, 3, 9, 22, -1, i32::MIN, i32::MAX] { + assert_eq!(texrender_stale(x, f), x != f); + } + } +} + +#[test] +fn edge_every_step_and_value() { + // The whole C value range a step can come back as: the enum and its + // neighbours, and the ends of u32 (an int of -1 or INT_MIN). + let mut values: Vec = (0..=40).collect(); + values.extend([u32::MAX, u32::MAX - 1, 0x8000_0000, 0x7fff_ffff]); + for v in values { + for b in [false, true] { + let next = draw_next_value(v, b); + match Draw::from_value(v) { + Some(step) => assert_eq!(next, draw_next(step, b).value()), + None => assert_eq!(next, v), + } + } + } + // Every step moves forward to a draw in at most four reads. + for step in Draw::ALL { + for m in 0u32..16 { + let (route, reads) = walk(step, [m & 1 != 0, m & 2 != 0, m & 4 != 0, m & 8 != 0]); + assert!(!route.is_read()); + assert!(reads.len() <= 4, "{step:?} {m}"); + } + } +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(4000))] + + #[test] + fn prop_checks(t in any::(), flags in any::(), rf in any::(), + data in any::(), enabled in any::()) { + prop_assert_eq!(video_none(t, flags), t != TYPE_FILTER && flags & FLAG_VIDEO == 0); + prop_assert_eq!( + async_update(t, flags, rf), + t == TYPE_INPUT && flags & FLAG_ASYNC != 0 && !rf + ); + prop_assert_eq!(inactive(data, enabled), !data || !enabled); + prop_assert_eq!(reads_enabled(data), data); + } + + #[test] + fn prop_draw(has_filters in any::(), values in any::<[bool; 4]>()) { + prop_assert_eq!(walk(draw(has_filters), values), old_draw(has_filters, values)); + } + + #[test] + fn prop_skip(same in any::(), flags in any::(), vr in any::(), + de in any::()) { + prop_assert_eq!(walk(skip(same, flags), [false, vr, false, de]), + old_skip(same, flags, vr, de)); + } + + #[test] + fn prop_bypass(same in any::(), allow in any::(), ff in any::(), + pf in any::(), s in -4i32..8, cs in -4i32..8) { + let reads = old_bypass_reads(same, allow, ff, pf); + prop_assert_eq!(bypass_reads_space(same, allow, ff, pf), reads); + prop_assert_eq!(bypass(same, allow, ff, pf, s, cs), reads && s == cs); + } + + #[test] + fn prop_main(flags in any::(), parent in any::(), filters in any::()) { + prop_assert_eq!( + main_default_effect(flags, parent, filters), + !parent && !filters && flags & FLAG_CUSTOM_DRAW == 0 + ); + prop_assert_eq!(main_reads_filters(parent), !parent); + } + + #[test] + fn prop_sizes(cx in any::(), cy in any::(), a in any::(), b in any::()) { + prop_assert_eq!(size_skip(cx, cy), cx == 0 || cy == 0); + prop_assert_eq!(texrender_stale(a, b), a != b); + } +} From e994d5659a71160a4a08f7fb7760135c38f60b75 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:40:22 -0400 Subject: [PATCH 4/6] obs-source: Port the source and filter render routing Ports libobs/obs-source-render-route.c.h, the source and filter render routing of obs-source.c, to the render_route module of rust/obs-source. obs-source.c.h stays C: every gs_* call and texrender, the refs, the render functions, get_base_width() and get_base_height(), the debug markers and every field read, in their order and under their conditions. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/render_route.rs): - filter_check(): no target first, then no parent (#2177); - video_reads_flags() and video_none(): a type that is not a filter reads its flags and draws nothing without VIDEO; - async_reads_flags(), async_reads_rendering_filter() and async_update(): an input reads its flags, an async input rendering_filter, and an async input not drawn by its own chain updates its textures; - reads_enabled() and inactive(): enabled read only with data; - draw(), draw_next() and skip(): the draws as a Draw route with the C's numbers; each Read* step names the one value C reads next, in the old order, and draw_next() moves to the next step or the draw; on the C's value (draw_next_value()) a value outside the enum comes back unchanged; - main_custom_draw(), main_srgb_aware(), main_reads_filters() and main_default_effect(); - bypass_reads_space() and bypass() (can_bypass(): the target is the parent, direct rendering allowed, the parent neither custom draw nor async, the SRGB flags agree, and then the spaces match), size_skip(), texrender_stale() and filter_render(); - tech_end_linear_srgb(), tech_name() (Given or Draw) and tech_end(). The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state and takes no lock. Shim (src/ffi/source_render_route.rs): the 23 functions keep the C signatures, plain values in and out; the enums come back as their C values. One pointer crosses: obs_source_render_route_tech_name() returns the name it is given, or a static c"Draw" for NULL, and never reads through it (gs_effect_get_technique() compares the name by its bytes, so the address differing from the C literal's does not matter). No other intentional differences. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-render-route (LIBOBS_PORTED_SOURCES), whose PCH is obs.h (the enums and flags) and obs-source-render-route.h. libobs.pch.h keeps the header for obs-source.c. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the 23 are local in all three, and the hybrid's come from the Rust archive. ci/test_ported_sources.py checks the group on every host. test_source_render_route_steps still compiles the C file into itself, unchanged. The new test_source_render_route_steps_rust builds the same test source against libobs_rust-static (and libm except on MSVC): the test calls only the 23 functions and includes their header and obs.h, so it takes libobs's include directories (SIMDe, obsconfig.h) and links the archive alone. It passes 18 of 18 against the shims. The filter tests (test_filter_color_space, test_filter_skip_removed, test_scroll_filter_wide_source, test_color_correction_16f, test_darken_transparent, test_scene_rotated_scale_filter, test_slideshow_size, test_texture_render and 28 more) see the swap through the hybrid and pass on all three builds. Tier 1 source::render_route passes 28/28. clippy -D warnings is clean for the host and x86_64-pc-windows-msvc. Refs #2189 --- ci/test_ported_sources.py | 1 + .../libobs-ported-source-render-route.pch.h | 12 + libobs/build/libobs.core-source+.h.c | 1 - .../libobs.ported-source-render-route+.h.c | 5 + libobs/build/source-sets.json | 1 + libobs/cmake/rust-exports.map | 25 ++ libobs/cmake/rust-hidden.txt | 28 ++ libobs/cmake/rust-unexports-macos.txt | 25 ++ libobs/obs-source-render-route.c.h | 2 +- rust/obs-source/src/ffi/mod.rs | 1 + .../obs-source/src/ffi/source_render_route.rs | 251 ++++++++++++++++++ rust/obs-source/src/render_route.rs | 189 ++++++++----- .../test_source_render_route_steps.cmake | 41 ++- 13 files changed, 506 insertions(+), 76 deletions(-) create mode 100644 libobs/build/libobs-ported-source-render-route.pch.h create mode 100644 libobs/build/libobs.ported-source-render-route+.h.c create mode 100644 rust/obs-source/src/ffi/source_render_route.rs diff --git a/ci/test_ported_sources.py b/ci/test_ported_sources.py index f8853904e2..305650ba7b 100644 --- a/ci/test_ported_sources.py +++ b/ci/test_ported_sources.py @@ -47,6 +47,7 @@ "obs-source-draw-plan.c.h.o", "obs-source-load.c.h.o", "obs-source-ptm-gate.c.h.o", + "obs-source-render-route.c.h.o", "obs-source-size.c.h.o", "obs-source-transition-clock.c.h.o", "obs-source-transition-render.c.h.o", diff --git a/libobs/build/libobs-ported-source-render-route.pch.h b/libobs/build/libobs-ported-source-render-route.pch.h new file mode 100644 index 0000000000..b037ea5eaa --- /dev/null +++ b/libobs/build/libobs-ported-source-render-route.pch.h @@ -0,0 +1,12 @@ +/* Precompiled header (BUILD.md) for the libobs unity group + * libobs.ported-source-render-route. obs-source-render-route.c needs its + * declarations (obs-source-render-route.h) and the enum obs_source_type, + * enum obs_allow_direct_render, enum gs_color_space and enum gs_color_format + * values and the OBS_SOURCE_* flags (obs.h), not the core, so the C oracle and + * the C test can compile it on its own. The implementation file includes this + * header and nothing else. */ + +#pragma once + +#include "obs.h" +#include "obs-source-render-route.h" diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index a7eadb07af..c4cf0c47b3 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -7,7 +7,6 @@ #include "libobs/obs-scene-item-audio-actions.c.h" #include "libobs/obs-scene.c.h" #include "libobs/obs-source-deinterlace.c.h" -#include "libobs/obs-source-render-route.c.h" #include "libobs/obs-source-transition.c.h" #include "libobs/obs-source.c.h" #include "libobs/obs-view.c.h" diff --git a/libobs/build/libobs.ported-source-render-route+.h.c b/libobs/build/libobs.ported-source-render-route+.h.c new file mode 100644 index 0000000000..d81a8ca33f --- /dev/null +++ b/libobs/build/libobs.ported-source-render-route+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): ported obs-source-render-route.c. */ + +#include "libobs/build/libobs-ported-source-render-route.pch.h" + +#include "libobs/obs-source-render-route.c.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index ddd054fc33..a3ec012096 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -197,6 +197,7 @@ "build/libobs.ported-source-load+.h.c", "build/libobs.ported-source-profiler+.h.c", "build/libobs.ported-source-ptm-gate+.h.c", + "build/libobs.ported-source-render-route+.h.c", "build/libobs.ported-source-size+.h.c", "build/libobs.ported-source-transition-clock+.h.c", "build/libobs.ported-source-transition-render+.h.c", diff --git a/libobs/cmake/rust-exports.map b/libobs/cmake/rust-exports.map index 8be12efa39..919f3c67a8 100644 --- a/libobs/cmake/rust-exports.map +++ b/libobs/cmake/rust-exports.map @@ -201,6 +201,31 @@ obs_source_ptm_gate_held; obs_source_ptm_gate_mutes; obs_source_ptm_gate_talk_mutes; + /* obs-source-render-route.c. obs-source.c calls them inside libobs. + * Not EXPORT. */ + obs_source_render_route_filter; + obs_source_render_route_video_reads_flags; + obs_source_render_route_video_none; + obs_source_render_route_async_reads_flags; + obs_source_render_route_async_reads_rendering_filter; + obs_source_render_route_async_update; + obs_source_render_route_reads_enabled; + obs_source_render_route_inactive; + obs_source_render_route_draw; + obs_source_render_route_draw_next; + obs_source_render_route_main_custom_draw; + obs_source_render_route_main_srgb_aware; + obs_source_render_route_main_reads_filters; + obs_source_render_route_main_default_effect; + obs_source_render_route_bypass_reads_space; + obs_source_render_route_bypass; + obs_source_render_route_size_skip; + obs_source_render_route_texrender_stale; + obs_source_render_route_filter_render; + obs_source_render_route_tech_end_linear_srgb; + obs_source_render_route_tech_name; + obs_source_render_route_tech_end; + obs_source_render_route_skip; /* obs-source-size.c. obs-source.c calls them inside libobs. Not EXPORT. */ obs_source_size_route; obs_source_size_entry; diff --git a/libobs/cmake/rust-hidden.txt b/libobs/cmake/rust-hidden.txt index d3dbcda4d8..7e56251857 100644 --- a/libobs/cmake/rust-hidden.txt +++ b/libobs/cmake/rust-hidden.txt @@ -213,6 +213,34 @@ obs_source_ptm_gate_stop_time obs_source_ptm_gate_held obs_source_ptm_gate_mutes obs_source_ptm_gate_talk_mutes +# obs-source-render-route.c: source_render_video(), obs_source_main_render(), +# can_bypass(), obs_source_process_filter_begin_with_color_space(), +# obs_source_process_filter_tech_end() and obs_source_skip_video_filter() in +# obs-source.c call them inside libobs. Declared in obs-source-render-route.h +# without EXPORT, so the shims stay hidden the same way. +obs_source_render_route_filter +obs_source_render_route_video_reads_flags +obs_source_render_route_video_none +obs_source_render_route_async_reads_flags +obs_source_render_route_async_reads_rendering_filter +obs_source_render_route_async_update +obs_source_render_route_reads_enabled +obs_source_render_route_inactive +obs_source_render_route_draw +obs_source_render_route_draw_next +obs_source_render_route_main_custom_draw +obs_source_render_route_main_srgb_aware +obs_source_render_route_main_reads_filters +obs_source_render_route_main_default_effect +obs_source_render_route_bypass_reads_space +obs_source_render_route_bypass +obs_source_render_route_size_skip +obs_source_render_route_texrender_stale +obs_source_render_route_filter_render +obs_source_render_route_tech_end_linear_srgb +obs_source_render_route_tech_name +obs_source_render_route_tech_end +obs_source_render_route_skip # obs-source-size.c: get_base_width(), get_base_height(), get_recurse_width() # and get_recurse_height() in obs-source.c call them inside libobs. Declared in # obs-source-size.h without EXPORT, so the shims stay hidden the same way. diff --git a/libobs/cmake/rust-unexports-macos.txt b/libobs/cmake/rust-unexports-macos.txt index e270d0279e..e361d13892 100644 --- a/libobs/cmake/rust-unexports-macos.txt +++ b/libobs/cmake/rust-unexports-macos.txt @@ -175,6 +175,31 @@ _obs_source_ptm_gate_stop_time _obs_source_ptm_gate_held _obs_source_ptm_gate_mutes _obs_source_ptm_gate_talk_mutes +# obs-source-render-route.c: obs-source.c calls them inside libobs. Not +# EXPORT. +_obs_source_render_route_filter +_obs_source_render_route_video_reads_flags +_obs_source_render_route_video_none +_obs_source_render_route_async_reads_flags +_obs_source_render_route_async_reads_rendering_filter +_obs_source_render_route_async_update +_obs_source_render_route_reads_enabled +_obs_source_render_route_inactive +_obs_source_render_route_draw +_obs_source_render_route_draw_next +_obs_source_render_route_main_custom_draw +_obs_source_render_route_main_srgb_aware +_obs_source_render_route_main_reads_filters +_obs_source_render_route_main_default_effect +_obs_source_render_route_bypass_reads_space +_obs_source_render_route_bypass +_obs_source_render_route_size_skip +_obs_source_render_route_texrender_stale +_obs_source_render_route_filter_render +_obs_source_render_route_tech_end_linear_srgb +_obs_source_render_route_tech_name +_obs_source_render_route_tech_end +_obs_source_render_route_skip # obs-source-size.c: obs-source.c calls them inside libobs. Not EXPORT. _obs_source_size_route _obs_source_size_entry diff --git a/libobs/obs-source-render-route.c.h b/libobs/obs-source-render-route.c.h index fabf26f811..d09b517136 100644 --- a/libobs/obs-source-render-route.c.h +++ b/libobs/obs-source-render-route.c.h @@ -15,7 +15,7 @@ along with this program. If not, see . ******************************************************************************/ -#include "libobs/build/libobs.pch.h" +#include "libobs/build/libobs-ported-source-render-route.pch.h" /* The source and filter render routing of obs-source.c, moved from * source_render_video(), obs_source_main_render(), can_bypass(), diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index 463ca288d4..f4595c2d6f 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -10,6 +10,7 @@ pub mod source_deinterlace_pacing; pub mod source_draw_plan; pub mod source_draw_technique; pub mod source_ptm_gate; +pub mod source_render_route; pub mod source_size; pub mod source_transition_clock; pub mod source_transition_render; diff --git a/rust/obs-source/src/ffi/source_render_route.rs b/rust/obs-source/src/ffi/source_render_route.rs new file mode 100644 index 0000000000..68289402b4 --- /dev/null +++ b/rust/obs-source/src/ffi/source_render_route.rs @@ -0,0 +1,251 @@ +//! C ABI for `libobs/obs-source-render-route.c.h`, the source and filter +//! render routing, declared in `libobs/obs-source-render-route.h` without +//! `EXPORT`. +//! +//! `source_render_video()`, `obs_source_main_render()`, `can_bypass()`, +//! `obs_source_process_filter_begin_with_color_space()`, +//! `obs_source_process_filter_tech_end()` and +//! `obs_source_skip_video_filter()` in `obs-source.c.h` (still C) make +//! every `gs_*` and texrender call, take the refs, call the renders and +//! read every field in their order, pass the values in and act on what +//! comes back. So `struct obs_source` is never mirrored, and these shims +//! take plain values, touch no memory, take no lock and call nothing in +//! libobs. All of them stay hidden (`rust-hidden.txt`). +//! +//! The three enums come back as the C's enums do: their values are 0 to 11, +//! so they are `int` or `unsigned int` and every value fits both. A draw step +//! passed in that is outside the enum comes back unchanged, as the C's +//! `switch` lets it through. +//! +//! One pointer crosses: `obs_source_render_route_tech_name()` returns the +//! name it is given, or a static `"Draw"` for NULL. It never reads through +//! the pointer. `gs_effect_get_technique()` compares the name by its bytes, +//! so the static's address differing from the C literal's does not matter. +//! +//! No other intentional differences from C. + +use std::ffi::CStr; +use std::os::raw::{c_char, c_int}; + +use crate::render_route::{self as core, TechName}; + +/// `enum obs_source_render_route_filter`. +#[allow(non_camel_case_types)] +pub type obs_source_render_route_filter = u32; + +/// `enum obs_source_render_route_draw`. +#[allow(non_camel_case_types)] +pub type obs_source_render_route_draw = u32; + +/// `enum obs_source_render_route_tech_end`. +#[allow(non_camel_case_types)] +pub type obs_source_render_route_tech_end = u32; + +/// The technique `obs_source_process_filter_tech_end()` draws with when the +/// filter gives none. +pub const DRAW: &CStr = c"Draw"; + +/// `enum obs_source_render_route_filter obs_source_render_route_filter(bool +/// has_target, bool has_parent)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_filter( + has_target: bool, + has_parent: bool, +) -> obs_source_render_route_filter { + core::filter_check(has_target, has_parent).value() +} + +/// `bool obs_source_render_route_video_reads_flags(int type)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_video_reads_flags(source_type: c_int) -> bool { + core::video_reads_flags(source_type) +} + +/// `bool obs_source_render_route_video_none(int type, uint32_t flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_video_none(source_type: c_int, flags: u32) -> bool { + core::video_none(source_type, flags) +} + +/// `bool obs_source_render_route_async_reads_flags(int type)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_async_reads_flags(source_type: c_int) -> bool { + core::async_reads_flags(source_type) +} + +/// `bool obs_source_render_route_async_reads_rendering_filter(int type, +/// uint32_t flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_async_reads_rendering_filter( + source_type: c_int, + flags: u32, +) -> bool { + core::async_reads_rendering_filter(source_type, flags) +} + +/// `bool obs_source_render_route_async_update(int type, uint32_t flags, bool +/// rendering_filter)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_async_update( + source_type: c_int, + flags: u32, + rendering_filter: bool, +) -> bool { + core::async_update(source_type, flags, rendering_filter) +} + +/// `bool obs_source_render_route_reads_enabled(bool has_data)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_reads_enabled(has_data: bool) -> bool { + core::reads_enabled(has_data) +} + +/// `bool obs_source_render_route_inactive(bool has_data, bool enabled)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_inactive(has_data: bool, enabled: bool) -> bool { + core::inactive(has_data, enabled) +} + +/// `enum obs_source_render_route_draw obs_source_render_route_draw(bool +/// has_filters)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_draw(has_filters: bool) -> obs_source_render_route_draw { + core::draw(has_filters).value() +} + +/// `enum obs_source_render_route_draw obs_source_render_route_draw_next(enum +/// obs_source_render_route_draw step, bool value)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_draw_next( + step: obs_source_render_route_draw, + value: bool, +) -> obs_source_render_route_draw { + core::draw_next_value(step, value) +} + +/// `bool obs_source_render_route_main_custom_draw(uint32_t flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_main_custom_draw(flags: u32) -> bool { + core::main_custom_draw(flags) +} + +/// `bool obs_source_render_route_main_srgb_aware(uint32_t flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_main_srgb_aware(flags: u32) -> bool { + core::main_srgb_aware(flags) +} + +/// `bool obs_source_render_route_main_reads_filters(bool has_filter_parent)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_main_reads_filters(has_filter_parent: bool) -> bool { + core::main_reads_filters(has_filter_parent) +} + +/// `bool obs_source_render_route_main_default_effect(uint32_t flags, bool +/// has_filter_parent, bool has_filters)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_main_default_effect( + flags: u32, + has_filter_parent: bool, + has_filters: bool, +) -> bool { + core::main_default_effect(flags, has_filter_parent, has_filters) +} + +/// `bool obs_source_render_route_bypass_reads_space(bool target_is_parent, +/// int allow_direct, uint32_t filter_flags, uint32_t parent_flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_bypass_reads_space( + target_is_parent: bool, + allow_direct: c_int, + filter_flags: u32, + parent_flags: u32, +) -> bool { + core::bypass_reads_space(target_is_parent, allow_direct, filter_flags, parent_flags) +} + +/// `bool obs_source_render_route_bypass(bool target_is_parent, int +/// allow_direct, uint32_t filter_flags, uint32_t parent_flags, int space, int +/// current_space)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_bypass( + target_is_parent: bool, + allow_direct: c_int, + filter_flags: u32, + parent_flags: u32, + space: c_int, + current_space: c_int, +) -> bool { + core::bypass( + target_is_parent, + allow_direct, + filter_flags, + parent_flags, + space, + current_space, + ) +} + +/// `bool obs_source_render_route_size_skip(int cx, int cy)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_size_skip(cx: c_int, cy: c_int) -> bool { + core::size_skip(cx, cy) +} + +/// `bool obs_source_render_route_texrender_stale(int texrender_format, int +/// format)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_texrender_stale( + texrender_format: c_int, + format: c_int, +) -> bool { + core::texrender_stale(texrender_format, format) +} + +/// `enum obs_source_render_route_draw +/// obs_source_render_route_filter_render(bool target_is_parent, uint32_t +/// parent_flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_filter_render( + target_is_parent: bool, + parent_flags: u32, +) -> obs_source_render_route_draw { + core::filter_render(target_is_parent, parent_flags).value() +} + +/// `bool obs_source_render_route_tech_end_linear_srgb(uint32_t +/// filter_flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_tech_end_linear_srgb(filter_flags: u32) -> bool { + core::tech_end_linear_srgb(filter_flags) +} + +/// `const char *obs_source_render_route_tech_name(const char *tech_name)`: +/// `tech_name` itself, or [`DRAW`] for NULL. The pointer is compared with +/// NULL and passed back, never read. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_tech_name(tech_name: *const c_char) -> *const c_char { + match core::tech_name(!tech_name.is_null()) { + TechName::Given => tech_name, + TechName::Draw => DRAW.as_ptr(), + } +} + +/// `enum obs_source_render_route_tech_end +/// obs_source_render_route_tech_end(bool bypass_active)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_tech_end( + bypass_active: bool, +) -> obs_source_render_route_tech_end { + core::tech_end(bypass_active).value() +} + +/// `enum obs_source_render_route_draw obs_source_render_route_skip(bool +/// target_is_parent, uint32_t parent_flags)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_render_route_skip( + target_is_parent: bool, + parent_flags: u32, +) -> obs_source_render_route_draw { + core::skip(target_is_parent, parent_flags).value() +} diff --git a/rust/obs-source/src/render_route.rs b/rust/obs-source/src/render_route.rs index ef88650cbf..25b612f2ba 100644 --- a/rust/obs-source/src/render_route.rs +++ b/rust/obs-source/src/render_route.rs @@ -41,6 +41,7 @@ clippy::unwrap_used, clippy::expect_used, clippy::panic, + clippy::todo, clippy::unimplemented, clippy::unreachable )] @@ -99,8 +100,11 @@ pub enum FilterCheck { impl FilterCheck { /// The C's value. pub const fn value(self) -> u32 { - let _ = (self,); - todo!() + match self { + Self::Ready => 0, + Self::NoTarget => 1, + Self::NoParent => 2, + } } } @@ -153,20 +157,52 @@ impl Draw { /// The C's value. pub const fn value(self) -> u32 { - let _ = (self,); - todo!() + match self { + Self::Default => 0, + Self::Filters => 1, + Self::Main => 2, + Self::Target => 3, + Self::Deinterlace => 4, + Self::Async => 5, + Self::ReadRenderingFilter => 6, + Self::ReadVideoRender => 7, + Self::ReadFilterTarget => 8, + Self::ReadDeinterlacing => 9, + Self::ReadTargetVideoRender => 10, + Self::ReadTargetDeinterlacing => 11, + } } /// The step of a C value, `None` outside the enum. pub const fn from_value(value: u32) -> Option { - let _ = (value,); - todo!() + match value { + 0 => Some(Self::Default), + 1 => Some(Self::Filters), + 2 => Some(Self::Main), + 3 => Some(Self::Target), + 4 => Some(Self::Deinterlace), + 5 => Some(Self::Async), + 6 => Some(Self::ReadRenderingFilter), + 7 => Some(Self::ReadVideoRender), + 8 => Some(Self::ReadFilterTarget), + 9 => Some(Self::ReadDeinterlacing), + 10 => Some(Self::ReadTargetVideoRender), + 11 => Some(Self::ReadTargetDeinterlacing), + _ => None, + } } /// Whether C has a value to read before the draw is known. pub const fn is_read(self) -> bool { - let _ = (self,); - todo!() + matches!( + self, + Self::ReadRenderingFilter + | Self::ReadVideoRender + | Self::ReadFilterTarget + | Self::ReadDeinterlacing + | Self::ReadTargetVideoRender + | Self::ReadTargetDeinterlacing + ) } } @@ -184,8 +220,10 @@ pub enum TechEnd { impl TechEnd { /// The C's value. pub const fn value(self) -> u32 { - let _ = (self,); - todo!() + match self { + Self::Bypass => 0, + Self::Texture => 1, + } } } @@ -201,107 +239,121 @@ pub enum TechName { /// `obs_source_render_route_filter()`: no target first, then no parent. A /// filter removed while it renders has neither (#2177). pub fn filter_check(has_target: bool, has_parent: bool) -> FilterCheck { - let _ = (has_target, has_parent); - todo!() + if !has_target { + FilterCheck::NoTarget + } else if !has_parent { + FilterCheck::NoParent + } else { + FilterCheck::Ready + } } /// `obs_source_render_route_video_reads_flags()`: the "no video" check /// reads the output flags for any type but a filter. pub fn video_reads_flags(source_type: i32) -> bool { - let _ = (source_type,); - todo!() + source_type != TYPE_FILTER } /// `obs_source_render_route_video_none()`: a source that is neither a /// filter nor [`FLAG_VIDEO`] draws nothing. `flags` is 0 when not read. pub fn video_none(source_type: i32, flags: u32) -> bool { - let _ = (source_type, flags); - todo!() + source_type != TYPE_FILTER && flags & FLAG_VIDEO == 0 } /// `obs_source_render_route_async_reads_flags()`: the async check reads the /// output flags (again) for an input. pub fn async_reads_flags(source_type: i32) -> bool { - let _ = (source_type,); - todo!() + source_type == TYPE_INPUT } /// `obs_source_render_route_async_reads_rendering_filter()`: the async /// check reads `rendering_filter` for an input with [`FLAG_ASYNC`]. pub fn async_reads_rendering_filter(source_type: i32, flags: u32) -> bool { - let _ = (source_type, flags); - todo!() + source_type == TYPE_INPUT && flags & FLAG_ASYNC != 0 } /// `obs_source_render_route_async_update()`: update the async textures of /// an async input not being drawn by its own filter chain. `flags` is 0 /// and `rendering_filter` false when not read. pub fn async_update(source_type: i32, flags: u32, rendering_filter: bool) -> bool { - let _ = (source_type, flags, rendering_filter); - todo!() + async_reads_rendering_filter(source_type, flags) && !rendering_filter } /// `obs_source_render_route_reads_enabled()`: `enabled` is read only with /// data. pub fn reads_enabled(has_data: bool) -> bool { - let _ = (has_data,); - todo!() + has_data } /// `obs_source_render_route_inactive()`: no data, or disabled. `enabled` is /// false when not read. pub fn inactive(has_data: bool, enabled: bool) -> bool { - let _ = (has_data, enabled); - todo!() + !has_data || !enabled } /// `obs_source_render_route_draw()`: the first step of /// `source_render_video()`'s draw. pub fn draw(has_filters: bool) -> Draw { - let _ = (has_filters,); - todo!() + if has_filters { + Draw::ReadRenderingFilter + } else { + Draw::ReadVideoRender + } } /// `obs_source_render_route_draw_next()`: the next step after reading the /// value a `Read*` step asks for. A draw comes back unchanged. pub fn draw_next(step: Draw, value: bool) -> Draw { - let _ = (step, value); - todo!() + match (step, value) { + (Draw::ReadRenderingFilter, false) => Draw::Filters, + (Draw::ReadRenderingFilter, true) => Draw::ReadVideoRender, + (Draw::ReadVideoRender | Draw::ReadTargetVideoRender, true) => Draw::Main, + (Draw::ReadVideoRender, false) => Draw::ReadFilterTarget, + (Draw::ReadFilterTarget, true) => Draw::Target, + (Draw::ReadFilterTarget, false) => Draw::ReadDeinterlacing, + (Draw::ReadDeinterlacing | Draw::ReadTargetDeinterlacing, true) => Draw::Deinterlace, + (Draw::ReadDeinterlacing | Draw::ReadTargetDeinterlacing, false) => Draw::Async, + (Draw::ReadTargetVideoRender, false) => Draw::ReadTargetDeinterlacing, + ( + Draw::Default + | Draw::Filters + | Draw::Main + | Draw::Target + | Draw::Deinterlace + | Draw::Async, + _, + ) => step, + } } /// [`draw_next`] on the C's value: a value outside the enum comes back /// unchanged. pub fn draw_next_value(step: u32, value: bool) -> u32 { - let _ = (step, value); - todo!() + Draw::from_value(step).map_or(step, |step| draw_next(step, value).value()) } /// `obs_source_render_route_main_custom_draw()`: [`FLAG_CUSTOM_DRAW`], the /// source gets no effect. pub fn main_custom_draw(flags: u32) -> bool { - let _ = (flags,); - todo!() + flags & FLAG_CUSTOM_DRAW != 0 } /// `obs_source_render_route_main_srgb_aware()`: [`FLAG_SRGB`], the source /// keeps the linear sRGB flag. pub fn main_srgb_aware(flags: u32) -> bool { - let _ = (flags,); - todo!() + flags & FLAG_SRGB != 0 } /// `obs_source_render_route_main_reads_filters()`: `filters.num` is read /// only for a source that is not a filter. pub fn main_reads_filters(has_filter_parent: bool) -> bool { - let _ = (has_filter_parent,); - todo!() + !has_filter_parent } /// `obs_source_render_route_main_default_effect()`: not a filter, no /// filters and not custom draw. `has_filters` is false when not read. pub fn main_default_effect(flags: u32, has_filter_parent: bool, has_filters: bool) -> bool { - let _ = (flags, has_filter_parent, has_filters); - todo!() + !has_filter_parent && !has_filters && !main_custom_draw(flags) } /// `obs_source_render_route_bypass_reads_space()`: the target is the @@ -313,8 +365,11 @@ pub fn bypass_reads_space( filter_flags: u32, parent_flags: u32, ) -> bool { - let _ = (target_is_parent, allow_direct, filter_flags, parent_flags); - todo!() + target_is_parent + && allow_direct == ALLOW_DIRECT_RENDERING + && parent_flags & FLAG_CUSTOM_DRAW == 0 + && parent_flags & FLAG_ASYNC == 0 + && filter_flags & FLAG_SRGB == parent_flags & FLAG_SRGB } /// `obs_source_render_route_bypass()` (`can_bypass()`): @@ -328,56 +383,55 @@ pub fn bypass( space: i32, current_space: i32, ) -> bool { - let _ = ( - target_is_parent, - allow_direct, - filter_flags, - parent_flags, - space, - current_space, - ); - todo!() + bypass_reads_space(target_is_parent, allow_direct, filter_flags, parent_flags) + && space == current_space } /// `obs_source_render_route_size_skip()`: a target of zero width or height. pub fn size_skip(cx: i32, cy: i32) -> bool { - let _ = (cx, cy); - todo!() + cx == 0 || cy == 0 } /// `obs_source_render_route_texrender_stale()`: the texrender has another /// format than the one asked for. pub fn texrender_stale(texrender_format: i32, format: i32) -> bool { - let _ = (texrender_format, format); - todo!() + texrender_format != format } /// `obs_source_render_route_filter_render()`: [`Draw::Default`] for the /// parent itself when it is neither custom draw nor async, else /// [`Draw::Target`]. pub fn filter_render(target_is_parent: bool, parent_flags: u32) -> Draw { - let _ = (target_is_parent, parent_flags); - todo!() + if target_is_parent && parent_flags & (FLAG_CUSTOM_DRAW | FLAG_ASYNC) == 0 { + Draw::Default + } else { + Draw::Target + } } /// `obs_source_render_route_tech_end_linear_srgb()`: the filter's /// [`FLAG_SRGB`]. pub fn tech_end_linear_srgb(filter_flags: u32) -> bool { - let _ = (filter_flags,); - todo!() + filter_flags & FLAG_SRGB != 0 } /// `obs_source_render_route_tech_name()`: the given name, or `"Draw"`. pub fn tech_name(has_tech_name: bool) -> TechName { - let _ = (has_tech_name,); - todo!() + if has_tech_name { + TechName::Given + } else { + TechName::Draw + } } /// `obs_source_render_route_tech_end()`: bypass, as begin decided, or the /// texture. pub fn tech_end(bypass_active: bool) -> TechEnd { - let _ = (bypass_active,); - todo!() + if bypass_active { + TechEnd::Bypass + } else { + TechEnd::Texture + } } /// `obs_source_render_route_skip()`: the first step of a skipped filter's @@ -385,6 +439,11 @@ pub fn tech_end(bypass_active: bool) -> TechEnd { /// [`Draw::Default`] for a parent that is neither custom draw nor async, /// else [`Draw::ReadTargetVideoRender`]. pub fn skip(target_is_parent: bool, parent_flags: u32) -> Draw { - let _ = (target_is_parent, parent_flags); - todo!() + if !target_is_parent { + Draw::Target + } else if parent_flags & (FLAG_CUSTOM_DRAW | FLAG_ASYNC) == 0 { + Draw::Default + } else { + Draw::ReadTargetVideoRender + } } diff --git a/test/cmocka/tests.d/test_source_render_route_steps.cmake b/test/cmocka/tests.d/test_source_render_route_steps.cmake index f86fc12e72..d795d34ccd 100644 --- a/test/cmocka/tests.d/test_source_render_route_steps.cmake +++ b/test/cmocka/tests.d/test_source_render_route_steps.cmake @@ -2,13 +2,36 @@ # libobs/obs-source.c (source_render_video(), obs_source_main_render(), # can_bypass(), obs_source_process_filter_begin_with_color_space(), # obs_source_process_filter_tech_end() and obs_source_skip_video_filter()), -# moved to libobs/obs-source-render-route.c: the target and parent check -# (#2177), the "no video", async update and inactive checks, every draw route -# with the values each one reads, the default-effect, sRGB and custom draw -# rules, the direct-render bypass for every flag, wiring and pair of colour -# spaces, the skip at zero size, the texrender format check and the technique -# name. Builds the moved file into itself. The functions are hidden in libobs, -# so this characterizes that C source; the test calls only them. -# test_filter_color_space, test_filter_skip_removed and the other filter tests -# cover the callers through the real libobs. +# moved to libobs/obs-source-render-route.c and ported to rust/obs-source +# (render_route): the target and parent check (#2177), the "no video", async +# update and inactive checks, every draw route with the values each one reads, +# the default-effect, sRGB and custom draw rules, the direct-render bypass for +# every flag, wiring and pair of colour spaces, the skip at zero size, the +# texrender format check and the technique name. Builds the moved file into +# itself. The functions are hidden in libobs, so this characterizes that C +# source; the test calls only them. test_filter_color_space, +# test_filter_skip_removed and the other filter tests cover the callers through +# the real libobs. # Target and test are declared in ../build/test-graph.json. + +# Exercise the unchanged characterization against the Rust shims. The test +# calls only the 23 hidden functions and includes their header and obs.h (for +# the enum obs_source_type, obs_allow_direct_render, gs_color_space and +# gs_color_format values and the OBS_SOURCE_* flags; obs.h pulls SIMDe and +# obsconfig.h, hence libobs's include directories), so it links the archive +# alone: its obs-source member needs nothing from libobs or the other crates. +# Linking the archive directly avoids exporting private symbols just for a +# test. +if(TARGET libobs_rust-static) + add_executable(test_source_render_route_steps_rust test_source_render_route_steps.c) + target_include_directories( + test_source_render_route_steps_rust + PRIVATE "${CMAKE_SOURCE_DIR}/libobs" $ ${CMOCKA_INCLUDE_DIR} + ) + target_link_libraries(test_source_render_route_steps_rust PRIVATE libobs_rust-static ${CMOCKA_LIBRARIES}) + if(NOT MSVC) + target_link_libraries(test_source_render_route_steps_rust PRIVATE m) + endif() + add_dependencies(cmocka-tests test_source_render_route_steps_rust) + add_test(test_source_render_route_steps_rust ${CMAKE_CURRENT_BINARY_DIR}/test_source_render_route_steps_rust) +endif() From bae5546bb3ba9512a82618bd96e53e2025eb4ebe Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:49:43 -0400 Subject: [PATCH 5/6] obs-source: Check the source and filter render routing port against the C Oracle (oracle/source_render_route.c): the original obs-source-render-route.c.h, unmodified, with its 23 functions renamed to oracle_*, plus every value the port uses (the four source types, the six OBS_SOURCE_* flags it names, the direct render choices, the four colour spaces, GS_RG16, and the filter check, draw and tech end values) and the sizes of the four libobs enums, the three route enums, int, bool and uint32_t. The rename macros also rename the three enum tags that share their function's name, so the size table names the renamed tags. The bool results are declared as u8 on the Rust side, the byte the C returns. Tier 3 (tests/source/render_route_parity.rs, 15 tests). Every function takes and returns plain values, so each run compares the bytes of the two results; obs_source_render_route_tech_name() must hand back the pointer it is given on both sides, and "Draw" for NULL on both. - the values and sizes against the C headers; - every boolean input of the single-call functions; - types, direct render choices, spaces, formats and sizes over every value from -70000 to 70000, every 65521st i32 over the whole range and the ends (205,569 values); - all 65,536 low 16-bit output flag patterns, with the high bits clear and set, for every rule that reads flags, for ten types; - can_bypass(): both wirings, 14 direct render choices, every pair of 65 flag patterns and every pair of 12 spaces (17,035,200 inputs), then every parent flag pattern for four filter flags and the sweeps above for the direct render choice and both spaces; - every draw step value from 0 to 70000 and near the ends of u32, both ways; - source_render_video()'s decisions in its order (the reads, the skips, the async update, the draw walk and obs_source_main_render()'s rules), one copy on each side: 166,400 sources; the begin, tech end and skip decisions of a filter in their order: 19,906,560 filters; - proptests (20,000 cases each) over full-range values. None of these functions divides or does arithmetic, so no input reaches a path that traps under MSVC x64 (_udiv128, INT_MIN / -1): no input is skipped on any target. Tier 1 and the parity module passed 6 runs in a row with fresh seeds (43/43 each). No regression file came up on the port; the ones the todo!() stub and the mutants below wrote are not kept. Mutation check: each mutant of src/render_route.rs against Tier 1, Tier 3 and test_source_render_route_steps_rust (the C characterization linked to the Rust archive). Failing tests per tier: | Mutant | Change | Tier 1 | Tier 3 | cmocka | |---|---|---|---|---| | M1 | video_none ignores the filter type | 3 | 4 | 2 | | M2 | async_update ignores rendering_filter | 3 | 4 | 2 | | M3 | enabled read without data | 2 | 3 | 1 | | M4 | filter target step draws main | 3 | 3 | 2 | | M5 | skipped filter's chain reads its own filter target | 3 | 3 | 2 | | M6 | bypass ignores the SRGB match | 3 | 4 | 2 | | M7 | bypass ignores the space | 3 | 5 | 2 | | M8 | zero size needs both sizes 0 | 2 | 3 | 1 | | M9 | texrender stale only for a smaller format | 3 | 4 | 1 | | M10 | filter render ignores ASYNC | 2 | 4 | 1 | | M11 | tech end linear sRGB inverted | 2 | 4 | 1 | | M12 | skip of another filter draws default | 4 | 4 | 2 | | M13 | a step outside the enum becomes DEFAULT | 2 | 2 | 1 | | M14 | no parent checked before no target | 1 | 3 | 1 | | M15 | default effect ignores custom draw | 3 | 4 | 1 | | M16 | direct render accepts any non-zero choice | 4 | 5 | 2 | All 16 are killed by every tier. Refs #2189 --- .../obs-c-oracle/oracle/source_render_route.c | 112 +++ rust/obs-c-oracle/src/lib.rs | 1 + rust/obs-c-oracle/src/source_render_route.rs | 136 +++ rust/obs-source/tests/source/main.rs | 1 + .../tests/source/render_route_parity.rs | 887 ++++++++++++++++++ 5 files changed, 1137 insertions(+) create mode 100644 rust/obs-c-oracle/oracle/source_render_route.c create mode 100644 rust/obs-c-oracle/src/source_render_route.rs create mode 100644 rust/obs-source/tests/source/render_route_parity.rs diff --git a/rust/obs-c-oracle/oracle/source_render_route.c b/rust/obs-c-oracle/oracle/source_render_route.c new file mode 100644 index 0000000000..0ecff56079 --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_render_route.c @@ -0,0 +1,112 @@ +/* Test-only oracle: the original libobs/obs-source-render-route.c.h, + * unmodified, with its 23 functions renamed to oracle_* so they can link + * next to the Rust shims. */ +#include +#include + +#define obs_source_render_route_filter oracle_obs_source_render_route_filter +#define obs_source_render_route_video_reads_flags oracle_obs_source_render_route_video_reads_flags +#define obs_source_render_route_video_none oracle_obs_source_render_route_video_none +#define obs_source_render_route_async_reads_flags oracle_obs_source_render_route_async_reads_flags +#define obs_source_render_route_async_reads_rendering_filter oracle_obs_source_render_route_async_reads_rendering_filter +#define obs_source_render_route_async_update oracle_obs_source_render_route_async_update +#define obs_source_render_route_reads_enabled oracle_obs_source_render_route_reads_enabled +#define obs_source_render_route_inactive oracle_obs_source_render_route_inactive +#define obs_source_render_route_draw oracle_obs_source_render_route_draw +#define obs_source_render_route_draw_next oracle_obs_source_render_route_draw_next +#define obs_source_render_route_main_custom_draw oracle_obs_source_render_route_main_custom_draw +#define obs_source_render_route_main_srgb_aware oracle_obs_source_render_route_main_srgb_aware +#define obs_source_render_route_main_reads_filters oracle_obs_source_render_route_main_reads_filters +#define obs_source_render_route_main_default_effect oracle_obs_source_render_route_main_default_effect +#define obs_source_render_route_bypass_reads_space oracle_obs_source_render_route_bypass_reads_space +#define obs_source_render_route_bypass oracle_obs_source_render_route_bypass +#define obs_source_render_route_size_skip oracle_obs_source_render_route_size_skip +#define obs_source_render_route_texrender_stale oracle_obs_source_render_route_texrender_stale +#define obs_source_render_route_filter_render oracle_obs_source_render_route_filter_render +#define obs_source_render_route_tech_end_linear_srgb oracle_obs_source_render_route_tech_end_linear_srgb +#define obs_source_render_route_tech_name oracle_obs_source_render_route_tech_name +#define obs_source_render_route_tech_end oracle_obs_source_render_route_tech_end +#define obs_source_render_route_skip oracle_obs_source_render_route_skip + +#include "obs-source-render-route.c.h" + +#undef obs_source_render_route_skip +#undef obs_source_render_route_tech_end +#undef obs_source_render_route_tech_name +#undef obs_source_render_route_tech_end_linear_srgb +#undef obs_source_render_route_filter_render +#undef obs_source_render_route_texrender_stale +#undef obs_source_render_route_size_skip +#undef obs_source_render_route_bypass +#undef obs_source_render_route_bypass_reads_space +#undef obs_source_render_route_main_default_effect +#undef obs_source_render_route_main_reads_filters +#undef obs_source_render_route_main_srgb_aware +#undef obs_source_render_route_main_custom_draw +#undef obs_source_render_route_draw_next +#undef obs_source_render_route_draw +#undef obs_source_render_route_inactive +#undef obs_source_render_route_reads_enabled +#undef obs_source_render_route_async_update +#undef obs_source_render_route_async_reads_rendering_filter +#undef obs_source_render_route_async_reads_flags +#undef obs_source_render_route_video_none +#undef obs_source_render_route_video_reads_flags +#undef obs_source_render_route_filter + +/* The enum values and sizes the port relies on, in the order of + * rust/obs-c-oracle/src/source_render_route.rs; INT64_MIN past the end. The + * renames above also renamed the three enum tags that share their function's + * name, so the size table names the renamed tags. */ +int64_t oracle_source_render_route_value(size_t which); + +int64_t oracle_source_render_route_value(size_t which) +{ + const int64_t values[] = { + (int64_t)OBS_SOURCE_TYPE_INPUT, + (int64_t)OBS_SOURCE_TYPE_FILTER, + (int64_t)OBS_SOURCE_TYPE_TRANSITION, + (int64_t)OBS_SOURCE_TYPE_SCENE, + (int64_t)OBS_SOURCE_VIDEO, + (int64_t)OBS_SOURCE_AUDIO, + (int64_t)OBS_SOURCE_ASYNC, + (int64_t)OBS_SOURCE_CUSTOM_DRAW, + (int64_t)OBS_SOURCE_CAP_DISABLED, + (int64_t)OBS_SOURCE_SRGB, + (int64_t)OBS_NO_DIRECT_RENDERING, + (int64_t)OBS_ALLOW_DIRECT_RENDERING, + (int64_t)GS_CS_SRGB, + (int64_t)GS_CS_SRGB_16F, + (int64_t)GS_CS_709_EXTENDED, + (int64_t)GS_CS_709_SCRGB, + (int64_t)GS_RG16, + (int64_t)OBS_SOURCE_RENDER_ROUTE_FILTER_READY, + (int64_t)OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET, + (int64_t)OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER, + (int64_t)OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING, + (int64_t)OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS, + (int64_t)OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE, + (int64_t)sizeof(enum obs_source_type), + (int64_t)sizeof(enum obs_allow_direct_render), + (int64_t)sizeof(enum gs_color_space), + (int64_t)sizeof(enum gs_color_format), + (int64_t)sizeof(enum oracle_obs_source_render_route_filter), + (int64_t)sizeof(enum oracle_obs_source_render_route_draw), + (int64_t)sizeof(enum oracle_obs_source_render_route_tech_end), + (int64_t)sizeof(int), + (int64_t)sizeof(bool), + (int64_t)sizeof(uint32_t), + }; + return which < sizeof(values) / sizeof(values[0]) ? values[which] : INT64_MIN; +} diff --git a/rust/obs-c-oracle/src/lib.rs b/rust/obs-c-oracle/src/lib.rs index c26fbe1e0a..fdba683e90 100644 --- a/rust/obs-c-oracle/src/lib.rs +++ b/rust/obs-c-oracle/src/lib.rs @@ -138,6 +138,7 @@ pub mod source_draw_plan; pub mod source_load; pub mod source_profiler; pub mod source_ptm_gate; +pub mod source_render_route; pub mod source_size; pub mod source_transition_clock; pub mod source_transition_render; diff --git a/rust/obs-c-oracle/src/source_render_route.rs b/rust/obs-c-oracle/src/source_render_route.rs new file mode 100644 index 0000000000..f7e0b2ad3f --- /dev/null +++ b/rust/obs-c-oracle/src/source_render_route.rs @@ -0,0 +1,136 @@ +//! `oracle_*` build of `libobs/obs-source-render-route.c.h` +//! (oracle/source_render_route.c): the source and filter render routing, +//! renamed. The `bool` results are declared as `u8`, the byte the C +//! returns, so a parity test compares it without assuming it is 0 or 1; +//! the enums as `u32`, their value. + +use std::os::raw::{c_char, c_int}; + +/// Index of each entry in `oracle_source_render_route_value`, in the C's +/// order. +pub const VALUE_NAMES: [&str; 44] = [ + "OBS_SOURCE_TYPE_INPUT", + "OBS_SOURCE_TYPE_FILTER", + "OBS_SOURCE_TYPE_TRANSITION", + "OBS_SOURCE_TYPE_SCENE", + "OBS_SOURCE_VIDEO", + "OBS_SOURCE_AUDIO", + "OBS_SOURCE_ASYNC", + "OBS_SOURCE_CUSTOM_DRAW", + "OBS_SOURCE_CAP_DISABLED", + "OBS_SOURCE_SRGB", + "OBS_NO_DIRECT_RENDERING", + "OBS_ALLOW_DIRECT_RENDERING", + "GS_CS_SRGB", + "GS_CS_SRGB_16F", + "GS_CS_709_EXTENDED", + "GS_CS_709_SCRGB", + "GS_RG16", + "OBS_SOURCE_RENDER_ROUTE_FILTER_READY", + "OBS_SOURCE_RENDER_ROUTE_FILTER_NO_TARGET", + "OBS_SOURCE_RENDER_ROUTE_FILTER_NO_PARENT", + "OBS_SOURCE_RENDER_ROUTE_DRAW_DEFAULT", + "OBS_SOURCE_RENDER_ROUTE_DRAW_FILTERS", + "OBS_SOURCE_RENDER_ROUTE_DRAW_MAIN", + "OBS_SOURCE_RENDER_ROUTE_DRAW_TARGET", + "OBS_SOURCE_RENDER_ROUTE_DRAW_DEINTERLACE", + "OBS_SOURCE_RENDER_ROUTE_DRAW_ASYNC", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_RENDERING_FILTER", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_VIDEO_RENDER", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_FILTER_TARGET", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_DEINTERLACING", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_VIDEO_RENDER", + "OBS_SOURCE_RENDER_ROUTE_DRAW_READ_TARGET_DEINTERLACING", + "OBS_SOURCE_RENDER_ROUTE_TECH_END_BYPASS", + "OBS_SOURCE_RENDER_ROUTE_TECH_END_TEXTURE", + "sizeof(enum obs_source_type)", + "sizeof(enum obs_allow_direct_render)", + "sizeof(enum gs_color_space)", + "sizeof(enum gs_color_format)", + "sizeof(enum obs_source_render_route_filter)", + "sizeof(enum obs_source_render_route_draw)", + "sizeof(enum obs_source_render_route_tech_end)", + "sizeof(int)", + "sizeof(bool)", + "sizeof(uint32_t)", +]; + +unsafe extern "C" { + /// `obs_source_render_route_filter()`. + pub fn oracle_obs_source_render_route_filter(has_target: bool, has_parent: bool) -> u32; + /// `obs_source_render_route_video_reads_flags()`. + pub fn oracle_obs_source_render_route_video_reads_flags(source_type: c_int) -> u8; + /// `obs_source_render_route_video_none()`. + pub fn oracle_obs_source_render_route_video_none(source_type: c_int, flags: u32) -> u8; + /// `obs_source_render_route_async_reads_flags()`. + pub fn oracle_obs_source_render_route_async_reads_flags(source_type: c_int) -> u8; + /// `obs_source_render_route_async_reads_rendering_filter()`. + pub fn oracle_obs_source_render_route_async_reads_rendering_filter( + source_type: c_int, + flags: u32, + ) -> u8; + /// `obs_source_render_route_async_update()`. + pub fn oracle_obs_source_render_route_async_update( + source_type: c_int, + flags: u32, + rendering_filter: bool, + ) -> u8; + /// `obs_source_render_route_reads_enabled()`. + pub fn oracle_obs_source_render_route_reads_enabled(has_data: bool) -> u8; + /// `obs_source_render_route_inactive()`. + pub fn oracle_obs_source_render_route_inactive(has_data: bool, enabled: bool) -> u8; + /// `obs_source_render_route_draw()`. + pub fn oracle_obs_source_render_route_draw(has_filters: bool) -> u32; + /// `obs_source_render_route_draw_next()`. + pub fn oracle_obs_source_render_route_draw_next(step: u32, value: bool) -> u32; + /// `obs_source_render_route_main_custom_draw()`. + pub fn oracle_obs_source_render_route_main_custom_draw(flags: u32) -> u8; + /// `obs_source_render_route_main_srgb_aware()`. + pub fn oracle_obs_source_render_route_main_srgb_aware(flags: u32) -> u8; + /// `obs_source_render_route_main_reads_filters()`. + pub fn oracle_obs_source_render_route_main_reads_filters(has_filter_parent: bool) -> u8; + /// `obs_source_render_route_main_default_effect()`. + pub fn oracle_obs_source_render_route_main_default_effect( + flags: u32, + has_filter_parent: bool, + has_filters: bool, + ) -> u8; + /// `obs_source_render_route_bypass_reads_space()`. + pub fn oracle_obs_source_render_route_bypass_reads_space( + target_is_parent: bool, + allow_direct: c_int, + filter_flags: u32, + parent_flags: u32, + ) -> u8; + /// `obs_source_render_route_bypass()`. + pub fn oracle_obs_source_render_route_bypass( + target_is_parent: bool, + allow_direct: c_int, + filter_flags: u32, + parent_flags: u32, + space: c_int, + current_space: c_int, + ) -> u8; + /// `obs_source_render_route_size_skip()`. + pub fn oracle_obs_source_render_route_size_skip(cx: c_int, cy: c_int) -> u8; + /// `obs_source_render_route_texrender_stale()`. + pub fn oracle_obs_source_render_route_texrender_stale( + texrender_format: c_int, + format: c_int, + ) -> u8; + /// `obs_source_render_route_filter_render()`. + pub fn oracle_obs_source_render_route_filter_render( + target_is_parent: bool, + parent_flags: u32, + ) -> u32; + /// `obs_source_render_route_tech_end_linear_srgb()`. + pub fn oracle_obs_source_render_route_tech_end_linear_srgb(filter_flags: u32) -> u8; + /// `obs_source_render_route_tech_name()`. + pub fn oracle_obs_source_render_route_tech_name(tech_name: *const c_char) -> *const c_char; + /// `obs_source_render_route_tech_end()`. + pub fn oracle_obs_source_render_route_tech_end(bypass_active: bool) -> u32; + /// `obs_source_render_route_skip()`. + pub fn oracle_obs_source_render_route_skip(target_is_parent: bool, parent_flags: u32) -> u32; + /// The entry named `VALUE_NAMES[which]`; `i64::MIN` out of range. + pub fn oracle_source_render_route_value(which: usize) -> i64; +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 81a9c57336..53be9d9b46 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -19,6 +19,7 @@ mod draw_plan_parity; mod ptm_gate; mod ptm_gate_parity; mod render_route; +mod render_route_parity; mod size_route; mod size_route_parity; mod transition_clock; diff --git a/rust/obs-source/tests/source/render_route_parity.rs b/rust/obs-source/tests/source/render_route_parity.rs new file mode 100644 index 0000000000..8895e20767 --- /dev/null +++ b/rust/obs-source/tests/source/render_route_parity.rs @@ -0,0 +1,887 @@ +//! Tier 3: the `obs_source_render_route_*` shims against the original C +//! (`obs-c-oracle`, `oracle/source_render_route.c`: the unmodified +//! `libobs/obs-source-render-route.c.h` with its functions renamed). +//! +//! Every function takes and returns plain values, so a run compares the +//! bytes of the two results: the C's `bool` as the byte it returns, the +//! enums as their value. `obs_source_render_route_tech_name()` returns a +//! pointer: the given one must come back itself on both sides, and NULL +//! must give `"Draw"` on both (the two literals live at different +//! addresses, which `gs_effect_get_technique()` never compares). +//! +//! Runs: +//! - the enum values, flags and type sizes against the C headers; +//! - types, direct render choices, spaces, formats and sizes over every +//! value from -70000 to 70000, every 65521st `i32` over the whole range +//! and the ends of `i32`; +//! - output flags: all 65,536 low 16-bit patterns, each with the high bits +//! clear and set, for every rule that reads flags; +//! - can_bypass(): every wiring, 14 direct render choices, every pair of +//! 65 flag patterns and every pair of 12 spaces (17,035,200 inputs), then +//! every parent flag pattern above for four filter flags; +//! - every draw step value from 0 to 70000 and the ends of `u32`, both ways; +//! - the decisions of `source_render_video()`, +//! `obs_source_process_filter_begin_with_color_space()`, +//! `obs_source_process_filter_tech_end()` and +//! `obs_source_skip_video_filter()` in their order, reads included, one +//! copy on each side, over every combination of their inputs; +//! - proptests (20,000 cases each) over full-range values. +//! +//! None of these functions divides or does arithmetic: there is no input on +//! which the C could trap (MSVC's `_udiv128` or `INT_MIN / -1`), so no input +//! is skipped on any target. +//! +//! No intentional differences beyond the `"Draw"` literal's address. + +use std::ffi::CStr; +use std::os::raw::{c_char, c_int}; + +use obs_c_oracle::source_render_route as oracle; +use obs_source::ffi::source_render_route as shim; +use obs_source::render_route::{ + ALLOW_DIRECT_RENDERING, Draw, FLAG_ASYNC, FLAG_AUDIO, FLAG_CAP_DISABLED, FLAG_CUSTOM_DRAW, + FLAG_SRGB, FLAG_VIDEO, GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, + GS_RG16, NO_DIRECT_RENDERING, TYPE_FILTER, TYPE_INPUT, TYPE_SCENE, TYPE_TRANSITION, +}; +use proptest::prelude::*; + +#[test] +fn values_match_the_c_headers() { + let mut rust: Vec = vec![ + TYPE_INPUT.into(), + TYPE_FILTER.into(), + TYPE_TRANSITION.into(), + TYPE_SCENE.into(), + FLAG_VIDEO.into(), + FLAG_AUDIO.into(), + FLAG_ASYNC.into(), + FLAG_CUSTOM_DRAW.into(), + FLAG_CAP_DISABLED.into(), + FLAG_SRGB.into(), + NO_DIRECT_RENDERING.into(), + ALLOW_DIRECT_RENDERING.into(), + GS_CS_SRGB.into(), + GS_CS_SRGB_16F.into(), + GS_CS_709_EXTENDED.into(), + GS_CS_709_SCRGB.into(), + GS_RG16.into(), + 0, + 1, + 2, + ]; + rust.extend(Draw::ALL.iter().map(|d| i64::from(d.value()))); + rust.extend([0, 1]); + // the four libobs enums, the three route enums, int, bool, uint32_t + rust.extend([4, 4, 4, 4, 4, 4, 4, 4, 1, 4]); + assert_eq!(rust.len(), oracle::VALUE_NAMES.len()); + for (which, (name, value)) in oracle::VALUE_NAMES.iter().zip(rust).enumerate() { + // SAFETY: a pure lookup. + let c = unsafe { oracle::oracle_source_render_route_value(which) }; + assert_eq!(value, c, "{name}"); + } + // SAFETY: a pure lookup, one past the end. + let past = unsafe { oracle::oracle_source_render_route_value(oracle::VALUE_NAMES.len()) }; + assert_eq!(past, i64::MIN, "the list has as many values as names"); +} + +// -------------------------------------------------------------------------- +// One call on each side, compared + +fn b(v: bool) -> u8 { + u8::from(v) +} + +fn check_filter(t: bool, p: bool) { + // SAFETY: plain values. + let c = unsafe { oracle::oracle_obs_source_render_route_filter(t, p) }; + assert_eq!( + shim::obs_source_render_route_filter(t, p), + c, + "filter {t} {p}" + ); +} + +fn check_type(ty: c_int) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_video_reads_flags(ty)), + oracle::oracle_obs_source_render_route_video_reads_flags(ty), + "video_reads_flags {ty}" + ); + assert_eq!( + b(shim::obs_source_render_route_async_reads_flags(ty)), + oracle::oracle_obs_source_render_route_async_reads_flags(ty), + "async_reads_flags {ty}" + ); + } +} + +fn check_type_flags(ty: c_int, flags: u32) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_video_none(ty, flags)), + oracle::oracle_obs_source_render_route_video_none(ty, flags), + "video_none {ty} {flags:#x}" + ); + assert_eq!( + b(shim::obs_source_render_route_async_reads_rendering_filter( + ty, flags + )), + oracle::oracle_obs_source_render_route_async_reads_rendering_filter(ty, flags), + "async_reads_rendering_filter {ty} {flags:#x}" + ); + for rf in [false, true] { + assert_eq!( + b(shim::obs_source_render_route_async_update(ty, flags, rf)), + oracle::oracle_obs_source_render_route_async_update(ty, flags, rf), + "async_update {ty} {flags:#x} {rf}" + ); + } + } +} + +fn check_flags(flags: u32) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_main_custom_draw(flags)), + oracle::oracle_obs_source_render_route_main_custom_draw(flags) + ); + assert_eq!( + b(shim::obs_source_render_route_main_srgb_aware(flags)), + oracle::oracle_obs_source_render_route_main_srgb_aware(flags) + ); + assert_eq!( + b(shim::obs_source_render_route_tech_end_linear_srgb(flags)), + oracle::oracle_obs_source_render_route_tech_end_linear_srgb(flags) + ); + for same in [false, true] { + assert_eq!( + shim::obs_source_render_route_filter_render(same, flags), + oracle::oracle_obs_source_render_route_filter_render(same, flags), + "filter_render {same} {flags:#x}" + ); + assert_eq!( + shim::obs_source_render_route_skip(same, flags), + oracle::oracle_obs_source_render_route_skip(same, flags), + "skip {same} {flags:#x}" + ); + for filters in [false, true] { + assert_eq!( + b(shim::obs_source_render_route_main_default_effect( + flags, same, filters + )), + oracle::oracle_obs_source_render_route_main_default_effect( + flags, same, filters + ), + "main_default_effect {flags:#x} {same} {filters}" + ); + } + } + } +} + +fn check_bypass(same: bool, allow: c_int, ff: u32, pf: u32, space: c_int, current: c_int) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_bypass( + same, allow, ff, pf, space, current + )), + oracle::oracle_obs_source_render_route_bypass(same, allow, ff, pf, space, current), + "bypass {same} {allow} {ff:#x} {pf:#x} {space} {current}" + ); + } +} + +fn check_bypass_reads(same: bool, allow: c_int, ff: u32, pf: u32) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_bypass_reads_space( + same, allow, ff, pf + )), + oracle::oracle_obs_source_render_route_bypass_reads_space(same, allow, ff, pf), + "bypass_reads_space {same} {allow} {ff:#x} {pf:#x}" + ); + } +} + +fn check_ints(x: c_int, y: c_int) { + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_size_skip(x, y)), + oracle::oracle_obs_source_render_route_size_skip(x, y), + "size_skip {x} {y}" + ); + assert_eq!( + b(shim::obs_source_render_route_texrender_stale(x, y)), + oracle::oracle_obs_source_render_route_texrender_stale(x, y), + "texrender_stale {x} {y}" + ); + } +} + +fn check_step(step: u32) { + for v in [false, true] { + // SAFETY: plain values. + let c = unsafe { oracle::oracle_obs_source_render_route_draw_next(step, v) }; + assert_eq!( + shim::obs_source_render_route_draw_next(step, v), + c, + "draw_next {step} {v}" + ); + } +} + +/// Every value from -70000 to 70000, every 65521st `i32` over the whole +/// range and the ends. +fn sweep_ints() -> Vec { + let mut all: Vec = (-70_000..=70_000).collect(); + all.extend((i32::MIN..=i32::MAX).step_by(65_521)); + all.extend((0..8).map(|i| i32::MIN + i)); + all.extend((0..8).map(|i| i32::MAX - i)); + all +} + +/// The low 16-bit patterns, each with the high bits clear and set. +fn every_low_flags() -> impl Iterator { + (0u32..=0xffff).flat_map(|low| [low, low | 0xffff_0000]) +} + +/// Every combination of the six bits the C test combines, then u32::MAX. +fn flag_combos() -> Vec { + let bits = [ + FLAG_VIDEO, + FLAG_ASYNC, + FLAG_CUSTOM_DRAW, + FLAG_SRGB, + FLAG_AUDIO, + FLAG_CAP_DISABLED, + ]; + let mut all: Vec = (0u32..64) + .map(|combo| { + bits.iter() + .enumerate() + .filter(|(bit, _)| combo & (1 << bit) != 0) + .fold(0, |flags, (_, flag)| flags | flag) + }) + .collect(); + all.push(u32::MAX); + all +} + +const TYPES: [c_int; 10] = [0, 1, 2, 3, -1, 4, 5, 99, i32::MIN, i32::MAX]; +const SPACES: [c_int; 12] = [0, 1, 2, 3, -2, -1, 4, 5, 99, -99, i32::MIN, i32::MAX]; +const ALLOW: [c_int; 14] = [ + 0, + 1, + 2, + 3, + -1, + -2, + 99, + 256, + 257, + 0x1_0001, + i32::MIN, + i32::MIN + 1, + i32::MAX, + i32::MAX - 1, +]; + +#[test] +fn bools_every_input() { + for t in [false, true] { + for p in [false, true] { + check_filter(t, p); + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_inactive(t, p)), + oracle::oracle_obs_source_render_route_inactive(t, p) + ); + } + } + // SAFETY: plain values. + unsafe { + assert_eq!( + b(shim::obs_source_render_route_reads_enabled(t)), + oracle::oracle_obs_source_render_route_reads_enabled(t) + ); + assert_eq!( + b(shim::obs_source_render_route_main_reads_filters(t)), + oracle::oracle_obs_source_render_route_main_reads_filters(t) + ); + assert_eq!( + shim::obs_source_render_route_draw(t), + oracle::oracle_obs_source_render_route_draw(t) + ); + assert_eq!( + shim::obs_source_render_route_tech_end(t), + oracle::oracle_obs_source_render_route_tech_end(t) + ); + } + } +} + +#[test] +fn types_sweep() { + for ty in sweep_ints() { + check_type(ty); + for flags in [0, FLAG_VIDEO, FLAG_ASYNC, FLAG_ASYNC | FLAG_VIDEO, u32::MAX] { + check_type_flags(ty, flags); + } + } +} + +#[test] +fn type_and_flag_patterns() { + for flags in every_low_flags() { + for ty in TYPES { + check_type_flags(ty, flags); + } + check_flags(flags); + } +} + +#[test] +fn bypass_every_combination() { + let combos = flag_combos(); + for same in [false, true] { + for allow in ALLOW { + for &ff in &combos { + for &pf in &combos { + check_bypass_reads(same, allow, ff, pf); + for s in SPACES { + for cs in SPACES { + check_bypass(same, allow, ff, pf, s, cs); + } + } + } + } + } + } +} + +#[test] +fn bypass_every_parent_pattern_and_value() { + for pf in every_low_flags() { + for ff in [0, FLAG_SRGB, u32::MAX, u32::MAX ^ FLAG_SRGB] { + check_bypass_reads(true, 1, ff, pf); + check_bypass(true, 1, ff, pf, 0, 0); + check_bypass(true, 1, ff, pf, 2, 3); + } + } + for v in sweep_ints() { + check_bypass_reads(true, v, 0, FLAG_VIDEO); + check_bypass(true, v, 0, FLAG_VIDEO, 0, 0); + check_bypass(true, 1, 0, FLAG_VIDEO, v, v); + check_bypass(true, 1, 0, FLAG_VIDEO, v, 0); + check_bypass(true, 1, 0, FLAG_VIDEO, 2, v); + } +} + +#[test] +fn sizes_and_formats() { + let ints = sweep_ints(); + let few = [0, 1, -1, 2, 22, 23, 1920, i32::MIN, i32::MAX]; + for &x in &ints { + for &y in &few { + check_ints(x, y); + check_ints(y, x); + } + check_ints(x, x); + } + for x in -64..=64 { + for y in -64..=64 { + check_ints(x, y); + } + } +} + +#[test] +fn every_step_value() { + for step in 0u32..=70_000 { + check_step(step); + } + for step in (0..64).map(|i| u32::MAX - i) { + check_step(step); + } + for step in (0..64).map(|i| 0x8000_0000 + i) { + check_step(step); + } +} + +#[test] +fn tech_name_pointers() { + static NAMES: [&CStr; 4] = [c"Draw", c"DrawCustom", c"", c"Draw\u{1}"]; + for name in NAMES { + let p = name.as_ptr(); + // SAFETY: the pointer is passed back, never read. + let c = unsafe { oracle::oracle_obs_source_render_route_tech_name(p) }; + assert_eq!(c, p); + assert_eq!(shim::obs_source_render_route_tech_name(p), p); + } + // A pointer that is not a string is passed back too, unread. + let odd = std::ptr::dangling::(); + // SAFETY: as above. + unsafe { + assert_eq!(oracle::oracle_obs_source_render_route_tech_name(odd), odd); + } + assert_eq!(shim::obs_source_render_route_tech_name(odd), odd); + // SAFETY: NULL gives a static string on each side. + let (c, r) = unsafe { + ( + CStr::from_ptr(oracle::oracle_obs_source_render_route_tech_name( + std::ptr::null(), + )), + CStr::from_ptr(shim::obs_source_render_route_tech_name(std::ptr::null())), + ) + }; + assert_eq!(c, r); + assert_eq!(r, c"Draw"); +} + +// -------------------------------------------------------------------------- +// The decisions of each caller in its order, one copy on each side + +/// The functions one side links. +struct Side { + filter: fn(bool, bool) -> u32, + video_reads_flags: fn(c_int) -> u8, + video_none: fn(c_int, u32) -> u8, + async_reads_flags: fn(c_int) -> u8, + async_reads_rendering_filter: fn(c_int, u32) -> u8, + async_update: fn(c_int, u32, bool) -> u8, + reads_enabled: fn(bool) -> u8, + inactive: fn(bool, bool) -> u8, + draw: fn(bool) -> u32, + draw_next: fn(u32, bool) -> u32, + main_custom_draw: fn(u32) -> u8, + main_srgb_aware: fn(u32) -> u8, + main_reads_filters: fn(bool) -> u8, + main_default_effect: fn(u32, bool, bool) -> u8, + bypass_reads_space: fn(bool, c_int, u32, u32) -> u8, + bypass: fn(bool, c_int, u32, u32, c_int, c_int) -> u8, + size_skip: fn(c_int, c_int) -> u8, + texrender_stale: fn(c_int, c_int) -> u8, + filter_render: fn(bool, u32) -> u32, + tech_end_linear_srgb: fn(u32) -> u8, + tech_end: fn(bool) -> u32, + skip: fn(bool, u32) -> u32, +} + +// Each oracle function wrapped by hand: the arities differ. +fn c_side() -> Side { + // SAFETY (each closure): plain values in and out. + Side { + filter: |t, p| unsafe { oracle::oracle_obs_source_render_route_filter(t, p) }, + video_reads_flags: |t| unsafe { + oracle::oracle_obs_source_render_route_video_reads_flags(t) + }, + video_none: |t, f| unsafe { oracle::oracle_obs_source_render_route_video_none(t, f) }, + async_reads_flags: |t| unsafe { + oracle::oracle_obs_source_render_route_async_reads_flags(t) + }, + async_reads_rendering_filter: |t, f| unsafe { + oracle::oracle_obs_source_render_route_async_reads_rendering_filter(t, f) + }, + async_update: |t, f, r| unsafe { + oracle::oracle_obs_source_render_route_async_update(t, f, r) + }, + reads_enabled: |d| unsafe { oracle::oracle_obs_source_render_route_reads_enabled(d) }, + inactive: |d, e| unsafe { oracle::oracle_obs_source_render_route_inactive(d, e) }, + draw: |f| unsafe { oracle::oracle_obs_source_render_route_draw(f) }, + draw_next: |s, v| unsafe { oracle::oracle_obs_source_render_route_draw_next(s, v) }, + main_custom_draw: |f| unsafe { oracle::oracle_obs_source_render_route_main_custom_draw(f) }, + main_srgb_aware: |f| unsafe { oracle::oracle_obs_source_render_route_main_srgb_aware(f) }, + main_reads_filters: |p| unsafe { + oracle::oracle_obs_source_render_route_main_reads_filters(p) + }, + main_default_effect: |f, p, h| unsafe { + oracle::oracle_obs_source_render_route_main_default_effect(f, p, h) + }, + bypass_reads_space: |s, a, ff, pf| unsafe { + oracle::oracle_obs_source_render_route_bypass_reads_space(s, a, ff, pf) + }, + bypass: |s, a, ff, pf, sp, cs| unsafe { + oracle::oracle_obs_source_render_route_bypass(s, a, ff, pf, sp, cs) + }, + size_skip: |x, y| unsafe { oracle::oracle_obs_source_render_route_size_skip(x, y) }, + texrender_stale: |x, y| unsafe { + oracle::oracle_obs_source_render_route_texrender_stale(x, y) + }, + filter_render: |s, f| unsafe { oracle::oracle_obs_source_render_route_filter_render(s, f) }, + tech_end_linear_srgb: |f| unsafe { + oracle::oracle_obs_source_render_route_tech_end_linear_srgb(f) + }, + tech_end: |b| unsafe { oracle::oracle_obs_source_render_route_tech_end(b) }, + skip: |s, f| unsafe { oracle::oracle_obs_source_render_route_skip(s, f) }, + } +} + +fn rust_side() -> Side { + Side { + filter: |t, p| shim::obs_source_render_route_filter(t, p), + video_reads_flags: |t| b(shim::obs_source_render_route_video_reads_flags(t)), + video_none: |t, f| b(shim::obs_source_render_route_video_none(t, f)), + async_reads_flags: |t| b(shim::obs_source_render_route_async_reads_flags(t)), + async_reads_rendering_filter: |t, f| { + b(shim::obs_source_render_route_async_reads_rendering_filter( + t, f, + )) + }, + async_update: |t, f, r| b(shim::obs_source_render_route_async_update(t, f, r)), + reads_enabled: |d| b(shim::obs_source_render_route_reads_enabled(d)), + inactive: |d, e| b(shim::obs_source_render_route_inactive(d, e)), + draw: |f| shim::obs_source_render_route_draw(f), + draw_next: |s, v| shim::obs_source_render_route_draw_next(s, v), + main_custom_draw: |f| b(shim::obs_source_render_route_main_custom_draw(f)), + main_srgb_aware: |f| b(shim::obs_source_render_route_main_srgb_aware(f)), + main_reads_filters: |p| b(shim::obs_source_render_route_main_reads_filters(p)), + main_default_effect: |f, p, h| { + b(shim::obs_source_render_route_main_default_effect(f, p, h)) + }, + bypass_reads_space: |s, a, ff, pf| { + b(shim::obs_source_render_route_bypass_reads_space( + s, a, ff, pf, + )) + }, + bypass: |s, a, ff, pf, sp, cs| { + b(shim::obs_source_render_route_bypass(s, a, ff, pf, sp, cs)) + }, + size_skip: |x, y| b(shim::obs_source_render_route_size_skip(x, y)), + texrender_stale: |x, y| b(shim::obs_source_render_route_texrender_stale(x, y)), + filter_render: |s, f| shim::obs_source_render_route_filter_render(s, f), + tech_end_linear_srgb: |f| b(shim::obs_source_render_route_tech_end_linear_srgb(f)), + tech_end: |b| shim::obs_source_render_route_tech_end(b), + skip: |s, f| shim::obs_source_render_route_skip(s, f), + } +} + +/// A source as `source_render_video()` and `obs_source_main_render()` see +/// it, and what they read. +#[derive(Clone, Copy, Debug)] +struct Source { + source_type: c_int, + flags: u32, + has_data: bool, + enabled: bool, + rendering_filter: bool, + has_filters: bool, + has_filter_parent: bool, + video_render: bool, + filter_target: bool, + deinterlacing: bool, +} + +/// What C reads, calls and draws, in order: a trace of the decisions. +fn trace_render_video(side: &Side, s: Source) -> Vec { + let mut t = Vec::new(); + let read_ty = s.source_type; + let reads = (side.video_reads_flags)(read_ty); + t.push(u32::from(reads)); + let flags = if reads != 0 { s.flags } else { 0 }; + if (side.video_none)(read_ty, flags) != 0 { + t.push(100); + return t; + } + let reads = (side.async_reads_flags)(s.source_type); + t.push(u32::from(reads)); + let flags = if reads != 0 { s.flags } else { 0 }; + let reads_rf = (side.async_reads_rendering_filter)(s.source_type, flags); + t.push(u32::from(reads_rf)); + let rf = reads_rf != 0 && s.rendering_filter; + t.push(u32::from((side.async_update)(s.source_type, flags, rf))); + let reads_enabled = (side.reads_enabled)(s.has_data); + t.push(u32::from(reads_enabled)); + if (side.inactive)(s.has_data, reads_enabled != 0 && s.enabled) != 0 { + t.push(101); + return t; + } + let mut route = (side.draw)(s.has_filters); + loop { + t.push(route); + let value = match Draw::from_value(route) { + Some(Draw::ReadRenderingFilter) => s.rendering_filter, + Some(Draw::ReadVideoRender) => s.video_render, + Some(Draw::ReadFilterTarget) => s.filter_target, + Some(Draw::ReadDeinterlacing) => s.deinterlacing, + _ => break, + }; + route = (side.draw_next)(route, value); + } + if Draw::from_value(route) == Some(Draw::Main) { + // obs_source_main_render() + t.push(u32::from((side.main_custom_draw)(s.flags))); + t.push(u32::from((side.main_srgb_aware)(s.flags))); + let reads = (side.main_reads_filters)(s.has_filter_parent); + t.push(u32::from(reads)); + t.push(u32::from((side.main_default_effect)( + s.flags, + s.has_filter_parent, + reads != 0 && s.has_filters, + ))); + } + t +} + +#[test] +fn render_video_decisions_every_source() { + let (c, r) = (c_side(), rust_side()); + let mut count = 0u64; + for source_type in TYPES { + for flags in flag_combos() { + for m in 0u32..256 { + let s = Source { + source_type, + flags, + has_data: m & 1 != 0, + enabled: m & 2 != 0, + rendering_filter: m & 4 != 0, + has_filters: m & 8 != 0, + has_filter_parent: m & 16 != 0, + video_render: m & 32 != 0, + filter_target: m & 64 != 0, + deinterlacing: m & 128 != 0, + }; + assert_eq!( + trace_render_video(&c, s), + trace_render_video(&r, s), + "{s:?}" + ); + count += 1; + } + } + } + assert_eq!(count, 10 * 65 * 256); +} + +/// A filter as the begin, tech end and skip steps see it. +#[derive(Clone, Copy, Debug)] +struct Filter { + has_target: bool, + has_parent: bool, + same: bool, + filter_flags: u32, + parent_flags: u32, + allow: c_int, + space: c_int, + current_space: c_int, + cx: c_int, + cy: c_int, + texrender: bool, + texrender_format: c_int, + format: c_int, + bypass_active: bool, + video_render: bool, + deinterlacing: bool, +} + +fn trace_begin(side: &Side, f: Filter) -> Vec { + let mut t = vec![(side.filter)(f.has_target, f.has_parent)]; + if t[0] != 0 { + return t; + } + let reads = (side.bypass_reads_space)(f.same, f.allow, f.filter_flags, f.parent_flags); + t.push(u32::from(reads)); + let current = if reads != 0 { f.current_space } else { 0 }; + let bypass = (side.bypass)( + f.same, + f.allow, + f.filter_flags, + f.parent_flags, + f.space, + current, + ); + t.push(u32::from(bypass)); + if bypass != 0 { + return t; + } + let skip = (side.size_skip)(f.cx, f.cy); + t.push(u32::from(skip)); + if skip != 0 { + t.extend(trace_skip(side, f)); + return t; + } + if f.texrender { + t.push(u32::from((side.texrender_stale)( + f.texrender_format, + f.format, + ))); + } + t.push((side.filter_render)(f.same, f.parent_flags)); + t +} + +fn trace_tech_end(side: &Side, f: Filter) -> Vec { + let mut t = vec![(side.filter)(f.has_target, f.has_parent)]; + if t[0] != 0 { + return t; + } + t.push(u32::from((side.tech_end_linear_srgb)(f.filter_flags))); + t.push((side.tech_end)(f.bypass_active)); + t +} + +fn trace_skip(side: &Side, f: Filter) -> Vec { + let mut t = vec![(side.filter)(f.has_target, f.has_parent)]; + if t[0] != 0 { + return t; + } + let mut route = (side.skip)(f.same, f.parent_flags); + loop { + t.push(route); + let value = match Draw::from_value(route) { + Some(Draw::ReadTargetVideoRender) => f.video_render, + Some(Draw::ReadTargetDeinterlacing) => f.deinterlacing, + _ => break, + }; + route = (side.draw_next)(route, value); + } + t +} + +#[test] +fn filter_decisions_every_filter() { + let (c, r) = (c_side(), rust_side()); + let few_flags = [ + 0, + FLAG_VIDEO, + FLAG_SRGB, + FLAG_ASYNC | FLAG_VIDEO, + FLAG_CUSTOM_DRAW | FLAG_VIDEO, + FLAG_CUSTOM_DRAW | FLAG_ASYNC, + FLAG_SRGB | FLAG_VIDEO, + FLAG_SRGB | FLAG_ASYNC, + u32::MAX, + ]; + let sizes = [0, 1, 1920, -1]; + let mut count = 0u64; + for wiring in 0u32..8 { + for &ff in &few_flags { + for &pf in &few_flags { + for allow in [0, 1, 2] { + for space in [0, 1, 2, 3, 4] { + for current_space in [0, 2, 3, -1] { + for &cx in &sizes { + for &cy in &sizes { + for m in 0u32..32 { + let f = Filter { + has_target: wiring & 1 != 0, + has_parent: wiring & 2 != 0, + same: wiring & 4 != 0, + filter_flags: ff, + parent_flags: pf, + allow, + space, + current_space, + cx, + cy, + texrender: m & 1 != 0, + texrender_format: if m & 2 != 0 { 3 } else { 9 }, + format: 3, + bypass_active: m & 4 != 0, + video_render: m & 8 != 0, + deinterlacing: m & 16 != 0, + }; + assert_eq!(trace_begin(&c, f), trace_begin(&r, f), "{f:?}"); + assert_eq!( + trace_tech_end(&c, f), + trace_tech_end(&r, f), + "{f:?}" + ); + assert_eq!(trace_skip(&c, f), trace_skip(&r, f), "{f:?}"); + count += 1; + } + } + } + } + } + } + } + } + } + assert_eq!(count, 8 * 9 * 9 * 3 * 5 * 4 * 4 * 4 * 32); +} + +// -------------------------------------------------------------------------- +// Proptests + +proptest! { + #![proptest_config(ProptestConfig::with_cases(20_000))] + + #[test] + fn prop_single_calls(ty in any::(), flags in any::(), x in any::(), + y in any::(), step in any::()) { + check_type(ty); + check_type_flags(ty, flags); + check_flags(flags); + check_ints(x, y); + check_step(step); + } + + #[test] + fn prop_bypass(same in any::(), allow in any::(), ff in any::(), + pf in any::(), space in -8i32..8, current in -8i32..8, + wide in any::()) { + check_bypass_reads(same, allow, ff, pf); + check_bypass(same, allow, ff, pf, space, current); + check_bypass(same, allow, ff, pf, wide, wide); + check_bypass(same, 1, ff & !(FLAG_ASYNC | FLAG_CUSTOM_DRAW), + ff & !(FLAG_ASYNC | FLAG_CUSTOM_DRAW), wide, space); + } + + #[test] + fn prop_render_video(source_type in prop_oneof![0i32..6, any::()], flags in any::(), + m in any::()) { + let s = Source { + source_type, + flags, + has_data: m & 1 != 0, + enabled: m & 2 != 0, + rendering_filter: m & 4 != 0, + has_filters: m & 8 != 0, + has_filter_parent: m & 16 != 0, + video_render: m & 32 != 0, + filter_target: m & 64 != 0, + deinterlacing: m & 128 != 0, + }; + prop_assert_eq!(trace_render_video(&c_side(), s), trace_render_video(&rust_side(), s)); + } + + #[test] + fn prop_filter(m in any::(), ff in any::(), pf in any::(), + allow in prop_oneof![0i32..3, any::()], + space in prop_oneof![0i32..4, any::()], + current_space in prop_oneof![0i32..4, any::()], + cx in prop_oneof![0i32..2, any::()], cy in prop_oneof![0i32..2, any::()], + texrender_format in -2i32..24, format in -2i32..24) { + let f = Filter { + has_target: m & 1 != 0, + has_parent: m & 2 != 0, + same: m & 4 != 0, + filter_flags: ff, + parent_flags: pf, + allow, + space, + current_space, + cx, + cy, + texrender: m & 8 != 0, + texrender_format, + format, + bypass_active: m & 16 != 0, + video_render: m & 32 != 0, + deinterlacing: m & 64 != 0, + }; + let (c, r) = (c_side(), rust_side()); + prop_assert_eq!(trace_begin(&c, f), trace_begin(&r, f)); + prop_assert_eq!(trace_tech_end(&c, f), trace_tech_end(&r, f)); + prop_assert_eq!(trace_skip(&c, f), trace_skip(&r, f)); + } +} From a266bdd26a17961e14655d8dd9dcba565f1e14a3 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:55:24 -0400 Subject: [PATCH 6/6] docs: Record the source and filter render routing port The rust/README.md crate table now lists libobs/obs-source-render-route.c.h among the rust/obs-source units, and its port table has a row for it: the 23 functions and what they decide, what stays C (every gs_* call and texrender, the refs, the render calls, get_base_width() and get_base_height(), the debug markers and every field read, in their order and under their conditions), why the decisions are many small calls with _reads_ functions and READ_ steps rather than one plan, the types, choices, spaces and formats as int, what is kept and characterized, the one pointer that crosses (the technique name), the _rust variant of the characterization, the real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source, so it needs no row. port_comparison gets its own bench file, benches/port_comparison/source_render_route.rs, in the sorted modules! list: 1024 source_render_video() decision runs (the checks, the draw walk and obs_source_main_render()'s rules), 1024 filter begin and tech end runs and 1024 skip runs, folded into checksums both sides must match. The fixtures are plain Rust arrays, nothing allocated with bmalloc, so the per-module allocation check passes. On x86_64 Linux (GCC, not pinned, load average 13 to 24; median per 1024 calls, two runs): render_video 47.0 and 31.4 us Rust, 79.6 and 33.6 us C; filter 8.3 and 7.2 us Rust, 9.3 and 12.3 us C; skip 4.3 and 13.9 us Rust, 4.2 and 8.9 us C. All within the noise of that machine: a few ns per source or filter either way. Fixes #2189 --- rust/README.md | 3 +- rust/obs-c-oracle/benches/port_comparison.rs | 3 + .../port_comparison/source_render_route.rs | 330 ++++++++++++++++++ rust/obs-source/Cargo.toml | 2 +- rust/obs-source/src/lib.rs | 5 +- 5 files changed, 340 insertions(+), 3 deletions(-) create mode 100644 rust/obs-c-oracle/benches/port_comparison/source_render_route.rs diff --git a/rust/README.md b/rust/README.md index 926bf044b3..c90b41448e 100644 --- a/rust/README.md +++ b/rust/README.md @@ -22,7 +22,7 @@ are in [docs/rust-port/naming.md](../docs/rust-port/naming.md#repository-layout- | `obs-missing-files/` | Port of `libobs/obs-missing-files.c.h` | | `obs-output/` | Ports of `libobs/obs-output-delay.c.h` (the stream delay), `libobs/obs-output-interleave.c.h` (the encoded packet interleaver), `libobs/obs-output-caption-text.c.h` (the caption text wrap) , `libobs/obs-output-protocols.c.h` (the output protocol-list match), `libobs/obs-encoder-roi.c.h` (the encoder region of interest checks and scaling), `libobs/obs-output-video-checks.c.h` (the output video size, conversion and capture checks), `libobs/obs-encoder-size.c.h` (the encoder size and info choices) and `libobs/obs-service-getters.c.h` (the service capability, connect-info, settings, properties and identity functions, and its lifecycle, references and activation) | | `obs-scene/` | Ports of the units extracted from `libobs/obs-scene.c`: `libobs/obs-scene-bounds.c.h`, `libobs/obs-scene-blend.c.h`, `libobs/obs-scene-coords.c.h`, `libobs/obs-scene-group-transform.c.h`, `libobs/obs-scene-audio-actions.c.h` (the core of the scene item visibility audio ramp), `libobs/obs-scene-extent.c.h` (the content extent of a scene or group), `libobs/obs-scene-crop.c.h` (the scene item crop arithmetic), `libobs/obs-scene-render-plan.c.h` (the per-item render decisions of `render_item` and `render_item_texture`), and `libobs/obs-scene-item-names.c.h` (the scene item save/load names and coordinate keys), and `libobs/obs-scene-audio-window.c.h` (the audio window decisions of `scene_audio_render`) | -| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | +| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-render-route.c.h` (the source and filter render routing), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | | `obs-audio-monitoring/` | Ports of `libobs/audio-monitoring/*` (so far `pulse/pulseaudio-stream-flags.c.h` and `pulse/pulseaudio-monitor-format.c.h`) | | `obs-audio-controls/` | Port of `libobs/obs-audio-controls.c.h` | | `obs-core/` | Ports of the units extracted from `libobs/obs.c.h`, `libobs/obs-module.c.h` and `libobs/obs-audio.c.h`: so far `libobs/obs-video-plan.c.h` (the video output plan), `libobs/obs-context-names.c.h` (the context names), `libobs/obs-source-load.c.h` (the source load migration and value rules), `libobs/obs-module-paths.c.h` (the module search paths), `libobs/obs-module-names.c.h` (module names and files), `libobs/obs-module-locale.c.h` (the module locale files), `libobs/obs-module-source-rules.c.h` (the source registration rules), `libobs/obs-module-type-rules.c.h` (the output, encoder and service registration rules), `libobs/obs-audio-mix.c.h` (the audio tick arithmetic); of `libobs/util/source-profiler.c.h` (per-source tick and render timing), which reads core state; of `libobs/obs-view-api.c.h` (views); and of `libobs/obs-video-copy-plan.c.h` (the output frame copy plan and scale effect choice, from `libobs/obs-video.c.h`); and of `libobs/obs-video-output-step.c.h` (the per-frame video output step decisions, from `libobs/obs-video.c.h`) | @@ -168,6 +168,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-source-deinterlace-pacing.c.h` | `obs-source::deinterlace_pacing` | Swapped in hybrid `libobs` (group `libobs.ported-source-deinterlace-pacing`). The frame pacing of double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x), from `deinterlace_get_closest_frames()` and `ready_deinterlace_frames()`, as six functions on plain integers: `obs_source_deinterlace_pacing_hold()` (an unbuffered source keeps its frame while the frame's end is ahead of the clock by less than `TS_JUMP_THRESHOLD`), `obs_source_deinterlace_pacing_wait_for_next()` and `_waiting_frame_ts()` (the "don't skip ahead" guard of an unbuffered source with two frames queued), `obs_source_deinterlace_pacing_half_duration()` (half the time from the frame before; with no usable frame before, that is none, a timestamp going backwards, or a gap past twice `TS_JUMP_THRESHOLD`, the half duration before when it is sane, else half the canvas interval, #1965), `obs_source_deinterlace_pacing_buffered_offset()` (a buffered source's offset from the source clock, #395) and `obs_source_deinterlace_pacing_reanchor()` (an unbuffered source's offset, kept unless the frame comes early or misses its tick and its first field, #395). They are declared in `obs-source-deinterlace-pacing.h` without `EXPORT`, so the shims stay hidden. `obs-source-deinterlace.c.h` keeps the `async_unbuffered` and `deinterlace_offset` tests before the hold and the guard, the field and `obs->video` reads and the stores in their order, the `async_frames` queue, the buffered walk, `first_frame()`, the textures and the render. Every sum and difference wraps as the C's `uint64_t` does, and the re-anchor's lateness is `cast_signed()` of the wrapped difference, the C's `(int64_t)`. The crate still depends on no other crate. Kept, characterized, not endorsed: a timestamp going backwards wraps the guard's end and the waiting timestamp; the offsets wrap when the frame's clock is ahead of the canvas, and a lateness of 2^63 or more counts as early; at 29.97 and 59.94 frames a second into a slightly faster canvas the offset re-anchors on every frame. No intentional differences. `test_deinterlace_pacing_steps` compiles the C into itself, and `test_deinterlace_pacing_steps_rust` runs the same test source against `libobs_rust-static`; `test_deinterlace_2x_field_order` sees the swap through the real libobs. Tier 3 compares the result bytes of every function over the special values and full-range proptests, and sequences of 25, 29.97, 50 and 59.94 frames a second into 30 and 60 fps, buffered and unbuffered, through a model of the caller (the queue, the buffered walk and the stores), every call and the state after every tick; `first_frame_cadences` runs the first frame of a new source and the first after a pause at those rates and checks its fields and that every later state is the one of before #1965. | | `libobs/obs-source-draw-plan.c.h` | `obs-source::draw_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-draw-plan`). The source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`, as eight functions on plain values: `obs_source_draw_plan_rotation_x()` and `_y()` (the translate that moves a rotated async frame back into its box: height or width for 270 and 90, both for 180, nothing for any other angle; the rotation is in 0-359 since #2136), `obs_source_draw_plan_async_linear_srgb()` (the graphics flag, or an SDR frame into anything but `GS_CS_SRGB`, or an HDR frame tonemapped into SDR), `obs_source_draw_plan_async_nonlinear_alpha()` (an SDR frame drawn linear whose alpha is not linear), `obs_source_draw_plan_technique()` (the technique for each pair of source and current colour spaces, with or without non-linear alpha; `source_render()`'s conversion uses the same table without it), `obs_source_draw_plan_reads_white_level()` and `_multiplier()` (into or out of scRGB, the level over 80 or 80 over the level, `obs_get_video_sdr_white_level()` read only then, the same for both callers) and `obs_source_draw_plan_convert()` (whether `source_render()` draws through `color_space_texrender`: the two spaces in different classes, SDR, 709_EXTENDED and scRGB). The techniques are `enum obs_source_draw_technique` (`obs-source-draw-technique.h`, `obs-source::draw_technique`), shared with the deinterlace draw decisions; C maps them to names. They are declared in `obs-source-draw-plan.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `gs_*` call in its order and under its conditions (the decisions are separate calls, not one plan, so neither `gs_get_linear_srgb()` nor the white level read is hoisted), `convert_video_space()`, `obs_get_video_sdr_white_level()`, `gs_get_format_from_space()`, the texrenders and the technique names. Colour spaces cross as `int` and the rotation as `long` (`c_long`), so values outside the enum or 0-359 take the C's paths. The multiplier is one `f32` division, the C's, bit for bit (NaN payloads and infinities included), and the size converts to `f32` as `(float)uint32_t` does. Kept, characterized, not endorsed: a white level of 0 gives a multiplier of 0 into scRGB and +inf out of it (-0 gives -inf, a NaN stays a NaN); a rotation outside 0-359 is not moved; an SDR frame into a current space outside the enum is drawn linear with non-linear alpha, with `Draw`. No intentional differences. `test_source_draw_plan_steps` compiles the C into itself, and `test_source_draw_plan_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_rotation_normalize`, `test_filter_color_space`, `test_source_color_space_steps`, `test_color_correction_16f`, `test_pipewire_cursor_hdr` and `test_texture_render` see the swap through the real libobs. Tier 3 compares the result bytes of every function: rotations around 0-359, a stride over all of `c_long` and past 32 bits, sizes around 2^24 and `u32::MAX`, every pair of 649 spaces with every flag, every pair of 12 spaces at 26 special white levels, both callers' decisions in their order, and proptests over any rotation, size, space and `f32` white level bit pattern. | | `libobs/obs-source-ptm-gate.c.h` | `obs-source::ptm_gate` | Swapped in hybrid `libobs` (group `libobs.ported-source-ptm-gate`). The push-to-mute and push-to-talk gate of `source_muted()`, which gives every audio capture callback its muted flag, as four functions on plain values: `obs_source_ptm_gate_stop_time()` (#2058's `push_to_stop_time()`: while a gate's key is held, the moment it stops holding once let go, `os_time` plus the delay in milliseconds, saturated at `UINT64_MAX` where the product or the sum would wrap, #2051; `get_source_volume()` stores its stop times through it too), `obs_source_ptm_gate_held()` (a key let go holds strictly before its stop time), and `obs_source_ptm_gate_mutes()` and `obs_source_ptm_gate_talk_mutes()` (push-to-mute mutes while enabled and active, push-to-talk while enabled and not active). They are declared in `obs-source-ptm-gate.h` without `EXPORT`, so the shims stay hidden. `source_muted()` and `get_source_volume()` stay C with every read and write of the source's fields in the same order: a stop time is stored only while its gate is enabled and its key pressed (the mix thread stores the same two fields), each pressed flag is read again for its active flag (the hotkey thread writes `user_push_to_*_pressed` without atomics), a stop time is read only when its key is not pressed, and `enabled`, `user_muted` and the enabled flags only where the C's short-circuits reached them. `get_source_volume()` goes through the same four for its stop times, holds and gates (#2158), with the mix's pressed flags and `muted`. No pointer crosses. The core's stop time is `os_time.saturating_add(delay_ms.saturating_mul(NS_PER_MS))`, the same as the C's two checks. Kept, characterized, not endorsed: a saturated stop time does not hold at `os_time` `UINT64_MAX` itself; a stop time stored before a gate was turned off still holds when it is turned back on; while held, an earlier packet moves the stop time back; the mix and the capture callbacks share the stop times, so either one's store holds the other open. No intentional differences. `test_source_ptm_gate_steps` compiles the C into itself, and `test_source_ptm_gate_steps_rust` runs the same test source against `libobs_rust-static`; `test_source_audio_ranges` (the keys pressed through the hotkey router, the stop time read back after a packet, the saturation edges) sees the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: the constant, the gates over their four inputs, the stop time and the hold over every pair of 46 special values and full-range proptests, and `source_muted()` copied onto a stand-in source over every combination of its six flags with stop times around the clock and saturated, and a full-range proptest, the result and both stored stop times. | +| `libobs/obs-source-render-route.c.h` | `obs-source::render_route` | Swapped in hybrid `libobs` (group `libobs.ported-source-render-route`). The source and filter render routing of `source_render_video()`, `obs_source_main_render()`, `can_bypass()`, `obs_source_process_filter_begin_with_color_space()`, `obs_source_process_filter_tech_end()` and `obs_source_skip_video_filter()`, as 23 functions on plain values: `obs_source_render_route_filter()` (the target and parent check, no target first, #2177), the "no video", async update and inactive checks of `source_render_video()` with a `_reads_` function for each value the old short circuits read on one side only, the draw as a route over `enum obs_source_render_route_draw` (`_draw()`, `_skip()` and `_filter_render()` start it, `_draw_next()` takes the one value a `READ_` step asks for and returns the next step or the draw: default render, filters, main render, filter target, deinterlace or async video), `obs_source_main_render()`'s custom draw, sRGB-aware and default-effect rules, `can_bypass()` (`_bypass_reads_space()`, then `_bypass()`; the SRGB and space rules of #1493's filter colour space class), the zero size skip, the texrender format check, and the tech end's linear sRGB flag, technique name (`"Draw"` for NULL) and bypass or texture. They are declared in `obs-source-render-route.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `gs_*` call and texrender, the refs, the render calls, `get_base_width()` and `get_base_height()` (it passes the sizes), the debug markers and every field read in its order and under its conditions; that is why the decisions are many small calls and not one plan per function. Types, the direct render choice, colour spaces and formats cross as `int`; `obs-source.c.h` checks the four enums are `int`-sized. A draw step outside the enum comes back unchanged. Kept, characterized, not endorsed: `ASYNC` without `VIDEO` is no video; a direct render choice outside the enum is not direct; a size of 2^31 or more is negative in the C's `int` and does not skip. One pointer crosses: the technique name, returned as given or as a static `"Draw"`, never read. No other intentional differences. `test_source_render_route_steps` compiles the C into itself, and `test_source_render_route_steps_rust` runs the same test source against `libobs_rust-static`; `test_filter_color_space`, `test_filter_skip_removed`, `test_scroll_filter_wide_source`, `test_color_correction_16f`, `test_darken_transparent`, `test_scene_rotated_scale_filter`, `test_slideshow_size` and `test_texture_render` see the swap through the real libobs. Tier 3 compares the result bytes of every function over every boolean, sweeps of types, choices, spaces, formats and sizes over all of `i32`, every 16-bit flag pattern, 17 million bypass inputs, every draw step value, and each caller's decisions in its order (19.9 million filters, 166,400 sources), plus full-range proptests. | | `libobs/obs-source-size.c.h` | `obs-source::size_route` | Swapped in hybrid `libobs` (group `libobs.ported-source-size`). The size query routing of `obs_source_get_width()` and `obs_source_get_height()`, as two functions: `obs_source_size_route()` (where `get_base_width()` and `get_base_height()` take a source's size from: a transition reports `transition_actual_cx`/`cy` while enabled and 0 otherwise, 6839ff7; a source with data and a size callback asks it, f1aa71b, a filter only while enabled, f04ef23; a filter without one asks its target; an active async source reports its frame, `async_width` and `async_height` swapped unless `async_rotation % 180 == 0`, b9d6675, and 0 before its first frame, 506e30d; anything else reports 0) and `obs_source_size_entry()` (where `obs_source_get_width()` starts: a filter at itself, ed8998e; any other source at its first filter if it has filters, 3510e69, else at itself). A filter is a source with a filter parent, 8a1486d, not one of the filter type. They are declared in `obs-source-size.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps `data_valid()`, `filter_mutex` (a filter takes no lock; any other source holds its `filter_mutex` while it reads `filters.num`, asks the start and reads the first filter), the recursion into the target and the first filter, the `get_width` and `get_height` callbacks and the field each route reads. It reads `info.type`, `context.data != NULL`, the axis's callback `!= NULL`, `filter_parent != NULL`, `enabled`, `async_active` and the `long` `async_rotation` into `struct obs_source_size_node` (passed by value, mirrored); one route serves both axes. No source pointer crosses. The rotation is `c_long` (64 bits on Linux and macOS, 32 on Windows) and the swap is `wrapping_rem(180) != 0`, the C's `%` for every value. `obs-source.c.h` checks that the header's filter and transition types match `obs-source.h` and that `enum obs_source_type` is `int`-sized. Kept, characterized, not endorsed: a filter with no parent (detached) asks its callback even while disabled, and without one reports its async frame or 0 instead of asking a target; a source with a callback but no data reports its async frame or 0. No intentional differences. `test_source_size_route_steps` compiles the C into itself, and `test_source_size_route_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_rotation_normalize` (#2136), `test_slideshow_size`, `test_filter_color_space`, `test_scene_item_crop`, `test_scene_rotated_scale_filter` and the transition and scene size tests see the swap through the real libobs. Tier 3 compares the node's layout and values, the route over every combination of the five flags with eight types and 46 rotations, every rotation from -100,000 to 100,000, the entry, and proptests over any node with any `c_long` rotation; a second oracle builds the unchanged C with a 32-bit `long`, as MSVC does, and is compared over the same rows and any `i32` rotation. | | `libobs/obs-source-transition-clock.c.h` | `obs-source::transition_clock` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition-clock`). The clock and fit decisions of the transitions in `obs-source-transition.c.h`, as seven functions on plain values: `obs_source_transition_clock_time()` (`calc_time()`: the manual value in manual mode, 0 up to and at the start, 1 from the end and for a duration of 0, the ratio in between), `obs_source_transition_clock_manual()` (the manual step of `obs_transition_tick()`: hold outside manual mode, snap for a torque of ±0, step otherwise; the C then calls `calc_torquef()`), `obs_source_transition_clock_size()` and `_fold()` (`get_cx()`/`get_cy()` and the size fold of `recalculate_transition_size()`), `obs_source_transition_clock_fit()` (the scale of `recalculate_transition_matrix()`: none for a child side of ±0, max-only, aspect, stretch), `obs_source_transition_clock_mix_spaces()` and `obs_source_transition_clock_min_ts()` (the earliest audio timestamp that is not 0, #130's snapshot read stays C). They are declared in `obs-source-transition-clock.h` without `EXPORT`, so the shims stay hidden. `obs-source-transition.c.h` keeps the locks, the field reads and writes, the child size and timestamp reads, `calc_torquef()`, the offsets passed to `add_alignment()` and their `(int)` casts, `add_alignment()`, the matrix build, `get_sample_time()`, the texrenders and the swap handover (#556). The modes, scale types and colour spaces cross as `int`; `obs-source-transition.c.h` checks the values and that the enums are `int`-sized. The fit writes its scale through two `float *` only where the C does, so a scale type outside the enum writes nothing. The C divides the clock in `long double`, whose width differs by platform (80-bit x87 on x86_64, 64 bits on MSVC and Apple silicon, 128 on aarch64 Linux), and the widths round a quotient within about 2^-54 of an `f32` tie differently, below 2^53 too (a 33.5 s transition: `33554426999 / 33554431999` is `0x3f7ffffd` in x87, `0x3f7ffffe` as a `double`). The port gives the x87 result for every `u64`: an `f64` division unless its quotient lies exactly on an `f32` tie (all but about 1 in 2^29 ratios), else and from 2^53 on an integer division rounded to 64 bits, then 24. The crate depends on no other crate. Kept, characterized, not endorsed: the manual value is not clamped and NaN passes through; any ratio of 1 - 2^-25 or more rounds to 1.0f, so a transition reaches t >= 1 that much early; a NaN target or torque steps to NaN; a transition of size 0 fits every child to a scale of 0 on that side; a scale type outside the enum gives a matrix but no scale (`recalculate_transition_matrix()` then reads a scale it never set; reported, not changed). Intentional difference: a null `scale_x` or `scale_y` is not written (the C writes through it; libobs passes its own `struct vec2`). `test_transition_clock_steps` compiles the C into itself, and `test_transition_clock_steps_rust` runs the same test source against `libobs_rust-static` (libobs linked first, for `calc_torquef()`); `test_transition_swap`, `test_studio_mode_swap`, `test_transition_recursive_destination`, `test_fade_to_color_mix`, `test_transition_audio_rescale` and `test_stinger_media_duration` see the swap through the real libobs. Tier 3 compares every result bit for bit (the fit's two floats from a guard fill, written or not) over every mode and scale type with special values, sweeps of near-tie ratios in 40 binades, every ratio up to a duration of 2^11 ns (2^15 in an ignored release run) and full-range proptests; where the oracle's `long double` is not the x87's it checks the C against a model of its width instead. In the manual tick only, two NaNs from `calc_torquef()` (not part of this port) count as equal. | | `libobs/obs-source-transition-render.c.h` | `obs-source::transition_render` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition-render`). The render and audio step decisions of the transitions in `obs-source-transition.c.h`, as nine functions on plain values, each asked where the C made the decision: `obs_source_transition_render_video_due()` and `_video_stop()` (the stop step of `obs_transition_video_render2()` and `obs_transition_video_render_direct()`: due at `t >= 1` in `f32` while the video transitions, so a NaN `t` never is; then the video ends, and the transition stops unless its audio runs on; the direct render first ends the audio of a transition that is not active, and `obs_source_active()` is still asked only once the step is due; render2 passes `active` as true), `obs_source_transition_render_draw()` (render2 draws both children through the callback with the video transitioning, the textures locked and a callback set; else B while the audio transitions; else A), `obs_source_transition_render_direct_index()` (B only for `OBS_TRANSITION_SOURCE_B` while transitioning), `obs_source_transition_render_audio_stop()` (with a timestamp at `t >= 1` while the audio transitions; without one only once the video has ended, and `calc_time()` is still asked only with one; the audio ends, and the transition stops unless its video runs on), `obs_source_transition_render_audio_mix()` (each child there while the audio transitions, else copy A), `obs_source_transition_render_audio_valid()` and `_audio_place()` (a child not pending and not duplicated; a timestamp of 0 reads nothing more; the start frame `ns_to_audio_frames(rate, ts - min_ts)` with the difference wrapping, `util_mul_div64()`'s formula per host and the `(size_t)` cast, late past `AUDIO_OUTPUT_FRAMES`; `obs_source_get_audio_mix()` is still read between the two checks) and `obs_source_transition_render_offset()` (the scaled child size of `recalculate_transition_matrix()` and the offsets before the `(int)` casts: each product rounded to `f32`, then subtracted). They are declared in `obs-source-transition-render.h` without `EXPORT`, so the shims stay hidden. `obs-source-transition.c.h` keeps the locks, the clock reads, `copy_transition_state()` and the refs, `obs_transition_stop()`, `handle_stop()` and the signals, `obs_source_active()`, the drop of removed children and `transition_calc_min_ts()` with its pending check, the texture trylock, the renders and callbacks, the size check before the callback, `mix_child()` and `get_sample_time()`, the mixer loop and the `memcpy()`, the `(int)` casts, `add_alignment()` and the matrix calls, each in its order and under its condition. The target crosses as `int`; `obs-source-transition.c.h` checks `OBS_TRANSITION_SOURCE_B`, `AUDIO_OUTPUT_FRAMES` and the enum's size at compile time. The crate depends on no other crate (`util_mul_div64()` is written out, wrapping outside MSVC x64 and exact there, as in `obs-scene`). Kept, characterized, not endorsed: a NaN `t` never ends a transition; a busy texture lock or a missing callback draws the old scene (A) during a video-only transition; a start frame of exactly `AUDIO_OUTPUT_FRAMES` mixes 0 frames (the scene's window uses `>=`); the re-read after the audio stop step does not drop removed children; a timestamp below `min_ts` wraps to a late frame, and a `min_ts` near `UINT64_MAX` wraps back to an early one. Intentional difference: a null `pos` is not written (the C writes through it; libobs passes its own `size_t`). `test_transition_render_steps` compiles the C into itself, and `test_transition_render_steps_rust` runs the same test source against `libobs_rust-static` alone; `test_transition_swap`, `test_studio_mode_swap`, `test_transition_scale_type`, `test_transition_recursive_destination`, `test_item_transition_empty_source`, `test_saved_transition`, `test_fade_to_color_mix`, `test_transition_audio_rescale` and `test_group_hide_transition_audio` see the swap through the real libobs. Tier 3 compares every result bit for bit (the frame from a guard fill, written or not), exhaustively over the flags and targets, over every `f32` `t` in an ignored release run, special timestamps and rates up to where `util_mul_div64()` wraps (on MSVC x64, where the C's `_udiv128()` faults for a start frame past 64 bits, which needs a rate past 1e9, those inputs check only the shim's saturation), every frame edge, and full-range proptests; in the offsets only, two NaN operands of one operation need only give a NaN on both sides (#2021). | diff --git a/rust/obs-c-oracle/benches/port_comparison.rs b/rust/obs-c-oracle/benches/port_comparison.rs index 3fac834eb9..02d9bc40e4 100644 --- a/rust/obs-c-oracle/benches/port_comparison.rs +++ b/rust/obs-c-oracle/benches/port_comparison.rs @@ -64,6 +64,8 @@ mod source_load; mod source_profiler; #[path = "port_comparison/source_ptm_gate.rs"] mod source_ptm_gate; +#[path = "port_comparison/source_render_route.rs"] +mod source_render_route; #[path = "port_comparison/source_size.rs"] mod source_size; #[path = "port_comparison/source_transition_clock.rs"] @@ -165,6 +167,7 @@ fn comparisons(criterion: &mut Criterion) { source_load, source_profiler, source_ptm_gate, + source_render_route, source_size, source_transition_clock, source_transition_render, diff --git a/rust/obs-c-oracle/benches/port_comparison/source_render_route.rs b/rust/obs-c-oracle/benches/port_comparison/source_render_route.rs new file mode 100644 index 0000000000..a5063c5cd2 --- /dev/null +++ b/rust/obs-c-oracle/benches/port_comparison/source_render_route.rs @@ -0,0 +1,330 @@ +//! obs-source: the source and filter render routing +//! (`libobs/obs-source-render-route.c.h`), which `source_render_video()` +//! runs for every source and filter drawn, every frame, and each filter's +//! `obs_source_process_filter_begin_with_color_space()`, +//! `obs_source_process_filter_tech_end()` and, when it skips, +//! `obs_source_skip_video_filter()`. Each measurement runs 1024 calls that +//! reach every branch and folds the results into a checksum that both sides +//! must match: +//! - `render_video`: the decisions of one `source_render_video()` in its +//! order (the "no video" check, the async update, the inactive check, the +//! draw walk and, for a main render, `obs_source_main_render()`'s rules), +//! over every type, the flags the rules read and every combination of the +//! source's booleans; +//! - `filter`: one filter's begin (the target and parent check, the bypass +//! with its space read, the zero size skip, the texrender format check and +//! the draw into it) and tech end (the linear sRGB flag and bypass or +//! texture), over every wiring, flag pattern, direct render choice and +//! space pair; +//! - `skip`: a skipped filter's check and draw walk. +//! +//! The fixtures are plain arrays on the Rust heap: nothing is allocated with +//! bmalloc on either side. +//! +//! Measured when it was added (x86_64 Linux, GCC, not pinned to a core, a +//! machine at a load average of 13 to 24; median per 1024 calls, two runs): +//! render_video 47.0 and 31.4 us Rust, 79.6 and 33.6 us C; filter 8.3 and +//! 7.2 us Rust, 9.3 and 12.3 us C; skip 4.3 and 13.9 us Rust, 4.2 and 8.9 us +//! C. Every pair is within the noise of that machine: a few ns per source or +//! filter either way, the calls being a handful of compares each. + +use std::hint::black_box; +use std::os::raw::c_int; + +use criterion::{Criterion, Throughput}; +use obs_c_oracle::source_render_route as c; +use obs_source::ffi::source_render_route as rs; + +const CALLS: usize = 1024; + +/// A small deterministic generator, so both sides see the same calls. +struct Lcg(u64); + +impl Lcg { + fn next(&mut self) -> u64 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + self.0 >> 16 + } + + fn pick(&mut self, from: &[T]) -> T { + let i = usize::try_from(self.next() % from.len() as u64).unwrap(); + from[i] + } +} + +/// The functions one side links, as plain Rust function pointers. +struct Side { + filter: fn(bool, bool) -> u32, + video_reads_flags: fn(c_int) -> bool, + video_none: fn(c_int, u32) -> bool, + async_reads_flags: fn(c_int) -> bool, + async_reads_rendering_filter: fn(c_int, u32) -> bool, + async_update: fn(c_int, u32, bool) -> bool, + reads_enabled: fn(bool) -> bool, + inactive: fn(bool, bool) -> bool, + draw: fn(bool) -> u32, + draw_next: fn(u32, bool) -> u32, + main_custom_draw: fn(u32) -> bool, + main_srgb_aware: fn(u32) -> bool, + main_reads_filters: fn(bool) -> bool, + main_default_effect: fn(u32, bool, bool) -> bool, + bypass_reads_space: fn(bool, c_int, u32, u32) -> bool, + bypass: fn(bool, c_int, u32, u32, c_int, c_int) -> bool, + size_skip: fn(c_int, c_int) -> bool, + texrender_stale: fn(c_int, c_int) -> bool, + filter_render: fn(bool, u32) -> u32, + tech_end_linear_srgb: fn(u32) -> bool, + tech_end: fn(bool) -> u32, + skip: fn(bool, u32) -> u32, +} + +const RUST: Side = Side { + filter: |t, p| rs::obs_source_render_route_filter(t, p), + video_reads_flags: |t| rs::obs_source_render_route_video_reads_flags(t), + video_none: |t, f| rs::obs_source_render_route_video_none(t, f), + async_reads_flags: |t| rs::obs_source_render_route_async_reads_flags(t), + async_reads_rendering_filter: |t, f| { + rs::obs_source_render_route_async_reads_rendering_filter(t, f) + }, + async_update: |t, f, r| rs::obs_source_render_route_async_update(t, f, r), + reads_enabled: |d| rs::obs_source_render_route_reads_enabled(d), + inactive: |d, e| rs::obs_source_render_route_inactive(d, e), + draw: |f| rs::obs_source_render_route_draw(f), + draw_next: |s, v| rs::obs_source_render_route_draw_next(s, v), + main_custom_draw: |f| rs::obs_source_render_route_main_custom_draw(f), + main_srgb_aware: |f| rs::obs_source_render_route_main_srgb_aware(f), + main_reads_filters: |p| rs::obs_source_render_route_main_reads_filters(p), + main_default_effect: |f, p, h| rs::obs_source_render_route_main_default_effect(f, p, h), + bypass_reads_space: |s, a, ff, pf| rs::obs_source_render_route_bypass_reads_space(s, a, ff, pf), + bypass: |s, a, ff, pf, sp, cs| rs::obs_source_render_route_bypass(s, a, ff, pf, sp, cs), + size_skip: |x, y| rs::obs_source_render_route_size_skip(x, y), + texrender_stale: |x, y| rs::obs_source_render_route_texrender_stale(x, y), + filter_render: |s, f| rs::obs_source_render_route_filter_render(s, f), + tech_end_linear_srgb: |f| rs::obs_source_render_route_tech_end_linear_srgb(f), + tech_end: |b| rs::obs_source_render_route_tech_end(b), + skip: |s, f| rs::obs_source_render_route_skip(s, f), +}; + +// SAFETY (every closure below): the oracle functions take and return plain +// values. +const C: Side = Side { + filter: |t, p| unsafe { c::oracle_obs_source_render_route_filter(t, p) }, + video_reads_flags: |t| unsafe { c::oracle_obs_source_render_route_video_reads_flags(t) != 0 }, + video_none: |t, f| unsafe { c::oracle_obs_source_render_route_video_none(t, f) != 0 }, + async_reads_flags: |t| unsafe { c::oracle_obs_source_render_route_async_reads_flags(t) != 0 }, + async_reads_rendering_filter: |t, f| unsafe { + c::oracle_obs_source_render_route_async_reads_rendering_filter(t, f) != 0 + }, + async_update: |t, f, r| unsafe { c::oracle_obs_source_render_route_async_update(t, f, r) != 0 }, + reads_enabled: |d| unsafe { c::oracle_obs_source_render_route_reads_enabled(d) != 0 }, + inactive: |d, e| unsafe { c::oracle_obs_source_render_route_inactive(d, e) != 0 }, + draw: |f| unsafe { c::oracle_obs_source_render_route_draw(f) }, + draw_next: |s, v| unsafe { c::oracle_obs_source_render_route_draw_next(s, v) }, + main_custom_draw: |f| unsafe { c::oracle_obs_source_render_route_main_custom_draw(f) != 0 }, + main_srgb_aware: |f| unsafe { c::oracle_obs_source_render_route_main_srgb_aware(f) != 0 }, + main_reads_filters: |p| unsafe { c::oracle_obs_source_render_route_main_reads_filters(p) != 0 }, + main_default_effect: |f, p, h| unsafe { + c::oracle_obs_source_render_route_main_default_effect(f, p, h) != 0 + }, + bypass_reads_space: |s, a, ff, pf| unsafe { + c::oracle_obs_source_render_route_bypass_reads_space(s, a, ff, pf) != 0 + }, + bypass: |s, a, ff, pf, sp, cs| unsafe { + c::oracle_obs_source_render_route_bypass(s, a, ff, pf, sp, cs) != 0 + }, + size_skip: |x, y| unsafe { c::oracle_obs_source_render_route_size_skip(x, y) != 0 }, + texrender_stale: |x, y| unsafe { c::oracle_obs_source_render_route_texrender_stale(x, y) != 0 }, + filter_render: |s, f| unsafe { c::oracle_obs_source_render_route_filter_render(s, f) }, + tech_end_linear_srgb: |f| unsafe { + c::oracle_obs_source_render_route_tech_end_linear_srgb(f) != 0 + }, + tech_end: |b| unsafe { c::oracle_obs_source_render_route_tech_end(b) }, + skip: |s, f| unsafe { c::oracle_obs_source_render_route_skip(s, f) }, +}; + +/// `OBS_SOURCE_VIDEO`, `_ASYNC`, `_CUSTOM_DRAW`, `_SRGB` and `_AUDIO`. +const FLAGS: [u32; 10] = [ + 1, + 1 | 4, + 1 | 8, + 1 | 1 << 15, + 1 | 4 | 1 << 15, + 2, + 4, + 8 | 1 << 15, + 0, + 1 | 2, +]; +/// The draw steps that read a value, in the C's numbers. +const READ_RENDERING_FILTER: u32 = 6; +const READ_VIDEO_RENDER: u32 = 7; +const READ_FILTER_TARGET: u32 = 8; +const READ_DEINTERLACING: u32 = 9; +const READ_TARGET_VIDEO_RENDER: u32 = 10; +const READ_TARGET_DEINTERLACING: u32 = 11; +const MAIN: u32 = 2; + +#[derive(Clone, Copy)] +struct Call { + source_type: c_int, + flags: u32, + parent_flags: u32, + bits: u32, + allow: c_int, + space: c_int, + current_space: c_int, + cx: c_int, + cy: c_int, + format: c_int, +} + +fn calls() -> Vec { + let mut g = Lcg(0x2189); + (0..CALLS) + .map(|_| Call { + source_type: g.pick(&[0, 0, 0, 1, 1, 2, 3]), + flags: g.pick(&FLAGS), + parent_flags: g.pick(&FLAGS), + bits: u32::try_from(g.next() & 0xffff).unwrap(), + allow: g.pick(&[1, 1, 0]), + space: g.pick(&[0, 0, 1, 2, 3]), + current_space: g.pick(&[0, 0, 2, 3]), + cx: g.pick(&[1920, 1280, 0]), + cy: g.pick(&[1080, 720, 1080, 0]), + format: g.pick(&[3, 3, 9]), + }) + .collect() +} + +fn bit(c: &Call, n: u32) -> bool { + c.bits & (1 << n) != 0 +} + +fn walk(side: &Side, mut route: u32, values: [bool; 6]) -> u32 { + loop { + let value = match route { + READ_RENDERING_FILTER => values[0], + READ_VIDEO_RENDER | READ_TARGET_VIDEO_RENDER => values[1], + READ_FILTER_TARGET => values[2], + READ_DEINTERLACING | READ_TARGET_DEINTERLACING => values[3], + _ => return route, + }; + route = (side.draw_next)(route, value); + } +} + +fn render_video(side: &Side, calls: &[Call]) -> u64 { + let mut sum = 0u64; + for c in calls { + let flags = if (side.video_reads_flags)(c.source_type) { + c.flags + } else { + 0 + }; + if (side.video_none)(c.source_type, flags) { + sum = sum.wrapping_mul(31).wrapping_add(1); + continue; + } + let flags = if (side.async_reads_flags)(c.source_type) { + c.flags + } else { + 0 + }; + let rf = (side.async_reads_rendering_filter)(c.source_type, flags) && bit(c, 0); + let update = (side.async_update)(c.source_type, flags, rf); + let has_data = bit(c, 1) || bit(c, 2); + let enabled = (side.reads_enabled)(has_data) && (bit(c, 3) || bit(c, 4)); + if (side.inactive)(has_data, enabled) { + sum = sum.wrapping_mul(31).wrapping_add(2 + u64::from(update)); + continue; + } + let values = [bit(c, 0), bit(c, 5), bit(c, 6), bit(c, 7), false, false]; + let route = walk(side, (side.draw)(bit(c, 8)), values); + let mut main = 0u64; + if route == MAIN { + let reads = (side.main_reads_filters)(bit(c, 9)); + main = u64::from((side.main_custom_draw)(c.flags)) + | u64::from((side.main_srgb_aware)(c.flags)) << 1 + | u64::from((side.main_default_effect)( + c.flags, + bit(c, 9), + reads && bit(c, 8), + )) << 2; + } + sum = sum + .wrapping_mul(31) + .wrapping_add(u64::from(route) << 8 | main << 4 | u64::from(update)); + } + sum +} + +fn filter(side: &Side, calls: &[Call]) -> u64 { + let mut sum = 0u64; + for c in calls { + let (has_target, has_parent, same) = (!bit(c, 10), !bit(c, 11), bit(c, 12)); + let check = (side.filter)(has_target, has_parent); + if check != 0 { + sum = sum.wrapping_mul(31).wrapping_add(u64::from(check)); + continue; + } + let reads = (side.bypass_reads_space)(same, c.allow, c.flags, c.parent_flags); + let current = if reads { c.current_space } else { 0 }; + let bypass = (side.bypass)(same, c.allow, c.flags, c.parent_flags, c.space, current); + let mut begin = u64::from(bypass); + if !bypass { + if (side.size_skip)(c.cx, c.cy) { + begin |= 2; + } else { + if bit(c, 13) { + begin |= u64::from((side.texrender_stale)(c.format, 3)) << 2; + } + begin |= u64::from((side.filter_render)(same, c.parent_flags)) << 3; + } + } + let end = u64::from((side.tech_end_linear_srgb)(c.flags)) + | u64::from((side.tech_end)(bypass)) << 1; + sum = sum.wrapping_mul(31).wrapping_add(begin << 4 | end); + } + sum +} + +fn skip(side: &Side, calls: &[Call]) -> u64 { + let mut sum = 0u64; + for c in calls { + let check = (side.filter)(!bit(c, 10), !bit(c, 11)); + let route = if check == 0 { + walk( + side, + (side.skip)(bit(c, 12), c.parent_flags), + [false, bit(c, 5), false, bit(c, 7), false, false], + ) + } else { + 100 + check + }; + sum = sum.wrapping_mul(31).wrapping_add(u64::from(route)); + } + sum +} + +pub(crate) fn benchmarks(criterion: &mut Criterion) { + let calls = calls(); + for (name, run) in [ + ("render_video", render_video as fn(&Side, &[Call]) -> u64), + ("filter", filter), + ("skip", skip), + ] { + assert_eq!( + run(&RUST, &calls), + run(&C, &calls), + "benchmark fixture differs: {name}" + ); + let mut group = criterion.benchmark_group(format!("source_render_route/{name}")); + group.throughput(Throughput::Elements(CALLS as u64)); + group.bench_function("rust", |b| b.iter(|| black_box(run(&RUST, &calls)))); + group.bench_function("c", |b| b.iter(|| black_box(run(&C, &calls)))); + group.finish(); + } +} diff --git a/rust/obs-source/Cargo.toml b/rust/obs-source/Cargo.toml index 92bf83fdc1..d2c4b30fc3 100644 --- a/rust/obs-source/Cargo.toml +++ b/rust/obs-source/Cargo.toml @@ -2,7 +2,7 @@ autotests = false name = "obs-source" version = "0.1.0" -description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" +description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-render-route.c.h (the source and filter render routing), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" edition.workspace = true rust-version.workspace = true license.workspace = true diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index a81efc4e11..9c03fd00bd 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -20,6 +20,9 @@ //! multiplier and the colour space conversion); //! `libobs/obs-source-ptm-gate.c.h`, the //! push-to-mute and push-to-talk gate of `source_muted()`; +//! `libobs/obs-source-render-route.c.h`, the source and filter render +//! routing (which draw a source or a filter takes, the filter bypass, the +//! default effect and the linear sRGB flags); //! `libobs/obs-source-size.c.h`, the size query routing of //! `obs_source_get_width()` and `obs_source_get_height()`; //! `libobs/obs-source-transition-clock.c.h`, the clock and fit decisions of @@ -30,7 +33,7 @@ //! //! The safe cores are [`async_pacing`], [`audio_input`], [`audio_timing`], //! [`color_space`], [`conversion_plan`], [`deinterlace_draw`], -//! [`deinterlace_pacing`], [`draw_plan`], [`ptm_gate`], [`size_route`], +//! [`deinterlace_pacing`], [`draw_plan`], [`ptm_gate`], [`render_route`], [`size_route`], //! [`transition_clock`], [`transition_render`] and [`volume_apply`]; [`draw_technique`] holds the //! technique and colour space values [`deinterlace_draw`] and [`draw_plan`] //! share. The C