forked from glium/glium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_image.rs
236 lines (196 loc) · 7.57 KB
/
compute_image.rs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Based on https://github.com/Erkaman/image-load-store-demo
// License included at the end of the file.
//
// This example showcases image load/store functionality in compute shaders, which
// allows you to read and write to arbitrary textures in a shader.
//
// Three compute shaders are launched. The first one draws the mandelbrot fractal. The second
// one applies a box filter on the output of the first. Finally, the third one copies data
// the original unsigned integer texture to a signed normalized integer texture.
// Finally, this last texture is blitted to the display framebuffer.
#[macro_use]
extern crate glium;
use std::time::Instant;
mod support;
use glium::{texture::UnsignedTexture2d, uniform, Surface, Texture2d};
fn main() {
// building the display, ie. the main object
let event_loop = winit::event_loop::EventLoop::new();
let wb = winit::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new()
.with_vsync(true)
.with_gl(glutin::GlRequest::Latest);
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
let start_time = Instant::now();
let fract_texture = UnsignedTexture2d::empty_with_format(
&display,
glium::texture::UncompressedUintFormat::U8U8U8U8,
glium::texture::MipmapsOption::NoMipmap,
1024,
1024,
)
.unwrap();
let final_texture = Texture2d::empty_with_format(
&display,
glium::texture::UncompressedFloatFormat::U8U8U8U8,
glium::texture::MipmapsOption::NoMipmap,
1024,
1024,
)
.unwrap();
let fractal_shader = glium::program::ComputeShader::from_source(&display, r#"\
#version 430
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
uniform uint uWidth;
uniform uint uHeight;
uniform float uTime;
uniform layout(binding=3, rgba8ui) writeonly uimage2D uFractalTexture;
void main() {
ivec2 i = ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
vec2 uv = vec2(i) * vec2(1.0 / float(uWidth), 1.0 / float(uHeight));
float n = 0.0;
vec2 c = vec2(-.745, .186) + (uv - 0.5)*(2.0+ 1.7*cos(1.8*uTime) ),
z = vec2(0.0);
const int M =128;
for (int i = 0; i<M; i++)
{
z = vec2(z.x*z.x - z.y*z.y, 2.*z.x*z.y) + c;
if (dot(z, z) > 2) break;
n++;
}
vec3 bla = vec3(0,0,0.0);
vec3 blu = vec3(0,0,0.8);
vec4 color;
if( n >= 0 && n <= M/2-1 ) { color = vec4( mix( vec3(0.2, 0.1, 0.4), blu, n / float(M/2-1) ), 1.0) ; }
if( n >= M/2 && n <= M ) { color = vec4( mix( blu, bla, float(n - M/2 ) / float(M/2) ), 1.0) ; }
imageStore(uFractalTexture, i , uvec4(color * 255.0f));
}
"#).unwrap();
let box_blur_shader = glium::program::ComputeShader::from_source(
&display,
r#"\
#version 430
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
uniform uint uWidth;
uniform uint uHeight;
uniform layout(binding=3, rgba8ui) uimage2D uFractalTexture;
// sample with clamping from the texture.
vec4 csample(ivec2 i) {
i = ivec2(clamp(i.x, 0, uWidth-1), clamp(i.y, 0, uHeight-1));
return imageLoad(uFractalTexture, i);
}
#define R 8
#define W (1.0 / ((1.0+2.0*float(R)) * (1.0+2.0*float(R))))
void main() {
ivec2 i = ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
vec4 sum = vec4(0.0);
// first compute the blurred color.
for(int x = -R; x <= +R; x++ )
for(int y = -R; y <= +R; y++ )
sum += W * csample(i + ivec2(x,y));
// now store the blurred color.
imageStore(uFractalTexture, i, uvec4(sum) );
}
"#,
)
.unwrap();
let copy_shader = glium::program::ComputeShader::from_source(
&display,
r#"\
#version 430
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
uniform layout(binding=3, rgba8ui) readonly uimage2D uFractalTexture;
uniform layout(binding=4, rgba8) writeonly image2D destTexture;
void main() {
ivec2 i = ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
vec3 c = vec3(imageLoad(uFractalTexture, i).xyz);
vec3 cnorm = c/255.0;
imageStore(destTexture, i, vec4(cnorm,1.0));
}
"#,
)
.unwrap();
support::start_loop(event_loop, move |events| {
let image_unit = fract_texture
.image_unit(glium::uniforms::ImageUnitFormat::RGBA8UI)
.unwrap()
.set_access(glium::uniforms::ImageUnitAccess::Write);
fractal_shader.execute(
uniform! {
uWidth: fract_texture.width(),
uHeight: fract_texture.height(),
uFractalTexture: image_unit,
uTime: Instant::now().duration_since(start_time.clone()).as_secs_f32(),
},
fract_texture.width(),
fract_texture.height(),
1,
);
let image_unit = fract_texture
.image_unit(glium::uniforms::ImageUnitFormat::RGBA8UI)
.unwrap()
.set_access(glium::uniforms::ImageUnitAccess::ReadWrite);
box_blur_shader.execute(
uniform! {
uWidth: fract_texture.width(),
uHeight: fract_texture.height(),
uFractalTexture: image_unit,
},
fract_texture.width(),
fract_texture.height(),
1,
);
let fract_unit = fract_texture
.image_unit(glium::uniforms::ImageUnitFormat::RGBA8UI)
.unwrap()
.set_access(glium::uniforms::ImageUnitAccess::Read);
let final_unit = final_texture
.image_unit(glium::uniforms::ImageUnitFormat::RGBA8)
.unwrap()
.set_access(glium::uniforms::ImageUnitAccess::Write);
copy_shader.execute(
uniform! {
uFractalTexture: fract_unit,
destTexture: final_unit,
},
fract_texture.width(),
fract_texture.height(),
1,
);
// drawing a frame
let target = display.draw();
final_texture
.as_surface()
.fill(&target, glium::uniforms::MagnifySamplerFilter::Nearest);
target.finish().unwrap();
// polling and handling the events received by the window
let mut action = support::Action::Continue;
for event in events {
match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
glutin::event::WindowEvent::CloseRequested => action = support::Action::Stop,
_ => (),
},
_ => (),
}
}
action
});
}
// The MIT License (MIT)
// Copyright (c) 2016 Eric Arnebäck
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.