2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2019-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "gfx-source-texture.hpp"
|
2020-04-25 23:04:04 +00:00
|
|
|
#include "obs/gs/gs-helper.hpp"
|
2022-05-26 00:58:55 +00:00
|
|
|
#include "obs/obs-tools.hpp"
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "warning-enable.hpp"
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::source_texture::~source_texture()
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child && _parent) {
|
2022-05-26 00:58:55 +00:00
|
|
|
obs_source_remove_active_child(_parent.get(), _child.get());
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
streamfx::gfx::source_texture::source_texture(streamfx::obs::source child, streamfx::obs::source parent) : _parent(parent), _child(child)
|
2018-11-07 13:09:03 +00:00
|
|
|
{
|
2022-05-26 00:58:55 +00:00
|
|
|
// Verify that 'child' and 'parent' exist.
|
|
|
|
if (!_child || !_parent) {
|
|
|
|
throw std::invalid_argument("Child or Parent does not exist.");
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2022-05-26 00:58:55 +00:00
|
|
|
// Verify that 'child' does not contain 'parent'.
|
2022-12-02 03:39:06 +00:00
|
|
|
if (::streamfx::obs::tools::source_find_source(child, parent)) {
|
2022-05-26 00:58:55 +00:00
|
|
|
throw std::runtime_error("Child contains Parent");
|
2022-12-02 03:39:06 +00:00
|
|
|
} else if (!obs_source_add_active_child(parent, child)) {
|
2022-05-26 00:58:55 +00:00
|
|
|
throw std::runtime_error("Child contains Parent");
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2022-05-26 00:58:55 +00:00
|
|
|
_rt = std::make_shared<streamfx::obs::gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2018-09-30 22:54:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
obs_source_t* streamfx::gfx::source_texture::get_object()
|
2018-09-30 22:54:18 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child) {
|
2022-05-26 00:58:55 +00:00
|
|
|
return _child.get();
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-09-30 22:54:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
obs_source_t* streamfx::gfx::source_texture::get_parent()
|
2018-09-30 22:54:18 +00:00
|
|
|
{
|
2022-05-26 00:58:55 +00:00
|
|
|
return _parent.get();
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::source_texture::clear()
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child && _parent) {
|
2022-05-26 00:58:55 +00:00
|
|
|
obs_source_remove_active_child(_parent.get(), _child.get());
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
2022-05-26 00:58:55 +00:00
|
|
|
_child = {};
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<streamfx::obs::gs::texture> streamfx::gfx::source_texture::render(std::size_t width, std::size_t height)
|
2018-11-07 13:09:03 +00:00
|
|
|
{
|
2018-03-05 14:17:34 +00:00
|
|
|
if ((width == 0) || (width >= 16384)) {
|
|
|
|
throw std::runtime_error("Width too large or too small.");
|
|
|
|
}
|
|
|
|
if ((height == 0) || (height >= 16384)) {
|
|
|
|
throw std::runtime_error("Height too large or too small.");
|
|
|
|
}
|
2022-05-26 00:58:55 +00:00
|
|
|
if (!_child || !_parent) {
|
2018-11-08 03:25:43 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
if (_child) {
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2023-05-13 12:35:46 +00:00
|
|
|
auto cctr = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_capture, "gfx::source_texture '%s'", obs_source_get_name(_child.get()));
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2020-07-12 16:41:50 +00:00
|
|
|
auto op = _rt->render(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
|
2018-09-28 21:31:21 +00:00
|
|
|
vec4 black;
|
|
|
|
vec4_zero(&black);
|
2020-07-12 16:41:50 +00:00
|
|
|
gs_ortho(0, static_cast<float>(width), 0, static_cast<float_t>(height), 0, 1);
|
2018-03-05 14:17:34 +00:00
|
|
|
gs_clear(GS_CLEAR_COLOR, &black, 0, 0);
|
2022-05-26 00:58:55 +00:00
|
|
|
obs_source_video_render(_child.get());
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
std::shared_ptr<streamfx::obs::gs::texture> tex;
|
2019-08-04 14:20:26 +00:00
|
|
|
_rt->get_texture(tex);
|
2018-03-05 14:17:34 +00:00
|
|
|
return tex;
|
|
|
|
}
|