Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
statetable.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_STATETABLE_H_
19 #define ION_GFX_STATETABLE_H_
20 
21 #include <bitset>
22 
23 #include "base/integral_types.h"
24 #include "ion/base/referent.h"
25 #include "ion/math/range.h"
26 #include "ion/math/vector.h"
27 
28 namespace ion {
29 namespace gfx {
30 
48 class ION_API StateTable : public base::Referent {
49  public:
51 
53 
55  enum Capability {
56  kBlend, // Corresponds to GL_BLEND.
57  kCullFace, // Corresponds to GL_CULL_FACE.
58  kDebugOutputSynchronous, // Corresponds to GL_DEBUG_OUTPUT_SYNCHRONOUS
59  kDepthTest, // Corresponds to GL_DEPTH_TEST.
60  kDither, // Corresponds to GL_DITHER.
61  kMultisample, // Corresponds to GL_MULTISAMPLE.
62  kPolygonOffsetFill, // Corresponds to GL_POLYGON_OFFSET_FILL.
63  kSampleAlphaToCoverage, // Corresponds to GL_SAMPLE_ALPHA_TO_COVERAGE.
64  kSampleCoverage, // Corresponds to GL_SAMPLE_COVERAGE.
65  kScissorTest, // Corresponds to GL_SCISSOR_TEST.
66  kStencilTest, // Corresponds to GL_STENCIL_TEST.
67  kNumCapabilities, // The number of supported capabilities.
68  };
69 
72  enum Value {
96  };
97 
99 
101 
104  kAdd, // Corresponds to GL_FUNC_ADD.
105  kReverseSubtract, // Corresponds to GL_FUNC_REVERSE_SUBTRACT.
106  kSubtract, // Corresponds to GL_FUNC_SUBTRACT
107  };
108 
111  kConstantAlpha, // Corresponds to GL_CONSTANT_ALPHA.
112  kConstantColor, // Corresponds to GL_CONSTANT_COLOR.
113  kDstAlpha, // Corresponds to GL_DST_ALPHA.
114  kDstColor, // Corresponds to GL_DST_COLOR.
115  kOne, // Corresponds to GL_ONE.
116  kOneMinusConstantAlpha, // Corresponds to GL_ONE_MINUS_CONSTANT_ALPHA.
117  kOneMinusConstantColor, // Corresponds to GL_ONE_MINUS_CONSTANT_COLOR.
118  kOneMinusDstAlpha, // Corresponds to GL_ONE_MINUS_DST_ALPHA.
119  kOneMinusDstColor, // Corresponds to GL_ONE_MINUS_DST_COLOR.
120  kOneMinusSrcAlpha, // Corresponds to GL_ONE_MINUS_SRC_ALPHA.
121  kOneMinusSrcColor, // Corresponds to GL_ONE_MINUS_SRC_COLOR.
122  kSrcAlpha, // Corresponds to GL_SRC_ALPHA.
123  kSrcAlphaSaturate, // Corresponds to GL_SRC_ALPHA_SATURATE.
124  kSrcColor, // Corresponds to GL_SRC_COLOR.
125  kZero, // Corresponds to GL_ZERO.
126  };
127 
130  kClearColorBufferBit, // Corresponds to GL_COLOR_BUFFER_BIT.
131  kClearDepthBufferBit, // Corresponds to GL_DEPTH_BUFFER_BIT.
132  kClearStencilBufferBit, // Corresponds to GL_STENCIL_BUFFER_BIT.
133  };
134 
137  kCullFront, // Corresponds to GL_FRONT.
138  kCullBack, // Corresponds to GL_BACK.
139  kCullFrontAndBack, // Corresponds to GL_FRONT_AND_BACK.
140  };
141 
144  kDepthAlways, // Corresponds to GL_ALWAYS.
145  kDepthEqual, // Corresponds to GL_EQUAL.
146  kDepthGreater, // Corresponds to GL_GREATER.
147  kDepthGreaterOrEqual, // Corresponds to GL_GEQUAL.
148  kDepthLess, // Corresponds to GL_LESS.
149  kDepthLessOrEqual, // Corresponds to GL_LEQUAL.
150  kDepthNever, // Corresponds to GL_NEVER.
151  kDepthNotEqual, // Corresponds to GL_NOTEQUAL.
152  };
153 
155  enum DrawBuffer {
156  kBack, // Corresponds to GL_BACK.
157  kBackLeft, // Corresponds to GL_BACK_LEFT.
158  kBackRight, // Corresponds to GL_BACK_RIGHT.
159  kFront, // Corresponds to GL_FRONT.
160  kFrontAndBack, // Corresponds to GL_FRONT_AND_BACK.
161  kFrontLeft, // Corresponds to GL_FRONT_LEFT.
162  kFrontRight, // Corresponds to GL_FRONT_RIGHT.
163  kLeft, // Corresponds to GL_LEFT.
164  kNone, // Corresponds to GL_NONE.
165  kRight, // Corresponds to GL_RIGHT.
166 };
167 
170  kClockwise, // Corresponds to GL_CW.
171  kCounterClockwise, // Corresponds to GL_CCW.
172  };
173 
175  enum HintMode {
176  kHintFastest, // Corresponds to GL_FASTEST.
177  kHintNicest, // Corresponds to GL_NICEST.
178  kHintDontCare, // Corresponds to GL_DONT_CARE.
179  };
180 
182  enum HintTarget {
183  kGenerateMipmapHint, // Corresponds to GL_GENERATE_MIPMAP_HINT.
184  };
185 
188  kStencilAlways, // Corresponds to GL_ALWAYS.
189  kStencilEqual, // Corresponds to GL_EQUAL.
190  kStencilGreater, // Corresponds to GL_GREATER.
191  kStencilGreaterOrEqual, // Corresponds to GL_GEQUAL.
192  kStencilLess, // Corresponds to GL_LESS.
193  kStencilLessOrEqual, // Corresponds to GL_LEQUAL.
194  kStencilNever, // Corresponds to GL_NEVER.
195  kStencilNotEqual, // Corresponds to GL_NOTEQUAL.
196  };
197 
200  kStencilDecrement, // Corresponds to GL_DECR.
201  kStencilDecrementAndWrap, // Corresponds to GL_DECR_WRAP.
202  kStencilIncrement, // Corresponds to GL_INCR.
203  kStencilIncrementAndWrap, // Corresponds to GL_INCR_WRAP.
204  kStencilInvert, // Corresponds to GL_INVERT.
205  kStencilKeep, // Corresponds to GL_KEEP.
206  kStencilReplace, // Corresponds to GL_REPLACE.
207  kStencilZero, // Corresponds to GL_ZERO.
208  };
209 
211 
212 
218  : default_width_(0),
219  default_height_(0) {
220  Reset();
221  }
222  StateTable(int default_width, int default_height)
223  : default_width_(default_width),
224  default_height_(default_height) {
225  Reset();
226  }
227 
232  void Reset();
233 
236  void ResetSetState() {
237  data_.capabilities_set.reset();
238  data_.values_set.reset();
239  }
240 
244  void MarkAllSet() {
245  data_.capabilities_set.set();
246  data_.values_set.set();
247  }
248 
251  void CopyFrom(const StateTable& other);
252 
264  void MergeValuesFrom(const StateTable& other,
265  const StateTable& state_to_test);
266 
269  void MergeNonClearValuesFrom(const StateTable& other,
270  const StateTable& state_to_test);
271 
272 
274 
276 
278  void Enable(Capability capability, bool is_enabled) {
279  data_.capabilities.set(capability, is_enabled);
280  data_.capabilities_set.set(capability);
281  }
282 
284  bool IsEnabled(Capability capability) const {
285  return data_.capabilities.test(capability);
286  }
287 
289  size_t GetEnabledCount() const {
290  return data_.capabilities.count();
291  }
292 
294  void ResetCapability(Capability capability) {
295  if (capability == kDither)
296  data_.capabilities.set(capability);
297  else
298  data_.capabilities.reset(capability);
299  data_.capabilities_set.reset(capability);
300  }
301 
305  bool IsCapabilitySet(Capability capability) const {
306  return data_.capabilities_set.test(capability);
307  }
308 
310  size_t GetSetCapabilityCount() const {
311  return data_.capabilities_set.count();
312  }
313 
315  static bool AreCapabilitiesSame(const StateTable& st0,
316  const StateTable& st1) {
317  return st0.data_.capabilities == st1.data_.capabilities;
318  }
319 
321  static int GetCapabilityCount() { return kNumCapabilities; }
322 
324 
326 
328  void ResetValue(Value value);
329 
332  bool IsValueSet(Value value) const {
333  return data_.values_set.test(value);
334  }
335 
337  size_t GetSetValueCount() const {
338  return data_.values_set.count();
339  }
340 
342  static int GetValueCount() { return kNumValues; }
343 
345 
349  void SetEnforceSettings(bool enforced) { data_.is_enforced = enforced; }
350  bool AreSettingsEnforced() const { return data_.is_enforced; }
351 
353 
355 
357  void SetBlendColor(const math::Vector4f& color);
358  const math::Vector4f& GetBlendColor() const { return data_.blend_color; }
359 
362  void SetBlendEquations(BlendEquation rgb_eq, BlendEquation alpha_eq);
363  BlendEquation GetRgbBlendEquation() const { return data_.rgb_blend_equation; }
365  return data_.alpha_blend_equation;
366  }
367 
370  void SetBlendFunctions(BlendFunctionFactor rgb_source_factor,
371  BlendFunctionFactor rgb_destination_factor,
372  BlendFunctionFactor alpha_source_factor,
373  BlendFunctionFactor alpha_destination_factor);
375  return data_.rgb_blend_source_factor;
376  }
378  return data_.rgb_blend_destination_factor;
379  }
381  return data_.alpha_blend_source_factor;
382  }
384  return data_.alpha_blend_destination_factor;
385  }
386 
388 
390 
392  void SetClearColor(const math::Vector4f& color);
393  const math::Vector4f& GetClearColor() const { return data_.clear_color; }
394 
396  void SetClearDepthValue(float value);
397  float GetClearDepthValue() const { return data_.clear_depth_value; }
398 
400  void SetClearStencilValue(int value);
401  int GetClearStencilValue() const { return data_.clear_stencil_value; }
402 
404 
406 
409  void SetColorWriteMasks(bool red, bool green, bool blue, bool alpha);
410  bool GetRedColorWriteMask() const { return data_.color_write_masks[0]; }
411  bool GetGreenColorWriteMask() const { return data_.color_write_masks[1]; }
412  bool GetBlueColorWriteMask() const { return data_.color_write_masks[2]; }
413  bool GetAlphaColorWriteMask() const { return data_.color_write_masks[3]; }
414 
416 
418 
421  void SetCullFaceMode(CullFaceMode mode);
422  CullFaceMode GetCullFaceMode() const { return data_.cull_face_mode; }
423 
426  void SetFrontFaceMode(FrontFaceMode mode);
427  FrontFaceMode GetFrontFaceMode() const { return data_.front_face_mode; }
428 
430 
432 
435  void SetDepthFunction(DepthFunction func);
436  DepthFunction GetDepthFunction() const { return data_.depth_function; }
437 
440  void SetDepthRange(const math::Range1f& range);
441  const math::Range1f& GetDepthRange() const { return data_.depth_range; }
442 
444  void SetDepthWriteMask(bool mask);
445  bool GetDepthWriteMask() const { return data_.depth_write_mask; }
446 
448 
450 
452  void SetDrawBuffer(DrawBuffer draw_buffer);
454  DrawBuffer GetDrawBuffer() const { return data_.draw_buffer; }
455 
457 
459 
461  void SetHint(HintTarget target, HintMode mode);
462  HintMode GetHint(HintTarget target) const { return data_.hints[target]; }
463 
465 
467 
469  void SetLineWidth(float width);
470  float GetLineWidth() const { return data_.line_width; }
471 
473 
475 
478  void SetPolygonOffset(float factor, float units);
479  float GetPolygonOffsetFactor() const { return data_.polygon_offset_factor; }
480  float GetPolygonOffsetUnits() const { return data_.polygon_offset_units; }
481 
483 
485 
488  void SetSampleCoverage(float value, bool is_inverted);
489  float GetSampleCoverageValue() const { return data_.sample_coverage_value; }
491  return data_.sample_coverage_inverted;
492  }
493 
495 
497 
499  void SetScissorBox(const math::Range2i& box);
500  const math::Range2i& GetScissorBox() const { return data_.scissor_box; }
501 
503 
505 
509  void SetStencilFunctions(
510  StencilFunction front_func, int front_reference_value, uint32 front_mask,
511  StencilFunction back_func, int back_reference_value, uint32 back_mask);
513  return data_.front_stencil_function;
514  }
516  return data_.back_stencil_function;
517  }
519  return data_.front_stencil_reference_value;
520  }
522  return data_.back_stencil_reference_value;
523  }
524  uint32 GetFrontStencilMask() const { return data_.front_stencil_mask; }
525  uint32 GetBackStencilMask() const { return data_.back_stencil_mask; }
526 
529  void SetStencilOperations(StencilOperation front_stencil_fail,
530  StencilOperation front_depth_fail,
531  StencilOperation front_pass,
532  StencilOperation back_stencil_fail,
533  StencilOperation back_depth_fail,
534  StencilOperation back_pass);
536  return data_.front_stencil_fail_op;
537  }
539  return data_.front_stencil_depth_fail_op;
540  }
542  return data_.front_stencil_pass_op;
543  }
545  return data_.back_stencil_fail_op;
546  }
548  return data_.back_stencil_depth_fail_op;
549  }
551  return data_.back_stencil_pass_op;
552  }
553 
556  void SetStencilWriteMasks(uint32 front_mask, uint32 back_mask);
557  uint32 GetFrontStencilWriteMask() const {
558  return data_.front_stencil_write_mask;
559  }
560  uint32 GetBackStencilWriteMask() const {
561  return data_.back_stencil_write_mask;
562  }
563 
565 
567 
569  void SetViewport(const math::Range2i& rect);
570  void SetViewport(int left, int bottom, int width, int height);
572  const math::Range2i& GetViewport() const { return data_.viewport; }
573 
575 
578  template <typename EnumType> static const char* GetEnumString(EnumType value);
579 
580  protected:
583  ~StateTable() override;
584 
585  private:
587  static const int kNumStateValues = kViewportValue + 1;
588 
590  static const int kNumClearMaskBits = kClearStencilBufferBit + 1;
591 
593  static const int kNumHints = kGenerateMipmapHint + 1;
594 
599  struct Data {
602  Data() {}
603 
607  explicit Data(bool unused);
608 
610  std::bitset<kNumCapabilities> capabilities_set;
611 
613  std::bitset<kNumStateValues> values_set;
614 
616  std::bitset<kNumCapabilities> capabilities;
617 
622  bool is_enforced;
623 
625  math::Vector4f blend_color;
626  BlendEquation rgb_blend_equation;
627  BlendEquation alpha_blend_equation;
628  BlendFunctionFactor rgb_blend_source_factor;
629  BlendFunctionFactor rgb_blend_destination_factor;
630  BlendFunctionFactor alpha_blend_source_factor;
631  BlendFunctionFactor alpha_blend_destination_factor;
632 
634  math::Vector4f clear_color;
635  float clear_depth_value;
636  int clear_stencil_value;
637 
639  bool color_write_masks[4]; // Red, green, blue, alpha.
640 
642  CullFaceMode cull_face_mode;
643  FrontFaceMode front_face_mode;
644 
646  DepthFunction depth_function;
647  math::Range1f depth_range;
648  bool depth_write_mask;
649 
651  DrawBuffer draw_buffer;
652 
654  HintMode hints[kNumHints];
655 
657  float line_width;
658 
660  float polygon_offset_factor;
661  float polygon_offset_units;
662 
664  float sample_coverage_value;
665  bool sample_coverage_inverted;
666 
668  math::Range2i scissor_box;
669 
671  StencilFunction front_stencil_function;
672  StencilFunction back_stencil_function;
673  int front_stencil_reference_value;
674  int back_stencil_reference_value;
675  uint32 front_stencil_mask;
676  uint32 back_stencil_mask;
677  StencilOperation front_stencil_fail_op;
678  StencilOperation front_stencil_depth_fail_op;
679  StencilOperation front_stencil_pass_op;
680  StencilOperation back_stencil_fail_op;
681  StencilOperation back_stencil_depth_fail_op;
682  StencilOperation back_stencil_pass_op;
683  uint32 front_stencil_write_mask;
684  uint32 back_stencil_write_mask;
685 
687  math::Range2i viewport;
688  };
689 
694  static const Data& GetDefaultData();
695 
697  int default_width_;
698  int default_height_;
699 
701  Data data_;
702 };
703 
706 
707 } // namespace gfx
708 } // namespace ion
709 
710 #endif // ION_GFX_STATETABLE_H_
const math::Range2i & GetViewport() const
Returns the viewport rectangle.
Definition: statetable.h:572
bool GetDepthWriteMask() const
Definition: statetable.h:445
int GetClearStencilValue() const
Definition: statetable.h:401
StencilFunction
OpenGL stencil functions.
Definition: statetable.h:187
FrontFaceMode
OpenGL front face modes.
Definition: statetable.h:169
BlendFunctionFactor GetAlphaBlendFunctionSourceFactor() const
Definition: statetable.h:380
bool GetRedColorWriteMask() const
Definition: statetable.h:410
uint32 GetFrontStencilMask() const
Definition: statetable.h:524
uint32 GetFrontStencilWriteMask() const
Definition: statetable.h:557
StencilOperation GetBackStencilFailOperation() const
Definition: statetable.h:544
bool GetAlphaColorWriteMask() const
Definition: statetable.h:413
uint32 GetBackStencilWriteMask() const
Definition: statetable.h:560
Value
OpenGL state value items.
Definition: statetable.h:72
float GetPolygonOffsetFactor() const
Definition: statetable.h:479
const math::Range1f & GetDepthRange() const
Definition: statetable.h:441
const math::Vector4f & GetClearColor() const
Definition: statetable.h:393
CullFaceMode
OpenGL cull face modes.
Definition: statetable.h:136
StencilOperation GetBackStencilPassOperation() const
Definition: statetable.h:550
size_t GetSetCapabilityCount() const
Returns the number of capabilities that are set in the instance.
Definition: statetable.h:310
bool IsEnabled(Capability capability) const
Returns a flag indicating whether a capability is enabled.
Definition: statetable.h:284
double value
DrawBuffer GetDrawBuffer() const
Returns the target draw buffer.
Definition: statetable.h:454
HintMode GetHint(HintTarget target) const
Definition: statetable.h:462
StencilOperation GetBackStencilDepthFailOperation() const
Definition: statetable.h:547
void ResetCapability(Capability capability)
Resets a capability flag to its default state.
Definition: statetable.h:294
BlendEquation GetAlphaBlendEquation() const
Definition: statetable.h:364
HintMode
OpenGL hint modes.
Definition: statetable.h:175
Capability
Enumerated types for StateTable items.
Definition: statetable.h:55
Range< 2, int32 > Range2i
Definition: range.h:371
void MarkAllSet()
Sets the "set" state to true for all capabilities and values of the StateTable; future calls to IsVal...
Definition: statetable.h:244
bool GetBlueColorWriteMask() const
Definition: statetable.h:412
base::ReferentPtr< StateTable >::Type StateTablePtr
Convenience typedef for shared pointer to a StateTable.
Definition: statetable.h:705
Thread-safe abstract base class.
Definition: referent.h:49
DepthFunction
OpenGL depth test functions.
Definition: statetable.h:143
static bool AreCapabilitiesSame(const StateTable &st0, const StateTable &st1)
Returns true if the capabilities set in two instances are the same.
Definition: statetable.h:315
static int GetValueCount()
Returns the number of Values.
Definition: statetable.h:342
StateTable()
The constructor initializes the instance to contain all default values.
Definition: statetable.h:217
StencilOperation GetFrontStencilPassOperation() const
Definition: statetable.h:541
float GetSampleCoverageValue() const
Definition: statetable.h:489
bool IsCapabilitySet(Capability capability) const
Returns a flag indicating whether a capability was set since the StateTable was constructed or since ...
Definition: statetable.h:305
BlendEquation GetRgbBlendEquation() const
Definition: statetable.h:363
static int GetCapabilityCount()
Returns the number of Capabilities.
Definition: statetable.h:321
CullFaceMode GetCullFaceMode() const
Definition: statetable.h:422
FrontFaceMode GetFrontFaceMode() const
Definition: statetable.h:427
DrawBuffer
OpenGL draw buffers.
Definition: statetable.h:155
const math::Vector4f & GetBlendColor() const
Definition: statetable.h:358
BlendFunctionFactor GetRgbBlendFunctionDestinationFactor() const
Definition: statetable.h:377
StencilOperation GetFrontStencilDepthFailOperation() const
Definition: statetable.h:538
BlendFunctionFactor GetRgbBlendFunctionSourceFactor() const
Definition: statetable.h:374
void ResetSetState()
Resets the "set" state of the StateTable; future calls to IsValueSet() or IsCapabilitySet() will retu...
Definition: statetable.h:236
StencilFunction GetFrontStencilFunction() const
Definition: statetable.h:512
void Enable(Capability capability, bool is_enabled)
Capability item functions.
Definition: statetable.h:278
float GetClearDepthValue() const
Definition: statetable.h:397
bool GetGreenColorWriteMask() const
Definition: statetable.h:411
BlendFunctionFactor
OpenGL blend function factors.
Definition: statetable.h:110
DepthFunction GetDepthFunction() const
Definition: statetable.h:436
void SetEnforceSettings(bool enforced)
Sets/returns whether enforcement is enabled.
Definition: statetable.h:349
int width
BlendEquation
Other enumerated types.
Definition: statetable.h:103
BlendFunctionFactor GetAlphaBlendFunctionDestinationFactor() const
Definition: statetable.h:383
HintTarget
OpenGL hint targets.
Definition: statetable.h:182
size_t GetEnabledCount() const
Returns the number of capabilities that are enabled in the instance.
Definition: statetable.h:289
int GetFrontStencilReferenceValue() const
Definition: statetable.h:518
StencilFunction GetBackStencilFunction() const
Definition: statetable.h:515
int GetBackStencilReferenceValue() const
Definition: statetable.h:521
bool IsValueSet(Value value) const
Returns a flag indicating whether a value was set since the StateTable was constructed or since the l...
Definition: statetable.h:332
bool AreSettingsEnforced() const
Definition: statetable.h:350
StateTable(int default_width, int default_height)
Definition: statetable.h:222
float GetPolygonOffsetUnits() const
Definition: statetable.h:480
StencilOperation
OpenGL stencil operations.
Definition: statetable.h:199
size_t GetSetValueCount() const
Returns the number of values that are set in the instance.
Definition: statetable.h:337
StencilOperation GetFrontStencilFailOperation() const
Definition: statetable.h:535
A StateTable represents a collection of graphical state items that affect OpenGL rendering.
Definition: statetable.h:48
uint32 GetBackStencilMask() const
Definition: statetable.h:525
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
const math::Range2i & GetScissorBox() const
Definition: statetable.h:500
float GetLineWidth() const
Definition: statetable.h:470
bool IsSampleCoverageInverted() const
Definition: statetable.h:490
ClearMaskBit
OpenGL clear mask bits.
Definition: statetable.h:129
Range< 1, float > Range1f
Definition: range.h:365