Cycles: Use native saturate function for CUDA
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 27 Apr 2015 19:13:03 +0000 (00:13 +0500)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 27 Apr 2015 19:38:32 +0000 (00:38 +0500)
This more a workaround for CUDA optimizer which can't optimize clamp(x, 0, 1)
into a single instruction and uses 4 instructions instead.

Original patch by @lockal with own modification:

  Don't make changes outside of the kernel. They don't make any difference
  anyway and term saturate() has a bit different meaning outside of kernel.

This gives around 2% of speedup in Barcelona file, but in more complex shader
setups with lots of math nodes with clamping speedup could be much nicer.

Subscribers: dingto

Projects: #cycles

Differential Revision: https://developer.blender.org/D1224

19 files changed:
intern/cycles/kernel/closure/bsdf_microfacet.h
intern/cycles/kernel/closure/bsdf_oren_nayar.h
intern/cycles/kernel/closure/bsdf_toon.h
intern/cycles/kernel/closure/bssrdf.h
intern/cycles/kernel/geom/geom_curve.h
intern/cycles/kernel/kernel_film.h
intern/cycles/kernel/kernel_globals.h
intern/cycles/kernel/kernel_passes.h
intern/cycles/kernel/svm/svm_brick.h
intern/cycles/kernel/svm/svm_closure.h
intern/cycles/kernel/svm/svm_gradient.h
intern/cycles/kernel/svm/svm_image.h
intern/cycles/kernel/svm/svm_math_util.h
intern/cycles/kernel/svm/svm_mix.h
intern/cycles/kernel/svm/svm_musgrave.h
intern/cycles/kernel/svm/svm_ramp.h
intern/cycles/render/buffers.cpp
intern/cycles/render/nodes.cpp
intern/cycles/util/util_math.h

