HLSL Vertex Shader输入错误

时间:2016-06-30 17:25:55

标签: c++ directx shader hlsl

我目前正在开展一个项目,但我的问题是我的Vertex着色器得到了错误的数据,因此我的位置值不再与我在开始时设置它们相同。

所以我在这里定义了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);

所以在我的SpriteRenderer类中,我有create方法,我设置了Input布局并创建了一个空的顶点缓冲区。

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;
}

我有渲染方法填充缓冲区,并且应该使用我编码的着色器渲染它:

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);  

}

所以我的大问题是,当我调试着色器时,我的初始位置半径等等甚至都不接近我想要的:

Debug VS

我现在试图解决这个问题,任何帮助都会非常感激。

编辑:HLSL代码可能有帮助; =)

    //--------------------------------------------------------------------------------------
// 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);
    }
}

1 个答案:

答案 0 :(得分:1)

下面对顶点缓冲区的绑定是错误的:

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

ID3D11DeviceContext::IASetVertexBuffers的seconde参数是要绑定的缓冲区的数量,它必须是一个,并且偏移量必须是有效的:

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

作为一般建议,您应该在创建时使用标志D3D11_CREATE_DEVICE_DEBUG打开调试设备,并在输出中查找任何消息。在Windows 10上,您可能必须先按照Microsoft说明installing the debug device

安装它