Skip to content

Commit 8bbccf7

Browse files
DeltaW0xocornut
authored andcommitted
Backends: SDLGPU3: Added sdl_gpu backend. (ocornut#8163, ocornut#7998, 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)
1 parent 940d954 commit 8bbccf7

12 files changed

+1676
-0
lines changed

backends/imgui_impl_sdlgpu3.cpp

Lines changed: 630 additions & 0 deletions
Large diffs are not rendered by default.

backends/imgui_impl_sdlgpu3.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// dear imgui: Renderer Backend for SDL_Gpu
2+
// This needs to be used along with the SDL3 Platform Backend
3+
4+
// Implemented features:
5+
// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
6+
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
7+
8+
// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
9+
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at http://github.com/ocornut/imgui/
10+
11+
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
12+
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
13+
// Learn about Dear ImGui:
14+
// - FAQ http://dearimgui.com/faq
15+
// - Getting Started http://dearimgui.com/getting-started
16+
// - Documentation http://dearimgui.com/docs (same as your local docs/ folder).
17+
// - Introduction, links and more at the top of imgui.cpp
18+
19+
// Important note to the reader who wish to integrate imgui_impl_sdlgpu3.cpp/.h in their own engine/app.
20+
// - Unline other backends, the user must call the function Imgui_ImplSDLGPU_PrepareDrawData BEFORE issuing a SDL_GPURenderPass containing ImGui_ImplSDLGPU_RenderDrawData.
21+
// 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.
22+
23+
#pragma once
24+
#include "imgui.h" // IMGUI_IMPL_API
25+
#ifndef IMGUI_DISABLE
26+
#include <SDL3/SDL_gpu.h>
27+
28+
// Initialization data, for ImGui_ImplSDLGPU_Init()
29+
// - Remember to set ColorTargetFormat to the correct format. If you're rendering to the swapchain, call SDL_GetGPUSwapchainTextureFormat to query the right value
30+
struct ImGui_ImplSDLGPU_InitInfo
31+
{
32+
SDL_GPUDevice* GpuDevice = nullptr;
33+
SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID;
34+
SDL_GPUSampleCount MSAASamples = SDL_GPU_SAMPLECOUNT_1;
35+
};
36+
37+
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
38+
IMGUI_IMPL_API bool ImGui_ImplSDLGPU_Init(ImGui_ImplSDLGPU_InitInfo* info);
39+
IMGUI_IMPL_API void ImGui_ImplSDLGPU_Shutdown();
40+
IMGUI_IMPL_API void ImGui_ImplSDLGPU_NewFrame();
41+
IMGUI_IMPL_API void Imgui_ImplSDLGPU_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer);
42+
IMGUI_IMPL_API void ImGui_ImplSDLGPU_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline = nullptr);
43+
IMGUI_IMPL_API bool ImGui_ImplSDLGPU_CreateFontsTexture();
44+
IMGUI_IMPL_API void ImGui_ImplSDLGPU_DestroyFontsTexture();
45+
46+
#endif // #ifndef IMGUI_DISABLE

backends/imgui_impl_sdlgpu3_shaders.h

Lines changed: 372 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1) Compile the raw shader files to SPIRV:
2+
3+
glslc -o vertex.spv -c shader.vert
4+
glslc -o fragment.spv -c shader.frag
5+
6+
7+
2) Build SDL_shadercross (http://github.com/libsdl-org/SDL_shadercross)
8+
9+
10+
3-A) Compiling for the Vulkan Driver:
11+
12+
Nothing to do, you just need the previous vertex.spv/fragment.spv, proceed to step 4
13+
14+
15+
3-B) Compiling for the DirectX 12 Driver:
16+
17+
./shadercross vertex.spv -s SPIRV -d DXBC -t vertex -e main -o vertex.dxbc
18+
./shadercross fragment.spv -s SPIRV -d DXBC -t fragment -e main -o fragment.dxbc
19+
20+
Proceed to step 4
21+
22+
23+
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):
24+
25+
./shadercross vertex.spv -s SPIRV -d MSL -t vertex -e main -o vertex.metal
26+
./shadercross fragment.spv -s SPIRV -d MSL -t fragment -e main -o fragment.metal
27+
28+
xcrun -sdk macosx metal -o vertex.ir -c vertex.metal
29+
xcrun -sdk macosx metal -o fragment.ir -c fragment.metal
30+
xcrun -sdk macosx metallib -o vertex.metallib -c vertex.ir
31+
xcrun -sdk macosx metallib -o fragment.metallib -c fragment.ir
32+
33+
Proceed to step 4
34+
35+
36+
4) Either find a way to load the shader bytecode from file, or use a tool like http://notisrac.github.io/FileToCArray/ to convert the file to a uint8_t array

