VTK-m  2.0
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::GetLinearFilteredColor
VTKM_EXEC ColorType GetLinearFilteredColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:146
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Height
vtkm::Id Height
Definition: Texture2D.h:195
vtkm::rendering::Texture2D::SetFilterMode
VTKM_CONT void SetFilterMode(TextureFilterMode filterMode)
Definition: Texture2D.h:77
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm::rendering::Texture2D::Texture2D
VTKM_CONT Texture2D()
Definition: Texture2D.h:50
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Round
VTKM_EXEC_CONT vtkm::Float32 Round(vtkm::Float32 x)
Round x to the nearest integral value.
Definition: Math.h:2263
vtkm::rendering::Texture2D::Texture2DSampler::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:239
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::rendering::Texture2D::GetExecObjectFactory
VTKM_CONT Texture2DSampler GetExecObjectFactory() const
Definition: Texture2D.h:85
vtkm::rendering::Texture2D::Texture2DSampler::Data
TextureDataHandle Data
Definition: Texture2D.h:237
vtkm::rendering::TextureFilterMode::NearestNeighbour
@ NearestNeighbour
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Width
vtkm::Id Width
Definition: Texture2D.h:194
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
VTKM_CONT Texture2DSamplerExecutionObject(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode, vtkm::cont::Token &token)
Definition: Texture2D.h:104
vtkm::rendering::TextureWrapMode
TextureWrapMode
Definition: Texture2D.h:28
vtkm::rendering::Texture2D::Texture2DSampler::Texture2DSampler
VTKM_CONT Texture2DSampler(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode)
Definition: Texture2D.h:212
vtkm::rendering::Texture2D< 1 >::TextureDataHandle
typename vtkm::cont::ArrayHandle< vtkm::UInt8 > TextureDataHandle
Definition: Texture2D.h:38
vtkm::Clamp
VTKM_EXEC_CONT vtkm::Float32 Clamp(vtkm::Float32 x, vtkm::Float32 lo, vtkm::Float32 hi)
Clamp x to the given range.
Definition: Math.h:1815
vtkm::rendering::Texture2D::ColorType
vtkm::Vec< vtkm::Float32, NumComponents > ColorType
Definition: Texture2D.h:39
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
UV_BOUNDS_CHECK
#define UV_BOUNDS_CHECK(u, v, NoneType)
Definition: Texture2D.h:43
vtkm::Floor
VTKM_EXEC_CONT vtkm::Float32 Floor(vtkm::Float32 x)
Round x to the largest integer value not greater than x.
Definition: Math.h:2204
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::GetWrapMode
VTKM_CONT TextureWrapMode GetWrapMode() const
Definition: Texture2D.h:80
vtkm::rendering::TextureFilterMode
TextureFilterMode
Definition: Texture2D.h:22
vtkm::rendering::Texture2D::Width
vtkm::Id Width
Definition: Texture2D.h:243
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::rendering::Texture2D::Texture2DSampler::PrepareForExecution
VTKM_CONT Texture2DSamplerExecutionObject< Device > PrepareForExecution(Device, vtkm::cont::Token &token) const
Definition: Texture2D.h:226
vtkm::rendering::Texture2D::Texture2D
VTKM_CONT Texture2D(vtkm::Id width, vtkm::Id height, const TextureDataHandle &data)
Definition: Texture2D.h:57
vtkm::rendering::Texture2D::Data
TextureDataHandle Data
Definition: Texture2D.h:245
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNearestNeighbourFilteredColor
VTKM_EXEC ColorType GetNearestNeighbourFilteredColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:138
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
VTKM_CONT Texture2DSamplerExecutionObject()
Definition: Texture2D.h:97
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNextCoords
VTKM_EXEC void GetNextCoords(vtkm::Id x, vtkm::Id y, vtkm::Id &xn, vtkm::Id &yn) const
Definition: Texture2D.h:178
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColorAtCoords
VTKM_EXEC ColorType GetColorAtCoords(vtkm::Id x, vtkm::Id y) const
Definition: Texture2D.h:166
vtkm::rendering::Texture2D::SetWrapMode
VTKM_CONT void SetWrapMode(TextureWrapMode wrapMode)
Definition: Texture2D.h:83
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:198
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:767
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
Definition: Types.h:154
vtkm::rendering::Texture2D::Texture2DSampler
Definition: Texture2D.h:201
vtkm::rendering::TextureWrapMode::Clamp
@ Clamp
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColor
VTKM_EXEC ColorType GetColor(vtkm::Float32 u, vtkm::Float32 v) const
Definition: Texture2D.h:119
vtkm::rendering::Texture2D::Texture2DSampler::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:238
vtkm::rendering::Texture2D::GetFilterMode
VTKM_CONT TextureFilterMode GetFilterMode() const
Definition: Texture2D.h:74
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::TextureFilterMode::Linear
@ Linear
vtkm::rendering::Texture2D::Texture2DSampler::Texture2DSampler
VTKM_CONT Texture2DSampler()
Definition: Texture2D.h:205
vtkm::rendering::Texture2D::Height
vtkm::Id Height
Definition: Texture2D.h:244
vtkm::rendering::Texture2D::Texture2DSamplerExecutionObject::Data
TextureExecPortal Data
Definition: Texture2D.h:196
vtkm::rendering::Texture2D::IsValid
VTKM_CONT bool IsValid() const
Definition: Texture2D.h:71