Cycles: Implement index output for hair node
[blender.git] / intern / cycles / kernel / svm / svm_geometry.h
index 501fed9500268a146de95f4bc4300dbbc03df3db..6c88d150b609894a7545aaa884aac7b625fc4cad 100644 (file)
@@ -1,42 +1,28 @@
 /*
- * Copyright 2011, Blender Foundation.
+ * Copyright 2011-2013 Blender Foundation
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 CCL_NAMESPACE_BEGIN
 
 /* Geometry Node */
 
-__device_inline float3 svm_tangent_from_generated(float3 P)
-{
-       float length = len(P);
-
-       if(length == 0.0f)
-               return make_float3(0.0f, 0.0f, 0.0f);
-
-       float u = 0.0f;
-       if(!(P.x == 0.0f && P.y == 0.0f))
-               u = (1.0f - atan2f(P.x, P.y))/(2.0f*M_PI_F);
-       
-       float v = 1.0f - acosf(clamp(P.z/length, -1.0f, 1.0f))/M_PI_F;
-
-       return make_float3(u, v, 0.0f);
-}
-
-__device void svm_node_geometry(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
+ccl_device_inline void svm_node_geometry(KernelGlobals *kg,
+                                         ShaderData *sd,
+                                         float *stack,
+                                         uint type,
+                                         uint out_offset)
 {
        float3 data;
 
@@ -44,43 +30,20 @@ __device void svm_node_geometry(KernelGlobals *kg, ShaderData *sd, float *stack,
                case NODE_GEOM_P: data = sd->P; break;
                case NODE_GEOM_N: data = sd->N; break;
 #ifdef __DPDU__
-               case NODE_GEOM_T: {
-                       if(sd->object != ~0) {
-                               int attr_offset = find_attribute(kg, sd, ATTR_STD_TANGENT);
-
-                               if(attr_offset != ATTR_STD_NOT_FOUND) {
-                                       data = triangle_attribute_float3(kg, sd, ATTR_ELEMENT_CORNER, attr_offset, NULL, NULL);
-                                       object_normal_transform(kg, sd, &data);
-                               }
-                               else {
-                                       attr_offset = find_attribute(kg, sd, ATTR_STD_GENERATED);
-
-                                       if(attr_offset != ATTR_STD_NOT_FOUND) {
-                                               data = triangle_attribute_float3(kg, sd, ATTR_ELEMENT_VERTEX, attr_offset, NULL, NULL);
-                                               svm_tangent_from_generated(data);
-                                               object_normal_transform(kg, sd, &data);
-                                       }
-                                       else
-                                               data = normalize(sd->dPdu);
-                               }
-                       }
-                       else
-                               data = normalize(sd->dPdu);
-
-                       break;
-               }
+               case NODE_GEOM_T: data = primitive_tangent(kg, sd); break;
 #endif
                case NODE_GEOM_I: data = sd->I; break;
                case NODE_GEOM_Ng: data = sd->Ng; break;
 #ifdef __UV__
                case NODE_GEOM_uv: data = make_float3(sd->u, sd->v, 0.0f); break;
 #endif
+               default: data = make_float3(0.0f, 0.0f, 0.0f);
        }
 
        stack_store_float3(stack, out_offset, data);
 }
 
-__device void svm_node_geometry_bump_dx(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
+ccl_device void svm_node_geometry_bump_dx(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
 {
 #ifdef __RAY_DIFFERENTIALS__
        float3 data;
@@ -97,7 +60,7 @@ __device void svm_node_geometry_bump_dx(KernelGlobals *kg, ShaderData *sd, float
 #endif
 }
 
-__device void svm_node_geometry_bump_dy(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
+ccl_device void svm_node_geometry_bump_dy(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
 {
 #ifdef __RAY_DIFFERENTIALS__
        float3 data;
@@ -116,7 +79,7 @@ __device void svm_node_geometry_bump_dy(KernelGlobals *kg, ShaderData *sd, float
 
 /* Object Info */
 
-__device void svm_node_object_info(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
+ccl_device void svm_node_object_info(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
 {
        float data;
 
@@ -127,7 +90,15 @@ __device void svm_node_object_info(KernelGlobals *kg, ShaderData *sd, float *sta
                }
                case NODE_INFO_OB_INDEX: data = object_pass_id(kg, sd->object); break;
                case NODE_INFO_MAT_INDEX: data = shader_pass_id(kg, sd); break;
-               case NODE_INFO_OB_RANDOM: data = object_random_number(kg, sd->object); break;
+               case NODE_INFO_OB_RANDOM: {
+                       if(sd->lamp != LAMP_NONE) {
+                               data = lamp_random_number(kg, sd->lamp);
+                       }
+                       else {
+                               data = object_random_number(kg, sd->object);
+                       }
+                       break;
+               }
                default: data = 0.0f; break;
        }
 
@@ -136,53 +107,99 @@ __device void svm_node_object_info(KernelGlobals *kg, ShaderData *sd, float *sta
 
 /* Particle Info */
 
-__device void svm_node_particle_info(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
+ccl_device void svm_node_particle_info(KernelGlobals *kg,
+                                       ShaderData *sd,
+                                       float *stack,
+                                       uint type,
+                                       uint out_offset)
 {
        switch(type) {
                case NODE_INFO_PAR_INDEX: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float(stack, out_offset, particle_index(kg, particle_id));
                        break;
                }
                case NODE_INFO_PAR_AGE: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float(stack, out_offset, particle_age(kg, particle_id));
                        break;
                }
                case NODE_INFO_PAR_LIFETIME: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float(stack, out_offset, particle_lifetime(kg, particle_id));
                        break;
                }
                case NODE_INFO_PAR_LOCATION: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float3(stack, out_offset, particle_location(kg, particle_id));
                        break;
                }
-               #if 0   /* XXX float4 currently not supported in SVM stack */
+#if 0  /* XXX float4 currently not supported in SVM stack */
                case NODE_INFO_PAR_ROTATION: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float4(stack, out_offset, particle_rotation(kg, particle_id));
                        break;
                }
-               #endif
+#endif
                case NODE_INFO_PAR_SIZE: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float(stack, out_offset, particle_size(kg, particle_id));
                        break;
                }
                case NODE_INFO_PAR_VELOCITY: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float3(stack, out_offset, particle_velocity(kg, particle_id));
                        break;
                }
                case NODE_INFO_PAR_ANGULAR_VELOCITY: {
-                       uint particle_id = object_particle_id(kg, sd->object);
+                       int particle_id = object_particle_id(kg, sd->object);
                        stack_store_float3(stack, out_offset, particle_angular_velocity(kg, particle_id));
                        break;
                }
        }
 }
 
+#ifdef __HAIR__
+
+/* Hair Info */
+
+ccl_device void svm_node_hair_info(KernelGlobals *kg,
+                                   ShaderData *sd,
+                                   float *stack,
+                                   uint type,
+                                   uint out_offset)
+{
+       float data;
+       float3 data3;
+
+       switch(type) {
+               case NODE_INFO_CURVE_IS_STRAND: {
+                       data = (sd->type & PRIMITIVE_ALL_CURVE) != 0;
+                       stack_store_float(stack, out_offset, data);
+                       break;
+               }
+               case NODE_INFO_CURVE_INTERCEPT:
+                       break; /* handled as attribute */
+               case NODE_INFO_CURVE_INDEX:
+                       break; /* handled as attribute */
+               case NODE_INFO_CURVE_THICKNESS: {
+                       data = curve_thickness(kg, sd);
+                       stack_store_float(stack, out_offset, data);
+                       break;
+               }
+               /*case NODE_INFO_CURVE_FADE: {
+                       data = sd->curve_transparency;
+                       stack_store_float(stack, out_offset, data);
+                       break;
+               }*/
+               case NODE_INFO_CURVE_TANGENT_NORMAL: {
+                       data3 = curve_tangent_normal(kg, sd);
+                       stack_store_float3(stack, out_offset, data3);
+                       break;
+               }
+       }
+}
+#endif
+
 CCL_NAMESPACE_END