Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Use C++20 std::span to wrap vectors #283

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libvisual/libvisual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ SET(libvisual_SOURCES
lv_param_validators.c
lv_cpu.c
lv_error.c
lv_math.c
lv_gl.c
lv_alpha_blend.c
lv_util.c

lv_actor.cpp
lv_alpha_blend.cpp
lv_audio.cpp
lv_bin.cpp
lv_buffer.cpp
Expand All @@ -76,6 +75,7 @@ SET(libvisual_SOURCES
lv_fourier.cpp
lv_input.cpp
lv_libvisual.cpp
lv_math.cpp
lv_morph.cpp
lv_param.cpp
lv_plugin.cpp
Expand Down
46 changes: 0 additions & 46 deletions libvisual/libvisual/lv_alpha_blend.c

This file was deleted.

99 changes: 99 additions & 0 deletions libvisual/libvisual/lv_alpha_blend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "config.h"
#include "lv_alpha_blend.h"
#include "lv_common.h"
#include "lv_cpu.h"
#include "lv_alpha_blend_orc.h"
#include <cassert>

namespace {

#pragma pack(1)

struct rgb16_t {
#if VISUAL_LITTLE_ENDIAN == 1
std::uint16_t b:5, g:6, r:5;
#else
std::uint16_t r:5, g:6, b:5;
#endif
};

#pragma pack()

static_assert (sizeof (rgb16_t) == 2);

} // anonymous namespace

namespace LV {

void alpha_blend_8 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha)
{
assert (dst.data () != src1.data ());
assert (dst.data () != src2.data ());
assert (src1.size () == src2.size ());

auto size {static_cast<int> (std::min (dst.size (), src1.size ()))};
::simd_interpolate_8 (dst.data (), src1.data (), src2.data (), alpha, size);
}

void alpha_blend_16 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha)
{
assert (dst.data () != src1.data ());
assert (dst.data () != src2.data ());
assert (src1.size () == src2.size ());

auto dst_ptr = reinterpret_cast<rgb16_t*> (dst.data ());
auto src1_ptr = reinterpret_cast<rgb16_t const*> (src1.data ());
auto src2_ptr = reinterpret_cast<rgb16_t const*> (src2.data ());

auto size {static_cast<int> (std::min (dst.size (), src1.size ()))};
auto pixel_count {size / sizeof (rgb16_t)};

for (std::size_t i {0}; i < pixel_count; i++) {
dst_ptr[i].r = (alpha * (src2_ptr[i].r - src1_ptr[i].r)) / 255 + src1_ptr[i].r;
dst_ptr[i].g = (alpha * (src2_ptr[i].g - src1_ptr[i].g)) / 255 + src1_ptr[i].g;
dst_ptr[i].b = (alpha * (src2_ptr[i].b - src1_ptr[i].b)) / 255 + src1_ptr[i].b;
}
}

void alpha_blend_24 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha)
{
assert (dst.data () != src1.data ());
assert (dst.data () != src2.data ());
assert (src1.size () == src2.size ());

auto size {static_cast<int> (std::min (dst.size (), src1.size ()))};
::simd_interpolate_8 (dst.data (), src1.data (), src2.data (), alpha, size * 3);
}

void alpha_blend_32 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha)
{
assert (dst.data () != src1.data ());
assert (dst.data () != src2.data ());
assert (src1.size () == src2.size ());

auto size {static_cast<int> (std::min (dst.size (), src1.size ()))};
::simd_interpolate_8 (dst.data (), src1.data (), src2.data (), alpha, size * 4);
}

} // LV namespace


void visual_alpha_blend_8 (uint8_t *LV_RESTRICT dest, const uint8_t *LV_RESTRICT src1, const uint8_t *LV_RESTRICT src2, visual_size_t size, uint8_t alpha)
{
simd_interpolate_8 (dest, src1, src2, alpha, (int) size);
}

void visual_alpha_blend_16 (uint8_t *LV_RESTRICT dest, const uint8_t *LV_RESTRICT src1, const uint8_t *LV_RESTRICT src2, visual_size_t size, uint8_t alpha)
{
LV::alpha_blend_16 ({dest, size}, {src1, size}, {src2, size}, alpha);
}

void visual_alpha_blend_24 (uint8_t *LV_RESTRICT dest, const uint8_t *LV_RESTRICT src1, const uint8_t *LV_RESTRICT src2, visual_size_t size, uint8_t alpha)
{
simd_interpolate_8 (dest, src1, src2, alpha, (int) size * 3);
}

void visual_alpha_blend_32 (uint8_t *LV_RESTRICT dest, const uint8_t *LV_RESTRICT src1, const uint8_t *LV_RESTRICT src2, visual_size_t size, uint8_t alpha)
{
simd_interpolate_8 (dest, src1, src2, alpha, (int) size * 4);
}
16 changes: 16 additions & 0 deletions libvisual/libvisual/lv_alpha_blend.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
#include <libvisual/lv_defines.h>
#include <libvisual/lv_types.h>

#ifdef __cplusplus

#include <cstdint>
#include <span>

namespace LV {

LV_API void alpha_blend_8 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha);
LV_API void alpha_blend_16 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha);
LV_API void alpha_blend_24 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha);
LV_API void alpha_blend_32 (std::span<std::uint8_t> dst, std::span<std::uint8_t const> src1, std::span<std::uint8_t const> src2, std::uint8_t alpha);

} // LV namespace

#endif /* __cplusplus */

LV_BEGIN_DECLS

LV_API void visual_alpha_blend_8 (uint8_t *LV_RESTRICT dest, const uint8_t *LV_RESTRICT src1, const uint8_t *LV_RESTRICT src2, visual_size_t size, uint8_t alpha);
Expand Down
19 changes: 19 additions & 0 deletions libvisual/libvisual/lv_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <libvisual/lv_intrusive_ptr.hpp>
#include <memory>
#include <span>
#include <cstdlib>

namespace LV {
Expand Down Expand Up @@ -88,6 +89,24 @@ namespace LV {
*/
void destroy_content ();

/**
* Returns an std::span that is a view of the entire buffer.
*/
template <typename T>
std::span<T> as_span () noexcept
{
return {static_cast<T*> (get_data ()), get_size () / sizeof (T)};
}

/**
* Returns an std::span that is a view of the entire buffer.
*/
template <typename T>
std::span<T const> as_span () const noexcept
{
return {static_cast<T const*> (get_data ()), get_size () / sizeof (T)};
}

/**
* Sets the data pair (data and its size).
*
Expand Down
109 changes: 0 additions & 109 deletions libvisual/libvisual/lv_math.c

This file was deleted.

Loading