Blender V5.0
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
7#include "kernel/svm/util.h"
8
10
11/* Brick */
12
13ccl_device_inline float brick_noise(uint n) /* fast integer noise */
14{
15 uint nn;
16 n = (n + 1013) & 0x7fffffff;
17 n = (n >> 13) ^ n;
18 nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
19 return 0.5f * ((float)nn / 1073741824.0f);
20}
21
23 const float mortar_size,
24 const float mortar_smooth,
25 const float bias,
26 float brick_width,
27 const float row_height,
28 const float offset_amount,
29 const int offset_frequency,
30 const float squash_amount,
31 const int squash_frequency)
32{
33 int bricknum;
34 int rownum;
35 float offset = 0.0f;
36 float x;
37 float y;
38
39 rownum = floor_to_int(p.y / row_height);
40
41 if (offset_frequency && squash_frequency) {
42 brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount; /* squash */
43 offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount); /* offset */
44 }
45
46 bricknum = floor_to_int((p.x + offset) / brick_width);
47
48 x = (p.x + offset) - brick_width * bricknum;
49 y = p.y - row_height * rownum;
50
51 const float tint = saturatef((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias));
52 float min_dist = min(min(x, y), min(brick_width - x, row_height - y));
53
54 float mortar;
55 if (min_dist >= mortar_size) {
56 mortar = 0.0f;
57 }
58 else if (mortar_smooth == 0.0f) {
59 mortar = 1.0f;
60 }
61 else {
62 min_dist = 1.0f - min_dist / mortar_size;
63 mortar = smoothstepf(min_dist / mortar_smooth);
64 }
65
66 return make_float2(tint, mortar);
67}
68
70 ccl_private float *stack,
71 const uint4 node,
72 int offset)
73{
74 const uint4 node2 = read_node(kg, &offset);
75 const uint4 node3 = read_node(kg, &offset);
76 const uint4 node4 = read_node(kg, &offset);
77
78 /* Input and Output Sockets */
79 uint co_offset;
80 uint color1_offset;
81 uint color2_offset;
82 uint mortar_offset;
83 uint scale_offset;
84 uint mortar_size_offset;
85 uint bias_offset;
86 uint brick_width_offset;
87 uint row_height_offset;
88 uint color_offset;
89 uint fac_offset;
90 uint mortar_smooth_offset;
91
92 /* RNA properties */
93 uint offset_frequency;
94 uint squash_frequency;
95
96 svm_unpack_node_uchar4(node.y, &co_offset, &color1_offset, &color2_offset, &mortar_offset);
98 node.z, &scale_offset, &mortar_size_offset, &bias_offset, &brick_width_offset);
100 node.w, &row_height_offset, &color_offset, &fac_offset, &mortar_smooth_offset);
101
102 svm_unpack_node_uchar2(node2.x, &offset_frequency, &squash_frequency);
103
104 const float3 co = stack_load_float3(stack, co_offset);
105
106 float3 color1 = stack_load_float3(stack, color1_offset);
107 const float3 color2 = stack_load_float3(stack, color2_offset);
108 const float3 mortar = stack_load_float3(stack, mortar_offset);
109
110 const float scale = stack_load_float_default(stack, scale_offset, node2.y);
111 const float mortar_size = stack_load_float_default(stack, mortar_size_offset, node2.z);
112 const float mortar_smooth = stack_load_float_default(stack, mortar_smooth_offset, node4.x);
113 const float bias = stack_load_float_default(stack, bias_offset, node2.w);
114 const float brick_width = stack_load_float_default(stack, brick_width_offset, node3.x);
115 const float row_height = stack_load_float_default(stack, row_height_offset, node3.y);
116 const float offset_amount = __int_as_float(node3.z);
117 const float squash_amount = __int_as_float(node3.w);
118
119 const float2 f2 = svm_brick(co * scale,
120 mortar_size,
121 mortar_smooth,
122 bias,
123 brick_width,
124 row_height,
125 offset_amount,
126 offset_frequency,
127 squash_amount,
128 squash_frequency);
129
130 const float tint = f2.x;
131 const float f = f2.y;
132
133 if (f != 1.0f) {
134 const float facm = 1.0f - tint;
135 color1 = facm * color1 + tint * color2;
136 }
137
138 if (stack_valid(color_offset)) {
139 stack_store_float3(stack, color_offset, color1 * (1.0f - f) + mortar * f);
140 }
141 if (stack_valid(fac_offset)) {
142 stack_store_float(stack, fac_offset, f);
143 }
144 return offset;
145}
146
unsigned int uint
ccl_device_noinline int svm_node_tex_brick(KernelGlobals kg, ccl_private float *stack, const uint4 node, int offset)
Definition brick.h:69
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)
Definition brick.h:22
CCL_NAMESPACE_BEGIN ccl_device_inline float brick_noise(uint n)
Definition brick.h:13
nullptr float
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *const offset)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline float stack_load_float_default(const ccl_private float *stack, const uint a, const uint value)
ccl_device_forceinline void svm_unpack_node_uchar2(const uint i, ccl_private uint *x, ccl_private uint *y)
ccl_device_forceinline void svm_unpack_node_uchar4(const uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(const uint a)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
#define ccl_private
#define ccl_device_noinline_cpu
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define saturatef(x)
#define __int_as_float(x)
ccl_device_inline float smoothstepf(const float f)
Definition math_base.h:485
ccl_device_inline int floor_to_int(const float f)
Definition math_base.h:412
#define make_float2
#define min(a, b)
Definition sort.cc:36
float x
float y
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
uint x
Definition types_uint4.h:13
uint y
Definition types_uint4.h:13
uint z
Definition types_uint4.h:13
uint w
Definition types_uint4.h:13