HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
R16G16B16A16SFloat.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Foundation/vec.hpp"
7#include "TTauri/Foundation/float16.hpp"
8#include "TTauri/Foundation/PixelMap.hpp"
9#include <immintrin.h>
10#include <emmintrin.h>
11#include <algorithm>
12
13namespace tt {
14
16 // Red, Green, Blue, Alpha in binary16 (native endian).
18
19public:
20 tt_force_inline R16G16B16A16SFloat() noexcept {
21 std::memset(v.data(), 0, sizeof(v));
22 }
23
24 tt_force_inline R16G16B16A16SFloat(R16G16B16A16SFloat const &rhs) noexcept = default;
25 tt_force_inline R16G16B16A16SFloat(R16G16B16A16SFloat &&rhs) noexcept = default;
26 tt_force_inline R16G16B16A16SFloat &operator=(R16G16B16A16SFloat const &rhs) noexcept = default;
27 tt_force_inline R16G16B16A16SFloat &operator=(R16G16B16A16SFloat &&rhs) noexcept = default;
28
29 tt_force_inline R16G16B16A16SFloat(vec const &rhs) noexcept {
30 ttlet rhs_fp16 = _mm_cvtps_ph(rhs, _MM_FROUND_CUR_DIRECTION);
31 _mm_storeu_si64(v.data(), rhs_fp16);
32 }
33
34 tt_force_inline R16G16B16A16SFloat &operator=(vec const &rhs) noexcept {
35 ttlet rhs_fp16 = _mm_cvtps_ph(rhs, _MM_FROUND_CUR_DIRECTION);
36 _mm_storeu_si64(v.data(), rhs_fp16);
37 return *this;
38 }
39
40 tt_force_inline operator vec () const noexcept {
41 ttlet rhs_fp16 = _mm_loadu_si64(v.data());
42 return _mm_cvtph_ps(rhs_fp16);
43 }
44
45 std::array<float16,4> const &get() const noexcept {
46 return v;
47 }
48
49 std::array<float16,4> &get() noexcept {
50 return v;
51 }
52
53 [[nodiscard]] tt_force_inline friend bool operator==(R16G16B16A16SFloat const &lhs, R16G16B16A16SFloat const &rhs) noexcept {
54 return lhs.v == rhs.v;
55 }
56 [[nodiscard]] tt_force_inline friend bool operator!=(R16G16B16A16SFloat const &lhs, R16G16B16A16SFloat const &rhs) noexcept {
57 return !(lhs == rhs);
58 }
59
60 [[nodiscard]] tt_force_inline friend R16G16B16A16SFloat makeTransparent(R16G16B16A16SFloat const &rhs) noexcept {
62 r.v = rhs.v;
63 std::get<3>(r.v) = 0x0000; // 0.0f
64 return r;
65 }
66};
67
68inline void fill(PixelMap<R16G16B16A16SFloat> &image, vec color) noexcept {
69 for (ssize_t y = 0; y != image.height; ++y) {
70 auto row = image[y];
71 for (ssize_t x = 0; x != image.width; ++x) {
72 row[x] = color;
73 }
74 }
75}
76
77inline void desaturate(PixelMap<R16G16B16A16SFloat> &image, float brightness) noexcept {
78 for (ssize_t y = 0; y != image.height; ++y) {
79 auto row = image[y];
80 for (ssize_t x = 0; x != image.width; ++x) {
81 row[x] = desaturate(static_cast<vec>(row[x]), brightness);
82 }
83 }
84}
85
86inline void composit(PixelMap<R16G16B16A16SFloat> &under, PixelMap<R16G16B16A16SFloat> const &over) noexcept
87{
88 tt_assert(over.height >= under.height);
89 tt_assert(over.width >= under.width);
90
91 for (ssize_t rowNr = 0; rowNr != under.height; ++rowNr) {
92 ttlet overRow = over.at(rowNr);
93 auto underRow = under.at(rowNr);
94 for (ssize_t columnNr = 0; columnNr != under.width; ++columnNr) {
95 ttlet &overPixel = overRow[columnNr];
96 auto &underPixel = underRow[columnNr];
97
98 underPixel = composit(static_cast<vec>(underPixel), static_cast<vec>(overPixel));
99 }
100 }
101}
102
103inline void composit(PixelMap<R16G16B16A16SFloat>& under, vec over, PixelMap<uint8_t> const& mask) noexcept
104{
105 tt_assert(mask.height >= under.height);
106 tt_assert(mask.width >= under.width);
107
108 auto maskPixel = vec::color(1.0f, 1.0f, 1.0f, 1.0f);
109
110 for (ssize_t rowNr = 0; rowNr != under.height; ++rowNr) {
111 ttlet maskRow = mask.at(rowNr);
112 auto underRow = under.at(rowNr);
113 for (ssize_t columnNr = 0; columnNr != under.width; ++columnNr) {
114 ttlet maskValue = maskRow[columnNr] / 255.0f;
115 maskPixel.a(maskValue);
116
117 auto& pixel = underRow[columnNr];
118 pixel = composit(pixel, over * maskPixel);
119 }
120 }
121}
122
123}
A 2D canvas of pixels.
Definition PixelMap.hpp:83
Definition R16G16B16A16SFloat.hpp:15
A 4D vector.
Definition vec.hpp:37
static tt_force_inline vec color(float r, float g, float b, float a=1.0f) noexcept
Create a color out of 3 to 4 values.
Definition vec.hpp:226
T data(T... args)
T memset(T... args)