encodings.glsl 3.1 KB
Newer Older
B
Ben Houston 已提交
1 2
// For a discussion of what this is, please read this: http://lousodrome.net/blog/light/2013/05/26/gamma-correct-and-hdr-rendering-in-a-32-bits-buffer/

B
Ben Houston 已提交
3 4 5
vec4 LinearToLinear( in vec4 value ) {
  return value;
}
6

7 8 9
vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
  return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
}
10
vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
11 12 13
  return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
}

B
Ben Houston 已提交
14
vec4 sRGBToLinear( in vec4 value ) {
15
  return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.w );
B
Ben Houston 已提交
16
}
17
vec4 LinearTosRGB( in vec4 value ) {
18
  return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.w );
19
}
20

B
Ben Houston 已提交
21 22 23
vec4 RGBEToLinear( in vec4 value ) {
  return vec4( value.xyz * exp2( value.w*256.0 - 128.0 ), 1.0 );
}
24 25 26 27 28
vec4 LinearToRGBE( in vec4 value ) {
  float maxComponent = max(max(value.r, value.g), value.b );
  float fExp = ceil( log2(maxComponent) );
  return vec4( value.rgb / exp2(fExp), (fExp + 128.0) / 255.0 );
}
29

30 31 32 33 34 35 36 37 38
// reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
  return vec4( value.xyz * value.w * maxRange, 1.0 );
}
vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
  float maxRGB = max( value.x, max( value.g, value.b ) );
  float M      = maxRGB / maxRange;
  M            = ceil( M * 255.0 ) / 255.0;
  return vec4( value.rgb / ( M * maxRange ), M );
B
Ben Houston 已提交
39
}
40

41 42 43 44 45 46 47
// reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
    return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
}
vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
    float maxRGB = max( value.x, max( value.g, value.b ) );
    float D      = max( maxRange / maxRGB, 1.0 );
48
    D            = min( floor( D ) / 255.0, 1.0 );
49
    return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
B
Ben Houston 已提交
50
}
51

52 53 54
// LogLuv reference: http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html

// M matrix, for encoding
55
const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );
56 57 58 59 60 61 62 63 64
vec4 LinearToLogLuv( in vec4 value )  {
  vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
  Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
  vec4 vResult;
  vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
  float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
  vResult.w = fract(Le);
  vResult.z = (Le - (floor(vResult.w*255.0))/255.0)/255.0;
  return vResult;
B
Ben Houston 已提交
65
}
66

67
// Inverse M matrix, for decoding
68
const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );
69 70 71 72 73 74 75 76
vec4 LogLuvToLinear( in vec4 value ) {
  float Le = value.z * 255.0 + value.w;
  vec3 Xp_Y_XYZp;
  Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
  Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
  Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
  vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
  return vec4( max(vRGB, 0.0), 1.0 );
B
Ben Houston 已提交
77
}