Skip to content

rust: Port the graphics state stacks (matrix, viewport, blend state) in graphics/graphics.c.h to obs-graphics #2517

Closed

Description

@vpacher

Unit

The state stacks of the graphics context in libobs/graphics/graphics.c.h, into rust/obs-graphics as obs_graphics::state_stacks:

  • Matrix stack: gs_matrix_push, gs_matrix_pop, gs_matrix_identity, gs_matrix_transpose, gs_matrix_set, gs_matrix_get, gs_matrix_mul, gs_matrix_rotquat, gs_matrix_rotaa, gs_matrix_translate, gs_matrix_scale, gs_matrix_rotaa4f, gs_matrix_translate3f, gs_matrix_scale3f (matrix_stack, cur_matrix; the math is obs_graphics::matrix4 already).
  • Viewport stack: gs_viewport_push (saves the device's viewport), gs_viewport_pop (restores it).
  • Blend state stack: gs_blend_state_push, gs_blend_state_pop and gs_reset_blend_state over cur_blend_state and blend_state_stack.

About 200 lines. The device calls stay C (gs_get_viewport, gs_set_viewport, gs_enable_blending, gs_blend_function_separate, gs_blend_op, which keep cur_blend_state up to date), and so do gs_valid and the rest of graphics.c.h. gs_projection_push/pop, gs_ortho, gs_frustum and gs_perspective only forward to the device and are not part of this unit.

Why

The matrix stack is on the render path: every scene item pushes, transforms and pops while it renders, and the D3D11, OpenGL and Metal devices read the top with gs_matrix_get for the view matrix. The math under it is Rust already, so each call is a C function around a Rust one; the port takes the stack itself to Rust. Nobody is in graphics.c now (#2508 prepares the scene, canvas, transition, output and encoder getters).

Phase and position

Phase 6 style (#444): a behaviour-neutral C extraction first, then the port. Crate obs-graphics, next to matrix4, sprite and texture_render. Later units in the same file: the immediate-mode vertex builder (gs_render_start/stop, gs_vertex*, gs_color, gs_texcoord).

Seam

An extraction to libobs/graphics/graphics-state-stacks.c.h behind graphics-state-stacks.h, on the graphics_subsystem fields by pointer (as obs-audio-discard.c takes the source's fields):

  • the matrix stack (DARRAY(struct matrix4) *, size_t *cur): push, pop (it says when the stack has only its last matrix, so graphics.c logs the error as now), and the top for the operations;
  • the viewport stack: push a given rectangle; pop returns the rectangle to restore, or none;
  • the blend state stack: push the current state; pop returns the state to restore, or none; the reset returns which of the two device steps to take (enable blending; the default function and op).

graphics.c.h keeps gs_valid, the device calls and the logging, and calls the extracted steps. The Rust side grows the C darrays with obs_util::ffi::darray, as other ports do.

Existing tests

  • The rendering tests use the stacks on the way: test_texture_render, test_sprite_flip_render, test_scene_rotated_scale_filter, test_scene_duplicate_scale_ref, test_group_empty_bounds, test_darken_transparent, test_color_correction_16f, test_scroll_filter_wide_source, test_filter_skip_removed, test_source_draw_plan_steps. No test checks the stacks directly.
  • New cases through graphics-state-stacks.h, on plain fields: push and pop at depth 1 to many, pop of the last matrix, every matrix operation on the top (identity, transpose, set, get, mul, rotate by quaternion and by axis-angle, translate, scale, the float variants) against matrix4; viewport push and pop, pop of an empty stack; blend state push, pop, pop of an empty stack, and the reset for each field that differs.

Bugs found while scoping, fixed before the port so that the port carries the right behaviour: gs_matrix_scale3f without gs_valid (a crash without a graphics context) and the wrong name in gs_reset_blend_state's log line (#2518, its own PR first); gs_reset_blend_state leaving a non-default blend op (#2519, in this PR as its own RED and GREEN commits on the extracted seam, before the port).

Plan

One PR:

  1. Extraction to graphics-state-stacks.c.h / .h, no change in behaviour (the rendering tests above, on the C reference and the hybrid).
  2. Characterization: the cases above against the C, in a new cmocka test (after bug: libobs/graphics: gs_matrix_scale3f crashes without a graphics context (and gs_reset_blend_state logs the wrong name) #2518).
  3. bug: libobs/graphics: gs_reset_blend_state leaves a non-default blend op when the blend function is the default #2519: RED (the reset with the default function and another op takes the op step) and GREEN (the fix in the C).
  4. RED: the safe core in obs-graphics with todo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: the depth after any push/pop sequence, the top after any operation sequence equals the same matrix4 steps).
  5. GREEN: the core, a thin #[no_mangle] shim for graphics-state-stacks.h, the include moved to a ported group, Tier 3 against the unmodified C in rust/obs-c-oracle/ (random operation sequences on all three stacks: results, the whole stacks and the current blend state), and port_comparison numbers for a push, transform and pop sequence as a scene item renders.
  6. Tier 2: the graphics tests with Rust off and on.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2517

    Plan as in the description: #2518 first (its own PR), then one PR here (extraction, characterization, #2519 RED and GREEN on the seam, port RED, GREEN with Tier 3 and port_comparison, Tier 2).

  2. self-assigned this
    on Oct 26, 2026
  3. moved this from In progress to Done in obs-rust Multiviewon Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions