2012-04-12 19:43:08 +00:00
//
// KRContext.cpp
// KREngine
//
// Created by Kearwood Gilbert on 12-04-12.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
2013-01-11 03:21:19 +00:00
# include "KREngine-common.h"
2012-04-12 19:43:08 +00:00
# include "KRContext.h"
2012-08-17 01:04:49 +00:00
# include "KRCamera.h"
2016-07-10 03:33:58 -07:00
# include "KRAudioManager.h"
# include "KRAudioSample.h"
2012-04-12 19:43:08 +00:00
2018-11-13 18:39:32 +07:00
# if defined(ANDROID)
# include <chrono>
# include <unistd.h>
# endif
2016-08-07 18:39:47 -07:00
# if defined(_WIN32) || defined(_WIN64)
# include <windows.h>
# endif
2012-10-19 23:17:43 +00:00
int KRContext : : KRENGINE_MAX_SHADER_HANDLES ;
2014-06-01 15:34:31 -07:00
int KRContext : : KRENGINE_GPU_MEM_MAX ;
int KRContext : : KRENGINE_GPU_MEM_TARGET ;
2012-10-19 23:17:43 +00:00
int KRContext : : KRENGINE_MAX_TEXTURE_DIM ;
int KRContext : : KRENGINE_MIN_TEXTURE_DIM ;
2014-04-23 01:43:00 -07:00
int KRContext : : KRENGINE_PRESTREAM_DISTANCE ;
2017-07-27 23:29:28 -07:00
int KRContext : : KRENGINE_SYS_ALLOCATION_GRANULARITY ;
int KRContext : : KRENGINE_SYS_PAGE_SIZE ;
2012-10-19 23:17:43 +00:00
2016-01-30 20:13:23 -08:00
# if TARGET_OS_IPHONE
# elif TARGET_OS_MAC
2016-07-10 03:33:58 -07:00
# elif defined(_WIN32) || defined(_WIN64)
2016-01-30 20:13:23 -08:00
2018-11-13 18:39:32 +07:00
# elif defined(ANDROID)
2016-01-30 20:13:23 -08:00
# else
# error Unsupported Platform
# endif
2012-11-01 22:16:59 +00:00
const char * KRContext : : extension_names [ KRENGINE_NUM_EXTENSIONS ] = {
" GL_EXT_texture_storage "
} ;
2013-12-14 16:06:44 -08:00
KRContext : : log_callback * KRContext : : s_log_callback = NULL ;
void * KRContext : : s_log_callback_user_data = NULL ;
2018-08-06 21:34:16 -07:00
KRContext : : KRContext ( )
: m_streamer ( * this )
, m_vulkanInstance ( VK_NULL_HANDLE )
2014-05-13 21:56:06 -07:00
{
2013-11-13 23:52:17 -08:00
m_streamingEnabled = false ;
2016-08-07 18:39:47 -07:00
# ifdef __APPLE__
2013-03-06 15:46:54 -08:00
mach_timebase_info ( & m_timebase_info ) ;
2016-08-07 18:39:47 -07:00
# endif
2013-03-06 15:46:54 -08:00
m_bDetectedExtensions = false ;
m_current_frame = 0 ;
2014-06-01 18:08:28 -07:00
m_last_memory_warning_frame = 0 ;
2014-06-25 00:45:00 -07:00
m_last_fully_streamed_frame = 0 ;
2013-03-06 15:46:54 -08:00
m_absolute_time = 0.0f ;
2012-09-11 03:06:35 +00:00
m_pBundleManager = new KRBundleManager ( * this ) ;
m_pShaderManager = new KRShaderManager ( * this ) ;
m_pTextureManager = new KRTextureManager ( * this ) ;
m_pMaterialManager = new KRMaterialManager ( * this , m_pTextureManager , m_pShaderManager ) ;
2014-05-13 21:56:06 -07:00
m_pMeshManager = new KRMeshManager ( * this ) ;
2012-09-11 03:06:35 +00:00
m_pSceneManager = new KRSceneManager ( * this ) ;
2012-12-01 02:03:18 +00:00
m_pAnimationManager = new KRAnimationManager ( * this ) ;
2012-12-07 00:20:06 +00:00
m_pAnimationCurveManager = new KRAnimationCurveManager ( * this ) ;
2013-01-05 04:04:58 +00:00
m_pSoundManager = new KRAudioManager ( * this ) ;
2013-01-02 22:00:29 +00:00
m_pUnknownManager = new KRUnknownManager ( * this ) ;
2013-11-13 23:52:17 -08:00
m_streamingEnabled = true ;
2017-07-27 23:29:28 -07:00
# if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO winSysInfo ;
GetSystemInfo ( & winSysInfo ) ;
KRENGINE_SYS_ALLOCATION_GRANULARITY = winSysInfo . dwAllocationGranularity ;
KRENGINE_SYS_PAGE_SIZE = winSysInfo . dwPageSize ;
2018-11-13 18:39:32 +07:00
# elif defined(__APPLE__) || defined(ANDROID)
2017-07-27 23:29:28 -07:00
KRENGINE_SYS_PAGE_SIZE = getpagesize ( ) ;
KRENGINE_SYS_ALLOCATION_GRANULARITY = KRENGINE_SYS_PAGE_SIZE ;
# else
# error Unsupported
# endif
2013-12-14 16:06:44 -08:00
2016-01-30 20:13:23 -08:00
createDeviceContexts ( ) ;
2012-04-12 19:43:08 +00:00
}
KRContext : : ~ KRContext ( ) {
2012-09-11 03:06:35 +00:00
2012-04-12 19:43:08 +00:00
if ( m_pSceneManager ) {
delete m_pSceneManager ;
m_pSceneManager = NULL ;
}
2014-05-13 21:56:06 -07:00
if ( m_pMeshManager ) {
delete m_pMeshManager ;
m_pMeshManager = NULL ;
2012-04-12 19:43:08 +00:00
}
if ( m_pTextureManager ) {
delete m_pTextureManager ;
m_pTextureManager = NULL ;
}
if ( m_pMaterialManager ) {
delete m_pMaterialManager ;
m_pMaterialManager = NULL ;
}
if ( m_pShaderManager ) {
delete m_pShaderManager ;
m_pShaderManager = NULL ;
}
2012-09-11 03:06:35 +00:00
2012-12-01 02:03:18 +00:00
if ( m_pAnimationManager ) {
delete m_pAnimationManager ;
m_pAnimationManager = NULL ;
}
2012-12-07 00:20:06 +00:00
if ( m_pAnimationCurveManager ) {
delete m_pAnimationCurveManager ;
m_pAnimationCurveManager = NULL ;
}
2013-01-05 04:04:58 +00:00
if ( m_pSoundManager ) {
delete m_pSoundManager ;
m_pSoundManager = NULL ;
}
2013-01-02 22:00:29 +00:00
if ( m_pUnknownManager ) {
delete m_pUnknownManager ;
m_pUnknownManager = NULL ;
}
2012-12-07 00:20:06 +00:00
2012-09-11 03:06:35 +00:00
// The bundles must be destroyed last, as the other objects may be using mmap'ed data from bundles
if ( m_pBundleManager ) {
delete m_pBundleManager ;
m_pBundleManager = NULL ;
}
2016-01-30 20:13:23 -08:00
destroyDeviceContexts ( ) ;
2012-04-12 19:43:08 +00:00
}
2013-12-14 16:06:44 -08:00
void KRContext : : SetLogCallback ( log_callback * log_callback , void * user_data )
{
s_log_callback = log_callback ;
s_log_callback_user_data = user_data ;
}
2017-07-27 23:37:46 -07:00
void KRContext : : Log ( log_level level , const std : : string message_format , . . . )
2013-12-14 16:06:44 -08:00
{
va_list args ;
va_start ( args , message_format ) ;
if ( s_log_callback ) {
const int LOG_BUFFER_SIZE = 32768 ;
char log_buffer [ LOG_BUFFER_SIZE ] ;
2013-12-14 18:13:00 -08:00
vsnprintf ( log_buffer , LOG_BUFFER_SIZE , message_format . c_str ( ) , args ) ;
2013-12-14 16:06:44 -08:00
s_log_callback ( s_log_callback_user_data , std : : string ( log_buffer ) , level ) ;
} else {
FILE * out_file = level = = LOG_LEVEL_INFORMATION ? stdout : stderr ;
fprintf ( out_file , " Kraken - INFO: " ) ;
2013-12-14 18:13:00 -08:00
vfprintf ( out_file , message_format . c_str ( ) , args ) ;
2013-12-14 16:06:44 -08:00
fprintf ( out_file , " \n " ) ;
}
va_end ( args ) ;
}
2012-09-11 03:06:35 +00:00
KRBundleManager * KRContext : : getBundleManager ( ) {
return m_pBundleManager ;
}
2012-04-12 19:43:08 +00:00
KRSceneManager * KRContext : : getSceneManager ( ) {
return m_pSceneManager ;
}
KRTextureManager * KRContext : : getTextureManager ( ) {
return m_pTextureManager ;
}
KRMaterialManager * KRContext : : getMaterialManager ( ) {
return m_pMaterialManager ;
}
KRShaderManager * KRContext : : getShaderManager ( ) {
return m_pShaderManager ;
}
2014-05-13 21:56:06 -07:00
KRMeshManager * KRContext : : getMeshManager ( ) {
return m_pMeshManager ;
2012-04-12 19:43:08 +00:00
}
2012-12-01 02:03:18 +00:00
KRAnimationManager * KRContext : : getAnimationManager ( ) {
return m_pAnimationManager ;
}
2012-12-07 00:20:06 +00:00
KRAnimationCurveManager * KRContext : : getAnimationCurveManager ( ) {
return m_pAnimationCurveManager ;
}
2013-01-05 04:04:58 +00:00
KRAudioManager * KRContext : : getAudioManager ( ) {
return m_pSoundManager ;
}
2013-01-02 22:00:29 +00:00
KRUnknownManager * KRContext : : getUnknownManager ( ) {
return m_pUnknownManager ;
}
2012-04-12 19:43:08 +00:00
2013-05-06 12:58:57 -07:00
std : : vector < KRResource * > KRContext : : getResources ( )
{
std : : vector < KRResource * > resources ;
for ( unordered_map < std : : string , KRScene * > : : iterator itr = m_pSceneManager - > getScenes ( ) . begin ( ) ; itr ! = m_pSceneManager - > getScenes ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
for ( unordered_map < std : : string , KRTexture * > : : iterator itr = m_pTextureManager - > getTextures ( ) . begin ( ) ; itr ! = m_pTextureManager - > getTextures ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
for ( unordered_map < std : : string , KRMaterial * > : : iterator itr = m_pMaterialManager - > getMaterials ( ) . begin ( ) ; itr ! = m_pMaterialManager - > getMaterials ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
2014-05-13 21:56:06 -07:00
for ( unordered_multimap < std : : string , KRMesh * > : : iterator itr = m_pMeshManager - > getModels ( ) . begin ( ) ; itr ! = m_pMeshManager - > getModels ( ) . end ( ) ; itr + + ) {
2013-05-06 12:58:57 -07:00
resources . push_back ( ( * itr ) . second ) ;
}
for ( unordered_map < std : : string , KRAnimation * > : : iterator itr = m_pAnimationManager - > getAnimations ( ) . begin ( ) ; itr ! = m_pAnimationManager - > getAnimations ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
for ( unordered_map < std : : string , KRAnimationCurve * > : : iterator itr = m_pAnimationCurveManager - > getAnimationCurves ( ) . begin ( ) ; itr ! = m_pAnimationCurveManager - > getAnimationCurves ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
for ( unordered_map < std : : string , KRAudioSample * > : : iterator itr = m_pSoundManager - > getSounds ( ) . begin ( ) ; itr ! = m_pSoundManager - > getSounds ( ) . end ( ) ; itr + + ) {
resources . push_back ( ( * itr ) . second ) ;
}
unordered_map < std : : string , unordered_map < std : : string , KRUnknown * > > unknowns = m_pUnknownManager - > getUnknowns ( ) ;
for ( unordered_map < std : : string , unordered_map < std : : string , KRUnknown * > > : : iterator itr = unknowns . begin ( ) ; itr ! = unknowns . end ( ) ; itr + + ) {
2013-05-06 14:06:29 -07:00
for ( unordered_map < std : : string , KRUnknown * > : : iterator itr2 = ( * itr ) . second . begin ( ) ; itr2 ! = ( * itr ) . second . end ( ) ; itr2 + + ) {
2013-05-06 12:58:57 -07:00
resources . push_back ( ( * itr2 ) . second ) ;
}
}
// FINDME, TODO - Not yet exporting shaders, as they are currently only being used as standard Kraken assets. In the future people may want their custom shaders to be exported.
return resources ;
}
2012-09-11 03:06:35 +00:00
void KRContext : : loadResource ( const std : : string & file_name , KRDataBlock * data ) {
std : : string name = KRResource : : GetFileBase ( file_name ) ;
std : : string extension = KRResource : : GetFileExtension ( file_name ) ;
2012-10-06 01:35:41 +00:00
// fprintf(stderr, "KRContext::loadResource - Loading: %s\n", file_name.c_str());
2012-09-11 03:06:35 +00:00
if ( extension . compare ( " krbundle " ) = = 0 ) {
m_pBundleManager - > loadBundle ( name . c_str ( ) , data ) ;
2013-01-09 22:37:23 +00:00
} else if ( extension . compare ( " krmesh " ) = = 0 ) {
2014-05-13 21:56:06 -07:00
m_pMeshManager - > loadModel ( name . c_str ( ) , data ) ;
2012-04-12 19:43:08 +00:00
} else if ( extension . compare ( " krscene " ) = = 0 ) {
2012-09-11 03:06:35 +00:00
m_pSceneManager - > loadScene ( name . c_str ( ) , data ) ;
2012-12-07 01:49:17 +00:00
} else if ( extension . compare ( " kranimation " ) = = 0 ) {
m_pAnimationManager - > loadAnimation ( name . c_str ( ) , data ) ;
2012-12-07 03:13:10 +00:00
} else if ( extension . compare ( " kranimationcurve " ) = = 0 ) {
2012-12-07 01:49:17 +00:00
m_pAnimationCurveManager - > loadAnimationCurve ( name . c_str ( ) , data ) ;
2012-04-12 19:43:08 +00:00
} else if ( extension . compare ( " pvr " ) = = 0 ) {
2012-10-25 03:15:28 +00:00
m_pTextureManager - > loadTexture ( name . c_str ( ) , extension . c_str ( ) , data ) ;
2014-01-03 02:17:27 -08:00
} else if ( extension . compare ( " ktx " ) = = 0 ) {
m_pTextureManager - > loadTexture ( name . c_str ( ) , extension . c_str ( ) , data ) ;
2012-10-25 03:15:28 +00:00
} else if ( extension . compare ( " tga " ) = = 0 ) {
m_pTextureManager - > loadTexture ( name . c_str ( ) , extension . c_str ( ) , data ) ;
2012-04-13 23:24:07 +00:00
} else if ( extension . compare ( " vsh " ) = = 0 ) {
2012-09-11 03:06:35 +00:00
m_pShaderManager - > loadVertexShader ( name . c_str ( ) , data ) ;
2012-04-13 23:24:07 +00:00
} else if ( extension . compare ( " fsh " ) = = 0 ) {
2012-09-11 03:06:35 +00:00
m_pShaderManager - > loadFragmentShader ( name . c_str ( ) , data ) ;
2012-04-12 19:43:08 +00:00
} else if ( extension . compare ( " mtl " ) = = 0 ) {
2012-09-11 03:06:35 +00:00
m_pMaterialManager - > load ( name . c_str ( ) , data ) ;
2013-01-05 04:04:58 +00:00
} else if ( extension . compare ( " mp3 " ) = = 0 ) {
m_pSoundManager - > load ( name . c_str ( ) , extension , data ) ;
} else if ( extension . compare ( " wav " ) = = 0 ) {
m_pSoundManager - > load ( name . c_str ( ) , extension , data ) ;
} else if ( extension . compare ( " aac " ) = = 0 ) {
m_pSoundManager - > load ( name . c_str ( ) , extension , data ) ;
2013-05-06 14:06:29 -07:00
} else if ( extension . compare ( " obj " ) = = 0 ) {
KRResource : : LoadObj ( * this , file_name ) ;
2013-05-06 19:33:51 -07:00
# if !TARGET_OS_IPHONE
2018-08-06 21:34:16 -07:00
/*
// FINDME, TODO, HACK! - Uncomment
2013-05-06 14:06:29 -07:00
} else if ( extension . compare ( " fbx " ) = = 0 ) {
KRResource : : LoadFbx ( * this , file_name ) ;
2018-08-06 21:34:16 -07:00
*/
2013-05-06 14:06:29 -07:00
} else if ( extension . compare ( " blend " ) = = 0 ) {
KRResource : : LoadBlenderScene ( * this , file_name ) ;
# endif
2012-09-11 03:06:35 +00:00
} else {
2013-01-02 22:00:29 +00:00
m_pUnknownManager - > load ( name , extension , data ) ;
2012-09-11 03:06:35 +00:00
}
}
void KRContext : : loadResource ( std : : string path ) {
KRDataBlock * data = new KRDataBlock ( ) ;
if ( data - > load ( path ) ) {
loadResource ( path , data ) ;
2012-04-12 19:43:08 +00:00
} else {
2013-12-14 16:06:44 -08:00
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " KRContext::loadResource - Failed to open file: %s " , path . c_str ( ) ) ;
2012-09-11 03:06:35 +00:00
delete data ;
2012-04-12 19:43:08 +00:00
}
2012-09-13 20:09:19 +00:00
}
2012-11-01 22:16:59 +00:00
void KRContext : : detectExtensions ( ) {
m_bDetectedExtensions = true ;
2012-11-17 00:15:52 +00:00
}
2012-12-07 01:49:17 +00:00
void KRContext : : startFrame ( float deltaTime )
2012-11-17 00:15:52 +00:00
{
2014-05-13 21:56:06 -07:00
m_streamer . startStreamer ( ) ;
2012-12-07 01:49:17 +00:00
m_pTextureManager - > startFrame ( deltaTime ) ;
m_pAnimationManager - > startFrame ( deltaTime ) ;
2013-02-28 19:09:27 -08:00
m_pSoundManager - > startFrame ( deltaTime ) ;
2014-05-13 21:56:06 -07:00
m_pMeshManager - > startFrame ( deltaTime ) ;
2012-11-17 00:15:52 +00:00
}
2012-11-23 01:02:22 +00:00
void KRContext : : endFrame ( float deltaTime )
2012-11-17 00:15:52 +00:00
{
2012-12-07 01:49:17 +00:00
m_pTextureManager - > endFrame ( deltaTime ) ;
m_pAnimationManager - > endFrame ( deltaTime ) ;
2014-05-13 21:56:06 -07:00
m_pMeshManager - > endFrame ( deltaTime ) ;
2012-11-17 00:15:52 +00:00
m_current_frame + + ;
2012-11-23 01:02:22 +00:00
m_absolute_time + = deltaTime ;
2012-11-17 00:15:52 +00:00
}
2012-11-23 01:02:22 +00:00
long KRContext : : getCurrentFrame ( ) const
2012-11-17 00:15:52 +00:00
{
return m_current_frame ;
}
2012-11-23 01:02:22 +00:00
2014-06-25 00:45:00 -07:00
long KRContext : : getLastFullyStreamedFrame ( ) const
{
return m_last_fully_streamed_frame ;
}
2012-11-23 01:02:22 +00:00
float KRContext : : getAbsoluteTime ( ) const
{
return m_absolute_time ;
}
2013-03-21 19:58:35 -07:00
2013-03-22 17:17:12 -07:00
long KRContext : : getAbsoluteTimeMilliseconds ( )
{
2018-11-13 18:39:32 +07:00
# if defined(ANDROID)
return std : : chrono : : duration_cast < std : : chrono : : milliseconds > (
std : : chrono : : system_clock : : now ( ) . time_since_epoch ( ) ) . count ( ) ;
# elif defined(__APPLE__)
2013-03-22 17:17:12 -07:00
return ( long ) ( mach_absolute_time ( ) / 1000 * m_timebase_info . numer / m_timebase_info . denom ) ; // Division done first to avoid potential overflow
2016-08-07 18:39:47 -07:00
# else
return ( long ) GetTickCount64 ( ) ;
# endif
2013-03-22 17:17:12 -07:00
}
2013-11-13 23:52:17 -08:00
bool KRContext : : getStreamingEnabled ( )
{
return m_streamingEnabled ;
}
void KRContext : : setStreamingEnabled ( bool enable )
{
m_streamingEnabled = enable ;
}
2013-11-16 02:34:18 -08:00
2016-07-10 03:33:58 -07:00
# if TARGET_OS_IPHONE || TARGET_OS_MAC
2013-11-16 02:34:18 -08:00
void KRContext : : getMemoryStats ( long & free_memory )
{
free_memory = 0 ;
2016-07-10 03:33:58 -07:00
2013-11-16 02:34:18 -08:00
mach_port_t host_port = mach_host_self ( ) ;
mach_msg_type_number_t host_size = sizeof ( vm_statistics_data_t ) / sizeof ( integer_t ) ;
vm_size_t pagesize = 0 ;
vm_statistics_data_t vm_stat ;
2017-07-09 20:13:48 -07:00
// int total_ram = 256 * 1024 * 1024;
2013-11-16 02:34:18 -08:00
if ( host_page_size ( host_port , & pagesize ) ! = KERN_SUCCESS ) {
2013-12-14 16:06:44 -08:00
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " Could not get VM page size. " ) ;
2013-11-16 02:34:18 -08:00
} else if ( host_statistics ( host_port , HOST_VM_INFO , ( host_info_t ) & vm_stat , & host_size ) ! = KERN_SUCCESS ) {
2013-12-14 16:06:44 -08:00
KRContext : : Log ( KRContext : : LOG_LEVEL_ERROR , " Could not get VM stats. " ) ;
2013-11-16 02:34:18 -08:00
} else {
2017-07-09 20:13:48 -07:00
// total_ram = (vm_stat.wire_count + vm_stat.active_count + vm_stat.inactive_count + vm_stat.free_count) * pagesize;
2013-11-16 02:34:18 -08:00
2014-06-03 02:40:56 -07:00
free_memory = ( vm_stat . free_count + vm_stat . inactive_count ) * pagesize ;
2013-11-16 02:34:18 -08:00
}
}
2014-05-13 21:56:06 -07:00
2016-07-10 03:33:58 -07:00
# endif
2014-05-13 21:56:06 -07:00
void KRContext : : doStreaming ( )
{
2018-08-06 21:34:16 -07:00
if ( m_streamingEnabled ) {
/*
long free_memory = KRENGINE_GPU_MEM_TARGET ;
long total_memory = KRENGINE_GPU_MEM_MAX ;
*/
/*
2014-06-03 02:40:56 -07:00
# if TARGET_OS_IPHONE
// FINDME, TODO, HACK! - Experimental code, need to expose through engine parameters
const long KRENGINE_RESERVE_MEMORY = 0x4000000 ; // 64MB
2018-08-06 21:34:16 -07:00
2014-06-03 02:40:56 -07:00
getMemoryStats ( free_memory ) ;
free_memory = KRCLAMP ( free_memory - KRENGINE_RESERVE_MEMORY , 0 , KRENGINE_GPU_MEM_TARGET ) ;
total_memory = KRMIN ( KRENGINE_GPU_MEM_MAX , free_memory * 3 / 4 + m_pTextureManager - > getMemUsed ( ) + m_pMeshManager - > getMemUsed ( ) ) ;
2018-08-06 21:34:16 -07:00
2014-06-03 02:40:56 -07:00
# endif
*/
/*
// FINDME, TODO - Experimental code, need to expose through engine parameters
const long MEMORY_WARNING_THROTTLE_FRAMES = 5 ;
2014-06-01 18:08:28 -07:00
bool memory_warning_throttle = m_last_memory_warning_frame ! = 0 & & m_current_frame - m_last_memory_warning_frame < MEMORY_WARNING_THROTTLE_FRAMES ;
2014-06-03 02:40:56 -07:00
if ( memory_warning_throttle ) {
free_memory = 0 ;
}
*/
2018-08-06 21:34:16 -07:00
2014-06-03 02:40:56 -07:00
/*
// FINDME, TODO - Experimental code, need to expose through engine parameters
const long MEMORY_WARNING_THROTTLE2_FRAMES = 30 ;
bool memory_warning_throttle2 = m_last_memory_warning_frame ! = 0 & & m_current_frame - m_last_memory_warning_frame < MEMORY_WARNING_THROTTLE2_FRAMES ;
if ( memory_warning_throttle2 ) {
total_memory / = 2 ;
free_memory / = 2 ;
}
*/
2018-08-06 21:34:16 -07:00
2014-06-03 02:40:56 -07:00
/*
m_pMeshManager - > doStreaming ( total_memory , free_memory ) ;
m_pTextureManager - > doStreaming ( total_memory , free_memory ) ;
*/
2018-08-06 21:34:16 -07:00
long streaming_start_frame = m_current_frame ;
long memoryRemaining = KRENGINE_GPU_MEM_TARGET ;
long memoryRemainingThisFrame = KRENGINE_GPU_MEM_MAX - m_pTextureManager - > getMemUsed ( ) - m_pMeshManager - > getMemUsed ( ) ;
long memoryRemainingThisFrameStart = memoryRemainingThisFrame ;
m_pMeshManager - > doStreaming ( memoryRemaining , memoryRemainingThisFrame ) ;
m_pTextureManager - > doStreaming ( memoryRemaining , memoryRemainingThisFrame ) ;
if ( memoryRemainingThisFrame = = memoryRemainingThisFrameStart & & memoryRemainingThisFrame > 0 ) {
m_last_fully_streamed_frame = streaming_start_frame ;
2014-05-13 21:56:06 -07:00
}
2018-08-06 21:34:16 -07:00
}
2014-05-13 21:56:06 -07:00
}
2014-06-01 18:08:28 -07:00
void KRContext : : receivedMemoryWarning ( )
{
2018-08-06 21:34:16 -07:00
m_last_memory_warning_frame = m_current_frame ;
2014-06-01 18:08:28 -07:00
}
2014-06-25 00:45:00 -07:00
2018-08-06 21:34:16 -07:00
void
KRContext : : createDeviceContexts ( )
{
// initialize the VkApplicationInfo structure
VkApplicationInfo app_info = { } ;
app_info . sType = VK_STRUCTURE_TYPE_APPLICATION_INFO ;
app_info . pNext = NULL ;
app_info . pApplicationName = " Test " ; // TODO - Change Me!
app_info . applicationVersion = 1 ;
app_info . pEngineName = " Kraken Engine " ;
app_info . engineVersion = 1 ;
app_info . apiVersion = VK_API_VERSION_1_0 ;
// initialize the VkInstanceCreateInfo structure
VkInstanceCreateInfo inst_info = { } ;
inst_info . sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ;
inst_info . pNext = NULL ;
inst_info . flags = 0 ;
inst_info . pApplicationInfo = & app_info ;
inst_info . enabledExtensionCount = 0 ;
inst_info . ppEnabledExtensionNames = NULL ;
inst_info . enabledLayerCount = 0 ;
inst_info . ppEnabledLayerNames = NULL ;
2018-10-09 11:16:41 -07:00
VkResult res = vkCreateInstance ( & inst_info , NULL , & m_vulkanInstance ) ;
if ( res ! = VK_SUCCESS ) {
destroyDeviceContexts ( ) ;
2018-08-06 21:34:16 -07:00
}
}
2014-06-25 00:45:00 -07:00
2018-08-06 21:34:16 -07:00
void
KRContext : : destroyDeviceContexts ( )
{
2018-10-09 11:16:41 -07:00
if ( m_vulkanInstance ! = VK_NULL_HANDLE ) {
2018-08-06 21:34:16 -07:00
vkDestroyInstance ( m_vulkanInstance , NULL ) ;
m_vulkanInstance = VK_NULL_HANDLE ;
}
}
void
KRContext : : activateStreamerContext ( )
2018-10-09 11:16:41 -07:00
{
2018-08-06 21:34:16 -07:00
}