Skip to content

Commit

Permalink
move more utility functions from helide into helium
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffamstutz committed Feb 29, 2024
1 parent d6b6489 commit bb80385
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
20 changes: 2 additions & 18 deletions libs/helide/frame/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ namespace helide {

// Helper functions ///////////////////////////////////////////////////////////

static uint32_t cvt_uint32(const float &f)
{
return static_cast<uint32_t>(255.f * std::clamp(f, 0.f, 1.f));
}

static uint32_t cvt_uint32(const float4 &v)
{
return (cvt_uint32(v.x) << 0) | (cvt_uint32(v.y) << 8)
| (cvt_uint32(v.z) << 16) | (cvt_uint32(v.w) << 24);
}

static uint32_t cvt_uint32_srgb(const float4 &v)
{
return cvt_uint32(float4(toneMap(v.x), toneMap(v.y), toneMap(v.z), v.w));
}

template <typename I, typename FUNC>
static void serial_for(I size, FUNC &&f)
{
Expand Down Expand Up @@ -263,12 +247,12 @@ void Frame::writeSample(int x, int y, const PixelSample &s)
auto *color = m_pixelBuffer.data() + (idx * m_perPixelBytes);
switch (m_colorType) {
case ANARI_UFIXED8_VEC4: {
auto c = cvt_uint32(s.color);
auto c = helium::math::cvt_uint32(s.color);
std::memcpy(color, &c, sizeof(c));
break;
}
case ANARI_UFIXED8_RGBA_SRGB: {
auto c = cvt_uint32_srgb(s.color);
auto c = helium::math::cvt_uint32_srgb(s.color);
std::memcpy(color, &c, sizeof(c));
break;
}
Expand Down
23 changes: 17 additions & 6 deletions libs/helium/helium_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ inline float toneMap(float v)
return v;
}

inline uint32_t cvt_uint32(const float &f)
{
return static_cast<uint32_t>(255.f * std::clamp(f, 0.f, 1.f));
}

inline uint32_t cvt_uint32(const anari::math::float4 &v)
{
return (cvt_uint32(v.x) << 0) | (cvt_uint32(v.y) << 8)
| (cvt_uint32(v.z) << 16) | (cvt_uint32(v.w) << 24);
}

inline uint32_t cvt_uint32_srgb(const anari::math::float4 &v)
{
return cvt_uint32(
anari::math::float4(toneMap(v.x), toneMap(v.y), toneMap(v.z), v.w));
}

struct Interpolant
{
int32_t lower;
Expand All @@ -126,12 +143,6 @@ inline Interpolant getInterpolant(float in, size_t size, bool texOffset = false)
return {low, high, frac};
}

template <typename T, typename U>
inline T lerp(const T &x, const T &y, const U &a)
{
return static_cast<T>(x * (U(1) - a) + y * a);
}

template <typename T>
inline void accumulateValue(T &a, const T &b, float interp)
{
Expand Down

0 comments on commit bb80385

Please sign in to comment.