Skip to content

Commit

Permalink
Support WebGL1, rearrange
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Dec 19, 2023
1 parent c5f70be commit bbbd075
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ vec3 ACESFilmicToneMapping( vec3 color ) {
}
// Matrices for rec 2020 <> rec 709 color space conversion
// matrix provided in row-major order so it has been transposed
// https://www.itu.int/pub/R-REP-BT.2407-2017
const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(
vec3( 1.6605, - 0.1246, - 0.0182 ),
vec3( - 0.5876, 1.1329, - 0.1006 ),
vec3( - 0.0728, - 0.0083, 1.1187 )
);
const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3(
vec3( 0.6274, 0.0691, 0.0164 ),
vec3( 0.3293, 0.9195, 0.0880 ),
vec3( 0.0433, 0.0113, 0.8956 )
);
// https://iolite-engine.com/blog_posts/minimal_agx_implementation
//
Expand All @@ -95,36 +109,26 @@ vec3 agxDefaultContrastApprox( vec3 x ) {
// Input and output encoded as Linear-sRGB.
vec3 AgXToneMapping( vec3 color ) {
// Matrices for rec 2020 <> rec 709 color space conversion
// matrix provided in row-major order so it has been transposed
// https://www.itu.int/pub/R-REP-BT.2407-2017
const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3(
vec3( 1.6605, - 0.1246, - 0.0182 ),
vec3( - 0.5876, 1.1329, - 0.1006 ),
vec3( - 0.0728, - 0.0083, 1.1187 )
);
// TODO: add implementation
const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = inverse( LINEAR_REC2020_TO_LINEAR_SRGB );
// AGX Tone Mapping implementation based on Filament, which is in turn based
// on Blender's implementation for rec 2020 colors:
// https://github.com/google/filament/pull/7236
// AgX constants
const mat3 AgXInsetMatrix = mat3(
vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ),
vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ),
vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 )
);
const mat3 AgXOutsetMatrixInv = mat3(
vec3( 0.899796955911611, 0.11142098895748, 0.11142098895748 ),
vec3( 0.0871996192028351, 0.875575586156966, 0.0871996192028349 ),
vec3( 0.013003424885555, 0.0130034248855548, 0.801379391839686 )
// explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatixInv
const mat3 AgXOutsetMatrix = mat3(
vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ),
vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ),
vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 )
);
const mat3 AgXOutsetMatrix = inverse( AgXOutsetMatrixInv );
const float AgxMinEv = - 12.47393; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY)
const float AgxMaxEv = 4.026069; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY)
// AGX Tone Mapping implementation based on Filament, which is in turn based
// on Blender's implementation for rec 2020 colors:
// https://github.com/google/filament/pull/7236
color = LINEAR_SRGB_TO_LINEAR_REC2020 * color;
color = max( vec3( 0.0 ), color );
Expand Down

0 comments on commit bbbd075

Please sign in to comment.