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
use crate::code::Program;
use crate::code::ComputeShader;
use crate::Resource;
use gl::types::*;
use std::ptr;
pub struct ComputeProgram {
id : u32
}
impl Program for ComputeProgram {
fn get_id(&self) -> u32 { self.id }
}
impl ComputeProgram {
pub fn new(compute_shader : ComputeShader) -> Result<ComputeProgram, String> {
let id = unsafe { gl::CreateProgram() };
unsafe {
gl::AttachShader(id, compute_shader.get_id());
gl::LinkProgram(id);
let mut success = 0;
gl::GetProgramiv(id, gl::LINK_STATUS, &mut success);
if success != gl::TRUE as i32 {
let mut len = 0;
gl::GetProgramiv(id, gl::INFO_LOG_LENGTH, &mut len);
let mut info_log = Vec::with_capacity(len as usize);
info_log.set_len((len as usize) - 1);
gl::GetProgramInfoLog(id, len, ptr::null_mut(), info_log.as_mut_ptr() as *mut GLchar);
return Err(String::from_utf8(info_log).unwrap());
}
}
Ok(Self {
id : id
})
}
pub fn compute(&self, groups: (usize, usize, usize)) {
unsafe {
gl::UseProgram(self.get_id());
gl::DispatchCompute(groups.0 as u32, groups.1 as u32, groups.2 as u32);
}
}
}
#[cfg(test)]
mod tests {
use crate::{ContextBuilder, ContextDisplay, initialize, ComputeShader, Program, ComputeProgram};
#[test]
fn write_to_buffer() {
use crate::Buffer;
let context_builder = ContextBuilder::new().with_display(ContextDisplay::None);
let mut context = context_builder.build();
context.make_current().unwrap();
initialize(|symbol| context.get_proc_address(symbol) as *const _);
let shader = ComputeShader::new(r#"
#version 460 compatibility
layout(binding = 0) buffer Vertices {
float Positions[];
};
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1 ) in;
void main() {
uint globalId = gl_GlobalInvocationID.x;
Positions[gl_GlobalInvocationID.x] = float(globalId);
}
"#).unwrap();
let mut program = ComputeProgram::new(shader).unwrap();
let length = 256;
let size = length * 4;
let buffer = Buffer::allocate(size);
assert_eq!(buffer.get_size(), size);
program.bind_buffer(&buffer, 0);
program.compute((length, 1, 1));
let mut expected_data : Vec<f32> = Vec::new();
for x in 0..length {
expected_data.push(x as f32);
}
let data_out : Vec<f32> = buffer.get_data();
assert_eq!(data_out, expected_data);
}
#[test]
fn write_to_texture2d() {
use crate::{Texture2D, TextureFormat, ColorFormat, ComponentFormat};
let context_builder = ContextBuilder::new().with_display(ContextDisplay::None);
let mut context = context_builder.build();
context.make_current().unwrap();
initialize(|symbol| context.get_proc_address(symbol) as *const _);
let shader = ComputeShader::new(r#"
#version 460 compatibility
layout (binding = 0, r32f) uniform image2D image;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1 ) in;
void main() {
ivec2 p = ivec2(gl_GlobalInvocationID.xy);\
imageStore(image, p, vec4(float(p.x + p.y * 16), 0.0, 0.0, 0.0));\
}"#).unwrap();
let mut program = ComputeProgram::new(shader).unwrap();
let dimension = (16, 16);
let format = TextureFormat(ColorFormat::R, ComponentFormat::F32);
let texture = Texture2D::allocate(dimension, &format);
assert_eq!(texture.get_dimension(), dimension);
program.bind_image(&texture, 0);
program.compute((dimension.0, dimension.1, 1));
let mut expected_data : Vec<f32> = Vec::new();
for y in 0..dimension.0 {
for x in 0..dimension.1 {
expected_data.push((x + y * dimension.0) as f32);
}
}
let data_out : Vec<f32> = texture.get_data();
assert_eq!(data_out, expected_data);
}
#[test]
fn write_to_texture3d() {
use crate::Texture3D;
use crate::TextureFormat;
use crate::ColorFormat;
use crate::ComponentFormat;
use crate::ComputeShader;
use crate::Program;
use crate::ComputeProgram;
let context_builder = ContextBuilder::new().with_display(ContextDisplay::None);
let mut context = context_builder.build();
context.make_current().unwrap();
initialize(|symbol| context.get_proc_address(symbol) as *const _);
let shader = ComputeShader::new(r#"
#version 460 compatibility
layout (binding = 0, r32f) uniform image3D image;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1 ) in;
void main() {
ivec3 p = ivec3(gl_GlobalInvocationID.xyz);\
float value = float(p.x + (p.y + p.z * 2) * 2);\
imageStore(image, p, vec4(value));\
}"#).unwrap();
let mut program = ComputeProgram::new(shader).unwrap();
let dimension = (2, 2, 2);
let format = TextureFormat(ColorFormat::RGBA, ComponentFormat::F32);
let texture = Texture3D::allocate(dimension, &format);
assert_eq!(texture.get_dimension(), dimension);
program.bind_image(&texture, 0);
program.compute(dimension);
let mut expected_data : Vec<f32> = Vec::new();
for z in 0..dimension.2 {
for y in 0..dimension.2 {
for x in 0..dimension.1 {
expected_data.push((x + (y + z * 2) * 2) as f32);
expected_data.push((x + (y + z * 2) * 2) as f32);
expected_data.push((x + (y + z * 2) * 2) as f32);
expected_data.push((x + (y + z * 2) * 2) as f32);
}
}
}
let data_out : Vec<f32> = texture.get_data();
assert_eq!(data_out, expected_data);
}
}