All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SortKey.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include <sstream>
7 
8 namespace Eegeo
9 {
10  namespace Rendering
11  {
12  class SortKey
13  {
14  typedef u64 SortKeyValue;
15 
16  static const u32 LayerFieldWidth = 6;
17  static const u32 LayerFieldShift = 58;
18  static const u64 LayerFieldMask = ((((u64)1) << LayerFieldWidth) - 1);
19  static const SortKeyValue LayerFieldInPlaceMask = (LayerFieldMask << LayerFieldShift);
20  static const SortKeyValue LayerFieldInPlaceInverseMask = (~LayerFieldInPlaceMask);
21 
22  static const u32 UserDefinedFieldWidth = 8;
23  static const u32 UserDefinedFieldShift = 50;
24  static const u64 UserDefinedFieldMask = ((((u64)1) << UserDefinedFieldWidth) - 1);
25  static const SortKeyValue UserDefinedInPlaceMask = (UserDefinedFieldMask << UserDefinedFieldShift);
26  static const SortKeyValue UserDefinedInPlaceInverseMask = (~UserDefinedInPlaceMask);
27 
28  static const u32 TranslucencyTypeFieldWidth = 2;
29  static const u32 TranslucencyTypeFieldShift = 48;
30  static const u64 TranslucencyTypeFieldMask = ((((u64)1) << TranslucencyTypeFieldWidth) - 1);
31  static const SortKeyValue TranslucencyTypeFieldInPlaceMask = (TranslucencyTypeFieldMask << TranslucencyTypeFieldShift);
32  static const SortKeyValue TranslucencyTypeFieldInPlaceInverseMask = (~TranslucencyTypeFieldInPlaceMask);
33 
34  static const u32 DepthFieldWidth = 24;
35  static const u32 DepthFieldShift = 24;
36  static const u64 DepthFieldMask = ((((u64)1) << DepthFieldWidth) - 1);
37  static const SortKeyValue DepthFieldInPlaceMask = (DepthFieldMask << DepthFieldShift);
38  static const SortKeyValue DepthFieldInPlaceInverseMask = (~DepthFieldInPlaceMask);
39 
40  static const u32 ShaderProgramFieldWidth = 8;
41  static const u32 ShaderProgramFieldShift = 16;
42  static const u64 ShaderProgramFieldMask = ((((u64)1) << ShaderProgramFieldWidth) - 1);
43  static const SortKeyValue ShaderProgramFieldInPlaceMask = (ShaderProgramFieldMask << ShaderProgramFieldShift);
44  static const SortKeyValue ShaderProgramFieldInPlaceInverseMask = (~ShaderProgramFieldInPlaceMask);
45 
46  static const u32 MaterialFieldWidth = 11;
47  static const u32 MaterialFieldShift = 5;
48  static const u64 MaterialFieldMask = ((((u64)1) << MaterialFieldWidth) - 1);
49  static const SortKeyValue MaterialFieldInPlaceMask = (MaterialFieldMask << MaterialFieldShift);
50  static const SortKeyValue MaterialFieldInPlaceInverseMask = (~MaterialFieldInPlaceMask);
51 
52  public:
53 
54  static const u64 MaxLayerValue = LayerFieldMask;
55  static const u64 MaxTranslucencyValue = TranslucencyTypeFieldMask;
56  static const u64 MaxShaderProgramValue = ShaderProgramFieldMask;
57  static const u64 MaxMaterialValue = MaterialFieldMask;
58  static const u64 MaxDepthValue = DepthFieldMask;
59 
60  SortKey()
61  : m_key(0)
62  {
63  }
64 
65  inline void SetUserDefined(u64 userDefined)
66  {
67  Eegeo_ASSERT(userDefined <= UserDefinedFieldMask, "user defined %ld is greater than maximum allowed value %ld", userDefined, UserDefinedFieldMask);
68  m_key = (m_key & UserDefinedInPlaceInverseMask) | (userDefined << UserDefinedFieldShift);
69  }
70 
71  inline u64 GetUserDefined() const
72  {
73  return ((m_key & UserDefinedInPlaceMask) >> UserDefinedFieldShift);
74  }
75 
76  inline void SetLayer(u64 layer)
77  {
78  Eegeo_ASSERT(layer <= LayerFieldMask, "layer %ld is greater than maximum allowed value %ld", layer, LayerFieldMask);
79  m_key = (m_key & LayerFieldInPlaceInverseMask) | (layer << LayerFieldShift);
80  }
81 
82  inline u64 GetLayer() const
83  {
84  return ((m_key & LayerFieldInPlaceMask) >> LayerFieldShift);
85  }
86 
87  inline void SetTranslucencyType(u64 translucencyType)
88  {
89  Eegeo_ASSERT(translucencyType <= TranslucencyTypeFieldMask, "translucencyType %ld is greater than maximum allowed value %ld", translucencyType, TranslucencyTypeFieldMask);
90  m_key = (m_key & TranslucencyTypeFieldInPlaceInverseMask) | (translucencyType << TranslucencyTypeFieldShift);
91  }
92 
93  inline u64 GetTranslucencyType() const
94  {
95  return ((m_key & TranslucencyTypeFieldInPlaceMask) >> TranslucencyTypeFieldShift);
96  }
97 
98  inline void SetShaderProgram(u64 shaderProgram)
99  {
100  Eegeo_ASSERT(shaderProgram <= ShaderProgramFieldMask);
101  m_key = (m_key & ShaderProgramFieldInPlaceInverseMask) | (shaderProgram << ShaderProgramFieldShift);
102  }
103 
104  inline u64 GetShaderProgram() const
105  {
106  return ((m_key & ShaderProgramFieldInPlaceMask) >> ShaderProgramFieldShift);
107  }
108 
109  inline void SetMaterial(u64 materialId)
110  {
111  Eegeo_ASSERT(materialId <= MaterialFieldMask);
112  m_key = (m_key & MaterialFieldInPlaceInverseMask) | (materialId << MaterialFieldShift);
113  }
114 
115  inline u64 GetMaterial() const
116  {
117  return ((m_key & MaterialFieldInPlaceMask) >> MaterialFieldShift);
118  }
119 
120  inline void SetDepth(u64 depth)
121  {
122  Eegeo_ASSERT(depth <= DepthFieldMask, "depth index %ld is greater than maximum allowed value %ld", depth, DepthFieldMask);
123  m_key = (m_key & DepthFieldInPlaceInverseMask) | (depth << DepthFieldShift);
124  }
125 
126  inline u64 GetDepth() const
127  {
128  return ((m_key & DepthFieldInPlaceMask) >> DepthFieldShift);
129  }
130 
131  inline SortKeyValue GetKey() const
132  {
133  return m_key;
134  }
135 
136  inline bool operator< (const SortKey &otherKey) const
137  {
138  return m_key < otherKey.m_key;
139  }
140 
141  inline bool operator!= (const SortKey &otherKey) const
142  {
143  return m_key != otherKey.m_key;
144  }
145 
146  static std::string ToString(const SortKey& key)
147  {
148  std::stringstream stream;
149 
150  stream << "Lyer:" << key.GetLayer() << "\tUD:" << key.GetUserDefined() << "\tTrns:" << key.GetTranslucencyType() << "\tShdr:" << key.GetShaderProgram() << "\tMtl:" << key.GetMaterial() << "\tDep:" << key.GetDepth();
151 
152  return stream.str();
153  }
154 
155  private:
156  SortKeyValue m_key;
157  };
158 
159  }
160 }