2017-09-17 20:35:48 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "gs-mipmapper.hpp"
|
2019-04-02 22:16:13 +00:00
|
|
|
#include "obs/gs/gs-helper.hpp"
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "plugin.hpp"
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
// OBS
|
|
|
|
#ifdef _MSC_VER
|
2018-09-27 04:09:43 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
2018-09-27 04:09:43 +00:00
|
|
|
#include <graphics/graphics.h>
|
|
|
|
#include <obs-module.h>
|
|
|
|
#include <obs.h>
|
2019-01-14 10:23:21 +00:00
|
|
|
#ifdef _MSC_VER
|
2018-09-27 04:09:43 +00:00
|
|
|
#pragma warning(pop)
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
2019-01-14 10:23:21 +00:00
|
|
|
extern "C" {
|
2019-04-19 07:42:15 +00:00
|
|
|
#include <Windows.h>
|
2017-09-17 20:35:48 +00:00
|
|
|
}
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
// Here be dragons!
|
|
|
|
// This is to add support for mipmap generation which is by default not possible with libobs.
|
|
|
|
// OBS hides a ton of possible things from us, which we'd have to simulate - or just hack around.
|
|
|
|
struct graphics_subsystem {
|
|
|
|
void* module;
|
|
|
|
gs_device_t* device;
|
|
|
|
// No other fields required.
|
|
|
|
};
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
|
|
|
#include <d3d11.h>
|
|
|
|
#include <dxgi.h>
|
2019-03-01 11:00:30 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
|
|
|
#endif
|
2018-09-27 04:09:43 +00:00
|
|
|
#include <util/windows/ComPtr.hpp>
|
2019-03-01 11:00:30 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
// Slaughtered copy of d3d11-subsystem.hpp gs_device. We only need up to device and context, the rest is "unknown" to us.
|
|
|
|
struct gs_d3d11_device {
|
|
|
|
ComPtr<IDXGIFactory1> factory;
|
|
|
|
ComPtr<IDXGIAdapter1> adapter;
|
|
|
|
ComPtr<ID3D11Device> device;
|
|
|
|
ComPtr<ID3D11DeviceContext> context;
|
|
|
|
// No other fields required.
|
|
|
|
};
|
|
|
|
#endif
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
gs::mipmapper::~mipmapper()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
_vb.reset();
|
|
|
|
_rt.reset();
|
|
|
|
_effect.reset();
|
2018-09-27 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gs::mipmapper::mipmapper()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
_vb = std::make_unique<gs::vertex_buffer>(uint32_t(6u), uint8_t(1u));
|
|
|
|
auto v0 = _vb->at(0);
|
2018-09-27 04:09:43 +00:00
|
|
|
v0.position->x = 0;
|
|
|
|
v0.position->y = 0;
|
|
|
|
v0.uv[0]->x = 0;
|
|
|
|
v0.uv[0]->y = 0;
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
auto v1 = _vb->at(1);
|
|
|
|
auto v4 = _vb->at(4);
|
2018-09-27 04:09:43 +00:00
|
|
|
v4.position->x = v1.position->x = 1.0;
|
|
|
|
v4.position->y = v1.position->y = 0;
|
|
|
|
v4.uv[0]->x = v1.uv[0]->x = 1.0;
|
|
|
|
v4.uv[0]->y = v1.uv[0]->y = 0;
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
auto v2 = _vb->at(2);
|
|
|
|
auto v3 = _vb->at(3);
|
2018-09-27 04:09:43 +00:00
|
|
|
v3.position->x = v2.position->x = 0;
|
|
|
|
v3.position->y = v2.position->y = 1.0;
|
|
|
|
v3.uv[0]->x = v2.uv[0]->x = 0;
|
|
|
|
v3.uv[0]->y = v2.uv[0]->y = 1.0;
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
auto v5 = _vb->at(5);
|
2018-09-27 04:09:43 +00:00
|
|
|
v5.position->x = 1.0;
|
|
|
|
v5.position->y = 1.0;
|
|
|
|
v5.uv[0]->x = 1.0;
|
|
|
|
v5.uv[0]->y = 1.0;
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_vb->update();
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2019-08-04 21:18:54 +00:00
|
|
|
char* effect_file = obs_module_file("effects/mipgen.effect");
|
2019-08-04 14:20:26 +00:00
|
|
|
_effect = std::make_unique<gs::effect>(effect_file);
|
2018-09-27 04:09:43 +00:00
|
|
|
bfree(effect_file);
|
2017-09-17 20:35:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 12:24:30 +00:00
|
|
|
void gs::mipmapper::rebuild(std::shared_ptr<gs::texture> source, std::shared_ptr<gs::texture> target,
|
|
|
|
gs::mipmapper::generator generator = gs::mipmapper::generator::Linear,
|
|
|
|
float_t strength = 1.0)
|
2018-09-27 04:09:43 +00:00
|
|
|
{
|
|
|
|
// Here be dragons! You have been warned.
|
|
|
|
|
|
|
|
// Do nothing if there is no texture given.
|
2018-09-28 12:24:30 +00:00
|
|
|
if (!source) {
|
2018-09-27 04:09:43 +00:00
|
|
|
#ifdef _DEBUG
|
2018-09-28 12:24:30 +00:00
|
|
|
assert(!source);
|
2018-09-27 04:09:43 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2018-09-28 12:24:30 +00:00
|
|
|
if (!target) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
assert(!target);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure texture sizes match
|
|
|
|
if ((source->get_width() != target->get_width()) || (source->get_height() != target->get_height())) {
|
|
|
|
throw std::invalid_argument("source and target must have same size");
|
|
|
|
}
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2018-09-28 12:24:30 +00:00
|
|
|
// Ensure texture types match
|
|
|
|
if ((source->get_type() != target->get_type())) {
|
|
|
|
throw std::invalid_argument("source and target must have same type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure texture formats match
|
|
|
|
if ((source->get_color_format() != target->get_color_format())) {
|
|
|
|
throw std::invalid_argument("source and target must have same format");
|
|
|
|
}
|
2018-09-28 21:32:35 +00:00
|
|
|
|
2019-04-02 22:16:13 +00:00
|
|
|
auto gctx = gs::context();
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-28 12:24:30 +00:00
|
|
|
// Copy original texture
|
2018-09-28 21:32:35 +00:00
|
|
|
//gs_copy_texture(target->get_object(), source->get_object());
|
2017-09-17 20:35:48 +00:00
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
// Test if we actually need to recreate the render target for a different format or at all.
|
2019-08-04 14:20:26 +00:00
|
|
|
if ((!_rt) || (source->get_color_format() != _rt->get_color_format())) {
|
|
|
|
_rt = std::make_unique<gs::rendertarget>(source->get_color_format(), GS_ZS_NONE);
|
2018-09-27 04:09:43 +00:00
|
|
|
}
|
2017-09-17 20:35:48 +00:00
|
|
|
|
|
|
|
// Render
|
2019-01-14 22:52:57 +00:00
|
|
|
graphics_t* ctx = gs_get_context();
|
2019-01-14 10:23:21 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
2019-01-14 22:52:57 +00:00
|
|
|
gs_d3d11_device* dev = reinterpret_cast<gs_d3d11_device*>(ctx->device);
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
2019-01-14 22:52:57 +00:00
|
|
|
int device_type = gs_get_device_type();
|
|
|
|
void* sobj = gs_texture_get_obj(source->get_object());
|
|
|
|
void* tobj = gs_texture_get_obj(target->get_object());
|
|
|
|
std::string technique = "Draw";
|
2018-09-27 04:09:43 +00:00
|
|
|
|
|
|
|
switch (generator) {
|
|
|
|
case generator::Point:
|
|
|
|
technique = "Point";
|
|
|
|
break;
|
|
|
|
case generator::Linear:
|
|
|
|
technique = "Linear";
|
|
|
|
break;
|
|
|
|
case generator::Sharpen:
|
|
|
|
technique = "Sharpen";
|
|
|
|
break;
|
|
|
|
case generator::Smoothen:
|
|
|
|
technique = "Smoothen";
|
|
|
|
break;
|
|
|
|
case generator::Bicubic:
|
|
|
|
technique = "Bicubic";
|
|
|
|
break;
|
|
|
|
case generator::Lanczos:
|
|
|
|
technique = "Lanczos";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
gs_load_vertexbuffer(_vb->update());
|
2018-09-27 04:09:43 +00:00
|
|
|
gs_load_indexbuffer(nullptr);
|
|
|
|
|
2018-09-28 12:24:30 +00:00
|
|
|
if (source->get_type() == gs::texture::type::Normal) {
|
|
|
|
size_t texture_width = source->get_width();
|
|
|
|
size_t texture_height = source->get_height();
|
2019-03-01 10:33:19 +00:00
|
|
|
float_t texel_width = 1.0f / texture_width;
|
|
|
|
float_t texel_height = 1.0f / texture_height;
|
2018-09-28 23:06:30 +00:00
|
|
|
size_t mip_levels = 1;
|
2018-09-27 04:21:39 +00:00
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
2018-09-29 14:57:17 +00:00
|
|
|
ID3D11Texture2D* target_t2 = nullptr;
|
|
|
|
ID3D11Texture2D* source_t2 = nullptr;
|
2018-09-27 04:21:39 +00:00
|
|
|
if (device_type == GS_DEVICE_DIRECT3D_11) {
|
|
|
|
// We definitely have a Direct3D11 resource.
|
2018-09-28 12:24:30 +00:00
|
|
|
D3D11_TEXTURE2D_DESC target_t2desc;
|
2018-09-28 23:06:30 +00:00
|
|
|
target_t2 = reinterpret_cast<ID3D11Texture2D*>(tobj);
|
|
|
|
source_t2 = reinterpret_cast<ID3D11Texture2D*>(sobj);
|
2018-09-28 12:24:30 +00:00
|
|
|
target_t2->GetDesc(&target_t2desc);
|
2018-09-28 21:32:35 +00:00
|
|
|
dev->context->CopySubresourceRegion(target_t2, 0, 0, 0, 0, source_t2, 0, nullptr);
|
2018-09-28 23:06:30 +00:00
|
|
|
mip_levels = target_t2desc.MipLevels;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (device_type == GS_DEVICE_OPENGL) {
|
|
|
|
// This is an OpenGL resource.
|
|
|
|
}
|
2018-09-28 21:32:35 +00:00
|
|
|
|
2018-09-28 23:06:30 +00:00
|
|
|
// If we do not have any miplevels, just stop now.
|
|
|
|
if (mip_levels == 1) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2018-09-28 23:06:30 +00:00
|
|
|
for (size_t mip = 1; mip < mip_levels; mip++) {
|
|
|
|
texture_width /= 2;
|
|
|
|
texture_height /= 2;
|
2018-09-29 14:57:17 +00:00
|
|
|
if (texture_width == 0) {
|
|
|
|
texture_width = 1;
|
|
|
|
}
|
|
|
|
if (texture_height == 0) {
|
|
|
|
texture_height = 1;
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:33:19 +00:00
|
|
|
texel_width = 1.0f / texture_width;
|
|
|
|
texel_height = 1.0f / texture_height;
|
2018-09-28 23:06:30 +00:00
|
|
|
|
|
|
|
// Draw mipmap layer
|
|
|
|
try {
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = _rt->render(uint32_t(texture_width), uint32_t(texture_height));
|
2018-09-28 23:06:30 +00:00
|
|
|
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_blend_function_separate(gs_blend_type::GS_BLEND_ONE, gs_blend_type::GS_BLEND_ZERO,
|
|
|
|
gs_blend_type::GS_BLEND_ONE, gs_blend_type::GS_BLEND_ZERO);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_ortho(0, 1, 0, 1, -1, 1);
|
|
|
|
|
|
|
|
vec4 black;
|
|
|
|
vec4_zero(&black);
|
|
|
|
gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH, &black, 0, 0);
|
|
|
|
|
2019-08-04 21:12:30 +00:00
|
|
|
_effect->get_parameter("image")->set_texture(target);
|
|
|
|
_effect->get_parameter("level")->set_int(int32_t(mip - 1));
|
|
|
|
_effect->get_parameter("imageTexel")->set_float2(texel_width, texel_height);
|
|
|
|
_effect->get_parameter("strength")->set_float(strength);
|
2018-09-28 23:06:30 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
while (gs_effect_loop(_effect->get_object(), technique.c_str())) {
|
|
|
|
gs_draw(gs_draw_mode::GS_TRIS, 0, _vb->size());
|
2018-09-27 04:09:43 +00:00
|
|
|
}
|
2018-09-28 23:06:30 +00:00
|
|
|
} catch (...) {
|
|
|
|
P_LOG_ERROR("Failed to render mipmap layer.");
|
|
|
|
}
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2018-09-28 23:06:30 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
|
|
|
if (device_type == GS_DEVICE_DIRECT3D_11) {
|
2018-09-27 04:09:43 +00:00
|
|
|
// Copy
|
|
|
|
ID3D11Texture2D* rt =
|
2019-08-04 14:20:26 +00:00
|
|
|
reinterpret_cast<ID3D11Texture2D*>(gs_texture_get_obj(_rt->get_object()));
|
2019-03-01 10:33:19 +00:00
|
|
|
uint32_t level = uint32_t(D3D11CalcSubresource(UINT(mip), 0, UINT(mip_levels)));
|
2018-09-28 21:32:35 +00:00
|
|
|
dev->context->CopySubresourceRegion(target_t2, level, 0, 0, 0, rt, 0, NULL);
|
2018-09-27 04:09:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-09-27 04:21:39 +00:00
|
|
|
}
|
2018-09-27 04:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gs_load_indexbuffer(nullptr);
|
|
|
|
gs_load_vertexbuffer(nullptr);
|
2017-09-17 20:35:48 +00:00
|
|
|
}
|