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
|
#[compute]
#version 450
VERSION_DEFINES
// Original version here:
// https://github.com/GPUOpen-LibrariesAndSDKs/GPUParticles11/blob/master/gpuparticles11/src/Shaders
//
// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
//
// 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.
//
#define SORT_SIZE 512
#define NUM_THREADS (SORT_SIZE / 2)
#define INVERSION (16 * 2 + 8 * 3)
#define ITERATIONS 1
layout(local_size_x = NUM_THREADS, local_size_y = 1, local_size_z = 1) in;
#ifndef MODE_SORT_STEP
shared vec2 g_LDS[SORT_SIZE];
#endif
layout(set = 1, binding = 0, std430) restrict buffer SortBuffer {
vec2 data[];
}
sort_buffer;
layout(push_constant, binding = 0, std430) uniform Params {
uint total_elements;
uint pad[3];
ivec4 job_params;
}
params;
void main() {
#ifdef MODE_SORT_BLOCK
uvec3 Gid = gl_WorkGroupID;
uvec3 DTid = gl_GlobalInvocationID;
uvec3 GTid = gl_LocalInvocationID;
uint GI = gl_LocalInvocationIndex;
int GlobalBaseIndex = int((Gid.x * SORT_SIZE) + GTid.x);
int LocalBaseIndex = int(GI);
int numElementsInThreadGroup = int(min(SORT_SIZE, params.total_elements - (Gid.x * SORT_SIZE)));
// Load shared data
int i;
for (i = 0; i < 2 * ITERATIONS; ++i) {
if (GI + i * NUM_THREADS < numElementsInThreadGroup)
g_LDS[LocalBaseIndex + i * NUM_THREADS] = sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS];
}
groupMemoryBarrier();
barrier();
// Bitonic sort
for (int nMergeSize = 2; nMergeSize <= SORT_SIZE; nMergeSize = nMergeSize * 2) {
for (int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 0; nMergeSubSize = nMergeSubSize >> 1) {
for (i = 0; i < ITERATIONS; ++i) {
int tmp_index = int(GI + NUM_THREADS * i);
int index_low = tmp_index & (nMergeSubSize - 1);
int index_high = 2 * (tmp_index - index_low);
int index = index_high + index_low;
int nSwapElem = nMergeSubSize == nMergeSize >> 1 ? index_high + (2 * nMergeSubSize - 1) - index_low : index_high + nMergeSubSize + index_low;
if (nSwapElem < numElementsInThreadGroup) {
vec2 a = g_LDS[index];
vec2 b = g_LDS[nSwapElem];
if (a.x > b.x) {
g_LDS[index] = b;
g_LDS[nSwapElem] = a;
}
}
groupMemoryBarrier();
barrier();
}
}
}
// Store shared data
for (i = 0; i < 2 * ITERATIONS; ++i) {
if (GI + i * NUM_THREADS < numElementsInThreadGroup) {
sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS] = g_LDS[LocalBaseIndex + i * NUM_THREADS];
}
}
#endif
#ifdef MODE_SORT_STEP
uvec3 Gid = gl_WorkGroupID;
uvec3 GTid = gl_LocalInvocationID;
ivec4 tgp;
tgp.x = int(Gid.x) * 256;
tgp.y = 0;
tgp.z = int(params.total_elements);
tgp.w = min(512, max(0, tgp.z - int(Gid.x) * 512));
uint localID = int(tgp.x) + GTid.x; // calculate threadID within this sortable-array
uint index_low = localID & (params.job_params.x - 1);
uint index_high = 2 * (localID - index_low);
uint index = tgp.y + index_high + index_low;
uint nSwapElem = tgp.y + index_high + params.job_params.y + params.job_params.z * index_low;
if (nSwapElem < tgp.y + tgp.z) {
vec2 a = sort_buffer.data[index];
vec2 b = sort_buffer.data[nSwapElem];
if (a.x > b.x) {
sort_buffer.data[index] = b;
sort_buffer.data[nSwapElem] = a;
}
}
#endif
#ifdef MODE_SORT_INNER
uvec3 Gid = gl_WorkGroupID;
uvec3 DTid = gl_GlobalInvocationID;
uvec3 GTid = gl_LocalInvocationID;
uint GI = gl_LocalInvocationIndex;
ivec4 tgp;
tgp.x = int(Gid.x * 256);
tgp.y = 0;
tgp.z = int(params.total_elements.x);
tgp.w = int(min(512, max(0, params.total_elements - Gid.x * 512)));
int GlobalBaseIndex = int(tgp.y + tgp.x * 2 + GTid.x);
int LocalBaseIndex = int(GI);
int i;
// Load shared data
for (i = 0; i < 2; ++i) {
if (GI + i * NUM_THREADS < tgp.w)
g_LDS[LocalBaseIndex + i * NUM_THREADS] = sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS];
}
groupMemoryBarrier();
barrier();
// sort threadgroup shared memory
for (int nMergeSubSize = SORT_SIZE >> 1; nMergeSubSize > 0; nMergeSubSize = nMergeSubSize >> 1) {
int tmp_index = int(GI);
int index_low = tmp_index & (nMergeSubSize - 1);
int index_high = 2 * (tmp_index - index_low);
int index = index_high + index_low;
int nSwapElem = index_high + nMergeSubSize + index_low;
if (nSwapElem < tgp.w) {
vec2 a = g_LDS[index];
vec2 b = g_LDS[nSwapElem];
if (a.x > b.x) {
g_LDS[index] = b;
g_LDS[nSwapElem] = a;
}
}
groupMemoryBarrier();
barrier();
}
// Store shared data
for (i = 0; i < 2; ++i) {
if (GI + i * NUM_THREADS < tgp.w) {
sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS] = g_LDS[LocalBaseIndex + i * NUM_THREADS];
}
}
#endif
}
|