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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/test_ported_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PORTED_ON_EVERY_HOST = (
"graphics/graphics-sprite.c.h.o",
"obs-audio-mix.c.h.o",
"obs-canvas-draw.c.h.o",
"obs-context-names.c.h.o",
"obs-encoder-roi.c.h.o",
"obs-encoder-size.c.h.o",
Expand Down
2 changes: 1 addition & 1 deletion docs/rust-port/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ One name per thing, so releases, crates and tags can't be confused (#320).
| `libobs/obs-source*.c.h` | `rust/obs-source` |
| `libobs/audio-monitoring/` | `rust/obs-audio-monitoring` |
| `libobs/obs-audio-controls.c.h` | `rust/obs-audio-controls` |
| `libobs/obs.c.h` and the units extracted from it (`obs-video-plan.c.h`, `obs-context-names.c.h`, `obs-source-load.c.h`), and those extracted from `libobs/obs-module.c.h` (`obs-module-paths.c.h`, `obs-module-names.c.h`, `obs-module-locale.c.h`, `obs-module-source-rules.c.h`, `obs-module-type-rules.c.h`), `libobs/obs-view-api.c.h`, and `obs-video-copy-plan.c.h` and `obs-video-output-step.c.h`, extracted from `libobs/obs-video.c.h` | `rust/obs-core` |
| `libobs/obs.c.h` and the units extracted from it (`obs-video-plan.c.h`, `obs-context-names.c.h`, `obs-source-load.c.h`), and those extracted from `libobs/obs-module.c.h` (`obs-module-paths.c.h`, `obs-module-names.c.h`, `obs-module-locale.c.h`, `obs-module-source-rules.c.h`, `obs-module-type-rules.c.h`), `libobs/obs-view-api.c.h`, and `obs-video-copy-plan.c.h` and `obs-video-output-step.c.h`, extracted from `libobs/obs-video.c.h`, and `obs-canvas-draw.c.h`, extracted from `libobs/obs.c.h` and `libobs/obs-display.c.h` | `rust/obs-core` |
| `plugins/obs-nvenc/` (CUDA/NVENC loader) | `rust/obs-nvenc`, linked through `rust/obs-nvenc-plugin` |
| GPU acceleration status (frontend, #846) | `rust/obs-gpu-status`, linked through `rust/obs-gpu-status-frontend` |
| all libobs ports, as one C ABI drop-in | `rust/libobs-rust` |
Expand Down
11 changes: 11 additions & 0 deletions libobs/build/libobs-ported-canvas-draw.pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Precompiled header (BUILD.md) for the libobs unity group
* libobs.ported-canvas-draw. obs-canvas-draw.c needs its declarations
* (obs-canvas-draw.h, which brings obs-source-draw-technique.h) and the
* gs_color_space enum (obs.h), so the C oracle and the C test can compile it
* on their own. The implementation file includes this header and nothing
* else. */

#pragma once

#include "obs.h"
#include "obs-canvas-draw.h"
3 changes: 3 additions & 0 deletions libobs/build/libobs.pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#pragma once

/* obs-canvas-draw.c */
#include "obs-canvas-draw.h"

/* obs-hotkey.c */
#include <inttypes.h>

Expand Down
6 changes: 6 additions & 0 deletions libobs/build/libobs.ported-canvas-draw+.h.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Unity group (BUILD.md): ported canvas texture draw and display clear
* decisions. */

#include "libobs/build/libobs-ported-canvas-draw.pch.h"

#include "libobs/obs-canvas-draw.c.h"
2 changes: 2 additions & 0 deletions libobs/build/source-sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"obs-audio-mix.h",
"obs-av1.h",
"obs-avc.h",
"obs-canvas-draw.h",
"obs-config.h",
"obs-core-modules-loader.h",
"obs-data.h",
Expand Down Expand Up @@ -151,6 +152,7 @@
"build/libobs.ported-audio-controls+.h.c",
"build/libobs.ported-audio-mix+.h.c",
"build/libobs.ported-callback+.h.c",
"build/libobs.ported-canvas-draw+.h.c",
"build/libobs.ported-context-names+.h.c",
"build/libobs.ported-data+.h.c",
"build/libobs.ported-effect-lifecycle+.h.c",
Expand Down
10 changes: 10 additions & 0 deletions libobs/cmake/rust-exports.map
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@
obs_video_step_ring;
obs_video_step_download;
obs_video_step_output;
/* obs-canvas-draw.c. obs.c and obs-display.c call them inside libobs.
* Not EXPORT. */
obs_canvas_draw_multiplier;
obs_canvas_draw_reads_white_level;
obs_canvas_draw_skip;
obs_canvas_draw_technique;
obs_display_clear_draws_solid;
obs_display_clear_flags;
obs_display_clear_linear;
obs_display_clear_reads_space;
/* util/source-profiler.c. obs.c, obs-video.c and obs-source.c call the
* frame hooks inside libobs. Declared in obs-internal.h, not EXPORT. */
source_profiler_reset_video;
Expand Down
11 changes: 11 additions & 0 deletions libobs/cmake/rust-hidden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ obs_video_step_render
obs_video_step_ring
obs_video_step_download
obs_video_step_output
# obs-canvas-draw.c: obs_render_canvas_texture_internal() in obs.c and
# render_display_begin() in obs-display.c call them inside libobs. Declared in
# obs-canvas-draw.h without EXPORT, so the shims stay hidden the same way.
obs_canvas_draw_multiplier
obs_canvas_draw_reads_white_level
obs_canvas_draw_skip
obs_canvas_draw_technique
obs_display_clear_draws_solid
obs_display_clear_flags
obs_display_clear_linear
obs_display_clear_reads_space
# util/source-profiler.c: obs.c, obs-video.c and obs-source.c call the frame
# hooks inside libobs. Declared in obs-internal.h without EXPORT, so the
# shims stay hidden the same way.
Expand Down
10 changes: 10 additions & 0 deletions libobs/cmake/rust-unexports-macos.txt
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ _obs_video_step_render
_obs_video_step_ring
_obs_video_step_download
_obs_video_step_output
# obs-canvas-draw.c: obs.c and obs-display.c call them inside libobs. Not
# EXPORT.
_obs_canvas_draw_multiplier
_obs_canvas_draw_reads_white_level
_obs_canvas_draw_skip
_obs_canvas_draw_technique
_obs_display_clear_draws_solid
_obs_display_clear_flags
_obs_display_clear_linear
_obs_display_clear_reads_space
# util/source-profiler.c: the frame hooks libobs calls inside itself. Not
# EXPORT.
_source_profiler_reset_video
Expand Down
91 changes: 91 additions & 0 deletions libobs/obs-canvas-draw.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/******************************************************************************
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>

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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "libobs/build/libobs-ported-canvas-draw.pch.h"

/* The canvas texture draw and display clear decisions, moved from
* obs_render_canvas_texture_internal() in obs.c and render_display_begin() in
* obs-display.c: only colour spaces, flags and the white level, no texture,
* effect or display. obs.c and obs-display.c make the calls they decide on.
* Ported to rust/obs-core (canvas_draw): the hybrid libobs drops this file. */

/* obs_render_canvas_texture_internal()'s early return. */
bool obs_canvas_draw_skip(bool has_mix, bool texture_rendered)
{
return !has_mix || !texture_rendered;
}

/* obs_render_canvas_texture_internal()'s technique switch. */
enum obs_source_draw_technique obs_canvas_draw_technique(int source_space, int current_space)
{
enum obs_source_draw_technique technique = OBS_SOURCE_DRAW_TECH_DRAW;

switch (current_space) {
case GS_CS_SRGB:
case GS_CS_SRGB_16F:
if (source_space == GS_CS_709_EXTENDED)
technique = OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP;
break;
case GS_CS_709_SCRGB:
technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY;
break;
case GS_CS_709_EXTENDED:
break;
}

return technique;
}

/* The multiplier switch: only GS_CS_709_SCRGB reads the white level. */
bool obs_canvas_draw_reads_white_level(int current_space)
{
switch (current_space) {
case GS_CS_709_SCRGB:
return true;
}

return false;
}

float obs_canvas_draw_multiplier(float sdr_white_level)
{
return sdr_white_level / 80.f;
}

/* render_display_begin()'s clear colour: the host first, then the space. */
bool obs_display_clear_reads_space(bool always_srgb)
{
return !always_srgb;
}

bool obs_display_clear_linear(int current_space)
{
return current_space == GS_CS_SRGB;
}

uint32_t obs_display_clear_flags(bool use_clear_workaround)
{
uint32_t clear_flags = OBS_DISPLAY_CLEAR_DEPTH | OBS_DISPLAY_CLEAR_STENCIL;
if (!use_clear_workaround)
clear_flags |= OBS_DISPLAY_CLEAR_COLOR;
return clear_flags;
}

bool obs_display_clear_draws_solid(bool use_clear_workaround)
{
return use_clear_workaround;
}
95 changes: 95 additions & 0 deletions libobs/obs-canvas-draw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/******************************************************************************
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>

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 <http://www.gnu.org/licenses/>.
******************************************************************************/

#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "obs-source-draw-technique.h"

/* The canvas texture draw and display clear decisions, on plain values
* (obs-canvas-draw.c):
* - obs_render_canvas_texture_internal() (obs.c): whether a canvas draws at
* all, and the technique and multiplier for each pair of source and
* current colour spaces;
* - render_display_begin() (obs-display.c): how the background colour
* converts to the clear colour, the clear flags, and whether the solid
* colour workaround draws.
*
* obs.c and obs-display.c keep every gs_* call in its order and under the
* same conditions, the effect and param lookups, the blend state,
* obs_get_video_sdr_white_level(), obs_host_display_clear_color_always_srgb(),
* the swapchain, the resize and the viewport: they pass their results in and
* act on what comes back. obs_get_video_sdr_white_level() is called only when
* obs_canvas_draw_reads_white_level() says so, and gs_get_color_space() for
* the clear colour only when obs_display_clear_reads_space() says so, as
* before. None of these functions takes a lock, calls into libobs or keeps
* state.
*
* Colour spaces are enum gs_color_space values, passed as int. A value
* outside the enum takes the paths it took before. The techniques are enum
* obs_source_draw_technique (obs-source-draw-technique.h), which the source
* draw decisions share; these return Draw, DrawMultiply and DrawTonemap only.
*
* Hidden in libobs: not EXPORT. */

/* GS_CLEAR_COLOR, GS_CLEAR_DEPTH and GS_CLEAR_STENCIL (graphics/graphics.h),
* copied so this header needs no graphics headers; obs-display.c checks them. */
#define OBS_DISPLAY_CLEAR_COLOR (1 << 0)
#define OBS_DISPLAY_CLEAR_DEPTH (1 << 1)
#define OBS_DISPLAY_CLEAR_STENCIL (1 << 2)

/* Whether obs_render_canvas_texture_internal() returns before drawing: the
* canvas has no mix (has_mix false), or its mix has not rendered its texture.
* obs.c reads texture_rendered only when there is a mix, and passes false
* otherwise. */
bool obs_canvas_draw_skip(bool has_mix, bool texture_rendered);

/* The technique the canvas texture draws with:
* - DrawTonemap for a GS_CS_709_EXTENDED source into SDR (GS_CS_SRGB,
* GS_CS_SRGB_16F);
* - DrawMultiply into GS_CS_709_SCRGB, from any source;
* - Draw for any other pair, a space outside the enum included. */
enum obs_source_draw_technique obs_canvas_draw_technique(int source_space, int current_space);

/* Whether obs_render_canvas_texture_internal() reads
* obs_get_video_sdr_white_level(): into GS_CS_709_SCRGB only. */
bool obs_canvas_draw_reads_white_level(int current_space);

/* The multiplier into GS_CS_709_SCRGB: sdr_white_level / 80.0f. One float
* division, so a white level of 0 gives 0 and a NaN stays a NaN. Any other
* current space keeps 1.0f, which obs.c sets without asking. */
float obs_canvas_draw_multiplier(float sdr_white_level);

/* Whether render_display_begin() asks gs_get_color_space() for the clear
* colour: only when the host does not always clear in sRGB
* (obs_host_display_clear_color_always_srgb(), asked first). */
bool obs_display_clear_reads_space(bool always_srgb);

/* Whether the background colour converts to a linear clear colour
* (vec4_from_rgba()) rather than sRGB (vec4_from_rgba_srgb()): only into
* GS_CS_SRGB. */
bool obs_display_clear_linear(int current_space);

/* The flags render_display_begin() clears with: depth and stencil always,
* colour too without the clear workaround. */
uint32_t obs_display_clear_flags(bool use_clear_workaround);

/* Whether render_display_begin() draws the clear colour with the solid
* effect after the viewport: only with the clear workaround. */
bool obs_display_clear_draws_solid(bool use_clear_workaround);
15 changes: 9 additions & 6 deletions libobs/obs-display.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ void obs_display_remove_draw_callback(obs_display_t *display, void (*draw)(void
pthread_mutex_unlock(&display->draw_callbacks_mutex);
}

/* The clear flags obs-canvas-draw.h copies. */
_Static_assert(OBS_DISPLAY_CLEAR_COLOR == GS_CLEAR_COLOR, "GS_CLEAR_COLOR");
_Static_assert(OBS_DISPLAY_CLEAR_DEPTH == GS_CLEAR_DEPTH, "GS_CLEAR_DEPTH");
_Static_assert(OBS_DISPLAY_CLEAR_STENCIL == GS_CLEAR_STENCIL, "GS_CLEAR_STENCIL");

static inline bool render_display_begin(struct obs_display *display, uint32_t cx, uint32_t cy, bool update_color_space)
{
struct vec4 clear_color;
Expand Down Expand Up @@ -187,19 +192,17 @@ static inline bool render_display_begin(struct obs_display *display, uint32_t cx
* render pass that returns the clear color for every fragment, the color becomes subject to automatic sRGB
* gamma encoding if the render target uses an sRGB color format.
*/
if (obs_host_display_clear_color_always_srgb())
if (!obs_display_clear_reads_space(obs_host_display_clear_color_always_srgb()))
vec4_from_rgba_srgb(&clear_color, display->background_color);
else if (gs_get_color_space() == GS_CS_SRGB)
else if (obs_display_clear_linear(gs_get_color_space()))
vec4_from_rgba(&clear_color, display->background_color);
else
vec4_from_rgba_srgb(&clear_color, display->background_color);
clear_color.w = 1.0f;

const bool use_clear_workaround = display->use_clear_workaround;

uint32_t clear_flags = GS_CLEAR_DEPTH | GS_CLEAR_STENCIL;
if (!use_clear_workaround)
clear_flags |= GS_CLEAR_COLOR;
const uint32_t clear_flags = obs_display_clear_flags(use_clear_workaround);
gs_clear(clear_flags, &clear_color, 1.0f, 0);

gs_enable_depth_test(false);
Expand All @@ -209,7 +212,7 @@ static inline bool render_display_begin(struct obs_display *display, uint32_t cx
gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
gs_set_viewport(0, 0, cx, cy);

if (use_clear_workaround) {
if (obs_display_clear_draws_solid(use_clear_workaround)) {
gs_effect_t *const solid_effect = obs->video.solid_effect;
gs_effect_set_vec4(gs_effect_get_param_by_name(solid_effect, "color"), &clear_color);
while (gs_effect_loop(solid_effect, "Solid"))
Expand Down
19 changes: 4 additions & 15 deletions libobs/obs.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2221,26 +2221,15 @@ static void obs_render_canvas_texture_internal(obs_canvas_t *canvas, enum gs_ble
gs_eparam_t *param;

video = canvas->mix;
if (!video || !video->texture_rendered)
if (obs_canvas_draw_skip(video != NULL, video && video->texture_rendered))
return;

const enum gs_color_space source_space = video->render_space;
const enum gs_color_space current_space = gs_get_color_space();
const char *tech_name = "Draw";
const char *tech_name = obs_source_draw_technique_name(obs_canvas_draw_technique(source_space, current_space));
float multiplier = 1.f;
switch (current_space) {
case GS_CS_SRGB:
case GS_CS_SRGB_16F:
if (source_space == GS_CS_709_EXTENDED)
tech_name = "DrawTonemap";
break;
case GS_CS_709_SCRGB:
tech_name = "DrawMultiply";
multiplier = obs_get_video_sdr_white_level() / 80.f;
break;
case GS_CS_709_EXTENDED:
break;
}
if (obs_canvas_draw_reads_white_level(current_space))
multiplier = obs_canvas_draw_multiplier(obs_get_video_sdr_white_level());

const bool previous = gs_framebuffer_srgb_enabled();
gs_enable_framebuffer_srgb(true);
Expand Down
Loading
Loading