Backends: SDLGPU3: Added sdl_gpu backend (amends). (#8163, #7998, #7988)

This commit is contained in:
ocornut
2025-01-09 16:08:14 +01:00
parent 8bbccf7a97
commit e799849272
10 changed files with 220 additions and 200 deletions

View File

@@ -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 // This needs to be used along with the SDL3 Platform Backend
// Implemented features: // Implemented features:
@@ -17,19 +17,19 @@
// - Introduction, links and more at the top of imgui.cpp // - 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. // 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. // 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 // CHANGELOG
// 2024-11-18: SDL_Gpu: Added the SDL_Gpu backend. // 2025-01-09: SDL_Gpu: Added the SDL_GPU3 backend.
#include "imgui.h" #include "imgui.h"
#ifndef IMGUI_DISABLE #ifndef IMGUI_DISABLE
#include "imgui_impl_sdlgpu3.h" #include "imgui_impl_sdlgpu3.h"
#include "imgui_impl_sdlgpu3_shaders.h" #include "imgui_impl_sdlgpu3_shaders.h"
// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU_RenderDrawData() // Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData()
struct ImGui_ImplSDLGPU_FrameData struct ImGui_ImplSDLGPU3_FrameData
{ {
SDL_GPUBuffer* VertexBuffer = nullptr; SDL_GPUBuffer* VertexBuffer = nullptr;
SDL_GPUBuffer* IndexBuffer = nullptr; SDL_GPUBuffer* IndexBuffer = nullptr;
@@ -37,10 +37,10 @@ struct ImGui_ImplSDLGPU_FrameData
uint32_t IndexBufferSize = 0; uint32_t IndexBufferSize = 0;
}; };
// SDL_Gpu Data // SDL_GPU Data
struct ImGui_ImplSDLGPU_Data struct ImGui_ImplSDLGPU3_Data
{ {
ImGui_ImplSDLGPU_InitInfo GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo GPUInitInfo;
// Graphics pipeline & shaders // Graphics pipeline & shaders
SDL_GPUShader* VertexShader = nullptr; SDL_GPUShader* VertexShader = nullptr;
@@ -50,16 +50,16 @@ struct ImGui_ImplSDLGPU_Data
// Font data // Font data
SDL_GPUSampler* FontSampler = nullptr; SDL_GPUSampler* FontSampler = nullptr;
SDL_GPUTexture* FontTexture = nullptr; SDL_GPUTexture* FontTexture = nullptr;
SDL_GPUTextureSamplerBinding FontBinding = {nullptr,nullptr}; SDL_GPUTextureSamplerBinding FontBinding = { nullptr, nullptr };
// Frame data for main window // Frame data for main window
ImGui_ImplSDLGPU_FrameData MainWindowFrameData; ImGui_ImplSDLGPU3_FrameData MainWindowFrameData;
}; };
// Forward Declarations // Forward Declarations
static bool ImGui_ImplSDLGPU_CreateDeviceObjects(); static bool ImGui_ImplSDLGPU3_CreateDeviceObjects();
static void ImGui_ImplSDLGPU_DestroyDeviceObjects(); static void ImGui_ImplSDLGPU3_DestroyDeviceObjects();
static void ImGui_ImplSDLGPU_DestroyFrameData(); static void ImGui_ImplSDLGPU3_DestroyFrameData();
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// FUNCTIONS // FUNCTIONS
@@ -68,38 +68,19 @@ static void ImGui_ImplSDLGPU_DestroyFrameData();
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts // 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. // 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. // 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_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo;
SDL_WaitForGPUIdle(v->GpuDevice); // Bind graphics pipeline
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); SDL_BindGPUGraphicsPipeline(render_pass,pipeline);
}
// Bind Vertex And Index Buffers: // Bind Vertex And Index Buffers
if (draw_data->TotalVtxCount > 0) if (draw_data->TotalVtxCount > 0)
{ {
SDL_GPUBufferBinding vertex_buffer_binding = {}; 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); SDL_BindGPUIndexBuffer(render_pass,&index_buffer_binding,sizeof(ImDrawIdx) == 2 ? SDL_GPU_INDEXELEMENTSIZE_16BIT : SDL_GPU_INDEXELEMENTSIZE_32BIT);
} }
// Setup viewport: // Setup viewport
{
SDL_GPUViewport viewport = {}; SDL_GPUViewport viewport = {};
viewport.x = 0; viewport.x = 0;
viewport.y = 0; viewport.y = 0;
viewport.w = fb_width; viewport.w = (float)fb_width;
viewport.h = fb_height; viewport.h = (float)fb_height;
viewport.min_depth = 0.0f; viewport.min_depth = 0.0f;
viewport.min_depth = 1.0f; viewport.min_depth = 1.0f;
SDL_SetGPUViewport(render_pass,&viewport); SDL_SetGPUViewport(render_pass,&viewport);
}
// Setup scale and translation: // 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. // 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;
struct UBO{
float scale[2];
float translation[2];
} ubo;
ubo.scale[0] = 2.0f / draw_data->DisplaySize.x; ubo.scale[0] = 2.0f / draw_data->DisplaySize.x;
ubo.scale[1] = 2.0f / draw_data->DisplaySize.y; ubo.scale[1] = 2.0f / draw_data->DisplaySize.y;
ubo.translation[0] = -1.0f - draw_data->DisplayPos.x * ubo.scale[0]; ubo.translation[0] = -1.0f - draw_data->DisplayPos.x * ubo.scale[0];
ubo.translation[1] = -1.0f - draw_data->DisplayPos.y * ubo.scale[1]; ubo.translation[1] = -1.0f - draw_data->DisplayPos.y * ubo.scale[1];
SDL_PushGPUVertexUniformData(command_buffer,0,&ubo,sizeof(UBO)); 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), // 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 // 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_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data,SDL_GPUCommandBuffer* command_buffer) void Imgui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer)
{ {
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) // 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_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); 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; return;
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
ImGui_ImplSDLGPU_FrameData* fd = &bd->MainWindowFrameData; ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx); uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
if (fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size)
if(fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size) CreateOrResizeBuffer(&fd->VertexBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
CreateOrResizeBuffer(&fd->VertexBuffer,&fd->VertexBufferSize,vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
if (fd->IndexBuffer == nullptr || fd->IndexBufferSize < index_size) 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 = {}; SDL_GPUTransferBufferCreateInfo vertex_transferbuffer_info = {};
vertex_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; vertex_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
vertex_transferbuffer_info.size = vertex_size; 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.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
index_transferbuffer_info.size = index_size; 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"); 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"); 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); ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->GpuDevice, vertex_transferbuffer, true);
ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->GpuDevice,index_transferbuffer,true); ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->GpuDevice, index_transferbuffer, true);
for(int n = 0; n < draw_data->CmdListsCount; n++){ for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* draw_list = draw_data->CmdLists[n]; const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert)); 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)); memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size; vtx_dst += draw_list->VtxBuffer.Size;
idx_dst += draw_list->IdxBuffer.Size; idx_dst += draw_list->IdxBuffer.Size;
} }
SDL_UnmapGPUTransferBuffer(v->GpuDevice,vertex_transferbuffer); SDL_UnmapGPUTransferBuffer(v->GpuDevice, vertex_transferbuffer);
SDL_UnmapGPUTransferBuffer(v->GpuDevice,index_transferbuffer); SDL_UnmapGPUTransferBuffer(v->GpuDevice, index_transferbuffer);
SDL_GPUTransferBufferLocation vertex_buffer_location = {}; SDL_GPUTransferBufferLocation vertex_buffer_location = {};
vertex_buffer_location.offset = 0; 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.buffer = fd->VertexBuffer;
vertex_buffer_region.offset = 0; vertex_buffer_region.offset = 0;
vertex_buffer_region.size = vertex_size; vertex_buffer_region.size = vertex_size;
SDL_GPUBufferRegion index_buffer_region = {}; SDL_GPUBufferRegion index_buffer_region = {};
index_buffer_region.buffer = fd->IndexBuffer; index_buffer_region.buffer = fd->IndexBuffer;
index_buffer_region.offset = 0; index_buffer_region.offset = 0;
index_buffer_region.size = index_size; index_buffer_region.size = index_size;
SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(command_buffer); SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(command_buffer);
SDL_UploadToGPUBuffer(copy_pass,&vertex_buffer_location,&vertex_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_UploadToGPUBuffer(copy_pass, &index_buffer_location, &index_buffer_region,true);
SDL_EndGPUCopyPass(copy_pass); SDL_EndGPUCopyPass(copy_pass);
SDL_ReleaseGPUTransferBuffer(v->GpuDevice, index_transferbuffer); SDL_ReleaseGPUTransferBuffer(v->GpuDevice, index_transferbuffer);
SDL_ReleaseGPUTransferBuffer(v->GpuDevice, vertex_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) // 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_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); 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; return;
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_FrameData* fd = &bd->MainWindowFrameData; ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
if (pipeline == nullptr) if (pipeline == nullptr)
pipeline = bd->Pipeline; 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 // Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports 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); SDL_SetGPUScissor(render_pass,&scissor_rect);
// Bind DescriptorSet with font or user texture // 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 // 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; 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 // 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. // 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) // 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_Rect scissor_rect { 0, 0, fb_width, fb_height };
SDL_SetGPUScissor(render_pass,&scissor_rect); SDL_SetGPUScissor(render_pass, &scissor_rect);
} }
bool ImGui_ImplSDLGPU_CreateFontsTexture() bool ImGui_ImplSDLGPU3_CreateFontsTexture()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
// Destroy existing texture (if any) // Destroy existing texture (if any)
if (bd->FontTexture) if (bd->FontTexture)
{ {
SDL_WaitForGPUIdle(v->GpuDevice); SDL_WaitForGPUIdle(v->GpuDevice);
ImGui_ImplSDLGPU_DestroyFontsTexture(); ImGui_ImplSDLGPU3_DestroyFontsTexture();
} }
unsigned char* pixels; unsigned char* pixels;
@@ -315,7 +308,7 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture()
texture_info.num_levels = 1; texture_info.num_levels = 1;
texture_info.sample_count = SDL_GPU_SAMPLECOUNT_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"); 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.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
font_transferbuffer_info.size = upload_size; 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"); 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); void* texture_ptr = SDL_MapGPUTransferBuffer(v->GpuDevice, font_transferbuffer, false);
memcpy(texture_ptr,pixels,upload_size); memcpy(texture_ptr, pixels, upload_size);
SDL_UnmapGPUTransferBuffer(v->GpuDevice,font_transferbuffer); SDL_UnmapGPUTransferBuffer(v->GpuDevice, font_transferbuffer);
SDL_GPUTextureTransferInfo font_transfer_info = {}; SDL_GPUTextureTransferInfo font_transfer_info = {};
font_transfer_info.offset = 0; font_transfer_info.offset = 0;
@@ -347,7 +340,7 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture()
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->GpuDevice); SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->GpuDevice);
SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd); 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_EndGPUCopyPass(copy_pass);
SDL_SubmitGPUCommandBuffer(cmd); SDL_SubmitGPUCommandBuffer(cmd);
SDL_ReleaseGPUTransferBuffer(v->GpuDevice, font_transferbuffer); SDL_ReleaseGPUTransferBuffer(v->GpuDevice, font_transferbuffer);
@@ -359,28 +352,26 @@ bool ImGui_ImplSDLGPU_CreateFontsTexture()
return true; return true;
} }
// You probably never need to call this, as it is called by ImGui_ImplSDLGPU_CreateFontsTexture() and ImGui_ImplSDLGPU_Shutdown(). // You probably never need to call this, as it is called by ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_Shutdown().
void ImGui_ImplSDLGPU_DestroyFontsTexture() void ImGui_ImplSDLGPU3_DestroyFontsTexture()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
if (bd->FontTexture)
if(bd->FontTexture)
{ {
SDL_ReleaseGPUTexture(v->GpuDevice,bd->FontTexture); SDL_ReleaseGPUTexture(v->GpuDevice, bd->FontTexture);
bd->FontBinding.texture = nullptr; bd->FontBinding.texture = nullptr;
bd->FontTexture = nullptr; bd->FontTexture = nullptr;
} }
io.Fonts->SetTexID(0); io.Fonts->SetTexID(0);
} }
static void Imgui_ImplSDLGPU_CreateShaders() static void Imgui_ImplSDLGPU3_CreateShaders()
{ {
// Create the shader modules // Create the shader modules
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
const char* driver = SDL_GetGPUDeviceDriver(v->GpuDevice); 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_storage_textures = 0;
fragment_shader_info.num_uniform_buffers = 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.format = SDL_GPU_SHADERFORMAT_SPIRV;
vertex_shader_info.code = spirv_vertex; vertex_shader_info.code = spirv_vertex;
vertex_shader_info.code_size = sizeof(spirv_vertex); vertex_shader_info.code_size = sizeof(spirv_vertex);
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV; fragment_shader_info.format = SDL_GPU_SHADERFORMAT_SPIRV;
fragment_shader_info.code = spirv_fragment; fragment_shader_info.code = spirv_fragment;
fragment_shader_info.code_size = sizeof(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.format = SDL_GPU_SHADERFORMAT_DXBC;
vertex_shader_info.code = dxbc_vertex; vertex_shader_info.code = dxbc_vertex;
vertex_shader_info.code_size = sizeof(dxbc_vertex); vertex_shader_info.code_size = sizeof(dxbc_vertex);
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC; fragment_shader_info.format = SDL_GPU_SHADERFORMAT_DXBC;
fragment_shader_info.code = dxbc_fragment; fragment_shader_info.code = dxbc_fragment;
fragment_shader_info.code_size = sizeof(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.format = SDL_GPU_SHADERFORMAT_METALLIB;
vertex_shader_info.code = metallib_vertex; vertex_shader_info.code = metallib_vertex;
vertex_shader_info.code_size = sizeof(metallib_vertex); vertex_shader_info.code_size = sizeof(metallib_vertex);
fragment_shader_info.entrypoint = "main0"; fragment_shader_info.entrypoint = "main0";
fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB; fragment_shader_info.format = SDL_GPU_SHADERFORMAT_METALLIB;
fragment_shader_info.code = metallib_fragment; fragment_shader_info.code = metallib_fragment;
fragment_shader_info.code_size = sizeof(metallib_fragment); fragment_shader_info.code_size = sizeof(metallib_fragment);
} }
#endif #endif
bd->VertexShader = SDL_CreateGPUShader(v->GpuDevice,&vertex_shader_info); bd->VertexShader = SDL_CreateGPUShader(v->GpuDevice, &vertex_shader_info);
bd->FragmentShader = SDL_CreateGPUShader(v->GpuDevice,&fragment_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->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"); 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_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
Imgui_ImplSDLGPU_CreateShaders(); Imgui_ImplSDLGPU3_CreateShaders();
SDL_GPUVertexBufferDescription vertex_buffer_desc[1]; SDL_GPUVertexBufferDescription vertex_buffer_desc[1];
vertex_buffer_desc[0].slot = 0; 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.depth_stencil_state = depth_stencil_state;
pipeline_info.target_info = target_info; 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"); 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_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
if (!bd->FontSampler) if (!bd->FontSampler)
{ {
@@ -546,53 +533,53 @@ bool ImGui_ImplSDLGPU_CreateDeviceObjects()
sampler_info.max_anisotropy = 1.0f; sampler_info.max_anisotropy = 1.0f;
sampler_info.enable_compare = false; 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; bd->FontBinding.sampler = bd->FontSampler;
IM_ASSERT(bd->FontSampler != nullptr && "Failed to create font sampler, call SDL_GetError() for more information"); IM_ASSERT(bd->FontSampler != nullptr && "Failed to create font sampler, call SDL_GetError() for more information");
} }
ImGui_ImplSDLGPU_CreateGraphicsPipeline(); ImGui_ImplSDLGPU3_CreateGraphicsPipeline();
return true; return true;
} }
void ImGui_ImplSDLGPU_DestroyFrameData() void ImGui_ImplSDLGPU3_DestroyFrameData()
{ {
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
SDL_ReleaseGPUBuffer(v->GpuDevice,bd->MainWindowFrameData.VertexBuffer); SDL_ReleaseGPUBuffer(v->GpuDevice, bd->MainWindowFrameData.VertexBuffer);
SDL_ReleaseGPUBuffer(v->GpuDevice,bd->MainWindowFrameData.IndexBuffer); SDL_ReleaseGPUBuffer(v->GpuDevice, bd->MainWindowFrameData.IndexBuffer);
bd->MainWindowFrameData.VertexBuffer = nullptr; bd->MainWindowFrameData.VertexBuffer = nullptr;
bd->MainWindowFrameData.IndexBuffer = nullptr; bd->MainWindowFrameData.IndexBuffer = nullptr;
bd->MainWindowFrameData.VertexBufferSize = 0; bd->MainWindowFrameData.VertexBufferSize = 0;
bd->MainWindowFrameData.IndexBufferSize = 0; bd->MainWindowFrameData.IndexBufferSize = 0;
} }
void ImGui_ImplSDLGPU_DestroyDeviceObjects() void ImGui_ImplSDLGPU3_DestroyDeviceObjects()
{ {
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU_InitInfo* v = &bd->GPUInitInfo; ImGui_ImplSDLGPU3_InitInfo* v = &bd->GPUInitInfo;
ImGui_ImplSDLGPU_DestroyFrameData(); ImGui_ImplSDLGPU3_DestroyFrameData();
ImGui_ImplSDLGPU_DestroyFontsTexture(); ImGui_ImplSDLGPU3_DestroyFontsTexture();
if(bd->VertexShader) { SDL_ReleaseGPUShader(v->GpuDevice,bd->VertexShader); bd->VertexShader = 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->FragmentShader) { SDL_ReleaseGPUShader(v->GpuDevice, bd->FragmentShader); bd->FragmentShader = nullptr;}
if(bd->FontSampler) { SDL_ReleaseGPUSampler(v->GpuDevice, bd->FontSampler); bd->FontSampler = 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->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(); ImGuiIO& io = ImGui::GetIO();
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
// Setup backend capabilities flags // 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.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. io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
IM_ASSERT(info->GpuDevice != nullptr); IM_ASSERT(info->GpuDevice != nullptr);
@@ -600,31 +587,31 @@ bool ImGui_ImplSDLGPU_Init(ImGui_ImplSDLGPU_InitInfo* info)
bd->GPUInitInfo = *info; bd->GPUInitInfo = *info;
ImGui_ImplSDLGPU_CreateDeviceObjects(); ImGui_ImplSDLGPU3_CreateDeviceObjects();
return true; 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?"); IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLGPU_DestroyDeviceObjects(); ImGui_ImplSDLGPU3_DestroyDeviceObjects();
io.BackendRendererName = nullptr; io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr; io.BackendRendererUserData = nullptr;
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
IM_DELETE(bd); IM_DELETE(bd);
} }
void ImGui_ImplSDLGPU_NewFrame() void ImGui_ImplSDLGPU3_NewFrame()
{ {
ImGui_ImplSDLGPU_Data* bd = ImGui_ImplSDLGPU_GetBackendData(); ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplVulkan_Init()?"); IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLGPU3_Init()?");
if(!bd->FontTexture) if (!bd->FontTexture)
ImGui_ImplSDLGPU_CreateFontsTexture(); ImGui_ImplSDLGPU3_CreateFontsTexture();
} }
#endif // #ifndef IMGUI_DISABLE #endif // #ifndef IMGUI_DISABLE

View File

@@ -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 // This needs to be used along with the SDL3 Platform Backend
// Implemented features: // Implemented features:
@@ -27,7 +27,7 @@
// Initialization data, for ImGui_ImplSDLGPU_Init() // 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 // - 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_GPUDevice* GpuDevice = nullptr;
SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID; 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! // 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 bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info);
IMGUI_IMPL_API void ImGui_ImplSDLGPU_Shutdown(); IMGUI_IMPL_API void ImGui_ImplSDLGPU3_Shutdown();
IMGUI_IMPL_API void ImGui_ImplSDLGPU_NewFrame(); IMGUI_IMPL_API void ImGui_ImplSDLGPU3_NewFrame();
IMGUI_IMPL_API void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer); IMGUI_IMPL_API void Imgui_ImplSDLGPU3_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 void ImGui_ImplSDLGPU3_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 bool ImGui_ImplSDLGPU3_CreateFontsTexture();
IMGUI_IMPL_API void ImGui_ImplSDLGPU_DestroyFontsTexture(); IMGUI_IMPL_API void ImGui_ImplSDLGPU3_DestroyFontsTexture();
#endif // #ifndef IMGUI_DISABLE #endif // #ifndef IMGUI_DISABLE

