diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-04-27 22:13:03 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-04-27 22:38:32 +0300 |
commit | ae7d84dbc1936ef7ddd00f9c22d074389f97f04f (patch) | |
tree | 7103596bc78785ed769e90c1570d654cc14a839d /intern/cycles | |
parent | 278ff15c7f3c4a91ba049b7a13ecf8a4183ebf67 (diff) |
Cycles: Use native saturate function for CUDA
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
Diffstat (limited to 'intern/cycles')
-rw-r--r-- | intern/cycles/kernel/closure/bsdf_microfacet.h | 16 | ||||
-rw-r--r-- | intern/cycles/kernel/closure/bsdf_oren_nayar.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/closure/bsdf_toon.h | 8 | ||||
-rw-r--r-- | intern/cycles/kernel/closure/bssrdf.h | 6 | ||||
-rw-r--r-- | intern/cycles/kernel/geom/geom_curve.h | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/kernel_film.h | 10 | ||||
-rw-r--r-- | intern/cycles/kernel/kernel_globals.h | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/kernel_passes.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_brick.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_closure.h | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_gradient.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_image.h | 6 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_math_util.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_mix.h | 8 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_musgrave.h | 2 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_ramp.h | 2 | ||||
-rw-r--r-- | intern/cycles/render/buffers.cpp | 4 | ||||
-rw-r--r-- | intern/cycles/render/nodes.cpp | 2 | ||||
-rw-r--r-- | intern/cycles/util/util_math.h | 9 |
19 files changed, 52 insertions, 43 deletions
diff --git a/intern/cycles/kernel/closure/bsdf_microfacet.h b/intern/cycles/kernel/closure/bsdf_microfacet.h index ca68b1eb940..6a50bbed3b3 100644 --- a/intern/cycles/kernel/closure/bsdf_microfacet.h +++ b/intern/cycles/kernel/closure/bsdf_microfacet.h @@ -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; diff --git a/intern/cycles/kernel/closure/bsdf_oren_nayar.h b/intern/cycles/kernel/closure/bsdf_oren_nayar.h index c476d4ca4e2..61b7cb11b02 100644 --- a/intern/cycles/kernel/closure/bsdf_oren_nayar.h +++ b/intern/cycles/kernel/closure/bsdf_oren_nayar.h @@ -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); diff --git a/intern/cycles/kernel/closure/bsdf_toon.h b/intern/cycles/kernel/closure/bsdf_toon.h index df03942638f..e5b6ab93a64 100644 --- a/intern/cycles/kernel/closure/bsdf_toon.h +++ b/intern/cycles/kernel/closure/bsdf_toon.h @@ -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; } diff --git a/intern/cycles/kernel/closure/bssrdf.h b/intern/cycles/kernel/closure/bssrdf.h index b2848262001..f817dcd5f2d 100644 --- a/intern/cycles/kernel/closure/bssrdf.h +++ b/intern/cycles/kernel/closure/bssrdf.h @@ -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; diff --git a/intern/cycles/kernel/geom/geom_curve.h b/intern/cycles/kernel/geom/geom_curve.h index c13858fc461..ec6c790d9f7 100644 --- a/intern/cycles/kernel/geom/geom_curve.h +++ b/intern/cycles/kernel/geom/geom_curve.h @@ -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; diff --git a/intern/cycles/kernel/kernel_film.h b/intern/cycles/kernel/kernel_film.h index 4668b40b86d..f9e9b413898 100644 --- a/intern/cycles/kernel/kernel_film.h +++ b/intern/cycles/kernel/kernel_film.h @@ -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; } diff --git a/intern/cycles/kernel/kernel_globals.h b/intern/cycles/kernel/kernel_globals.h index 0a9753baca2..be2c879adb9 100644 --- a/intern/cycles/kernel/kernel_globals.h +++ b/intern/cycles/kernel/kernel_globals.h @@ -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); diff --git a/intern/cycles/kernel/kernel_passes.h b/intern/cycles/kernel/kernel_passes.h index 6bb39ee485d..8910e26fcc6 100644 --- a/intern/cycles/kernel/kernel_passes.h +++ b/intern/cycles/kernel/kernel_passes.h @@ -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; diff --git a/intern/cycles/kernel/svm/svm_brick.h b/intern/cycles/kernel/svm/svm_brick.h index 33a2a5c7598..fcf8f47b77e 100644 --- a/intern/cycles/kernel/svm/svm_brick.h +++ b/intern/cycles/kernel/svm/svm_brick.h @@ -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) || diff --git a/intern/cycles/kernel/svm/svm_closure.h b/intern/cycles/kernel/svm/svm_closure.h index 8afdb73fb38..0d2d155f827 100644 --- a/intern/cycles/kernel/svm/svm_closure.h +++ b/intern/cycles/kernel/svm/svm_closure.h @@ -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; diff --git a/intern/cycles/kernel/svm/svm_gradient.h b/intern/cycles/kernel/svm/svm_gradient.h index a5e385faddc..53d7b4f812c 100644 --- a/intern/cycles/kernel/svm/svm_gradient.h +++ b/intern/cycles/kernel/svm/svm_gradient.h @@ -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); diff --git a/intern/cycles/kernel/svm/svm_image.h b/intern/cycles/kernel/svm/svm_image.h index ccc1e3df730..08a6c01162c 100644 --- a/intern/cycles/kernel/svm/svm_image.h +++ b/intern/cycles/kernel/svm/svm_image.h @@ -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 { diff --git a/intern/cycles/kernel/svm/svm_math_util.h b/intern/cycles/kernel/svm/svm_math_util.h index 39cc14d5e8e..ff9e662e931 100644 --- a/intern/cycles/kernel/svm/svm_math_util.h +++ b/intern/cycles/kernel/svm/svm_math_util.h @@ -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; diff --git a/intern/cycles/kernel/svm/svm_mix.h b/intern/cycles/kernel/svm/svm_mix.h index b6b1966cb3b..6111214acba 100644 --- a/intern/cycles/kernel/svm/svm_mix.h +++ b/intern/cycles/kernel/svm/svm_mix.h @@ -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); diff --git a/intern/cycles/kernel/svm/svm_musgrave.h b/intern/cycles/kernel/svm/svm_musgrave.h index 2f81ddaa74c..2a0a8e0e87d 100644 --- a/intern/cycles/kernel/svm/svm_musgrave.h +++ b/intern/cycles/kernel/svm/svm_musgrave.h @@ -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; diff --git a/intern/cycles/kernel/svm/svm_ramp.h b/intern/cycles/kernel/svm/svm_ramp.h index 998f649a571..062ab013b1f 100644 --- a/intern/cycles/kernel/svm/svm_ramp.h +++ b/intern/cycles/kernel/svm/svm_ramp.h @@ -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); diff --git a/intern/cycles/render/buffers.cpp b/intern/cycles/render/buffers.cpp index 5202bf5862c..dc00da1bf88 100644 --- a/intern/cycles/render/buffers.cpp +++ b/intern/cycles/render/buffers.cpp @@ -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); } } } diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 747bafcc904..8529855ff2e 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -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), diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h index 6208608a233..f48625ec95d 100644 --- a/intern/cycles/util/util_math.h +++ b/intern/cycles/util/util_math.h @@ -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; |