glTF icon indicating copy to clipboard operation
glTF copied to clipboard

[Clarification] [bufferView -> accessor -> primitive attributes] and how to map them to vertex structure

Open ziocleto opened this issue 6 years ago • 5 comments

Hi, sorry if this has already been asked but I couldn't pinpoint it in here!

I'm writing a GLTF exporter based on tinyGLTF, I have a scene with a classic PBR vertex format. VertexFormat = { Pos,UV0,Nor,Tan,Col } VBO = [VertexFormat...] and I have a VBO (OpenGL in this case) defined as an array with those matching vertex attributed in a classic Array of Structures representation.

Now from what I've understood a bufferView and accessor will handle Structure of Arrays IE:

VBO= [ Pos = [{x,y,z}, ...], UV0 = [{x,y}, ...], ... ]

When I define the mesh primitives I can only set an index to an accessor for the vertex component, POSITION, NORMAL, etc, the accessor can only reference a contiguous chunk of memory defined in the bufferView, I can't just declare all the vertex component at once.

So in order to convert to the VBO vertex format, I will have to do heavy processing in order to reshuffle those arrays into structs. I'm pretty sure having a PBR vertex format it's quite common? How do we serialize it to GPU memory without extra work using Array of Structures?

(Looking into sparse accessors doesn't seem to be the answer)

Cheers, Dado.

ziocleto avatar Sep 01 '20 14:09 ziocleto

You can use AoS layout for vert data. Look at the bufferView.byteStride and accessor.byteOffset properties.

scurest avatar Sep 01 '20 16:09 scurest

Hi,

I can specify byteStride ok, but then accessor.byteOffset should take into account the offset within the stride, but it's not, I'm pretty confused about that.

IE: (Simple Pos + UV0 vertex structure, 20 bytes => 12 pos + 8 uv )

    "bufferViews": [
        {
            "buffer": 0,
            "byteLength": 76768,
            "byteOffset": 0,
            "byteStride": 20,
            "target": 34962
        }
    ]
"accessors": [
        {
            "bufferView": 0,
            "byteOffset": 0,
            "componentType": 5126,
            "count": 2399,
            "max": [
                0.961799,
                1.6397,
                0.539252
            ],
            "min": [
                -0.692985,
                0.0992937,
                -0.613282
            ],
            "type": "VEC3"
        },
        {
            "bufferView": 0,
            "byteOffset": 20, **<==========THIS IS WRONG==========**
            "componentType": 5126,
            "count": 2399,
            "type": "VEC2"
        }
    ]

How and where do I specify the offset within the stride?

Cheers, Dado.

ziocleto avatar Sep 01 '20 17:09 ziocleto

Use 12 for the byteOffset for the UVs.

    POS POS POS UV0 UV0 | POS POS POS UV0 UV0 | ...
off: 0   4   8   12  16 |  20  24  28  32  36 | ...
POS: X   X   X          |  X   X   X          | ...
UV0:             X   X  |              X   X  | ...

scurest avatar Sep 01 '20 17:09 scurest

Oh sorry I meant 12 of course :facepalm:

So byteOffset in accessor is either the offset within the stride (AoS) or the offset to the next array (SoA)? And I guess the way to understand if it's either one or the other is if more accessors share the same bufferView is a AoS, otherwise it's a SoA?

Thanks a lot for that clarification btw!

Cheers, Dado.

ziocleto avatar Sep 01 '20 17:09 ziocleto

Also see: https://community.khronos.org/t/clarity-of-accessor-offset-and-glvertexarrayattribformat/106240

donmccurdy avatar Sep 01 '20 17:09 donmccurdy