From 2097664c0a614128a3f036f9fa87854066b83eba Mon Sep 17 00:00:00 2001 From: kearwood Date: Fri, 19 Oct 2012 23:17:43 +0000 Subject: [PATCH] - Implemented device detection and automatic selection of KREngine memory management parameters - Now detecting iPad 3 and adjusting parameters to utilize the greater RAM capacity --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40135 --- KREngine/KREngine/Classes/KRCamera.h | 3 +-- KREngine/KREngine/Classes/KRContext.cpp | 10 +++++++ KREngine/KREngine/Classes/KRContext.h | 13 +++++++++ KREngine/KREngine/Classes/KREngine-common.h | 2 ++ KREngine/KREngine/Classes/KREngine.mm | 27 +++++++++++++++++++ KREngine/KREngine/Classes/KRModel.h | 2 ++ KREngine/KREngine/Classes/KRModelManager.cpp | 2 +- KREngine/KREngine/Classes/KRModelManager.h | 3 --- KREngine/KREngine/Classes/KRShaderManager.cpp | 2 +- KREngine/KREngine/Classes/KRShaderManager.h | 4 ++- .../KREngine/Classes/KRTextureManager.cpp | 11 ++++---- KREngine/KREngine/Classes/KRTextureManager.h | 9 +------ 12 files changed, 67 insertions(+), 21 deletions(-) diff --git a/KREngine/KREngine/Classes/KRCamera.h b/KREngine/KREngine/Classes/KRCamera.h index 48717f0..19d2fda 100644 --- a/KREngine/KREngine/Classes/KRCamera.h +++ b/KREngine/KREngine/Classes/KRCamera.h @@ -40,7 +40,7 @@ #import "KRShader.h" #import "KRContextObject.h" #import "KRTexture.h" - +#import "KRContext.h" #define KRENGINE_MAX_SHADOW_BUFFERS 3 #define KRENGINE_SHADOW_MAP_WIDTH 2048 @@ -48,7 +48,6 @@ class KRInstance; class KRScene; -class KRContext; class KRCamera : public KRContextObject { public: diff --git a/KREngine/KREngine/Classes/KRContext.cpp b/KREngine/KREngine/Classes/KRContext.cpp index 075ac19..d56d8ef 100644 --- a/KREngine/KREngine/Classes/KRContext.cpp +++ b/KREngine/KREngine/Classes/KRContext.cpp @@ -11,6 +11,16 @@ #include "KRContext.h" #include "KRCamera.h" +int KRContext::KRENGINE_MAX_VBO_HANDLES; +int KRContext::KRENGINE_MAX_VBO_MEM; +int KRContext::KRENGINE_MAX_SHADER_HANDLES; +int KRContext::KRENGINE_MAX_TEXTURE_HANDLES; +int KRContext::KRENGINE_MAX_TEXTURE_MEM; +int KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX; +int KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN; +int KRContext::KRENGINE_MAX_TEXTURE_DIM; +int KRContext::KRENGINE_MIN_TEXTURE_DIM; + KRContext::KRContext() { m_pBundleManager = new KRBundleManager(*this); m_pShaderManager = new KRShaderManager(*this); diff --git a/KREngine/KREngine/Classes/KRContext.h b/KREngine/KREngine/Classes/KRContext.h index 953bac2..028bda5 100644 --- a/KREngine/KREngine/Classes/KRContext.h +++ b/KREngine/KREngine/Classes/KRContext.h @@ -9,14 +9,27 @@ #ifndef KREngine_KRContext_h #define KREngine_KRContext_h +#import "KREngine-common.h" #import "KRBundleManager.h" #import "KRSceneManager.h" #import "KRTextureManager.h" #import "KRMaterialManager.h" #import "KRShaderManager.h" #import "KRModelManager.h" + class KRContext { public: + static int KRENGINE_MAX_VBO_HANDLES; + static int KRENGINE_MAX_VBO_MEM; + static int KRENGINE_MAX_SHADER_HANDLES; + static int KRENGINE_MAX_TEXTURE_HANDLES; + static int KRENGINE_MAX_TEXTURE_MEM; + static int KRENGINE_TARGET_TEXTURE_MEM_MAX; + static int KRENGINE_TARGET_TEXTURE_MEM_MIN; + static int KRENGINE_MAX_TEXTURE_DIM; + static int KRENGINE_MIN_TEXTURE_DIM; + + KRContext(); ~KRContext(); diff --git a/KREngine/KREngine/Classes/KREngine-common.h b/KREngine/KREngine/Classes/KREngine-common.h index f80e375..dcb95bf 100644 --- a/KREngine/KREngine/Classes/KREngine-common.h +++ b/KREngine/KREngine/Classes/KREngine-common.h @@ -11,6 +11,8 @@ #ifndef KREngine_KREngine_common_h #define KREngine_KREngine_common_h +#define KRENGINE_MAX_TEXTURE_UNITS 8 + #import #import #import diff --git a/KREngine/KREngine/Classes/KREngine.mm b/KREngine/KREngine/Classes/KREngine.mm index bcc84fe..6a8a55f 100644 --- a/KREngine/KREngine/Classes/KREngine.mm +++ b/KREngine/KREngine/Classes/KREngine.mm @@ -29,6 +29,8 @@ // or implied, of Kearwood Gilbert. // +#import + #import "KREngine.h" #import "KRVector3.h" #import "KRScene.h" @@ -64,6 +66,31 @@ double const PI = 3.141592653589793f; - (id)init { + BOOL isIpad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad; + BOOL isRetina = [[UIScreen mainScreen] scale] >= 2.0; + + if(isIpad && isRetina) { + KRContext::KRENGINE_MAX_VBO_HANDLES = 10000; + KRContext::KRENGINE_MAX_VBO_MEM = 128000000 * 2; + KRContext::KRENGINE_MAX_SHADER_HANDLES = 100; + KRContext::KRENGINE_MAX_TEXTURE_HANDLES = 10000; + KRContext::KRENGINE_MAX_TEXTURE_MEM = 64000000 * 2; + KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX = 48000000 * 2; + KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN = 32000000 * 2; + KRContext::KRENGINE_MAX_TEXTURE_DIM = 2048; + KRContext::KRENGINE_MIN_TEXTURE_DIM = 64; + } else { + KRContext::KRENGINE_MAX_VBO_HANDLES = 10000; + KRContext::KRENGINE_MAX_VBO_MEM = 128000000; + KRContext::KRENGINE_MAX_SHADER_HANDLES = 100; + KRContext::KRENGINE_MAX_TEXTURE_HANDLES = 10000; + KRContext::KRENGINE_MAX_TEXTURE_MEM = 64000000; + KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX = 48000000; + KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN = 32000000; + KRContext::KRENGINE_MAX_TEXTURE_DIM = 2048; + KRContext::KRENGINE_MIN_TEXTURE_DIM = 64; + } + _camera = NULL; _context = NULL; if ((self = [super init])) { diff --git a/KREngine/KREngine/Classes/KRModel.h b/KREngine/KREngine/Classes/KRModel.h index 2086b42..431481c 100644 --- a/KREngine/KREngine/Classes/KRModel.h +++ b/KREngine/KREngine/Classes/KRModel.h @@ -54,6 +54,8 @@ using std::list; #import "KRCamera.h" class KRMaterial; +class KRNode; + class KRModel : public KRResource { diff --git a/KREngine/KREngine/Classes/KRModelManager.cpp b/KREngine/KREngine/Classes/KRModelManager.cpp index b0e4bc9..c2bcc58 100644 --- a/KREngine/KREngine/Classes/KRModelManager.cpp +++ b/KREngine/KREngine/Classes/KRModelManager.cpp @@ -120,7 +120,7 @@ void KRModelManager::bindVBO(GLvoid *data, GLsizeiptr size, bool enable_vertex, } else { m_vboMemUsed += size; - while(m_vbosPool.size() + m_vbosActive.size() >= KRENGINE_MAX_VBO_HANDLES || m_vboMemUsed >= KRENGINE_MAX_VBO_MEM) { + while(m_vbosPool.size() + m_vbosActive.size() >= KRContext::KRENGINE_MAX_VBO_HANDLES || m_vboMemUsed >= KRContext::KRENGINE_MAX_VBO_MEM) { if(m_vbosPool.empty()) { fprintf(stderr, "flushBuffers due to VBO exhaustion...\n"); m_pContext->rotateBuffers(false); diff --git a/KREngine/KREngine/Classes/KRModelManager.h b/KREngine/KREngine/Classes/KRModelManager.h index a913a90..dc0b894 100644 --- a/KREngine/KREngine/Classes/KRModelManager.h +++ b/KREngine/KREngine/Classes/KRModelManager.h @@ -32,9 +32,6 @@ #ifndef KRMODELMANAGER_H #define KRMODELMANAGER_H -#define KRENGINE_MAX_VBO_HANDLES 10000 -#define KRENGINE_MAX_VBO_MEM 128000000 - #import "KREngine-common.h" #import "KRContextObject.h" #import "KRDataBlock.h" diff --git a/KREngine/KREngine/Classes/KRShaderManager.cpp b/KREngine/KREngine/Classes/KRShaderManager.cpp index dcc7679..1323c52 100644 --- a/KREngine/KREngine/Classes/KRShaderManager.cpp +++ b/KREngine/KREngine/Classes/KRShaderManager.cpp @@ -53,7 +53,7 @@ KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera, KRShader *pShader = m_shaders[szKey]; if(pShader == NULL) { - if(m_shaders.size() > KRENGINE_MAX_SHADER_HANDLES) { + if(m_shaders.size() > KRContext::KRENGINE_MAX_SHADER_HANDLES) { // Keep the size of the shader cache reasonable std::map::iterator itr = m_shaders.begin(); delete (*itr).second; diff --git a/KREngine/KREngine/Classes/KRShaderManager.h b/KREngine/KREngine/Classes/KRShaderManager.h index 315c89c..2eddfd7 100644 --- a/KREngine/KREngine/Classes/KRShaderManager.h +++ b/KREngine/KREngine/Classes/KRShaderManager.h @@ -35,6 +35,7 @@ #import #import "KRCamera.h" #import "KRDataBlock.h" +#import "KRNode.h" using std::map; using std::vector; @@ -44,7 +45,8 @@ using std::vector; #ifndef KRSHADERMANAGER_H #define KRSHADERMANAGER_H -#define KRENGINE_MAX_SHADER_HANDLES 100 +class KRShader; +class KRCamera; class KRShaderManager : public KRContextObject { public: diff --git a/KREngine/KREngine/Classes/KRTextureManager.cpp b/KREngine/KREngine/Classes/KRTextureManager.cpp index 72e4f84..fb6ea85 100644 --- a/KREngine/KREngine/Classes/KRTextureManager.cpp +++ b/KREngine/KREngine/Classes/KRTextureManager.cpp @@ -33,6 +33,7 @@ #include "KRContext.h" #include "KRTexture2D.h" #include "KRTextureCube.h" +#include "KRContext.h" #include KRTextureManager::KRTextureManager(KRContext &context) : KRContextObject(context) { @@ -119,7 +120,7 @@ void KRTextureManager::selectTexture(int iTextureUnit, KRTexture *pTexture, int GLDEBUG(glBindTexture(GL_TEXTURE_2D, 0)); } m_boundTextures[iTextureUnit] = pTexture; - while(m_activeTextures.size() + m_poolTextures.size() > KRENGINE_MAX_TEXTURE_HANDLES || m_textureMemUsed > KRENGINE_MAX_TEXTURE_MEM) { + while(m_activeTextures.size() + m_poolTextures.size() > KRContext::KRENGINE_MAX_TEXTURE_HANDLES || m_textureMemUsed > KRContext::KRContext::KRENGINE_MAX_TEXTURE_MEM) { if(m_poolTextures.empty()) { fprintf(stderr, "Kraken - Texture swapping...\n"); decreaseLODCap(); @@ -150,10 +151,10 @@ size_t KRTextureManager::getActiveMemUsed() { void KRTextureManager::rotateBuffers(bool new_frame) { - if(new_frame && m_activeTextureMemUsed < KRENGINE_TARGET_TEXTURE_MEM_MIN && m_activeTextureMemUsed * 4 < KRENGINE_TARGET_TEXTURE_MEM_MAX) { + if(new_frame && m_activeTextureMemUsed < KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN && m_activeTextureMemUsed * 4 < KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX) { // Increasing the LOD level will generally increase active texture memory usage by 4 times, don't increase the texture level until we can ensure that the LOD won't immediately be dropped back to the current level increaseLODCap(); - } else if(new_frame && m_activeTextureMemUsed > KRENGINE_TARGET_TEXTURE_MEM_MAX) { + } else if(new_frame && m_activeTextureMemUsed > KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX) { decreaseLODCap(); } m_poolTextures.insert(m_activeTextures.begin(), m_activeTextures.end()); @@ -174,14 +175,14 @@ void KRTextureManager::rotateBuffers(bool new_frame) void KRTextureManager::decreaseLODCap() { - if(m_lod_max_dim_cap > KRENGINE_MIN_TEXTURE_DIM) { + if(m_lod_max_dim_cap > KRContext::KRENGINE_MIN_TEXTURE_DIM) { m_lod_max_dim_cap = m_lod_max_dim_cap >> 1; } } void KRTextureManager::increaseLODCap() { - if(m_lod_max_dim_cap < KRENGINE_MAX_TEXTURE_DIM) { + if(m_lod_max_dim_cap < KRContext::KRENGINE_MAX_TEXTURE_DIM) { m_lod_max_dim_cap = m_lod_max_dim_cap << 1; } } diff --git a/KREngine/KREngine/Classes/KRTextureManager.h b/KREngine/KREngine/Classes/KRTextureManager.h index 942a43f..f1f8b5e 100644 --- a/KREngine/KREngine/Classes/KRTextureManager.h +++ b/KREngine/KREngine/Classes/KRTextureManager.h @@ -29,14 +29,6 @@ // or implied, of Kearwood Gilbert. // -#define KRENGINE_MAX_TEXTURE_UNITS 8 -#define KRENGINE_MAX_TEXTURE_HANDLES 10000 -#define KRENGINE_MAX_TEXTURE_MEM 64000000 -#define KRENGINE_TARGET_TEXTURE_MEM_MAX 48000000 -#define KRENGINE_TARGET_TEXTURE_MEM_MIN 32000000 -#define KRENGINE_MAX_TEXTURE_DIM 2048 -#define KRENGINE_MIN_TEXTURE_DIM 64 - #ifndef KRTEXTUREMANAGER_H #define KRTEXTUREMANAGER_H @@ -44,6 +36,7 @@ #include "KRContextObject.h" #include "KREngine-common.h" #include "KRDataBlock.h" +#include "KRContext.h" #include #include