-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathDemoComputeShaderExecute2D.kt
More file actions
60 lines (59 loc) · 2.33 KB
/
Copy pathDemoComputeShaderExecute2D.kt
File metadata and controls
60 lines (59 loc) · 2.33 KB
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
import org.openrndr.application
import org.openrndr.draw.ComputeShader
import org.openrndr.draw.colorBuffer
import org.openrndr.draw.font.BufferAccess
import org.openrndr.draw.imageBinding
import org.openrndr.extra.computeshaders.computeShaderExecuteDimensions
import org.openrndr.extra.computeshaders.resolution
/**
* A compute shader computing an image of an arbitrary size. If dimensions of an image are not a multiplicity
* of a workgroup size dimensions (`local_size_x`, `local_size_y`), then excessive calculations will happen,
* and they should be discarded with the initial if statement in the compute shader.
* The [computeShaderExecuteDimensions] function will calculate proper `executeDimensions` for the `ComputeShader`
* outputting such a 2D image.
*/
fun main() = application {
program {
val image = colorBuffer(
width = width - 1, // we are changing to image size to uneven on purpose
height = height -1
)
val shader = ComputeShader.fromCode(
code = """
|#version 430
|layout (local_size_x = 8, local_size_y = 8) in;
|uniform writeonly image2D image;
|uniform ivec2 resolution;
|uniform float seconds;
|
|void main() {
| ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
| if (coord.x >= resolution.x || coord.y >= resolution.y) {
| return;
| }
| vec4 color = vec4(
| coord.x / float(resolution.x),
| coord.y / float(resolution.y),
| sin(seconds) * .5 + .5,
| 1.0
| );
| imageStore(image, coord, color);
|}
""".trimMargin(),
name = "image-computer"
).apply {
uniform("resolution", image.resolution)
image("image", 0, image.imageBinding(0, BufferAccess.WRITE))
}
val executeDimensions = computeShaderExecuteDimensions(
image.resolution,
localSizeX = 8,
localSizeY = 8
)
extend {
shader.uniform("seconds", seconds)
shader.execute(executeDimensions)
drawer.image(image)
}
}
}