Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor "SetVertexBufferArray" to "SetVertexBuffers" #13

Open
LukasBanana opened this issue Jun 21, 2018 · 1 comment
Open

Refactor "SetVertexBufferArray" to "SetVertexBuffers" #13

LukasBanana opened this issue Jun 21, 2018 · 1 comment
Assignees
Labels
feature request Requested features and TODO lists help wanted reminder

Comments

@LukasBanana
Copy link
Owner

LukasBanana commented Jun 21, 2018

The idea is to get rid of all ...Array interfaces as well as Set...Array functions in the CommandBuffer interface. Instead, the new ResourceHeap interface should be used, which complies with the new rendering APIs. The SetVertexBufferArray function should be refactored to the following:

void SetVertexBuffers(std::uint32_t numVertexBuffers, Buffer* const * vertexBuffers);

For all renderers (except OpenGL) this can be trivially implemented by using an uninitialized local C-style array that is passed to the native renderer API like this:

void D3D11CommandBuffer::SetVertexBuffers(
    std::uint32_t  numVertexBuffers,
    Buffer* const* vertexBuffers)
{
    /* Uninitialized local arrays are trivially constructed on the stack */
    ID3D11Buffer* buffers[g_maxVertexBuffers];
    UINT strides[g_maxVertexBuffers];
    UINT offsets[g_maxVertexBuffers];

    /* Convert vertex buffer array to native parameters */
    numVertexBuffers = std::min(numVertexBuffers, g_maxVertexBuffers);
    for (std::uint32_t i = 0; i < numVertexBuffers; ++i)
    {
        auto vertexBufferD3D = LLGL_CAST(D3D11VertexBuffer*, vertexBuffers[i]);
        buffers[i] = vertexBufferD3D->GetNative();
        strides[i] = vertexBufferD3D->GetStride();
        offsets[i] = 0;
    }

    /* Pass local arrays to native API */
    context_->IASetVertexBuffers(0, numVertexBuffers, buffers, strides, offsets);
}

This works analogous to the SetViewports and SetScissors functions.

For OpenGL, however, this requires a little more effort. The idea is to convert the GLVertexArrayObject member variable in GLVertexBuffer into shared pointer. Then all vertex buffers used in a call to SetVertexBuffers refer to the same VAO. When these buffers are used in the SetVertexBuffers function for the first time, this VAO is created 'on the fly'. When they are used together once more, this VAO can be reused. This allows to dynamically change the vertex buffers without letting the client programmer manage a BufferArray object.

Caveat:
To make a buffer usable for multiple sets of vertex buffers, each GLVertexBuffer must hold a container of shared pointers. Otherwise, some VAOs could be created and deleted every frame :-(
So either each GLVertexBuffer needs this list, which must be iterated for each buffer used in the SetVertexBuffers function, or some sort of hash map must be involved to quickly find the correct VAO for a set of buffers.

Alternative 1:
Get rid of all ...Array interfaces except of BufferArray (but only for vertex buffer arrays).

Alternative 2:
Both SetVertexBufferArray and SetVertexBuffers are supported. The former one is supported for optimization purposes and the latter one is supported for flexibility purposes.

Update (12/07/2018):
Maybe the GL_ARB_vertex_attrib_binding extension can help here.

@LukasBanana LukasBanana added reminder feature request Requested features and TODO lists labels Jun 21, 2018
@LukasBanana LukasBanana self-assigned this Jun 21, 2018
@LukasBanana LukasBanana changed the title Add "SetVertexBuffers" to dynamically set multiple vertex buffers Refactore "SetVertexBufferArray" to "SetVertexBuffers" Jun 21, 2018
@LukasBanana LukasBanana changed the title Refactore "SetVertexBufferArray" to "SetVertexBuffers" Refactor "SetVertexBufferArray" to "SetVertexBuffers" Jun 21, 2018
@LukasBanana
Copy link
Owner Author

LukasBanana commented Jul 2, 2018

The TextureArray and SamplerArray interfaces have been removed with 1d49e1f.
Use ResourceHeap interface instead (shown in Tutorial02: Tessellation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Requested features and TODO lists help wanted reminder
Projects
None yet
Development

No branches or pull requests

1 participant