libobs-winrt: Add winrt_capture_get_color_space
Needed for HDR window/monitor capture using WGC.master
parent
d9d4b69a59
commit
302862fe33
|
@ -109,10 +109,21 @@ static bool get_client_box(HWND window, uint32_t width, uint32_t height,
|
||||||
return client_box_available;
|
return client_box_available;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DXGI_FORMAT get_pixel_format(HWND window, HMONITOR monitor)
|
||||||
|
{
|
||||||
|
if (window)
|
||||||
|
monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
|
||||||
|
|
||||||
|
return (monitor && gs_is_monitor_hdr(monitor))
|
||||||
|
? DXGI_FORMAT_R16G16B16A16_FLOAT
|
||||||
|
: DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||||
|
}
|
||||||
|
|
||||||
struct winrt_capture {
|
struct winrt_capture {
|
||||||
HWND window;
|
HWND window;
|
||||||
bool client_area;
|
bool client_area;
|
||||||
HMONITOR monitor;
|
HMONITOR monitor;
|
||||||
|
DXGI_FORMAT format;
|
||||||
|
|
||||||
bool capture_cursor;
|
bool capture_cursor;
|
||||||
BOOL cursor_visible;
|
BOOL cursor_visible;
|
||||||
|
@ -151,8 +162,6 @@ struct winrt_capture {
|
||||||
Direct3D11CaptureFramePool const &sender,
|
Direct3D11CaptureFramePool const &sender,
|
||||||
winrt::Windows::Foundation::IInspectable const &)
|
winrt::Windows::Foundation::IInspectable const &)
|
||||||
{
|
{
|
||||||
obs_enter_graphics();
|
|
||||||
|
|
||||||
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame
|
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFrame
|
||||||
frame = sender.TryGetNextFrame();
|
frame = sender.TryGetNextFrame();
|
||||||
const winrt::Windows::Graphics::SizeInt32 frame_content_size =
|
const winrt::Windows::Graphics::SizeInt32 frame_content_size =
|
||||||
|
@ -166,61 +175,78 @@ struct winrt_capture {
|
||||||
D3D11_TEXTURE2D_DESC desc;
|
D3D11_TEXTURE2D_DESC desc;
|
||||||
frame_surface->GetDesc(&desc);
|
frame_surface->GetDesc(&desc);
|
||||||
|
|
||||||
if (!client_area || get_client_box(window, desc.Width,
|
obs_enter_graphics();
|
||||||
desc.Height, &client_box)) {
|
|
||||||
if (client_area) {
|
|
||||||
texture_width =
|
|
||||||
client_box.right - client_box.left;
|
|
||||||
texture_height =
|
|
||||||
client_box.bottom - client_box.top;
|
|
||||||
} else {
|
|
||||||
texture_width = desc.Width;
|
|
||||||
texture_height = desc.Height;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (texture) {
|
if (desc.Format == get_pixel_format(window, monitor)) {
|
||||||
if (texture_width !=
|
if (!client_area ||
|
||||||
gs_texture_get_width(texture) ||
|
get_client_box(window, desc.Width, desc.Height,
|
||||||
texture_height !=
|
&client_box)) {
|
||||||
gs_texture_get_height(texture)) {
|
if (client_area) {
|
||||||
gs_texture_destroy(texture);
|
texture_width = client_box.right -
|
||||||
texture = nullptr;
|
client_box.left;
|
||||||
|
texture_height = client_box.bottom -
|
||||||
|
client_box.top;
|
||||||
|
} else {
|
||||||
|
texture_width = desc.Width;
|
||||||
|
texture_height = desc.Height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (texture) {
|
||||||
|
if (texture_width !=
|
||||||
|
gs_texture_get_width(
|
||||||
|
texture) ||
|
||||||
|
texture_height !=
|
||||||
|
gs_texture_get_height(
|
||||||
|
texture)) {
|
||||||
|
gs_texture_destroy(texture);
|
||||||
|
texture = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!texture) {
|
||||||
|
const gs_color_format color_format =
|
||||||
|
desc.Format == DXGI_FORMAT_R16G16B16A16_FLOAT
|
||||||
|
? GS_RGBA16F
|
||||||
|
: GS_BGRA;
|
||||||
|
texture = gs_texture_create(
|
||||||
|
texture_width, texture_height,
|
||||||
|
color_format, 1, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client_area) {
|
||||||
|
context->CopySubresourceRegion(
|
||||||
|
(ID3D11Texture2D *)
|
||||||
|
gs_texture_get_obj(
|
||||||
|
texture),
|
||||||
|
0, 0, 0, 0, frame_surface.get(),
|
||||||
|
0, &client_box);
|
||||||
|
} else {
|
||||||
|
/* if they gave an SRV, we could avoid this copy */
|
||||||
|
context->CopyResource(
|
||||||
|
(ID3D11Texture2D *)
|
||||||
|
gs_texture_get_obj(
|
||||||
|
texture),
|
||||||
|
frame_surface.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
texture_written = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!texture) {
|
if (frame_content_size.Width != last_size.Width ||
|
||||||
texture = gs_texture_create(texture_width,
|
frame_content_size.Height != last_size.Height) {
|
||||||
texture_height,
|
format = desc.Format;
|
||||||
GS_BGRA, 1, NULL,
|
frame_pool.Recreate(
|
||||||
0);
|
device,
|
||||||
|
static_cast<
|
||||||
|
winrt::Windows::Graphics::DirectX::
|
||||||
|
DirectXPixelFormat>(
|
||||||
|
format),
|
||||||
|
2, frame_content_size);
|
||||||
|
|
||||||
|
last_size = frame_content_size;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (client_area) {
|
active = FALSE;
|
||||||
context->CopySubresourceRegion(
|
|
||||||
(ID3D11Texture2D *)gs_texture_get_obj(
|
|
||||||
texture),
|
|
||||||
0, 0, 0, 0, frame_surface.get(), 0,
|
|
||||||
&client_box);
|
|
||||||
} else {
|
|
||||||
/* if they gave an SRV, we could avoid this copy */
|
|
||||||
context->CopyResource(
|
|
||||||
(ID3D11Texture2D *)gs_texture_get_obj(
|
|
||||||
texture),
|
|
||||||
frame_surface.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
texture_written = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (frame_content_size.Width != last_size.Width ||
|
|
||||||
frame_content_size.Height != last_size.Height) {
|
|
||||||
frame_pool.Recreate(
|
|
||||||
device,
|
|
||||||
winrt::Windows::Graphics::DirectX::
|
|
||||||
DirectXPixelFormat::B8G8R8A8UIntNormalized,
|
|
||||||
2, frame_content_size);
|
|
||||||
|
|
||||||
last_size = frame_content_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
obs_leave_graphics();
|
obs_leave_graphics();
|
||||||
|
@ -358,8 +384,9 @@ static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
|
||||||
frame_pool = winrt::Windows::Graphics::Capture::
|
frame_pool = winrt::Windows::Graphics::Capture::
|
||||||
Direct3D11CaptureFramePool::Create(
|
Direct3D11CaptureFramePool::Create(
|
||||||
device,
|
device,
|
||||||
winrt::Windows::Graphics::DirectX::
|
static_cast<winrt::Windows::Graphics::DirectX::
|
||||||
DirectXPixelFormat::B8G8R8A8UIntNormalized,
|
DirectXPixelFormat>(
|
||||||
|
capture->format),
|
||||||
2, capture->last_size);
|
2, capture->last_size);
|
||||||
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
||||||
frame_pool.CreateCaptureSession(item);
|
frame_pool.CreateCaptureSession(item);
|
||||||
|
@ -434,12 +461,13 @@ try {
|
||||||
device = inspectable.as<winrt::Windows::Graphics::DirectX::
|
device = inspectable.as<winrt::Windows::Graphics::DirectX::
|
||||||
Direct3D11::IDirect3DDevice>();
|
Direct3D11::IDirect3DDevice>();
|
||||||
const winrt::Windows::Graphics::SizeInt32 size = item.Size();
|
const winrt::Windows::Graphics::SizeInt32 size = item.Size();
|
||||||
|
const DXGI_FORMAT format = get_pixel_format(window, monitor);
|
||||||
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
|
const winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool
|
||||||
frame_pool = winrt::Windows::Graphics::Capture::
|
frame_pool = winrt::Windows::Graphics::Capture::
|
||||||
Direct3D11CaptureFramePool::Create(
|
Direct3D11CaptureFramePool::Create(
|
||||||
device,
|
device,
|
||||||
winrt::Windows::Graphics::DirectX::
|
static_cast<winrt::Windows::Graphics::DirectX::
|
||||||
DirectXPixelFormat::B8G8R8A8UIntNormalized,
|
DirectXPixelFormat>(format),
|
||||||
2, size);
|
2, size);
|
||||||
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
const winrt::Windows::Graphics::Capture::GraphicsCaptureSession session =
|
||||||
frame_pool.CreateCaptureSession(item);
|
frame_pool.CreateCaptureSession(item);
|
||||||
|
@ -463,6 +491,7 @@ try {
|
||||||
capture->window = window;
|
capture->window = window;
|
||||||
capture->client_area = client_area;
|
capture->client_area = client_area;
|
||||||
capture->monitor = monitor;
|
capture->monitor = monitor;
|
||||||
|
capture->format = format;
|
||||||
capture->capture_cursor = cursor && cursor_toggle_supported;
|
capture->capture_cursor = cursor && cursor_toggle_supported;
|
||||||
capture->cursor_visible = cursor;
|
capture->cursor_visible = cursor;
|
||||||
capture->item = item;
|
capture->item = item;
|
||||||
|
@ -566,33 +595,6 @@ extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_texture(struct winrt_capture *capture)
|
|
||||||
{
|
|
||||||
gs_effect_t *const effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
|
|
||||||
gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
|
|
||||||
gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
|
|
||||||
|
|
||||||
const bool previous = gs_framebuffer_srgb_enabled();
|
|
||||||
gs_enable_framebuffer_srgb(true);
|
|
||||||
gs_enable_blending(false);
|
|
||||||
|
|
||||||
gs_texture_t *const texture = capture->texture;
|
|
||||||
gs_effect_set_texture_srgb(image, texture);
|
|
||||||
|
|
||||||
const size_t passes = gs_technique_begin(tech);
|
|
||||||
for (size_t i = 0; i < passes; i++) {
|
|
||||||
if (gs_technique_begin_pass(tech, i)) {
|
|
||||||
gs_draw_sprite(texture, 0, 0, 0);
|
|
||||||
|
|
||||||
gs_technique_end_pass(tech);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gs_technique_end(tech);
|
|
||||||
|
|
||||||
gs_enable_blending(true);
|
|
||||||
gs_enable_framebuffer_srgb(previous);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
|
extern "C" EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture)
|
||||||
{
|
{
|
||||||
return capture->active;
|
return capture->active;
|
||||||
|
@ -626,10 +628,67 @@ extern "C" EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" EXPORT enum gs_color_space
|
||||||
|
winrt_capture_get_color_space(const struct winrt_capture *capture)
|
||||||
|
{
|
||||||
|
return (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT)
|
||||||
|
? GS_CS_709_EXTENDED
|
||||||
|
: GS_CS_SRGB;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture)
|
extern "C" EXPORT void winrt_capture_render(struct winrt_capture *capture)
|
||||||
{
|
{
|
||||||
if (capture->texture_written)
|
if (capture->texture_written) {
|
||||||
draw_texture(capture);
|
const char *tech_name = "Draw";
|
||||||
|
float multiplier = 1.f;
|
||||||
|
const gs_color_space current_space = gs_get_color_space();
|
||||||
|
if (capture->format == DXGI_FORMAT_R16G16B16A16_FLOAT) {
|
||||||
|
switch (current_space) {
|
||||||
|
case GS_CS_SRGB:
|
||||||
|
case GS_CS_SRGB_16F:
|
||||||
|
tech_name = "DrawMultiplyTonemap";
|
||||||
|
multiplier =
|
||||||
|
80.f / obs_get_video_sdr_white_level();
|
||||||
|
break;
|
||||||
|
case GS_CS_709_EXTENDED:
|
||||||
|
tech_name = "DrawMultiply";
|
||||||
|
multiplier =
|
||||||
|
80.f / obs_get_video_sdr_white_level();
|
||||||
|
}
|
||||||
|
} else if (current_space == GS_CS_709_SCRGB) {
|
||||||
|
tech_name = "DrawMultiply";
|
||||||
|
multiplier = obs_get_video_sdr_white_level() / 80.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
gs_effect_t *const effect =
|
||||||
|
obs_get_base_effect(OBS_EFFECT_OPAQUE);
|
||||||
|
gs_technique_t *tech =
|
||||||
|
gs_effect_get_technique(effect, tech_name);
|
||||||
|
|
||||||
|
const bool previous = gs_framebuffer_srgb_enabled();
|
||||||
|
gs_enable_framebuffer_srgb(true);
|
||||||
|
gs_enable_blending(false);
|
||||||
|
|
||||||
|
gs_texture_t *const texture = capture->texture;
|
||||||
|
gs_effect_set_texture_srgb(
|
||||||
|
gs_effect_get_param_by_name(effect, "image"), texture);
|
||||||
|
gs_effect_set_float(gs_effect_get_param_by_name(effect,
|
||||||
|
"multiplier"),
|
||||||
|
multiplier);
|
||||||
|
|
||||||
|
const size_t passes = gs_technique_begin(tech);
|
||||||
|
for (size_t i = 0; i < passes; i++) {
|
||||||
|
if (gs_technique_begin_pass(tech, i)) {
|
||||||
|
gs_draw_sprite(texture, 0, 0, 0);
|
||||||
|
|
||||||
|
gs_technique_end_pass(tech);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gs_technique_end(tech);
|
||||||
|
|
||||||
|
gs_enable_blending(true);
|
||||||
|
gs_enable_framebuffer_srgb(previous);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" EXPORT uint32_t
|
extern "C" EXPORT uint32_t
|
||||||
|
|
|
@ -20,6 +20,8 @@ EXPORT void winrt_capture_free(struct winrt_capture *capture);
|
||||||
EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture);
|
EXPORT BOOL winrt_capture_active(const struct winrt_capture *capture);
|
||||||
EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
|
EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
|
||||||
BOOL visible);
|
BOOL visible);
|
||||||
|
EXPORT enum gs_color_space
|
||||||
|
winrt_capture_get_color_space(const struct winrt_capture *capture);
|
||||||
EXPORT void winrt_capture_render(struct winrt_capture *capture);
|
EXPORT void winrt_capture_render(struct winrt_capture *capture);
|
||||||
EXPORT uint32_t winrt_capture_width(const struct winrt_capture *capture);
|
EXPORT uint32_t winrt_capture_width(const struct winrt_capture *capture);
|
||||||
EXPORT uint32_t winrt_capture_height(const struct winrt_capture *capture);
|
EXPORT uint32_t winrt_capture_height(const struct winrt_capture *capture);
|
||||||
|
|
Loading…
Reference in New Issue