Skip to content

Commit f4e19a6

Browse files
committed
Bug: tessellation shaders not loading when separate files
1 parent f9d60b3 commit f4e19a6

File tree

7 files changed

+97
-4
lines changed

7 files changed

+97
-4
lines changed

demosys/loaders/program/separate.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def load(self):
1717
vs_source,
1818
geometry_source=geo_source,
1919
fragment_source=fs_source,
20-
tess_control_shader=tc_source,
21-
tess_evaluation_shader=te_source,
20+
tess_control_source=tc_source,
21+
tess_evaluation_source=te_source,
2222
)
2323
prog = shaders.create()
2424

demosys/opengl/program.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def from_single(cls, meta: ProgramDescription, source: str):
7070

7171
@classmethod
7272
def from_separate(cls, meta: ProgramDescription, vertex_source, geometry_source=None, fragment_source=None,
73-
tess_control_shader=None, tess_evaluation_shader=None):
73+
tess_control_source=None, tess_evaluation_source=None):
7474
"""Initialize multiple shader strings"""
7575
instance = cls(meta)
7676
instance.vertex_source = ShaderSource(
@@ -93,6 +93,20 @@ def from_separate(cls, meta: ProgramDescription, vertex_source, geometry_source=
9393
fragment_source,
9494
)
9595

96+
if tess_control_source:
97+
instance.tess_control_source = ShaderSource(
98+
TESS_CONTROL_SHADER,
99+
meta.path or meta.tess_control_shader,
100+
tess_control_source,
101+
)
102+
103+
if tess_evaluation_source:
104+
instance.tess_evaluation_source = ShaderSource(
105+
TESS_EVALUATION_SHADER,
106+
meta.path or meta.tess_control_shader,
107+
tess_evaluation_source,
108+
)
109+
96110
return instance
97111

98112
def create(self):

examples/terrain/dependencies.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
effect_packages = []
44

55
resources = [
6-
ProgramDescription(label="terrain", path="terrain/terrain.glsl"),
6+
# Program in one single file
7+
# ProgramDescription(label="terrain", path="terrain/terrain.glsl"),
8+
9+
# Program split into separate shader files
10+
ProgramDescription(
11+
label="terrain",
12+
vertex_shader="terrain/terrain_vs.glsl",
13+
tess_control_shader="terrain/terrain_tc.glsl",
14+
tess_evaluation_shader="terrain/terrain_te.glsl",
15+
fragment_shader="terrain/terrain_fs.glsl",
16+
),
717
TextureDescription(label="heightmap", path="terrain/Ridges_01_DISP.jpg"),
818
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 410
2+
3+
out vec4 fragColor;
4+
in vec2 ts_uv;
5+
uniform sampler2D heightmap;
6+
7+
void main() {
8+
float color = texture(heightmap, ts_uv).r;
9+
fragColor = vec4(color);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 410
2+
3+
layout(vertices = 3) out;
4+
5+
in vec3 v_position[];
6+
out vec3 tc_position[];
7+
8+
uniform float TessLevelInner;
9+
uniform float TessLevelOuter;
10+
11+
void main() {
12+
tc_position[gl_InvocationID] = v_position[gl_InvocationID];
13+
if (gl_InvocationID == 0) {
14+
// Resolution inside the triangle
15+
gl_TessLevelInner[0] = TessLevelInner;
16+
17+
// How many segments each edge of the triagle should be split into
18+
gl_TessLevelOuter[0] = TessLevelOuter;
19+
gl_TessLevelOuter[1] = TessLevelOuter;
20+
gl_TessLevelOuter[2] = TessLevelOuter;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#version 410
2+
3+
layout(triangles, equal_spacing, ccw) in;
4+
5+
in vec3 tc_position[];
6+
out vec2 ts_uv;
7+
8+
uniform mat4 m_mv;
9+
uniform mat4 m_proj;
10+
uniform sampler2D heightmap;
11+
12+
void main() {
13+
// gl_TessCoord is barycentric coordinates inside the triangle representing the new vertex positions
14+
// We multiply that with the three vertex position to get the new vertex position
15+
vec3 p0 = gl_TessCoord.x * tc_position[0];
16+
vec3 p1 = gl_TessCoord.y * tc_position[1];
17+
vec3 p2 = gl_TessCoord.z * tc_position[2];
18+
19+
// Add them together to get the actual world posision
20+
vec3 pos = p0 + p1 + p2;
21+
22+
// Read the height from the heightmap
23+
vec2 uv = vec2(0.5, 0.5) + vec2(pos.xz) / 400.0;
24+
float height = texture(heightmap, uv).r;
25+
26+
// Emit new vertex
27+
gl_Position = m_proj * m_mv * vec4(vec3(pos.x, height * 25.0, pos.z), 1.0);
28+
ts_uv = uv;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 410
2+
3+
in vec3 in_position;
4+
out vec3 v_position;
5+
6+
void main() {
7+
v_position = in_position.xyz;
8+
}

0 commit comments

Comments
 (0)