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

#pragma once

#include "common/Embree.h"
#ifdef ISPC
#include "openvkl/openvkl.isph"
// comment break to prevent clang-format from reordering openvkl includes
#include "openvkl/device/openvkl.isph"
#else
#include "openvkl/openvkl.h"
// comment break to prevent clang-format from reordering openvkl includes
#include "openvkl/device/openvkl.h"
#endif
#include "common/FeatureFlags.ih"
#include "rkcommon/math/box.ih"
// c++ shared
#include "VolumeShared.h"

OSPRAY_BEGIN_ISPC_NAMESPACE

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

inline float Volume_getSample(const Volume *uniform volume, const vec3f &P)
{
  return vklComputeSampleV(&volume->vklSampler, &((const vkl_vec3f &)P));
}

inline vec3f Volume_getGradient(const Volume *uniform volume,
    const vec3f &P,
    const uniform FeatureFlagsHandler &ffh)
{
  const uniform FeatureFlags ff = getFeatureFlags(ffh);

  vkl_vec3f result = vklComputeGradientV(&volume->vklSampler,
      &((const vkl_vec3f &)P)
#ifdef OSPRAY_TARGET_SYCL
          ,
      0,
      0.f,
      ff.volume
#endif
  );

  // TODO: remove it once VKL no longer returns sporadic NaNs
  if (isnan(result.x))
    result.x = 1.f;
  if (isnan(result.y))
    result.y = 1.f;
  if (isnan(result.z))
    result.z = 1.f;

  return *((varying vec3f *)&result);
}

RTC_SYCL_INDIRECTLY_CALLABLE unmasked void Volume_intersect_kernel(
    RTCIntersectFunctionNArguments *uniform args);

OSPRAY_END_ISPC_NAMESPACE

#endif
