Utils.h#

Contains several definitions used throughout the PowerVR Framework.

Includes#

  • array

  • cassert

  • cstdint

  • cstring

  • glm.h (glm.h)

Included By#

Namespaces#

Functions#

Source Code#

#pragma once

#include <cstdint>
#include <array>
#include <cassert>
#include <cstring>
#ifndef PVRCORE_NO_GLM
#include "glm.h"
#endif

namespace pvr {
namespace utils {

inline uint32_t packRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { return (static_cast<uint32_t>(((a) << 24) | ((b) << 16) | ((g) << 8) | (r))); }

inline uint32_t packRGBA(float r, float g, float b, float a)
{
    return packRGBA(static_cast<uint8_t>(r * 255), static_cast<uint8_t>(g * 255), static_cast<uint8_t>(b * 255), static_cast<uint8_t>(a * 255));
}

template<typename Toutput_, typename Tinput_>
Toutput_ reinterpretBits(const Tinput_& value)
{
    Toutput_ ret = static_cast<Toutput_>(0);
    memcpy(&ret, &value, sizeof(Tinput_));
    return ret;
}

template<typename T1_>
std::array<T1_, sizeof(T1_)> readBits(const T1_& value)
{
    std::array<char, sizeof(T1_)> retval;
    memcpy(&retval[0], &value, sizeof(T1_));
    return retval;
}

template<typename T>
inline void memSet(T& dst, int32_t i)
{
    memset(&dst, i, sizeof(T));
}

template<typename T1, typename T2>
inline void memCopy(T1& dst, const T2& src)
{
    assert(sizeof(T1) == sizeof(T2));
    memcpy(&dst, &src, sizeof(T1));
}

template<typename T1, typename T2>
inline void memCopyFromVolatile(T1& dst, const volatile T2& src)
{
    assert(sizeof(T1) == sizeof(T2));
    memcpy(&dst, &const_cast<const T2&>(src), sizeof(T1));
}

template<typename T1, typename T2>
inline void memCopyToVolatile(volatile T1& dst, const T2& src)
{
    assert(sizeof(T1) == sizeof(T2));
    memcpy(&const_cast<T1&>(dst), &src, sizeof(T1));
}

#ifndef PVRCORE_NO_GLM

inline glm::vec3 convertLRGBtoSRGB(const glm::vec3& lRGB)
{
    return glm::mix(lRGB * 12.92f, glm::pow(lRGB, glm::vec3(0.416f)) * 1.055f - 0.055f, glm::vec3(lRGB.x > 0.00313f, lRGB.y > 0.00313f, lRGB.z > 0.00313f));
}

inline glm::vec4 convertLRGBtoSRGB(const glm::vec4& lRGB) { return glm::vec4(convertLRGBtoSRGB(glm::vec3(lRGB)), lRGB.a); }

#endif
} // namespace utils
} // namespace pvr