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

#pragma once

#include "../common/DifferentialGeometry.ih"
#include "rkcommon/math/vec.ih"
#include "texture/TextureDispatch.ih"
// c++ shared
#include "TextureShared.h"

OSPRAY_BEGIN_ISPC_NAMESPACE

inline uniform bool hasAlpha(const uniform Texture *uniform self)
{
  return (self == NULL) ? false : self->hasAlpha;
}

/*! helper function that returns the sampled value for the first
  channel of the given texture

  Right now, this function always asks the texture for all four
  channels, and then discards all but one; later implementations may
  have specialized 'get1f' methods with the texture

  \note self may NOT be NULL!
*/
inline float get1f(
    const uniform Texture *uniform self, const varying DifferentialGeometry &dg)
{
  vec4f ret = Texture_dispatch_get(self, dg);
  return ret.x;
}

/*! helper function that returns the sampled value for the first three
  channels of the given texture

  Right now, this function always asks the texture for all four
  channels, and then discards all but one; later implementations may
  have specialized 'get3f' methods with the texture

  \note self may NOT be NULL!
*/
inline vec3f get3f(
    const uniform Texture *uniform self, const varying DifferentialGeometry &dg)
{
  vec4f ret = Texture_dispatch_get(self, dg);
  return make_vec3f(ret);
}

/*! helper function that returns the sampled value of the four
  channels of the given texture.

  Note that it's up to the texture to define clearly what happens if
  we ask for four channels even if the texture has less physical
  channels.

  \note self may NOT be NULL!
*/
inline vec4f get4f(
    const uniform Texture *uniform self, const varying DifferentialGeometry &dg)
{
  return Texture_dispatch_get(self, dg);
}

/*! helper function that returns the sampled values interpreted as a normal */
inline vec3f getNormal(
    const uniform Texture *uniform self, const varying DifferentialGeometry &dg)
{
  if (self == NULL)
    return make_vec3f(0.f, 0.f, 1.f);
  else
    return Texture_dispatch_getNormal(self, dg);
}

/*! helper function: get1f() with a default value if the texture is NULL */
inline float get1f(const uniform Texture *uniform self,
    const varying DifferentialGeometry &dg,
    const varying float defaultValue)
{
  if (self == NULL)
    return defaultValue;
  else
    return get1f(self, dg);
}

/*! helper function: get3f() with a default value if the texture is NULL */
inline vec3f get3f(const uniform Texture *uniform self,
    const varying DifferentialGeometry &dg,
    const varying vec3f defaultValue)
{
  if (self == NULL)
    return defaultValue;
  else
    return get3f(self, dg);
}

/*! helper function: get4f() with a default value if the texture is NULL */
inline vec4f get4f(const uniform Texture *uniform self,
    const varying DifferentialGeometry &dg,
    const varying vec4f defaultValue)
{
  if (self == NULL)
    return defaultValue;
  else
    return get4f(self, dg);
}
OSPRAY_END_ISPC_NAMESPACE
