gs-texture: Formatting, refactoring and cleanup

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2018-09-27 05:09:38 +02:00
parent 4c1e66e27b
commit 52cb7a6d20
2 changed files with 109 additions and 170 deletions

View file

@ -18,22 +18,21 @@
*/
#include "gs-texture.h"
#include <fstream>
#include <stdexcept>
#include <sys/stat.h>
#include <fstream>
extern "C" {
#pragma warning( push )
#pragma warning( disable: 4201 )
#include <util/platform.h>
#pragma warning(push)
#pragma warning(disable : 4201)
#include <obs.h>
#pragma warning( pop )
#include <util/platform.h>
#pragma warning(pop)
}
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) {
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)
{
if (width == 0)
throw std::logic_error("width must be at least 1");
if (height == 0)
@ -45,16 +44,16 @@ gs::texture::texture(uint32_t width, uint32_t height,
if (mip_levels > 1 || ((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps)) {
bool isPOT = (pow(2, (int64_t)floor(log(width) / log(2))) == width)
&& (pow(2, (int64_t)floor(log(height) / log(2))) == height);
&& (pow(2, (int64_t)floor(log(height) / log(2))) == height);
if (!isPOT)
throw std::logic_error("mip mapping requires power of two dimensions");
}
obs_enter_graphics();
m_texture = gs_texture_create(width, height, format, mip_levels, mip_data,
m_texture = gs_texture_create(
width, height, format, mip_levels, mip_data,
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0)
);
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
obs_leave_graphics();
if (!m_texture)
@ -63,10 +62,9 @@ gs::texture::texture(uint32_t width, uint32_t height,
m_textureType = type::Normal;
}
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) {
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)
{
if (width == 0)
throw std::logic_error("width must be at least 1");
if (height == 0)
@ -80,17 +78,17 @@ gs::texture::texture(uint32_t width, uint32_t height, uint32_t depth,
if (mip_levels > 1 || ((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps)) {
bool isPOT = (pow(2, (int64_t)floor(log(width) / log(2))) == width)
&& (pow(2, (int64_t)floor(log(height) / log(2))) == height)
&& (pow(2, (int64_t)floor(log(depth) / log(2))) == depth);
&& (pow(2, (int64_t)floor(log(height) / log(2))) == height)
&& (pow(2, (int64_t)floor(log(depth) / log(2))) == depth);
if (!isPOT)
throw std::logic_error("mip mapping requires power of two dimensions");
}
obs_enter_graphics();
m_texture = gs_voltexture_create(width, height, depth, format, mip_levels, mip_data,
m_texture = gs_voltexture_create(
width, height, depth, format, mip_levels, mip_data,
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0)
);
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
obs_leave_graphics();
if (!m_texture)
@ -99,10 +97,9 @@ gs::texture::texture(uint32_t width, uint32_t height, uint32_t depth,
m_textureType = type::Volume;
}
gs::texture::texture(uint32_t size, gs_color_format format, uint32_t mip_levels, const
uint8_t **mip_data,
gs::texture::flags texture_flags) {
gs::texture::texture(uint32_t size, gs_color_format format, uint32_t mip_levels, const uint8_t** mip_data,
gs::texture::flags texture_flags)
{
if (size == 0)
throw std::logic_error("size must be at least 1");
if (mip_levels == 0)
@ -117,10 +114,10 @@ gs::texture::texture(uint32_t size, gs_color_format format, uint32_t mip_levels,
}
obs_enter_graphics();
m_texture = gs_cubetexture_create(size, format, mip_levels, mip_data,
m_texture = gs_cubetexture_create(
size, format, mip_levels, mip_data,
(((texture_flags & flags::Dynamic) == flags::Dynamic) ? GS_DYNAMIC : 0)
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0)
);
| (((texture_flags & flags::BuildMipMaps) == flags::BuildMipMaps) ? GS_BUILD_MIPMAPS : 0));
obs_leave_graphics();
if (!m_texture)
@ -129,7 +126,8 @@ gs::texture::texture(uint32_t size, gs_color_format format, uint32_t mip_levels,
m_textureType = type::Cube;
}
gs::texture::texture(std::string file) {
gs::texture::texture(std::string file)
{
struct stat st;
if (os_stat(file.c_str(), &st) != 0)
throw std::ios_base::failure(file);
@ -142,127 +140,78 @@ gs::texture::texture(std::string file) {
throw std::runtime_error("Failed to load texture.");
}
gs::texture::texture(texture& other) {
throw std::logic_error("not yet implemented");
//obs_enter_graphics();
//switch (gs_get_texture_type(other.m_texture)) {
// case GS_TEXTURE_2D:
// uint32_t width = gs_texture_get_width(other.m_texture);
// uint32_t height = gs_texture_get_height(other.m_texture);
// gs_color_format format = gs_texture_get_color_format(other.m_texture);
// uint32_t mult = 0;
// switch (format) {
// case GS_A8:
// case GS_R8:
// mult = 1;
// break;
// case GS_R16:
// case GS_R16F:
// mult = 2;
// break;
// case GS_RG16F:
// case GS_R32F:
// case GS_BGRA:
// case GS_BGRX:
// case GS_RGBA:
// case GS_R10G10B10A2:
// mult = 4;
// break;
// case GS_RGBA16:
// case GS_RGBA16F:
// case GS_RG32F:
// mult = 8;
// break;
// case GS_RGBA32F:
// mult = 16;
// break;
// case GS_DXT1:
// case GS_DXT3:
// case GS_DXT5:
// mult = 8;
// break;
// }
// uint8_t* buf = new uint8_t[width * height * mult];
// m_texture = gs_texture_create(width, height, format,
// 1, &buf);
// delete buf;
// break;
// case GS_TEXTURE_3D:
// uint32_t width = gs_voltexture_get_width(other.m_texture);
// uint32_t height = gs_voltexture_get_height(other.m_texture);
// uint32_t depth = gs_voltexture_get_height(other.m_texture);
// gs_color_format format = gs_voltexture_get_color_format(other.m_texture);
// break;
// case GS_TEXTURE_CUBE:
// uint32_t size = gs_cubetexture_get_size(other.m_texture);
// gs_color_format format = gs_cubetexture_get_color_format(other.m_texture);
// gs_copy_texture()
// break;
//}
//obs_leave_graphics();
}
gs::texture::~texture() {
gs::texture::~texture()
{
if (m_isOwner && m_texture) {
obs_enter_graphics();
switch (gs_get_texture_type(m_texture)) {
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;
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;
}
obs_leave_graphics();
}
m_texture = nullptr;
}
void gs::texture::load(int unit) {
void gs::texture::load(int unit)
{
obs_enter_graphics();
gs_load_texture(m_texture, unit);
obs_leave_graphics();
}
gs_texture_t* gs::texture::get_object() {
gs_texture_t* gs::texture::get_object()
{
return m_texture;
}
uint32_t gs::texture::get_width() {
uint32_t gs::texture::get_width()
{
switch (m_textureType) {
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);
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);
}
return 0;
}
uint32_t gs::texture::get_height() {
uint32_t gs::texture::get_height()
{
switch (m_textureType) {
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);
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);
}
return 0;
}
uint32_t gs::texture::get_depth() {
uint32_t gs::texture::get_depth()
{
switch (m_textureType) {
case type::Normal:
return 1;
case type::Volume:
return gs_voltexture_get_depth(m_texture);
case type::Cube:
return 6;
case type::Normal:
return 1;
case type::Volume:
return gs_voltexture_get_depth(m_texture);
case type::Cube:
return 6;
}
return 0;
}
gs::texture::type gs::texture::get_type()
{
return m_textureType;
}

View file

@ -18,37 +18,46 @@
*/
#pragma once
#include <inttypes.h>
#include <cinttypes>
#include <string>
#include <utility.h>
#include "utility.h"
extern "C" {
#pragma warning( push )
#pragma warning( disable: 4201 )
#pragma warning(push)
#pragma warning(disable : 4201)
#include <graphics/graphics.h>
#pragma warning( pop )
#pragma warning(pop)
}
namespace gs {
class texture {
public:
enum class type {
Normal,
Volume,
Cube
};
enum class type : uint8_t { Normal, Volume, Cube };
enum class flags : size_t {
enum class flags : uint8_t {
None,
Dynamic,
BuildMipMaps,
};
enum class mip_method : uint8_t {
Point,
Linear,
Bilinear,
Sharpen,
Smoothen,
Bicubic,
Lanczos,
};
protected:
gs_texture_t * m_texture;
bool m_isOwner = true;
type m_textureType = type::Normal;
gs_texture_t* m_texture;
bool m_isOwner = true;
type m_textureType = type::Normal;
public:
~texture();
/*!
* \brief Create a 2D Texture
*
@ -57,12 +66,10 @@ namespace gs {
* \param format Color Format to use
* \param mip_levels Number of Mip Levels available
* \param mip_data Texture data including mipmaps
* \param flags Texture Flags
* \param texture_flags Texture Flags
*/
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);
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);
/*!
* \brief Create a 3D Texture
@ -73,12 +80,10 @@ namespace gs {
* \param format Color Format to use
* \param mip_levels Number of Mip Levels available
* \param mip_data Texture data including mipmaps
* \param flags Texture Flags
* \param texture_flags Texture Flags
*/
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);
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);
/*!
* \brief Create a Cube Texture
@ -87,12 +92,10 @@ namespace gs {
* \param format Color Format to use
* \param mip_levels Number of Mip Levels available
* \param mip_data Texture data including mipmaps
* \param flags Texture Flags
* \param texture_flags Texture Flags
*/
texture(uint32_t size,
gs_color_format format, uint32_t mip_levels,
const uint8_t **mip_data,
gs::texture::flags texture_flags);
texture(uint32_t size, gs_color_format format, uint32_t mip_levels, const uint8_t** mip_data,
gs::texture::flags texture_flags);
/*!
* \brief Load a texture from a file
@ -110,22 +113,7 @@ namespace gs {
* \brief Create a texture from an existing gs_texture_t object.
*/
texture(gs_texture_t* tex, bool takeOwnership = false) : m_texture(tex), m_isOwner(takeOwnership) {}
/*!
* \brief Copy an existing texture
*
* Create a Texture instance from an existing texture.
* This will not take ownership of the underlying gs_texture_t object.
*
* \param other
* \return
*/
texture(texture& other);
texture() : m_texture(nullptr) {}
virtual ~texture();
void load(int unit);
gs_texture_t* get_object();
@ -135,7 +123,9 @@ namespace gs {
uint32_t get_height();
uint32_t get_depth();
gs::texture::type get_type();
};
ENABLE_BITMASK_OPERATORS(gs::texture::flags)
}
} // namespace gs