[2.1] V2 Mesh - Shared Vertex Buffer?

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


Post Reply
User avatar
Kaylx
Greenskin
Posts: 123
Joined: Sun May 22, 2011 10:45 pm
Location: Scotland
x 7

[2.1] V2 Mesh - Shared Vertex Buffer?

Post by Kaylx »

Hey guys,

I'm trying to create a Mesh on the fly the shares 1 vertex buffer between multiple SubMeshes which use different index buffers.
I've got it working in the sense that it renders etc, but when Ogre shutsdown it throws an exception inside VaoManager::destroyVertexBuffer because the buffer has already been destroyed.
Clearly i've done something wrong but i'm not sure what.

Does anyone have a 'bare bones' example of how to do such things?
My own attempt just modified the DynamicGeometry sample to create 1 VertexBufferPacked then 6 IndexBufferPacked (1 for each face of the cube) inside createStaticMesh.
Here's an excerpt of the relevant code:

Code: Select all

        ...

        Ogre::VertexBufferPacked *vertexBuffer = 0;
        try
        {
            //Create the actual vertex buffer.
            vertexBuffer = vaoManager->createVertexBuffer( vertexElements, 8,
                                                           partialMesh ? Ogre::BT_DEFAULT :
                                                                         Ogre::BT_IMMUTABLE,
                                                           cubeVertices, true );
        }
        catch( Ogre::Exception &e )
        {
            OGRE_FREE_SIMD( vertexBuffer, Ogre::MEMCATEGORY_GEOMETRY );
            vertexBuffer = 0;
            throw e;
        }

        for( size_t i = 0; i < 6; ++i )
        {
          //Create one submesh
          Ogre::SubMesh *subMesh = mesh->createSubMesh();

          //Now the Vao. We'll just use one vertex buffer source (multi-source not working yet)
          Ogre::VertexBufferPackedVec vertexBuffers;
          vertexBuffers.push_back( vertexBuffer );

          Ogre::IndexBufferPacked *indexBuffer = createIndexBuffer(i); //Create the actual index buffer
          Ogre::VertexArrayObject *vao = vaoManager->createVertexArrayObject(
            vertexBuffers, indexBuffer, Ogre::OT_TRIANGLE_LIST );

          //Each Vao pushed to the vector refers to an LOD level.
          //Must be in sync with mesh->mLodValues & mesh->mNumLods if you use more than one level
          subMesh->mVao[Ogre::VpNormal].push_back( vao );
          //Use the same geometry for shadow casting.
          subMesh->mVao[Ogre::VpShadow].push_back( vao );

          //Assign a material to each face...
        }

        ...
Any help would be much appreciated.

Kaylx
farrer
Halfling
Posts: 64
Joined: Mon Sep 12, 2011 7:35 pm
x 13

Re: [2.1] V2 Mesh - Shared Vertex Buffer?

Post by farrer »

Take a look at the keepAsShadow parameter of CreateVertexBuffer:
If keepAsShadow is false, caller is responsible for freeing the data

If keepAsShadow is true, we're responsible for freeing the pointer. We will free the pointer using OGRE_FREE_SIMD( MEMCATEGORY_GEOMETRY ), in which case the pointer must* have been allocated using OGRE_MALLOC_SIMD( MEMCATEGORY_GEOMETRY )
User avatar
Kaylx
Greenskin
Posts: 123
Joined: Sun May 22, 2011 10:45 pm
Location: Scotland
x 7

Re: [2.1] V2 Mesh - Shared Vertex Buffer?

Post by Kaylx »

Freeing the shadow buffer isn't the problem. It's destroying the actual VertexBufferPacked* that's the problem (presumably because it isn't referenced counted).
I believe the issue is with the fact the same VertexBufferPacked pointer is used across several submeshes.
Ogre throws an exception here:

Code: Select all

        // VaoManager::destroyVertexBuffer ...
        if( itor == mBuffers[BP_TYPE_VERTEX].end() )
        {
            OGRE_EXCEPT( Exception::ERR_INVALID_STATE,
                         "Vertex Buffer has already been destroyed or "
                         "doesn't belong to this VaoManager.",
                         "VaoManager::destroyVertexBuffer" );
        }
I'm guessing V2 meshes don't support sharing...
In fact i just found this:

Code: Select all

        // Mesh::importV1 ...
        if( mesh->sharedVertexData[VpNormal] )
        {
            LogManager::getSingleton().logMessage( "WARNING: Mesh '" + mesh->getName() +
                                                   "' has shared vertices. They're being "
                                                   "'unshared' for importing to v2" );
            v1::MeshManager::unshareVertices( mesh );
        }
Back to the drawing board i guess.
tlogre
Gnoblar
Posts: 18
Joined: Tue Oct 03, 2017 7:27 pm
x 1

Re: [2.1] V2 Mesh - Shared Vertex Buffer?

Post by tlogre »

I replied to this older thread just to get a confirmation:

Does V2 support shared vertex buffers within a mesh?


I have a large set of vertices that I would like to utilize in multiple sub meshes that each have different materials.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: [2.1] V2 Mesh - Shared Vertex Buffer?

Post by dark_sylinc »

tlogre wrote:I replied to this older thread just to get a confirmation:
Does V2 support shared vertex buffers within a mesh?

I have a large set of vertices that I would like to utilize in multiple sub meshes that each have different materials.
It does but it's not encouraged. This means that if you force vertex buffer sharing, on destruction Ogre will try to delete the same buffer twice, hence you need to delete the buffer yourself and unset it from the submeshes before shutdown to prevent Ogre from crashing.

Other utility functions like the ones in VertexShadowMapHelper might not behave correctly if buffers are shared.

If you manage the buffers yourself, rendering-wise it will work.
Post Reply