[Bf-blender-cvs] [4f89f4e1b04] tmp-eevee-material-refactor: EEVEE: Use alpha hash shader for alpha clip material

Clément Foucault noreply at git.blender.org
Thu May 14 16:58:56 CEST 2020


Commit: 4f89f4e1b046ef6fb2548bae8b0c9717f6fb5a66
Author: Clément Foucault
Date:   Fri May 8 15:36:20 2020 +0200
Branches: tmp-eevee-material-refactor
https://developer.blender.org/rB4f89f4e1b046ef6fb2548bae8b0c9717f6fb5a66

EEVEE: Use alpha hash shader for alpha clip material

This reduces the number of shader combination. We use the nodetree material
output node shader code to do the alpha comparison.

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

M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/engines/eevee/shaders/prepass_frag.glsl
M	source/blender/gpu/shaders/material/gpu_shader_material_output_material.glsl
M	source/blender/nodes/shader/nodes/node_shader_output_material.c

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

diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index 03cdb02a48a..45f1f0b28f4 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -328,7 +328,7 @@ static char *eevee_get_defines(int options)
   if ((options & VAR_MAT_SHADOW) != 0) {
     BLI_dynstr_append(ds, "#define SHADOW_SHADER\n");
   }
-  if ((options & VAR_MAT_HASH) != 0) {
+  if ((options & (VAR_MAT_HASH | VAR_MAT_CLIP)) != 0) {
     BLI_dynstr_append(ds, "#define USE_ALPHA_HASH\n");
   }
   if ((options & VAR_MAT_BLEND) != 0) {
diff --git a/source/blender/draw/engines/eevee/shaders/prepass_frag.glsl b/source/blender/draw/engines/eevee/shaders/prepass_frag.glsl
index b49dbfceba2..9acd8f998f6 100644
--- a/source/blender/draw/engines/eevee/shaders/prepass_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/prepass_frag.glsl
@@ -45,26 +45,22 @@ float hashed_alpha_threshold(vec3 co)
   /* Find our final, uniformly distributed alpha threshold. */
   float threshold = (x < one_a) ? ((x < a) ? cases.x : cases.y) : cases.z;
 
+  /* Jitter the threshold for TAA accumulation. */
+  threshold = fract(threshold + alphaHashOffset);
+
   /* Avoids threshold == 0. */
   threshold = clamp(threshold, 1.0e-6, 1.0);
 
-  /* Jitter the threshold for TAA accumulation. */
-  return fract(threshold + alphaHashOffset);
+  return threshold;
 }
 
 #endif
 
-#ifdef USE_ALPHA_CLIP
-uniform float alphaThreshold;
-#endif
+#define NODETREE_EXEC
 
 void main()
 {
-  /* For now do nothing.
-   * In the future, output object motion blur. */
-
-#if defined(USE_ALPHA_HASH) || defined(USE_ALPHA_CLIP)
-#  define NODETREE_EXEC
+#if defined(USE_ALPHA_HASH)
 
   Closure cl = nodetree_exec();
 
@@ -75,11 +71,6 @@ void main()
   if (opacity < hashed_alpha_threshold(worldPosition)) {
     discard;
   }
-#  elif defined(USE_ALPHA_CLIP)
-  /* Alpha clip */
-  if (opacity <= alphaThreshold) {
-    discard;
-  }
 #  endif
 #endif
 }
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_output_material.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_output_material.glsl
index 62f76d46088..4cb00c15b78 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_output_material.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_output_material.glsl
@@ -1,8 +1,16 @@
-void node_output_material(Closure surface, Closure volume, vec3 displacement, out Closure result)
+void node_output_material(
+    Closure surface, Closure volume, vec3 displacement, float alpha_threshold, out Closure result)
 {
 #ifdef VOLUMETRICS
   result = volume;
 #else
   result = surface;
+#  if defined(USE_ALPHA_HASH)
+  /* Alpha clip emulation. */
+  if (alpha_threshold >= 0.0) {
+    float alpha = saturate(1.0 - avg(result.transmittance));
+    result.transmittance = vec3(step(alpha, alpha_threshold));
+  }
+#  endif
 #endif
 }
diff --git a/source/blender/nodes/shader/nodes/node_shader_output_material.c b/source/blender/nodes/shader/nodes/node_shader_output_material.c
index 4b7bd964052..578262e9f17 100644
--- a/source/blender/nodes/shader/nodes/node_shader_output_material.c
+++ b/source/blender/nodes/shader/nodes/node_shader_output_material.c
@@ -45,9 +45,18 @@ static int node_shader_gpu_output_material(GPUMaterial *mat,
                                            GPUNodeStack *in,
                                            GPUNodeStack *out)
 {
-  GPUNodeLink *outlink;
+  GPUNodeLink *outlink, *alpha_threshold_link;
 
-  GPU_stack_link(mat, node, "node_output_material", in, out, &outlink);
+  Material *ma = GPU_material_get_material(mat);
+  if (ma && ma->blend_method == MA_BM_CLIP) {
+    alpha_threshold_link = GPU_uniform(&ma->alpha_threshold);
+  }
+  else {
+    static float no_alpha_threshold = -1.0f;
+    alpha_threshold_link = GPU_uniform(&no_alpha_threshold);
+  }
+
+  GPU_stack_link(mat, node, "node_output_material", in, out, alpha_threshold_link, &outlink);
   GPU_material_output_link(mat, outlink);
 
   return true;



More information about the Bf-blender-cvs mailing list