VTK-m  2.0
ChannelBufferOperations.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtkm_rendering_raytracing_ChannelBuffer_Operations_h
11 #define vtkm_rendering_raytracing_ChannelBuffer_Operations_h
12 
13 #include <vtkm/Types.h>
14 
15 #include <vtkm/cont/Algorithm.h>
17 
20 
23 
24 namespace vtkm
25 {
26 namespace rendering
27 {
28 namespace raytracing
29 {
30 namespace detail
31 {
32 
33 class CompactBuffer : public vtkm::worklet::WorkletMapField
34 {
35 protected:
36  const vtkm::Id NumChannels; // the number of channels in the buffer
37 
38 public:
39  VTKM_CONT
40  CompactBuffer(const vtkm::Int32 numChannels)
41  : NumChannels(numChannels)
42  {
43  }
44  using ControlSignature = void(FieldIn, WholeArrayIn, FieldIn, WholeArrayOut);
45  using ExecutionSignature = void(_1, _2, _3, _4, WorkIndex);
46  template <typename InBufferPortalType, typename OutBufferPortalType>
47  VTKM_EXEC void operator()(const vtkm::UInt8& mask,
48  const InBufferPortalType& inBuffer,
49  const vtkm::Id& offset,
50  OutBufferPortalType& outBuffer,
51  const vtkm::Id& index) const
52  {
53  if (mask == 0)
54  {
55  return;
56  }
57  vtkm::Id inIndex = index * NumChannels;
58  vtkm::Id outIndex = offset * NumChannels;
59  for (vtkm::Int32 i = 0; i < NumChannels; ++i)
60  {
61  BOUNDS_CHECK(inBuffer, inIndex + i);
62  BOUNDS_CHECK(outBuffer, outIndex + i);
63  outBuffer.Set(outIndex + i, inBuffer.Get(inIndex + i));
64  }
65  }
66 }; //class Compact
67 
68 class InitBuffer : public vtkm::worklet::WorkletMapField
69 {
70 protected:
71  vtkm::Int32 NumChannels;
72 
73 public:
74  VTKM_CONT
75  InitBuffer(const vtkm::Int32 numChannels)
76  : NumChannels(numChannels)
77  {
78  }
79  using ControlSignature = void(FieldOut, WholeArrayIn);
80  using ExecutionSignature = void(_1, _2, WorkIndex);
81  template <typename ValueType, typename PortalType>
82  VTKM_EXEC void operator()(ValueType& outValue,
83  const PortalType& source,
84  const vtkm::Id& index) const
85  {
86  outValue = source.Get(index % NumChannels);
87  }
88 }; //class InitBuffer
89 
90 
91 } // namespace detail
92 
94 {
95 public:
96  template <typename Precision>
97  static void Compact(ChannelBuffer<Precision>& buffer,
99  const vtkm::Id& newSize)
100  {
102  offsets.Allocate(buffer.Size);
104  vtkm::cont::Algorithm::ScanExclusive(castedMasks, offsets);
105 
106  vtkm::cont::ArrayHandle<Precision> compactedBuffer;
107  compactedBuffer.Allocate(newSize * buffer.NumChannels);
108 
110  detail::CompactBuffer(buffer.NumChannels));
111  dispatcher.Invoke(masks, buffer.Buffer, offsets, compactedBuffer);
112  buffer.Buffer = compactedBuffer;
113  buffer.Size = newSize;
114  }
115 
116  template <typename Device, typename Precision>
118  vtkm::cont::ArrayHandle<Precision> sourceSignature,
119  Device)
120  {
121  if (sourceSignature.GetNumberOfValues() != buffer.NumChannels)
122  {
123  std::string msg = "ChannelBuffer: number of bins in sourse signature must match NumChannels";
124  throw vtkm::cont::ErrorBadValue(msg);
125  }
127  detail::InitBuffer(buffer.NumChannels));
128  initBufferDispatcher.SetDevice(Device());
129  initBufferDispatcher.Invoke(buffer.Buffer, sourceSignature);
130  }
131 
132  template <typename Device, typename Precision>
133  static void InitConst(ChannelBuffer<Precision>& buffer, const Precision value, Device)
134  {
135  vtkm::cont::ArrayHandleConstant<Precision> valueHandle(value, buffer.GetBufferLength());
136  vtkm::cont::Algorithm::Copy(Device(), valueHandle, buffer.Buffer);
137  }
138 };
139 }
140 }
141 } // namespace vtkm::rendering::raytracing
142 #endif
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
ArrayHandleCast.h
Types.h
vtkm::rendering::raytracing::ChannelBuffer::GetBufferLength
vtkm::Id GetBufferLength() const
WorkletMapField.h
vtkm::cont::ArrayHandle::Allocate
VTKM_CONT void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:465
ChannelBuffer.h
vtkm::rendering::raytracing::ChannelBuffer::Size
vtkm::Id Size
Definition: ChannelBuffer.h:49
vtkm::cont::Algorithm::Copy
static VTKM_CONT bool Copy(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, vtkm::cont::ArrayHandle< U, COut > &output)
Definition: Algorithm.h:410
vtkm::rendering::raytracing::ChannelBuffer
Mananges a buffer that contains many channels per value (e.g., RGBA values).
Definition: ChannelBuffer.h:45
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
DispatcherMapField.h
vtkm::rendering::raytracing::ChannelBufferOperations::InitChannels
static void InitChannels(ChannelBuffer< Precision > &buffer, vtkm::cont::ArrayHandle< Precision > sourceSignature, Device)
Definition: ChannelBufferOperations.h:117
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
Algorithm.h
vtkm::cont::Algorithm::ScanExclusive
static VTKM_CONT T ScanExclusive(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, vtkm::cont::ArrayHandle< T, COut > &output)
Definition: Algorithm.h:816
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::ArrayHandleConstant
An array handle with a constant value.
Definition: ArrayHandleConstant.h:63
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::rendering::raytracing::ChannelBufferOperations::InitConst
static void InitConst(ChannelBuffer< Precision > &buffer, const Precision value, Device)
Definition: ChannelBufferOperations.h:133
BOUNDS_CHECK
#define BOUNDS_CHECK(HANDLE, INDEX)
Definition: RayTracingTypeDefs.h:31
vtkm::rendering::raytracing::ChannelBuffer::Buffer
vtkm::cont::ArrayHandle< Precision > Buffer
Definition: ChannelBuffer.h:54
vtkm::rendering::raytracing::ChannelBufferOperations::Compact
static void Compact(ChannelBuffer< Precision > &buffer, vtkm::cont::ArrayHandle< UInt8 > &masks, const vtkm::Id &newSize)
Definition: ChannelBufferOperations.h:97
vtkm::cont::ErrorBadValue
This class is thrown when a VTKm function or method encounters an invalid value that inhibits progres...
Definition: ErrorBadValue.h:25
vtkm::cont::ArrayHandleCast
Cast the values of an array to the specified type, on demand.
Definition: ArrayHandleCast.h:141
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::rendering::raytracing::ChannelBuffer::NumChannels
vtkm::Int32 NumChannels
Definition: ChannelBuffer.h:48
Worklets.h
vtkm::rendering::raytracing::ChannelBufferOperations
Definition: ChannelBufferOperations.h:93
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38