gs-vertexbuffer: Formatting and various fixes

With the addition of clang-format and cppcheck, and the changes to .editorconfig, various changes need to be added.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2018-09-27 02:49:00 +02:00
parent dac0553377
commit c3e67bad97
2 changed files with 113 additions and 87 deletions

View file

@ -18,16 +18,17 @@
*/ */
#include "gs-vertexbuffer.h" #include "gs-vertexbuffer.h"
#include "util-memory.h"
#include <stdexcept> #include <stdexcept>
#include "util-memory.h"
extern "C" { extern "C" {
#pragma warning( push ) #pragma warning(push)
#pragma warning( disable: 4201 ) #pragma warning(disable : 4201)
#include <obs.h> #include <obs.h>
#pragma warning( pop ) #pragma warning(pop)
} }
gs::vertex_buffer::~vertex_buffer() { gs::vertex_buffer::~vertex_buffer()
{
if (m_positions) { if (m_positions) {
util::free_aligned(m_positions); util::free_aligned(m_positions);
m_positions = nullptr; m_positions = nullptr;
@ -69,32 +70,40 @@ gs::vertex_buffer::~vertex_buffer() {
} }
} }
gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) { // cppcheck-suppress uninitMemberVar
gs::vertex_buffer::vertex_buffer() : vertex_buffer(MAXIMUM_VERTICES) {}
gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices)
: m_size(maximumVertices), m_capacity(maximumVertices), m_layers(MAXIMUM_UVW_LAYERS), m_positions(nullptr),
m_normals(nullptr), m_tangents(nullptr), m_colors(nullptr), m_vertexbufferdata(nullptr), m_vertexbuffer(nullptr),
m_layerdata(nullptr)
{
if (maximumVertices > MAXIMUM_VERTICES) { if (maximumVertices > MAXIMUM_VERTICES) {
throw std::out_of_range("maximumVertices out of range"); throw std::out_of_range("maximumVertices out of range");
} }
// Assign limits.
m_capacity = maximumVertices;
m_size = m_capacity;
m_layers = MAXIMUM_UVW_LAYERS;
// Allocate memory for data. // Allocate memory for data.
m_vertexbufferdata = gs_vbdata_create(); m_vertexbufferdata = gs_vbdata_create();
m_vertexbufferdata->num = m_capacity; m_vertexbufferdata->num = m_capacity;
m_vertexbufferdata->points = m_positions = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity); m_vertexbufferdata->points = m_positions = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity);
std::memset(m_positions, 0, sizeof(vec3) * m_capacity);
m_vertexbufferdata->normals = m_normals = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity); m_vertexbufferdata->normals = m_normals = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity);
std::memset(m_normals, 0, sizeof(vec3) * m_capacity);
m_vertexbufferdata->tangents = m_tangents = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity); m_vertexbufferdata->tangents = m_tangents = (vec3*)util::malloc_aligned(16, sizeof(vec3) * m_capacity);
std::memset(m_tangents, 0, sizeof(vec3) * m_capacity);
m_vertexbufferdata->colors = m_colors = (uint32_t*)util::malloc_aligned(16, sizeof(uint32_t) * m_capacity); m_vertexbufferdata->colors = m_colors = (uint32_t*)util::malloc_aligned(16, sizeof(uint32_t) * m_capacity);
// cppcheck-suppress memsetClassFloat
std::memset(m_positions, 0, sizeof(vec3) * m_capacity);
// cppcheck-suppress memsetClassFloat
std::memset(m_normals, 0, sizeof(vec3) * m_capacity);
// cppcheck-suppress memsetClassFloat
std::memset(m_tangents, 0, sizeof(vec3) * m_capacity);
std::memset(m_colors, 0, sizeof(uint32_t) * m_capacity); std::memset(m_colors, 0, sizeof(uint32_t) * m_capacity);
m_vertexbufferdata->num_tex = m_layers; m_vertexbufferdata->num_tex = m_layers;
m_vertexbufferdata->tvarray = m_layerdata = (gs_tvertarray*)util::malloc_aligned(16, sizeof(gs_tvertarray)* m_layers); m_vertexbufferdata->tvarray = m_layerdata =
(gs_tvertarray*)util::malloc_aligned(16, sizeof(gs_tvertarray) * m_layers);
for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) {
m_layerdata[n].array = m_uvs[n] = (vec4*)util::malloc_aligned(16, sizeof(vec4) * m_capacity); m_layerdata[n].array = m_uvs[n] = (vec4*)util::malloc_aligned(16, sizeof(vec4) * m_capacity);
m_layerdata[n].width = 4; m_layerdata[n].width = 4;
std::memset(m_uvs[n], 0, sizeof(vec4) * m_capacity); std::memset(m_uvs[n], 0, sizeof(vec4) * m_capacity);
} }
@ -102,7 +111,7 @@ gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) {
obs_enter_graphics(); obs_enter_graphics();
m_vertexbuffer = gs_vertexbuffer_create(m_vertexbufferdata, GS_DYNAMIC); m_vertexbuffer = gs_vertexbuffer_create(m_vertexbufferdata, GS_DYNAMIC);
std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data)); std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data));
m_vertexbufferdata->num = m_capacity; m_vertexbufferdata->num = m_capacity;
m_vertexbufferdata->num_tex = m_layers; m_vertexbufferdata->num_tex = m_layers;
obs_leave_graphics(); obs_leave_graphics();
if (!m_vertexbuffer) { if (!m_vertexbuffer) {
@ -110,7 +119,9 @@ gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) {
} }
} }
gs::vertex_buffer::vertex_buffer(gs_vertbuffer_t* vb) { // cppcheck-suppress uninitMemberVar
gs::vertex_buffer::vertex_buffer(gs_vertbuffer_t* vb)
{
obs_enter_graphics(); obs_enter_graphics();
gs_vb_data* vbd = gs_vertexbuffer_get_data(vb); gs_vb_data* vbd = gs_vertexbuffer_get_data(vb);
if (!vbd) if (!vbd)
@ -134,8 +145,8 @@ gs::vertex_buffer::vertex_buffer(gs_vertbuffer_t* vb) {
std::memcpy(m_uvs[n], vbd->tvarray[n].array, vbd->num * sizeof(vec4)); std::memcpy(m_uvs[n], vbd->tvarray[n].array, vbd->num * sizeof(vec4));
} else { } else {
for (size_t idx = 0; idx < m_capacity; idx++) { for (size_t idx = 0; idx < m_capacity; idx++) {
float* mem = reinterpret_cast<float*>(vbd->tvarray[n].array) float* mem = reinterpret_cast<float*>(vbd->tvarray[n].array) + (idx * vbd->tvarray[n].width);
+ (idx * vbd->tvarray[n].width); // cppcheck-suppress memsetClassFloat
std::memset(&m_uvs[n][idx], 0, sizeof(vec4)); std::memset(&m_uvs[n][idx], 0, sizeof(vec4));
std::memcpy(&m_uvs[n][idx], mem, vbd->tvarray[n].width); std::memcpy(&m_uvs[n][idx], mem, vbd->tvarray[n].width);
} }
@ -146,7 +157,9 @@ gs::vertex_buffer::vertex_buffer(gs_vertbuffer_t* vb) {
obs_leave_graphics(); obs_leave_graphics();
} }
gs::vertex_buffer::vertex_buffer(vertex_buffer const& other) : vertex_buffer(other.m_capacity) { // cppcheck-suppress uninitMemberVar
gs::vertex_buffer::vertex_buffer(vertex_buffer const& other) : vertex_buffer(other.m_capacity)
{
// Copy Constructor // Copy Constructor
std::memcpy(m_positions, other.m_positions, m_capacity * sizeof(vec3)); std::memcpy(m_positions, other.m_positions, m_capacity * sizeof(vec3));
std::memcpy(m_normals, other.m_normals, m_capacity * sizeof(vec3)); std::memcpy(m_normals, other.m_normals, m_capacity * sizeof(vec3));
@ -157,23 +170,25 @@ gs::vertex_buffer::vertex_buffer(vertex_buffer const& other) : vertex_buffer(oth
} }
} }
gs::vertex_buffer::vertex_buffer(vertex_buffer const&& other) { gs::vertex_buffer::vertex_buffer(vertex_buffer const&& other)
{
// Move Constructor // Move Constructor
m_capacity = other.m_capacity; m_capacity = other.m_capacity;
m_size = other.m_size; m_size = other.m_size;
m_layers = other.m_layers; m_layers = other.m_layers;
m_positions = other.m_positions; m_positions = other.m_positions;
m_normals = other.m_normals; m_normals = other.m_normals;
m_tangents = other.m_tangents; m_tangents = other.m_tangents;
for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) {
m_uvs[n] = other.m_uvs[n]; m_uvs[n] = other.m_uvs[n];
} }
m_vertexbufferdata = other.m_vertexbufferdata; m_vertexbufferdata = other.m_vertexbufferdata;
m_vertexbuffer = other.m_vertexbuffer; m_vertexbuffer = other.m_vertexbuffer;
m_layerdata = other.m_layerdata; m_layerdata = other.m_layerdata;
} }
void gs::vertex_buffer::operator=(vertex_buffer const&& other) { void gs::vertex_buffer::operator=(vertex_buffer const&& other)
{
// Move Assignment // Move Assignment
/// First self-destruct (semi-destruct itself). /// First self-destruct (semi-destruct itself).
if (m_positions) { if (m_positions) {
@ -217,36 +232,40 @@ void gs::vertex_buffer::operator=(vertex_buffer const&& other) {
} }
/// Then assign new values. /// Then assign new values.
m_capacity = other.m_capacity; m_capacity = other.m_capacity;
m_size = other.m_size; m_size = other.m_size;
m_layers = other.m_layers; m_layers = other.m_layers;
m_positions = other.m_positions; m_positions = other.m_positions;
m_normals = other.m_normals; m_normals = other.m_normals;
m_tangents = other.m_tangents; m_tangents = other.m_tangents;
for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) {
m_uvs[n] = other.m_uvs[n]; m_uvs[n] = other.m_uvs[n];
} }
m_vertexbufferdata = other.m_vertexbufferdata; m_vertexbufferdata = other.m_vertexbufferdata;
m_vertexbuffer = other.m_vertexbuffer; m_vertexbuffer = other.m_vertexbuffer;
m_layerdata = other.m_layerdata; m_layerdata = other.m_layerdata;
} }
void gs::vertex_buffer::resize(uint32_t new_size) { void gs::vertex_buffer::resize(uint32_t new_size)
{
if (new_size > m_capacity) { if (new_size > m_capacity) {
throw std::out_of_range("new_size out of range"); throw std::out_of_range("new_size out of range");
} }
m_size = new_size; m_size = new_size;
} }
uint32_t gs::vertex_buffer::size() { uint32_t gs::vertex_buffer::size()
{
return m_size; return m_size;
} }
bool gs::vertex_buffer::empty() { bool gs::vertex_buffer::empty()
{
return m_size == 0; return m_size == 0;
} }
const gs::vertex gs::vertex_buffer::at(uint32_t idx) { const gs::vertex gs::vertex_buffer::at(uint32_t idx)
{
if ((idx < 0) || (idx >= m_size)) { if ((idx < 0) || (idx >= m_size)) {
throw std::out_of_range("idx out of range"); throw std::out_of_range("idx out of range");
} }
@ -258,42 +277,51 @@ const gs::vertex gs::vertex_buffer::at(uint32_t idx) {
return vtx; return vtx;
} }
const gs::vertex gs::vertex_buffer::operator[](uint32_t const pos) { const gs::vertex gs::vertex_buffer::operator[](uint32_t const pos)
{
return at(pos); return at(pos);
} }
void gs::vertex_buffer::set_uv_layers(uint32_t layers) { void gs::vertex_buffer::set_uv_layers(uint32_t layers)
{
m_layers = layers; m_layers = layers;
} }
uint32_t gs::vertex_buffer::get_uv_layers() { uint32_t gs::vertex_buffer::get_uv_layers()
{
return m_layers; return m_layers;
} }
vec3* gs::vertex_buffer::get_positions() { vec3* gs::vertex_buffer::get_positions()
{
return m_positions; return m_positions;
} }
vec3* gs::vertex_buffer::get_normals() { vec3* gs::vertex_buffer::get_normals()
{
return m_normals; return m_normals;
} }
vec3* gs::vertex_buffer::get_tangents() { vec3* gs::vertex_buffer::get_tangents()
{
return m_tangents; return m_tangents;
} }
uint32_t* gs::vertex_buffer::get_colors() { uint32_t* gs::vertex_buffer::get_colors()
{
return m_colors; return m_colors;
} }
vec4* gs::vertex_buffer::get_uv_layer(size_t idx) { vec4* gs::vertex_buffer::get_uv_layer(size_t idx)
{
if ((idx < 0) || (idx >= m_layers)) { if ((idx < 0) || (idx >= m_layers)) {
throw std::out_of_range("idx out of range"); throw std::out_of_range("idx out of range");
} }
return m_uvs[idx]; return m_uvs[idx];
} }
gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) { gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU)
{
if (!refreshGPU) if (!refreshGPU)
return m_vertexbuffer; return m_vertexbuffer;
@ -304,13 +332,13 @@ gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) {
obs_enter_graphics(); obs_enter_graphics();
m_vertexbufferdata = gs_vertexbuffer_get_data(m_vertexbuffer); m_vertexbufferdata = gs_vertexbuffer_get_data(m_vertexbuffer);
std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data)); std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data));
m_vertexbufferdata->num = m_capacity; m_vertexbufferdata->num = m_capacity;
m_vertexbufferdata->points = m_positions; m_vertexbufferdata->points = m_positions;
m_vertexbufferdata->normals = m_normals; m_vertexbufferdata->normals = m_normals;
m_vertexbufferdata->tangents = m_tangents; m_vertexbufferdata->tangents = m_tangents;
m_vertexbufferdata->colors = m_colors; m_vertexbufferdata->colors = m_colors;
m_vertexbufferdata->num_tex = m_layers; m_vertexbufferdata->num_tex = m_layers;
m_vertexbufferdata->tvarray = m_layerdata; m_vertexbufferdata->tvarray = m_layerdata;
for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) {
m_layerdata[n].array = m_uvs[n]; m_layerdata[n].array = m_uvs[n];
m_layerdata[n].width = 4; m_layerdata[n].width = 4;
@ -322,7 +350,7 @@ gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) {
// WORKAROUND: OBS Studio 20.x and below incorrectly deletes data that it doesn't own. // WORKAROUND: OBS Studio 20.x and below incorrectly deletes data that it doesn't own.
std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data)); std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data));
m_vertexbufferdata->num = m_capacity; m_vertexbufferdata->num = m_capacity;
m_vertexbufferdata->num_tex = m_layers; m_vertexbufferdata->num_tex = m_layers;
for (uint32_t n = 0; n < m_layers; n++) { for (uint32_t n = 0; n < m_layers; n++) {
m_layerdata[n].width = 4; m_layerdata[n].width = 4;
@ -331,6 +359,7 @@ gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) {
return m_vertexbuffer; return m_vertexbuffer;
} }
gs_vertbuffer_t* gs::vertex_buffer::update() { gs_vertbuffer_t* gs::vertex_buffer::update()
{
return update(true); return update(true);
} }