View File

@@ -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: 1) Compile the raw shader files to SPIRV:
glslc -o vertex.spv -c shader.vert glslc -o vertex.spv -c shader.vert
@@ -33,4 +37,4 @@
Proceed to step 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 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.

View File

@@ -3,7 +3,8 @@ layout(location = 0) out vec4 fColor;
layout(set=2, binding=0) uniform sampler2D sTexture; layout(set=2, binding=0) uniform sampler2D sTexture;
layout(location = 0) in struct { layout(location = 0) in struct
{
vec4 Color; vec4 Color;
vec2 UV; vec2 UV;
} In; } In;

View File

@@ -3,12 +3,14 @@ layout(location = 0) in vec2 aPos;
layout(location = 1) in vec2 aUV; layout(location = 1) in vec2 aUV;
layout(location = 2) in vec4 aColor; layout(location = 2) in vec4 aColor;
layout(set=1,binding=0) uniform UBO { layout(set=1,binding=0) uniform UBO
{
vec2 uScale; vec2 uScale;
vec2 uTranslate; vec2 uTranslate;
} ubo; } ubo;
layout(location = 0) out struct { layout(location = 0) out struct
{
vec4 Color; vec4 Color;
vec2 UV; vec2 UV;
} Out; } Out;

View File

@@ -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)