index ca68b1eb9409396b1979930c1e5ca8e1fda03581..6a50bbed3b3a26cbd8f1893b35a0a4c0d0356e80 100644 (file)
@@ -235,7 +235,7 @@ ccl_device_inline float3 microfacet_sample_stretched(
 
 ccl_device int bsdf_microfacet_ggx_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
        sc->data1 = sc->data0; /* alpha_y */
        
        sc->type = CLOSURE_BSDF_MICROFACET_GGX_ID;
@@ -245,8 +245,8 @@ ccl_device int bsdf_microfacet_ggx_setup(ShaderClosure *sc)
 
 ccl_device int bsdf_microfacet_ggx_aniso_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
-       sc->data1 = clamp(sc->data1, 0.0f, 1.0f); /* alpha_y */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
+       sc->data1 = saturate(sc->data1); /* alpha_y */
        
        sc->type = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
 
@@ -255,7 +255,7 @@ ccl_device int bsdf_microfacet_ggx_aniso_setup(ShaderClosure *sc)
 
 ccl_device int bsdf_microfacet_ggx_refraction_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
        sc->data1 = sc->data0; /* alpha_y */
 
        sc->type = CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID;
@@ -588,7 +588,7 @@ ccl_device int bsdf_microfacet_ggx_sample(KernelGlobals *kg, const ShaderClosure
 
 ccl_device int bsdf_microfacet_beckmann_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
        sc->data1 = sc->data0; /* alpha_y */
 
        sc->type = CLOSURE_BSDF_MICROFACET_BECKMANN_ID;
@@ -597,8 +597,8 @@ ccl_device int bsdf_microfacet_beckmann_setup(ShaderClosure *sc)
 
 ccl_device int bsdf_microfacet_beckmann_aniso_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
-       sc->data1 = clamp(sc->data1, 0.0f, 1.0f); /* alpha_y */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
+       sc->data1 = saturate(sc->data1); /* alpha_y */
 
        sc->type = CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID;
        return SD_BSDF|SD_BSDF_HAS_EVAL;
@@ -606,7 +606,7 @@ ccl_device int bsdf_microfacet_beckmann_aniso_setup(ShaderClosure *sc)
 
 ccl_device int bsdf_microfacet_beckmann_refraction_setup(ShaderClosure *sc)
 {
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f); /* alpha_x */
+       sc->data0 = saturate(sc->data0); /* alpha_x */
        sc->data1 = sc->data0; /* alpha_y */
 
        sc->type = CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID;
index c476d4ca4e27e277844f55c03a2f471280b18cde..61b7cb11b02917513e64833ea3e2677fda90da05 100644 (file)
@@ -37,7 +37,7 @@ ccl_device int bsdf_oren_nayar_setup(ShaderClosure *sc)
 
        sc->type = CLOSURE_BSDF_OREN_NAYAR_ID;
 
-       sigma = clamp(sigma, 0.0f, 1.0f);
+       sigma = saturate(sigma);
 
        float div = 1.0f / (M_PI_F + ((3.0f * M_PI_F - 4.0f) / 6.0f) * sigma);
 
index df03942638f885d70fd8fb8593e6c6762d3548ef..e5b6ab93a649ff9cac3709fad76867d2d2cbcfc2 100644 (file)
@@ -40,8 +40,8 @@ CCL_NAMESPACE_BEGIN
 ccl_device int bsdf_diffuse_toon_setup(ShaderClosure *sc)
 {
        sc->type = CLOSURE_BSDF_DIFFUSE_TOON_ID;
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f);
-       sc->data1 = clamp(sc->data1, 0.0f, 1.0f);
+       sc->data0 = saturate(sc->data0);
+       sc->data1 = saturate(sc->data1);
 
        return SD_BSDF|SD_BSDF_HAS_EVAL;
 }
@@ -120,8 +120,8 @@ ccl_device int bsdf_diffuse_toon_sample(const ShaderClosure *sc, float3 Ng, floa
 ccl_device int bsdf_glossy_toon_setup(ShaderClosure *sc)
 {
        sc->type = CLOSURE_BSDF_GLOSSY_TOON_ID;
-       sc->data0 = clamp(sc->data0, 0.0f, 1.0f);
-       sc->data1 = clamp(sc->data1, 0.0f, 1.0f);
+       sc->data0 = saturate(sc->data0);
+       sc->data1 = saturate(sc->data1);
 
        return SD_BSDF|SD_BSDF_HAS_EVAL;
 }
index b284826200196e17434d863625c66a3703b3c389..f817dcd5f2d5e5b099439eabfe6825703a0c50c6 100644 (file)
@@ -30,8 +30,8 @@ ccl_device int bssrdf_setup(ShaderClosure *sc, ClosureType type)
                return flag;
        }
        else {
-               sc->data1 = clamp(sc->data1, 0.0f, 1.0f); /* texture blur */
-               sc->T.x = clamp(sc->T.x, 0.0f, 1.0f); /* sharpness */
+               sc->data1 = saturate(sc->data1); /* texture blur */
+               sc->T.x = saturate(sc->T.x); /* sharpness */
                sc->type = type;
 
                return SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSSRDF;
@@ -168,7 +168,7 @@ ccl_device float bssrdf_cubic_quintic_root_find(float xi)
                if(fabsf(f) < tolerance || f_ == 0.0f)
                        break;
 
-               x = clamp(x - f/f_, 0.0f, 1.0f);
+               x = saturate(x - f/f_);
        }
 
        return x;
index c13858fc461e77c9f636a6d504e8c3492390597c..ec6c790d9f73520ae2cd6e2b98a810a13d7ce33d 100644 (file)
@@ -465,7 +465,7 @@ ccl_device_inline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Intersect
                                        continue;
                                }
                                w = -(p_st.x * tg.x + p_st.y * tg.y) / w;
-                               w = clamp((float)w, 0.0f, 1.0f);
+                               w = saturate(w);
 
                                /* compute u on the curve segment */
                                u = i_st * (1 - w) + i_en * w;
