Skip to content

Commit 9c054b1

Browse files
committed
SDL_GPU Shader and Instructions
Added source sharder files for the SDL_GPU Backend and instructions on how to compile them
1 parent 96a260e commit 9c054b1

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
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 (https://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 https://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+
}

0 commit comments

Comments
 (0)