{
 "id": "spectral-upsampling-sigmoid",
 "title": "Spectral upsampling with sigmoid polynomials",
 "aliases": [],
 "domain": "post",
 "subdomain": "spectral rendering",
 "stage": "post",
 "realtime": "offline",
 "platforms": [
  "cpu",
  "python",
  "cuda"
 ],
 "year_introduced": 2019,
 "inputs": [
  "ldr-image",
  "texture"
 ],
 "outputs": [
  "radiance"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Maps any RGB colour to a smooth reflectance spectrum by fitting a sigmoid of a quadratic polynomial, stored in a 3D coefficient table. Lets spectral renderers accept ordinary RGB textures.",
 "how_it_works": "Reflectance spectra must be smooth and bounded between 0 and 1. Jakob and Hanika model a spectrum as s(lambda) = sigmoid(c0 lambda^2 + c1 lambda + c2). For each RGB colour in the gamut, an offline optimisation finds the three coefficients that make the spectrum, when integrated against the colour matching functions and illuminant, reproduce the colour, and the solution is continuous across the gamut. The coefficients are tabulated on a 3D grid parametrised by the largest channel and the other two, with a nonlinear scale for resolution near the gamut boundary. At render time a texture's RGB is looked up with trilinear interpolation, and a spectrum is evaluated at the sampled wavelength with two multiply-adds and a sigmoid. White maps exactly to a flat spectrum.",
 "when_to_use": "Choose it when a spectral path tracer must ingest RGB assets with plausible metamerism, or when you need round-trip RGB to spectrum to RGB conversions.\n\nPrefer plain RGB rendering when materials are not spectrally sensitive. Prefer measured spectra for scientific accuracy. It pairs with `film-emulation` pipelines that start from spectral data.",
 "cost": {
  "class": "offline",
  "notes": "Table fit is offline, runtime is a table lookup plus a polynomial and sigmoid per wavelength."
 },
 "failure_modes": [
  "Illuminant-dependent: the same RGB reflects differently under unusual lights and the spectra are not unique",
  "Cannot represent spectra with sharp features such as fluorescent or iridescent materials",
  "Table must be fitted per colour space and illuminant",
  "Out-of-gamut colours need clamping"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1111/cgf.13626",
   "type": "paper",
   "title": "A Low‐Dimensional Function Space for Efficient Spectral Upsampling",
   "authors": [
    "Wenzel Jakob",
    "Johannes Hanika"
   ],
   "year": 2019,
   "venue": "Computer Graphics Forum",
   "url": "https://doi.org/10.1111/cgf.13626",
   "ids": {
    "doi": "10.1111/cgf.13626"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13626",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13626",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13626",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13626",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13626",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "jakob2019dimensional"
  },
  {
   "role": "implementation",
   "key": "gh:mitsuba-renderer/rgb2spec",
   "type": "code",
   "title": "mitsuba-renderer/rgb2spec",
   "description": "A Low-Dimensional Function Space for Efficient Spectral Upsampling",
   "authors": [
    "mitsuba-renderer"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/mitsuba-renderer/rgb2spec",
   "ids": {
    "repo": "mitsuba-renderer/rgb2spec"
   },
   "license": "BSD-3-Clause",
   "extra": {
    "stars": 36,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-09-16T16:16:37Z",
    "language": "C",
    "homepage": null,
    "license_name": "BSD 3-Clause \"New\" or \"Revised\" License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/rgb2spec",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mitsubarenderer2018mitsuba"
  },
  {
   "role": "implementation",
   "key": "gh:mitsuba-renderer/mitsuba3",
   "type": "code",
   "title": "mitsuba-renderer/mitsuba3",
   "description": "Mitsuba 3: A Retargetable Forward and Inverse Renderer",
   "authors": [
    "mitsuba-renderer"
   ],
   "year": 2021,
   "venue": "GitHub",
   "url": "https://github.com/mitsuba-renderer/mitsuba3",
   "ids": {
    "repo": "mitsuba-renderer/mitsuba3"
   },
   "license": "NOASSERTION",
   "extra": {
    "stars": 3003,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-28T12:37:12Z",
    "language": "C++",
    "homepage": "https://www.mitsuba-renderer.org/",
    "license_name": "Other"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/mitsuba-renderer/mitsuba3",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "mitsubarenderer2021mitsuba"
  }
 ],
 "presets": [
  {
   "id": "spectral-upsampling-sigmoid-live",
   "title": "Spectral Upsampling with Sigmoid Polynomials — RGB multiply (left) vs Jakob-Hanika quadratic sigmoid spectral bounce & scope (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ILLUM_TEMP": 0.55,
    "P_SATURATION": 0.85,
    "P_SCOPE_GAIN": 0.75,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ILLUM_TEMP = 0.55; // P Illuminant Shift [0.0, 1.0, 0.02]\nconst float P_SATURATION = 0.85; // P Albedo Chroma [0.2, 1.0, 0.02]\nconst float P_SCOPE_GAIN = 0.75; // P Spectrum Scope [0.0, 1.0, 0.05]\nconst float P_SPLIT = 0.48; // P RGB / Spectral [0.0, 1.0, 0.02]\n\n// Jakob & Hanika 2019 sigmoid basis: s(x) = 0.5 + x / (2 * sqrt(1 + x^2))\nfloat sigPoly(float lam, vec3 coeff) {\n  float x = (coeff.x * lam + coeff.y) * lam + coeff.z;\n  return 0.5 + 0.5 * x / sqrt(1.0 + x * x);\n}\n\n// Smooth analytic approximation of CIE 1931 RGB-like spectral sensitivities for lam in [0, 1] (380..720nm)\nvec3 wavelengthToRGB(float lam) {\n  float r = exp(-pow((lam - 0.74) / 0.16, 2.0)) + 0.25 * exp(-pow((lam - 0.14) / 0.09, 2.0));\n  float g = exp(-pow((lam - 0.48) / 0.15, 2.0));\n  float b = exp(-pow((lam - 0.18) / 0.12, 2.0));\n  return vec3(r, g, b);\n}\n\n// Fit quadratic sigmoid coefficients (c0, c1, c2) that reproduce target RGB reflectance smoothly in (0, 1)\nvec3 rgbToSigmoidCoeff(vec3 rgb) {\n  vec3 c = clamp(rgb, 0.02, 0.98);\n  // Map primary balance into peak wavelength & curvature of quadratic polynomial\n  float c0 = -10.5 * (c.g - 0.5 * (c.r + c.b));\n  float c1 = 9.5 * (c.r - c.b) - c0;\n  float meanVal = (c.r + c.g + c.b) * 0.3333;\n  float c2 = (meanVal - 0.5) * 4.2 - 0.35 * c1 - 0.22 * c0;\n  return vec3(c0, c1, c2);\n}\n\n// Illuminant spectral power distribution (sweeping from sodium-amber narrowband to cool sky daylight)\nfloat evalIlluminant(float lam, float temp) {\n  float warm = 1.45 * exp(-pow((lam - 0.68) / 0.22, 2.0)) + 0.35 * exp(-pow((lam - 0.44) / 0.14, 2.0));\n  float cool = 1.35 * exp(-pow((lam - 0.24) / 0.24, 2.0)) + 0.90 * exp(-pow((lam - 0.54) / 0.28, 2.0));\n  return mix(warm, cool, temp);\n}\n\nvec3 integrateSpectralBounce(vec3 alb1, vec3 alb2, float temp) {\n  vec3 k1 = rgbToSigmoidCoeff(alb1);\n  vec3 k2 = rgbToSigmoidCoeff(alb2);\n  vec3 acc = vec3(0.0);\n  vec3 normW = vec3(0.0);\n  for (int i = 0; i < 10; i++) {\n    float lam = (float(i) + 0.5) / 10.0;\n    float s1 = sigPoly(lam, k1);\n    float s2 = sigPoly(lam, k2);\n    float ill = evalIlluminant(lam, temp);\n    vec3 cmf = wavelengthToRGB(lam);\n    acc += cmf * (s1 * s2 * ill);\n    normW += cmf * ill;\n  }\n  return acc / max(normW, vec3(1e-4));\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isSpectral = screenX >= split;\n  float temp = clamp(P_ILLUM_TEMP + 0.28 * sin(iTime * 0.55), 0.0, 1.0);\n  float sat = clamp(P_SATURATION, 0.15, 1.0);\n  float scopeG = clamp(P_SCOPE_GAIN, 0.0, 1.0);\n\n  // Illuminant RGB equivalent for left side naive multiplication\n  vec3 illRGB = mix(vec3(1.18, 0.72, 0.34), vec3(0.54, 0.84, 1.22), temp);\n  vec3 col = mix(vec3(0.05, 0.06, 0.09), vec3(0.11, 0.14, 0.20), smoothstep(-0.4, 0.5, uv.y));\n\n  // Three colorful ceramic spheres demonstrating spectral vs RGB multi-bounce hue preservation\n  vec3 ro = vec3(0.0, 0.28, 2.35);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.04, -1.5));\n  vec3 lDir = normalize(vec3(0.55, 0.78, 0.42));\n\n  vec3 centers[3];\n  vec3 albedos[3];\n  centers[0] = vec3(-0.56, 0.04, 0.0);\n  centers[1] = vec3(0.00, 0.08, 0.05);\n  centers[2] = vec3(0.56, 0.04, 0.0);\n  albedos[0] = mix(vec3(0.6), vec3(0.95, 0.24, 0.36), sat);\n  albedos[1] = mix(vec3(0.6), vec3(0.24, 0.86, 0.44), sat);\n  albedos[2] = mix(vec3(0.6), vec3(0.22, 0.48, 0.96), sat);\n\n  float tMin = 1e5;\n  int hitId = -1;\n  vec3 hp = vec3(0.0);\n  vec3 hn = vec3(0.0, 1.0, 0.0);\n\n  // Ground reflector plane (warm ochre bounce floor)\n  if (rd.y < -0.005) {\n    float tf = (-0.28 - ro.y) / rd.y;\n    if (tf > 0.05 && tf < 12.0) {\n      tMin = tf;\n      hitId = 10;\n      hp = ro + rd * tf;\n      hn = vec3(0.0, 1.0, 0.0);\n    }\n  }\n  for (int i = 0; i < 3; i++) {\n    vec3 oc = ro - centers[i];\n    float b = dot(oc, rd);\n    float d = b * b - (dot(oc, oc) - 0.28 * 0.28);\n    if (d > 0.0) {\n      float ts = -b - sqrt(d);\n      if (ts > 0.05 && ts < tMin) {\n        tMin = ts;\n        hitId = i;\n        hp = ro + rd * ts;\n        hn = normalize(hp - centers[i]);\n      }\n    }\n  }\n\n  vec3 floorAlb = mix(vec3(0.6), vec3(0.92, 0.68, 0.22), sat * 0.85);\n  if (hitId >= 0) {\n    float ndl = max(dot(hn, lDir), 0.0);\n    vec3 surfAlb = (hitId == 10) ? floorAlb : albedos[hitId];\n    vec3 bounceAlb = (hitId == 10) ? albedos[1] : floorAlb;\n    float bounceMask = (hitId == 10) ? smoothstep(0.95, 0.20, length(hp.xz)) : max(-hn.y * 0.85 + 0.25, 0.0);\n\n    vec3 directC;\n    vec3 bounceC;\n    if (isSpectral) {\n      // Spectral integration of sigmoid polynomial reflectance * illuminant\n      directC = integrateSpectralBounce(surfAlb, vec3(0.96), temp) * 1.35;\n      bounceC = integrateSpectralBounce(surfAlb, bounceAlb, temp) * 0.95;\n    } else {\n      // Naive RGB tristimulus componentwise multiplication (loses metamerism & shifts secondary bounce hues)\n      directC = surfAlb * illRGB * 1.15;\n      bounceC = surfAlb * bounceAlb * illRGB * 0.95;\n    }\n    float spec = (hitId == 10) ? 0.0 : pow(max(dot(reflect(-lDir, hn), -rd), 0.0), 36.0) * 0.65;\n    col = directC * (0.22 + 0.95 * ndl) + bounceC * bounceMask * 0.72 + illRGB * spec;\n    if (hitId == 10) col = mix(vec3(0.05, 0.06, 0.09), col, exp(-tMin * 0.14));\n  }\n\n  // Bottom live spectral curve oscilloscope showing s(lambda) = sigmoid(c0*l^2 + c1*l + c2) across 380-720 nm\n  vec2 suv = fragCoord / max(iResolution.xy, vec2(1.0));\n  if (suv.y < 0.24 && scopeG > 0.01) {\n    float lam = clamp((suv.x - 0.06) / 0.88, 0.0, 1.0);\n    float yLocal = clamp((suv.y - 0.03) / 0.18, 0.0, 1.0);\n    vec3 panel = vec3(0.04, 0.05, 0.08);\n    // Faint wavelength rainbow baseline\n    vec3 rainbow = normalize(wavelengthToRGB(lam) + vec3(0.05)) * 0.25;\n    panel += rainbow * smoothstep(0.18, 0.0, yLocal);\n\n    // Plot the three smooth quadratic sigmoid spectra for the three spheres\n    for (int k = 0; k < 3; k++) {\n      float sVal = sigPoly(lam, rgbToSigmoidCoeff(albedos[k]));\n      float line = smoothstep(0.038, 0.008, abs(yLocal - sVal));\n      panel = mix(panel, albedos[k] * 1.45, line);\n    }\n    // Plot current illuminant SPD dashed curve\n    float illV = clamp(evalIlluminant(lam, temp) * 0.55, 0.0, 0.98);\n    float illLine = smoothstep(0.024, 0.006, abs(yLocal - illV)) * step(0.4, fract(suv.x * 48.0));\n    panel = mix(panel, vec3(0.95, 0.96, 1.0), illLine * 0.85);\n    col = mix(col, panel, scopeG * 0.90);\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.98, 1.0), sep);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "spectral-upsampling-sigmoid",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:26.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5729,
      "std": 0.149,
      "min": 0.3505,
      "max": 0.9328
     },
     "image": "/specimens/spectral-upsampling-sigmoid-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "film-emulation",
  "oklab"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "film-emulation",
   "why": "Spectral reconstruction of RGB feeds spectral film models."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/post/spectral-upsampling-sigmoid.json"
 },
 "solves": [
  "spectral renderer can't use my rgb textures",
  "need to turn rgb colours into plausible spectra",
  "rgb to spectrum and back must round trip",
  "metamerism and colour accuracy in a spectral path tracer",
  "smooth reflectance curves from three numbers"
 ],
 "authority": "Jakob & Hanika, 2019",
 "url": "https://texelatlas.vercel.app/t/spectral-upsampling-sigmoid/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 4.77,
  "tier": "light",
  "p50_ms": 0.1569,
  "p95_ms": 0.1625,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "spectral-upsampling-sigmoid-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 4.03,
   "tier": "light",
   "p50_ms": 0.0641,
   "p95_ms": 0.0657,
   "speedup_x": 2.45,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 3.35,
   "tier": "light",
   "p50_ms": 0.0965,
   "p95_ms": 0.0967,
   "speedup_x": 1.63,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}