Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/sort.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 // Original version here:
8 // https://github.com/GPUOpen-LibrariesAndSDKs/GPUParticles11/blob/master/gpuparticles11/src/Shaders
9
10 //
11 // Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 // THE SOFTWARE.
30 //
31
32 #define SORT_SIZE 512
33 #define NUM_THREADS (SORT_SIZE / 2)
34 #define INVERSION (16 * 2 + 8 * 3)
35 #define ITERATIONS 1
36
37 layout(local_size_x = NUM_THREADS, local_size_y = 1, local_size_z = 1) in;
38
39 #ifndef MODE_SORT_STEP
40
41 shared vec2 g_LDS[SORT_SIZE];
42
43 #endif
44
45 layout(set = 1, binding = 0, std430) restrict buffer SortBuffer {
46 vec2 data[];
47 }
48 sort_buffer;
49
50 layout(push_constant, std430) uniform Params {
51 uint total_elements;
52 uint pad[3];
53 ivec4 job_params;
54 }
55 params;
56
57 void main() {
58 #ifdef MODE_SORT_BLOCK
59
60 uvec3 Gid = gl_WorkGroupID;
61 uvec3 DTid = gl_GlobalInvocationID;
62 uvec3 GTid = gl_LocalInvocationID;
63 uint GI = gl_LocalInvocationIndex;
64
65 int GlobalBaseIndex = int((Gid.x * SORT_SIZE) + GTid.x);
66 int LocalBaseIndex = int(GI);
67 int numElementsInThreadGroup = int(min(SORT_SIZE, params.total_elements - (Gid.x * SORT_SIZE)));
68
69 // Load shared data
70
71 int i;
72 for (i = 0; i < 2 * ITERATIONS; ++i) {
73 if (GI + i * NUM_THREADS < numElementsInThreadGroup) {
74 g_LDS[LocalBaseIndex + i * NUM_THREADS] = sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS];
75 }
76 }
77
78 groupMemoryBarrier();
79 barrier();
80
81 // Bitonic sort
82 for (int nMergeSize = 2; nMergeSize <= SORT_SIZE; nMergeSize = nMergeSize * 2) {
83 for (int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 0; nMergeSubSize = nMergeSubSize >> 1) {
84 for (i = 0; i < ITERATIONS; ++i) {
85 int tmp_index = int(GI + NUM_THREADS * i);
86 int index_low = tmp_index & (nMergeSubSize - 1);
87 int index_high = 2 * (tmp_index - index_low);
88 int index = index_high + index_low;
89
90 int nSwapElem = nMergeSubSize == nMergeSize >> 1 ? index_high + (2 * nMergeSubSize - 1) - index_low : index_high + nMergeSubSize + index_low;
91 if (nSwapElem < numElementsInThreadGroup) {
92 vec2 a = g_LDS[index];
93 vec2 b = g_LDS[nSwapElem];
94
95 if (a.x > b.x) {
96 g_LDS[index] = b;
97 g_LDS[nSwapElem] = a;
98 }
99 }
100 groupMemoryBarrier();
101 barrier();
102 }
103 }
104 }
105
106 // Store shared data
107 for (i = 0; i < 2 * ITERATIONS; ++i) {
108 if (GI + i * NUM_THREADS < numElementsInThreadGroup) {
109 sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS] = g_LDS[LocalBaseIndex + i * NUM_THREADS];
110 }
111 }
112
113 #endif
114
115 #ifdef MODE_SORT_STEP
116
117 uvec3 Gid = gl_WorkGroupID;
118 uvec3 GTid = gl_LocalInvocationID;
119
120 ivec4 tgp;
121
122 tgp.x = int(Gid.x) * 256;
123 tgp.y = 0;
124 tgp.z = int(params.total_elements);
125 tgp.w = min(512, max(0, tgp.z - int(Gid.x) * 512));
126
127 uint localID = int(tgp.x) + GTid.x; // calculate threadID within this sortable-array
128
129 uint index_low = localID & (params.job_params.x - 1);
130 uint index_high = 2 * (localID - index_low);
131
132 uint index = tgp.y + index_high + index_low;
133 uint nSwapElem = tgp.y + index_high + params.job_params.y + params.job_params.z * index_low;
134
135 if (nSwapElem < tgp.y + tgp.z) {
136 vec2 a = sort_buffer.data[index];
137 vec2 b = sort_buffer.data[nSwapElem];
138
139 if (a.x > b.x) {
140 sort_buffer.data[index] = b;
141 sort_buffer.data[nSwapElem] = a;
142 }
143 }
144
145 #endif
146
147 #ifdef MODE_SORT_INNER
148
149 uvec3 Gid = gl_WorkGroupID;
150 uvec3 DTid = gl_GlobalInvocationID;
151 uvec3 GTid = gl_LocalInvocationID;
152 uint GI = gl_LocalInvocationIndex;
153
154 ivec4 tgp;
155
156 tgp.x = int(Gid.x * 256);
157 tgp.y = 0;
158 tgp.z = int(params.total_elements.x);
159 tgp.w = int(min(512, max(0, params.total_elements - Gid.x * 512)));
160
161 int GlobalBaseIndex = int(tgp.y + tgp.x * 2 + GTid.x);
162 int LocalBaseIndex = int(GI);
163 int i;
164
165 // Load shared data
166 for (i = 0; i < 2; ++i) {
167 if (GI + i * NUM_THREADS < tgp.w) {
168 g_LDS[LocalBaseIndex + i * NUM_THREADS] = sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS];
169 }
170 }
171
172 groupMemoryBarrier();
173 barrier();
174
175 // sort threadgroup shared memory
176 for (int nMergeSubSize = SORT_SIZE >> 1; nMergeSubSize > 0; nMergeSubSize = nMergeSubSize >> 1) {
177 int tmp_index = int(GI);
178 int index_low = tmp_index & (nMergeSubSize - 1);
179 int index_high = 2 * (tmp_index - index_low);
180 int index = index_high + index_low;
181
182 int nSwapElem = index_high + nMergeSubSize + index_low;
183
184 if (nSwapElem < tgp.w) {
185 vec2 a = g_LDS[index];
186 vec2 b = g_LDS[nSwapElem];
187
188 if (a.x > b.x) {
189 g_LDS[index] = b;
190 g_LDS[nSwapElem] = a;
191 }
192 }
193 groupMemoryBarrier();
194 barrier();
195 }
196
197 // Store shared data
198 for (i = 0; i < 2; ++i) {
199 if (GI + i * NUM_THREADS < tgp.w) {
200 sort_buffer.data[GlobalBaseIndex + i * NUM_THREADS] = g_LDS[LocalBaseIndex + i * NUM_THREADS];
201 }
202 }
203
204 #endif
205 }