Skip to content

Commit

Permalink
Ensure texture filtering happens in linear space
Browse files Browse the repository at this point in the history
Adapt existing fix for Windows game capture.
  • Loading branch information
jpark37 authored and jp9000 committed Oct 1, 2021
1 parent 94cbfba commit 9d15e25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
24 changes: 23 additions & 1 deletion browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,31 @@ void BrowserClient::OnAcceleratedPaint(CefRefPtr<CefBrowser>, PaintElementType,

if (shared_handle != last_handle) {
obs_enter_graphics();
gs_texture_destroy(bs->texture);

if (bs->texture) {
if (bs->extra_texture) {
gs_texture_destroy(bs->extra_texture);
bs->extra_texture = nullptr;
}
gs_texture_destroy(bs->texture);
bs->texture = nullptr;
}

bs->texture = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
if (bs->texture) {
const uint32_t cx = gs_texture_get_width(bs->texture);
const uint32_t cy = gs_texture_get_height(bs->texture);
const gs_color_format format =
gs_texture_get_color_format(bs->texture);
const gs_color_format linear_format =
gs_generalize_format(format);
if (linear_format != format) {
bs->extra_texture = gs_texture_create(
cx, cy, linear_format, 1, nullptr, 0);
}
}

obs_leave_graphics();

last_handle = shared_handle;
Expand Down
25 changes: 21 additions & 4 deletions obs-browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,16 @@ void BrowserSource::Render()
gs_effect_t *effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
#endif

bool linear_sample = extra_texture == NULL;
gs_texture_t *draw_texture = texture;
if (!linear_sample &&
!obs_source_get_texcoords_centered(source)) {
gs_copy_texture(extra_texture, texture);
draw_texture = extra_texture;

linear_sample = true;
}

const bool previous = gs_framebuffer_srgb_enabled();
gs_enable_framebuffer_srgb(true);

Expand All @@ -571,12 +581,19 @@ void BrowserSource::Render()

gs_eparam_t *const image =
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, texture);

const uint32_t flip_flag = flip ? GS_FLIP_V : 0;
const char *tech;
if (linear_sample) {
gs_effect_set_texture_srgb(image, draw_texture);
tech = "Draw";
} else {
gs_effect_set_texture(image, draw_texture);
tech = "DrawSrgbDecompress";
}

while (gs_effect_loop(effect, "DrawSrgbDecompress"))
gs_draw_sprite(texture, flip_flag, 0, 0);
const uint32_t flip_flag = flip ? GS_FLIP_V : 0;
while (gs_effect_loop(effect, tech))
gs_draw_sprite(draw_texture, flip_flag, 0, 0);

gs_blend_state_pop();

Expand Down
5 changes: 5 additions & 0 deletions obs-browser-source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct BrowserSource {
std::string url;
std::string css;
gs_texture_t *texture = nullptr;
gs_texture_t *extra_texture = nullptr;
int width = 0;
int height = 0;
bool fps_custom = false;
Expand All @@ -83,6 +84,10 @@ struct BrowserSource {
{
if (texture) {
obs_enter_graphics();
if (extra_texture) {
gs_texture_destroy(extra_texture);
extra_texture = nullptr;
}
gs_texture_destroy(texture);
texture = nullptr;
obs_leave_graphics();
Expand Down

0 comments on commit 9d15e25

Please sign in to comment.