BVB Source Codes

CRYENGINE Show MergedMeshRenderNode.h Source code

Return Download CRYENGINE: download MergedMeshRenderNode.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef _PROCEDURALVEGETATION_RENDERNODE_
  4. #define _PROCEDURALVEGETATION_RENDERNODE_
  5.  
  6. // Global define to enable and disable some debugging features (defined &
  7. // described below) to help finding issues in merged meshes at runtime.
  8. #define MMRM_DEBUG 1
  9.  
  10. // Enable this define to include code that performs debug visualization of the
  11. // state of merged meshes
  12. #define MMRM_RENDER_DEBUG 1
  13.  
  14. // Enable this define to perform memory footprint monitoring
  15. #define MMRM_SIZE_INFO 1
  16.  
  17. // The maximum number of potential colliders is set below. If more potential
  18. // colliders are found than this limit below, only this number of colliders
  19. // will be respected
  20. #define MMRM_MAX_COLLIDERS (48)
  21.  
  22. // The maximum number of potential projectiles is set below. If more potential
  23. // projectiles are found than this limit below, only this number of projectiles
  24. // will be respected
  25. #define MMRM_MAX_PROJECTILES (12)
  26.  
  27. // The maximum number of contacts that a deformable is allowed to generate.
  28. #define MMRM_MAX_CONTACTS (32)
  29.  
  30. // Disable this define to run all mergedmesh related jobs sequentially, may or
  31. // may not help find bugs.
  32. #define MMRM_USE_JOB_SYSTEM 1
  33.  
  34. // The number of wind force samples along each principle axis.
  35. #define MMRM_WIND_DIM 4
  36.  
  37. // The dimensions of the density grid on each patch (16x16x16 means one sample every metre)
  38. #define MMRM_DENSITY_DIM 16
  39.  
  40. // Enable code that checks for boundaries when accessing and writing back the
  41. // interleaved baked buffers.
  42. #define MMRM_USE_BOUNDS_CHECK 0
  43.  
  44. // Enable mmrm local debug printing statements
  45. #define MMRM_DEBUG_PRINT 0
  46.  
  47. // Enable mmrm - local assertions
  48. #define MMRM_DEBUG_ASSERT 0
  49.  
  50. // Set this to one to enable full fp32 simulation
  51. #define MMRM_SIMULATION_USES_FP32 1
  52.  
  53. // Enforce bending via relative angles : Ensure that the spine local-space
  54. // angle relative to its parent does not differ too far from the resting
  55. // pose. If so a simple lerp is performed to ensure (not correct, but efficient
  56. // & sufficient).
  57. #define MMRM_SPINE_SLERP_BENDING 1
  58.  
  59. // Enforce bending - ensure that the height of the current spine vertex
  60. // relative to the line segment defined by it's immediate neighbour vertices
  61. // matches the initial height recorded in it's rest-pose
  62. #define MMRM_SPINE_HEIGHT_BENDING 0
  63.  
  64. // Bending enforcement options are mutually exclusive
  65. #if MMRM_SPINE_HEIGHT_BENDING && MMRM_SPINE_SLERP_BENDING
  66.         #error Please select either MMRM_SPINE_HEIGHT_BENDING or MMRM_SPINE_SLERP_BENDING
  67. #endif
  68.  
  69. // Visualize the wind sample grid (NOTE: requires USE_JOB_SYSTEM to be false!)
  70. #define MMRM_VISUALIZE_WINDFIELD 0
  71.  
  72. // Visualize the wind samples from the wind grid (NOTE: requires USE_JOB_SYSTEM
  73. // to be false!)
  74. #define MMRM_VISUALIZE_WINDSAMPLES 0
  75.  
  76. // Visualize the forces acting on the simulated particles (NOTE: requires
  77. // USE_JOB_SYSTEM to be false!)
  78. #define MMRM_VISUALIZE_FORCES 0
  79.  
  80. // Enables/disables the use of vectorized instructions for large parts of the
  81. // simulation / skinning
  82. #define MMRM_USE_VECTORIZED_SSE_INSTRUCTIONS CRY_PLATFORM_SSE2
  83.  
  84. // Enables/disables the use of the frameprofiler for mergedmesh geometry stages
  85. #define MMRM_ENABLE_PROFILER 1
  86.  
  87. // Unrolls the geometry baking loops, can result in very large functions
  88. // exceeding the instruction cache footprint. Best to be turned off on AMD
  89. // Jaguar platforms, as the tight loop exceeds the 32kb L1I cache size
  90. #define MMRM_UNROLL_GEOMETRY_BAKING_LOOPS 1
  91. #if CRY_PLATFORM_DURANGO || CRY_PLATFORM_ORBIS
  92.         #undef MMRM_UNROLL_GEOMETRY_BAKING_LOOPS
  93.         #define MMRM_UNROLL_GEOMETRY_BAKING_LOOPS 0
  94. #endif
  95.  
  96. // Number of instances that will submitted per job
  97. #define MMRM_MAX_SAMPLES_PER_BATCH (1024u)
  98.  
  99. // disable vectorized instructions in debug builds
  100. #if defined(_DEBUG)
  101.         #undef MMRM_USE_VECTORIZED_SSE_INSTRUCTIONS
  102.         #define MMRM_USE_VECTORIZED_SSE_INSTRUCTIONS 0
  103. #endif
  104. // The compile-time default poolsize in bytes
  105. #define MMRM_DEFAULT_POOLSIZE_STR "2750"
  106.  
  107. // Increase the damping for this amount of time when undergoing severe contacts
  108. #define MMRM_PLASTICITY_TIME (10000)
  109.  
  110. // The fixed timestep for the simulator
  111. #define MMRM_FIXED_STEP (0.035f)
  112.  
  113. // The enable debug visualization of the clusters
  114. #define MMRM_CLUSTER_VISUALIZATION 1
  115.  
  116. #if !MMRM_DEBUG || defined(_RELEASE)
  117.         #undef MEMORY_ENABLE_DEBUG_HEAP
  118.         #undef MMRM_RENDER_DEBUG
  119.         #undef MMRM_PROFILE
  120.         #undef MMRM_SIZE_INFO
  121.         #undef MMRM_CLUSTER_VISUALIZATION
  122.         #define MEMORY_ENABLE_DEBUG_HEAP   0
  123.         #define MMRM_RENDER_DEBUG          0
  124.         #define MMRM_PROFILE               0
  125.         #define MMRM_SIZE_INFO             0
  126.         #define MMRM_CLUSTER_VISUALIZATION 0
  127. #endif
  128.  
  129. #if MMRM_DEBUG_PRINT
  130.         #define mmrm_printf(...) CryLog(__VA_ARGS__)
  131. #endif
  132. #ifndef mmrm_printf
  133.         #define mmrm_printf(...) (void)0
  134. #endif
  135.  
  136. #if MMRM_DEBUG_ASSERT
  137.         #define mmrm_assert(x) do { if ((x) == false) __debugbreak(); } while (false)
  138. #endif
  139. #ifndef mmrm_assert
  140.         #define mmrm_assert(x) (void)0
  141. #endif
  142.  
  143. ////////////////////////////////////////////////////////////////////////////////
  144. // Forward declarations
  145. struct SMMRMGroupHeader;
  146. struct SMMRM;
  147. struct SSampleDensity;
  148. struct SProcVegSample;
  149. struct SMMRMProfilingInfo;
  150. struct SMergedMeshPriorityCmp;
  151. struct SMMRMProjectile;
  152. struct SMergedMeshSectorChunk;
  153. struct SMergedMeshInstanceCompressed;
  154.  
  155. ////////////////////////////////////////////////////////////////////////////////
  156. // RenderNode for merged meshes
  157. //
  158. // Merged meshes work by storing multiple linear lists of instances (currently
  159. // position, scale and rotation) each associated with a specially preprocessed
  160. // cgf. If the merged mesh rendernode is visible, a culling job per instance
  161. // type is spawned that marks the instances that are visible and also
  162. // accumulates the required buffer sizes for the rendermesh that will be used
  163. // to render the instances in least number of drawcalls possible (taking size
  164. // limitations (0xffff max vertex count), material switches and other
  165. // limitations into account). After culling, the resulting rendermesh(es) will
  166. // be created and the buffer's queried for their data location and finally an
  167. // update job is spawned for each visible instance type that will bake the
  168. // actual per-instance mesh into the large buffer at the pre-determined offsets
  169. // calculated during culling.
  170. //
  171. // If the instance type's associated cgf contains touchbending (foliage spines)
  172. // , an additional simple simulation pass is triggered that will simulate the
  173. // bones with a simple one-iteration PDB based approach and skin the resulting
  174. // instance. Proper wind-sampling is taken into account.
  175. //
  176. // To alleviate the storage requirements of the instance list (and the
  177. // simulation state should touch bending be enabled on an instance type), the
  178. // actual storage can be streamed out and in on demand on runtime.
  179. //
  180. // ToDo items:
  181. //
  182. //  - The rendermesh is re-created every frame and not pooled currently, so
  183. // vram can (and will) run out of bounds if care is not taken to reduce the
  184. // load. This will be addressed in a near-future commit.
  185. //
  186. //  - The cgf is preprocessed at runtime, this can be moved to level export time
  187. //
  188. class CMergedMeshRenderNode
  189.         : public IRenderNode
  190.           , public IStreamCallback
  191.           , public Cry3DEngineBase
  192. {
  193.         friend class COctreeNode;
  194.         friend class CMergedMeshesManager;
  195.         friend struct SMergedMeshInstanceSorter;
  196.         friend class CTerrain;
  197.         // FrameID of the last frame this rendernode was drawn and updated
  198.         uint32 m_LastDrawFrame;
  199.         uint32 m_LastUpdateFrame;
  200.  
  201.         // The state the rendernode can reside in
  202.         enum State
  203.         {
  204.                 INITIALIZED = 0,    // initialized (created)
  205.                 DIRTY,              // changes to the instance list or any attached cgf has been made
  206.                 PREPARING,          // the attached cgfs are being preprocessed asynchronously
  207.                 PREPARED,           // default state if instance data is not present (opposite of streamed in)
  208.                 STREAMING,          // instance data is being streamed in
  209.                 STREAMED_IN,        // instance data is present and the rendernode can be rendered
  210.                 RENDERNODE_STATE_ERROR
  211.         };
  212.         inline const char* ToString(State state) const
  213.         {
  214.                 switch (state)
  215.                 {
  216.                 case INITIALIZED:
  217.                         return "INITIALIZED";
  218.                 case DIRTY:
  219.                         return "DIRTY";
  220.                 case PREPARING:
  221.                         return "PREPARING";
  222.                 case PREPARED:
  223.                         return "PREPARED";
  224.                 case STREAMING:
  225.                         return "STREAMING";
  226.                 case STREAMED_IN:
  227.                         return "STREAMED_IN";
  228.                 case RENDERNODE_STATE_ERROR:
  229.                         return "ERROR";
  230.                 }
  231.                 return "UNKNOWN";
  232.         }
  233.         volatile State m_State;
  234.         enum RENDERMESH_UPDATE_TYPE
  235.         {
  236.                 RUT_STATIC  = 0,
  237.                 RUT_DYNAMIC = 1,
  238.         };
  239.  
  240.         // The number of instances this rendernode maintains
  241.         size_t         m_Instances;
  242.         mutable size_t m_SpineCount;
  243.         mutable size_t m_DeformCount;
  244.  
  245.         // The distance to camera
  246.         float m_DistanceSQ;
  247.  
  248.         // The internal extents used as to determine the unique spatial hash of this
  249.         // rendernode
  250.         AABB m_internalAABB;
  251.  
  252.         // The extents of the instance geometry attached to this rendernode
  253.         AABB m_visibleAABB;
  254.  
  255.         // Position of this rendernode - corresponds to the center of the internal
  256.         // aabb
  257.         Vec3 m_pos;
  258.  
  259.         // Initial position of this rendernode before applying world shift
  260.         Vec3 m_initPos;
  261.  
  262.         // A z rotation to apply to this rendernode
  263.         float m_zRotation;
  264.  
  265.         // The (external) lod
  266.         int m_nLod;
  267.  
  268.         // Is the mesh already put into the active list?
  269.         unsigned int m_nActive : 1;
  270.  
  271.         // Is the mesh already put into the visible list?
  272.         unsigned int m_nVisible : 1;
  273.  
  274.         // Does the mesh need a static mesh update
  275.         unsigned int m_needsStaticMeshUpdate : 1;
  276.  
  277.         // Does the mesh need a dynamic mesh update
  278.         unsigned int m_needsDynamicMeshUpdate : 1;
  279.  
  280.         // Is the mesh needs a static mesh update
  281.         unsigned int m_needsPostRenderStatic : 1;
  282.  
  283.         // Is the mesh needs a static mesh update
  284.         unsigned int m_needsPostRenderDynamic : 1;
  285.  
  286.         // If the mesh owns its groups
  287.         unsigned int m_ownsGroups : 1;
  288.  
  289.         // The render state
  290.         enum RenderMode
  291.         {
  292.                 DYNAMIC = 0, // dynamic - prebaked & optionally simualted unique meshes
  293.                 INSTANCED,   // instanced - preprocessed
  294.                 NOT_VISIBLE, // simply not visible
  295.         };
  296.         RenderMode m_RenderMode;
  297.  
  298.         // VRAM Memory footprint of the corresponding buffers in bytes
  299.         uint32 m_SizeInVRam;
  300.  
  301.         // The instance lists.
  302.         SMMRMGroupHeader* m_groups;
  303.         uint32            m_nGroups;
  304.  
  305.         // Stored render params because this rendernode is rendered in a two-stage
  306.         // process.
  307.         SRendParams m_rendParams;
  308.  
  309.         // The wind force sample lattice. Get's bilinearly interpolated during the
  310.         // simulation.
  311.         Vec3* m_wind;
  312.  
  313.         // The list of surface types used by the instances in this node
  314.         ISurfaceType* m_surface_types[MMRM_MAX_SURFACE_TYPES];
  315.  
  316.         // The density samples
  317.         SSampleDensity* m_density;
  318.  
  319.         // The list of colliders, approximated as spheres. Probably more elaborate
  320.         // primitives will be supported in the future
  321.         primitives::sphere* m_Colliders;
  322.  
  323.         // Number of active colliders
  324.         int m_nColliders;
  325.  
  326.         // The list of potential projectiles, approximated as spheres and directions.
  327.         SMMRMProjectile* m_Projectiles;
  328.  
  329.         // Number of active projectiles
  330.         int m_nProjectiles;
  331.  
  332.         // The jobstate for all cull jobs
  333.         JobManager::SJobState m_cullState;
  334.  
  335.         // The jobstate for all mesh update jobs
  336.         JobManager::SJobState m_updateState;
  337.  
  338.         // The jobstate for initializing spines
  339.         JobManager::SJobState m_spineInitializationState;
  340.  
  341.         // The list of rendermeshes that belong to this object
  342.         std::vector<SMMRM> m_renderMeshes[2];
  343.  
  344.         // The list of rendermeshes that belong to this object
  345.         std::vector<std::pair<IMaterial*, IStatObj*>> m_usedMaterials;
  346.  
  347.         // Only walid if we are currently streaming in data
  348.         IReadStream_AutoPtr m_pReadStream;
  349.  
  350. #ifdef SEG_WORLD
  351.         uint16 m_nStaticTypeSlot;
  352. #endif
  353.  
  354.         // Are spines active and present in memory?
  355.         bool m_SpinesActive : 1;
  356.  
  357.         // Debug visualization (gets the lod passed the rendernode has selected in)
  358.         void DebugRender(int nLod);
  359.  
  360.         // Checks if a member of a group requests to cast shadows
  361.         bool GroupsCastShadows(RENDERMESH_UPDATE_TYPE type);
  362.  
  363.         // Submits the merged rendermesh to the renderer
  364.         void RenderRenderMesh(
  365.           RENDERMESH_UPDATE_TYPE type
  366.           , const float fDistance
  367.           , const SRenderingPassInfo& passInfo
  368.           );
  369.  
  370.         // Creates the rendermesh based on the results of the culling jobs
  371.         // Note: The culling jobs have to have all completed *before* calling this method
  372.         void CreateRenderMesh(RENDERMESH_UPDATE_TYPE, const SRenderingPassInfo &passInfo);
  373.  
  374.         // Deletes the rendermesh - note if any update jobs are still in flight, this
  375.         // call will block
  376.         // The zap parameter actually frees the memory of the std vectors in flight
  377.         bool DeleteRenderMesh(RENDERMESH_UPDATE_TYPE, bool block = true, bool zap = false);
  378.  
  379.         // Print the state to the screen
  380.         void PrintState(float&);
  381.  
  382.         // Calculate the sample density grid.
  383.         void CalculateDensity();
  384.  
  385.         // Query the sample density grid. Returns the number of surface types filled into the surface types list
  386.         size_t QueryDensity(const Vec3& pos, IMergedMeshesManager::TFixedSurfacePtrTypeArray& surfaceTypes, IMergedMeshesManager::TFixedDensityArray& density);
  387.  
  388.         // Resets the node to the initial dirty state
  389.         void Reset();
  390.  
  391. public:
  392.         CMergedMeshRenderNode();
  393.         virtual ~CMergedMeshRenderNode();
  394.  
  395.         // Set the configured position and offset and create the sample maps
  396.         bool Setup(const AABB& aabb, const AABB& visAbb, const PodArray<SProcVegSample>*);
  397.  
  398.         // Add a instance type to the merged mesh rendernode and preallocate memory
  399.         // for the actual instances
  400.         // Returns false if the group cannot be created for some reason.
  401.         bool        AddGroup(uint32 statInstGroupId, uint32 numInstances);
  402.         // Return the (internal) size of this rendernode
  403.         const AABB& GetExtents() const { return m_internalAABB; }
  404.  
  405.         // Adds an instance to the map
  406.         IRenderNode* AddInstance(const SProcVegSample&);
  407.         // Removes an instance from the map
  408.         size_t RemoveInstance(size_t, size_t);
  409.  
  410.         // Clear the instance map data - zaps everything
  411.         void       Clear();
  412.  
  413.         size_t     SizeInVRam() const      { return m_SizeInVRam; }
  414.  
  415.         const AABB GetInternalBBox() const { return m_internalAABB; }
  416.  
  417.         bool       IsDynamic() const       { return m_RenderMode == DYNAMIC; }
  418.  
  419.         // Access to the instance types of this
  420.         uint32                  NumGroups() const;
  421.         const SMMRMGroupHeader* Group(size_t index) const;
  422.  
  423.         // Returns the (mainmemory) size in bytes of the allocated internal structures to represent
  424.         // all the instances.
  425.         uint32 MetaSize() const;
  426.  
  427.         // Returns the number of visible instances
  428.         uint32 VisibleInstances(uint32 frameId) const;
  429.  
  430.         // Returns the number of spine instances
  431.         uint32 SpineCount() const;
  432.  
  433.         // Returns the number of deformed instances
  434.         uint32 DeformCount() const;
  435.  
  436.         // Are spines active in memory?
  437.         bool SpinesActive() const { return m_SpinesActive; }
  438.  
  439.         // Activate Spines in memory
  440.         void ActivateSpines();
  441.  
  442.         // Remove the spines from memory
  443.         void RemoveSpines();
  444.  
  445.         // Is visible?
  446.         bool Visible() const;
  447.  
  448.         // Distance to Camera
  449.         float DistanceSq() const { return m_DistanceSQ; }
  450.  
  451.         // Active/Inactive flags
  452.         int  IsActive() const             { return m_nActive; }
  453.         void SetActive(int active)        { m_nActive = active; }
  454.  
  455.         int  IsInVisibleSet() const       { return m_nVisible; }
  456.         void SetInVisibleSet(int visible) { m_nVisible = visible; }
  457.  
  458.         //
  459.         void OverrideViewDistRatio(float value);
  460.         void OverrideLodRatio(float value);
  461.  
  462.         // Important Note: This method should not be called from any client code,
  463.         // it is called implicitly from Setup. Sadly, it has to be public
  464.         // because DECLARE_CLASS_JOB macros only work on public methods.
  465.         bool PrepareRenderMesh(RENDERMESH_UPDATE_TYPE);
  466.  
  467.         // Compile the instance data into a streamable chunk
  468.         bool Compile(byte* pData, int& nSize, string* pName, std::vector<struct IStatInstGroup*>* pVegGroupTable, const Vec3& segmentOffset = Vec3(0, 0, 0));
  469.  
  470.         // Fills the array full of samples
  471.         void FillSamples(DynArray<Vec2>&);
  472.  
  473.         // Stream in data from the filesystem for this rendernode
  474.         // Note the extents of the internal aabb are used to determine the filename
  475.         bool StreamIn();
  476.  
  477.         // Flush out any allocated data
  478.         bool StreamOut();
  479.  
  480.         bool SyncAllJobs();
  481.  
  482.         bool StreamedIn() const { return m_State == STREAMED_IN; }
  483.  
  484.         // Update streamable components
  485.         bool UpdateStreamableComponents(float fImportance, float fEntDistance, bool bFullUpdate);
  486.  
  487.         Vec3 GetSamplePos(size_t, size_t) const;
  488.         AABB GetSampleAABB(size_t, size_t) const;
  489.  
  490.         bool PostRender(const SRenderingPassInfo& passInfo);
  491.  
  492.         // util job entry function used by AsyncStreaming Callback
  493.         void InitializeSamples(float fExtents, const uint8* pBuffer);
  494.         // util job entry function used by Activate Spines
  495.         void InitializeSpines();
  496.  
  497.         void QueryColliders();
  498.         void QueryProjectiles();
  499.         void SampleWind();
  500.  
  501.         //////////////////////////////////////////////////////////////////////
  502.         // Inherited from IStreamCallback
  503.         //////////////////////////////////////////////////////////////////////
  504.         void StreamAsyncOnComplete(IReadStream* pStream, unsigned nError);
  505.         void StreamOnComplete(IReadStream* pStream, unsigned nError);
  506.  
  507.         //////////////////////////////////////////////////////////////////////
  508.         // Inherited from IRenderNode
  509.         //////////////////////////////////////////////////////////////////////
  510.         const char*             GetName() const             { return "Runtime MergedMesh";  }
  511.         const char*             GetEntityClassName() const  { return "Runtime MergedMesh"; };
  512.         Vec3                    GetPos(bool bWorldOnly = true) const;
  513.         const AABB              GetBBox() const             { return m_visibleAABB; };
  514.         void                    SetBBox(const AABB& WSBBox) {};
  515.         void                    FillBBox(AABB& aabb);
  516.         void                    OffsetPosition(const Vec3& delta);
  517.         void                    Render(const struct SRendParams& EntDrawParams, const SRenderingPassInfo& passInfo);
  518.  
  519.         struct IPhysicalEntity* GetPhysics() const                 { return NULL; };
  520.         void                    SetPhysics(IPhysicalEntity* pPhys) {};
  521.         void                    SetMaterial(IMaterial* pMat)       {}
  522.         IMaterial*              GetMaterial(Vec3* pHitPos = NULL) const;
  523.         IMaterial*              GetMaterialOverride()              { return NULL; }
  524.         EERType                 GetRenderNodeType();
  525.         float                   GetMaxViewDist();
  526.         void                    GetMemoryUsage(ICrySizer* pSizer) const {}
  527.  
  528.         ILINE StatInstGroup&    GetStatObjGroup(const int index) const
  529.         {
  530. #ifdef SEG_WORLD
  531.                 return GetObjManager()->m_lstStaticTypes[m_nStaticTypeSlot][index];
  532. #else
  533.                 return GetObjManager()->m_lstStaticTypes[0][index];
  534. #endif
  535.         }
  536.         ILINE CStatObj* GetStatObj(const int index) const
  537.         {
  538. #ifdef SEG_WORLD
  539.                 return GetObjManager()->m_lstStaticTypes[m_nStaticTypeSlot][index].GetStatObj();
  540. #else
  541.                 return GetObjManager()->m_lstStaticTypes[0][index].GetStatObj();
  542. #endif
  543.         }
  544. };
  545.  
  546. ////////////////////////////////////////////////////////////////////////////////
  547. // RenderNode Proxy node sandbox
  548. //
  549. // Instances of this node are just a refcounting wrapper exposed to the editor
  550. // during vegetation editing. Construction and destruction of instances of this
  551. // class correspond to adding and removing instances to a real
  552. // MergedMeshRendernode
  553. //
  554. class CMergedMeshInstanceProxy
  555.         : public IRenderNode
  556.           , public Cry3DEngineBase
  557. {
  558.         friend class CMergedMeshRenderNode;
  559.  
  560.         CMergedMeshRenderNode* m_host;
  561.         size_t                 m_headerIndex;
  562.         size_t                 m_sampleIndex;
  563.  
  564.         CMergedMeshInstanceProxy(CMergedMeshRenderNode*, size_t, size_t);
  565.  
  566. public:
  567.         CMergedMeshInstanceProxy();
  568.         virtual ~CMergedMeshInstanceProxy();
  569.  
  570.         //////////////////////////////////////////////////////////////////////
  571.         // Inherited from IRenderNode
  572.         //////////////////////////////////////////////////////////////////////
  573.         const char*             GetName() const                                                                     { return "Runtime MergedMesh Proxy Instance";  }
  574.         const char*             GetEntityClassName() const                                                          { return "Runtime MergedMesh Proxy Instance"; };
  575.         Vec3                    GetPos(bool bWorldOnly = true) const                                                { return m_host->GetSamplePos(m_headerIndex, m_sampleIndex); };
  576.         const AABB              GetBBox() const                                                                     { return m_host->GetSampleAABB(m_headerIndex, m_sampleIndex); };
  577.         void                    SetBBox(const AABB& WSBBox)                                                         { __debugbreak(); };
  578.         void                    OffsetPosition(const Vec3& delta)                                                   {}
  579.         void                    Render(const struct SRendParams& EntDrawParams, const SRenderingPassInfo& passInfo) { __debugbreak(); }
  580.  
  581.         struct IPhysicalEntity* GetPhysics() const                                                                  { __debugbreak(); return NULL; };
  582.         void                    SetPhysics(IPhysicalEntity* pPhys)                                                  { __debugbreak(); };
  583.         void                    SetMaterial(IMaterial* pMat)                                                        { __debugbreak(); }
  584.         IMaterial*              GetMaterial(Vec3* pHitPos = NULL) const                                             { __debugbreak(); return NULL; }
  585.         IMaterial*              GetMaterialOverride()                                                               { __debugbreak(); return NULL; }
  586.         EERType                 GetRenderNodeType()                                                                 { __debugbreak(); return eERType_MergedMesh; }
  587.         float                   GetMaxViewDist()                                                                    { __debugbreak(); return FLT_MAX; }
  588.         void                    GetMemoryUsage(ICrySizer* pSizer) const                                             { __debugbreak(); }
  589. };
  590.  
  591. ////////////////////////////////////////////////////////////////////////////////
  592. // MergedMesh Manager
  593. //
  594. // Contains a wrapping spatial grid of mergedmesh rendernodes
  595. struct SProjectile
  596. {
  597.         Vec3             current_pos;
  598.         Vec3             initial_pos;
  599.         Vec3             direction;
  600.         float            lifetime;
  601.         float            size;
  602.         IPhysicalEntity* entity;
  603.  
  604.         SProjectile()
  605.                 : current_pos()
  606.                 , initial_pos()
  607.                 , direction()
  608.                 , lifetime()
  609.                 , size()
  610.                 , entity()
  611.         {}
  612.  
  613.         ~SProjectile() { memset(this, 0, sizeof(*this)); }
  614.         bool operator<(const SProjectile& other) const { return entity < other.entity; }
  615. };
  616.  
  617. class CMergedMeshesManager
  618.         : public Cry3DEngineBase
  619.           , public IMergedMeshesManager
  620. {
  621.         friend class CMergedMeshRenderNode;
  622.         friend class CDeformableNode;
  623.         friend inline void QueryProjectiles(SMMRMProjectile*&, int&, const AABB&);
  624.  
  625.         enum { HashDimXY = 32, HashDimZ = 2, MaxProjectiles = 1024, MaxUnloadFrames = 128 };
  626.  
  627.         typedef std::vector<CMergedMeshRenderNode*> NodeListT;
  628.         typedef std::vector<CMergedMeshRenderNode*> NodeArrayT;
  629.         typedef std::vector<SProjectile>            ProjectileArrayT;
  630.         typedef DynArray<SInstanceSector>           InstanceSectors;
  631.  
  632.         // For tracking fast moving projectiles
  633.         static int OnPhysPostStep(const EventPhys*);
  634.  
  635.         NodeArrayT       m_Nodes[HashDimXY][HashDimXY][HashDimZ];
  636.         NodeArrayT       m_ActiveNodes;
  637.         NodeArrayT       m_StreamedOutNodes;
  638.         NodeArrayT       m_VisibleNodes;
  639.         NodeArrayT       m_SegNodes;
  640.         NodeArrayT       m_PostRenderNodes;
  641.         ProjectileArrayT m_Projectiles;
  642.         InstanceSectors  m_InstanceSectors;
  643.         volatile int     m_ProjectileLock;
  644.  
  645.         // The jobstate for all mesh update jobs
  646.         JobManager::SJobState  m_updateState;
  647.  
  648.         size_t                 m_CurrentSizeInVramDynamic;
  649.         size_t                 m_CurrentSizeInVramInstanced;
  650.         size_t                 m_CurrentSizeInMainMem;
  651.         size_t                 m_GeomSizeInMainMem;
  652.         size_t                 m_InstanceCount;
  653.         size_t                 m_VisibleInstances;
  654.         size_t                 m_InstanceSize;
  655.         size_t                 m_SpineSize;
  656.         size_t                 m_nActiveNodes;
  657.         size_t                 m_nStreamedOutNodes;
  658.         bool                   m_PoolOverFlow;
  659.         bool                   m_MeshListPresent;
  660.  
  661.         AABB                   m_CachedBBs[4];
  662.         int                    m_CachedLRU[4];
  663.         CMergedMeshRenderNode* m_CachedNodes[4];
  664.  
  665. #if MMRM_CLUSTER_VISUALIZATION
  666.         DynArray<SMeshAreaCluster> m_clusters;
  667. #endif // MMRM_CLUSTER_VISUALIZATION
  668.  
  669.         CMergedMeshRenderNode* FindNode(const Vec3& pos);
  670.  
  671.         void                   AddProjectile(const SProjectile&);
  672.  
  673. public:
  674.         CMergedMeshesManager();
  675.         ~CMergedMeshesManager();
  676.  
  677.         void Init();
  678.         void Shutdown();
  679.  
  680.         // Called by the
  681.         void UpdateViewDistRatio(float);
  682.         void UpdateLodRatio(float);
  683.  
  684.         // Compile sectors
  685.         bool                                         CompileSectors(std::vector<struct IStatInstGroup*>* pVegGroupTable);
  686.         const IMergedMeshesManager::SInstanceSector& GetInstanceSector(size_t index) const { return m_InstanceSectors[index]; }
  687.         size_t                                       GetInstanceSectorCount() const        { return m_InstanceSectors.size(); }
  688.         void                                         ClearInstanceSectors();
  689.  
  690.         // Compile cluster areas
  691.         bool CompileAreas(DynArray<SMeshAreaCluster>& clusters, int flags);
  692.  
  693.         //      Query the sample density grid. Returns the number of surface types filled into the surface types list
  694.         size_t QueryDensity(const Vec3& pos, IMergedMeshesManager::TFixedSurfacePtrTypeArray& surfaceTypes, IMergedMeshesManager::TFixedDensityArray& density);
  695.  
  696.         // Fill in the density values
  697.         void                   CalculateDensity();
  698.  
  699.         bool                   GetUsedMeshes(DynArray<string>& pMeshNames);
  700.  
  701.         void                   PreloadMeshes();
  702.         bool                   SyncPreparationStep();
  703.  
  704.         IRenderNode*           AddInstance(const SProcVegSample&);
  705.         CMergedMeshRenderNode* GetNode(const Vec3& vPos);
  706.         void                   RemoveMergedMesh(CMergedMeshRenderNode*);
  707.  
  708.         void                   PostRenderMeshes(const SRenderingPassInfo& passInfo);
  709.         void                   RegisterForPostRender(CMergedMeshRenderNode*);
  710.  
  711.         // Called once a frame
  712.         void   Update(const SRenderingPassInfo& passInfo);
  713.  
  714.         void   SortActiveInstances(const SRenderingPassInfo& passInfo);
  715.         void   SortActiveInstances_Async(const SRenderingPassInfo passInfo); //NOTE: called internally
  716.         void   ResetActiveNodes();
  717.  
  718.         size_t CurrentSizeInVram() const          { return CurrentSizeInVramDynamic() + CurrentSizeInVramInstanced(); }
  719.         size_t CurrentSizeInVramDynamic() const   { return m_CurrentSizeInVramDynamic; }
  720.         size_t CurrentSizeInVramInstanced() const { return m_CurrentSizeInVramInstanced; }
  721.         size_t CurrentSizeInMainMem() const       { return m_CurrentSizeInMainMem; }
  722.         size_t GeomSizeInMainMem() const          { return m_GeomSizeInMainMem; }
  723.         size_t InstanceCount() const              { return m_InstanceCount; }
  724.         size_t VisibleInstances() const           { return m_VisibleInstances; }
  725.         size_t InstanceSize() const               { return m_InstanceSize; }
  726.         size_t SpineSize() const                  { return m_SpineSize; }
  727.         size_t ActiveNodes() const                { return m_nActiveNodes; }
  728.         size_t StreamedOutNodes() const           { return m_nStreamedOutNodes; }
  729.         bool   PoolOverFlow() const               { return m_PoolOverFlow; }
  730.  
  731.         void   PrepareSegmentData(const AABB& aabb);
  732.         int    GetSegmentNodeCount() { return m_SegNodes.size(); }
  733.         int    GetCompiledDataSize(uint32 index);
  734.         bool   GetCompiledData(uint32 index, byte* pData, int nSize, string* pName, std::vector<struct IStatInstGroup*>** ppStatInstGroupTable, const Vec3& segmentOffset);
  735. };
  736.  
  737. #endif
  738.  
downloadMergedMeshRenderNode.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top