LukasBanana / LLGL

Low Level Graphics Library (LLGL) is a thin abstraction layer for the modern graphics APIs OpenGL, Direct3D, Vulkan, and Metal
BSD 3-Clause "New" or "Revised" License
2.05k stars 139 forks source link

Query result buffer support #22

Open LukasBanana opened 6 years ago

LukasBanana commented 6 years ago

D3D12 always requires an explicit buffer resource for the results of a query heap. OpenGL added a buffer type GL_QUERY_BUFFER so the query results can be used in a shader. This functionality should be added to LLGL.

Proposal:

enum class BufferType {
    /* ... */
    QueryResult,
};

struct BufferFlags {
    enum {
        /* ... */
        // Force creation of query buffer also for GL backend.
        // Otherwise, the buffer is only created for D3D12.
        QueryShaderReadAccess = (1 << 3),
    };
};

class CommandBuffer {
    /* ... */
    void ResolveQuery(
        Buffer&         dstBuffer,
        std::uint64_t   dstOffset,
        QueryHeap&      srcQueryHeap,
        std::uint32_t   firstQuery,
        std::uint32_t   numQueries
    );
};

Example:

myCmdBuffer->Begin();
myCmdBuffer->BeginRenderPass();
myCmdBuffer->BeginQuery(*myQuery, 0);
/* ... */
myCmdBuffer->EndQuery(*myQuery, 0);
myCmdBuffer->EndRenderPass();
myCmdBuffer->ResolveQuery(*myResultBuffer, 0, *myQuery, 0, 1);
myCmdBuffer->End();

myCmdQueue->Submit(*myCmdBuffer);

std::uint64_t result = 0;
if (myCmdQueue->QueryResult(*myQuery, 0, 1, &result, sizeof(result))) {
    /* ... */
}