My Project
RenderableChunk.h
1 #pragma once
2 #include "BlockRenderTask.h"
3 #include "BlockCommon.h"
4 #include "ShapeAABB.h"
5 #include "BlockReadWriteLock.h"
6 
7 namespace ParaEngine
8 {
9  class BlockRegion;
10  class BlockRenderTask;
11  class Block;
12  class BlockChunk;
13  class CBlockWorld;
14  class ParaVertexBufferPool;
15  class BlockGeneralTessellator;
16 
18  {
19  public:
20  enum ChunkBuildState
21  {
22  // chunk vertex buffer is ready on device
23  ChunkBuild_Ready,
24  // main thread is requesting to rebuild vertex buffer
25  ChunkBuild_RequestRebuild,
26  // chunk data is rebuilding in (possibly in another thread).
27  ChunkBuild_Rebuilding,
28  // building thread has finished, and request the main thread to upload the buffer
29  ChunkBuild_RequestBufferUpload,
30  // initial state: chunk has never been built before.
31  ChunkBuild_empty,
32  };
33 
34 
36  ~RenderableChunk();
37 
38  void ReuseChunk(BlockRegion* m_ownerBlockRegion,int16_t chunkIndex);
39 
40  void ClearChunkData();
41 
42  void ReleaseVertexBuffers();
43 
44  void Reset();
45 
51  bool ShouldRebuildRenderBuffer(CBlockWorld* pWorld, bool bNewBuffer = true, bool bUpdatedBuffer = true);
52 
56  bool RebuildRenderBuffer(CBlockWorld* pWorld, bool bAsyncMode);
57 
58 
60  void FillRenderQueue(CBlockWorld* pWorld);
61 
62  void OnLeaveWorld();
63 
64  void DeleteDeviceObjects();
65 
66  void ClearRenderTasks();
67 
68  void RendererRecreated();
69  bool IsEmptyChunk();
70 
71  inline uint16_t GetChunkIndex(){return m_packedChunkID;}
72 
75 
78 
79  void FillRenderQueue(CBlockWorld* pWorld,Vector3& renderOfs,float verticalOffset);
80 
83  void SetChunkDirty(bool isDirty)
84  {
85  m_isDirty = isDirty;
86  }
87 
91  bool IsDirty() const {
92  return m_isDirty;
93  }
94 
95  bool IsDirtyByNeighbor();
98 
100  bool IsIntersect(CShapeSphere& sphere);
101 
103  bool IsNearbyChunksLoaded();
104 
105  RenderableChunk::ChunkBuildState GetChunkBuildState() const;
106  bool IsBuildingBuffer() const;
107  bool IsReadyOrEmpty() const;
108  void SetChunkBuildState(RenderableChunk::ChunkBuildState val);
109 
110  static void StaticInit();
111  static void StaticRelease();
112  static ParaVertexBufferPool* GetVertexBufferPool();
113 
115  void WaitUntilChunkReady();
116 
124  void RebuildRenderBufferToMemory(Scoped_ReadLock<BlockReadWriteLock>* Lock_, int* pnCpuYieldCount);
125  void ClearBuilderBuffer();
128 
129  /* total number of faces. */
130  int32 GetTotalFaceCount() const;
131 
134  int32 GetDelayedRebuildTick() const;
135  void SetDelayedRebuildTick(int32 val);
136 
138  int16 GetChunkViewDistance() const;
139  void SetChunkViewDistance(int16 val);
140 
142  int16 GetViewIndex() const;
143  void SetViewIndex(int16 val);
144 
146  int GetRenderFrameCount() const;
147  void SetRenderFrameCount(int val);
148 
150  int GetVertexBufferBytes();
153  int GetLastBufferBytes();
154 
156  static int GetTotalRenderableChunks();
157 
158  std::vector<BlockRenderTask*> GetRenderTasks();
159 
160  const CShapeBox& GetShapeAABB() const;
161 
165  int GetShaderID() const;
166  void SetShaderID(int val);
167 
169  bool IsMainRenderer() const;
170  void SetIsMainRenderer(bool val);
171 
172  bool IsDirtyByBlockChange() const;
173  void IsDirtyByBlockChange(bool val);
174  public:
176  {
177  BlockTemplate* m_pTemplate;
178  uint32_t m_blockData;
179  uint32_t m_nFaceCount;
180 
181  //packedBlockId
182  std::vector<uint16_t> instances;
183  InstanceGroup() :m_pTemplate(NULL), m_blockData(0), m_nFaceCount(0){}
184  inline void reset(){
185  m_pTemplate = 0;
186  m_nFaceCount = 0;
187  instances.clear();
188  }
189  inline bool isEmpty() const {
190  return !(m_pTemplate || instances.size() > 0);
191  }
192  inline void AddFace(uint32_t nCount){
193  m_nFaceCount += nCount;
194  }
195  inline void AddInstance(uint16_t nIndex, uint32_t nFaceCount = 0){
196  instances.push_back(nIndex);
197  AddFace(nFaceCount);
198  }
199  inline uint32_t GetFaceCount() const { return m_nFaceCount; }
200  void operator+=(InstanceGroup& right){
201  AddFace(right.GetFaceCount());
202  instances.insert(instances.end(), right.instances.begin(), right.instances.end());
203  }
208  bool CanShareRenderBufferWith(InstanceGroup* pOther = NULL);
209  };
211  std::vector<InstanceGroup* >& GetInstanceGroups();
212 
214  std::map<int32_t, int>& GetInstanceMap();
215 
216  static void StaticReleaseInstGroup(std::vector<RenderableChunk::InstanceGroup* >* pInstances);
217 
220 
221  protected:
222  void BuildRenderGroup();
223 
224  void ResetInstanceGroups();
225 
226  int32 BuildInstanceGroupsByIdAndData(BlockChunk* pChunk);
227  void SortAndMergeInstanceGroupsByTexture();
228 
230  ParaVertexBuffer* RequestVertexBuffer(int32 nFaceCountInVertexBuffer);
231  ParaMemoryBuffer RequestMemoryBuffer(int32 nFaceCountInVertexBuffer, int32* pBufferIndex);
232 
233  protected:
234  std::vector<BlockRenderTask*> m_builder_tasks;
235  std::vector<ParaMemoryBuffer> m_memoryBuffers;
236 
237  private:
239  static int s_nTotalRenderableChunks;
240  CBlockWorld* m_pWorld;
241  int16_t m_regionX, m_regionZ;
242  int16_t m_packedChunkID;
243  bool m_isDirty;
244  int32 m_nRenderFrameCount;
245  int32 m_nLastVertexBufferBytes;
248  int32 m_nDelayedRebuildTick;
250  int16 m_nChunkViewDistance;
252  int16 m_nViewIndex;
253 
254  ChunkBuildState m_chunkBuildState;
255 
259  int m_dwShaderID;
260 
261  std::vector<BlockRenderTask*> m_renderTasks;
262  std::vector<ParaVertexBuffer*> m_vertexBuffers;
263  CShapeBox m_pShapeAABB;
264 
265  int32 m_totalFaceCount;
266 
268  bool m_bIsMainRenderer : 1;
270  bool m_bIsDirtyByBlockChange : 1;
271  };
272 }
void UploadFromMemoryToDeviceBuffer()
only call in main thread
Definition: RenderableChunk.cpp:849
pure memory buffer, useful for filling in another thread and then upload to a real vertex buffer in t...
Definition: ParaVertexBuffer.h:87
ParaVertexBuffer * RequestVertexBuffer(int32 nFaceCountInVertexBuffer)
each rectangle face is 2 triangles or 4 vertices.
Definition: RenderableChunk.cpp:491
simple scoped read lock function
Definition: BlockReadWriteLock.h:123
different physics engine has different winding order.
Definition: EventBinding.h:32
bool CanShareRenderBufferWith(InstanceGroup *pOther=NULL)
whether this instance group can share vertex buffer with the other instance, providing that both inst...
Definition: RenderableChunk.cpp:1068
pool of vertex buffer object.
Definition: ParaVertexBufferPool.h:10
void SetChunkDirty(bool isDirty)
only dirty if chunk is first loaded or reused and all render buffer content can not be used to render...
Definition: RenderableChunk.h:83
void RebuildRenderBufferToMemory(Scoped_ReadLock< BlockReadWriteLock > *Lock_, int *pnCpuYieldCount)
call this function to generate all vertices to memory buffer.
Definition: RenderableChunk.cpp:657
int GetShaderID() const
this determined how the vertex buffer format is.
Definition: RenderableChunk.cpp:999
Definition: RenderableChunk.h:17
cross platform vertex/index buffer.
Definition: ParaVertexBuffer.h:18
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
BlockChunk * GetChunk()
get the associated raw chunk data
Definition: RenderableChunk.cpp:436
static int GetTotalRenderableChunks()
total number of renderable chunks in memory.
Definition: RenderableChunk.cpp:984
bool IsDirty() const
only dirty if chunk is first loaded or reused and all render buffer content can not be used to render...
Definition: RenderableChunk.h:91
std::vector< InstanceGroup *> & GetInstanceGroups()
this function returns thread local data
Definition: RenderableChunk.cpp:536
BlockGeneralTessellator & GetBlockTessellator()
get thread local block tessellator
Definition: RenderableChunk.cpp:516
Definition: ShapeSphere.h:6
void FillRenderQueue(CBlockWorld *pWorld)
fill the render queue with buffer data.
base class for an instance of block world
Definition: BlockWorld.h:35
bool RebuildRenderBuffer(CBlockWorld *pWorld, bool bAsyncMode)
every frame, we will ensure some new buffer is updated, and some updated buffer is updated as well...
Definition: RenderableChunk.cpp:58
int GetRenderFrameCount() const
when this chunk should be rendered last time.
Definition: RenderableChunk.cpp:1038
int32 GetDelayedRebuildTick() const
how many render ticks that this chunk has been delayed from buffer rebuilding.
Definition: RenderableChunk.cpp:900
Chunk is a 16*16*16 inside a region.
Definition: BlockChunk.h:138
Int16x3 GetChunkPosWs()
chunk position in world space.
Definition: RenderableChunk.cpp:893
bool GetIsDirtyByBlockChange()
whether this chunk is made dirty by actual block changes.
Definition: RenderableChunk.cpp:415
block template base class.
Definition: BlockTemplate.h:15
int GetLastBufferBytes()
this is usually same as GetVertexBufferBytes(), except that when we clear vertex buffer, this size remain unchanged until the chunk is reused.
Definition: RenderableChunk.cpp:1058
int16 GetViewIndex() const
each chunk has a unique view index, the smaller the closer to the current view center.
Definition: RenderableChunk.cpp:920
custom model tessellation like button, stairs, etc.
Definition: BlockTessellators.h:72
bool IsIntersect(CShapeSphere &sphere)
whether the chunk intersect with the given sphere.
Definition: RenderableChunk.cpp:421
bool ShouldRebuildRenderBuffer(CBlockWorld *pWorld, bool bNewBuffer=true, bool bUpdatedBuffer=true)
every frame, we will ensure some new buffer is updated, and some updated buffer is updated as well...
Definition: RenderableChunk.cpp:46
bool IsNearbyChunksLoaded()
return true if the nearby 4 chunk columns have been generated (and loaded)
Definition: RenderableChunk.cpp:447
bool IsMainRenderer() const
for main renderer (default to true), we will set chuck dirty to false, whenever the buffer is rebuilt...
Definition: RenderableChunk.cpp:1009
void WaitUntilChunkReady()
only used when the chunk is destroyed.
Definition: RenderableChunk.cpp:648
std::map< int32_t, int > & GetInstanceMap()
mapping from a hashed value of block_template id and template data if any.
Definition: RenderableChunk.cpp:544
int16 GetChunkViewDistance() const
distance to current camera in chunk space.
Definition: RenderableChunk.cpp:910
512*512 region
Definition: BlockRegion.h:22
int GetVertexBufferBytes()
get the current size of all of its vertex buffer.
Definition: RenderableChunk.cpp:1048
basic block world coordinate
Definition: BlockCoordinate.h:10
Definition: RenderableChunk.h:175
a min max box.
Definition: ShapeAABB.h:190