2015-03-25 130 views
2

我不明白這個命令結構如何工作。所有這些似乎都是有意義的(在文檔中,我還沒有實際調用函數),除了firstIndex。實際上,它看起來好像文檔中存在拼寫錯誤。在給glMultiDrawElementsIndirect的命令結構中,firstIndex參數意味着什麼?

這裏我似乎在每一個地方,看到我查找相關文件全文:

通過間接尋址的參數被包裝成一個結構,它採用的形式(C):

typedef struct { 
    uint count; 
    uint instanceCount; 
    uint firstIndex; 
    uint baseVertex; 
    uint baseInstance; 
} DrawElementsIndirectCommand; 

到glMultiDrawElementsIndirect單個呼叫是等價的,假設沒有錯誤產生:

GLsizei n; 
for (n = 0; n < drawcount; n++) { 
    const DrawElementsIndirectCommand *cmd; 
    if (stride != 0) { 
     cmd = (const DrawElementsIndirectCommand *)((uintptr)indirect + n * stride); 
    } else { 
     cmd = (const DrawElementsIndirectCommand *)indirect + n; 
    } 

    glDrawElementsInstancedBaseVertexBaseInstance(mode, 
                cmd->count, 
                type, 
                cmd->firstIndex + size-of-type, 
                cmd->instanceCount, 
                cmd->baseVertex, 
                cmd->baseInstance); 
} 

但是這些頁面並沒有說「size-of-type」是什麼意思,或者它爲什麼被添加到firstIndex中,而不是被乘以它。看起來,glDrawElementsInstancedBaseVertexBaseInstance在那裏需要一個字節偏移量,所以我認爲firstIndex是指向GL_ELEMENT_ARRAY_BUFFER頂點索引數組的索引 - 因此,索引索引 - 和類型大小一個頂點索引的大小(以字節爲單位),你需要在這裏將索引轉換爲索引數組,將其轉換爲該數組的字節偏移量。

但是......該轉換將表示爲乘法,他們說+不*。 :(

對嗎?是firstIndex是頂點索引數組中的一個入口的索引,而size-of-type是頂點索引的大小(以字節爲單位),而+ a是否爲錯誤?如果不是,我是什麼失蹤?

回答

4

這僅僅是在man page一個錯字。雖然手冊頁都在官方網站上,他們不是正式文件,他們相當經常包含錯誤或遺漏。

的官方文檔是spec文件,它確實有一個乘法而不是加法。從OpenGL 4.5規範的第353/354頁:

命令

void DrawElementsIndirect(enum mode, enum type, const void *indirect); 

相當於

typedef struct { 
     uint count; 
     uint instanceCount; 
     uint firstIndex; 
     int baseVertex; 
     uint baseInstance; 
    } DrawElementsIndirectCommand; 

    if (no element array buffer is bound) { 
     generate appropriate error 
    } else { 
     DrawElementsIndirectCommand *cmd = 
      (DrawElementsIndirectCommand *)indirect; 
     DrawElementsInstancedBaseVertexBaseInstance(mode, 
      cmd->count, type, 
      cmd->firstIndex * size-of-type, 
      cmd->instanceCount, cmd->baseVertex, 
      cmd->baseInstance); 
    } 

所以firstIndex幾乎是你已經猜到了。它是索引緩衝區(也稱爲元素數組緩衝區)的偏移量,非常類似glDrawElements()的最後一個元素。在這種情況下,唯一輕微的摺痕是偏移量以索引爲單位進行度量,而glDrawElements()則是以字節爲單位度量的。這就是size-of-type的乘法運算的地方。

例如,假設您有一個元素數組緩衝區,其中包含GL_UNSIGNED_SHORT類型的索引。你希望你的繪圖命令從第50個索引開始使用這個緩衝區的索引。對於glDrawElements(),您最後一個參數將傳入100,因爲偏移量是以字節爲單位的,並且每個索引都是兩個字節。對於glDrawElementsIndirect()中的firstIndex值,您將使用50,因爲它是在索引中測量的。在規範中乘以size-of-type(在這種情況下爲2)說明了這種差異,如果將firstIndex設置爲50,則字節偏移將爲100,因此與用於glDrawElements()的偏移相匹配。

+0

我正在查看規範,沒有看到firstIndex和size-of-type實際上是什麼意思,但是......我想我會打電話看看會發生什麼。 – mjwach 2015-03-25 18:11:41

+0

@mjwach:如果你有'GL_UNSIGNED_INT'索引,** 2 **如果你有'GL_UNSIGNED_SHORT'索引和** 1 **如果你有'GL_UNSIGNEDB_BYTE'索引提示:不這樣做,硬件不喜歡8位頂點索引)。 – 2015-03-26 01:25:22

+0

@mjwach我補充說明了什麼是價值。這聽起來像你已經找到了那部分。 – 2015-03-26 04:50:22