HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
sfloat_rgba16.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../color/color.hpp"
8#include "../float16.hpp"
9#include "../pixel_map.hpp"
10#include "../geometry/corner_shapes.hpp"
11#include "../rapid/numeric_array.hpp"
12#include "../hash.hpp"
13#include <immintrin.h>
14#include <emmintrin.h>
15#include <algorithm>
16
17namespace tt {
18
20 // Red, Green, Blue, Alpha in binary16 (native endian).
22
23public:
24 sfloat_rgba16() noexcept
25 {
26 std::memset(v.data(), 0, sizeof(v));
27 }
28
29 sfloat_rgba16(sfloat_rgba16 const &rhs) noexcept = default;
30 sfloat_rgba16(sfloat_rgba16 &&rhs) noexcept = default;
31 sfloat_rgba16 &operator=(sfloat_rgba16 const &rhs) noexcept = default;
32 sfloat_rgba16 &operator=(sfloat_rgba16 &&rhs) noexcept = default;
33
34 sfloat_rgba16(f32x4 const &rhs) noexcept
35 {
36 ttlet rhs_fp16 = f32x4_to_f16x8(rhs);
37 rhs_fp16.store<sizeof(v)>(reinterpret_cast<std::byte *>(v.data()));
38 }
39
40 sfloat_rgba16 &operator=(f32x4 const &rhs) noexcept
41 {
42 ttlet rhs_fp16 = f32x4_to_f16x8(rhs);
43 rhs_fp16.store<sizeof(v)>(reinterpret_cast<std::byte *>(v.data()));
44 return *this;
45 }
46
47 explicit operator f32x4() const noexcept
48 {
49 auto tmp = i16x8::load<sizeof(v)>(reinterpret_cast<std::byte const*>(v.data()));
50 return f16x8_to_f32x4(tmp);
51 }
52
53 sfloat_rgba16(color const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
54
55 sfloat_rgba16 &operator=(color const &rhs) noexcept
56 {
57 return *this = static_cast<f32x4>(rhs);
58 }
59
60 explicit operator color() const noexcept
61 {
62 return color{static_cast<f32x4>(*this)};
63 }
64
65 [[nodiscard]] sfloat_rgba16(corner_shapes const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
66
67 [[nodiscard]] size_t hash() const noexcept
68 {
69 return hash_mix(v[0], v[1], v[2], v[3]);
70 }
71
72 [[nodiscard]] friend bool operator==(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept
73 {
74 return lhs.v == rhs.v;
75 }
76 [[nodiscard]] friend bool operator!=(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept
77 {
78 return !(lhs == rhs);
79 }
80
81 [[nodiscard]] friend sfloat_rgba16 makeTransparent(sfloat_rgba16 const &rhs) noexcept
82 {
84 r.v = rhs.v;
85 std::get<3>(r.v) = 0x0000; // 0.0f
86 return r;
87 }
88};
89
90inline void fill(pixel_map<sfloat_rgba16> &image, f32x4 color) noexcept
91{
92 for (ssize_t y = 0; y != image.height(); ++y) {
93 auto row = image[y];
94 for (ssize_t x = 0; x != image.width(); ++x) {
95 row[x] = color;
96 }
97 }
98}
99
100inline void composit(pixel_map<sfloat_rgba16> &under, pixel_map<sfloat_rgba16> const &over) noexcept
101{
102 tt_assert(over.height() >= under.height());
103 tt_assert(over.width() >= under.width());
104
105 for (ssize_t rowNr = 0; rowNr != under.height(); ++rowNr) {
106 ttlet overRow = over.at(rowNr);
107 auto underRow = under.at(rowNr);
108 for (ssize_t columnNr = 0; columnNr != under.width(); ++columnNr) {
109 ttlet &overPixel = overRow[columnNr];
110 auto &underPixel = underRow[columnNr];
111
112 underPixel = composit(static_cast<f32x4>(underPixel), static_cast<f32x4>(overPixel));
113 }
114 }
115}
116
117inline void composit(pixel_map<sfloat_rgba16> &under, color over, pixel_map<uint8_t> const &mask) noexcept
118{
119 tt_assert(mask.height() >= under.height());
120 tt_assert(mask.width() >= under.width());
121
122 auto maskPixel = color{1.0f, 1.0f, 1.0f, 1.0f};
123
124 for (ssize_t rowNr = 0; rowNr != under.height(); ++rowNr) {
125 ttlet maskRow = mask.at(rowNr);
126 auto underRow = under.at(rowNr);
127 for (ssize_t columnNr = 0; columnNr != under.width(); ++columnNr) {
128 ttlet maskValue = maskRow[columnNr] / 255.0f;
129 maskPixel.a() = maskValue;
130
131 auto &pixel = underRow[columnNr];
132 pixel = composit(static_cast<color>(pixel), over * maskPixel);
133 }
134 }
135}
136
137} // namespace tt
138
139namespace std {
140
141template<>
142struct std::hash<tt::sfloat_rgba16> {
143 size_t operator()(tt::sfloat_rgba16 const &rhs) const noexcept
144 {
145 return rhs.hash();
146 }
147};
148
149}
STL namespace.
This is a RGBA floating point color.
Definition color.hpp:36
Definition corner_shapes.hpp:9
A 2D canvas of pixels.
Definition pixel_map.hpp:101
Definition sfloat_rgba16.hpp:19
T data(T... args)
T memset(T... args)
T operator()(T... args)