[Bf-blender-cvs] [2302ba2245f] blender2.8: Eevee: Refactor of lit_surface_frag.glsl

Clément Foucault noreply at git.blender.org
Fri Nov 24 06:47:45 CET 2017


Commit: 2302ba2245f1e65dde7870cc9ab610ded5abc364
Author: Clément Foucault
Date:   Fri Nov 24 06:15:42 2017 +0100
Branches: blender2.8
https://developer.blender.org/rB2302ba2245f1e65dde7870cc9ab610ded5abc364

Eevee: Refactor of lit_surface_frag.glsl

This cleanup removes the need of gigantic code duplication for each closure.

This also make some preformance improvement since it removes some branches and duplicated loops.

It also fix some mismatch (between cycles and eevee) with the principled shader.

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

M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/engines/eevee/shaders/default_frag.glsl
M	source/blender/draw/engines/eevee/shaders/lamps_lib.glsl
M	source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_material.glsl

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

diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index 62e2cd3805c..e216dc7e60e 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -499,7 +499,10 @@ void EEVEE_materials_init(EEVEE_StorageList *stl)
 		BLI_dynstr_append(ds_frag, datatoc_ltc_lib_glsl);
 		BLI_dynstr_append(ds_frag, datatoc_bsdf_direct_lib_glsl);
 		BLI_dynstr_append(ds_frag, datatoc_lamps_lib_glsl);
-		BLI_dynstr_append(ds_frag, datatoc_lit_surface_frag_glsl);
+		for (int i = 0; i < 7; ++i) {
+			/* Add one for each Closure */
+			BLI_dynstr_append(ds_frag, datatoc_lit_surface_frag_glsl);
+		}
 		BLI_dynstr_append(ds_frag, datatoc_volumetric_lib_glsl);
 		e_data.frag_shader_lib = BLI_dynstr_get_cstring(ds_frag);
 		BLI_dynstr_free(ds_frag);
diff --git a/source/blender/draw/engines/eevee/shaders/default_frag.glsl b/source/blender/draw/engines/eevee/shaders/default_frag.glsl
index 4ba4192abbd..91746a6f082 100644
--- a/source/blender/draw/engines/eevee/shaders/default_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/default_frag.glsl
@@ -7,12 +7,13 @@ uniform float roughness;
 Closure nodetree_exec(void)
 {
 	vec3 dielectric = vec3(0.034) * specular * 2.0;
-	vec3 diffuse = mix(basecol, vec3(0.0), metallic);
+	vec3 albedo = mix(basecol, vec3(0.0), metallic);
 	vec3 f0 = mix(dielectric, basecol, metallic);
-	vec3 ssr_spec;
-	vec3 radiance = eevee_surface_lit((gl_FrontFacing) ? worldNormal : -worldNormal, diffuse, f0, roughness, 1.0, 0, ssr_spec);
+	vec3 N = (gl_FrontFacing) ? worldNormal : -worldNormal;
+	vec3 out_diff, out_spec, ssr_spec;
+	eevee_closure_default(N, albedo, f0, 0, roughness, 1.0, out_diff, out_spec, ssr_spec);
 
-	Closure result = Closure(radiance, 1.0, vec4(ssr_spec, roughness), normal_encode(normalize(viewNormal), viewCameraVec), 0);
+	Closure result = Closure(out_spec + out_diff, 1.0, vec4(ssr_spec, roughness), normal_encode(normalize(viewNormal), viewCameraVec), 0);
 
 	return result;
 }
