Blender V4.5
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 ShaderData *sd,
71 ccl_private float *stack,
72 const uint4 node,
73 int offset)
74{
75 const uint4 node2 = read_node(kg, &offset);
76 const uint4 node3 = read_node(kg, &offset);
77 const uint4 node4 = read_node(kg, &offset);
78
79 /* Input and Output Sockets */
80 uint co_offset;
81 uint color1_offset;
82 uint color2_offset;
83 uint mortar_offset;
84 uint scale_offset;
85 uint mortar_size_offset;
86 uint bias_offset;
87 uint brick_width_offset;
88 uint row_height_offset;
89 uint color_offset;
90 uint fac_offset;
91 uint mortar_smooth_offset;
92
93 /* RNA properties */
94 uint offset_frequency;
95 uint squash_frequency;
96
97 svm_unpack_node_uchar4(node.y, &co_offset, &color1_offset, &color2_offset, &mortar_offset);
99 node.z, &scale_offset, &mortar_size_offset, &bias_offset, &brick_width_offset);
101 node.w, &row_height_offset, &color_offset, &fac_offset, &mortar_smooth_offset);
102
103 svm_unpack_node_uchar2(node2.x, &offset_frequency, &squash_frequency);
104
105 const float3 co = stack_load_float3(stack, co_offset);
106
107 float3 color1 = stack_load_float3(stack, color1_offset);
108 const float3 color2 = stack_load_float3(stack, color2_offset);
109 const float3 mortar = stack_load_float3(stack, mortar_offset);
110
111 const float scale = stack_load_float_default(stack, scale_offset, node2.y);
112 const float mortar_size = stack_load_float_default(stack, mortar_size_offset, node2.z);
113 const float mortar_smooth = stack_load_float_default(stack, mortar_smooth_offset, node4.x);
114 const float bias = stack_load_float_default(stack, bias_offset, node2.w);
115 const float brick_width = stack_load_float_default(stack, brick_width_offset, node3.x);
116 const float row_height = stack_load_float_default(stack, row_height_offset, node3.y);
117 const float offset_amount = __int_as_float(node3.z);
118 const float squash_amount = __int_as_float(node3.w);
119
120 const float2 f2 = svm_brick(co * scale,
121 mortar_size,
122 mortar_smooth,
123 bias,
124 brick_width,
125 row_height,
126 offset_amount,
127 offset_frequency,
128 squash_amount,
129 squash_frequency);
130
131 const float tint = f2.x;
132 const float f = f2.y;
133
134 if (f != 1.0f) {
135 const float facm = 1.0f - tint;
136 color1 = facm * color1 + tint * color2;
137 }
138
139 if (stack_valid(color_offset)) {
140 stack_store_float3(stack, color_offset, color1 * (1.0f - f) + mortar * f);
141 }
142 if (stack_valid(fac_offset)) {
143 stack_store_float(stack, fac_offset, f);
144 }
145 return offset;
146}
147
unsigned int uint
ccl_device_noinline int svm_node_tex_brick(KernelGlobals kg, ccl_private ShaderData *sd, 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
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)
ccl_device_forceinline float2 make_float2(const float x, const float y)
#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 min(a, b)
Definition sort.cc:36
float x
float y
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
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