VTK-m  2.2
Texture2D.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 
11 #ifndef vtk_m_rendering_Texture2D_h
12 #define vtk_m_rendering_Texture2D_h
13 
14 #include <vtkm/cont/ArrayHandle.h>
16 
17 namespace vtkm
18 {
19 namespace rendering
20 {
21 
23 {
25  Linear,
26 }; // enum TextureFilterMode
27 
28 enum class TextureWrapMode
29 {
30  Clamp,
31  Repeat,
32 }; // enum TextureWrapMode
33 
34 template <vtkm::IdComponent NumComponents>
35 class Texture2D
36 {
37 public:
40 
41  class Texture2DSampler;
42 
43 #define UV_BOUNDS_CHECK(u, v, NoneType) \
44  if (u < 0.0f || u > 1.0f || v < 0.0f || v > 1.0f) \
45  { \
46  return NoneType(); \
47  }
48 
49  VTKM_CONT
51  : Width(0)
52  , Height(0)
53  {
54  }
55 
56  VTKM_CONT
57  Texture2D(vtkm::Id width, vtkm::Id height, const TextureDataHandle& data)
58  : Width(width)
59  , Height(height)
62  {
63  VTKM_ASSERT(data.GetNumberOfValues() == (Width * Height * NumComponents));
64  // We do not know the lifetime of the underlying data source of input `data`. Since it might
65  // be from a shallow copy of the data source, we make a deep copy of the input data and keep
66  // it's portal. The copy operation is very fast.
67  this->Data.DeepCopyFrom(data);
68  }
69 
70  VTKM_CONT
71  bool IsValid() const { return Width > 0 && Height > 0; }
72 
73  VTKM_CONT
74  TextureFilterMode GetFilterMode() const { return this->FilterMode; }
75 
76  VTKM_CONT
77  void SetFilterMode(TextureFilterMode filterMode) { this->FilterMode = filterMode; }
78 
79  VTKM_CONT
80  TextureWrapMode GetWrapMode() const { return this->WrapMode; }
81 
82  VTKM_CONT
83  void SetWrapMode(TextureWrapMode wrapMode) { this->WrapMode = wrapMode; }
84 
85  VTKM_CONT Texture2DSampler GetExecObjectFactory() const
86  {
87  return Texture2DSampler(Width, Height, Data, FilterMode, WrapMode);
88  }
89 
90  template <typename Device>
92  {
93  public:
94  using TextureExecPortal = typename TextureDataHandle::ReadPortalType;
95 
96  VTKM_CONT
98  : Width(0)
99  , Height(0)
100  {
101  }
102 
103  VTKM_CONT
105  vtkm::Id height,
106  const TextureDataHandle& data,
107  TextureFilterMode filterMode,
108  TextureWrapMode wrapMode,
109  vtkm::cont::Token& token)
110  : Width(width)
111  , Height(height)
112  , Data(data.PrepareForInput(Device(), token))
113  , FilterMode(filterMode)
114  , WrapMode(wrapMode)
115  {
116  }
117 
118  VTKM_EXEC
120  {
121  v = 1.0f - v;
122  UV_BOUNDS_CHECK(u, v, ColorType);
123  switch (FilterMode)
124  {
127 
129  return GetLinearFilteredColor(u, v);
130 
131  default:
132  return ColorType();
133  }
134  }
135 
136  private:
137  VTKM_EXEC
139  {
140  vtkm::Id x = static_cast<vtkm::Id>(vtkm::Round(u * static_cast<vtkm::Float32>(Width - 1)));
141  vtkm::Id y = static_cast<vtkm::Id>(vtkm::Round(v * static_cast<vtkm::Float32>(Height - 1)));
142  return GetColorAtCoords(x, y);
143  }
144 
145  VTKM_EXEC
147  {
148  u = u * static_cast<vtkm::Float32>(Width) - 0.5f;
149  v = v * static_cast<vtkm::Float32>(Height) - 0.5f;
150  vtkm::Id x = static_cast<vtkm::Id>(vtkm::Floor(u));
151  vtkm::Id y = static_cast<vtkm::Id>(vtkm::Floor(v));
152  vtkm::Float32 uRatio = u - static_cast<vtkm::Float32>(x);
153  vtkm::Float32 vRatio = v - static_cast<vtkm::Float32>(y);
154  vtkm::Float32 uOpposite = 1.0f - uRatio;
155  vtkm::Float32 vOpposite = 1.0f - vRatio;
156  vtkm::Id xn, yn;
157  GetNextCoords(x, y, xn, yn);
158  ColorType c1 = GetColorAtCoords(x, y);
159  ColorType c2 = GetColorAtCoords(xn, y);
160  ColorType c3 = GetColorAtCoords(x, yn);
161  ColorType c4 = GetColorAtCoords(xn, yn);
162  return (c1 * uOpposite + c2 * uRatio) * vOpposite + (c3 * uOpposite + c4 * uRatio) * vRatio;
163  }
164 
165  VTKM_EXEC
167  {
168  vtkm::Id idx = (y * Width + x) * NumComponents;
169  ColorType color;
170  for (vtkm::IdComponent i = 0; i < NumComponents; ++i)
171  {
172  color[i] = Data.Get(idx + i) / 255.0f;
173  }
174  return color;
175  }
176 
177  VTKM_EXEC
178  inline void GetNextCoords(vtkm::Id x, vtkm::Id y, vtkm::Id& xn, vtkm::Id& yn) const
179  {
180  switch (WrapMode)
181  {
183  xn = (x + 1) < Width ? (x + 1) : x;
184  yn = (y + 1) < Height ? (y + 1) : y;
185  break;
187  default:
188  xn = (x + 1) % Width;
189  yn = (y + 1) % Height;
190  break;
191  }
192  }
193 
199  };
200 
202  {
203  public:
204  VTKM_CONT
206  : Width(0)
207  , Height(0)
208  {
209  }
210 
211  VTKM_CONT
213  vtkm::Id height,
214  const TextureDataHandle& data,
215  TextureFilterMode filterMode,
216  TextureWrapMode wrapMode)
217  : Width(width)
218  , Height(height)
219  , Data(data)
220  , FilterMode(filterMode)
221  , WrapMode(wrapMode)
222  {
223  }
224 
225  template <typename Device>
227  Device,
228  vtkm::cont::Token& token) const
229  {
231  this->Width, this->Height, this->Data, this->FilterMode, this->WrapMode, token);
232  }
233 
234  private:
240  }; // class Texture2DSampler
241 
242 private:
248 }; // class Texture2D
249 }
250 } // namespace vtkm::rendering
251 
252 #endif // vtk_m_rendering_Texture2D_h
vtkm::cont::ArrayHandle< vtkm::UInt8 >
ArrayHandle.h
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Height
vtkm::Id Height
Definition: Texture2D.h:195
vtkm::Floor
vtkm::Float32 Floor(vtkm::Float32 x)
Definition: Math.h:2230
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColorAtCoords
ColorType GetColorAtCoords(vtkm::Id x, vtkm::Id y) const
Definition: Texture2D.h:166
vtkm::Clamp
vtkm::Float32 Clamp(vtkm::Float32 x, vtkm::Float32 lo, vtkm::Float32 hi)
Definition: Math.h:1827
vtkm::rendering::Texture2D::Texture2DSampler::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:239
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::rendering::Texture2D::Texture2DSampler::Data
TextureDataHandle Data
Definition: Texture2D.h:237
vtkm::rendering::Texture2D::SetFilterMode
void SetFilterMode(TextureFilterMode filterMode)
Definition: Texture2D.h:77
vtkm::rendering::TextureFilterMode::NearestNeighbour
@ NearestNeighbour
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Width
vtkm::Id Width
Definition: Texture2D.h:194
vtkm::rendering::Texture2D::GetFilterMode
TextureFilterMode GetFilterMode() const
Definition: Texture2D.h:74
vtkm::rendering::TextureWrapMode
TextureWrapMode
Definition: Texture2D.h:28
vtkm::rendering::Texture2D::GetWrapMode
TextureWrapMode GetWrapMode() const
Definition: Texture2D.h:80
vtkm::rendering::Texture2D< 1 >::TextureDataHandle
typename vtkm::cont::ArrayHandle< vtkm::UInt8 > TextureDataHandle
Definition: Texture2D.h:38
vtkm::rendering::Texture2D::ColorType
vtkm::Vec< vtkm::Float32, NumComponents > ColorType
Definition: Texture2D.h:39
vtkm::rendering::Texture2D::Texture2D
Texture2D(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data)
Definition: Texture2D.h:57
UV_BOUNDS_CHECK
#define UV_BOUNDS_CHECK(u, v, NoneType)
Definition: Texture2D.h:43
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject
Definition: Texture2D.h:91
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::rendering::Texture2D::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:247
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
Texture2DSamplerExecutionObject()
Definition: Texture2D.h:97
vtkm::rendering::Texture2D::Texture2DSampler::PrepareForExecution
Texture2DSamplerExecutionObject< Device > PrepareForExecution(Device, vtkm::cont::Token &token) const
Definition: Texture2D.h:226
vtkm::rendering::Texture2D::Texture2DSampler::Texture2DSampler
Texture2DSampler()
Definition: Texture2D.h:205
vtkm::rendering::TextureFilterMode
TextureFilterMode
Definition: Texture2D.h:22
vtkm::Round
vtkm::Float32 Round(vtkm::Float32 x)
Definition: Math.h:2291
vtkm::rendering::Texture2D::Width
vtkm::Id Width
Definition: Texture2D.h:243
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::rendering::Texture2D::Data
TextureDataHandle Data
Definition: Texture2D.h:245
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNextCoords
void GetNextCoords(vtkm::Id x, vtkm::Id y, vtkm::Id &xn, vtkm::Id &yn) const
Definition: Texture2D.h:178
vtkm::rendering::Texture2D::SetWrapMode
void SetWrapMode(TextureWrapMode wrapMode)
Definition: Texture2D.h:83
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
Texture2DSamplerExecutionObject(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode, vtkm::cont::Token &token)
Definition: Texture2D.h:104
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:198
vtkm::rendering::Texture2D::IsValid
bool IsValid() const
Definition: Texture2D.h:71
vtkm::cont::ExecutionObjectBase
Base ExecutionObjectBase for execution objects to inherit from so that you can use an arbitrary objec...
Definition: ExecutionObjectBase.h:31
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::rendering::Texture2D::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:246
vtkm::rendering::Texture2D::Texture2DSampler::Height
vtkm::Id Height
Definition: Texture2D.h:236
vtkm::rendering::TextureWrapMode::Repeat
@ Repeat
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::TextureExecPortal
typename TextureDataHandle::ReadPortalType TextureExecPortal
Definition: Texture2D.h:94
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::rendering::Texture2D::Texture2DSampler
Definition: Texture2D.h:201
vtkm::rendering::TextureWrapMode::Clamp
@ Clamp
vtkm::rendering::Texture2D::GetExecObjectFactory
Texture2DSampler GetExecObjectFactory() const
Definition: Texture2D.h:85
vtkm::rendering::Texture2D::Texture2DSampler::Texture2DSampler
Texture2DSampler(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode)
Definition: Texture2D.h:212
vtkm::rendering::Texture2D::Texture2DSampler::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:238
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetLinearFilteredColor
ColorType GetLinearFilteredColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:146
vtkm::rendering::Texture2D
Definition: Texture2D.h:35
vtkm::rendering::Texture2D::Texture2DSampler::Width
vtkm::Id Width
Definition: Texture2D.h:235
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:197
ExecutionObjectBase.h
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNearestNeighbourFilteredColor
ColorType GetNearestNeighbourFilteredColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:138
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColor
ColorType GetColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:119
vtkm::rendering::TextureFilterMode::Linear
@ Linear
vtkm::rendering::Texture2D::Texture2D
Texture2D()
Definition: Texture2D.h:50
vtkm::rendering::Texture2D::Height
vtkm::Id Height
Definition: Texture2D.h:244
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Data
TextureExecPortal Data
Definition: Texture2D.h:196