Jump to content
  • Advertisement
Sign in to follow this  
tmason

OpenGL Speeding up loading of uniforms and drawing ...

This topic is 1343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

So I have this strange issue with my code working very slowly at first and then "speeding up" after about 15-30 seconds of running depending on the size of the model.

The code in question is my drawing functionality for objects.

The first code that is initially dreadfully slow is loading of uniforms into OpenGL and the second set of code is the actual draws.

The thing that really bothers me is when things speed up after as mentioned 30 seconds. I ensured that it isn't system load, etc.

Has anyone seen this before?

Thanks.
 

void OpenGLObject::DrawMe(void) {

	ViewModelMatrix = (*ViewMatrix) * ModelMatrix;
	MVPMatrix = (*ProjectionMatrix) * (*ViewMatrix) * ModelMatrix;
	NormalMatrix = glm::transpose(glm::inverse(glm::mat3(MVPMatrix)));

	glBindVertexArray(VertextArrayObjectID);
	
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetMVPMatrixID()), 1, GL_FALSE, glm::value_ptr(MVPMatrix));
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetViewMatrixID()), 1, GL_FALSE, glm::value_ptr((*ViewMatrix)));
	glUniformMatrix4fv((*AssociatedOpenGLProgram->GetViewModelMatrixID()), 1, GL_FALSE, glm::value_ptr(ViewModelMatrix));
	glUniformMatrix3fv((*AssociatedOpenGLProgram->GetNormalMatrixID()), 1, GL_FALSE, glm::value_ptr(NormalMatrix));

	/*
	
	Slow code here.
	
	*/
	
	AssociatedMaterial->LoadColorsIntoOpenGL(&AssociatedOpenGLProgram[0]);
	
	/*
	
	End slow code.
	
	*/

	if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

		GLfloat EnableWireframe = 1.0f;
		glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	}
	else {

		if (AssociatedMaterial->HasAssociatedTexture()) {

			GLfloat ObjectHasMaterial = 1.0f;
			glUniform1fv((*AssociatedOpenGLProgram->GetObjectHasTextureID()), 1, &ObjectHasMaterial);
			glUniform1i((*AssociatedOpenGLProgram->GetTextureSamplerID()), 1);

			AssociatedMaterial->BindTexture(1);

		}
		else {

			glBindTexture(GL_TEXTURE_2D, NULL);

		}

	}

	/*
	
	Slow code here.
	
	*/
	
	glDrawElementsInstanced(GL_TRIANGLES, NumOfIndices, GL_UNSIGNED_INT, NULL, 1);
	
	/*
	
	End slow code.
	
	*/

	if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

		GLfloat EnableWireframe = 0.0f;
		glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	}

}

And the referenced Uniforms function "AssociatedMaterial->LoadColorsIntoOpenGL(&AssociatedOpenGLProgram[0]);" :
 

void OpenGLCompatibilityMaterial::LoadColorsIntoOpenGL(OpenGLProgram* CurrentProgram) {

	glUniform4fv((*CurrentProgram->GetAmbientColorID()), 1, GetAmbientColor());
	glUniform4fv((*CurrentProgram->GetDiffuseColorID()), 1, GetDiffuseColor());
	glUniform4fv((*CurrentProgram->GetEmissiveColorID()), 1, GetEmissiveColor());
	glUniform4fv((*CurrentProgram->GetSpecularColorID()), 1, GetSpecularColor());

	GLfloat MeshShininess = GetShininess();
	glUniform1fv((*CurrentProgram->GetMeshShininessID()), 1, &MeshShininess);

}

Share this post


Link to post
Share on other sites
Advertisement
How many milliseconds per frame does that 'slow' function take in the slow and fast situations?
Is the performance difference on the CPU time per frame or the gpu time per frame?

GL drivers are known to very quickly compile unoptimized shaders initially, and then slowly work on compiling more optimal ones in the background. Something like that could be affecting you.

Share this post


Link to post
Share on other sites

How many milliseconds per frame does that 'slow' function take in the slow and fast situations?
Is the performance difference on the CPU time per frame or the gpu time per frame?

GL drivers are known to very quickly compile unoptimized shaders initially, and then slowly work on compiling more optimal ones in the background. Something like that could be affecting you.

 

The performance I measured is on the CPU side; in a slow situation it will take about 3-10 milliseconds per frame (maybe more) but then when things speed up the operation is almost instantaneous.

 

Interesting on the compiler situation. Any way to get the GL driver to compile better shaders up front? I would pay the cost (maybe 10 additional seconds of load time?) versus jittery behavior when the environment is fully loaded.

 

Thank you for your time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!