From 68627626854c27c2135cab72b48b648cb638c8cb Mon Sep 17 00:00:00 2001 From: Lukas Stockner Date: Thu, 24 May 2018 02:51:41 +0200 Subject: Cycles/Compositor: Add arctan2 operation to the Math node The Math node currently has the normal atan() function, but for actual angles this is fairly useless without additional nodes to handle the signs. Since the node has two inputs anyways, it only makes sense to add an arctan2 option. Reviewers: sergey, brecht Differential Revision: https://developer.blender.org/D3430 --- intern/cycles/kernel/kernel_compat_opencl.h | 1 + intern/cycles/kernel/shaders/node_math.osl | 2 ++ intern/cycles/kernel/svm/svm_math_util.h | 2 ++ intern/cycles/kernel/svm/svm_types.h | 1 + intern/cycles/render/nodes.cpp | 1 + source/blender/compositor/nodes/COM_MathNode.cpp | 3 +++ .../blender/compositor/operations/COM_MathBaseOperation.cpp | 13 +++++++++++++ .../blender/compositor/operations/COM_MathBaseOperation.h | 6 ++++++ source/blender/gpu/shaders/gpu_shader_material.glsl | 5 +++++ source/blender/makesdna/DNA_node_types.h | 1 + source/blender/makesrna/intern/rna_nodetree.c | 1 + source/blender/nodes/shader/nodes/node_shader_math.c | 7 +++++++ source/blender/nodes/texture/nodes/node_texture_math.c | 6 ++++++ 13 files changed, 49 insertions(+) diff --git a/intern/cycles/kernel/kernel_compat_opencl.h b/intern/cycles/kernel/kernel_compat_opencl.h index 671c47e2225..ff7b69ab08f 100644 --- a/intern/cycles/kernel/kernel_compat_opencl.h +++ b/intern/cycles/kernel/kernel_compat_opencl.h @@ -116,6 +116,7 @@ #define asinf(x) asin(((float)(x))) #define acosf(x) acos(((float)(x))) #define atanf(x) atan(((float)(x))) +#define atan2f(x, y) atan2(((float)(x)), ((float)(y))) #define floorf(x) floor(((float)(x))) #define ceilf(x) ceil(((float)(x))) #define hypotf(x, y) hypot(((float)(x)), ((float)(y))) diff --git a/intern/cycles/kernel/shaders/node_math.osl b/intern/cycles/kernel/shaders/node_math.osl index f309ef7c6f3..c5fcbc311d3 100644 --- a/intern/cycles/kernel/shaders/node_math.osl +++ b/intern/cycles/kernel/shaders/node_math.osl @@ -95,6 +95,8 @@ shader node_math( Value = safe_modulo(Value1, Value2); else if (type == "absolute") Value = fabs(Value1); + else if (type == "arctan2") + Value = atan2(Value1, Value2); if (use_clamp) Value = clamp(Value, 0.0, 1.0); diff --git a/intern/cycles/kernel/svm/svm_math_util.h b/intern/cycles/kernel/svm/svm_math_util.h index 1ce7777aac3..8e6bc73ddc7 100644 --- a/intern/cycles/kernel/svm/svm_math_util.h +++ b/intern/cycles/kernel/svm/svm_math_util.h @@ -92,6 +92,8 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2) Fac = safe_modulo(Fac1, Fac2); else if(type == NODE_MATH_ABSOLUTE) Fac = fabsf(Fac1); + else if(type == NODE_MATH_ARCTAN2) + Fac = atan2f(Fac1, Fac2); else if(type == NODE_MATH_CLAMP) Fac = saturate(Fac1); else diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index 4c3a5975fb8..dc62e25b340 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -259,6 +259,7 @@ typedef enum NodeMath { NODE_MATH_GREATER_THAN, NODE_MATH_MODULO, NODE_MATH_ABSOLUTE, + NODE_MATH_ARCTAN2, NODE_MATH_CLAMP /* used for the clamp UI option */ } NodeMath; diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 01399c85bc0..c468924fa66 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -4953,6 +4953,7 @@ NODE_DEFINE(MathNode) type_enum.insert("greater_than", NODE_MATH_GREATER_THAN); type_enum.insert("modulo", NODE_MATH_MODULO); type_enum.insert("absolute", NODE_MATH_ABSOLUTE); + type_enum.insert("arctan2", NODE_MATH_ARCTAN2); SOCKET_ENUM(type, "Type", type_enum, NODE_MATH_ADD); SOCKET_BOOLEAN(use_clamp, "Use Clamp", false); diff --git a/source/blender/compositor/nodes/COM_MathNode.cpp b/source/blender/compositor/nodes/COM_MathNode.cpp index eb6bb2caf56..0fb6933afe7 100644 --- a/source/blender/compositor/nodes/COM_MathNode.cpp +++ b/source/blender/compositor/nodes/COM_MathNode.cpp @@ -86,6 +86,9 @@ void MathNode::convertToOperations(NodeConverter &converter, const CompositorCon case NODE_MATH_ABS: operation = new MathAbsoluteOperation(); break; + case NODE_MATH_ATAN2: + operation = new MathArcTan2Operation(); + break; } if (operation) { diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.cpp b/source/blender/compositor/operations/COM_MathBaseOperation.cpp index 32a1e77b9a7..dbc91980acd 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.cpp +++ b/source/blender/compositor/operations/COM_MathBaseOperation.cpp @@ -343,3 +343,16 @@ void MathAbsoluteOperation::executePixelSampled(float output[4], float x, float clampIfNeeded(output); } + +void MathArcTan2Operation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + output[0] = atan2(inputValue1[0], inputValue2[0]); + + clampIfNeeded(output); +} \ No newline at end of file diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.h b/source/blender/compositor/operations/COM_MathBaseOperation.h index 32cd19f1fb9..04019372711 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.h +++ b/source/blender/compositor/operations/COM_MathBaseOperation.h @@ -169,4 +169,10 @@ public: void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); }; +class MathArcTan2Operation : public MathBaseOperation { +public: + MathArcTan2Operation() : MathBaseOperation() {} + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + #endif diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl index a0ca719e207..aace5e95169 100644 --- a/source/blender/gpu/shaders/gpu_shader_material.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material.glsl @@ -413,6 +413,11 @@ void math_abs(float val1, out float outval) outval = abs(val1); } +void math_atan2(float val1, float val2, out float outval) +{ + outval = atan(val1, val2); +} + void squeeze(float val, float width, float center, out float outval) { outval = 1.0 / (1.0 + pow(2.71828183, -((val - center) * width))); diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h index 8ae1a79f8f7..8d3ab29a2fb 100644 --- a/source/blender/makesdna/DNA_node_types.h +++ b/source/blender/makesdna/DNA_node_types.h @@ -1060,6 +1060,7 @@ enum { NODE_MATH_GREATER = 16, NODE_MATH_MOD = 17, NODE_MATH_ABS = 18, + NODE_MATH_ATAN2 = 19, }; /* mix rgb node flags */ diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index 773ca307a8c..f5c464c0758 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -140,6 +140,7 @@ const EnumPropertyItem rna_enum_node_math_items[] = { {NODE_MATH_GREATER, "GREATER_THAN", 0, "Greater Than", ""}, {NODE_MATH_MOD, "MODULO", 0, "Modulo", ""}, {NODE_MATH_ABS, "ABSOLUTE", 0, "Absolute", ""}, + {NODE_MATH_ATAN2, "ARCTAN2", 0, "Arctan2", ""}, {0, NULL, 0, NULL, NULL} }; diff --git a/source/blender/nodes/shader/nodes/node_shader_math.c b/source/blender/nodes/shader/nodes/node_shader_math.c index 2a1e936570d..9fa906729dc 100644 --- a/source/blender/nodes/shader/nodes/node_shader_math.c +++ b/source/blender/nodes/shader/nodes/node_shader_math.c @@ -221,6 +221,11 @@ static void node_shader_exec_math(void *UNUSED(data), int UNUSED(thread), bNode r = fabsf(a); break; } + case NODE_MATH_ATAN2: + { + r = atan2(a, b); + break; + } } if (node->custom2 & SHD_MATH_CLAMP) { CLAMP(r, 0.0f, 1.0f); @@ -235,6 +240,7 @@ static int gpu_shader_math(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED( "math_divide", "math_sine", "math_cosine", "math_tangent", "math_asin", "math_acos", "math_atan", "math_pow", "math_log", "math_min", "math_max", "math_round", "math_less_than", "math_greater_than", "math_modulo", "math_abs", + "math_atan2" }; switch (node->custom1) { @@ -249,6 +255,7 @@ static int gpu_shader_math(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED( case NODE_MATH_LESS: case NODE_MATH_GREATER: case NODE_MATH_MOD: + case NODE_MATH_ATAN2: GPU_stack_link(mat, names[node->custom1], in, out); break; case NODE_MATH_SIN: diff --git a/source/blender/nodes/texture/nodes/node_texture_math.c b/source/blender/nodes/texture/nodes/node_texture_math.c index 19bc16fb82d..d8dc2a62625 100644 --- a/source/blender/nodes/texture/nodes/node_texture_math.c +++ b/source/blender/nodes/texture/nodes/node_texture_math.c @@ -189,6 +189,12 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor break; } + case NODE_MATH_ATAN2: + { + *out = atan2(in0, in1); + break; + } + default: { BLI_assert(0); -- cgit v1.2.3 From b4a8b813993138cc831d4c04f0f94bdafc51e7fe Mon Sep 17 00:00:00 2001 From: Lukas Stockner Date: Sun, 1 Apr 2018 02:10:27 +0200 Subject: Cycles Denoising: Don't use atomics in the accumulation kernel on CPUs The GPU kernel needs to use atomics for accumulation since all offsets are processed in parallel, but on CPUs that's not the case, so we can disable them there for a considerable speedup. --- intern/cycles/kernel/filter/filter_reconstruction.h | 5 +++++ intern/cycles/util/util_math_matrix.h | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/intern/cycles/kernel/filter/filter_reconstruction.h b/intern/cycles/kernel/filter/filter_reconstruction.h index b7bf322f9ce..58740d5b06a 100644 --- a/intern/cycles/kernel/filter/filter_reconstruction.h +++ b/intern/cycles/kernel/filter/filter_reconstruction.h @@ -61,8 +61,13 @@ ccl_device_inline void kernel_filter_construct_gramian(int x, int y, make_int2(x+dx, y+dy), buffer + q_offset, pass_stride, *rank, design_row, transform, stride); +#ifdef __KERNEL_GPU__ math_trimatrix_add_gramian_strided(XtWX, (*rank)+1, design_row, weight, stride); math_vec3_add_strided(XtWY, (*rank)+1, design_row, weight * q_color, stride); +#else + math_trimatrix_add_gramian(XtWX, (*rank)+1, design_row, weight); + math_vec3_add(XtWY, (*rank)+1, design_row, weight * q_color); +#endif } ccl_device_inline void kernel_filter_finalize(int x, int y, diff --git a/intern/cycles/util/util_math_matrix.h b/intern/cycles/util/util_math_matrix.h index 382dad64ea5..9ffcb9659b2 100644 --- a/intern/cycles/util/util_math_matrix.h +++ b/intern/cycles/util/util_math_matrix.h @@ -144,6 +144,18 @@ ccl_device_inline void math_trimatrix_add_gramian_strided(ccl_global float *A, } } +ccl_device_inline void math_trimatrix_add_gramian(ccl_global float *A, + int n, + const float *ccl_restrict v, + float weight) +{ + for(int row = 0; row < n; row++) { + for(int col = 0; col <= row; col++) { + MATHS(A, row, col, 1) += v[row]*v[col]*weight; + } + } +} + /* Transpose matrix A inplace. */ ccl_device_inline void math_matrix_transpose(ccl_global float *A, int n, int stride) { -- cgit v1.2.3 From 5e2f9c5c67f5c1673553bd1e07586ba85fcb8df9 Mon Sep 17 00:00:00 2001 From: Lukas Stockner Date: Thu, 24 May 2018 19:06:50 +0200 Subject: Cycles: Cleanup: Remove duplicated atan2f definition for OpenCL Turns out that atan2f was already defined for OpenCL. --- intern/cycles/kernel/kernel_compat_opencl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/intern/cycles/kernel/kernel_compat_opencl.h b/intern/cycles/kernel/kernel_compat_opencl.h index ff7b69ab08f..671c47e2225 100644 --- a/intern/cycles/kernel/kernel_compat_opencl.h +++ b/intern/cycles/kernel/kernel_compat_opencl.h @@ -116,7 +116,6 @@ #define asinf(x) asin(((float)(x))) #define acosf(x) acos(((float)(x))) #define atanf(x) atan(((float)(x))) -#define atan2f(x, y) atan2(((float)(x)), ((float)(y))) #define floorf(x) floor(((float)(x))) #define ceilf(x) ceil(((float)(x))) #define hypotf(x, y) hypot(((float)(x)), ((float)(y))) -- cgit v1.2.3 From d02335a1950c81fffea815f31e47bca92ab7d33c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 25 May 2018 10:00:33 +0200 Subject: 3D View: add pixelsize function w/o UI scale --- source/blender/editors/include/ED_view3d.h | 1 + source/blender/editors/space_view3d/view3d_project.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h index 2767e041627..aeb1ccfd28a 100644 --- a/source/blender/editors/include/ED_view3d.h +++ b/source/blender/editors/include/ED_view3d.h @@ -216,6 +216,7 @@ eV3DProjStatus ED_view3d_project_float_global(const struct ARegion *ar, const fl eV3DProjStatus ED_view3d_project_float_object(const struct ARegion *ar, const float co[3], float r_co[2], const eV3DProjTest flag); float ED_view3d_pixel_size(const struct RegionView3D *rv3d, const float co[3]); +float ED_view3d_pixel_size_no_ui_scale(const struct RegionView3D *rv3d, const float co[3]); float ED_view3d_calc_zfac(const struct RegionView3D *rv3d, const float co[3], bool *r_flip); bool ED_view3d_clip_segment(const struct RegionView3D *rv3d, float ray_start[3], float ray_end[3]); diff --git a/source/blender/editors/space_view3d/view3d_project.c b/source/blender/editors/space_view3d/view3d_project.c index 2e876bc48b3..a9a8150c91d 100644 --- a/source/blender/editors/space_view3d/view3d_project.c +++ b/source/blender/editors/space_view3d/view3d_project.c @@ -283,6 +283,11 @@ float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3]) return mul_project_m4_v3_zfac((float(*)[4])rv3d->persmat, co) * rv3d->pixsize * U.pixelsize; } +float ED_view3d_pixel_size_no_ui_scale(const RegionView3D *rv3d, const float co[3]) +{ + return mul_project_m4_v3_zfac((float(*)[4])rv3d->persmat, co) * rv3d->pixsize; +} + /** * Calculate a depth value from \a co, use with #ED_view3d_win_to_delta */ -- cgit v1.2.3