Skip to content

Commit

Permalink
Thickness & depth target for screen space fluid rendering
Browse files Browse the repository at this point in the history
still work in progress. Unclear how depth target should be handled exactly (what kind of depth, how to clear, etc.)
  • Loading branch information
Wumpf committed Jun 27, 2020
1 parent 5304f9f commit 69b660a
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 61 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ strum_macros = "0.18"
strum = "0.18"
bytemuck = "1.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0"

# todo: Get independentBlending upstream
[patch."https://github.com/gfx-rs/wgpu"]
wgpu-types = { version = "0.5.0", path = "../wgpu/wgpu-types" }
wgpu-core = { version = "0.5.0", path = "../wgpu/wgpu-core" }
20 changes: 20 additions & 0 deletions shader/screenspace_fluid/compose.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 460

#include "../fluid_render_info.glsl"
#include "../per_frame_resources.glsl"
#include "../utilities.glsl"

layout(set = 2, binding = 0) uniform texture2D ParticleDepth;
layout(set = 2, binding = 1) uniform texture2D ParticleThickness;
layout(set = 2, binding = 2, HDR_BACKBUFFER_IMAGE_FORMAT) uniform restrict image2D Backbuffer;

layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

void main() {
float particleDepth = texelFetch(ParticleDepth, ivec2(gl_GlobalInvocationID.xy), 0).r;
float particleThickness = texelFetch(ParticleThickness, ivec2(gl_GlobalInvocationID.xy), 0).r;
vec4 previousColor = imageLoad(Backbuffer, ivec2(gl_GlobalInvocationID.xy));

vec4 outputColor = vec4(particleThickness * 0.01);
imageStore(Backbuffer, ivec2(gl_GlobalInvocationID.xy), outputColor);
}
14 changes: 0 additions & 14 deletions shader/screenspace_fluid/final_compose.comp

This file was deleted.

33 changes: 33 additions & 0 deletions shader/screenspace_fluid/particles.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#version 460

#include "../per_frame_resources.glsl"
#include "../utilities.glsl"

layout(location = 0) in vec3 in_WorldPosition;
layout(location = 1) in vec3 in_ParticleWorldPosition;
layout(location = 2) in float in_Radius;
layout(location = 0) out float out_Depth;
layout(location = 1) out float out_Thickness;

// Note that we promise to only lessen the depth value, so gpu can still do some hi-z/early depth culling
layout(depth_less) out float gl_FragDepth;

void main() {
vec3 rayDir = normalize(in_WorldPosition - Camera.Position);
float cameraDistance;
// TODO: Elipsoids using APIC matrix?
if (!sphereIntersect(in_ParticleWorldPosition, in_Radius, Camera.Position, rayDir, cameraDistance))
discard;

vec3 sphereWorldPos = Camera.Position + cameraDistance * rayDir;
vec3 normal = (sphereWorldPos - in_ParticleWorldPosition) / in_Radius;

// Adjust depth buffer value.
// TODO: Necessary for this step?
vec2 projected_zw = (Camera.ViewProjection * vec4(sphereWorldPos, 1.0)).zw; // (trusting optimizer to pick the right thing ;-))
gl_FragDepth = projected_zw.x / projected_zw.y;

// TODO: What kind of depth
out_Depth = cameraDistance;
out_Thickness = in_Radius;
}
19 changes: 19 additions & 0 deletions shader/screenspace_fluid/particles.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 450

#include "fluid_render_info.glsl"
#include "per_frame_resources.glsl"
#include "sphere_particles.glsl"
#include "utilities.glsl"

out gl_PerVertex { vec4 gl_Position; };

layout(location = 0) out vec3 out_WorldPosition;
layout(location = 1) out vec3 out_ParticleWorldPosition;
layout(location = 2) out float out_Radius;

void main() {
out_Radius = 0.25 * Rendering.FluidGridToWorldScale;
out_ParticleWorldPosition = Particles[gl_InstanceIndex].Position * Rendering.FluidGridToWorldScale + Rendering.FluidWorldOrigin;
out_WorldPosition = spanParticle(out_ParticleWorldPosition, out_Radius);
gl_Position = Camera.ViewProjection * vec4(out_WorldPosition, 1.0);
}
13 changes: 13 additions & 0 deletions shader/utilities.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ vec3 colormapHeat(float t) { return saturate(vec3(t * 3, t * 3 - 1, t * 3 - 2));
// t = [-1; 1]
vec3 colormapCoolToWarm(float t) { return t < 0.0 ? mix(vec3(1.0), vec3(0.0, 0.0, 1.0), -t) : mix(vec3(1.0), vec3(1.0, 0.0, 0.0), t); }

bool sphereIntersect(vec3 spherePosition, float radius, vec3 rayOrigin, vec3 rayDir, out float sphereDistance) {
// Sphere intersect raycast.
// (uses equation based intersect: rayOrigin + t * rayDir, ||sphereOrigin-pointOnSphere||= r*r, [...])
vec3 particleCenterToCamera = rayOrigin - spherePosition; // (often denoted as oc == OriginCenter)
float b = dot(particleCenterToCamera, rayDir);
float c = dot(particleCenterToCamera, particleCenterToCamera) - radius * radius;
float discr = b * b - c;
if (discr < 0.0)
return false;
sphereDistance = -b - sqrt(discr);
return true;
}

#endif // INCLUDE_UTILITIES
6 changes: 3 additions & 3 deletions src/renderer/scene_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl SceneRenderer {
encoder: &mut wgpu::CommandEncoder,
pipeline_manager: &PipelineManager,
backbuffer: &wgpu::TextureView,
depth: &wgpu::TextureView,
depthbuffer: &wgpu::TextureView,
per_frame_bind_group: &wgpu::BindGroup,
) {
// Opaque
Expand All @@ -162,7 +162,7 @@ impl SceneRenderer {
},
}],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor {
attachment: depth,
attachment: depthbuffer,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: true,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl SceneRenderer {
{
if let FluidRenderingMode::ScreenSpaceFluid = self.fluid_rendering_mode {
self.screenspace_fluid
.draw(encoder, pipeline_manager, per_frame_bind_group, &scene.fluid());
.draw(encoder, pipeline_manager, depthbuffer, per_frame_bind_group, &scene.fluid());
}
}
}
Expand Down
Loading

0 comments on commit 69b660a

Please sign in to comment.