0
votes

I've been search for a good text methodolgy recently and found one at http://www.braynzarsoft.net/Articles/index.php?p=VA&article=Easy-Font-Rendering-in-DirectX-11 , which is a good site. I can't seem to get it to run! I've resolved most of the errors but, however on debug, I get HLSL errors:

D3D11: ERROR: ID3D11DeviceContext::Draw: The Vertex Shader expects application provided input data (which is to say data other than hardware auto-generated values such as VertexID or InstanceID). Therefore an Input Assembler object is expected, but none is bound. [ EXECUTION ERROR #349: DEVICE_DRAW_INPUTLAYOUT_NOT_SET ]

Creeped? I am! Since this message is spamming like mad, the issue is most likely happening in my DrawText() function or a bad implementation in the initialize function (Below)

The InitializeGeneralResources() function:

void InfiniteText::InitializeGeneralResources(){
float textureWidth=1024.0f;
UINT numLetters=32;

D3DX11CompileFromFile(L"Font.hlsl", NULL, NULL, "FONT_VS", "vs_5_0",0,0,0,&FontvsBuffer,0,0);
D3DX11CompileFromFile(L"Font.hlsl", NULL, NULL, "FONT_PS", "ps_5_0",0,0,0,&FontpsBuffer,&ppErrorMsgs,0);
iD3D.Device->CreateVertexShader(FontvsBuffer->GetBufferPointer(),FontvsBuffer->GetBufferSize(), NULL, &Fontvs);
iD3D.Device->CreatePixelShader(FontpsBuffer->GetBufferPointer(),FontpsBuffer->GetBufferSize(), NULL, &Fontps);

ID3D11InputLayout* InLayout;
D3D11_INPUT_ELEMENT_DESC IEDesc[]={
            {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
            {"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0},
        };

UINT NumElements = ARRAYSIZE(IEDesc);
iD3D.Device->CreateInputLayout(IEDesc,NumElements,FontvsBuffer->GetBufferPointer(),FontvsBuffer->GetBufferSize(),&InLayout);
iD3D.DeviceContext->IASetInputLayout(InLayout);

D3D11_SAMPLER_DESC FontSamplerDesc;
FontSamplerDesc.MaxAnisotropy=1;
FontSamplerDesc.AddressU=D3D11_TEXTURE_ADDRESS_WRAP;
FontSamplerDesc.AddressV=D3D11_TEXTURE_ADDRESS_WRAP;
FontSamplerDesc.AddressW=D3D11_TEXTURE_ADDRESS_WRAP;
FontSamplerDesc.Filter=D3D11_FILTER_MIN_MAG_MIP_LINEAR;
FontSamplerDesc.MipLODBias=0.0f;

D3DX11CreateShaderResourceViewFromFile(iD3D.Device, L"Font.dds", NULL, NULL, &FontSRV, NULL);
iD3D.Device->CreateSamplerState(&FontSamplerDesc, &FontSRVSampler);

D3D11_BUFFER_DESC Vbufferdescription;
ZeroMemory(&Vbufferdescription, sizeof(Vbufferdescription));

Vbufferdescription.BindFlags=D3D11_BIND_VERTEX_BUFFER;
Vbufferdescription.Usage=D3D11_USAGE_DYNAMIC;
Vbufferdescription.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE;
Vbufferdescription.ByteWidth=sizeof(VertexText)*6*numLetters;
Vbufferdescription.MiscFlags=0;

iD3D.Device->CreateBuffer(&Vbufferdescription, NULL, &FontVertexBuffer);
}

I have a rather simple HLSL file:

cbuffer ConstantBuffer:register( b0 )
{
float4x4 WVP;

}

struct VOut
{
float4 position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
};

VOut FONT_VS(float4 position : POSITION, float2 TexCoord : TEXCOORD)
{
    VOut output;

output.position = mul(position, WVP);
output.TexCoord = TexCoord;

return output;
}


float2 FONT_PS(float4 position : SV_POSITION, float2 TexCoord : TEXCOORD0) : SV_TARGET
{
return TexCoord;
}

(The DrawString() function)

bool InfiniteText::DrawString(char* Text, float xPos, float yPos){
int letterSize = sizeof(VertexText)*6;
int textSize = strlen(Text);

if(textSize > numLetters)
    textSize=numLetters;

float cScreenWidth = 32.0f/iD3D.cWidth;
float cScreenHeight= 32.0f/iD3D.cHeight;

float TexelWidth= 32.0f/textureWidth;

D3D11_MAPPED_SUBRESOURCE MappedSub;
iD3D.DeviceContext->Map(FontVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedSub);
VertexText* Sprite = (VertexText*)MappedSub.pData;

const int indexA = static_cast<int>('A');
const int indexZ = static_cast<int>('Z');

for(int i=0; i<textSize;i++){
    float thisStartX=xPos+(cScreenWidth*static_cast<char>(i));
    float thisEndX=thisStartX + cScreenWidth;
    float thisStartY=yPos;
    float thisEndY=thisStartY + cScreenHeight;

    Sprite[0].Translation=XMFLOAT3(thisEndX,thisEndY,1.0f);
    Sprite[1].Translation=XMFLOAT3(thisEndX,yPos,1.0f);
    Sprite[2].Translation=XMFLOAT3(thisStartX,yPos,1.0f);
    Sprite[3].Translation=XMFLOAT3(thisStartX,yPos,1.0f);
    Sprite[4].Translation=XMFLOAT3(thisStartX,thisEndY,1.0f);
    Sprite[5].Translation=XMFLOAT3(thisEndX,thisEndY,1.0f);

    UINT TexLookup=0;
    UINT Letter= static_cast<int>(Text[i]);

    if (Letter < indexA || Letter > indexZ){
        TexLookup=(indexA - indexZ) +1;
    }
    else{
        TexLookup=(Letter-indexA);
    }

    float texStart = 0.0f + (TexelWidth*static_cast<float>(TexLookup));
    float TexEnd = texStart + TexelWidth;

    Sprite[0].TextureCoord = XMFLOAT2(TexEnd,0.0f);
    Sprite[1].TextureCoord = XMFLOAT2(TexEnd,1.0f);
    Sprite[2].TextureCoord = XMFLOAT2(texStart,1.0f);
    Sprite[3].TextureCoord = XMFLOAT2(texStart,1.0f);
    Sprite[4].TextureCoord = XMFLOAT2(texStart,0.0f);
    Sprite[5].TextureCoord = XMFLOAT2(TexEnd,0.0f);

    Sprite= Sprite + 6;
}
    iD3D.DeviceContext->Unmap(FontVertexBuffer,0); //MAP END

    UINT stride=sizeof(VertexText);
    UINT offset=0;
    iD3D.DeviceContext->VSSetShader(iText.Fontvs,0,0);
    iD3D.DeviceContext->PSSetShader(iText.Fontps,0,0);

    //Projection=XMMatrixPerspectiveFovLH(XM_PIDIV2, 1.0, 0.0f, 1000.0f);
    Projection=iD3D.mProjection;
    iD3D.WorldCB.mWorldVP=XMMatrixTranspose(Projection);

    iD3D.DeviceContext->UpdateSubresource(iD3D.MatrixBuffer, 0, NULL, &iD3D.WorldCB, 0, 0);
    iD3D.DeviceContext->VSSetConstantBuffers(0,1,&iD3D.MatrixBuffer);

    iD3D.DeviceContext->IASetVertexBuffers(0,1, &iText.FontVertexBuffer, &stride, &offset);
    iD3D.DeviceContext->PSSetShaderResources(0,1, &iText.FontSRV);
    iD3D.DeviceContext->PSSetSamplers(0,1,&iText.FontSRVSampler);
    iD3D.DeviceContext->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
    iD3D.DeviceContext->Draw(6*textSize,0);

    return true;


}

If you made it this far, thank you. I think that it might be that my .hlsl file might not be configured properly to receive textures, and I might need to know how textures interface properly with the shader to produce the output. Thanks!

1

1 Answers

0
votes

The HLSL was not written correctly to include Texture2D x: register( t0 ) or SamplerState x : register( s0 ), taking in necessary resources for the texture. Another problem has surfaced, but this question has been solved.