Commit: 23ccf5767fea1d7f1b50405334249b1a7a69d5c9
Author: Brecht Van Lommel
Date:   Sat Mar 10 18:18:05 2018 +0100
Branches: master
https://developer.blender.org/rB23ccf5767fea1d7f1b50405334249b1a7a69d5c9

Cycles: add roughness baking support, using squared roughness convention.

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

M       intern/cycles/blender/addon/properties.py
M       intern/cycles/blender/blender_session.cpp
M       intern/cycles/kernel/kernel_bake.h
M       intern/cycles/kernel/kernel_shader.h
M       intern/cycles/kernel/kernel_types.h
M       intern/cycles/render/bake.cpp
M       source/blender/makesdna/DNA_scene_types.h
M       source/blender/makesrna/intern/rna_render.c

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

diff --git a/intern/cycles/blender/addon/properties.py 
b/intern/cycles/blender/addon/properties.py
index 1b95c365616..8dbd80f3747 100644
--- a/intern/cycles/blender/addon/properties.py
+++ b/intern/cycles/blender/addon/properties.py
@@ -571,6 +571,7 @@ class CyclesRenderSettings(bpy.types.PropertyGroup):
                 ('SHADOW', "Shadow", ""),
                 ('NORMAL', "Normal", ""),
                 ('UV', "UV", ""),
+                ('ROUGHNESS', "Roughness", ""),
                 ('EMIT', "Emit", ""),
                 ('ENVIRONMENT', "Environment", ""),
                 ('DIFFUSE', "Diffuse", ""),
diff --git a/intern/cycles/blender/blender_session.cpp 
b/intern/cycles/blender/blender_session.cpp
index 9d1e87b763c..00d23b9095e 100644
--- a/intern/cycles/blender/blender_session.cpp
+++ b/intern/cycles/blender/blender_session.cpp
@@ -252,6 +252,8 @@ static ShaderEvalType get_shader_type(const string& 
pass_type)
                return SHADER_EVAL_NORMAL;
        else if(strcmp(shader_type, "UV")==0)
                return SHADER_EVAL_UV;
+       else if(strcmp(shader_type, "ROUGHNESS")==0)
+               return SHADER_EVAL_ROUGHNESS;
        else if(strcmp(shader_type, "DIFFUSE_COLOR")==0)
                return SHADER_EVAL_DIFFUSE_COLOR;
        else if(strcmp(shader_type, "GLOSSY_COLOR")==0)
