forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.comp
102 lines (85 loc) · 2.44 KB
/
Example.comp
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
// GLSL compute shader
#version 450 core
#define MIN_DIST ( 0.5 )
#define MAX_DIST ( 0.5 )
#define MIN_RADIUS ( 0.1 )
#define MAX_RADIUS ( 0.1 )
#define M_PI ( 3.141592654 )
layout(std140, binding = 2) uniform SceneState
{
float time;
uint numSceneObjects;
};
struct SceneObject
{
mat2 rotation;
vec2 position;
vec2 _pad0;
};
layout(std430, binding = 3) writeonly buffer InstanceBuffer
{
SceneObject sceneObjects[];
};
struct DrawIndirectArguments
{
uint numVertices;
uint numInstances;
uint firstVertex;
uint firstInstance;
};
layout(std430, binding = 4) writeonly buffer IndirectArgBuffer
{
DrawIndirectArguments drawArgs[];
};
void WriteDrawArgsTri(out DrawIndirectArguments args, uint numInstances, uint firstInstance)
{
args.numVertices = 3;
args.numInstances = numInstances;
args.firstVertex = 0;
args.firstInstance = firstInstance;
}
void WriteDrawArgsQuad(out DrawIndirectArguments args, uint numInstances, uint firstInstance)
{
args.numVertices = 4;
args.numInstances = numInstances;
args.firstVertex = 3;
args.firstInstance = firstInstance;
}
void WriteSceneObject(out SceneObject obj, uint idx)
{
// Compute scene object parameters
float a = fract(time*0.1 + float(idx)/float(numSceneObjects))*M_PI*2.0;
float t = sin(a)*0.5 + 0.5;
float d = mix(MIN_DIST, MAX_DIST, t);
float r = mix(MIN_RADIUS, MAX_RADIUS, t);
float s = sin(a*2.0);
float c = cos(a*2.0);
// Compute rotation and position
obj.rotation = mat2(c*r, s*r, -s*r, c*r);
obj.position = vec2(sin(a)*d, cos(a)*d);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
// Compute shader main function
void main()
{
uvec3 threadID = gl_GlobalInvocationID;
// Write draw arguments to indirect argument buffer
if (threadID.x == 0)
{
float t = fract(time*0.1);
if (t < 0.5)
{
uint n = uint(mix(0.0, float(numSceneObjects), t * 2.0));
WriteDrawArgsTri(drawArgs[0], n, 0);
WriteDrawArgsQuad(drawArgs[1], numSceneObjects - n, n);
}
else
{
uint n = uint(mix(0.0, float(numSceneObjects), (t - 0.5) * 2.0));
WriteDrawArgsQuad(drawArgs[1], n, 0);
WriteDrawArgsTri(drawArgs[0], numSceneObjects - n, n);
}
}
// Write scene transformations
WriteSceneObject(sceneObjects[threadID.x], threadID.x);
}