-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_sorter.cpp
213 lines (160 loc) · 6.33 KB
/
gpu_sorter.cpp
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
#include <gpu_sorter.h>
gpu_sorter::gpu_sorter() {
count = 0;
n_pad = 0;
distance_in_ssbo = 0;
distance_out_ssbo = 0;
indices_out_ssbo = 0;
prefix_sum_ssbo = 0;
blocksums_ssbo = 0;
}
gpu_sorter::~gpu_sorter() {
delete_buffers();
}
bool gpu_sorter::init(context& ctx, size_t position_count) {
if(!load_shader_progs(ctx))
return false;
delete_buffers();
count = position_count;
unsigned int n = count;
group_size = 64;
unsigned int block_size = 4 * group_size * 2;
// Calculate padding for n to next multiple of blocksize.
n_pad = block_size - (n % (block_size));
if(n % block_size == 0)
n_pad = 0;
group_count = (n + n_pad + group_size - 1) / group_size;
scan_group_size = (n + n_pad + block_size - 1) / block_size;
unsigned int blocksum_offset_shift = static_cast<unsigned int>(log2f(block_size));
blocksum_offset_shift -= 1;
unsigned int n_blocksums = scan_group_size * 2;
unsigned int num = 1;
while(n_blocksums > num)
num <<= 1;
n_blocksums = num;
size_t data_size = (n + n_pad) * sizeof(unsigned int);
size_t blocksums_size = 4 * n_blocksums * sizeof(unsigned int);
size_t prefix_sum_size = data_size;
glGenBuffers(1, &distance_in_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, distance_in_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, data_size, (void*)0, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glGenBuffers(1, &distance_out_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, distance_out_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, data_size, (void*)0, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glGenBuffers(1, &indices_out_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, indices_out_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, data_size, (void*)0, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glGenBuffers(1, &prefix_sum_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, prefix_sum_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, prefix_sum_size, (void*)0, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glGenBuffers(1, &blocksums_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, blocksums_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, blocksums_size, (void*)0, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
distance_prog.enable(ctx);
distance_prog.set_uniform(ctx, "n", n);
distance_prog.set_uniform(ctx, "n_padded", n + n_pad);
distance_prog.disable(ctx);
scan_local_prog.enable(ctx);
scan_local_prog.set_uniform(ctx, "n", n + n_pad);
scan_local_prog.disable(ctx);
scan_global_prog.enable(ctx);
scan_global_prog.set_uniform(ctx, "n", n_blocksums);
scan_global_prog.disable(ctx);
scatter_prog.enable(ctx);
scatter_prog.set_uniform(ctx, "n", n + n_pad);
scatter_prog.set_uniform(ctx, "blocksum_offset_shift", blocksum_offset_shift);
scatter_prog.disable(ctx);
return true;
}
void gpu_sorter::sort(context& ctx, GLuint position_buffer, GLuint index_buffer, vec3 eye_pos) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, position_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, distance_in_ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, index_buffer);
distance_prog.enable(ctx);
distance_prog.set_uniform(ctx, "eye_pos", eye_pos);
glDispatchCompute(group_size, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
distance_prog.disable(ctx);
// Example for reading an OpenGL buffer into a vector
/*std::vector<float> data(3, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, distance_in_ssbo);
void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 3, GL_MAP_WRITE_BIT);
memcpy(data.data(), ptr, 3 * sizeof(float));
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);*/
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, prefix_sum_ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, blocksums_ssbo);
for(unsigned int b = 0; b < 32; b += 2) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, distance_in_ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, distance_out_ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, index_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, indices_out_ssbo);
scan_local_prog.enable(ctx);
scan_local_prog.set_uniform(ctx, "bit", b);
glDispatchCompute(scan_group_size, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
scan_local_prog.disable(ctx);
scan_global_prog.enable(ctx);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
scan_global_prog.disable(ctx);
scatter_prog.enable(ctx);
scatter_prog.set_uniform(ctx, "bit", b);
glDispatchCompute(group_count, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
scatter_prog.disable(ctx);
std::swap(distance_in_ssbo, distance_out_ssbo);
std::swap(index_buffer, indices_out_ssbo);
}
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, 0);
}
bool gpu_sorter::load_shader_progs(context& ctx) {
bool res = true;
if(!distance_prog.is_created()) {
if(!distance_prog.build_program(ctx, "distance.glpr", true)) {
std::cerr << "ERROR in gpu_sorter::init() ... could not build program distance.glpr" << std::endl;
res = false;
}
}
if(!scan_local_prog.is_created()) {
if(!scan_local_prog.build_program(ctx, "scan_local.glpr", true)) {
std::cerr << "ERROR in gpu_sorter::init() ... could not build program scan_local.glpr" << std::endl;
res = false;
}
}
if(!scan_global_prog.is_created()) {
if(!scan_global_prog.build_program(ctx, "scan_global.glpr", true)) {
std::cerr << "ERROR in gpu_sorter::init() ... could not build program scan_global.glpr" << std::endl;
res = false;
}
}
if(!scatter_prog.is_created()) {
if(!scatter_prog.build_program(ctx, "scatter.glpr", true)) {
std::cerr << "ERROR in gpu_sorter::init() ... could not build program scatter.glpr" << std::endl;
res = false;
}
}
return res;
}
void gpu_sorter::delete_buffers() {
if(distance_in_ssbo != 0)
glDeleteBuffers(1, &distance_in_ssbo);
if(distance_out_ssbo != 0)
glDeleteBuffers(1, &distance_out_ssbo);
if(indices_out_ssbo != 0)
glDeleteBuffers(1, &indices_out_ssbo);
if(prefix_sum_ssbo != 0)
glDeleteBuffers(1, &prefix_sum_ssbo);
if(blocksums_ssbo != 0)
glDeleteBuffers(1, &blocksums_ssbo);
}