diff --git a/source/blender/draw/engines/eevee/shaders/lamps_lib.glsl b/source/blender/draw/engines/eevee/shaders/lamps_lib.glsl
index 1b4addaa4ed..933f056c401 100644
--- a/source/blender/draw/engines/eevee/shaders/lamps_lib.glsl
+++ b/source/blender/draw/engines/eevee/shaders/lamps_lib.glsl
@@ -296,9 +296,13 @@ vec3 sss_profile(float s) {
 
 vec3 light_translucent(LightData ld, vec3 W, vec3 N, vec4 l_vector, float scale)
 {
-	vec3 vis = vec3(1.0);
+#if !defined(USE_TRANSLUCENCY) || defined(VOLUMETRICS)
+	return vec3(0.0);
+#endif
 
 #ifndef VOLUMETRICS
+	vec3 vis = vec3(1.0);
+
 	/* Only shadowed light can produce translucency */
 	if (ld.l_shadowid >= 0.0) {
 		ShadowData data = shadows_data[int(ld.l_shadowid)];
@@ -410,9 +414,9 @@ vec3 light_translucent(LightData ld, vec3 W, vec3 N, vec4 l_vector, float scale)
 	else {
 		vis = vec3(0.0);
 	}
-#endif
 
 	return vis;
+#endif
 }
 
 #ifdef HAIR_SHADER
diff --git a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
index 44410be700b..6ecaf0a627b 100644
--- a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
@@ -1,4 +1,7 @@
 
+#ifndef LIT_SURFACE_UNIFORM
+#define LIT_SURFACE_UNIFORM
+
 uniform int light_count;
 uniform int probe_count;
 uniform int grid_count;
@@ -28,37 +31,162 @@ in vec3 viewNormal;
 uniform float maxRoughness;
 uniform int rayCount;
 
-/* ----------- default -----------  */
-
-vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao, int ssr_id, out vec3 ssr_spec)
+#endif /* LIT_SURFACE_UNIFORM */
+
+/** AUTO CONFIG
+ * We include the file multiple times each time with a different configuration.
+ * This leads to a lot of deadcode. Better idea would be to only generate the one needed.
+ */
+#if !defined(SURFACE_DEFAULT)
+	#define SURFACE_DEFAULT
+	#define CLOSURE_NAME eevee_closure_default
+	#define CLOSURE_DIFFUSE
+	#define CLOSURE_GLOSSY
+#endif /* SURFACE_DEFAULT */
+
+#if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME)
+	#define SURFACE_PRINCIPLED
+	#define CLOSURE_NAME eevee_closure_principled
+	#define CLOSURE_DIFFUSE
+	#define CLOSURE_GLOSSY
+	#define CLOSURE_CLEARCOAT
+	#define CLOSURE_REFRACTION
+	#define CLOSURE_SUBSURFACE
+#endif /* SURFACE_PRINCIPLED */
+
+#if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME)
+	#define SURFACE_DIFFUSE
+	#define CLOSURE_NAME eevee_closure_diffuse
+	#define CLOSURE_DIFFUSE
+#endif /* SURFACE_DIFFUSE */
+
+#if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME)
+	#define SURFACE_SUBSURFACE
+	#define CLOSURE_NAME eevee_closure_subsurface
+	#define CLOSURE_DIFFUSE
+	#define CLOSURE_SUBSURFACE
+#endif /* SURFACE_SUBSURFACE */
+
+#if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME)
+	#define SURFACE_GLOSSY
+	#define CLOSURE_NAME eevee_closure_glossy
+	#define CLOSURE_GLOSSY
+#endif /* SURFACE_GLOSSY */
+
+#if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME)
+	#define SURFACE_REFRACT
+	#define CLOSURE_NAME eevee_closure_refraction
+	#define CLOSURE_REFRACTION
+#endif /* SURFACE_REFRACT */
+
+#if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME)
+	#define SURFACE_GLASS
+	#define CLOSURE_NAME eevee_closure_glass
+	#define CLOSURE_GLOSSY
+	#define CLOSURE_REFRACTION
+#endif /* SURFACE_GLASS */
+
+/* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */
+#ifdef CLOSURE_CLEARCOAT
+	#ifndef CLOSURE_GLOSSY
+		#define CLOSURE_GLOSSY
+	#endif
+#endif /* CLOSURE_CLEARCOAT */
+
+void CLOSURE_NAME(
+        vec3 N
+#ifdef CLOSURE_DIFFUSE
+        , vec3 albedo
+#endif
+#ifdef CLOSURE_GLOSSY
+        , vec3 f0, int ssr_id
+#endif
+#if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION)
+        , float roughness
+#endif
+#ifdef CLOSURE_CLEARCOAT
+        , vec3 C_N, float C_intensity, float C_roughness
+#endif
+#if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE)
+        , float ao
+#endif
+#ifdef CLOSURE_SUBSURFACE
+        , float sss_scale
+#endif
+#ifdef CLOSURE_REFRACTION
+        , float ior
+#endif
+#ifdef CLOSURE_DIFFUSE
+        , out vec3 out_diff
+#endif
+#ifdef CLOSURE_SUBSURFACE
+        , out vec3 out_trans
+#endif
+#ifdef CLOSURE_GLOSSY
+        , out vec3 out_spec
+#endif
+#ifdef CLOSURE_REFRACTION
+        , out vec3 out_refr
+#endif
+#ifdef CLOSURE_GLOSSY
+        , out vec3 ssr_spec
+#endif
+        )
 {
+#ifdef CLOSURE_DIFFUSE
+	out_diff = vec3(0.0);
+#endif
+
+#ifdef CLOSURE_SUBSURFACE
+	out_trans = vec3(0.0);
+#endif
+
+#ifdef CLOSURE_GLOSSY
+	out_spec = vec3(0.0);
+#endif
+
+#ifdef CLOSURE_REFRACTION
+	out_refr = vec3(0.0);
+#endif
+
 	/* Zero length vectors cause issues, see: T51979. */
-#if 0
-	N = normalize(N);
-#else
-	{
-		float len = length(N);
-		if (isnan(len)) {
-			return vec3(0.0);
-		}
-		N /= len;
+	float len = length(N);
+	if (isnan(len)) {
+		return;
 	}
+	N /= len;
+
+#ifdef CLOSURE_CLEARCOAT
+	len = length(C_N);
+	if (isnan(len)) {
+		return;
+	}
+	C_N /= len;
 #endif
 
+#if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION)
 	roughness = clamp(roughness, 1e-8, 0.9999);
 	float roughnessSquared = roughness * roughness;
+#endif
+
+#ifdef CLOSURE_CLEARCOAT
+	C_roughness = clamp(C_roughness, 1e-8, 0.9999);
+	float C_roughnessSquared = C_roughness * C_roughness;
+#endif
 
 	vec3 V = cameraVec;
 
-	/* ---------------- SCENE LAMPS LIGHTING ----------------- */
+	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));
+
+	/* ---------------------------------------------------------------- */
+	/* -------------------- SCENE LAMPS LIGHTING ---------------------- */
+	/* ---------------------------------------------------------------- */
 
 #ifdef HAIR_SHADER
 	vec3 norm_view = cross(V, N);
 	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
 #endif
 
-	vec3 diff = vec3(0.0);
-	vec3 spec = vec3(0.0);
 	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
 		LightData ld = lights_data[i];
 
@@ -73,188 +201,71 @@ vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao,
 		float occlu_trans, occlu;
 		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);
 
