Blender V4.3
brick.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9/* Brick */
10
11ccl_device_inline float brick_noise(uint n) /* fast integer noise */
12{
13 uint nn;
14 n = (n + 1013) & 0x7fffffff;
15 n = (n >> 13) ^ n;
16 nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
17 return 0.5f * ((float)nn / 1073741824.0f);
18}
19
21 float mortar_size,
22 float mortar_smooth,
23 float bias,
24 float brick_width,
25 float row_height,
26 float offset_amount,
27 int offset_frequency,
28 float squash_amount,
29 int squash_frequency)
30{
31 int bricknum, rownum;
32 float offset = 0.0f;
33 float x, y;
34
35 rownum = floor_to_int(p.y / row_height);
36
37 if (offset_frequency && squash_frequency) {
38 brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount; /* squash */
39 offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount); /* offset */
40 }
41
42 bricknum = floor_to_int((p.x + offset) / brick_width);
43
44 x = (p.x + offset) - brick_width * bricknum;
45 y = p.y - row_height * rownum;
46
47 float tint = saturatef((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias));
48 float min_dist = min(min(x, y), min(brick_width - x, row_height - y));
49
50 float mortar;
51 if (min_dist >= mortar_size) {
52 mortar = 0.0f;
53 }
54 else if (mortar_smooth == 0.0f) {
55 mortar = 1.0f;
56 }
57 else {
58 min_dist = 1.0f - min_dist / mortar_size;
59 mortar = smoothstepf(min_dist / mortar_smooth);
60 }
61
62 return make_float2(tint, mortar);
63}
64
66 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
67{
68 uint4 node2 = read_node(kg, &offset);
69 uint4 node3 = read_node(kg, &offset);
70 uint4 node4 = read_node(kg, &offset);
71
72 /* Input and Output Sockets */
73 uint co_offset, color1_offset, color2_offset, mortar_offset, scale_offset;
74 uint mortar_size_offset, bias_offset, brick_width_offset, row_height_offset;
75 uint color_offset, fac_offset, mortar_smooth_offset;
76
77 /* RNA properties */
78 uint offset_frequency, squash_frequency;
79
80 svm_unpack_node_uchar4(node.y, &co_offset, &color1_offset, &color2_offset, &mortar_offset);
82 node.z, &scale_offset, &mortar_size_offset, &bias_offset, &brick_width_offset);
84 node.w, &row_height_offset, &color_offset, &fac_offset, &mortar_smooth_offset);
85
86 svm_unpack_node_uchar2(node2.x, &offset_frequency, &squash_frequency);
87
88 float3 co = stack_load_float3(stack, co_offset);
89
90 float3 color1 = stack_load_float3(stack, color1_offset);
91 float3 color2 = stack_load_float3(stack, color2_offset);
92 float3 mortar = stack_load_float3(stack, mortar_offset);
93
94 float scale = stack_load_float_default(stack, scale_offset, node2.y);
95 float mortar_size = stack_load_float_default(stack, mortar_size_offset, node2.z);
96 float mortar_smooth = stack_load_float_default(stack, mortar_smooth_offset, node4.x);
97 float bias = stack_load_float_default(stack, bias_offset, node2.w);
98 float brick_width = stack_load_float_default(stack, brick_width_offset, node3.x);
99 float row_height = stack_load_float_default(stack, row_height_offset, node3.y);
100 float offset_amount = __int_as_float(node3.z);
101 float squash_amount = __int_as_float(node3.w);
102
103 float2 f2 = svm_brick(co * scale,
104 mortar_size,
105 mortar_smooth,
106 bias,
107 brick_width,
108 row_height,
109 offset_amount,
110 offset_frequency,
111 squash_amount,
112 squash_frequency);
113
114 float tint = f2.x;
115 float f = f2.y;
116
117 if (f != 1.0f) {
118 float facm = 1.0f - tint;
119 color1 = facm * color1 + tint * color2;
120 }
121
122 if (stack_valid(color_offset))
123 stack_store_float3(stack, color_offset, color1 * (1.0f - f) + mortar * f);
124 if (stack_valid(fac_offset))
125 stack_store_float(stack, fac_offset, f);
126 return offset;
127}
128
unsigned int uint
ccl_device_noinline int svm_node_tex_brick(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition brick.h:65
CCL_NAMESPACE_BEGIN ccl_device_inline float brick_noise(uint n)
Definition brick.h:11
ccl_device_noinline_cpu float2 svm_brick(float3 p, float mortar_size, float mortar_smooth, float bias, float brick_width, float row_height, float offset_amount, int offset_frequency, float squash_amount, int squash_frequency)
Definition brick.h:20
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define ccl_private
#define ccl_device_noinline_cpu
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float2 make_float2(const float x, const float y)
#define __int_as_float(x)
draw_view in_light_buf[] float
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_forceinline void svm_unpack_node_uchar2(uint i, ccl_private uint *x, ccl_private uint *y)
ccl_device_inline float stack_load_float_default(ccl_private float *stack, uint a, uint value)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
ShaderData
#define min(a, b)
Definition sort.c:32
float x
float y
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
uint x
Definition types_uint4.h:15
uint y
Definition types_uint4.h:15
uint z
Definition types_uint4.h:15
uint w
Definition types_uint4.h:15
ccl_device_inline int floor_to_int(float f)
Definition util/math.h:429
ccl_device_inline float smoothstepf(float f)
Definition util/math.h:508