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

Added SDL_gpu backend but maybe do not merge? #7998

Open
wants to merge 3 commits into
base: docking
Choose a base branch
from

Conversation

Acclution
Copy link

Added SDL_gpu support.. though because SDL3 and SDL_gpu are new and they are changing the API quite often then it might not be good to merge to docking.. maybe a new branch? So feel free to reject if this is wrong 😅 .

Made this pull request to just get the ball rolling. Working on my project that does not use that much ImGui stuff so I dont know how much is working haha. I do know ImageButton works at least. Mostly modified from imgui_impl_vulkan and sdlrenderer3 backends. I got an issue with Y being flipped that I fixed with changing the scale and translation uniform. The shaders are changed to use uniform buffer instead of push constant and the texture is on set 2 instead of 0. I guess that is how SDL_gpu wants it? and my code formatter did a number on the formatting of the code haha 😅

Tested on Linux and with Vulkan. I have the shaders compiled only for vulkan so if D3D or others then you need to compile them for those sdl drivers.

Just wanted to share my implementation.. so if this is already in the works or if it is ugly just reject haha.
I don't think I will be actively maintaining it as well soo yea merge or use at your own risk I guess haha.

Edit: Aa just noticed the formatting guide in the pulll request doc.. so will fix that at least soonish

How to use:

// Initialize
ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert),
										  sizeof(ImDrawIdx));
IMGUI_CHECKVERSION();

// this initializes imgui for SDL
if(!ImGui_ImplSDL3_InitForSDLGpu(TheSDL_window, TheSDL_GPUDevice))
{
	return false;
}

if(!ImGui_ImplSDLGpu_Init(TheSDL_GPUDevice, TheSDL_window))
{
	LogError("ImGui_ImplVulkan_Init failed");
	return false;
}

// Loop
ImGui_ImplSDLGpu_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();

// Render
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();

SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(TheSDL_GPUDevice);
SDL_GPUTexture* swapchain = SDL_AcquireGPUSwapchainTexture(cmd, TheSDL_window, &resolution.width, &resolution.height);

// Clear the screen
SDL_GPUColorTargetInfo color_target_info{};
color_target_info.texture = swapchain;
color_target_info.clear_color.r = 0.0f;
color_target_info.clear_color.g = 0.0f;
color_target_info.clear_color.b = 0.0f;
color_target_info.clear_color.a = 1.0f;
color_target_info.load_op = SDL_GPU_LOADOP_CLEAR;
color_target_info.store_op = SDL_GPU_STOREOP_STORE;

SDL_GPURenderPass* renderPass = SDL_BeginGPURenderPass(cmd, &color_target_info, 1, nullptr);
// Render your stuff here
SDL_EndGPURenderPass(renderPass);

// Render ImGui
ImGui_ImplSDLGpu_RenderDrawData(draw_data, cmd, swapchain);

// End render
SDL_SubmitGPUCommandBuffer(cmd);

// Shutdown
ImGui_ImplSDLGpu_Shutdown();
ImGui_ImplSDL3_Shutdown();

@ocornut
Copy link
Owner

ocornut commented Sep 19, 2024

Thank you. Linking to #7988.

Some quick feedback:

  • This would need to be called _impl_sdlgpu3.cpp/.h.
  • This would need an example_sdl3_sdlgpu3/ example.
  • Since the SDL api spells it "GPU" uppercase I believe our symbols should do the same.

@ocornut ocornut changed the title Added SDL_gpu backen but maybe do not merge? Added SDL_gpu backend but maybe do not merge? Sep 19, 2024
@Acclution
Copy link
Author

Aa nice I had not even noticed the #7988 issue haha.

True I will fix the other stuff later after work =)

@Acclution
Copy link
Author

pushed fixes for the feedback and an untested example 😅

@ocornut
Copy link
Owner

ocornut commented Sep 19, 2024

Thank you! I should be honest, the most important thing I am looking for in a PR is trust that things are done with extreme amount of care. Untested stuff and miscellaneous changes that are not explained (e.g. changes to files in vulkan/ folder) are unfortunately effectively the best way to get me to not look at a PR in a timely manner.

I realize you suggested "maybe do not merge" so this could be intended as a reference only. Even unfinished/unmerged PR are useful when it comes to eventually get feature in. But if the intent is to be eventually merged it's always better when things are done neatly :)

@Acclution
Copy link
Author

Yea. The PR was just intended for reference on my part/show how I did it so feel free to reject. And I did it in one day without much though as well so that is why I was a bit hesitant at first but thought that it could serve as a reference at least =)

@ocornut
Copy link
Owner

ocornut commented Sep 19, 2024

Thank you!
It's pleasantly nice that this new API can be useful with not so many lines of code.

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.

2 participants