[Bf-blender-cvs] [bdab538b301] blender-v3.2-release: Fix Eevee blackbody wrong with non-default scene linear color space

Brecht Van Lommel noreply at git.blender.org
Mon May 23 22:22:50 CEST 2022


Commit: bdab538b3019406cfbd53d99bc40c4dbae27393c
Author: Brecht Van Lommel
Date:   Mon May 23 20:54:09 2022 +0200
Branches: blender-v3.2-release
https://developer.blender.org/rBbdab538b3019406cfbd53d99bc40c4dbae27393c

Fix Eevee blackbody wrong with non-default scene linear color space

* Port over new code tables from Cycles
* Convert Rec.709 to scene linear for lookup table.
* Move code for wavelength and blackbody to IMB so they can access the
  required transforms, which are not in blenlib.
* Remove clamping from blackbody shader to bypass the texture read.
  Since it's variable now easiest to just always read from the texture
  than pass additional parameters.
* Fold XYZ to RGB conversion into the wavelength table.

Ref T68926

===================================================================

M	source/blender/blenlib/BLI_math_color.h
M	source/blender/blenlib/intern/math_color.c
M	source/blender/draw/intern/draw_fluid.c
M	source/blender/gpu/shaders/material/gpu_shader_material_blackbody.glsl
M	source/blender/gpu/shaders/material/gpu_shader_material_wavelength.glsl
M	source/blender/imbuf/IMB_colormanagement.h
M	source/blender/imbuf/intern/colormanagement.c
M	source/blender/nodes/shader/nodes/node_shader_blackbody.cc
M	source/blender/nodes/shader/nodes/node_shader_volume_principled.cc
M	source/blender/nodes/shader/nodes/node_shader_wavelength.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h
index dcc00064e47..64b820a22b0 100644
--- a/source/blender/blenlib/BLI_math_color.h
+++ b/source/blender/blenlib/BLI_math_color.h
@@ -186,9 +186,6 @@ MINLINE void rgba_uchar_args_test_set(
     unsigned char col[4], unsigned char r, unsigned char g, unsigned char b, unsigned char a);
 MINLINE void cpack_cpy_3ub(unsigned char r_col[3], unsigned int pack);
 
-void blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max);
-void wavelength_to_xyz_table(float *r_table, int width);
-
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c
index aeeaf47d813..0955f5d1b5a 100644
--- a/source/blender/blenlib/intern/math_color.c
+++ b/source/blender/blenlib/intern/math_color.c
@@ -597,158 +597,3 @@ void BLI_init_srgb_conversion(void)
     BLI_color_to_srgb_table[i] = (unsigned short)(b * 0x100);
   }
 }
