Compare commits

..

2 Commits

2 changed files with 91 additions and 28 deletions

47
font.fx
View File

@ -13,7 +13,12 @@ SamplerState samPoint {
struct VS_INPUT
{
float2 Pos : POSITION;
float4 Pos : TEXCOORD;
};
struct GS_INPUT
{
float4 Pos : SV_POSITION;
};
struct PS_INPUT
@ -22,18 +27,42 @@ struct PS_INPUT
float2 Tex : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;
float2 Pos = input.Pos * vGlyphScale + vPosition;
GS_INPUT VS(VS_INPUT input)
{
GS_INPUT output;
output.Pos = input.Pos;
return output;
}
static const float2 vertices[] = {
float2( 0.0f, 0.0f), // -- top right
float2( 1.0f, 0.0f), // -- top left
float2( 0.0f, -1.0f), // -- bottom right
float2( 1.0f, -1.0f), // -- bottom left
};
[maxvertexcount(4)]
void GS (point GS_INPUT input[1], inout TriangleStream<PS_INPUT> TriStream)
{
PS_INPUT output;
for (int i = 0; i < 4; i++) {
float2 Pos;
//Pos = vertices[i] * vGlyphScale + vPosition;
Pos = vertices[i] * vGlyphScale + input[0].Pos.xy;
Pos = Pos * vInvScreenSize + float2(-1, 1);
output.Pos = float4(Pos.xy, 0, 1);
float2 Tex = float2(input.Pos.x, -input.Pos.y);
output.Tex = vertices[i] * float2(1, -1);
//output.Tex = (output.Tex + vCharCoord) * vTexScale;
output.Tex = (output.Tex + input[0].Pos.zw) * vTexScale;
output.Tex = (Tex + vCharCoord) * vTexScale;
return output;
TriStream.Append(output);
}
TriStream.RestartStrip();
}
float4 PS(PS_INPUT input) : SV_Target
@ -50,7 +79,7 @@ technique10 Font
pass P0
{
SetVertexShader(CompileShader(vs_4_0, VS()));
SetGeometryShader(NULL);
SetGeometryShader(CompileShader(gs_4_0, GS()));
SetPixelShader(CompileShader(ps_4_0, PS()));
}
}

View File

@ -370,16 +370,8 @@ HRESULT LoadMesh()
return S_OK;
}
struct FontVertex {
D3DXVECTOR2 Pos;
};
const FontVertex FontVertices[] = {
D3DXVECTOR2( 0.0f, 0.0f), // -- top right
D3DXVECTOR2( 1.0f, 0.0f), // -- top left
D3DXVECTOR2( 0.0f, -1.0f), // -- bottom right
D3DXVECTOR2( 1.0f, -1.0f), // -- bottom left
};
const int g_iFontBufferLength = 512;
typedef D3DXVECTOR4 FontVertex;
HRESULT InitFontBuffers()
{
@ -422,7 +414,7 @@ HRESULT InitFontBuffers()
//////////////////////////////////////////////////////////////////////
D3D10_INPUT_ELEMENT_DESC layout[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0},
};
UINT numElements = (sizeof (layout)) / (sizeof (layout[0]));
@ -443,16 +435,15 @@ HRESULT InitFontBuffers()
//////////////////////////////////////////////////////////////////////
D3D10_BUFFER_DESC bd;
D3D10_SUBRESOURCE_DATA initData;
// position
bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = (sizeof (FontVertices));
bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = (sizeof (FontVertex)) * g_iFontBufferLength;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;
initData.pSysMem = FontVertices;
hr = g_pd3dDevice->CreateBuffer(&bd, &initData, &g_pVertexBuffersFont[0]);
hr = g_pd3dDevice->CreateBuffer(&bd, NULL, &g_pVertexBuffersFont[0]);
if (FAILED(hr)) {
print("CreateBuffer\n");
return hr;
@ -899,6 +890,49 @@ void RenderModel(float t)
void RenderFont()
{
//////////////////////////////////////////////////////////////////////
// dynamic vertex buffer
//////////////////////////////////////////////////////////////////////
FontVertex * pData;
HRESULT hr;
hr = g_pVertexBuffersFont[0]->Map(D3D10_MAP_WRITE_DISCARD,
0,
(void **)&pData);
if (FAILED(hr)) {
print("g_pVertexBuffersFont->Map");
}
int advance = 10;
const char * s = "asdf";
int ix = 0;
const int charStride = (int)(g_FontSize.Texture.Width / g_FontSize.Glyph.Width);
while (ix < g_iFontBufferLength && *s) {
char c = *s++;
float px = (float)advance;
float py = -10.0;
float cx = 0;
float cy = 0;
if (c >= 0x20 && c <= 0x7f) {
c -= 0x20;
cx = (float)(c % charStride);
cy = (float)(c / charStride);
print("%c %f %f\n", c + 0x20, cx, cy);
}
pData[ix++] = FontVertex(px, py, cx, cy);
advance += g_FontSize.Glyph.Width;
}
g_pVertexBuffersFont[0]->Unmap();
//////////////////////////////////////////////////////////////////////
// effect variables
//////////////////////////////////////////////////////////////////////
D3DXVECTOR2 invScreenSize = D3DXVECTOR2(2.0f / (float)g_ViewportSize.Width,
2.0f / (float)g_ViewportSize.Height);
@ -923,7 +957,7 @@ void RenderFont()
UINT offset[] = { 0 };
g_pd3dDevice->IASetInputLayout(g_pVertexLayoutFont);
g_pd3dDevice->IASetVertexBuffers(0, g_dwVertexBufferCountFont, g_pVertexBuffersFont, stride, offset);
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);
D3D10_TECHNIQUE_DESC techDesc;
g_pTechniqueFont->GetDesc(&techDesc);