View File

@@ -68,6 +68,7 @@ Other changes:
- Demo: Added label edition to Property Editor demo + fix an ID issue. (#8266) [@moritz-h] - 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 misc/cpp/imgui_stdlib.h/.cpp not supporting IMGUI_DISABLE. (#8294) [@juur]
- Misc: Fixed MinGW builds uses UTF-8 friendly _wfopen(). (#8300) - 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 - Backends: Allegro5: Avoid calling al_set_mouse_cursor() repeatedly since it appears
to leak on on X11 (#8256). [@Helodity] to leak on on X11 (#8256). [@Helodity]
- Backends: Metal: Fixed leaks when using metal-cpp. (#8276, #8166) [@selimsandal] - Backends: Metal: Fixed leaks when using metal-cpp. (#8276, #8166) [@selimsandal]

View File

@@ -139,8 +139,8 @@ This support building with Emscripten and targeting WebGL.<BR>
Prefer using that if you are using modern GL or WebGL in your application. 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/) <BR> [example_sdl2_sdlrenderer2/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_sdlrenderer2/) <BR>
SDL2 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL2 (most graphics backends are supported underneath) <BR> SDL2 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL2 example.<BR>
= main.cpp + imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer.cpp <BR> = main.cpp + imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp <BR>
This requires SDL 2.0.18+ (released November 2021) <BR> This requires SDL 2.0.18+ (released November 2021) <BR>
[example_sdl2_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_vulkan/) <BR> [example_sdl2_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_vulkan/) <BR>
@@ -149,6 +149,26 @@ SDL2 (Win32, Mac, Linux, etc.) + Vulkan example. <BR>
This is quite long and tedious, because: Vulkan. <BR> This is quite long and tedious, because: Vulkan. <BR>
For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp. 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/) <BR>
SDL3 (Win32, Mac, Linux, etc.) + OpenGL3+/ES2/ES3 example. <BR>
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_opengl3.cpp <BR>
This uses more modern GL calls and custom shaders. <BR>
This support building with Emscripten and targeting WebGL.<BR>
[example_sdl3_sdlgpu3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_sdlgpu3/) <BR>
SDL3 (Win32, Mac, Linux, etc.) + SDL_GPU for SDL3 example.<BR>
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_sdlrenderer3.cpp <BR>
[example_sdl3_sdlrenderer3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_sdlrenderer3/) <BR>
SDL3 (Win32, Mac, Linux, etc.) + SDL_Renderer for SDL3 example.<BR>
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_sdlrenderer3.cpp <BR>
[example_sdl3_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_vulkan/) <BR>
SDL3 (Win32, Mac, Linux, etc.) + Vulkan example. <BR>
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_vulkan.cpp <BR>
This is quite long and tedious, because: Vulkan. <BR>
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/) <BR> [example_win32_directx9/](https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx9/) <BR>
DirectX9 example, Windows only. <BR> DirectX9 example, Windows only. <BR>
= main.cpp + imgui_impl_win32.cpp + imgui_impl_dx9.cpp = main.cpp + imgui_impl_win32.cpp + imgui_impl_dx9.cpp

View File

@@ -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!** 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): 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. - Platforms: GLFW, SDL2/SDL3, Win32, Glut, OSX, Android.
- Frameworks: Allegro5, Emscripten. - Frameworks: Allegro5, Emscripten.

