0.6.1
texture.hpp
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include "image.hpp"
32 
33 namespace gli
34 {
35  class texture
36  {
37  public:
38  typedef size_t size_type;
40 
41  texture();
42 
43  texture(
44  size_type const & Layers,
45  size_type const & Faces,
46  size_type const & Levels,
47  format_type const & Format,
48  storage::dim_type const & Dimensions);
49 
50  texture(storage const & Storage);
51 
52  texture(
53  storage const & Storage,
54  format_type const & Format,
55  size_type BaseLayer, size_type MaxLayer,
56  size_type BaseFace, size_type MaxFace,
57  size_type BaseLevel, size_type MaxLevel);
58 
59  bool empty() const;
60  format_type format() const;
61 
62  size_type baseLayer() const;
63  size_type maxLayer() const;
64  size_type layers() const;
65 
66  size_type baseFace() const;
67  size_type maxFace() const;
68  size_type faces() const;
69 
70  size_type baseLevel() const;
71  size_type maxLevel() const;
72  size_type levels() const;
73 
74  size_type size() const;
75  template <typename genType>
76  size_type size() const;
77 
78  void * data();
79  template <typename genType>
80  genType * data();
81  void const * data() const;
82  template <typename genType>
83  genType const * data() const;
84 
85  void clear();
86  template <typename genType>
87  void clear(genType const & Texel);
88 
89  protected:
91  format_type const Format;
92  size_type const BaseLayer;
93  size_type const MaxLayer;
94  size_type const BaseFace;
95  size_type const MaxFace;
96  size_type const BaseLevel;
97  size_type const MaxLevel;
98  void * const Data;
99  size_type const Size;
100 
101  private:
102  void * const compute_data();
103  size_type compute_size() const;
104  };
105 
107  : Format(static_cast<gli::format>(FORMAT_INVALID))
108  , BaseLayer(0), MaxLayer(0)
109  , BaseFace(0), MaxFace(0)
110  , BaseLevel(0), MaxLevel(0)
111  , Data(nullptr)
112  , Size(0)
113  {}
114 
115  inline texture::texture
116  (
117  size_type const & Layers,
118  size_type const & Faces,
119  size_type const & Levels,
120  format_type const & Format,
121  storage::dim_type const & Dimensions
122  )
123  : Storage(Layers, Faces, Levels, Format, Dimensions)
124  , Format(Format)
125  , BaseLayer(0), MaxLayer(Layers - 1)
126  , BaseFace(0), MaxFace(Faces - 1)
127  , BaseLevel(0), MaxLevel(Levels - 1)
128  , Data(this->compute_data())
129  , Size(this->compute_size())
130  {}
131 
132  inline texture::texture
133  (
134  storage const & Storage
135  )
136  : Storage(Storage)
137  , Format(Storage.format())
138  , BaseLayer(0), MaxLayer(Storage.layers() - 1)
139  , BaseFace(0), MaxFace(Storage.faces() - 1)
140  , BaseLevel(0), MaxLevel(Storage.levels() - 1)
141  , Data(this->compute_data())
142  , Size(this->compute_size())
143  {}
144 
145  inline texture::texture
146  (
147  storage const & Storage,
148  format_type const & Format,
149  size_type BaseLayer, size_type MaxLayer,
150  size_type BaseFace, size_type MaxFace,
151  size_type BaseLevel, size_type MaxLevel
152  )
153  : Storage(Storage)
154  , Format(Format)
155  , BaseLayer(BaseLayer), MaxLayer(MaxLayer)
156  , BaseFace(BaseFace), MaxFace(MaxFace)
157  , BaseLevel(BaseLevel), MaxLevel(MaxLevel)
158  , Data(this->compute_data())
159  , Size(this->compute_size())
160  {}
161 
163  {
164  assert(!this->empty());
165 
166  return this->Size;
167  }
168 
169  template <typename genType>
171  {
172  assert(!this->empty());
173  assert(block_size(this->Storage.format()) >= sizeof(genType));
174 
175  return this->size() / sizeof(genType);
176  }
177 
178  inline void * texture::data()
179  {
180  return this->Data;
181  }
182 
183  inline void const * texture::data() const
184  {
185  return this->Data;
186  }
187 
188  template <typename genType>
189  inline genType * texture::data()
190  {
191  assert(!this->empty());
192  assert(block_size(this->format()) >= sizeof(genType));
193 
194  return reinterpret_cast<genType *>(this->data());
195  }
196 
197  template <typename genType>
198  inline genType const * texture::data() const
199  {
200  assert(!this->empty());
201  assert(block_size(this->format()) >= sizeof(genType));
202 
203  return reinterpret_cast<genType const *>(this->data());
204  }
205 
206  inline bool texture::empty() const
207  {
208  return this->Storage.empty();
209  }
210 
212  {
213  return this->Format;
214  }
215 
217  {
218  return this->BaseLayer;
219  }
220 
222  {
223  return this->MaxLayer;
224  }
225 
227  {
228  return this->maxLayer() - this->baseLayer() + 1;
229  }
230 
232  {
233  return this->BaseFace;
234  }
235 
237  {
238  return this->MaxFace;
239  }
240 
242  {
243  //assert(this->maxFace() - this->baseFace() + 1 == 1);
244  return this->maxFace() - this->baseFace() + 1;
245  }
246 
248  {
249  return this->BaseLevel;
250  }
251 
253  {
254  return this->MaxLevel;
255  }
256 
258  {
259  return this->maxLevel() - this->baseLevel() + 1;
260  }
261 
262  inline void texture::clear()
263  {
264  memset(this->data<glm::byte>(), 0, this->size<glm::byte>());
265  }
266 
267  template <typename genType>
268  inline void texture::clear(genType const & Texel)
269  {
270  assert(!this->empty());
271  assert(block_size(this->Storage.format()) == sizeof(genType));
272 
273  genType* Data = this->data<genType>();
274  size_type const TexelCount = this->size<genType>();
275 
276  for(size_type TexelIndex = 0; TexelIndex < TexelCount; ++TexelIndex)
277  *(Data + TexelIndex) = Texel;
278  }
279 
280  void * const texture::compute_data()
281  {
282  size_type const offset = detail::imageAddressing(
283  this->Storage, this->baseLayer(), this->baseFace(), this->baseLevel());
284 
285  return this->Storage.data() + offset;
286  }
287 
288  inline texture::size_type texture::compute_size() const
289  {
290  assert(!this->empty());
291 
292  return this->Storage.layer_size(
293  this->baseFace(), this->maxFace(),
294  this->baseLevel(), this->maxLevel()) * this->layers();
295  }
296 }//namespace gli
297 
298 #include "texture1d.hpp"
299 #include "texture1d_array.hpp"
300 #include "texture2d.hpp"
301 #include "texture2d_array.hpp"
302 #include "texture3d.hpp"
303 #include "texture_cube.hpp"
304 #include "texture_cube_array.hpp"
305 
306 #include "addressing.hpp"
307 
308 #include "texture1d.inl"
309 #include "texture1d_array.inl"
310 #include "texture2d.inl"
311 #include "texture2d_array.inl"
312 #include "texture3d.inl"
313 #include "texture_cube.inl"
314 #include "texture_cube_array.inl"
size_t size_type
Definition: texture.hpp:38
size_type layers() const
OpenGL Image (gli.g-truc.net)
size_type levels() const
Definition: texture.hpp:257
size_type const MaxLayer
Definition: texture.hpp:93
OpenGL Image (gli.g-truc.net)
dim3_t dim_type
Definition: storage.hpp:59
OpenGL Image (gli.g-truc.net)
OpenGL Image (gli.g-truc.net)
size_type const Size
Definition: texture.hpp:99
format
Definition: format.hpp:33
std::uint32_t block_size(format const &Format)
size_type baseFace() const
Definition: texture.hpp:231
size_type layers() const
Definition: texture.hpp:226
size_type baseLayer() const
Definition: texture.hpp:216
size_type maxLayer() const
Definition: texture.hpp:221
size_type const BaseFace
Definition: texture.hpp:94
size_type layer_size(size_type const &BaseFace, size_type const &MaxFace, size_type const &BaseLevel, size_type const &MaxLevel) const
OpenGL Image (gli.g-truc.net)
size_type const BaseLayer
Definition: texture.hpp:92
OpenGL Image (gli.g-truc.net)
data_type * data()
size_type const MaxFace
Definition: texture.hpp:95
size_t imageAddressing(storage const &Storage, size_t const &LayerOffset, size_t const &FaceOffset, size_t const &LevelOffset)
storage Storage
Definition: texture.hpp:90
size_type const MaxLevel
Definition: texture.hpp:97
void *const Data
Definition: texture.hpp:98
size_type faces() const
Definition: texture.hpp:241
format_type const Format
Definition: texture.hpp:91
size_type faces() const
bool empty() const
bool empty() const
Definition: texture.hpp:206
OpenGL Image (gli.g-truc.net)
format_type format() const
size_type maxLevel() const
Definition: texture.hpp:252
size_type size() const
Definition: texture.hpp:162
size_type const BaseLevel
Definition: texture.hpp:96
OpenGL Image (gli.g-truc.net)
format format_type
Definition: texture.hpp:39
void * data()
Definition: texture.hpp:178
size_type maxFace() const
Definition: texture.hpp:236
size_type baseLevel() const
Definition: texture.hpp:247
format_type format() const
Definition: texture.hpp:211
void clear()
Definition: texture.hpp:262
size_type levels() const
OpenGL Image (gli.g-truc.net)