-1

I get this error on

result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); 

it says invalid arguments

Im using directX11 and creating a vertex buffer class

Heres the code:

bool VertexBuffer::Init(ID3D11Device * device, Shader * shader, float size, bool writable)
{

    m_shader = shader;


    unsigned long* indices;

    HRESULT result;
    D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
    D3D11_SUBRESOURCE_DATA vertexData, indexData;

    m_vertexCount = 4;
    m_indexCount = 6;

    m_vertices = new VertexType[m_vertexCount];
    indices = new unsigned long[m_indexCount];

    float halfSize = size / 2.0f;

    //Hardcoded quad from here...
    //Load vertex array:


    m_vertices[0].position = D3DXVECTOR3(-halfSize, -halfSize, 0.0f);
    m_vertices[0].uv = D3DXVECTOR2(0.0f, 1.0f);

    m_vertices[1].position = D3DXVECTOR3(-halfSize, halfSize, 0.0f);
    m_vertices[1].uv = D3DXVECTOR2(0.0f, 0.0f);

    m_vertices[2].position = D3DXVECTOR3(halfSize, halfSize, 0.0f);
    m_vertices[2].uv = D3DXVECTOR2(1.0f, 0.0f);

    m_vertices[3].position = D3DXVECTOR3(halfSize, -halfSize, 0.0f);
    m_vertices[3].uv = D3DXVECTOR2(1.0f, 1.0f);

    //Load indices:
    indices[0] = 0;
    indices[1] = 1;
    indices[2] = 2;
    indices[3] = 0;
    indices[4] = 2;
    indices[5] = 3;

    //To here.

    //Setup vertex buffer desc:
    vertexBufferDesc.Usage = (writable) ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(VertexType)*m_vertexCount;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = (writable) ? D3D11_CPU_ACCESS_WRITE : D3D11_CPU_ACCESS_READ;
    vertexBufferDesc.MiscFlags = 0;
    vertexBufferDesc.StructureByteStride = 0;

    //Setup vertex data:
    vertexData.pSysMem = m_vertices;
    vertexData.SysMemPitch = 0;
    vertexData.SysMemSlicePitch = 0;

    //Create vertex buffer:
    result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); // Here's the error im getting
    if (FAILED(result))
    {
        Error(41);
        return false;
    }

Any idea whats wrong ?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • This question doesn't contain enough information to answer the question. We would have to see what `m_vertexBuffer` is to answer this question. Questions about compilation errors are easier to answer when they contain compilable examples which produce the described behavior. Please read about [MCVE]. – François Andrieux Aug 22 '17 at 14:09
  • Turn on the DirectX debug layer. This will tell you more details about the arguments that are invalid. – Nico Schertler Aug 22 '17 at 14:25
  • m_vertexBuffer is a d3dxBuffer* and is currently NULL but is supposed to be set in create buffer – Tim Roberts Aug 23 '17 at 18:58

1 Answers1

0

I think vertexBufferDesc.CPUAccessFlags is set to invalid value when writable is false. It should be set to 0 since usage is set to D3D11_USAGE_DEFAULT. From D3D11_CPU_ACCESS_FLAG documentation:

D3D11_CPU_ACCESS_READ

The resource is to be mappable so that the CPU can read its contents. Resources created with this flag cannot be set as either inputs or outputs to the pipeline and must be created with staging usage (see D3D11_USAGE).

user7860670
  • 35,849
  • 4
  • 58
  • 84