View file

@ -18,24 +18,30 @@
*/ */
#pragma once #pragma once
#include <cinttypes>
#include "gs-limits.h" #include "gs-limits.h"
#include "gs-vertex.h" #include "gs-vertex.h"
#include "util-math.h" #include "util-math.h"
#include "util-memory.h" #include "util-memory.h"
#include <inttypes.h>
extern "C" { extern "C" {
#pragma warning( push ) #pragma warning(push)
#pragma warning( disable: 4201 ) #pragma warning(disable : 4201)
#include <graphics/graphics.h> #include <graphics/graphics.h>
#pragma warning( pop ) #pragma warning(pop)
} }
namespace gs { namespace gs {
class vertex_buffer { class vertex_buffer {
public: public:
#pragma region Constructor & Destructor #pragma region Constructor& Destructor
virtual ~vertex_buffer(); virtual ~vertex_buffer();
/*!
* \brief Create a Vertex Buffer with the default number of Vertices.
*/
vertex_buffer();
/*! /*!
* \brief Create a Vertex Buffer with a specific number of Vertices. * \brief Create a Vertex Buffer with a specific number of Vertices.
* *
@ -43,13 +49,6 @@ namespace gs {
*/ */
vertex_buffer(uint32_t maximumVertices); vertex_buffer(uint32_t maximumVertices);
/*!
* \brief Create a Vertex Buffer with the maximum number of Vertices.
*
* \param maximumVertices Maximum amount of vertices to store.
*/
vertex_buffer() : vertex_buffer(MAXIMUM_VERTICES) {};
/*! /*!
* \brief Create a copy of a Vertex Buffer * \brief Create a copy of a Vertex Buffer
* Full Description below * Full Description below
@ -58,9 +57,9 @@ namespace gs {
*/ */
vertex_buffer(gs_vertbuffer_t* other); vertex_buffer(gs_vertbuffer_t* other);
#pragma endregion Constructor & Destructor #pragma endregion Constructor& Destructor
#pragma region Copy/Move Constructors #pragma region Copy / Move Constructors
// Copy Constructor & Assignments // Copy Constructor & Assignments
/*! /*!
@ -96,9 +95,7 @@ namespace gs {
* \param other * \param other
*/ */
void operator=(vertex_buffer const&& other); void operator=(vertex_buffer const&& other);
#pragma endregion Copy/Move Constructors #pragma endregion Copy / Move Constructors
void resize(uint32_t new_size); void resize(uint32_t new_size);
@ -154,11 +151,11 @@ namespace gs {
*/ */
vec4* get_uv_layer(size_t idx); vec4* get_uv_layer(size_t idx);
#pragma region Update / Grab GS object #pragma region Update / Grab GS object
gs_vertbuffer_t* update(); gs_vertbuffer_t* update();
gs_vertbuffer_t* update(bool refreshGPU); gs_vertbuffer_t* update(bool refreshGPU);
#pragma endregion Update / Grab GS object #pragma endregion Update / Grab GS object
private: private:
uint32_t m_size; uint32_t m_size;
@ -166,15 +163,15 @@ namespace gs {
uint32_t m_layers; uint32_t m_layers;
// Memory Storage // Memory Storage
vec3 *m_positions; vec3* m_positions;
vec3 *m_normals; vec3* m_normals;
vec3 *m_tangents; vec3* m_tangents;
uint32_t *m_colors; uint32_t* m_colors;
vec4 *m_uvs[MAXIMUM_UVW_LAYERS]; vec4* m_uvs[MAXIMUM_UVW_LAYERS];
// OBS GS Data // OBS GS Data
gs_vb_data* m_vertexbufferdata; gs_vb_data* m_vertexbufferdata;
gs_vertbuffer_t* m_vertexbuffer; gs_vertbuffer_t* m_vertexbuffer;
gs_tvertarray* m_layerdata; gs_tvertarray* m_layerdata;
}; };
} } // namespace gs