2011-10-25 05:03:10 +00:00
//
2013-01-09 22:37:23 +00:00
// KRMeshManager.h
2012-03-15 20:09:01 +00:00
// KREngine
2011-10-25 05:03:10 +00:00
//
2012-03-15 20:09:01 +00:00
// Copyright 2012 Kearwood Gilbert. All rights reserved.
2011-10-25 06:16:47 +00:00
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
2011-10-25 05:03:10 +00:00
//
2013-01-09 22:37:23 +00:00
# ifndef KRMESHMANAGER_H
# define KRMESHMANAGER_H
2011-10-25 05:03:10 +00:00
2013-01-11 03:21:19 +00:00
# include "KREngine-common.h"
# include "KRContextObject.h"
# include "KRDataBlock.h"
2013-03-21 19:58:35 -07:00
# include "KRNode.h"
2012-03-15 23:58:37 +00:00
2012-04-12 19:43:08 +00:00
class KRContext ;
2013-01-09 22:37:23 +00:00
class KRMesh ;
2011-10-25 05:03:10 +00:00
2013-01-09 22:37:23 +00:00
class KRMeshManager : public KRContextObject {
2011-10-25 05:03:10 +00:00
public :
2012-11-23 01:02:22 +00:00
static const int KRENGINE_MAX_VOLUMETRIC_PLANES = 500 ;
static const int KRENGINE_MAX_RANDOM_PARTICLES = 150000 ;
2012-11-14 21:46:30 +00:00
2013-01-09 22:37:23 +00:00
KRMeshManager ( KRContext & context ) ;
virtual ~ KRMeshManager ( ) ;
2011-10-25 05:03:10 +00:00
2013-03-21 17:32:26 -07:00
void startFrame ( float deltaTime ) ;
2013-03-21 19:58:35 -07:00
void endFrame ( float deltaTime ) ;
2014-05-27 22:51:52 -07:00
void firstFrame ( ) ;
2012-09-13 20:09:19 +00:00
2013-01-09 22:37:23 +00:00
KRMesh * loadModel ( const char * szName , KRDataBlock * pData ) ;
std : : vector < KRMesh * > getModel ( const char * szName ) ;
void addModel ( KRMesh * model ) ;
2011-10-25 05:03:10 +00:00
2012-04-05 21:18:25 +00:00
std : : vector < std : : string > getModelNames ( ) ;
2013-04-25 17:23:36 -07:00
unordered_multimap < std : : string , KRMesh * > & getModels ( ) ;
2012-04-05 21:18:25 +00:00
2014-05-15 23:33:01 -07:00
class KRVBOData {
public :
2014-05-27 22:51:52 -07:00
typedef enum {
STREAMING ,
CONSTANT ,
TEMPORARY
} vbo_type ;
2014-05-15 23:33:01 -07:00
KRVBOData ( ) ;
2014-05-27 22:51:52 -07:00
KRVBOData ( KRMeshManager * manager , KRDataBlock & data , KRDataBlock & index_data , int vertex_attrib_flags , bool static_vbo , vbo_type t ) ;
void init ( KRMeshManager * manager , KRDataBlock & data , KRDataBlock & index_data , int vertex_attrib_flags , bool static_vbo , vbo_type t ) ;
2014-05-15 23:33:01 -07:00
~ KRVBOData ( ) ;
KRDataBlock * m_data ;
KRDataBlock * m_index_data ;
2014-05-27 22:51:52 -07:00
bool isVBOLoaded ( ) { return m_is_vbo_loaded ; }
bool isVBOReady ( ) { return m_is_vbo_ready ; }
2014-05-15 23:33:01 -07:00
void load ( ) ;
void unload ( ) ;
void bind ( ) ;
// Disable copy constructors
KRVBOData ( const KRVBOData & o ) = delete ;
KRVBOData ( KRVBOData & o ) = delete ;
2014-06-03 02:40:56 -07:00
long getSize ( ) { return m_size ; }
2014-05-15 23:33:01 -07:00
2014-05-25 23:11:15 -07:00
void resetPoolExpiry ( float lodCoverage ) ;
2014-05-27 22:51:52 -07:00
long getLastFrameUsed ( ) { return m_last_frame_used ; }
vbo_type getType ( ) { return m_type ; }
float getStreamPriority ( ) ;
void _swapHandles ( ) ;
2014-05-25 23:11:15 -07:00
2014-05-15 23:33:01 -07:00
private :
2014-05-25 23:11:15 -07:00
KRMeshManager * m_manager ;
2014-05-15 23:33:01 -07:00
int m_vertex_attrib_flags ;
GLuint m_vbo_handle ;
GLuint m_vbo_handle_indexes ;
GLuint m_vao_handle ;
GLsizeiptr m_size ;
2014-05-25 23:11:15 -07:00
long m_last_frame_used ;
float m_last_frame_max_lod_coverage ;
2014-05-27 22:51:52 -07:00
vbo_type m_type ;
bool m_static_vbo ;
bool m_is_vbo_loaded ;
bool m_is_vbo_ready ;
2014-05-15 23:33:01 -07:00
} ;
2014-05-27 22:51:52 -07:00
void bindVBO ( KRVBOData * vbo_data , float lodCoverage ) ;
void bindVBO ( KRDataBlock & data , KRDataBlock & index_data , int vertex_attrib_flags , bool static_vbo , float lodCoverage ) ;
2012-09-13 20:09:19 +00:00
void unbindVBO ( ) ;
2012-09-11 04:32:04 +00:00
long getMemUsed ( ) ;
2013-03-21 17:32:26 -07:00
long getMemActive ( ) ;
2012-09-11 03:06:35 +00:00
2014-05-13 23:49:03 -07:00
static void configureAttribs ( __int32_t attributes ) ;
2012-11-02 20:50:45 +00:00
typedef struct {
GLfloat x ;
GLfloat y ;
GLfloat z ;
2017-07-29 01:24:49 -07:00
} Vector3D ;
2012-11-02 20:50:45 +00:00
typedef struct {
GLfloat u ;
GLfloat v ;
} TexCoord ;
typedef struct {
2017-07-29 01:24:49 -07:00
Vector3D vertex ;
2012-11-02 20:50:45 +00:00
TexCoord uva ;
} RandomParticleVertexData ;
2012-11-09 09:18:38 +00:00
typedef struct {
2017-07-29 01:24:49 -07:00
Vector3D vertex ;
2012-11-09 09:18:38 +00:00
} VolumetricLightingVertexData ;
2013-03-21 17:32:26 -07:00
2013-11-09 16:08:08 -08:00
KRDataBlock & getRandomParticles ( ) ;
KRDataBlock & getVolumetricLightingVertexes ( ) ;
2012-11-02 20:50:45 +00:00
2013-03-21 17:32:26 -07:00
long getMemoryTransferedThisFrame ( ) ;
int getActiveVBOCount ( ) ;
2013-03-21 19:58:35 -07:00
struct draw_call_info {
KRNode : : RenderPass pass ;
char object_name [ 256 ] ;
char material_name [ 256 ] ;
int vertex_count ;
} ;
void log_draw_call ( KRNode : : RenderPass pass , const std : : string & object_name , const std : : string & material_name , int vertex_count ) ;
std : : vector < draw_call_info > getDrawCalls ( ) ;
2013-03-21 17:32:26 -07:00
2013-11-09 16:08:08 -08:00
2014-05-15 23:33:01 -07:00
KRVBOData KRENGINE_VBO_DATA_3D_CUBE_VERTICES ;
KRVBOData KRENGINE_VBO_DATA_2D_SQUARE_VERTICES ;
void doStreaming ( long & memoryRemaining , long & memoryRemainingThisFrame ) ;
private :
2013-11-09 16:08:08 -08:00
KRDataBlock KRENGINE_VBO_3D_CUBE_VERTICES , KRENGINE_VBO_3D_CUBE_INDEXES ;
__int32_t KRENGINE_VBO_3D_CUBE_ATTRIBS ;
KRDataBlock KRENGINE_VBO_2D_SQUARE_VERTICES , KRENGINE_VBO_2D_SQUARE_INDEXES ;
__int32_t KRENGINE_VBO_2D_SQUARE_ATTRIBS ;
2013-04-25 17:23:36 -07:00
unordered_multimap < std : : string , KRMesh * > m_models ; // Multiple models with the same name/key may be inserted, representing multiple LOD levels of the model
2012-09-11 03:06:35 +00:00
2012-09-11 04:32:04 +00:00
long m_vboMemUsed ;
2014-05-13 23:49:03 -07:00
KRVBOData * m_currentVBO ;
2012-09-11 03:06:35 +00:00
2014-05-13 23:49:03 -07:00
unordered_map < KRDataBlock * , KRVBOData * > m_vbosActive ;
2014-05-27 22:51:52 -07:00
std : : vector < std : : pair < float , KRVBOData * > > m_activeVBOs_streamer ;
std : : vector < std : : pair < float , KRVBOData * > > m_activeVBOs_streamer_copy ;
2012-11-02 20:50:45 +00:00
2013-11-09 16:08:08 -08:00
KRDataBlock m_randomParticleVertexData ;
KRDataBlock m_volumetricLightingVertexData ;
2012-11-09 09:18:38 +00:00
2013-03-21 17:32:26 -07:00
long m_memoryTransferredThisFrame ;
2013-03-21 19:58:35 -07:00
std : : vector < draw_call_info > m_draw_calls ;
bool m_draw_call_logging_enabled ;
bool m_draw_call_log_used ;
2014-05-27 22:51:52 -07:00
bool m_first_frame ;
std : : mutex m_streamerFenceMutex ;
bool m_streamerComplete ;
void balanceVBOMemory ( long & memoryRemaining , long & memoryRemainingThisFrame ) ;
void primeVBO ( KRVBOData * vbo_data ) ;
2013-03-21 17:32:26 -07:00
2011-10-25 05:03:10 +00:00
} ;
# endif