Cycles: option to make background visible through glass transparent.
[blender-staging.git] / intern / cycles / kernel / closure / bsdf.h
index a6bba8bf74d57f5a7fe4806abc26b93972ca60cb..6f0bdb3fa38214338c8d330b3dfed419bc26c379 100644 (file)
 #include "kernel/closure/bsdf_hair.h"
 #include "kernel/closure/bsdf_principled_diffuse.h"
 #include "kernel/closure/bsdf_principled_sheen.h"
-#ifdef __SUBSURFACE__
-#  include "kernel/closure/bssrdf.h"
-#endif
+#include "kernel/closure/bssrdf.h"
 #ifdef __VOLUME__
 #  include "kernel/closure/volume.h"
 #endif
 
 CCL_NAMESPACE_BEGIN
 
+/* Returns the square of the roughness of the closure if it has roughness,
+ * 0 for singular closures and 1 otherwise. */
+ccl_device_inline float bsdf_get_roughness_squared(const ShaderClosure *sc)
+{
+       if(CLOSURE_IS_BSDF_SINGULAR(sc->type)) {
+               return 0.0f;
+       }
+
+       if(CLOSURE_IS_BSDF_MICROFACET(sc->type)) {
+               MicrofacetBsdf *bsdf = (MicrofacetBsdf*) sc;
+               return bsdf->alpha_x*bsdf->alpha_y;
+       }
+
+       return 1.0f;
+}
+
 ccl_device_forceinline int bsdf_sample(KernelGlobals *kg,
                                        ShaderData *sd,
                                        const ShaderClosure *sc,
@@ -159,6 +173,17 @@ ccl_device_forceinline int bsdf_sample(KernelGlobals *kg,
                        break;
        }
 
+       /* Test if BSDF sample should be treated as transparent for background. */
+       if(label & LABEL_TRANSMIT) {
+               float threshold_squared = kernel_data.background.transparent_roughness_squared_threshold;
+
+               if(threshold_squared >= 0.0f) {
+                       if(bsdf_get_roughness_squared(sc) <= threshold_squared) {
+                               label |= LABEL_TRANSMIT_TRANSPARENT;
+                       }
+               }
+       }
+
        return label;
 }
 
@@ -423,6 +448,11 @@ ccl_device bool bsdf_merge(ShaderClosure *a, ShaderClosure *b)
                case CLOSURE_BSDF_HAIR_REFLECTION_ID:
                case CLOSURE_BSDF_HAIR_TRANSMISSION_ID:
                        return bsdf_hair_merge(a, b);
+#ifdef __PRINCIPLED__
+               case CLOSURE_BSDF_PRINCIPLED_DIFFUSE_ID:
+               case CLOSURE_BSDF_BSSRDF_PRINCIPLED_ID:
+                       return bsdf_principled_diffuse_merge(a, b);
+#endif
 #ifdef __VOLUME__
                case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
                        return volume_henyey_greenstein_merge(a, b);