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.hpp"
8#include "../float16.hpp"
9#include "../pixel_map.hpp"
10#include "../geometry/corner_shapes.hpp"
11#include "../geometry/numeric_array.hpp"
12#include <immintrin.h>
13#include <emmintrin.h>
14#include <algorithm>
15
16namespace tt {
17
19 // Red, Green, Blue, Alpha in binary16 (native endian).
21
22public:
23 sfloat_rgba16() noexcept
24 {
25 std::memset(v.data(), 0, sizeof(v));
26 }
27
28 sfloat_rgba16(sfloat_rgba16 const &rhs) noexcept = default;
29 sfloat_rgba16(sfloat_rgba16 &&rhs) noexcept = default;
30 sfloat_rgba16 &operator=(sfloat_rgba16 const &rhs) noexcept = default;
31 sfloat_rgba16 &operator=(sfloat_rgba16 &&rhs) noexcept = default;
32
33 sfloat_rgba16(f32x4 const &rhs) noexcept
34 {
35 ttlet rhs_fp16 = _mm_cvtps_ph(to_m128(rhs), _MM_FROUND_CUR_DIRECTION);
36 _mm_storeu_si64(v.data(), rhs_fp16);
37 }
38
39 sfloat_rgba16 &operator=(f32x4 const &rhs) noexcept
40 {
41 ttlet rhs_fp16 = _mm_cvtps_ph(to_m128(rhs), _MM_FROUND_CUR_DIRECTION);
42 _mm_storeu_si64(v.data(), rhs_fp16);
43 return *this;
44 }
45
46 explicit operator f32x4() const noexcept
47 {
48 ttlet rhs_fp16 = _mm_loadu_si64(v.data());
49 return f32x4{to_f32x4_raw(_mm_cvtph_ps(rhs_fp16))};
50 }
51
52 sfloat_rgba16(color const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
53
54 sfloat_rgba16 &operator=(color const &rhs) noexcept
55 {
56 return *this = static_cast<f32x4>(rhs);
57 }
58
59 explicit operator color() const noexcept
60 {
61 return color{static_cast<f32x4>(*this)};
62 }
63
64 [[nodiscard]] sfloat_rgba16(corner_shapes const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
65
66 // std::array<float16,4> const &get() const noexcept {
67 // return v;
68 //}
69 //
70 // std::array<float16,4> &get() noexcept {
71 // return v;
72 //}
73
74 [[nodiscard]] friend bool operator==(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept
75 {
76 return lhs.v == rhs.v;
77 }
78 [[nodiscard]] friend bool operator!=(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept
79 {
80 return !(lhs == rhs);
81 }
82
83 [[nodiscard]] friend sfloat_rgba16 makeTransparent(sfloat_rgba16 const &rhs) noexcept
84 {
86 r.v = rhs.v;
87 std::get<3>(r.v) = 0x0000; // 0.0f
88 return r;
89 }
90};
91
92inline void fill(pixel_map<sfloat_rgba16> &image, f32x4 color) noexcept
93{
94 for (ssize_t y = 0; y != image.height(); ++y) {
95 auto row = image[y];
96 for (ssize_t x = 0; x != image.width(); ++x) {
97 row[x] = color;
98 }
99 }
100}
101
102inline void composit(pixel_map<sfloat_rgba16> &under, pixel_map<sfloat_rgba16> const &over) noexcept
103{
104 tt_assert(over.height() >= under.height());
105 tt_assert(over.width() >= under.width());
106
107 for (ssize_t rowNr = 0; rowNr != under.height(); ++rowNr) {
108 ttlet overRow = over.at(rowNr);
109 auto underRow = under.at(rowNr);
110 for (ssize_t columnNr = 0; columnNr != under.width(); ++columnNr) {
111 ttlet &overPixel = overRow[columnNr];
112 auto &underPixel = underRow[columnNr];
113
114 underPixel = composit(static_cast<f32x4>(underPixel), static_cast<f32x4>(overPixel));
115 }
116 }
117}
118
119inline void composit(pixel_map<sfloat_rgba16> &under, color over, pixel_map<uint8_t> const &mask) noexcept
120{
121 tt_assert(mask.height() >= under.height());
122 tt_assert(mask.width() >= under.width());
123
124 auto maskPixel = color{1.0f, 1.0f, 1.0f, 1.0f};
125
126 for (ssize_t rowNr = 0; rowNr != under.height(); ++rowNr) {
127 ttlet maskRow = mask.at(rowNr);
128 auto underRow = under.at(rowNr);
129 for (ssize_t columnNr = 0; columnNr != under.width(); ++columnNr) {
130 ttlet maskValue = maskRow[columnNr] / 255.0f;
131 maskPixel.a() = maskValue;
132
133 auto &pixel = underRow[columnNr];
134 pixel = composit(static_cast<color>(pixel), over * maskPixel);
135 }
136 }
137}
138
139} // namespace tt
This is a RGBA floating point color.
Definition color.hpp:39
Definition sfloat_rgba16.hpp:18
Definition corner_shapes.hpp:9
A 2D canvas of pixels.
Definition pixel_map.hpp:100
T data(T... args)
T memset(T... args)