Repository navigation
Description
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 isobs_graphics::matrix4already). - 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_popandgs_reset_blend_stateovercur_blend_stateandblend_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, sographics.clogs 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) againstmatrix4; 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:
- Extraction to
graphics-state-stacks.c.h/.h, no change in behaviour (the rendering tests above, on the C reference and the hybrid). - 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).
- 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).
- RED: the safe core in
obs-graphicswithtodo!(), 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 samematrix4steps). - GREEN: the core, a thin
#[no_mangle]shim forgraphics-state-stacks.h, the include moved to a ported group, Tier 3 against the unmodified C inrust/obs-c-oracle/(random operation sequences on all three stacks: results, the whole stacks and the current blend state), andport_comparisonnumbers for a push, transform and pop sequence as a scene item renders. - Tier 2: the graphics tests with Rust off and on.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
vpacher commentedon Oct 26, 2026
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).obs-graphics: RED for the graphics state stacks (todo!)
obs-graphics: Port the graphics state stacks
obs-graphics: Port the graphics state stacks (obs-rust#2517, fixes ob…