@@ -577,7 +577,7 @@ ccl_device_inline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Intersect
                                }
 
                                float w = (zcentre + (tg.z * correction)) * invl;
-                               w = clamp((float)w, 0.0f, 1.0f);
+                               w = saturate(w);
                                /* compute u on the curve segment */
                                u = i_st * (1 - w) + i_en * w;
 
index 4668b40b86dfcebda07e9255607a01f21770995b..f9e9b41389867df73a8f1ea3f0378f6e3997e194 100644 (file)
@@ -27,7 +27,7 @@ ccl_device float4 film_map(KernelGlobals *kg, float4 irradiance, float scale)
        result.z = color_scene_linear_to_srgb(result.z*exposure);
 
        /* clamp since alpha might be > 1.0 due to russian roulette */
-       result.w = clamp(result.w, 0.0f, 1.0f);
+       result.w = saturate(result.w);
 
        return result;
 }
@@ -37,10 +37,10 @@ ccl_device uchar4 film_float_to_byte(float4 color)
        uchar4 result;
 
        /* simple float to byte conversion */
-       result.x = (uchar)clamp(color.x*255.0f, 0.0f, 255.0f);
-       result.y = (uchar)clamp(color.y*255.0f, 0.0f, 255.0f);
-       result.z = (uchar)clamp(color.z*255.0f, 0.0f, 255.0f);
-       result.w = (uchar)clamp(color.w*255.0f, 0.0f, 255.0f);
+       result.x = (uchar)(saturate(color.x)*255.0f);
+       result.y = (uchar)(saturate(color.y)*255.0f);
+       result.z = (uchar)(saturate(color.z)*255.0f);
+       result.w = (uchar)(saturate(color.w)*255.0f);
 
        return result;
 }
