io/kernels/colorize.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include "chrono_batch_render/device/kernels/shared.h" | ||
| 5 | |||
| 6 | #ifndef __SLANG__ | ||
| 7 | #include "chrono_batch_render/io/kernels/encoding.h" | ||
| 8 | #endif | ||
| 9 | |||
| 10 | namespace chrono::batch_render { | ||
| 11 | |||
| 12 | #ifndef __SLANG__ | ||
| 13 | using std::abs; | ||
| 14 | using std::isfinite; | ||
| 15 | using std::log; | ||
| 16 | #endif | ||
| 17 | |||
| 18 | static const uint kColorizeThreads = 256; | ||
| 19 | static const uint kNoRange = 0xFFFFFFFFu; | ||
| 20 | static const uint kFixedMinimum = 1; | ||
| 21 | static const uint kFixedMaximum = 2; | ||
| 22 | static const uint kLogarithmic = 4; | ||
| 23 | static const uint kReversed = 8; | ||
| 24 | static const uint kCentered = 16; | ||
| 25 | static const uint kWriteBounds = 32; | ||
| 26 | static const uint kColormapGrayscale = 0; | ||
| 27 | static const uint kColormapTurbo = 1; | ||
| 28 | static const uint kColormapViridis = 2; | ||
| 29 | static const float kSmallestLogValue = 1e-5f; | ||
| 30 | |||
| 31 | struct ColorizeParams { | ||
| 32 | Ptr<uint> input; | ||
| 33 | Ptr<uint> output; | ||
| 34 | Ptr<uint> range; | ||
| 35 | Ptr<float> bounds; | ||
| 36 | uint count; | ||
| 37 | uint channels; | ||
| 38 | uint channel; | ||
| 39 | uint colormap; | ||
| 40 | uint flags; | ||
| 41 | float minimum; | ||
| 42 | float maximum; | ||
| 43 | float scale; | ||
| 44 | }; | ||
| 45 | |||
| 46 | CHRONO_BATCH_RENDER_CHECK_SIZE(ColorizeParams, 64); | ||
| 47 | |||
| 48 | struct ScalarBounds { | ||
| 49 | float low; | ||
| 50 | float high; | ||
| 51 | }; | ||
| 52 | |||
| 53 | 663686 | CHRONO_BATCH_RENDER_SHARED uint PackRgba8(uint r, uint g, uint b, uint a) { | |
| 54 | 663686 | return r | (g << 8) | (b << 16) | (a << 24); | |
| 55 | } | ||
| 56 | |||
| 57 | 1413730 | CHRONO_BATCH_RENDER_SHARED uint Unorm8(float value) { | |
| 58 | 1413730 | return uint(floor(clamp(value, 0.0f, 1.0f) * 255.0f + 0.5f)); | |
| 59 | } | ||
| 60 | |||
| 61 | 726258 | CHRONO_BATCH_RENDER_SHARED float Polynomial(float t, float c0, float c1, float c2, float c3, | |
| 62 | float c4, float c5, float c6) { | ||
| 63 | 726258 | return clamp(c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))), 0.0f, 1.0f); | |
| 64 | } | ||
| 65 | |||
| 66 | 486438 | CHRONO_BATCH_RENDER_SHARED uint ColormapRgba(uint colormap, float position) { | |
| 67 | 486438 | const float t = clamp(position, 0.0f, 1.0f); | |
| 68 | 486438 | if (colormap == kColormapTurbo) { | |
| 69 | 182508 | return PackRgba8(Unorm8(Polynomial(t, 0.13572138f, 4.61539260f, -42.66032258f, | |
| 70 | 132.13108234f, -152.94239396f, 59.28637943f, 0.0f)), | ||
| 71 | Unorm8(Polynomial(t, 0.09140261f, 2.19418839f, 4.84296658f, -14.18503333f, | ||
| 72 | 4.27729857f, 2.82956604f, 0.0f)), | ||
| 73 | Unorm8(Polynomial(t, 0.10667330f, 12.64194608f, -60.58204836f, | ||
| 74 | 110.36276771f, -89.90310912f, 27.34824973f, 0.0f)), | ||
| 75 | 182508 | 255); | |
| 76 | } | ||
| 77 | 303930 | if (colormap == kColormapViridis) { | |
| 78 | 59578 | return PackRgba8(Unorm8(Polynomial(t, 0.27772733f, 0.10509304f, -0.33086183f, -4.63423050f, | |
| 79 | 6.22826994f, 4.77638500f, -5.43545586f)), | ||
| 80 | Unorm8(Polynomial(t, 0.00540734f, 1.40461353f, 0.21484756f, -5.79910097f, | ||
| 81 | 14.17993337f, -13.74514538f, 4.64585261f)), | ||
| 82 | Unorm8(Polynomial(t, 0.33409981f, 1.38459016f, 0.09509516f, -19.33244096f, | ||
| 83 | 56.69055260f, -65.35303263f, 26.31243525f)), | ||
| 84 | 59578 | 255); | |
| 85 | } | ||
| 86 | 244352 | const uint shade = Unorm8(t); | |
| 87 | 244352 | return PackRgba8(shade, shade, shade, 255); | |
| 88 | } | ||
| 89 | |||
| 90 | 1022168 | CHRONO_BATCH_RENDER_SHARED bool ScalarShown(float value, uint flags) { | |
| 91 | 997518 | return isfinite(value) && ((flags & kLogarithmic) == 0 || value > 0.0f); | |
| 92 | } | ||
| 93 | |||
| 94 | CHRONO_BATCH_RENDER_SHARED uint OrderedBits(float value) { | ||
| 95 | const uint bits = asuint(value); | ||
| 96 | return (bits & 0x80000000u) != 0 ? ~bits : bits | 0x80000000u; | ||
| 97 | } | ||
| 98 | |||
| 99 | CHRONO_BATCH_RENDER_SHARED float OrderedFloat(uint bits) { | ||
| 100 | return asfloat((bits & 0x80000000u) != 0 ? bits & 0x7FFFFFFFu : ~bits); | ||
| 101 | } | ||
| 102 | |||
| 103 | 23 | CHRONO_BATCH_RENDER_SHARED ScalarBounds ResolveBounds(float low, float high, float minimum, | |
| 104 | float maximum, uint flags) { | ||
| 105 | 23 | ScalarBounds bounds; | |
| 106 | 23 | bounds.low = (flags & kFixedMinimum) != 0 ? minimum : low; | |
| 107 | 23 | bounds.high = (flags & kFixedMaximum) != 0 ? maximum : high; | |
| 108 | 23 | if ((flags & kCentered) != 0 && isfinite(bounds.low) && isfinite(bounds.high)) { | |
| 109 | 4 | const float extent = max(abs(bounds.low), abs(bounds.high)); | |
| 110 | 4 | bounds.low = -extent; | |
| 111 | 4 | bounds.high = extent; | |
| 112 | } | ||
| 113 | 23 | if ((flags & kLogarithmic) != 0) { | |
| 114 | 10 | bounds.low = max(bounds.low, kSmallestLogValue); | |
| 115 | } | ||
| 116 | 23 | bounds.high = max(bounds.high, bounds.low); | |
| 117 | 23 | return bounds; | |
| 118 | } | ||
| 119 | |||
| 120 | 511084 | CHRONO_BATCH_RENDER_SHARED uint ScalarRgba(float value, ScalarBounds bounds, uint flags, | |
| 121 | uint colormap) { | ||
| 122 | 511084 | if (!ScalarShown(value, flags)) { | |
| 123 | return PackRgba8(0, 0, 0, 255); | ||
| 124 | } | ||
| 125 | 486434 | const bool logarithmic = (flags & kLogarithmic) != 0; | |
| 126 | 486434 | const float low = logarithmic ? log(bounds.low) : bounds.low; | |
| 127 | 486434 | const float span = (logarithmic ? log(bounds.high) : bounds.high) - low; | |
| 128 | 486434 | const float clamped = clamp(value, bounds.low, bounds.high); | |
| 129 | 486434 | float t = (flags & kCentered) != 0 ? 0.5f : 0.0f; | |
| 130 | 486434 | if (span > 0.0f) { | |
| 131 | 486431 | t = ((logarithmic ? log(clamped) : clamped) - low) / span; | |
| 132 | } | ||
| 133 | 486434 | return ColormapRgba(colormap, (flags & kReversed) != 0 ? 1.0f - t : t); | |
| 134 | } | ||
| 135 | |||
| 136 | 63300 | CHRONO_BATCH_RENDER_SHARED uint NormalRgba(float x, float y, float z) { | |
| 137 | 63300 | if (x == 0 && y == 0 && z == 0) { | |
| 138 | return 0; | ||
| 139 | } | ||
| 140 | 63300 | return PackRgba8(Unorm8(x * 0.5f + 0.5f), Unorm8(y * 0.5f + 0.5f), Unorm8(z * 0.5f + 0.5f), | |
| 141 | 63300 | 255); | |
| 142 | } | ||
| 143 | |||
| 144 | 63305 | CHRONO_BATCH_RENDER_SHARED uint HdrRgba(float r, float g, float b, float a, float scale) { | |
| 145 | 63305 | return PackRgba8(Unorm8(EncodeSrgb(r * scale)), Unorm8(EncodeSrgb(g * scale)), | |
| 146 | 63305 | Unorm8(EncodeSrgb(b * scale)), Unorm8(a)); | |
| 147 | } | ||
| 148 | |||
| 149 | 189900 | CHRONO_BATCH_RENDER_SHARED uint MixIdWord(uint key, uint word) { | |
| 150 | 189900 | return (key ^ word) * 16777619u; | |
| 151 | } | ||
| 152 | |||
| 153 | 50643 | CHRONO_BATCH_RENDER_SHARED uint IdRgba(uint key) { | |
| 154 | 50643 | const uint scaled = ((key * 2654435769u) >> 16) * 6u; | |
| 155 | 50643 | const uint sector = scaled >> 16; | |
| 156 | 50643 | const uint f = (scaled >> 8) & 255u; | |
| 157 | 50643 | const uint high = 242u; | |
| 158 | 50643 | const uint low = 73u; | |
| 159 | 50643 | const uint rise = low + (high - low) * f / 255u; | |
| 160 | 50643 | const uint fall = high - (high - low) * f / 255u; | |
| 161 | 50643 | if (sector == 0) { | |
| 162 | 8646 | return PackRgba8(high, rise, low, 255); | |
| 163 | } | ||
| 164 | if (sector == 1) { | ||
| 165 | 8645 | return PackRgba8(fall, high, low, 255); | |
| 166 | } | ||
| 167 | if (sector == 2) { | ||
| 168 | 9882 | return PackRgba8(low, high, rise, 255); | |
| 169 | } | ||
| 170 | if (sector == 3) { | ||
| 171 | 9883 | return PackRgba8(low, fall, high, 255); | |
| 172 | } | ||
| 173 | if (sector == 4) { | ||
| 174 | 4941 | return PackRgba8(rise, low, high, 255); | |
| 175 | } | ||
| 176 | 8646 | return PackRgba8(high, low, fall, 255); | |
| 177 | } | ||
| 178 | |||
| 179 | } | ||
| 180 |