[Bf-blender-cvs] [2a22b8af04e] master: Eevee: Make use of dual source blending for volumetric resolve

Clément Foucault noreply at git.blender.org
Thu Jun 27 14:41:42 CEST 2019


Commit: 2a22b8af04eb2edccfd3d378b8e279bec54639e0
Author: Clément Foucault
Date:   Thu Jun 27 14:40:29 2019 +0200
Branches: master
https://developer.blender.org/rB2a22b8af04eb2edccfd3d378b8e279bec54639e0

Eevee: Make use of dual source blending for volumetric resolve

This simplify the code and add an example use of dual source
blending.

===================================================================

M	source/blender/draw/engines/eevee/eevee_volumes.c
M	source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
M	source/blender/draw/engines/eevee/shaders/volumetric_lib.glsl
M	source/blender/draw/engines/eevee/shaders/volumetric_resolve_frag.glsl

===================================================================

diff --git a/source/blender/draw/engines/eevee/eevee_volumes.c b/source/blender/draw/engines/eevee/eevee_volumes.c
index 9162a604d7c..79a133ed7fc 100644
--- a/source/blender/draw/engines/eevee/eevee_volumes.c
+++ b/source/blender/draw/engines/eevee/eevee_volumes.c
@@ -509,11 +509,10 @@ void EEVEE_volumes_cache_finish(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
 
     DRW_shgroup_call_procedural_triangles(grp, NULL, common_data->vol_tex_size[2]);
 
-    DRW_PASS_CREATE(psl->volumetric_resolve_ps, DRW_STATE_WRITE_COLOR);
+    DRW_PASS_CREATE(psl->volumetric_resolve_ps, DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_CUSTOM);
     grp = DRW_shgroup_create(e_data.volumetric_resolve_sh, psl->volumetric_resolve_ps);
     DRW_shgroup_uniform_texture_ref(grp, "inScattering", &txl->volume_scatter);
     DRW_shgroup_uniform_texture_ref(grp, "inTransmittance", &txl->volume_transmit);
-    DRW_shgroup_uniform_texture_ref(grp, "inSceneColor", &e_data.color_src);
     DRW_shgroup_uniform_texture_ref(grp, "inSceneDepth", &e_data.depth_src);
     DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
 
@@ -638,22 +637,13 @@ void EEVEE_volumes_resolve(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *veda
 
   if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
     DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
-
-    e_data.color_src = txl->color;
     e_data.depth_src = dtxl->depth;
 
-    /* Step 4: Apply for opaque */
-    GPU_framebuffer_bind(fbl->effect_color_fb);
+    /* Apply for opaque geometry. */
+    GPU_framebuffer_bind(fbl->main_color_fb);
     DRW_draw_pass(psl->volumetric_resolve_ps);
 
-    /* Swap the buffers and rebind depth to the current buffer */
-    SWAP(GPUFrameBuffer *, fbl->main_fb, fbl->effect_fb);
-    SWAP(GPUFrameBuffer *, fbl->main_color_fb, fbl->effect_color_fb);
-    SWAP(GPUTexture *, txl->color, txl->color_post);
-
-    /* Restore */
-    GPU_framebuffer_texture_detach(fbl->effect_fb, dtxl->depth);
-    GPU_framebuffer_texture_attach(fbl->main_fb, dtxl->depth, 0, 0);
+    /* Restore. */
     GPU_framebuffer_bind(fbl->main_fb);
   }
 }
diff --git a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
index c332f68728f..3a31afc0224 100644
--- a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
+++ b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
@@ -899,7 +899,10 @@ Closure nodetree_exec(void); /* Prototype */
 
 #    if defined(USE_ALPHA_BLEND)
 /* Prototype because this file is included before volumetric_lib.glsl */
-vec4 volumetric_resolve(vec4 scene_color, vec2 frag_uvs, float frag_depth);
+void volumetric_resolve(vec2 frag_uvs,
+                        float frag_depth,
+                        out vec3 transmittance,
+                        out vec3 scattering);
 #    endif
 
 #    define NODETREE_EXEC