diff --git a/intern/cycles/kernel/kernel_bake.h 
b/intern/cycles/kernel/kernel_bake.h
index b3c2450d10e..79e6d1b4862 100644
--- a/intern/cycles/kernel/kernel_bake.h
+++ b/intern/cycles/kernel/kernel_bake.h
@@ -330,15 +330,30 @@ ccl_device void kernel_bake_evaluate(KernelGlobals *kg, 
ccl_global uint4 *input,
        switch(type) {
                /* data passes */
                case SHADER_EVAL_NORMAL:
+               case SHADER_EVAL_ROUGHNESS:
+               case SHADER_EVAL_EMISSION:
                {
-                       float3 N = sd.N;
-                       if((sd.flag & SD_HAS_BUMP)) {
-                               shader_eval_surface(kg, &sd, &state, 0);
-                               N = shader_bsdf_average_normal(kg, &sd);
+                       if(type != SHADER_EVAL_NORMAL || (sd.flag & 
SD_HAS_BUMP)) {
+                               int path_flag = (type == SHADER_EVAL_EMISSION) 
? PATH_RAY_EMISSION : 0;
+                               shader_eval_surface(kg, &sd, &state, path_flag);
                        }
 
-                       /* encoding: normal = (2 * color) - 1 */
-                       out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
+                       if(type == SHADER_EVAL_NORMAL) {
+                               float3 N = sd.N;
+                               if(sd.flag & SD_HAS_BUMP) {
+                                       N = shader_bsdf_average_normal(kg, &sd);
+                               }
+
+                               /* encoding: normal = (2 * color) - 1 */
+                               out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
+                       }
+                       else if(type == SHADER_EVAL_ROUGHNESS) {
+                               float roughness = 
shader_bsdf_average_roughness(&sd);
+                               out = make_float3(roughness, roughness, 
roughness);
+                       }
+                       else {
+                               out = shader_emissive_eval(kg, &sd);
+                       }
                        break;
                }
                case SHADER_EVAL_UV:
@@ -346,13 +361,6 @@ ccl_device void kernel_bake_evaluate(KernelGlobals *kg, 
ccl_global uint4 *input,
                        out = primitive_uv(kg, &sd);
                        break;
                }
-               case SHADER_EVAL_EMISSION:
-               {
-                       shader_eval_surface(kg, &sd, &state, PATH_RAY_EMISSION);
-                       out = shader_emissive_eval(kg, &sd);
-                       break;
-               }
-
 #ifdef __PASSES__
                /* light passes */
                case SHADER_EVAL_AO:
diff --git a/intern/cycles/kernel/kernel_shader.h 
b/intern/cycles/kernel/kernel_shader.h
index fc8d06fc33d..937a50cba8b 100644
--- a/intern/cycles/kernel/kernel_shader.h
+++ b/intern/cycles/kernel/kernel_shader.h
@@ -763,6 +763,26 @@ ccl_device int shader_bsdf_sample_closure(KernelGlobals 
*kg, ShaderData *sd,
        return label;
 }
 
+ccl_device float shader_bsdf_average_roughness(ShaderData *sd)
+{
+       float roughness = 0.0f;
+       float sum_weight = 0.0f;
+
+       for(int i = 0; i < sd->num_closure; i++) {
+               ShaderClosure *sc = &sd->closure[i];
+
+               if(CLOSURE_IS_BSDF(sc->type)) {
+                       /* sqrt once to undo the squaring from multiplying 
roughness on the
+                        * two axes, and once for the squared roughness 
convention. */
+                       float weight = fabsf(average(sc->weight));
+                       roughness += weight * 
sqrtf(safe_sqrtf(bsdf_get_roughness_squared(sc)));
+                       sum_weight += weight;
+               }
+       }
+
+       return (sum_weight > 0.0f) ? roughness / sum_weight : 0.0f;
+}
+
 ccl_device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float 
roughness)
 {
        for(int i = 0; i < sd->num_closure; i++) {
@@ -875,7 +895,7 @@ ccl_device float3 shader_bsdf_average_normal(KernelGlobals 
*kg, ShaderData *sd)
        for(int i = 0; i < sd->num_closure; i++) {
                ShaderClosure *sc = &sd->closure[i];
                if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type))
-                       N += sc->N*average(sc->weight);
+                       N += sc->N*fabsf(average(sc->weight));
        }
 
        return (is_zero(N))? sd->N : normalize(N);
@@ -892,11 +912,11 @@ ccl_device float3 shader_bsdf_ao(KernelGlobals *kg, 
ShaderData *sd, float ao_fac
                if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
                        const DiffuseBsdf *bsdf = (const DiffuseBsdf*)sc;
                        eval += sc->weight*ao_factor;
-                       N += bsdf->N*average(sc->weight);
+                       N += bsdf->N*fabsf(average(sc->weight));
                }
                else if(CLOSURE_IS_AMBIENT_OCCLUSION(sc->type)) {
                        eval += sc->weight;
-                       N += sd->N*average(sc->weight);
+                       N += sd->N*fabsf(average(sc->weight));
                }
        }
 
diff --git a/intern/cycles/kernel/kernel_types.h 
b/intern/cycles/kernel/kernel_types.h
index 977ceac12ea..11657003259 100644
--- a/intern/cycles/kernel/kernel_types.h
+++ b/intern/cycles/kernel/kernel_types.h
@@ -263,6 +263,7 @@ typedef enum ShaderEvalType {
        /* data passes */
        SHADER_EVAL_NORMAL,
        SHADER_EVAL_UV,
+       SHADER_EVAL_ROUGHNESS,
        SHADER_EVAL_DIFFUSE_COLOR,
        SHADER_EVAL_GLOSSY_COLOR,
        SHADER_EVAL_TRANSMISSION_COLOR,
diff --git a/intern/cycles/render/bake.cpp b/intern/cycles/render/bake.cpp
index 1fef7a0188f..927e04abc7f 100644
--- a/intern/cycles/render/bake.cpp
+++ b/intern/cycles/render/bake.cpp
@@ -247,7 +247,7 @@ void BakeManager::device_free(Device * /*device*/, 
DeviceScene * /*dscene*/)
 
 int BakeManager::aa_samples(Scene *scene, BakeData *bake_data, ShaderEvalType 
type)
 {
-       if(type == SHADER_EVAL_UV) {
+       if(type == SHADER_EVAL_UV || type == SHADER_EVAL_ROUGHNESS) {
                return 1;
        }
        else if(type == SHADER_EVAL_NORMAL) {
diff --git a/source/blender/makesdna/DNA_scene_types.h 
b/source/blender/makesdna/DNA_scene_types.h
index 1cc5cbf8c42..b41e649b628 100644
--- a/source/blender/makesdna/DNA_scene_types.h
+++ b/source/blender/makesdna/DNA_scene_types.h
@@ -251,6 +251,7 @@ typedef enum eScenePassType {
        SCE_PASS_SUBSURFACE_DIRECT        = (1 << 28),
        SCE_PASS_SUBSURFACE_INDIRECT      = (1 << 29),
        SCE_PASS_SUBSURFACE_COLOR         = (1 << 30),
+       SCE_PASS_ROUGHNESS                = (1 << 31),
 } eScenePassType;
 
 #define RE_PASSNAME_COMBINED "Combined"
diff --git a/source/blender/makesrna/intern/rna_render.c 
b/source/blender/makesrna/intern/rna_render.c
index d3b3ed51981..98ae6eb0480 100644
--- a/source/blender/makesrna/intern/rna_render.c
+++ b/source/blender/makesrna/intern/rna_render.c
@@ -89,6 +89,7 @@ const EnumPropertyItem rna_enum_bake_pass_type_items[] = {
        {SCE_PASS_SHADOW, "SHADOW", 0, "Shadow", ""},
        {SCE_PASS_NORMAL, "NORMAL", 0, "Normal", ""},
        {SCE_PASS_UV, "UV", 0, "UV", ""},
+       {SCE_PASS_ROUGHNESS, "ROUGHNESS", 0, "ROUGHNESS", ""},
        {SCE_PASS_EMIT, "EMIT", 0, "Emit", ""},
        {SCE_PASS_ENVIRONMENT, "ENVIRONMENT", 0, "Environment", ""},
        {SCE_PASS_DIFFUSE_COLOR, "DIFFUSE", 0, "Diffuse", ""},

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to