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

SDL_GPU Backend #8163

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

SDL_GPU Backend #8163

wants to merge 13 commits into from

Conversation

DeltaW0x
Copy link

@DeltaW0x DeltaW0x commented Nov 19, 2024

New PR because I irreparably messed up the commit history on the old one.
Now that SDL3 has reached ABI stability, I've added a SDL_Gpu backend to ImGui. The only notable difference from other backends lies in the fact that SDL_Gpu doesn't allow copy passes to occur during a renderpass.
There are two solutions to this problem (that I thought of):

  • Take control of the entire RenderPass for ImGui, which could (and will) limit what the user can do with the rendered texture (like rendering to a different RenderTarget).
  • Require the user to call a new function Imgui_ImplSDLGPU_PrepareDrawData before starting a user-created RenderPass, which uploads all the vertex/index buffer data.

I chose the second option because I thought it was the more useful one, but the code can be rewritten easily to support the former if deemed necessary. I've included shaders for SPIRV, DXCB and METALLIB for MacOS, I'll be able to compile the remaining ones for iOS and tvOS as soon as I'm able to install the SDKs for those.

I also wrote an example for the Visual Studio solution and added a Make file for other platforms

Added source sharder files for the SDL_GPU Backend and instructions on how to compile them
Update example_sdl3_sdlgpu.vcxproj
Changed sdl3-config --libs to pkg-config --libs sdl3 when linking libraries
@DeltaW0x
Copy link
Author

DeltaW0x commented Nov 19, 2024

I think I've added everything, now the Makefile works on both Linux and MacOS. Let me know if there's anything else to fix or add or test

Forward-declare enums ad structs instead of including SDL_gpu.h in header file
@povik
Copy link

povik commented Nov 20, 2024

I might be doing something wrong but when integrating this I had to change the target_info on the pipeline to match my other pipelines in order to have depth and stencil test configuration from the imgui pipeline properly applied. This is on Metal.

Without the patch below the imgui content rendered behind, not in front of the scene:

diff --git a/backends/imgui_impl_sdlgpu3.cpp b/backends/imgui_impl_sdlgpu3.cpp
index cfcf12d4..e2a88b4d 100644
--- a/backends/imgui_impl_sdlgpu3.cpp
+++ b/backends/imgui_impl_sdlgpu3.cpp
@@ -505,7 +505,8 @@ static void ImGui_ImplSDLGPU_CreateGraphicsPipeline()
     SDL_GPUGraphicsPipelineTargetInfo target_info = {};
     target_info.num_color_targets = 1;
     target_info.color_target_descriptions = color_target_desc;
-    target_info.has_depth_stencil_target = false;
+    target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
+    target_info.has_depth_stencil_target = true;
     
     SDL_GPUGraphicsPipelineCreateInfo pipeline_info = {};
     pipeline_info.vertex_shader = bd->VertexShader;

@DeltaW0x
Copy link
Author

DeltaW0x commented Nov 20, 2024

If you want to apply it on top of other rendered geometry in an existing renderpass then yes, you would need to use a depth buffer, but I believe that usually you would render ImGui as the last renderpass of your application, so it gets drawn on top of everything else without problems or a zbuffer. I can add a custom pipeline input though, so you can pass what you need without problems

@povik
Copy link

povik commented Nov 20, 2024

Sounds like I need to separate out a render pass for imgui, in which case the imgui backend will work as-is. Thanks! (My knowledge of modern rendering APIs is limited.)

@DeltaW0x
Copy link
Author

DeltaW0x commented Nov 20, 2024

I've added a custom pipeline input in ImGui_ImplSDLGPU_RenderDrawData, you can try that or just do the separate renderpass. Having a separate pass is useful when you want to disable rending ImGui at all too, like when shipping for a game or something similar

@povik
Copy link

povik commented Nov 20, 2024

I've made it work with a separate render pass.

@ocornut
Copy link
Owner

ocornut commented Nov 20, 2024

Good job, thanks!
I'm presently trying to focus on other features but I hope to get back to this soon-ish.

@DeltaW0x
Copy link
Author

Please take your time, I'll keep working on it to improve some things anyways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants