1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(
name: "Visibility",
resources: [
(
name: "matrices",
kind: Texture(kind: Sampler2D, fallback: White),
binding: 0
),
(
name: "tileBuffer",
kind: Texture(kind: USampler2D, fallback: White),
binding: 1
),
(
name: "properties",
kind: PropertyGroup([
(name: "viewProjection", kind: Matrix4()),
(name: "tileSize", kind: Int()),
(name: "frameBufferHeight", kind: Float()),
]),
binding: 0
),
],
passes: [
(
name: "Primary",
draw_parameters: DrawParameters(
cull_face: Some(Back),
color_write: ColorMask(
red: true,
green: true,
blue: true,
alpha: true,
),
depth_write: false,
stencil_test: None,
depth_test: Some(LessOrEqual),
blend: Some(BlendParameters(
func: BlendFunc(
sfactor: One,
dfactor: One,
alpha_sfactor: One,
alpha_dfactor: One,
),
equation: BlendEquation(
rgb: Add,
alpha: Add
)
)),
stencil_op: StencilOp(
fail: Keep,
zfail: Keep,
zpass: Zero,
write_mask: 0xFFFF_FFFF,
),
scissor_box: None
),
vertex_shader:
r#"
layout (location = 0) in vec3 vertexPosition;
flat out uint objectIndex;
void main()
{
objectIndex = uint(gl_InstanceID);
gl_Position = (properties.viewProjection * S_FetchMatrix(matrices, gl_InstanceID)) * vec4(vertexPosition, 1.0);
}
"#,
fragment_shader:
r#"
out vec4 FragColor;
flat in uint objectIndex;
void main()
{
int x = int(gl_FragCoord.x) / properties.tileSize;
int y = int(properties.frameBufferHeight - gl_FragCoord.y) / properties.tileSize;
int bitIndex = -1;
int tileDataIndex = x * 33;
int count = int(texelFetch(tileBuffer, ivec2(tileDataIndex, y), 0).x);
int objectsListStartIndex = tileDataIndex + 1;
for (int i = 0; i < count; ++i) {
uint pixelObjectIndex = uint(texelFetch(tileBuffer, ivec2(objectsListStartIndex + i, y), 0).x);
if (pixelObjectIndex == objectIndex) {
bitIndex = i;
break;
}
}
if (bitIndex < 0) {
FragColor = vec4(0.0, 0.0, 0.0, 0.0);
} else {
uint outMask = uint(1 << bitIndex);
float r = float(outMask & 255u) / 255.0;
float g = float((outMask & 65280u) >> 8) / 255.0;
float b = float((outMask & 16711680u) >> 16) / 255.0;
float a = float((outMask & 4278190080u) >> 24) / 255.0;
FragColor = vec4(r, g, b, a);
}
}
"#,
)
]
)