PDA

View Full Version : Reusing VBO's causing errors.



CaptainSnugglebottom
02-16-2018, 07:03 AM
I realize this thread (https://www.opengl.org/discussion_boards/showthread.php/200192-Program-crashes-when-trying-to-draw-a-triangle-using-glDrawArrays?p=1290488) is a bit old, but I'd rather post here than make a new thread about the same problem.

My renderer has 4 stages, and for whatever reason stages 2 3 and 4 cause issues. They work on their own, and when one of the stages is commented out, but they fail when working together. When everything is enabled and glDrawArrays in stage 4 is called, the application exists with ACCESS VIOLATION error in the nvoglv64.dll file.

I do initialize every opengl object (nothing is 0, and as I mentioned they all work perfectly when at least 1 other stage is commented out). I also checked the FBO, and it didn't show any errors.


// STAGE 1 (Solid Render) ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (subSceneDebugFlag) { std::cout << " Stage 1..." << std::endl; }

glBindFramebuffer(GL_FRAMEBUFFER, this->subSceneFBO1);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(this->subSceneShaderArray[0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->colorMapID);
glUniform1i(this->stage1ColorMapLocation, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->normalMapID);
glUniform1i(this->stage1NormalMapLocation, 1);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->specularMapID);
glUniform1i(this->stage1SpecularMapLocation, 2);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->lightMapID);
glUniform1i(this->stage1LightMapLocation, 3);

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);

// Binding SSBO
glBindBuffer(GL_SHADER_STORAGE_BUFFER, this->objectSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this->objectSSBO);

// Binding VBO
glBindVertexArray(this->objectVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->objectVBO);
glDrawArrays(GL_TRIANGLES, 0, vertexIndex);

glDisableVertexAttribArray(5);
glDisableVertexAttribArray(4);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(2);

// STAGE 2 (Solid Shading) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (subSceneDebugFlag) { std::cout << " Stage 2..." << std::endl; }

glBindFramebuffer(GL_FRAMEBUFFER, this->subSceneFBO2);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(this->subSceneShaderArray[1]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->stage1ColorMapID);
glUniform1i(this->stage2ColorMapLocation, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, this->stage1NormalMapID);
glUniform1i(this->stage2NormalMapLocation, 1);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, this->stage1SpecularMapID);
glUniform1i(this->stage2SpecularMapLocation, 2);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, this->stage1LightMapID);
glUniform1i(this->stage2LightMapLocation, 3);

glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, this->stage1DepthMapID);
glUniform1i(this->stage2DepthMapLocation, 4);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindVertexArray(this->subSceneVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->subSceneVBO);
glDrawArrays(GL_TRIANGLES, 0, 6);

// STAGE 3 (Alpha Shading) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (subSceneDebugFlag) { std::cout << " Stage 3..." << std::endl; }

glUseProgram(this->subSceneShaderArray[2]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->colorMapID);
glUniform1i(this->stage3ColorMapLocation, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->normalMapID);
glUniform1i(this->stage3NormalMapLocation, 1);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->specularMapID);
glUniform1i(this->stage3SpecularMapLocation, 2);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, this->textureAsset->lightMapID);
glUniform1i(this->stage3LightMapLocation, 3);

glDepthMask(GL_FALSE);
glEnable(GL_BLEND);

glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);

// Binding SSBO
glBindBuffer(GL_SHADER_STORAGE_BUFFER, this->objectSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, this->objectSSBO);

// Binding VBO
glBindVertexArray(this->objectVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->objectVBO);
glDrawArrays(GL_TRIANGLES, 0, vertexIndex);

glDepthMask(GL_TRUE);

// STAGE 4 (Draw To Buffer) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (subSceneDebugFlag) { std::cout << " Stage 4..." << std::endl; }

glBindFramebuffer(GL_FRAMEBUFFER, this->subSceneFBO4);

glUseProgram(this->subSceneShaderArray[3]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->stage2ColorMapID);
glUniform1i(this->stage4ColorMapLocation, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, this->stage1ControlMapID);
glUniform1i(this->stage4ControlMapLocation, 1);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindVertexArray(this->subSceneVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->subSceneVBO);
glDrawArrays(GL_TRIANGLES, 0, 6);

I actually think it might be the VBO, since the thing works without either stage 2 or 4. I enabled everything and used glDrawArrays(GL_TRIANGLES, 0, 3) in the stage 4, and it drew exactly 1 triangle as expected, but drawing 2 causes the error.

It is the same VAO/VBO as used in stage 2, which is even more strange.

The VAO/VBO is initialized as:

// Sub Scene VBO
glGenBuffers(1, &(this->subSceneVBO));
glBindBuffer(GL_ARRAY_BUFFER, this->subSceneVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(this->subScenePlanePoints), this->subScenePlanePoints, GL_STATIC_DRAW);

glGenVertexArrays(1, &(this->subSceneVAO));
glBindVertexArray(this->subSceneVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->subSceneVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);

I am really lost here, it's driving me crazy. What are the other ways I can explore to figure out where the problem is?

I updated the drivers to the latest version, and the problem remained.


EDIT:

Never mind that, I realized that I only EnableVertexAttribArray once when I made the VAO. I removed all of those things and it works now.