-		diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans;
-		spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu;
-#else
-		diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
-		spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0);
-#endif
-	}
-
-	/* Accumulate outgoing radiance */
-	vec3 out_light = diff * albedo + spec * float(specToggle);
-
-#ifdef HAIR_SHADER
-	N = -norm_view;
-#endif
-
-	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */
-
-	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
-	vec4 spec_accum = vec4(0.0);
-
-	/* SSR lobe is applied later in a defered style */
-	if (!(ssrToggle && ssr_id == outputSsrId)) {
-		/* Planar Reflections */
-		for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
-			PlanarData pd = planars_data[i];
-
-			float fade = probe_attenuation_planar(pd, worldPosition, N, roughness);
-
-			if (fade > 0.0) {
-				vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
-				accumulate_light(spec, fade, spec_accum);
-			}
-		}
-
-		/* Specular probes */
-		vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);
-
-		/* Starts at 1 because 0 is world probe */
-		for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
-			CubeData cd = probes_data[i];
-
-			float fade = probe_attenuation_cube(cd, worldPosition);
-
-			if (fade > 0.0) {
-				vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
-				accumulate_light(spec, fade, spec_accum);
-			}
-		}
-
-		/* World Specular */
-		if (spec_accum.a < 0.999) {
-			vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
-			accumulate_light(spec, 1.0, spec_accum);
-		}
-	}
-
-	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));
-
-	/* Ambient Occlusion */
-	vec

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list