Blender  V2.93
svm_brick.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 /* Brick */
20 
21 ccl_device_inline float brick_noise(uint n) /* fast integer noise */
22 {
23  uint nn;
24  n = (n + 1013) & 0x7fffffff;
25  n = (n >> 13) ^ n;
26  nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
27  return 0.5f * ((float)nn / 1073741824.0f);
28 }
29 
31  float mortar_size,
32  float mortar_smooth,
33  float bias,
34  float brick_width,
35  float row_height,
36  float offset_amount,
37  int offset_frequency,
38  float squash_amount,
39  int squash_frequency)
40 {
41  int bricknum, rownum;
42  float offset = 0.0f;
43  float x, y;
44 
45  rownum = floor_to_int(p.y / row_height);
46 
47  if (offset_frequency && squash_frequency) {
48  brick_width *= (rownum % squash_frequency) ? 1.0f : squash_amount; /* squash */
49  offset = (rownum % offset_frequency) ? 0.0f : (brick_width * offset_amount); /* offset */
50  }
51 
52  bricknum = floor_to_int((p.x + offset) / brick_width);
53 
54  x = (p.x + offset) - brick_width * bricknum;
55  y = p.y - row_height * rownum;
56 
57  float tint = saturate((brick_noise((rownum << 16) + (bricknum & 0xFFFF)) + bias));
58  float min_dist = min(min(x, y), min(brick_width - x, row_height - y));
59 
60  float mortar;
61  if (min_dist >= mortar_size) {
62  mortar = 0.0f;
63  }
64  else if (mortar_smooth == 0.0f) {
65  mortar = 1.0f;
66  }
67  else {
68  min_dist = 1.0f - min_dist / mortar_size;
69  mortar = (min_dist < mortar_smooth) ? smoothstepf(min_dist / mortar_smooth) : 1.0f;
70  }
71 
72  return make_float2(tint, mortar);
73 }
74 
76  KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
77 {
78  uint4 node2 = read_node(kg, offset);
79  uint4 node3 = read_node(kg, offset);
80  uint4 node4 = read_node(kg, offset);
81 
82  /* Input and Output Sockets */
83  uint co_offset, color1_offset, color2_offset, mortar_offset, scale_offset;
84  uint mortar_size_offset, bias_offset, brick_width_offset, row_height_offset;
85  uint color_offset, fac_offset, mortar_smooth_offset;
86 
87  /* RNA properties */
88  uint offset_frequency, squash_frequency;
89 
90  svm_unpack_node_uchar4(node.y, &co_offset, &color1_offset, &color2_offset, &mortar_offset);
92  node.z, &scale_offset, &mortar_size_offset, &bias_offset, &brick_width_offset);
94  node.w, &row_height_offset, &color_offset, &fac_offset, &mortar_smooth_offset);
95 
96  svm_unpack_node_uchar2(node2.x, &offset_frequency, &squash_frequency);
97 
98  float3 co = stack_load_float3(stack, co_offset);
99 
100  float3 color1 = stack_load_float3(stack, color1_offset);
101  float3 color2 = stack_load_float3(stack, color2_offset);
102  float3 mortar = stack_load_float3(stack, mortar_offset);
103 
104  float scale = stack_load_float_default(stack, scale_offset, node2.y);
105  float mortar_size = stack_load_float_default(stack, mortar_size_offset, node2.z);
106  float mortar_smooth = stack_load_float_default(stack, mortar_smooth_offset, node4.x);
107  float bias = stack_load_float_default(stack, bias_offset, node2.w);
108  float brick_width = stack_load_float_default(stack, brick_width_offset, node3.x);
109  float row_height = stack_load_float_default(stack, row_height_offset, node3.y);
110  float offset_amount = __int_as_float(node3.z);
111  float squash_amount = __int_as_float(node3.w);
112 
113  float2 f2 = svm_brick(co * scale,
114  mortar_size,
115  mortar_smooth,
116  bias,
117  brick_width,
118  row_height,
119  offset_amount,
120  offset_frequency,
121  squash_amount,
122  squash_frequency);
123 
124  float tint = f2.x;
125  float f = f2.y;
126 
127  if (f != 1.0f) {
128  float facm = 1.0f - tint;
129  color1 = facm * color1 + tint * color2;
130  }
131 
132  if (stack_valid(color_offset))
133  stack_store_float3(stack, color_offset, color1 * (1.0f - f) + mortar * f);
134  if (stack_valid(fac_offset))
135  stack_store_float(stack, fac_offset, f);
136 }
137 
typedef float(TangentPoint)[2]
unsigned int uint
Definition: BLI_sys_types.h:83
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
OperationNode * node
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
ccl_device_inline void stack_store_float(float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar2(uint i, uint *x, uint *y)
ccl_device_inline bool stack_valid(uint a)
#define ccl_device
#define ccl_device_noinline_cpu
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define make_float2(x, y)
ShaderData
#define min(a, b)
Definition: sort.c:51
float y
Definition: sky_float3.h:35
float x
Definition: sky_float3.h:35
ccl_device void svm_node_tex_brick(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
Definition: svm_brick.h:75
CCL_NAMESPACE_BEGIN ccl_device_inline float brick_noise(uint n)
Definition: svm_brick.h:21
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: svm_brick.h:30
ccl_device_inline float saturate(float a)
Definition: util_math.h:315
ccl_device_inline int floor_to_int(float f)
Definition: util_math.h:326
ccl_device_inline float smoothstepf(float f)
Definition: util_math.h:399
ccl_device_inline float __int_as_float(int i)
Definition: util_math.h:212