index 0a9753baca2ea30fb8aeee87d9ee52bd4dc163dc..be2c879adb9594a6afbcb24976e17c006991f88a 100644 (file)
@@ -94,7 +94,7 @@ typedef struct KernelGlobals {
 
 ccl_device float lookup_table_read(KernelGlobals *kg, float x, int offset, int size)
 {
-       x = clamp(x, 0.0f, 1.0f)*(size-1);
+       x = saturate(x)*(size-1);
 
        int index = min(float_to_int(x), size-1);
        int nindex = min(index+1, size-1);
@@ -110,7 +110,7 @@ ccl_device float lookup_table_read(KernelGlobals *kg, float x, int offset, int s
 
 ccl_device float lookup_table_read_2D(KernelGlobals *kg, float x, float y, int offset, int xsize, int ysize)
 {
-       y = clamp(y, 0.0f, 1.0f)*(ysize-1);
+       y = saturate(y)*(ysize-1);
 
        int index = min(float_to_int(y), ysize-1);
        int nindex = min(index+1, ysize-1);
index 6bb39ee485dbeb4d4754bdcdd788a3d291ac9668..8910e26fcc63aa17e443b9f1e6d2d5581a5c43d7 100644 (file)
@@ -102,7 +102,7 @@ ccl_device_inline void kernel_write_data_passes(KernelGlobals *kg, ccl_global fl
                float mist_inv_depth = kernel_data.film.mist_inv_depth;
 
                float depth = camera_distance(kg, sd->P);
-               float mist = clamp((depth - mist_start)*mist_inv_depth, 0.0f, 1.0f);
+               float mist = saturate((depth - mist_start)*mist_inv_depth);
 
                /* falloff */
                float mist_falloff = kernel_data.film.mist_falloff;
index 33a2a5c7598dcd5241642b0aaa84790b0a1690a4..fcf8f47b77e0379b7ece6160ff71a4f4dd5f694f 100644 (file)
@@ -47,7 +47,7 @@ ccl_device_noinline float2 svm_brick(float3 p, float mortar_size, float bias,
        y = p.y - row_height*rownum;
 
        return make_float2(
-               clamp((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias), 0.0f, 1.0f),
+               saturate((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias)),
 
                (x < mortar_size || y < mortar_size ||
                x > (brick_width - mortar_size) ||
index 8afdb73fb38bc0c76bdb7135bccaaeeb208803b4..0d2d155f827eaa3c7e23b85f5a1e512aeba851af 100644 (file)
@@ -347,7 +347,7 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
                                sc->N = N;
 
                                /* sigma */
-                               sc->data0 = clamp(param1, 0.0f, 1.0f);
+                               sc->data0 = saturate(param1);
                                sc->data1 = 0.0f;
                                sc->data2 = 0.0f;
                                sd->flag |= bsdf_ashikhmin_velvet_setup(sc);
@@ -655,7 +655,7 @@ ccl_device void svm_node_mix_closure(ShaderData *sd, float *stack, uint4 node)
        decode_node_uchar4(node.y, &weight_offset, &in_weight_offset, &weight1_offset, &weight2_offset);
 
        float weight = stack_load_float(stack, weight_offset);
-       weight = clamp(weight, 0.0f, 1.0f);
+       weight = saturate(weight);
 
        float in_weight = (stack_valid(in_weight_offset))? stack_load_float(stack, in_weight_offset): 1.0f;
 
index a5e385faddcb2cacb4deb5a15b110d46e008ef2e..53d7b4f812cb0c281441387856de895636c5b056 100644 (file)
@@ -66,7 +66,7 @@ ccl_device void svm_node_tex_gradient(ShaderData *sd, float *stack, uint4 node)
        float3 co = stack_load_float3(stack, co_offset);
 
        float f = svm_gradient(co, (NodeGradientType)type);
-       f = clamp(f, 0.0f, 1.0f);
+       f = saturate(f);
 
        if(stack_valid(fac_offset))
                stack_store_float(stack, fac_offset, f);
index ccc1e3df730ff01148481ffa84b677632dc10a25..08a6c01162c76d52cdbc96fed1edd32b87524a21 100644 (file)
@@ -433,17 +433,17 @@ ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float
                /* in case of blending, test for mixes between two textures */
                if(N.z < (1.0f - limit)*(N.y + N.x)) {
                        weight.x = N.x/(N.x + N.y);
-                       weight.x = clamp((weight.x - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
+                       weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
                        weight.y = 1.0f - weight.x;
                }
                else if(N.x < (1.0f - limit)*(N.y + N.z)) {
                        weight.y = N.y/(N.y + N.z);
-                       weight.y = clamp((weight.y - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
+                       weight.y = saturate((weight.y - 0.5f*(1.0f - blend))/blend);
                        weight.z = 1.0f - weight.y;
                }
                else if(N.y < (1.0f - limit)*(N.x + N.z)) {
                        weight.x = N.x/(N.x + N.z);
-                       weight.x = clamp((weight.x - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
+                       weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
                        weight.z = 1.0f - weight.x;
                }
                else {
index 39cc14d5e8edbbbaadc603fcd4591e400e37236b..ff9e662e93194229f09421ef9d2ea29b31a1d039 100644 (file)
@@ -97,7 +97,7 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2)
        else if(type == NODE_MATH_ABSOLUTE)
                Fac = fabsf(Fac1);
        else if(type == NODE_MATH_CLAMP)
-               Fac = clamp(Fac1, 0.0f, 1.0f);
+               Fac = saturate(Fac1);
        else
                Fac = 0.0f;
        
index b6b1966cb3b22ee1b233e2cbbbdaef2ab64b8bc5..6111214acba97ed4c078fa1b0d8bc23d9acf47ea 100644 (file)
@@ -254,16 +254,16 @@ ccl_device float3 svm_mix_clamp(float3 col)
 {
        float3 outcol = col;
 
-       outcol.x = clamp(col.x, 0.0f, 1.0f);
-       outcol.y = clamp(col.y, 0.0f, 1.0f);
-       outcol.z = clamp(col.z, 0.0f, 1.0f);
+       outcol.x = saturate(col.x);
+       outcol.y = saturate(col.y);
+       outcol.z = saturate(col.z);
 
        return outcol;
 }
 
 ccl_device float3 svm_mix(NodeMix type, float fac, float3 c1, float3 c2)
 {
-       float t = clamp(fac, 0.0f, 1.0f);
+       float t = saturate(fac);
 
        switch(type) {
                case NODE_MIX_BLEND: return svm_mix_blend(t, c1, c2);
index 2f81ddaa74cca5f61590dfba89767fdaf9c02b5e..2a0a8e0e87db08aa4502005b689ef87c2a8a5138 100644 (file)
@@ -168,7 +168,7 @@ ccl_device_noinline float noise_musgrave_ridged_multi_fractal(float3 p, NodeNois
 
        for(i = 1; i < float_to_int(octaves); i++) {
                p *= lacunarity;
-               weight = clamp(signal * gain, 0.0f, 1.0f);
+               weight = saturate(signal * gain);
                signal = offset - fabsf(snoise(p));
                signal *= signal;
                signal *= weight;
index 998f649a571ebb339cb9d534f5f9a811cd36ce15..062ab013b1f72d9dc338048bc6221f90c0753d49 100644 (file)
@@ -21,7 +21,7 @@ CCL_NAMESPACE_BEGIN
 
 ccl_device float4 rgb_ramp_lookup(KernelGlobals *kg, int offset, float f, bool interpolate)
 {
-       f = clamp(f, 0.0f, 1.0f)*(RAMP_TABLE_SIZE-1);
+       f = saturate(f)*(RAMP_TABLE_SIZE-1);
 
        /* clamp int as well in case of NaN */
        int i = clamp(float_to_int(f), 0, RAMP_TABLE_SIZE-1);
index 5202bf5862ca829cbab4df4bccba55a509484bac..dc00da1bf8854df01e4e7c3f977eac169918438b 100644 (file)
@@ -187,7 +187,7 @@ bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int
                        else if(type == PASS_MIST) {
                                for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
                                        float f = *in;
-                                       pixels[0] = clamp(f*scale_exposure, 0.0f, 1.0f);
+                                       pixels[0] = saturate(f*scale_exposure);
                                }
                        }
 #ifdef WITH_CYCLES_DEBUG
@@ -298,7 +298,7 @@ bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int
                                        pixels[2] = f.z*scale_exposure;
 
                                        /* clamp since alpha might be > 1.0 due to russian roulette */
-                                       pixels[3] = clamp(f.w*scale, 0.0f, 1.0f);
+                                       pixels[3] = saturate(f.w*scale);
                                }
                        }
                }
index 747bafcc9044c218f23437c02019552704bea1e6..8529855ff2efc1748327bb0f913f845171a3a4b8 100644 (file)
@@ -3752,7 +3752,7 @@ void MathNode::compile(SVMCompiler& compiler)
                                                 value1_in->value.x,
                                                 value2_in->value.x);
                if(use_clamp) {
-                       optimized_value = clamp(optimized_value, 0.0f, 1.0f);
+                       optimized_value = saturate(optimized_value);
                }
                compiler.add_node(NODE_VALUE_F,
                                  __float_as_int(optimized_value),
index 6208608a233268db962b6924f1a445382cb86bc6..f48625ec95d176a9ee0dee1b06a0450c810dfe09 100644 (file)
@@ -175,6 +175,15 @@ ccl_device_inline float clamp(float a, float mn, float mx)
 
 #endif
 
+#ifndef __KERNEL_CUDA__
+
+ccl_device_inline float saturate(float a)
+{
+       return clamp(a, 0.0f, 1.0f);
+}
+
+#endif
+
 ccl_device_inline int float_to_int(float f)
 {
        return (int)f;