From e79984927266f9b44302b2b43e70cf674513cffd Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 9 Jan 2025 16:08:14 +0100 Subject: [PATCH] Backends: SDLGPU3: Added sdl_gpu backend (amends). (#8163, #7998, #7988) --- backends/imgui_impl_sdlgpu3.cpp | 301 ++++++++++++------------ backends/imgui_impl_sdlgpu3.h | 18 +- backends/sdlgpu3/build_instructions.txt | 6 +- backends/sdlgpu3/shader.frag | 5 +- backends/sdlgpu3/shader.vert | 8 +- backends/vulkan/build_instructions.txt | 4 + docs/CHANGELOG.txt | 1 + docs/EXAMPLES.md | 24 +- docs/README.md | 2 +- examples/example_sdl3_sdlgpu3/main.cpp | 53 +++-- 10 files changed, 221 insertions(+), 201 deletions(-) create mode 100644 backends/vulkan/build_instructions.txt diff --git a/backends/imgui_impl_sdlgpu3.cpp b/backends/imgui_impl_sdlgpu3.cpp index 31e899b77d92..e681efdc39ab 100644 --- a/backends/imgui_impl_sdlgpu3.cpp +++ b/backends/imgui_impl_sdlgpu3.cpp @@ -1,4 +1,4 @@ -// dear imgui: Renderer Backend for SDL_Gpu +// dear imgui: Renderer Backend for SDL_GPU // This needs to be used along with the SDL3 Platform Backend // Implemented features: @@ -17,19 +17,19 @@ // - Introduction, links and more at the top of imgui.cpp // Important note to the reader who wish to integrate imgui_impl_sdlgpu3.cpp/.h in their own engine/app. -// - Unline other backends, the user must call the function Imgui_ImplSDLGPU_PrepareDrawData BEFORE issuing a SDL_GPURenderPass containing ImGui_ImplSDLGPU_RenderDrawData. +// - Unlike other backends, the user must call the function Imgui_ImplSDLGPU3_PrepareDrawData() BEFORE issuing a SDL_GPURenderPass containing ImGui_ImplSDLGPU3_RenderDrawData. // Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info. // CHANGELOG -// 2024-11-18: SDL_Gpu: Added the SDL_Gpu backend. +// 2025-01-09: SDL_Gpu: Added the SDL_GPU3 backend. #include "imgui.h" #ifndef IMGUI_DISABLE #include "imgui_impl_sdlgpu3.h" #include "imgui_impl_sdlgpu3_shaders.h" -// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU_RenderDrawData() -struct ImGui_ImplSDLGPU_FrameData +// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData() +struct ImGui_ImplSDLGPU3_FrameData { SDL_GPUBuffer* VertexBuffer = nullptr; SDL_GPUBuffer* IndexBuffer = nullptr; @@ -37,10 +37,10 @@ struct ImGui_ImplSDLGPU_FrameData uint32_t IndexBufferSize = 0; }; -// SDL_Gpu Data -struct ImGui_ImplSDLGPU_Data +// SDL_GPU Data +struct ImGui_ImplSDLGPU3_Data { - ImGui_ImplSDLGPU_InitInfo GPUInitInfo; + ImGui_ImplSDLGPU3_InitInfo GPUInitInfo; // Graphics pipeline & shaders SDL_GPUShader* VertexShader = nullptr; @@ -50,16 +50,16 @@ struct ImGui_ImplSDLGPU_Data // Font data SDL_GPUSampler* FontSampler = nullptr; SDL_GPUTexture* FontTexture = nullptr; - SDL_GPUTextureSamplerBinding FontBinding = {nullptr,nullptr}; + SDL_GPUTextureSamplerBinding FontBinding = { nullptr, nullptr }; // Frame data for main window - ImGui_ImplSDLGPU_FrameData MainWindowFrameData; + ImGui_ImplSDLGPU3_FrameData MainWindowFrameData; }; // Forward Declarations -static bool ImGui_ImplSDLGPU_CreateDeviceObjects(); -static void ImGui_ImplSDLGPU_DestroyDeviceObjects(); -static void ImGui_ImplSDLGPU_DestroyFrameData(); +static bool ImGui_ImplSDLGPU3_CreateDeviceObjects(); +static void ImGui_ImplSDLGPU3_DestroyDeviceObjects(); +static void ImGui_ImplSDLGPU3_DestroyFrameData(); //----------------------------------------------------------------------------- // FUNCTIONS @@ -68,38 +68,19 @@ static void ImGui_ImplSDLGPU_DestroyFrameData(); // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. // FIXME: multi-context support has never been tested. -static ImGui_ImplSDLGPU_Data* ImGui_ImplSDLGPU_GetBackendData() +static ImGui_ImplSDLGPU3_Data* ImGui_ImplSDLGPU3_GetBackendData() { - return ImGui::GetCurrentContext() ? (ImGui_ImplSDLGPU_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; + return ImGui::GetCurrentContext() ? (ImGui_ImplSDLGPU3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; } -static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer,uint32_t* old_size, uint32_t new_size,SDL_GPUBufferUsageFlags usage) +static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGraphicsPipeline* pipeline, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass * render_pass, ImGui_ImplSDLGPU3_FrameData* fd, uint32_t fb_width, uint32_t fb_height) { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + //ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); - SDL_WaitForGPUIdle(v->GpuDevice); - SDL_ReleaseGPUBuffer(v->GpuDevice, *buffer); - - SDL_GPUBufferCreateInfo buffer_info = {}; - buffer_info.usage = usage; - buffer_info.size = new_size; - buffer_info.props = 0; - *buffer = SDL_CreateGPUBuffer(v->GpuDevice,&buffer_info); - *old_size = new_size; - IM_ASSERT(*buffer != nullptr && "Failed to create GPU Buffer, call SDL_GetError() for more information"); -} - -static void ImGui_ImplSDLGPU_SetupRenderState(ImDrawData* draw_data,SDL_GPUGraphicsPipeline* pipeline, SDL_GPUCommandBuffer* command_buffer,SDL_GPURenderPass * render_pass, ImGui_ImplSDLGPU_FrameData* fd, uint32_t fb_width, uint32_t fb_height) -{ - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - - // Bind graphics pipeline: - { - SDL_BindGPUGraphicsPipeline(render_pass,pipeline); - } + // Bind graphics pipeline + SDL_BindGPUGraphicsPipeline(render_pass,pipeline); - // Bind Vertex And Index Buffers: + // Bind Vertex And Index Buffers if (draw_data->TotalVtxCount > 0) { SDL_GPUBufferBinding vertex_buffer_binding = {}; @@ -112,56 +93,66 @@ static void ImGui_ImplSDLGPU_SetupRenderState(ImDrawData* draw_data,SDL_GPUGraph SDL_BindGPUIndexBuffer(render_pass,&index_buffer_binding,sizeof(ImDrawIdx) == 2 ? SDL_GPU_INDEXELEMENTSIZE_16BIT : SDL_GPU_INDEXELEMENTSIZE_32BIT); } - // Setup viewport: - { - SDL_GPUViewport viewport = {}; - viewport.x = 0; - viewport.y = 0; - viewport.w = fb_width; - viewport.h = fb_height; - viewport.min_depth = 0.0f; - viewport.min_depth = 1.0f; - SDL_SetGPUViewport(render_pass,&viewport); - } - - // Setup scale and translation: + // Setup viewport + SDL_GPUViewport viewport = {}; + viewport.x = 0; + viewport.y = 0; + viewport.w = (float)fb_width; + viewport.h = (float)fb_height; + viewport.min_depth = 0.0f; + viewport.min_depth = 1.0f; + SDL_SetGPUViewport(render_pass,&viewport); + + // Setup scale and translation // Our visible imgui space lies from draw_data->DisplayPps (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. - { - struct UBO{ - float scale[2]; - float translation[2]; - } ubo; - ubo.scale[0] = 2.0f / draw_data->DisplaySize.x; - ubo.scale[1] = 2.0f / draw_data->DisplaySize.y; - ubo.translation[0] = -1.0f - draw_data->DisplayPos.x * ubo.scale[0]; - ubo.translation[1] = -1.0f - draw_data->DisplayPos.y * ubo.scale[1]; - SDL_PushGPUVertexUniformData(command_buffer,0,&ubo,sizeof(UBO)); - } + struct UBO { float scale[2]; float translation[2]; } ubo; + ubo.scale[0] = 2.0f / draw_data->DisplaySize.x; + ubo.scale[1] = 2.0f / draw_data->DisplaySize.y; + ubo.translation[0] = -1.0f - draw_data->DisplayPos.x * ubo.scale[0]; + ubo.translation[1] = -1.0f - draw_data->DisplayPos.y * ubo.scale[1]; + SDL_PushGPUVertexUniformData(command_buffer, 0, &ubo, sizeof(UBO)); } -// SDL_GPU doesn't allow copy passes to occur while a render or compute pass is bound +static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage) +{ + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; + + SDL_WaitForGPUIdle(v->GpuDevice); + SDL_ReleaseGPUBuffer(v->GpuDevice, *buffer); + + SDL_GPUBufferCreateInfo buffer_info = {}; + buffer_info.usage = usage; + buffer_info.size = new_size; + buffer_info.props = 0; + *buffer = SDL_CreateGPUBuffer(v->GpuDevice, &buffer_info); + *old_size = new_size; + IM_ASSERT(*buffer != nullptr && "Failed to create GPU Buffer, call SDL_GetError() for more information"); +} + +// SDL_GPU doesn't allow copy passes to occur while a render or compute pass is bound! // The only way to allow a user to supply their own RenderPass (to render to a texture instead of the window for example), -// is to split the upload part of ImGui_ImplSDLGPU_RenderDrawData to another function that needs to be called by the user before rendering -void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data,SDL_GPUCommandBuffer* command_buffer) +// is to split the upload part of ImGui_ImplSDLGPU3_RenderDrawData() to another function that needs to be called by the user before rendering. +void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); - if(fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount <= 0) + if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount <= 0) return; - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; - ImGui_ImplSDLGPU_FrameData* fd = &bd->MainWindowFrameData; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData; uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx); - - if(fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size) - CreateOrResizeBuffer(&fd->VertexBuffer,&fd->VertexBufferSize,vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX); + if (fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size) + CreateOrResizeBuffer(&fd->VertexBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX); if (fd->IndexBuffer == nullptr || fd->IndexBufferSize < index_size) - CreateOrResizeBuffer(&fd->IndexBuffer,&fd->IndexBufferSize,index_size, SDL_GPU_BUFFERUSAGE_INDEX); + CreateOrResizeBuffer(&fd->IndexBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX); + // FIXME: It feels like more code could be shared there. SDL_GPUTransferBufferCreateInfo vertex_transferbuffer_info = {}; vertex_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; vertex_transferbuffer_info.size = vertex_size; @@ -169,22 +160,23 @@ void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data,SDL_GPUCommandBuffer index_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; index_transferbuffer_info.size = index_size; - SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice,&vertex_transferbuffer_info); + SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice, &vertex_transferbuffer_info); IM_ASSERT(vertex_transferbuffer != nullptr && "Failed to create the vertex transfer buffer, call SDL_GetError() for more information"); - SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice,&index_transferbuffer_info); + SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice, &index_transferbuffer_info); IM_ASSERT(index_transferbuffer != nullptr && "Failed to create the index transfer buffer, call SDL_GetError() for more information"); - ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->GpuDevice,vertex_transferbuffer,true); - ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->GpuDevice,index_transferbuffer,true); - for(int n = 0; n < draw_data->CmdListsCount; n++){ + ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->GpuDevice, vertex_transferbuffer, true); + ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->GpuDevice, index_transferbuffer, true); + for (int n = 0; n < draw_data->CmdListsCount; n++) + { const ImDrawList* draw_list = draw_data->CmdLists[n]; memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert)); memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); vtx_dst += draw_list->VtxBuffer.Size; idx_dst += draw_list->IdxBuffer.Size; } - SDL_UnmapGPUTransferBuffer(v->GpuDevice,vertex_transferbuffer); - SDL_UnmapGPUTransferBuffer(v->GpuDevice,index_transferbuffer); + SDL_UnmapGPUTransferBuffer(v->GpuDevice, vertex_transferbuffer); + SDL_UnmapGPUTransferBuffer(v->GpuDevice, index_transferbuffer); SDL_GPUTransferBufferLocation vertex_buffer_location = {}; vertex_buffer_location.offset = 0; @@ -197,34 +189,35 @@ void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data,SDL_GPUCommandBuffer vertex_buffer_region.buffer = fd->VertexBuffer; vertex_buffer_region.offset = 0; vertex_buffer_region.size = vertex_size; + SDL_GPUBufferRegion index_buffer_region = {}; index_buffer_region.buffer = fd->IndexBuffer; index_buffer_region.offset = 0; index_buffer_region.size = index_size; SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(command_buffer); - SDL_UploadToGPUBuffer(copy_pass,&vertex_buffer_location,&vertex_buffer_region,true); - SDL_UploadToGPUBuffer(copy_pass,&index_buffer_location,&index_buffer_region,true); + SDL_UploadToGPUBuffer(copy_pass, &vertex_buffer_location, &vertex_buffer_region,true); + SDL_UploadToGPUBuffer(copy_pass, &index_buffer_location, &index_buffer_region,true); SDL_EndGPUCopyPass(copy_pass); SDL_ReleaseGPUTransferBuffer(v->GpuDevice, index_transferbuffer); SDL_ReleaseGPUTransferBuffer(v->GpuDevice, vertex_transferbuffer); } -void ImGui_ImplSDLGPU_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline) +void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); - if(fb_width <= 0 || fb_height <= 0) + if (fb_width <= 0 || fb_height <= 0) return; - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_FrameData* fd = &bd->MainWindowFrameData; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData; if (pipeline == nullptr) pipeline = bd->Pipeline; - ImGui_ImplSDLGPU_SetupRenderState(draw_data,pipeline,command_buffer,render_pass,fd,fb_width,fb_height); + ImGui_ImplSDLGPU3_SetupRenderState(draw_data, pipeline, command_buffer, render_pass, fd, fb_width, fb_height); // Will project scissor/clipping rectangles into framebuffer space ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports @@ -267,10 +260,10 @@ void ImGui_ImplSDLGPU_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer SDL_SetGPUScissor(render_pass,&scissor_rect); // Bind DescriptorSet with font or user texture - SDL_BindGPUFragmentSamplers(render_pass,0,(SDL_GPUTextureSamplerBinding*)pcmd->GetTexID(),1); + SDL_BindGPUFragmentSamplers(render_pass, 0, (SDL_GPUTextureSamplerBinding*)pcmd->GetTexID(), 1); // Draw - SDL_DrawGPUIndexedPrimitives(render_pass,pcmd->ElemCount,1,pcmd->IdxOffset + global_idx_offset,pcmd->VtxOffset + global_vtx_offset,0); + SDL_DrawGPUIndexedPrimitives(render_pass, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0); } } global_idx_offset += draw_list->IdxBuffer.Size; @@ -281,21 +274,21 @@ void ImGui_ImplSDLGPU_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer // Our last values will leak into user/application rendering if you forgot to call SDL_SetGPUViewport() and SDL_SetGPUScissor() yourself to explicitly set that state // In theory we should aim to backup/restore those values but I am not sure this is possible. // We perform a call to SDL_SetGPUScissor() to set back a full viewport which is likely to fix things for 99% users but technically this is not perfect. (See github #4644) - SDL_Rect scissor_rect {0,0,fb_width,fb_height}; - SDL_SetGPUScissor(render_pass,&scissor_rect); + SDL_Rect scissor_rect { 0, 0, fb_width, fb_height }; + SDL_SetGPUScissor(render_pass, &scissor_rect); } -bool ImGui_ImplSDLGPU_CreateFontsTexture() +bool ImGui_ImplSDLGPU3_CreateFontsTexture() { ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; // Destroy existing texture (if any) if (bd->FontTexture) { SDL_WaitForGPUIdle(v->GpuDevice); - ImGui_ImplSDLGPU_DestroyFontsTexture(); + ImGui_ImplSDLGPU3_DestroyFontsTexture(); } unsigned char* pixels; @@ -315,7 +308,7 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture() texture_info.num_levels = 1; texture_info.sample_count = SDL_GPU_SAMPLECOUNT_1; - bd->FontTexture = SDL_CreateGPUTexture(v->GpuDevice,&texture_info); + bd->FontTexture = SDL_CreateGPUTexture(v->GpuDevice, &texture_info); IM_ASSERT(bd->FontTexture && "Failed to create font texture, call SDL_GetError() for more info"); } @@ -328,12 +321,12 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture() font_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; font_transferbuffer_info.size = upload_size; - SDL_GPUTransferBuffer* font_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice,&font_transferbuffer_info); + SDL_GPUTransferBuffer* font_transferbuffer = SDL_CreateGPUTransferBuffer(v->GpuDevice, &font_transferbuffer_info); IM_ASSERT(font_transferbuffer != nullptr && "Failed to create font transfer buffer, call SDL_GetError() for more information"); - void* texture_ptr = SDL_MapGPUTransferBuffer(v->GpuDevice,font_transferbuffer,false); - memcpy(texture_ptr,pixels,upload_size); - SDL_UnmapGPUTransferBuffer(v->GpuDevice,font_transferbuffer); + void* texture_ptr = SDL_MapGPUTransferBuffer(v->GpuDevice, font_transferbuffer, false); + memcpy(texture_ptr, pixels, upload_size); + SDL_UnmapGPUTransferBuffer(v->GpuDevice, font_transferbuffer); SDL_GPUTextureTransferInfo font_transfer_info = {}; font_transfer_info.offset = 0; @@ -347,7 +340,7 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture() SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->GpuDevice); SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd); - SDL_UploadToGPUTexture(copy_pass,&font_transfer_info,&font_texture_region,false); + SDL_UploadToGPUTexture(copy_pass, &font_transfer_info, &font_texture_region, false); SDL_EndGPUCopyPass(copy_pass); SDL_SubmitGPUCommandBuffer(cmd); SDL_ReleaseGPUTransferBuffer(v->GpuDevice, font_transferbuffer); @@ -359,28 +352,26 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture() return true; } -// You probably never need to call this, as it is called by ImGui_ImplSDLGPU_CreateFontsTexture() and ImGui_ImplSDLGPU_Shutdown(). -void ImGui_ImplSDLGPU_DestroyFontsTexture() +// You probably never need to call this, as it is called by ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_Shutdown(). +void ImGui_ImplSDLGPU3_DestroyFontsTexture() { ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; - - if(bd->FontTexture) + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; + if (bd->FontTexture) { - SDL_ReleaseGPUTexture(v->GpuDevice,bd->FontTexture); + SDL_ReleaseGPUTexture(v->GpuDevice, bd->FontTexture); bd->FontBinding.texture = nullptr; bd->FontTexture = nullptr; } - io.Fonts->SetTexID(0); } -static void Imgui_ImplSDLGPU_CreateShaders() +static void Imgui_ImplSDLGPU3_CreateShaders() { // Create the shader modules - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; const char* driver = SDL_GetGPUDeviceDriver(v->GpuDevice); @@ -400,22 +391,20 @@ static void Imgui_ImplSDLGPU_CreateShaders() fragment_shader_info.num_storage_textures = 0; fragment_shader_info.num_uniform_buffers = 0; - if(strcmp(driver,"vulkan") == 0) + if (strcmp(driver, "vulkan") == 0) { vertex_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV; vertex_shader_info.code = spirv_vertex; vertex_shader_info.code_size = sizeof(spirv_vertex); - fragment_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV; fragment_shader_info.code = spirv_fragment; fragment_shader_info.code_size = sizeof(spirv_fragment); } - else if (strcmp(driver,"direct3d12") == 0) + else if (strcmp(driver, "direct3d12") == 0) { vertex_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC; vertex_shader_info.code = dxbc_vertex; vertex_shader_info.code_size = sizeof(dxbc_vertex); - fragment_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC; fragment_shader_info.code = dxbc_fragment; fragment_shader_info.code_size = sizeof(dxbc_fragment); @@ -427,25 +416,23 @@ static void Imgui_ImplSDLGPU_CreateShaders() vertex_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB; vertex_shader_info.code = metallib_vertex; vertex_shader_info.code_size = sizeof(metallib_vertex); - fragment_shader_info.entrypoint = "main0"; fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB; fragment_shader_info.code = metallib_fragment; fragment_shader_info.code_size = sizeof(metallib_fragment); } #endif - bd->VertexShader = SDL_CreateGPUShader(v->GpuDevice,&vertex_shader_info); - bd->FragmentShader = SDL_CreateGPUShader(v->GpuDevice,&fragment_shader_info); - + bd->VertexShader = SDL_CreateGPUShader(v->GpuDevice, &vertex_shader_info); + bd->FragmentShader = SDL_CreateGPUShader(v->GpuDevice, &fragment_shader_info); IM_ASSERT(bd->VertexShader != nullptr && "Failed to create vertex shader, call SDL_GetError() for more information"); IM_ASSERT(bd->FragmentShader != nullptr && "Failed to create fragment shader, call SDL_GetError() for more information"); } -static void ImGui_ImplSDLGPU_CreateGraphicsPipeline() +static void ImGui_ImplSDLGPU3_CreateGraphicsPipeline() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; - Imgui_ImplSDLGPU_CreateShaders(); + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; + Imgui_ImplSDLGPU3_CreateShaders(); SDL_GPUVertexBufferDescription vertex_buffer_desc[1]; vertex_buffer_desc[0].slot = 0; @@ -520,14 +507,14 @@ static void ImGui_ImplSDLGPU_CreateGraphicsPipeline() pipeline_info.depth_stencil_state = depth_stencil_state; pipeline_info.target_info = target_info; - bd->Pipeline = SDL_CreateGPUGraphicsPipeline(v->GpuDevice,&pipeline_info); + bd->Pipeline = SDL_CreateGPUGraphicsPipeline(v->GpuDevice, &pipeline_info); IM_ASSERT(bd->Pipeline != nullptr && "Failed to create graphics pipeline, call SDL_GetError() for more information"); } -bool ImGui_ImplSDLGPU_CreateDeviceObjects() +bool ImGui_ImplSDLGPU3_CreateDeviceObjects() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; if (!bd->FontSampler) { @@ -546,53 +533,53 @@ bool ImGui_ImplSDLGPU_CreateDeviceObjects() sampler_info.max_anisotropy = 1.0f; sampler_info.enable_compare = false; - bd->FontSampler = SDL_CreateGPUSampler(v->GpuDevice,&sampler_info); + bd->FontSampler = SDL_CreateGPUSampler(v->GpuDevice, &sampler_info); bd->FontBinding.sampler = bd->FontSampler; IM_ASSERT(bd->FontSampler != nullptr && "Failed to create font sampler, call SDL_GetError() for more information"); } - ImGui_ImplSDLGPU_CreateGraphicsPipeline(); + ImGui_ImplSDLGPU3_CreateGraphicsPipeline(); return true; } -void ImGui_ImplSDLGPU_DestroyFrameData() +void ImGui_ImplSDLGPU3_DestroyFrameData() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; - SDL_ReleaseGPUBuffer(v->GpuDevice,bd->MainWindowFrameData.VertexBuffer); - SDL_ReleaseGPUBuffer(v->GpuDevice,bd->MainWindowFrameData.IndexBuffer); + SDL_ReleaseGPUBuffer(v->GpuDevice, bd->MainWindowFrameData.VertexBuffer); + SDL_ReleaseGPUBuffer(v->GpuDevice, bd->MainWindowFrameData.IndexBuffer); bd->MainWindowFrameData.VertexBuffer = nullptr; bd->MainWindowFrameData.IndexBuffer = nullptr; bd->MainWindowFrameData.VertexBufferSize = 0; bd->MainWindowFrameData.IndexBufferSize = 0; } -void ImGui_ImplSDLGPU_DestroyDeviceObjects() +void ImGui_ImplSDLGPU3_DestroyDeviceObjects() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo; - ImGui_ImplSDLGPU_DestroyFrameData(); - ImGui_ImplSDLGPU_DestroyFontsTexture(); + ImGui_ImplSDLGPU3_DestroyFrameData(); + ImGui_ImplSDLGPU3_DestroyFontsTexture(); - if(bd->VertexShader) { SDL_ReleaseGPUShader(v->GpuDevice,bd->VertexShader); bd->VertexShader = nullptr;} - if(bd->FragmentShader) { SDL_ReleaseGPUShader(v->GpuDevice,bd->FragmentShader); bd->FragmentShader = nullptr;} - if(bd->FontSampler) { SDL_ReleaseGPUSampler(v->GpuDevice, bd->FontSampler); bd->FontSampler = nullptr;} - if(bd->Pipeline) { SDL_ReleaseGPUGraphicsPipeline(v->GpuDevice,bd->Pipeline); bd->Pipeline = nullptr;} + if (bd->VertexShader) { SDL_ReleaseGPUShader(v->GpuDevice, bd->VertexShader); bd->VertexShader = nullptr;} + if (bd->FragmentShader) { SDL_ReleaseGPUShader(v->GpuDevice, bd->FragmentShader); bd->FragmentShader = nullptr;} + if (bd->FontSampler) { SDL_ReleaseGPUSampler(v->GpuDevice, bd->FontSampler); bd->FontSampler = nullptr;} + if (bd->Pipeline) { SDL_ReleaseGPUGraphicsPipeline(v->GpuDevice, bd->Pipeline); bd->Pipeline = nullptr;} } -bool ImGui_ImplSDLGPU_Init(ImGui_ImplSDLGPU_InitInfo* info) +bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info) { ImGuiIO& io = ImGui::GetIO(); IMGUI_CHECKVERSION(); IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); // Setup backend capabilities flags - ImGui_ImplSDLGPU_Data* bd = IM_NEW(ImGui_ImplSDLGPU_Data)(); + ImGui_ImplSDLGPU3_Data* bd = IM_NEW(ImGui_ImplSDLGPU3_Data)(); io.BackendRendererUserData = (void*)bd; - io.BackendRendererName = "imgui_impl_sdlgpu"; + io.BackendRendererName = "imgui_impl_sdlgpu3"; io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. IM_ASSERT(info->GpuDevice != nullptr); @@ -600,31 +587,31 @@ bool ImGui_ImplSDLGPU_Init(ImGui_ImplSDLGPU_InitInfo* info) bd->GPUInitInfo = *info; - ImGui_ImplSDLGPU_CreateDeviceObjects(); + ImGui_ImplSDLGPU3_CreateDeviceObjects(); return true; } -void ImGui_ImplSDLGPU_Shutdown() +void ImGui_ImplSDLGPU3_Shutdown() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplSDLGPU_DestroyDeviceObjects(); + ImGui_ImplSDLGPU3_DestroyDeviceObjects(); io.BackendRendererName = nullptr; io.BackendRendererUserData = nullptr; io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; IM_DELETE(bd); } -void ImGui_ImplSDLGPU_NewFrame() +void ImGui_ImplSDLGPU3_NewFrame() { - ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); - IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplVulkan_Init()?"); + ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData(); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLGPU3_Init()?"); - if(!bd->FontTexture) - ImGui_ImplSDLGPU_CreateFontsTexture(); + if (!bd->FontTexture) + ImGui_ImplSDLGPU3_CreateFontsTexture(); } #endif // #ifndef IMGUI_DISABLE diff --git a/backends/imgui_impl_sdlgpu3.h b/backends/imgui_impl_sdlgpu3.h index 18d3001e400c..ff9c751c8660 100644 --- a/backends/imgui_impl_sdlgpu3.h +++ b/backends/imgui_impl_sdlgpu3.h @@ -1,4 +1,4 @@ -// dear imgui: Renderer Backend for SDL_Gpu +// dear imgui: Renderer Backend for SDL_GPU // This needs to be used along with the SDL3 Platform Backend // Implemented features: @@ -27,7 +27,7 @@ // Initialization data, for ImGui_ImplSDLGPU_Init() // - Remember to set ColorTargetFormat to the correct format. If you're rendering to the swapchain, call SDL_GetGPUSwapchainTextureFormat to query the right value -struct ImGui_ImplSDLGPU_InitInfo +struct ImGui_ImplSDLGPU3_InitInfo { SDL_GPUDevice* GpuDevice = nullptr; SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID; @@ -35,12 +35,12 @@ struct ImGui_ImplSDLGPU_InitInfo }; // Follow "Getting Started" link and check examples/ folder to learn about using backends! -IMGUI_IMPL_API bool ImGui_ImplSDLGPU_Init(ImGui_ImplSDLGPU_InitInfo* info); -IMGUI_IMPL_API void ImGui_ImplSDLGPU_Shutdown(); -IMGUI_IMPL_API void ImGui_ImplSDLGPU_NewFrame(); -IMGUI_IMPL_API void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer); -IMGUI_IMPL_API void ImGui_ImplSDLGPU_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline = nullptr); -IMGUI_IMPL_API bool ImGui_ImplSDLGPU_CreateFontsTexture(); -IMGUI_IMPL_API void ImGui_ImplSDLGPU_DestroyFontsTexture(); +IMGUI_IMPL_API bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info); +IMGUI_IMPL_API void ImGui_ImplSDLGPU3_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplSDLGPU3_NewFrame(); +IMGUI_IMPL_API void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer); +IMGUI_IMPL_API void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline = nullptr); +IMGUI_IMPL_API bool ImGui_ImplSDLGPU3_CreateFontsTexture(); +IMGUI_IMPL_API void ImGui_ImplSDLGPU3_DestroyFontsTexture(); #endif // #ifndef IMGUI_DISABLE diff --git a/backends/sdlgpu3/build_instructions.txt b/backends/sdlgpu3/build_instructions.txt index 751dc97a4e0b..25f4a5d28318 100644 --- a/backends/sdlgpu3/build_instructions.txt +++ b/backends/sdlgpu3/build_instructions.txt @@ -1,3 +1,7 @@ + +Instructions to rebuild imgui_impl_sdlgpu3_shaders.h +(You don't need to copy this folder if you are using the backend as-is) + 1) Compile the raw shader files to SPIRV: glslc -o vertex.spv -c shader.vert @@ -33,4 +37,4 @@ Proceed to step 4 -4) Either find a way to load the shader bytecode from file, or use a tool like https://notisrac.github.io/FileToCArray/ to convert the file to a uint8_t array \ No newline at end of file +4) Use a tool like https://notisrac.github.io/FileToCArray/ or misc/fonts/binary_to_compressed_c.cpp in imgui repository to convert the file to a uint8_t array. diff --git a/backends/sdlgpu3/shader.frag b/backends/sdlgpu3/shader.frag index 2de1f76a3b23..ab9ce184e0fa 100644 --- a/backends/sdlgpu3/shader.frag +++ b/backends/sdlgpu3/shader.frag @@ -3,7 +3,8 @@ layout(location = 0) out vec4 fColor; layout(set=2, binding=0) uniform sampler2D sTexture; -layout(location = 0) in struct { +layout(location = 0) in struct +{ vec4 Color; vec2 UV; } In; @@ -11,4 +12,4 @@ layout(location = 0) in struct { void main() { fColor = In.Color * texture(sTexture, In.UV.st); -} \ No newline at end of file +} diff --git a/backends/sdlgpu3/shader.vert b/backends/sdlgpu3/shader.vert index 3363da8b2a7b..3a85a9038192 100644 --- a/backends/sdlgpu3/shader.vert +++ b/backends/sdlgpu3/shader.vert @@ -3,12 +3,14 @@ layout(location = 0) in vec2 aPos; layout(location = 1) in vec2 aUV; layout(location = 2) in vec4 aColor; -layout(set=1,binding=0) uniform UBO { +layout(set=1,binding=0) uniform UBO +{ vec2 uScale; vec2 uTranslate; } ubo; -layout(location = 0) out struct { +layout(location = 0) out struct +{ vec4 Color; vec2 UV; } Out; @@ -19,4 +21,4 @@ void main() Out.UV = aUV; gl_Position = vec4(aPos * ubo.uScale + ubo.uTranslate, 0, 1); gl_Position.y *= -1.0f; -} \ No newline at end of file +} diff --git a/backends/vulkan/build_instructions.txt b/backends/vulkan/build_instructions.txt new file mode 100644 index 000000000000..1f028d96f53e --- /dev/null +++ b/backends/vulkan/build_instructions.txt @@ -0,0 +1,4 @@ + +Script to rebuild shaders stored inside imgui_impl_vulkan.h +(You don't need to copy this folder if you are using the backend as-is) + diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 00df8a746418..80b95681037c 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -68,6 +68,7 @@ Other changes: - Demo: Added label edition to Property Editor demo + fix an ID issue. (#8266) [@moritz-h] - Misc: Fixed misc/cpp/imgui_stdlib.h/.cpp not supporting IMGUI_DISABLE. (#8294) [@juur] - Misc: Fixed MinGW builds uses UTF-8 friendly _wfopen(). (#8300) +- Backends: SDL_GPU for SDL3: Added backend for SDL_GPU! (#8163, #7998, #7988) [@DeltaW0x]. - Backends: Allegro5: Avoid calling al_set_mouse_cursor() repeatedly since it appears to leak on on X11 (#8256). [@Helodity] - Backends: Metal: Fixed leaks when using metal-cpp. (#8276, #8166) [@selimsandal] diff --git a/docs/EXAMPLES.md b/docs/EXAMPLES.md index 224beb1c16a0..0df73059bda2 100644 --- a/docs/EXAMPLES.md +++ b/docs/EXAMPLES.md @@ -139,8 +139,8 @@ This support building with Emscripten and targeting WebGL.
Prefer using that if you are using modern GL or WebGL in your application. [example_sdl2_sdlrenderer2/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_sdlrenderer2/)
-SDL2 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL2 (most graphics backends are supported underneath)
-= main.cpp + imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer.cpp
+SDL2 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL2 example.
+= main.cpp + imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp
This requires SDL 2.0.18+ (released November 2021)
[example_sdl2_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_vulkan/)
@@ -149,6 +149,26 @@ SDL2 (Win32, Mac, Linux, etc.) + Vulkan example.
This is quite long and tedious, because: Vulkan.
For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp. +[example_sdl3_opengl3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_opengl3/)
+SDL3 (Win32, Mac, Linux, etc.) + OpenGL3+/ES2/ES3 example.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_opengl3.cpp
+This uses more modern GL calls and custom shaders.
+This support building with Emscripten and targeting WebGL.
+ +[example_sdl3_sdlgpu3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_sdlgpu3/)
+SDL3 (Win32, Mac, Linux, etc.) + SDL_GPU for SDL3 example.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_sdlrenderer3.cpp
+ +[example_sdl3_sdlrenderer3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_sdlrenderer3/)
+SDL3 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL3 example.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_sdlrenderer3.cpp
+ +[example_sdl3_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_vulkan/)
+SDL3 (Win32, Mac, Linux, etc.) + Vulkan example.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_vulkan.cpp
+This is quite long and tedious, because: Vulkan.
+For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp. + [example_win32_directx9/](https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx9/)
DirectX9 example, Windows only.
= main.cpp + imgui_impl_win32.cpp + imgui_impl_dx9.cpp diff --git a/docs/README.md b/docs/README.md index 388d8eac63af..9b437311af12 100644 --- a/docs/README.md +++ b/docs/README.md @@ -123,7 +123,7 @@ On most platforms and when using C++, **you should be able to use a combination Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/keyboard/gamepad inputs 2) uploading a texture to your GPU/render engine 3) providing a render function that can bind textures and render textured triangles, which is essentially what Backends are doing. The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications doing just that: setting up a window and using backends. If you follow the [Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started) guide it should in theory takes you less than an hour to integrate Dear ImGui. **Make sure to spend time reading the [FAQ](https://www.dearimgui.com/faq), comments, and the examples applications!** Officially maintained backends/bindings (in repository): -- Renderers: DirectX9, DirectX10, DirectX11, DirectX12, Metal, OpenGL/ES/ES2, SDL_Renderer, Vulkan, WebGPU. +- Renderers: DirectX9, DirectX10, DirectX11, DirectX12, Metal, OpenGL/ES/ES2, SDL_GPU, SDL_Renderer2/3, Vulkan, WebGPU. - Platforms: GLFW, SDL2/SDL3, Win32, Glut, OSX, Android. - Frameworks: Allegro5, Emscripten. diff --git a/examples/example_sdl3_sdlgpu3/main.cpp b/examples/example_sdl3_sdlgpu3/main.cpp index 6c29962dd0b8..353c10857809 100644 --- a/examples/example_sdl3_sdlgpu3/main.cpp +++ b/examples/example_sdl3_sdlgpu3/main.cpp @@ -1,4 +1,4 @@ -// Dear ImGui: standalone example application for SDL_Gpu +// Dear ImGui: standalone example application for SDL3 + SDL_GPU // Learn about Dear ImGui: // - FAQ https://dearimgui.com/faq @@ -17,7 +17,6 @@ #include // abort #include - // This example doesn't compile with Emscripten yet! Awaiting SDL3 support. #ifdef __EMSCRIPTEN__ #include "../libs/emscripten/emscripten_mainloop_stub.h" @@ -34,23 +33,23 @@ int main(int, char**) } // Create SDL window graphics context - SDL_Window* window = SDL_CreateWindow("Dear ImGui SDLGpu example", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); + SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_GPU example", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); if (window == nullptr) { printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); return -1; } - // Create SDL Gpu Device - SDL_GPUDevice* gpuDevice = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB,true,nullptr); - if (gpuDevice == nullptr) + // Create GPU Device + SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB,true,nullptr); + if (gpu_device == nullptr) { printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError()); return -1; } // Claim window for GPU Device - if (!SDL_ClaimWindowForGPUDevice(gpuDevice, window)) + if (!SDL_ClaimWindowForGPUDevice(gpu_device, window)) { printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError()); return -1; @@ -69,11 +68,11 @@ int main(int, char**) // Setup Platform/Renderer backends ImGui_ImplSDL3_InitForOther(window); - ImGui_ImplSDLGPU_InitInfo init_info = {}; - init_info.GpuDevice = gpuDevice; - init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpuDevice, window); + ImGui_ImplSDLGPU3_InitInfo init_info = {}; + init_info.GpuDevice = gpu_device; + init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window); init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; - ImGui_ImplSDLGPU_Init(&init_info); + ImGui_ImplSDLGPU3_Init(&init_info); // Load Fonts // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. @@ -119,9 +118,9 @@ int main(int, char**) SDL_Delay(10); continue; } - + // Start the Dear ImGui frame - ImGui_ImplSDLGPU_NewFrame(); + ImGui_ImplSDLGPU3_NewFrame(); ImGui_ImplSDL3_NewFrame(); ImGui::NewFrame(); @@ -167,18 +166,15 @@ int main(int, char**) ImDrawData* draw_data = ImGui::GetDrawData(); const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); - // Acquire a GPU command buffer - SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpuDevice); + SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device); // Acquire a GPU command buffer - //Acquire a swapchain texture SDL_GPUTexture* swapchain_texture; - SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); + SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); // Acquire a swapchain texture if (swapchain_texture != nullptr && !is_minimized) { - // !!! THIS IS MANDATORY !!! - // Call Imgui_ImplSDLGPU_PrepareDrawData to upload the vertex/index buffer - Imgui_ImplSDLGPU_PrepareDrawData(draw_data, command_buffer); + // This is mandatory: call Imgui_ImplSDLGPU3_PrepareDrawData() to upload the vertex/index buffer! + Imgui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer); // Setup and start a render pass SDL_GPUColorTargetInfo target_info = {}; @@ -190,21 +186,26 @@ int main(int, char**) target_info.layer_or_depth_plane = 0; target_info.cycle = false; SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr); - /// Render ImGui - ImGui_ImplSDLGPU_RenderDrawData(draw_data, command_buffer, render_pass); + + // Render ImGui + ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass); + SDL_EndGPURenderPass(render_pass); } + // Submit the command buffer SDL_SubmitGPUCommandBuffer(command_buffer); } // Cleanup - SDL_WaitForGPUIdle(gpuDevice); + SDL_WaitForGPUIdle(gpu_device); ImGui_ImplSDL3_Shutdown(); - ImGui_ImplSDLGPU_Shutdown(); + ImGui_ImplSDLGPU3_Shutdown(); ImGui::DestroyContext(); - SDL_ReleaseWindowFromGPUDevice(gpuDevice, window); - SDL_DestroyGPUDevice(gpuDevice); + + SDL_ReleaseWindowFromGPUDevice(gpu_device, window); + SDL_DestroyGPUDevice(gpu_device); SDL_DestroyWindow(window); SDL_Quit(); + return 0; }