0337c88a5434556b55c85c119fac645e0299c81e
[blender.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         if(flags & NODE_AO_INSIDE) {
37                 N = -N;
38         }
39
40         float3 T, B;
41         make_orthonormals(N, &T, &B);
42
43         int unoccluded = 0;
44         for(int sample = 0; sample < num_samples; sample++) {
45                 float disk_u, disk_v;
46                 path_branched_rng_2D(kg, state->rng_hash, state, sample, num_samples,
47                                      PRNG_BEVEL_U, &disk_u, &disk_v);
48
49                 float2 d = concentric_sample_disk(disk_u, disk_v);
50                 float3 D = make_float3(d.x, d.y, safe_sqrtf(1.0f - dot(d, d)));
51
52                 /* Create ray. */
53                 Ray ray;
54                 ray.P = ray_offset(sd->P, N);
55                 ray.D = D.x*T + D.y*B + D.z*N;
56                 ray.t = max_dist;
57                 ray.time = sd->time;
58
59                 if(flags & NODE_AO_ONLY_LOCAL) {
60                         if(!scene_intersect_local(kg,
61                                                   ray,
62                                                   NULL,
63                                                   sd->object,
64                                                   NULL,
65                                                   0)) {
66                                 unoccluded++;
67                         }
68                 }
69                 else {
70                         Intersection isect;
71                         if(!scene_intersect(kg,
72                                             ray,
73                                             PATH_RAY_SHADOW_OPAQUE,
74                                             &isect,
75                                             NULL,
76                                             0.0f, 0.0f)) {
77                                 unoccluded++;
78                         }
79                 }
80         }
81
82         return ((float) unoccluded) / num_samples;
83 }
84
85 ccl_device void svm_node_ao(KernelGlobals *kg,
86                             ShaderData *sd,
87                             ccl_addr_space PathState *state,
88                             float *stack,
89                             uint4 node)
90 {
91         uint flags, dist_offset, normal_offset, out_ao_offset;
92         decode_node_uchar4(node.y, &flags, &dist_offset, &normal_offset, &out_ao_offset);
93
94         uint color_offset, out_color_offset, samples;
95         decode_node_uchar4(node.z, &color_offset, &out_color_offset, &samples, NULL);
96
97         float dist = stack_load_float_default(stack, dist_offset, node.w);
98         float3 normal = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
99         float ao = svm_ao(kg, sd, normal, state, dist, samples, flags);
100
101         if(stack_valid(out_ao_offset)) {
102                 stack_store_float(stack, out_ao_offset, ao);
103         }
104
105         if(stack_valid(out_color_offset)) {
106                 float3 color = stack_load_float3(stack, color_offset);
107                 stack_store_float3(stack, out_color_offset, ao * color);
108         }
109 }
110
111 CCL_NAMESPACE_END