Cleanup: fix compiler warnings.
[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                 ray.dP = sd->dP;
64                 ray.dD = differential3_zero();
65
66                 if(flags & NODE_AO_ONLY_LOCAL) {
67                         if(!scene_intersect_local(kg,
68                                                   ray,
69                                                   NULL,
70                                                   sd->object,
71                                                   NULL,
72                                                   0)) {
73                                 unoccluded++;
74                         }
75                 }
76                 else {
77                         Intersection isect;
78                         if(!scene_intersect(kg,
79                                             ray,
80                                             PATH_RAY_SHADOW_OPAQUE,
81                                             &isect,
82                                             NULL,
83                                             0.0f, 0.0f)) {
84                                 unoccluded++;
85                         }
86                 }
87         }
88
89         return ((float) unoccluded) / num_samples;
90 }
91
92 ccl_device void svm_node_ao(KernelGlobals *kg,
93                             ShaderData *sd,
94                             ccl_addr_space PathState *state,
95                             float *stack,
96                             uint4 node)
97 {
98         uint flags, dist_offset, normal_offset, out_ao_offset;
99         decode_node_uchar4(node.y, &flags, &dist_offset, &normal_offset, &out_ao_offset);
100
101         uint color_offset, out_color_offset, samples;
102         decode_node_uchar4(node.z, &color_offset, &out_color_offset, &samples, NULL);
103
104         float dist = stack_load_float_default(stack, dist_offset, node.w);
105         float3 normal = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
106         float ao = svm_ao(kg, sd, normal, state, dist, samples, flags);
107
108         if(stack_valid(out_ao_offset)) {
109                 stack_store_float(stack, out_ao_offset, ao);
110         }
111
112         if(stack_valid(out_color_offset)) {
113                 float3 color = stack_load_float3(stack, color_offset);
114                 stack_store_float3(stack, out_color_offset, ao * color);
115         }
116 }
117
118 CCL_NAMESPACE_END