Immediate-Mode-UI / Nuklear

A single-header ANSI C immediate mode cross-platform GUI library
https://immediate-mode-ui.github.io/Nuklear/doc/index.html
8.86k stars 535 forks source link

How to do multiple render with one update #625

Closed lautriva closed 2 months ago

lautriva commented 3 months ago

Hi, I wonder if it is possible to render multiple times My usecase : I am integrating Nuklear into a VR application, and I need to render the GUI on both eyes But when I call the render multiple times it seems the UI is frozen and not responding to events

Everything works in desktop when using a single render() call, so I think maybe its related to some operations that should be done once

Here is in pseudo-code how its done:

void update_gui_events()
{
    nk_input_begin(context);

    // ...
    // Calls to nk_input_*** to send inputs to Nuklear based on the current state
    // I'm using NK_KEYSTATE_BASED_INPUT
    // ...

    nk_input_end(context);
}

void generate_gui()
{
    if (nk_begin_titled(...)
    {
        // ... Window content
    }
    nk_end(context);
}

void render_gui()
{
    // ...
    // Engine specific calls to init the gui render
    // ...

    nk_buffer_init_fixed(&vertices, ...);
    nk_buffer_init_fixed(&elements, ...);
    nk_convert(ctx, command_buffer, &vertices, &elements, ...);

    nk_draw_foreach(cmd, ctx, command_buffer)
    {
        // ...
        // Engine specific calls to draw the vertices
        // ...
    }

    nk_clear(ctx);
    nk_buffer_free(&vertices);
    nk_buffer_free(&elements);

    // ...
    // Engine specific calls to end the gui render
    // ...
}

Then on each frame :

update_gui_events();
generate_gui();

for (eye : all_eyes)
{
    // ...
    // Engine specific calls to prepare rendering + scene render
    // ...

    render_gui();
}
RobLoach commented 2 months ago

Depends on if your renderer allows splitting the update state of the GUI and the drawing. The only thing in the update state that really changes is the input. Check through your render code and make sure it's not changing anything for the input, and not calling nk_clear().

lautriva commented 2 months ago

Yes, I already split the the gui update and drawing

I don't have access to the VR hardware at the moment but I'll take a look to update my renderer to not use nk_clear() at the end of the draw (named render_gui in my example) but call it at the end of the frame

Thanks