diff --git a/source/gs-vertexbuffer.cpp b/source/gs-vertexbuffer.cpp index 3cb86b1e..91026e13 100644 --- a/source/gs-vertexbuffer.cpp +++ b/source/gs-vertexbuffer.cpp @@ -18,16 +18,17 @@ */ #include "gs-vertexbuffer.h" -#include "util-memory.h" #include +#include "util-memory.h" extern "C" { -#pragma warning( push ) -#pragma warning( disable: 4201 ) +#pragma warning(push) +#pragma warning(disable : 4201) #include -#pragma warning( pop ) +#pragma warning(pop) } -gs::vertex_buffer::~vertex_buffer() { +gs::vertex_buffer::~vertex_buffer() +{ if (m_positions) { util::free_aligned(m_positions); 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) { 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. - m_vertexbufferdata = gs_vbdata_create(); - m_vertexbufferdata->num = m_capacity; + m_vertexbufferdata = gs_vbdata_create(); + m_vertexbufferdata->num = 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); - std::memset(m_normals, 0, 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); + + // 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); + 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++) { 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); } @@ -102,7 +111,7 @@ gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) { obs_enter_graphics(); m_vertexbuffer = gs_vertexbuffer_create(m_vertexbufferdata, GS_DYNAMIC); 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; obs_leave_graphics(); 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(); gs_vb_data* vbd = gs_vertexbuffer_get_data(vb); 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)); } else { for (size_t idx = 0; idx < m_capacity; idx++) { - float* mem = reinterpret_cast(vbd->tvarray[n].array) - + (idx * vbd->tvarray[n].width); + float* mem = reinterpret_cast(vbd->tvarray[n].array) + (idx * vbd->tvarray[n].width); + // cppcheck-suppress memsetClassFloat std::memset(&m_uvs[n][idx], 0, sizeof(vec4)); 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(); } -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 std::memcpy(m_positions, other.m_positions, 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 - m_capacity = other.m_capacity; - m_size = other.m_size; - m_layers = other.m_layers; + m_capacity = other.m_capacity; + m_size = other.m_size; + m_layers = other.m_layers; m_positions = other.m_positions; - m_normals = other.m_normals; - m_tangents = other.m_tangents; + m_normals = other.m_normals; + m_tangents = other.m_tangents; for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { m_uvs[n] = other.m_uvs[n]; } m_vertexbufferdata = other.m_vertexbufferdata; - m_vertexbuffer = other.m_vertexbuffer; - m_layerdata = other.m_layerdata; + m_vertexbuffer = other.m_vertexbuffer; + 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 /// First self-destruct (semi-destruct itself). if (m_positions) { @@ -217,36 +232,40 @@ void gs::vertex_buffer::operator=(vertex_buffer const&& other) { } /// Then assign new values. - m_capacity = other.m_capacity; - m_size = other.m_size; - m_layers = other.m_layers; + m_capacity = other.m_capacity; + m_size = other.m_size; + m_layers = other.m_layers; m_positions = other.m_positions; - m_normals = other.m_normals; - m_tangents = other.m_tangents; + m_normals = other.m_normals; + m_tangents = other.m_tangents; for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { m_uvs[n] = other.m_uvs[n]; } m_vertexbufferdata = other.m_vertexbufferdata; - m_vertexbuffer = other.m_vertexbuffer; - m_layerdata = other.m_layerdata; + m_vertexbuffer = other.m_vertexbuffer; + 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) { throw std::out_of_range("new_size out of range"); } m_size = new_size; } -uint32_t gs::vertex_buffer::size() { +uint32_t gs::vertex_buffer::size() +{ return m_size; } -bool gs::vertex_buffer::empty() { +bool gs::vertex_buffer::empty() +{ 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)) { 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; } -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); } -void gs::vertex_buffer::set_uv_layers(uint32_t layers) { +void gs::vertex_buffer::set_uv_layers(uint32_t layers) +{ m_layers = layers; } -uint32_t gs::vertex_buffer::get_uv_layers() { +uint32_t gs::vertex_buffer::get_uv_layers() +{ return m_layers; } -vec3* gs::vertex_buffer::get_positions() { +vec3* gs::vertex_buffer::get_positions() +{ return m_positions; } -vec3* gs::vertex_buffer::get_normals() { +vec3* gs::vertex_buffer::get_normals() +{ return m_normals; } -vec3* gs::vertex_buffer::get_tangents() { +vec3* gs::vertex_buffer::get_tangents() +{ return m_tangents; } -uint32_t* gs::vertex_buffer::get_colors() { +uint32_t* gs::vertex_buffer::get_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)) { throw std::out_of_range("idx out of range"); } return m_uvs[idx]; } -gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) { +gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) +{ if (!refreshGPU) return m_vertexbuffer; @@ -304,13 +332,13 @@ gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) { obs_enter_graphics(); m_vertexbufferdata = gs_vertexbuffer_get_data(m_vertexbuffer); std::memset(m_vertexbufferdata, 0, sizeof(gs_vb_data)); - m_vertexbufferdata->num = m_capacity; - m_vertexbufferdata->points = m_positions; - m_vertexbufferdata->normals = m_normals; + m_vertexbufferdata->num = m_capacity; + m_vertexbufferdata->points = m_positions; + m_vertexbufferdata->normals = m_normals; m_vertexbufferdata->tangents = m_tangents; - m_vertexbufferdata->colors = m_colors; - m_vertexbufferdata->num_tex = m_layers; - m_vertexbufferdata->tvarray = m_layerdata; + m_vertexbufferdata->colors = m_colors; + m_vertexbufferdata->num_tex = m_layers; + m_vertexbufferdata->tvarray = m_layerdata; for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { m_layerdata[n].array = m_uvs[n]; 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. 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; for (uint32_t n = 0; n < m_layers; n++) { m_layerdata[n].width = 4; @@ -331,6 +359,7 @@ gs_vertbuffer_t* gs::vertex_buffer::update(bool refreshGPU) { return m_vertexbuffer; } -gs_vertbuffer_t* gs::vertex_buffer::update() { +gs_vertbuffer_t* gs::vertex_buffer::update() +{ return update(true); } diff --git a/source/gs-vertexbuffer.h b/source/gs-vertexbuffer.h index 88118d89..0c6313c6 100644 --- a/source/gs-vertexbuffer.h +++ b/source/gs-vertexbuffer.h @@ -18,24 +18,30 @@ */ #pragma once +#include #include "gs-limits.h" #include "gs-vertex.h" #include "util-math.h" #include "util-memory.h" -#include + extern "C" { -#pragma warning( push ) -#pragma warning( disable: 4201 ) +#pragma warning(push) +#pragma warning(disable : 4201) #include -#pragma warning( pop ) +#pragma warning(pop) } namespace gs { class vertex_buffer { public: - #pragma region Constructor & Destructor +#pragma region Constructor& Destructor 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. * @@ -43,13 +49,6 @@ namespace gs { */ 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 * Full Description below @@ -58,9 +57,9 @@ namespace gs { */ 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 /*! @@ -96,9 +95,7 @@ namespace gs { * \param other */ void operator=(vertex_buffer const&& other); - #pragma endregion Copy/Move Constructors - - +#pragma endregion Copy / Move Constructors void resize(uint32_t new_size); @@ -154,11 +151,11 @@ namespace gs { */ 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(bool refreshGPU); - #pragma endregion Update / Grab GS object +#pragma endregion Update / Grab GS object private: uint32_t m_size; @@ -166,15 +163,15 @@ namespace gs { uint32_t m_layers; // Memory Storage - vec3 *m_positions; - vec3 *m_normals; - vec3 *m_tangents; - uint32_t *m_colors; - vec4 *m_uvs[MAXIMUM_UVW_LAYERS]; + vec3* m_positions; + vec3* m_normals; + vec3* m_tangents; + uint32_t* m_colors; + vec4* m_uvs[MAXIMUM_UVW_LAYERS]; // OBS GS Data - gs_vb_data* m_vertexbufferdata; + gs_vb_data* m_vertexbufferdata; gs_vertbuffer_t* m_vertexbuffer; - gs_tvertarray* m_layerdata; + gs_tvertarray* m_layerdata; }; -} +} // namespace gs