| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; |
| 8 |
|
| 9 |
#define PARTICLE_FLAG_ACTIVE uint(1) |
| 10 |
#define PARTICLE_FLAG_STARTED uint(2) |
| 11 |
#define PARTICLE_FLAG_TRAILED uint(4) |
| 12 |
|
| 13 |
struct ParticleData { |
| 14 |
mat4 xform; |
| 15 |
vec3 velocity; |
| 16 |
uint flags; |
| 17 |
vec4 color; |
| 18 |
vec4 custom; |
| 19 |
#ifdef USERDATA_COUNT |
| 20 |
vec4 userdata[USERDATA_COUNT]; |
| 21 |
#endif |
| 22 |
}; |
| 23 |
|
| 24 |
layout(set = 0, binding = 1, std430) restrict readonly buffer Particles { |
| 25 |
ParticleData data[]; |
| 26 |
} |
| 27 |
particles; |
| 28 |
|
| 29 |
layout(set = 0, binding = 2, std430) restrict writeonly buffer Transforms { |
| 30 |
vec4 data[]; |
| 31 |
} |
| 32 |
instances; |
| 33 |
|
| 34 |
#ifdef USE_SORT_BUFFER |
| 35 |
|
| 36 |
layout(set = 1, binding = 0, std430) restrict buffer SortBuffer { |
| 37 |
vec2 data[]; |
| 38 |
} |
| 39 |
sort_buffer; |
| 40 |
|
| 41 |
#endif // USE_SORT_BUFFER |
| 42 |
|
| 43 |
layout(set = 2, binding = 0, std430) restrict readonly buffer TrailBindPoses { |
| 44 |
mat4 data[]; |
| 45 |
} |
| 46 |
trail_bind_poses; |
| 47 |
|
| 48 |
#define PARAMS_FLAG_ORDER_BY_LIFETIME 1 |
| 49 |
#define PARAMS_FLAG_COPY_MODE_2D 2 |
| 50 |
|
| 51 |
layout(push_constant, std430) uniform Params { |
| 52 |
vec3 sort_direction; |
| 53 |
uint total_particles; |
| 54 |
|
| 55 |
uint trail_size; |
| 56 |
uint trail_total; |
| 57 |
float frame_delta; |
| 58 |
float frame_remainder; |
| 59 |
|
| 60 |
vec3 align_up; |
| 61 |
uint align_mode; |
| 62 |
|
| 63 |
uint lifetime_split; |
| 64 |
bool lifetime_reverse; |
| 65 |
uint motion_vectors_current_offset; |
| 66 |
uint flags; |
| 67 |
|
| 68 |
mat4 inv_emission_transform; |
| 69 |
} |
| 70 |
params; |
| 71 |
|
| 72 |
#define TRANSFORM_ALIGN_DISABLED 0 |
| 73 |
#define TRANSFORM_ALIGN_Z_BILLBOARD 1 |
| 74 |
#define TRANSFORM_ALIGN_Y_TO_VELOCITY 2 |
| 75 |
#define TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY 3 |
| 76 |
|
| 77 |
void main() { |
| 78 |
#ifdef MODE_FILL_SORT_BUFFER |
| 79 |
|
| 80 |
uint particle = gl_GlobalInvocationID.x; |
| 81 |
if (particle >= params.total_particles) { |
| 82 |
return; //discard |
| 83 |
} |
| 84 |
|
| 85 |
uint src_particle = particle; |
| 86 |
if (params.trail_size > 1) { |
| 87 |
src_particle = src_particle * params.trail_size + params.trail_size / 2; //use trail center for sorting |
| 88 |
} |
| 89 |
sort_buffer.data[particle].x = dot(params.sort_direction, particles.data[src_particle].xform[3].xyz); |
| 90 |
sort_buffer.data[particle].y = float(particle); |
| 91 |
#endif |
| 92 |
|
| 93 |
#ifdef MODE_FILL_INSTANCES |
| 94 |
|
| 95 |
uint particle = gl_GlobalInvocationID.x; |
| 96 |
|
| 97 |
if (particle >= params.total_particles) { |
| 98 |
return; //discard |
| 99 |
} |
| 100 |
|
| 101 |
#ifdef USE_SORT_BUFFER |
| 102 |
|
| 103 |
if (params.trail_size > 1) { |
| 104 |
particle = uint(sort_buffer.data[particle / params.trail_size].y) + (particle % params.trail_size); |
| 105 |
} else { |
| 106 |
particle = uint(sort_buffer.data[particle].y); //use index from sort buffer |
| 107 |
} |
| 108 |
#else |
| 109 |
if (bool(params.flags & PARAMS_FLAG_ORDER_BY_LIFETIME)) { |
| 110 |
if (params.trail_size > 1) { |
| 111 |
uint limit = (params.total_particles / params.trail_size) - params.lifetime_split; |
| 112 |
|
| 113 |
uint base_index = particle / params.trail_size; |
| 114 |
uint base_offset = particle % params.trail_size; |
| 115 |
|
| 116 |
if (params.lifetime_reverse) { |
| 117 |
base_index = (params.total_particles / params.trail_size) - base_index - 1; |
| 118 |
} |
| 119 |
|
| 120 |
if (base_index < limit) { |
| 121 |
base_index = params.lifetime_split + base_index; |
| 122 |
} else { |
| 123 |
base_index -= limit; |
| 124 |
} |
| 125 |
|
| 126 |
particle = base_index * params.trail_size + base_offset; |
| 127 |
|
| 128 |
} else { |
| 129 |
uint limit = params.total_particles - params.lifetime_split; |
| 130 |
|
| 131 |
if (params.lifetime_reverse) { |
| 132 |
particle = params.total_particles - particle - 1; |
| 133 |
} |
| 134 |
|
| 135 |
if (particle < limit) { |
| 136 |
particle = params.lifetime_split + particle; |
| 137 |
} else { |
| 138 |
particle -= limit; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
#endif // USE_SORT_BUFFER |
| 143 |
|
| 144 |
mat4 txform; |
| 145 |
|
| 146 |
if (bool(particles.data[particle].flags & PARTICLE_FLAG_ACTIVE) || bool(particles.data[particle].flags & PARTICLE_FLAG_TRAILED)) { |
| 147 |
txform = particles.data[particle].xform; |
| 148 |
if (params.trail_size > 1) { |
| 149 |
// Since the steps don't fit precisely in the history frames, must do a tiny bit of |
| 150 |
// interpolation to get them close to their intended location. |
| 151 |
uint part_ofs = particle % params.trail_size; |
| 152 |
float natural_ofs = fract((float(part_ofs) / float(params.trail_size)) * float(params.trail_total)) * params.frame_delta; |
| 153 |
|
| 154 |
txform[3].xyz -= particles.data[particle].velocity * natural_ofs; |
| 155 |
} |
| 156 |
|
| 157 |
switch (params.align_mode) { |
| 158 |
case TRANSFORM_ALIGN_DISABLED: { |
| 159 |
} break; //nothing |
| 160 |
case TRANSFORM_ALIGN_Z_BILLBOARD: { |
| 161 |
mat3 local = mat3(normalize(cross(params.align_up, params.sort_direction)), params.align_up, params.sort_direction); |
| 162 |
local = local * mat3(txform); |
| 163 |
txform[0].xyz = local[0]; |
| 164 |
txform[1].xyz = local[1]; |
| 165 |
txform[2].xyz = local[2]; |
| 166 |
|
| 167 |
} break; |
| 168 |
case TRANSFORM_ALIGN_Y_TO_VELOCITY: { |
| 169 |
vec3 v = particles.data[particle].velocity; |
| 170 |
float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0; |
| 171 |
if (length(v) > 0.0) { |
| 172 |
txform[1].xyz = normalize(v); |
| 173 |
} else { |
| 174 |
txform[1].xyz = normalize(txform[1].xyz); |
| 175 |
} |
| 176 |
|
| 177 |
txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz)); |
| 178 |
txform[2].xyz = vec3(0.0, 0.0, 1.0) * s; |
| 179 |
txform[0].xyz *= s; |
| 180 |
txform[1].xyz *= s; |
| 181 |
} break; |
| 182 |
case TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY: { |
| 183 |
vec3 v = particles.data[particle].velocity; |
| 184 |
vec3 sv = v - params.sort_direction * dot(params.sort_direction, v); //screen velocity |
| 185 |
|
| 186 |
if (length(sv) == 0) { |
| 187 |
sv = params.align_up; |
| 188 |
} |
| 189 |
|
| 190 |
sv = normalize(sv); |
| 191 |
|
| 192 |
txform[0].xyz = normalize(cross(sv, params.sort_direction)) * length(txform[0]); |
| 193 |
txform[1].xyz = sv * length(txform[1]); |
| 194 |
txform[2].xyz = params.sort_direction * length(txform[2]); |
| 195 |
|
| 196 |
} break; |
| 197 |
} |
| 198 |
|
| 199 |
txform[3].xyz += particles.data[particle].velocity * params.frame_remainder; |
| 200 |
|
| 201 |
if (params.trail_size > 1) { |
| 202 |
uint part_ofs = particle % params.trail_size; |
| 203 |
txform = txform * trail_bind_poses.data[part_ofs]; |
| 204 |
} |
| 205 |
|
| 206 |
if (bool(params.flags & PARAMS_FLAG_COPY_MODE_2D)) { |
| 207 |
// In global mode, bring 2D particles to local coordinates |
| 208 |
// as they will be drawn with the node position as origin. |
| 209 |
txform = params.inv_emission_transform * txform; |
| 210 |
} |
| 211 |
} else { |
| 212 |
// Set scale to zero and translate to -INF so particle will be invisible |
| 213 |
// even for materials that ignore rotation/scale (i.e. billboards). |
| 214 |
txform = mat4(vec4(0.0), vec4(0.0), vec4(0.0), vec4(-1.0 / 0.0, -1.0 / 0.0, -1.0 / 0.0, 0.0)); |
| 215 |
} |
| 216 |
txform = transpose(txform); |
| 217 |
|
| 218 |
uint instance_index = gl_GlobalInvocationID.x + params.motion_vectors_current_offset; |
| 219 |
if (bool(params.flags & PARAMS_FLAG_COPY_MODE_2D)) { |
| 220 |
uint write_offset = instance_index * (2 + 1 + 1); //xform + color + custom |
| 221 |
|
| 222 |
instances.data[write_offset + 0] = txform[0]; |
| 223 |
instances.data[write_offset + 1] = txform[1]; |
| 224 |
instances.data[write_offset + 2] = particles.data[particle].color; |
| 225 |
instances.data[write_offset + 3] = particles.data[particle].custom; |
| 226 |
} else { |
| 227 |
uint write_offset = instance_index * (3 + 1 + 1); //xform + color + custom |
| 228 |
|
| 229 |
instances.data[write_offset + 0] = txform[0]; |
| 230 |
instances.data[write_offset + 1] = txform[1]; |
| 231 |
instances.data[write_offset + 2] = txform[2]; |
| 232 |
instances.data[write_offset + 3] = particles.data[particle].color; |
| 233 |
instances.data[write_offset + 4] = particles.data[particle].custom; |
| 234 |
} |
| 235 |
|
| 236 |
#endif |
| 237 |
} |