|
| 1 | +/* Image Filters RGB32 Brightness |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#ifdef PA_AutoDispatch_x64_19_IceLake |
| 8 | + |
| 9 | +#include <immintrin.h> |
| 10 | +#include "Kernels/ImageFilters/Kernels_ImageFilter_Basic_Routines.h" |
| 11 | +#include "Kernels_ImageFilter_RGB32_Brightness.h" |
| 12 | + |
| 13 | +//#include <iostream> |
| 14 | +//using std::cout; |
| 15 | +//using std::endl; |
| 16 | + |
| 17 | +namespace PokemonAutomation{ |
| 18 | +namespace Kernels{ |
| 19 | + |
| 20 | + |
| 21 | +namespace{ |
| 22 | + |
| 23 | +struct PartialWordMask{ |
| 24 | + __mmask16 m; |
| 25 | + |
| 26 | + PA_FORCE_INLINE PartialWordMask(size_t left) |
| 27 | + : m(((__mmask16)1 << left) - 1) |
| 28 | + {} |
| 29 | +}; |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +class ToBlackWhite_RgbBrightness_x64_AVX512{ |
| 37 | +public: |
| 38 | + static const size_t VECTOR_SIZE = 16; |
| 39 | + using Mask = PartialWordMask; |
| 40 | + |
| 41 | +public: |
| 42 | + ToBlackWhite_RgbBrightness_x64_AVX512( |
| 43 | + bool in_range_black, |
| 44 | + uint32_t min_brightness, uint32_t max_brightness |
| 45 | + ) |
| 46 | + : m_in_range_black(in_range_black ? 0xffff : 0) |
| 47 | + , m_min_brightness(_mm512_set1_epi32(min_brightness)) |
| 48 | + , m_threshold(_mm512_set1_epi32(max_brightness - min_brightness)) |
| 49 | + , m_count(_mm512_setzero_si512()) |
| 50 | + {} |
| 51 | + |
| 52 | + PA_FORCE_INLINE size_t count() const{ |
| 53 | + return _mm512_reduce_add_epi32(m_count); |
| 54 | + } |
| 55 | + |
| 56 | + PA_FORCE_INLINE void process_full(uint32_t* out, const uint32_t* in){ |
| 57 | + __m512i pixel = _mm512_loadu_si512((const __m512i*)in); |
| 58 | + __mmask16 in_range_pixels = process_word(pixel); |
| 59 | + m_count = _mm512_mask_sub_epi32(m_count, in_range_pixels, m_count, _mm512_set1_epi32(-1)); |
| 60 | + _mm512_storeu_si512((__m512i*)out, pixel); |
| 61 | + } |
| 62 | + PA_FORCE_INLINE void process_partial(uint32_t* out, const uint32_t* in, const Mask& mask){ |
| 63 | + __m512i pixel = _mm512_maskz_loadu_epi32(mask.m, in); |
| 64 | + __mmask16 in_range_pixels = process_word(pixel); |
| 65 | + in_range_pixels &= mask.m; |
| 66 | + m_count = _mm512_mask_sub_epi32(m_count, in_range_pixels, m_count, _mm512_set1_epi32(-1)); |
| 67 | + _mm512_mask_storeu_epi32(out, mask.m, pixel); |
| 68 | + } |
| 69 | + |
| 70 | +private: |
| 71 | + PA_FORCE_INLINE __mmask16 process_word(__m512i& pixel){ |
| 72 | +// cout << "asdf" << endl; |
| 73 | +// __m512i r = _mm512_and_si512(_mm512_srli_epi32(pixel, 16), _mm512_set1_epi32(0x000000ff)); |
| 74 | +// __m512i g = _mm512_and_si512(_mm512_srli_epi32(pixel, 8), _mm512_set1_epi32(0x000000ff)); |
| 75 | +// __m512i b = _mm512_and_si512(pixel, _mm512_set1_epi32(0x000000ff)); |
| 76 | + |
| 77 | + // Remove the alpha channel. |
| 78 | + pixel = _mm512_and_si512(pixel, _mm512_set1_epi32(0x00ffffff)); |
| 79 | + |
| 80 | + // Horizontally sum up 4 x 8-bit integers in each 32-bit pixel. |
| 81 | + pixel = _mm512_dpbusd_epi32(_mm512_setzero_si512(), pixel, _mm512_set1_epi8(1)); |
| 82 | + |
| 83 | + // Find the ones that are in range. |
| 84 | + pixel = _mm512_sub_epi32(pixel, m_min_brightness); |
| 85 | + __mmask16 cmp16 = _mm512_cmple_epu32_mask(pixel, m_threshold); |
| 86 | + |
| 87 | + // Set to black or white. |
| 88 | + pixel = _mm512_mask_blend_epi32(cmp16 ^ m_in_range_black, _mm512_set1_epi32(0xff000000), _mm512_set1_epi32(-1)); |
| 89 | + |
| 90 | + return cmp16; |
| 91 | + } |
| 92 | + |
| 93 | +private: |
| 94 | + const __mmask16 m_in_range_black; |
| 95 | + const __m512i m_min_brightness; |
| 96 | + const __m512i m_threshold; |
| 97 | + __m512i m_count; |
| 98 | +}; |
| 99 | +size_t to_blackwhite_rgb32_brightness_x64_AVX512VNNI( |
| 100 | + const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height, |
| 101 | + uint32_t* out, size_t out_bytes_per_row, |
| 102 | + bool in_range_black, |
| 103 | + uint32_t min_brightness, uint32_t max_brightness |
| 104 | +){ |
| 105 | + ToBlackWhite_RgbBrightness_x64_AVX512 filter(in_range_black, min_brightness, max_brightness); |
| 106 | + filter_per_pixel(in, in_bytes_per_row, width, height, filter, out, out_bytes_per_row); |
| 107 | + return filter.count(); |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +} |
| 131 | +} |
| 132 | +#endif |
0 commit comments