Graphics Glitch: Disappear and Warp?

Started by
1 comment, last by noodleBowl 9 years ago
I am testing out my sprite batcher and I came across a case where I would get a graphics glitch

When setting my sprite batcher's max batch to a certain number (I have set it to 20000, 25000, 50000) I come across a big glitch where my quads seem to wrap / disappear (it gets really bad if the max is set to 20k, the quads seem to flicker in some cases here). I have not seen this glitch with the lower max batch numbers, so now I'm kind of stuck wondering what is going on?

Does anyone know where I might be going wrong?

Other infor / code:

I'm drawing 50005 quads where the max batch is set to 20k
There are 3 different textures being drawn too, which show all correctly in terms of the image

//Map the VBO
void SpriteBatcher::MapBuffer()
{
	if (mapPointer != NULL)
		return;


	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	if (mapBufferOffset == mapBufferLength)
	{
		mapBufferOffset = 0;
		baseVertex = 0;
	    glBufferData(GL_ARRAY_BUFFER, mapBufferLength, NULL, GL_DYNAMIC_DRAW);
	}

	mapPointer = (GLfloat*)glMapBufferRange(GL_ARRAY_BUFFER, mapBufferOffset, mapBufferLength - mapBufferOffset, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
	if (mapPointer == NULL)
		throw std::runtime_error("Sprite Batcher Error. Map Pointer is NULL");
}

//Flush check: Figure out if we need a new buffer cause the current is full or changed texture
void SpriteBatcher::FlushCheck(GLuint texture)
{
	if (batchTexture == texture && mapBufferOffset != mapBufferLength)
		return;

	Render();
	MapBuffer();
	batchTexture = texture;
}

//Render the batch or set of quads
void SpriteBatcher::Render()
{
	if (mapIndex == 0)
		return;

	UnmapBuffer();
	
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, batchTexture);
	glUniform1i(shaderProgram.uniforms[1].location, 0);

	glBindVertexArray(vao);
	glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)(mapIndex * QUAD_TO_VERTEX_COMPONENT_COUNT_RATIO * INDICES_COUNT_PER_QUAD), GL_UNSIGNED_SHORT, (const void*)NULL, baseVertex);
	baseVertex += (GLint)(mapIndex * VERTEX_PER_QUAD_COUNT_TO_VERTEX_COMPONENT_COUNT_RATIO);
	mapIndex = 0;
}


//Inside main
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

batch.Begin(orthoMatrix);

for (int i = 0, j = 0; i < 25000; ++i, j += 2)
	batch.Draw(debug2, pos[j], pos[j + 1]);

for (int i = 25000, j = 25000; i < 50000; ++i, j += 2)
	batch.Draw(debug1, pos[j], pos[j + 1]);

batch.Draw(debug1, 50.0f, 50.0f);
batch.Draw(debug2, 150.0f, 50.0f);
batch.Draw(debug3, 350.0f, 350.0f);
batch.Draw(debug1, 350.0f, 350.0f);

batch.End();

Advertisement

You need 4 vertices for a rectangular sprite. For 20000 you need 80000 vertices. The maximum value that can be represented by an GL_UNSIGNED_SHORT index is 65535. So you can't draw them with a single draw call.

This is only a guess since I don't see the code that creates the batches.

You need 4 vertices for a rectangular sprite. For 20000 you need 80000 vertices. The maximum value that can be represented by an GL_UNSIGNED_SHORT index is 65535. So you can't draw them with a single draw call.

This is only a guess since I don't see the code that creates the batches.

I totally never would have thought that my issue was a overflow error!
I switched it to use UNSIGNED INT instead and the glitch is gone!

This topic is closed to new replies.

Advertisement