Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
texture.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_TEXTURE_H_
19 #define ION_GFX_TEXTURE_H_
20 
21 #include <bitset>
22 
23 #include "base/integral_types.h"
24 #include "base/macros.h"
25 #include "ion/base/invalid.h"
26 #include "ion/base/referent.h"
28 #include "ion/gfx/image.h"
29 #include "ion/gfx/resourceholder.h"
30 #include "ion/gfx/sampler.h"
31 #include "ion/math/vector.h"
32 
33 namespace ion {
34 namespace gfx {
35 
38 static const size_t kMipmapSlotCount = 16;
39 
41 class ION_API TextureBase : public ResourceHolder {
42  public:
46  struct SubImage {
47  SubImage() : level(0U) {}
48 
49  SubImage(size_t level_in, const math::Point2ui& offset_in,
50  const ImagePtr& image_in)
51  : level(level_in),
52  offset(offset_in[0], offset_in[1], 0U),
53  image(image_in) {}
54  SubImage(size_t level_in, const math::Point3ui& offset_in,
55  const ImagePtr& image_in)
56  : level(level_in), offset(offset_in), image(image_in) {}
57 
58  size_t level;
59  math::Point3ui offset;
61  };
62 
64  enum Changes {
65  kBaseLevelChanged = kNumBaseChanges,
75  kNumChanges
76  };
77 
78  enum Swizzle {
83  };
84 
85  enum TextureType {
87  kTexture
88  };
89 
91  void SetSampler(const SamplerPtr& sampler);
92  const SamplerPtr& GetSampler() const { return sampler_.Get(); }
93 
106  void SetImmutableImage(const ImagePtr& image, size_t levels);
107  const ImagePtr& GetImmutableImage() const { return immutable_image_.Get(); }
110  size_t GetImmutableLevels() const { return immutable_levels_; }
111 
114  void SetBaseLevel(int level) { base_level_.Set(level); }
115  int GetBaseLevel() const { return base_level_.Get(); }
116 
119  void SetMaxLevel(int level) { max_level_.Set(level); }
120  int GetMaxLevel() const { return max_level_.Get(); }
121 
125  void SetSwizzleRed(Swizzle r) { swizzle_red_.Set(r); }
126  Swizzle GetSwizzleRed() const { return swizzle_red_.Get(); }
127  void SetSwizzleGreen(Swizzle g) { swizzle_green_.Set(g); }
128  Swizzle GetSwizzleGreen() const { return swizzle_green_.Get(); }
129  void SetSwizzleBlue(Swizzle b) { swizzle_blue_.Set(b); }
130  Swizzle GetSwizzleBlue() const { return swizzle_blue_.Get(); }
131  void SetSwizzleAlpha(Swizzle a) { swizzle_alpha_.Set(a); }
132  Swizzle GetSwizzleAlpha() const { return swizzle_alpha_.Get(); }
135  swizzle_red_.Set(r);
136  swizzle_green_.Set(g);
137  swizzle_blue_.Set(b);
138  swizzle_alpha_.Set(a);
139  }
140 
142  TextureType GetTextureType() const { return texture_type_; }
143 
146  void SetMultisampling(int samples, bool fixed_sample_locations) {
147  if (samples < 0) {
148  LOG(WARNING) << "Ignoring bad number of samples: " << samples;
149  return;
150  }
151  multisample_samples_.Set(samples);
152  multisample_fixed_sample_locations_.Set(fixed_sample_locations);
153  }
154  int GetMultisampleSamples() const {
155  return multisample_samples_.Get();
156  }
158  return multisample_fixed_sample_locations_.Get();
159  }
160 
161  protected:
164  class Face {
165  public:
166  Face(TextureBase* texture, int sub_image_changed_bit,
167  int mipmaps_changed_start_bit);
168 
172  void SetSubImage(size_t level, const math::Point2ui offset,
173  const ImagePtr& image);
174 
177  void SetSubImage(size_t level, const math::Point3ui offset,
178  const ImagePtr& image);
179 
182  return sub_images_;
183  }
184 
186  void ClearSubImages() const {
187  sub_images_.clear();
188  }
189 
192  bool HasImage(size_t level) const {
193  return level < mipmaps_set_.size() && mipmaps_set_.test(level);
194  }
195 
198  const ImagePtr GetImage(size_t level) const {
199  if (HasImage(level))
200  return mipmaps_.Get(level);
201  else
202  return ImagePtr();
203  }
204 
206  size_t GetImageCount() const {
207  return mipmaps_set_.count();
208  }
209 
218  void SetImage(size_t level, const ImagePtr& image_in, TextureBase* texture);
219 
220  private:
224  Field<bool> sub_images_changed_;
226  mutable base::AllocVector<TextureBase::SubImage> sub_images_;
227  VectorField<ImagePtr> mipmaps_;
228  std::bitset<kMipmapSlotCount> mipmaps_set_;
229  };
230 
232  explicit TextureBase(TextureType type);
233 
236  ~TextureBase() override;
237 
238  private:
239  Field<SamplerPtr> sampler_;
240  Field<int> base_level_;
241  Field<int> max_level_;
242  RangedField<Swizzle> swizzle_red_;
243  RangedField<Swizzle> swizzle_green_;
244  RangedField<Swizzle> swizzle_blue_;
245  RangedField<Swizzle> swizzle_alpha_;
246 
248  TextureType texture_type_;
249 
251  Field<ImagePtr> immutable_image_;
253  size_t immutable_levels_;
254 
256  Field<int> multisample_samples_;
257  Field<bool> multisample_fixed_sample_locations_;
258 
259  DISALLOW_IMPLICIT_CONSTRUCTORS(TextureBase);
260 };
261 
264 class ION_API Texture : public TextureBase {
265  public:
267  enum Changes {
268  kSubImageChanged = TextureBase::kNumChanges,
271  kNumChanges = kMipmapChanged + kMipmapSlotCount
272  };
273 
274  Texture();
275 
277  void SetImage(size_t level, const ImagePtr& image) {
278  if (GetImmutableImage().Get())
279  LOG(ERROR) << "ION: SetImage() called on immutable texture \""
280  << GetLabel()
281  << "\". Use SetSubImage() to update an immutable texture.";
282  else
283  face_.SetImage(level, image, this);
284  }
285  bool HasImage(size_t level) const {
286  return face_.HasImage(level);
287  }
288  const ImagePtr GetImage(size_t level) const {
289  return face_.GetImage(level);
290  }
291  size_t GetImageCount() const {
292  return face_.GetImageCount();
293  }
294  void SetSubImage(size_t level, const math::Point2ui offset,
295  const ImagePtr& image) {
296  SetSubImage(level, math::Point3ui(offset[0], offset[1], 0), image);
297  }
298  void SetSubImage(size_t level, const math::Point3ui offset,
299  const ImagePtr& image) {
300  face_.SetSubImage(level, offset, image);
301  }
303  return face_.GetSubImages();
304  }
305  void ClearSubImages() const {
306  face_.ClearSubImages();
307  }
308 
311  static bool ExpectedDimensionsForMipmap(const uint32 mipmap_width,
312  const uint32 mipmap_height,
313  const uint32 mipmap_level,
314  const uint32 base_width,
315  const uint32 base_height,
316  uint32* expected_width,
317  uint32* expected_height);
318 
319  protected:
322  ~Texture() override;
323 
324  private:
326  void OnNotify(const base::Notifier* notifier) override;
327 
329  Face face_;
330 };
331 
334 
335 } // namespace gfx
336 } // namespace ion
337 
338 #endif // ION_GFX_TEXTURE_H_
const ImagePtr GetImage(size_t level) const
Returns the image at the specified mipmap level, or NULL if this is not mipmapped.
Definition: texture.h:198
bool IsMultisampleFixedSampleLocations() const
Definition: texture.h:157
A Texture object represents the image data and mipmaps associated with a single texture.
Definition: texture.h:264
int level
base::ReferentPtr< Image >::Type ImagePtr
Definition: image.h:29
void SetSwizzleBlue(Swizzle b)
Definition: texture.h:129
std::string type
Definition: printer.cc:353
Changes
Changes that affect this resource. Some changes are forwarded from Sampler.
Definition: texture.h:64
const SamplerPtr & GetSampler() const
Definition: texture.h:92
void SetMultisampling(int samples, bool fixed_sample_locations)
Enables/disables and sets parameters for texture multisampling.
Definition: texture.h:146
Wrapper around a sub-image, which is defined as an image, the xy offset of where it should be placed ...
Definition: texture.h:46
SubImage(size_t level_in, const math::Point2ui &offset_in, const ImagePtr &image_in)
Definition: texture.h:49
size_t GetImageCount() const
Definition: texture.h:291
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
A Notifier both sends notifications to and receives notifications from other Notifiers.
Definition: notifier.h:35
A Field that holds a vector of up to some number of values.
Swizzle GetSwizzleGreen() const
Definition: texture.h:128
void SetBaseLevel(int level)
Sets/returns the index of the lowest mipmap level to use when rendering.
Definition: texture.h:114
int GetBaseLevel() const
Definition: texture.h:115
uint32 offset
void ClearSubImages() const
Definition: texture.h:305
bool HasImage(size_t level) const
Definition: texture.h:285
int GetMaxLevel() const
Definition: texture.h:120
void ClearSubImages() const
Clears the vector of sub-images.
Definition: texture.h:186
Internal class that wraps texture data: a single image or a stack of mipmaps, and any sub- or layered...
Definition: texture.h:164
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
void SetSwizzleRed(Swizzle r)
Sets/returns the color component to use when the color channels of a texture are used in a shader...
Definition: texture.h:125
int GetMultisampleSamples() const
Definition: texture.h:154
bool HasImage(size_t level) const
Returns true if there is a mipmap image at the specified level stored here.
Definition: texture.h:192
Swizzle GetSwizzleAlpha() const
Definition: texture.h:132
const Grid & image
The original monochrome image data, as doubles (0 - 1).
Definition: sdfutils.cc:90
const base::AllocVector< SubImage > & GetSubImages() const
Returns the vector of sub-images; it may be empty.
Definition: texture.h:181
void SetSwizzleAlpha(Swizzle a)
Definition: texture.h:131
SubImage(size_t level_in, const math::Point3ui &offset_in, const ImagePtr &image_in)
Definition: texture.h:54
TexturePtr texture
The Texture to add sub-image data to.
Definition: fontimage.cc:107
Swizzle GetSwizzleBlue() const
Definition: texture.h:130
This is an internal base class for all texture types.
Definition: texture.h:41
A generic field that represents some state in the resource.
void SetSwizzles(Swizzle r, Swizzle g, Swizzle b, Swizzle a)
Sets all swizzles at once.
Definition: texture.h:134
size_t GetImageCount() const
Gets the number of mipmap images that have been set.
Definition: texture.h:206
void SetMaxLevel(int level)
Sets/returns the index of the highest mipmap level to use when rendering.
Definition: texture.h:119
void SetImage(size_t level, const ImagePtr &image)
See comments in TextureBase::Face.
Definition: texture.h:277
const ImagePtr GetImage(size_t level) const
Definition: texture.h:288
TextureType GetTextureType() const
Returns what type of texture that this is.
Definition: texture.h:142
const base::AllocVector< SubImage > & GetSubImages() const
Definition: texture.h:302
kMipmapChanged must be last since it is a range of slots.
Definition: texture.h:270
void SetSwizzleGreen(Swizzle g)
Definition: texture.h:127
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
Swizzle GetSwizzleRed() const
Definition: texture.h:126
size_t GetImmutableLevels() const
Returns the number of immutable mipmap levels used by this texture.
Definition: texture.h:110
const ImagePtr & GetImmutableImage() const
Definition: texture.h:107
ResourceHolder is an internal base class for objects that hold resources managed by an outside entity...
base::ReferentPtr< Texture >::Type TexturePtr
Convenience typedef for shared pointer to a Texture.
Definition: texture.h:333
void SetSubImage(size_t level, const math::Point3ui offset, const ImagePtr &image)
Definition: texture.h:298
void SetSubImage(size_t level, const math::Point2ui offset, const ImagePtr &image)
Definition: texture.h:294
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50