Jump to content
  • Advertisement
Sign in to follow this  

Making a font engine

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

First post here. Hope I don't do anything that's not okay here.

I'm making a font engine for my Tetris game, but I've run into problems. Problem is I don't know where the problem is, cause everything seems right. Except that no text is drawn of course.

Here is the effects file, anything strange there? The first part is for my colored tetris cubes, and it works fine. It's the second part, that I'm trying to make my font shader.

The code is based on the tutorials from Rastertek, but with some modifications. I have a completely different class structure, and I'm trying to use XNA math.

[source lang="cpp"]cbuffer cbPerObject
float4x4 WVP;

float4 Pos : SV_POSITION;
float4 Color : COLOR;

VS_COLOR_OUTPUT VS_Color(float4 inPos : POSITION, float4 inColor : COLOR)
output.Pos = mul(inPos, WVP);
output.Color = inColor;

return output;

float4 PS_Color(VS_COLOR_OUTPUT input) : SV_TARGET
return input.Color;

float4 position: SV_POSITION;
float2 tex: TEXCOORD0;


// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, WVP);

// Store the texture coordinates for the pixel shader.
output.tex = input.tex;

return output;

Texture2D shaderTexture;
SamplerState SampleType;

cbuffer PixelBuffer
float4 pixelColor;

float4 color;

// Sample the texture pixel at this location.
color = shaderTexture.Sample(SampleType, input.tex);

// If the color is black on the texture then treat this pixel as transparent.
if(color.r == 0.0f)
color.a = 0.0f;

// If the color is other than black on the texture then this is a pixel in the font so draw it using the font pixel color.
color.a = 1.0f;
color = color * pixelColor;

return color;

Here is the Text::Render() which is supposed to render the text:

[source lang="cpp"]void Text::Render() {

unsigned int stride, offset;
D3DXVECTOR4 pixelColor;

// Set vertex buffer stride and offset.
stride = sizeof(TextureVertex);
offset = 0;

// Set the vertex buffer to active in the input assembler so it can be rendered.
pD3DEngine->getDeviceContext()->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);

// Set the index buffer to active in the input assembler so it can be rendered.
pD3DEngine->getDeviceContext()->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.

// Create a pixel color vector with the input sentence color.
pixelColor = D3DXVECTOR4(colorR, colorG, colorB, 1.0f);

XMFLOAT4X4 worldMatrix;
XMStoreFloat4x4(&worldMatrix, XMMatrixIdentity());
XMMATRIX WVP = XMLoadFloat4x4(&worldMatrix) * pD3DEngine->getViewMatrix() * pD3DEngine->getProjectionMatrix();

// Lock the constant buffer so it can be written to.
D3D11_MAPPED_SUBRESOURCE mappedResource;
hr = pD3DEngine->getDeviceContext()->Map(pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

// Get a pointer to the data in the constant buffer.
cbPerObject* dataPtr;
dataPtr = (cbPerObject*)mappedResource.pData;

XMStoreFloat4x4(&dataPtr->WVP, XMMatrixTranspose(WVP));
// Unlock the constant buffer.
pD3DEngine->getDeviceContext()->Unmap(pConstantBuffer, 0);

// Now set the constant buffer in the vertex shader with the updated values.
pD3DEngine->getDeviceContext()->VSSetConstantBuffers(0, 1, &pConstantBuffer);

// Set shader texture resource in the pixel shader.
ID3D11ShaderResourceView* tex = pFont->GetTexture()->GetTexture();
pD3DEngine->getDeviceContext()->PSSetShaderResources(0, 1, &tex);

pD3DEngine->getDeviceContext()->VSSetShader(pFontShader->GetVertexShader(), 0, 0);
pD3DEngine->getDeviceContext()->PSSetShader(pFontShader->GetPixelShader(), 0, 0);

// Set the sampler state in the pixel shader.
pD3DEngine->getDeviceContext()->PSSetSamplers(0, 1, &pSampleState);

// Render the triangles.
pD3DEngine->getDeviceContext()->DrawIndexed(numIndices, 0, 0);
I'm happy to post more code on request, if this code looks allright.

Btw: the constant buffer struct is just
[source lang="cpp"]struct cbPerObject {

EDIT: Found out that my DrawIndexed call yields:
ID3D11DeviceContext::DrawIndexed: Constant Buffer expected at slot 0 of the Pixel Shader unit (with size at least 16 bytes) but none is bound. This is OK, as reads of an unbound Constant Buffer are defined to return 0. It is also posible the developer knows the data will not be used anyway. This is only a problem if the developer actually intended to bind a Constant Buffer here. [ EXECUTION INFO #350: DEVICE_DRAW_CONSTANT_BUFFER_NOT_SET ]

Why is this? I do set the constant buffer! Edited by SamiHuutoniemi

Share this post

Link to post
Share on other sites
You set the Vertex shaders CBuffer only, but in your pixel shader, you have another CBuffer called PixelBuffer, which is never set but it is used (which I assume was meant to be filled with the [color=#000000][font=Consolas,]pixelColor variable)[/font]. you need to use PSSetConstantBuffers to bind the CBuffer for your pixel shader (or remove the CBuffer and pass it as a per instance parameter).

As for debugging, best way is to create a debug device while developing (and set the full verbose DX debug output) and have SysInternals' DbgView running in the background. It also requires you to be a bit more familiar with the API, and read the debug messages more carefully. Edited by Necrolis

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!