2017-09-17 19:55:16 +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-texture.hpp"
|
2018-09-27 03:09:38 +00:00
|
|
|
#include <fstream>
|
2017-09-17 19:55:16 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <sys/stat.h>
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "util-math.hpp"
|
2018-09-27 03:09:38 +00:00
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
// OBS
|
|
|
|
#ifdef _MSC_VER
|
2018-09-27 03:09:38 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
2018-04-28 23:03:03 +00:00
|
|
|
#include <obs.h>
|
2018-09-27 03:09:38 +00:00
|
|
|
#include <util/platform.h>
|
2019-01-14 10:23:21 +00:00
|
|
|
#ifdef _MSC_VER
|
2018-09-27 03:09:38 +00:00
|
|
|
#pragma warning(pop)
|
2019-01-14 10:23:21 +00:00
|
|
|
#endif
|
2017-09-17 19:55:16 +00:00
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs::texture::texture(uint32_t width, uint32_t height, gs_color_format format, uint32_t mip_levels,
|
|
|
|
const uint8_t** mip_data, gs::texture::flags texture_flags)
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
if (width == 0)
|
|
|
|
throw std::logic_error("width must be at least 1");
|
|
|
|
if (height == 0)
|
|
|
|
throw std::logic_error("height must be at least 1");
|
|
|
|
if (mip_levels == 0)
|
|
|
|
throw std::logic_error("mip_levels must be at least 1");
|
|
|
|
|
2018-04-28 23:03:03 +00:00
|
|
|
if (mip_levels > 1 || ((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps)) {
|
2018-09-28 12:23:47 +00:00
|
|
|
bool isPOT = util::math::is_power_of_two(width) && util::math::is_power_of_two(height);
|
2017-09-17 19:55:16 +00:00
|
|
|
if (!isPOT)
|
|
|
|
throw std::logic_error("mip mapping requires power of two dimensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
2018-09-27 03:09:38 +00:00
|
|
|
m_texture = gs_texture_create(
|
|
|
|
width, height, format, mip_levels, mip_data,
|
2018-04-28 23:03:03 +00:00
|
|
|
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
|
2018-09-27 03:09:38 +00:00
|
|
|
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
|
2017-09-17 19:55:16 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
|
|
|
|
if (!m_texture)
|
|
|
|
throw std::runtime_error("Failed to create texture.");
|
2018-04-28 23:03:03 +00:00
|
|
|
|
|
|
|
m_textureType = type::Normal;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs::texture::texture(uint32_t width, uint32_t height, uint32_t depth, gs_color_format format, uint32_t mip_levels,
|
|
|
|
const uint8_t** mip_data, gs::texture::flags texture_flags)
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
if (width == 0)
|
|
|
|
throw std::logic_error("width must be at least 1");
|
|
|
|
if (height == 0)
|
|
|
|
throw std::logic_error("height must be at least 1");
|
|
|
|
if (depth == 0)
|
|
|
|
throw std::logic_error("depth must be at least 1");
|
|
|
|
if (mip_levels == 0)
|
|
|
|
throw std::logic_error("mip_levels must be at least 1");
|
|
|
|
|
2018-04-28 23:03:03 +00:00
|
|
|
if (mip_levels > 1 || ((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps)) {
|
2017-09-17 19:55:16 +00:00
|
|
|
bool isPOT = (pow(2, (int64_t)floor(log(width) / log(2))) == width)
|
2018-09-27 03:09:38 +00:00
|
|
|
&& (pow(2, (int64_t)floor(log(height) / log(2))) == height)
|
|
|
|
&& (pow(2, (int64_t)floor(log(depth) / log(2))) == depth);
|
2017-09-17 19:55:16 +00:00
|
|
|
if (!isPOT)
|
|
|
|
throw std::logic_error("mip mapping requires power of two dimensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
2018-09-27 03:09:38 +00:00
|
|
|
m_texture = gs_voltexture_create(
|
|
|
|
width, height, depth, format, mip_levels, mip_data,
|
2018-04-28 23:03:03 +00:00
|
|
|
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
|
2018-09-27 03:09:38 +00:00
|
|
|
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
|
2017-09-17 19:55:16 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
|
|
|
|
if (!m_texture)
|
|
|
|
throw std::runtime_error("Failed to create texture.");
|
2018-04-28 23:03:03 +00:00
|
|
|
|
|
|
|
m_textureType = type::Volume;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs::texture::texture(uint32_t size, gs_color_format format, uint32_t mip_levels, const uint8_t** mip_data,
|
|
|
|
gs::texture::flags texture_flags)
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
if (size == 0)
|
|
|
|
throw std::logic_error("size must be at least 1");
|
|
|
|
if (mip_levels == 0)
|
|
|
|
throw std::logic_error("mip_levels must be at least 1");
|
|
|
|
|
2018-04-28 23:03:03 +00:00
|
|
|
if (mip_levels > 1 || ((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps)) {
|
2017-09-17 19:55:16 +00:00
|
|
|
bool isPOT = (pow(2, (int64_t)floor(log(size) / log(2))) == size);
|
|
|
|
if (!isPOT)
|
|
|
|
throw std::logic_error("mip mapping requires power of two dimensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
2018-09-27 03:09:38 +00:00
|
|
|
m_texture = gs_cubetexture_create(
|
|
|
|
size, format, mip_levels, mip_data,
|
2018-04-28 23:03:03 +00:00
|
|
|
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
|
2018-09-27 03:09:38 +00:00
|
|
|
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
|
2017-09-17 19:55:16 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
|
|
|
|
if (!m_texture)
|
|
|
|
throw std::runtime_error("Failed to create texture.");
|
2018-04-28 23:03:03 +00:00
|
|
|
|
|
|
|
m_textureType = type::Cube;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs::texture::texture(std::string file)
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
struct stat st;
|
|
|
|
if (os_stat(file.c_str(), &st) != 0)
|
|
|
|
throw std::ios_base::failure(file);
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
|
|
|
m_texture = gs_texture_create_from_file(file.c_str());
|
|
|
|
obs_leave_graphics();
|
|
|
|
|
|
|
|
if (!m_texture)
|
|
|
|
throw std::runtime_error("Failed to load texture.");
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs::texture::~texture()
|
|
|
|
{
|
2018-03-05 14:10:19 +00:00
|
|
|
if (m_isOwner && m_texture) {
|
2017-09-17 19:55:16 +00:00
|
|
|
obs_enter_graphics();
|
|
|
|
switch (gs_get_texture_type(m_texture)) {
|
2018-09-27 03:09:38 +00:00
|
|
|
case GS_TEXTURE_2D:
|
|
|
|
gs_texture_destroy(m_texture);
|
|
|
|
break;
|
|
|
|
case GS_TEXTURE_3D:
|
|
|
|
gs_voltexture_destroy(m_texture);
|
|
|
|
break;
|
|
|
|
case GS_TEXTURE_CUBE:
|
|
|
|
gs_cubetexture_destroy(m_texture);
|
|
|
|
break;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
m_texture = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
void gs::texture::load(int unit)
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
obs_enter_graphics();
|
|
|
|
gs_load_texture(m_texture, unit);
|
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
gs_texture_t* gs::texture::get_object()
|
|
|
|
{
|
2017-09-17 19:55:16 +00:00
|
|
|
return m_texture;
|
|
|
|
}
|
2018-04-28 23:03:03 +00:00
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
uint32_t gs::texture::get_width()
|
|
|
|
{
|
2018-04-28 23:03:03 +00:00
|
|
|
switch (m_textureType) {
|
2018-09-27 03:09:38 +00:00
|
|
|
case type::Normal:
|
|
|
|
return gs_texture_get_width(m_texture);
|
|
|
|
case type::Volume:
|
|
|
|
return gs_voltexture_get_width(m_texture);
|
|
|
|
case type::Cube:
|
|
|
|
return gs_cubetexture_get_size(m_texture);
|
2018-04-28 23:03:03 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
uint32_t gs::texture::get_height()
|
|
|
|
{
|
2018-04-28 23:03:03 +00:00
|
|
|
switch (m_textureType) {
|
2018-09-27 03:09:38 +00:00
|
|
|
case type::Normal:
|
|
|
|
return gs_texture_get_height(m_texture);
|
|
|
|
case type::Volume:
|
|
|
|
return gs_voltexture_get_height(m_texture);
|
|
|
|
case type::Cube:
|
|
|
|
return gs_cubetexture_get_size(m_texture);
|
2018-04-28 23:03:03 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:09:38 +00:00
|
|
|
uint32_t gs::texture::get_depth()
|
|
|
|
{
|
2018-04-28 23:03:03 +00:00
|
|
|
switch (m_textureType) {
|
2018-09-27 03:09:38 +00:00
|
|
|
case type::Normal:
|
|
|
|
return 1;
|
|
|
|
case type::Volume:
|
|
|
|
return gs_voltexture_get_depth(m_texture);
|
|
|
|
case type::Cube:
|
|
|
|
return 6;
|
2018-04-28 23:03:03 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-27 03:09:38 +00:00
|
|
|
|
|
|
|
gs::texture::type gs::texture::get_type()
|
|
|
|
{
|
|
|
|
return m_textureType;
|
|
|
|
}
|
2018-09-27 03:11:37 +00:00
|
|
|
|
|
|
|
gs_color_format gs::texture::get_color_format()
|
|
|
|
{
|
|
|
|
return gs_texture_get_color_format(m_texture);
|
|
|
|
}
|