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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gs-mipmapper.h"
|
2018-09-28 21:32:35 +00:00
|
|
|
#include "plugin.h"
|
2018-09-27 04:09:43 +00:00
|
|
|
|
2017-09-17 20:35:48 +00:00
|
|
|
extern "C" {
|
2018-09-27 04:09:43 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
|
|
|
#include <graphics/graphics.h>
|
|
|
|
#include <obs-module.h>
|
|
|
|
#include <obs.h>
|
|
|
|
#pragma warning(pop)
|
|
|
|
#if defined(WIN32) || defined(WIN64)
|
|
|
|
#include <windows.h>
|
|
|
|
#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>
|
|
|
|
#include <util/windows/ComPtr.hpp>
|
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()
|
|
|
|
{
|
2018-09-28 21:32:35 +00:00
|
|
|
vertex_buffer.reset();
|
|
|
|
render_target.reset();
|
2018-09-27 04:09:43 +00:00
|
|
|
effect.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
gs::mipmapper::mipmapper()
|
|
|
|
{
|
2018-09-28 21:32:35 +00:00
|
|
|
vertex_buffer = std::make_unique<gs::vertex_buffer>(6, 1);
|
|
|
|
auto v0 = vertex_buffer->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;
|
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
auto v1 = vertex_buffer->at(1);
|
|
|
|
auto v4 = vertex_buffer->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;
|
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
auto v2 = vertex_buffer->at(2);
|
|
|
|
auto v3 = vertex_buffer->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;
|
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
auto v5 = vertex_buffer->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;
|
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
vertex_buffer->update();
|
2018-09-27 04:09:43 +00:00
|
|
|
|
|
|
|
char* effect_file = obs_module_file("effects/mipgen.effect");
|
|
|
|
effect = std::make_unique<gs::effect>(effect_file);
|
|
|
|
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
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
obs_enter_graphics();
|
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.
|
|
|
|
if ((!render_target) || (source->get_color_format() != render_target->get_color_format())) {
|
|
|
|
render_target = 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
|
2018-09-27 04:09:43 +00:00
|
|
|
graphics_t* ctx = gs_get_context();
|
|
|
|
gs_d3d11_device* dev = reinterpret_cast<gs_d3d11_device*>(ctx->device);
|
|
|
|
int device_type = gs_get_device_type();
|
2018-09-28 12:24:30 +00:00
|
|
|
void* sobj = gs_texture_get_obj(source->get_object());
|
|
|
|
void* tobj = gs_texture_get_obj(target->get_object());
|
2018-09-27 04:09:43 +00:00
|
|
|
std::string technique = "Draw";
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:32:35 +00:00
|
|
|
gs_load_vertexbuffer(vertex_buffer->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();
|
2018-09-27 04:21:39 +00:00
|
|
|
float_t texel_width = 1.0 / texture_width;
|
|
|
|
float_t texel_height = 1.0 / 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) {
|
|
|
|
obs_leave_graphics();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
texel_width = 1.0 / texture_width;
|
|
|
|
texel_height = 1.0 / texture_height;
|
2018-09-28 23:06:30 +00:00
|
|
|
|
|
|
|
// Draw mipmap layer
|
|
|
|
try {
|
|
|
|
auto op = render_target->render(texture_width, texture_height);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
effect->get_parameter("image").set_texture(target);
|
|
|
|
effect->get_parameter("level").set_int(mip - 1);
|
|
|
|
effect->get_parameter("imageTexel").set_float2(texel_width, texel_height);
|
|
|
|
effect->get_parameter("strength").set_float(strength);
|
|
|
|
|
|
|
|
while (gs_effect_loop(effect->get_object(), technique.c_str())) {
|
|
|
|
gs_draw(gs_draw_mode::GS_TRIS, 0, vertex_buffer->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 =
|
2018-09-28 21:32:35 +00:00
|
|
|
reinterpret_cast<ID3D11Texture2D*>(gs_texture_get_obj(render_target->get_object()));
|
2018-09-28 23:06:30 +00:00
|
|
|
uint32_t level = D3D11CalcSubresource(mip, 0, 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
|
|
|
|
2018-09-27 04:09:43 +00:00
|
|
|
obs_leave_graphics();
|
2017-09-17 20:35:48 +00:00
|
|
|
}
|