Compare commits

..

No commits in common. "8bcf7d5d7974866578a801642f44174c0356ba6a" and "16f2ee8a1e34673e4712d071058320dd9664e6a0" have entirely different histories.

2 changed files with 28 additions and 91 deletions

49
font.fx
View File

@ -13,12 +13,7 @@ SamplerState samPoint {
struct VS_INPUT struct VS_INPUT
{ {
float4 Pos : TEXCOORD; float2 Pos : POSITION;
};
struct GS_INPUT
{
float4 Pos : SV_POSITION;
}; };
struct PS_INPUT struct PS_INPUT
@ -27,44 +22,20 @@ struct PS_INPUT
float2 Tex : TEXCOORD0; float2 Tex : TEXCOORD0;
}; };
PS_INPUT VS(VS_INPUT input)
GS_INPUT VS(VS_INPUT input)
{ {
GS_INPUT output; PS_INPUT output = (PS_INPUT)0;
output.Pos = input.Pos; float2 Pos = input.Pos * vGlyphScale + vPosition;
Pos = Pos * vInvScreenSize + float2(-1, 1);
output.Pos = float4(Pos.xy, 0, 1);
float2 Tex = float2(input.Pos.x, -input.Pos.y);
output.Tex = (Tex + vCharCoord) * vTexScale;
return output; 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);
output.Tex = vertices[i] * float2(1, -1);
//output.Tex = (output.Tex + vCharCoord) * vTexScale;
output.Tex = (output.Tex + input[0].Pos.zw) * vTexScale;
TriStream.Append(output);
}
TriStream.RestartStrip();
}
float4 PS(PS_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);
@ -79,7 +50,7 @@ technique10 Font
pass P0 pass P0
{ {
SetVertexShader(CompileShader(vs_4_0, VS())); SetVertexShader(CompileShader(vs_4_0, VS()));
SetGeometryShader(CompileShader(gs_4_0, GS())); SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, PS())); SetPixelShader(CompileShader(ps_4_0, PS()));
} }
} }

View File

@ -370,8 +370,16 @@ HRESULT LoadMesh()
return S_OK; return S_OK;
} }
const int g_iFontBufferLength = 512; struct FontVertex {
typedef D3DXVECTOR4 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
};
HRESULT InitFontBuffers() HRESULT InitFontBuffers()
{ {
@ -414,7 +422,7 @@ HRESULT InitFontBuffers()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
D3D10_INPUT_ELEMENT_DESC layout[] = { D3D10_INPUT_ELEMENT_DESC layout[] = {
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0}, {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0},
}; };
UINT numElements = (sizeof (layout)) / (sizeof (layout[0])); UINT numElements = (sizeof (layout)) / (sizeof (layout[0]));
@ -435,15 +443,16 @@ HRESULT InitFontBuffers()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
D3D10_BUFFER_DESC bd; D3D10_BUFFER_DESC bd;
D3D10_SUBRESOURCE_DATA initData;
// position // position
bd.Usage = D3D10_USAGE_DYNAMIC; bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = (sizeof (FontVertex)) * g_iFontBufferLength; bd.ByteWidth = (sizeof (FontVertices));
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.CPUAccessFlags = 0;
bd.MiscFlags = 0; bd.MiscFlags = 0;
initData.pSysMem = FontVertices;
hr = g_pd3dDevice->CreateBuffer(&bd, NULL, &g_pVertexBuffersFont[0]); hr = g_pd3dDevice->CreateBuffer(&bd, &initData, &g_pVertexBuffersFont[0]);
if (FAILED(hr)) { if (FAILED(hr)) {
print("CreateBuffer\n"); print("CreateBuffer\n");
return hr; return hr;
@ -890,49 +899,6 @@ 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);
@ -957,7 +923,7 @@ void RenderFont()
UINT offset[] = { 0 }; UINT offset[] = { 0 };
g_pd3dDevice->IASetInputLayout(g_pVertexLayoutFont); g_pd3dDevice->IASetInputLayout(g_pVertexLayoutFont);
g_pd3dDevice->IASetVertexBuffers(0, g_dwVertexBufferCountFont, g_pVertexBuffersFont, stride, offset); g_pd3dDevice->IASetVertexBuffers(0, g_dwVertexBufferCountFont, g_pVertexBuffersFont, stride, offset);
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST); g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
D3D10_TECHNIQUE_DESC techDesc; D3D10_TECHNIQUE_DESC techDesc;
g_pTechniqueFont->GetDesc(&techDesc); g_pTechniqueFont->GetDesc(&techDesc);