Viskores  1.0
Texture2D.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 
19 #ifndef viskores_rendering_Texture2D_h
20 #define viskores_rendering_Texture2D_h
21 
24 
25 namespace viskores
26 {
27 namespace rendering
28 {
29 
31 {
33  Linear,
34 }; // enum TextureFilterMode
35 
36 enum class TextureWrapMode
37 {
38  Clamp,
39  Repeat,
40 }; // enum TextureWrapMode
41 
42 template <viskores::IdComponent NumComponents>
43 class Texture2D
44 {
45 public:
48 
49  class Texture2DSampler;
50 
51 #define UV_BOUNDS_CHECK(u, v, NoneType) \
52  if (u < 0.0f || u > 1.0f || v < 0.0f || v > 1.0f) \
53  { \
54  return NoneType(); \
55  }
56 
59  : Width(0)
60  , Height(0)
61  {
62  }
63 
66  : Width(width)
67  , Height(height)
70  {
71  VISKORES_ASSERT(data.GetNumberOfValues() == (Width * Height * NumComponents));
72  // We do not know the lifetime of the underlying data source of input `data`. Since it might
73  // be from a shallow copy of the data source, we make a deep copy of the input data and keep
74  // it's portal. The copy operation is very fast.
75  this->Data.DeepCopyFrom(data);
76  }
77 
79  bool IsValid() const { return Width > 0 && Height > 0; }
80 
82  TextureFilterMode GetFilterMode() const { return this->FilterMode; }
83 
85  void SetFilterMode(TextureFilterMode filterMode) { this->FilterMode = filterMode; }
86 
88  TextureWrapMode GetWrapMode() const { return this->WrapMode; }
89 
91  void SetWrapMode(TextureWrapMode wrapMode) { this->WrapMode = wrapMode; }
92 
93  VISKORES_CONT Texture2DSampler GetExecObjectFactory() const
94  {
95  return Texture2DSampler(Width, Height, Data, FilterMode, WrapMode);
96  }
97 
98  template <typename Device>
100  {
101  public:
102  using TextureExecPortal = typename TextureDataHandle::ReadPortalType;
103 
106  : Width(0)
107  , Height(0)
108  {
109  }
110 
113  viskores::Id height,
114  const TextureDataHandle& data,
115  TextureFilterMode filterMode,
116  TextureWrapMode wrapMode,
117  viskores::cont::Token& token)
118  : Width(width)
119  , Height(height)
120  , Data(data.PrepareForInput(Device(), token))
121  , FilterMode(filterMode)
122  , WrapMode(wrapMode)
123  {
124  }
125 
128  {
129  v = 1.0f - v;
130  UV_BOUNDS_CHECK(u, v, ColorType);
131  switch (FilterMode)
132  {
135 
137  return GetLinearFilteredColor(u, v);
138 
139  default:
140  return ColorType();
141  }
142  }
143 
144  private:
147  viskores::Float32 v) const
148  {
149  viskores::Id x =
150  static_cast<viskores::Id>(viskores::Round(u * static_cast<viskores::Float32>(Width - 1)));
151  viskores::Id y =
152  static_cast<viskores::Id>(viskores::Round(v * static_cast<viskores::Float32>(Height - 1)));
153  return GetColorAtCoords(x, y);
154  }
155 
158  {
159  u = u * static_cast<viskores::Float32>(Width) - 0.5f;
160  v = v * static_cast<viskores::Float32>(Height) - 0.5f;
161  viskores::Id x = static_cast<viskores::Id>(viskores::Floor(u));
162  viskores::Id y = static_cast<viskores::Id>(viskores::Floor(v));
163  viskores::Float32 uRatio = u - static_cast<viskores::Float32>(x);
164  viskores::Float32 vRatio = v - static_cast<viskores::Float32>(y);
165  viskores::Float32 uOpposite = 1.0f - uRatio;
166  viskores::Float32 vOpposite = 1.0f - vRatio;
167  viskores::Id xn, yn;
168  GetNextCoords(x, y, xn, yn);
169  ColorType c1 = GetColorAtCoords(x, y);
170  ColorType c2 = GetColorAtCoords(xn, y);
171  ColorType c3 = GetColorAtCoords(x, yn);
172  ColorType c4 = GetColorAtCoords(xn, yn);
173  return (c1 * uOpposite + c2 * uRatio) * vOpposite + (c3 * uOpposite + c4 * uRatio) * vRatio;
174  }
175 
178  {
179  viskores::Id idx = (y * Width + x) * NumComponents;
180  ColorType color;
181  for (viskores::IdComponent i = 0; i < NumComponents; ++i)
182  {
183  color[i] = Data.Get(idx + i) / 255.0f;
184  }
185  return color;
186  }
187 
190  viskores::Id y,
191  viskores::Id& xn,
192  viskores::Id& yn) const
193  {
194  switch (WrapMode)
195  {
197  xn = (x + 1) < Width ? (x + 1) : x;
198  yn = (y + 1) < Height ? (y + 1) : y;
199  break;
201  default:
202  xn = (x + 1) % Width;
203  yn = (y + 1) % Height;
204  break;
205  }
206  }
207 
213  };
214 
216  {
217  public:
220  : Width(0)
221  , Height(0)
222  {
223  }
224 
227  viskores::Id height,
228  const TextureDataHandle& data,
229  TextureFilterMode filterMode,
230  TextureWrapMode wrapMode)
231  : Width(width)
232  , Height(height)
233  , Data(data)
234  , FilterMode(filterMode)
235  , WrapMode(wrapMode)
236  {
237  }
238 
239  template <typename Device>
241  Device,
242  viskores::cont::Token& token) const
243  {
245  this->Width, this->Height, this->Data, this->FilterMode, this->WrapMode, token);
246  }
247 
248  private:
254  }; // class Texture2DSampler
255 
256 private:
262 }; // class Texture2D
263 }
264 } // namespace viskores::rendering
265 
266 #endif // viskores_rendering_Texture2D_h
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
Texture2DSamplerExecutionObject()
Definition: Texture2D.h:105
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNextCoords
void GetNextCoords(viskores::Id x, viskores::Id y, viskores::Id &xn, viskores::Id &yn) const
Definition: Texture2D.h:189
ArrayHandle.h
viskores::rendering::Texture2D::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:260
viskores::rendering::Texture2D::Height
viskores::Id Height
Definition: Texture2D.h:258
viskores::rendering::TextureFilterMode::NearestNeighbour
@ NearestNeighbour
viskores::rendering::TextureWrapMode::Clamp
@ Clamp
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:211
viskores::rendering::Texture2D::GetFilterMode
TextureFilterMode GetFilterMode() const
Definition: Texture2D.h:82
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::Height
viskores::Id Height
Definition: Texture2D.h:209
viskores::rendering::Texture2D::Texture2DSampler::Texture2DSampler
Texture2DSampler(viskores::Id width, viskores::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode)
Definition: Texture2D.h:226
viskores::rendering::Texture2D::IsValid
bool IsValid() const
Definition: Texture2D.h:79
viskores::rendering::Texture2D::SetWrapMode
void SetWrapMode(TextureWrapMode wrapMode)
Definition: Texture2D.h:91
viskores::rendering::Texture2D::Texture2DSampler::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:253
viskores::cont::ArrayHandle< viskores::UInt8 >
viskores::rendering::Texture2D::ColorType
viskores::Vec< viskores::Float32, NumComponents > ColorType
Definition: Texture2D.h:47
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::rendering::Texture2D::Texture2DSampler::Height
viskores::Id Height
Definition: Texture2D.h:250
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::Data
TextureExecPortal Data
Definition: Texture2D.h:210
viskores::Round
viskores::Float32 Round(viskores::Float32 x)
Definition: Math.h:2299
UV_BOUNDS_CHECK
#define UV_BOUNDS_CHECK(u, v, NoneType)
Definition: Texture2D.h:51
viskores::rendering::Texture2D::Texture2DSampler::PrepareForExecution
Texture2DSamplerExecutionObject< Device > PrepareForExecution(Device, viskores::cont::Token &token) const
Definition: Texture2D.h:240
viskores::rendering::Texture2D::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:261
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores::rendering::Texture2D::GetExecObjectFactory
Texture2DSampler GetExecObjectFactory() const
Definition: Texture2D.h:93
viskores::rendering::TextureWrapMode
TextureWrapMode
Definition: Texture2D.h:36
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColorAtCoords
ColorType GetColorAtCoords(viskores::Id x, viskores::Id y) const
Definition: Texture2D.h:177
viskores::rendering::TextureFilterMode::Linear
@ Linear
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::Width
viskores::Id Width
Definition: Texture2D.h:208
viskores::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:165
viskores::rendering::Texture2D::Texture2D
Texture2D()
Definition: Texture2D.h:58
viskores::Clamp
viskores::Float32 Clamp(viskores::Float32 x, viskores::Float32 lo, viskores::Float32 hi)
Definition: Math.h:1835
viskores::rendering::Texture2D::Texture2DSampler::Texture2DSampler
Texture2DSampler()
Definition: Texture2D.h:219
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::rendering::Texture2D::Texture2DSampler::Data
TextureDataHandle Data
Definition: Texture2D.h:251
viskores::rendering::Texture2D::Texture2D
Texture2D(viskores::Id width, viskores::Id height, const TextureDataHandle &data)
Definition: Texture2D.h:65
viskores::rendering::Texture2D::Data
TextureDataHandle Data
Definition: Texture2D.h:259
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::GetNearestNeighbourFilteredColor
ColorType GetNearestNeighbourFilteredColor(viskores::Float32 u, viskores::Float32 v) const
Definition: Texture2D.h:146
viskores::rendering::Texture2D::Texture2DSampler::FilterMode
TextureFilterMode FilterMode
Definition: Texture2D.h:252
viskores::rendering::Texture2D
Definition: Texture2D.h:43
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject
Definition: Texture2D.h:99
viskores::rendering::Texture2D::GetWrapMode
TextureWrapMode GetWrapMode() const
Definition: Texture2D.h:88
viskores::rendering::Texture2D::Width
viskores::Id Width
Definition: Texture2D.h:257
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::GetColor
ColorType GetColor(viskores::Float32 u, viskores::Float32 v) const
Definition: Texture2D.h:127
viskores::rendering::TextureWrapMode::Repeat
@ Repeat
viskores::rendering::Texture2D< 1 >::TextureDataHandle
typename viskores::cont::ArrayHandle< viskores::UInt8 > TextureDataHandle
Definition: Texture2D.h:46
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::TextureExecPortal
typename TextureDataHandle::ReadPortalType TextureExecPortal
Definition: Texture2D.h:102
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::Texture2DSamplerExecutionObject
Texture2DSamplerExecutionObject(viskores::Id width, viskores::Id height, const TextureDataHandle &data, TextureFilterMode filterMode, TextureWrapMode wrapMode, viskores::cont::Token &token)
Definition: Texture2D.h:112
viskores::Floor
viskores::Float32 Floor(viskores::Float32 x)
Definition: Math.h:2238
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::GetLinearFilteredColor
ColorType GetLinearFilteredColor(viskores::Float32 u, viskores::Float32 v) const
Definition: Texture2D.h:157
viskores::rendering::Texture2D::Texture2DSamplerExecutionObject::WrapMode
TextureWrapMode WrapMode
Definition: Texture2D.h:212
ExecutionObjectBase.h
viskores::rendering::Texture2D::SetFilterMode
void SetFilterMode(TextureFilterMode filterMode)
Definition: Texture2D.h:85
viskores::Vec
A short fixed-length array.
Definition: Types.h:365
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::ExecutionObjectBase
Base ExecutionObjectBase for execution objects to inherit from so that you can use an arbitrary objec...
Definition: ExecutionObjectBase.h:39
viskores::rendering::Texture2D::Texture2DSampler::Width
viskores::Id Width
Definition: Texture2D.h:249
VISKORES_EXEC
#define VISKORES_EXEC
Definition: ExportMacros.h:59
viskores::rendering::TextureFilterMode
TextureFilterMode
Definition: Texture2D.h:30
viskores::rendering::Texture2D::Texture2DSampler
Definition: Texture2D.h:215