2015-08-26 16:30:15 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2015-08-26 16:30:15 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
2016-04-13 19:52:15 +00:00
|
|
|
#ifndef vtk_m_interop_TransferToOpenGL_h
|
|
|
|
#define vtk_m_interop_TransferToOpenGL_h
|
2015-08-26 16:30:15 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
2017-08-11 19:36:24 +00:00
|
|
|
#include <vtkm/cont/DeviceAdapter.h>
|
|
|
|
#include <vtkm/cont/ErrorBadValue.h>
|
2017-08-18 17:23:46 +00:00
|
|
|
#include <vtkm/cont/TryExecute.h>
|
2016-04-13 19:52:15 +00:00
|
|
|
#include <vtkm/interop/BufferState.h>
|
|
|
|
#include <vtkm/interop/internal/TransferToOpenGL.h>
|
2015-08-26 16:30:15 +00:00
|
|
|
|
2017-08-11 19:36:24 +00:00
|
|
|
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
|
|
|
|
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
|
|
|
|
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace interop
|
|
|
|
{
|
2015-08-26 16:30:15 +00:00
|
|
|
|
|
|
|
/// \brief Manages transferring an ArrayHandle to opengl .
|
|
|
|
///
|
|
|
|
/// \c TransferToOpenGL manages to transfer the contents of an ArrayHandle
|
2015-11-03 15:22:32 +00:00
|
|
|
/// to OpenGL as efficiently as possible. Will use the given \p state to determine
|
|
|
|
/// what buffer handle to use, and the type to bind the buffer handle too.
|
2018-07-25 15:11:05 +00:00
|
|
|
/// Lastly state also holds on to per backend resources that allow for efficient
|
|
|
|
/// updating to open gl.
|
2015-08-26 16:30:15 +00:00
|
|
|
///
|
|
|
|
/// This function keeps the buffer as the active buffer of the input type.
|
|
|
|
///
|
|
|
|
///
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename ValueType, class StorageTag, class DeviceAdapterTag>
|
2018-12-27 16:44:19 +00:00
|
|
|
VTKM_CONT void TransferToOpenGL(const vtkm::cont::ArrayHandle<ValueType, StorageTag>& handle,
|
2017-05-26 17:53:28 +00:00
|
|
|
BufferState& state,
|
|
|
|
DeviceAdapterTag)
|
2015-08-26 16:30:15 +00:00
|
|
|
{
|
2016-04-13 19:52:15 +00:00
|
|
|
vtkm::interop::internal::TransferToOpenGL<ValueType, DeviceAdapterTag> toGL(state);
|
2018-07-25 15:11:05 +00:00
|
|
|
toGL.Transfer(handle);
|
2015-08-26 16:30:15 +00:00
|
|
|
}
|
2017-08-11 19:36:24 +00:00
|
|
|
|
2018-07-25 15:11:05 +00:00
|
|
|
/// \brief Manages transferring an ArrayHandle to opengl .
|
2017-08-11 19:36:24 +00:00
|
|
|
///
|
2018-07-25 15:11:05 +00:00
|
|
|
/// \c TransferToOpenGL manages to transfer the contents of an ArrayHandle
|
|
|
|
/// to OpenGL as efficiently as possible. Will use the given \p state to determine
|
|
|
|
/// what buffer handle to use, and the type to bind the buffer handle too.
|
|
|
|
/// If the type of buffer hasn't been determined the transfer will use
|
|
|
|
/// deduceAndSetBufferType to do so. Lastly state also holds on to per backend resources
|
|
|
|
/// that allow for efficient updating to open gl
|
|
|
|
///
|
|
|
|
/// This function keeps the buffer as the active buffer of the input type.
|
|
|
|
///
|
|
|
|
/// This function will throw exceptions if the transfer wasn't possible
|
2017-08-11 19:36:24 +00:00
|
|
|
///
|
2018-07-25 15:11:05 +00:00
|
|
|
template <typename ValueType, typename StorageTag>
|
2018-12-27 16:44:19 +00:00
|
|
|
VTKM_CONT void TransferToOpenGL(const vtkm::cont::ArrayHandle<ValueType, StorageTag>& handle,
|
2017-08-11 19:36:24 +00:00
|
|
|
BufferState& state)
|
|
|
|
{
|
2022-09-26 15:14:08 +00:00
|
|
|
// First, try to transfer data that already exists on a device.
|
|
|
|
bool success = vtkm::cont::TryExecute([&](auto device) {
|
|
|
|
if (handle.IsOnDevice(device))
|
|
|
|
{
|
|
|
|
TransferToOpenGL(handle, state, device);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-07-25 15:11:05 +00:00
|
|
|
if (!success)
|
2017-08-18 17:23:46 +00:00
|
|
|
{
|
2022-09-26 15:14:08 +00:00
|
|
|
// Generally, we are here because the array is not already on a device
|
|
|
|
// or for some reason the transfer failed on that device. Try any device.
|
|
|
|
success = vtkm::cont::TryExecute([&](auto device) {
|
|
|
|
TransferToOpenGL(handle, state, device);
|
|
|
|
return true;
|
|
|
|
});
|
2017-08-18 17:23:46 +00:00
|
|
|
}
|
2018-07-25 15:11:05 +00:00
|
|
|
if (!success)
|
2017-08-11 19:36:24 +00:00
|
|
|
{
|
2022-09-26 15:14:08 +00:00
|
|
|
throw vtkm::cont::ErrorBadValue("Failed to transfer array to OpenGL on any device.");
|
2017-08-11 19:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 16:30:15 +00:00
|
|
|
|
2016-04-13 19:52:15 +00:00
|
|
|
#endif //vtk_m_interop_TransferToOpenGL_h
|