7d512f7ff4ddac213514d1a8bb91fb7af2a6df18
[blender.git] / intern / cycles / kernel / svm / svm_geometry.h
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
17 CCL_NAMESPACE_BEGIN
18
19 /* Geometry Node */
20
21 ccl_device_inline void svm_node_geometry(KernelGlobals *kg,
22                                          ShaderData *sd,
23                                          float *stack,
24                                          uint type,
25                                          uint out_offset)
26 {
27         float3 data;
28
29         switch(type) {
30                 case NODE_GEOM_P: data = ccl_fetch(sd, P); break;
31                 case NODE_GEOM_N: data = ccl_fetch(sd, N); break;
32 #ifdef __DPDU__
33                 case NODE_GEOM_T: data = primitive_tangent(kg, sd); break;
34 #endif
35                 case NODE_GEOM_I: data = ccl_fetch(sd, I); break;
36                 case NODE_GEOM_Ng: data = ccl_fetch(sd, Ng); break;
37 #ifdef __UV__
38                 case NODE_GEOM_uv: data = make_float3(ccl_fetch(sd, u), ccl_fetch(sd, v), 0.0f); break;
39 #endif
40         }
41
42         stack_store_float3(stack, out_offset, data);
43 }
44
45 ccl_device void svm_node_geometry_bump_dx(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
46 {
47 #ifdef __RAY_DIFFERENTIALS__
48         float3 data;
49
50         switch(type) {
51                 case NODE_GEOM_P: data = ccl_fetch(sd, P) + ccl_fetch(sd, dP).dx; break;
52                 case NODE_GEOM_uv: data = make_float3(ccl_fetch(sd, u) + ccl_fetch(sd, du).dx, ccl_fetch(sd, v) + ccl_fetch(sd, dv).dx, 0.0f); break;
53                 default: svm_node_geometry(kg, sd, stack, type, out_offset); return;
54         }
55
56         stack_store_float3(stack, out_offset, data);
57 #else
58         svm_node_geometry(kg, sd, stack, type, out_offset);
59 #endif
60 }
61
62 ccl_device void svm_node_geometry_bump_dy(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
63 {
64 #ifdef __RAY_DIFFERENTIALS__
65         float3 data;
66
67         switch(type) {
68                 case NODE_GEOM_P: data = ccl_fetch(sd, P) + ccl_fetch(sd, dP).dy; break;
69                 case NODE_GEOM_uv: data = make_float3(ccl_fetch(sd, u) + ccl_fetch(sd, du).dy, ccl_fetch(sd, v) + ccl_fetch(sd, dv).dy, 0.0f); break;
70                 default: svm_node_geometry(kg, sd, stack, type, out_offset); return;
71         }
72
73         stack_store_float3(stack, out_offset, data);
74 #else
75         svm_node_geometry(kg, sd, stack, type, out_offset);
76 #endif
77 }
78
79 /* Object Info */
80
81 ccl_device void svm_node_object_info(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
82 {
83         float data;
84
85         switch(type) {
86                 case NODE_INFO_OB_LOCATION: {
87                         stack_store_float3(stack, out_offset, object_location(kg, sd));
88                         return;
89                 }
90                 case NODE_INFO_OB_INDEX: data = object_pass_id(kg, ccl_fetch(sd, object)); break;
91                 case NODE_INFO_MAT_INDEX: data = shader_pass_id(kg, sd); break;
92                 case NODE_INFO_OB_RANDOM: data = object_random_number(kg, ccl_fetch(sd, object)); break;
93                 default: data = 0.0f; break;
94         }
95
96         stack_store_float(stack, out_offset, data);
97 }
98
99 /* Particle Info */
100
101 ccl_device void svm_node_particle_info(KernelGlobals *kg,
102                                        ShaderData *sd,
103                                        float *stack,
104                                        uint type,
105                                        uint out_offset)
106 {
107         switch(type) {
108                 case NODE_INFO_PAR_INDEX: {
109                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
110                         stack_store_float(stack, out_offset, particle_index(kg, particle_id));
111                         break;
112                 }
113                 case NODE_INFO_PAR_AGE: {
114                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
115                         stack_store_float(stack, out_offset, particle_age(kg, particle_id));
116                         break;
117                 }
118                 case NODE_INFO_PAR_LIFETIME: {
119                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
120                         stack_store_float(stack, out_offset, particle_lifetime(kg, particle_id));
121                         break;
122                 }
123                 case NODE_INFO_PAR_LOCATION: {
124                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
125                         stack_store_float3(stack, out_offset, particle_location(kg, particle_id));
126                         break;
127                 }
128 #if 0   /* XXX float4 currently not supported in SVM stack */
129                 case NODE_INFO_PAR_ROTATION: {
130                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
131                         stack_store_float4(stack, out_offset, particle_rotation(kg, particle_id));
132                         break;
133                 }
134 #endif
135                 case NODE_INFO_PAR_SIZE: {
136                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
137                         stack_store_float(stack, out_offset, particle_size(kg, particle_id));
138                         break;
139                 }
140                 case NODE_INFO_PAR_VELOCITY: {
141                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
142                         stack_store_float3(stack, out_offset, particle_velocity(kg, particle_id));
143                         break;
144                 }
145                 case NODE_INFO_PAR_ANGULAR_VELOCITY: {
146                         int particle_id = object_particle_id(kg, ccl_fetch(sd, object));
147                         stack_store_float3(stack, out_offset, particle_angular_velocity(kg, particle_id));
148                         break;
149                 }
150         }
151 }
152
153 #ifdef __HAIR__
154
155 /* Hair Info */
156
157 ccl_device void svm_node_hair_info(KernelGlobals *kg,
158                                    ShaderData *sd,
159                                    float *stack,
160                                    uint type,
161                                    uint out_offset)
162 {
163         float data;
164         float3 data3;
165
166         switch(type) {
167                 case NODE_INFO_CURVE_IS_STRAND: {
168                         data = (ccl_fetch(sd, type) & PRIMITIVE_ALL_CURVE) != 0;
169                         stack_store_float(stack, out_offset, data);
170                         break;
171                 }
172                 case NODE_INFO_CURVE_INTERCEPT:
173                         break; /* handled as attribute */
174                 case NODE_INFO_CURVE_THICKNESS: {
175                         data = curve_thickness(kg, sd);
176                         stack_store_float(stack, out_offset, data);
177                         break;
178                 }
179                 /*case NODE_INFO_CURVE_FADE: {
180                         data = ccl_fetch(sd, curve_transparency);
181                         stack_store_float(stack, out_offset, data);
182                         break;
183                 }*/
184                 case NODE_INFO_CURVE_TANGENT_NORMAL: {
185                         data3 = curve_tangent_normal(kg, sd);
186                         stack_store_float3(stack, out_offset, data3);
187                         break;
188                 }
189         }
190 }
191 #endif
192
193 CCL_NAMESPACE_END
194