Added additional validation logic for debugging
Corrected memory corruption in VBO's! No longer have random visual artifacts when models with > 65535 triangles are displayed. --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40104
This commit is contained in:
@@ -403,7 +403,7 @@ void KRCamera::renderFrame(KRScene &scene, KRMat4 &viewMatrix, KRVector3 &lightD
|
||||
GLDEBUG(glDepthMask(GL_TRUE));
|
||||
|
||||
|
||||
fprintf(stderr, "VBO Mem: %i Kbyte Texture Mem: %i Kbyte\n", (int)m_pContext->getModelManager()->getMemUsed() / 1024, (int)m_pContext->getTextureManager()->getMemUsed() / 1024);
|
||||
fprintf(stderr, "VBO Mem: %i Kbyte Texture Mem: %i Kbyte Shader Handles: %i\n", (int)m_pContext->getModelManager()->getMemUsed() / 1024, (int)m_pContext->getTextureManager()->getMemUsed() / 1024, (int)m_pContext->getShaderManager()->getShaderHandlesUsed());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ KRMaterialManager::~KRMaterialManager() {
|
||||
KRMaterial *KRMaterialManager::getMaterial(const char *szName) {
|
||||
map<std::string, KRMaterial *>::iterator itr = m_materials.find(szName);
|
||||
if(itr == m_materials.end()) {
|
||||
fprintf(stderr, "Material not found: %s\n", szName);
|
||||
// Not found
|
||||
return NULL;
|
||||
} else {
|
||||
|
||||
@@ -122,13 +122,15 @@ void KRMesh::renderSubmesh(int iSubmesh) {
|
||||
int cVertexes = pSubmesh->vertex_count;
|
||||
while(cVertexes > 0) {
|
||||
GLsizei cBufferVertexes = iBuffer < cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE;
|
||||
|
||||
assert(pVertexData + iBuffer * MAX_VBO_SIZE >= m_pData->getStart());
|
||||
int vertex_size = sizeof(VertexData) ;
|
||||
void *vbo_end = (unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE + vertex_size * cBufferVertexes;
|
||||
assert(pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size >= m_pData->getStart());
|
||||
|
||||
void *vbo_end = (unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size + vertex_size * cBufferVertexes;
|
||||
void *buffer_end = m_pData->getEnd();
|
||||
assert(vbo_end <= buffer_end);
|
||||
m_pContext->getModelManager()->bindVBO((unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE, vertex_size * cBufferVertexes, true, true, true, true, true);
|
||||
assert(cBufferVertexes <= 65535);
|
||||
|
||||
m_pContext->getModelManager()->bindVBO((unsigned char *)pVertexData + iBuffer * MAX_VBO_SIZE * vertex_size, vertex_size * cBufferVertexes, true, true, true, true, true);
|
||||
|
||||
|
||||
if(iVertex + cVertexes >= MAX_VBO_SIZE) {
|
||||
@@ -140,10 +142,11 @@ void KRMesh::renderSubmesh(int iSubmesh) {
|
||||
iBuffer++;
|
||||
} else {
|
||||
assert(iVertex + cVertexes <= cBufferVertexes);
|
||||
|
||||
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes));
|
||||
cVertexes = 0;
|
||||
}
|
||||
//m_pContext->getModelManager()->unbindVBO();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -103,6 +103,7 @@ void KRModelManager::bindVBO(GLvoid *data, GLsizeiptr size, bool enable_vertex,
|
||||
|
||||
while(m_vbosPool.size() + m_vbosActive.size() >= KRENGINE_MAX_VBO_HANDLES || m_vboMemUsed >= KRENGINE_MAX_VBO_MEM) {
|
||||
if(m_vbosPool.empty()) {
|
||||
fprintf(stderr, "flushBuffers due to VBO exhaustion...\n");
|
||||
m_pContext->rotateBuffers();
|
||||
}
|
||||
std::map<GLvoid *, vbo_info_type>::iterator first_itr = m_vbosPool.begin();
|
||||
@@ -116,9 +117,9 @@ void KRModelManager::bindVBO(GLvoid *data, GLsizeiptr size, bool enable_vertex,
|
||||
m_currentVBO.handle = -1;
|
||||
GLDEBUG(glGenBuffers(1, &m_currentVBO.handle));
|
||||
|
||||
|
||||
GLDEBUG(glBindBuffer(GL_ARRAY_BUFFER, m_currentVBO.handle));
|
||||
GLDEBUG(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
|
||||
//fprintf(stderr, "glBufferData(GL_ARRAY_BUFFER, %ld, data, GL_STATIC_DRAW)\n", size);
|
||||
|
||||
m_currentVBO.size = size;
|
||||
m_currentVBO.data = data;
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#ifndef KRMODELMANAGER_H
|
||||
#define KRMODELMANAGER_H
|
||||
|
||||
#define KRENGINE_MAX_VBO_HANDLES 1000
|
||||
#define KRENGINE_MAX_VBO_HANDLES 10000
|
||||
#define KRENGINE_MAX_VBO_MEM 50000000
|
||||
|
||||
#import "KREngine-common.h"
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
//
|
||||
|
||||
#include "KRShader.h"
|
||||
#import "assert.h"
|
||||
|
||||
KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) {
|
||||
strcpy(m_szKey, szKey);
|
||||
@@ -264,15 +265,21 @@ bool KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix
|
||||
#if defined(DEBUG)
|
||||
GLint logLength;
|
||||
|
||||
GLint validate_status = GL_FALSE;
|
||||
GLDEBUG(glValidateProgram(m_iProgram));
|
||||
GLDEBUG(glGetProgramiv(m_iProgram, GL_INFO_LOG_LENGTH, &logLength));
|
||||
if (logLength > 0)
|
||||
{
|
||||
GLchar *log = (GLchar *)malloc(logLength);
|
||||
assert(log != NULL);
|
||||
GLDEBUG(glGetProgramInfoLog(m_iProgram, logLength, &logLength, log));
|
||||
fprintf(stderr, "KREngine - Failed to validate shader program: %s\n Program validate log:\n%s", m_szKey, log);
|
||||
free(log);
|
||||
GLDEBUG(glGetProgramiv(m_iProgram, GL_VALIDATE_STATUS, &validate_status));
|
||||
if(validate_status != GL_TRUE) {
|
||||
fprintf(stderr, "KREngine - Failed to validate shader program: %s\n", m_szKey);
|
||||
GLDEBUG(glGetProgramiv(m_iProgram, GL_INFO_LOG_LENGTH, &logLength));
|
||||
if (logLength > 0)
|
||||
{
|
||||
GLchar *log = (GLchar *)malloc(logLength);
|
||||
assert(log != NULL);
|
||||
GLDEBUG(glGetProgramInfoLog(m_iProgram, logLength, &logLength, log));
|
||||
fprintf(stderr, "Program validate log:\n%s", log);
|
||||
free(log);
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -62,7 +62,7 @@ KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera,
|
||||
std::map<std::string, KRShader *>::iterator itr = m_shaders.begin();
|
||||
delete (*itr).second;
|
||||
m_shaders.erase(itr);
|
||||
fprintf(stderr, "Swapping shaders...\n");
|
||||
//fprintf(stderr, "Swapping shaders...\n");
|
||||
}
|
||||
|
||||
stringstream stream;
|
||||
@@ -148,3 +148,7 @@ const std::string &KRShaderManager::getFragShaderSource(const std::string &name)
|
||||
const std::string &KRShaderManager::getVertShaderSource(const std::string &name) {
|
||||
return m_vertShaderSource[name];
|
||||
}
|
||||
|
||||
long KRShaderManager::getShaderHandlesUsed() {
|
||||
return m_shaders.size();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ using std::vector;
|
||||
#ifndef KRSHADERMANAGER_H
|
||||
#define KRSHADERMANAGER_H
|
||||
|
||||
#define KRENGINE_MAX_SHADER_HANDLES 1000
|
||||
#define KRENGINE_MAX_SHADER_HANDLES 100
|
||||
|
||||
class KRShaderManager : public KRContextObject {
|
||||
public:
|
||||
@@ -59,6 +59,8 @@ public:
|
||||
|
||||
KRShader *getShader(std::string shader_name, KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass);
|
||||
|
||||
long getShaderHandlesUsed();
|
||||
|
||||
private:
|
||||
std::map<std::string, KRShader *> m_shaders;
|
||||
|
||||
|
||||
@@ -99,6 +99,7 @@ void KRTextureManager::selectTexture(int iTextureUnit, KRTexture *pTexture) {
|
||||
m_boundTextures[iTextureUnit] = pTexture;
|
||||
while(m_activeTextures.size() + m_poolTextures.size() > KRENGINE_MAX_TEXTURE_HANDLES || m_textureMemUsed > KRENGINE_MAX_TEXTURE_MEM) {
|
||||
if(m_poolTextures.empty()) {
|
||||
//fprintf(stderr, "flushBuffers due to texture exhaustion...\n");
|
||||
m_pContext->rotateBuffers();
|
||||
}
|
||||
// Keep texture size within limits
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
#define KRENGINE_MAX_TEXTURE_UNITS 8
|
||||
#define KRENGINE_MAX_TEXTURE_HANDLES 10000
|
||||
#define KRENGINE_MAX_TEXTURE_MEM 200000000
|
||||
#define KRENGINE_MAX_TEXTURE_MEM 100000000
|
||||
|
||||
#ifndef KRTEXTUREMANAGER_H
|
||||
#define KRTEXTUREMANAGER_H
|
||||
|
||||
@@ -106,7 +106,7 @@
|
||||
}
|
||||
|
||||
[self.engine setNearZ: 5.0];
|
||||
[self.engine setFarZ: 1000.0];
|
||||
[self.engine setFarZ: 3000.0];
|
||||
//[renderEngine setNearZ: 1.0];
|
||||
//[renderEngine setFarZ: 3000.0];
|
||||
|
||||
|
||||
@@ -180,100 +180,101 @@
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
|
||||
- (void)drawView:(id)sender
|
||||
{
|
||||
if(glView.context && glView.engine) {
|
||||
//glGetError(); // Clear any prior errors...
|
||||
|
||||
|
||||
CFTimeInterval frame_start_time = CACurrentMediaTime();
|
||||
|
||||
//NSAutoreleasePool *framePool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
|
||||
CFTimeInterval time = [displayLink timestamp];
|
||||
float deltaTime = (time - _lastTime);
|
||||
_lastTime = time;
|
||||
|
||||
const GLfloat PI = 3.14159265;
|
||||
const GLfloat d2r = PI * 2 / 360;
|
||||
|
||||
|
||||
KREngine *engine = glView.engine;
|
||||
int iParam = int(dLeftSlider * ([engine getParameterCount] + 1));
|
||||
if(iParam > [engine getParameterCount]) {
|
||||
iParam = [engine getParameterCount];
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(cParamDisplayFrames && iParam < [engine getParameterCount]) {
|
||||
cParamDisplayFrames--;
|
||||
char szText[256];
|
||||
const char *szName = [[engine getParameterLabelWithIndex: iParam] UTF8String];
|
||||
double dValue = [engine getParameterValueWithIndex: iParam];
|
||||
switch([engine getParameterTypeWithIndex: iParam]) {
|
||||
case KRENGINE_PARAMETER_INT:
|
||||
sprintf(szText, "%s: %i", szName, (int)dValue);
|
||||
break;
|
||||
case KRENGINE_PARAMETER_BOOL:
|
||||
sprintf(szText, "%s: %s", szName, dValue == 0.0 ? "false" : "true");
|
||||
break;
|
||||
case KRENGINE_PARAMETER_FLOAT:
|
||||
sprintf(szText, "%s: %f", szName, dValue);
|
||||
break;
|
||||
}
|
||||
NSString *debug_text = [[NSString alloc] initWithUTF8String:szText];
|
||||
engine.debug_text = debug_text;
|
||||
[debug_text release];
|
||||
} else {
|
||||
engine.debug_text = @"";
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(bUpdateParam) {
|
||||
bUpdateParam = false;
|
||||
// @synchronized(self) {
|
||||
if(glView.context && glView.engine) {
|
||||
//glGetError(); // Clear any prior errors...
|
||||
|
||||
double dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam];
|
||||
switch([engine getParameterTypeWithIndex: iParam]) {
|
||||
case KRENGINE_PARAMETER_INT:
|
||||
dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] + 0.5 - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam];
|
||||
[engine setParameterValueWithIndex: iParam Value: dValue];
|
||||
break;
|
||||
case KRENGINE_PARAMETER_BOOL:
|
||||
[engine setParameterValueWithIndex: iParam Value: 1.0 - dValue];
|
||||
break;
|
||||
case KRENGINE_PARAMETER_FLOAT:
|
||||
[engine setParameterValueWithIndex: iParam Value: dValue];
|
||||
break;
|
||||
|
||||
CFTimeInterval frame_start_time = CACurrentMediaTime();
|
||||
|
||||
//NSAutoreleasePool *framePool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
|
||||
CFTimeInterval time = [displayLink timestamp];
|
||||
float deltaTime = (time - _lastTime);
|
||||
_lastTime = time;
|
||||
|
||||
const GLfloat PI = 3.14159265;
|
||||
const GLfloat d2r = PI * 2 / 360;
|
||||
|
||||
|
||||
KREngine *engine = glView.engine;
|
||||
int iParam = int(dLeftSlider * ([engine getParameterCount] + 1));
|
||||
if(iParam > [engine getParameterCount]) {
|
||||
iParam = [engine getParameterCount];
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(cParamDisplayFrames && iParam < [engine getParameterCount]) {
|
||||
cParamDisplayFrames--;
|
||||
char szText[256];
|
||||
const char *szName = [[engine getParameterLabelWithIndex: iParam] UTF8String];
|
||||
double dValue = [engine getParameterValueWithIndex: iParam];
|
||||
switch([engine getParameterTypeWithIndex: iParam]) {
|
||||
case KRENGINE_PARAMETER_INT:
|
||||
sprintf(szText, "%s: %i", szName, (int)dValue);
|
||||
break;
|
||||
case KRENGINE_PARAMETER_BOOL:
|
||||
sprintf(szText, "%s: %s", szName, dValue == 0.0 ? "false" : "true");
|
||||
break;
|
||||
case KRENGINE_PARAMETER_FLOAT:
|
||||
sprintf(szText, "%s: %f", szName, dValue);
|
||||
break;
|
||||
}
|
||||
NSString *debug_text = [[NSString alloc] initWithUTF8String:szText];
|
||||
engine.debug_text = debug_text;
|
||||
[debug_text release];
|
||||
} else {
|
||||
engine.debug_text = @"";
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(bUpdateParam) {
|
||||
bUpdateParam = false;
|
||||
|
||||
double dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam];
|
||||
switch([engine getParameterTypeWithIndex: iParam]) {
|
||||
case KRENGINE_PARAMETER_INT:
|
||||
dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] + 0.5 - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam];
|
||||
[engine setParameterValueWithIndex: iParam Value: dValue];
|
||||
break;
|
||||
case KRENGINE_PARAMETER_BOOL:
|
||||
[engine setParameterValueWithIndex: iParam Value: 1.0 - dValue];
|
||||
break;
|
||||
case KRENGINE_PARAMETER_FLOAT:
|
||||
[engine setParameterValueWithIndex: iParam Value: dValue];
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
double dScaleFactor = 200.0f * deltaTime;
|
||||
|
||||
camera_position.z += (-cos(camera_pitch) * cos(camera_yaw) * leftStickDeltaX + -cos(camera_pitch) * cos(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor;
|
||||
camera_position.x += (cos(camera_pitch) * sin(camera_yaw) * leftStickDeltaX + cos(camera_pitch) * sin(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor;
|
||||
camera_position.y += sin(camera_pitch) * leftStickDeltaX * dScaleFactor;
|
||||
camera_yaw += rightStickDeltaY * 180.0 * d2r * deltaTime;
|
||||
camera_pitch += rightStickDeltaX * 180.0 * d2r * deltaTime;
|
||||
|
||||
|
||||
|
||||
assert([EAGLContext setCurrentContext:glView.context]);
|
||||
[glView setDisplayFramebuffer];
|
||||
KRScene *scene = [glView getScene];
|
||||
[engine renderScene: scene WithPosition:camera_position Yaw: camera_yaw Pitch: camera_pitch Roll:0.0f];
|
||||
[glView presentFramebuffer];
|
||||
|
||||
//[framePool release];
|
||||
|
||||
double frameTime = CACurrentMediaTime() - frame_start_time;
|
||||
|
||||
//NSLog(@"frameTime = %.1f ms (%.2f fps / %.2f fps) - %.2f%%", frameTime * 1000.0f, 1.0f / frameTime, 1.0f / deltaTime, frameTime / deltaTime * 100.0f);
|
||||
}
|
||||
|
||||
double dScaleFactor = 200.0f * deltaTime;
|
||||
|
||||
camera_position.z += (-cos(camera_pitch) * cos(camera_yaw) * leftStickDeltaX + -cos(camera_pitch) * cos(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor;
|
||||
camera_position.x += (cos(camera_pitch) * sin(camera_yaw) * leftStickDeltaX + cos(camera_pitch) * sin(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor;
|
||||
camera_position.y += sin(camera_pitch) * leftStickDeltaX * dScaleFactor;
|
||||
camera_yaw += rightStickDeltaY * 180.0 * d2r * deltaTime;
|
||||
camera_pitch += rightStickDeltaX * 180.0 * d2r * deltaTime;
|
||||
|
||||
|
||||
|
||||
assert([EAGLContext setCurrentContext:glView.context]);
|
||||
[glView setDisplayFramebuffer];
|
||||
KRScene *scene = [glView getScene];
|
||||
[engine renderScene: scene WithPosition:camera_position Yaw: camera_yaw Pitch: camera_pitch Roll:0.0f];
|
||||
[glView presentFramebuffer];
|
||||
|
||||
//[framePool release];
|
||||
|
||||
double frameTime = CACurrentMediaTime() - frame_start_time;
|
||||
|
||||
//NSLog(@"frameTime = %.1f ms (%.2f fps / %.2f fps) - %.2f%%", frameTime * 1000.0f, 1.0f / frameTime, 1.0f / deltaTime, frameTime / deltaTime * 100.0f);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user