// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "math/random.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

// Hash function used in noise functions
inline unsigned int noiseHash(const vec3ui &v, unsigned int seed)
{
  unsigned int hash = seed;
  hash = MurmurHash3_mix(hash, v.x);
  hash = MurmurHash3_mix(hash, v.y);
  hash = MurmurHash3_mix(hash, v.z);
  hash ^= 3 * 4;
  hash = MurmurHash3_finalize(hash);
  return hash;
}

inline unsigned int cellNoise1ui(const vec3f &v)
{
  const vec3ui key = intbits(floor(v));
  return noiseHash(key, 0x537e6612);
}

inline float cellNoise1f(const vec3f &v)
{
  const vec3ui key = intbits(floor(v));
  return to_float_unorm(noiseHash(key, 0x537e6612));
}

inline vec3f cellNoise3f(const vec3f &v)
{
  const vec3ui key = intbits(floor(v));
  return make_vec3f(to_float_unorm(noiseHash(key, 0xf7acd0ce)),
      to_float_unorm(noiseHash(key, 0x6e2bf582)),
      to_float_unorm(noiseHash(key, 0xc6ae6d0d)));
}

OSPRAY_END_ISPC_NAMESPACE