@@ -914,7 +917,9 @@ void main()
 #    if defined(USE_ALPHA_BLEND)
   /* XXX fragile, better use real viewport resolution */
   vec2 uvs = gl_FragCoord.xy / vec2(2 * textureSize(maxzBuffer, 0).xy);
-  fragColor.rgb = volumetric_resolve(vec4(cl.radiance, cl.opacity), uvs, gl_FragCoord.z).rgb;
+  vec3 transmittance, scattering;
+  volumetric_resolve(uvs, gl_FragCoord.z, transmittance, scattering);
+  fragColor.rgb = cl.radiance * transmittance + scattering;
   fragColor.a = cl.opacity;
 #    else
   fragColor = vec4(cl.radiance, cl.opacity);
diff --git a/source/blender/draw/engines/eevee/shaders/volumetric_lib.glsl b/source/blender/draw/engines/eevee/shaders/volumetric_lib.glsl
index 580b53231a0..40eb3da42d1 100644
--- a/source/blender/draw/engines/eevee/shaders/volumetric_lib.glsl
+++ b/source/blender/draw/engines/eevee/shaders/volumetric_lib.glsl
@@ -145,15 +145,13 @@ vec3 irradiance_volumetric(vec3 wpos)
 uniform sampler3D inScattering;
 uniform sampler3D inTransmittance;
 
-vec4 volumetric_resolve(vec4 scene_color, vec2 frag_uvs, float frag_depth)
+void volumetric_resolve(vec2 frag_uvs,
+                        float frag_depth,
+                        out vec3 transmittance,
+                        out vec3 scattering)
 {
   vec3 volume_cos = ndc_to_volume(vec3(frag_uvs, frag_depth));
 
-  vec3 scattering = texture(inScattering, volume_cos).rgb;
-  vec3 transmittance = texture(inTransmittance, volume_cos).rgb;
-
-  /* Approximate volume alpha by using a monochromatic transmitance
-   * and adding it to the scene alpha. */
-  float final_alpha = mix(1.0, scene_color.a, dot(transmittance, vec3(1.0 / 3.0)));
-  return vec4(scene_color.rgb * transmittance + scattering, final_alpha);
+  scattering = texture(inScattering, volume_cos).rgb;
+  transmittance = texture(inTransmittance, volume_cos).rgb;
 }
diff --git a/source/blender/draw/engines/eevee/shaders/volumetric_resolve_frag.glsl b/source/blender/draw/engines/eevee/shaders/volumetric_resolve_frag.glsl
index 2a0fdf69760..757eb59eaa1 100644
--- a/source/blender/draw/engines/eevee/shaders/volumetric_resolve_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/volumetric_resolve_frag.glsl
@@ -6,16 +6,24 @@
  * Note that we do the blending ourself instead of relying
  * on hardware blending which would require 2 pass. */
 
-uniform sampler2D inSceneColor;
 uniform sampler2D inSceneDepth;
 
-out vec4 FragColor;
+/* Blend equation is : FragColor0 + FragColor1 * DstColor */
+layout(location = 0, index = 0) out vec4 FragColor0;
+layout(location = 0, index = 1) out vec4 FragColor1;
 
 void main()
 {
   vec2 uvs = gl_FragCoord.xy / vec2(textureSize(inSceneDepth, 0));
-  vec4 scene_color = texture(inSceneColor, uvs);
   float scene_depth = texture(inSceneDepth, uvs).r;
 
-  FragColor = volumetric_resolve(scene_color, uvs, scene_depth);
+  vec3 transmittance, scattering;
+  volumetric_resolve(uvs, scene_depth, transmittance, scattering);
+
+  /* Approximate volume alpha by using a monochromatic transmittance
+   * and adding it to the scene alpha. */
+  float alpha = dot(transmittance, vec3(1.0 / 3.0));
+
+  FragColor0 = vec4(scattering, 1.0 - alpha);
+  FragColor1 = vec4(transmittance, alpha);
 }



More information about the Bf-blender-cvs mailing list