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

#pragma once

#include "Instance.ih"
#include "OSPCommon.ih"
#include "rkcommon/math/AffineSpace.ih"
#include "rkcommon/math/box.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

struct VolumetricModel;

// Volume hit structure ///////////////////////////////////////////////////////

struct VolumeInterval
{
  VolumetricModel *volumetricModel;
  Instance *instance;
  range1f interval;
  unsigned int primID;
  unsigned int geomID;
  unsigned int instID;
};

inline void initVolumeInterval(VolumeInterval &hit)
{
  hit.volumetricModel = NULL;
  hit.interval.lower = inf;
  hit.interval.upper = -hit.interval.lower;
}

inline bool hasInterval(const VolumeInterval &vi)
{
  return !isEmpty(vi.interval);
}

struct VolumeIntervals
{
  unsigned int numVolumeIntervals;
  uniform unsigned int numAllocated;
  varying VolumeInterval *uniform intervals;
};

#ifndef OSPRAY_TARGET_SYCL
inline void allocVolumeIntervals(VolumeIntervals &intervals)
{
  intervals.numVolumeIntervals = 0;
  intervals.numAllocated = 0;
  intervals.intervals = (varying VolumeInterval * uniform) pushTLS(0);
}

inline void freeVolumeIntervals(VolumeIntervals &intervals)
{
  popTLS(intervals.intervals);
  intervals.intervals = NULL;
}
#endif

struct RayQueryContextVolume
{
  RTCRayQueryContext ectx;
  varying VolumeIntervals *intervals;
};

inline void InitRayQueryContextVolume(RayQueryContextVolume *uniform context,
    varying VolumeIntervals *uniform intervals)
{
  rtcInitRayQueryContext(&context->ectx);
  context->intervals = intervals;
}

OSPRAY_END_ISPC_NAMESPACE
#endif
