forked from bartvdbraak/blender
Cycles / Code cleanup:
* Move hsv and xyz color functions into the dedicated util files (util_color.h and node_color.h). * svm_lerp moved into util_math.h and renamed to lerp_interp, as it's used for the wavelength node now as well.
This commit is contained in:
parent
cf359f6c7f
commit
0dfc5fc342
@ -58,6 +58,26 @@ color color_unpremultiply(color c, float alpha)
|
||||
|
||||
/* Color Operations */
|
||||
|
||||
color xyY_to_xyz(float x, float y, float Y)
|
||||
{
|
||||
float X, Z;
|
||||
|
||||
if (y != 0.0) X = (x / y) * Y;
|
||||
else X = 0.0;
|
||||
|
||||
if (y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
|
||||
else Z = 0.0;
|
||||
|
||||
return color(X, Y, Z);
|
||||
}
|
||||
|
||||
color xyz_to_rgb(float x, float y, float z)
|
||||
{
|
||||
return color( 3.240479 * x + -1.537150 * y + -0.498535 * z,
|
||||
-0.969256 * x + 1.875991 * y + 0.041556 * z,
|
||||
0.055648 * x + -0.204043 * y + 1.057311 * z);
|
||||
}
|
||||
|
||||
color rgb_to_hsv(color rgb)
|
||||
{
|
||||
float cmax, cmin, h, s, v, cdelta;
|
||||
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "stdosl.h"
|
||||
#include "node_color.h"
|
||||
|
||||
struct KernelSunSky {
|
||||
/* sun direction in spherical and cartesian */
|
||||
@ -28,26 +29,6 @@ struct KernelSunSky {
|
||||
float perez_Y[5], perez_x[5], perez_y[5];
|
||||
};
|
||||
|
||||
color xyY_to_xyz(float x, float y, float Y)
|
||||
{
|
||||
float X, Z;
|
||||
|
||||
if (y != 0.0) X = (x / y) * Y;
|
||||
else X = 0.0;
|
||||
|
||||
if (y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
|
||||
else Z = 0.0;
|
||||
|
||||
return color(X, Y, Z);
|
||||
}
|
||||
|
||||
color xyz_to_rgb(float x, float y, float z)
|
||||
{
|
||||
return color( 3.240479 * x + -1.537150 * y + -0.498535 * z,
|
||||
-0.969256 * x + 1.875991 * y + 0.041556 * z,
|
||||
0.055648 * x + -0.204043 * y + 1.057311 * z);
|
||||
}
|
||||
|
||||
float sky_angle_between(float thetav, float phiv, float theta, float phi)
|
||||
{
|
||||
float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta);
|
||||
|
@ -21,80 +21,6 @@
|
||||
|
||||
CCL_NAMESPACE_BEGIN
|
||||
|
||||
__device float3 rgb_to_hsv(float3 rgb)
|
||||
{
|
||||
float cmax, cmin, h, s, v, cdelta;
|
||||
float3 c;
|
||||
|
||||
cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
|
||||
cmin = min(rgb.x, min(rgb.y, rgb.z));
|
||||
cdelta = cmax - cmin;
|
||||
|
||||
v = cmax;
|
||||
|
||||
if(cmax != 0.0f) {
|
||||
s = cdelta/cmax;
|
||||
}
|
||||
else {
|
||||
s = 0.0f;
|
||||
h = 0.0f;
|
||||
}
|
||||
|
||||
if(s == 0.0f) {
|
||||
h = 0.0f;
|
||||
}
|
||||
else {
|
||||
float3 cmax3 = make_float3(cmax, cmax, cmax);
|
||||
c = (cmax3 - rgb)/cdelta;
|
||||
|
||||
if(rgb.x == cmax) h = c.z - c.y;
|
||||
else if(rgb.y == cmax) h = 2.0f + c.x - c.z;
|
||||
else h = 4.0f + c.y - c.x;
|
||||
|
||||
h /= 6.0f;
|
||||
|
||||
if(h < 0.0f)
|
||||
h += 1.0f;
|
||||
}
|
||||
|
||||
return make_float3(h, s, v);
|
||||
}
|
||||
|
||||
__device float3 hsv_to_rgb(float3 hsv)
|
||||
{
|
||||
float i, f, p, q, t, h, s, v;
|
||||
float3 rgb;
|
||||
|
||||
h = hsv.x;
|
||||
s = hsv.y;
|
||||
v = hsv.z;
|
||||
|
||||
if(s == 0.0f) {
|
||||
rgb = make_float3(v, v, v);
|
||||
}
|
||||
else {
|
||||
if(h == 1.0f)
|
||||
h = 0.0f;
|
||||
|
||||
h *= 6.0f;
|
||||
i = floorf(h);
|
||||
f = h - i;
|
||||
rgb = make_float3(f, f, f);
|
||||
p = v*(1.0f-s);
|
||||
q = v*(1.0f-(s*f));
|
||||
t = v*(1.0f-(s*(1.0f-f)));
|
||||
|
||||
if(i == 0.0f) rgb = make_float3(v, t, p);
|
||||
else if(i == 1.0f) rgb = make_float3(q, v, p);
|
||||
else if(i == 2.0f) rgb = make_float3(p, v, t);
|
||||
else if(i == 3.0f) rgb = make_float3(p, q, v);
|
||||
else if(i == 4.0f) rgb = make_float3(t, p, v);
|
||||
else rgb = make_float3(v, p, q);
|
||||
}
|
||||
|
||||
return rgb;
|
||||
}
|
||||
|
||||
__device void svm_node_hsv(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_color_offset, uint fac_offset, uint out_color_offset, int *offset)
|
||||
{
|
||||
/* read extra data */
|
||||
|
@ -16,28 +16,21 @@
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "svm_hsv.h"
|
||||
|
||||
CCL_NAMESPACE_BEGIN
|
||||
|
||||
__device float3 svm_lerp(const float3 a, const float3 b, float t)
|
||||
{
|
||||
return (a * (1.0f - t) + b * t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_blend(float t, float3 col1, float3 col2)
|
||||
{
|
||||
return svm_lerp(col1, col2, t);
|
||||
return lerp_interp(col1, col2, t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_add(float t, float3 col1, float3 col2)
|
||||
{
|
||||
return svm_lerp(col1, col1 + col2, t);
|
||||
return lerp_interp(col1, col1 + col2, t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_mul(float t, float3 col1, float3 col2)
|
||||
{
|
||||
return svm_lerp(col1, col1 * col2, t);
|
||||
return lerp_interp(col1, col1 * col2, t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_screen(float t, float3 col1, float3 col2)
|
||||
@ -75,7 +68,7 @@ __device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
|
||||
|
||||
__device float3 svm_mix_sub(float t, float3 col1, float3 col2)
|
||||
{
|
||||
return svm_lerp(col1, col1 - col2, t);
|
||||
return lerp_interp(col1, col1 - col2, t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_div(float t, float3 col1, float3 col2)
|
||||
@ -93,7 +86,7 @@ __device float3 svm_mix_div(float t, float3 col1, float3 col2)
|
||||
|
||||
__device float3 svm_mix_diff(float t, float3 col1, float3 col2)
|
||||
{
|
||||
return svm_lerp(col1, fabs(col1 - col2), t);
|
||||
return lerp_interp(col1, fabs(col1 - col2), t);
|
||||
}
|
||||
|
||||
__device float3 svm_mix_dark(float t, float3 col1, float3 col2)
|
||||
@ -191,7 +184,7 @@ __device float3 svm_mix_hue(float t, float3 col1, float3 col2)
|
||||
hsv.x = hsv2.x;
|
||||
float3 tmp = hsv_to_rgb(hsv);
|
||||
|
||||
outcol = svm_lerp(outcol, tmp, t);
|
||||
outcol = lerp_interp(outcol, tmp, t);
|
||||
}
|
||||
|
||||
return outcol;
|
||||
@ -238,7 +231,7 @@ __device float3 svm_mix_color(float t, float3 col1, float3 col2)
|
||||
hsv.y = hsv2.y;
|
||||
float3 tmp = hsv_to_rgb(hsv);
|
||||
|
||||
outcol = svm_lerp(outcol, tmp, t);
|
||||
outcol = lerp_interp(outcol, tmp, t);
|
||||
}
|
||||
|
||||
return outcol;
|
||||
|
@ -18,26 +18,6 @@
|
||||
|
||||
CCL_NAMESPACE_BEGIN
|
||||
|
||||
__device float3 xyY_to_xyz(float x, float y, float Y)
|
||||
{
|
||||
float X, Z;
|
||||
|
||||
if(y != 0.0f) X = (x / y) * Y;
|
||||
else X = 0.0f;
|
||||
|
||||
if(y != 0.0f && Y != 0.0f) Z = (1.0f - x - y) / y * Y;
|
||||
else Z = 0.0f;
|
||||
|
||||
return make_float3(X, Y, Z);
|
||||
}
|
||||
|
||||
__device float3 xyz_to_rgb(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);
|
||||
}
|
||||
|
||||
/*
|
||||
* "A Practical Analytic Model for Daylight"
|
||||
* A. J. Preetham, Peter Shirley, Brian Smits
|
||||
|
@ -34,19 +34,6 @@ 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
|
||||
@ -96,10 +83,10 @@ __device void svm_node_wavelength(ShaderData *sd, float *stack, uint wavelength,
|
||||
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);
|
||||
rgb = lerp_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 = xyz_to_rgb(rgb.x, rgb.y, rgb.z);
|
||||
rgb *= 1.0/2.52; // Empirical scale from lg to make all comps <= 1
|
||||
|
||||
/* Clamp to Zero if values are smaller */
|
||||
|
@ -40,6 +40,100 @@ __device float color_scene_linear_to_srgb(float c)
|
||||
return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
|
||||
}
|
||||
|
||||
__device float3 rgb_to_hsv(float3 rgb)
|
||||
{
|
||||
float cmax, cmin, h, s, v, cdelta;
|
||||
float3 c;
|
||||
|
||||
cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
|
||||
cmin = min(rgb.x, min(rgb.y, rgb.z));
|
||||
cdelta = cmax - cmin;
|
||||
|
||||
v = cmax;
|
||||
|
||||
if(cmax != 0.0f) {
|
||||
s = cdelta/cmax;
|
||||
}
|
||||
else {
|
||||
s = 0.0f;
|
||||
h = 0.0f;
|
||||
}
|
||||
|
||||
if(s == 0.0f) {
|
||||
h = 0.0f;
|
||||
}
|
||||
else {
|
||||
float3 cmax3 = make_float3(cmax, cmax, cmax);
|
||||
c = (cmax3 - rgb)/cdelta;
|
||||
|
||||
if(rgb.x == cmax) h = c.z - c.y;
|
||||
else if(rgb.y == cmax) h = 2.0f + c.x - c.z;
|
||||
else h = 4.0f + c.y - c.x;
|
||||
|
||||
h /= 6.0f;
|
||||
|
||||
if(h < 0.0f)
|
||||
h += 1.0f;
|
||||
}
|
||||
|
||||
return make_float3(h, s, v);
|
||||
}
|
||||
|
||||
__device float3 hsv_to_rgb(float3 hsv)
|
||||
{
|
||||
float i, f, p, q, t, h, s, v;
|
||||
float3 rgb;
|
||||
|
||||
h = hsv.x;
|
||||
s = hsv.y;
|
||||
v = hsv.z;
|
||||
|
||||
if(s == 0.0f) {
|
||||
rgb = make_float3(v, v, v);
|
||||
}
|
||||
else {
|
||||
if(h == 1.0f)
|
||||
h = 0.0f;
|
||||
|
||||
h *= 6.0f;
|
||||
i = floorf(h);
|
||||
f = h - i;
|
||||
rgb = make_float3(f, f, f);
|
||||
p = v*(1.0f-s);
|
||||
q = v*(1.0f-(s*f));
|
||||
t = v*(1.0f-(s*(1.0f-f)));
|
||||
|
||||
if(i == 0.0f) rgb = make_float3(v, t, p);
|
||||
else if(i == 1.0f) rgb = make_float3(q, v, p);
|
||||
else if(i == 2.0f) rgb = make_float3(p, v, t);
|
||||
else if(i == 3.0f) rgb = make_float3(p, q, v);
|
||||
else if(i == 4.0f) rgb = make_float3(t, p, v);
|
||||
else rgb = make_float3(v, p, q);
|
||||
}
|
||||
|
||||
return rgb;
|
||||
}
|
||||
|
||||
__device float3 xyY_to_xyz(float x, float y, float Y)
|
||||
{
|
||||
float X, Z;
|
||||
|
||||
if(y != 0.0f) X = (x / y) * Y;
|
||||
else X = 0.0f;
|
||||
|
||||
if(y != 0.0f && Y != 0.0f) Z = (1.0f - x - y) / y * Y;
|
||||
else Z = 0.0f;
|
||||
|
||||
return make_float3(X, Y, Z);
|
||||
}
|
||||
|
||||
__device float3 xyz_to_rgb(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);
|
||||
}
|
||||
|
||||
#ifndef __KERNEL_OPENCL__
|
||||
|
||||
__device float3 color_srgb_to_scene_linear(float3 c)
|
||||
|
@ -590,6 +590,11 @@ __device_inline float3 interp(float3 a, float3 b, float t)
|
||||
return a + t*(b - a);
|
||||
}
|
||||
|
||||
__device_inline float3 lerp_interp(const float3 a, const float3 b, float t)
|
||||
{
|
||||
return (a * (1.0f - t) + b * t);
|
||||
}
|
||||
|
||||
__device_inline bool is_zero(const float3 a)
|
||||
{
|
||||
#ifdef __KERNEL_SSE__
|
||||
|
Loading…
Reference in New Issue
Block a user