font.fx: geometry shaded text string

This commit is contained in:
Zack Buhman 2026-01-06 22:48:18 -06:00
parent c2557116a8
commit 8bcf7d5d79
2 changed files with 71 additions and 30 deletions

31
font.fx
View File

@ -13,21 +13,26 @@ SamplerState samPoint {
struct VS_INPUT struct VS_INPUT
{ {
float2 Pos : POSITION; float4 Pos : TEXCOORD;
}; };
struct GSPS_INPUT struct GS_INPUT
{
float4 Pos : SV_POSITION;
};
struct PS_INPUT
{ {
float4 Pos : SV_POSITION; float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0; float2 Tex : TEXCOORD0;
}; };
GSPS_INPUT VS(VS_INPUT input)
{
GSPS_INPUT output = (GSPS_INPUT)0;
output.Pos = float4(0, 0, 0, 0); GS_INPUT VS(VS_INPUT input)
output.Tex = float2(0, 0); {
GS_INPUT output;
output.Pos = input.Pos;
return output; return output;
} }
@ -40,25 +45,27 @@ static const float2 vertices[] = {
}; };
[maxvertexcount(4)] [maxvertexcount(4)]
void GS (point GSPS_INPUT input[1], inout TriangleStream<GSPS_INPUT> TriStream) void GS (point GS_INPUT input[1], inout TriangleStream<PS_INPUT> TriStream)
{ {
GSPS_INPUT output; PS_INPUT output;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
float2 Pos; float2 Pos;
Pos = vertices[i] * vGlyphScale + vPosition; //Pos = vertices[i] * vGlyphScale + vPosition;
Pos = vertices[i] * vGlyphScale + input[0].Pos.xy;
Pos = Pos * vInvScreenSize + float2(-1, 1); Pos = Pos * vInvScreenSize + float2(-1, 1);
output.Pos = float4(Pos.xy, 0, 1); output.Pos = float4(Pos.xy, 0, 1);
output.Tex = vertices[i] * float2(1, -1); output.Tex = vertices[i] * float2(1, -1);
output.Tex = (output.Tex + vCharCoord) * vTexScale; //output.Tex = (output.Tex + vCharCoord) * vTexScale;
output.Tex = (output.Tex + input[0].Pos.zw) * vTexScale;
TriStream.Append(output); TriStream.Append(output);
} }
TriStream.RestartStrip(); TriStream.RestartStrip();
} }
float4 PS(GSPS_INPUT input) : SV_Target float4 PS(PS_INPUT input) : SV_Target
{ {
float4 texColor = txDiffuse.Sample(samPoint, input.Tex); float4 texColor = txDiffuse.Sample(samPoint, input.Tex);

View File

@ -370,16 +370,8 @@ HRESULT LoadMesh()
return S_OK; return S_OK;
} }
struct FontVertex { const int g_iFontBufferLength = 512;
D3DXVECTOR2 Pos; typedef D3DXVECTOR4 FontVertex;
};
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
};
HRESULT InitFontBuffers() HRESULT InitFontBuffers()
{ {
@ -422,7 +414,7 @@ HRESULT InitFontBuffers()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
D3D10_INPUT_ELEMENT_DESC layout[] = { 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])); UINT numElements = (sizeof (layout)) / (sizeof (layout[0]));
@ -443,16 +435,15 @@ HRESULT InitFontBuffers()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
D3D10_BUFFER_DESC bd; D3D10_BUFFER_DESC bd;
D3D10_SUBRESOURCE_DATA initData;
// position // position
bd.Usage = D3D10_USAGE_DEFAULT; bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = (sizeof (FontVertices)); bd.ByteWidth = (sizeof (FontVertex)) * g_iFontBufferLength;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0; 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)) { if (FAILED(hr)) {
print("CreateBuffer\n"); print("CreateBuffer\n");
return hr; return hr;
@ -899,6 +890,49 @@ void RenderModel(float t)
void RenderFont() 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, D3DXVECTOR2 invScreenSize = D3DXVECTOR2(2.0f / (float)g_ViewportSize.Width,
2.0f / (float)g_ViewportSize.Height); 2.0f / (float)g_ViewportSize.Height);
@ -930,7 +964,7 @@ void RenderFont()
for (UINT p = 0; p < techDesc.Passes; p++) { for (UINT p = 0; p < techDesc.Passes; p++) {
g_pTechniqueFont->GetPassByIndex(p)->Apply(0); g_pTechniqueFont->GetPassByIndex(p)->Apply(0);
g_pd3dDevice->Draw(1, 0); g_pd3dDevice->Draw(4, 0);
} }
} }