0

I'm trying to build the command array but I keep on getting "broken" mesh draws. here is the struct I try to populate:

My Vertex/indices are stored in buffers as :

 QVector<QVector3D> mVertex; // all meshes in 1 vector
 QVector<unsigned int> mIndices; // all meshes in 1 vector
 int mIndicesCount = mIndices.size(); // this is per mesh accessible
 int mVertexCount = mVertex.size(); // this is per mesh accessible

The loop :

int skip =0;
int offset =0;
for (size_t u = 0; u < jobSize; ++u) {
    DrawElementsIndirectCommand *cmd = &dstCmds[u];
    cmd->count = mNodeList[u]->mIndicesCount;
    cmd->instanceCount = 1;
    cmd->firstIndex = skip;
    cmd->baseVertex = offset;
    cmd->baseInstance = 1;
    skip += (mNodeList[u]->mIndicesCount * sizeof(unsigned int));
    offset += (mNodeList[u]->mVertexCount / sizeof(unsigned int));
}

Does any1 see any errors here? I'm lost.

Also tried this :

    skip += (mNodeList[u]->mIndicesCount / sizeof(unsigned int));
    offset += (mNodeList[u]->mVertexCount);

based on > OpenGL glMultiDrawElementsIndirect with Interleaved Buffers

EDIT 2 I could not get it to work with the suggesions in comments, or I did somethingw rong... here is the main code responsible for building the buffers & commands. PS. this exercise is about trying to follow AZDO - https://github.com/nvMcJohn/apitest/blob/master/src/solutions/untexturedobjects/gl/bufferstorage.cpp

int jobSize = mNodeList.size();

QVector<QVector3D> mVertex;
QVector<QVector3D> mNormals;
QVector<unsigned int> mIndices;
for (auto &node:mNodeList) {
    mVertex.append(node->mVertex);
    mNormals.append(node->mVertexNormal);
    mIndices.append(node->mIndices);
}
glBindVertexArray(m_varray);

glBindBuffer(GL_ARRAY_BUFFER, m_vb);
glBufferData(GL_ARRAY_BUFFER, mVertex.size() * sizeof(QVector3D), &mVertex[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ib);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndices.size() * sizeof(unsigned int), &mIndices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
mShader->enableAttributeArray("In_v3Pos");
mShader->setAttributeBuffer("In_v3Pos", GL_FLOAT, 0, 3, sizeof(QVector3D));

glBindBuffer(GL_ARRAY_BUFFER, m_vn);
glBufferData(GL_ARRAY_BUFFER, mNormals.size() * sizeof(QVector3D), &mNormals[0], GL_STATIC_DRAW);
mShader->enableAttributeArray("In_v3Color");
mShader->setAttributeBuffer("In_v3Color", GL_FLOAT, 0, 3, sizeof(QVector3D));


const GLbitfield mapFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT;
const GLbitfield createFlags = mapFlags | GL_DYNAMIC_STORAGE_BIT;

mCommands.Destroy();
mCommands.Create(BufferStorage::PersistentlyMappedBuffer, GL_DRAW_INDIRECT_BUFFER, 3 * jobSize, createFlags, mapFlags);
mTransformBuffer.Destroy();
mTransformBuffer.Create(BufferStorage::PersistentlyMappedBuffer, GL_SHADER_STORAGE_BUFFER, 3 * jobSize, createFlags, mapFlags);

glBindVertexArray(0);

DrawElementsIndirectCommand *dstCmds = mCommands.Reserve(jobSize);

int skip = 0;
int offset = 0;
for (size_t u = 0; u < jobSize; ++u) {
    DrawElementsIndirectCommand *cmd = &dstCmds[u];
    cmd->count = mNodeList[u]->mIndicesCount;
    cmd->instanceCount = 1;
    cmd->firstIndex = skip*sizeof(unsigned int);
    cmd->baseVertex = offset;
    cmd->baseInstance = 0;
    skip += mNodeList[u]->mIndicesCount ;
    offset += mNodeList[u]->mVertexCount;
}
Dariusz
  • 960
  • 13
  • 36
  • Is there a reason why your `baseInstance` is 1 instead of 0? – Nicol Bolas Jan 29 '19 at 04:39
  • Not realny. I was trying lots of different "combinations" to try to get it to work but sadly nothing renders properly. I either get broken meshes or only one render right. By the way thanks to whoever down voted it. Ur real help! :) – Dariusz Jan 29 '19 at 11:10
  • Balanced out the downvote =) The main error I see here is that `skip` gets incremented by `mNodeList[u]->mIndicesCount * sizeof(unsigned int)`, which in turn makes it `sizeof(unsigned int)` times greater than it needs to be. Same with `offset`. – hidefromkgb Jan 29 '19 at 11:34
  • Hmmm should it not get incremented? Like add previous mesh count so that it knows how much he should offset in to index buffet? Thanks for up !! ^^ hmm I though its like gldrawVertexArray( or something like that can't remember name now) render when we add previous mehs offsets on each loop draw so gl knows how much we need to offset by.... oh wait does gl do it by itself ?! Wow can't wait to get home and try it ! – Dariusz Jan 29 '19 at 13:29
  • Nope I could not get it to work sadly. I have updated my code with extra info and the creation of buffer workflow. Any help would ben great. I'm totally lost at it. – Dariusz Jan 29 '19 at 16:41
  • @Dariusz Do you have the full code posted anywhere, e.g. Github or Pastebin? – hidefromkgb Jan 29 '19 at 18:59
  • @hidefromkgb sorry for super long reply, I'll mock up some test and post in on git, just need some time to sort out other things, thanks! – Dariusz Feb 05 '19 at 16:35
  • @Dariusz OK, will be waiting. – hidefromkgb Feb 05 '19 at 17:05

0 Answers0