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

#pragma once

#include "texture2d.isph"
#include "ospray/math/AffineSpace.ih"


//! Texture2D including coordinate transformation, plus helpers

struct TextureParam
{
  uniform Texture2D* map;
  affine2f xform;
};

inline uniform TextureParam make_TextureParam(uniform Texture2D * uniform tex, const uniform affine2f &xform)
{
  uniform TextureParam t;
  t.map = tex;
  t.xform = xform;
  return t;
}

inline bool valid(const uniform TextureParam uniform &tex)
{
  return tex.map;
}

inline float get1f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv)
{
  return get1f(tex.map, tex.xform * uv);
}

inline float get1f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv,
                   const varying float defaultValue)
{
  if (tex.map == NULL)
    return defaultValue;
  return get1f(tex.map, tex.xform * uv);
}

inline Vec3f get3f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv)
{
  return get3f(tex.map, tex.xform * uv);
}

inline Vec3f get3f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv,
                   const varying Vec3f defaultValue)
{
  if (tex.map == NULL)
    return defaultValue;
  return get3f(tex.map, tex.xform * uv);
}

inline Vec4f get4f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv)
{
  return get4f(tex.map, tex.xform * uv);
}

inline Vec4f get4f(const uniform TextureParam uniform &tex,
                   const varying Vec2f uv,
                   const varying Vec4f defaultValue)
{
  if (tex.map == NULL)
    return defaultValue;
  return get4f(tex.map, tex.xform * uv);
}

