Cycles / Wavelength to RGB node:
[blender.git] / intern / cycles / kernel / svm / svm_wavelength.h
index 2ae17b8a224069f4e560ae1f9fa62c00893d2a3e..f9dd24daceff8569c56b31b762c1a31571f77f6f 100644 (file)
  * modification, are permitted provided that the following conditions are
  * met:
  * * Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
+ *      notice, this list of conditions and the following disclaimer.
  * * Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
  * * Neither the name of Sony Pictures Imageworks nor the names of its
- *   contributors may be used to endorse or promote products derived from
- *   this software without specific prior written permission.
+ *      contributors may be used to endorse or promote products derived from
+ *      this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -34,28 +34,15 @@ CCL_NAMESPACE_BEGIN
 
 /* Wavelength to RGB */
 
-/* ToDo: Move these 2 functions to an util file */
-__device float3 xyz_to_rgb_wave(float x, float y, float z)
-{
-       return make_float3(3.240479f * x + -1.537150f * y + -0.498535f * z,
-                                         -0.969256f * x +  1.875991f * y +  0.041556f * z,
-                                          0.055648f * x + -0.204043f * y +  1.057311f * z);
-}
-
-__device float3 wavelength_lerp(const float3 a, const float3 b, float t)
-{
-       return (a * (1.0f - t) + b * t);
-}
-
 __device void svm_node_wavelength(ShaderData *sd, float *stack, uint wavelength, uint color_out)
 {      
        // CIE colour matching functions xBar, yBar, and zBar for
-       //   wavelengths from 380 through 780 nanometers, every 5
-       //   nanometers.  For a wavelength lambda in this range:
-       //        cie_colour_match[(lambda - 380) / 5][0] = xBar
-       //        cie_colour_match[(lambda - 380) / 5][1] = yBar
-       //        cie_colour_match[(lambda - 380) / 5][2] = zBar
-       float cie_colour_match[81][3] = {
+       //       wavelengths from 380 through 780 nanometers, every 5
+       //       nanometers.  For a wavelength lambda in this range:
+       //                cie_colour_match[(lambda - 380) / 5][0] = xBar
+       //                cie_colour_match[(lambda - 380) / 5][1] = yBar
+       //                cie_colour_match[(lambda - 380) / 5][2] = zBar
+       const float cie_colour_match[81][3] = {
                {0.0014,0.0000,0.0065}, {0.0022,0.0001,0.0105}, {0.0042,0.0001,0.0201},
                {0.0076,0.0002,0.0362}, {0.0143,0.0004,0.0679}, {0.0232,0.0006,0.1102},
                {0.0435,0.0012,0.2074}, {0.0776,0.0022,0.3713}, {0.1344,0.0040,0.6456},
@@ -86,27 +73,27 @@ __device void svm_node_wavelength(ShaderData *sd, float *stack, uint wavelength,
        };
 
        float lambda_nm = stack_load_float(stack, wavelength);
-       float3 rgb;
+       float ii = (lambda_nm-380.0f) * (1.0f/5.0f);  // scaled 0..80
+       int i = float_to_int(ii);
+       float3 color;
        
-    float ii = (lambda_nm-380.0f) / 5.0f;  // scaled 0..80
-    int i = float_to_int(ii);
-    if (i < 0 || i >= 80) {
-        rgb = make_float3(0.0f, 0.0f, 0.0f);
+       if (i < 0 || i >= 80) {
+               color = make_float3(0.0f, 0.0f, 0.0f);
        }
        else {
                ii -= i;
-               float *c = cie_colour_match[i];
-               rgb = wavelength_lerp(make_float3(c[0], c[1], c[2]), make_float3(c[3], c[4], c[5]), ii);
+               const float *c = cie_colour_match[i];
+               color = interp(make_float3(c[0], c[1], c[2]), make_float3(c[3], c[4], c[5]), ii);
        }
        
-       rgb = xyz_to_rgb_wave(rgb.x, rgb.y, rgb.z);
-       rgb *= 1.0/2.52;    // Empirical scale from lg to make all comps <= 1
+       color = xyz_to_rgb(color.x, color.y, color.z);
+       color *= 1.0f/2.52f;    // Empirical scale from lg to make all comps <= 1
        
-       /* Clamp to Zero if values are smaller */
-       rgb = max(rgb, make_float3(0.0f, 0.0f, 0.0f));
+       /* Clamp to zero if values are smaller */
+       color = max(color, make_float3(0.0f, 0.0f, 0.0f));
 
        if(stack_valid(color_out))
-               stack_store_float3(stack, color_out, rgb);
+               stack_store_float3(stack, color_out, color);
 }
 
 CCL_NAMESPACE_END