Skip to content

Commit fbc5358

Browse files
authored
Add GPU texture and sampler support (Rust-SDL2#88)
This change adds all the bindings required to apply textures in fragment shaders with the GPU module, and a new gpu-texture example to demonstrate them.
1 parent 7997900 commit fbc5358

File tree

8 files changed

+926
-13
lines changed

8 files changed

+926
-13
lines changed

assets/texture.bmp

1.13 KB
Binary file not shown.

examples/gpu-texture.rs

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

examples/shaders/compile.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ glslc -O triangle.frag -o triangle.frag.spv
33
glslc -O triangle.vert -o triangle.vert.spv
44
glslc -O cube.frag -o cube.frag.spv
55
glslc -O cube.vert -o cube.vert.spv
6+
glslc -O cube-texture.frag -o cube-texture.frag.spv
7+
glslc -O cube-texture.vert -o cube-texture.vert.spv

examples/shaders/cube-texture.frag

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#version 450
2+
3+
// Texture sampler
4+
layout (set = 2, binding = 0) uniform sampler2D tex_sampler;
5+
6+
// Texture coordinates from the vertex shader
7+
layout (location = 0) in vec2 tex_coord;
8+
9+
// Color from our vertex shader
10+
layout (location = 1) in vec3 frag_color;
11+
12+
// Final color of the pixel
13+
layout (location = 0) out vec4 final_color;
14+
15+
void main() {
16+
final_color = texture(tex_sampler, tex_coord) * vec4(frag_color, 1.0);
17+
}
684 Bytes
Binary file not shown.

examples/shaders/cube-texture.vert

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#version 450
2+
3+
// Get the vertex position from the vertex buffer
4+
layout (location = 0) in vec3 pos;
5+
layout (location = 1) in vec2 tex_coord;
6+
7+
// Output texture coordinates to the fragment shader
8+
layout (location = 0) out vec2 out_tex_coord;
9+
10+
// Output a color to the fragment shader
11+
layout (location = 1) out vec3 frag_color;
12+
13+
// Uniforms that are pushed via SDL_PushGPUVertexUniformData
14+
layout(set = 1, binding = 0) uniform PushConstants {
15+
float rotation;
16+
};
17+
18+
// Generates an orthographic projection matrix
19+
mat4 ortho(float left, float right, float bottom, float top, float near, float far) {
20+
return mat4(
21+
2.0 / (right - left), 0, 0, 0,
22+
0, 2.0 / (top - bottom), 0, 0,
23+
// Note: this is assuming a clip space of [0, 1] on the Z axis, which is what Vulkan uses.
24+
// In OpenGL, the clip space is [-1, 1] and this would need to be adjusted.
25+
0, 0, -1.0 / (far - near), 0,
26+
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -near / (far - near), 1
27+
);
28+
}
29+
30+
// Generates a simple isometric view matrix since the program isn't
31+
// passing in a uniform view matrix. Without this, we'd just see the
32+
// front side of the cube and nothing else.
33+
mat4 isometric_view_matrix() {
34+
float angleX = radians(35.26); // Tilt
35+
float angleY = radians(rotation); // Rotate
36+
37+
mat4 rotateX = mat4(
38+
1, 0, 0, 0,
39+
0, cos(angleX), sin(angleX), 0,
40+
0, -sin(angleX), cos(angleX), 0,
41+
0, 0, 0, 1
42+
);
43+
44+
mat4 rotateY = mat4(
45+
cos(angleY), 0, -sin(angleY), 0,
46+
0, 1, 0, 0,
47+
sin(angleY), 0, cos(angleY), 0,
48+
0, 0, 0, 1
49+
);
50+
51+
return rotateX * rotateY;
52+
}
53+
54+
void main(void) {
55+
// Calculate the final vertex position by multiplying in the projection and view matrices.
56+
// Ordinarily, these matrices would be passed in as uniforms, but here they're
57+
// being calculated in-shader to avoid pulling in a matrix multiplication library.
58+
mat4 proj_matrix = ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
59+
mat4 view_matrix = isometric_view_matrix();
60+
gl_Position = proj_matrix * view_matrix * vec4(pos, 1.0);
61+
out_tex_coord = tex_coord;
62+
63+
// Create a frag color based on the vertex position
64+
frag_color = normalize(pos) * 0.5 + 0.5;
65+
}
1.67 KB
Binary file not shown.

0 commit comments

Comments
 (0)