backends/sdlgpu3/shader.frag

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 450 core
2+
layout(location = 0) out vec4 fColor;
3+
4+
layout(set=2, binding=0) uniform sampler2D sTexture;
5+
6+
layout(location = 0) in struct {
7+
vec4 Color;
8+
vec2 UV;
9+
} In;
10+
11+
void main()
12+
{
13+
fColor = In.Color * texture(sTexture, In.UV.st);
14+
}

backends/sdlgpu3/shader.vert

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 450 core
2+
layout(location = 0) in vec2 aPos;
3+
layout(location = 1) in vec2 aUV;
4+
layout(location = 2) in vec4 aColor;
5+
6+
layout(set=1,binding=0) uniform UBO {
7+
vec2 uScale;
8+
vec2 uTranslate;
9+
} ubo;
10+
11+
layout(location = 0) out struct {
12+
vec4 Color;
13+
vec2 UV;
14+
} Out;
15+
16+
void main()
17+
{
18+
Out.Color = aColor;
19+
Out.UV = aUV;
20+
gl_Position = vec4(aPos * ubo.uScale + ubo.uTranslate, 0, 1);
21+
gl_Position.y *= -1.0f;
22+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#
2+
# Cross Platform Makefile
3+
# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
4+
#
5+
# You will need SDL3 (http://www.libsdl.org) which is still unreleased/unpackaged.
6+
7+
#CXX = g++
8+
#CXX = clang++
9+
10+
EXE = example_sdl3_sdlgpu3
11+
IMGUI_DIR = ../..
12+
SOURCES = main.cpp
13+
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
14+
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_sdlgpu3.cpp
15+
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
16+
UNAME_S := $(shell uname -s)
17+
18+
CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
19+
CXXFLAGS += -g -Wall -Wformat
20+
LIBS =
21+
22+
23+
##---------------------------------------------------------------------
24+
## BUILD FLAGS PER PLATFORM
25+
##---------------------------------------------------------------------
26+
27+
ifeq ($(UNAME_S), Linux) #LINUX
28+
ECHO_MESSAGE = "Linux"
29+
LIBS += -ldl `pkg-config sdl3 --libs`
30+
31+
CXXFLAGS += `pkg-config sdl3 --cflags`
32+
CFLAGS = $(CXXFLAGS)
33+
endif
34+
35+
ifeq ($(UNAME_S), Darwin) #APPLE
36+
ECHO_MESSAGE = "Mac OS X"
37+
LIBS += -framework Cocoa -framework IOKit -framework CoreVideo `pkg-config --libs sdl3`
38+
LIBS += -L/usr/local/lib -L/opt/local/lib
39+
40+
CXXFLAGS += `pkg-config sdl3 --cflags`
41+
CXXFLAGS += -I/usr/local/include -I/opt/local/include
42+
CFLAGS = $(CXXFLAGS)
43+
endif
44+
45+
ifeq ($(OS), Windows_NT)
46+
ECHO_MESSAGE = "MinGW"
47+
LIBS += -lgdi32 -limm32 `pkg-config --static --libs sdl3`
48+
49+
CXXFLAGS += `pkg-config --cflags sdl3`
50+
CFLAGS = $(CXXFLAGS)
51+
endif
52+
53+
##---------------------------------------------------------------------
54+
## BUILD RULES
55+
##---------------------------------------------------------------------
56+
57+
%.o:%.cpp
58+
$(CXX) $(CXXFLAGS) -c -o $@ $<
59+
60+
%.o:$(IMGUI_DIR)/%.cpp
61+
$(CXX) $(CXXFLAGS) -c -o $@ $<
62+
63+
%.o:$(IMGUI_DIR)/backends/%.cpp
64+
$(CXX) $(CXXFLAGS) -c -o $@ $<
65+
66+
all: $(EXE)
67+
@echo Build complete for $(ECHO_MESSAGE)
68+
69+
$(EXE): $(OBJS)
70+
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
71+
72+
clean:
73+
rm -f $(EXE) $(OBJS)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@REM Build for Visual Studio compiler. Run your copy of vcvars64.bat or vcvarsall.bat to setup command-line compiler.
2+
3+
@set OUT_EXE=example_sdl3_sdlgpu3
4+
@set INCLUDES=/I..\.. /I..\..\backends /I%SDL3_DIR%\include
5+
@set SOURCES=main.cpp ..\..\backends\imgui_impl_sdl3.cpp ..\..\backends\imgui_impl_sdlgpu3.cpp ..\..\imgui*.cpp
6+
@set LIBS=/LIBPATH:%SDL3_DIR%\lib\x64 SDL3.lib shell32.lib
7+
8+
@set OUT_DIR=Debug
9+
mkdir %OUT_DIR%
10+
cl /nologo /Zi /MD /utf-8 %INCLUDES% %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% /subsystem:console
11+
12+
@set OUT_DIR=Release
13+
@REM mkdir %OUT_DIR%
14+
@REM cl /nologo /Zi /MD /utf-8 /Ox /Oi %INCLUDES% %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% /subsystem:console

0 commit comments

Comments
 (0)