Sign in to follow this  

Efficient Font Rendering

Recommended Posts

Im looking for a more effcient way for rendering fonts within my application (bitmaps). Ive been profiling some of my code and have found that the font rendering to be taking up a large sum of frame time, which I would like to reduce. At the moment I have a dynamic vertex buffer which is enough to render 2000 characters to screen, which I edit each time text needs to be rendered, by changing the vertex positions and UV coordinates. This buffer is then rendered in a single draw call. You can see this below
		const char* l_pChar = pText;
		char l_Char = (*l_pChar);
		float U = 1.0f / (float)l_pFont->m_Columns;
		float V = 1.0f / (float)l_pFont->m_Rows;
		size_t x = 0;
		size_t y = 0;
		size_t i = 0;
		while(l_Char != '\0')
			if(l_Char == '\n') 
				{ ++y; x = 0; l_Char = *(++l_pChar); continue;}
			int R = (*l_pChar) / l_pFont->m_Rows;			
			int C = (*l_pChar) % l_pFont->m_Columns;
			// Set up UV coordinates
			l_Buffer[0 + (i * 6)] = VertexPT((-1.0f + l_pFont->m_Padding * (2*x)), ( 1.0f - (2*y)), 0.0f,	(C * U),		(R * V)		);	
			l_Buffer[1 + (i * 6)] = VertexPT(( 1.0f + l_pFont->m_Padding * (2*x)), ( 1.0f - (2*y)), 0.0f,	(C * U) + U,	(R * V)		);				
			l_Buffer[2 + (i * 6)] = VertexPT(( 1.0f + l_pFont->m_Padding * (2*x)), (-1.0f - (2*y)), 0.0f,	(C * U) + U,	(R * V) + V	);			
			l_Buffer[3 + (i * 6)] = VertexPT((-1.0f + l_pFont->m_Padding * (2*x)), ( 1.0f - (2*y)), 0.0f,	(C * U),		(R * V)		);		   
			l_Buffer[4 + (i * 6)] = VertexPT(( 1.0f + l_pFont->m_Padding * (2*x)), (-1.0f - (2*y)), 0.0f,	(C * U) + U,	(R * V) + V	);		   
			l_Buffer[5 + (i * 6)] = VertexPT((-1.0f + l_pFont->m_Padding * (2*x)), (-1.0f - (2*y)), 0.0f,	(C * U),		(R * V) + V	);		 
			l_Char = *(++l_pChar);
		static VertexPT* l_pVBuffer = 0;
		m_pVBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&l_pVBuffer);
			memcpy(l_pVBuffer, (void*)l_Buffer, sizeof(VertexPT) * 6 * strlen(pText));

		static fsMatrix44 T,S;
		T.Translation(Scale+Pos.x, -(Scale*AspectRatio)+Pos.y, 0.0f);
		S.Scaling(Scale, Scale*AspectRatio, Scale);

			m_pFXVarColour->AsVector()->SetFloatVectorArray((float*)&Colour, 0, 4);
			UINT stride = sizeof(VertexPT);
			UINT offset = 0;
			m_pD3dDevice->IASetVertexBuffers(0, 1, &m_pVBuffer, &stride, &offset);
			m_pD3dDevice->Draw(6*strlen(pText), 0);

Other than to cache buffers, are there any more effient techniques? Hopefully something that doesnt require the updating of dynamic buffers each frame.

Share this post

Link to post
Share on other sites
Without looking at the code ...

If your text is not too dynamic you can render the text to a seperate render target (read: texture) and display this texture on a single rectangle. This applies if you have lengthy texts of hundreds of characters that do not change from frame to frame but only every few seconds or so.

Just render the text a single time as you do now but to a texture. And from then on just display this texture.

But if you have to deal with very dynamic texts that you do not know and hence can't preprocess at all you have not much options at all. Then you can only try caching whole words if your text is based on a specific language.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this