View File

@@ -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: // Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq // - FAQ https://dearimgui.com/faq
@@ -17,7 +17,6 @@
#include <stdlib.h> // abort #include <stdlib.h> // abort
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
// This example doesn't compile with Emscripten yet! Awaiting SDL3 support. // This example doesn't compile with Emscripten yet! Awaiting SDL3 support.
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
#include "../libs/emscripten/emscripten_mainloop_stub.h" #include "../libs/emscripten/emscripten_mainloop_stub.h"
@@ -34,23 +33,23 @@ int main(int, char**)
} }
// Create SDL window graphics context // 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) if (window == nullptr)
{ {
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1; return -1;
} }
// Create SDL Gpu Device // Create GPU Device
SDL_GPUDevice* gpuDevice = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB,true,nullptr); SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB,true,nullptr);
if (gpuDevice == nullptr) if (gpu_device == nullptr)
{ {
printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError()); printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError());
return -1; return -1;
} }
// Claim window for GPU Device // Claim window for GPU Device
if (!SDL_ClaimWindowForGPUDevice(gpuDevice, window)) if (!SDL_ClaimWindowForGPUDevice(gpu_device, window))
{ {
printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError()); printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError());
return -1; return -1;
@@ -69,11 +68,11 @@ int main(int, char**)
// Setup Platform/Renderer backends // Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForOther(window); ImGui_ImplSDL3_InitForOther(window);
ImGui_ImplSDLGPU_InitInfo init_info = {}; ImGui_ImplSDLGPU3_InitInfo init_info = {};
init_info.GpuDevice = gpuDevice; init_info.GpuDevice = gpu_device;
init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpuDevice, window); init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window);
init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1;
ImGui_ImplSDLGPU_Init(&init_info); ImGui_ImplSDLGPU3_Init(&init_info);
// Load Fonts // 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. // - 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.
@@ -121,7 +120,7 @@ int main(int, char**)
} }
// Start the Dear ImGui frame // Start the Dear ImGui frame
ImGui_ImplSDLGPU_NewFrame(); ImGui_ImplSDLGPU3_NewFrame();
ImGui_ImplSDL3_NewFrame(); ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
@@ -167,18 +166,15 @@ int main(int, char**)
ImDrawData* draw_data = ImGui::GetDrawData(); ImDrawData* draw_data = ImGui::GetDrawData();
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); 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(gpu_device); // Acquire a GPU command buffer
SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpuDevice);
//Acquire a swapchain texture
SDL_GPUTexture* 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) if (swapchain_texture != nullptr && !is_minimized)
{ {
// !!! THIS IS MANDATORY !!! // This is mandatory: call Imgui_ImplSDLGPU3_PrepareDrawData() to upload the vertex/index buffer!
// Call Imgui_ImplSDLGPU_PrepareDrawData to upload the vertex/index buffer Imgui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer);
Imgui_ImplSDLGPU_PrepareDrawData(draw_data, command_buffer);
// Setup and start a render pass // Setup and start a render pass
SDL_GPUColorTargetInfo target_info = {}; SDL_GPUColorTargetInfo target_info = {};
@@ -190,21 +186,26 @@ int main(int, char**)
target_info.layer_or_depth_plane = 0; target_info.layer_or_depth_plane = 0;
target_info.cycle = false; target_info.cycle = false;
SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr); 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); SDL_EndGPURenderPass(render_pass);
} }
// Submit the command buffer // Submit the command buffer
SDL_SubmitGPUCommandBuffer(command_buffer); SDL_SubmitGPUCommandBuffer(command_buffer);
} }
// Cleanup // Cleanup
SDL_WaitForGPUIdle(gpuDevice); SDL_WaitForGPUIdle(gpu_device);
ImGui_ImplSDL3_Shutdown(); ImGui_ImplSDL3_Shutdown();
ImGui_ImplSDLGPU_Shutdown(); ImGui_ImplSDLGPU3_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();
SDL_ReleaseWindowFromGPUDevice(gpuDevice, window);
SDL_DestroyGPUDevice(gpuDevice); SDL_ReleaseWindowFromGPUDevice(gpu_device, window);
SDL_DestroyGPUDevice(gpu_device);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }