Skip to content
Snippets Groups Projects
Unverified Commit bad7325c authored by TheJackiMonster's avatar TheJackiMonster
Browse files

Fixed fluid compute shader and linearized depth

parent 24765f10
No related branches found
No related tags found
1 merge request!106Created initial firework project
...@@ -20,7 +20,18 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; ...@@ -20,7 +20,18 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
#define NUM_VOXEL_SAMPLES 32 #define NUM_VOXEL_SAMPLES 32
shared vec2 sc_data [NUM_VOXEL_SAMPLES];
void main() { void main() {
const float localRadian = 0.25f * pi * randomData[gl_LocalInvocationIndex % randomData.length()];
sc_data[gl_LocalInvocationIndex % NUM_VOXEL_SAMPLES] = vec2(
sin(localRadian), cos(localRadian)
);
memoryBarrierShared();
barrier();
const ivec2 res = imageSize(outImage); const ivec2 res = imageSize(outImage);
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, res))){ if(any(greaterThanEqual(gl_GlobalInvocationID.xy, res))){
...@@ -34,34 +45,15 @@ void main() { ...@@ -34,34 +45,15 @@ void main() {
vec4 outTrails = imageLoad(inTrails, uv); vec4 outTrails = imageLoad(inTrails, uv);
vec2 pos = (vec2(uv) + vec2(0.5f)) / vec2(res); vec2 pos = (vec2(uv) + vec2(0.5f)) / vec2(res);
vec2 size = vec2(textureSize(sampler2D(voxelTexture, voxelSampler), 0));
vec4 outSamples = vec4(0.0f);
const uint globalID = uv.y + uv.x * res.y;
for (uint i = 0; i < NUM_VOXEL_SAMPLES; i++) {
vec2 noise = vec2(
randomData[(globalID * NUM_VOXEL_SAMPLES * 2 + i * 2 + 0) % randomData.length()],
randomData[(globalID * NUM_VOXEL_SAMPLES * 2 + i * 2 + 1) % randomData.length()]
);
float noiseF = max(0.0f, 1.0f - length(noise));
outSamples += texture(
sampler2D(voxelTexture, voxelSampler),
pos + noise * (NUM_VOXEL_SAMPLES - 1.0f) / size
) * (noiseF * noiseF);
}
outSamples /= NUM_VOXEL_SAMPLES; vec4 outSamples = texture(sampler2D(voxelTexture, voxelSampler), pos);
vec4 result = vec4(0.0f); vec4 result = vec4(0.0f);
result = smokeBlend(result, outParticles); result = smokeBlend(result, outParticles);
result = smokeBlend(result, outTrails); result = smokeBlend(result, outTrails);
result = smokeBlend(result, outSmoke); result = smokeBlend(result, outSmoke);
result = smokeBlend(result, outSamples); result = smokeBlend(result, outSamples * 0.1f);
result.r = clamp(result.r, 0, 1); result.r = clamp(result.r, 0, 1);
result.g = clamp(result.g, 0, 1); result.g = clamp(result.g, 0, 1);
......
...@@ -37,11 +37,12 @@ vec4 loadCachedData(vec3 position, ivec3 offset, ivec3 size) { ...@@ -37,11 +37,12 @@ vec4 loadCachedData(vec3 position, ivec3 offset, ivec3 size) {
uvec3 localId = gl_LocalInvocationID; uvec3 localId = gl_LocalInvocationID;
ivec3 index = ivec3(localId) + offset; ivec3 index = ivec3(localId) + offset;
if ((any(lessThan(index, ivec3(0)))) || (any(greaterThan(index, ivec3(gl_WorkGroupSize))))) { // TOO SPECIAL FOR GPU TO WORK..!
return getDataFrom(position, vec3(offset) / vec3(size)); //if ((any(lessThan(index, ivec3(0)))) || (any(greaterThan(index, ivec3(gl_WorkGroupSize))))) {
} else { return getDataFrom(position, vec3(offset) / vec3(size));
return cachedData[index.x][index.y][index.z]; //} else {
} // return cachedData[index.x][index.y][index.z];
//}
} }
void main() { void main() {
......
...@@ -8,4 +8,11 @@ ...@@ -8,4 +8,11 @@
#define voxel_write(img, pos, value) imageStore(img, pos, uvec4(VOXEL_NORM_VALUE * value)); #define voxel_write(img, pos, value) imageStore(img, pos, uvec4(VOXEL_NORM_VALUE * value));
#define voxel_read(img, pos) imageLoad(img, pos).r / float(VOXEL_NORM_VALUE); #define voxel_read(img, pos) imageLoad(img, pos).r / float(VOXEL_NORM_VALUE);
// https://stackoverflow.com/questions/51108596/linearize-depth
float linearize_depth(float d,float zNear,float zFar) {
return zNear * zFar / (zFar + d * (zNear - zFar));
}
#define voxel_pos(pos) vec3((pos.xy + vec2(1.0f)) * 0.5f, linearize_depth(pos.y, 0.1f, 50.0f))
#endif // VOXEL_INC #endif // VOXEL_INC
\ No newline at end of file
...@@ -43,7 +43,7 @@ void main() { ...@@ -43,7 +43,7 @@ void main() {
} }
vec3 ndc_pos = cs_pos.xyz / cs_pos.w; vec3 ndc_pos = cs_pos.xyz / cs_pos.w;
vec3 pos = (ndc_pos + vec3(1, 1, 0)) * vec3(0.5f, 0.5f, 1.0f); vec3 pos = voxel_pos(ndc_pos);
if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) { if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) {
return; return;
......
...@@ -52,7 +52,7 @@ void main() { ...@@ -52,7 +52,7 @@ void main() {
} }
vec3 ndc_pos = cs_pos.xyz / cs_pos.w; vec3 ndc_pos = cs_pos.xyz / cs_pos.w;
vec3 pos = (ndc_pos + vec3(1, 1, 0)) * vec3(0.5f, 0.5f, 1.0f); vec3 pos = voxel_pos(ndc_pos);
if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) { if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) {
return; return;
......
...@@ -73,7 +73,7 @@ void main() { ...@@ -73,7 +73,7 @@ void main() {
} }
vec3 ndc_pos = cs_pos.xyz / cs_pos.w; vec3 ndc_pos = cs_pos.xyz / cs_pos.w;
vec3 pos = (ndc_pos + vec3(1, 1, 0)) * vec3(0.5f, 0.5f, 1.0f); vec3 pos = voxel_pos(ndc_pos);
if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) { if ((any(greaterThanEqual(pos, vec3(1.5f)))) || (any(lessThanEqual(pos, vec3(-0.5f))))) {
continue; continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment