Skip to content

Commit

Permalink
Backends: SDLGPU3: Added sdl_gpu backend. (ocornut#8163, ocornut#7998,
Browse files Browse the repository at this point in the history
…ocornut#7988)

+Squashed: Optimized shader source code encoding by ocornut (ocornut#8163, ocornut#7998, ocornut#7988)
(squashed to avoid storing both in git history, 130 KB->62 KB)
  • Loading branch information
DeltaW0x authored and ocornut committed Jan 9, 2025
1 parent 940d954 commit 8bbccf7
Show file tree
Hide file tree
Showing 12 changed files with 1,676 additions and 0 deletions.
630 changes: 630 additions & 0 deletions backends/imgui_impl_sdlgpu3.cpp

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions backends/imgui_impl_sdlgpu3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// dear imgui: Renderer Backend for SDL_Gpu
// This needs to be used along with the SDL3 Platform Backend

// Implemented features:
// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.

// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/

// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - 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.
// 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.

#pragma once
#include "imgui.h" // IMGUI_IMPL_API
#ifndef IMGUI_DISABLE
#include <SDL3/SDL_gpu.h>

// 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
{
SDL_GPUDevice* GpuDevice = nullptr;
SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID;
SDL_GPUSampleCount MSAASamples = SDL_GPU_SAMPLECOUNT_1;
};

// 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();

#endif // #ifndef IMGUI_DISABLE
372 changes: 372 additions & 0 deletions backends/imgui_impl_sdlgpu3_shaders.h

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions backends/sdlgpu3/build_instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1) Compile the raw shader files to SPIRV:

glslc -o vertex.spv -c shader.vert
glslc -o fragment.spv -c shader.frag


2) Build SDL_shadercross (https://github.com/libsdl-org/SDL_shadercross)


3-A) Compiling for the Vulkan Driver:

Nothing to do, you just need the previous vertex.spv/fragment.spv, proceed to step 4


3-B) Compiling for the DirectX 12 Driver:

./shadercross vertex.spv -s SPIRV -d DXBC -t vertex -e main -o vertex.dxbc
./shadercross fragment.spv -s SPIRV -d DXBC -t fragment -e main -o fragment.dxbc

Proceed to step 4


3-C) Compiling for Metal (On windows you'll need the Metal Developer Tools for Windows, on linux you might use wine, but I never tested it):

./shadercross vertex.spv -s SPIRV -d MSL -t vertex -e main -o vertex.metal
./shadercross fragment.spv -s SPIRV -d MSL -t fragment -e main -o fragment.metal

xcrun -sdk macosx metal -o vertex.ir -c vertex.metal
xcrun -sdk macosx metal -o fragment.ir -c fragment.metal
xcrun -sdk macosx metallib -o vertex.metallib -c vertex.ir
xcrun -sdk macosx metallib -o fragment.metallib -c fragment.ir

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
14 changes: 14 additions & 0 deletions backends/sdlgpu3/shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450 core
layout(location = 0) out vec4 fColor;

layout(set=2, binding=0) uniform sampler2D sTexture;

layout(location = 0) in struct {
vec4 Color;
vec2 UV;
} In;

void main()
{
fColor = In.Color * texture(sTexture, In.UV.st);
}
22 changes: 22 additions & 0 deletions backends/sdlgpu3/shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#version 450 core
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 {
vec2 uScale;
vec2 uTranslate;
} ubo;

layout(location = 0) out struct {
vec4 Color;
vec2 UV;
} Out;

void main()
{
Out.Color = aColor;
Out.UV = aUV;
gl_Position = vec4(aPos * ubo.uScale + ubo.uTranslate, 0, 1);
gl_Position.y *= -1.0f;
}
73 changes: 73 additions & 0 deletions examples/example_sdl3_sdlgpu3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Cross Platform Makefile
# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
#
# You will need SDL3 (http://www.libsdl.org) which is still unreleased/unpackaged.

#CXX = g++
#CXX = clang++

EXE = example_sdl3_sdlgpu3
IMGUI_DIR = ../..
SOURCES = main.cpp
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_sdlgpu3.cpp
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
UNAME_S := $(shell uname -s)

CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS += -g -Wall -Wformat
LIBS =


##---------------------------------------------------------------------
## BUILD FLAGS PER PLATFORM
##---------------------------------------------------------------------

ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
LIBS += -ldl `pkg-config sdl3 --libs`

CXXFLAGS += `pkg-config sdl3 --cflags`
CFLAGS = $(CXXFLAGS)
endif

ifeq ($(UNAME_S), Darwin) #APPLE
ECHO_MESSAGE = "Mac OS X"
LIBS += -framework Cocoa -framework IOKit -framework CoreVideo `pkg-config --libs sdl3`
LIBS += -L/usr/local/lib -L/opt/local/lib

CXXFLAGS += `pkg-config sdl3 --cflags`
CXXFLAGS += -I/usr/local/include -I/opt/local/include
CFLAGS = $(CXXFLAGS)
endif

ifeq ($(OS), Windows_NT)
ECHO_MESSAGE = "MinGW"
LIBS += -lgdi32 -limm32 `pkg-config --static --libs sdl3`

CXXFLAGS += `pkg-config --cflags sdl3`
CFLAGS = $(CXXFLAGS)
endif

##---------------------------------------------------------------------
## BUILD RULES
##---------------------------------------------------------------------

%.o:%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(IMGUI_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(IMGUI_DIR)/backends/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<

all: $(EXE)
@echo Build complete for $(ECHO_MESSAGE)

$(EXE): $(OBJS)
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)

clean:
rm -f $(EXE) $(OBJS)
14 changes: 14 additions & 0 deletions examples/example_sdl3_sdlgpu3/build_win64.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@REM Build for Visual Studio compiler. Run your copy of vcvars64.bat or vcvarsall.bat to setup command-line compiler.

@set OUT_EXE=example_sdl3_sdlgpu3
@set INCLUDES=/I..\.. /I..\..\backends /I%SDL3_DIR%\include
@set SOURCES=main.cpp ..\..\backends\imgui_impl_sdl3.cpp ..\..\backends\imgui_impl_sdlgpu3.cpp ..\..\imgui*.cpp
@set LIBS=/LIBPATH:%SDL3_DIR%\lib\x64 SDL3.lib shell32.lib

@set OUT_DIR=Debug
mkdir %OUT_DIR%
cl /nologo /Zi /MD /utf-8 %INCLUDES% %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% /subsystem:console

@set OUT_DIR=Release
@REM mkdir %OUT_DIR%
@REM cl /nologo /Zi /MD /utf-8 /Ox /Oi %INCLUDES% %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% /subsystem:console
Loading

0 comments on commit 8bbccf7

Please sign in to comment.