Jump to content
  • Advertisement
Sign in to follow this  
wforl

Efficient Font Rendering

This topic is 3126 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

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
	PROF("Build")
		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	);		 
			
			++i;
			l_Char = *(++l_pChar);
			++x;
		}
	ENDPROF
	
	PROF("Map")
		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));
		m_pVBuffer->Unmap();
	ENDPROF

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

		g_pEngine->GetRenderer()->SetDepthStencilState(DEPTH_STENCIL_DISABLE_WRITE_AND_TEST);
		g_pEngine->GetRenderer()->SetBlendState(BLEND_ALPHA_MUL);
			m_pD3dDevice->IASetInputLayout(m_pInputLayout);
			m_pD3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
			m_pFXVarTex->AsShaderResource()->SetResource(l_pFont->m_pBitmapFontTexture->GetSRView());
			m_pFXVarWVP->AsMatrix()->SetMatrix(S*T);
			m_pFXVarColour->AsVector()->SetFloatVectorArray((float*)&Colour, 0, 4);
			m_pFXTech->GetPassByIndex(0)->Apply(0);
			UINT stride = sizeof(VertexPT);
			UINT offset = 0;
			m_pD3dDevice->IASetVertexBuffers(0, 1, &m_pVBuffer, &stride, &offset);
			m_pD3dDevice->Draw(6*strlen(pText), 0);
		g_pEngine->GetRenderer()->SetBlendState(BLEND_DEFAULT);
		g_pEngine->GetRenderer()->SetDepthStencilState(DEPTH_STENCIL_DEFAULT);
	ENDPROF


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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!