Blender  V2.93
svm_ao.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2018 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 #ifdef __SHADER_RAYTRACE__
20 
21 ccl_device_noinline float svm_ao(KernelGlobals *kg,
22  ShaderData *sd,
23  float3 N,
25  float max_dist,
26  int num_samples,
27  int flags)
28 {
29  if (flags & NODE_AO_GLOBAL_RADIUS) {
30  max_dist = kernel_data.background.ao_distance;
31  }
32 
33  /* Early out if no sampling needed. */
34  if (max_dist <= 0.0f || num_samples < 1 || sd->object == OBJECT_NONE) {
35  return 1.0f;
36  }
37 
38  /* Can't raytrace from shaders like displacement, before BVH exists. */
39  if (kernel_data.bvh.bvh_layout == BVH_LAYOUT_NONE) {
40  return 1.0f;
41  }
42 
43  if (flags & NODE_AO_INSIDE) {
44  N = -N;
45  }
46 
47  float3 T, B;
48  make_orthonormals(N, &T, &B);
49 
50  int unoccluded = 0;
51  for (int sample = 0; sample < num_samples; sample++) {
52  float disk_u, disk_v;
54  kg, state->rng_hash, state, sample, num_samples, PRNG_BEVEL_U, &disk_u, &disk_v);
55 
56  float2 d = concentric_sample_disk(disk_u, disk_v);
57  float3 D = make_float3(d.x, d.y, safe_sqrtf(1.0f - dot(d, d)));
58 
59  /* Create ray. */
60  Ray ray;
61  ray.P = ray_offset(sd->P, N);
62  ray.D = D.x * T + D.y * B + D.z * N;
63  ray.t = max_dist;
64  ray.time = sd->time;
65  ray.dP = sd->dP;
66  ray.dD = differential3_zero();
67 
68  if (flags & NODE_AO_ONLY_LOCAL) {
69  if (!scene_intersect_local(kg, &ray, NULL, sd->object, NULL, 0)) {
70  unoccluded++;
71  }
72  }
73  else {
74  Intersection isect;
75  if (!scene_intersect(kg, &ray, PATH_RAY_SHADOW_OPAQUE, &isect)) {
76  unoccluded++;
77  }
78  }
79  }
80 
81  return ((float)unoccluded) / num_samples;
82 }
83 
84 ccl_device void svm_node_ao(
85  KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state, float *stack, uint4 node)
86 {
87  uint flags, dist_offset, normal_offset, out_ao_offset;
88  svm_unpack_node_uchar4(node.y, &flags, &dist_offset, &normal_offset, &out_ao_offset);
89 
90  uint color_offset, out_color_offset, samples;
91  svm_unpack_node_uchar3(node.z, &color_offset, &out_color_offset, &samples);
92 
93  float dist = stack_load_float_default(stack, dist_offset, node.w);
94  float3 normal = stack_valid(normal_offset) ? stack_load_float3(stack, normal_offset) : sd->N;
95  float ao = svm_ao(kg, sd, normal, state, dist, samples, flags);
96 
97  if (stack_valid(out_ao_offset)) {
98  stack_store_float(stack, out_ao_offset, ao);
99  }
100 
101  if (stack_valid(out_color_offset)) {
102  float3 color = stack_load_float3(stack, color_offset);
103  stack_store_float3(stack, out_color_offset, ao * color);
104  }
105 }
106 
107 #endif /* __SHADER_RAYTRACE__ */
108 
MINLINE float safe_sqrtf(float a)
unsigned int uint
Definition: BLI_sys_types.h:83
OperationNode * node
IconTextureDrawCall normal
ccl_device_inline float3 ray_offset(float3 P, float3 Ng)
ccl_device_intersect bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(float *stack, uint a)
ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, uint *x, uint *y, uint *z)
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_inline bool stack_valid(uint a)
#define kernel_data
#define ccl_addr_space
#define ccl_device
#define ccl_device_noinline
#define CCL_NAMESPACE_END
#define make_float3(x, y, z)
ccl_device differential3 differential3_zero()
ccl_device float2 concentric_sample_disk(float u1, float u2)
ccl_device_inline void path_branched_rng_2D(KernelGlobals *kg, uint rng_hash, const ccl_addr_space PathState *state, int branch, int num_branches, int dimension, float *fx, float *fy)
@ PRNG_BEVEL_U
Definition: kernel_types.h:250
@ PATH_RAY_SHADOW_OPAQUE
Definition: kernel_types.h:277
#define OBJECT_NONE
Definition: kernel_types.h:59
ShaderData
@ BVH_LAYOUT_NONE
static ulong state[N]
#define T
#define B
static void sample(SocketReader *reader, int x, int y, float color[4])
params N
float t
Definition: kernel_types.h:649
differential3 dD
Definition: kernel_types.h:660
float3 P
Definition: kernel_types.h:647
float time
Definition: kernel_types.h:650
differential3 dP
Definition: kernel_types.h:659
float3 D
Definition: kernel_types.h:648
@ NODE_AO_INSIDE
Definition: svm_types.h:507
@ NODE_AO_GLOBAL_RADIUS
Definition: svm_types.h:508
@ NODE_AO_ONLY_LOCAL
Definition: svm_types.h:506
ccl_device_inline void make_orthonormals(const float3 N, float3 *a, float3 *b)
Definition: util_math.h:477
ccl_device_inline float dot(const float2 &a, const float2 &b)
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:29