Commit 9465d261 authored by Kenneth Moreland's avatar Kenneth Moreland

Adjust TransferFunction worklet to get color table as ExecObj argument

Previously, the `TransferFunction` worklet required you to call
`PrepareForExecution` on the `ColorTable` and give that to the
constructor. Bad things can happen if the `ColorTable` used in the
constructor does not match the device the worklet actually gets invoked
on. Change this to make the `ColorTable` a worklet argument of type
`ExecObj`. The dispatcher will automatically call
`ColorTable::PrepareForInput` and guarantee that the devices match.
parent 11996f13
......@@ -26,23 +26,6 @@ namespace vtkm
{
namespace cont
{
namespace detail
{
struct map_color_table
{
template <typename DeviceAdapter, typename ColorTable, typename... Args>
inline bool operator()(DeviceAdapter device, ColorTable&& colors, Args&&... args) const
{
vtkm::cont::Token token;
vtkm::worklet::colorconversion::TransferFunction transfer(
colors->PrepareForExecution(device, token));
vtkm::cont::Invoker invoke(device);
invoke(transfer, std::forward<Args>(args)...);
return true;
}
};
}
//---------------------------------------------------------------------------
template <typename T, typename S>
......@@ -123,14 +106,18 @@ template <typename T, typename S>
bool ColorTable::Map(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec4ui_8>& rgbaOut) const
{
return vtkm::cont::TryExecute(detail::map_color_table{}, this, values, rgbaOut);
vtkm::cont::Invoker invoke;
invoke(vtkm::worklet::colorconversion::TransferFunction{}, values, *this, rgbaOut);
return true;
}
//---------------------------------------------------------------------------
template <typename T, typename S>
bool ColorTable::Map(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec3ui_8>& rgbOut) const
{
return vtkm::cont::TryExecute(detail::map_color_table{}, this, values, rgbOut);
vtkm::cont::Invoker invoke;
invoke(vtkm::worklet::colorconversion::TransferFunction{}, values, *this, rgbOut);
return true;
}
//---------------------------------------------------------------------------
template <typename T, int N, typename S>
......
......@@ -439,13 +439,8 @@ public:
auto samples = vtkm::cont::make_ArrayHandle({ 0.0, 0.5, 1.0 });
vtkm::cont::ArrayHandle<vtkm::Vec4ui_8> colors;
{
vtkm::cont::Token token;
TransferFunction transfer(table.PrepareForExecution(DeviceAdapterTag{}, token));
vtkm::worklet::DispatcherMapField<TransferFunction> dispatcher(transfer);
dispatcher.SetDevice(DeviceAdapterTag());
dispatcher.Invoke(samples, colors);
}
vtkm::cont::Invoker invoke;
invoke(TransferFunction{}, samples, table, colors);
CheckColors(colors, { { 14, 28, 31, 255 }, { 21, 150, 21, 255 }, { 255, 251, 230, 255 } });
}
......
......@@ -23,28 +23,27 @@ namespace colorconversion
struct TransferFunction : public vtkm::worklet::WorkletMapField
{
TransferFunction(const vtkm::exec::ColorTable table)
: ColorTable(table)
{
}
using ControlSignature = void(FieldIn in, FieldOut color);
using ExecutionSignature = void(_1, _2);
using ControlSignature = void(FieldIn in, ExecObject colorTable, FieldOut color);
using ExecutionSignature = void(_1, _2, _3);
template <typename T>
VTKM_EXEC void operator()(const T& in, vtkm::Vec3ui_8& output) const
VTKM_EXEC void operator()(const T& in,
const vtkm::exec::ColorTable& colorTable,
vtkm::Vec3ui_8& output) const
{
vtkm::Vec<float, 3> rgb = this->ColorTable.MapThroughColorSpace(static_cast<double>(in));
vtkm::Vec<float, 3> rgb = colorTable.MapThroughColorSpace(static_cast<double>(in));
output[0] = colorconversion::ColorToUChar(rgb[0]);
output[1] = colorconversion::ColorToUChar(rgb[1]);
output[2] = colorconversion::ColorToUChar(rgb[2]);
}
template <typename T>
VTKM_EXEC void operator()(const T& in, vtkm::Vec4ui_8& output) const
VTKM_EXEC void operator()(const T& in,
const vtkm::exec::ColorTable& colorTable,
vtkm::Vec4ui_8& output) const
{
vtkm::Vec<float, 3> rgb = this->ColorTable.MapThroughColorSpace(static_cast<double>(in));
float alpha = this->ColorTable.MapThroughOpacitySpace(static_cast<double>(in));
vtkm::Vec<float, 3> rgb = colorTable.MapThroughColorSpace(static_cast<double>(in));
float alpha = colorTable.MapThroughOpacitySpace(static_cast<double>(in));
output[0] = colorconversion::ColorToUChar(rgb[0]);
output[1] = colorconversion::ColorToUChar(rgb[1]);
output[2] = colorconversion::ColorToUChar(rgb[2]);
......@@ -52,23 +51,25 @@ struct TransferFunction : public vtkm::worklet::WorkletMapField
}
template <typename T>
VTKM_EXEC void operator()(const T& in, vtkm::Vec<float, 3>& output) const
VTKM_EXEC void operator()(const T& in,
const vtkm::exec::ColorTable& colorTable,
vtkm::Vec3f_32& output) const
{
output = this->ColorTable.MapThroughColorSpace(static_cast<double>(in));
output = colorTable.MapThroughColorSpace(static_cast<double>(in));
}
template <typename T>
VTKM_EXEC void operator()(const T& in, vtkm::Vec<float, 4>& output) const
VTKM_EXEC void operator()(const T& in,
const vtkm::exec::ColorTable& colorTable,
vtkm::Vec4f_32& output) const
{
vtkm::Vec<float, 3> rgb = this->ColorTable.MapThroughColorSpace(static_cast<double>(in));
float alpha = this->ColorTable.MapThroughOpacitySpace(static_cast<double>(in));
vtkm::Vec3f_32 rgb = colorTable.MapThroughColorSpace(static_cast<vtkm::Float64>(in));
vtkm::Float32 alpha = colorTable.MapThroughOpacitySpace(static_cast<vtkm::Float64>(in));
output[0] = rgb[0];
output[1] = rgb[1];
output[2] = rgb[2];
output[3] = alpha;
}
const vtkm::exec::ColorTable ColorTable;
};
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment