All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BatchedIconRenderable.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "RenderableBase.h"
6 #include "Rendering.h"
7 #include "SortKey.h"
8 #include "Types.h"
9 #include "GlBuffer.h"
10 
11 #include "BatchedIconAppearance.h"
12 #include "BatchedIconRangeBuilder.h"
13 
14 #include "AllVertexTypes.h"
15 
16 #include <deque>
17 
18 namespace Eegeo
19 {
20  namespace Icons
21  {
23  {
25 
26  const BatchedIconAppearance* pAppearance;
27  bool IsStencilMaskPass;
28  bool NeedsShaderSetup;
29  };
30 
31 
32  // TODO: This looks pretty general.
34  {
35  public:
36  static Rendering::SortKey MakeSortKey(const Rendering::Materials::IMaterial& material,
37  Rendering::LayerIds::Values layerId,
38  int subLayer);
39 
41  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
42  const Rendering::SortKey& sortKey,
43  int vertexStrideBytes,
44  const Text::GlBuffer<u16>& quadIndexBuffer
45  );
46 
48 
49  virtual void SetAppearance(const BatchedIconAppearance& appearance);
50 
51  virtual const BatchedIconAppearance& GetAppearanceForCurrentRange() const;
52 
53  virtual bool ShouldRender() const
54  {
55  return GetVertexCount() > 0;
56  }
57 
58  virtual void Finalize();
59 
60  virtual void Upload() = 0;
61 
62  virtual void Reset() = 0;
63 
64  bool IsStencilMaskPass() const
65  {
66  return m_pDrawState->IsStencilMaskPass;
67  }
68 
69  bool NeedsShaderSetup() const
70  {
71  return m_pDrawState->NeedsShaderSetup;
72  }
73 
74  protected:
75  void Draw(Rendering::GLState& glState, int batchQuadOffset) const;
76 
77  virtual int GetVertexCount() const = 0;
78 
79  virtual const void* GetVertexData(int quadOffset, int quadCount) const = 0;
80 
81  private:
82  void EndRange(int currentIndexCount) const;
83 
84  int GetCurrentQuadCount() const;
85 
86  BatchedIconRangeBuilder* m_pRangeBuilder;
87 
88  typedef std::deque<BatchedIconRange> TAppearanceRanges;
89  TAppearanceRanges* m_pAppearanceRanges;
90  BatchedIconRenderableDrawState* m_pDrawState;
91 
92  const Text::GlBuffer<u16>& m_quadIndexBuffer;
93 
94  };
95 
96  // TODO: Everything below this line is pretty much a direct copy of TBatchedTextRenderable - Unify batched quad rendering!
97 
98  template <typename TVertex>
100  {
101  public:
102  typedef TVertex VertexType;
104  typedef std::vector<VertexType> VertexListType;
106 
108  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
109  const Rendering::SortKey& sortKey,
110  const TBatchedIconRenderable* pPrimary,
111  const Text::GlBuffer<u16>& quadIndexBuffer,
112  VertexBufferType& vertexBuffer)
113  : BatchedIconRenderable(pMaterial, pVertexBinding, sortKey, static_cast<int>(sizeof(VertexType)), quadIndexBuffer)
114  , m_pPrimary(pPrimary)
115  , m_vertexBuffer(vertexBuffer)
116  , m_pVertexList((pPrimary == NULL) ? Eegeo_NEW(VertexListType)() : NULL)
117  , m_vertexBufferElementOffset(0)
118  {
119  }
120 
122  {
123  Eegeo_DELETE m_pVertexList;
124  }
125 
126  void Upload()
127  {
128  if (IsPrimary())
129  {
130  m_vertexBufferElementOffset = m_vertexBuffer.GetElementOffset();
131  m_vertexBuffer.Upload(*m_pVertexList);
132  }
133  else
134  {
135  m_vertexBufferElementOffset = -1;
136  }
137  }
138 
139  void Render(Rendering::GLState& glState) const
140  {
141  const int quadOffset = GetVertexBufferElementOffset() / 4;
142 
143  m_vertexBuffer.Bind();
144 
145  BatchedIconRenderable::Draw(glState, quadOffset);
146 
147  m_vertexBuffer.Unbind();
148 
149  if (IsPrimary())
150  {
151  m_pVertexList->clear();
152  }
153  }
154 
155  void Reset()
156  {
157  if (m_pVertexList)
158  {
159  m_pVertexList->clear();
160  }
161  }
162 
163  VertexListType& GetVertexList() const
164  {
165  // Replica delegates to its Primary
166  return IsPrimary()
167  ? *m_pVertexList
168  : m_pPrimary->GetVertexList();
169  }
170 
171  bool IsPrimary() const
172  {
173  return m_pPrimary == NULL;
174  }
175 
176  bool IsReplica() const
177  {
178  return !IsPrimary();
179  }
180 
181  private:
182 
183  int GetVertexBufferElementOffset() const
184  {
185  const int vertexBufferElementOffset = IsPrimary()
186  ? m_vertexBufferElementOffset
187  : m_pPrimary->GetVertexBufferElementOffset();
188 
189  return vertexBufferElementOffset;
190  }
191 
192  virtual int GetVertexCount() const
193  {
194  return static_cast<int>(GetVertexList().size());
195  }
196 
197  virtual const void* GetVertexData(int quadOffset, int quadCount) const
198  {
199  const VertexListType& vertexList = GetVertexList();
200  Eegeo_ASSERT((quadOffset+quadCount)*4 <= vertexList.size());
201 
202  const VertexType* pVertexData = &(vertexList[quadOffset*4]);
203 
204  return static_cast<const void*>(pVertexData);
205  }
206 
207  const TBatchedIconRenderable* m_pPrimary;
208  VertexBufferType& m_vertexBuffer;
209  VertexListType* m_pVertexList;
210  int m_vertexBufferElementOffset;
211 
212  };
213 
214  class BatchedScreenIconRenderable : public TBatchedIconRenderable<Rendering::VertexTypes::ScreenTextVertex>
215  {
216  public:
218  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
219  const Rendering::SortKey& sortKey,
220  const TBatchedIconRenderable* pPrimary,
221  const Text::GlBuffer<u16>& quadIndexBuffer,
223  )
224  : ThisType::ThisType(pMaterial, pVertexBinding, sortKey, pPrimary, quadIndexBuffer, screenTextVertexBuffer)
225  {
226 
227  }
228  };
229 
230 
231  class BatchedWorldIconRenderable : public TBatchedIconRenderable<Rendering::VertexTypes::WorldTextVertex>
232  {
233  public:
235  const Rendering::VertexLayouts::VertexBinding* pVertexBinding,
236  const Rendering::SortKey& sortKey,
237  const TBatchedIconRenderable* pPrimary,
238  const Text::GlBuffer<u16>& quadIndexBuffer,
240  )
241  : ThisType::ThisType(pMaterial, pVertexBinding, sortKey, pPrimary, quadIndexBuffer, worldTextVertexBuffer)
242  {
243 
244  }
245  };
246  }
247 }