Search code examples
c++directxshaderhlsl

HLSL Vertex Shader gets wrong input


I am currently working on an project but my problem is that my Vertex shader gets the wrong data, so my position values aren't any longer the same as i set them at the beginning.

So this is where I define the position/anchor of my Sprite

struct SpriteVertex
{
    DirectX::XMFLOAT3 position;     
    float radius;                  
    int textureIndex;         
};

    //Sprite renderer 
    vector<SpriteVertex> sprite_vertices;
    SpriteVertex current;
    current.position.x = 0;
    current.position.y = 0;
    current.position.z = 0;
    current.radius = 100;
    current.textureIndex = 0;
    sprite_vertices.push_back(current);



    g_SpriteRenderer->renderSprites(pd3dImmediateContext, sprite_vertices, g_camera);

So in my SpriteRenderer Class I have the create method where I set up the Input layout and create an empty vertex buffer.

HRESULT SpriteRenderer::create(ID3D11Device* pDevice)
{
    cout << "Spriterender Create has been called" << endl;

    HRESULT hr;


    D3D11_BUFFER_DESC bd;
    ZeroMemory(&bd, sizeof(bd));
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = 1024 * sizeof(SpriteVertex);
    bd.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;

    V(pDevice->CreateBuffer(&bd , nullptr, &m_pVertexBuffer));

    const D3D11_INPUT_ELEMENT_DESC layout[] ={
    { "POSITION",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "RADIUS",    0, DXGI_FORMAT_R32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXTUREINDEX",0,DXGI_FORMAT_R32_SINT,0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    UINT numEements = sizeof(layout) / sizeof(layout[0]);

    D3DX11_PASS_DESC pd;
    V_RETURN(m_pEffect->GetTechniqueByName("Render")->GetPassByName("SpritePass")->GetDesc(&pd));

    V_RETURN(pDevice->CreateInputLayout(layout, numEements, pd.pIAInputSignature, pd.IAInputSignatureSize, &m_pInputLayout));


    return S_OK;
}

And I have the render method which fills the buffer and is supposed to render it with the shader I coded:

void SpriteRenderer::renderSprites(ID3D11DeviceContext* context, const std::vector<SpriteVertex>& sprites, const CFirstPersonCamera& camera)
{
//cout << "SpriterenderrenderSprites has been called" << endl;

D3D11_BOX box;
box.left = 0; box.right = sprites.size()*sizeof(SpriteVertex);
box.top = 0; box.bottom = 1;
box.front = 0; box.back = 1;

context->UpdateSubresource(m_pVertexBuffer,0,&box,&sprites[0],0,0);

const UINT size = sizeof(SpriteVertex);

context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
context->IASetInputLayout(m_pInputLayout);
context->IASetVertexBuffers(0, 0, &m_pVertexBuffer, &size, nullptr);

//setting shader resouirces
DirectX::XMMATRIX worldviewProj =camera.GetViewMatrix()*camera.GetProjMatrix();
m_pEffect->GetVariableByName("g_ViewProjection")->AsMatrix()->SetMatrix(( float* ) &worldviewProj);
m_pEffect->GetVariableByName("g_cameraRight")->AsVector()->SetFloatVector((float*) &camera.GetWorldRight());
m_pEffect->GetVariableByName("g_cameraUP")->AsVector()->SetFloatVector((float*)&camera.GetWorldUp());

m_pEffect->GetTechniqueByName("Render")->GetPassByName("SpritePass")->Apply( 0,context);
context->Draw(size,0);  

}

So my big problem is that when I debug the Shaders that my inital Position radius and so on aren't even close to what I want:

Debug VS

I'm trying to fix this now for ever any help would be really appreciated.

EDIT: HLSL Code might help ;=)

    //--------------------------------------------------------------------------------------
// Shader resources
//--------------------------------------------------------------------------------------



//--------------------------------------------------------------------------------------
// Constant buffers
//--------------------------------------------------------------------------------------
cbuffer cbCOnstant
{
    matrix g_ViewProjection;
    float4 g_cameraRight;
    float4 g_cameraUP;
};


//--------------------------------------------------------------------------------------
// Structs
//--------------------------------------------------------------------------------------
struct SpriteVertex
{  
    float3 POSITION : POSITION;
    float RADIUS: RADIUS;
    int TEXIN : TEXTUREINDEX;
};

struct PSVertex
{
    float4 POSITION : SV_Position;
    int TEXIN : TEXTUREINDEX;
};

//--------------------------------------------------------------------------------------
// Rasterizer states
//--------------------------------------------------------------------------------------
RasterizerState rsCullNone 
{
    CullMode = None;
};

//--------------------------------------------------------------------------------------
// DepthStates
//--------------------------------------------------------------------------------------
DepthStencilState EnableDepth
{
    DepthEnable = TRUE;
    DepthWriteMask = ALL;
    DepthFunc = LESS_EQUAL;
};
BlendState NoBlending
{
    AlphaToCoverageEnable = FALSE;
    BlendEnable[0] = FALSE;
};


//--------------------------------------------------------------------------------------
// Shaders
//--------------------------------------------------------------------------------------
SpriteVertex DummyVS(SpriteVertex Input)
{    
    return Input;
}

[maxvertexcount(4)]
void SpriteGS(point SpriteVertex vertex[1], inout TriangleStream<PSVertex> stream){

    PSVertex input;
    input.TEXIN = vertex[0].TEXIN; 


    //bottom left   
    input.POSITION = mul(float4(vertex[0].POSITION,1) - vertex[0].RADIUS * g_cameraRight - vertex[0].RADIUS * g_cameraUP, g_ViewProjection);
    stream.Append(input);

    //top left

    input.POSITION = mul(float4(vertex[0].POSITION,1) - vertex[0].RADIUS * g_cameraRight + vertex[0].RADIUS * g_cameraUP, g_ViewProjection);
    stream.Append(input);

    //top right
   input.POSITION = mul(float4(vertex[0].POSITION,1) + vertex[0].RADIUS * g_cameraRight + vertex[0].RADIUS * g_cameraUP, g_ViewProjection);
    stream.Append(input);  

         //bot right

    input.POSITION = mul(float4(vertex[0].POSITION,1) + vertex[0].RADIUS * g_cameraRight - vertex[0].RADIUS * g_cameraUP, g_ViewProjection);
    stream.Append(input);

}



float4 DummyPS(PSVertex input) : SV_Target0
{
 return float4(1, 1, 0, 1);
}

//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique11 Render
{
    pass SpritePass
    {
        SetVertexShader(CompileShader(vs_5_0, DummyVS()));
        SetGeometryShader(CompileShader(gs_5_0, SpriteGS()));
        SetPixelShader(CompileShader(ps_5_0, DummyPS()));

        SetRasterizerState(rsCullNone);
        SetDepthStencilState(EnableDepth,0);
        SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
    }
}

Solution

  • Your binding of the vertex buffer below is wrong :

    context->IASetVertexBuffers(0, 0, &m_pVertexBuffer, &size, nullptr);
    

    The seconde arguments of ID3D11DeviceContext::IASetVertexBuffers is the number of buffer to bind, it has to be one here, and offsets has to be valid :

    UINT offset = 0;
    context->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &size, &offset);
    

    As a general advice, you should turn on the debug device at creation with the flag D3D11_CREATE_DEVICE_DEBUG and look for any message in the output. On windows 10, you may have to install it first following Microsoft instructions installing the debug device