/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation * * SPDX-License-Identifier: Apache-2.0 */ #pragma once #include "kernel/svm/node_types.h" #include "kernel/svm/util.h" CCL_NAMESPACE_BEGIN /* Brick */ ccl_device_inline float brick_noise(uint n) /* fast integer noise */ { uint nn; n = (n + 1013) & 0x7fffffff; n = (n >> 13) ^ n; nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; return 0.5f * ((float)nn / 1073741824.0f); } ccl_device_noinline_cpu float2 svm_brick(const float3 p, const float mortar_size, const float mortar_smooth, const float bias, float brick_width, const float row_height, const float offset_amount, const int offset_frequency, const float squash_amount, const int squash_frequency) { int bricknum; int rownum; float offset = 0.0f; float x; float y; rownum = floor_to_int(p.y / row_height); if (offset_frequency && squash_frequency) { brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount; /* squash */ offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount); /* offset */ } bricknum = floor_to_int((p.x + offset) / brick_width); x = (p.x + offset) - brick_width * bricknum; y = p.y - row_height * rownum; const float tint = saturatef((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias)); float min_dist = min(min(x, y), min(brick_width - x, row_height - y)); float mortar; if (min_dist >= mortar_size) { mortar = 0.0f; } else if (mortar_smooth == 0.0f) { mortar = 1.0f; } else { min_dist = 1.0f - min_dist / mortar_size; mortar = smoothstepf(min_dist / mortar_smooth); } return make_float2(tint, mortar); } ccl_device_noinline void svm_node_tex_brick(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexBrick &ccl_restrict node) { const float3 co = stack_load_float3(stack, node.co); float3 color1 = stack_load(stack, node.color1); const float3 color2 = stack_load(stack, node.color2); const float3 mortar = stack_load(stack, node.mortar); const float scale = stack_load(stack, node.scale); const float mortar_size = stack_load(stack, node.mortar_size); const float mortar_smooth = stack_load(stack, node.mortar_smooth); const float bias = stack_load(stack, node.bias); const float brick_width = stack_load(stack, node.brick_width); const float row_height = stack_load(stack, node.row_height); const float2 f2 = svm_brick(co * scale, mortar_size, mortar_smooth, bias, brick_width, row_height, node.offset_amount, node.offset_frequency, node.squash_amount, node.squash_frequency); const float tint = f2.x; const float f = f2.y; if (f != 1.0f) { const float facm = 1.0f - tint; color1 = facm * color1 + tint * color2; } if (stack_valid(node.color_offset)) { stack_store_float3(stack, node.color_offset, color1 * (1.0f - f) + mortar * f); } if (stack_valid(node.fac_offset)) { stack_store_float(stack, node.fac_offset, f); } } CCL_NAMESPACE_END