forked from treeform/shady
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute1.nim
133 lines (117 loc) · 3.82 KB
/
compute1.nim
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import opengl, pixie, shady, shady/compute
initOffscreenWindow()
# Setup the uniforms.
var inputCommandBuffer*: Uniform[SamplerBuffer]
var outputImageBuffer*: UniformWriteOnly[UImageBuffer]
var dimensions*: Uniform[IVec4] # ivec4(width, height, 0, 0)
# The shader itself.
proc commandsToImage() =
var pos = gl_GlobalInvocationID
for x in 0 ..< dimensions.x:
pos.x = x.uint32
let value = uint32(texelFetch(inputCommandBuffer, int32(pos.x)).x)
#echo pos.x, " ", value
let colorValue = uvec4(
128,
0,
value,
255
)
imageStore(outputImageBuffer, int32(pos.y * uint32(dimensions.x) + pos.x), colorValue)
# Setup the input and output data.
for i in 0 ..< 256:
inputCommandBuffer.data.add(i.float32)
outputImageBuffer.image = newImage(256, 256)
dimensions = ivec4(
outputImageBuffer.image.width.int32,
outputImageBuffer.image.height.int32,
0,
0
)
template runComputeOnGpu(computeShader: proc(), invocationSize: UVec3) =
let computeShaderSrc = toGLSL(
computeShader,
"430",
extra = "layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n"
)
writeFile("examples/compute1_sh.comp", computeShaderSrc)
var shaderId = compileComputeShader((
"examples/compute1_sh.comp",
computeShaderSrc
))
glUseProgram(shaderId)
# Setup outputImageBuffer.
var
outputBufferId: GLuint
outputTextureId: GLuint
glGenBuffers(1, outputBufferId.addr)
glBindBuffer(GL_TEXTURE_BUFFER, outputBufferId)
glBufferData(GL_TEXTURE_BUFFER, outputImageBuffer.image.data.len * 4, nil, GL_STATIC_DRAW)
let outputImageBufferLoc = glGetUniformLocation(shaderId, "outputImageBuffer")
glUniform1i(outputImageBufferLoc, 0)
glActiveTexture(GL_TEXTURE0)
glGenTextures(1, outputTextureId.addr)
glBindTexture(GL_TEXTURE_BUFFER, outputTextureId)
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8UI, outputBufferId)
glBindImageTexture(
0,
outputTextureId,
0,
GL_FALSE,
0,
GL_WRITE_ONLY,
GL_RGBA8UI
)
glBindTexture(GL_TEXTURE_BUFFER, outputTextureId)
# Setup inputCommandBuffer.
var
inputCommandBufferId: GLuint
commandTextureId: GLuint
glGenTextures(1, commandTextureId.addr)
glActiveTexture(GL_TEXTURE1)
glGenBuffers(1, inputCommandBufferId.addr)
let byteLength = inputCommandBuffer.data.len * 4
glBindBuffer(GL_TEXTURE_BUFFER, inputCommandBufferId)
glBufferData(
GL_TEXTURE_BUFFER,
byteLength,
inputCommandBuffer.data[0].addr,
GL_STATIC_DRAW
)
glBindTexture(GL_TEXTURE_BUFFER, commandTextureId)
glTexBuffer(
GL_TEXTURE_BUFFER,
GL_R32F,
inputCommandBufferId
)
glBindTexture(GL_TEXTURE_BUFFER, commandTextureId)
let inputCommandBufferLoc = glGetUniformLocation(shaderId, "inputCommandBuffer")
glUniform1i(inputCommandBufferLoc, 1)
# Setup dimensions uniform.
let dimensionsLoc = glGetUniformLocation(shaderId, "dimensions")
glUniform4i(dimensionsLoc, dimensions.x, dimensions.y, dimensions.z, dimensions.w)
# Run the shader.
glDispatchCompute(
invocationSize.x.GLuint,
invocationSize.y.GLuint,
invocationSize.z.GLuint
)
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)
# Read back the outputImageBuffer.
let p = cast[ptr UncheckedArray[uint8]](
glMapNamedBuffer(outputBufferId, GL_READ_ONLY)
)
copyMem(
outputImageBuffer.image.data[0].addr,
p,
outputImageBuffer.image.data.len * 4
)
discard glUnmapNamedBuffer(outputBufferId)
# Run it on CPU.
runComputeOnCpu(commandsToImage, uvec3(1, outputImageBuffer.image.height.uint32, 1))
outputImageBuffer.image.writeFile("examples/compute1_output_cpu.png")
# Just in case clear the image before running GPU.
outputImageBuffer.image.fill(rgbx(0, 0, 0, 0))
# Run it on the GPU.
runComputeOnGpu(commandsToImage, uvec3(1, outputImageBuffer.image.height.uint32, 1))
outputImageBuffer.image.writeFile("examples/compute1_output_gpu.png")