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));
|
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) {
|
KRMaterial *KRMaterialManager::getMaterial(const char *szName) {
|
||||||
map<std::string, KRMaterial *>::iterator itr = m_materials.find(szName);
|
map<std::string, KRMaterial *>::iterator itr = m_materials.find(szName);
|
||||||
if(itr == m_materials.end()) {
|
if(itr == m_materials.end()) {
|
||||||
|
fprintf(stderr, "Material not found: %s\n", szName);
|
||||||
// Not found
|
// Not found
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -122,13 +122,15 @@ void KRMesh::renderSubmesh(int iSubmesh) {
|
|||||||
int cVertexes = pSubmesh->vertex_count;
|
int cVertexes = pSubmesh->vertex_count;
|
||||||
while(cVertexes > 0) {
|
while(cVertexes > 0) {
|
||||||
GLsizei cBufferVertexes = iBuffer < cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE;
|
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) ;
|
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();
|
void *buffer_end = m_pData->getEnd();
|
||||||
assert(vbo_end <= buffer_end);
|
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) {
|
if(iVertex + cVertexes >= MAX_VBO_SIZE) {
|
||||||
@@ -140,10 +142,11 @@ void KRMesh::renderSubmesh(int iSubmesh) {
|
|||||||
iBuffer++;
|
iBuffer++;
|
||||||
} else {
|
} else {
|
||||||
assert(iVertex + cVertexes <= cBufferVertexes);
|
assert(iVertex + cVertexes <= cBufferVertexes);
|
||||||
|
|
||||||
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes));
|
GLDEBUG(glDrawArrays(GL_TRIANGLES, iVertex, cVertexes));
|
||||||
cVertexes = 0;
|
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) {
|
while(m_vbosPool.size() + m_vbosActive.size() >= KRENGINE_MAX_VBO_HANDLES || m_vboMemUsed >= KRENGINE_MAX_VBO_MEM) {
|
||||||
if(m_vbosPool.empty()) {
|
if(m_vbosPool.empty()) {
|
||||||
|
fprintf(stderr, "flushBuffers due to VBO exhaustion...\n");
|
||||||
m_pContext->rotateBuffers();
|
m_pContext->rotateBuffers();
|
||||||
}
|
}
|
||||||
std::map<GLvoid *, vbo_info_type>::iterator first_itr = m_vbosPool.begin();
|
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;
|
m_currentVBO.handle = -1;
|
||||||
GLDEBUG(glGenBuffers(1, &m_currentVBO.handle));
|
GLDEBUG(glGenBuffers(1, &m_currentVBO.handle));
|
||||||
|
|
||||||
|
|
||||||
GLDEBUG(glBindBuffer(GL_ARRAY_BUFFER, m_currentVBO.handle));
|
GLDEBUG(glBindBuffer(GL_ARRAY_BUFFER, m_currentVBO.handle));
|
||||||
GLDEBUG(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
|
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.size = size;
|
||||||
m_currentVBO.data = data;
|
m_currentVBO.data = data;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
#ifndef KRMODELMANAGER_H
|
#ifndef KRMODELMANAGER_H
|
||||||
#define KRMODELMANAGER_H
|
#define KRMODELMANAGER_H
|
||||||
|
|
||||||
#define KRENGINE_MAX_VBO_HANDLES 1000
|
#define KRENGINE_MAX_VBO_HANDLES 10000
|
||||||
#define KRENGINE_MAX_VBO_MEM 50000000
|
#define KRENGINE_MAX_VBO_MEM 50000000
|
||||||
|
|
||||||
#import "KREngine-common.h"
|
#import "KREngine-common.h"
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "KRShader.h"
|
#include "KRShader.h"
|
||||||
|
#import "assert.h"
|
||||||
|
|
||||||
KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) {
|
KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) {
|
||||||
strcpy(m_szKey, szKey);
|
strcpy(m_szKey, szKey);
|
||||||
@@ -264,15 +265,21 @@ bool KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix
|
|||||||
#if defined(DEBUG)
|
#if defined(DEBUG)
|
||||||
GLint logLength;
|
GLint logLength;
|
||||||
|
|
||||||
|
GLint validate_status = GL_FALSE;
|
||||||
GLDEBUG(glValidateProgram(m_iProgram));
|
GLDEBUG(glValidateProgram(m_iProgram));
|
||||||
|
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));
|
GLDEBUG(glGetProgramiv(m_iProgram, GL_INFO_LOG_LENGTH, &logLength));
|
||||||
if (logLength > 0)
|
if (logLength > 0)
|
||||||
{
|
{
|
||||||
GLchar *log = (GLchar *)malloc(logLength);
|
GLchar *log = (GLchar *)malloc(logLength);
|
||||||
assert(log != NULL);
|
assert(log != NULL);
|
||||||
GLDEBUG(glGetProgramInfoLog(m_iProgram, logLength, &logLength, log));
|
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);
|
fprintf(stderr, "Program validate log:\n%s", log);
|
||||||
free(log);
|
free(log);
|
||||||
|
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera,
|
|||||||
std::map<std::string, KRShader *>::iterator itr = m_shaders.begin();
|
std::map<std::string, KRShader *>::iterator itr = m_shaders.begin();
|
||||||
delete (*itr).second;
|
delete (*itr).second;
|
||||||
m_shaders.erase(itr);
|
m_shaders.erase(itr);
|
||||||
fprintf(stderr, "Swapping shaders...\n");
|
//fprintf(stderr, "Swapping shaders...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
stringstream stream;
|
stringstream stream;
|
||||||
@@ -148,3 +148,7 @@ const std::string &KRShaderManager::getFragShaderSource(const std::string &name)
|
|||||||
const std::string &KRShaderManager::getVertShaderSource(const std::string &name) {
|
const std::string &KRShaderManager::getVertShaderSource(const std::string &name) {
|
||||||
return m_vertShaderSource[name];
|
return m_vertShaderSource[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long KRShaderManager::getShaderHandlesUsed() {
|
||||||
|
return m_shaders.size();
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ using std::vector;
|
|||||||
#ifndef KRSHADERMANAGER_H
|
#ifndef KRSHADERMANAGER_H
|
||||||
#define KRSHADERMANAGER_H
|
#define KRSHADERMANAGER_H
|
||||||
|
|
||||||
#define KRENGINE_MAX_SHADER_HANDLES 1000
|
#define KRENGINE_MAX_SHADER_HANDLES 100
|
||||||
|
|
||||||
class KRShaderManager : public KRContextObject {
|
class KRShaderManager : public KRContextObject {
|
||||||
public:
|
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);
|
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:
|
private:
|
||||||
std::map<std::string, KRShader *> m_shaders;
|
std::map<std::string, KRShader *> m_shaders;
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ void KRTextureManager::selectTexture(int iTextureUnit, KRTexture *pTexture) {
|
|||||||
m_boundTextures[iTextureUnit] = pTexture;
|
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() > KRENGINE_MAX_TEXTURE_HANDLES || m_textureMemUsed > KRENGINE_MAX_TEXTURE_MEM) {
|
||||||
if(m_poolTextures.empty()) {
|
if(m_poolTextures.empty()) {
|
||||||
|
//fprintf(stderr, "flushBuffers due to texture exhaustion...\n");
|
||||||
m_pContext->rotateBuffers();
|
m_pContext->rotateBuffers();
|
||||||
}
|
}
|
||||||
// Keep texture size within limits
|
// Keep texture size within limits
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
#define KRENGINE_MAX_TEXTURE_UNITS 8
|
#define KRENGINE_MAX_TEXTURE_UNITS 8
|
||||||
#define KRENGINE_MAX_TEXTURE_HANDLES 10000
|
#define KRENGINE_MAX_TEXTURE_HANDLES 10000
|
||||||
#define KRENGINE_MAX_TEXTURE_MEM 200000000
|
#define KRENGINE_MAX_TEXTURE_MEM 100000000
|
||||||
|
|
||||||
#ifndef KRTEXTUREMANAGER_H
|
#ifndef KRTEXTUREMANAGER_H
|
||||||
#define KRTEXTUREMANAGER_H
|
#define KRTEXTUREMANAGER_H
|
||||||
|
|||||||
@@ -106,7 +106,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
[self.engine setNearZ: 5.0];
|
[self.engine setNearZ: 5.0];
|
||||||
[self.engine setFarZ: 1000.0];
|
[self.engine setFarZ: 3000.0];
|
||||||
//[renderEngine setNearZ: 1.0];
|
//[renderEngine setNearZ: 1.0];
|
||||||
//[renderEngine setFarZ: 3000.0];
|
//[renderEngine setFarZ: 3000.0];
|
||||||
|
|
||||||
|
|||||||
@@ -180,9 +180,9 @@
|
|||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
- (void)drawView:(id)sender
|
- (void)drawView:(id)sender
|
||||||
{
|
{
|
||||||
|
// @synchronized(self) {
|
||||||
if(glView.context && glView.engine) {
|
if(glView.context && glView.engine) {
|
||||||
//glGetError(); // Clear any prior errors...
|
//glGetError(); // Clear any prior errors...
|
||||||
|
|
||||||
@@ -274,6 +274,7 @@
|
|||||||
|
|
||||||
//NSLog(@"frameTime = %.1f ms (%.2f fps / %.2f fps) - %.2f%%", frameTime * 1000.0f, 1.0f / frameTime, 1.0f / deltaTime, frameTime / deltaTime * 100.0f);
|
//NSLog(@"frameTime = %.1f ms (%.2f fps / %.2f fps) - %.2f%%", frameTime * 1000.0f, 1.0f / frameTime, 1.0f / deltaTime, frameTime / deltaTime * 100.0f);
|
||||||
}
|
}
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user