C++ HLSL顶点着色器获取错误的输入

C++ HLSL顶点着色器获取错误的输入,c++,directx,shader,hlsl,C++,Directx,Shader,Hlsl,我目前正在处理一个项目,但我的问题是我的顶点着色器获取了错误的数据,因此我的位置值不再与我在开始时设置的位置值相同 这就是我定义精灵位置/锚的地方 struct SpriteVertex { DirectX::XMFLOAT3 position; float radius; int textureIndex; }; //Sprite renderer vector<SpriteVer

我目前正在处理一个项目,但我的问题是我的顶点着色器获取了错误的数据,因此我的位置值不再与我在开始时设置的位置值相同

这就是我定义精灵位置/锚的地方

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);
struct SpriteVertex
{
DirectX::XMFLOAT3位置;
浮动半径;
内部纹理索引;
};
//精灵渲染器
向量sprite_顶点;
精神顶点电流;
当前位置x=0;
当前位置y=0;
当前位置z=0;
电流半径=100;
current.textureIndex=0;
精灵顶点。向后推(当前);
g_SpriteRenderer->renderSprites(pd3dImmediateContext、sprite_顶点、g_摄影机);
因此,在我的spritender类中,我有一个create方法,在其中设置输入布局并创建一个空的顶点缓冲区

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;
}
HRESULT spriteender::创建(ID3D11Device*pDevice)
{
cout-GetTechniqueByName(“Render”)->GetPassByName(“SpritePass”)->GetDesc(&pd));
V_RETURN(pDevice->CreateInputLayout(布局、会议、pd.piInputSignature、pd.IAInputSignatureSize和m_pInputLayout));
返回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);  
void spriteender::renderSprites(ID3D11DeviceContext*上下文,常量std::vector和sprites,常量CFirstPersonCamera和camera)
{
//cout iSetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
上下文->iSetInputLayout(m_pInputLayout);
上下文->iSetVertexBuffers(0、0和mpvertExBuffer、大小和nullptr);
//设置着色器资源
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(“呈现”)->GetPassByName(“SpritePass”)->Apply(0,上下文);
上下文->绘图(大小,0);
}

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

我正在努力解决这个问题,如果有任何帮助,我将不胜感激

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

//--------------------------------------------------------------------------------------
//着色器资源
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//恒定缓冲区
//--------------------------------------------------------------------------------------
cbuffer CbF常数
{
矩阵g_投影;
漂浮式4 g_相机;
浮子4 g_cameraUP;
};
//--------------------------------------------------------------------------------------
//结构
//--------------------------------------------------------------------------------------
结构SpriteVertex
{  
浮动位置3:位置;
浮动半径:半径;
int-TEXIN:TEXTUREINDEX;
};
结构顶点
{
浮动4位置:SV_位置;
int-TEXIN:TEXTUREINDEX;
};
//--------------------------------------------------------------------------------------
//光栅化状态
//--------------------------------------------------------------------------------------
光栅化状态rsCullNone
{
模式=无;
};
//--------------------------------------------------------------------------------------
//深度状态
//--------------------------------------------------------------------------------------
DepthStencilState启用深度
{
DepthEnable=TRUE;
depthritemask=ALL;
深度函数=小于等于;
};
混合状态研磨
{
AlphaToCoverageEnable=错误;
BlendEnable[0]=FALSE;
};
//--------------------------------------------------------------------------------------
//着色器
//--------------------------------------------------------------------------------------
SpriteVertex DummyVS(SpriteVertex输入)
{    
返回输入;
}
[maxvertexcount(4)]
空心精灵(点精灵顶点[1],在非三角形流中){
顶点输入;
input.TEXIN=顶点[0].TEXIN;
//左下角
input.POSITION=mul(float4(顶点[0]。位置,1)-顶点[0]。半径*g_cameraRight-顶点[0]。半径*g_cameraUP,g_ViewProjection);
追加(输入);
//左上角
input.POSITION=mul(float4(顶点[0]。位置,1)-顶点[0]。半径*g_cameraRight+顶点[0]。半径*g_cameraUP,g_ViewProjection);
追加(输入);
//右上角
input.POSITION=mul(float4(顶点[0]。位置,1)+顶点[0]。半径*g_cameraRight+顶点[0]。半径*g_cameraUP,g_ViewProjection);
追加(输入);
//机器人权利
input.POSITION=mul(float4(顶点[0]。位置,1)+顶点[0]。半径*g_cameraRight-顶点[0]。半径*g_cameraUP,g_ViewProjection);
追加(输入);
}
float4 DummyPS(PSVertex输入):SV_Target0
{
返回浮动4(1,1,0,1);
}
//--------------------------------------------------------------------------------------
//技巧
//--------------------------------------------------------------------------------------
技术11渲染
{
传雪糕
{
SetVertexShader(编译头(vs_5_0,DummyVS());
SetGeometryShader(编译头(gs_5_0,SpriteGS());
SetPixelShader(CompileShader(ps_5_0,DummyPS());
SetRasterizerState(rsCullNone);
SetDepthStencilState(启用深度,0);
SetBlendState(NoBlending,float4(0.0f,0.0f,0.0f,0.0f),0xffffff);
}
}

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

context->IASetVertexBuffers(0, 0, &m_pVertexBuffer, &size, nullptr);
的第二个参数是要绑定的缓冲区数,此处必须为1,偏移量必须有效:

UINT offset = 0;
context->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &size, &offset);
作为一个将军
UINT offset = 0;
context->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &size, &offset);