-
-/* ****************************** blackbody ******************************** */
-
-/* Calculate color in range 800..12000 using an approximation
- * a/x+bx+c for R and G and ((at + b)t + c)t + d) for B
- * Max absolute error for RGB is (0.00095, 0.00077, 0.00057),
- * which is enough to get the same 8 bit/channel color.
- */
-
-static const float blackbody_table_r[6][3] = {
-    {2.52432244e+03f, -1.06185848e-03f, 3.11067539e+00f},
-    {3.37763626e+03f, -4.34581697e-04f, 1.64843306e+00f},
-    {4.10671449e+03f, -8.61949938e-05f, 6.41423749e-01f},
-    {4.66849800e+03f, 2.85655028e-05f, 1.29075375e-01f},
-    {4.60124770e+03f, 2.89727618e-05f, 1.48001316e-01f},
-    {3.78765709e+03f, 9.36026367e-06f, 3.98995841e-01f},
-};
-
-static const float blackbody_table_g[6][3] = {
-    {-7.50343014e+02f, 3.15679613e-04f, 4.73464526e-01f},
-    {-1.00402363e+03f, 1.29189794e-04f, 9.08181524e-01f},
-    {-1.22075471e+03f, 2.56245413e-05f, 1.20753416e+00f},
-    {-1.42546105e+03f, -4.01730887e-05f, 1.44002695e+00f},
-    {-1.18134453e+03f, -2.18913373e-05f, 1.30656109e+00f},
-    {-5.00279505e+02f, -4.59745390e-06f, 1.09090465e+00f},
-};
-
-static const float blackbody_table_b[6][4] = {
-    {0.0f, 0.0f, 0.0f, 0.0f},
-    {0.0f, 0.0f, 0.0f, 0.0f},
-    {0.0f, 0.0f, 0.0f, 0.0f},
-    {-2.02524603e-11f, 1.79435860e-07f, -2.60561875e-04f, -1.41761141e-02f},
-    {-2.22463426e-13f, -1.55078698e-08f, 3.81675160e-04f, -7.30646033e-01f},
-    {6.72595954e-13f, -2.73059993e-08f, 4.24068546e-04f, -7.52204323e-01f},
-};
-
-static void blackbody_temperature_to_rgb(float rgb[3], float t)
-{
-  if (t >= 12000.0f) {
-    rgb[0] = 0.826270103f;
-    rgb[1] = 0.994478524f;
-    rgb[2] = 1.56626022f;
-  }
-  else if (t < 965.0f) {
-    rgb[0] = 4.70366907f;
-    rgb[1] = 0.0f;
-    rgb[2] = 0.0f;
-  }
-  else {
-    int i = (t >= 6365.0f) ? 5 :
-            (t >= 3315.0f) ? 4 :
-            (t >= 1902.0f) ? 3 :
-            (t >= 1449.0f) ? 2 :
-            (t >= 1167.0f) ? 1 :
-                             0;
-
-    const float *r = blackbody_table_r[i];
-    const float *g = blackbody_table_g[i];
-    const float *b = blackbody_table_b[i];
-
-    const float t_inv = 1.0f / t;
-    rgb[0] = r[0] * t_inv + r[1] * t + r[2];
-    rgb[1] = g[0] * t_inv + g[1] * t + g[2];
-    rgb[2] = ((b[0] * t + b[1]) * t + b[2]) * t + b[3];
-  }
-}
-
-void blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max)
-{
-  for (int i = 0; i < width; i++) {
-    float temperature = min + (max - min) / (float)width * (float)i;
-
-    float rgb[3];
-    blackbody_temperature_to_rgb(rgb, temperature);
-
-    copy_v3_v3(&r_table[i * 4], rgb);
-    r_table[i * 4 + 3] = 0.0f;
-  }
-}
-
-/* ****************************** wavelength ******************************** */
-/* Wavelength to RGB. */
-
-/**
- * CIE color matching functions `xBar`, `yBar`, and `zBar` for
- * wavelengths from 380 through 780 nanometers, every 5 nanometers.
- *
- * For a wavelength lambda in this range:
- * \code{.txt}
- * cie_color_match[(lambda - 380) / 5][0] = xBar
- * cie_color_match[(lambda - 380) / 5][1] = yBar
- * cie_color_match[(lambda - 380) / 5][2] = zBar
- * \endcode
- */
-
-static float cie_colour_match[81][3] = {
-    {0.0014f, 0.0000f, 0.0065f}, {0.0022f, 0.0001f, 0.0105f}, {0.0042f, 0.0001f, 0.0201f},
-    {0.0076f, 0.0002f, 0.0362f}, {0.0143f, 0.0004f, 0.0679f}, {0.0232f, 0.0006f, 0.1102f},
-    {0.0435f, 0.0012f, 0.2074f}, {0.0776f, 0.0022f, 0.3713f}, {0.1344f, 0.0040f, 0.6456f},
-    {0.2148f, 0.0073f, 1.0391f}, {0.2839f, 0.0116f, 1.3856f}, {0.3285f, 0.0168f, 1.6230f},
-    {0.3483f, 0.0230f, 1.7471f}, {0.3481f, 0.0298f, 1.7826f}, {0.3362f, 0.0380f, 1.7721f},
-    {0.3187f, 0.0480f, 1.7441f}, {0.2908f, 0.0600f, 1.6692f}, {0.2511f, 0.0739f, 1.5281f},
-    {0.1954f, 0.0910f, 1.2876f}, {0.1421f, 0.1126f, 1.0419f}, {0.0956f, 0.1390f, 0.8130f},
-    {0.0580f, 0.1693f, 0.6162f}, {0.0320f, 0.2080f, 0.4652f}, {0.0147f, 0.2586f, 0.3533f},
-    {0.0049f, 0.3230f, 0.2720f}, {0.0024f, 0.4073f, 0.2123f}, {0.0093f, 0.5030f, 0.1582f},
-    {0.0291f, 0.6082f, 0.1117f}, {0.0633f, 0.7100f, 0.0782f}, {0.1096f, 0.7932f, 0.0573f},
-    {0.1655f, 0.8620f, 0.0422f}, {0.2257f, 0.9149f, 0.0298f}, {0.2904f, 0.9540f, 0.0203f},
-    {0.3597f, 0.9803f, 0.0134f}, {0.4334f, 0.9950f, 0.0087f}, {0.5121f, 1.0000f, 0.0057f},
-    {0.5945f, 0.9950f, 0.0039f}, {0.6784f, 0.9786f, 0.0027f}, {0.7621f, 0.9520f, 0.0021f},
-    {0.8425f, 0.9154f, 0.0018f}, {0.9163f, 0.8700f, 0.0017f}, {0.9786f, 0.8163f, 0.0014f},
-    {1.0263f, 0.7570f, 0.0011f}, {1.0567f, 0.6949f, 0.0010f}, {1.0622f, 0.6310f, 0.0008f},
-    {1.0456f, 0.5668f, 0.0006f}, {1.0026f, 0.5030f, 0.0003f}, {0.9384f, 0.4412f, 0.0002f},
-    {0.8544f, 0.3810f, 0.0002f}, {0.7514f, 0.3210f, 0.0001f}, {0.6424f, 0.2650f, 0.0000f},
-    {0.5419f, 0.2170f, 0.0000f}, {0.4479f, 0.1750f, 0.0000f}, {0.3608f, 0.1382f, 0.0000f},
-    {0.2835f, 0.1070f, 0.0000f}, {0.2187f, 0.0816f, 0.0000f}, {0.1649f, 0.0610f, 0.0000f},
-    {0.1212f, 0.0446f, 0.0000f}, {0.0874f, 0.0320f, 0.0000f}, {0.0636f, 0.0232f, 0.0000f},
-    {0.0468f, 0.0170f, 0.0000f}, {0.0329f, 0.0119f, 0.0000f}, {0.0227f, 0.0082f, 0.0000f},
-    {0.0158f, 0.0057f, 0.0000f}, {0.0114f, 0.0041f, 0.0000f}, {0.0081f, 0.0029f, 0.0000f},
-    {0.0058f, 0.0021f, 0.0000f}, {0.0041f, 0.0015f, 0.0000f}, {0.0029f, 0.0010f, 0.0000f},
-    {0.0020f, 0.0007f, 0.0000f}, {0.0014f, 0.0005f, 0.0000f}, {0.0010f, 0.0004f, 0.0000f},
-    {0.0007f, 0.0002f, 0.0000f}, {0.0005f, 0.0002f, 0.0000f}, {0.0003f, 0.0001f, 0.0000f},
-    {0.0002f, 0.0001f, 0.0000f}, {0.0002f, 0.0001f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f},
-    {0.0001f, 0.0000f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, {0.0000f, 0.0000f, 0.0000f}};
-
-static void wavelength_to_xyz(float xyz[3], float lambda_nm)
-{
-  float ii = (lambda_nm - 380.0f) * (1.0f / 5.0f); /* Scaled 0..80. */
-  int i = (int)ii;
-
-  if (i < 0 || i >= 80) {
-    xyz[0] = 0.0f;
-    xyz[1] = 0.0f;
-    xyz[2] = 0.0f;
-  }
-  else {
-    ii -= (float)i;
-    const float *c = cie_colour_match[i];
-    xyz[0] = c[0] + ii * (c[3] - c[0]);
-    xyz[1] = c[1] + ii * (c[4] - c[1]);
-    xyz[2] = c[2] + ii * (c[5] - c[2]);
-  }
-}
-
-void wavelength_to_xyz_table(float *r_table, int width)
-{
-  for (int i = 0; i < width; i++) {
-    float temperature = 380 + 400 / (float)width * (float)i;
-
-    float rgb[3];
-    wavelength_to_xyz(rgb, temperature);
-
-    copy_v3_v3(&r_table[i * 4], rgb);
-    r_table[i * 4 + 3] = 0.0f;
-  }
-}
diff --git a/source/blender/draw/intern/draw_fluid.c b/source/blender/draw/intern/draw_fluid.c
index d3d4bbf505e..c34025ebe52 100644
--- a/source/blender/draw/intern/draw_fluid.c
+++ b/source/blender/draw/intern/draw_fluid.c
@@ -20,6 +20,8 @@
 
 #include "BKE_colorband.h"
 
+#include "IMB_colormanagement.h"
+
 #include "GPU_texture.h"
 
 #include "draw_manager.h"
@@ -52,7 +54,7 @@ static void create_flame_spectrum_texture(float *data)
   float *spec_pixels = (float *)MEM_mallocN(TFUNC_WIDTH * 4 * 16 * 16 * sizeof(float),
                                             "spec_pixels");
 
-  blackbody_temperature_to_rgb_table(data, TFUNC_WIDTH, 1500, 3000);
+  IMB_colormanagement_blackbody_temperature_to_rgb_table(data, TFUNC_WIDTH, 1500, 3000);
 
   for (int i = 0; i < 16; i++) {
     for (int j = 0; j < 16; j++) {
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_blackbody.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_blackbody.glsl
index d0111aa3839..e257483f364 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_blackbody.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_blackbody.glsl
@@ -1,13 +1,5 @@
 void node_blackbody(float temperature, sampler1DArray spectrummap, float layer, out vec4 color)
 {
-  if (temperature >= 12000.0) {
-    color = vec4(0.826270103, 0.994478524, 1.56626022, 1.0);
-  }
-  else if (temperature < 965.0) {
-    color = vec4(4.70366907, 0.0, 0.0, 1.0);
-  }
-  else {
-    float t = (temperature - 965.0) / (12000.0 - 965.0);
-    color = vec4(texture(spectrummap, vec2(t, layer)).rgb, 1.0);
-  }
+  float t = (temperature - 800.0) / (12000.0 - 800.0);
+  color = vec4(texture(spectrummap, vec2(t, layer)).rgb, 1.0);
 }
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_wavelength.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_wavelength.glsl
index 2c5d38eabbe..48d627dcd0b 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_wavelength.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_wavelength.glsl
@@ -1,15 +1,7 @@
-void node_wavelength(float wavelength,
-                     sampler1DArray spectrummap,
-                     float layer,
-                     vec3 xyz_to_r,
-                     vec3 xyz_to_g,
-                     vec3 xyz_to_b,
-                     out vec4 color)
+void node_wavelength(float wavelength, sampler1DArray spectrummap, float layer, out vec4 color)
 {
-  mat3 xyz_to_rgb = mat3(xyz_to_r, xyz_to_g, xyz_to_b);
   float t = (wavelength - 380.0) / (780.0 - 380.0);
-  vec3 xyz = texture(spectrummap, vec2(t, layer)).rgb;
-  vec3 rgb = xyz * xyz_to_rgb;
+  vec3 rgb = texture(spe

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list