-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add an example to use ImGui with SDL / SDL_Renderer #3926
Conversation
Is ImGui::EndFrame(); missing? |
Date are patched on merge so no worries about that part.
|
Congrats on getting this change merged into SDL! There have been a few small refactors here and there, so whenever you feel you are ready and assuming you still want this merged,
We'll apply a polish pass for coding style over that. |
Hey @ocornut |
… they want, and Dear ImGui can be drawn on top of their game. Don't clear the SDL_Renderer or call SDL_RenderPresent() on it.
…s set via SDL by the user. Also allows us to honor ImDrawCallback_ResetRenderState draw command.
…ill work with user-supplied SDL_Texture's.
Integrate more nicely with SDL applications, support more Dear ImGui features
Thanks for recent updates.
|
@ocornut thanks ! here's a few modifications |
Working on polishing and merging this now. |
@ocornut what is this ImGuiBackendFlags_RendererHasVtxOffset / what benefit ? what API should look like to handle that ? |
It allow single window to use large meshes (more than 64k vertices) while keeping 16-bit indices (32-bit indices are opt-in compile time. See how other backends use the Details at #2591 E.g. DX11 upload a single vtx and idx buffer and then draw with: ctx->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset); DX9: bd->pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, pcmd->VtxOffset + global_vtx_offset, 0, (UINT)cmd_list->VtxBuffer.Size, pcmd->IdxOffset + global_idx_offset, pcmd->ElemCount / 3); OpenGL does: #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if (bd->GlVersion >= 320)
glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset);
else
#endif
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); You can use dear imgui without but when doing large plots or very dense windows 16-bit indices without support for that VtxOffset can be limiting. |
@ocornut ok thanks ! It seems to me the SDL_RenderGeometryRaw API definition is compatible with VtxOffset, This can be done but using the last parameter 'size_indices' of SDL_RenderGeometryRaw. Though SDL backend aren't optimized for this, but this may changes without changing this API |
} | ||
idx_buffer += pcmd->ElemCount; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should render clipping be disabled at the end of the ImGui_ImplSDLRenderer_RenderDrawData()?
i.e., SDL_RenderSetClipRect(bd->SDLRenderer, NULL);
Any additional render calls with that SDL_Renderer made by the user will only be able to be seen when the imgui windows are over the area in which the user has drawn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is SDL_RenderSetClipRect(bd->SDLRenderer, NULL);
moved in ImGui_ImplSDLRenderer_SetupRenderState();
I'd say, imgui should get all original user settings, save them, set its settings, and set user settings back ...
But I'm not sure how it's meant to be intergrated with user non-imgui code.
Now pushed a fix to backup/restore the user's viewport and cliprect e443ea1 |
@1bsyl Sorry for late answer, I forgot this message.
Those are two different things. SDL_Renderer supports 16 and 32 bits indices just fine, but not an offset into the vertex buffer. As a result using 16-bit indices it is impossible to have a large vertex buffer as the indices can never reach the whole buffer. VtxOffset is a different parameter which is currently missing from the SDL_RenderGeometryRaw() API. Now that SDL 2.0.18 is out I am afraid it would requires a new function name to add those. |
@ocornut That would be using the API with taking into account the offset like this :
https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_sdlrenderer.cpp#L173 |
Oh my bad I think you are right. I was assuming we uploaded the whole buffer ahead of time. But this API it suggest that every draw call will reupload the entire ImDrawList vtx buffer to GPU (or less when reaching VtxOffset > 0), which is going to be pretty wasteful (at least halving the upload performance since most windows have 2 draw calls). |
ok! Yes, you're correct: vertices butter is re-uploaded every time because SDL_Renderer queue internally all commands. But this could be improved at some point in the future / or auto-detected / (adding eventually a SDL hint). |
…) with 16-bit indices, enable 'ImGuiBackendFlags_RendererHasVtxOffset' in this backend. (#3926)
Added support for VtxOffset with 5c388c3 |
If this SDL2 pull request is merged
libsdl-org/SDL#4195
Then it will be possible to render triangles with SDL_Renderer.
This current PR is simply an example using ImGui + SDL/SDL_renderer
(could be opengl, gles2, d3d11, metal or software rendering back-end underneath)
Only tested on linux with Makefile.
Though I've updated all build files taken from the initial "example_sdl_opengl2" example.
NB:
I've never use ImGui before and this was just done in a couple of hours.
It was just done as a proof of concept and it may need to be implemented in a better way.