Blender V5.3
kernel/util/colorspace.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8#include "kernel/tables.h"
9
10#include "util/types_spectrum.h"
11
13
15{
16 return make_float3(dot(make_float3(kernel_data.film.xyz_to_r), xyz),
17 dot(make_float3(kernel_data.film.xyz_to_g), xyz),
18 dot(make_float3(kernel_data.film.xyz_to_b), xyz));
19}
20
22{
23 return max(xyz_to_rgb(kg, xyz), zero_float3());
24}
25
27{
28 return (kernel_data.film.is_rec709) ?
29 rec709 :
30 make_float3(dot(make_float3(kernel_data.film.rec709_to_r), rec709),
31 dot(make_float3(kernel_data.film.rec709_to_g), rec709),
32 dot(make_float3(kernel_data.film.rec709_to_b), rec709));
33}
34
35template<class T> ccl_device auto linear_rgb_to_gray(KernelGlobals kg, const T c)
36{
37 return dot(c, make_float3(kernel_data.film.rgb_to_y));
38}
39
41{
42 return rgb;
43}
44
49
54
55#ifdef __SPECTRAL__
56/* Given a wavelength, convert it to rgb in the working color space, assuming D65 illuminant.
57 * [Metameric: Spectral Uplifting via Controllable Color Constraints]
58 * (https://markvanderuit.nl/files/2023-07-23-paper-metameric/metameric-paper.pdf)
59 * by Mark van de Ruit and Elmar Eisemann, Eq. (1). */
60ccl_device_inline float3 wavelength_to_rgb_d65(KernelGlobals kg, const float lambda_um)
61{
62 int i;
63 const float f = floorfrac((lambda_um - WAVELENGTH_CIE_MIN) / WAVELENGTH_DLAMBDA, &i);
64
65 if (i < 0 || i >= WAVELENGTH_RESOLUTION) {
66 return zero_float3();
67 }
68
69 ccl_constant float *c = cie_color_match[i];
70 float3 xyz = mix(make_float3(c[0], c[1], c[2]), make_float3(c[3], c[4], c[5]), f);
71 xyz *= mix(cie_d65_spd[i], cie_d65_spd[i + 1], f);
72
73 return xyz_to_rgb(kg, xyz * CIE_D65_NORMALIZATION);
74}
75
76/* Sample wavelength in um.
77 * See `intern/cycles/app/cie_d65_luminance_fit.py` for the fitting. */
78ccl_device_inline float sample_wavelength(float rand, ccl_private float *prob = nullptr)
79{
80 constexpr float a = 21.71348444564851f;
81 constexpr float x0 = 0.5554867905834258f;
82 constexpr float y0 = 0.021659159132699574f;
83 constexpr float N = 0.9707633294863183f;
84
85 rand = N * rand + y0;
86
87 if (prob) {
88 /* The derivative of F(x) = 1/(1+e^-(a(x-x0))) is F'(x) = a * F(x) * (1 - F(x)). */
89 *prob = a * rand * (1.0f - rand);
90 /* Normalization */
92 }
93
94 const float wavelength = -fast_logf(1.0f / rand - 1.0f) / a + x0;
95 return clamp(wavelength, WAVELENGTH_CIE_MIN, WAVELENGTH_CIE_MAX);
96}
97#endif
98
btScalar max
#define kernel_data
#define ccl_constant
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
ccl_inline_constant float cie_color_match[][3]
#define CIE_D65_NORMALIZATION
ccl_inline_constant float cie_d65_spd[]
#define WAVELENGTH_DLAMBDA
#define WAVELENGTH_CIE_MIN
#define WAVELENGTH_CIE_MAX
#define WAVELENGTH_RESOLUTION
ccl_device float spectrum_to_gray(KernelGlobals kg, Spectrum c)
ccl_device float3 xyz_to_rgb_clamped(KernelGlobals kg, const float3 xyz)
ccl_device_inline float3 spectrum_to_rgb(Spectrum s)
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
ccl_device_inline Spectrum rgb_to_spectrum(const float3 rgb)
ccl_device auto linear_rgb_to_gray(KernelGlobals kg, const T c)
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
Definition math_base.h:464
ccl_device_inline float fast_logf(const float x)
Definition math_fast.h:379
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
#define T
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define mix
#define ccl_device
float3 Spectrum