15d074780c4353dd504671037a3ac6d7dce13d4f
[blender-staging.git] / intern / cycles / kernel / svm / svm_ao.h
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
17 CCL_NAMESPACE_BEGIN
18
19 ccl_device_noinline float svm_ao(KernelGlobals *kg,
20                                  ShaderData *sd,
21                                  float3 N,
22                                  ccl_addr_space PathState *state,
23                                  float max_dist,
24                                  int num_samples,
25                                  int flags)
26 {
27         if(flags & NODE_AO_GLOBAL_RADIUS) {
28                 max_dist = kernel_data.background.ao_distance;
29         }
30
31         /* Early out if no sampling needed. */
32         if(max_dist <= 0.0f || num_samples < 1 || sd->object == OBJECT_NONE) {
33                 return 1.0f;
34         }
35
36         /* Can't raytrace from shaders like displacement, before BVH exists. */
37         if (kernel_data.bvh.bvh_layout == BVH_LAYOUT_NONE) {
38                 return 1.0f;
39         }
40
41         if(flags & NODE_AO_INSIDE) {
42                 N = -N;
43         }
44
45         float3 T, B;
46         make_orthonormals(N, &T, &B);
47
48         int unoccluded = 0;
49         for(int sample = 0; sample < num_samples; sample++) {
50                 float disk_u, disk_v;
51                 path_branched_rng_2D(kg, state->rng_hash, state, sample, num_samples,
52                                      PRNG_BEVEL_U, &disk_u, &disk_v);
53
54                 float2 d = concentric_sample_disk(disk_u, disk_v);
55                 float3 D = make_float3(d.x, d.y, safe_sqrtf(1.0f - dot(d, d)));
56
57                 /* Create ray. */
58                 Ray ray;
59                 ray.P = ray_offset(sd->P, N);
60                 ray.D = D.x*T + D.y*B + D.z*N;
61                 ray.t = max_dist;
62                 ray.time = sd->time;
63
64                 if(flags & NODE_AO_ONLY_LOCAL) {
65                         if(!scene_intersect_local(kg,
66                                                   ray,
67                                                   NULL,
68                                                   sd->object,
69                                                   NULL,
70                                                   0)) {
71                                 unoccluded++;
72                         }
73                 }
74                 else {
75                         Intersection isect;
76                         if(!scene_intersect(kg,
77                                             ray,
78                                             PATH_RAY_SHADOW_OPAQUE,
79                                             &isect,
80                                             NULL,
81                                             0.0f, 0.0f)) {
82                                 unoccluded++;
83                         }
84                 }
85         }
86
87         return ((float) unoccluded) / num_samples;
88 }
89
90 ccl_device void svm_node_ao(KernelGlobals *kg,
91                             ShaderData *sd,
92                             ccl_addr_space PathState *state,
93                             float *stack,
94                             uint4 node)
95 {
96         uint flags, dist_offset, normal_offset, out_ao_offset;
97         decode_node_uchar4(node.y, &flags, &dist_offset, &normal_offset, &out_ao_offset);
98
99         uint color_offset, out_color_offset, samples;
100         decode_node_uchar4(node.z, &color_offset, &out_color_offset, &samples, NULL);
101
102         float dist = stack_load_float_default(stack, dist_offset, node.w);
103         float3 normal = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
104         float ao = svm_ao(kg, sd, normal, state, dist, samples, flags);
105
106         if(stack_valid(out_ao_offset)) {
107                 stack_store_float(stack, out_ao_offset, ao);
108         }
109
110         if(stack_valid(out_color_offset)) {
111                 float3 color = stack_load_float3(stack, color_offset);
112                 stack_store_float3(stack, out_color_offset, ao * color);
113         }
114 }
115
116 CCL_NAMESPACE_END